tomwalters@0
|
1 /*
|
tomwalters@0
|
2 saitonap.c SAI to NAP format conversion.
|
tomwalters@0
|
3 ------------
|
tomwalters@0
|
4
|
tomwalters@0
|
5 Read a SAI header and a succession of frames.
|
tomwalters@0
|
6 Write a NAP header, (created from the SAI header).
|
tomwalters@0
|
7 Write each frame in NAP format.
|
tomwalters@0
|
8
|
tomwalters@0
|
9 */
|
tomwalters@0
|
10
|
tomwalters@0
|
11
|
tomwalters@0
|
12 #include <stdio.h>
|
tomwalters@0
|
13 #include <math.h>
|
tomwalters@0
|
14 #include "header.h"
|
tomwalters@0
|
15 #include "options.h"
|
tomwalters@0
|
16 #include "units.h"
|
tomwalters@0
|
17 #include "strmatch.h"
|
tomwalters@0
|
18
|
tomwalters@0
|
19 char applic[] = "SAI to NAP format conversion. " ;
|
tomwalters@0
|
20
|
tomwalters@0
|
21 static char *helpstr, *debugstr, *fstr, *headerstr ;
|
tomwalters@0
|
22
|
tomwalters@0
|
23 static Options option[] = {
|
tomwalters@0
|
24 { "help" , "off" , &helpstr , "help" , DEBUG },
|
tomwalters@0
|
25 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
|
tomwalters@0
|
26 { "frame" , "min-max" , &fstr , "select frames inclusively" , VAL },
|
tomwalters@0
|
27 { "header" , "on" , &headerstr , "nap header" , VAL },
|
tomwalters@0
|
28 ( char * ) 0 } ;
|
tomwalters@0
|
29
|
tomwalters@0
|
30
|
tomwalters@0
|
31 int frameheight, framewidth ; /* Sai parameters read from header */
|
tomwalters@0
|
32 int frames ;
|
tomwalters@0
|
33
|
tomwalters@0
|
34 main(argc, argv)
|
tomwalters@0
|
35 int argc ;
|
tomwalters@0
|
36 char *argv[] ;
|
tomwalters@0
|
37 {
|
tomwalters@0
|
38 FILE *fp ;
|
tomwalters@0
|
39 int i, framebytes, startbytes, frameshift;
|
tomwalters@0
|
40 char *header, *napheader, *SaiHeader();
|
tomwalters@0
|
41 short *frame ;
|
tomwalters@0
|
42 char *val1, *val2 ;
|
tomwalters@0
|
43 int a,b ;
|
tomwalters@0
|
44
|
tomwalters@0
|
45 fp = openopts( option,argc,argv ) ;
|
tomwalters@0
|
46 if ( !isoff( helpstr ) )
|
tomwalters@0
|
47 helpopts( helpstr, argv[0], applic, option ) ;
|
tomwalters@0
|
48
|
tomwalters@0
|
49 if ( (header = ReadHeader(fp)) == (char *) 0 ) {
|
tomwalters@0
|
50 fprintf(stderr,"saitonap: header not found\n");
|
tomwalters@0
|
51 exit(1);
|
tomwalters@0
|
52 }
|
tomwalters@0
|
53
|
tomwalters@0
|
54 frameheight = HeaderInt( header, "frameheight" );
|
tomwalters@0
|
55 framewidth = HeaderInt( header, "framewidth" );
|
tomwalters@0
|
56 frames = HeaderInt( header, "frames" );
|
tomwalters@0
|
57
|
tomwalters@0
|
58 framebytes = frameheight * framewidth * sizeof(short) ;
|
tomwalters@0
|
59
|
tomwalters@0
|
60
|
tomwalters@0
|
61 /* Get limits on specified number of frames */
|
tomwalters@0
|
62
|
tomwalters@0
|
63 if ( getvals( fstr, &val1, &val2, "-" ) == BADVAL ) {
|
tomwalters@0
|
64 fprintf(stderr,"saitonap: bad frame selector [%s]\n", fstr ) ;
|
tomwalters@0
|
65 exit( 1 ) ;
|
tomwalters@0
|
66 }
|
tomwalters@0
|
67 if ( ismin( val1 ) ) a = 1 ;
|
tomwalters@0
|
68 else if ( ismax( val1 ) ) a = frames ;
|
tomwalters@0
|
69 else a = atoi( val1 ) ;
|
tomwalters@0
|
70
|
tomwalters@0
|
71 if ( isempty( val2 ) ) b = a ;
|
tomwalters@0
|
72 else if ( ismin( val2 ) ) b = 1 ;
|
tomwalters@0
|
73 else if ( ismax( val2 ) ) b = frames ;
|
tomwalters@0
|
74 else b = atoi( val2 ) ;
|
tomwalters@0
|
75
|
tomwalters@0
|
76 if (a<=0 || b>frames || a>b) {
|
tomwalters@0
|
77 fprintf(stderr,"saitonap: bad frame specifier \n" );
|
tomwalters@0
|
78 exit(1);
|
tomwalters@0
|
79 }
|
tomwalters@0
|
80
|
tomwalters@0
|
81
|
tomwalters@0
|
82 /* Allocate space for framebytes of sai data */
|
tomwalters@0
|
83
|
tomwalters@0
|
84 if ( ( frame = (short *)malloc( framebytes )) == NULL ) {
|
tomwalters@0
|
85 fprintf(stderr,"saitonap: malloc out of space\n");
|
tomwalters@0
|
86 exit(1);
|
tomwalters@0
|
87 }
|
tomwalters@0
|
88
|
tomwalters@0
|
89 if ( ison( headerstr ) )
|
tomwalters@0
|
90 napheader = SaiHeader(header) ;
|
tomwalters@0
|
91
|
tomwalters@0
|
92 /* Read and write framebytes of i/p sai data */
|
tomwalters@0
|
93
|
tomwalters@0
|
94 for (i=1 ; i<a && fread( frame,framebytes,1,fp ) ; i++)
|
tomwalters@0
|
95 ;
|
tomwalters@0
|
96
|
tomwalters@0
|
97 if ( i < a ) {
|
tomwalters@0
|
98 fprintf(stderr,"saitonap: insufficient data after header \n");
|
tomwalters@0
|
99 exit(1);
|
tomwalters@0
|
100 }
|
tomwalters@0
|
101
|
tomwalters@0
|
102 if (b > 0)
|
tomwalters@0
|
103 for ( ; i<=b && fread( frame,framebytes,1,fp ) ; i++)
|
tomwalters@0
|
104 writeframe( frame, napheader ) ;
|
tomwalters@0
|
105 else
|
tomwalters@0
|
106 while ( fread( frame,framebytes,1,fp ) )
|
tomwalters@0
|
107 writeframe( frame, napheader ) ;
|
tomwalters@0
|
108
|
tomwalters@0
|
109 fclose(fp);
|
tomwalters@0
|
110 fprintf(stderr,"saitonap done\n" ) ;
|
tomwalters@0
|
111
|
tomwalters@0
|
112 }
|
tomwalters@0
|
113
|
tomwalters@0
|
114
|
tomwalters@0
|
115
|
tomwalters@0
|
116 /* Write a frame in nap format */
|
tomwalters@0
|
117
|
tomwalters@0
|
118 writeframe( frame, header )
|
tomwalters@0
|
119 short *frame ;
|
tomwalters@0
|
120 char *header ;
|
tomwalters@0
|
121 {
|
tomwalters@0
|
122 int i, row, col;
|
tomwalters@0
|
123
|
tomwalters@0
|
124 if ( ison( headerstr ) )
|
tomwalters@0
|
125 WriteHeader( header, stdout ) ;
|
tomwalters@0
|
126 for ( col=0 ; col < framewidth ; col++ )
|
tomwalters@0
|
127 for ( row=0, i=col ; row < frameheight ; row++, i+=framewidth )
|
tomwalters@0
|
128 fwrite( &frame[i], sizeof(short), 1, stdout );
|
tomwalters@0
|
129 }
|
tomwalters@0
|
130
|
tomwalters@0
|
131
|
tomwalters@0
|
132
|
tomwalters@0
|
133 /*
|
tomwalters@0
|
134 Copy the original sai header to a new nap header, changing in order:
|
tomwalters@0
|
135 frames
|
tomwalters@0
|
136 frameshift
|
tomwalters@0
|
137 framewidth
|
tomwalters@0
|
138 frameheight
|
tomwalters@0
|
139 framebytes
|
tomwalters@0
|
140 Then change applic name [gensai] to [gennap] in the Version string.
|
tomwalters@0
|
141 Finally, update the new header_bytes, and return the new header.
|
tomwalters@0
|
142 */
|
tomwalters@0
|
143
|
tomwalters@0
|
144 char *SaiHeader( SAIheader )
|
tomwalters@0
|
145 char *SAIheader ;
|
tomwalters@0
|
146 {
|
tomwalters@0
|
147 char *NAPheader;
|
tomwalters@0
|
148 char *p0, *p1, *p2, *s, str[64];
|
tomwalters@0
|
149
|
tomwalters@0
|
150 NAPheader = (char *)malloc( strlen(SAIheader) + 64 ) ;
|
tomwalters@0
|
151
|
tomwalters@0
|
152 p0 = NAPheader ;
|
tomwalters@0
|
153 p1 = SAIheader ;
|
tomwalters@0
|
154
|
tomwalters@0
|
155
|
tomwalters@0
|
156 /** copy up to frames **/
|
tomwalters@0
|
157
|
tomwalters@0
|
158 p2 = HeaderString( SAIheader , "frames" ) ;
|
tomwalters@0
|
159 while( p1 < p2 )
|
tomwalters@0
|
160 *p0++ = *p1++ ;
|
tomwalters@0
|
161
|
tomwalters@0
|
162 sprintf(str,"%d\n", framewidth);
|
tomwalters@0
|
163 for (s = str ; *s != '\n' ; )
|
tomwalters@0
|
164 *p0++ = *s++;
|
tomwalters@0
|
165 *p0++ = *s;
|
tomwalters@0
|
166 while (*p1 != '\n')
|
tomwalters@0
|
167 *p1++;
|
tomwalters@0
|
168 *p1++;
|
tomwalters@0
|
169
|
tomwalters@0
|
170
|
tomwalters@0
|
171 /** copy up to frameshift **/
|
tomwalters@0
|
172
|
tomwalters@0
|
173 p2 = HeaderString( SAIheader , "frameshift" ) ;
|
tomwalters@0
|
174 while ( p1 < p2 )
|
tomwalters@0
|
175 *p0++ = *p1++ ;
|
tomwalters@0
|
176
|
tomwalters@0
|
177 sprintf(str,"%d\n", 1 );
|
tomwalters@0
|
178 for (s = str ; *s != '\n' ; )
|
tomwalters@0
|
179 *p0++ = *s++;
|
tomwalters@0
|
180 *p0++ = *s;
|
tomwalters@0
|
181 while (*p1 != '\n')
|
tomwalters@0
|
182 *p1++;
|
tomwalters@0
|
183 *p1++;
|
tomwalters@0
|
184
|
tomwalters@0
|
185
|
tomwalters@0
|
186 /** copy up to framewidth **/
|
tomwalters@0
|
187
|
tomwalters@0
|
188 p2 = HeaderString( SAIheader , "framewidth" ) ;
|
tomwalters@0
|
189 while ( p1 < p2 )
|
tomwalters@0
|
190 *p0++ = *p1++ ;
|
tomwalters@0
|
191
|
tomwalters@0
|
192 sprintf(str,"%d\n", 1 );
|
tomwalters@0
|
193 for (s = str ; *s != '\n' ; )
|
tomwalters@0
|
194 *p0++ = *s++;
|
tomwalters@0
|
195 *p0++ = *s;
|
tomwalters@0
|
196 while (*p1 != '\n')
|
tomwalters@0
|
197 *p1++;
|
tomwalters@0
|
198 *p1++;
|
tomwalters@0
|
199
|
tomwalters@0
|
200
|
tomwalters@0
|
201 /** copy up to frameheight **/
|
tomwalters@0
|
202
|
tomwalters@0
|
203 p2 = HeaderString( SAIheader , "frameheight" ) ;
|
tomwalters@0
|
204 while ( p1 < p2 )
|
tomwalters@0
|
205 *p0++ = *p1++ ;
|
tomwalters@0
|
206
|
tomwalters@0
|
207 sprintf(str,"%d\n", frameheight);
|
tomwalters@0
|
208 for (s = str ; *s != '\n' ; )
|
tomwalters@0
|
209 *p0++ = *s++;
|
tomwalters@0
|
210 *p0++ = *s;
|
tomwalters@0
|
211 while (*p1 != '\n')
|
tomwalters@0
|
212 *p1++;
|
tomwalters@0
|
213 *p1++;
|
tomwalters@0
|
214
|
tomwalters@0
|
215
|
tomwalters@0
|
216 /** copy up to framebytes **/
|
tomwalters@0
|
217
|
tomwalters@0
|
218 p2 = HeaderString( SAIheader , "framebytes" ) ;
|
tomwalters@0
|
219 while ( p1 < p2 )
|
tomwalters@0
|
220 *p0++ = *p1++ ;
|
tomwalters@0
|
221
|
tomwalters@0
|
222 sprintf(str,"%d\n", frameheight * sizeof( short ) );
|
tomwalters@0
|
223 for (s = str ; *s != '\n' ; )
|
tomwalters@0
|
224 *p0++ = *s++;
|
tomwalters@0
|
225 *p0++ = *s;
|
tomwalters@0
|
226 while (*p1 != '\n')
|
tomwalters@0
|
227 *p1++;
|
tomwalters@0
|
228 *p1++;
|
tomwalters@0
|
229
|
tomwalters@0
|
230
|
tomwalters@0
|
231 /** copy rest of header **/
|
tomwalters@0
|
232
|
tomwalters@0
|
233 p2 = HeaderString( SAIheader , "Version" ) ;
|
tomwalters@0
|
234 while ( p1 < p2 )
|
tomwalters@0
|
235 *p0++ = *p1++ ;
|
tomwalters@0
|
236
|
tomwalters@0
|
237 while (*p1 != ']') /* change applic name [gensai] to [gennap] */
|
tomwalters@0
|
238 *p0++ = *p1++ ;
|
tomwalters@0
|
239 *(p0-3) = 'n' ;
|
tomwalters@0
|
240 *(p0-2) = 'a' ;
|
tomwalters@0
|
241 *(p0-1) = 'p' ;
|
tomwalters@0
|
242 while (*p1 != '\n')
|
tomwalters@0
|
243 *p0++ = *p1++ ;
|
tomwalters@0
|
244 *p0++ = *p1++ ;
|
tomwalters@0
|
245
|
tomwalters@0
|
246
|
tomwalters@0
|
247 /** update header_bytes **/
|
tomwalters@0
|
248
|
tomwalters@0
|
249 sprintf(str, "%0*d", 7, p0-NAPheader);
|
tomwalters@0
|
250 p0 = HeaderString( NAPheader , "header_bytes" ) ;
|
tomwalters@0
|
251 s = str;
|
tomwalters@0
|
252 while(*p0 != '\n')
|
tomwalters@0
|
253 *p0++ = *s++ ;
|
tomwalters@0
|
254
|
tomwalters@0
|
255
|
tomwalters@0
|
256 return NAPheader;
|
tomwalters@0
|
257 }
|
tomwalters@0
|
258
|