ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/spec_rgb.c
Revision: 2.6
Committed: Wed Oct 2 11:05:18 1996 UTC (27 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +16 -8 lines
Log Message:
made testing for stdprims more sensible

File Contents

# User Rev Content
1 greg 1.2 /* Copyright (c) 1990 Regents of the University of California */
2    
3 greg 1.1 #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Convert colors and spectral ranges.
9     */
10    
11     #include "color.h"
12    
13 greg 2.5
14     RGBPRIMS stdprims = STDPRIMS; /* standard primary chromaticities */
15    
16 greg 1.1 /*
17     * The following table contains the CIE tristimulus integrals
18     * for X, Y, and Z. The table is cumulative, so that
19     * each color coordinate integrates to 1.
20     */
21    
22     #define STARTWL 380 /* starting wavelength (nanometers) */
23     #define INCWL 10 /* wavelength increment */
24     #define NINC 40 /* # of values */
25    
26     static BYTE chroma[3][NINC] = {
27     { /* X */
28     0, 0, 0, 2, 6, 13, 22, 30, 36, 41,
29     42, 43, 43, 44, 46, 52, 60, 71, 87, 106,
30     128, 153, 178, 200, 219, 233, 243, 249, 252, 254,
31     255, 255, 255, 255, 255, 255, 255, 255, 255, 255
32     }, { /* Y */
33     0, 0, 0, 0, 0, 1, 2, 4, 7, 11,
34     17, 24, 34, 48, 64, 84, 105, 127, 148, 169,
35     188, 205, 220, 232, 240, 246, 250, 253, 254, 255,
36     255, 255, 255, 255, 255, 255, 255, 255, 255, 255
37     }, { /* Z */
38     0, 0, 2, 10, 32, 66, 118, 153, 191, 220,
39     237, 246, 251, 253, 254, 255, 255, 255, 255, 255,
40     255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
41     255, 255, 255, 255, 255, 255, 255, 255, 255, 255
42     }
43     };
44    
45 greg 2.5 COLORMAT xyz2rgbmat = { /* XYZ to RGB */
46 greg 2.3 {(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD,
47     (CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD,
48     (CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD},
49     {(CIE_y_b - CIE_y_r - CIE_y_b*CIE_x_r + CIE_y_r*CIE_x_b)/CIE_C_gD,
50     (CIE_x_r - CIE_x_b - CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r)/CIE_C_gD,
51     (CIE_x_b*CIE_y_r - CIE_x_r*CIE_y_b)/CIE_C_gD},
52     {(CIE_y_r - CIE_y_g - CIE_y_r*CIE_x_g + CIE_y_g*CIE_x_r)/CIE_C_bD,
53     (CIE_x_g - CIE_x_r - CIE_x_g*CIE_y_r + CIE_x_r*CIE_y_g)/CIE_C_bD,
54     (CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD}
55 greg 1.2 };
56 greg 1.1
57 greg 2.5 COLORMAT rgb2xyzmat = { /* RGB to XYZ */
58 greg 2.4 {CIE_x_r*CIE_C_rD/CIE_D,CIE_x_g*CIE_C_gD/CIE_D,CIE_x_b*CIE_C_bD/CIE_D},
59     {CIE_y_r*CIE_C_rD/CIE_D,CIE_y_g*CIE_C_gD/CIE_D,CIE_y_b*CIE_C_bD/CIE_D},
60     {(1.-CIE_x_r-CIE_y_r)*CIE_C_rD/CIE_D,
61     (1.-CIE_x_g-CIE_y_g)*CIE_C_gD/CIE_D,
62     (1.-CIE_x_b-CIE_y_b)*CIE_C_bD/CIE_D}
63     };
64 greg 1.2
65    
66 greg 2.4
67 greg 1.1 spec_rgb(col, s, e) /* compute RGB color from spectral range */
68     COLOR col;
69     int s, e;
70     {
71     COLOR ciecolor;
72    
73     spec_cie(ciecolor, s, e);
74     cie_rgb(col, ciecolor);
75     }
76    
77    
78     spec_cie(col, s, e) /* compute a color from a spectral range */
79     COLOR col; /* returned color */
80     int s, e; /* starting and ending wavelengths */
81     {
82     register int i, d, r;
83    
84     s -= STARTWL;
85     if (s < 0)
86     s = 0;
87    
88     e -= STARTWL;
89 greg 1.2 if (e <= s) {
90 greg 2.5 col[CIEX] = col[CIEY] = col[CIEZ] = 0.0;
91 greg 1.2 return;
92     }
93 greg 1.1 if (e >= INCWL*(NINC - 1))
94     e = INCWL*(NINC - 1) - 1;
95    
96     d = e / INCWL; /* interpolate values */
97     r = e % INCWL;
98     for (i = 0; i < 3; i++)
99     col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r;
100    
101     d = s / INCWL;
102     r = s % INCWL;
103     for (i = 0; i < 3; i++)
104     col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
105    
106 greg 2.5 col[CIEX] = (col[CIEX] + 0.5) * (1./(256*INCWL));
107     col[CIEY] = (col[CIEY] + 0.5) * (1./(256*INCWL));
108     col[CIEZ] = (col[CIEZ] + 0.5) * (1./(256*INCWL));
109 greg 1.1 }
110    
111    
112 greg 2.5 colortrans(c2, mat, c1) /* convert c1 by mat and put into c2 */
113     register COLORMAT mat;
114     register COLOR c1, c2;
115 greg 1.1 {
116 greg 2.5 static float cout[3];
117 greg 1.1
118 greg 2.5 cout[0] = mat[0][0]*c1[0] + mat[0][1]*c1[1] + mat[0][2]*c1[2];
119     cout[1] = mat[1][0]*c1[0] + mat[1][1]*c1[1] + mat[1][2]*c1[2];
120     cout[2] = mat[2][0]*c1[0] + mat[2][1]*c1[1] + mat[2][2]*c1[2];
121     if((c2[0] = cout[0]) < 0.) c2[0] = 0.;
122     if((c2[1] = cout[1]) < 0.) c2[1] = 0.;
123     if((c2[2] = cout[2]) < 0.) c2[2] = 0.;
124 greg 2.4 }
125    
126    
127 greg 2.5 multcolormat(m3, m2, m1) /* multiply m1 by m2 and put into m3 */
128     COLORMAT m1, m2, m3; /* m3 can be either m1 or m2 w/o harm */
129 greg 2.4 {
130 greg 2.5 COLORMAT mt;
131     register int i, j;
132 greg 2.4
133     for (i = 0; i < 3; i++)
134 greg 2.5 for (j = 0; j < 3; j++)
135     mt[i][j] = m1[i][0]*m2[0][j] +
136     m1[i][1]*m2[1][j] +
137     m1[i][2]*m2[2][j] ;
138     cpcolormat(m3, mt);
139     }
140    
141    
142     compxyz2rgbmat(mat, pr) /* compute conversion from CIE to RGB space */
143     COLORMAT mat;
144     register RGBPRIMS pr;
145     {
146     double C_rD, C_gD, C_bD;
147    
148 greg 2.6 if (pr == stdprims) { /* can use xyz2rgbmat */
149     cpcolormat(mat, xyz2rgbmat);
150     return;
151     }
152 greg 2.5 C_rD = (1./pr[WHT][CIEY]) *
153     ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) -
154     pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) +
155     pr[GRN][CIEX]*pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] ) ;
156     C_gD = (1./pr[WHT][CIEY]) *
157     ( pr[WHT][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) -
158     pr[WHT][CIEY]*(pr[BLU][CIEX] - pr[RED][CIEX]) -
159     pr[RED][CIEX]*pr[BLU][CIEY] + pr[BLU][CIEX]*pr[RED][CIEY] ) ;
160     C_bD = (1./pr[WHT][CIEY]) *
161     ( pr[WHT][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) -
162     pr[WHT][CIEY]*(pr[RED][CIEX] - pr[GRN][CIEX]) +
163     pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY] ) ;
164    
165     mat[0][0] = (pr[GRN][CIEY] - pr[BLU][CIEY] -
166     pr[BLU][CIEX]*pr[GRN][CIEY] +
167     pr[BLU][CIEY]*pr[GRN][CIEX])/C_rD ;
168     mat[0][1] = (pr[BLU][CIEX] - pr[GRN][CIEX] -
169     pr[BLU][CIEX]*pr[GRN][CIEY] +
170     pr[GRN][CIEX]*pr[BLU][CIEY])/C_rD ;
171     mat[0][2] = (pr[GRN][CIEX]*pr[BLU][CIEY] -
172     pr[BLU][CIEX]*pr[GRN][CIEY])/C_rD ;
173     mat[1][0] = (pr[BLU][CIEY] - pr[RED][CIEY] -
174     pr[BLU][CIEY]*pr[RED][CIEX] +
175     pr[RED][CIEY]*pr[BLU][CIEX])/C_gD ;
176     mat[1][1] = (pr[RED][CIEX] - pr[BLU][CIEX] -
177     pr[RED][CIEX]*pr[BLU][CIEY] +
178     pr[BLU][CIEX]*pr[RED][CIEY])/C_gD ;
179     mat[1][2] = (pr[BLU][CIEX]*pr[RED][CIEY] -
180     pr[RED][CIEX]*pr[BLU][CIEY])/C_gD ;
181     mat[2][0] = (pr[RED][CIEY] - pr[GRN][CIEY] -
182     pr[RED][CIEY]*pr[GRN][CIEX] +
183     pr[GRN][CIEY]*pr[RED][CIEX])/C_bD ;
184     mat[2][1] = (pr[GRN][CIEX] - pr[RED][CIEX] -
185     pr[GRN][CIEX]*pr[RED][CIEY] +
186     pr[RED][CIEX]*pr[GRN][CIEY])/C_bD ;
187     mat[2][2] = (pr[RED][CIEX]*pr[GRN][CIEY] -
188     pr[GRN][CIEX]*pr[RED][CIEY])/C_bD ;
189     }
190    
191    
192     comprgb2xyzmat(mat, pr) /* compute conversion from RGB to CIE space */
193     COLORMAT mat;
194     register RGBPRIMS pr;
195     {
196     double C_rD, C_gD, C_bD, D;
197    
198 greg 2.6 if (pr == stdprims) { /* can use rgb2xyzmat */
199     cpcolormat(mat, rgb2xyzmat);
200     return;
201     }
202 greg 2.5 C_rD = (1./pr[WHT][CIEY]) *
203     ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) -
204     pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) +
205     pr[GRN][CIEX]*pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] ) ;
206     C_gD = (1./pr[WHT][CIEY]) *
207     ( pr[WHT][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) -
208     pr[WHT][CIEY]*(pr[BLU][CIEX] - pr[RED][CIEX]) -
209     pr[RED][CIEX]*pr[BLU][CIEY] + pr[BLU][CIEX]*pr[RED][CIEY] ) ;
210     C_bD = (1./pr[WHT][CIEY]) *
211     ( pr[WHT][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) -
212     pr[WHT][CIEY]*(pr[RED][CIEX] - pr[GRN][CIEX]) +
213     pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY] ) ;
214     D = pr[RED][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) +
215     pr[GRN][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) +
216     pr[BLU][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) ;
217     mat[0][0] = pr[RED][CIEX]*C_rD/D;
218     mat[0][1] = pr[GRN][CIEX]*C_gD/D;
219     mat[0][2] = pr[BLU][CIEX]*C_bD/D;
220     mat[1][0] = pr[RED][CIEY]*C_rD/D;
221     mat[1][1] = pr[GRN][CIEY]*C_gD/D;
222     mat[1][2] = pr[BLU][CIEY]*C_bD/D;
223     mat[2][0] = (1.-pr[RED][CIEX]-pr[RED][CIEY])*C_rD/D;
224     mat[2][1] = (1.-pr[GRN][CIEX]-pr[GRN][CIEY])*C_gD/D;
225     mat[2][2] = (1.-pr[BLU][CIEX]-pr[BLU][CIEY])*C_bD/D;
226     }
227    
228    
229     comprgb2rgbmat(mat, pr1, pr2) /* compute conversion from RGB1 to RGB2 */
230     COLORMAT mat;
231     RGBPRIMS pr1, pr2;
232     {
233     COLORMAT pr1toxyz, xyztopr2;
234    
235 greg 2.6 if (pr1 == pr2) {
236     mat[0][0] = mat[1][1] = mat[2][2] = 1.0;
237     mat[0][1] = mat[0][2] = mat[1][0] =
238     mat[1][2] = mat[2][0] = mat[2][1] = 0.0;
239     return;
240     }
241     comprgb2xyzmat(pr1toxyz, pr1);
242     compxyz2rgbmat(xyztopr2, pr2);
243 greg 2.5 /* combine transforms */
244     multcolormat(mat, pr1toxyz, xyztopr2);
245 greg 1.1 }