27 |
|
#endif |
28 |
|
|
29 |
|
#ifndef TABSIZ |
30 |
< |
#define TABSIZ 97 /* table size (prime) */ |
30 |
> |
#define TABSIZ 997 /* table size (prime) */ |
31 |
|
#endif |
32 |
|
|
33 |
|
#define hash(s) (shash(s)%TABSIZ) |
81 |
|
error(SYSTEM, errmsg); |
82 |
|
} |
83 |
|
/* get dimensions */ |
84 |
< |
if (fgetval(fp, 'i', (char *)&asize) <= 0) |
84 |
> |
if (fgetval(fp, 'i', &asize) <= 0) |
85 |
|
goto scanerr; |
86 |
|
if ((asize <= 0) | (asize > MAXDDIM)) { |
87 |
|
sprintf(errmsg, "bad number of dimensions for \"%s\"", dname); |
94 |
|
dp->nd = asize; |
95 |
|
asize = 1; |
96 |
|
for (i = 0; i < dp->nd; i++) { |
97 |
< |
if (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0) |
97 |
> |
if (fgetval(fp, DATATY, &dp->dim[i].org) <= 0) |
98 |
|
goto scanerr; |
99 |
< |
if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0) |
99 |
> |
if (fgetval(fp, DATATY, &dp->dim[i].siz) <= 0) |
100 |
|
goto scanerr; |
101 |
< |
if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0) |
101 |
> |
if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0) |
102 |
|
goto scanerr; |
103 |
|
if (dp->dim[i].ne < 2) |
104 |
|
goto scanerr; |
109 |
|
if (dp->dim[i].p == NULL) |
110 |
|
goto memerr; |
111 |
|
for (j = 0; j < dp->dim[i].ne; j++) |
112 |
< |
if (fgetval(fp, DATATY, |
113 |
< |
(char *)&dp->dim[i].p[j]) <= 0) |
112 |
> |
if (fgetval(fp, DATATY, &dp->dim[i].p[j]) <= 0) |
113 |
|
goto scanerr; |
114 |
|
for (j = 1; j < dp->dim[i].ne-1; j++) |
115 |
|
if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) != |
125 |
|
goto memerr; |
126 |
|
|
127 |
|
for (i = 0; i < asize; i++) |
128 |
< |
if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0) |
128 |
> |
if (fgetval(fp, DATATY, &dp->arr.d[i]) <= 0) |
129 |
|
goto scanerr; |
130 |
|
fclose(fp); |
131 |
|
i = hash(dname); |
132 |
|
dp->next = dtab[i]; |
133 |
|
return(dtab[i] = dp); |
135 |
– |
|
134 |
|
memerr: |
135 |
|
error(SYSTEM, "out of memory in getdata"); |
136 |
|
scanerr: |
137 |
|
sprintf(errmsg, "%s in data file \"%s\"", |
138 |
|
feof(fp) ? "unexpected EOF" : "bad format", dfname); |
139 |
|
error(USER, errmsg); |
140 |
< |
return NULL; /* pro forma return */ |
140 |
> |
return NULL; /* pro forma return */ |
141 |
|
} |
142 |
|
|
143 |
|
|
151 |
|
|
152 |
|
if (isaspect(s)) |
153 |
|
*(double*)iap *= aspectval(s); |
154 |
< |
else if (formatval(fmt, s) && !globmatch(PICFMT, fmt)) |
154 |
> |
else if (formatval(fmt, s) && strcmp(fmt, COLRFMT)) |
155 |
|
*(double*)iap = 0.0; |
156 |
|
return(0); |
157 |
|
} |
158 |
|
|
161 |
– |
|
159 |
|
DATARRAY * |
160 |
|
getpict( /* get picture pname */ |
161 |
|
char *pname |
185 |
|
|
186 |
|
pp[0].name = savestr(pname); |
187 |
|
|
188 |
< |
if ((fp = fopen(pfname, "r")) == NULL) { |
188 |
> |
if ((fp = fopen(pfname, "rb")) == NULL) { |
189 |
|
sprintf(errmsg, "cannot open picture file \"%s\"", pfname); |
190 |
|
error(SYSTEM, errmsg); |
191 |
|
} |
195 |
– |
SET_FILE_BINARY(fp); |
192 |
|
/* get dimensions */ |
193 |
|
inpaspect = 1.0; |
194 |
|
getheader(fp, headaspect, &inpaspect); |
234 |
|
copycolr(pp[0].arr.c[i], scanin[x]); |
235 |
|
} |
236 |
|
} |
237 |
< |
free((void *)scanin); |
237 |
> |
free(scanin); |
238 |
|
fclose(fp); |
239 |
|
i = hash(pname); |
240 |
|
pp[0].next = dtab[i]; /* link into picture list */ |
244 |
|
pp[1].type = GRN; |
245 |
|
pp[2].type = BLU; |
246 |
|
return(dtab[i] = pp); |
251 |
– |
|
247 |
|
memerr: |
248 |
|
error(SYSTEM, "out of memory in getpict"); |
249 |
|
readerr: |
250 |
|
sprintf(errmsg, "bad picture file \"%s\"", pfname); |
251 |
|
error(USER, errmsg); |
252 |
< |
return NULL; /* pro forma return */ |
252 |
> |
return NULL; /* pro forma return */ |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
+ |
/* header info type for hyperspectral image */ |
257 |
+ |
typedef struct { |
258 |
+ |
float wlpart[4]; /* wavelength partitions */ |
259 |
+ |
int nc; /* number of components */ |
260 |
+ |
double inpaspect; /* pixel aspect ratio */ |
261 |
+ |
} SPECINFO; |
262 |
+ |
|
263 |
+ |
static int |
264 |
+ |
specheadline( /* get info for spectral image */ |
265 |
+ |
char *s, |
266 |
+ |
void *cdp |
267 |
+ |
) |
268 |
+ |
{ |
269 |
+ |
SPECINFO *sip = (SPECINFO *)cdp; |
270 |
+ |
char fmt[MAXFMTLEN]; |
271 |
+ |
|
272 |
+ |
if (isaspect(s)) |
273 |
+ |
sip->inpaspect *= aspectval(s); |
274 |
+ |
else if (isncomp(s)) |
275 |
+ |
sip->nc = ncompval(s); |
276 |
+ |
else if (iswlsplit(s)) |
277 |
+ |
wlsplitval(sip->wlpart, s); |
278 |
+ |
else if (formatval(fmt, s) && strcmp(fmt, SPECFMT)) |
279 |
+ |
return(-1); |
280 |
+ |
return(0); |
281 |
+ |
} |
282 |
+ |
|
283 |
+ |
DATARRAY * |
284 |
+ |
getspec( /* load hyperspectral image as data */ |
285 |
+ |
char *sname |
286 |
+ |
) |
287 |
+ |
{ |
288 |
+ |
SPECINFO si; |
289 |
+ |
char *pfname; |
290 |
+ |
FILE *fp; |
291 |
+ |
int sl, ns; |
292 |
+ |
int y, i; |
293 |
+ |
DATARRAY *pp; |
294 |
+ |
/* look for array in list */ |
295 |
+ |
for (pp = dtab[hash(sname)]; pp != NULL; pp = pp->next) |
296 |
+ |
if (!strcmp(sname, pp->name)) |
297 |
+ |
return(pp); /* found! */ |
298 |
+ |
|
299 |
+ |
if ((pfname = getpath(sname, getrlibpath(), R_OK)) == NULL) { |
300 |
+ |
sprintf(errmsg, "cannot find hyperspectral image \"%s\"", sname); |
301 |
+ |
error(SYSTEM, errmsg); |
302 |
+ |
} |
303 |
+ |
if ((fp = fopen(pfname, "rb")) == NULL) { |
304 |
+ |
sprintf(errmsg, "cannot open hyperspectral image \"%s\"", pfname); |
305 |
+ |
error(SYSTEM, errmsg); |
306 |
+ |
} |
307 |
+ |
si.wlpart[3] = 0; |
308 |
+ |
si.nc = 0; |
309 |
+ |
si.inpaspect = 1.0; |
310 |
+ |
if (getheader(fp, specheadline, &si) < 0 || |
311 |
+ |
(si.nc <= 3) | (si.nc > MAXCSAMP) | (si.wlpart[3] < 1) || |
312 |
+ |
!fscnresolu(&sl, &ns, fp)) |
313 |
+ |
goto readerr; |
314 |
+ |
|
315 |
+ |
if ((pp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) |
316 |
+ |
goto memerr; |
317 |
+ |
|
318 |
+ |
pp->name = savestr(sname); |
319 |
+ |
pp->type = SPECTY; |
320 |
+ |
pp->nd = 3; |
321 |
+ |
pp->dim[0].ne = ns; |
322 |
+ |
pp->dim[1].ne = sl; |
323 |
+ |
pp->dim[0].org = |
324 |
+ |
pp->dim[1].org = 0.0; |
325 |
+ |
if (sl <= ns*si.inpaspect) { |
326 |
+ |
pp->dim[0].siz = si.inpaspect * (double)ns/sl; |
327 |
+ |
pp->dim[1].siz = 1.0; |
328 |
+ |
} else { |
329 |
+ |
pp->dim[0].siz = 1.0; |
330 |
+ |
pp->dim[1].siz = (double)sl/ns / si.inpaspect; |
331 |
+ |
} |
332 |
+ |
pp->dim[2].ne = si.nc; |
333 |
+ |
pp->dim[2].siz = si.wlpart[3] - si.wlpart[0]; |
334 |
+ |
pp->dim[2].org = si.wlpart[0] + 0.5*pp->dim[2].siz/si.nc; |
335 |
+ |
pp->dim[2].siz *= (si.nc - 1.0)/si.nc; |
336 |
+ |
pp->dim[0].p = pp->dim[1].p = pp->dim[2].p = NULL; |
337 |
+ |
i = ns*sl*(si.nc+1); |
338 |
+ |
#if PSIZWARN |
339 |
+ |
if (i > PSIZWARN) { /* memory warning */ |
340 |
+ |
sprintf(errmsg, "hyperspectral image \"%s\" using %.1f MB of memory", |
341 |
+ |
sname, i*(1.0/(1024*1024))); |
342 |
+ |
error(WARNING, errmsg); |
343 |
+ |
} |
344 |
+ |
#endif |
345 |
+ |
if ((pp->arr.s = (uby8 *)malloc(i)) == NULL) |
346 |
+ |
goto memerr; |
347 |
+ |
for (y = 0; y < ns; y++) /* read each scanline */ |
348 |
+ |
if (freadscolrs(pp->arr.s + y*sl*(si.nc+1), si.nc, sl, fp) < 0) |
349 |
+ |
goto readerr; |
350 |
+ |
fclose(fp); |
351 |
+ |
i = hash(sname); /* insert in hash table */ |
352 |
+ |
pp->next = dtab[i]; |
353 |
+ |
return(dtab[i] = pp); |
354 |
+ |
memerr: |
355 |
+ |
error(SYSTEM, "out of memory in getspec"); |
356 |
+ |
readerr: |
357 |
+ |
sprintf(errmsg, "bad hyperspectral image \"%s\"", pfname); |
358 |
+ |
error(USER, errmsg); |
359 |
+ |
return NULL; /* pro forma return */ |
360 |
+ |
} |
361 |
+ |
|
362 |
+ |
|
363 |
|
void |
364 |
|
freedata( /* release data array reference */ |
365 |
|
DATARRAY *dta |
373 |
|
if (dta == NULL) { /* free all if NULL */ |
374 |
|
hval = 0; nents = TABSIZ; |
375 |
|
} else { |
376 |
+ |
if (dta->next == dta) { |
377 |
+ |
free(dta); /* unlisted temp array */ |
378 |
+ |
return; |
379 |
+ |
} |
380 |
|
hval = hash(dta->name); nents = 1; |
381 |
+ |
if (!*dta->name) { /* not a data file? */ |
382 |
+ |
dta->next = dtab[hval]; |
383 |
+ |
dtab[hval] = dta; /* ...fake position */ |
384 |
+ |
} |
385 |
|
} |
386 |
|
while (nents--) { |
387 |
|
head.next = dtab[hval]; |
389 |
|
while ((dp = dpl->next) != NULL) |
390 |
|
if ((dta == NULL) | (dta == dp)) { |
391 |
|
dpl->next = dp->next; |
392 |
< |
if (dp->type == DATATY) |
283 |
< |
free((void *)dp->arr.d); |
284 |
< |
else |
285 |
< |
free((void *)dp->arr.c); |
392 |
> |
free(dp->arr.p); |
393 |
|
for (i = 0; i < dp->nd; i++) |
394 |
|
if (dp->dim[i].p != NULL) |
395 |
< |
free((void *)dp->dim[i].p); |
395 |
> |
free(dp->dim[i].p); |
396 |
|
freestr(dp->name); |
397 |
< |
free((void *)dp); |
397 |
> |
free(dp); |
398 |
|
} else |
399 |
|
dpl = dp; |
400 |
|
dtab[hval++] = head.next; |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
< |
double |
406 |
< |
datavalue( /* interpolate data value at a point */ |
407 |
< |
DATARRAY *dp, |
301 |
< |
double *pt |
302 |
< |
) |
405 |
> |
/* internal call to interpolate data value or vector */ |
406 |
> |
static double |
407 |
> |
data_interp(DATARRAY *dp, double *pt, double coef, DATATYPE *rvec) |
408 |
|
{ |
409 |
< |
DATARRAY sd; |
410 |
< |
int asize; |
411 |
< |
int lower, upper; |
307 |
< |
int i; |
308 |
< |
double x, y0, y1; |
409 |
> |
DATARRAY sd; |
410 |
> |
int stride, i; |
411 |
> |
double x, c0, c1, y0, y1; |
412 |
|
/* set up dimensions for recursion */ |
413 |
|
if (dp->nd > 1) { |
414 |
|
sd.name = dp->name; |
415 |
|
sd.type = dp->type; |
416 |
|
sd.nd = dp->nd - 1; |
417 |
< |
asize = 1; |
418 |
< |
for (i = 0; i < sd.nd; i++) { |
419 |
< |
sd.dim[i].org = dp->dim[i+1].org; |
420 |
< |
sd.dim[i].siz = dp->dim[i+1].siz; |
318 |
< |
sd.dim[i].p = dp->dim[i+1].p; |
319 |
< |
asize *= sd.dim[i].ne = dp->dim[i+1].ne; |
320 |
< |
} |
417 |
> |
memcpy(sd.dim, dp->dim+1, sd.nd*sizeof(struct dadim)); |
418 |
> |
stride = sd.dim[i = sd.nd-1].ne + (sd.type==SPECTY); |
419 |
> |
while (i-- > 0) |
420 |
> |
stride *= sd.dim[i].ne; |
421 |
|
} |
422 |
|
/* get independent variable */ |
423 |
|
if (dp->dim[0].p == NULL) { /* evenly spaced points */ |
429 |
|
else if (i > dp->dim[0].ne - 2) |
430 |
|
i = dp->dim[0].ne - 2; |
431 |
|
} else { /* unevenly spaced points */ |
432 |
+ |
int lower, upper; |
433 |
|
if (dp->dim[0].siz > 0.0) { |
434 |
|
lower = 0; |
435 |
|
upper = dp->dim[0].ne; |
444 |
|
else |
445 |
|
upper = i; |
446 |
|
} while (i != (lower + upper) >> 1); |
447 |
+ |
|
448 |
|
if (i > dp->dim[0].ne - 2) |
449 |
|
i = dp->dim[0].ne - 2; |
450 |
+ |
|
451 |
|
x = i + (pt[0] - dp->dim[0].p[i]) / |
452 |
|
(dp->dim[0].p[i+1] - dp->dim[0].p[i]); |
453 |
|
} |
454 |
+ |
/* |
455 |
+ |
* Compute interpolation coefficients: |
456 |
+ |
* extrapolate as far as one division, then |
457 |
+ |
* taper off harmonically to zero. |
458 |
+ |
*/ |
459 |
+ |
if (x > i+2) { |
460 |
+ |
c0 = 1./(i-1 - x); |
461 |
+ |
c1 = -2.*c0; |
462 |
+ |
} else if (x < i-1) { |
463 |
+ |
c1 = 1./(i - x); |
464 |
+ |
c0 = -2.*c1; |
465 |
+ |
} else { |
466 |
+ |
c0 = i+1 - x; |
467 |
+ |
c1 = x - i; |
468 |
+ |
} |
469 |
+ |
c0 *= coef; |
470 |
+ |
c1 *= coef; |
471 |
+ |
/* check if vector interp */ |
472 |
+ |
if ((dp->nd == 2) & (rvec != NULL)) { |
473 |
+ |
if (dp->type == DATATY) { |
474 |
+ |
sd.arr.d = dp->arr.d + i*stride; |
475 |
+ |
for (i = sd.dim[0].ne; i--; ) |
476 |
+ |
rvec[i] += c0*sd.arr.d[i] |
477 |
+ |
+ c1*sd.arr.d[i+stride]; |
478 |
+ |
} else if (dp->type == SPECTY) { |
479 |
+ |
double f; |
480 |
+ |
sd.arr.s = dp->arr.s + i*stride; |
481 |
+ |
f = ldexp(1.0, (int)sd.arr.s[sd.dim[0].ne] |
482 |
+ |
- (COLXS+8)); |
483 |
+ |
for (i = sd.dim[0].ne; i--; ) |
484 |
+ |
rvec[i] += c0*f*(sd.arr.s[i] + 0.5); |
485 |
+ |
sd.arr.s += stride; |
486 |
+ |
f = ldexp(1.0, (int)sd.arr.s[sd.dim[0].ne] |
487 |
+ |
- (COLXS+8)); |
488 |
+ |
for (i = sd.dim[0].ne; i--; ) |
489 |
+ |
rvec[i] += c1*f*(sd.arr.s[i] + 0.5); |
490 |
+ |
} else { |
491 |
+ |
sd.arr.c = dp->arr.c + i*stride; |
492 |
+ |
for (i = sd.dim[0].ne; i--; ) |
493 |
+ |
rvec[i] += c0*colrval(sd.arr.c[i],sd.type) |
494 |
+ |
+ c1*colrval(sd.arr.c[i+stride],sd.type); |
495 |
+ |
} |
496 |
+ |
return(0.); /* return value ignored */ |
497 |
+ |
} |
498 |
|
/* get dependent variable */ |
499 |
|
if (dp->nd > 1) { |
500 |
|
if (dp->type == DATATY) { |
501 |
< |
sd.arr.d = dp->arr.d + i*asize; |
502 |
< |
y0 = datavalue(&sd, pt+1); |
503 |
< |
sd.arr.d = dp->arr.d + (i+1)*asize; |
504 |
< |
y1 = datavalue(&sd, pt+1); |
501 |
> |
sd.arr.d = dp->arr.d + i*stride; |
502 |
> |
y0 = data_interp(&sd, pt+1, c0, rvec); |
503 |
> |
sd.arr.d += stride; |
504 |
> |
} else if (dp->type == SPECTY) { |
505 |
> |
sd.arr.s = dp->arr.s + i*stride; |
506 |
> |
y0 = data_interp(&sd, pt+1, c0, rvec); |
507 |
> |
sd.arr.s += stride; |
508 |
|
} else { |
509 |
< |
sd.arr.c = dp->arr.c + i*asize; |
510 |
< |
y0 = datavalue(&sd, pt+1); |
511 |
< |
sd.arr.c = dp->arr.c + (i+1)*asize; |
362 |
< |
y1 = datavalue(&sd, pt+1); |
509 |
> |
sd.arr.c = dp->arr.c + i*stride; |
510 |
> |
y0 = data_interp(&sd, pt+1, c0, rvec); |
511 |
> |
sd.arr.c += stride; |
512 |
|
} |
513 |
< |
} else { |
513 |
> |
y1 = data_interp(&sd, pt+1, c1, rvec); |
514 |
> |
} else { /* end of recursion */ |
515 |
|
if (dp->type == DATATY) { |
516 |
|
y0 = dp->arr.d[i]; |
517 |
|
y1 = dp->arr.d[i+1]; |
518 |
+ |
} else if (dp->type == SPECTY) { |
519 |
+ |
if (dp->arr.s[dp->dim[0].ne]) { |
520 |
+ |
double f = ldexp(1.0, -(COLXS+8) + |
521 |
+ |
(int)dp->arr.s[dp->dim[0].ne]); |
522 |
+ |
y0 = (dp->arr.s[i] + 0.5)*f; |
523 |
+ |
y1 = (dp->arr.s[i+1] + 0.5)*f; |
524 |
+ |
} else |
525 |
+ |
y0 = y1 = 0.0; |
526 |
|
} else { |
527 |
|
y0 = colrval(dp->arr.c[i],dp->type); |
528 |
|
y1 = colrval(dp->arr.c[i+1],dp->type); |
529 |
|
} |
530 |
+ |
y0 *= c0; |
531 |
+ |
y1 *= c1; |
532 |
|
} |
533 |
< |
/* |
534 |
< |
* Extrapolate as far as one division, then |
375 |
< |
* taper off harmonically to zero. |
376 |
< |
*/ |
377 |
< |
if (x > i+2) |
378 |
< |
return( (2*y1-y0)/(x-(i-1)) ); |
533 |
> |
return(y0 + y1); /* coefficients already applied */ |
534 |
> |
} |
535 |
|
|
380 |
– |
if (x < i-1) |
381 |
– |
return( (2*y0-y1)/(i-x) ); |
536 |
|
|
537 |
< |
return( y0*((i+1)-x) + y1*(x-i) ); |
537 |
> |
double |
538 |
> |
datavalue( /* interpolate data value at a point */ |
539 |
> |
DATARRAY *dp, |
540 |
> |
double *pt |
541 |
> |
) |
542 |
> |
{ |
543 |
> |
return(data_interp(dp, pt, 1., NULL)); |
544 |
|
} |
545 |
+ |
|
546 |
+ |
|
547 |
+ |
/* Interpolate final vector corresponding to last dimension in data array */ |
548 |
+ |
DATARRAY * |
549 |
+ |
datavector(DATARRAY *dp, double *pt) |
550 |
+ |
{ |
551 |
+ |
DATARRAY *newdp; |
552 |
+ |
|
553 |
+ |
if (dp->nd < 2) |
554 |
+ |
error(INTERNAL, "datavector() called with 1-D array"); |
555 |
+ |
/* create vector array */ |
556 |
+ |
newdp = (DATARRAY *)malloc(sizeof(DATARRAY) - |
557 |
+ |
(MAXDDIM-1)*sizeof(struct dadim) + |
558 |
+ |
sizeof(DATATYPE)*dp->dim[dp->nd-1].ne); |
559 |
+ |
if (newdp == NULL) |
560 |
+ |
error(SYSTEM, "out of memory in datavector"); |
561 |
+ |
newdp->next = newdp; /* flags us as temp vector */ |
562 |
+ |
newdp->name = dp->name; |
563 |
+ |
newdp->type = DATATY; |
564 |
+ |
newdp->nd = 1; /* vector data goes here */ |
565 |
+ |
newdp->dim[0] = dp->dim[dp->nd-1]; |
566 |
+ |
newdp->arr.d = (DATATYPE *)(newdp->dim + 1); |
567 |
+ |
memset(newdp->arr.d, 0, sizeof(DATATYPE)*newdp->dim[0].ne); |
568 |
+ |
|
569 |
+ |
(void)data_interp(dp, pt, 1., newdp->arr.d); |
570 |
+ |
|
571 |
+ |
return(newdp); /* will be free'd using free() */ |
572 |
+ |
} |
573 |
+ |
|