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.1 by greg, Fri Feb 18 00:40:25 2011 UTC vs.
Revision 3.8 by greg, Sun Apr 5 01:32:01 2015 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 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 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 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},
44                          106770L, .147, .077, 54.363
45                          };
46                                  /* Derived CIE 1931 Primaries (imaginary) */
47 < static C_COLOR  cie_xp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
47 > static const C_COLOR    cie_xp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
48                          {-174,-198,-195,-197,-202,-213,-235,-272,-333,
49                          -444,-688,-1232,-2393,-4497,-6876,-6758,-5256,
50                          -3100,-815,1320,3200,4782,5998,6861,7408,7754,
# Line 40 | Line 52 | static C_COLOR cie_xp = { 1, NULL, C_CDSPEC|C_CSSPEC|C
52                          8336,8336,8336,8336,8336,8336},
53                          127424L, 1., .0,
54                          };
55 < static C_COLOR  cie_yp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
55 > static const C_COLOR    cie_yp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
56                          {-451,-431,-431,-430,-427,-417,-399,-366,-312,
57                          -204,57,691,2142,4990,8810,9871,9122,7321,5145,
58                          3023,1123,-473,-1704,-2572,-3127,-3474,-3704,
# Line 48 | Line 60 | static C_COLOR cie_yp = { 1, NULL, C_CDSPEC|C_CSSPEC|C
60                          -4066,-4066,-4066,-4066,-4066,-4066},
61                          -23035L, .0, 1.,
62                          };
63 < static C_COLOR  cie_zp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
63 > static const C_COLOR    cie_zp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
64                          {4051,4054,4052,4053,4054,4056,4059,4064,4071,
65                          4074,4056,3967,3677,2933,1492,313,-440,-795,
66                          -904,-918,-898,-884,-869,-863,-855,-855,-851,
# Line 57 | Line 69 | static C_COLOR cie_zp = { 1, NULL, C_CDSPEC|C_CSSPEC|C
69                          36057L, .0, .0,
70                          };
71  
72 +
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];
78 +
79 +        c_ccvt(cin, C_CSXY);
80 +
81 +        xyz[0] = cin->cx/cin->cy * cieY;
82 +        xyz[1] = cieY;
83 +        xyz[2] = (1. - cin->cx - cin->cy)/cin->cy * cieY;
84 +
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 + /* 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[1] = XYZfromSharp[1][0]*cin[0] + XYZfromSharp[1][1]*cin[1] +
100 +                                XYZfromSharp[1][2]*cin[2];
101 +        if (xyz[1] <= 1e-6) {
102 +                *cout = c_dfcolor;      /* punting, here... */
103 +                return xyz[1];
104 +        }
105 +        xyz[0] = XYZfromSharp[0][0]*cin[0] + XYZfromSharp[0][1]*cin[1] +
106 +                                XYZfromSharp[0][2]*cin[2];
107 +        xyz[2] = XYZfromSharp[2][0]*cin[0] + XYZfromSharp[2][1]*cin[1] +
108 +                                XYZfromSharp[2][2]*cin[2];
109 +        
110 +        sf = 1./(xyz[0] + xyz[1] + xyz[2]);
111 +
112 +        cout->cx = xyz[0] * sf;
113 +        cout->cy = xyz[1] * sf;
114 +        cout->flags = C_CDXY|C_CSXY;
115 +        
116 +        return(xyz[1]);
117 + }
118 +
119 + /* assign arbitrary spectrum and return Y value */
120 + double
121 + c_sset(C_COLOR *clr, double wlmin, double wlmax, const float spec[], int nwl)
122 + {
123 +        double  yval, scale;
124 +        float   va[C_CNSS];
125 +        int     i, pos, n, imax, wl;
126 +        double  wl0, wlstep;
127 +        double  boxpos, boxstep;
128 +                                        /* check arguments */
129 +        if ((nwl <= 1) | (spec == NULL) | (wlmin >= C_CMAXWL) |
130 +                        (wlmax <= C_CMINWL) | (wlmin >= wlmax))
131 +                return(0.);
132 +        wlstep = (wlmax - wlmin)/(nwl-1);
133 +        while (wlmin < C_CMINWL) {
134 +                wlmin += wlstep;
135 +                --nwl; ++spec;
136 +        }
137 +        while (wlmax > C_CMAXWL) {
138 +                wlmax -= wlstep;
139 +                --nwl;
140 +        }
141 +        if ((nwl <= 1) | (wlmin >= wlmax))
142 +                return(0.);
143 +        imax = nwl;                     /* box filter if necessary */
144 +        boxpos = 0;
145 +        boxstep = 1;
146 +        if (wlstep < C_CWLI) {
147 +                imax = (wlmax - wlmin)/C_CWLI + 1e-7;
148 +                boxpos = (wlmin - C_CMINWL)/C_CWLI;
149 +                boxstep = wlstep/C_CWLI;
150 +                wlstep = C_CWLI;
151 +        }
152 +        scale = 0.;                     /* get values and maximum */
153 +        yval = 0.;
154 +        pos = 0;
155 +        for (i = 0; i < imax; i++) {
156 +                va[i] = 0.; n = 0;
157 +                while (boxpos < i+.5 && pos < nwl) {
158 +                        va[i] += spec[pos++];
159 +                        n++;
160 +                        boxpos += boxstep;
161 +                }
162 +                if (n > 1)
163 +                        va[i] /= (double)n;
164 +                if (va[i] > scale)
165 +                        scale = va[i];
166 +                else if (va[i] < -scale)
167 +                        scale = -va[i];
168 +                yval += va[i] * c_y31.ssamp[i];
169 +        }
170 +        if (scale <= 1e-7)
171 +                return(0.);
172 +        yval /= (double)c_y31.ssum;
173 +        scale = C_CMAXV / scale;
174 +        clr->ssum = 0;                  /* convert to our spacing */
175 +        wl0 = wlmin;
176 +        pos = 0;
177 +        for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI)
178 +                if ((wl < wlmin) | (wl > wlmax))
179 +                        clr->ssamp[i] = 0;
180 +                else {
181 +                        while (wl0 + wlstep < wl+1e-7) {
182 +                                wl0 += wlstep;
183 +                                pos++;
184 +                        }
185 +                        if ((wl+1e-7 >= wl0) & (wl-1e-7 <= wl0))
186 +                                clr->ssamp[i] = scale*va[pos] + .5;
187 +                        else            /* interpolate if necessary */
188 +                                clr->ssamp[i] = .5 + scale / wlstep *
189 +                                                ( va[pos]*(wl0+wlstep - wl) +
190 +                                                        va[pos+1]*(wl - wl0) );
191 +                        clr->ssum += clr->ssamp[i];
192 +                }
193 +        clr->flags = C_CDSPEC|C_CSSPEC;
194 +        return(yval);
195 + }
196 +
197   /* check if color is grey */
198   int
199   c_isgrey(C_COLOR *clr)
# Line 87 | Line 224 | c_ccvt(C_COLOR *clr, int fl)
224          if (fl & C_CSXY) {              /* cspec -> cxy */
225                  x = y = z = 0.;
226                  for (i = 0; i < C_CNSS; i++) {
227 <                        x += cie_xf.ssamp[i] * clr->ssamp[i];
228 <                        y += cie_yf.ssamp[i] * clr->ssamp[i];
229 <                        z += cie_zf.ssamp[i] * clr->ssamp[i];
227 >                        x += c_x31.ssamp[i] * clr->ssamp[i];
228 >                        y += c_y31.ssamp[i] * clr->ssamp[i];
229 >                        z += c_z31.ssamp[i] * clr->ssamp[i];
230                  }
231 <                x /= (double)cie_xf.ssum;
232 <                y /= (double)cie_yf.ssum;
233 <                z /= (double)cie_zf.ssum;
231 >                x /= (double)c_x31.ssum;
232 >                y /= (double)c_y31.ssum;
233 >                z /= (double)c_z31.ssum;
234                  z += x + y;
235                  clr->cx = x / z;
236                  clr->cy = y / z;
237                  clr->flags |= C_CSXY;
238 <        } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
238 >        }
239 >        if (fl & C_CSSPEC) {            /* cxy -> cspec */
240                  x = clr->cx;
241                  y = clr->cy;
242                  z = 1. - x - y;
# Line 117 | Line 255 | c_ccvt(C_COLOR *clr, int fl)
255                  if (clr->flags & C_CSSPEC) {            /* from spectrum */
256                          y = 0.;
257                          for (i = 0; i < C_CNSS; i++)
258 <                                y += cie_yf.ssamp[i] * clr->ssamp[i];
258 >                                y += c_y31.ssamp[i] * clr->ssamp[i];
259                          clr->eff = C_CLPWM * y / clr->ssum;
260                  } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
261 <                        clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
262 <                                        (1. - clr->cx - clr->cy)*cie_zf.eff;
261 >                        clr->eff = clr->cx*c_x31.eff + clr->cy*c_y31.eff +
262 >                                        (1. - clr->cx - clr->cy)*c_z31.eff;
263                  }
264                  clr->flags |= C_CSEFF;
265          }
266   }
267  
268 < /* mix two colors according to weights given -- cres can be c1 or c2 */
268 > /* mix two colors according to weights given -- cres may be c1 or c2 */
269   void
270   c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w2, C_COLOR *c2)
271   {
272          double  scale;
135        float   cmix[C_CNSS];
273          int     i;
274  
275          if ((c1->flags|c2->flags) & C_CDSPEC) {         /* spectral mixing */
276 +                float   cmix[C_CNSS];
277 +
278                  c_ccvt(c1, C_CSSPEC|C_CSEFF);
279                  c_ccvt(c2, C_CSSPEC|C_CSEFF);
280                  w1 /= c1->eff*c1->ssum;
# Line 145 | Line 284 | c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w
284                          cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i];
285                          if (cmix[i] > scale)
286                                  scale = cmix[i];
287 +                        else if (cmix[i] < -scale)
288 +                                scale = -cmix[i];
289                  }
290                  scale = C_CMAXV / scale;
291                  cres->ssum = 0;
# Line 168 | Line 309 | c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w
309          }
310   }
311  
312 + /* multiply two colors -- cres may be c1 or c2 */
313 + double
314 + c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR *c2, double y2)
315 + {
316 +        double  yres;
317 +        int     i;
318 +
319 +        if ((c1->flags|c2->flags) & C_CDSPEC) {
320 +                long    cmix[C_CNSS], cmax = 0;         /* spectral multiply */
321 +
322 +                c_ccvt(c1, C_CSSPEC|C_CSEFF);
323 +                c_ccvt(c2, C_CSSPEC|C_CSEFF);
324 +                for (i = 0; i < C_CNSS; i++) {
325 +                        cmix[i] = c1->ssamp[i] * c2->ssamp[i];
326 +                        if (cmix[i] > cmax)
327 +                                cmax = cmix[i];
328 +                        else if (cmix[i] < -cmax)
329 +                                cmax = -cmix[i];
330 +                }
331 +                cmax /= C_CMAXV;
332 +                if (!cmax) {
333 +                        *cres = c_dfcolor;
334 +                        return(0.);
335 +                }
336 +                cres->ssum = 0;
337 +                for (i = 0; i < C_CNSS; i++)
338 +                        cres->ssum += cres->ssamp[i] = cmix[i] / cmax;
339 +                cres->flags = C_CDSPEC|C_CSSPEC;
340 +
341 +                c_ccvt(cres, C_CSEFF);                  /* below is correct */
342 +                yres = y1 * y2 * c_y31.ssum * C_CLPWM /
343 +                        (c1->eff*c1->ssum * c2->eff*c2->ssum) *
344 +                        cres->eff*( cres->ssum*(double)cmax +
345 +                                                C_CNSS/2.0*(cmax-1) );
346 +        } else {
347 +                float   rgb1[3], rgb2[3];               /* CIE xy multiply */
348 +
349 +                c_toSharpRGB(c1, y1, rgb1);
350 +                c_toSharpRGB(c2, y2, rgb2);
351 +                
352 +                rgb2[0] *= rgb1[0]; rgb2[1] *= rgb1[1]; rgb2[2] *= rgb1[2];
353 +
354 +                yres = c_fromSharpRGB(rgb2, cres);
355 +        }
356 +        return(yres);
357 + }
358 +
359   #define C1              3.741832e-16    /* W-m^2 */
360   #define C2              1.4388e-2       /* m-K */
361  
362   #define bbsp(l,t)       (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.)))
363 < #define bblm(t)         (C2/5./(t))
363 > #define bblm(t)         (C2*0.2/(t))
364  
365   /* set black body spectrum */
366   int
# Line 202 | Line 390 | c_bbtemp(C_COLOR *clr, double tk)
390   #undef  C2
391   #undef  bbsp
392   #undef  bblm
393 +
394 + #define UV_NORMF        410.
395 +
396 + /* encode (x,y) chromaticity */
397 + C_CHROMA
398 + c_encodeChroma(C_COLOR *clr)
399 + {
400 +        double  df;
401 +        int     ub, vb;
402 +
403 +        c_ccvt(clr, C_CSXY);
404 +        df = UV_NORMF/(-2.*clr->cx + 12.*clr->cy + 3.);
405 +        ub = 4.*clr->cx * df;
406 +        if (ub < 0) ub = 0;
407 +        else if (ub > 0xff) ub = 0xff;
408 +        vb = 9.*clr->cy * df;
409 +        if (vb < 0) vb = 0;
410 +        else if (vb > 0xff) vb = 0xff;
411 +
412 +        return(vb<<8 | ub);
413 + }
414 +
415 + /* decode (x,y) chromaticity */
416 + void
417 + c_decodeChroma(C_COLOR *cres, C_CHROMA ccode)
418 + {
419 +        double  up = ((ccode & 0xff) + .5)*(1./UV_NORMF);
420 +        double  vp = ((ccode>>8 & 0xff) + .5)*(1./UV_NORMF);
421 +        double  df = 1./(6.*up - 16.*vp + 12.);
422 +
423 +        cres->cx = 9.*up * df;
424 +        cres->cy = 4.*vp * df;
425 +        cres->flags = C_CDXY|C_CSXY;
426 + }
427 +
428 + #undef  UV_NORMF

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines