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