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