--- ray/src/rt/data.c 1995/08/22 08:51:57 2.11 +++ ray/src/rt/data.c 1996/02/15 18:46:54 2.12 @@ -1,4 +1,4 @@ -/* Copyright (c) 1995 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -6,8 +6,6 @@ static char SCCSid[] = "$SunId$ LBL"; /* * data.c - routines dealing with interpolated data. - * - * 6/4/86 */ #include "standard.h" @@ -38,9 +36,7 @@ extern char *getlibpath(); /* library search path */ static DATARRAY *dtab[TABSIZ]; /* data array list */ -static DATARRAY *ptab[TABSIZ]; /* picture list */ - DATARRAY * getdata(dname) /* get data array dname */ char *dname; @@ -79,27 +75,27 @@ char *dname; sprintf(errmsg, "cannot find data file \"%s\"", dname); error(USER, errmsg); } - if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) - goto memerr; - - dp->name = savestr(dname); - if ((fp = fopen(dfname, "r")) == NULL) { sprintf(errmsg, "cannot open data file \"%s\"", dfname); error(SYSTEM, errmsg); } /* get dimensions */ - if (fgetval(fp, 'i', &dp->nd) <= 0) + if (fgetval(fp, 'i', &asize) <= 0) goto scanerr; - if (dp->nd <= 0 || dp->nd > MAXDDIM) { + if (asize <= 0 | asize > MAXDDIM) { sprintf(errmsg, "bad number of dimensions for \"%s\"", dname); error(USER, errmsg); } + if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) + goto memerr; + dp->name = savestr(dname); + dp->type = DATATY; + dp->nd = asize; asize = 1; for (i = 0; i < dp->nd; i++) { - if (fgetval(fp, 'd', &dp->dim[i].org) <= 0) + if (fgetval(fp, DATATY, &dp->dim[i].org) <= 0) goto scanerr; - if (fgetval(fp, 'd', &dp->dim[i].siz) <= 0) + if (fgetval(fp, DATATY, &dp->dim[i].siz) <= 0) goto scanerr; if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0) goto scanerr; @@ -107,11 +103,12 @@ char *dname; goto scanerr; asize *= dp->dim[i].ne; if ((dp->dim[i].siz -= dp->dim[i].org) == 0) { - dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double)); + dp->dim[i].p = (DATATYPE *) + malloc(dp->dim[i].ne*sizeof(DATATYPE)); if (dp->dim[i].p == NULL) goto memerr; for (j = 0; j < dp->dim[i].ne; j++) - if (fgetval(fp, 'd', &dp->dim[i].p[j]) <= 0) + if (fgetval(fp, DATATY, &dp->dim[i].p[j]) <= 0) goto scanerr; for (j = 1; j < dp->dim[i].ne-1; j++) if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) != @@ -123,11 +120,11 @@ char *dname; } else dp->dim[i].p = NULL; } - if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL) + if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL) goto memerr; for (i = 0; i < asize; i++) - if (fgetval(fp, DATATY, &dp->arr[i]) <= 0) + if (fgetval(fp, DATATY, &dp->arr.d[i]) <= 0) goto scanerr; fclose(fp); i = hash(dname); @@ -160,7 +157,7 @@ char *pname; double inpaspect; char *pfname; FILE *fp; - COLOR *scanin; + COLR *scanin; int sl, ns; RESOLU inpres; FLOAT loc[2]; @@ -168,7 +165,7 @@ char *pname; register int x, i; register DATARRAY *pp; /* look for array in list */ - for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next) + for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next) if (!strcmp(pname, pp->name)) return(pp); /* found! */ @@ -176,12 +173,10 @@ char *pname; sprintf(errmsg, "cannot find picture file \"%s\"", pname); error(USER, errmsg); } - if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL) + if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL) goto memerr; - pp[0].name = - pp[1].name = - pp[2].name = savestr(pname); + pp[0].name = savestr(pname); if ((fp = fopen(pfname, "r")) == NULL) { sprintf(errmsg, "cannot open picture file \"%s\"", pfname); @@ -195,60 +190,56 @@ char *pname; getheader(fp, headaspect, &inpaspect); if (!fgetsresolu(&inpres, fp)) goto readerr; + pp[0].nd = 2; + pp[0].dim[0].ne = inpres.yr; + pp[0].dim[1].ne = inpres.xr; + pp[0].dim[0].org = + pp[0].dim[1].org = 0.0; + if (inpres.xr <= inpres.yr*inpaspect) { + pp[0].dim[0].siz = inpaspect * + (double)inpres.yr/inpres.xr; + pp[0].dim[1].siz = 1.0; + } else { + pp[0].dim[0].siz = 1.0; + pp[0].dim[1].siz = (double)inpres.xr/inpres.yr / + inpaspect; + } + pp[0].dim[0].p = pp[0].dim[1].p = NULL; + sl = scanlen(&inpres); /* allocate array */ + ns = numscans(&inpres); + i = ns*sl*sizeof(COLR); #if PSIZWARN - /* check memory usage */ - i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr; - if (i > PSIZWARN) { + if (i > PSIZWARN) { /* memory warning */ sprintf(errmsg, "picture file \"%s\" using %d bytes of memory", pname, i); error(WARNING, errmsg); } #endif - for (i = 0; i < 3; i++) { - pp[i].nd = 2; - 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 (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)inpres.xr/inpres.yr / - inpaspect; - } - pp[i].dim[0].p = pp[i].dim[1].p = NULL; - pp[i].arr = (DATATYPE *) - malloc(inpres.xr*inpres.yr*sizeof(DATATYPE)); - if (pp[i].arr == NULL) - goto memerr; - } + if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL) + goto memerr; /* load picture */ - sl = scanlen(&inpres); - ns = numscans(&inpres); - if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL) + if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL) goto memerr; for (y = 0; y < ns; y++) { - if (freadscan(scanin, sl, fp) < 0) + if (freadcolrs(scanin, sl, fp) < 0) goto readerr; 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); + copycolr(pp[0].arr.c[i], scanin[x]); } } free((char *)scanin); fclose(fp); i = hash(pname); - pp[0].next = - pp[1].next = - pp[2].next = ptab[i]; - return(ptab[i] = pp); + pp[0].next = dtab[i]; /* link into picture list */ + copystruct(&pp[1], &pp[0]); + copystruct(&pp[2], &pp[0]); + pp[0].type = RED; /* differentiate RGB records */ + pp[1].type = GRN; + pp[2].type = BLU; + return(dtab[i] = pp); memerr: error(SYSTEM, "out of memory in getpict"); @@ -277,7 +268,10 @@ char *dname; while ((dp = dpl->next) != NULL) if (dname == NULL || !strcmp(dname, dp->name)) { dpl->next = dp->next; - free((char *)dp->arr); + if (dp->type == DATATY) + free((char *)dp->arr.d); + else + free((char *)dp->arr.c); for (i = 0; i < dp->nd; i++) if (dp->dim[i].p != NULL) free((char *)dp->dim[i].p); @@ -290,36 +284,6 @@ char *dname; } -freepict(pname) /* free memory associated with pname */ -char *pname; -{ - DATARRAY head; - int hval, nents; - register DATARRAY *pp, *ppl; - - if (pname == NULL) { /* free all if NULL */ - hval = 0; nents = TABSIZ; - } else { - hval = hash(pname); nents = 1; - } - while (nents--) { - head.next = ptab[hval]; - 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); - } else - ppl = pp; - ptab[hval++] = head.next; - } -} - - double datavalue(dp, pt) /* interpolate data value at a point */ register DATARRAY *dp; @@ -331,13 +295,17 @@ double *pt; register int i; double x, y0, y1; /* set up dimensions for recursion */ - sd.nd = dp->nd - 1; - asize = 1; - for (i = 0; i < sd.nd; i++) { - sd.dim[i].org = dp->dim[i+1].org; - sd.dim[i].siz = dp->dim[i+1].siz; - sd.dim[i].p = dp->dim[i+1].p; - asize *= sd.dim[i].ne = dp->dim[i+1].ne; + if (dp->nd > 1) { + sd.name = dp->name; + sd.type = dp->type; + sd.nd = dp->nd - 1; + asize = 1; + for (i = 0; i < sd.nd; i++) { + sd.dim[i].org = dp->dim[i+1].org; + sd.dim[i].siz = dp->dim[i+1].siz; + sd.dim[i].p = dp->dim[i+1].p; + asize *= sd.dim[i].ne = dp->dim[i+1].ne; + } } /* get independent variable */ if (dp->dim[0].p == NULL) { /* evenly spaced points */ @@ -369,14 +337,26 @@ double *pt; (dp->dim[0].p[i+1] - dp->dim[0].p[i]); } /* get dependent variable */ - if (dp->nd == 1) { - y0 = dp->arr[i]; - y1 = dp->arr[i+1]; + if (dp->nd > 1) { + if (dp->type == DATATY) { + sd.arr.d = dp->arr.d + i*asize; + y0 = datavalue(&sd, pt+1); + sd.arr.d = dp->arr.d + (i+1)*asize; + y1 = datavalue(&sd, pt+1); + } else { + sd.arr.c = dp->arr.c + i*asize; + y0 = datavalue(&sd, pt+1); + sd.arr.c = dp->arr.c + (i+1)*asize; + y1 = datavalue(&sd, pt+1); + } } else { - sd.arr = &dp->arr[i*asize]; - y0 = datavalue(&sd, pt+1); - sd.arr = &dp->arr[(i+1)*asize]; - y1 = datavalue(&sd, pt+1); + if (dp->type == DATATY) { + y0 = dp->arr.d[i]; + y1 = dp->arr.d[i+1]; + } else { + y0 = colrval(dp->arr.c[i],dp->type); + y1 = colrval(dp->arr.c[i+1],dp->type); + } } /* * Extrapolate as far as one division, then