ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/data.c
Revision: 2.2
Committed: Mon Aug 19 18:06:14 2024 UTC (8 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +2 -2 lines
Log Message:
fix(rvu,rtrace,rpict,rcontrib): specpict was placing images upside down

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: data.c,v 2.1 2024/08/02 18:47:25 greg Exp $";
3 #endif
4 /*
5 * data.c - routines dealing with interpolated data.
6 */
7
8 #include "copyright.h"
9
10 #include <time.h>
11
12 #include "platform.h"
13 #include "paths.h"
14 #include "standard.h"
15 #include "color.h"
16 #include "view.h"
17 #include "data.h"
18
19 /* picture memory usage before warning */
20 #ifndef PSIZWARN
21 #ifdef SMLMEM
22 #define PSIZWARN 3000000
23 #else
24 #define PSIZWARN 50000000
25 #endif
26 #endif
27
28 #ifndef TABSIZ
29 #define TABSIZ 997 /* table size (prime) */
30 #endif
31
32 #define hash(s) (shash(s)%TABSIZ)
33
34
35 static DATARRAY *dtab[TABSIZ]; /* data array list */
36
37 static gethfunc headaspect;
38
39
40 DATARRAY *
41 getdata( /* get data array dname */
42 char *dname
43 )
44 {
45 char *dfname;
46 FILE *fp;
47 int asize=0;
48 int i, j;
49 DATARRAY *dp;
50 /* look for array in list */
51 for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
52 if (!strcmp(dname, dp->name))
53 return(dp); /* found! */
54 /*
55 * If we haven't loaded the data already, we will look
56 * for it in the directories specified by the library path.
57 *
58 * The file has the following format:
59 *
60 * N
61 * beg0 end0 n0
62 * beg1 end1 n1
63 * . . .
64 * begN endN nN
65 * data, later dimensions changing faster
66 * . . .
67 *
68 * For irregularly spaced points, the following can be
69 * substituted for begi endi ni:
70 *
71 * 0 0 ni p0i p1i .. pni
72 */
73
74 if ((dfname = getpath(dname, getrlibpath(), R_OK)) == NULL) {
75 sprintf(errmsg, "cannot find data file \"%s\"", dname);
76 error(SYSTEM, errmsg);
77 }
78 if ((fp = fopen(dfname, "r")) == NULL) {
79 sprintf(errmsg, "cannot open data file \"%s\"", dfname);
80 error(SYSTEM, errmsg);
81 }
82 /* get dimensions */
83 if (fgetval(fp, 'i', &asize) <= 0)
84 goto scanerr;
85 if ((asize <= 0) | (asize > MAXDDIM)) {
86 sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
87 error(USER, errmsg);
88 }
89 if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
90 goto memerr;
91 dp->name = savestr(dname);
92 dp->type = DATATY;
93 dp->nd = asize;
94 asize = 1;
95 for (i = 0; i < dp->nd; i++) {
96 if (fgetval(fp, DATATY, &dp->dim[i].org) <= 0)
97 goto scanerr;
98 if (fgetval(fp, DATATY, &dp->dim[i].siz) <= 0)
99 goto scanerr;
100 if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0)
101 goto scanerr;
102 if (dp->dim[i].ne < 2)
103 goto scanerr;
104 asize *= dp->dim[i].ne;
105 if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
106 dp->dim[i].p = (DATATYPE *)
107 malloc(dp->dim[i].ne*sizeof(DATATYPE));
108 if (dp->dim[i].p == NULL)
109 goto memerr;
110 for (j = 0; j < dp->dim[i].ne; j++)
111 if (fgetval(fp, DATATY, &dp->dim[i].p[j]) <= 0)
112 goto scanerr;
113 for (j = 1; j < dp->dim[i].ne-1; j++)
114 if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
115 (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
116 goto scanerr;
117 dp->dim[i].org = dp->dim[i].p[0];
118 dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
119 - dp->dim[i].p[0];
120 } else
121 dp->dim[i].p = NULL;
122 }
123 if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
124 goto memerr;
125
126 for (i = 0; i < asize; i++)
127 if (fgetval(fp, DATATY, &dp->arr.d[i]) <= 0)
128 goto scanerr;
129 fclose(fp);
130 i = hash(dname);
131 dp->next = dtab[i];
132 return(dtab[i] = dp);
133 memerr:
134 error(SYSTEM, "out of memory in getdata");
135 scanerr:
136 sprintf(errmsg, "%s in data file \"%s\"",
137 feof(fp) ? "unexpected EOF" : "bad format", dfname);
138 error(USER, errmsg);
139 return NULL; /* pro forma return */
140 }
141
142
143 static int
144 headaspect( /* check string for aspect ratio */
145 char *s,
146 void *iap
147 )
148 {
149 char fmt[MAXFMTLEN];
150
151 if (isaspect(s))
152 *(double*)iap *= aspectval(s);
153 else if (formatval(fmt, s) && strcmp(fmt, COLRFMT))
154 *(double*)iap = 0.0;
155 return(0);
156 }
157
158 DATARRAY *
159 getpict( /* get picture pname */
160 char *pname
161 )
162 {
163 double inpaspect;
164 char *pfname;
165 FILE *fp;
166 COLR *scanin;
167 int sl, ns;
168 RESOLU inpres;
169 RREAL loc[2];
170 int y;
171 int x, i;
172 DATARRAY *pp;
173 /* look for array in list */
174 for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next)
175 if (!strcmp(pname, pp->name))
176 return(pp); /* found! */
177
178 if ((pfname = getpath(pname, getrlibpath(), R_OK)) == NULL) {
179 sprintf(errmsg, "cannot find picture file \"%s\"", pname);
180 error(SYSTEM, errmsg);
181 }
182 if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
183 goto memerr;
184
185 pp[0].name = savestr(pname);
186
187 if ((fp = fopen(pfname, "rb")) == NULL) {
188 sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
189 error(SYSTEM, errmsg);
190 }
191 /* get dimensions */
192 inpaspect = 1.0;
193 getheader(fp, headaspect, &inpaspect);
194 if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
195 goto readerr;
196 pp[0].nd = 2;
197 pp[0].dim[0].ne = inpres.yr;
198 pp[0].dim[1].ne = inpres.xr;
199 pp[0].dim[0].org =
200 pp[0].dim[1].org = 0.0;
201 if (inpres.xr <= inpres.yr*inpaspect) {
202 pp[0].dim[0].siz = inpaspect *
203 (double)inpres.yr/inpres.xr;
204 pp[0].dim[1].siz = 1.0;
205 } else {
206 pp[0].dim[0].siz = 1.0;
207 pp[0].dim[1].siz = (double)inpres.xr/inpres.yr /
208 inpaspect;
209 }
210 pp[0].dim[0].p = pp[0].dim[1].p = NULL;
211 sl = scanlen(&inpres); /* allocate array */
212 ns = numscans(&inpres);
213 i = ns*sl*sizeof(COLR);
214 #if PSIZWARN
215 if (i > PSIZWARN) { /* memory warning */
216 sprintf(errmsg, "picture file \"%s\" using %.1f MB of memory",
217 pname, i*(1.0/(1024*1024)));
218 error(WARNING, errmsg);
219 }
220 #endif
221 if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
222 goto memerr;
223 /* load picture */
224 if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
225 goto memerr;
226 for (y = 0; y < ns; y++) {
227 if (freadcolrs(scanin, sl, fp) < 0)
228 goto readerr;
229 for (x = 0; x < sl; x++) {
230 pix2loc(loc, &inpres, x, y);
231 i = (int)(loc[1]*inpres.yr)*inpres.xr +
232 (int)(loc[0]*inpres.xr);
233 copycolr(pp[0].arr.c[i], scanin[x]);
234 }
235 }
236 free(scanin);
237 fclose(fp);
238 i = hash(pname);
239 pp[0].next = dtab[i]; /* link into picture list */
240 pp[1] = pp[0];
241 pp[2] = pp[0];
242 pp[0].type = RED; /* differentiate RGB records */
243 pp[1].type = GRN;
244 pp[2].type = BLU;
245 return(dtab[i] = pp);
246 memerr:
247 error(SYSTEM, "out of memory in getpict");
248 readerr:
249 sprintf(errmsg, "bad picture file \"%s\"", pfname);
250 error(USER, errmsg);
251 return NULL; /* pro forma return */
252 }
253
254
255 /* header info type for hyperspectral image */
256 typedef struct {
257 float wlpart[4]; /* wavelength partitions */
258 int nc; /* number of components */
259 double inpaspect; /* pixel aspect ratio */
260 } SPECINFO;
261
262 static int
263 specheadline( /* get info for spectral image */
264 char *s,
265 void *cdp
266 )
267 {
268 SPECINFO *sip = (SPECINFO *)cdp;
269 char fmt[MAXFMTLEN];
270
271 if (isaspect(s))
272 sip->inpaspect *= aspectval(s);
273 else if (isncomp(s))
274 sip->nc = ncompval(s);
275 else if (iswlsplit(s))
276 wlsplitval(sip->wlpart, s);
277 else if (formatval(fmt, s) && strcmp(fmt, SPECFMT))
278 return(-1);
279 return(0);
280 }
281
282 DATARRAY *
283 getspec( /* load hyperspectral image as data */
284 char *sname
285 )
286 {
287 SPECINFO si;
288 char *pfname;
289 FILE *fp;
290 int sl, ns;
291 int y, i;
292 DATARRAY *pp;
293 /* look for array in list */
294 for (pp = dtab[hash(sname)]; pp != NULL; pp = pp->next)
295 if (!strcmp(sname, pp->name))
296 return(pp); /* found! */
297
298 if ((pfname = getpath(sname, getrlibpath(), R_OK)) == NULL) {
299 sprintf(errmsg, "cannot find hyperspectral image \"%s\"", sname);
300 error(SYSTEM, errmsg);
301 }
302 if ((fp = fopen(pfname, "rb")) == NULL) {
303 sprintf(errmsg, "cannot open hyperspectral image \"%s\"", pfname);
304 error(SYSTEM, errmsg);
305 }
306 si.wlpart[3] = 0;
307 si.nc = 0;
308 si.inpaspect = 1.0;
309 if (getheader(fp, specheadline, &si) < 0 ||
310 (si.nc <= 3) | (si.nc > MAXCSAMP) | (si.wlpart[3] < 1) ||
311 !fscnresolu(&sl, &ns, fp))
312 goto readerr;
313
314 if ((pp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
315 goto memerr;
316
317 pp->name = savestr(sname);
318 pp->type = SPECTY;
319 pp->nd = 3;
320 pp->dim[0].ne = ns;
321 pp->dim[1].ne = sl;
322 pp->dim[0].org =
323 pp->dim[1].org = 0.0;
324 if (sl <= ns*si.inpaspect) {
325 pp->dim[0].siz = si.inpaspect * (double)ns/sl;
326 pp->dim[1].siz = 1.0;
327 } else {
328 pp->dim[0].siz = 1.0;
329 pp->dim[1].siz = (double)sl/ns / si.inpaspect;
330 }
331 pp->dim[2].ne = si.nc;
332 pp->dim[2].siz = si.wlpart[3] - si.wlpart[0];
333 pp->dim[2].org = si.wlpart[0] + 0.5*pp->dim[2].siz/si.nc;
334 pp->dim[2].siz *= (si.nc - 1.0)/si.nc;
335 pp->dim[0].p = pp->dim[1].p = pp->dim[2].p = NULL;
336 i = ns*sl*(si.nc+1);
337 #if PSIZWARN
338 if (i > PSIZWARN) { /* memory warning */
339 sprintf(errmsg, "hyperspectral image \"%s\" using %.1f MB of memory",
340 sname, i*(1.0/(1024*1024)));
341 error(WARNING, errmsg);
342 }
343 #endif
344 if ((pp->arr.s = (uby8 *)malloc(i)) == NULL)
345 goto memerr;
346 for (y = ns; y-- > 0; ) /* read each scanline */
347 if (freadscolrs(pp->arr.s + y*sl*(si.nc+1), si.nc, sl, fp) < 0)
348 goto readerr;
349 fclose(fp);
350 i = hash(sname); /* insert in hash table */
351 pp->next = dtab[i];
352 return(dtab[i] = pp);
353 memerr:
354 error(SYSTEM, "out of memory in getspec");
355 readerr:
356 sprintf(errmsg, "bad hyperspectral image \"%s\"", pfname);
357 error(USER, errmsg);
358 return NULL; /* pro forma return */
359 }
360
361
362 void
363 freedata( /* release data array reference */
364 DATARRAY *dta
365 )
366 {
367 DATARRAY head;
368 int hval, nents;
369 DATARRAY *dpl, *dp;
370 int i;
371
372 if (dta == NULL) { /* free all if NULL */
373 hval = 0; nents = TABSIZ;
374 } else {
375 if (dta->next == dta) {
376 free(dta); /* unlisted temp array */
377 return;
378 }
379 hval = hash(dta->name); nents = 1;
380 if (!*dta->name) { /* not a data file? */
381 dta->next = dtab[hval];
382 dtab[hval] = dta; /* ...fake position */
383 }
384 }
385 while (nents--) {
386 head.next = dtab[hval];
387 dpl = &head;
388 while ((dp = dpl->next) != NULL)
389 if ((dta == NULL) | (dta == dp)) {
390 dpl->next = dp->next;
391 free(dp->arr.p);
392 for (i = 0; i < dp->nd; i++)
393 if (dp->dim[i].p != NULL)
394 free(dp->dim[i].p);
395 freestr(dp->name);
396 free(dp);
397 } else
398 dpl = dp;
399 dtab[hval++] = head.next;
400 }
401 }
402
403
404 /* internal call to interpolate data value or vector */
405 static double
406 data_interp(DATARRAY *dp, double *pt, double coef, DATATYPE *rvec)
407 {
408 DATARRAY sd;
409 int stride, i;
410 double x, c0, c1, y0, y1;
411 /* unlikely, but may as well check */
412 if ((-FTINY <= coef) & (coef <= FTINY))
413 return(0.);
414 /* set up dimensions for recursion */
415 if (dp->nd > 1) {
416 sd.name = dp->name;
417 sd.type = dp->type;
418 sd.nd = dp->nd - 1;
419 memcpy(sd.dim, dp->dim+1, sd.nd*sizeof(struct dadim));
420 stride = sd.dim[i = sd.nd-1].ne + (sd.type==SPECTY);
421 while (i-- > 0)
422 stride *= sd.dim[i].ne;
423 }
424 /* get independent variable */
425 if (dp->dim[0].p == NULL) { /* evenly spaced points */
426 x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
427 x *= (double)(dp->dim[0].ne - 1);
428 i = x;
429 if (i < 0)
430 i = 0;
431 else if (i > dp->dim[0].ne - 2)
432 i = dp->dim[0].ne - 2;
433 } else { /* unevenly spaced points */
434 int lower, upper;
435 if (dp->dim[0].siz > 0.) {
436 lower = 0;
437 upper = dp->dim[0].ne;
438 } else {
439 lower = dp->dim[0].ne;
440 upper = 0;
441 }
442 do {
443 i = (lower + upper) >> 1;
444 if (pt[0] >= dp->dim[0].p[i])
445 lower = i;
446 else
447 upper = i;
448 } while (i != (lower + upper) >> 1);
449
450 if (i > dp->dim[0].ne - 2)
451 i = dp->dim[0].ne - 2;
452
453 x = i + (pt[0] - dp->dim[0].p[i]) /
454 (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
455 }
456 /*
457 * Compute interpolation coefficients:
458 * extrapolate as far as one division, then
459 * taper off harmonically to zero.
460 */
461 if (x > i+2) {
462 c0 = 1./(i-1 - x);
463 c1 = -2.*c0;
464 } else if (x < i-1) {
465 c1 = 1./(i - x);
466 c0 = -2.*c1;
467 } else {
468 c0 = i+1 - x;
469 c1 = x - i;
470 }
471 c0 *= coef;
472 c1 *= coef;
473 /* check if vector interp */
474 if ((dp->nd == 2) & (rvec != NULL)) {
475 if (dp->type == DATATY) {
476 sd.arr.d = dp->arr.d + i*stride;
477 for (i = sd.dim[0].ne; i--; )
478 rvec[i] += c0*sd.arr.d[i]
479 + c1*sd.arr.d[i+stride];
480 } else if (dp->type == SPECTY) {
481 double f;
482 sd.arr.s = dp->arr.s + i*stride;
483 if ((sd.arr.s[sd.dim[0].ne] > 0) & ((-FTINY>c0)|(c0>FTINY))) {
484 f = ldexp(c0, (int)sd.arr.s[sd.dim[0].ne]-(COLXS+8));
485 for (i = sd.dim[0].ne; i--; )
486 rvec[i] += f*(sd.arr.s[i] + .5);
487 }
488 sd.arr.s += stride;
489 if ((sd.arr.s[sd.dim[0].ne] > 0) & ((-FTINY>c1)|(c1>FTINY))) {
490 f = ldexp(c1, (int)sd.arr.s[sd.dim[0].ne]-(COLXS+8));
491 for (i = sd.dim[0].ne; i--; )
492 rvec[i] += f*(sd.arr.s[i] + .5);
493 }
494 } else {
495 sd.arr.c = dp->arr.c + i*stride;
496 for (i = sd.dim[0].ne; i--; )
497 rvec[i] += c0*colrval(sd.arr.c[i],sd.type)
498 + c1*colrval(sd.arr.c[i+stride],sd.type);
499 }
500 return(0.); /* return value ignored */
501 }
502 /* get dependent variable */
503 if (dp->nd > 1) {
504 if (dp->type == DATATY) {
505 sd.arr.d = dp->arr.d + i*stride;
506 y0 = data_interp(&sd, pt+1, c0, rvec);
507 sd.arr.d += stride;
508 } else if (dp->type == SPECTY) {
509 sd.arr.s = dp->arr.s + i*stride;
510 y0 = data_interp(&sd, pt+1, c0, rvec);
511 sd.arr.s += stride;
512 } else {
513 sd.arr.c = dp->arr.c + i*stride;
514 y0 = data_interp(&sd, pt+1, c0, rvec);
515 sd.arr.c += stride;
516 }
517 y1 = data_interp(&sd, pt+1, c1, rvec);
518 } else { /* end of recursion */
519 if (dp->type == DATATY) {
520 y0 = dp->arr.d[i];
521 y1 = dp->arr.d[i+1];
522 } else if (dp->type == SPECTY) {
523 if (dp->arr.s[dp->dim[0].ne]) {
524 double f = dp->arr.s[dp->dim[0].ne]
525 ? ldexp(1., -(COLXS+8) +
526 (int)dp->arr.s[dp->dim[0].ne])
527 : 0.;
528 y0 = f*(dp->arr.s[i] + 0.5);
529 y1 = f*(dp->arr.s[i+1] + 0.5);
530 } else
531 y0 = y1 = 0.;
532 } else {
533 y0 = colrval(dp->arr.c[i],dp->type);
534 y1 = colrval(dp->arr.c[i+1],dp->type);
535 }
536 y0 *= c0;
537 y1 *= c1;
538 }
539 return(y0 + y1); /* coefficients already applied */
540 }
541
542
543 double
544 datavalue( /* interpolate data value at a point */
545 DATARRAY *dp,
546 double *pt
547 )
548 {
549 return(data_interp(dp, pt, 1., NULL));
550 }
551
552
553 /* Interpolate final vector corresponding to last dimension in data array */
554 DATARRAY *
555 datavector(DATARRAY *dp, double *pt)
556 {
557 DATARRAY *newdp;
558
559 if (dp->nd < 2)
560 error(INTERNAL, "datavector() called with 1-D array");
561 /* create vector array */
562 newdp = (DATARRAY *)malloc(sizeof(DATARRAY) -
563 (MAXDDIM-1)*sizeof(struct dadim) +
564 sizeof(DATATYPE)*dp->dim[dp->nd-1].ne);
565 if (newdp == NULL)
566 error(SYSTEM, "out of memory in datavector");
567 newdp->next = newdp; /* flags us as temp vector */
568 newdp->name = dp->name;
569 newdp->type = DATATY;
570 newdp->nd = 1; /* vector data goes here */
571 newdp->dim[0] = dp->dim[dp->nd-1];
572 newdp->arr.d = (DATATYPE *)(newdp->dim + 1);
573 memset(newdp->arr.d, 0, sizeof(DATATYPE)*newdp->dim[0].ne);
574
575 (void)data_interp(dp, pt, 1., newdp->arr.d);
576
577 return(newdp); /* will be free'd using free() */
578 }