ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/spec_rgb.c
(Generate patch)

Comparing ray/src/common/spec_rgb.c (file contents):
Revision 2.12 by greg, Tue Feb 25 02:47:22 2003 UTC vs.
Revision 2.21 by greg, Mon Apr 12 21:24:07 2010 UTC

# Line 10 | Line 10 | static const char      RCSid[] = "$Id$";
10  
11   #include "copyright.h"
12  
13 < #include "color.h"
13 > #include <stdio.h>
14   #include <string.h>
15 + #include "color.h"
16  
17   #define CEPS    1e-4                    /* color epsilon */
18  
# Line 90 | Line 91 | COLORMAT  ivkmat = {           /* inverse Sharp primary matrix
91  
92  
93   void
94 < spec_rgb(col, s, e)             /* compute RGB color from spectral range */
95 < COLOR  col;
96 < int  s, e;
94 > spec_rgb(                       /* compute RGB color from spectral range */
95 > COLOR  col,
96 > int  s,
97 > int  e
98 > )
99   {
100          COLOR  ciecolor;
101  
# Line 102 | Line 105 | int  s, e;
105  
106  
107   void
108 < spec_cie(col, s, e)             /* compute a color from a spectral range */
109 < COLOR  col;             /* returned color */
110 < int  s, e;              /* starting and ending wavelengths */
108 > spec_cie(                       /* compute a color from a spectral range */
109 > COLOR  col,             /* returned color */
110 > int  s,                 /* starting and ending wavelengths */
111 > int  e
112 > )
113   {
114          register int  i, d, r;
115          
# Line 137 | Line 142 | int  s, e;             /* starting and ending wavelengths */
142  
143  
144   void
145 < cie_rgb(rgb, xyz)               /* convert CIE color to standard RGB */
146 < COLOR   rgb;
147 < COLOR  xyz;
145 > cie_rgb(                        /* convert CIE color to standard RGB */
146 > COLOR   rgb,
147 > COLOR  xyz
148 > )
149   {
150          colortrans(rgb, xyz2rgbmat, xyz);
151          clipgamut(rgb, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite);
# Line 147 | Line 153 | COLOR  xyz;
153  
154  
155   int
156 < clipgamut(col, brt, gamut, lower, upper)        /* clip to gamut cube */
157 < COLOR  col;
158 < double  brt;
159 < int  gamut;
160 < COLOR  lower, upper;
156 > clipgamut(                      /* clip to gamut cube */
157 > COLOR  col,
158 > double  brt,
159 > int  gamut,
160 > COLOR  lower,
161 > COLOR  upper
162 > )
163   {
164          int  rflags = 0;
165          double  brtmin, brtmax, v, vv;
# Line 177 | Line 185 | COLOR  lower, upper;
185          vv = 1.;                        /* check each limit */
186          for (i = 0; i < 3; i++)
187                  if (gamut & CGAMUT_LOWER && col[i] < lower[i]) {
188 <                        v = (lower[i]+CEPS - cgry[i])/(col[i] - cgry[i]);
188 >                        v = (lower[i] - cgry[i])/(col[i] - cgry[i]);
189                          if (v < vv) vv = v;
190                          rflags |= CGAMUT_LOWER;
191                  } else if (gamut & CGAMUT_UPPER && col[i] > upper[i]) {
192 <                        v = (upper[i]-CEPS - cgry[i])/(col[i] - cgry[i]);
192 >                        v = (upper[i] - cgry[i])/(col[i] - cgry[i]);
193                          if (v < vv) vv = v;
194                          rflags |= CGAMUT_UPPER;
195                  }
# Line 193 | Line 201 | COLOR  lower, upper;
201  
202  
203   void
204 < colortrans(c2, mat, c1)         /* convert c1 by mat and put into c2 */
205 < register COLOR  c2;
206 < register COLORMAT  mat;
207 < register COLOR  c1;
204 > colortrans(                     /* convert c1 by mat and put into c2 */
205 > register COLOR  c2,
206 > register COLORMAT  mat,
207 > register COLOR  c1
208 > )
209   {
210          COLOR   cout;
211  
# Line 209 | Line 218 | register COLOR  c1;
218  
219  
220   void
221 < multcolormat(m3, m2, m1)        /* multiply m1 by m2 and put into m3 */
222 < COLORMAT  m3;                   /* m3 can be either m1 or m2 w/o harm */
223 < COLORMAT  m2, m1;
221 > multcolormat(                   /* multiply m1 by m2 and put into m3 */
222 > COLORMAT  m3,                   /* m3 can be either m1 or m2 w/o harm */
223 > COLORMAT  m2,
224 > COLORMAT  m1
225 > )
226   {
227          COLORMAT  mt;
228          register int  i, j;
# Line 225 | Line 236 | COLORMAT  m2, m1;
236   }
237  
238  
239 < void
240 < compxyz2rgbmat(mat, pr)         /* compute conversion from CIE to RGB space */
241 < COLORMAT  mat;
242 < register RGBPRIMS  pr;
239 > int
240 > colorprimsOK(                   /* are color primaries reasonable? */
241 > RGBPRIMS  pr
242 > )
243   {
244 +        int     i, j;
245 +        
246 +        for (i = 0; i < 4; i++) {
247 +                if ((pr[i][CIEX] <= -1.) | (pr[i][CIEY] <= -1.))
248 +                        return(0);
249 +                if ((pr[i][CIEX] >= 2.) | (pr[i][CIEY] >= 2.))
250 +                        return(0);
251 +                if (pr[i][CIEX] + pr[i][CIEY] <= -1.)
252 +                        return(0);
253 +                if (pr[i][CIEX] + pr[i][CIEY] >= 2.)
254 +                        return(0);
255 +        }
256 +        for (i = 0; i < 4; i++)
257 +                for (j = i+1; j < 4; j++)
258 +                        if (CEQ(pr[i][CIEX],pr[j][CIEX]) &&
259 +                                        CEQ(pr[i][CIEY],pr[j][CIEY]))
260 +                                return(0);
261 +        return(1);
262 + }
263 +
264 +
265 +
266 + int
267 + compxyz2rgbmat(                 /* compute conversion from CIE to RGB space */
268 + COLORMAT  mat,
269 + register RGBPRIMS  pr
270 + )
271 + {
272          double  C_rD, C_gD, C_bD;
273  
274          if (pr == stdprims) {   /* can use xyz2rgbmat */
275                  cpcolormat(mat, xyz2rgbmat);
276 <                return;
276 >                return(1);
277          }
278 +        if (CEQ(pr[WHT][CIEX],0.) | CEQ(pr[WHT][CIEY],0.))
279 +                return(0);
280          C_rD = (1./pr[WHT][CIEY]) *
281                          ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) -
282                            pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) +
283                    pr[GRN][CIEX]*pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] ) ;
284 +        if (CEQ(C_rD,0.))
285 +                return(0);
286          C_gD = (1./pr[WHT][CIEY]) *
287                          ( pr[WHT][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) -
288                            pr[WHT][CIEY]*(pr[BLU][CIEX] - pr[RED][CIEX]) -
289                    pr[RED][CIEX]*pr[BLU][CIEY] + pr[BLU][CIEX]*pr[RED][CIEY] ) ;
290 +        if (CEQ(C_gD,0.))
291 +                return(0);
292          C_bD = (1./pr[WHT][CIEY]) *
293                          ( pr[WHT][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) -
294                            pr[WHT][CIEY]*(pr[RED][CIEX] - pr[GRN][CIEX]) +
295                    pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY] ) ;
296 <
296 >        if (CEQ(C_bD,0.))
297 >                return(0);
298          mat[0][0] = (pr[GRN][CIEY] - pr[BLU][CIEY] -
299                          pr[BLU][CIEX]*pr[GRN][CIEY] +
300                          pr[BLU][CIEY]*pr[GRN][CIEX])/C_rD ;
# Line 273 | Line 319 | register RGBPRIMS  pr;
319                          pr[RED][CIEX]*pr[GRN][CIEY])/C_bD ;
320          mat[2][2] = (pr[RED][CIEX]*pr[GRN][CIEY] -
321                          pr[GRN][CIEX]*pr[RED][CIEY])/C_bD ;
322 +        return(1);
323   }
324  
325  
326 < void
327 < comprgb2xyzmat(mat, pr)         /* compute conversion from RGB to CIE space */
328 < COLORMAT  mat;
329 < register RGBPRIMS  pr;
326 > int
327 > comprgb2xyzmat(                 /* compute conversion from RGB to CIE space */
328 > COLORMAT  mat,
329 > register RGBPRIMS  pr
330 > )
331   {
332          double  C_rD, C_gD, C_bD, D;
333  
334          if (pr == stdprims) {   /* can use rgb2xyzmat */
335                  cpcolormat(mat, rgb2xyzmat);
336 <                return;
336 >                return(1);
337          }
338 +        if (CEQ(pr[WHT][CIEX],0.) | CEQ(pr[WHT][CIEY],0.))
339 +                return(0);
340          C_rD = (1./pr[WHT][CIEY]) *
341                          ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) -
342                            pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) +
# Line 302 | Line 352 | register RGBPRIMS  pr;
352          D = pr[RED][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) +
353                          pr[GRN][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) +
354                          pr[BLU][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) ;
355 +        if (CEQ(D,0.))
356 +                return(0);
357          mat[0][0] = pr[RED][CIEX]*C_rD/D;
358          mat[0][1] = pr[GRN][CIEX]*C_gD/D;
359          mat[0][2] = pr[BLU][CIEX]*C_bD/D;
# Line 311 | Line 363 | register RGBPRIMS  pr;
363          mat[2][0] = (1.-pr[RED][CIEX]-pr[RED][CIEY])*C_rD/D;
364          mat[2][1] = (1.-pr[GRN][CIEX]-pr[GRN][CIEY])*C_gD/D;
365          mat[2][2] = (1.-pr[BLU][CIEX]-pr[BLU][CIEY])*C_bD/D;
366 +        return(1);
367   }
368  
369  
370 < void
371 < comprgb2rgbmat(mat, pr1, pr2)   /* compute conversion from RGB1 to RGB2 */
372 < COLORMAT  mat;
373 < RGBPRIMS  pr1, pr2;
370 > int
371 > comprgb2rgbmat(                 /* compute conversion from RGB1 to RGB2 */
372 > COLORMAT  mat,
373 > RGBPRIMS  pr1,
374 > RGBPRIMS  pr2
375 > )
376   {
377          COLORMAT  pr1toxyz, xyztopr2;
378  
# Line 325 | Line 380 | RGBPRIMS  pr1, pr2;
380                  mat[0][0] = mat[1][1] = mat[2][2] = 1.0;
381                  mat[0][1] = mat[0][2] = mat[1][0] =
382                  mat[1][2] = mat[2][0] = mat[2][1] = 0.0;
383 <                return;
383 >                return(1);
384          }
385 <        comprgb2xyzmat(pr1toxyz, pr1);
386 <        compxyz2rgbmat(xyztopr2, pr2);
385 >        if (!comprgb2xyzmat(pr1toxyz, pr1))
386 >                return(0);
387 >        if (!compxyz2rgbmat(xyztopr2, pr2))
388 >                return(0);
389                                  /* combine transforms */
390          multcolormat(mat, pr1toxyz, xyztopr2);
391 +        return(1);
392   }
393  
394  
395 < void
396 < compxyzWBmat(mat, wht1, wht2)   /* CIE von Kries transform from wht1 to wht2 */
397 < COLORMAT  mat;
398 < float  wht1[2], wht2[2];
395 > int
396 > compxyzWBmat(                   /* CIE von Kries transform from wht1 to wht2 */
397 > COLORMAT  mat,
398 > float  wht1[2],
399 > float  wht2[2]
400 > )
401   {
402          COLOR   cw1, cw2;
403          if (XYEQ(wht1,wht2)) {
404                  mat[0][0] = mat[1][1] = mat[2][2] = 1.0;
405                  mat[0][1] = mat[0][2] = mat[1][0] =
406                  mat[1][2] = mat[2][0] = mat[2][1] = 0.0;
407 <                return;
407 >                return(1);
408          }
409 +        if (CEQ(wht1[CIEX],0.) | CEQ(wht1[CIEY],0.))
410 +                return(0);
411          cw1[RED] = wht1[CIEX]/wht1[CIEY];
412          cw1[GRN] = 1.;
413          cw1[BLU] = (1. - wht1[CIEX] - wht1[CIEY])/wht1[CIEY];
414          colortrans(cw1, vkmat, cw1);
415 +        if (CEQ(wht2[CIEX],0.) | CEQ(wht2[CIEY],0.))
416 +                return(0);
417          cw2[RED] = wht2[CIEX]/wht2[CIEY];
418          cw2[GRN] = 1.;
419          cw2[BLU] = (1. - wht2[CIEX] - wht2[CIEY])/wht2[CIEY];
420          colortrans(cw2, vkmat, cw2);
421 +        if (CEQ(cw1[RED],0.) | CEQ(cw1[GRN],0.) | CEQ(cw1[BLU],0.))
422 +                return(0);
423          mat[0][0] = cw2[RED]/cw1[RED];
424          mat[1][1] = cw2[GRN]/cw1[GRN];
425          mat[2][2] = cw2[BLU]/cw1[BLU];
# Line 361 | Line 427 | float  wht1[2], wht2[2];
427          mat[1][2] = mat[2][0] = mat[2][1] = 0.0;
428          multcolormat(mat, vkmat, mat);
429          multcolormat(mat, mat, ivkmat);
430 +        return(1);
431   }
432  
433  
434 < void
435 < compxyz2rgbWBmat(mat, pr)       /* von Kries conversion from CIE to RGB space */
436 < COLORMAT  mat;
437 < RGBPRIMS  pr;
434 > int
435 > compxyz2rgbWBmat(               /* von Kries conversion from CIE to RGB space */
436 > COLORMAT  mat,
437 > RGBPRIMS  pr
438 > )
439   {
440          COLORMAT        wbmat;
441  
442 <        compxyz2rgbmat(mat, pr);
442 >        if (!compxyz2rgbmat(mat, pr))
443 >                return(0);
444          if (XYEQ(pr[WHT],xyneu))
445 <                return;
446 <        compxyzWBmat(wbmat, xyneu, pr[WHT]);
445 >                return(1);
446 >        if (!compxyzWBmat(wbmat, xyneu, pr[WHT]))
447 >                return(0);
448          multcolormat(mat, wbmat, mat);
449 +        return(1);
450   }
451  
452 < void
453 < comprgb2xyzWBmat(mat, pr)       /* von Kries conversion from RGB to CIE space */
454 < COLORMAT  mat;
455 < RGBPRIMS  pr;
452 > int
453 > comprgb2xyzWBmat(               /* von Kries conversion from RGB to CIE space */
454 > COLORMAT  mat,
455 > RGBPRIMS  pr
456 > )
457   {
458          COLORMAT        wbmat;
459          
460 <        comprgb2xyzmat(mat, pr);
460 >        if (!comprgb2xyzmat(mat, pr))
461 >                return(0);
462          if (XYEQ(pr[WHT],xyneu))
463 <                return;
464 <        compxyzWBmat(wbmat, pr[WHT], xyneu);
463 >                return(1);
464 >        if (!compxyzWBmat(wbmat, pr[WHT], xyneu))
465 >                return(0);
466          multcolormat(mat, mat, wbmat);
467 +        return(1);
468   }
469  
470 < void
471 < comprgb2rgbWBmat(mat, pr1, pr2) /* von Kries conversion from RGB1 to RGB2 */
472 < COLORMAT  mat;
473 < RGBPRIMS  pr1, pr2;
470 > int
471 > comprgb2rgbWBmat(               /* von Kries conversion from RGB1 to RGB2 */
472 > COLORMAT  mat,
473 > RGBPRIMS  pr1,
474 > RGBPRIMS  pr2
475 > )
476   {
477          COLORMAT  pr1toxyz, xyztopr2, wbmat;
478  
# Line 403 | Line 480 | RGBPRIMS  pr1, pr2;
480                  mat[0][0] = mat[1][1] = mat[2][2] = 1.0;
481                  mat[0][1] = mat[0][2] = mat[1][0] =
482                  mat[1][2] = mat[2][0] = mat[2][1] = 0.0;
483 <                return;
483 >                return(1);
484          }
485 <        comprgb2xyzmat(pr1toxyz, pr1);
486 <        compxyzWBmat(wbmat, pr1[WHT], pr2[WHT]);
487 <        compxyz2rgbmat(xyztopr2, pr2);
485 >        if (!comprgb2xyzmat(pr1toxyz, pr1))
486 >                return(0);
487 >        if (!compxyzWBmat(wbmat, pr1[WHT], pr2[WHT]))
488 >                return(0);
489 >        if (!compxyz2rgbmat(xyztopr2, pr2))
490 >                return(0);
491                                  /* combine transforms */
492          multcolormat(mat, pr1toxyz, wbmat);
493          multcolormat(mat, mat, xyztopr2);
494 +        return(1);
495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines