ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
Revision: 1.2
Committed: Thu Jul 27 22:25:10 1989 UTC (34 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
Changed getpath to check permissions

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1986 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * data.c - routines dealing with interpolated data.
9     *
10     * 6/4/86
11     */
12    
13     #include "standard.h"
14    
15     #include "color.h"
16    
17     #include "data.h"
18    
19    
20     extern char *libpath; /* library search path */
21    
22     static DATARRAY *dlist = NULL; /* data array list */
23    
24     static DATARRAY *plist = NULL; /* picture list */
25    
26    
27     DATARRAY *
28     getdata(dname) /* get data array dname */
29     char *dname;
30     {
31     char *dfname;
32     FILE *fp;
33     int asize;
34     register int i;
35     register DATARRAY *dp;
36     /* look for array in list */
37     for (dp = dlist; dp != NULL; dp = dp->next)
38     if (!strcmp(dname, dp->name))
39     return(dp); /* found! */
40    
41     /*
42     * If we haven't loaded the data already, we will look
43     * for it in the directorys specified by the library path.
44     *
45     * The file has the following format:
46     *
47     * #dimensions
48     * beg0 end0 n0
49     * beg1 end1 n1
50     * . . .
51     * data, later dimensions changing faster
52     * . . .
53     *
54     */
55    
56 greg 1.2 if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
57 greg 1.1 sprintf(errmsg, "cannot find data file \"%s\"", dname);
58     error(USER, errmsg);
59     }
60     if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
61     goto memerr;
62    
63     dp->name = savestr(dname);
64    
65     if ((fp = fopen(dfname, "r")) == NULL) {
66     sprintf(errmsg, "cannot open data file \"%s\"", dfname);
67     error(SYSTEM, errmsg);
68     }
69     /* get dimensions */
70     if (fscanf(fp, "%d", &dp->nd) != 1)
71     goto scanerr;
72     if (dp->nd <= 0 || dp->nd > MAXDIM) {
73     sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
74     error(USER, errmsg);
75     }
76     asize = 1;
77     for (i = 0; i < dp->nd; i++) {
78     if (fscanf(fp, "%lf %lf %d",
79     &dp->dim[i].org, &dp->dim[i].siz,
80     &dp->dim[i].ne) != 3)
81     goto scanerr;
82     dp->dim[i].siz -= dp->dim[i].org;
83     asize *= dp->dim[i].ne;
84     }
85     if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
86     goto memerr;
87    
88     for (i = 0; i < asize; i++)
89     if (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
90     goto scanerr;
91    
92     fclose(fp);
93     dp->next = dlist;
94     return(dlist = dp);
95    
96     memerr:
97     error(SYSTEM, "out of memory in getdata");
98     scanerr:
99     sprintf(errmsg, "%s in data file \"%s\"",
100     feof(fp) ? "unexpected EOF" : "bad format", dfname);
101     error(USER, errmsg);
102     }
103    
104    
105     DATARRAY *
106     getpict(pname) /* get picture pname */
107     char *pname;
108     {
109     extern char *libpath;
110     char *pfname;
111     FILE *fp;
112     COLOR *scanin;
113     int width, height;
114     int x, y;
115     register int i;
116     register DATARRAY *pp;
117     /* look for array in list */
118     for (pp = plist; pp != NULL; pp = pp->next)
119     if (!strcmp(pname, pp->name))
120     return(pp); /* found! */
121    
122 greg 1.2 if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
123 greg 1.1 sprintf(errmsg, "cannot find picture file \"%s\"", pname);
124     error(USER, errmsg);
125     }
126     if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL)
127     goto memerr;
128    
129     pp[0].name =
130     pp[1].name =
131     pp[2].name = savestr(pname);
132    
133     if ((fp = fopen(pfname, "r")) == NULL) {
134     sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
135     error(SYSTEM, errmsg);
136     }
137     /* get dimensions */
138     getheader(fp, NULL);
139     if (fscanf(fp, "-Y %d +X %d\n", &height, &width) != 2)
140     goto readerr;
141     for (i = 0; i < 3; i++) {
142     pp[i].nd = 2;
143     pp[i].dim[0].ne = width;
144     pp[i].dim[1].ne = height;
145     pp[i].dim[0].org =
146     pp[i].dim[1].org = 0.0;
147     if (width <= height) {
148     pp[i].dim[0].siz = 1.0;
149     pp[i].dim[1].siz = (double)height/width;
150     } else {
151     pp[i].dim[0].siz = (double)width/height;
152     pp[i].dim[1].siz = 1.0;
153     }
154     pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
155     if (pp[i].arr == NULL)
156     goto memerr;
157     }
158     /* load picture */
159     if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
160     goto memerr;
161     for (y = height-1; y >= 0; y--) {
162     if (freadscan(scanin, width, fp) < 0)
163     goto readerr;
164     for (x = 0; x < width; x++)
165     for (i = 0; i < 3; i++)
166     pp[i].arr[x*height+y] = colval(scanin[x],i);
167     }
168     free((char *)scanin);
169     fclose(fp);
170     pp[0].next =
171     pp[1].next =
172     pp[2].next = plist;
173     return(plist = pp);
174    
175     memerr:
176     error(SYSTEM, "out of memory in getpict");
177     readerr:
178     sprintf(errmsg, "bad picture file \"%s\"", pfname);
179     error(USER, errmsg);
180     }
181    
182    
183     freedata(dname) /* free memory associated with dname */
184     char *dname;
185     {
186     register DATARRAY *dp, *dpl;
187    
188     for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
189     if (!strcmp(dname, dp->name)) {
190     if (dpl == NULL)
191     dlist = dp->next;
192     else
193     dpl->next = dp->next;
194     free((char *)dp->arr);
195     freestr(dp->name);
196     free((char *)dp);
197     return;
198     }
199     }
200    
201    
202     freepict(pname) /* free memory associated with pname */
203     char *pname;
204     {
205     register DATARRAY *pp, *ppl;
206    
207     for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
208     if (!strcmp(pname, pp->name)) {
209     if (ppl == NULL)
210     plist = pp->next;
211     else
212     ppl->next = pp->next;
213     free((char *)pp[0].arr);
214     free((char *)pp[1].arr);
215     free((char *)pp[2].arr);
216     freestr(pp[0].name);
217     free((char *)pp);
218     return;
219     }
220     }
221    
222    
223     double
224     datavalue(dp, pt) /* interpolate data value at a point */
225     register DATARRAY *dp;
226     double *pt;
227     {
228     DATARRAY sd;
229     int asize;
230     register int i;
231     double x, y, y0, y1;
232    
233     sd.nd = dp->nd - 1;
234     asize = 1;
235     for (i = 0; i < sd.nd; i++) {
236     sd.dim[i].org = dp->dim[i+1].org;
237     sd.dim[i].siz = dp->dim[i+1].siz;
238     asize *= sd.dim[i].ne = dp->dim[i+1].ne;
239     }
240    
241     x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
242    
243     x *= dp->dim[0].ne - 1;
244    
245     i = x;
246     if (i < 0)
247     i = 0;
248     else if (i > dp->dim[0].ne - 2)
249     i = dp->dim[0].ne - 2;
250    
251     if (dp->nd == 1) {
252     y0 = dp->arr[i];
253     y1 = dp->arr[i+1];
254     } else {
255     sd.arr = &dp->arr[i*asize];
256     y0 = datavalue(&sd, pt+1);
257     sd.arr = &dp->arr[(i+1)*asize];
258     y1 = datavalue(&sd, pt+1);
259     }
260    
261     /*
262     * Extrapolate as far as one division, then
263     * taper off harmonically to zero.
264     */
265     if (x > i+2)
266     y = (2*y1-y0)/(x-i-1);
267     else if (x < i-1)
268     y = (2*y0-y1)/(i-x);
269     else
270     y = y0*((i+1)-x) + y1*(x-i);
271    
272     return(y);
273     }