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