comparison xaim/cartoon.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 Copyright (c) Applied Psychology Unit, Medical Research Council. 1993
3 ===========================================================================
4
5 Permission to use, copy, modify, and distribute this software without fee
6 is hereby granted for research purposes, provided that this copyright
7 notice appears in all copies and in all supporting documentation, and that
8 the software is not redistributed for any fee (except for a nominal
9 shipping charge). Anyone wanting to incorporate all or part of this
10 software in a commercial product must obtain a license from the Medical
11 Research Council.
12
13 The MRC makes no representations about the suitability of this
14 software for any purpose. It is provided "as is" without express or
15 implied warranty.
16
17 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
18 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
19 THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 SOFTWARE.
24 */
25
26 /*
27 * cartoon.c
28 * ----------
29 *
30 *
31 * M. Akeroyd. July 1993. version 1.10
32 * (ReadHeader is August 30, 1993)
33 *
34 * Revisions: MAA: Christmas 1993.
35 */
36
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include "xreview.h"
42
43
44
45 /* General Parameters */
46
47 extern char progname[MAX_STRING_LENGTH];
48 extern int verboseflag;
49
50 extern char *data_pointer;
51 extern long location[MAX_FRAMES];
52 extern int reviewimage_bitmap_pad;
53 extern int reviewimage_bytesperline;
54
55
56 /* .ctn parameters */
57
58 extern char header[MAX_LINES_HEADER][MAX_LINE_LENGTH];
59 extern int header_lines;
60 extern int no_frames;
61 extern int depth;
62 extern long samplerate;
63 extern int frameheight;
64 extern int framewidth_samples;
65 extern int frameshift_samples;
66 extern double frstep_aid;
67 extern double pwidth;
68 extern double nwidth;
69 extern int mincf;
70 extern int maxcf;
71
72 /* X .... */
73
74 extern int width_ctn;
75 extern int height_ctn;
76 extern int x_ctn;
77 extern int y_ctn;
78
79
80
81 /*---------------------------------------------------------------------------*/
82 /*---------------------------------------------------------------------------*/
83
84
85
86 int readheader(FILE *inputfp, char inputfn[])
87 {
88 /* August 30, 1993
89 * Most of the effort went into getting it to load the exact number of
90 * bytes, because the bitmap info follows on continously from the header.
91 * This version adds-up the number of bytes it actually has loaded, and if
92 * it needs anymore, gratutiously loads them.
93 * It seems to work with:
94 * DECstation cartoon displayed on DECstation server
95 * DECstation ... ... Sun ...
96 * Sun ... ... ... DECstation ...
97 * Sun ... ... ... Sun ...
98 * DECstation ... ... Linux 386 PC server.
99 */
100
101 /* Assumed necessary conditions of a .ctn file:
102 * first line says "header_bytes= ... "
103 * last line says "Version= ..."
104 */
105
106 char tempstring[MAX_STRING_LENGTH];
107 char *p_equal=" "; /* pointer to where the '=' is in the headerline */
108 int bytes_required = 0; /* num of bytes in header, as in "header_bytes=...
109 * This is RETURNed */
110 int bytes_loaded = 0; /* number actually loaded */
111 int counter;
112 int status;
113
114
115 /* Get first line */
116 header_lines = 0;
117 fgets(header[0], MAX_LINE_LENGTH, inputfp);
118
119 /* If the First Line ISN'T "header_bytes ...", add another ".ctn" to the end
120 * and try again. tempstring holds the original name, before ".ctn" got
121 * added.
122 */
123
124 strcpy(tempstring, inputfn);
125
126 if(strspn(header[0], "header_bytes") == 0) {
127 fclose(inputfp);
128 strcat(inputfn, INPUT_EXT);
129 inputfp = fopen(inputfn, "rb");
130 if (inputfp == NULL) {
131 fprintf(stderr, "xreview : unable to open file %s\n", tempstring);
132 exit(-1);}
133 fgets(header[0], MAX_LINE_LENGTH, inputfp);
134 if(strspn(header[0], "header_bytes") == 0) {
135 fprintf(stderr, "xreview: is %s a .ctn file? \n", inputfn);
136 exit(-1);}}
137
138 /* Find out how many bytes there SHOULD be */
139 p_equal = strchr(header[0], '=');
140 bytes_required = atoi(++p_equal);
141
142
143 /*----------------------------------------*/
144
145
146 /* Loop on remaining lines, saving important information as required
147 * Last line has "Version" in it.
148 */
149
150 while (strncmp(fgets(header[++header_lines], MAX_LINE_LENGTH, inputfp),
151 "Version=", 8) != 0) {
152
153 if (strncmp(header[header_lines], "frames=", 7) == 0 ) {
154 p_equal = strchr(header[header_lines], '=');
155 no_frames = atoi(++p_equal); }
156
157 if (strncmp(header[header_lines], "samplerate", 11) == 0 ) {
158 /* For some unknown reason, the samplerate has a "." at the
159 * end of it. Well, sometimes it does.
160 */
161 strncpy(tempstring, header[header_lines],
162 (strlen(header[header_lines])-0));
163 p_equal = strchr(tempstring, '=');
164 samplerate = atol(++p_equal); }
165
166 if (strncmp(header[header_lines], "frameheight=", 12) == 0 ) {
167 p_equal = strchr(header[header_lines], '=');
168 frameheight = atoi(++p_equal); }
169
170 if (strncmp(header[header_lines], "framewidth=", 11) == 0 ) {
171 p_equal = strchr(header[header_lines], '=');
172 framewidth_samples = atoi(++p_equal); }
173
174 if (strncmp(header[header_lines], "frameshift=", 11) == 0 ) {
175 p_equal = strchr(header[header_lines], '=');
176 frameshift_samples = atoi(++p_equal); }
177
178 if (strncmp(header[header_lines], "frstep_aid=", 11) == 0 ) {
179 p_equal = strchr(header[header_lines], '=');
180 frstep_aid = atof(++p_equal);}
181
182 if (strncmp(header[header_lines], "pwidth_aid=", 11) == 0 ) {
183 p_equal = strchr(header[header_lines], '=');
184 pwidth = atof(++p_equal); }
185
186 if (strncmp(header[header_lines], "nwidth_aid=", 11) == 0 ) {
187 p_equal = strchr(header[header_lines], '=');
188 nwidth = atof(++p_equal); }
189
190 if (strncmp(header[header_lines], "mincf_afb=", 10) == 0 ) {
191 p_equal = strchr(header[header_lines], '=');
192 mincf = atoi(++p_equal); }
193
194 if (strncmp(header[header_lines], "maxcf_afb=", 10) == 0 ) {
195 p_equal = strchr(header[header_lines], '=');
196 maxcf = atoi(++p_equal); }
197
198 if (strncmp(header[header_lines], "width_win=", 10) == 0 ) {
199 p_equal = strchr(header[header_lines], '=');
200 width_ctn = atoi(++p_equal); }
201
202 if (strncmp(header[header_lines], "height_win=", 11) == 0 ) {
203 p_equal = strchr(header[header_lines], '=');
204 height_ctn = atoi(++p_equal); }
205
206 if (strncmp(header[header_lines], "x0_win=", 7) == 0 ) {
207 if (strncmp(header[header_lines], "x0_win=center", 13) == 0 ) {
208 /* -1 is used to mean 'center' */
209 x_ctn = -1;}
210 else {
211 p_equal = strchr(header[header_lines], '=');
212 x_ctn = atoi(++p_equal); } }
213
214 if (strncmp(header[header_lines], "y0_win=", 7) == 0 ) {
215 if (strncmp(header[header_lines], "y0_win=center", 13) == 0 ) {
216 /* -1 is used to mean 'center' */
217 y_ctn = -1;}
218 else {
219 p_equal = strchr(header[header_lines], '=');
220 y_ctn = atoi(++p_equal); } }
221
222 }
223
224 /*---------------------------------*/
225
226 /* Some checks */
227
228 if (width_ctn == 0) {
229 if (verboseflag == ON) fprintf(stderr, "\n");
230 fprintf(stderr, "xreview : width of cartoon window = 0. Stopping. \n");
231 exit(-1);}
232
233 if (height_ctn == 0) {
234 if (verboseflag == ON) fprintf(stderr, "\n");
235 fprintf(stderr, "xreview : height of cartoon window = 0. Stopping. \n");
236 exit(-1);}
237
238 if (no_frames == 0) {
239 if (verboseflag == ON) fprintf(stderr, "\n");
240 fprintf(stderr, "xreview : total frames = 0. Stopping. \n");
241 exit(-1);}
242
243 if (frameheight == 0) {
244 if (verboseflag == ON) fprintf(stderr, "\n");
245 fprintf(stderr, "xreview : frameheight of cartoon = 0. Stopping. \n");
246 exit(-1);}
247
248 /*-----------------------------------------------*/
249
250 /* how many bytes have we loaded ? */
251 for (counter=0; counter<=header_lines; counter++)
252 bytes_loaded += strlen(header[counter]);
253
254 /* read some more bytes, till we are at the end of the header */
255 for (counter = 1; counter <= (bytes_required - bytes_loaded); counter++) {
256 status = fgetc(inputfp);
257 if (status == EOF) {
258 if (verboseflag == ON) fprintf(stderr, "\n");
259 fprintf(stderr, "xreview : unable to finish loading of header. \n");
260 exit(-1);}}
261
262 return bytes_required;
263 }
264
265
266
267
268
269 /*------------------------------------------------------------------------*/
270 /*------------------------------------------------------------------------*/
271
272
273
274
275 FILE *open_file(char filefn[], FILE *dir_default, int streamtype)
276 {
277 /* Opens a file stream associated with filefn[].
278 * If that's null, then the stream is a copy of the dir_default:
279 * stdin / stdout / stderr.
280 * 'streamtype' is either READ (for "rb") or WRITE ("wb")
281 *
282 * If it doesn't work, put ".ctn" on the end and try again.
283 */
284
285 FILE *filefp = NULL;
286
287 if (strcmp(filefn, "") == 0)
288 filefp = dir_default;
289 else {
290 if (streamtype == READ)
291 filefp = fopen(filefn, "rb");
292 else
293 filefp = fopen(filefn, "wb");}
294
295 if (filefp == NULL) {
296 strcat(filefn, INPUT_EXT);
297 if (streamtype == READ)
298 filefp = fopen(filefn, "rb");
299 else
300 filefp = fopen(filefn, "wb");
301 if (filefp == NULL) {
302 fprintf(stderr, "xreview : unable to open file %s\n", filefn);
303 exit(-1);}
304 }
305 return filefp;
306
307 }
308
309
310
311
312 /*---------------------------------------------------------------------*/
313 /*---------------------------------------------------------------------*/
314
315
316
317
318
319 void close_files(FILE *fp)
320 {
321
322 fclose(fp);
323
324 if ( ferror(fp) != 0) {
325 if (verboseflag == ON) fprintf(stderr, "\n");
326 fprintf(stderr, "xreview : error closing file.\n");
327 exit(-1);}
328 }
329
330
331
332
333
334 /*---------------------------------------------------------------------*/
335 /*---------------------------------------------------------------------*/
336
337
338
339
340
341 void loaddata(FILE *inputfp)
342 {
343 int required_bytes;
344 int frame;
345 long status;
346
347 /*-----------------------------*/
348
349 if (verboseflag==ON) {
350 fprintf(stderr, " loading frame ");
351 fflush(stderr);}
352
353 required_bytes = (long) height_ctn*reviewimage_bytesperline*depth;
354
355 if (no_frames >= MAX_FRAMES) {
356 if (verboseflag == ON) fprintf(stderr, "\n");
357 fprintf(stderr, "xreview : too many frames (%i) \n", no_frames);
358 exit(-1);}
359
360 /*-----------------------------*/
361
362 for (frame=1; frame <= no_frames; frame++) {
363
364 if (verboseflag==ON) {
365 fprintf(stderr, "%i ", frame);
366 fflush(stderr);}
367
368 location[frame] = (long) data_pointer;
369
370 clearerr(inputfp);
371 status = (long) fread (data_pointer, (size_t) 1, (size_t) required_bytes, inputfp);
372
373 if ((status != required_bytes) && frame == 1){
374 if (verboseflag == ON) fprintf(stderr, "\n");
375 fprintf(stderr, "xreview : error reading first frame: wanted %li bytes, got %li\n", required_bytes, status);
376 exit(-1);}
377
378 if ((status != required_bytes) && frame != 1){
379 if (verboseflag == ON) fprintf(stderr, "\n");
380 fprintf(stderr, "xreview : frame %i: wanted %li bytes, got %li. Continuing with %i frames.\n", frame, required_bytes, status, frame-1);
381 frame--;
382 no_frames=frame;}
383
384 if ((int) ferror(inputfp) != 0) {
385 if (verboseflag == ON) fprintf(stderr, "\n");
386 fprintf(stderr, "xreview : error reading frame %i\n", frame);
387 exit(-1);}
388
389 data_pointer = data_pointer + required_bytes;
390
391 } /* frame */
392
393 /*-----------------------------*/
394
395 data_pointer = (char *) location[1];
396
397 }
398
399
400
401
402
403
404 /* The End */
405 /*---------------------------------------------------------------------*/
406 /*---------------------------------------------------------------------*/