1 |
– |
/* Copyright (c) 1994 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> |
10 |
+ |
#include <stdlib.h> |
11 |
|
#include <string.h> |
12 |
|
#include "parser.h" |
13 |
|
#include "lookup.h" |
35 |
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
36 |
|
|
37 |
|
/* CIE 1931 Standard Observer curves */ |
38 |
< |
static C_COLOR cie_xf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
38 |
> |
static C_COLOR cie_xf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
39 |
|
{14,42,143,435,1344,2839,3483,3362,2908,1954,956, |
40 |
|
320,49,93,633,1655,2904,4334,5945,7621,9163,10263, |
41 |
|
10622,10026,8544,6424,4479,2835,1649,874,468,227, |
42 |
|
114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230 |
43 |
|
}; |
44 |
< |
static C_COLOR cie_yf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
44 |
> |
static C_COLOR cie_yf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
45 |
|
{0,1,4,12,40,116,230,380,600,910,1390,2080,3230, |
46 |
|
5030,7100,8620,9540,9950,9950,9520,8700,7570,6310, |
47 |
|
5030,3810,2650,1750,1070,610,320,170,82,41,21,10, |
48 |
|
5,2,1,1,0,0}, 106856L, .398, .542, 493.525 |
49 |
|
}; |
50 |
< |
static C_COLOR cie_zf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
50 |
> |
static C_COLOR cie_zf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
51 |
|
{65,201,679,2074,6456,13856,17471,17721,16692, |
52 |
|
12876,8130,4652,2720,1582,782,422,203,87,39,21,17, |
53 |
|
11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, |
54 |
|
106770L, .147, .077, 54.363 |
55 |
|
}; |
56 |
|
/* Derived CIE 1931 Primaries (imaginary) */ |
57 |
< |
static C_COLOR cie_xp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
57 |
> |
static C_COLOR cie_xp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY, |
58 |
|
{-174,-198,-195,-197,-202,-213,-235,-272,-333, |
59 |
|
-444,-688,-1232,-2393,-4497,-6876,-6758,-5256, |
60 |
|
-3100,-815,1320,3200,4782,5998,6861,7408,7754, |
62 |
|
8336,8336,8336,8336,8336,8336}, |
63 |
|
127424L, 1., .0, |
64 |
|
}; |
65 |
< |
static C_COLOR cie_yp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
65 |
> |
static C_COLOR cie_yp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY, |
66 |
|
{-451,-431,-431,-430,-427,-417,-399,-366,-312, |
67 |
|
-204,57,691,2142,4990,8810,9871,9122,7321,5145, |
68 |
|
3023,1123,-473,-1704,-2572,-3127,-3474,-3704, |
70 |
|
-4066,-4066,-4066,-4066,-4066,-4066}, |
71 |
|
-23035L, .0, 1., |
72 |
|
}; |
73 |
< |
static C_COLOR cie_zp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY, |
73 |
> |
static C_COLOR cie_zp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY, |
74 |
|
{4051,4054,4052,4053,4054,4056,4059,4064,4071, |
75 |
|
4074,4056,3967,3677,2933,1492,313,-440,-795, |
76 |
|
-904,-918,-898,-884,-869,-863,-855,-855,-851, |
103 |
|
c_ccname = NULL; |
104 |
|
return(MG_OK); |
105 |
|
} |
106 |
+ |
if (!isname(av[1])) |
107 |
+ |
return(MG_EILL); |
108 |
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
109 |
|
if (lp == NULL) |
110 |
|
return(MG_EMEM); |
128 |
|
c_ccname = lp->key; |
129 |
|
c_ccolor = (C_COLOR *)lp->data; |
130 |
|
c_ccolor->clock = 0; |
131 |
+ |
c_ccolor->client_data = NULL; |
132 |
|
} |
133 |
|
i = c_ccolor->clock; |
134 |
|
if (ac == 3) { /* use default template */ |
176 |
|
if (!isflt(av[1])) |
177 |
|
return(MG_ETYPE); |
178 |
|
wsum = atof(av[1]); |
178 |
– |
if (wsum < 0.) |
179 |
– |
return(MG_EILL); |
179 |
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
180 |
|
return(MG_EMEM); |
181 |
|
if (lp->data == NULL) |
185 |
|
if (!isflt(av[i])) |
186 |
|
return(MG_ETYPE); |
187 |
|
w = atof(av[i]); |
189 |
– |
if (w < 0.) |
190 |
– |
return(MG_EILL); |
188 |
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
189 |
|
return(MG_EMEM); |
190 |
|
if (lp->data == NULL) |
193 |
|
w, (C_COLOR *)lp->data); |
194 |
|
wsum += w; |
195 |
|
} |
196 |
+ |
if (wsum <= 0.) |
197 |
+ |
return(MG_EILL); |
198 |
|
c_ccolor->clock++; |
199 |
|
return(MG_OK); |
200 |
|
} |
220 |
|
c_cmname = NULL; |
221 |
|
return(MG_OK); |
222 |
|
} |
223 |
+ |
if (!isname(av[1])) |
224 |
+ |
return(MG_EILL); |
225 |
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
226 |
|
if (lp == NULL) |
227 |
|
return(MG_EMEM); |
245 |
|
c_cmname = lp->key; |
246 |
|
c_cmaterial = (C_MATERIAL *)lp->data; |
247 |
|
c_cmaterial->clock = 0; |
248 |
+ |
c_cmaterial->client_data = NULL; |
249 |
|
} |
250 |
|
i = c_cmaterial->clock; |
251 |
|
if (ac == 3) { /* use default template */ |
261 |
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
262 |
|
c_cmaterial->clock = i + 1; |
263 |
|
return(MG_OK); |
264 |
+ |
case MG_E_IR: /* set index of refraction */ |
265 |
+ |
if (ac != 3) |
266 |
+ |
return(MG_EARGC); |
267 |
+ |
if (!isflt(av[1]) | !isflt(av[2])) |
268 |
+ |
return(MG_ETYPE); |
269 |
+ |
c_cmaterial->nr = atof(av[1]); |
270 |
+ |
c_cmaterial->ni = atof(av[2]); |
271 |
+ |
if (c_cmaterial->nr <= FTINY) |
272 |
+ |
return(MG_EILL); |
273 |
+ |
c_cmaterial->clock++; |
274 |
+ |
return(MG_OK); |
275 |
|
case MG_E_RD: /* set diffuse reflectance */ |
276 |
|
if (ac != 2) |
277 |
|
return(MG_EARGC); |
368 |
|
c_cvname = NULL; |
369 |
|
return(MG_OK); |
370 |
|
} |
371 |
+ |
if (!isname(av[1])) |
372 |
+ |
return(MG_EILL); |
373 |
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
374 |
|
if (lp == NULL) |
375 |
|
return(MG_EMEM); |
392 |
|
return(MG_EMEM); |
393 |
|
c_cvname = lp->key; |
394 |
|
c_cvertex = (C_VERTEX *)lp->data; |
395 |
+ |
c_cvertex->clock = 0; |
396 |
+ |
c_cvertex->client_data = NULL; |
397 |
|
} |
398 |
|
i = c_cvertex->clock; |
399 |
|
if (ac == 3) { /* use default template */ |
571 |
|
int n, imax; |
572 |
|
int wl; |
573 |
|
double wl0, wlstep; |
574 |
+ |
double boxpos, boxstep; |
575 |
|
/* check bounds */ |
576 |
|
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
577 |
|
return(MG_EILL); |
584 |
|
wlmax -= wlstep; |
585 |
|
ac--; |
586 |
|
} |
569 |
– |
if (ac < 2) |
570 |
– |
return(MG_EILL); |
587 |
|
imax = ac; /* box filter if necessary */ |
588 |
+ |
boxpos = 0; |
589 |
+ |
boxstep = 1; |
590 |
|
if (wlstep < C_CWLI) { |
591 |
+ |
imax = (wlmax - wlmin)/C_CWLI + (1-FTINY); |
592 |
+ |
boxpos = (wlmin - C_CMINWL)/C_CWLI; |
593 |
+ |
boxstep = wlstep/C_CWLI; |
594 |
|
wlstep = C_CWLI; |
574 |
– |
imax = (wlmax - wlmin)/wlstep; |
595 |
|
} |
596 |
|
scale = 0.; /* get values and maximum */ |
597 |
|
pos = 0; |
598 |
|
for (i = 0; i < imax; i++) { |
599 |
|
va[i] = 0.; n = 0; |
600 |
< |
while (pos < (i+.5)*ac/imax) { |
600 |
> |
while (boxpos < i+.5 && pos < ac) { |
601 |
|
if (!isflt(av[pos])) |
602 |
|
return(MG_ETYPE); |
603 |
|
va[i] += atof(av[pos++]); |
604 |
|
n++; |
605 |
+ |
boxpos += boxstep; |
606 |
|
} |
607 |
|
if (n > 1) |
608 |
|
va[i] /= (double)n; |
588 |
– |
if (va[i] < 0.) |
589 |
– |
return(MG_EILL); |
609 |
|
if (va[i] > scale) |
610 |
|
scale = va[i]; |
611 |
+ |
else if (va[i] < -scale) |
612 |
+ |
scale = -va[i]; |
613 |
|
} |
614 |
< |
if (scale == 0.) |
614 |
> |
if (scale <= FTINY) |
615 |
|
return(MG_EILL); |
616 |
|
scale = C_CMAXV / scale; |
617 |
|
clr->ssum = 0; /* convert to our spacing */ |
667 |
|
} else { /* CIE xy mixing */ |
668 |
|
c_ccvt(c1, C_CSXY); |
669 |
|
c_ccvt(c2, C_CSXY); |
670 |
< |
scale = 1. / (w1/c1->cy + w2/c2->cy); |
670 |
> |
scale = w1/c1->cy + w2/c2->cy; |
671 |
> |
if (scale == 0.) |
672 |
> |
return; |
673 |
> |
scale = 1. / scale; |
674 |
|
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
675 |
|
cres->cy = (w1 + w2) * scale; |
676 |
|
cres->flags = C_CDXY|C_CSXY; |