--- ray/src/common/ccolor.c 2015/04/05 01:32:01 3.8 +++ ray/src/common/ccolor.c 2016/01/23 18:58:35 3.9 @@ -1,14 +1,18 @@ #ifndef lint -static const char RCSid[] = "$Id: ccolor.c,v 3.8 2015/04/05 01:32:01 greg Exp $"; +static const char RCSid[] = "$Id: ccolor.c,v 3.9 2016/01/23 18:58:35 greg Exp $"; #endif /* * Spectral color handling routines */ #include +#include #include #include "ccolor.h" +#undef frand +#define frand() (rand()*(1./(RAND_MAX+.5))) + /* Sharp primary matrix */ float XYZtoSharp[3][3] = { { 1.2694, -0.0988, -0.1706}, @@ -24,6 +28,8 @@ float XYZfromSharp[3][3] = { const C_COLOR c_dfcolor = C_DEFCOLOR; +const C_CHROMA c_dfchroma = 49750; /* c_encodeChroma(&c_dfcolor) */ + /* CIE 1931 Standard Observer curves */ const C_COLOR c_x31 = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, {14,42,143,435,1344,2839,3483,3362,2908,1954,956, @@ -183,9 +189,9 @@ c_sset(C_COLOR *clr, double wlmin, double wlmax, const pos++; } if ((wl+1e-7 >= wl0) & (wl-1e-7 <= wl0)) - clr->ssamp[i] = scale*va[pos] + .5; + clr->ssamp[i] = scale*va[pos] + frand(); else /* interpolate if necessary */ - clr->ssamp[i] = .5 + scale / wlstep * + clr->ssamp[i] = frand() + scale / wlstep * ( va[pos]*(wl0+wlstep - wl) + va[pos+1]*(wl - wl0) ); clr->ssum += clr->ssamp[i]; @@ -243,7 +249,7 @@ c_ccvt(C_COLOR *clr, int fl) clr->ssum = 0; for (i = 0; i < C_CNSS; i++) { clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i] - + z*cie_zp.ssamp[i] + .5; + + z*cie_zp.ssamp[i] + frand(); if (clr->ssamp[i] < 0) /* out of gamut! */ clr->ssamp[i] = 0; else @@ -290,7 +296,7 @@ c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w scale = C_CMAXV / scale; cres->ssum = 0; for (i = 0; i < C_CNSS; i++) - cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; + cres->ssum += cres->ssamp[i] = scale*cmix[i] + frand(); cres->flags = C_CDSPEC|C_CSSPEC; } else { /* CIE xy mixing */ c_ccvt(c1, C_CSXY); @@ -380,7 +386,7 @@ c_bbtemp(C_COLOR *clr, double tk) clr->ssum = 0; for (i = 0; i < C_CNSS; i++) { wl = (C_CMINWL + i*C_CWLI)*1e-9; - clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5; + clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + frand(); } clr->flags = C_CDSPEC|C_CSSPEC; return(1); @@ -402,12 +408,12 @@ c_encodeChroma(C_COLOR *clr) c_ccvt(clr, C_CSXY); df = UV_NORMF/(-2.*clr->cx + 12.*clr->cy + 3.); - ub = 4.*clr->cx * df; - if (ub < 0) ub = 0; - else if (ub > 0xff) ub = 0xff; - vb = 9.*clr->cy * df; - if (vb < 0) vb = 0; - else if (vb > 0xff) vb = 0xff; + ub = 4.*clr->cx*df + frand(); + ub *= (ub > 0); + if (ub > 0xff) ub = 0xff; + vb = 9.*clr->cy*df + frand(); + vb *= (vb > 0); + if (vb > 0xff) vb = 0xff; return(vb<<8 | ub); } @@ -416,8 +422,8 @@ c_encodeChroma(C_COLOR *clr) void c_decodeChroma(C_COLOR *cres, C_CHROMA ccode) { - double up = ((ccode & 0xff) + .5)*(1./UV_NORMF); - double vp = ((ccode>>8 & 0xff) + .5)*(1./UV_NORMF); + double up = (ccode & 0xff)*(1./UV_NORMF); + double vp = (ccode>>8 & 0xff)*(1./UV_NORMF); double df = 1./(6.*up - 16.*vp + 12.); cres->cx = 9.*up * df;