| 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 |
+ |
#include "copyright.h" |
| 12 |
+ |
|
| 13 |
+ |
#include <stdio.h> |
| 14 |
+ |
#include <string.h> |
| 15 |
|
#include "color.h" |
| 16 |
|
|
| 17 |
+ |
#define CEPS 1e-4 /* color epsilon */ |
| 18 |
|
|
| 19 |
< |
RGBPRIMS stdprims = STDPRIMS; /* standard primary chromaticities */ |
| 19 |
> |
#define CEQ(v1,v2) ((v1) <= (v2)+CEPS && (v2) <= (v1)+CEPS) |
| 20 |
|
|
| 21 |
+ |
#define XYEQ(c1,c2) (CEQ((c1)[CIEX],(c2)[CIEX]) && CEQ((c1)[CIEY],(c2)[CIEY])) |
| 22 |
+ |
|
| 23 |
+ |
|
| 24 |
+ |
RGBPRIMS stdprims = STDPRIMS; /* standard primary chromaticities */ |
| 25 |
+ |
|
| 26 |
|
COLOR cblack = BLKCOLOR; /* global black color */ |
| 27 |
|
COLOR cwhite = WHTCOLOR; /* global white color */ |
| 28 |
|
|
| 29 |
+ |
float xyneu[2] = {1./3., 1./3.}; /* neutral xy chromaticities */ |
| 30 |
+ |
|
| 31 |
|
/* |
| 32 |
|
* The following table contains the CIE tristimulus integrals |
| 33 |
|
* for X, Y, and Z. The table is cumulative, so that |
| 57 |
|
} |
| 58 |
|
}; |
| 59 |
|
|
| 60 |
< |
COLORMAT xyz2rgbmat = { /* XYZ to RGB */ |
| 60 |
> |
COLORMAT xyz2rgbmat = { /* XYZ to RGB (no white balance) */ |
| 61 |
|
{(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD, |
| 62 |
|
(CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD, |
| 63 |
|
(CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD}, |
| 69 |
|
(CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD} |
| 70 |
|
}; |
| 71 |
|
|
| 72 |
< |
COLORMAT rgb2xyzmat = { /* RGB to XYZ */ |
| 72 |
> |
COLORMAT rgb2xyzmat = { /* RGB to XYZ (no white balance) */ |
| 73 |
|
{CIE_x_r*CIE_C_rD/CIE_D,CIE_x_g*CIE_C_gD/CIE_D,CIE_x_b*CIE_C_bD/CIE_D}, |
| 74 |
|
{CIE_y_r*CIE_C_rD/CIE_D,CIE_y_g*CIE_C_gD/CIE_D,CIE_y_b*CIE_C_bD/CIE_D}, |
| 75 |
|
{(1.-CIE_x_r-CIE_y_r)*CIE_C_rD/CIE_D, |
| 77 |
|
(1.-CIE_x_b-CIE_y_b)*CIE_C_bD/CIE_D} |
| 78 |
|
}; |
| 79 |
|
|
| 80 |
+ |
COLORMAT vkmat = { /* Sharp primary matrix */ |
| 81 |
+ |
{ 1.2694, -0.0988, -0.1706}, |
| 82 |
+ |
{-0.8364, 1.8006, 0.0357}, |
| 83 |
+ |
{ 0.0297, -0.0315, 1.0018} |
| 84 |
+ |
}; |
| 85 |
|
|
| 86 |
+ |
COLORMAT ivkmat = { /* inverse Sharp primary matrix */ |
| 87 |
+ |
{ 0.8156, 0.0472, 0.1372}, |
| 88 |
+ |
{ 0.3791, 0.5769, 0.0440}, |
| 89 |
+ |
{-0.0123, 0.0167, 0.9955} |
| 90 |
+ |
}; |
| 91 |
|
|
| 92 |
+ |
|
| 93 |
+ |
void |
| 94 |
|
spec_rgb(col, s, e) /* compute RGB color from spectral range */ |
| 95 |
|
COLOR col; |
| 96 |
|
int s, e; |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
+ |
void |
| 106 |
|
spec_cie(col, s, e) /* compute a color from a spectral range */ |
| 107 |
|
COLOR col; /* returned color */ |
| 108 |
|
int s, e; /* starting and ending wavelengths */ |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
+ |
void |
| 141 |
|
cie_rgb(rgb, xyz) /* convert CIE color to standard RGB */ |
| 142 |
< |
COLOR rgb, xyz; |
| 142 |
> |
COLOR rgb; |
| 143 |
> |
COLOR xyz; |
| 144 |
|
{ |
| 145 |
|
colortrans(rgb, xyz2rgbmat, xyz); |
| 146 |
|
clipgamut(rgb, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite); |
| 178 |
|
vv = 1.; /* check each limit */ |
| 179 |
|
for (i = 0; i < 3; i++) |
| 180 |
|
if (gamut & CGAMUT_LOWER && col[i] < lower[i]) { |
| 181 |
< |
v = (lower[i] - cgry[i])/(col[i] - cgry[i]); |
| 181 |
> |
v = (lower[i]+CEPS - cgry[i])/(col[i] - cgry[i]); |
| 182 |
|
if (v < vv) vv = v; |
| 183 |
|
rflags |= CGAMUT_LOWER; |
| 184 |
|
} else if (gamut & CGAMUT_UPPER && col[i] > upper[i]) { |
| 185 |
< |
v = (upper[i] - cgry[i])/(col[i] - cgry[i]); |
| 185 |
> |
v = (upper[i]-CEPS - cgry[i])/(col[i] - cgry[i]); |
| 186 |
|
if (v < vv) vv = v; |
| 187 |
|
rflags |= CGAMUT_UPPER; |
| 188 |
|
} |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
+ |
void |
| 197 |
|
colortrans(c2, mat, c1) /* convert c1 by mat and put into c2 */ |
| 198 |
+ |
register COLOR c2; |
| 199 |
|
register COLORMAT mat; |
| 200 |
< |
register COLOR c1, c2; |
| 200 |
> |
register COLOR c1; |
| 201 |
|
{ |
| 202 |
|
COLOR cout; |
| 203 |
|
|
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
+ |
void |
| 213 |
|
multcolormat(m3, m2, m1) /* multiply m1 by m2 and put into m3 */ |
| 214 |
< |
COLORMAT m1, m2, m3; /* m3 can be either m1 or m2 w/o harm */ |
| 214 |
> |
COLORMAT m3; /* m3 can be either m1 or m2 w/o harm */ |
| 215 |
> |
COLORMAT m2, m1; |
| 216 |
|
{ |
| 217 |
|
COLORMAT mt; |
| 218 |
|
register int i, j; |
| 226 |
|
} |
| 227 |
|
|
| 228 |
|
|
| 229 |
+ |
void |
| 230 |
|
compxyz2rgbmat(mat, pr) /* compute conversion from CIE to RGB space */ |
| 231 |
|
COLORMAT mat; |
| 232 |
|
register RGBPRIMS pr; |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
|
| 280 |
+ |
void |
| 281 |
|
comprgb2xyzmat(mat, pr) /* compute conversion from RGB to CIE space */ |
| 282 |
|
COLORMAT mat; |
| 283 |
|
register RGBPRIMS pr; |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
|
| 318 |
+ |
void |
| 319 |
|
comprgb2rgbmat(mat, pr1, pr2) /* compute conversion from RGB1 to RGB2 */ |
| 320 |
|
COLORMAT mat; |
| 321 |
|
RGBPRIMS pr1, pr2; |
| 332 |
|
compxyz2rgbmat(xyztopr2, pr2); |
| 333 |
|
/* combine transforms */ |
| 334 |
|
multcolormat(mat, pr1toxyz, xyztopr2); |
| 335 |
+ |
} |
| 336 |
+ |
|
| 337 |
+ |
|
| 338 |
+ |
void |
| 339 |
+ |
compxyzWBmat(mat, wht1, wht2) /* CIE von Kries transform from wht1 to wht2 */ |
| 340 |
+ |
COLORMAT mat; |
| 341 |
+ |
float wht1[2], wht2[2]; |
| 342 |
+ |
{ |
| 343 |
+ |
COLOR cw1, cw2; |
| 344 |
+ |
if (XYEQ(wht1,wht2)) { |
| 345 |
+ |
mat[0][0] = mat[1][1] = mat[2][2] = 1.0; |
| 346 |
+ |
mat[0][1] = mat[0][2] = mat[1][0] = |
| 347 |
+ |
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
| 348 |
+ |
return; |
| 349 |
+ |
} |
| 350 |
+ |
cw1[RED] = wht1[CIEX]/wht1[CIEY]; |
| 351 |
+ |
cw1[GRN] = 1.; |
| 352 |
+ |
cw1[BLU] = (1. - wht1[CIEX] - wht1[CIEY])/wht1[CIEY]; |
| 353 |
+ |
colortrans(cw1, vkmat, cw1); |
| 354 |
+ |
cw2[RED] = wht2[CIEX]/wht2[CIEY]; |
| 355 |
+ |
cw2[GRN] = 1.; |
| 356 |
+ |
cw2[BLU] = (1. - wht2[CIEX] - wht2[CIEY])/wht2[CIEY]; |
| 357 |
+ |
colortrans(cw2, vkmat, cw2); |
| 358 |
+ |
mat[0][0] = cw2[RED]/cw1[RED]; |
| 359 |
+ |
mat[1][1] = cw2[GRN]/cw1[GRN]; |
| 360 |
+ |
mat[2][2] = cw2[BLU]/cw1[BLU]; |
| 361 |
+ |
mat[0][1] = mat[0][2] = mat[1][0] = |
| 362 |
+ |
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
| 363 |
+ |
multcolormat(mat, vkmat, mat); |
| 364 |
+ |
multcolormat(mat, mat, ivkmat); |
| 365 |
+ |
} |
| 366 |
+ |
|
| 367 |
+ |
|
| 368 |
+ |
void |
| 369 |
+ |
compxyz2rgbWBmat(mat, pr) /* von Kries conversion from CIE to RGB space */ |
| 370 |
+ |
COLORMAT mat; |
| 371 |
+ |
RGBPRIMS pr; |
| 372 |
+ |
{ |
| 373 |
+ |
COLORMAT wbmat; |
| 374 |
+ |
|
| 375 |
+ |
compxyz2rgbmat(mat, pr); |
| 376 |
+ |
if (XYEQ(pr[WHT],xyneu)) |
| 377 |
+ |
return; |
| 378 |
+ |
compxyzWBmat(wbmat, xyneu, pr[WHT]); |
| 379 |
+ |
multcolormat(mat, wbmat, mat); |
| 380 |
+ |
} |
| 381 |
+ |
|
| 382 |
+ |
void |
| 383 |
+ |
comprgb2xyzWBmat(mat, pr) /* von Kries conversion from RGB to CIE space */ |
| 384 |
+ |
COLORMAT mat; |
| 385 |
+ |
RGBPRIMS pr; |
| 386 |
+ |
{ |
| 387 |
+ |
COLORMAT wbmat; |
| 388 |
+ |
|
| 389 |
+ |
comprgb2xyzmat(mat, pr); |
| 390 |
+ |
if (XYEQ(pr[WHT],xyneu)) |
| 391 |
+ |
return; |
| 392 |
+ |
compxyzWBmat(wbmat, pr[WHT], xyneu); |
| 393 |
+ |
multcolormat(mat, mat, wbmat); |
| 394 |
+ |
} |
| 395 |
+ |
|
| 396 |
+ |
void |
| 397 |
+ |
comprgb2rgbWBmat(mat, pr1, pr2) /* von Kries conversion from RGB1 to RGB2 */ |
| 398 |
+ |
COLORMAT mat; |
| 399 |
+ |
RGBPRIMS pr1, pr2; |
| 400 |
+ |
{ |
| 401 |
+ |
COLORMAT pr1toxyz, xyztopr2, wbmat; |
| 402 |
+ |
|
| 403 |
+ |
if (pr1 == pr2) { |
| 404 |
+ |
mat[0][0] = mat[1][1] = mat[2][2] = 1.0; |
| 405 |
+ |
mat[0][1] = mat[0][2] = mat[1][0] = |
| 406 |
+ |
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
| 407 |
+ |
return; |
| 408 |
+ |
} |
| 409 |
+ |
comprgb2xyzmat(pr1toxyz, pr1); |
| 410 |
+ |
compxyzWBmat(wbmat, pr1[WHT], pr2[WHT]); |
| 411 |
+ |
compxyz2rgbmat(xyztopr2, pr2); |
| 412 |
+ |
/* combine transforms */ |
| 413 |
+ |
multcolormat(mat, pr1toxyz, wbmat); |
| 414 |
+ |
multcolormat(mat, mat, xyztopr2); |
| 415 |
|
} |