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.5 by greg, Tue Jun 26 17:59:16 2012 UTC vs.
Revision 3.10 by greg, Wed Apr 5 00:54:50 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                                          /* Sharp primary matrix */
17   float   XYZtoSharp[3][3] = {
18          { 1.2694, -0.0988, -0.1706},
# Line 24 | Line 28 | float  XYZfromSharp[3][3] = {
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   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,
# Line 96 | Line 102 | c_fromSharpRGB(float cin[3], C_COLOR *cout)
102   {
103          double  xyz[3], sf;
104  
99        xyz[0] = XYZfromSharp[0][0]*cin[0] + XYZfromSharp[0][1]*cin[1] +
100                                XYZfromSharp[0][2]*cin[2];
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 <                                
115 >        
116          sf = 1./(xyz[0] + xyz[1] + xyz[2]);
117  
118          cout->cx = xyz[0] * sf;
# Line 112 | 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 134 | 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 177 | 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 229 | Line 241 | c_ccvt(C_COLOR *clr, int fl)
241                  clr->cx = x / z;
242                  clr->cy = y / z;
243                  clr->flags |= C_CSXY;
244 <        } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
244 >        }
245 >        if (fl & C_CSSPEC) {            /* cxy -> cspec */
246                  x = clr->cx;
247                  y = clr->cy;
248                  z = 1. - x - y;
249                  clr->ssum = 0;
250                  for (i = 0; i < C_CNSS; i++) {
251                          clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i]
252 <                                        + z*cie_zp.ssamp[i] + .5;
252 >                                        + z*cie_zp.ssamp[i] + frand();
253                          if (clr->ssamp[i] < 0)          /* out of gamut! */
254                                  clr->ssamp[i] = 0;
255                          else
# Line 283 | Line 296 | c_cmix(C_COLOR *cres, double w1, C_COLOR *c1, double w
296                  scale = C_CMAXV / scale;
297                  cres->ssum = 0;
298                  for (i = 0; i < C_CNSS; i++)
299 <                        cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5;
299 >                        cres->ssum += cres->ssamp[i] = scale*cmix[i] + frand();
300                  cres->flags = C_CDSPEC|C_CSSPEC;
301          } else {                                        /* CIE xy mixing */
302                  c_ccvt(c1, C_CSXY);
# Line 331 | Line 344 | c_cmult(C_COLOR *cres, C_COLOR *c1, double y1, C_COLOR
344                          cres->ssum += cres->ssamp[i] = cmix[i] / cmax;
345                  cres->flags = C_CDSPEC|C_CSSPEC;
346  
347 <                c_ccvt(cres, C_CSEFF);                  /* don't touch below */
347 >                c_ccvt(cres, C_CSEFF);                  /* below is correct */
348                  yres = y1 * y2 * c_y31.ssum * C_CLPWM /
349                          (c1->eff*c1->ssum * c2->eff*c2->ssum) *
350                          cres->eff*( cres->ssum*(double)cmax +
# Line 373 | Line 386 | c_bbtemp(C_COLOR *clr, double tk)
386          clr->ssum = 0;
387          for (i = 0; i < C_CNSS; i++) {
388                  wl = (C_CMINWL + i*C_CWLI)*1e-9;
389 <                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5;
389 >                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + frand();
390          }
391          clr->flags = C_CDSPEC|C_CSSPEC;
392          return(1);
# Line 383 | Line 396 | c_bbtemp(C_COLOR *clr, double tk)
396   #undef  C2
397   #undef  bbsp
398   #undef  bblm
399 +
400 + #define UV_NORMF        410.
401 +
402 + /* encode (x,y) chromaticity */
403 + C_CHROMA
404 + c_encodeChroma(C_COLOR *clr)
405 + {
406 +        double  df;
407 +        int     ub, vb;
408 +
409 +        c_ccvt(clr, C_CSXY);
410 +        df = UV_NORMF/(-2.*clr->cx + 12.*clr->cy + 3.);
411 +        ub = 4.*clr->cx*df + frand();
412 +        if (ub > 0xff) ub = 0xff;
413 +        else ub *= (ub > 0);
414 +        vb = 9.*clr->cy*df + frand();
415 +        if (vb > 0xff) vb = 0xff;
416 +        else vb *= (vb > 0);
417 +
418 +        return(vb<<8 | ub);
419 + }
420 +
421 + /* decode (x,y) chromaticity */
422 + void
423 + c_decodeChroma(C_COLOR *cres, C_CHROMA ccode)
424 + {
425 +        double  up = (ccode & 0xff)*(1./UV_NORMF);
426 +        double  vp = (ccode>>8 & 0xff)*(1./UV_NORMF);
427 +        double  df = 1./(6.*up - 16.*vp + 12.);
428 +
429 +        cres->cx = 9.*up * df;
430 +        cres->cy = 4.*vp * df;
431 +        cres->flags = C_CDXY|C_CSXY;
432 + }
433 +
434 + #undef  UV_NORMF

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines