26 |
|
|
27 |
|
/* the current contexts */ |
28 |
|
C_COLOR *c_ccolor = &c_uncolor; |
29 |
+ |
char *c_ccname = NULL; |
30 |
|
C_MATERIAL *c_cmaterial = &c_unmaterial; |
31 |
+ |
char *c_cmname = NULL; |
32 |
|
C_VERTEX *c_cvertex = &c_unvertex; |
33 |
+ |
char *c_cvname = NULL; |
34 |
|
|
35 |
|
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 |
|
|
39 |
< |
/* 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; |
39 |
> |
/* CIE 1931 Standard Observer curves */ |
40 |
> |
static C_COLOR cie_xf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
41 |
> |
{14,42,143,435,1344,2839,3483,3362,2908,1954,956, |
42 |
> |
320,49,93,633,1655,2904,4334,5945,7621,9163,10263, |
43 |
> |
10622,10026,8544,6424,4479,2835,1649,874,468,227, |
44 |
> |
114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230 |
45 |
> |
}; |
46 |
> |
static C_COLOR cie_yf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
47 |
> |
{0,1,4,12,40,116,230,380,600,910,1390,2080,3230, |
48 |
> |
5030,7100,8620,9540,9950,9950,9520,8700,7570,6310, |
49 |
> |
5030,3810,2650,1750,1070,610,320,170,82,41,21,10, |
50 |
> |
5,2,1,1,0,0}, 106856L, .398, .542, 493.525 |
51 |
> |
}; |
52 |
> |
static C_COLOR cie_zf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
53 |
> |
{65,201,679,2074,6456,13856,17471,17721,16692, |
54 |
> |
12876,8130,4652,2720,1582,782,422,203,87,39,21,17, |
55 |
> |
11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, |
56 |
> |
106770L, .147, .077, 54.363 |
57 |
> |
}; |
58 |
> |
/* Derived CIE 1931 Primaries (imaginary) */ |
59 |
> |
static C_COLOR cie_xp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
60 |
> |
{-174,-198,-195,-197,-202,-213,-235,-272,-333, |
61 |
> |
-444,-688,-1232,-2393,-4497,-6876,-6758,-5256, |
62 |
> |
-3100,-815,1320,3200,4782,5998,6861,7408,7754, |
63 |
> |
7980,8120,8199,8240,8271,8292,8309,8283,8469, |
64 |
> |
8336,8336,8336,8336,8336,8336}, |
65 |
> |
127424L, 1., .0, |
66 |
> |
}; |
67 |
> |
static C_COLOR cie_yp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
68 |
> |
{-451,-431,-431,-430,-427,-417,-399,-366,-312, |
69 |
> |
-204,57,691,2142,4990,8810,9871,9122,7321,5145, |
70 |
> |
3023,1123,-473,-1704,-2572,-3127,-3474,-3704, |
71 |
> |
-3846,-3927,-3968,-3999,-4021,-4038,-4012,-4201, |
72 |
> |
-4066,-4066,-4066,-4066,-4066,-4066}, |
73 |
> |
-23035L, .0, 1., |
74 |
> |
}; |
75 |
> |
static C_COLOR cie_zp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
76 |
> |
{4051,4054,4052,4053,4054,4056,4059,4064,4071, |
77 |
> |
4074,4056,3967,3677,2933,1492,313,-440,-795, |
78 |
> |
-904,-918,-898,-884,-869,-863,-855,-855,-851, |
79 |
> |
-848,-847,-846,-846,-846,-845,-846,-843,-845, |
80 |
> |
-845,-845,-845,-845,-845}, |
81 |
> |
36057L, .0, .0, |
82 |
> |
}; |
83 |
|
|
84 |
|
static int setspectrum(); |
85 |
+ |
static int setbbtemp(); |
86 |
|
static void mixcolors(); |
87 |
|
|
88 |
|
|
102 |
|
if (ac == 1) { /* set unnamed color context */ |
103 |
|
c_uncolor = c_dfcolor; |
104 |
|
c_ccolor = &c_uncolor; |
105 |
+ |
c_ccname = NULL; |
106 |
|
return(MG_OK); |
107 |
|
} |
108 |
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
109 |
|
if (lp == NULL) |
110 |
|
return(MG_EMEM); |
111 |
+ |
c_ccname = lp->key; |
112 |
+ |
c_ccolor = (C_COLOR *)lp->data; |
113 |
|
if (ac == 2) { /* reestablish previous context */ |
114 |
< |
if (lp->data == NULL) |
114 |
> |
if (c_ccolor == NULL) |
115 |
|
return(MG_EUNDEF); |
69 |
– |
c_ccolor = (C_COLOR *)lp->data; |
116 |
|
return(MG_OK); |
117 |
|
} |
118 |
|
if (av[2][0] != '=' || av[2][1]) |
119 |
|
return(MG_ETYPE); |
120 |
< |
if (lp->key == NULL) { /* create new color context */ |
120 |
> |
if (c_ccolor == NULL) { /* create new color context */ |
121 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
122 |
|
if (lp->key == NULL) |
123 |
|
return(MG_EMEM); |
125 |
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
126 |
|
if (lp->data == NULL) |
127 |
|
return(MG_EMEM); |
128 |
+ |
c_ccname = lp->key; |
129 |
+ |
c_ccolor = (C_COLOR *)lp->data; |
130 |
+ |
c_ccolor->clock = 0; |
131 |
|
} |
132 |
< |
c_ccolor = (C_COLOR *)lp->data; |
132 |
> |
i = c_ccolor->clock; |
133 |
|
if (ac == 3) { /* use default template */ |
134 |
|
*c_ccolor = c_dfcolor; |
135 |
< |
c_ccolor->name = lp->key; |
135 |
> |
c_ccolor->clock = i + 1; |
136 |
|
return(MG_OK); |
137 |
|
} |
138 |
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
141 |
|
if (lp->data == NULL) |
142 |
|
return(MG_EUNDEF); |
143 |
|
*c_ccolor = *(C_COLOR *)lp->data; |
144 |
< |
c_ccolor->name = lp->key; |
96 |
< |
c_ccolor->clock = 1; |
97 |
< |
if (ac > 4) |
98 |
< |
return(MG_EARGC); |
144 |
> |
c_ccolor->clock = i + 1; |
145 |
|
return(MG_OK); |
146 |
|
case MG_E_CXY: /* assign CIE XY value */ |
147 |
|
if (ac != 3) |
148 |
|
return(MG_EARGC); |
149 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
149 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
150 |
|
return(MG_ETYPE); |
151 |
|
c_ccolor->cx = atof(av[1]); |
152 |
|
c_ccolor->cy = atof(av[2]); |
159 |
|
case MG_E_CSPEC: /* assign spectral values */ |
160 |
|
if (ac < 5) |
161 |
|
return(MG_EARGC); |
162 |
< |
if (!isint(av[1]) || !isint(av[2])) |
162 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
163 |
|
return(MG_ETYPE); |
164 |
< |
return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]), |
164 |
> |
return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]), |
165 |
|
ac-3, av+3)); |
166 |
+ |
case MG_E_CCT: /* assign black body spectrum */ |
167 |
+ |
if (ac != 2) |
168 |
+ |
return(MG_EARGC); |
169 |
+ |
if (!isflt(av[1])) |
170 |
+ |
return(MG_ETYPE); |
171 |
+ |
return(setbbtemp(c_ccolor, atof(av[1]))); |
172 |
|
case MG_E_CMIX: /* mix colors */ |
173 |
|
if (ac < 5 || (ac-1)%2) |
174 |
|
return(MG_EARGC); |
196 |
|
w, (C_COLOR *)lp->data); |
197 |
|
wsum += w; |
198 |
|
} |
199 |
+ |
c_ccolor->clock++; |
200 |
|
return(MG_OK); |
201 |
|
} |
202 |
|
return(MG_EUNK); |
208 |
|
int ac; |
209 |
|
register char **av; |
210 |
|
{ |
211 |
+ |
int i; |
212 |
|
register LUENT *lp; |
213 |
|
|
214 |
|
switch (mg_entity(av[0])) { |
218 |
|
if (ac == 1) { /* set unnamed material context */ |
219 |
|
c_unmaterial = c_dfmaterial; |
220 |
|
c_cmaterial = &c_unmaterial; |
221 |
+ |
c_cmname = NULL; |
222 |
|
return(MG_OK); |
223 |
|
} |
224 |
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
225 |
|
if (lp == NULL) |
226 |
|
return(MG_EMEM); |
227 |
+ |
c_cmname = lp->key; |
228 |
+ |
c_cmaterial = (C_MATERIAL *)lp->data; |
229 |
|
if (ac == 2) { /* reestablish previous context */ |
230 |
< |
if (lp->data == NULL) |
230 |
> |
if (c_cmaterial == NULL) |
231 |
|
return(MG_EUNDEF); |
175 |
– |
c_cmaterial = (C_MATERIAL *)lp->data; |
232 |
|
return(MG_OK); |
233 |
|
} |
234 |
|
if (av[2][0] != '=' || av[2][1]) |
235 |
|
return(MG_ETYPE); |
236 |
< |
if (lp->key == NULL) { /* create new material context */ |
236 |
> |
if (c_cmaterial == NULL) { /* create new material */ |
237 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
238 |
|
if (lp->key == NULL) |
239 |
|
return(MG_EMEM); |
241 |
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
242 |
|
if (lp->data == NULL) |
243 |
|
return(MG_EMEM); |
244 |
+ |
c_cmname = lp->key; |
245 |
+ |
c_cmaterial = (C_MATERIAL *)lp->data; |
246 |
+ |
c_cmaterial->clock = 0; |
247 |
|
} |
248 |
< |
c_cmaterial = (C_MATERIAL *)lp->data; |
248 |
> |
i = c_cmaterial->clock; |
249 |
|
if (ac == 3) { /* use default template */ |
250 |
|
*c_cmaterial = c_dfmaterial; |
251 |
< |
c_cmaterial->name = lp->key; |
251 |
> |
c_cmaterial->clock = i + 1; |
252 |
|
return(MG_OK); |
253 |
|
} |
254 |
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
257 |
|
if (lp->data == NULL) |
258 |
|
return(MG_EUNDEF); |
259 |
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
260 |
< |
c_cmaterial->name = lp->key; |
261 |
< |
c_cmaterial->clock = 1; |
262 |
< |
if (ac > 4) |
260 |
> |
c_cmaterial->clock = i + 1; |
261 |
> |
return(MG_OK); |
262 |
> |
case MG_E_IR: /* set index of refraction */ |
263 |
> |
if (ac != 3) |
264 |
|
return(MG_EARGC); |
265 |
+ |
if (!isflt(av[1]) | !isflt(av[2])) |
266 |
+ |
return(MG_ETYPE); |
267 |
+ |
c_cmaterial->nr = atof(av[1]); |
268 |
+ |
c_cmaterial->ni = atof(av[2]); |
269 |
+ |
if (c_cmaterial->nr <= FTINY) |
270 |
+ |
return(MG_EILL); |
271 |
+ |
c_cmaterial->clock++; |
272 |
|
return(MG_OK); |
273 |
|
case MG_E_RD: /* set diffuse reflectance */ |
274 |
|
if (ac != 2) |
306 |
|
case MG_E_RS: /* set specular reflectance */ |
307 |
|
if (ac != 3) |
308 |
|
return(MG_EARGC); |
309 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
309 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
310 |
|
return(MG_ETYPE); |
311 |
|
c_cmaterial->rs = atof(av[1]); |
312 |
|
c_cmaterial->rs_a = atof(av[2]); |
319 |
|
case MG_E_TS: /* set specular transmittance */ |
320 |
|
if (ac != 3) |
321 |
|
return(MG_EARGC); |
322 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
322 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
323 |
|
return(MG_ETYPE); |
324 |
|
c_cmaterial->ts = atof(av[1]); |
325 |
|
c_cmaterial->ts_a = atof(av[2]); |
329 |
|
c_cmaterial->ts_c = *c_ccolor; |
330 |
|
c_cmaterial->clock++; |
331 |
|
return(MG_OK); |
332 |
+ |
case MG_E_SIDES: /* set number of sides */ |
333 |
+ |
if (ac != 2) |
334 |
+ |
return(MG_EARGC); |
335 |
+ |
if (!isint(av[1])) |
336 |
+ |
return(MG_ETYPE); |
337 |
+ |
i = atoi(av[1]); |
338 |
+ |
if (i == 1) |
339 |
+ |
c_cmaterial->sided = 1; |
340 |
+ |
else if (i == 2) |
341 |
+ |
c_cmaterial->sided = 0; |
342 |
+ |
else |
343 |
+ |
return(MG_EILL); |
344 |
+ |
c_cmaterial->clock++; |
345 |
+ |
return(MG_OK); |
346 |
|
} |
347 |
|
return(MG_EUNK); |
348 |
|
} |
353 |
|
int ac; |
354 |
|
register char **av; |
355 |
|
{ |
356 |
+ |
int i; |
357 |
|
register LUENT *lp; |
358 |
|
|
359 |
|
switch (mg_entity(av[0])) { |
363 |
|
if (ac == 1) { /* set unnamed vertex context */ |
364 |
|
c_unvertex = c_dfvertex; |
365 |
|
c_cvertex = &c_unvertex; |
366 |
+ |
c_cvname = NULL; |
367 |
|
return(MG_OK); |
368 |
|
} |
369 |
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
370 |
|
if (lp == NULL) |
371 |
|
return(MG_EMEM); |
372 |
+ |
c_cvname = lp->key; |
373 |
+ |
c_cvertex = (C_VERTEX *)lp->data; |
374 |
|
if (ac == 2) { /* reestablish previous context */ |
375 |
< |
if (lp->data == NULL) |
375 |
> |
if (c_cvertex == NULL) |
376 |
|
return(MG_EUNDEF); |
292 |
– |
c_cvertex = (C_VERTEX *)lp->data; |
377 |
|
return(MG_OK); |
378 |
|
} |
379 |
|
if (av[2][0] != '=' || av[2][1]) |
380 |
|
return(MG_ETYPE); |
381 |
< |
if (lp->key == NULL) { /* create new vertex context */ |
381 |
> |
if (c_cvertex == NULL) { /* create new vertex context */ |
382 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
383 |
|
if (lp->key == NULL) |
384 |
|
return(MG_EMEM); |
386 |
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
387 |
|
if (lp->data == NULL) |
388 |
|
return(MG_EMEM); |
389 |
+ |
c_cvname = lp->key; |
390 |
+ |
c_cvertex = (C_VERTEX *)lp->data; |
391 |
|
} |
392 |
< |
c_cvertex = (C_VERTEX *)lp->data; |
392 |
> |
i = c_cvertex->clock; |
393 |
|
if (ac == 3) { /* use default template */ |
394 |
|
*c_cvertex = c_dfvertex; |
395 |
< |
c_cvertex->name = lp->key |
395 |
> |
c_cvertex->clock = i + 1; |
396 |
|
return(MG_OK); |
397 |
|
} |
398 |
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
401 |
|
if (lp->data == NULL) |
402 |
|
return(MG_EUNDEF); |
403 |
|
*c_cvertex = *(C_VERTEX *)lp->data; |
404 |
< |
c_cvertex->name = lp->key |
319 |
< |
c_cvertex->clock = 1; |
320 |
< |
if (ac > 4) |
321 |
< |
return(MG_EARGC); |
404 |
> |
c_cvertex->clock = i + 1; |
405 |
|
return(MG_OK); |
406 |
|
case MG_E_POINT: /* set point */ |
407 |
|
if (ac != 4) |
408 |
|
return(MG_EARGC); |
409 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
409 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
410 |
|
return(MG_ETYPE); |
411 |
|
c_cvertex->p[0] = atof(av[1]); |
412 |
|
c_cvertex->p[1] = atof(av[2]); |
416 |
|
case MG_E_NORMAL: /* set normal */ |
417 |
|
if (ac != 4) |
418 |
|
return(MG_EARGC); |
419 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
419 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
420 |
|
return(MG_ETYPE); |
421 |
|
c_cvertex->n[0] = atof(av[1]); |
422 |
|
c_cvertex->n[1] = atof(av[2]); |
434 |
|
{ |
435 |
|
c_uncolor = c_dfcolor; |
436 |
|
c_ccolor = &c_uncolor; |
437 |
+ |
c_ccname = NULL; |
438 |
|
lu_done(&clr_tab); |
439 |
|
c_unmaterial = c_dfmaterial; |
440 |
|
c_cmaterial = &c_unmaterial; |
441 |
+ |
c_cmname = NULL; |
442 |
|
lu_done(&mat_tab); |
443 |
|
c_unvertex = c_dfvertex; |
444 |
|
c_cvertex = &c_unvertex; |
445 |
+ |
c_cvname = NULL; |
446 |
|
lu_done(&vtx_tab); |
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
+ |
C_MATERIAL * |
451 |
+ |
c_getmaterial(name) /* get a named material */ |
452 |
+ |
char *name; |
453 |
+ |
{ |
454 |
+ |
register LUENT *lp; |
455 |
+ |
|
456 |
+ |
if ((lp = lu_find(&mat_tab, name)) == NULL) |
457 |
+ |
return(NULL); |
458 |
+ |
return((C_MATERIAL *)lp->data); |
459 |
+ |
} |
460 |
+ |
|
461 |
+ |
|
462 |
|
C_VERTEX * |
463 |
|
c_getvert(name) /* get a named vertex */ |
464 |
|
char *name; |
503 |
|
double x, y, z; |
504 |
|
register int i; |
505 |
|
|
506 |
< |
if (clr->flags & fl) /* already done */ |
506 |
> |
fl &= ~clr->flags; /* ignore what's done */ |
507 |
> |
if (!fl) /* everything's done! */ |
508 |
|
return; |
509 |
|
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
510 |
|
*clr = c_dfcolor; |
511 |
< |
else if (fl & C_CSXY) { /* cspec -> cxy */ |
511 |
> |
if (fl & C_CSXY) { /* cspec -> cxy */ |
512 |
|
x = y = z = 0.; |
513 |
|
for (i = 0; i < C_CNSS; i++) { |
514 |
|
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
515 |
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
516 |
|
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
517 |
|
} |
518 |
+ |
x /= (double)cie_xf.ssum; |
519 |
+ |
y /= (double)cie_yf.ssum; |
520 |
+ |
z /= (double)cie_zf.ssum; |
521 |
|
z += x + y; |
522 |
|
clr->cx = x / z; |
523 |
|
clr->cy = y / z; |
524 |
|
clr->flags |= C_CSXY; |
525 |
< |
} else { /* cxy -> cspec */ |
526 |
< |
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
527 |
< |
x = clr->cx * z / cie_xf.ssum; |
528 |
< |
y = clr->cy * z / cie_yf.ssum; |
427 |
< |
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum; |
525 |
> |
} else if (fl & C_CSSPEC) { /* cxy -> cspec */ |
526 |
> |
x = clr->cx; |
527 |
> |
y = clr->cy; |
528 |
> |
z = 1. - x - y; |
529 |
|
clr->ssum = 0; |
530 |
< |
for (i = 0; i < C_CNSS; i++) |
531 |
< |
clr->ssum += clr->ssamp[i] = |
532 |
< |
x * cie_xf.ssamp[i] + |
533 |
< |
y * cie_yf.ssamp[i] + |
534 |
< |
z * cie_zf.ssamp[i] ; |
530 |
> |
for (i = 0; i < C_CNSS; i++) { |
531 |
> |
clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i] |
532 |
> |
+ z*cie_zp.ssamp[i] + .5; |
533 |
> |
if (clr->ssamp[i] < 0) /* out of gamut! */ |
534 |
> |
clr->ssamp[i] = 0; |
535 |
> |
else |
536 |
> |
clr->ssum += clr->ssamp[i]; |
537 |
> |
} |
538 |
|
clr->flags |= C_CSSPEC; |
539 |
|
} |
540 |
+ |
if (fl & C_CSEFF) { /* compute efficacy */ |
541 |
+ |
if (clr->flags & C_CSSPEC) { /* from spectrum */ |
542 |
+ |
y = 0.; |
543 |
+ |
for (i = 0; i < C_CNSS; i++) |
544 |
+ |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
545 |
+ |
clr->eff = C_CLPWM * y / clr->ssum; |
546 |
+ |
} else /* clr->flags & C_CSXY */ { /* from (x,y) */ |
547 |
+ |
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff + |
548 |
+ |
(1. - clr->cx - clr->cy)*cie_zf.eff; |
549 |
+ |
} |
550 |
+ |
clr->flags |= C_CSEFF; |
551 |
+ |
} |
552 |
|
} |
553 |
|
|
554 |
|
|
555 |
|
static int |
556 |
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
557 |
|
register C_COLOR *clr; |
558 |
< |
int wlmin, wlmax; |
558 |
> |
double wlmin, wlmax; |
559 |
|
int ac; |
560 |
|
char **av; |
561 |
|
{ |
562 |
|
double scale; |
563 |
< |
float *va; |
564 |
< |
register int i; |
565 |
< |
int wl, pos; |
563 |
> |
float va[C_CNSS]; |
564 |
> |
register int i, pos; |
565 |
> |
int n, imax; |
566 |
> |
int wl; |
567 |
|
double wl0, wlstep; |
568 |
< |
|
569 |
< |
if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL) |
568 |
> |
double boxpos, boxstep; |
569 |
> |
/* check bounds */ |
570 |
> |
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
571 |
|
return(MG_EILL); |
572 |
< |
if ((va = (float *)malloc(ac*sizeof(float))) == NULL) |
573 |
< |
return(MG_EMEM); |
572 |
> |
wlstep = (wlmax - wlmin)/(ac-1); |
573 |
> |
while (wlmin < C_CMINWL) { |
574 |
> |
wlmin += wlstep; |
575 |
> |
ac--; av++; |
576 |
> |
} |
577 |
> |
while (wlmax > C_CMAXWL) { |
578 |
> |
wlmax -= wlstep; |
579 |
> |
ac--; |
580 |
> |
} |
581 |
> |
imax = ac; /* box filter if necessary */ |
582 |
> |
boxpos = 0; |
583 |
> |
boxstep = 1; |
584 |
> |
if (wlstep < C_CWLI) { |
585 |
> |
imax = (wlmax - wlmin)/C_CWLI + (1-FTINY); |
586 |
> |
boxpos = (wlmin - C_CMINWL)/C_CWLI; |
587 |
> |
boxstep = wlstep/C_CWLI; |
588 |
> |
wlstep = C_CWLI; |
589 |
> |
} |
590 |
|
scale = 0.; /* get values and maximum */ |
591 |
< |
for (i = 0; i < ac; i++) { |
592 |
< |
if (!isflt(av[i])) |
593 |
< |
return(MG_ETYPE); |
594 |
< |
va[i] = atof(av[i]); |
591 |
> |
pos = 0; |
592 |
> |
for (i = 0; i < imax; i++) { |
593 |
> |
va[i] = 0.; n = 0; |
594 |
> |
while (boxpos < i+.5 && pos < ac) { |
595 |
> |
if (!isflt(av[pos])) |
596 |
> |
return(MG_ETYPE); |
597 |
> |
va[i] += atof(av[pos++]); |
598 |
> |
n++; |
599 |
> |
boxpos += boxstep; |
600 |
> |
} |
601 |
> |
if (n > 1) |
602 |
> |
va[i] /= (double)n; |
603 |
|
if (va[i] < 0.) |
604 |
|
return(MG_EILL); |
605 |
|
if (va[i] > scale) |
606 |
|
scale = va[i]; |
607 |
|
} |
608 |
< |
if (scale == 0.) |
608 |
> |
if (scale <= FTINY) |
609 |
|
return(MG_EILL); |
610 |
|
scale = C_CMAXV / scale; |
611 |
|
clr->ssum = 0; /* convert to our spacing */ |
612 |
|
wl0 = wlmin; |
471 |
– |
wlstep = (double)(wlmax - wlmin)/(ac-1); |
613 |
|
pos = 0; |
614 |
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
615 |
< |
if (wl < wlmin || wl > wlmax) |
615 |
> |
if (wl < wlmin | wl > wlmax) |
616 |
|
clr->ssamp[i] = 0; |
617 |
|
else { |
618 |
|
while (wl0 + wlstep < wl+FTINY) { |
619 |
|
wl0 += wlstep; |
620 |
|
pos++; |
621 |
|
} |
622 |
< |
if (wl+FTINY >= wl0 && wl-FTINY <= wl0) |
623 |
< |
clr->ssamp[i] = scale*va[pos]; |
622 |
> |
if (wl+FTINY >= wl0 & wl-FTINY <= wl0) |
623 |
> |
clr->ssamp[i] = scale*va[pos] + .5; |
624 |
|
else /* interpolate if necessary */ |
625 |
< |
clr->ssamp[i] = scale / wlstep * |
625 |
> |
clr->ssamp[i] = .5 + scale / wlstep * |
626 |
|
( va[pos]*(wl0+wlstep - wl) + |
627 |
|
va[pos+1]*(wl - wl0) ); |
628 |
|
clr->ssum += clr->ssamp[i]; |
629 |
|
} |
630 |
|
clr->flags = C_CDSPEC|C_CSSPEC; |
631 |
|
clr->clock++; |
491 |
– |
free((MEM_PTR)va); |
632 |
|
return(MG_OK); |
633 |
|
} |
634 |
|
|
643 |
|
register int i; |
644 |
|
|
645 |
|
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
646 |
< |
c_ccvt(c1, C_CSSPEC); |
647 |
< |
c_ccvt(c2, C_CSSPEC); |
648 |
< |
w1 /= (double)c1->ssum; |
649 |
< |
w2 /= (double)c2->ssum; |
646 |
> |
c_ccvt(c1, C_CSSPEC|C_CSEFF); |
647 |
> |
c_ccvt(c2, C_CSSPEC|C_CSEFF); |
648 |
> |
w1 /= c1->eff*c1->ssum; |
649 |
> |
w2 /= c2->eff*c2->ssum; |
650 |
|
scale = 0.; |
651 |
|
for (i = 0; i < C_CNSS; i++) { |
652 |
|
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
666 |
|
cres->cy = (w1 + w2) * scale; |
667 |
|
cres->flags = C_CDXY|C_CSXY; |
668 |
|
} |
529 |
– |
cres->clock++; /* record the change */ |
669 |
|
} |
670 |
+ |
|
671 |
+ |
|
672 |
+ |
#define C1 3.741832e-16 /* W-m^2 */ |
673 |
+ |
#define C2 1.4388e-2 /* m-K */ |
674 |
+ |
|
675 |
+ |
#define bbsp(l,t) (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.))) |
676 |
+ |
#define bblm(t) (C2/5./(t)) |
677 |
+ |
|
678 |
+ |
static int |
679 |
+ |
setbbtemp(clr, tk) /* set black body spectrum */ |
680 |
+ |
register C_COLOR *clr; |
681 |
+ |
double tk; |
682 |
+ |
{ |
683 |
+ |
double sf, wl; |
684 |
+ |
register int i; |
685 |
+ |
|
686 |
+ |
if (tk < 1000) |
687 |
+ |
return(MG_EILL); |
688 |
+ |
wl = bblm(tk); /* scalefactor based on peak */ |
689 |
+ |
if (wl < C_CMINWL*1e-9) |
690 |
+ |
wl = C_CMINWL*1e-9; |
691 |
+ |
else if (wl > C_CMAXWL*1e-9) |
692 |
+ |
wl = C_CMAXWL*1e-9; |
693 |
+ |
sf = C_CMAXV/bbsp(wl,tk); |
694 |
+ |
clr->ssum = 0; |
695 |
+ |
for (i = 0; i < C_CNSS; i++) { |
696 |
+ |
wl = (C_CMINWL + i*C_CWLI)*1e-9; |
697 |
+ |
clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5; |
698 |
+ |
} |
699 |
+ |
clr->flags = C_CDSPEC|C_CSSPEC; |
700 |
+ |
clr->clock++; |
701 |
+ |
return(MG_OK); |
702 |
+ |
} |
703 |
+ |
|
704 |
+ |
#undef C1 |
705 |
+ |
#undef C2 |
706 |
+ |
#undef bbsp |
707 |
+ |
#undef bblm |