--- ray/src/common/spec_rgb.c 1990/09/22 10:44:56 1.1 +++ ray/src/common/spec_rgb.c 1994/07/07 15:21:25 2.4 @@ -1,3 +1,5 @@ +/* Copyright (c) 1990 Regents of the University of California */ + #ifndef lint static char SCCSid[] = "$SunId$ LBL"; #endif @@ -37,7 +39,28 @@ static BYTE chroma[3][NINC] = { } }; +static float xyz2rgbmat[3][3] = { /* XYZ to RGB */ + {(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}, + {(CIE_y_b - CIE_y_r - CIE_y_b*CIE_x_r + CIE_y_r*CIE_x_b)/CIE_C_gD, + (CIE_x_r - CIE_x_b - CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r)/CIE_C_gD, + (CIE_x_b*CIE_y_r - CIE_x_r*CIE_y_b)/CIE_C_gD}, + {(CIE_y_r - CIE_y_g - CIE_y_r*CIE_x_g + CIE_y_g*CIE_x_r)/CIE_C_bD, + (CIE_x_g - CIE_x_r - CIE_x_g*CIE_y_r + CIE_x_r*CIE_y_g)/CIE_C_bD, + (CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD} +}; +static float rgb2xyzmat[3][3] = { /* RGB to XYZ */ + {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, + (1.-CIE_x_g-CIE_y_g)*CIE_C_gD/CIE_D, + (1.-CIE_x_b-CIE_y_b)*CIE_C_bD/CIE_D} +}; + + + spec_rgb(col, s, e) /* compute RGB color from spectral range */ COLOR col; int s, e; @@ -60,6 +83,10 @@ int s, e; /* starting and ending wavelengths */ s = 0; e -= STARTWL; + if (e <= s) { + col[RED] = col[GRN] = col[BLU] = 0.0; + return; + } if (e >= INCWL*(NINC - 1)) e = INCWL*(NINC - 1) - 1; @@ -79,21 +106,28 @@ int s, e; /* starting and ending wavelengths */ } -cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB (NTSC) */ +cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB */ register COLOR rgbcolor, ciecolor; { - static float cmat[3][3] = { - 1.73, -.48, -.26, - -.81, 1.65, -.02, - .08, -.17, 1.28, - }; register int i; for (i = 0; i < 3; i++) { - rgbcolor[i] = cmat[i][0]*ciecolor[0] + - cmat[i][1]*ciecolor[1] + - cmat[i][2]*ciecolor[2] ; + rgbcolor[i] = xyz2rgbmat[i][0]*ciecolor[0] + + xyz2rgbmat[i][1]*ciecolor[1] + + xyz2rgbmat[i][2]*ciecolor[2] ; if (rgbcolor[i] < 0.0) rgbcolor[i] = 0.0; } +} + + +rgb_cie(ciecolor, rgbcolor) /* convert RGB to CIE */ +register COLOR ciecolor, rgbcolor; +{ + register int i; + + for (i = 0; i < 3; i++) + ciecolor[i] = rgb2xyzmat[i][0]*rgbcolor[0] + + rgb2xyzmat[i][1]*rgbcolor[1] + + rgb2xyzmat[i][2]*rgbcolor[2] ; }