tomwalters@0: /* tomwalters@0: Copyright (c) Applied Psychology Unit, Medical Research Council. 1993 tomwalters@0: =========================================================================== tomwalters@0: tomwalters@0: Permission to use, copy, modify, and distribute this software without fee tomwalters@0: is hereby granted for research purposes, provided that this copyright tomwalters@0: notice appears in all copies and in all supporting documentation, and that tomwalters@0: the software is not redistributed for any fee (except for a nominal tomwalters@0: shipping charge). Anyone wanting to incorporate all or part of this tomwalters@0: software in a commercial product must obtain a license from the Medical tomwalters@0: Research Council. tomwalters@0: tomwalters@0: The MRC makes no representations about the suitability of this tomwalters@0: software for any purpose. It is provided "as is" without express or tomwalters@0: implied warranty. tomwalters@0: tomwalters@0: THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING tomwalters@0: ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL tomwalters@0: THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES tomwalters@0: OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, tomwalters@0: WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, tomwalters@0: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS tomwalters@0: SOFTWARE. tomwalters@0: */ tomwalters@0: tomwalters@0: /* tomwalters@0: * cartoon.c tomwalters@0: * ---------- tomwalters@0: * tomwalters@0: * tomwalters@0: * M. Akeroyd. July 1993. version 1.10 tomwalters@0: * (ReadHeader is August 30, 1993) tomwalters@0: * tomwalters@0: * Revisions: MAA: Christmas 1993. tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "xreview.h" tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* General Parameters */ tomwalters@0: tomwalters@0: extern char progname[MAX_STRING_LENGTH]; tomwalters@0: extern int verboseflag; tomwalters@0: tomwalters@0: extern char *data_pointer; tomwalters@0: extern long location[MAX_FRAMES]; tomwalters@0: extern int reviewimage_bitmap_pad; tomwalters@0: extern int reviewimage_bytesperline; tomwalters@0: tomwalters@0: tomwalters@0: /* .ctn parameters */ tomwalters@0: tomwalters@0: extern char header[MAX_LINES_HEADER][MAX_LINE_LENGTH]; tomwalters@0: extern int header_lines; tomwalters@0: extern int no_frames; tomwalters@0: extern int depth; tomwalters@0: extern long samplerate; tomwalters@0: extern int frameheight; tomwalters@0: extern int framewidth_samples; tomwalters@0: extern int frameshift_samples; tomwalters@0: extern double frstep_aid; tomwalters@0: extern double pwidth; tomwalters@0: extern double nwidth; tomwalters@0: extern int mincf; tomwalters@0: extern int maxcf; tomwalters@0: tomwalters@0: /* X .... */ tomwalters@0: tomwalters@0: extern int width_ctn; tomwalters@0: extern int height_ctn; tomwalters@0: extern int x_ctn; tomwalters@0: extern int y_ctn; tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /*---------------------------------------------------------------------------*/ tomwalters@0: /*---------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: int readheader(FILE *inputfp, char inputfn[]) tomwalters@0: { tomwalters@0: /* August 30, 1993 tomwalters@0: * Most of the effort went into getting it to load the exact number of tomwalters@0: * bytes, because the bitmap info follows on continously from the header. tomwalters@0: * This version adds-up the number of bytes it actually has loaded, and if tomwalters@0: * it needs anymore, gratutiously loads them. tomwalters@0: * It seems to work with: tomwalters@0: * DECstation cartoon displayed on DECstation server tomwalters@0: * DECstation ... ... Sun ... tomwalters@0: * Sun ... ... ... DECstation ... tomwalters@0: * Sun ... ... ... Sun ... tomwalters@0: * DECstation ... ... Linux 386 PC server. tomwalters@0: */ tomwalters@0: tomwalters@0: /* Assumed necessary conditions of a .ctn file: tomwalters@0: * first line says "header_bytes= ... " tomwalters@0: * last line says "Version= ..." tomwalters@0: */ tomwalters@0: tomwalters@0: char tempstring[MAX_STRING_LENGTH]; tomwalters@0: char *p_equal=" "; /* pointer to where the '=' is in the headerline */ tomwalters@0: int bytes_required = 0; /* num of bytes in header, as in "header_bytes=... tomwalters@0: * This is RETURNed */ tomwalters@0: int bytes_loaded = 0; /* number actually loaded */ tomwalters@0: int counter; tomwalters@0: int status; tomwalters@0: tomwalters@0: tomwalters@0: /* Get first line */ tomwalters@0: header_lines = 0; tomwalters@0: fgets(header[0], MAX_LINE_LENGTH, inputfp); tomwalters@0: tomwalters@0: /* If the First Line ISN'T "header_bytes ...", add another ".ctn" to the end tomwalters@0: * and try again. tempstring holds the original name, before ".ctn" got tomwalters@0: * added. tomwalters@0: */ tomwalters@0: tomwalters@0: strcpy(tempstring, inputfn); tomwalters@0: tomwalters@0: if(strspn(header[0], "header_bytes") == 0) { tomwalters@0: fclose(inputfp); tomwalters@0: strcat(inputfn, INPUT_EXT); tomwalters@0: inputfp = fopen(inputfn, "rb"); tomwalters@0: if (inputfp == NULL) { tomwalters@0: fprintf(stderr, "xreview : unable to open file %s\n", tempstring); tomwalters@0: exit(-1);} tomwalters@0: fgets(header[0], MAX_LINE_LENGTH, inputfp); tomwalters@0: if(strspn(header[0], "header_bytes") == 0) { tomwalters@0: fprintf(stderr, "xreview: is %s a .ctn file? \n", inputfn); tomwalters@0: exit(-1);}} tomwalters@0: tomwalters@0: /* Find out how many bytes there SHOULD be */ tomwalters@0: p_equal = strchr(header[0], '='); tomwalters@0: bytes_required = atoi(++p_equal); tomwalters@0: tomwalters@0: tomwalters@0: /*----------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: /* Loop on remaining lines, saving important information as required tomwalters@0: * Last line has "Version" in it. tomwalters@0: */ tomwalters@0: tomwalters@0: while (strncmp(fgets(header[++header_lines], MAX_LINE_LENGTH, inputfp), tomwalters@0: "Version=", 8) != 0) { tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "frames=", 7) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: no_frames = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "samplerate", 11) == 0 ) { tomwalters@0: /* For some unknown reason, the samplerate has a "." at the tomwalters@0: * end of it. Well, sometimes it does. tomwalters@0: */ tomwalters@0: strncpy(tempstring, header[header_lines], tomwalters@0: (strlen(header[header_lines])-0)); tomwalters@0: p_equal = strchr(tempstring, '='); tomwalters@0: samplerate = atol(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "frameheight=", 12) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: frameheight = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "framewidth=", 11) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: framewidth_samples = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "frameshift=", 11) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: frameshift_samples = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "frstep_aid=", 11) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: frstep_aid = atof(++p_equal);} tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "pwidth_aid=", 11) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: pwidth = atof(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "nwidth_aid=", 11) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: nwidth = atof(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "mincf_afb=", 10) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: mincf = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "maxcf_afb=", 10) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: maxcf = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "width_win=", 10) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: width_ctn = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "height_win=", 11) == 0 ) { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: height_ctn = atoi(++p_equal); } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "x0_win=", 7) == 0 ) { tomwalters@0: if (strncmp(header[header_lines], "x0_win=center", 13) == 0 ) { tomwalters@0: /* -1 is used to mean 'center' */ tomwalters@0: x_ctn = -1;} tomwalters@0: else { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: x_ctn = atoi(++p_equal); } } tomwalters@0: tomwalters@0: if (strncmp(header[header_lines], "y0_win=", 7) == 0 ) { tomwalters@0: if (strncmp(header[header_lines], "y0_win=center", 13) == 0 ) { tomwalters@0: /* -1 is used to mean 'center' */ tomwalters@0: y_ctn = -1;} tomwalters@0: else { tomwalters@0: p_equal = strchr(header[header_lines], '='); tomwalters@0: y_ctn = atoi(++p_equal); } } tomwalters@0: tomwalters@0: } tomwalters@0: tomwalters@0: /*---------------------------------*/ tomwalters@0: tomwalters@0: /* Some checks */ tomwalters@0: tomwalters@0: if (width_ctn == 0) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : width of cartoon window = 0. Stopping. \n"); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: if (height_ctn == 0) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : height of cartoon window = 0. Stopping. \n"); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: if (no_frames == 0) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : total frames = 0. Stopping. \n"); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: if (frameheight == 0) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : frameheight of cartoon = 0. Stopping. \n"); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: /*-----------------------------------------------*/ tomwalters@0: tomwalters@0: /* how many bytes have we loaded ? */ tomwalters@0: for (counter=0; counter<=header_lines; counter++) tomwalters@0: bytes_loaded += strlen(header[counter]); tomwalters@0: tomwalters@0: /* read some more bytes, till we are at the end of the header */ tomwalters@0: for (counter = 1; counter <= (bytes_required - bytes_loaded); counter++) { tomwalters@0: status = fgetc(inputfp); tomwalters@0: if (status == EOF) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : unable to finish loading of header. \n"); tomwalters@0: exit(-1);}} tomwalters@0: tomwalters@0: return bytes_required; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /*------------------------------------------------------------------------*/ tomwalters@0: /*------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: FILE *open_file(char filefn[], FILE *dir_default, int streamtype) tomwalters@0: { tomwalters@0: /* Opens a file stream associated with filefn[]. tomwalters@0: * If that's null, then the stream is a copy of the dir_default: tomwalters@0: * stdin / stdout / stderr. tomwalters@0: * 'streamtype' is either READ (for "rb") or WRITE ("wb") tomwalters@0: * tomwalters@0: * If it doesn't work, put ".ctn" on the end and try again. tomwalters@0: */ tomwalters@0: tomwalters@0: FILE *filefp = NULL; tomwalters@0: tomwalters@0: if (strcmp(filefn, "") == 0) tomwalters@0: filefp = dir_default; tomwalters@0: else { tomwalters@0: if (streamtype == READ) tomwalters@0: filefp = fopen(filefn, "rb"); tomwalters@0: else tomwalters@0: filefp = fopen(filefn, "wb");} tomwalters@0: tomwalters@0: if (filefp == NULL) { tomwalters@0: strcat(filefn, INPUT_EXT); tomwalters@0: if (streamtype == READ) tomwalters@0: filefp = fopen(filefn, "rb"); tomwalters@0: else tomwalters@0: filefp = fopen(filefn, "wb"); tomwalters@0: if (filefp == NULL) { tomwalters@0: fprintf(stderr, "xreview : unable to open file %s\n", filefn); tomwalters@0: exit(-1);} tomwalters@0: } tomwalters@0: return filefp; tomwalters@0: tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /*---------------------------------------------------------------------*/ tomwalters@0: /*---------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: void close_files(FILE *fp) tomwalters@0: { tomwalters@0: tomwalters@0: fclose(fp); tomwalters@0: tomwalters@0: if ( ferror(fp) != 0) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : error closing file.\n"); tomwalters@0: exit(-1);} tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /*---------------------------------------------------------------------*/ tomwalters@0: /*---------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: void loaddata(FILE *inputfp) tomwalters@0: { tomwalters@0: int required_bytes; tomwalters@0: int frame; tomwalters@0: long status; tomwalters@0: tomwalters@0: /*-----------------------------*/ tomwalters@0: tomwalters@0: if (verboseflag==ON) { tomwalters@0: fprintf(stderr, " loading frame "); tomwalters@0: fflush(stderr);} tomwalters@0: tomwalters@0: required_bytes = (long) height_ctn*reviewimage_bytesperline*depth; tomwalters@0: tomwalters@0: if (no_frames >= MAX_FRAMES) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : too many frames (%i) \n", no_frames); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: /*-----------------------------*/ tomwalters@0: tomwalters@0: for (frame=1; frame <= no_frames; frame++) { tomwalters@0: tomwalters@0: if (verboseflag==ON) { tomwalters@0: fprintf(stderr, "%i ", frame); tomwalters@0: fflush(stderr);} tomwalters@0: tomwalters@0: location[frame] = (long) data_pointer; tomwalters@0: tomwalters@0: clearerr(inputfp); tomwalters@0: status = (long) fread (data_pointer, (size_t) 1, (size_t) required_bytes, inputfp); tomwalters@0: tomwalters@0: if ((status != required_bytes) && frame == 1){ tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : error reading first frame: wanted %li bytes, got %li\n", required_bytes, status); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: if ((status != required_bytes) && frame != 1){ tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : frame %i: wanted %li bytes, got %li. Continuing with %i frames.\n", frame, required_bytes, status, frame-1); tomwalters@0: frame--; tomwalters@0: no_frames=frame;} tomwalters@0: tomwalters@0: if ((int) ferror(inputfp) != 0) { tomwalters@0: if (verboseflag == ON) fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, "xreview : error reading frame %i\n", frame); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: data_pointer = data_pointer + required_bytes; tomwalters@0: tomwalters@0: } /* frame */ tomwalters@0: tomwalters@0: /*-----------------------------*/ tomwalters@0: tomwalters@0: data_pointer = (char *) location[1]; tomwalters@0: tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* The End */ tomwalters@0: /*---------------------------------------------------------------------*/ tomwalters@0: /*---------------------------------------------------------------------*/