1 |
– |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Convert colors and spectral ranges. |
6 |
+ |
* Added von Kries white-balance calculations 10/01 (GW). |
7 |
+ |
* |
8 |
+ |
* Externals declared in color.h |
9 |
|
*/ |
10 |
|
|
11 |
+ |
/* ==================================================================== |
12 |
+ |
* The Radiance Software License, Version 1.0 |
13 |
+ |
* |
14 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
15 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
16 |
+ |
* |
17 |
+ |
* Redistribution and use in source and binary forms, with or without |
18 |
+ |
* modification, are permitted provided that the following conditions |
19 |
+ |
* are met: |
20 |
+ |
* |
21 |
+ |
* 1. Redistributions of source code must retain the above copyright |
22 |
+ |
* notice, this list of conditions and the following disclaimer. |
23 |
+ |
* |
24 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
25 |
+ |
* notice, this list of conditions and the following disclaimer in |
26 |
+ |
* the documentation and/or other materials provided with the |
27 |
+ |
* distribution. |
28 |
+ |
* |
29 |
+ |
* 3. The end-user documentation included with the redistribution, |
30 |
+ |
* if any, must include the following acknowledgment: |
31 |
+ |
* "This product includes Radiance software |
32 |
+ |
* (http://radsite.lbl.gov/) |
33 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
34 |
+ |
* (http://www.lbl.gov/)." |
35 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
36 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
37 |
+ |
* |
38 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
39 |
+ |
* and "The Regents of the University of California" must |
40 |
+ |
* not be used to endorse or promote products derived from this |
41 |
+ |
* software without prior written permission. For written |
42 |
+ |
* permission, please contact [email protected]. |
43 |
+ |
* |
44 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
45 |
+ |
* nor may "Radiance" appear in their name, without prior written |
46 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
47 |
+ |
* |
48 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
49 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
50 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
51 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
52 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
53 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
54 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
55 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
56 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
57 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
58 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
59 |
+ |
* SUCH DAMAGE. |
60 |
+ |
* ==================================================================== |
61 |
+ |
* |
62 |
+ |
* This software consists of voluntary contributions made by many |
63 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
64 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
65 |
+ |
* <http://www.lbl.gov/>. |
66 |
+ |
*/ |
67 |
+ |
|
68 |
|
#include "color.h" |
69 |
+ |
#include <string.h> |
70 |
|
|
71 |
+ |
#define CEPS 1e-4 /* color epsilon */ |
72 |
|
|
73 |
< |
RGBPRIMS stdprims = STDPRIMS; /* standard primary chromaticities */ |
73 |
> |
#define CEQ(v1,v2) ((v1) <= (v2)+CEPS && (v2) <= (v1)+CEPS) |
74 |
|
|
75 |
+ |
#define XYEQ(c1,c2) (CEQ((c1)[CIEX],(c2)[CIEX]) && CEQ((c1)[CIEY],(c2)[CIEY])) |
76 |
+ |
|
77 |
+ |
|
78 |
+ |
RGBPRIMS stdprims = STDPRIMS; /* standard primary chromaticities */ |
79 |
+ |
|
80 |
|
COLOR cblack = BLKCOLOR; /* global black color */ |
81 |
|
COLOR cwhite = WHTCOLOR; /* global white color */ |
82 |
|
|
83 |
+ |
float xyneu[2] = {1./3., 1./3.}; /* neutral xy chromaticities */ |
84 |
+ |
|
85 |
|
/* |
86 |
|
* The following table contains the CIE tristimulus integrals |
87 |
|
* for X, Y, and Z. The table is cumulative, so that |
111 |
|
} |
112 |
|
}; |
113 |
|
|
114 |
< |
COLORMAT xyz2rgbmat = { /* XYZ to RGB */ |
114 |
> |
COLORMAT xyz2rgbmat = { /* XYZ to RGB (no white balance) */ |
115 |
|
{(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD, |
116 |
|
(CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD, |
117 |
|
(CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD}, |
123 |
|
(CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD} |
124 |
|
}; |
125 |
|
|
126 |
< |
COLORMAT rgb2xyzmat = { /* RGB to XYZ */ |
126 |
> |
COLORMAT rgb2xyzmat = { /* RGB to XYZ (no white balance) */ |
127 |
|
{CIE_x_r*CIE_C_rD/CIE_D,CIE_x_g*CIE_C_gD/CIE_D,CIE_x_b*CIE_C_bD/CIE_D}, |
128 |
|
{CIE_y_r*CIE_C_rD/CIE_D,CIE_y_g*CIE_C_gD/CIE_D,CIE_y_b*CIE_C_bD/CIE_D}, |
129 |
|
{(1.-CIE_x_r-CIE_y_r)*CIE_C_rD/CIE_D, |
131 |
|
(1.-CIE_x_b-CIE_y_b)*CIE_C_bD/CIE_D} |
132 |
|
}; |
133 |
|
|
134 |
+ |
COLORMAT vkmat = { /* Sharp primary matrix */ |
135 |
+ |
{ 1.2694, -0.0988, -0.1706}, |
136 |
+ |
{-0.8364, 1.8006, 0.0357}, |
137 |
+ |
{ 0.0297, -0.0315, 1.0018} |
138 |
+ |
}; |
139 |
|
|
140 |
+ |
COLORMAT ivkmat = { /* inverse Sharp primary matrix */ |
141 |
+ |
{ 0.8156, 0.0472, 0.1372}, |
142 |
+ |
{ 0.3791, 0.5769, 0.0440}, |
143 |
+ |
{-0.0123, 0.0167, 0.9955} |
144 |
+ |
}; |
145 |
|
|
146 |
+ |
|
147 |
+ |
void |
148 |
|
spec_rgb(col, s, e) /* compute RGB color from spectral range */ |
149 |
|
COLOR col; |
150 |
|
int s, e; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
+ |
void |
160 |
|
spec_cie(col, s, e) /* compute a color from a spectral range */ |
161 |
|
COLOR col; /* returned color */ |
162 |
|
int s, e; /* starting and ending wavelengths */ |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
+ |
void |
195 |
|
cie_rgb(rgb, xyz) /* convert CIE color to standard RGB */ |
196 |
< |
COLOR rgb, xyz; |
196 |
> |
COLOR rgb; |
197 |
> |
COLOR xyz; |
198 |
|
{ |
199 |
|
colortrans(rgb, xyz2rgbmat, xyz); |
200 |
|
clipgamut(rgb, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite); |
232 |
|
vv = 1.; /* check each limit */ |
233 |
|
for (i = 0; i < 3; i++) |
234 |
|
if (gamut & CGAMUT_LOWER && col[i] < lower[i]) { |
235 |
< |
v = (lower[i] - cgry[i])/(col[i] - cgry[i]); |
235 |
> |
v = (lower[i]+CEPS - cgry[i])/(col[i] - cgry[i]); |
236 |
|
if (v < vv) vv = v; |
237 |
|
rflags |= CGAMUT_LOWER; |
238 |
|
} else if (gamut & CGAMUT_UPPER && col[i] > upper[i]) { |
239 |
< |
v = (upper[i] - cgry[i])/(col[i] - cgry[i]); |
239 |
> |
v = (upper[i]-CEPS - cgry[i])/(col[i] - cgry[i]); |
240 |
|
if (v < vv) vv = v; |
241 |
|
rflags |= CGAMUT_UPPER; |
242 |
|
} |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
+ |
void |
251 |
|
colortrans(c2, mat, c1) /* convert c1 by mat and put into c2 */ |
252 |
+ |
register COLOR c2; |
253 |
|
register COLORMAT mat; |
254 |
< |
register COLOR c1, c2; |
254 |
> |
register COLOR c1; |
255 |
|
{ |
256 |
|
COLOR cout; |
257 |
|
|
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
+ |
void |
267 |
|
multcolormat(m3, m2, m1) /* multiply m1 by m2 and put into m3 */ |
268 |
< |
COLORMAT m1, m2, m3; /* m3 can be either m1 or m2 w/o harm */ |
268 |
> |
COLORMAT m3; /* m3 can be either m1 or m2 w/o harm */ |
269 |
> |
COLORMAT m2, m1; |
270 |
|
{ |
271 |
|
COLORMAT mt; |
272 |
|
register int i, j; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
+ |
void |
284 |
|
compxyz2rgbmat(mat, pr) /* compute conversion from CIE to RGB space */ |
285 |
|
COLORMAT mat; |
286 |
|
register RGBPRIMS pr; |
331 |
|
} |
332 |
|
|
333 |
|
|
334 |
+ |
void |
335 |
|
comprgb2xyzmat(mat, pr) /* compute conversion from RGB to CIE space */ |
336 |
|
COLORMAT mat; |
337 |
|
register RGBPRIMS pr; |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
+ |
void |
373 |
|
comprgb2rgbmat(mat, pr1, pr2) /* compute conversion from RGB1 to RGB2 */ |
374 |
|
COLORMAT mat; |
375 |
|
RGBPRIMS pr1, pr2; |
386 |
|
compxyz2rgbmat(xyztopr2, pr2); |
387 |
|
/* combine transforms */ |
388 |
|
multcolormat(mat, pr1toxyz, xyztopr2); |
389 |
+ |
} |
390 |
+ |
|
391 |
+ |
|
392 |
+ |
void |
393 |
+ |
compxyzWBmat(mat, wht1, wht2) /* CIE von Kries transform from wht1 to wht2 */ |
394 |
+ |
COLORMAT mat; |
395 |
+ |
float wht1[2], wht2[2]; |
396 |
+ |
{ |
397 |
+ |
COLOR cw1, cw2; |
398 |
+ |
if (XYEQ(wht1,wht2)) { |
399 |
+ |
mat[0][0] = mat[1][1] = mat[2][2] = 1.0; |
400 |
+ |
mat[0][1] = mat[0][2] = mat[1][0] = |
401 |
+ |
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
402 |
+ |
return; |
403 |
+ |
} |
404 |
+ |
cw1[RED] = wht1[CIEX]/wht1[CIEY]; |
405 |
+ |
cw1[GRN] = 1.; |
406 |
+ |
cw1[BLU] = (1. - wht1[CIEX] - wht1[CIEY])/wht1[CIEY]; |
407 |
+ |
colortrans(cw1, vkmat, cw1); |
408 |
+ |
cw2[RED] = wht2[CIEX]/wht2[CIEY]; |
409 |
+ |
cw2[GRN] = 1.; |
410 |
+ |
cw2[BLU] = (1. - wht2[CIEX] - wht2[CIEY])/wht2[CIEY]; |
411 |
+ |
colortrans(cw2, vkmat, cw2); |
412 |
+ |
mat[0][0] = cw2[RED]/cw1[RED]; |
413 |
+ |
mat[1][1] = cw2[GRN]/cw1[GRN]; |
414 |
+ |
mat[2][2] = cw2[BLU]/cw1[BLU]; |
415 |
+ |
mat[0][1] = mat[0][2] = mat[1][0] = |
416 |
+ |
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
417 |
+ |
multcolormat(mat, vkmat, mat); |
418 |
+ |
multcolormat(mat, mat, ivkmat); |
419 |
+ |
} |
420 |
+ |
|
421 |
+ |
|
422 |
+ |
void |
423 |
+ |
compxyz2rgbWBmat(mat, pr) /* von Kries conversion from CIE to RGB space */ |
424 |
+ |
COLORMAT mat; |
425 |
+ |
RGBPRIMS pr; |
426 |
+ |
{ |
427 |
+ |
COLORMAT wbmat; |
428 |
+ |
|
429 |
+ |
compxyz2rgbmat(mat, pr); |
430 |
+ |
if (XYEQ(pr[WHT],xyneu)) |
431 |
+ |
return; |
432 |
+ |
compxyzWBmat(wbmat, xyneu, pr[WHT]); |
433 |
+ |
multcolormat(mat, wbmat, mat); |
434 |
+ |
} |
435 |
+ |
|
436 |
+ |
void |
437 |
+ |
comprgb2xyzWBmat(mat, pr) /* von Kries conversion from RGB to CIE space */ |
438 |
+ |
COLORMAT mat; |
439 |
+ |
RGBPRIMS pr; |
440 |
+ |
{ |
441 |
+ |
COLORMAT wbmat; |
442 |
+ |
|
443 |
+ |
comprgb2xyzmat(mat, pr); |
444 |
+ |
if (XYEQ(pr[WHT],xyneu)) |
445 |
+ |
return; |
446 |
+ |
compxyzWBmat(wbmat, pr[WHT], xyneu); |
447 |
+ |
multcolormat(mat, mat, wbmat); |
448 |
+ |
} |
449 |
+ |
|
450 |
+ |
void |
451 |
+ |
comprgb2rgbWBmat(mat, pr1, pr2) /* von Kries conversion from RGB1 to RGB2 */ |
452 |
+ |
COLORMAT mat; |
453 |
+ |
RGBPRIMS pr1, pr2; |
454 |
+ |
{ |
455 |
+ |
COLORMAT pr1toxyz, xyztopr2, wbmat; |
456 |
+ |
|
457 |
+ |
if (pr1 == pr2) { |
458 |
+ |
mat[0][0] = mat[1][1] = mat[2][2] = 1.0; |
459 |
+ |
mat[0][1] = mat[0][2] = mat[1][0] = |
460 |
+ |
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
461 |
+ |
return; |
462 |
+ |
} |
463 |
+ |
comprgb2xyzmat(pr1toxyz, pr1); |
464 |
+ |
compxyzWBmat(wbmat, pr1[WHT], pr2[WHT]); |
465 |
+ |
compxyz2rgbmat(xyztopr2, pr2); |
466 |
+ |
/* combine transforms */ |
467 |
+ |
multcolormat(mat, pr1toxyz, wbmat); |
468 |
+ |
multcolormat(mat, mat, xyztopr2); |
469 |
|
} |