Mercurial > hg > aim92
comparison tools/bufframe.c @ 0:5242703e91d3 tip
Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author | tomwalters |
---|---|
date | Fri, 20 May 2011 15:19:45 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:5242703e91d3 |
---|---|
1 /* | |
2 bufframe.c shifting AIM output frame buffer. | |
3 ---------- | |
4 | |
5 | |
6 | |
7 Examples: | |
8 | |
9 1. To convert gennap output to multiple animated frames: | |
10 | |
11 gennap len=16ms display=off output=stdout file | \ | |
12 bufframe width=8ms frstep=0.2ms anim=on > file.sai | |
13 gensai useprev=on top=1000 cegc -(for landscape plot) | |
14 genspl useprev=on top=1000 pensize=2 cegc -(for spiral plot) | |
15 | |
16 (Note: spirals look better in a square box, so you might use options: | |
17 dencf=1 width=500 height=500 ) | |
18 | |
19 2. To view the basilar membrane from a cross section, animating the waves on it. | |
20 | |
21 genbmm mincf=220 maxcf=660 len=8ms output=stdout display=off file | \ | |
22 bufframe width=1p frstep=1p Tran=on display=on anim=on > foo.sai | |
23 gensai bott=-100 useprev=on mag=.2 foo | |
24 | |
25 or: | |
26 | |
27 genbmm mincf=220 maxcf=660 len=32ms output=stdout display=off file | \ | |
28 bufframe width=1p frstep=1p Tran=on Header=off > foo | |
29 | |
30 x11play -n75 -a500 foo | |
31 | |
32 */ | |
33 | |
34 | |
35 | |
36 #include <stdio.h> | |
37 #include <math.h> | |
38 #include "header.h" | |
39 #include "options.h" | |
40 #include "units.h" | |
41 #include "strmatch.h" | |
42 | |
43 | |
44 char applic[] = "shifting AIM output frame buffer. " ; | |
45 | |
46 | |
47 static char *helpstr, *debugstr, *dispstr, *anistr, *transtr ; | |
48 static char *widthstr, *shiftstr, *headerstr, *framestr ; | |
49 | |
50 static Options option[] = { | |
51 { "help" , "off" , &helpstr , "help" , DEBUG }, | |
52 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG }, | |
53 { "width" , "32ms" , &widthstr , "Width of image frame." , VAL }, | |
54 { "frstep" , "16ms" , &shiftstr , "Step between image frames." , VAL }, | |
55 { "frames" , "min-max" , &framestr , "Select frames inclusively" , VAL }, | |
56 { "Transpose" , "off" , &transtr , "Transpose image frames" , SETFLAG }, | |
57 { "Header" , "on" , &headerstr , "Header (for gensai useprevious=on)." , VAL }, | |
58 { "display" , "on" , &dispstr , "Display output image." , SETFLAG }, | |
59 { "animate" , "off" , &anistr , "Animate cartoon." , SETFLAG }, | |
60 ( char * ) 0 } ; | |
61 | |
62 | |
63 int frameheight, framewidth, frames, framebytes ; | |
64 int Newframeheight, Newframewidth, Newframes, Newframebytes ; | |
65 | |
66 int rows, cols, numframes ; | |
67 int Newrows, Newcols, Newnumframes ; | |
68 | |
69 int samplerate ; | |
70 int frstep ; | |
71 int application ; | |
72 int startbytes ; | |
73 int frshift ; | |
74 int Transpose ; | |
75 | |
76 main(argc, argv) | |
77 int argc ; | |
78 char *argv[] ; | |
79 { | |
80 FILE *fp ; | |
81 char *header, *Header() ; | |
82 short *frame, *endframe ; | |
83 char *val1, *val2 ; | |
84 int i,j,k, a,b ; | |
85 | |
86 fp = openopts( option,argc,argv ) ; | |
87 if ( !isoff( helpstr ) ) | |
88 helpopts( helpstr, argv[0], applic, option ) ; | |
89 | |
90 if ( ( header = ReadHeader( fp ) ) == (char *) 0 ) { | |
91 fprintf( stderr, "edframe: header not found\n" ) ; | |
92 exit( 1 ) ; | |
93 } | |
94 frameheight = HeaderInt( header, "frameheight" ); | |
95 framewidth = HeaderInt( header, "framewidth" ); | |
96 frames = HeaderInt( header, "frames" ); | |
97 samplerate = HeaderSamplerate( header ); | |
98 | |
99 if ( ( application = Applic( header) ) < 0 ) { | |
100 fprintf(stderr,"bufframe: application name not found in header\n" ) ; | |
101 exit( 1 ) ; | |
102 } | |
103 if ( Format( application ) != NAP ) { | |
104 fprintf(stderr,"bufframe: buffer for %s format not implemented\n", gen_applics[application] ) ; | |
105 exit( 1 ) ; | |
106 } | |
107 frame_to_matrix( NAP, &rows, &cols, &numframes, frameheight, framewidth, frames ) ; | |
108 | |
109 Transpose = ison( transtr ) ; | |
110 frstep = to_p( shiftstr, samplerate ) ; | |
111 Newrows = frameheight ; | |
112 Newcols = to_p( widthstr, samplerate ) ; | |
113 Newnumframes = ( cols - Newcols ) / frstep + 1 ; /* since: Newcols + ( Newnumframes - 1 ) * frstep <= cols */ | |
114 | |
115 | |
116 /* Get limits on specified number of frames */ | |
117 | |
118 if ( getvals( framestr, &val1, &val2, "-" ) == BADVAL ) { | |
119 fprintf(stderr,"bufframe: bad frame selector [%s]\n", framestr ) ; | |
120 exit( 1 ) ; | |
121 } | |
122 if ( ismin( val1 ) ) a = 0 ; | |
123 else if ( ismax( val1 ) ) a = Newnumframes - 1 ; | |
124 else if ( Units( val1 ) ) a = (int)to_p( val1, samplerate ) / frstep ; | |
125 else a = atoi( val1 ) ; | |
126 | |
127 if ( isempty( val2 ) ) b = a ; | |
128 else if ( ismin( val2 ) ) b = 0 ; | |
129 else if ( ismax( val2 ) ) b = Newnumframes - 1 ; | |
130 else if ( Units( val2 ) ) b = (int)to_p( val2, samplerate ) / frstep ; | |
131 else b = atoi( val2 ) ; | |
132 | |
133 if (a<0 || b>=Newnumframes || a>b) { | |
134 if ( b>=Newnumframes ) fprintf(stderr,"bufframe: bad frame selector [%d frames needed but %d available]\n", b+1, Newnumframes ); | |
135 else fprintf(stderr,"bufframe: bad frame selector \n" ); | |
136 exit(1); | |
137 } | |
138 | |
139 Newnumframes = b - a + 1 ; | |
140 | |
141 framebytes = Newrows * ( Newcols + ( Newnumframes - 1 ) * frstep ) * sizeof(short) ; | |
142 | |
143 if ( Transpose ) | |
144 matrix_to_frame( SAI, Newcols, Newrows, Newnumframes, &Newframeheight, &Newframewidth, &Newframes ) ; | |
145 else | |
146 matrix_to_frame( SAI, Newrows, Newcols, Newnumframes, &Newframeheight, &Newframewidth, &Newframes ) ; | |
147 | |
148 Newframebytes = Newframeheight * Newframewidth * sizeof(short) ; | |
149 | |
150 if ( ison( headerstr ) ) | |
151 WriteHeader( Header( header ), stdout ) ; | |
152 | |
153 | |
154 /* Allocate space for framebytes of data */ | |
155 | |
156 if ( ( frame = (short *)malloc( framebytes ) ) == NULL ) { | |
157 fprintf( stderr, "bufframe: malloc out of space\n" ) ; | |
158 exit( 1 ) ; | |
159 } | |
160 | |
161 frshift = frstep * rows ; | |
162 | |
163 /* Seek past `a' frames in blocks of framebytes */ | |
164 | |
165 startbytes = a * frshift * sizeof(short) ; | |
166 for (i=framebytes ; i < startbytes ; i += framebytes) | |
167 if ( fread( frame, framebytes, 1, fp ) == NULL ) { | |
168 fprintf(stderr,"bufframe: missing data after header\n"); | |
169 exit(1); | |
170 } | |
171 if ( (startbytes -= (i - framebytes)) > 0 ) | |
172 if ( fread( frame, startbytes, 1, fp ) == NULL ) { | |
173 fprintf(stderr,"bufframe: missing data after header\n"); | |
174 exit(1); | |
175 } | |
176 | |
177 /* Read framebytes of data */ | |
178 | |
179 if ( fread( frame, framebytes, 1, fp ) == NULL ) { | |
180 fprintf(stderr,"bufframe: insufficient data after header\n"); | |
181 exit(1); | |
182 } | |
183 fclose( fp ) ; | |
184 | |
185 endframe = frame + Newframes * frshift ; | |
186 | |
187 fprintf(stderr,"bufframe: %d frames [height=%d width=%d]\n", Newnumframes, Newframeheight, Newframewidth ); | |
188 | |
189 if ( ison( debugstr ) ) { | |
190 fprintf( stderr," frameheight=%3d framewidth=%3d frames=%3d framebytes=%d\n", frameheight, framewidth, frames, framebytes ) ; | |
191 fprintf( stderr,"Newframeheight=%3d Newframewidth=%3d Newframes=%3d Newframebytes=%d\n", Newframeheight, Newframewidth, Newframes, Newframebytes ) ; | |
192 fprintf( stderr," rows=%3d cols=%3d numframes=%d\n", rows, cols, numframes ) ; | |
193 fprintf( stderr," Newrows=%3d Newcols=%3d Newnumframes=%d\n", Newrows, Newcols, Newnumframes ) ; | |
194 fprintf( stderr,"frstep=%d startbytes=%d frshift=%d\n", frstep, startbytes, frshift ) ; | |
195 exit( 1 ) ; | |
196 } | |
197 | |
198 if ( Transpose ) { | |
199 | |
200 for ( ; frame < endframe ; frame += frshift ) | |
201 for ( j=0 ; j < Newcols ; j++ ) | |
202 fwrite( frame + j*frameheight, sizeof(short), Newrows, stdout ) ; | |
203 } | |
204 | |
205 else { | |
206 for ( ; frame < endframe ; frame += frshift ) | |
207 for (j=0 ; j < Newrows ; j++) | |
208 for (k=0 , i=j ; k < Newcols ; k++, i+=frameheight) | |
209 fwrite( &frame[i], sizeof(short), 1, stdout ); | |
210 } | |
211 fprintf(stderr,"bufframe: done\n" ); | |
212 } | |
213 | |
214 | |
215 | |
216 /* Return 1 if the str has appended units. Otherwise return 0. */ | |
217 /* (A freqency specifier with no units is interpreted as a channel number) */ | |
218 | |
219 Units(str) | |
220 char *str ; | |
221 { | |
222 char *eptr = str + strlen( str ) ; | |
223 | |
224 if( isdigit( *--eptr ) ) return 0 ; /* last char is digit, so no units */ | |
225 else return 1 ; | |
226 } | |
227 | |
228 | |
229 /* | |
230 Copy the original (NAP format) header to a new sai header, changing in order: | |
231 frstep | |
232 frames | |
233 framewidth | |
234 frameheight | |
235 framebytes | |
236 animate | |
237 display | |
238 Copy application name "sai" into the version string. | |
239 Finally, update the new header_bytes, and return the new header. | |
240 */ | |
241 | |
242 char *Header( oldheader ) | |
243 char *oldheader ; | |
244 { | |
245 char *saiheader; | |
246 char *p0, *p1, *p2, *s, str[64]; | |
247 | |
248 saiheader = (char *)malloc( strlen(oldheader) + 256 ) ; | |
249 | |
250 p0 = saiheader ; | |
251 p1 = oldheader ; | |
252 | |
253 /* copy to first item after the header_bytes */ | |
254 | |
255 p2 = HeaderString( oldheader , "header_bytes" ) ; | |
256 while( p1 < p2 ) | |
257 *p0++ = *p1++ ; | |
258 while (*p1 != '\n') | |
259 *p0++ = *p1++ ; | |
260 *p0++ = *p1++; | |
261 | |
262 /* insert frstep_aid at top of new header */ | |
263 | |
264 sprintf( str,"frstep_aid=%s\n", shiftstr ) ; | |
265 for (s = str ; *s != '\n' ; ) | |
266 *p0++ = *s++; | |
267 *p0++ = *s; | |
268 | |
269 | |
270 /** copy up to channels_afb (if transpose changes apparent number of channels) **/ | |
271 | |
272 if ( Transpose ) { | |
273 p2 = HeaderString( oldheader , "channels_afb" ) ; | |
274 while( p1 < p2 ) | |
275 *p0++ = *p1++ ; | |
276 | |
277 sprintf(str,"%d\n", Newframeheight); | |
278 for (s = str ; *s != '\n' ; ) | |
279 *p0++ = *s++; | |
280 *p0++ = *s; | |
281 while (*p1 != '\n') | |
282 *p1++; | |
283 *p1++; | |
284 } | |
285 | |
286 | |
287 /** copy up to frames **/ | |
288 | |
289 p2 = HeaderString( oldheader , "frames" ) ; | |
290 while( p1 < p2 ) | |
291 *p0++ = *p1++ ; | |
292 | |
293 sprintf(str,"%d\n", Newframes); | |
294 for (s = str ; *s != '\n' ; ) | |
295 *p0++ = *s++; | |
296 *p0++ = *s; | |
297 while (*p1 != '\n') | |
298 *p1++; | |
299 *p1++; | |
300 | |
301 | |
302 /** copy up to framewidth **/ | |
303 | |
304 p2 = HeaderString( oldheader , "framewidth" ) ; | |
305 while ( p1 < p2 ) | |
306 *p0++ = *p1++ ; | |
307 | |
308 sprintf(str,"%d\n", Newframewidth); | |
309 for (s = str ; *s != '\n' ; ) | |
310 *p0++ = *s++; | |
311 *p0++ = *s; | |
312 while (*p1 != '\n') | |
313 *p1++; | |
314 *p1++; | |
315 | |
316 | |
317 /** copy up to frameheight **/ | |
318 | |
319 p2 = HeaderString( oldheader , "frameheight" ) ; | |
320 while ( p1 < p2 ) | |
321 *p0++ = *p1++ ; | |
322 | |
323 sprintf(str,"%d\n", Newframeheight); | |
324 for (s = str ; *s != '\n' ; ) | |
325 *p0++ = *s++; | |
326 *p0++ = *s; | |
327 while (*p1 != '\n') | |
328 *p1++; | |
329 *p1++; | |
330 | |
331 | |
332 /** copy up to framebytes **/ | |
333 | |
334 p2 = HeaderString( oldheader , "framebytes" ) ; | |
335 while ( p1 < p2 ) | |
336 *p0++ = *p1++ ; | |
337 | |
338 sprintf(str,"%d\n", Newframebytes); | |
339 for (s = str ; *s != '\n' ; ) | |
340 *p0++ = *s++; | |
341 *p0++ = *s; | |
342 while (*p1 != '\n') | |
343 *p1++; | |
344 *p1++; | |
345 | |
346 | |
347 /** copy up to animate_ctn **/ | |
348 | |
349 p2 = HeaderString( oldheader , "animate_ctn" ) ; | |
350 while ( p1 < p2 ) | |
351 *p0++ = *p1++ ; | |
352 | |
353 sprintf(str,"%s\n", anistr ); | |
354 for (s = str ; *s != '\n' ; ) | |
355 *p0++ = *s++; | |
356 *p0++ = *s; | |
357 while (*p1 != '\n') | |
358 *p1++; | |
359 *p1++; | |
360 | |
361 | |
362 /** copy up to display **/ | |
363 | |
364 p2 = HeaderString( oldheader , "display" ) ; | |
365 while ( p1 < p2 ) | |
366 *p0++ = *p1++ ; | |
367 | |
368 sprintf(str,"%s\n", dispstr ); | |
369 for (s = str ; *s != '\n' ; ) | |
370 *p0++ = *s++; | |
371 *p0++ = *s; | |
372 while (*p1 != '\n') | |
373 *p1++; | |
374 *p1++; | |
375 | |
376 | |
377 /** copy rest of header **/ | |
378 | |
379 p2 = HeaderString( oldheader , "Version" ) ; | |
380 while ( p1 < p2 ) | |
381 *p0++ = *p1++ ; | |
382 | |
383 /** ( copy application name "sai" into header ) **/ | |
384 | |
385 if ( ( p2 = ApplicString( oldheader ) ) == (char *)0 ) { | |
386 fprintf(stderr,"bufframe: application name not found in header\n"); | |
387 exit( 1 ) ; | |
388 } | |
389 while ( p1 < p2 ) | |
390 *p0++ = *p1++ ; | |
391 | |
392 *p0++ = 's' ; *p0++ = 'a' ; *p0++ = 'i' ; | |
393 p1 += 3 ; | |
394 | |
395 while (*p1 != '\n') | |
396 *p0++ = *p1++ ; | |
397 *p0++ = *p1++ ; | |
398 | |
399 | |
400 /** update header_bytes **/ | |
401 | |
402 sprintf(str, "%0*d", 7, p0-saiheader); | |
403 p0 = HeaderString( saiheader , "header_bytes" ) ; | |
404 s = str; | |
405 while(*p0 != '\n') | |
406 *p0++ = *s++ ; | |
407 | |
408 | |
409 return saiheader; | |
410 } | |
411 | |
412 | |
413 | |
414 |