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