1 |
– |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Context handlers |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
< |
#include <math.h> |
9 |
> |
#include <stdlib.h> |
10 |
|
#include <string.h> |
11 |
|
#include "parser.h" |
12 |
|
#include "lookup.h" |
13 |
|
|
14 |
|
/* default context values */ |
18 |
– |
static C_COLOR c_dfcolor = C_DEFCOLOR; |
15 |
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL; |
16 |
|
static C_VERTEX c_dfvertex = C_DEFVERTEX; |
17 |
|
|
32 |
|
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
33 |
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
34 |
|
|
39 |
– |
/* CIE 1931 Standard Observer curves */ |
40 |
– |
static C_COLOR cie_xf = { 1, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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 |
– |
|
35 |
|
static int setspectrum(); |
85 |
– |
static int setbbtemp(); |
86 |
– |
static void mixcolors(); |
36 |
|
|
37 |
|
|
38 |
|
int |
103 |
|
c_ccolor->cx = atof(av[1]); |
104 |
|
c_ccolor->cy = atof(av[2]); |
105 |
|
c_ccolor->flags = C_CDXY|C_CSXY; |
106 |
< |
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
107 |
< |
c_ccolor->cx + c_ccolor->cy > 1.) |
106 |
> |
if ((c_ccolor->cx < 0.) | (c_ccolor->cy < 0.) | |
107 |
> |
(c_ccolor->cx + c_ccolor->cy > 1.)) |
108 |
|
return(MG_EILL); |
109 |
|
c_ccolor->clock++; |
110 |
|
return(MG_OK); |
120 |
|
return(MG_EARGC); |
121 |
|
if (!isflt(av[1])) |
122 |
|
return(MG_ETYPE); |
123 |
< |
return(setbbtemp(c_ccolor, atof(av[1]))); |
123 |
> |
if (!c_bbtemp(c_ccolor, atof(av[1]))) |
124 |
> |
return(MG_EILL); |
125 |
> |
c_ccolor->clock++; |
126 |
> |
return(MG_OK); |
127 |
|
case MG_E_CMIX: /* mix colors */ |
128 |
|
if (ac < 5 || (ac-1)%2) |
129 |
|
return(MG_EARGC); |
143 |
|
return(MG_EMEM); |
144 |
|
if (lp->data == NULL) |
145 |
|
return(MG_EUNDEF); |
146 |
< |
mixcolors(c_ccolor, wsum, c_ccolor, |
146 |
> |
c_cmix(c_ccolor, wsum, c_ccolor, |
147 |
|
w, (C_COLOR *)lp->data); |
148 |
|
wsum += w; |
149 |
|
} |
232 |
|
if (!isflt(av[1])) |
233 |
|
return(MG_ETYPE); |
234 |
|
c_cmaterial->rd = atof(av[1]); |
235 |
< |
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.) |
235 |
> |
if ((c_cmaterial->rd < 0.) | (c_cmaterial->rd > 1.)) |
236 |
|
return(MG_EILL); |
237 |
|
c_cmaterial->rd_c = *c_ccolor; |
238 |
|
c_cmaterial->clock++; |
254 |
|
if (!isflt(av[1])) |
255 |
|
return(MG_ETYPE); |
256 |
|
c_cmaterial->td = atof(av[1]); |
257 |
< |
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
257 |
> |
if ((c_cmaterial->td < 0.) | (c_cmaterial->td > 1.)) |
258 |
|
return(MG_EILL); |
259 |
|
c_cmaterial->td_c = *c_ccolor; |
260 |
|
c_cmaterial->clock++; |
266 |
|
return(MG_ETYPE); |
267 |
|
c_cmaterial->rs = atof(av[1]); |
268 |
|
c_cmaterial->rs_a = atof(av[2]); |
269 |
< |
if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. | |
270 |
< |
c_cmaterial->rs_a < 0.) |
269 |
> |
if ((c_cmaterial->rs < 0.) | (c_cmaterial->rs > 1.) | |
270 |
> |
(c_cmaterial->rs_a < 0.)) |
271 |
|
return(MG_EILL); |
272 |
|
c_cmaterial->rs_c = *c_ccolor; |
273 |
|
c_cmaterial->clock++; |
279 |
|
return(MG_ETYPE); |
280 |
|
c_cmaterial->ts = atof(av[1]); |
281 |
|
c_cmaterial->ts_a = atof(av[2]); |
282 |
< |
if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. | |
283 |
< |
c_cmaterial->ts_a < 0.) |
282 |
> |
if ((c_cmaterial->ts < 0.) | (c_cmaterial->ts > 1.) | |
283 |
> |
(c_cmaterial->ts_a < 0.)) |
284 |
|
return(MG_EILL); |
285 |
|
c_cmaterial->ts_c = *c_ccolor; |
286 |
|
c_cmaterial->clock++; |
443 |
|
} |
444 |
|
|
445 |
|
|
494 |
– |
int |
495 |
– |
c_isgrey(clr) /* check if color is grey */ |
496 |
– |
register C_COLOR *clr; |
497 |
– |
{ |
498 |
– |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) |
499 |
– |
return(1); /* no settings == grey */ |
500 |
– |
c_ccvt(clr, C_CSXY); |
501 |
– |
return(clr->cx >= .323 && clr->cx <= .343 && |
502 |
– |
clr->cy >= .323 && clr->cy <= .343); |
503 |
– |
} |
504 |
– |
|
505 |
– |
|
506 |
– |
void |
507 |
– |
c_ccvt(clr, fl) /* convert color representations */ |
508 |
– |
register C_COLOR *clr; |
509 |
– |
int fl; |
510 |
– |
{ |
511 |
– |
double x, y, z; |
512 |
– |
register int i; |
513 |
– |
|
514 |
– |
fl &= ~clr->flags; /* ignore what's done */ |
515 |
– |
if (!fl) /* everything's done! */ |
516 |
– |
return; |
517 |
– |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
518 |
– |
*clr = c_dfcolor; |
519 |
– |
if (fl & C_CSXY) { /* cspec -> cxy */ |
520 |
– |
x = y = z = 0.; |
521 |
– |
for (i = 0; i < C_CNSS; i++) { |
522 |
– |
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
523 |
– |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
524 |
– |
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
525 |
– |
} |
526 |
– |
x /= (double)cie_xf.ssum; |
527 |
– |
y /= (double)cie_yf.ssum; |
528 |
– |
z /= (double)cie_zf.ssum; |
529 |
– |
z += x + y; |
530 |
– |
clr->cx = x / z; |
531 |
– |
clr->cy = y / z; |
532 |
– |
clr->flags |= C_CSXY; |
533 |
– |
} else if (fl & C_CSSPEC) { /* cxy -> cspec */ |
534 |
– |
x = clr->cx; |
535 |
– |
y = clr->cy; |
536 |
– |
z = 1. - x - y; |
537 |
– |
clr->ssum = 0; |
538 |
– |
for (i = 0; i < C_CNSS; i++) { |
539 |
– |
clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i] |
540 |
– |
+ z*cie_zp.ssamp[i] + .5; |
541 |
– |
if (clr->ssamp[i] < 0) /* out of gamut! */ |
542 |
– |
clr->ssamp[i] = 0; |
543 |
– |
else |
544 |
– |
clr->ssum += clr->ssamp[i]; |
545 |
– |
} |
546 |
– |
clr->flags |= C_CSSPEC; |
547 |
– |
} |
548 |
– |
if (fl & C_CSEFF) { /* compute efficacy */ |
549 |
– |
if (clr->flags & C_CSSPEC) { /* from spectrum */ |
550 |
– |
y = 0.; |
551 |
– |
for (i = 0; i < C_CNSS; i++) |
552 |
– |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
553 |
– |
clr->eff = C_CLPWM * y / clr->ssum; |
554 |
– |
} else /* clr->flags & C_CSXY */ { /* from (x,y) */ |
555 |
– |
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff + |
556 |
– |
(1. - clr->cx - clr->cy)*cie_zf.eff; |
557 |
– |
} |
558 |
– |
clr->flags |= C_CSEFF; |
559 |
– |
} |
560 |
– |
} |
561 |
– |
|
562 |
– |
|
446 |
|
static int |
447 |
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
448 |
|
register C_COLOR *clr; |
458 |
|
double wl0, wlstep; |
459 |
|
double boxpos, boxstep; |
460 |
|
/* check bounds */ |
461 |
< |
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
461 |
> |
if ((wlmax <= C_CMINWL) | (wlmax <= wlmin) | (wlmin >= C_CMAXWL)) |
462 |
|
return(MG_EILL); |
463 |
|
wlstep = (wlmax - wlmin)/(ac-1); |
464 |
|
while (wlmin < C_CMINWL) { |
503 |
|
wl0 = wlmin; |
504 |
|
pos = 0; |
505 |
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
506 |
< |
if (wl < wlmin | wl > wlmax) |
506 |
> |
if ((wl < wlmin) | (wl > wlmax)) |
507 |
|
clr->ssamp[i] = 0; |
508 |
|
else { |
509 |
|
while (wl0 + wlstep < wl+FTINY) { |
510 |
|
wl0 += wlstep; |
511 |
|
pos++; |
512 |
|
} |
513 |
< |
if (wl+FTINY >= wl0 & wl-FTINY <= wl0) |
513 |
> |
if ((wl+FTINY >= wl0) & (wl-FTINY <= wl0)) |
514 |
|
clr->ssamp[i] = scale*va[pos] + .5; |
515 |
|
else /* interpolate if necessary */ |
516 |
|
clr->ssamp[i] = .5 + scale / wlstep * |
522 |
|
clr->clock++; |
523 |
|
return(MG_OK); |
524 |
|
} |
642 |
– |
|
643 |
– |
|
644 |
– |
static void |
645 |
– |
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
646 |
– |
register C_COLOR *cres, *c1, *c2; |
647 |
– |
double w1, w2; |
648 |
– |
{ |
649 |
– |
double scale; |
650 |
– |
float cmix[C_CNSS]; |
651 |
– |
register int i; |
652 |
– |
|
653 |
– |
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
654 |
– |
c_ccvt(c1, C_CSSPEC|C_CSEFF); |
655 |
– |
c_ccvt(c2, C_CSSPEC|C_CSEFF); |
656 |
– |
w1 /= c1->eff*c1->ssum; |
657 |
– |
w2 /= c2->eff*c2->ssum; |
658 |
– |
scale = 0.; |
659 |
– |
for (i = 0; i < C_CNSS; i++) { |
660 |
– |
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
661 |
– |
if (cmix[i] > scale) |
662 |
– |
scale = cmix[i]; |
663 |
– |
} |
664 |
– |
scale = C_CMAXV / scale; |
665 |
– |
cres->ssum = 0; |
666 |
– |
for (i = 0; i < C_CNSS; i++) |
667 |
– |
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
668 |
– |
cres->flags = C_CDSPEC|C_CSSPEC; |
669 |
– |
} else { /* CIE xy mixing */ |
670 |
– |
c_ccvt(c1, C_CSXY); |
671 |
– |
c_ccvt(c2, C_CSXY); |
672 |
– |
scale = w1/c1->cy + w2/c2->cy; |
673 |
– |
if (scale == 0.) |
674 |
– |
return; |
675 |
– |
scale = 1. / scale; |
676 |
– |
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
677 |
– |
cres->cy = (w1 + w2) * scale; |
678 |
– |
cres->flags = C_CDXY|C_CSXY; |
679 |
– |
} |
680 |
– |
} |
681 |
– |
|
682 |
– |
|
683 |
– |
#define C1 3.741832e-16 /* W-m^2 */ |
684 |
– |
#define C2 1.4388e-2 /* m-K */ |
685 |
– |
|
686 |
– |
#define bbsp(l,t) (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.))) |
687 |
– |
#define bblm(t) (C2/5./(t)) |
688 |
– |
|
689 |
– |
static int |
690 |
– |
setbbtemp(clr, tk) /* set black body spectrum */ |
691 |
– |
register C_COLOR *clr; |
692 |
– |
double tk; |
693 |
– |
{ |
694 |
– |
double sf, wl; |
695 |
– |
register int i; |
696 |
– |
|
697 |
– |
if (tk < 1000) |
698 |
– |
return(MG_EILL); |
699 |
– |
wl = bblm(tk); /* scalefactor based on peak */ |
700 |
– |
if (wl < C_CMINWL*1e-9) |
701 |
– |
wl = C_CMINWL*1e-9; |
702 |
– |
else if (wl > C_CMAXWL*1e-9) |
703 |
– |
wl = C_CMAXWL*1e-9; |
704 |
– |
sf = C_CMAXV/bbsp(wl,tk); |
705 |
– |
clr->ssum = 0; |
706 |
– |
for (i = 0; i < C_CNSS; i++) { |
707 |
– |
wl = (C_CMINWL + i*C_CWLI)*1e-9; |
708 |
– |
clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5; |
709 |
– |
} |
710 |
– |
clr->flags = C_CDSPEC|C_CSSPEC; |
711 |
– |
clr->clock++; |
712 |
– |
return(MG_OK); |
713 |
– |
} |
714 |
– |
|
715 |
– |
#undef C1 |
716 |
– |
#undef C2 |
717 |
– |
#undef bbsp |
718 |
– |
#undef bblm |