ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
Revision: 1.5
Committed: Tue Apr 3 17:48:46 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +16 -4 lines
Log Message:
Added check for pixel aspect ratio in getpict()

File Contents

# Content
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, j;
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 * N
48 * beg0 end0 n0
49 * beg1 end1 n1
50 * . . .
51 * begN endN nN
52 * data, later dimensions changing faster
53 * . . .
54 *
55 * For irregularly spaced points, the following can be
56 * substituted for begi endi ni:
57 *
58 * @ ni p0i p1i .. pni
59 */
60
61 if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
62 sprintf(errmsg, "cannot find data file \"%s\"", dname);
63 error(USER, errmsg);
64 }
65 if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
66 goto memerr;
67
68 dp->name = savestr(dname);
69
70 if ((fp = fopen(dfname, "r")) == NULL) {
71 sprintf(errmsg, "cannot open data file \"%s\"", dfname);
72 error(SYSTEM, errmsg);
73 }
74 /* get dimensions */
75 if (fscanf(fp, "%d", &dp->nd) != 1)
76 goto scanerr;
77 if (dp->nd <= 0 || dp->nd > MAXDIM) {
78 sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
79 error(USER, errmsg);
80 }
81 asize = 1;
82 for (i = 0; i < dp->nd; i++) {
83 if (fscanf(fp, "%lf %lf %d",
84 &dp->dim[i].org, &dp->dim[i].siz,
85 &dp->dim[i].ne) == 3) {
86 dp->dim[i].siz -= dp->dim[i].org;
87 dp->dim[i].p = NULL;
88 } else if (fscanf(fp, "@ %d", &dp->dim[i].ne) == 1) {
89 dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
90 if (dp->dim[i].p == NULL)
91 goto memerr;
92 for (j = 0; j < dp->dim[i].ne; j++)
93 if (fscanf(fp, "%lf", &dp->dim[i].p[j]) != 1)
94 goto scanerr;
95 for (j = 1; j < dp->dim[i].ne-1; j++)
96 if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
97 (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
98 goto scanerr;
99 dp->dim[i].org = dp->dim[i].p[0];
100 dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
101 - dp->dim[i].p[0];
102 } else
103 goto scanerr;
104 if (dp->dim[i].siz == 0.0 || dp->dim[i].ne < 2)
105 goto scanerr;
106 asize *= dp->dim[i].ne;
107 }
108 if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
109 goto memerr;
110
111 for (i = 0; i < asize; i++)
112 if (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
113 goto scanerr;
114
115 fclose(fp);
116 dp->next = dlist;
117 return(dlist = dp);
118
119 memerr:
120 error(SYSTEM, "out of memory in getdata");
121 scanerr:
122 sprintf(errmsg, "%s in data file \"%s\"",
123 feof(fp) ? "unexpected EOF" : "bad format", dfname);
124 error(USER, errmsg);
125 }
126
127
128 static double inpaspect; /* aspect ratio of input picture */
129
130 static
131 headaspect(s) /* check string for aspect ratio */
132 char *s;
133 {
134 if (isaspect(s))
135 inpaspect *= aspectval(s);
136 }
137
138
139 DATARRAY *
140 getpict(pname) /* get picture pname */
141 char *pname;
142 {
143 extern char *libpath;
144 char *pfname;
145 FILE *fp;
146 COLOR *scanin;
147 int width, height;
148 int x, y;
149 register int i;
150 register DATARRAY *pp;
151 /* look for array in list */
152 for (pp = plist; pp != NULL; pp = pp->next)
153 if (!strcmp(pname, pp->name))
154 return(pp); /* found! */
155
156 if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
157 sprintf(errmsg, "cannot find picture file \"%s\"", pname);
158 error(USER, errmsg);
159 }
160 if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL)
161 goto memerr;
162
163 pp[0].name =
164 pp[1].name =
165 pp[2].name = savestr(pname);
166
167 if ((fp = fopen(pfname, "r")) == NULL) {
168 sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
169 error(SYSTEM, errmsg);
170 }
171 /* get dimensions */
172 inpaspect = 1.0;
173 getheader(fp, headaspect);
174 if (fgetresolu(&width, &height, fp) != (YMAJOR|YDECR))
175 goto readerr;
176 for (i = 0; i < 3; i++) {
177 pp[i].nd = 2;
178 pp[i].dim[0].ne = width;
179 pp[i].dim[1].ne = height;
180 pp[i].dim[0].org =
181 pp[i].dim[1].org = 0.0;
182 if (width <= height*inpaspect) {
183 pp[i].dim[0].siz = 1.0;
184 pp[i].dim[1].siz = inpaspect*(double)height/width;
185 } else {
186 pp[i].dim[0].siz = (double)width/height/inpaspect;
187 pp[i].dim[1].siz = 1.0;
188 }
189 pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
190 if (pp[i].arr == NULL)
191 goto memerr;
192 }
193 /* load picture */
194 if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
195 goto memerr;
196 for (y = height-1; y >= 0; y--) {
197 if (freadscan(scanin, width, fp) < 0)
198 goto readerr;
199 for (x = 0; x < width; x++)
200 for (i = 0; i < 3; i++)
201 pp[i].arr[x*height+y] = colval(scanin[x],i);
202 }
203 free((char *)scanin);
204 fclose(fp);
205 pp[0].next =
206 pp[1].next =
207 pp[2].next = plist;
208 return(plist = pp);
209
210 memerr:
211 error(SYSTEM, "out of memory in getpict");
212 readerr:
213 sprintf(errmsg, "bad picture file \"%s\"", pfname);
214 error(USER, errmsg);
215 }
216
217
218 freedata(dname) /* free memory associated with dname */
219 char *dname;
220 {
221 register DATARRAY *dp, *dpl;
222 register int i;
223
224 for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
225 if (!strcmp(dname, dp->name)) {
226 if (dpl == NULL)
227 dlist = dp->next;
228 else
229 dpl->next = dp->next;
230 free((char *)dp->arr);
231 for (i = 0; i < dp->nd; i++)
232 if (dp->dim[i].p != NULL)
233 free((char *)dp->dim[i].p);
234 freestr(dp->name);
235 free((char *)dp);
236 return;
237 }
238 }
239
240
241 freepict(pname) /* free memory associated with pname */
242 char *pname;
243 {
244 register DATARRAY *pp, *ppl;
245
246 for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
247 if (!strcmp(pname, pp->name)) {
248 if (ppl == NULL)
249 plist = pp->next;
250 else
251 ppl->next = pp->next;
252 free((char *)pp[0].arr);
253 free((char *)pp[1].arr);
254 free((char *)pp[2].arr);
255 freestr(pp[0].name);
256 free((char *)pp);
257 return;
258 }
259 }
260
261
262 double
263 datavalue(dp, pt) /* interpolate data value at a point */
264 register DATARRAY *dp;
265 double *pt;
266 {
267 DATARRAY sd;
268 int asize;
269 register int i;
270 double x, y, y0, y1;
271 /* set up dimensions for recursion */
272 sd.nd = dp->nd - 1;
273 asize = 1;
274 for (i = 0; i < sd.nd; i++) {
275 sd.dim[i].org = dp->dim[i+1].org;
276 sd.dim[i].siz = dp->dim[i+1].siz;
277 sd.dim[i].p = dp->dim[i+1].p;
278 asize *= sd.dim[i].ne = dp->dim[i+1].ne;
279 }
280 /* get independent variable */
281 if (dp->dim[0].p == NULL) { /* evenly spaced points */
282 x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
283 x = x * (dp->dim[0].ne - 1);
284 i = x;
285 if (i < 0)
286 i = 0;
287 else if (i > dp->dim[0].ne - 2)
288 i = dp->dim[0].ne - 2;
289 } else { /* unevenly spaced points */
290 if (dp->dim[0].siz > 0.0)
291 for (i = 0; i < dp->dim[0].ne; i++)
292 if (pt[0] < dp->dim[0].p[i])
293 break;
294 else
295 for (i = 0; i < dp->dim[0].ne; i++)
296 if (pt[0] >= dp->dim[0].p[i])
297 break;
298 if (i <= 0)
299 i = 0;
300 else if (i >= dp->dim[0].ne)
301 i = dp->dim[0].ne - 2;
302 else
303 i--;
304 x = i + (pt[0] - dp->dim[0].p[i]) /
305 (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
306 }
307 /* get dependent variable */
308 if (dp->nd == 1) {
309 y0 = dp->arr[i];
310 y1 = dp->arr[i+1];
311 } else {
312 sd.arr = &dp->arr[i*asize];
313 y0 = datavalue(&sd, pt+1);
314 sd.arr = &dp->arr[(i+1)*asize];
315 y1 = datavalue(&sd, pt+1);
316 }
317 /*
318 * Extrapolate as far as one division, then
319 * taper off harmonically to zero.
320 */
321 if (x > i+2)
322 y = (2*y1-y0)/(x-i-1);
323 else if (x < i-1)
324 y = (2*y0-y1)/(i-x);
325 else
326 y = y0*((i+1)-x) + y1*(x-i);
327
328 return(y);
329 }