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