ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
(Generate patch)

Comparing ray/src/rt/data.c (file contents):
Revision 1.10 by greg, Mon Jul 22 13:02:24 1991 UTC vs.
Revision 2.9 by greg, Fri May 13 13:05:13 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "color.h"
16  
17 + #include  "resolu.h"
18 +
19   #include  "data.h"
20  
21 +                                /* picture memory usage before warning */
22 + #ifndef PSIZWARN
23 + #ifdef BIGMEM
24 + #define PSIZWARN        3000000
25 + #else
26 + #define PSIZWARN        1000000
27 + #endif
28 + #endif
29  
30 + #ifndef TABSIZ
31 + #define TABSIZ          97              /* table size (prime) */
32 + #endif
33 +
34 + #define hash(s)         (shash(s)%TABSIZ)
35 +
36 +
37   extern char  *fgetword();
21 extern double  atof();
38  
39 < extern char  *libpath;                  /* library search path */
39 > extern char  *getlibpath();             /* library search path */
40  
41 < static DATARRAY  *dlist = NULL;         /* data array list */
41 > static DATARRAY  *dtab[TABSIZ];         /* data array list */
42  
43 < static DATARRAY  *plist = NULL;         /* picture list */
43 > static DATARRAY  *ptab[TABSIZ];         /* picture list */
44  
45  
46   DATARRAY *
# Line 38 | Line 54 | char  *dname;
54          register int  i, j;
55          register DATARRAY  *dp;
56                                                  /* look for array in list */
57 <        for (dp = dlist; dp != NULL; dp = dp->next)
57 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
58                  if (!strcmp(dname, dp->name))
59                          return(dp);             /* found! */
60  
61          /*
62           *      If we haven't loaded the data already, we will look
63 <         *  for it in the directorys specified by the library path.
63 >         *  for it in the directories specified by the library path.
64           *
65           *      The file has the following format:
66           *
# Line 62 | Line 78 | char  *dname;
78           *              0 0 ni p0i p1i .. pni
79           */
80  
81 <        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
81 >        if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) {
82                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
83                  error(USER, errmsg);
84          }
# Line 126 | Line 142 | char  *dname;
142                  dp->arr[i] = atof(word);
143          }
144          fclose(fp);
145 <        dp->next = dlist;
146 <        return(dlist = dp);
145 >        i = hash(dname);
146 >        dp->next = dtab[i];
147 >        return(dtab[i] = dp);
148  
149   memerr:
150          error(SYSTEM, "out of memory in getdata");
# Line 138 | Line 155 | scanerr:
155   }
156  
157  
141 static double  inpaspect;               /* aspect ratio of input picture */
142
158   static
159 < headaspect(s)                           /* check string for aspect ratio */
159 > headaspect(s, iap)                      /* check string for aspect ratio */
160   char  *s;
161 + double  *iap;
162   {
163          if (isaspect(s))
164 <                inpaspect *= aspectval(s);
164 >                *iap *= aspectval(s);
165   }
166  
167  
# Line 153 | Line 169 | DATARRAY *
169   getpict(pname)                          /* get picture pname */
170   char  *pname;
171   {
172 <        extern char  *libpath;
172 >        double  inpaspect;
173          char  *pfname;
174          FILE  *fp;
175          COLOR  *scanin;
176 <        int  width, height;
177 <        int  x, y;
178 <        register int  i;
176 >        int  sl, ns;
177 >        RESOLU  inpres;
178 >        FLOAT  loc[2];
179 >        int  y;
180 >        register int  x, i;
181          register DATARRAY  *pp;
182                                                  /* look for array in list */
183 <        for (pp = plist; pp != NULL; pp = pp->next)
183 >        for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next)
184                  if (!strcmp(pname, pp->name))
185                          return(pp);             /* found! */
186  
187 <        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
187 >        if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) {
188                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
189                  error(USER, errmsg);
190          }
# Line 181 | Line 199 | char  *pname;
199                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
200                  error(SYSTEM, errmsg);
201          }
202 + #ifdef MSDOS
203 +        setmode(fileno(fp), O_BINARY);
204 + #endif
205                                                  /* get dimensions */
206          inpaspect = 1.0;
207 <        getheader(fp, headaspect);
208 <        if (fgetresolu(&width, &height, fp) != (YMAJOR|YDECR))
207 >        getheader(fp, headaspect, &inpaspect);
208 >        if (!fgetsresolu(&inpres, fp))
209                  goto readerr;
210 + #if PSIZWARN
211 +                                                /* check memory usage */
212 +        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
213 +        if (i > PSIZWARN) {
214 +                sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
215 +                                pname, i);
216 +                error(WARNING, errmsg);
217 +        }
218 + #endif
219          for (i = 0; i < 3; i++) {
220                  pp[i].nd = 2;
221 <                pp[i].dim[0].ne = height;
222 <                pp[i].dim[1].ne = width;
221 >                pp[i].dim[0].ne = inpres.yr;
222 >                pp[i].dim[1].ne = inpres.xr;
223                  pp[i].dim[0].org =
224                  pp[i].dim[1].org = 0.0;
225 <                if (width <= height*inpaspect) {
226 <                        pp[i].dim[0].siz = inpaspect*(double)height/width;
225 >                if (inpres.xr <= inpres.yr*inpaspect) {
226 >                        pp[i].dim[0].siz = inpaspect *
227 >                                                (double)inpres.yr/inpres.xr;
228                          pp[i].dim[1].siz = 1.0;
229                  } else {
230                          pp[i].dim[0].siz = 1.0;
231 <                        pp[i].dim[1].siz = (double)width/height/inpaspect;
231 >                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
232 >                                                inpaspect;
233                  }
234                  pp[i].dim[0].p = pp[i].dim[1].p = NULL;
235 <                pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
235 >                pp[i].arr = (DATATYPE *)
236 >                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
237                  if (pp[i].arr == NULL)
238                          goto memerr;
239          }
240                                                          /* load picture */
241 <        if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
241 >        sl = scanlen(&inpres);
242 >        ns = numscans(&inpres);
243 >        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
244                  goto memerr;
245 <        for (y = height-1; y >= 0; y--) {
246 <                if (freadscan(scanin, width, fp) < 0)
245 >        for (y = 0; y < ns; y++) {
246 >                if (freadscan(scanin, sl, fp) < 0)
247                          goto readerr;
248 <                for (x = 0; x < width; x++)
249 <                        for (i = 0; i < 3; i++)
250 <                                pp[i].arr[y*width+x] = colval(scanin[x],i);
248 >                for (x = 0; x < sl; x++) {
249 >                        pix2loc(loc, &inpres, x, y);
250 >                        i = (int)(loc[1]*inpres.yr)*inpres.xr +
251 >                                        (int)(loc[0]*inpres.xr);
252 >                        pp[0].arr[i] = colval(scanin[x],RED);
253 >                        pp[1].arr[i] = colval(scanin[x],GRN);
254 >                        pp[2].arr[i] = colval(scanin[x],BLU);
255 >                }
256          }
257          free((char *)scanin);
258          fclose(fp);
259 +        i = hash(pname);
260          pp[0].next =
261          pp[1].next =
262 <        pp[2].next = plist;
263 <        return(plist = pp);
262 >        pp[2].next = ptab[i];
263 >        return(ptab[i] = pp);
264  
265   memerr:
266          error(SYSTEM, "out of memory in getpict");
# Line 232 | Line 273 | readerr:
273   freedata(dname)                 /* free memory associated with dname */
274   char  *dname;
275   {
276 +        DATARRAY  head;
277 +        int  hval, nents;
278          register DATARRAY  *dp, *dpl;
279          register int  i;
280  
281 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
282 <                if (!strcmp(dname, dp->name)) {
283 <                        if (dpl == NULL)
284 <                                dlist = dp->next;
285 <                        else
281 >        if (dname == NULL) {                    /* free all if NULL */
282 >                hval = 0; nents = TABSIZ;
283 >        } else {
284 >                hval = hash(dname); nents = 1;
285 >        }
286 >        while (nents--) {
287 >                head.next = dtab[hval];
288 >                dpl = &head;
289 >                while ((dp = dpl->next) != NULL)
290 >                        if (dname == NULL || !strcmp(dname, dp->name)) {
291                                  dpl->next = dp->next;
292 <                        free((char *)dp->arr);
293 <                        for (i = 0; i < dp->nd; i++)
294 <                                if (dp->dim[i].p != NULL)
295 <                                        free((char *)dp->dim[i].p);
296 <                        freestr(dp->name);
297 <                        free((char *)dp);
298 <                        return;
299 <                }
292 >                                free((char *)dp->arr);
293 >                                for (i = 0; i < dp->nd; i++)
294 >                                        if (dp->dim[i].p != NULL)
295 >                                                free((char *)dp->dim[i].p);
296 >                                freestr(dp->name);
297 >                                free((char *)dp);
298 >                        } else
299 >                                dpl = dp;
300 >                dtab[hval++] = head.next;
301 >        }
302   }
303  
304  
305   freepict(pname)                 /* free memory associated with pname */
306   char  *pname;
307   {
308 +        DATARRAY  head;
309 +        int  hval, nents;
310          register DATARRAY  *pp, *ppl;
311  
312 <        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
313 <                if (!strcmp(pname, pp->name)) {
314 <                        if (ppl == NULL)
315 <                                plist = pp->next;
316 <                        else
312 >        if (pname == NULL) {                    /* free all if NULL */
313 >                hval = 0; nents = TABSIZ;
314 >        } else {
315 >                hval = hash(pname); nents = 1;
316 >        }
317 >        while (nents--) {
318 >                head.next = ptab[hval];
319 >                ppl = &head;
320 >                while ((pp = ppl->next) != NULL)
321 >                        if (pname == NULL || !strcmp(pname, pp->name)) {
322                                  ppl->next = pp->next;
323 <                        free((char *)pp[0].arr);
324 <                        free((char *)pp[1].arr);
325 <                        free((char *)pp[2].arr);
326 <                        freestr(pp[0].name);
327 <                        free((char *)pp);
328 <                        return;
329 <                }
323 >                                free((char *)pp[0].arr);
324 >                                free((char *)pp[1].arr);
325 >                                free((char *)pp[2].arr);
326 >                                freestr(pp[0].name);
327 >                                free((char *)pp);
328 >                        } else
329 >                                ppl = pp;
330 >                ptab[hval++] = head.next;
331 >        }
332   }
333  
334  
335   double
336   datavalue(dp, pt)               /* interpolate data value at a point */
337   register DATARRAY  *dp;
338 < double  *pt;
338 > double  *pt;
339   {
340          DATARRAY  sd;
341          int  asize;
342          int  lower, upper;
343          register int  i;
344 <        double  x, y, y0, y1;
344 >        double  x, y0, y1;
345                                          /* set up dimensions for recursion */
346          sd.nd = dp->nd - 1;
347          asize = 1;
# Line 295 | Line 354 | double  *pt;
354                                          /* get independent variable */
355          if (dp->dim[0].p == NULL) {             /* evenly spaced points */
356                  x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
357 <                x = x * (dp->dim[0].ne - 1);
357 >                x *= (double)(dp->dim[0].ne - 1);
358                  i = x;
359                  if (i < 0)
360                          i = 0;
# Line 336 | Line 395 | double  *pt;
395           * taper off harmonically to zero.
396           */
397          if (x > i+2)
398 <                y = (2*y1-y0)/(x-i-1);
340 <        else if (x < i-1)
341 <                y = (2*y0-y1)/(i-x);
342 <        else
343 <                y = y0*((i+1)-x) + y1*(x-i);
398 >                return( (2*y1-y0)/(x-(i-1)) );
399  
400 <        return(y);
400 >        if (x < i-1)
401 >                return( (2*y0-y1)/(i-x) );
402 >
403 >        return( y0*((i+1)-x) + y1*(x-i) );
404   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines