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.6 by greg, Fri Apr 6 14:12:21 1990 UTC vs.
Revision 2.7 by greg, Mon Feb 8 13:12:28 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 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();
38 +
39   extern char  *libpath;                  /* 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 *
47   getdata(dname)                          /* get data array dname */
48   char  *dname;
49   {
50 +        char  word[64];
51          char  *dfname;
52          FILE  *fp;
53          int  asize;
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 72 | Line 92 | char  *dname;
92                  error(SYSTEM, errmsg);
93          }
94                                                          /* get dimensions */
95 <        if (fscanf(fp, "%d", &dp->nd) != 1)
95 >        if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
96                  goto scanerr;
97 <        if (dp->nd <= 0 || dp->nd > MAXDIM) {
97 >        dp->nd = atoi(word);
98 >        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
99                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
100                  error(USER, errmsg);
101          }
102          asize = 1;
103          for (i = 0; i < dp->nd; i++) {
104 <                if (fscanf(fp, "%lf %lf %d",
84 <                                &dp->dim[i].org, &dp->dim[i].siz,
85 <                                &dp->dim[i].ne) != 3)
104 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
105                          goto scanerr;
106 +                dp->dim[i].org = atof(word);
107 +                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
108 +                        goto scanerr;
109 +                dp->dim[i].siz = atof(word);
110 +                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
111 +                        goto scanerr;
112 +                dp->dim[i].ne = atoi(word);
113                  if (dp->dim[i].ne < 2)
114                          goto scanerr;
115                  asize *= dp->dim[i].ne;
# Line 91 | Line 117 | char  *dname;
117                          dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
118                          if (dp->dim[i].p == NULL)
119                                  goto memerr;
120 <                        for (j = 0; j < dp->dim[i].ne; j++)
121 <                                if (fscanf(fp, "%lf", &dp->dim[i].p[j]) != 1)
120 >                        for (j = 0; j < dp->dim[i].ne; j++) {
121 >                                if (fgetword(word, sizeof(word), fp) == NULL ||
122 >                                                !isflt(word))
123                                          goto scanerr;
124 +                                dp->dim[i].p[j] = atof(word);
125 +                        }
126                          for (j = 1; j < dp->dim[i].ne-1; j++)
127                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
128                                          (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
# Line 107 | Line 136 | char  *dname;
136          if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
137                  goto memerr;
138          
139 <        for (i = 0; i < asize; i++)
140 <                if (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
139 >        for (i = 0; i < asize; i++) {
140 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
141                          goto scanerr;
142 <        
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 124 | Line 155 | scanerr:
155   }
156  
157  
127 static double  inpaspect;               /* aspect ratio of input picture */
128
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 140 | Line 170 | getpict(pname)                         /* get picture pname */
170   char  *pname;
171   {
172          extern char  *libpath;
173 +        double  inpaspect;
174          char  *pfname;
175          FILE  *fp;
176          COLOR  *scanin;
177 <        int  width, height;
178 <        int  x, y;
179 <        register int  i;
177 >        int  sl, ns;
178 >        RESOLU  inpres;
179 >        FLOAT  loc[2];
180 >        int  y;
181 >        register int  x, i;
182          register DATARRAY  *pp;
183                                                  /* look for array in list */
184 <        for (pp = plist; pp != NULL; pp = pp->next)
184 >        for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next)
185                  if (!strcmp(pname, pp->name))
186                          return(pp);             /* found! */
187  
# Line 167 | Line 200 | char  *pname;
200                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
201                  error(SYSTEM, errmsg);
202          }
203 + #ifdef MSDOS
204 +        setmode(fileno(fp), O_BINARY);
205 + #endif
206                                                  /* get dimensions */
207          inpaspect = 1.0;
208 <        getheader(fp, headaspect);
209 <        if (fgetresolu(&width, &height, fp) != (YMAJOR|YDECR))
208 >        getheader(fp, headaspect, &inpaspect);
209 >        if (!fgetsresolu(&inpres, fp))
210                  goto readerr;
211 + #if PSIZWARN
212 +                                                /* check memory usage */
213 +        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
214 +        if (i > PSIZWARN) {
215 +                sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
216 +                                pname, i);
217 +                error(WARNING, errmsg);
218 +        }
219 + #endif
220          for (i = 0; i < 3; i++) {
221                  pp[i].nd = 2;
222 <                pp[i].dim[0].ne = width;
223 <                pp[i].dim[1].ne = height;
222 >                pp[i].dim[0].ne = inpres.yr;
223 >                pp[i].dim[1].ne = inpres.xr;
224                  pp[i].dim[0].org =
225                  pp[i].dim[1].org = 0.0;
226 <                if (width <= height*inpaspect) {
227 <                        pp[i].dim[0].siz = 1.0;
228 <                        pp[i].dim[1].siz = inpaspect*(double)height/width;
184 <                } else {
185 <                        pp[i].dim[0].siz = (double)width/height/inpaspect;
226 >                if (inpres.xr <= inpres.yr*inpaspect) {
227 >                        pp[i].dim[0].siz = inpaspect *
228 >                                                (double)inpres.yr/inpres.xr;
229                          pp[i].dim[1].siz = 1.0;
230 +                } else {
231 +                        pp[i].dim[0].siz = 1.0;
232 +                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
233 +                                                inpaspect;
234                  }
235 <                pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
235 >                pp[i].dim[0].p = pp[i].dim[1].p = NULL;
236 >                pp[i].arr = (DATATYPE *)
237 >                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
238                  if (pp[i].arr == NULL)
239                          goto memerr;
240          }
241                                                          /* load picture */
242 <        if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
242 >        sl = scanlen(&inpres);
243 >        ns = numscans(&inpres);
244 >        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
245                  goto memerr;
246 <        for (y = height-1; y >= 0; y--) {
247 <                if (freadscan(scanin, width, fp) < 0)
246 >        for (y = 0; y < ns; y++) {
247 >                if (freadscan(scanin, sl, fp) < 0)
248                          goto readerr;
249 <                for (x = 0; x < width; x++)
250 <                        for (i = 0; i < 3; i++)
251 <                                pp[i].arr[x*height+y] = colval(scanin[x],i);
249 >                for (x = 0; x < sl; x++) {
250 >                        pix2loc(loc, &inpres, x, y);
251 >                        i = (int)(loc[1]*inpres.yr)*inpres.xr +
252 >                                        (int)(loc[0]*inpres.xr);
253 >                        pp[0].arr[i] = colval(scanin[x],RED);
254 >                        pp[1].arr[i] = colval(scanin[x],GRN);
255 >                        pp[2].arr[i] = colval(scanin[x],BLU);
256 >                }
257          }
258          free((char *)scanin);
259          fclose(fp);
260 +        i = hash(pname);
261          pp[0].next =
262          pp[1].next =
263 <        pp[2].next = plist;
264 <        return(plist = pp);
263 >        pp[2].next = ptab[i];
264 >        return(ptab[i] = pp);
265  
266   memerr:
267          error(SYSTEM, "out of memory in getpict");
# Line 217 | Line 274 | readerr:
274   freedata(dname)                 /* free memory associated with dname */
275   char  *dname;
276   {
277 +        DATARRAY  head;
278 +        int  hval, nents;
279          register DATARRAY  *dp, *dpl;
280          register int  i;
281  
282 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
283 <                if (!strcmp(dname, dp->name)) {
284 <                        if (dpl == NULL)
285 <                                dlist = dp->next;
286 <                        else
282 >        if (dname == NULL) {                    /* free all if NULL */
283 >                hval = 0; nents = TABSIZ;
284 >        } else {
285 >                hval = hash(dname); nents = 1;
286 >        }
287 >        while (nents--) {
288 >                head.next = dtab[hval];
289 >                dpl = &head;
290 >                while ((dp = dpl->next) != NULL)
291 >                        if (dname == NULL || !strcmp(dname, dp->name)) {
292                                  dpl->next = dp->next;
293 <                        free((char *)dp->arr);
294 <                        for (i = 0; i < dp->nd; i++)
295 <                                if (dp->dim[i].p != NULL)
296 <                                        free((char *)dp->dim[i].p);
297 <                        freestr(dp->name);
298 <                        free((char *)dp);
299 <                        return;
300 <                }
293 >                                free((char *)dp->arr);
294 >                                for (i = 0; i < dp->nd; i++)
295 >                                        if (dp->dim[i].p != NULL)
296 >                                                free((char *)dp->dim[i].p);
297 >                                freestr(dp->name);
298 >                                free((char *)dp);
299 >                        } else
300 >                                dpl = dp;
301 >                dtab[hval++] = head.next;
302 >        }
303   }
304  
305  
306   freepict(pname)                 /* free memory associated with pname */
307   char  *pname;
308   {
309 +        DATARRAY  head;
310 +        int  hval, nents;
311          register DATARRAY  *pp, *ppl;
312  
313 <        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
314 <                if (!strcmp(pname, pp->name)) {
315 <                        if (ppl == NULL)
316 <                                plist = pp->next;
317 <                        else
313 >        if (pname == NULL) {                    /* free all if NULL */
314 >                hval = 0; nents = TABSIZ;
315 >        } else {
316 >                hval = hash(pname); nents = 1;
317 >        }
318 >        while (nents--) {
319 >                head.next = ptab[hval];
320 >                ppl = &head;
321 >                while ((pp = ppl->next) != NULL)
322 >                        if (pname == NULL || !strcmp(pname, pp->name)) {
323                                  ppl->next = pp->next;
324 <                        free((char *)pp[0].arr);
325 <                        free((char *)pp[1].arr);
326 <                        free((char *)pp[2].arr);
327 <                        freestr(pp[0].name);
328 <                        free((char *)pp);
329 <                        return;
330 <                }
324 >                                free((char *)pp[0].arr);
325 >                                free((char *)pp[1].arr);
326 >                                free((char *)pp[2].arr);
327 >                                freestr(pp[0].name);
328 >                                free((char *)pp);
329 >                        } else
330 >                                ppl = pp;
331 >                ptab[hval++] = head.next;
332 >        }
333   }
334  
335  
336   double
337   datavalue(dp, pt)               /* interpolate data value at a point */
338   register DATARRAY  *dp;
339 < double  *pt;
339 > double  *pt;
340   {
341          DATARRAY  sd;
342          int  asize;
343          int  lower, upper;
344          register int  i;
345 <        double  x, y, y0, y1;
345 >        double  x, y, y0, y1;
346                                          /* set up dimensions for recursion */
347          sd.nd = dp->nd - 1;
348          asize = 1;
# Line 298 | Line 373 | double  *pt;
373                          i = (lower + upper) >> 1;
374                          if (pt[0] >= dp->dim[0].p[i])
375                                  lower = i;
376 <                        else if (pt[0] < dp->dim[0].p[i])
376 >                        else
377                                  upper = i;
378                  } while (i != (lower + upper) >> 1);
379 <                if (i < 0)
305 <                        i = 0;
306 <                else if (i > dp->dim[0].ne - 2)
379 >                if (i > dp->dim[0].ne - 2)
380                          i = dp->dim[0].ne - 2;
381                  x = i + (pt[0] - dp->dim[0].p[i]) /
382                                  (dp->dim[0].p[i+1] - dp->dim[0].p[i]);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines