ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/spec_rgb.c
Revision: 2.5
Committed: Sun Oct 15 14:08:01 1995 UTC (28 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +126 -22 lines
Log Message:
made changes for new 32-bit_rle_xyze picture type

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     C_rD = (1./pr[WHT][CIEY]) *
149     ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) -
150     pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) +
151     pr[GRN][CIEX]*pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] ) ;
152     C_gD = (1./pr[WHT][CIEY]) *
153     ( pr[WHT][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) -
154     pr[WHT][CIEY]*(pr[BLU][CIEX] - pr[RED][CIEX]) -
155     pr[RED][CIEX]*pr[BLU][CIEY] + pr[BLU][CIEX]*pr[RED][CIEY] ) ;
156     C_bD = (1./pr[WHT][CIEY]) *
157     ( pr[WHT][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) -
158     pr[WHT][CIEY]*(pr[RED][CIEX] - pr[GRN][CIEX]) +
159     pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY] ) ;
160    
161     mat[0][0] = (pr[GRN][CIEY] - pr[BLU][CIEY] -
162     pr[BLU][CIEX]*pr[GRN][CIEY] +
163     pr[BLU][CIEY]*pr[GRN][CIEX])/C_rD ;
164     mat[0][1] = (pr[BLU][CIEX] - pr[GRN][CIEX] -
165     pr[BLU][CIEX]*pr[GRN][CIEY] +
166     pr[GRN][CIEX]*pr[BLU][CIEY])/C_rD ;
167     mat[0][2] = (pr[GRN][CIEX]*pr[BLU][CIEY] -
168     pr[BLU][CIEX]*pr[GRN][CIEY])/C_rD ;
169     mat[1][0] = (pr[BLU][CIEY] - pr[RED][CIEY] -
170     pr[BLU][CIEY]*pr[RED][CIEX] +
171     pr[RED][CIEY]*pr[BLU][CIEX])/C_gD ;
172     mat[1][1] = (pr[RED][CIEX] - pr[BLU][CIEX] -
173     pr[RED][CIEX]*pr[BLU][CIEY] +
174     pr[BLU][CIEX]*pr[RED][CIEY])/C_gD ;
175     mat[1][2] = (pr[BLU][CIEX]*pr[RED][CIEY] -
176     pr[RED][CIEX]*pr[BLU][CIEY])/C_gD ;
177     mat[2][0] = (pr[RED][CIEY] - pr[GRN][CIEY] -
178     pr[RED][CIEY]*pr[GRN][CIEX] +
179     pr[GRN][CIEY]*pr[RED][CIEX])/C_bD ;
180     mat[2][1] = (pr[GRN][CIEX] - pr[RED][CIEX] -
181     pr[GRN][CIEX]*pr[RED][CIEY] +
182     pr[RED][CIEX]*pr[GRN][CIEY])/C_bD ;
183     mat[2][2] = (pr[RED][CIEX]*pr[GRN][CIEY] -
184     pr[GRN][CIEX]*pr[RED][CIEY])/C_bD ;
185     }
186    
187    
188     comprgb2xyzmat(mat, pr) /* compute conversion from RGB to CIE space */
189     COLORMAT mat;
190     register RGBPRIMS pr;
191     {
192     double C_rD, C_gD, C_bD, D;
193    
194     C_rD = (1./pr[WHT][CIEY]) *
195     ( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) -
196     pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) +
197     pr[GRN][CIEX]*pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] ) ;
198     C_gD = (1./pr[WHT][CIEY]) *
199     ( pr[WHT][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) -
200     pr[WHT][CIEY]*(pr[BLU][CIEX] - pr[RED][CIEX]) -
201     pr[RED][CIEX]*pr[BLU][CIEY] + pr[BLU][CIEX]*pr[RED][CIEY] ) ;
202     C_bD = (1./pr[WHT][CIEY]) *
203     ( pr[WHT][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) -
204     pr[WHT][CIEY]*(pr[RED][CIEX] - pr[GRN][CIEX]) +
205     pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY] ) ;
206     D = pr[RED][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) +
207     pr[GRN][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) +
208     pr[BLU][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) ;
209     mat[0][0] = pr[RED][CIEX]*C_rD/D;
210     mat[0][1] = pr[GRN][CIEX]*C_gD/D;
211     mat[0][2] = pr[BLU][CIEX]*C_bD/D;
212     mat[1][0] = pr[RED][CIEY]*C_rD/D;
213     mat[1][1] = pr[GRN][CIEY]*C_gD/D;
214     mat[1][2] = pr[BLU][CIEY]*C_bD/D;
215     mat[2][0] = (1.-pr[RED][CIEX]-pr[RED][CIEY])*C_rD/D;
216     mat[2][1] = (1.-pr[GRN][CIEX]-pr[GRN][CIEY])*C_gD/D;
217     mat[2][2] = (1.-pr[BLU][CIEX]-pr[BLU][CIEY])*C_bD/D;
218     }
219    
220    
221     comprgb2rgbmat(mat, pr1, pr2) /* compute conversion from RGB1 to RGB2 */
222     COLORMAT mat;
223     RGBPRIMS pr1, pr2;
224     {
225     COLORMAT pr1toxyz, xyztopr2;
226    
227     if (pr1 == stdprims) /* can use rgb2xyzmat */
228     cpcolormat(pr1toxyz, rgb2xyzmat);
229     else /* otherwise compute it */
230     comprgb2xyzmat(pr1toxyz, pr1);
231     if (pr2 == stdprims) /* can use xyz2rgbmat */
232     cpcolormat(xyztopr2, xyz2rgbmat);
233     else /* otherwise compute it */
234     compxyz2rgbmat(xyztopr2, pr2);
235     /* combine transforms */
236     multcolormat(mat, pr1toxyz, xyztopr2);
237 greg 1.1 }