--- ray/src/rt/data.c 1991/07/22 13:02:24 1.10 +++ ray/src/rt/data.c 1992/11/22 10:02:57 2.5 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -14,17 +14,18 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" +#include "resolu.h" + #include "data.h" extern char *fgetword(); -extern double atof(); extern char *libpath; /* library search path */ -static DATARRAY *dlist = NULL; /* data array list */ +static DATARRAY *dlist = NULL; /* data array list */ -static DATARRAY *plist = NULL; /* picture list */ +static DATARRAY *plist = NULL; /* picture list */ DATARRAY * @@ -44,7 +45,7 @@ char *dname; /* * If we haven't loaded the data already, we will look - * for it in the directorys specified by the library path. + * for it in the directories specified by the library path. * * The file has the following format: * @@ -138,14 +139,13 @@ scanerr: } -static double inpaspect; /* aspect ratio of input picture */ - static -headaspect(s) /* check string for aspect ratio */ +headaspect(s, iap) /* check string for aspect ratio */ char *s; +double *iap; { if (isaspect(s)) - inpaspect *= aspectval(s); + *iap *= aspectval(s); } @@ -154,12 +154,15 @@ getpict(pname) /* get picture pname */ char *pname; { extern char *libpath; + double inpaspect; char *pfname; FILE *fp; COLOR *scanin; - int width, height; - int x, y; - register int i; + int sl, ns; + RESOLU inpres; + FLOAT loc[2]; + int y; + register int x, i; register DATARRAY *pp; /* look for array in list */ for (pp = plist; pp != NULL; pp = pp->next) @@ -181,38 +184,51 @@ char *pname; sprintf(errmsg, "cannot open picture file \"%s\"", pfname); error(SYSTEM, errmsg); } +#ifdef MSDOS + setmode(fileno(fp), O_BINARY); +#endif /* get dimensions */ inpaspect = 1.0; - getheader(fp, headaspect); - if (fgetresolu(&width, &height, fp) != (YMAJOR|YDECR)) + getheader(fp, headaspect, &inpaspect); + if (!fgetsresolu(&inpres, fp)) goto readerr; for (i = 0; i < 3; i++) { pp[i].nd = 2; - pp[i].dim[0].ne = height; - pp[i].dim[1].ne = width; + pp[i].dim[0].ne = inpres.yr; + pp[i].dim[1].ne = inpres.xr; pp[i].dim[0].org = pp[i].dim[1].org = 0.0; - if (width <= height*inpaspect) { - pp[i].dim[0].siz = inpaspect*(double)height/width; + if (inpres.xr <= inpres.yr*inpaspect) { + pp[i].dim[0].siz = inpaspect * + (double)inpres.yr/inpres.xr; pp[i].dim[1].siz = 1.0; } else { pp[i].dim[0].siz = 1.0; - pp[i].dim[1].siz = (double)width/height/inpaspect; + pp[i].dim[1].siz = (double)inpres.xr/inpres.yr / + inpaspect; } pp[i].dim[0].p = pp[i].dim[1].p = NULL; - pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE)); + pp[i].arr = (DATATYPE *) + malloc(inpres.xr*inpres.yr*sizeof(DATATYPE)); if (pp[i].arr == NULL) goto memerr; } /* load picture */ - if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL) + sl = scanlen(&inpres); + ns = numscans(&inpres); + if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL) goto memerr; - for (y = height-1; y >= 0; y--) { - if (freadscan(scanin, width, fp) < 0) + for (y = 0; y < ns; y++) { + if (freadscan(scanin, sl, fp) < 0) goto readerr; - for (x = 0; x < width; x++) - for (i = 0; i < 3; i++) - pp[i].arr[y*width+x] = colval(scanin[x],i); + for (x = 0; x < sl; x++) { + pix2loc(loc, &inpres, x, y); + i = (int)(loc[1]*inpres.yr)*inpres.xr + + (int)(loc[0]*inpres.xr); + pp[0].arr[i] = colval(scanin[x],RED); + pp[1].arr[i] = colval(scanin[x],GRN); + pp[2].arr[i] = colval(scanin[x],BLU); + } } free((char *)scanin); fclose(fp); @@ -232,57 +248,59 @@ readerr: freedata(dname) /* free memory associated with dname */ char *dname; { + DATARRAY head; register DATARRAY *dp, *dpl; register int i; - for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next) - if (!strcmp(dname, dp->name)) { - if (dpl == NULL) - dlist = dp->next; - else - dpl->next = dp->next; + head.next = dlist; + dpl = &head; + while ((dp = dpl->next) != NULL) + if (dname == NULL || !strcmp(dname, dp->name)) { + dpl->next = dp->next; free((char *)dp->arr); for (i = 0; i < dp->nd; i++) if (dp->dim[i].p != NULL) free((char *)dp->dim[i].p); freestr(dp->name); free((char *)dp); - return; - } + } else + dpl = dp; + dlist = head.next; } freepict(pname) /* free memory associated with pname */ char *pname; { + DATARRAY head; register DATARRAY *pp, *ppl; - for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next) - if (!strcmp(pname, pp->name)) { - if (ppl == NULL) - plist = pp->next; - else - ppl->next = pp->next; + head.next = plist; + ppl = &head; + while ((pp = ppl->next) != NULL) + if (pname == NULL || !strcmp(pname, pp->name)) { + ppl->next = pp->next; free((char *)pp[0].arr); free((char *)pp[1].arr); free((char *)pp[2].arr); freestr(pp[0].name); free((char *)pp); - return; - } + } else + ppl = pp; + plist = head.next; } double datavalue(dp, pt) /* interpolate data value at a point */ register DATARRAY *dp; -double *pt; +double *pt; { DATARRAY sd; int asize; int lower, upper; register int i; - double x, y, y0, y1; + double x, y, y0, y1; /* set up dimensions for recursion */ sd.nd = dp->nd - 1; asize = 1;