1 |
greg |
1.1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Context handlers |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <stdio.h> |
12 |
greg |
1.2 |
#include <math.h> |
13 |
greg |
1.1 |
#include <string.h> |
14 |
|
|
#include "parser.h" |
15 |
|
|
#include "lookup.h" |
16 |
|
|
|
17 |
|
|
/* default context values */ |
18 |
|
|
static C_COLOR c_dfcolor = C_DEFCOLOR; |
19 |
|
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL; |
20 |
|
|
static C_VERTEX c_dfvertex = C_DEFVERTEX; |
21 |
|
|
|
22 |
|
|
/* the unnamed contexts */ |
23 |
|
|
static C_COLOR c_uncolor = C_DEFCOLOR; |
24 |
|
|
static C_MATERIAL c_unmaterial = C_DEFMATERIAL; |
25 |
|
|
static C_VERTEX c_unvertex = C_DEFVERTEX; |
26 |
|
|
|
27 |
|
|
/* the current contexts */ |
28 |
|
|
C_COLOR *c_ccolor = &c_uncolor; |
29 |
greg |
1.12 |
char *c_ccname = NULL; |
30 |
greg |
1.1 |
C_MATERIAL *c_cmaterial = &c_unmaterial; |
31 |
greg |
1.12 |
char *c_cmname = NULL; |
32 |
greg |
1.1 |
C_VERTEX *c_cvertex = &c_unvertex; |
33 |
greg |
1.12 |
char *c_cvname = NULL; |
34 |
greg |
1.1 |
|
35 |
greg |
1.3 |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
36 |
|
|
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
37 |
|
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
38 |
greg |
1.1 |
|
39 |
greg |
1.5 |
/* CIE 1931 Standard Observer */ |
40 |
|
|
static C_COLOR cie_xf = C_CIEX; |
41 |
|
|
static C_COLOR cie_yf = C_CIEY; |
42 |
|
|
static C_COLOR cie_zf = C_CIEZ; |
43 |
greg |
1.1 |
|
44 |
greg |
1.5 |
static int setspectrum(); |
45 |
greg |
1.18 |
static int setbbtemp(); |
46 |
greg |
1.5 |
static void mixcolors(); |
47 |
|
|
|
48 |
|
|
|
49 |
greg |
1.1 |
int |
50 |
|
|
c_hcolor(ac, av) /* handle color entity */ |
51 |
|
|
int ac; |
52 |
|
|
register char **av; |
53 |
|
|
{ |
54 |
greg |
1.5 |
double w, wsum; |
55 |
|
|
register int i; |
56 |
greg |
1.1 |
register LUENT *lp; |
57 |
|
|
|
58 |
|
|
switch (mg_entity(av[0])) { |
59 |
|
|
case MG_E_COLOR: /* get/set color context */ |
60 |
greg |
1.4 |
if (ac > 4) |
61 |
|
|
return(MG_EARGC); |
62 |
greg |
1.1 |
if (ac == 1) { /* set unnamed color context */ |
63 |
|
|
c_uncolor = c_dfcolor; |
64 |
|
|
c_ccolor = &c_uncolor; |
65 |
greg |
1.12 |
c_ccname = NULL; |
66 |
greg |
1.1 |
return(MG_OK); |
67 |
|
|
} |
68 |
|
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
69 |
|
|
if (lp == NULL) |
70 |
|
|
return(MG_EMEM); |
71 |
greg |
1.14 |
c_ccname = lp->key; |
72 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data; |
73 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
74 |
greg |
1.9 |
if (c_ccolor == NULL) |
75 |
greg |
1.1 |
return(MG_EUNDEF); |
76 |
|
|
return(MG_OK); |
77 |
|
|
} |
78 |
|
|
if (av[2][0] != '=' || av[2][1]) |
79 |
|
|
return(MG_ETYPE); |
80 |
greg |
1.9 |
if (c_ccolor == NULL) { /* create new color context */ |
81 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
82 |
|
|
if (lp->key == NULL) |
83 |
|
|
return(MG_EMEM); |
84 |
|
|
strcpy(lp->key, av[1]); |
85 |
|
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
86 |
|
|
if (lp->data == NULL) |
87 |
|
|
return(MG_EMEM); |
88 |
greg |
1.14 |
c_ccname = lp->key; |
89 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data; |
90 |
|
|
c_ccolor->clock = 0; |
91 |
greg |
1.1 |
} |
92 |
greg |
1.10 |
i = c_ccolor->clock; |
93 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
94 |
|
|
*c_ccolor = c_dfcolor; |
95 |
greg |
1.9 |
c_ccolor->clock = i + 1; |
96 |
greg |
1.1 |
return(MG_OK); |
97 |
|
|
} |
98 |
|
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
99 |
|
|
if (lp == NULL) |
100 |
|
|
return(MG_EMEM); |
101 |
|
|
if (lp->data == NULL) |
102 |
|
|
return(MG_EUNDEF); |
103 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
104 |
greg |
1.9 |
c_ccolor->clock = i + 1; |
105 |
greg |
1.1 |
return(MG_OK); |
106 |
|
|
case MG_E_CXY: /* assign CIE XY value */ |
107 |
|
|
if (ac != 3) |
108 |
|
|
return(MG_EARGC); |
109 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
110 |
greg |
1.1 |
return(MG_ETYPE); |
111 |
|
|
c_ccolor->cx = atof(av[1]); |
112 |
|
|
c_ccolor->cy = atof(av[2]); |
113 |
greg |
1.5 |
c_ccolor->flags = C_CDXY|C_CSXY; |
114 |
greg |
1.1 |
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
115 |
|
|
c_ccolor->cx + c_ccolor->cy > 1.) |
116 |
|
|
return(MG_EILL); |
117 |
greg |
1.8 |
c_ccolor->clock++; |
118 |
greg |
1.1 |
return(MG_OK); |
119 |
greg |
1.5 |
case MG_E_CSPEC: /* assign spectral values */ |
120 |
|
|
if (ac < 5) |
121 |
|
|
return(MG_EARGC); |
122 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
123 |
greg |
1.5 |
return(MG_ETYPE); |
124 |
greg |
1.17 |
return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]), |
125 |
greg |
1.5 |
ac-3, av+3)); |
126 |
greg |
1.18 |
case MG_E_CCT: /* assign black body spectrum */ |
127 |
|
|
if (ac != 2) |
128 |
|
|
return(MG_EARGC); |
129 |
|
|
if (!isflt(av[1])) |
130 |
|
|
return(MG_ETYPE); |
131 |
|
|
return(setbbtemp(c_ccolor, atof(av[1]))); |
132 |
greg |
1.5 |
case MG_E_CMIX: /* mix colors */ |
133 |
|
|
if (ac < 5 || (ac-1)%2) |
134 |
|
|
return(MG_EARGC); |
135 |
|
|
if (!isflt(av[1])) |
136 |
|
|
return(MG_ETYPE); |
137 |
|
|
wsum = atof(av[1]); |
138 |
|
|
if (wsum < 0.) |
139 |
|
|
return(MG_EILL); |
140 |
|
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
141 |
|
|
return(MG_EMEM); |
142 |
|
|
if (lp->data == NULL) |
143 |
|
|
return(MG_EUNDEF); |
144 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
145 |
|
|
for (i = 3; i < ac; i += 2) { |
146 |
|
|
if (!isflt(av[i])) |
147 |
|
|
return(MG_ETYPE); |
148 |
|
|
w = atof(av[i]); |
149 |
|
|
if (w < 0.) |
150 |
|
|
return(MG_EILL); |
151 |
|
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
152 |
|
|
return(MG_EMEM); |
153 |
|
|
if (lp->data == NULL) |
154 |
|
|
return(MG_EUNDEF); |
155 |
|
|
mixcolors(c_ccolor, wsum, c_ccolor, |
156 |
|
|
w, (C_COLOR *)lp->data); |
157 |
|
|
wsum += w; |
158 |
|
|
} |
159 |
greg |
1.10 |
c_ccolor->clock++; |
160 |
greg |
1.5 |
return(MG_OK); |
161 |
greg |
1.1 |
} |
162 |
|
|
return(MG_EUNK); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
|
166 |
|
|
int |
167 |
|
|
c_hmaterial(ac, av) /* handle material entity */ |
168 |
|
|
int ac; |
169 |
|
|
register char **av; |
170 |
|
|
{ |
171 |
greg |
1.9 |
int i; |
172 |
greg |
1.1 |
register LUENT *lp; |
173 |
|
|
|
174 |
|
|
switch (mg_entity(av[0])) { |
175 |
|
|
case MG_E_MATERIAL: /* get/set material context */ |
176 |
greg |
1.4 |
if (ac > 4) |
177 |
|
|
return(MG_EARGC); |
178 |
greg |
1.1 |
if (ac == 1) { /* set unnamed material context */ |
179 |
|
|
c_unmaterial = c_dfmaterial; |
180 |
|
|
c_cmaterial = &c_unmaterial; |
181 |
greg |
1.12 |
c_cmname = NULL; |
182 |
greg |
1.1 |
return(MG_OK); |
183 |
|
|
} |
184 |
|
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
185 |
|
|
if (lp == NULL) |
186 |
|
|
return(MG_EMEM); |
187 |
greg |
1.14 |
c_cmname = lp->key; |
188 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data; |
189 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
190 |
greg |
1.9 |
if (c_cmaterial == NULL) |
191 |
greg |
1.1 |
return(MG_EUNDEF); |
192 |
|
|
return(MG_OK); |
193 |
|
|
} |
194 |
|
|
if (av[2][0] != '=' || av[2][1]) |
195 |
|
|
return(MG_ETYPE); |
196 |
greg |
1.9 |
if (c_cmaterial == NULL) { /* create new material */ |
197 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
198 |
|
|
if (lp->key == NULL) |
199 |
|
|
return(MG_EMEM); |
200 |
|
|
strcpy(lp->key, av[1]); |
201 |
|
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
202 |
|
|
if (lp->data == NULL) |
203 |
|
|
return(MG_EMEM); |
204 |
greg |
1.14 |
c_cmname = lp->key; |
205 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data; |
206 |
|
|
c_cmaterial->clock = 0; |
207 |
greg |
1.1 |
} |
208 |
greg |
1.10 |
i = c_cmaterial->clock; |
209 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
210 |
|
|
*c_cmaterial = c_dfmaterial; |
211 |
greg |
1.9 |
c_cmaterial->clock = i + 1; |
212 |
greg |
1.1 |
return(MG_OK); |
213 |
|
|
} |
214 |
|
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
215 |
|
|
if (lp == NULL) |
216 |
|
|
return(MG_EMEM); |
217 |
|
|
if (lp->data == NULL) |
218 |
|
|
return(MG_EUNDEF); |
219 |
|
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
220 |
greg |
1.9 |
c_cmaterial->clock = i + 1; |
221 |
greg |
1.1 |
return(MG_OK); |
222 |
|
|
case MG_E_RD: /* set diffuse reflectance */ |
223 |
|
|
if (ac != 2) |
224 |
|
|
return(MG_EARGC); |
225 |
|
|
if (!isflt(av[1])) |
226 |
|
|
return(MG_ETYPE); |
227 |
|
|
c_cmaterial->rd = atof(av[1]); |
228 |
|
|
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.) |
229 |
|
|
return(MG_EILL); |
230 |
|
|
c_cmaterial->rd_c = *c_ccolor; |
231 |
greg |
1.2 |
c_cmaterial->clock++; |
232 |
greg |
1.1 |
return(MG_OK); |
233 |
|
|
case MG_E_ED: /* set diffuse emittance */ |
234 |
|
|
if (ac != 2) |
235 |
|
|
return(MG_EARGC); |
236 |
|
|
if (!isflt(av[1])) |
237 |
|
|
return(MG_ETYPE); |
238 |
|
|
c_cmaterial->ed = atof(av[1]); |
239 |
|
|
if (c_cmaterial->ed < 0.) |
240 |
|
|
return(MG_EILL); |
241 |
|
|
c_cmaterial->ed_c = *c_ccolor; |
242 |
greg |
1.2 |
c_cmaterial->clock++; |
243 |
greg |
1.1 |
return(MG_OK); |
244 |
|
|
case MG_E_TD: /* set diffuse transmittance */ |
245 |
|
|
if (ac != 2) |
246 |
|
|
return(MG_EARGC); |
247 |
|
|
if (!isflt(av[1])) |
248 |
|
|
return(MG_ETYPE); |
249 |
|
|
c_cmaterial->td = atof(av[1]); |
250 |
|
|
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
251 |
|
|
return(MG_EILL); |
252 |
|
|
c_cmaterial->td_c = *c_ccolor; |
253 |
greg |
1.8 |
c_cmaterial->clock++; |
254 |
greg |
1.1 |
return(MG_OK); |
255 |
|
|
case MG_E_RS: /* set specular reflectance */ |
256 |
|
|
if (ac != 3) |
257 |
|
|
return(MG_EARGC); |
258 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
259 |
greg |
1.1 |
return(MG_ETYPE); |
260 |
|
|
c_cmaterial->rs = atof(av[1]); |
261 |
|
|
c_cmaterial->rs_a = atof(av[2]); |
262 |
|
|
if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. | |
263 |
|
|
c_cmaterial->rs_a < 0.) |
264 |
|
|
return(MG_EILL); |
265 |
|
|
c_cmaterial->rs_c = *c_ccolor; |
266 |
greg |
1.2 |
c_cmaterial->clock++; |
267 |
greg |
1.1 |
return(MG_OK); |
268 |
|
|
case MG_E_TS: /* set specular transmittance */ |
269 |
|
|
if (ac != 3) |
270 |
|
|
return(MG_EARGC); |
271 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2])) |
272 |
greg |
1.1 |
return(MG_ETYPE); |
273 |
|
|
c_cmaterial->ts = atof(av[1]); |
274 |
|
|
c_cmaterial->ts_a = atof(av[2]); |
275 |
|
|
if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. | |
276 |
|
|
c_cmaterial->ts_a < 0.) |
277 |
|
|
return(MG_EILL); |
278 |
|
|
c_cmaterial->ts_c = *c_ccolor; |
279 |
greg |
1.2 |
c_cmaterial->clock++; |
280 |
greg |
1.1 |
return(MG_OK); |
281 |
greg |
1.13 |
case MG_E_SIDES: /* set number of sides */ |
282 |
|
|
if (ac != 2) |
283 |
|
|
return(MG_EARGC); |
284 |
|
|
if (!isint(av[1])) |
285 |
|
|
return(MG_ETYPE); |
286 |
|
|
i = atoi(av[1]); |
287 |
|
|
if (i == 1) |
288 |
|
|
c_cmaterial->sided = 1; |
289 |
|
|
else if (i == 2) |
290 |
|
|
c_cmaterial->sided = 0; |
291 |
|
|
else |
292 |
|
|
return(MG_EILL); |
293 |
|
|
c_cmaterial->clock++; |
294 |
|
|
return(MG_OK); |
295 |
greg |
1.1 |
} |
296 |
|
|
return(MG_EUNK); |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
|
300 |
|
|
int |
301 |
|
|
c_hvertex(ac, av) /* handle a vertex entity */ |
302 |
|
|
int ac; |
303 |
|
|
register char **av; |
304 |
|
|
{ |
305 |
greg |
1.9 |
int i; |
306 |
greg |
1.1 |
register LUENT *lp; |
307 |
|
|
|
308 |
|
|
switch (mg_entity(av[0])) { |
309 |
|
|
case MG_E_VERTEX: /* get/set vertex context */ |
310 |
greg |
1.4 |
if (ac > 4) |
311 |
|
|
return(MG_EARGC); |
312 |
greg |
1.1 |
if (ac == 1) { /* set unnamed vertex context */ |
313 |
|
|
c_unvertex = c_dfvertex; |
314 |
|
|
c_cvertex = &c_unvertex; |
315 |
greg |
1.12 |
c_cvname = NULL; |
316 |
greg |
1.1 |
return(MG_OK); |
317 |
|
|
} |
318 |
|
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
319 |
|
|
if (lp == NULL) |
320 |
|
|
return(MG_EMEM); |
321 |
greg |
1.14 |
c_cvname = lp->key; |
322 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data; |
323 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
324 |
greg |
1.9 |
if (c_cvertex == NULL) |
325 |
greg |
1.1 |
return(MG_EUNDEF); |
326 |
|
|
return(MG_OK); |
327 |
|
|
} |
328 |
|
|
if (av[2][0] != '=' || av[2][1]) |
329 |
|
|
return(MG_ETYPE); |
330 |
greg |
1.9 |
if (c_cvertex == NULL) { /* create new vertex context */ |
331 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
332 |
|
|
if (lp->key == NULL) |
333 |
|
|
return(MG_EMEM); |
334 |
|
|
strcpy(lp->key, av[1]); |
335 |
|
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
336 |
|
|
if (lp->data == NULL) |
337 |
|
|
return(MG_EMEM); |
338 |
greg |
1.14 |
c_cvname = lp->key; |
339 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data; |
340 |
greg |
1.1 |
} |
341 |
greg |
1.10 |
i = c_cvertex->clock; |
342 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
343 |
|
|
*c_cvertex = c_dfvertex; |
344 |
greg |
1.9 |
c_cvertex->clock = i + 1; |
345 |
greg |
1.1 |
return(MG_OK); |
346 |
|
|
} |
347 |
|
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
348 |
|
|
if (lp == NULL) |
349 |
|
|
return(MG_EMEM); |
350 |
|
|
if (lp->data == NULL) |
351 |
|
|
return(MG_EUNDEF); |
352 |
|
|
*c_cvertex = *(C_VERTEX *)lp->data; |
353 |
greg |
1.9 |
c_cvertex->clock = i + 1; |
354 |
greg |
1.1 |
return(MG_OK); |
355 |
|
|
case MG_E_POINT: /* set point */ |
356 |
|
|
if (ac != 4) |
357 |
|
|
return(MG_EARGC); |
358 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
359 |
greg |
1.1 |
return(MG_ETYPE); |
360 |
|
|
c_cvertex->p[0] = atof(av[1]); |
361 |
|
|
c_cvertex->p[1] = atof(av[2]); |
362 |
|
|
c_cvertex->p[2] = atof(av[3]); |
363 |
greg |
1.8 |
c_cvertex->clock++; |
364 |
greg |
1.1 |
return(MG_OK); |
365 |
|
|
case MG_E_NORMAL: /* set normal */ |
366 |
|
|
if (ac != 4) |
367 |
|
|
return(MG_EARGC); |
368 |
greg |
1.18 |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
369 |
greg |
1.1 |
return(MG_ETYPE); |
370 |
|
|
c_cvertex->n[0] = atof(av[1]); |
371 |
|
|
c_cvertex->n[1] = atof(av[2]); |
372 |
|
|
c_cvertex->n[2] = atof(av[3]); |
373 |
|
|
(void)normalize(c_cvertex->n); |
374 |
greg |
1.8 |
c_cvertex->clock++; |
375 |
greg |
1.1 |
return(MG_OK); |
376 |
|
|
} |
377 |
|
|
return(MG_EUNK); |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
|
381 |
|
|
void |
382 |
|
|
c_clearall() /* empty context tables */ |
383 |
|
|
{ |
384 |
|
|
c_uncolor = c_dfcolor; |
385 |
|
|
c_ccolor = &c_uncolor; |
386 |
greg |
1.15 |
c_ccname = NULL; |
387 |
greg |
1.3 |
lu_done(&clr_tab); |
388 |
greg |
1.1 |
c_unmaterial = c_dfmaterial; |
389 |
|
|
c_cmaterial = &c_unmaterial; |
390 |
greg |
1.15 |
c_cmname = NULL; |
391 |
greg |
1.3 |
lu_done(&mat_tab); |
392 |
greg |
1.1 |
c_unvertex = c_dfvertex; |
393 |
|
|
c_cvertex = &c_unvertex; |
394 |
greg |
1.15 |
c_cvname = NULL; |
395 |
greg |
1.3 |
lu_done(&vtx_tab); |
396 |
greg |
1.1 |
} |
397 |
|
|
|
398 |
|
|
|
399 |
greg |
1.11 |
C_MATERIAL * |
400 |
|
|
c_getmaterial(name) /* get a named material */ |
401 |
|
|
char *name; |
402 |
|
|
{ |
403 |
|
|
register LUENT *lp; |
404 |
|
|
|
405 |
|
|
if ((lp = lu_find(&mat_tab, name)) == NULL) |
406 |
|
|
return(NULL); |
407 |
|
|
return((C_MATERIAL *)lp->data); |
408 |
|
|
} |
409 |
|
|
|
410 |
|
|
|
411 |
greg |
1.1 |
C_VERTEX * |
412 |
|
|
c_getvert(name) /* get a named vertex */ |
413 |
|
|
char *name; |
414 |
|
|
{ |
415 |
|
|
register LUENT *lp; |
416 |
|
|
|
417 |
|
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
418 |
|
|
return(NULL); |
419 |
|
|
return((C_VERTEX *)lp->data); |
420 |
greg |
1.5 |
} |
421 |
|
|
|
422 |
|
|
|
423 |
greg |
1.7 |
C_COLOR * |
424 |
|
|
c_getcolor(name) /* get a named color */ |
425 |
|
|
char *name; |
426 |
|
|
{ |
427 |
|
|
register LUENT *lp; |
428 |
|
|
|
429 |
|
|
if ((lp = lu_find(&clr_tab, name)) == NULL) |
430 |
|
|
return(NULL); |
431 |
|
|
return((C_COLOR *)lp->data); |
432 |
|
|
} |
433 |
|
|
|
434 |
|
|
|
435 |
greg |
1.5 |
int |
436 |
|
|
c_isgrey(clr) /* check if color is grey */ |
437 |
|
|
register C_COLOR *clr; |
438 |
|
|
{ |
439 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) |
440 |
greg |
1.5 |
return(1); /* no settings == grey */ |
441 |
|
|
c_ccvt(clr, C_CSXY); |
442 |
|
|
return(clr->cx >= .323 && clr->cx <= .343 && |
443 |
|
|
clr->cy >= .323 && clr->cy <= .343); |
444 |
|
|
} |
445 |
|
|
|
446 |
|
|
|
447 |
|
|
void |
448 |
|
|
c_ccvt(clr, fl) /* convert color representations */ |
449 |
|
|
register C_COLOR *clr; |
450 |
|
|
int fl; |
451 |
|
|
{ |
452 |
|
|
double x, y, z; |
453 |
|
|
register int i; |
454 |
|
|
|
455 |
greg |
1.16 |
fl &= ~clr->flags; /* ignore what's done */ |
456 |
|
|
if (!fl) /* everything's done! */ |
457 |
greg |
1.5 |
return; |
458 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
459 |
greg |
1.5 |
*clr = c_dfcolor; |
460 |
greg |
1.16 |
if (fl & C_CSXY) { /* cspec -> cxy */ |
461 |
greg |
1.5 |
x = y = z = 0.; |
462 |
|
|
for (i = 0; i < C_CNSS; i++) { |
463 |
|
|
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
464 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
465 |
|
|
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
466 |
|
|
} |
467 |
greg |
1.17 |
x /= (double)cie_xf.ssum; |
468 |
|
|
y /= (double)cie_yf.ssum; |
469 |
|
|
z /= (double)cie_zf.ssum; |
470 |
greg |
1.5 |
z += x + y; |
471 |
|
|
clr->cx = x / z; |
472 |
|
|
clr->cy = y / z; |
473 |
|
|
clr->flags |= C_CSXY; |
474 |
greg |
1.16 |
} else if (fl & C_CSSPEC) { /* cxy -> cspec */ |
475 |
greg |
1.5 |
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
476 |
|
|
x = clr->cx * z / cie_xf.ssum; |
477 |
|
|
y = clr->cy * z / cie_yf.ssum; |
478 |
|
|
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum; |
479 |
|
|
clr->ssum = 0; |
480 |
|
|
for (i = 0; i < C_CNSS; i++) |
481 |
|
|
clr->ssum += clr->ssamp[i] = |
482 |
|
|
x * cie_xf.ssamp[i] + |
483 |
|
|
y * cie_yf.ssamp[i] + |
484 |
greg |
1.18 |
z * cie_zf.ssamp[i] + .5; |
485 |
greg |
1.5 |
clr->flags |= C_CSSPEC; |
486 |
|
|
} |
487 |
greg |
1.16 |
if (fl & C_CSEFF) { /* compute efficacy */ |
488 |
|
|
if (clr->flags & C_CDSPEC) { /* from spectrum */ |
489 |
|
|
y = 0.; |
490 |
|
|
for (i = 0; i < C_CNSS; i++) |
491 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
492 |
|
|
clr->eff = C_CLPWM * y / clr->ssum; |
493 |
|
|
} else /* clr->flags & C_CDXY */ { /* from (x,y) */ |
494 |
|
|
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff + |
495 |
|
|
(1. - clr->cx - clr->cy)*cie_zf.eff; |
496 |
|
|
} |
497 |
|
|
clr->flags |= C_CSEFF; |
498 |
|
|
} |
499 |
greg |
1.5 |
} |
500 |
|
|
|
501 |
|
|
|
502 |
|
|
static int |
503 |
|
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
504 |
|
|
register C_COLOR *clr; |
505 |
greg |
1.17 |
double wlmin, wlmax; |
506 |
greg |
1.5 |
int ac; |
507 |
|
|
char **av; |
508 |
|
|
{ |
509 |
|
|
double scale; |
510 |
greg |
1.17 |
float va[C_CNSS]; |
511 |
|
|
register int i, pos; |
512 |
|
|
int n, imax; |
513 |
|
|
int wl; |
514 |
greg |
1.5 |
double wl0, wlstep; |
515 |
greg |
1.17 |
/* check bounds */ |
516 |
|
|
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
517 |
greg |
1.5 |
return(MG_EILL); |
518 |
greg |
1.17 |
wlstep = (wlmax - wlmin)/(ac-1); |
519 |
|
|
while (wlmin < C_CMINWL) { |
520 |
|
|
wlmin += wlstep; |
521 |
|
|
ac--; av++; |
522 |
|
|
} |
523 |
|
|
while (wlmax > C_CMAXWL) { |
524 |
|
|
wlmax -= wlstep; |
525 |
|
|
ac--; |
526 |
|
|
} |
527 |
|
|
if (ac < 2) |
528 |
|
|
return(MG_EILL); |
529 |
|
|
imax = ac; /* box filter if necessary */ |
530 |
|
|
if (wlstep < C_CWLI) { |
531 |
|
|
wlstep = C_CWLI; |
532 |
|
|
imax = (wlmax - wlmin)/wlstep; |
533 |
|
|
} |
534 |
greg |
1.5 |
scale = 0.; /* get values and maximum */ |
535 |
greg |
1.17 |
pos = 0; |
536 |
|
|
for (i = 0; i < imax; i++) { |
537 |
|
|
va[i] = 0.; n = 0; |
538 |
|
|
while (pos < (i+.5)*ac/imax) { |
539 |
|
|
if (!isflt(av[pos])) |
540 |
|
|
return(MG_ETYPE); |
541 |
|
|
va[i] += atof(av[pos++]); |
542 |
|
|
n++; |
543 |
|
|
} |
544 |
|
|
if (n > 1) |
545 |
|
|
va[i] /= (double)n; |
546 |
greg |
1.5 |
if (va[i] < 0.) |
547 |
|
|
return(MG_EILL); |
548 |
|
|
if (va[i] > scale) |
549 |
|
|
scale = va[i]; |
550 |
|
|
} |
551 |
|
|
if (scale == 0.) |
552 |
|
|
return(MG_EILL); |
553 |
|
|
scale = C_CMAXV / scale; |
554 |
|
|
clr->ssum = 0; /* convert to our spacing */ |
555 |
|
|
wl0 = wlmin; |
556 |
|
|
pos = 0; |
557 |
|
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
558 |
greg |
1.17 |
if (wl < wlmin | wl > wlmax) |
559 |
greg |
1.5 |
clr->ssamp[i] = 0; |
560 |
|
|
else { |
561 |
|
|
while (wl0 + wlstep < wl+FTINY) { |
562 |
|
|
wl0 += wlstep; |
563 |
|
|
pos++; |
564 |
|
|
} |
565 |
greg |
1.17 |
if (wl+FTINY >= wl0 & wl-FTINY <= wl0) |
566 |
greg |
1.18 |
clr->ssamp[i] = scale*va[pos] + .5; |
567 |
greg |
1.5 |
else /* interpolate if necessary */ |
568 |
greg |
1.18 |
clr->ssamp[i] = .5 + scale / wlstep * |
569 |
greg |
1.5 |
( va[pos]*(wl0+wlstep - wl) + |
570 |
|
|
va[pos+1]*(wl - wl0) ); |
571 |
|
|
clr->ssum += clr->ssamp[i]; |
572 |
|
|
} |
573 |
|
|
clr->flags = C_CDSPEC|C_CSSPEC; |
574 |
greg |
1.8 |
clr->clock++; |
575 |
greg |
1.5 |
return(MG_OK); |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
|
579 |
|
|
static void |
580 |
|
|
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
581 |
|
|
register C_COLOR *cres, *c1, *c2; |
582 |
|
|
double w1, w2; |
583 |
|
|
{ |
584 |
|
|
double scale; |
585 |
|
|
float cmix[C_CNSS]; |
586 |
|
|
register int i; |
587 |
|
|
|
588 |
|
|
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
589 |
greg |
1.16 |
c_ccvt(c1, C_CSSPEC|C_CSEFF); |
590 |
|
|
c_ccvt(c2, C_CSSPEC|C_CSEFF); |
591 |
|
|
w1 /= c1->eff*c1->ssum; |
592 |
greg |
1.17 |
w2 /= c2->eff*c2->ssum; |
593 |
greg |
1.5 |
scale = 0.; |
594 |
|
|
for (i = 0; i < C_CNSS; i++) { |
595 |
|
|
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
596 |
|
|
if (cmix[i] > scale) |
597 |
|
|
scale = cmix[i]; |
598 |
|
|
} |
599 |
|
|
scale = C_CMAXV / scale; |
600 |
|
|
cres->ssum = 0; |
601 |
|
|
for (i = 0; i < C_CNSS; i++) |
602 |
|
|
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
603 |
|
|
cres->flags = C_CDSPEC|C_CSSPEC; |
604 |
|
|
} else { /* CIE xy mixing */ |
605 |
|
|
c_ccvt(c1, C_CSXY); |
606 |
|
|
c_ccvt(c2, C_CSXY); |
607 |
|
|
scale = 1. / (w1/c1->cy + w2/c2->cy); |
608 |
|
|
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
609 |
|
|
cres->cy = (w1 + w2) * scale; |
610 |
|
|
cres->flags = C_CDXY|C_CSXY; |
611 |
|
|
} |
612 |
greg |
1.1 |
} |
613 |
greg |
1.18 |
|
614 |
|
|
|
615 |
|
|
#define C1 3.741832e-16 /* W-m^2 */ |
616 |
|
|
#define C2 1.4388e-2 /* m-K */ |
617 |
|
|
|
618 |
|
|
#define bbsp(l,t) (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.))) |
619 |
|
|
#define bblm(t) (C2/5./(t)) |
620 |
|
|
|
621 |
|
|
static int |
622 |
|
|
setbbtemp(clr, tk) /* set black body spectrum */ |
623 |
|
|
register C_COLOR *clr; |
624 |
|
|
double tk; |
625 |
|
|
{ |
626 |
|
|
double sf, wl; |
627 |
|
|
register int i; |
628 |
|
|
|
629 |
|
|
if (tk < 1000) |
630 |
|
|
return(MG_EILL); |
631 |
|
|
wl = bblm(tk); /* scalefactor based on peak */ |
632 |
|
|
if (wl < C_CMINWL*1e-9) |
633 |
|
|
wl = C_CMINWL*1e-9; |
634 |
|
|
else if (wl > C_CMAXWL*1e-9) |
635 |
|
|
wl = C_CMAXWL*1e-9; |
636 |
|
|
sf = C_CMAXV/bbsp(wl,tk); |
637 |
|
|
clr->ssum = 0; |
638 |
|
|
for (i = 0; i < C_CNSS; i++) { |
639 |
|
|
wl = (C_CMINWL + i*C_CWLI)*1e-9; |
640 |
|
|
clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5; |
641 |
|
|
} |
642 |
|
|
clr->flags = C_CDSPEC|C_CSSPEC; |
643 |
|
|
clr->clock++; |
644 |
|
|
return(MG_OK); |
645 |
|
|
} |
646 |
|
|
|
647 |
|
|
#undef C1 |
648 |
|
|
#undef C2 |
649 |
|
|
#undef bbsp |
650 |
|
|
#undef bblm |