ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/spec_rgb.c
Revision: 2.3
Committed: Wed Jun 29 18:16:58 1994 UTC (29 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +10 -12 lines
Log Message:
changed RGB primaries and went to true (i.e. equal-energy) white

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     /*
14     * The following table contains the CIE tristimulus integrals
15     * for X, Y, and Z. The table is cumulative, so that
16     * each color coordinate integrates to 1.
17     */
18    
19     #define STARTWL 380 /* starting wavelength (nanometers) */
20     #define INCWL 10 /* wavelength increment */
21     #define NINC 40 /* # of values */
22    
23     static BYTE chroma[3][NINC] = {
24     { /* X */
25     0, 0, 0, 2, 6, 13, 22, 30, 36, 41,
26     42, 43, 43, 44, 46, 52, 60, 71, 87, 106,
27     128, 153, 178, 200, 219, 233, 243, 249, 252, 254,
28     255, 255, 255, 255, 255, 255, 255, 255, 255, 255
29     }, { /* Y */
30     0, 0, 0, 0, 0, 1, 2, 4, 7, 11,
31     17, 24, 34, 48, 64, 84, 105, 127, 148, 169,
32     188, 205, 220, 232, 240, 246, 250, 253, 254, 255,
33     255, 255, 255, 255, 255, 255, 255, 255, 255, 255
34     }, { /* Z */
35     0, 0, 2, 10, 32, 66, 118, 153, 191, 220,
36     237, 246, 251, 253, 254, 255, 255, 255, 255, 255,
37     255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
38     255, 255, 255, 255, 255, 255, 255, 255, 255, 255
39     }
40     };
41    
42 greg 2.3 static float xyz2rgbmat[3][3] = { /* XYZ to RGB */
43     {(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD,
44     (CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD,
45     (CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD},
46     {(CIE_y_b - CIE_y_r - CIE_y_b*CIE_x_r + CIE_y_r*CIE_x_b)/CIE_C_gD,
47     (CIE_x_r - CIE_x_b - CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r)/CIE_C_gD,
48     (CIE_x_b*CIE_y_r - CIE_x_r*CIE_y_b)/CIE_C_gD},
49     {(CIE_y_r - CIE_y_g - CIE_y_r*CIE_x_g + CIE_y_g*CIE_x_r)/CIE_C_bD,
50     (CIE_x_g - CIE_x_r - CIE_x_g*CIE_y_r + CIE_x_r*CIE_y_g)/CIE_C_bD,
51     (CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD}
52 greg 1.2 };
53 greg 1.1
54 greg 1.2
55    
56 greg 1.1 spec_rgb(col, s, e) /* compute RGB color from spectral range */
57     COLOR col;
58     int s, e;
59     {
60     COLOR ciecolor;
61    
62     spec_cie(ciecolor, s, e);
63     cie_rgb(col, ciecolor);
64     }
65    
66    
67     spec_cie(col, s, e) /* compute a color from a spectral range */
68     COLOR col; /* returned color */
69     int s, e; /* starting and ending wavelengths */
70     {
71     register int i, d, r;
72    
73     s -= STARTWL;
74     if (s < 0)
75     s = 0;
76    
77     e -= STARTWL;
78 greg 1.2 if (e <= s) {
79     col[RED] = col[GRN] = col[BLU] = 0.0;
80     return;
81     }
82 greg 1.1 if (e >= INCWL*(NINC - 1))
83     e = INCWL*(NINC - 1) - 1;
84    
85     d = e / INCWL; /* interpolate values */
86     r = e % INCWL;
87     for (i = 0; i < 3; i++)
88     col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r;
89    
90     d = s / INCWL;
91     r = s % INCWL;
92     for (i = 0; i < 3; i++)
93     col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
94    
95     col[RED] = (col[RED] + 0.5) / (256*INCWL);
96     col[GRN] = (col[GRN] + 0.5) / (256*INCWL);
97     col[BLU] = (col[BLU] + 0.5) / (256*INCWL);
98     }
99    
100    
101 greg 1.3 cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB */
102 greg 1.1 register COLOR rgbcolor, ciecolor;
103     {
104     register int i;
105    
106     for (i = 0; i < 3; i++) {
107 greg 1.2 rgbcolor[i] = xyz2rgbmat[i][0]*ciecolor[0] +
108     xyz2rgbmat[i][1]*ciecolor[1] +
109     xyz2rgbmat[i][2]*ciecolor[2] ;
110 greg 1.1 if (rgbcolor[i] < 0.0)
111     rgbcolor[i] = 0.0;
112     }
113     }