ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/ccolor.c
(Generate patch)

Comparing ray/src/common/ccolor.c (file contents):
Revision 3.3 by greg, Thu May 17 17:10:23 2012 UTC vs.
Revision 3.11 by greg, Thu Apr 13 00:42:01 2017 UTC

# Line 6 | Line 6 | static const char RCSid[] = "$Id$";
6   */
7  
8   #include <stdio.h>
9 + #include <stdlib.h>
10   #include <math.h>
11   #include "ccolor.h"
12  
13 + #undef frand
14 + #define frand() (rand()*(1./(RAND_MAX+.5)))
15  
16 < C_COLOR         c_dfcolor = C_DEFCOLOR;
16 >                                        /* Sharp primary matrix */
17 > float   XYZtoSharp[3][3] = {
18 >        { 1.2694, -0.0988, -0.1706},
19 >        {-0.8364,  1.8006,  0.0357},
20 >        { 0.0297, -0.0315,  1.0018}
21 > };
22 >                                        /* inverse Sharp primary matrix */
23 > float   XYZfromSharp[3][3] = {
24 >        { 0.8156,  0.0472,  0.1372},
25 >        { 0.3791,  0.5769,  0.0440},
26 >        {-0.0123,  0.0167,  0.9955}
27 > };
28  
29 + const C_COLOR   c_dfcolor = C_DEFCOLOR;
30 +
31 + const C_CHROMA  c_dfchroma = 49750;     /* c_encodeChroma(&c_dfcolor) */
32 +
33                                  /* CIE 1931 Standard Observer curves */
34 < static const C_COLOR    cie_xf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
34 > const C_COLOR   c_x31 = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
35                          {14,42,143,435,1344,2839,3483,3362,2908,1954,956,
36                          320,49,93,633,1655,2904,4334,5945,7621,9163,10263,
37                          10622,10026,8544,6424,4479,2835,1649,874,468,227,
38                          114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230
39                          };
40 < static const C_COLOR    cie_yf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
40 > const C_COLOR   c_y31 = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
41                          {0,1,4,12,40,116,230,380,600,910,1390,2080,3230,
42                          5030,7100,8620,9540,9950,9950,9520,8700,7570,6310,
43                          5030,3810,2650,1750,1070,610,320,170,82,41,21,10,
44                          5,2,1,1,0,0}, 106856L, .398, .542, 493.525
45                          };
46 < static const C_COLOR    cie_zf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
46 > const C_COLOR   c_z31 = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
47                          {65,201,679,2074,6456,13856,17471,17721,16692,
48                          12876,8130,4652,2720,1582,782,422,203,87,39,21,17,
49                          11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
# Line 57 | Line 75 | static const C_COLOR   cie_zp = { 1, NULL, C_CDSPEC|C_CS
75                          36057L, .0, .0,
76                          };
77  
60                                        /* Sharp primary matrix */
61 static const float      toSharp[3][3] = {
62        { 1.2694, -0.0988, -0.1706},
63        {-0.8364,  1.8006,  0.0357},
64        { 0.0297, -0.0315,  1.0018}
65 };
66                                        /* inverse Sharp primary matrix */
67 static const float      fromSharp[3][3] = {
68        { 0.8156,  0.0472,  0.1372},
69        { 0.3791,  0.5769,  0.0440},
70        {-0.0123,  0.0167,  0.9955}
71 };
78  
79 < static void
79 > /* convert to sharpened RGB color for low-error operations */
80 > void
81   c_toSharpRGB(C_COLOR *cin, double cieY, float cout[3])
82   {
83          double  xyz[3];
# Line 81 | Line 88 | c_toSharpRGB(C_COLOR *cin, double cieY, float cout[3])
88          xyz[1] = cieY;
89          xyz[2] = (1. - cin->cx - cin->cy)/cin->cy * cieY;
90  
91 <        cout[0] = toSharp[0][0]*xyz[0] + toSharp[0][1]*xyz[1] +
92 <                                toSharp[0][2]*xyz[2];
93 <        cout[1] = toSharp[1][0]*xyz[0] + toSharp[1][1]*xyz[1] +
94 <                                toSharp[1][2]*xyz[2];
95 <        cout[2] = toSharp[2][0]*xyz[0] + toSharp[2][1]*xyz[1] +
96 <                                toSharp[2][2]*xyz[2];
91 >        cout[0] = XYZtoSharp[0][0]*xyz[0] + XYZtoSharp[0][1]*xyz[1] +
92 >                                XYZtoSharp[0][2]*xyz[2];
93 >        cout[1] = XYZtoSharp[1][0]*xyz[0] + XYZtoSharp[1][1]*xyz[1] +
94 >                                XYZtoSharp[1][2]*xyz[2];
95 >        cout[2] = XYZtoSharp[2][0]*xyz[0] + XYZtoSharp[2][1]*xyz[1] +
96 >                                XYZtoSharp[2][2]*xyz[2];
97   }
98  
99 < static double
99 > /* convert back from sharpened RGB color */
100 > double
101   c_fromSharpRGB(float cin[3], C_COLOR *cout)
102   {
103          double  xyz[3], sf;
104  
105 <        xyz[0] = fromSharp[0][0]*cin[0] + fromSharp[0][1]*cin[1] +
106 <                                fromSharp[0][2]*cin[2];
107 <        xyz[1] = fromSharp[1][0]*cin[0] + fromSharp[1][1]*cin[1] +
108 <                                fromSharp[1][2]*cin[2];
109 <        xyz[2] = fromSharp[2][0]*cin[0] + fromSharp[2][1]*cin[1] +
110 <                                fromSharp[2][2]*cin[2];
111 <                                
105 >        xyz[1] = XYZfromSharp[1][0]*cin[0] + XYZfromSharp[1][1]*cin[1] +
106 >                                XYZfromSharp[1][2]*cin[2];
107 >        if (xyz[1] <= 1e-6) {
108 >                *cout = c_dfcolor;      /* punting, here... */
109 >                return xyz[1];
110 >        }
111 >        xyz[0] = XYZfromSharp[0][0]*cin[0] + XYZfromSharp[0][1]*cin[1] +
112 >                                XYZfromSharp[0][2]*cin[2];
113 >        xyz[2] = XYZfromSharp[2][0]*cin[0] + XYZfromSharp[2][1]*cin[1] +
114 >                                XYZfromSharp[2][2]*cin[2];
115 >        
116          sf = 1./(xyz[0] + xyz[1] + xyz[2]);
117  
118          cout->cx = xyz[0] * sf;
# Line 110 | Line 122 | c_fromSharpRGB(float cin[3], C_COLOR *cout)
122          return(xyz[1]);
123   }
124  
125 < /* assign arbitrary spectrum */
125 > /* assign arbitrary spectrum and return Y value */
126   double
127   c_sset(C_COLOR *clr, double wlmin, double wlmax, const float spec[], int nwl)
128   {
# Line 132 | Line 144 | c_sset(C_COLOR *clr, double wlmin, double wlmax, const
144                  wlmax -= wlstep;
145                  --nwl;
146          }
147 +        if ((nwl <= 1) | (wlmin >= wlmax))
148 +                return(0.);
149          imax = nwl;                     /* box filter if necessary */
150          boxpos = 0;
151          boxstep = 1;
# Line 157 | Line 171 | c_sset(C_COLOR *clr, double wlmin, double wlmax, const
171                          scale = va[i];
172                  else if (va[i] < -scale)
173                          scale = -va[i];
174 <                yval += va[i] * cie_yf.ssamp[i];
174 >                yval += va[i] * c_y31.ssamp[i];
175          }
176          if (scale <= 1e-7)
177                  return(0.);
178 <        yval /= (double)cie_yf.ssum;
178 >        yval /= (double)c_y31.ssum;
179          scale = C_CMAXV / scale;
180          clr->ssum = 0;                  /* convert to our spacing */
181          wl0 = wlmin;
# Line 175 | Line 189 | c_sset(C_COLOR *clr, double wlmin, double wlmax, const
189                                  pos++;
190                          }
191                          if ((wl+1e-7 >= wl0) & (wl-1e-7 <= wl0))
192 <                                clr->ssamp[i] = scale*va[pos] + .5;
192 >                                clr->ssamp[i] = scale*va[pos] + frand();
193                          else            /* interpolate if necessary */
194 <                                clr->ssamp[i] = .5 + scale / wlstep *
194 >                                clr->ssamp[i] = frand() + scale / wlstep *
195                                                  ( va[pos]*(wl0+wlstep - wl) +
196                                                          va[pos+1]*(wl - wl0) );
197                          clr->ssum += clr->ssamp[i];
# Line 216 | Line 230 | c_ccvt(C_COLOR *clr, int fl)
230          if (fl & C_CSXY) {              /* cspec -> cxy */
231                  x = y = z = 0.;
232                  for (i = 0; i < C_CNSS; i++) {
233 <                        x += cie_xf.ssamp[i] * clr->ssamp[i];
234 <                        y += cie_yf.ssamp[i] * clr->ssamp[i];
235 <                        z += cie_zf.ssamp[i] * clr->ssamp[i];
233 >                        x += c_x31.ssamp[i] * clr->ssamp[i];
234 >                        y += c_y31.ssamp[i] * clr->ssamp[i];
235 >                        z += c_z31.ssamp[i] * clr->ssamp[i];
236                  }
237 <                x /= (double)cie_xf.ssum;
238 <                y /= (double)cie_yf.ssum;
239 <                z /= (double)cie_zf.ssum;
237 >                x /= (double)c_x31.ssum;
238 >                y /= (double)c_y31.ssum;
239 >                z /= (double)c_z31.ssum;
240                  z += x + y;
241 <                clr->cx = x / z;
242 <                clr->cy = y / z;
241 >                if (z > 1e-6) {
242 >                        clr->cx = x / z;
243 >                        clr->cy = y / z;
244 >                } else
245 >                        clr->cx = clr->cy = 1./3.;
246                  clr->flags |= C_CSXY;
247 <        } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
247 >        }
248 >        if (fl & C_CSSPEC) {            /* cxy -> cspec */
249                  x = clr->cx;
250                  y = clr->cy;
251                  z = 1. - x - y;
252                  clr->ssum = 0;
253                  for (i = 0; i < C_CNSS; i++) {
254                          clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i]
255 <                                        + z*cie_zp.ssamp[i] + .5;
255 >                                        + z*cie_zp.ssamp[i] + frand();
256                          if (clr->ssamp[i] < 0)          /* out of gamut! */
257                                  clr->ssamp[i] = 0;
258                          else
# Line 246 | Line 264 | c_ccvt(C_COLOR *clr, int fl)
264                  if (clr->flags & C_CSSPEC) {            /* from spectrum */
265                          y = 0.;
266                          for (i = 0; i < C_CNSS; i++)
267 <                                y += cie_yf.ssamp[i] * clr->ssamp[i];
268 <                        clr->eff = C_CLPWM * y / clr->ssum;
267 >                                y += c_y31.ssamp[i] * clr->ssamp[i];
268 >                        clr->eff = C_CLPWM * y / (clr->ssum + 0.0001);
269                  } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
270 <                        clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
271 <                                        (1. - clr->cx - clr->cy)*cie_zf.eff;
270 >                        clr->eff = clr->cx*c_x31.eff + clr->cy*c_y31.eff +
271 >                                        (1. - clr->cx - clr->cy)*c_z31.eff;
272                  }
273                  clr->flags |= C_CSEFF;
274          }
# Line 263 | Line 281 | c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w
281          double  scale;
282          int     i;
283  
284 +        if (w1 == 0) {
285 +                *cres = *c2;
286 +                return;
287 +        }
288 +        if (w2 == 0) {
289 +                *cres = *c1;
290 +                return;
291 +        }
292          if ((c1->flags|c2->flags) & C_CDSPEC) {         /* spectral mixing */
293                  float   cmix[C_CNSS];
294  
295                  c_ccvt(c1, C_CSSPEC|C_CSEFF);
296                  c_ccvt(c2, C_CSSPEC|C_CSEFF);
297 +                if (c1->ssum*c2->ssum == 0) {
298 +                        *cres = c_dfcolor;
299 +                        return;
300 +                }
301                  w1 /= c1->eff*c1->ssum;
302                  w2 /= c2->eff*c2->ssum;
303                  scale = 0.;
# Line 281 | Line 311 | c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w
311                  scale = C_CMAXV / scale;
312                  cres->ssum = 0;
313                  for (i = 0; i < C_CNSS; i++)
314 <                        cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5;
314 >                        cres->ssum += cres->ssamp[i] = scale*cmix[i] + frand();
315                  cres->flags = C_CDSPEC|C_CSSPEC;
316          } else {                                        /* CIE xy mixing */
317                  c_ccvt(c1, C_CSXY);
# Line 329 | Line 359 | c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR
359                          cres->ssum += cres->ssamp[i] = cmix[i] / cmax;
360                  cres->flags = C_CDSPEC|C_CSSPEC;
361  
362 <                c_ccvt(cres, C_CSEFF);                  /* nasty, but true */
363 <                yres = y1 * y2 * cie_yf.ssum * C_CLPWM /
362 >                c_ccvt(cres, C_CSEFF);                  /* below is correct */
363 >                yres = y1 * y2 * c_y31.ssum * C_CLPWM /
364                          (c1->eff*c1->ssum * c2->eff*c2->ssum) *
365                          cres->eff*( cres->ssum*(double)cmax +
366                                                  C_CNSS/2.0*(cmax-1) );
# Line 371 | Line 401 | c_bbtemp(C_COLOR *clr, double tk)
401          clr->ssum = 0;
402          for (i = 0; i < C_CNSS; i++) {
403                  wl = (C_CMINWL + i*C_CWLI)*1e-9;
404 <                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5;
404 >                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + frand();
405          }
406          clr->flags = C_CDSPEC|C_CSSPEC;
407          return(1);
# Line 381 | Line 411 | c_bbtemp(C_COLOR *clr, double tk)
411   #undef  C2
412   #undef  bbsp
413   #undef  bblm
414 +
415 + #define UV_NORMF        410.
416 +
417 + /* encode (x,y) chromaticity */
418 + C_CHROMA
419 + c_encodeChroma(C_COLOR *clr)
420 + {
421 +        double  df;
422 +        int     ub, vb;
423 +
424 +        c_ccvt(clr, C_CSXY);
425 +        df = UV_NORMF/(-2.*clr->cx + 12.*clr->cy + 3.);
426 +        ub = 4.*clr->cx*df + frand();
427 +        if (ub > 0xff) ub = 0xff;
428 +        else ub *= (ub > 0);
429 +        vb = 9.*clr->cy*df + frand();
430 +        if (vb > 0xff) vb = 0xff;
431 +        else vb *= (vb > 0);
432 +
433 +        return(vb<<8 | ub);
434 + }
435 +
436 + /* decode (x,y) chromaticity */
437 + void
438 + c_decodeChroma(C_COLOR *cres, C_CHROMA ccode)
439 + {
440 +        double  up = (ccode & 0xff)*(1./UV_NORMF);
441 +        double  vp = (ccode>>8 & 0xff)*(1./UV_NORMF);
442 +        double  df = 1./(6.*up - 16.*vp + 12.);
443 +
444 +        cres->cx = 9.*up * df;
445 +        cres->cy = 4.*vp * df;
446 +        cres->flags = C_CDXY|C_CSXY;
447 + }
448 +
449 + #undef  UV_NORMF

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines