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.8 by greg, Sun Apr 5 01:32:01 2015 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                                          /* 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 183 | 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 232 | Line 238 | c_ccvt(C_COLOR *clr, int fl)
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          }
248          if (fl & C_CSSPEC) {            /* cxy -> cspec */
# Line 243 | Line 252 | c_ccvt(C_COLOR *clr, int fl)
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 256 | Line 265 | c_ccvt(C_COLOR *clr, int fl)
265                          y = 0.;
266                          for (i = 0; i < C_CNSS; i++)
267                                  y += c_y31.ssamp[i] * clr->ssamp[i];
268 <                        clr->eff = C_CLPWM * y / clr->ssum;
268 >                        clr->eff = C_CLPWM * y / (clr->ssum + 0.0001);
269                  } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
270                          clr->eff = clr->cx*c_x31.eff + clr->cy*c_y31.eff +
271                                          (1. - clr->cx - clr->cy)*c_z31.eff;
# Line 272 | 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 290 | 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 380 | 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 402 | Line 423 | c_encodeChroma(C_COLOR *clr)
423  
424          c_ccvt(clr, C_CSXY);
425          df = UV_NORMF/(-2.*clr->cx + 12.*clr->cy + 3.);
426 <        ub = 4.*clr->cx * df;
427 <        if (ub < 0) ub = 0;
428 <        else if (ub > 0xff) ub = 0xff;
429 <        vb = 9.*clr->cy * df;
430 <        if (vb < 0) vb = 0;
431 <        else if (vb > 0xff) vb = 0xff;
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   }
# Line 416 | Line 437 | c_encodeChroma(C_COLOR *clr)
437   void
438   c_decodeChroma(C_COLOR *cres, C_CHROMA ccode)
439   {
440 <        double  up = ((ccode & 0xff) + .5)*(1./UV_NORMF);
441 <        double  vp = ((ccode>>8 & 0xff) + .5)*(1./UV_NORMF);
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines