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