1 |
– |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* data.c - routines dealing with interpolated data. |
9 |
– |
* |
10 |
– |
* 6/4/86 |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "standard.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
+ |
#include "standard.h" |
11 |
+ |
#include "platform.h" |
12 |
|
#include "color.h" |
16 |
– |
|
13 |
|
#include "resolu.h" |
18 |
– |
|
14 |
|
#include "data.h" |
15 |
|
|
16 |
|
/* picture memory usage before warning */ |
17 |
|
#ifndef PSIZWARN |
18 |
< |
#ifdef BIGMEM |
19 |
< |
#define PSIZWARN 3000000 |
18 |
> |
#ifdef SMLMEM |
19 |
> |
#define PSIZWARN 1500000 |
20 |
|
#else |
21 |
< |
#define PSIZWARN 1000000 |
21 |
> |
#define PSIZWARN 5000000 |
22 |
|
#endif |
23 |
|
#endif |
24 |
|
|
29 |
|
#define hash(s) (shash(s)%TABSIZ) |
30 |
|
|
31 |
|
|
37 |
– |
extern char *fgetword(); |
38 |
– |
|
39 |
– |
extern char *libpath; /* library search path */ |
40 |
– |
|
32 |
|
static DATARRAY *dtab[TABSIZ]; /* data array list */ |
33 |
|
|
43 |
– |
static DATARRAY *ptab[TABSIZ]; /* picture list */ |
34 |
|
|
45 |
– |
|
35 |
|
DATARRAY * |
36 |
|
getdata(dname) /* get data array dname */ |
37 |
|
char *dname; |
38 |
|
{ |
50 |
– |
char word[64]; |
39 |
|
char *dfname; |
40 |
|
FILE *fp; |
41 |
|
int asize; |
45 |
|
for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next) |
46 |
|
if (!strcmp(dname, dp->name)) |
47 |
|
return(dp); /* found! */ |
60 |
– |
|
48 |
|
/* |
49 |
|
* If we haven't loaded the data already, we will look |
50 |
|
* for it in the directories specified by the library path. |
65 |
|
* 0 0 ni p0i p1i .. pni |
66 |
|
*/ |
67 |
|
|
68 |
< |
if ((dfname = getpath(dname, libpath, R_OK)) == NULL) { |
68 |
> |
if ((dfname = getpath(dname, getrlibpath(), R_OK)) == NULL) { |
69 |
|
sprintf(errmsg, "cannot find data file \"%s\"", dname); |
70 |
|
error(USER, errmsg); |
71 |
|
} |
85 |
– |
if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) |
86 |
– |
goto memerr; |
87 |
– |
|
88 |
– |
dp->name = savestr(dname); |
89 |
– |
|
72 |
|
if ((fp = fopen(dfname, "r")) == NULL) { |
73 |
|
sprintf(errmsg, "cannot open data file \"%s\"", dfname); |
74 |
|
error(SYSTEM, errmsg); |
75 |
|
} |
76 |
|
/* get dimensions */ |
77 |
< |
if (fgetword(word, sizeof(word), fp) == NULL || !isint(word)) |
77 |
> |
if (fgetval(fp, 'i', (char *)&asize) <= 0) |
78 |
|
goto scanerr; |
79 |
< |
dp->nd = atoi(word); |
98 |
< |
if (dp->nd <= 0 || dp->nd > MAXDDIM) { |
79 |
> |
if (asize <= 0 | asize > MAXDDIM) { |
80 |
|
sprintf(errmsg, "bad number of dimensions for \"%s\"", dname); |
81 |
|
error(USER, errmsg); |
82 |
|
} |
83 |
+ |
if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) |
84 |
+ |
goto memerr; |
85 |
+ |
dp->name = savestr(dname); |
86 |
+ |
dp->type = DATATY; |
87 |
+ |
dp->nd = asize; |
88 |
|
asize = 1; |
89 |
|
for (i = 0; i < dp->nd; i++) { |
90 |
< |
if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word)) |
90 |
> |
if (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0) |
91 |
|
goto scanerr; |
92 |
< |
dp->dim[i].org = atof(word); |
107 |
< |
if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word)) |
92 |
> |
if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0) |
93 |
|
goto scanerr; |
94 |
< |
dp->dim[i].siz = atof(word); |
110 |
< |
if (fgetword(word, sizeof(word), fp) == NULL || !isint(word)) |
94 |
> |
if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0) |
95 |
|
goto scanerr; |
112 |
– |
dp->dim[i].ne = atoi(word); |
96 |
|
if (dp->dim[i].ne < 2) |
97 |
|
goto scanerr; |
98 |
|
asize *= dp->dim[i].ne; |
99 |
|
if ((dp->dim[i].siz -= dp->dim[i].org) == 0) { |
100 |
< |
dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double)); |
100 |
> |
dp->dim[i].p = (DATATYPE *) |
101 |
> |
malloc(dp->dim[i].ne*sizeof(DATATYPE)); |
102 |
|
if (dp->dim[i].p == NULL) |
103 |
|
goto memerr; |
104 |
< |
for (j = 0; j < dp->dim[i].ne; j++) { |
105 |
< |
if (fgetword(word, sizeof(word), fp) == NULL || |
106 |
< |
!isflt(word)) |
104 |
> |
for (j = 0; j < dp->dim[i].ne; j++) |
105 |
> |
if (fgetval(fp, DATATY, |
106 |
> |
(char *)&dp->dim[i].p[j]) <= 0) |
107 |
|
goto scanerr; |
124 |
– |
dp->dim[i].p[j] = atof(word); |
125 |
– |
} |
108 |
|
for (j = 1; j < dp->dim[i].ne-1; j++) |
109 |
|
if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) != |
110 |
|
(dp->dim[i].p[j] < dp->dim[i].p[j+1])) |
115 |
|
} else |
116 |
|
dp->dim[i].p = NULL; |
117 |
|
} |
118 |
< |
if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL) |
118 |
> |
if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL) |
119 |
|
goto memerr; |
120 |
|
|
121 |
< |
for (i = 0; i < asize; i++) { |
122 |
< |
if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word)) |
121 |
> |
for (i = 0; i < asize; i++) |
122 |
> |
if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0) |
123 |
|
goto scanerr; |
142 |
– |
dp->arr[i] = atof(word); |
143 |
– |
} |
124 |
|
fclose(fp); |
125 |
|
i = hash(dname); |
126 |
|
dp->next = dtab[i]; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
< |
static |
138 |
> |
static int |
139 |
|
headaspect(s, iap) /* check string for aspect ratio */ |
140 |
|
char *s; |
141 |
|
double *iap; |
142 |
|
{ |
143 |
+ |
char fmt[32]; |
144 |
+ |
|
145 |
|
if (isaspect(s)) |
146 |
|
*iap *= aspectval(s); |
147 |
+ |
else if (formatval(fmt, s) && !globmatch(PICFMT, fmt)) |
148 |
+ |
*iap = 0.0; |
149 |
+ |
return(0); |
150 |
|
} |
151 |
|
|
152 |
|
|
154 |
|
getpict(pname) /* get picture pname */ |
155 |
|
char *pname; |
156 |
|
{ |
172 |
– |
extern char *libpath; |
157 |
|
double inpaspect; |
158 |
|
char *pfname; |
159 |
|
FILE *fp; |
160 |
< |
COLOR *scanin; |
160 |
> |
COLR *scanin; |
161 |
|
int sl, ns; |
162 |
|
RESOLU inpres; |
163 |
|
FLOAT loc[2]; |
165 |
|
register int x, i; |
166 |
|
register DATARRAY *pp; |
167 |
|
/* look for array in list */ |
168 |
< |
for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next) |
168 |
> |
for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next) |
169 |
|
if (!strcmp(pname, pp->name)) |
170 |
|
return(pp); /* found! */ |
171 |
|
|
172 |
< |
if ((pfname = getpath(pname, libpath, R_OK)) == NULL) { |
172 |
> |
if ((pfname = getpath(pname, getrlibpath(), R_OK)) == NULL) { |
173 |
|
sprintf(errmsg, "cannot find picture file \"%s\"", pname); |
174 |
|
error(USER, errmsg); |
175 |
|
} |
176 |
< |
if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL) |
176 |
> |
if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL) |
177 |
|
goto memerr; |
178 |
|
|
179 |
< |
pp[0].name = |
196 |
< |
pp[1].name = |
197 |
< |
pp[2].name = savestr(pname); |
179 |
> |
pp[0].name = savestr(pname); |
180 |
|
|
181 |
|
if ((fp = fopen(pfname, "r")) == NULL) { |
182 |
|
sprintf(errmsg, "cannot open picture file \"%s\"", pfname); |
183 |
|
error(SYSTEM, errmsg); |
184 |
|
} |
185 |
< |
#ifdef MSDOS |
204 |
< |
setmode(fileno(fp), O_BINARY); |
205 |
< |
#endif |
185 |
> |
SET_FILE_BINARY(fp); |
186 |
|
/* get dimensions */ |
187 |
|
inpaspect = 1.0; |
188 |
< |
getheader(fp, headaspect, &inpaspect); |
189 |
< |
if (!fgetsresolu(&inpres, fp)) |
188 |
> |
getheader(fp, headaspect, (char *)&inpaspect); |
189 |
> |
if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp)) |
190 |
|
goto readerr; |
191 |
+ |
pp[0].nd = 2; |
192 |
+ |
pp[0].dim[0].ne = inpres.yr; |
193 |
+ |
pp[0].dim[1].ne = inpres.xr; |
194 |
+ |
pp[0].dim[0].org = |
195 |
+ |
pp[0].dim[1].org = 0.0; |
196 |
+ |
if (inpres.xr <= inpres.yr*inpaspect) { |
197 |
+ |
pp[0].dim[0].siz = inpaspect * |
198 |
+ |
(double)inpres.yr/inpres.xr; |
199 |
+ |
pp[0].dim[1].siz = 1.0; |
200 |
+ |
} else { |
201 |
+ |
pp[0].dim[0].siz = 1.0; |
202 |
+ |
pp[0].dim[1].siz = (double)inpres.xr/inpres.yr / |
203 |
+ |
inpaspect; |
204 |
+ |
} |
205 |
+ |
pp[0].dim[0].p = pp[0].dim[1].p = NULL; |
206 |
+ |
sl = scanlen(&inpres); /* allocate array */ |
207 |
+ |
ns = numscans(&inpres); |
208 |
+ |
i = ns*sl*sizeof(COLR); |
209 |
|
#if PSIZWARN |
210 |
< |
/* check memory usage */ |
213 |
< |
i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr; |
214 |
< |
if (i > PSIZWARN) { |
210 |
> |
if (i > PSIZWARN) { /* memory warning */ |
211 |
|
sprintf(errmsg, "picture file \"%s\" using %d bytes of memory", |
212 |
|
pname, i); |
213 |
|
error(WARNING, errmsg); |
214 |
|
} |
215 |
|
#endif |
216 |
< |
for (i = 0; i < 3; i++) { |
217 |
< |
pp[i].nd = 2; |
222 |
< |
pp[i].dim[0].ne = inpres.yr; |
223 |
< |
pp[i].dim[1].ne = inpres.xr; |
224 |
< |
pp[i].dim[0].org = |
225 |
< |
pp[i].dim[1].org = 0.0; |
226 |
< |
if (inpres.xr <= inpres.yr*inpaspect) { |
227 |
< |
pp[i].dim[0].siz = inpaspect * |
228 |
< |
(double)inpres.yr/inpres.xr; |
229 |
< |
pp[i].dim[1].siz = 1.0; |
230 |
< |
} else { |
231 |
< |
pp[i].dim[0].siz = 1.0; |
232 |
< |
pp[i].dim[1].siz = (double)inpres.xr/inpres.yr / |
233 |
< |
inpaspect; |
234 |
< |
} |
235 |
< |
pp[i].dim[0].p = pp[i].dim[1].p = NULL; |
236 |
< |
pp[i].arr = (DATATYPE *) |
237 |
< |
malloc(inpres.xr*inpres.yr*sizeof(DATATYPE)); |
238 |
< |
if (pp[i].arr == NULL) |
239 |
< |
goto memerr; |
240 |
< |
} |
216 |
> |
if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL) |
217 |
> |
goto memerr; |
218 |
|
/* load picture */ |
219 |
< |
sl = scanlen(&inpres); |
243 |
< |
ns = numscans(&inpres); |
244 |
< |
if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL) |
219 |
> |
if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL) |
220 |
|
goto memerr; |
221 |
|
for (y = 0; y < ns; y++) { |
222 |
< |
if (freadscan(scanin, sl, fp) < 0) |
222 |
> |
if (freadcolrs(scanin, sl, fp) < 0) |
223 |
|
goto readerr; |
224 |
|
for (x = 0; x < sl; x++) { |
225 |
|
pix2loc(loc, &inpres, x, y); |
226 |
|
i = (int)(loc[1]*inpres.yr)*inpres.xr + |
227 |
|
(int)(loc[0]*inpres.xr); |
228 |
< |
pp[0].arr[i] = colval(scanin[x],RED); |
254 |
< |
pp[1].arr[i] = colval(scanin[x],GRN); |
255 |
< |
pp[2].arr[i] = colval(scanin[x],BLU); |
228 |
> |
copycolr(pp[0].arr.c[i], scanin[x]); |
229 |
|
} |
230 |
|
} |
231 |
< |
free((char *)scanin); |
231 |
> |
free((void *)scanin); |
232 |
|
fclose(fp); |
233 |
|
i = hash(pname); |
234 |
< |
pp[0].next = |
235 |
< |
pp[1].next = |
236 |
< |
pp[2].next = ptab[i]; |
237 |
< |
return(ptab[i] = pp); |
234 |
> |
pp[0].next = dtab[i]; /* link into picture list */ |
235 |
> |
copystruct(&pp[1], &pp[0]); |
236 |
> |
copystruct(&pp[2], &pp[0]); |
237 |
> |
pp[0].type = RED; /* differentiate RGB records */ |
238 |
> |
pp[1].type = GRN; |
239 |
> |
pp[2].type = BLU; |
240 |
> |
return(dtab[i] = pp); |
241 |
|
|
242 |
|
memerr: |
243 |
|
error(SYSTEM, "out of memory in getpict"); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
< |
freedata(dname) /* free memory associated with dname */ |
251 |
< |
char *dname; |
250 |
> |
void |
251 |
> |
freedata(dta) /* release data array reference */ |
252 |
> |
DATARRAY *dta; |
253 |
|
{ |
254 |
|
DATARRAY head; |
255 |
|
int hval, nents; |
256 |
< |
register DATARRAY *dp, *dpl; |
256 |
> |
register DATARRAY *dpl, *dp; |
257 |
|
register int i; |
258 |
|
|
259 |
< |
if (dname == NULL) { /* free all if NULL */ |
259 |
> |
if (dta == NULL) { /* free all if NULL */ |
260 |
|
hval = 0; nents = TABSIZ; |
261 |
|
} else { |
262 |
< |
hval = hash(dname); nents = 1; |
262 |
> |
hval = hash(dta->name); nents = 1; |
263 |
|
} |
264 |
|
while (nents--) { |
265 |
|
head.next = dtab[hval]; |
266 |
|
dpl = &head; |
267 |
|
while ((dp = dpl->next) != NULL) |
268 |
< |
if (dname == NULL || !strcmp(dname, dp->name)) { |
268 |
> |
if ((dta == NULL | dta == dp)) { |
269 |
|
dpl->next = dp->next; |
270 |
< |
free((char *)dp->arr); |
270 |
> |
if (dp->type == DATATY) |
271 |
> |
free((void *)dp->arr.d); |
272 |
> |
else |
273 |
> |
free((void *)dp->arr.c); |
274 |
|
for (i = 0; i < dp->nd; i++) |
275 |
|
if (dp->dim[i].p != NULL) |
276 |
< |
free((char *)dp->dim[i].p); |
276 |
> |
free((void *)dp->dim[i].p); |
277 |
|
freestr(dp->name); |
278 |
< |
free((char *)dp); |
278 |
> |
free((void *)dp); |
279 |
|
} else |
280 |
|
dpl = dp; |
281 |
|
dtab[hval++] = head.next; |
283 |
|
} |
284 |
|
|
285 |
|
|
306 |
– |
freepict(pname) /* free memory associated with pname */ |
307 |
– |
char *pname; |
308 |
– |
{ |
309 |
– |
DATARRAY head; |
310 |
– |
int hval, nents; |
311 |
– |
register DATARRAY *pp, *ppl; |
312 |
– |
|
313 |
– |
if (pname == NULL) { /* free all if NULL */ |
314 |
– |
hval = 0; nents = TABSIZ; |
315 |
– |
} else { |
316 |
– |
hval = hash(pname); nents = 1; |
317 |
– |
} |
318 |
– |
while (nents--) { |
319 |
– |
head.next = ptab[hval]; |
320 |
– |
ppl = &head; |
321 |
– |
while ((pp = ppl->next) != NULL) |
322 |
– |
if (pname == NULL || !strcmp(pname, pp->name)) { |
323 |
– |
ppl->next = pp->next; |
324 |
– |
free((char *)pp[0].arr); |
325 |
– |
free((char *)pp[1].arr); |
326 |
– |
free((char *)pp[2].arr); |
327 |
– |
freestr(pp[0].name); |
328 |
– |
free((char *)pp); |
329 |
– |
} else |
330 |
– |
ppl = pp; |
331 |
– |
ptab[hval++] = head.next; |
332 |
– |
} |
333 |
– |
} |
334 |
– |
|
335 |
– |
|
286 |
|
double |
287 |
|
datavalue(dp, pt) /* interpolate data value at a point */ |
288 |
|
register DATARRAY *dp; |
292 |
|
int asize; |
293 |
|
int lower, upper; |
294 |
|
register int i; |
295 |
< |
double x, y, y0, y1; |
295 |
> |
double x, y0, y1; |
296 |
|
/* set up dimensions for recursion */ |
297 |
< |
sd.nd = dp->nd - 1; |
298 |
< |
asize = 1; |
299 |
< |
for (i = 0; i < sd.nd; i++) { |
300 |
< |
sd.dim[i].org = dp->dim[i+1].org; |
301 |
< |
sd.dim[i].siz = dp->dim[i+1].siz; |
302 |
< |
sd.dim[i].p = dp->dim[i+1].p; |
303 |
< |
asize *= sd.dim[i].ne = dp->dim[i+1].ne; |
297 |
> |
if (dp->nd > 1) { |
298 |
> |
sd.name = dp->name; |
299 |
> |
sd.type = dp->type; |
300 |
> |
sd.nd = dp->nd - 1; |
301 |
> |
asize = 1; |
302 |
> |
for (i = 0; i < sd.nd; i++) { |
303 |
> |
sd.dim[i].org = dp->dim[i+1].org; |
304 |
> |
sd.dim[i].siz = dp->dim[i+1].siz; |
305 |
> |
sd.dim[i].p = dp->dim[i+1].p; |
306 |
> |
asize *= sd.dim[i].ne = dp->dim[i+1].ne; |
307 |
> |
} |
308 |
|
} |
309 |
|
/* get independent variable */ |
310 |
|
if (dp->dim[0].p == NULL) { /* evenly spaced points */ |
311 |
|
x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz; |
312 |
< |
x = x * (dp->dim[0].ne - 1); |
312 |
> |
x *= (double)(dp->dim[0].ne - 1); |
313 |
|
i = x; |
314 |
|
if (i < 0) |
315 |
|
i = 0; |
336 |
|
(dp->dim[0].p[i+1] - dp->dim[0].p[i]); |
337 |
|
} |
338 |
|
/* get dependent variable */ |
339 |
< |
if (dp->nd == 1) { |
340 |
< |
y0 = dp->arr[i]; |
341 |
< |
y1 = dp->arr[i+1]; |
339 |
> |
if (dp->nd > 1) { |
340 |
> |
if (dp->type == DATATY) { |
341 |
> |
sd.arr.d = dp->arr.d + i*asize; |
342 |
> |
y0 = datavalue(&sd, pt+1); |
343 |
> |
sd.arr.d = dp->arr.d + (i+1)*asize; |
344 |
> |
y1 = datavalue(&sd, pt+1); |
345 |
> |
} else { |
346 |
> |
sd.arr.c = dp->arr.c + i*asize; |
347 |
> |
y0 = datavalue(&sd, pt+1); |
348 |
> |
sd.arr.c = dp->arr.c + (i+1)*asize; |
349 |
> |
y1 = datavalue(&sd, pt+1); |
350 |
> |
} |
351 |
|
} else { |
352 |
< |
sd.arr = &dp->arr[i*asize]; |
353 |
< |
y0 = datavalue(&sd, pt+1); |
354 |
< |
sd.arr = &dp->arr[(i+1)*asize]; |
355 |
< |
y1 = datavalue(&sd, pt+1); |
352 |
> |
if (dp->type == DATATY) { |
353 |
> |
y0 = dp->arr.d[i]; |
354 |
> |
y1 = dp->arr.d[i+1]; |
355 |
> |
} else { |
356 |
> |
y0 = colrval(dp->arr.c[i],dp->type); |
357 |
> |
y1 = colrval(dp->arr.c[i+1],dp->type); |
358 |
> |
} |
359 |
|
} |
360 |
|
/* |
361 |
|
* Extrapolate as far as one division, then |
362 |
|
* taper off harmonically to zero. |
363 |
|
*/ |
364 |
|
if (x > i+2) |
365 |
< |
y = (2*y1-y0)/(x-i-1); |
400 |
< |
else if (x < i-1) |
401 |
< |
y = (2*y0-y1)/(i-x); |
402 |
< |
else |
403 |
< |
y = y0*((i+1)-x) + y1*(x-i); |
365 |
> |
return( (2*y1-y0)/(x-(i-1)) ); |
366 |
|
|
367 |
< |
return(y); |
367 |
> |
if (x < i-1) |
368 |
> |
return( (2*y0-y1)/(i-x) ); |
369 |
> |
|
370 |
> |
return( y0*((i+1)-x) + y1*(x-i) ); |
371 |
|
} |