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.6 by greg, Tue Jul 17 23:40:24 2012 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9   #include <math.h>
10   #include "ccolor.h"
11  
12 +                                        /* Sharp primary matrix */
13 + float   XYZtoSharp[3][3] = {
14 +        { 1.2694, -0.0988, -0.1706},
15 +        {-0.8364,  1.8006,  0.0357},
16 +        { 0.0297, -0.0315,  1.0018}
17 + };
18 +                                        /* inverse Sharp primary matrix */
19 + float   XYZfromSharp[3][3] = {
20 +        { 0.8156,  0.0472,  0.1372},
21 +        { 0.3791,  0.5769,  0.0440},
22 +        {-0.0123,  0.0167,  0.9955}
23 + };
24  
25 < C_COLOR         c_dfcolor = C_DEFCOLOR;
25 > const C_COLOR   c_dfcolor = C_DEFCOLOR;
26  
27                                  /* CIE 1931 Standard Observer curves */
28 < static const C_COLOR    cie_xf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
28 > const C_COLOR   c_x31 = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
29                          {14,42,143,435,1344,2839,3483,3362,2908,1954,956,
30                          320,49,93,633,1655,2904,4334,5945,7621,9163,10263,
31                          10622,10026,8544,6424,4479,2835,1649,874,468,227,
32                          114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230
33                          };
34 < static const C_COLOR    cie_yf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
34 > const C_COLOR   c_y31 = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
35                          {0,1,4,12,40,116,230,380,600,910,1390,2080,3230,
36                          5030,7100,8620,9540,9950,9950,9520,8700,7570,6310,
37                          5030,3810,2650,1750,1070,610,320,170,82,41,21,10,
38                          5,2,1,1,0,0}, 106856L, .398, .542, 493.525
39                          };
40 < static const C_COLOR    cie_zf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
40 > const C_COLOR   c_z31 = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
41                          {65,201,679,2074,6456,13856,17471,17721,16692,
42                          12876,8130,4652,2720,1582,782,422,203,87,39,21,17,
43                          11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
# Line 57 | Line 69 | static const C_COLOR   cie_zp = { 1, NULL, C_CDSPEC|C_CS
69                          36057L, .0, .0,
70                          };
71  
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 };
72  
73 < static void
73 > /* convert to sharpened RGB color for low-error operations */
74 > void
75   c_toSharpRGB(C_COLOR *cin, double cieY, float cout[3])
76   {
77          double  xyz[3];
# Line 81 | Line 82 | c_toSharpRGB(C_COLOR *cin, double cieY, float cout[3])
82          xyz[1] = cieY;
83          xyz[2] = (1. - cin->cx - cin->cy)/cin->cy * cieY;
84  
85 <        cout[0] = toSharp[0][0]*xyz[0] + toSharp[0][1]*xyz[1] +
86 <                                toSharp[0][2]*xyz[2];
87 <        cout[1] = toSharp[1][0]*xyz[0] + toSharp[1][1]*xyz[1] +
88 <                                toSharp[1][2]*xyz[2];
89 <        cout[2] = toSharp[2][0]*xyz[0] + toSharp[2][1]*xyz[1] +
90 <                                toSharp[2][2]*xyz[2];
85 >        cout[0] = XYZtoSharp[0][0]*xyz[0] + XYZtoSharp[0][1]*xyz[1] +
86 >                                XYZtoSharp[0][2]*xyz[2];
87 >        cout[1] = XYZtoSharp[1][0]*xyz[0] + XYZtoSharp[1][1]*xyz[1] +
88 >                                XYZtoSharp[1][2]*xyz[2];
89 >        cout[2] = XYZtoSharp[2][0]*xyz[0] + XYZtoSharp[2][1]*xyz[1] +
90 >                                XYZtoSharp[2][2]*xyz[2];
91   }
92  
93 < static double
93 > /* convert back from sharpened RGB color */
94 > double
95   c_fromSharpRGB(float cin[3], C_COLOR *cout)
96   {
97          double  xyz[3], sf;
98  
99 <        xyz[0] = fromSharp[0][0]*cin[0] + fromSharp[0][1]*cin[1] +
100 <                                fromSharp[0][2]*cin[2];
101 <        xyz[1] = fromSharp[1][0]*cin[0] + fromSharp[1][1]*cin[1] +
102 <                                fromSharp[1][2]*cin[2];
103 <        xyz[2] = fromSharp[2][0]*cin[0] + fromSharp[2][1]*cin[1] +
104 <                                fromSharp[2][2]*cin[2];
99 >        xyz[0] = XYZfromSharp[0][0]*cin[0] + XYZfromSharp[0][1]*cin[1] +
100 >                                XYZfromSharp[0][2]*cin[2];
101 >        xyz[1] = XYZfromSharp[1][0]*cin[0] + XYZfromSharp[1][1]*cin[1] +
102 >                                XYZfromSharp[1][2]*cin[2];
103 >        xyz[2] = XYZfromSharp[2][0]*cin[0] + XYZfromSharp[2][1]*cin[1] +
104 >                                XYZfromSharp[2][2]*cin[2];
105                                  
106          sf = 1./(xyz[0] + xyz[1] + xyz[2]);
107  
# Line 110 | Line 112 | c_fromSharpRGB(float cin[3], C_COLOR *cout)
112          return(xyz[1]);
113   }
114  
115 < /* assign arbitrary spectrum */
115 > /* assign arbitrary spectrum and return Y value */
116   double
117   c_sset(C_COLOR *clr, double wlmin, double wlmax, const float spec[], int nwl)
118   {
# Line 132 | Line 134 | c_sset(C_COLOR *clr, double wlmin, double wlmax, const
134                  wlmax -= wlstep;
135                  --nwl;
136          }
137 +        if ((nwl <= 1) | (wlmin >= wlmax))
138 +                return(0.);
139          imax = nwl;                     /* box filter if necessary */
140          boxpos = 0;
141          boxstep = 1;
# Line 157 | Line 161 | c_sset(C_COLOR *clr, double wlmin, double wlmax, const
161                          scale = va[i];
162                  else if (va[i] < -scale)
163                          scale = -va[i];
164 <                yval += va[i] * cie_yf.ssamp[i];
164 >                yval += va[i] * c_y31.ssamp[i];
165          }
166          if (scale <= 1e-7)
167                  return(0.);
168 <        yval /= (double)cie_yf.ssum;
168 >        yval /= (double)c_y31.ssum;
169          scale = C_CMAXV / scale;
170          clr->ssum = 0;                  /* convert to our spacing */
171          wl0 = wlmin;
# Line 216 | Line 220 | c_ccvt(C_COLOR *clr, int fl)
220          if (fl & C_CSXY) {              /* cspec -> cxy */
221                  x = y = z = 0.;
222                  for (i = 0; i < C_CNSS; i++) {
223 <                        x += cie_xf.ssamp[i] * clr->ssamp[i];
224 <                        y += cie_yf.ssamp[i] * clr->ssamp[i];
225 <                        z += cie_zf.ssamp[i] * clr->ssamp[i];
223 >                        x += c_x31.ssamp[i] * clr->ssamp[i];
224 >                        y += c_y31.ssamp[i] * clr->ssamp[i];
225 >                        z += c_z31.ssamp[i] * clr->ssamp[i];
226                  }
227 <                x /= (double)cie_xf.ssum;
228 <                y /= (double)cie_yf.ssum;
229 <                z /= (double)cie_zf.ssum;
227 >                x /= (double)c_x31.ssum;
228 >                y /= (double)c_y31.ssum;
229 >                z /= (double)c_z31.ssum;
230                  z += x + y;
231                  clr->cx = x / z;
232                  clr->cy = y / z;
233                  clr->flags |= C_CSXY;
234 <        } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
234 >        }
235 >        if (fl & C_CSSPEC) {            /* cxy -> cspec */
236                  x = clr->cx;
237                  y = clr->cy;
238                  z = 1. - x - y;
# Line 246 | Line 251 | c_ccvt(C_COLOR *clr, int fl)
251                  if (clr->flags & C_CSSPEC) {            /* from spectrum */
252                          y = 0.;
253                          for (i = 0; i < C_CNSS; i++)
254 <                                y += cie_yf.ssamp[i] * clr->ssamp[i];
254 >                                y += c_y31.ssamp[i] * clr->ssamp[i];
255                          clr->eff = C_CLPWM * y / clr->ssum;
256                  } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
257 <                        clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
258 <                                        (1. - clr->cx - clr->cy)*cie_zf.eff;
257 >                        clr->eff = clr->cx*c_x31.eff + clr->cy*c_y31.eff +
258 >                                        (1. - clr->cx - clr->cy)*c_z31.eff;
259                  }
260                  clr->flags |= C_CSEFF;
261          }
# Line 329 | Line 334 | c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR
334                          cres->ssum += cres->ssamp[i] = cmix[i] / cmax;
335                  cres->flags = C_CDSPEC|C_CSSPEC;
336  
337 <                c_ccvt(cres, C_CSEFF);                  /* nasty, but true */
338 <                yres = y1 * y2 * cie_yf.ssum * C_CLPWM /
337 >                c_ccvt(cres, C_CSEFF);                  /* below is correct */
338 >                yres = y1 * y2 * c_y31.ssum * C_CLPWM /
339                          (c1->eff*c1->ssum * c2->eff*c2->ssum) *
340                          cres->eff*( cres->ssum*(double)cmax +
341                                                  C_CNSS/2.0*(cmax-1) );

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines