--- ray/src/common/spec_rgb.c 1997/04/17 15:42:02 2.9 +++ ray/src/common/spec_rgb.c 2010/04/12 21:24:07 2.21 @@ -1,21 +1,33 @@ -/* Copyright (c) 1997 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: spec_rgb.c,v 2.21 2010/04/12 21:24:07 greg Exp $"; #endif - /* * Convert colors and spectral ranges. + * Added von Kries white-balance calculations 10/01 (GW). + * + * Externals declared in color.h */ +#include "copyright.h" + +#include +#include #include "color.h" +#define CEPS 1e-4 /* color epsilon */ -RGBPRIMS stdprims = STDPRIMS; /* standard primary chromaticities */ +#define CEQ(v1,v2) ((v1) <= (v2)+CEPS && (v2) <= (v1)+CEPS) +#define XYEQ(c1,c2) (CEQ((c1)[CIEX],(c2)[CIEX]) && CEQ((c1)[CIEY],(c2)[CIEY])) + + +RGBPRIMS stdprims = STDPRIMS; /* standard primary chromaticities */ + COLOR cblack = BLKCOLOR; /* global black color */ COLOR cwhite = WHTCOLOR; /* global white color */ +float xyneu[2] = {1./3., 1./3.}; /* neutral xy chromaticities */ + /* * The following table contains the CIE tristimulus integrals * for X, Y, and Z. The table is cumulative, so that @@ -45,7 +57,7 @@ static BYTE chroma[3][NINC] = { } }; -COLORMAT xyz2rgbmat = { /* XYZ to RGB */ +COLORMAT xyz2rgbmat = { /* XYZ to RGB (no white balance) */ {(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD, (CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD, (CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD}, @@ -57,7 +69,7 @@ COLORMAT xyz2rgbmat = { /* XYZ to RGB */ (CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD} }; -COLORMAT rgb2xyzmat = { /* RGB to XYZ */ +COLORMAT rgb2xyzmat = { /* RGB to XYZ (no white balance) */ {CIE_x_r*CIE_C_rD/CIE_D,CIE_x_g*CIE_C_gD/CIE_D,CIE_x_b*CIE_C_bD/CIE_D}, {CIE_y_r*CIE_C_rD/CIE_D,CIE_y_g*CIE_C_gD/CIE_D,CIE_y_b*CIE_C_bD/CIE_D}, {(1.-CIE_x_r-CIE_y_r)*CIE_C_rD/CIE_D, @@ -65,11 +77,25 @@ COLORMAT rgb2xyzmat = { /* RGB to XYZ */ (1.-CIE_x_b-CIE_y_b)*CIE_C_bD/CIE_D} }; +COLORMAT vkmat = { /* Sharp primary matrix */ + { 1.2694, -0.0988, -0.1706}, + {-0.8364, 1.8006, 0.0357}, + { 0.0297, -0.0315, 1.0018} +}; +COLORMAT ivkmat = { /* inverse Sharp primary matrix */ + { 0.8156, 0.0472, 0.1372}, + { 0.3791, 0.5769, 0.0440}, + {-0.0123, 0.0167, 0.9955} +}; -spec_rgb(col, s, e) /* compute RGB color from spectral range */ -COLOR col; -int s, e; + +void +spec_rgb( /* compute RGB color from spectral range */ +COLOR col, +int s, +int e +) { COLOR ciecolor; @@ -78,9 +104,12 @@ int s, e; } -spec_cie(col, s, e) /* compute a color from a spectral range */ -COLOR col; /* returned color */ -int s, e; /* starting and ending wavelengths */ +void +spec_cie( /* compute a color from a spectral range */ +COLOR col, /* returned color */ +int s, /* starting and ending wavelengths */ +int e +) { register int i, d, r; @@ -112,8 +141,11 @@ int s, e; /* starting and ending wavelengths */ } -cie_rgb(rgb, xyz) /* convert CIE color to standard RGB */ -COLOR rgb, xyz; +void +cie_rgb( /* convert CIE color to standard RGB */ +COLOR rgb, +COLOR xyz +) { colortrans(rgb, xyz2rgbmat, xyz); clipgamut(rgb, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite); @@ -121,11 +153,13 @@ COLOR rgb, xyz; int -clipgamut(col, brt, gamut, lower, upper) /* clip to gamut cube */ -COLOR col; -double brt; -int gamut; -COLOR lower, upper; +clipgamut( /* clip to gamut cube */ +COLOR col, +double brt, +int gamut, +COLOR lower, +COLOR upper +) { int rflags = 0; double brtmin, brtmax, v, vv; @@ -166,9 +200,12 @@ COLOR lower, upper; } -colortrans(c2, mat, c1) /* convert c1 by mat and put into c2 */ -register COLORMAT mat; -register COLOR c1, c2; +void +colortrans( /* convert c1 by mat and put into c2 */ +register COLOR c2, +register COLORMAT mat, +register COLOR c1 +) { COLOR cout; @@ -180,8 +217,12 @@ register COLOR c1, c2; } -multcolormat(m3, m2, m1) /* multiply m1 by m2 and put into m3 */ -COLORMAT m1, m2, m3; /* m3 can be either m1 or m2 w/o harm */ +void +multcolormat( /* multiply m1 by m2 and put into m3 */ +COLORMAT m3, /* m3 can be either m1 or m2 w/o harm */ +COLORMAT m2, +COLORMAT m1 +) { COLORMAT mt; register int i, j; @@ -195,29 +236,65 @@ COLORMAT m1, m2, m3; /* m3 can be either m1 or m2 w/ } -compxyz2rgbmat(mat, pr) /* compute conversion from CIE to RGB space */ -COLORMAT mat; -register RGBPRIMS pr; +int +colorprimsOK( /* are color primaries reasonable? */ +RGBPRIMS pr +) { + int i, j; + + for (i = 0; i < 4; i++) { + if ((pr[i][CIEX] <= -1.) | (pr[i][CIEY] <= -1.)) + return(0); + if ((pr[i][CIEX] >= 2.) | (pr[i][CIEY] >= 2.)) + return(0); + if (pr[i][CIEX] + pr[i][CIEY] <= -1.) + return(0); + if (pr[i][CIEX] + pr[i][CIEY] >= 2.) + return(0); + } + for (i = 0; i < 4; i++) + for (j = i+1; j < 4; j++) + if (CEQ(pr[i][CIEX],pr[j][CIEX]) && + CEQ(pr[i][CIEY],pr[j][CIEY])) + return(0); + return(1); +} + + + +int +compxyz2rgbmat( /* compute conversion from CIE to RGB space */ +COLORMAT mat, +register RGBPRIMS pr +) +{ double C_rD, C_gD, C_bD; if (pr == stdprims) { /* can use xyz2rgbmat */ cpcolormat(mat, xyz2rgbmat); - return; + return(1); } + if (CEQ(pr[WHT][CIEX],0.) | CEQ(pr[WHT][CIEY],0.)) + return(0); C_rD = (1./pr[WHT][CIEY]) * ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) - pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) + pr[GRN][CIEX]*pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] ) ; + if (CEQ(C_rD,0.)) + return(0); C_gD = (1./pr[WHT][CIEY]) * ( pr[WHT][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) - pr[WHT][CIEY]*(pr[BLU][CIEX] - pr[RED][CIEX]) - pr[RED][CIEX]*pr[BLU][CIEY] + pr[BLU][CIEX]*pr[RED][CIEY] ) ; + if (CEQ(C_gD,0.)) + return(0); C_bD = (1./pr[WHT][CIEY]) * ( pr[WHT][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) - pr[WHT][CIEY]*(pr[RED][CIEX] - pr[GRN][CIEX]) + pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY] ) ; - + if (CEQ(C_bD,0.)) + return(0); mat[0][0] = (pr[GRN][CIEY] - pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] + pr[BLU][CIEY]*pr[GRN][CIEX])/C_rD ; @@ -242,19 +319,24 @@ register RGBPRIMS pr; pr[RED][CIEX]*pr[GRN][CIEY])/C_bD ; mat[2][2] = (pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY])/C_bD ; + return(1); } -comprgb2xyzmat(mat, pr) /* compute conversion from RGB to CIE space */ -COLORMAT mat; -register RGBPRIMS pr; +int +comprgb2xyzmat( /* compute conversion from RGB to CIE space */ +COLORMAT mat, +register RGBPRIMS pr +) { double C_rD, C_gD, C_bD, D; if (pr == stdprims) { /* can use rgb2xyzmat */ cpcolormat(mat, rgb2xyzmat); - return; + return(1); } + if (CEQ(pr[WHT][CIEX],0.) | CEQ(pr[WHT][CIEY],0.)) + return(0); C_rD = (1./pr[WHT][CIEY]) * ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) - pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) + @@ -270,6 +352,8 @@ register RGBPRIMS pr; D = pr[RED][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) + pr[GRN][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) + pr[BLU][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) ; + if (CEQ(D,0.)) + return(0); mat[0][0] = pr[RED][CIEX]*C_rD/D; mat[0][1] = pr[GRN][CIEX]*C_gD/D; mat[0][2] = pr[BLU][CIEX]*C_bD/D; @@ -279,12 +363,16 @@ register RGBPRIMS pr; mat[2][0] = (1.-pr[RED][CIEX]-pr[RED][CIEY])*C_rD/D; mat[2][1] = (1.-pr[GRN][CIEX]-pr[GRN][CIEY])*C_gD/D; mat[2][2] = (1.-pr[BLU][CIEX]-pr[BLU][CIEY])*C_bD/D; + return(1); } -comprgb2rgbmat(mat, pr1, pr2) /* compute conversion from RGB1 to RGB2 */ -COLORMAT mat; -RGBPRIMS pr1, pr2; +int +comprgb2rgbmat( /* compute conversion from RGB1 to RGB2 */ +COLORMAT mat, +RGBPRIMS pr1, +RGBPRIMS pr2 +) { COLORMAT pr1toxyz, xyztopr2; @@ -292,10 +380,116 @@ RGBPRIMS pr1, pr2; mat[0][0] = mat[1][1] = mat[2][2] = 1.0; mat[0][1] = mat[0][2] = mat[1][0] = mat[1][2] = mat[2][0] = mat[2][1] = 0.0; - return; + return(1); } - comprgb2xyzmat(pr1toxyz, pr1); - compxyz2rgbmat(xyztopr2, pr2); + if (!comprgb2xyzmat(pr1toxyz, pr1)) + return(0); + if (!compxyz2rgbmat(xyztopr2, pr2)) + return(0); /* combine transforms */ multcolormat(mat, pr1toxyz, xyztopr2); + return(1); +} + + +int +compxyzWBmat( /* CIE von Kries transform from wht1 to wht2 */ +COLORMAT mat, +float wht1[2], +float wht2[2] +) +{ + COLOR cw1, cw2; + if (XYEQ(wht1,wht2)) { + mat[0][0] = mat[1][1] = mat[2][2] = 1.0; + mat[0][1] = mat[0][2] = mat[1][0] = + mat[1][2] = mat[2][0] = mat[2][1] = 0.0; + return(1); + } + if (CEQ(wht1[CIEX],0.) | CEQ(wht1[CIEY],0.)) + return(0); + cw1[RED] = wht1[CIEX]/wht1[CIEY]; + cw1[GRN] = 1.; + cw1[BLU] = (1. - wht1[CIEX] - wht1[CIEY])/wht1[CIEY]; + colortrans(cw1, vkmat, cw1); + if (CEQ(wht2[CIEX],0.) | CEQ(wht2[CIEY],0.)) + return(0); + cw2[RED] = wht2[CIEX]/wht2[CIEY]; + cw2[GRN] = 1.; + cw2[BLU] = (1. - wht2[CIEX] - wht2[CIEY])/wht2[CIEY]; + colortrans(cw2, vkmat, cw2); + if (CEQ(cw1[RED],0.) | CEQ(cw1[GRN],0.) | CEQ(cw1[BLU],0.)) + return(0); + mat[0][0] = cw2[RED]/cw1[RED]; + mat[1][1] = cw2[GRN]/cw1[GRN]; + mat[2][2] = cw2[BLU]/cw1[BLU]; + mat[0][1] = mat[0][2] = mat[1][0] = + mat[1][2] = mat[2][0] = mat[2][1] = 0.0; + multcolormat(mat, vkmat, mat); + multcolormat(mat, mat, ivkmat); + return(1); +} + + +int +compxyz2rgbWBmat( /* von Kries conversion from CIE to RGB space */ +COLORMAT mat, +RGBPRIMS pr +) +{ + COLORMAT wbmat; + + if (!compxyz2rgbmat(mat, pr)) + return(0); + if (XYEQ(pr[WHT],xyneu)) + return(1); + if (!compxyzWBmat(wbmat, xyneu, pr[WHT])) + return(0); + multcolormat(mat, wbmat, mat); + return(1); +} + +int +comprgb2xyzWBmat( /* von Kries conversion from RGB to CIE space */ +COLORMAT mat, +RGBPRIMS pr +) +{ + COLORMAT wbmat; + + if (!comprgb2xyzmat(mat, pr)) + return(0); + if (XYEQ(pr[WHT],xyneu)) + return(1); + if (!compxyzWBmat(wbmat, pr[WHT], xyneu)) + return(0); + multcolormat(mat, mat, wbmat); + return(1); +} + +int +comprgb2rgbWBmat( /* von Kries conversion from RGB1 to RGB2 */ +COLORMAT mat, +RGBPRIMS pr1, +RGBPRIMS pr2 +) +{ + COLORMAT pr1toxyz, xyztopr2, wbmat; + + if (pr1 == pr2) { + mat[0][0] = mat[1][1] = mat[2][2] = 1.0; + mat[0][1] = mat[0][2] = mat[1][0] = + mat[1][2] = mat[2][0] = mat[2][1] = 0.0; + return(1); + } + if (!comprgb2xyzmat(pr1toxyz, pr1)) + return(0); + if (!compxyzWBmat(wbmat, pr1[WHT], pr2[WHT])) + return(0); + if (!compxyz2rgbmat(xyztopr2, pr2)) + return(0); + /* combine transforms */ + multcolormat(mat, pr1toxyz, wbmat); + multcolormat(mat, mat, xyztopr2); + return(1); }