| 1 |
< |
/* Copyright (c) 1994 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1997 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 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(); |
| 105 |
|
c_ccname = NULL; |
| 106 |
|
return(MG_OK); |
| 107 |
|
} |
| 108 |
+ |
if (!isname(av[1])) |
| 109 |
+ |
return(MG_EILL); |
| 110 |
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
| 111 |
|
if (lp == NULL) |
| 112 |
|
return(MG_EMEM); |
| 177 |
|
if (!isflt(av[1])) |
| 178 |
|
return(MG_ETYPE); |
| 179 |
|
wsum = atof(av[1]); |
| 138 |
– |
if (wsum < 0.) |
| 139 |
– |
return(MG_EILL); |
| 180 |
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
| 181 |
|
return(MG_EMEM); |
| 182 |
|
if (lp->data == NULL) |
| 186 |
|
if (!isflt(av[i])) |
| 187 |
|
return(MG_ETYPE); |
| 188 |
|
w = atof(av[i]); |
| 149 |
– |
if (w < 0.) |
| 150 |
– |
return(MG_EILL); |
| 189 |
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
| 190 |
|
return(MG_EMEM); |
| 191 |
|
if (lp->data == NULL) |
| 194 |
|
w, (C_COLOR *)lp->data); |
| 195 |
|
wsum += w; |
| 196 |
|
} |
| 197 |
+ |
if (wsum <= 0.) |
| 198 |
+ |
return(MG_EILL); |
| 199 |
|
c_ccolor->clock++; |
| 200 |
|
return(MG_OK); |
| 201 |
|
} |
| 221 |
|
c_cmname = NULL; |
| 222 |
|
return(MG_OK); |
| 223 |
|
} |
| 224 |
+ |
if (!isname(av[1])) |
| 225 |
+ |
return(MG_EILL); |
| 226 |
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
| 227 |
|
if (lp == NULL) |
| 228 |
|
return(MG_EMEM); |
| 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); |
| 527 |
|
clr->cy = y / z; |
| 528 |
|
clr->flags |= C_CSXY; |
| 529 |
|
} else if (fl & C_CSSPEC) { /* cxy -> cspec */ |
| 530 |
< |
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
| 531 |
< |
x = clr->cx * z / cie_xf.ssum; |
| 532 |
< |
y = clr->cy * z / cie_yf.ssum; |
| 478 |
< |
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum; |
| 530 |
> |
x = clr->cx; |
| 531 |
> |
y = clr->cy; |
| 532 |
> |
z = 1. - x - y; |
| 533 |
|
clr->ssum = 0; |
| 534 |
< |
for (i = 0; i < C_CNSS; i++) |
| 535 |
< |
clr->ssum += clr->ssamp[i] = |
| 536 |
< |
x * cie_xf.ssamp[i] + |
| 537 |
< |
y * cie_yf.ssamp[i] + |
| 538 |
< |
z * cie_zf.ssamp[i] + .5; |
| 534 |
> |
for (i = 0; i < C_CNSS; i++) { |
| 535 |
> |
clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i] |
| 536 |
> |
+ z*cie_zp.ssamp[i] + .5; |
| 537 |
> |
if (clr->ssamp[i] < 0) /* out of gamut! */ |
| 538 |
> |
clr->ssamp[i] = 0; |
| 539 |
> |
else |
| 540 |
> |
clr->ssum += clr->ssamp[i]; |
| 541 |
> |
} |
| 542 |
|
clr->flags |= C_CSSPEC; |
| 543 |
|
} |
| 544 |
|
if (fl & C_CSEFF) { /* compute efficacy */ |
| 545 |
< |
if (clr->flags & C_CDSPEC) { /* from spectrum */ |
| 545 |
> |
if (clr->flags & C_CSSPEC) { /* from spectrum */ |
| 546 |
|
y = 0.; |
| 547 |
|
for (i = 0; i < C_CNSS; i++) |
| 548 |
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
| 549 |
|
clr->eff = C_CLPWM * y / clr->ssum; |
| 550 |
< |
} else /* clr->flags & C_CDXY */ { /* from (x,y) */ |
| 550 |
> |
} else /* clr->flags & C_CSXY */ { /* from (x,y) */ |
| 551 |
|
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff + |
| 552 |
|
(1. - clr->cx - clr->cy)*cie_zf.eff; |
| 553 |
|
} |
| 569 |
|
int n, imax; |
| 570 |
|
int wl; |
| 571 |
|
double wl0, wlstep; |
| 572 |
+ |
double boxpos, boxstep; |
| 573 |
|
/* check bounds */ |
| 574 |
|
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
| 575 |
|
return(MG_EILL); |
| 582 |
|
wlmax -= wlstep; |
| 583 |
|
ac--; |
| 584 |
|
} |
| 527 |
– |
if (ac < 2) |
| 528 |
– |
return(MG_EILL); |
| 585 |
|
imax = ac; /* box filter if necessary */ |
| 586 |
+ |
boxpos = 0; |
| 587 |
+ |
boxstep = 1; |
| 588 |
|
if (wlstep < C_CWLI) { |
| 589 |
+ |
imax = (wlmax - wlmin)/C_CWLI + (1-FTINY); |
| 590 |
+ |
boxpos = (wlmin - C_CMINWL)/C_CWLI; |
| 591 |
+ |
boxstep = wlstep/C_CWLI; |
| 592 |
|
wlstep = C_CWLI; |
| 532 |
– |
imax = (wlmax - wlmin)/wlstep; |
| 593 |
|
} |
| 594 |
|
scale = 0.; /* get values and maximum */ |
| 595 |
|
pos = 0; |
| 596 |
|
for (i = 0; i < imax; i++) { |
| 597 |
|
va[i] = 0.; n = 0; |
| 598 |
< |
while (pos < (i+.5)*ac/imax) { |
| 598 |
> |
while (boxpos < i+.5 && pos < ac) { |
| 599 |
|
if (!isflt(av[pos])) |
| 600 |
|
return(MG_ETYPE); |
| 601 |
|
va[i] += atof(av[pos++]); |
| 602 |
|
n++; |
| 603 |
+ |
boxpos += boxstep; |
| 604 |
|
} |
| 605 |
|
if (n > 1) |
| 606 |
|
va[i] /= (double)n; |
| 546 |
– |
if (va[i] < 0.) |
| 547 |
– |
return(MG_EILL); |
| 607 |
|
if (va[i] > scale) |
| 608 |
|
scale = va[i]; |
| 609 |
+ |
else if (va[i] < -scale) |
| 610 |
+ |
scale = -va[i]; |
| 611 |
|
} |
| 612 |
< |
if (scale == 0.) |
| 612 |
> |
if (scale <= FTINY) |
| 613 |
|
return(MG_EILL); |
| 614 |
|
scale = C_CMAXV / scale; |
| 615 |
|
clr->ssum = 0; /* convert to our spacing */ |
| 665 |
|
} else { /* CIE xy mixing */ |
| 666 |
|
c_ccvt(c1, C_CSXY); |
| 667 |
|
c_ccvt(c2, C_CSXY); |
| 668 |
< |
scale = 1. / (w1/c1->cy + w2/c2->cy); |
| 668 |
> |
scale = w1/c1->cy + w2/c2->cy; |
| 669 |
> |
if (scale == 0.) |
| 670 |
> |
return; |
| 671 |
> |
scale = 1. / scale; |
| 672 |
|
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
| 673 |
|
cres->cy = (w1 + w2) * scale; |
| 674 |
|
cres->flags = C_CDXY|C_CSXY; |