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.8 by greg, Tue Jan 15 22:24:19 1991 UTC vs.
Revision 2.10 by greg, Fri Jun 30 16:07:44 1995 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 < extern char  *libpath;                  /* library search path */
30 > #ifndef TABSIZ
31 > #define TABSIZ          97              /* table size (prime) */
32 > #endif
33  
34 < static DATARRAY  *dlist = NULL;         /* data array list */
34 > #define hash(s)         (shash(s)%TABSIZ)
35  
24 static DATARRAY  *plist = NULL;         /* picture list */
36  
37 + extern char  *getlibpath();             /* library search path */
38  
39 + static DATARRAY  *dtab[TABSIZ];         /* data array list */
40 +
41 + static DATARRAY  *ptab[TABSIZ];         /* picture list */
42 +
43 +
44   DATARRAY *
45   getdata(dname)                          /* get data array dname */
46   char  *dname;
# Line 34 | Line 51 | char  *dname;
51          register int  i, j;
52          register DATARRAY  *dp;
53                                                  /* look for array in list */
54 <        for (dp = dlist; dp != NULL; dp = dp->next)
54 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
55                  if (!strcmp(dname, dp->name))
56                          return(dp);             /* found! */
57  
58          /*
59           *      If we haven't loaded the data already, we will look
60 <         *  for it in the directorys specified by the library path.
60 >         *  for it in the directories specified by the library path.
61           *
62           *      The file has the following format:
63           *
# Line 58 | Line 75 | char  *dname;
75           *              0 0 ni p0i p1i .. pni
76           */
77  
78 <        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
78 >        if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) {
79                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
80                  error(USER, errmsg);
81          }
# Line 72 | Line 89 | char  *dname;
89                  error(SYSTEM, errmsg);
90          }
91                                                          /* get dimensions */
92 <        if (fscanf(fp, "%d", &dp->nd) != 1)
92 >        if (fgetval(fp, 'i', &dp->nd) <= 0)
93                  goto scanerr;
94 <        if (dp->nd <= 0 || dp->nd > MAXDIM) {
94 >        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
95                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
96                  error(USER, errmsg);
97          }
98          asize = 1;
99          for (i = 0; i < dp->nd; i++) {
100 <                if (fscanf(fp, "%lf %lf %d",
84 <                                &dp->dim[i].org, &dp->dim[i].siz,
85 <                                &dp->dim[i].ne) != 3)
100 >                if (fgetval(fp, 'd', &dp->dim[i].org) <= 0)
101                          goto scanerr;
102 +                if (fgetval(fp, 'd', &dp->dim[i].siz) <= 0)
103 +                        goto scanerr;
104 +                if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0)
105 +                        goto scanerr;
106                  if (dp->dim[i].ne < 2)
107                          goto scanerr;
108                  asize *= dp->dim[i].ne;
# Line 92 | Line 111 | char  *dname;
111                          if (dp->dim[i].p == NULL)
112                                  goto memerr;
113                          for (j = 0; j < dp->dim[i].ne; j++)
114 <                                if (fscanf(fp, "%lf", &dp->dim[i].p[j]) != 1)
114 >                                if (fgetval(fp, 'd', &dp->dim[i].p[i]) <= 0)
115                                          goto scanerr;
116                          for (j = 1; j < dp->dim[i].ne-1; j++)
117                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
# Line 108 | Line 127 | char  *dname;
127                  goto memerr;
128          
129          for (i = 0; i < asize; i++)
130 <                if (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
130 >                if (fgetval(fp, DATATY, &dp->arr[i]) <= 0)
131                          goto scanerr;
113        
132          fclose(fp);
133 <        dp->next = dlist;
134 <        return(dlist = dp);
133 >        i = hash(dname);
134 >        dp->next = dtab[i];
135 >        return(dtab[i] = dp);
136  
137   memerr:
138          error(SYSTEM, "out of memory in getdata");
# Line 124 | Line 143 | scanerr:
143   }
144  
145  
127 static double  inpaspect;               /* aspect ratio of input picture */
128
146   static
147 < headaspect(s)                           /* check string for aspect ratio */
147 > headaspect(s, iap)                      /* check string for aspect ratio */
148   char  *s;
149 + double  *iap;
150   {
151          if (isaspect(s))
152 <                inpaspect *= aspectval(s);
152 >                *iap *= aspectval(s);
153   }
154  
155  
# Line 139 | Line 157 | DATARRAY *
157   getpict(pname)                          /* get picture pname */
158   char  *pname;
159   {
160 <        extern char  *libpath;
160 >        double  inpaspect;
161          char  *pfname;
162          FILE  *fp;
163          COLOR  *scanin;
164 <        int  width, height;
165 <        int  x, y;
166 <        register int  i;
164 >        int  sl, ns;
165 >        RESOLU  inpres;
166 >        FLOAT  loc[2];
167 >        int  y;
168 >        register int  x, i;
169          register DATARRAY  *pp;
170                                                  /* look for array in list */
171 <        for (pp = plist; pp != NULL; pp = pp->next)
171 >        for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next)
172                  if (!strcmp(pname, pp->name))
173                          return(pp);             /* found! */
174  
175 <        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
175 >        if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) {
176                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
177                  error(USER, errmsg);
178          }
# Line 167 | Line 187 | char  *pname;
187                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
188                  error(SYSTEM, errmsg);
189          }
190 + #ifdef MSDOS
191 +        setmode(fileno(fp), O_BINARY);
192 + #endif
193                                                  /* get dimensions */
194          inpaspect = 1.0;
195 <        getheader(fp, headaspect);
196 <        if (fgetresolu(&width, &height, fp) != (YMAJOR|YDECR))
195 >        getheader(fp, headaspect, &inpaspect);
196 >        if (!fgetsresolu(&inpres, fp))
197                  goto readerr;
198 + #if PSIZWARN
199 +                                                /* check memory usage */
200 +        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
201 +        if (i > PSIZWARN) {
202 +                sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
203 +                                pname, i);
204 +                error(WARNING, errmsg);
205 +        }
206 + #endif
207          for (i = 0; i < 3; i++) {
208                  pp[i].nd = 2;
209 <                pp[i].dim[0].ne = height;
210 <                pp[i].dim[1].ne = width;
209 >                pp[i].dim[0].ne = inpres.yr;
210 >                pp[i].dim[1].ne = inpres.xr;
211                  pp[i].dim[0].org =
212                  pp[i].dim[1].org = 0.0;
213 <                if (width <= height*inpaspect) {
214 <                        pp[i].dim[0].siz = inpaspect*(double)height/width;
213 >                if (inpres.xr <= inpres.yr*inpaspect) {
214 >                        pp[i].dim[0].siz = inpaspect *
215 >                                                (double)inpres.yr/inpres.xr;
216                          pp[i].dim[1].siz = 1.0;
217                  } else {
218                          pp[i].dim[0].siz = 1.0;
219 <                        pp[i].dim[1].siz = (double)width/height/inpaspect;
219 >                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
220 >                                                inpaspect;
221                  }
222                  pp[i].dim[0].p = pp[i].dim[1].p = NULL;
223 <                pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
223 >                pp[i].arr = (DATATYPE *)
224 >                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
225                  if (pp[i].arr == NULL)
226                          goto memerr;
227          }
228                                                          /* load picture */
229 <        if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
229 >        sl = scanlen(&inpres);
230 >        ns = numscans(&inpres);
231 >        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
232                  goto memerr;
233 <        for (y = height-1; y >= 0; y--) {
234 <                if (freadscan(scanin, width, fp) < 0)
233 >        for (y = 0; y < ns; y++) {
234 >                if (freadscan(scanin, sl, fp) < 0)
235                          goto readerr;
236 <                for (x = 0; x < width; x++)
237 <                        for (i = 0; i < 3; i++)
238 <                                pp[i].arr[y*width+x] = colval(scanin[x],i);
236 >                for (x = 0; x < sl; x++) {
237 >                        pix2loc(loc, &inpres, x, y);
238 >                        i = (int)(loc[1]*inpres.yr)*inpres.xr +
239 >                                        (int)(loc[0]*inpres.xr);
240 >                        pp[0].arr[i] = colval(scanin[x],RED);
241 >                        pp[1].arr[i] = colval(scanin[x],GRN);
242 >                        pp[2].arr[i] = colval(scanin[x],BLU);
243 >                }
244          }
245          free((char *)scanin);
246          fclose(fp);
247 +        i = hash(pname);
248          pp[0].next =
249          pp[1].next =
250 <        pp[2].next = plist;
251 <        return(plist = pp);
250 >        pp[2].next = ptab[i];
251 >        return(ptab[i] = pp);
252  
253   memerr:
254          error(SYSTEM, "out of memory in getpict");
# Line 218 | Line 261 | readerr:
261   freedata(dname)                 /* free memory associated with dname */
262   char  *dname;
263   {
264 +        DATARRAY  head;
265 +        int  hval, nents;
266          register DATARRAY  *dp, *dpl;
267          register int  i;
268  
269 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
270 <                if (!strcmp(dname, dp->name)) {
271 <                        if (dpl == NULL)
272 <                                dlist = dp->next;
273 <                        else
269 >        if (dname == NULL) {                    /* free all if NULL */
270 >                hval = 0; nents = TABSIZ;
271 >        } else {
272 >                hval = hash(dname); nents = 1;
273 >        }
274 >        while (nents--) {
275 >                head.next = dtab[hval];
276 >                dpl = &head;
277 >                while ((dp = dpl->next) != NULL)
278 >                        if (dname == NULL || !strcmp(dname, dp->name)) {
279                                  dpl->next = dp->next;
280 <                        free((char *)dp->arr);
281 <                        for (i = 0; i < dp->nd; i++)
282 <                                if (dp->dim[i].p != NULL)
283 <                                        free((char *)dp->dim[i].p);
284 <                        freestr(dp->name);
285 <                        free((char *)dp);
286 <                        return;
287 <                }
280 >                                free((char *)dp->arr);
281 >                                for (i = 0; i < dp->nd; i++)
282 >                                        if (dp->dim[i].p != NULL)
283 >                                                free((char *)dp->dim[i].p);
284 >                                freestr(dp->name);
285 >                                free((char *)dp);
286 >                        } else
287 >                                dpl = dp;
288 >                dtab[hval++] = head.next;
289 >        }
290   }
291  
292  
293   freepict(pname)                 /* free memory associated with pname */
294   char  *pname;
295   {
296 +        DATARRAY  head;
297 +        int  hval, nents;
298          register DATARRAY  *pp, *ppl;
299  
300 <        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
301 <                if (!strcmp(pname, pp->name)) {
302 <                        if (ppl == NULL)
303 <                                plist = pp->next;
304 <                        else
300 >        if (pname == NULL) {                    /* free all if NULL */
301 >                hval = 0; nents = TABSIZ;
302 >        } else {
303 >                hval = hash(pname); nents = 1;
304 >        }
305 >        while (nents--) {
306 >                head.next = ptab[hval];
307 >                ppl = &head;
308 >                while ((pp = ppl->next) != NULL)
309 >                        if (pname == NULL || !strcmp(pname, pp->name)) {
310                                  ppl->next = pp->next;
311 <                        free((char *)pp[0].arr);
312 <                        free((char *)pp[1].arr);
313 <                        free((char *)pp[2].arr);
314 <                        freestr(pp[0].name);
315 <                        free((char *)pp);
316 <                        return;
317 <                }
311 >                                free((char *)pp[0].arr);
312 >                                free((char *)pp[1].arr);
313 >                                free((char *)pp[2].arr);
314 >                                freestr(pp[0].name);
315 >                                free((char *)pp);
316 >                        } else
317 >                                ppl = pp;
318 >                ptab[hval++] = head.next;
319 >        }
320   }
321  
322  
323   double
324   datavalue(dp, pt)               /* interpolate data value at a point */
325   register DATARRAY  *dp;
326 < double  *pt;
326 > double  *pt;
327   {
328          DATARRAY  sd;
329          int  asize;
330          int  lower, upper;
331          register int  i;
332 <        double  x, y, y0, y1;
332 >        double  x, y0, y1;
333                                          /* set up dimensions for recursion */
334          sd.nd = dp->nd - 1;
335          asize = 1;
# Line 281 | Line 342 | double  *pt;
342                                          /* get independent variable */
343          if (dp->dim[0].p == NULL) {             /* evenly spaced points */
344                  x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
345 <                x = x * (dp->dim[0].ne - 1);
345 >                x *= (double)(dp->dim[0].ne - 1);
346                  i = x;
347                  if (i < 0)
348                          i = 0;
# Line 322 | Line 383 | double  *pt;
383           * taper off harmonically to zero.
384           */
385          if (x > i+2)
386 <                y = (2*y1-y0)/(x-i-1);
326 <        else if (x < i-1)
327 <                y = (2*y0-y1)/(i-x);
328 <        else
329 <                y = y0*((i+1)-x) + y1*(x-i);
386 >                return( (2*y1-y0)/(x-(i-1)) );
387  
388 <        return(y);
388 >        if (x < i-1)
389 >                return( (2*y0-y1)/(i-x) );
390 >
391 >        return( y0*((i+1)-x) + y1*(x-i) );
392   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines