--- ray/src/common/ccolor.c 2012/05/17 05:47:59 3.2 +++ ray/src/common/ccolor.c 2012/05/18 20:43:13 3.4 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ccolor.c,v 3.2 2012/05/17 05:47:59 greg Exp $"; +static const char RCSid[] = "$Id: ccolor.c,v 3.4 2012/05/18 20:43:13 greg Exp $"; #endif /* * Spectral color handling routines @@ -9,6 +9,18 @@ static const char RCSid[] = "$Id: ccolor.c,v 3.2 2012/ #include #include "ccolor.h" + /* Sharp primary matrix */ +float XYZtoSharp[3][3] = { + { 1.2694, -0.0988, -0.1706}, + {-0.8364, 1.8006, 0.0357}, + { 0.0297, -0.0315, 1.0018} +}; + /* inverse Sharp primary matrix */ +float XYZfromSharp[3][3] = { + { 0.8156, 0.0472, 0.1372}, + { 0.3791, 0.5769, 0.0440}, + {-0.0123, 0.0167, 0.9955} +}; C_COLOR c_dfcolor = C_DEFCOLOR; @@ -57,20 +69,9 @@ static const C_COLOR cie_zp = { 1, NULL, C_CDSPEC|C_CS 36057L, .0, .0, }; - /* Sharp primary matrix */ -static const float toSharp[3][3] = { - { 1.2694, -0.0988, -0.1706}, - {-0.8364, 1.8006, 0.0357}, - { 0.0297, -0.0315, 1.0018} -}; - /* inverse Sharp primary matrix */ -static const float fromSharp[3][3] = { - { 0.8156, 0.0472, 0.1372}, - { 0.3791, 0.5769, 0.0440}, - {-0.0123, 0.0167, 0.9955} -}; -static void +/* convert to sharpened RGB color for low-error operations */ +void c_toSharpRGB(C_COLOR *cin, double cieY, float cout[3]) { double xyz[3]; @@ -81,25 +82,26 @@ c_toSharpRGB(C_COLOR *cin, double cieY, float cout[3]) xyz[1] = cieY; xyz[2] = (1. - cin->cx - cin->cy)/cin->cy * cieY; - cout[0] = toSharp[0][0]*xyz[0] + toSharp[0][1]*xyz[1] + - toSharp[0][2]*xyz[2]; - cout[1] = toSharp[1][0]*xyz[0] + toSharp[1][1]*xyz[1] + - toSharp[1][2]*xyz[2]; - cout[2] = toSharp[2][0]*xyz[0] + toSharp[2][1]*xyz[1] + - toSharp[2][2]*xyz[2]; + cout[0] = XYZtoSharp[0][0]*xyz[0] + XYZtoSharp[0][1]*xyz[1] + + XYZtoSharp[0][2]*xyz[2]; + cout[1] = XYZtoSharp[1][0]*xyz[0] + XYZtoSharp[1][1]*xyz[1] + + XYZtoSharp[1][2]*xyz[2]; + cout[2] = XYZtoSharp[2][0]*xyz[0] + XYZtoSharp[2][1]*xyz[1] + + XYZtoSharp[2][2]*xyz[2]; } -static double +/* convert back from sharpened RGB color */ +double c_fromSharpRGB(float cin[3], C_COLOR *cout) { double xyz[3], sf; - xyz[0] = fromSharp[0][0]*cin[0] + fromSharp[0][1]*cin[1] + - fromSharp[0][2]*cin[2]; - xyz[1] = fromSharp[1][0]*cin[0] + fromSharp[1][1]*cin[1] + - fromSharp[1][2]*cin[2]; - xyz[2] = fromSharp[2][0]*cin[0] + fromSharp[2][1]*cin[1] + - fromSharp[2][2]*cin[2]; + xyz[0] = XYZfromSharp[0][0]*cin[0] + XYZfromSharp[0][1]*cin[1] + + XYZfromSharp[0][2]*cin[2]; + xyz[1] = XYZfromSharp[1][0]*cin[0] + XYZfromSharp[1][1]*cin[1] + + XYZfromSharp[1][2]*cin[2]; + xyz[2] = XYZfromSharp[2][0]*cin[0] + XYZfromSharp[2][1]*cin[1] + + XYZfromSharp[2][2]*cin[2]; sf = 1./(xyz[0] + xyz[1] + xyz[2]); @@ -110,6 +112,82 @@ c_fromSharpRGB(float cin[3], C_COLOR *cout) return(xyz[1]); } +/* assign arbitrary spectrum */ +double +c_sset(C_COLOR *clr, double wlmin, double wlmax, const float spec[], int nwl) +{ + double yval, scale; + float va[C_CNSS]; + int i, pos, n, imax, wl; + double wl0, wlstep; + double boxpos, boxstep; + /* check arguments */ + if ((nwl <= 1) | (spec == NULL) | (wlmin >= C_CMAXWL) | + (wlmax <= C_CMINWL) | (wlmin >= wlmax)) + return(0.); + wlstep = (wlmax - wlmin)/(nwl-1); + while (wlmin < C_CMINWL) { + wlmin += wlstep; + --nwl; ++spec; + } + while (wlmax > C_CMAXWL) { + wlmax -= wlstep; + --nwl; + } + imax = nwl; /* box filter if necessary */ + boxpos = 0; + boxstep = 1; + if (wlstep < C_CWLI) { + imax = (wlmax - wlmin)/C_CWLI + 1e-7; + boxpos = (wlmin - C_CMINWL)/C_CWLI; + boxstep = wlstep/C_CWLI; + wlstep = C_CWLI; + } + scale = 0.; /* get values and maximum */ + yval = 0.; + pos = 0; + for (i = 0; i < imax; i++) { + va[i] = 0.; n = 0; + while (boxpos < i+.5 && pos < nwl) { + va[i] += spec[pos++]; + n++; + boxpos += boxstep; + } + if (n > 1) + va[i] /= (double)n; + if (va[i] > scale) + scale = va[i]; + else if (va[i] < -scale) + scale = -va[i]; + yval += va[i] * cie_yf.ssamp[i]; + } + if (scale <= 1e-7) + return(0.); + yval /= (double)cie_yf.ssum; + scale = C_CMAXV / scale; + clr->ssum = 0; /* convert to our spacing */ + wl0 = wlmin; + pos = 0; + for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) + if ((wl < wlmin) | (wl > wlmax)) + clr->ssamp[i] = 0; + else { + while (wl0 + wlstep < wl+1e-7) { + wl0 += wlstep; + pos++; + } + if ((wl+1e-7 >= wl0) & (wl-1e-7 <= wl0)) + clr->ssamp[i] = scale*va[pos] + .5; + else /* interpolate if necessary */ + clr->ssamp[i] = .5 + scale / wlstep * + ( va[pos]*(wl0+wlstep - wl) + + va[pos+1]*(wl - wl0) ); + clr->ssum += clr->ssamp[i]; + } + clr->flags = C_CDSPEC|C_CSSPEC; + return(yval); +} + /* check if color is grey */ int c_isgrey(C_COLOR *clr) @@ -199,6 +277,8 @@ c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; if (cmix[i] > scale) scale = cmix[i]; + else if (cmix[i] < -scale) + scale = -cmix[i]; } scale = C_CMAXV / scale; cres->ssum = 0; @@ -238,6 +318,8 @@ c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR cmix[i] = c1->ssamp[i] * c2->ssamp[i]; if (cmix[i] > cmax) cmax = cmix[i]; + else if (cmix[i] < -cmax) + cmax = -cmix[i]; } cmax /= C_CMAXV; if (!cmax) { @@ -250,7 +332,7 @@ c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR cres->flags = C_CDSPEC|C_CSSPEC; c_ccvt(cres, C_CSEFF); /* nasty, but true */ - yres = (y1 * y2 * cie_yf.ssum * C_CLPWM) / + yres = y1 * y2 * cie_yf.ssum * C_CLPWM / (c1->eff*c1->ssum * c2->eff*c2->ssum) * cres->eff*( cres->ssum*(double)cmax + C_CNSS/2.0*(cmax-1) );