1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: p_data.c,v 2.9 2014/07/08 18:25:00 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* p_data.c - routine for stored patterns. |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include "ray.h" |
11 |
#include "data.h" |
12 |
#include "func.h" |
13 |
#include "rtotypes.h" |
14 |
|
15 |
/* |
16 |
* A stored pattern can either be brightness, |
17 |
* color, or spectral data. Brightness data is specified as: |
18 |
* |
19 |
* modifier brightdata name |
20 |
* 4+ func dfname vfname v0 v1 .. xf |
21 |
* 0 |
22 |
* n A1 A2 .. |
23 |
* |
24 |
* Color data is specified as: |
25 |
* |
26 |
* modifier colordata name |
27 |
* 8+ rfunc gfunc bfunc rdfname gdfname bdfname vfname v0 v1 .. xf |
28 |
* 0 |
29 |
* n A1 A2 .. |
30 |
* |
31 |
* Color picture data is specified as: |
32 |
* |
33 |
* modifier colorpict name |
34 |
* 7+ rfunc gfunc bfunc pfname vfname vx vy xf |
35 |
* 0 |
36 |
* n A1 A2 .. |
37 |
* |
38 |
* A simple spectrum is specified as: |
39 |
* |
40 |
* modifier spectrum name |
41 |
* 0 |
42 |
* 0 |
43 |
* 5+ nmA nmB s1 s2 s3 .. |
44 |
* |
45 |
* A constant spectrum from a data file is given as: |
46 |
* |
47 |
* modifier specfile name |
48 |
* 1 dfname |
49 |
* 0 |
50 |
* 0 |
51 |
* |
52 |
* Vfname is the name of the file where the variable definitions |
53 |
* can be found. The list of real arguments can be accessed by |
54 |
* definitions in the file. The dfnames are the data file |
55 |
* names. The dimensions of the data files and the number |
56 |
* of variables must match. The funcs take a single argument |
57 |
* for brightdata, and three for colordata and colorpict to produce |
58 |
* interpolated values from the file. The xf is a transform spec |
59 |
* to get from the original coordinates to the current coordinates. |
60 |
*/ |
61 |
|
62 |
|
63 |
int |
64 |
p_bdata( /* interpolate brightness data */ |
65 |
OBJREC *m, |
66 |
RAY *r |
67 |
) |
68 |
{ |
69 |
double bval; |
70 |
double pt[MAXDDIM]; |
71 |
DATARRAY *dp; |
72 |
MFUNC *mf; |
73 |
int i; |
74 |
|
75 |
if (m->oargs.nsargs < 4) |
76 |
objerror(m, USER, "bad # arguments"); |
77 |
dp = getdata(m->oargs.sarg[1]); |
78 |
i = (1 << dp->nd) - 1; |
79 |
mf = getfunc(m, 2, i<<3, 0); |
80 |
setfunc(m, r); |
81 |
errno = 0; |
82 |
for (i = dp->nd; i-- > 0; ) { |
83 |
pt[i] = evalue(mf->ep[i]); |
84 |
if ((errno == EDOM) | (errno == ERANGE)) |
85 |
goto computerr; |
86 |
} |
87 |
bval = datavalue(dp, pt); |
88 |
errno = 0; |
89 |
bval = funvalue(m->oargs.sarg[0], 1, &bval); |
90 |
if ((errno == EDOM) | (errno == ERANGE)) |
91 |
goto computerr; |
92 |
scalescolor(r->pcol, bval); |
93 |
return(0); |
94 |
computerr: |
95 |
objerror(m, WARNING, "compute error"); |
96 |
return(0); |
97 |
} |
98 |
|
99 |
|
100 |
int |
101 |
p_cdata( /* interpolate color data */ |
102 |
OBJREC *m, |
103 |
RAY *r |
104 |
) |
105 |
{ |
106 |
double col[3]; |
107 |
COLOR cval; |
108 |
double pt[MAXDDIM]; |
109 |
int nv; |
110 |
DATARRAY *dp; |
111 |
MFUNC *mf; |
112 |
int i; |
113 |
|
114 |
if (m->oargs.nsargs < 8) |
115 |
objerror(m, USER, "bad # arguments"); |
116 |
dp = getdata(m->oargs.sarg[3]); |
117 |
i = (1 << (nv = dp->nd)) - 1; |
118 |
mf = getfunc(m, 6, i<<7, 0); |
119 |
setfunc(m, r); |
120 |
errno = 0; |
121 |
for (i = 0; i < nv; i++) { |
122 |
pt[i] = evalue(mf->ep[i]); |
123 |
if ((errno == EDOM) | (errno == ERANGE)) |
124 |
goto computerr; |
125 |
} |
126 |
col[0] = datavalue(dp, pt); |
127 |
for (i = 1; i < 3; i++) { |
128 |
dp = getdata(m->oargs.sarg[i+3]); |
129 |
if (dp->nd != nv) |
130 |
objerror(m, USER, "dimension error"); |
131 |
col[i] = datavalue(dp, pt); |
132 |
} |
133 |
errno = 0; |
134 |
for (i = 0; i < 3; i++) |
135 |
if (fundefined(m->oargs.sarg[i]) < 3) |
136 |
colval(cval,i) = funvalue(m->oargs.sarg[i], 1, col+i); |
137 |
else |
138 |
colval(cval,i) = funvalue(m->oargs.sarg[i], 3, col); |
139 |
if ((errno == EDOM) | (errno == ERANGE)) |
140 |
goto computerr; |
141 |
smultcolor(r->pcol, cval); |
142 |
return(0); |
143 |
computerr: |
144 |
objerror(m, WARNING, "compute error"); |
145 |
return(0); |
146 |
} |
147 |
|
148 |
|
149 |
int |
150 |
p_pdata( /* interpolate picture data */ |
151 |
OBJREC *m, |
152 |
RAY *r |
153 |
) |
154 |
{ |
155 |
double col[3]; |
156 |
COLOR cval; |
157 |
double pt[2]; |
158 |
DATARRAY *dp; |
159 |
MFUNC *mf; |
160 |
int i; |
161 |
|
162 |
if (m->oargs.nsargs < 7) |
163 |
objerror(m, USER, "bad # arguments"); |
164 |
mf = getfunc(m, 4, 0x3<<5, 0); |
165 |
setfunc(m, r); |
166 |
errno = 0; |
167 |
pt[1] = evalue(mf->ep[0]); /* y major ordering */ |
168 |
pt[0] = evalue(mf->ep[1]); |
169 |
if ((errno == EDOM) | (errno == ERANGE)) |
170 |
goto computerr; |
171 |
dp = getpict(m->oargs.sarg[3]); |
172 |
for (i = 0; i < 3; i++) |
173 |
col[i] = datavalue(dp+i, pt); |
174 |
errno = 0; |
175 |
for (i = 0; i < 3; i++) |
176 |
if (fundefined(m->oargs.sarg[i]) < 3) |
177 |
colval(cval,i) = funvalue(m->oargs.sarg[i], 1, col+i); |
178 |
else |
179 |
colval(cval,i) = funvalue(m->oargs.sarg[i], 3, col); |
180 |
if ((errno == EDOM) | (errno == ERANGE)) |
181 |
goto computerr; |
182 |
smultcolor(r->pcol, cval); |
183 |
return(0); |
184 |
|
185 |
computerr: |
186 |
objerror(m, WARNING, "compute error"); |
187 |
return(0); |
188 |
} |
189 |
|
190 |
|
191 |
int |
192 |
p_spectrum( /* simple constant spectrum */ |
193 |
OBJREC *m, |
194 |
RAY *r |
195 |
) |
196 |
{ |
197 |
COLORV *scval; |
198 |
|
199 |
if ((scval = (COLORV *)m->os) == NULL) { |
200 |
COLORV *sinp; |
201 |
double hstep; |
202 |
int i; |
203 |
if (m->oargs.nfargs < 5) |
204 |
objerror(m, USER, "bad # arguments"); |
205 |
sinp = (COLORV *)malloc(sizeof(COLORV)*(m->oargs.nfargs-2)); |
206 |
scval = (COLORV *)malloc(sizeof(COLORV)*NCSAMP); |
207 |
if ((sinp == NULL) | (scval == NULL)) |
208 |
objerror(m, SYSTEM, "out of memory"); |
209 |
for (i = m->oargs.nfargs-2; i--; ) |
210 |
sinp[i] = (COLORV)m->oargs.farg[i+2]; |
211 |
hstep = 0.5 * (m->oargs.farg[1] - m->oargs.farg[0]) / |
212 |
(m->oargs.nfargs-3.0); |
213 |
convertscolor(scval, NCSAMP, WLPART[0], WLPART[3], |
214 |
sinp, m->oargs.nfargs-2, |
215 |
m->oargs.farg[0]-hstep, m->oargs.farg[1]+hstep); |
216 |
free(sinp); |
217 |
m->os = (void *)scval; |
218 |
} |
219 |
smultscolor(r->pcol, scval); |
220 |
return(0); |
221 |
} |
222 |
|
223 |
|
224 |
int |
225 |
p_specfile( /* constant spectrum from 1-D data file */ |
226 |
OBJREC *m, |
227 |
RAY *r |
228 |
) |
229 |
{ |
230 |
COLORV *scval; |
231 |
|
232 |
if ((scval = (COLORV *)m->os) == NULL) { |
233 |
DATARRAY *dp; |
234 |
COLORV *sinp; |
235 |
double step; |
236 |
int i; |
237 |
if (m->oargs.nsargs != 1) |
238 |
objerror(m, USER, "bad # arguments"); |
239 |
dp = getdata(m->oargs.sarg[0]); |
240 |
if (dp->nd != 1) |
241 |
objerror(m, USER, "data file must be 1-dimensional"); |
242 |
|
243 |
sinp = (COLORV *)malloc(sizeof(COLORV)*dp->dim[0].ne); |
244 |
scval = (COLORV *)malloc(sizeof(COLORV)*NCSAMP); |
245 |
if ((sinp == NULL) | (scval == NULL)) |
246 |
objerror(m, SYSTEM, "out of memory"); |
247 |
step = dp->dim[0].siz / (dp->dim[0].ne - 1.0); |
248 |
for (i = dp->dim[0].ne; i-- > 0; ) { |
249 |
double wl = dp->dim[0].org + i*step; |
250 |
sinp[i] = (COLORV)datavalue(dp, &wl); |
251 |
} |
252 |
convertscolor(scval, NCSAMP, WLPART[0], WLPART[3], |
253 |
sinp, dp->dim[0].ne, dp->dim[0].org-.5*step, |
254 |
dp->dim[0].org+dp->dim[0].siz+.5*step); |
255 |
free(sinp); |
256 |
m->os = (void *)scval; |
257 |
} |
258 |
smultscolor(r->pcol, scval); |
259 |
return(0); |
260 |
} |