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.2 by greg, Thu May 17 05:47:59 2012 UTC vs.
Revision 3.4 by greg, Fri May 18 20:43:13 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;
26  
# 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 */
116 + double
117 + c_sset(C_COLOR *clr, double wlmin, double wlmax, const float spec[], int nwl)
118 + {
119 +        double  yval, scale;
120 +        float   va[C_CNSS];
121 +        int     i, pos, n, imax, wl;
122 +        double  wl0, wlstep;
123 +        double  boxpos, boxstep;
124 +                                        /* check arguments */
125 +        if ((nwl <= 1) | (spec == NULL) | (wlmin >= C_CMAXWL) |
126 +                        (wlmax <= C_CMINWL) | (wlmin >= wlmax))
127 +                return(0.);
128 +        wlstep = (wlmax - wlmin)/(nwl-1);
129 +        while (wlmin < C_CMINWL) {
130 +                wlmin += wlstep;
131 +                --nwl; ++spec;
132 +        }
133 +        while (wlmax > C_CMAXWL) {
134 +                wlmax -= wlstep;
135 +                --nwl;
136 +        }
137 +        imax = nwl;                     /* box filter if necessary */
138 +        boxpos = 0;
139 +        boxstep = 1;
140 +        if (wlstep < C_CWLI) {
141 +                imax = (wlmax - wlmin)/C_CWLI + 1e-7;
142 +                boxpos = (wlmin - C_CMINWL)/C_CWLI;
143 +                boxstep = wlstep/C_CWLI;
144 +                wlstep = C_CWLI;
145 +        }
146 +        scale = 0.;                     /* get values and maximum */
147 +        yval = 0.;
148 +        pos = 0;
149 +        for (i = 0; i < imax; i++) {
150 +                va[i] = 0.; n = 0;
151 +                while (boxpos < i+.5 && pos < nwl) {
152 +                        va[i] += spec[pos++];
153 +                        n++;
154 +                        boxpos += boxstep;
155 +                }
156 +                if (n > 1)
157 +                        va[i] /= (double)n;
158 +                if (va[i] > scale)
159 +                        scale = va[i];
160 +                else if (va[i] < -scale)
161 +                        scale = -va[i];
162 +                yval += va[i] * cie_yf.ssamp[i];
163 +        }
164 +        if (scale <= 1e-7)
165 +                return(0.);
166 +        yval /= (double)cie_yf.ssum;
167 +        scale = C_CMAXV / scale;
168 +        clr->ssum = 0;                  /* convert to our spacing */
169 +        wl0 = wlmin;
170 +        pos = 0;
171 +        for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI)
172 +                if ((wl < wlmin) | (wl > wlmax))
173 +                        clr->ssamp[i] = 0;
174 +                else {
175 +                        while (wl0 + wlstep < wl+1e-7) {
176 +                                wl0 += wlstep;
177 +                                pos++;
178 +                        }
179 +                        if ((wl+1e-7 >= wl0) & (wl-1e-7 <= wl0))
180 +                                clr->ssamp[i] = scale*va[pos] + .5;
181 +                        else            /* interpolate if necessary */
182 +                                clr->ssamp[i] = .5 + scale / wlstep *
183 +                                                ( va[pos]*(wl0+wlstep - wl) +
184 +                                                        va[pos+1]*(wl - wl0) );
185 +                        clr->ssum += clr->ssamp[i];
186 +                }
187 +        clr->flags = C_CDSPEC|C_CSSPEC;
188 +        return(yval);
189 + }
190 +
191   /* check if color is grey */
192   int
193   c_isgrey(C_COLOR *clr)
# Line 199 | Line 277 | c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w
277                          cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i];
278                          if (cmix[i] > scale)
279                                  scale = cmix[i];
280 +                        else if (cmix[i] < -scale)
281 +                                scale = -cmix[i];
282                  }
283                  scale = C_CMAXV / scale;
284                  cres->ssum = 0;
# Line 238 | Line 318 | c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR
318                          cmix[i] = c1->ssamp[i] * c2->ssamp[i];
319                          if (cmix[i] > cmax)
320                                  cmax = cmix[i];
321 +                        else if (cmix[i] < -cmax)
322 +                                cmax = -cmix[i];
323                  }
324                  cmax /= C_CMAXV;
325                  if (!cmax) {
# Line 250 | Line 332 | c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR
332                  cres->flags = C_CDSPEC|C_CSSPEC;
333  
334                  c_ccvt(cres, C_CSEFF);                  /* nasty, but true */
335 <                yres = (y1 * y2 * cie_yf.ssum * C_CLPWM) /
335 >                yres = y1 * y2 * cie_yf.ssum * C_CLPWM /
336                          (c1->eff*c1->ssum * c2->eff*c2->ssum) *
337                          cres->eff*( cres->ssum*(double)cmax +
338                                                  C_CNSS/2.0*(cmax-1) );

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines