10 |
|
|
11 |
|
#include "copyright.h" |
12 |
|
|
13 |
< |
#include "color.h" |
13 |
> |
#include <stdio.h> |
14 |
|
#include <string.h> |
15 |
+ |
#include "color.h" |
16 |
|
|
17 |
|
#define CEPS 1e-4 /* color epsilon */ |
18 |
|
|
91 |
|
|
92 |
|
|
93 |
|
void |
94 |
< |
spec_rgb(col, s, e) /* compute RGB color from spectral range */ |
95 |
< |
COLOR col; |
96 |
< |
int s, e; |
94 |
> |
spec_rgb( /* compute RGB color from spectral range */ |
95 |
> |
COLOR col, |
96 |
> |
int s, |
97 |
> |
int e |
98 |
> |
) |
99 |
|
{ |
100 |
|
COLOR ciecolor; |
101 |
|
|
105 |
|
|
106 |
|
|
107 |
|
void |
108 |
< |
spec_cie(col, s, e) /* compute a color from a spectral range */ |
109 |
< |
COLOR col; /* returned color */ |
110 |
< |
int s, e; /* starting and ending wavelengths */ |
108 |
> |
spec_cie( /* compute a color from a spectral range */ |
109 |
> |
COLOR col, /* returned color */ |
110 |
> |
int s, /* starting and ending wavelengths */ |
111 |
> |
int e |
112 |
> |
) |
113 |
|
{ |
114 |
|
register int i, d, r; |
115 |
|
|
142 |
|
|
143 |
|
|
144 |
|
void |
145 |
< |
cie_rgb(rgb, xyz) /* convert CIE color to standard RGB */ |
146 |
< |
COLOR rgb; |
147 |
< |
COLOR xyz; |
145 |
> |
cie_rgb( /* convert CIE color to standard RGB */ |
146 |
> |
COLOR rgb, |
147 |
> |
COLOR xyz |
148 |
> |
) |
149 |
|
{ |
150 |
|
colortrans(rgb, xyz2rgbmat, xyz); |
151 |
|
clipgamut(rgb, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite); |
153 |
|
|
154 |
|
|
155 |
|
int |
156 |
< |
clipgamut(col, brt, gamut, lower, upper) /* clip to gamut cube */ |
157 |
< |
COLOR col; |
158 |
< |
double brt; |
159 |
< |
int gamut; |
160 |
< |
COLOR lower, upper; |
156 |
> |
clipgamut( /* clip to gamut cube */ |
157 |
> |
COLOR col, |
158 |
> |
double brt, |
159 |
> |
int gamut, |
160 |
> |
COLOR lower, |
161 |
> |
COLOR upper |
162 |
> |
) |
163 |
|
{ |
164 |
|
int rflags = 0; |
165 |
|
double brtmin, brtmax, v, vv; |
185 |
|
vv = 1.; /* check each limit */ |
186 |
|
for (i = 0; i < 3; i++) |
187 |
|
if (gamut & CGAMUT_LOWER && col[i] < lower[i]) { |
188 |
< |
v = (lower[i]+CEPS - cgry[i])/(col[i] - cgry[i]); |
188 |
> |
v = (lower[i] - cgry[i])/(col[i] - cgry[i]); |
189 |
|
if (v < vv) vv = v; |
190 |
|
rflags |= CGAMUT_LOWER; |
191 |
|
} else if (gamut & CGAMUT_UPPER && col[i] > upper[i]) { |
192 |
< |
v = (upper[i]-CEPS - cgry[i])/(col[i] - cgry[i]); |
192 |
> |
v = (upper[i] - cgry[i])/(col[i] - cgry[i]); |
193 |
|
if (v < vv) vv = v; |
194 |
|
rflags |= CGAMUT_UPPER; |
195 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
void |
204 |
< |
colortrans(c2, mat, c1) /* convert c1 by mat and put into c2 */ |
205 |
< |
register COLOR c2; |
206 |
< |
register COLORMAT mat; |
207 |
< |
register COLOR c1; |
204 |
> |
colortrans( /* convert c1 by mat and put into c2 */ |
205 |
> |
register COLOR c2, |
206 |
> |
register COLORMAT mat, |
207 |
> |
register COLOR c1 |
208 |
> |
) |
209 |
|
{ |
210 |
|
COLOR cout; |
211 |
|
|
218 |
|
|
219 |
|
|
220 |
|
void |
221 |
< |
multcolormat(m3, m2, m1) /* multiply m1 by m2 and put into m3 */ |
222 |
< |
COLORMAT m3; /* m3 can be either m1 or m2 w/o harm */ |
223 |
< |
COLORMAT m2, m1; |
221 |
> |
multcolormat( /* multiply m1 by m2 and put into m3 */ |
222 |
> |
COLORMAT m3, /* m3 can be either m1 or m2 w/o harm */ |
223 |
> |
COLORMAT m2, |
224 |
> |
COLORMAT m1 |
225 |
> |
) |
226 |
|
{ |
227 |
|
COLORMAT mt; |
228 |
|
register int i, j; |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
< |
void |
240 |
< |
compxyz2rgbmat(mat, pr) /* compute conversion from CIE to RGB space */ |
241 |
< |
COLORMAT mat; |
242 |
< |
register RGBPRIMS pr; |
239 |
> |
int |
240 |
> |
colorprimsOK( /* are color primaries reasonable? */ |
241 |
> |
RGBPRIMS pr |
242 |
> |
) |
243 |
|
{ |
244 |
+ |
int i, j; |
245 |
+ |
|
246 |
+ |
for (i = 0; i < 4; i++) { |
247 |
+ |
if ((pr[i][CIEX] <= -.5) | (pr[i][CIEY] <= -.5)) |
248 |
+ |
return(0); |
249 |
+ |
if ((pr[i][CIEX] >= 1.5) | (pr[i][CIEY] >= 1.5)) |
250 |
+ |
return(0); |
251 |
+ |
if (pr[i][CIEX] + pr[i][CIEY] >= 1.5) |
252 |
+ |
return(0); |
253 |
+ |
} |
254 |
+ |
for (i = 0; i < 4; i++) |
255 |
+ |
for (j = i+1; j < 4; j++) |
256 |
+ |
if (CEQ(pr[i][CIEX],pr[j][CIEX]) && |
257 |
+ |
CEQ(pr[i][CIEY],pr[j][CIEY])) |
258 |
+ |
return(0); |
259 |
+ |
return(1); |
260 |
+ |
} |
261 |
+ |
|
262 |
+ |
|
263 |
+ |
|
264 |
+ |
int |
265 |
+ |
compxyz2rgbmat( /* compute conversion from CIE to RGB space */ |
266 |
+ |
COLORMAT mat, |
267 |
+ |
register RGBPRIMS pr |
268 |
+ |
) |
269 |
+ |
{ |
270 |
|
double C_rD, C_gD, C_bD; |
271 |
|
|
272 |
|
if (pr == stdprims) { /* can use xyz2rgbmat */ |
273 |
|
cpcolormat(mat, xyz2rgbmat); |
274 |
< |
return; |
274 |
> |
return(1); |
275 |
|
} |
276 |
+ |
if (CEQ(pr[WHT][CIEX],0.) | CEQ(pr[WHT][CIEY],0.)) |
277 |
+ |
return(0); |
278 |
|
C_rD = (1./pr[WHT][CIEY]) * |
279 |
|
( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) - |
280 |
|
pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) + |
281 |
|
pr[GRN][CIEX]*pr[BLU][CIEY] - pr[BLU][CIEX]*pr[GRN][CIEY] ) ; |
282 |
+ |
if (CEQ(C_rD,0.)) |
283 |
+ |
return(0); |
284 |
|
C_gD = (1./pr[WHT][CIEY]) * |
285 |
|
( pr[WHT][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) - |
286 |
|
pr[WHT][CIEY]*(pr[BLU][CIEX] - pr[RED][CIEX]) - |
287 |
|
pr[RED][CIEX]*pr[BLU][CIEY] + pr[BLU][CIEX]*pr[RED][CIEY] ) ; |
288 |
+ |
if (CEQ(C_gD,0.)) |
289 |
+ |
return(0); |
290 |
|
C_bD = (1./pr[WHT][CIEY]) * |
291 |
|
( pr[WHT][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) - |
292 |
|
pr[WHT][CIEY]*(pr[RED][CIEX] - pr[GRN][CIEX]) + |
293 |
|
pr[RED][CIEX]*pr[GRN][CIEY] - pr[GRN][CIEX]*pr[RED][CIEY] ) ; |
294 |
< |
|
294 |
> |
if (CEQ(C_bD,0.)) |
295 |
> |
return(0); |
296 |
|
mat[0][0] = (pr[GRN][CIEY] - pr[BLU][CIEY] - |
297 |
|
pr[BLU][CIEX]*pr[GRN][CIEY] + |
298 |
|
pr[BLU][CIEY]*pr[GRN][CIEX])/C_rD ; |
317 |
|
pr[RED][CIEX]*pr[GRN][CIEY])/C_bD ; |
318 |
|
mat[2][2] = (pr[RED][CIEX]*pr[GRN][CIEY] - |
319 |
|
pr[GRN][CIEX]*pr[RED][CIEY])/C_bD ; |
320 |
+ |
return(1); |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
< |
void |
325 |
< |
comprgb2xyzmat(mat, pr) /* compute conversion from RGB to CIE space */ |
326 |
< |
COLORMAT mat; |
327 |
< |
register RGBPRIMS pr; |
324 |
> |
int |
325 |
> |
comprgb2xyzmat( /* compute conversion from RGB to CIE space */ |
326 |
> |
COLORMAT mat, |
327 |
> |
register RGBPRIMS pr |
328 |
> |
) |
329 |
|
{ |
330 |
|
double C_rD, C_gD, C_bD, D; |
331 |
|
|
332 |
|
if (pr == stdprims) { /* can use rgb2xyzmat */ |
333 |
|
cpcolormat(mat, rgb2xyzmat); |
334 |
< |
return; |
334 |
> |
return(1); |
335 |
|
} |
336 |
+ |
if (CEQ(pr[WHT][CIEX],0.) | CEQ(pr[WHT][CIEY],0.)) |
337 |
+ |
return(0); |
338 |
|
C_rD = (1./pr[WHT][CIEY]) * |
339 |
|
( pr[WHT][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) - |
340 |
|
pr[WHT][CIEY]*(pr[GRN][CIEX] - pr[BLU][CIEX]) + |
350 |
|
D = pr[RED][CIEX]*(pr[GRN][CIEY] - pr[BLU][CIEY]) + |
351 |
|
pr[GRN][CIEX]*(pr[BLU][CIEY] - pr[RED][CIEY]) + |
352 |
|
pr[BLU][CIEX]*(pr[RED][CIEY] - pr[GRN][CIEY]) ; |
353 |
+ |
if (CEQ(D,0.)) |
354 |
+ |
return(0); |
355 |
|
mat[0][0] = pr[RED][CIEX]*C_rD/D; |
356 |
|
mat[0][1] = pr[GRN][CIEX]*C_gD/D; |
357 |
|
mat[0][2] = pr[BLU][CIEX]*C_bD/D; |
361 |
|
mat[2][0] = (1.-pr[RED][CIEX]-pr[RED][CIEY])*C_rD/D; |
362 |
|
mat[2][1] = (1.-pr[GRN][CIEX]-pr[GRN][CIEY])*C_gD/D; |
363 |
|
mat[2][2] = (1.-pr[BLU][CIEX]-pr[BLU][CIEY])*C_bD/D; |
364 |
+ |
return(1); |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
< |
void |
369 |
< |
comprgb2rgbmat(mat, pr1, pr2) /* compute conversion from RGB1 to RGB2 */ |
370 |
< |
COLORMAT mat; |
371 |
< |
RGBPRIMS pr1, pr2; |
368 |
> |
int |
369 |
> |
comprgb2rgbmat( /* compute conversion from RGB1 to RGB2 */ |
370 |
> |
COLORMAT mat, |
371 |
> |
RGBPRIMS pr1, |
372 |
> |
RGBPRIMS pr2 |
373 |
> |
) |
374 |
|
{ |
375 |
|
COLORMAT pr1toxyz, xyztopr2; |
376 |
|
|
378 |
|
mat[0][0] = mat[1][1] = mat[2][2] = 1.0; |
379 |
|
mat[0][1] = mat[0][2] = mat[1][0] = |
380 |
|
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
381 |
< |
return; |
381 |
> |
return(1); |
382 |
|
} |
383 |
< |
comprgb2xyzmat(pr1toxyz, pr1); |
384 |
< |
compxyz2rgbmat(xyztopr2, pr2); |
383 |
> |
if (!comprgb2xyzmat(pr1toxyz, pr1)) |
384 |
> |
return(0); |
385 |
> |
if (!compxyz2rgbmat(xyztopr2, pr2)) |
386 |
> |
return(0); |
387 |
|
/* combine transforms */ |
388 |
|
multcolormat(mat, pr1toxyz, xyztopr2); |
389 |
+ |
return(1); |
390 |
|
} |
391 |
|
|
392 |
|
|
393 |
< |
void |
394 |
< |
compxyzWBmat(mat, wht1, wht2) /* CIE von Kries transform from wht1 to wht2 */ |
395 |
< |
COLORMAT mat; |
396 |
< |
float wht1[2], wht2[2]; |
393 |
> |
int |
394 |
> |
compxyzWBmat( /* CIE von Kries transform from wht1 to wht2 */ |
395 |
> |
COLORMAT mat, |
396 |
> |
float wht1[2], |
397 |
> |
float wht2[2] |
398 |
> |
) |
399 |
|
{ |
400 |
|
COLOR cw1, cw2; |
401 |
|
if (XYEQ(wht1,wht2)) { |
402 |
|
mat[0][0] = mat[1][1] = mat[2][2] = 1.0; |
403 |
|
mat[0][1] = mat[0][2] = mat[1][0] = |
404 |
|
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
405 |
< |
return; |
405 |
> |
return(1); |
406 |
|
} |
407 |
+ |
if (CEQ(wht1[CIEX],0.) | CEQ(wht1[CIEY],0.)) |
408 |
+ |
return(0); |
409 |
|
cw1[RED] = wht1[CIEX]/wht1[CIEY]; |
410 |
|
cw1[GRN] = 1.; |
411 |
|
cw1[BLU] = (1. - wht1[CIEX] - wht1[CIEY])/wht1[CIEY]; |
412 |
|
colortrans(cw1, vkmat, cw1); |
413 |
+ |
if (CEQ(wht2[CIEX],0.) | CEQ(wht2[CIEY],0.)) |
414 |
+ |
return(0); |
415 |
|
cw2[RED] = wht2[CIEX]/wht2[CIEY]; |
416 |
|
cw2[GRN] = 1.; |
417 |
|
cw2[BLU] = (1. - wht2[CIEX] - wht2[CIEY])/wht2[CIEY]; |
418 |
|
colortrans(cw2, vkmat, cw2); |
419 |
+ |
if (CEQ(cw1[RED],0.) | CEQ(cw1[GRN],0.) | CEQ(cw1[BLU],0.)) |
420 |
+ |
return(0); |
421 |
|
mat[0][0] = cw2[RED]/cw1[RED]; |
422 |
|
mat[1][1] = cw2[GRN]/cw1[GRN]; |
423 |
|
mat[2][2] = cw2[BLU]/cw1[BLU]; |
425 |
|
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
426 |
|
multcolormat(mat, vkmat, mat); |
427 |
|
multcolormat(mat, mat, ivkmat); |
428 |
+ |
return(1); |
429 |
|
} |
430 |
|
|
431 |
|
|
432 |
< |
void |
433 |
< |
compxyz2rgbWBmat(mat, pr) /* von Kries conversion from CIE to RGB space */ |
434 |
< |
COLORMAT mat; |
435 |
< |
RGBPRIMS pr; |
432 |
> |
int |
433 |
> |
compxyz2rgbWBmat( /* von Kries conversion from CIE to RGB space */ |
434 |
> |
COLORMAT mat, |
435 |
> |
RGBPRIMS pr |
436 |
> |
) |
437 |
|
{ |
438 |
|
COLORMAT wbmat; |
439 |
|
|
440 |
< |
compxyz2rgbmat(mat, pr); |
440 |
> |
if (!compxyz2rgbmat(mat, pr)) |
441 |
> |
return(0); |
442 |
|
if (XYEQ(pr[WHT],xyneu)) |
443 |
< |
return; |
444 |
< |
compxyzWBmat(wbmat, xyneu, pr[WHT]); |
443 |
> |
return(1); |
444 |
> |
if (!compxyzWBmat(wbmat, xyneu, pr[WHT])) |
445 |
> |
return(0); |
446 |
|
multcolormat(mat, wbmat, mat); |
447 |
+ |
return(1); |
448 |
|
} |
449 |
|
|
450 |
< |
void |
451 |
< |
comprgb2xyzWBmat(mat, pr) /* von Kries conversion from RGB to CIE space */ |
452 |
< |
COLORMAT mat; |
453 |
< |
RGBPRIMS pr; |
450 |
> |
int |
451 |
> |
comprgb2xyzWBmat( /* von Kries conversion from RGB to CIE space */ |
452 |
> |
COLORMAT mat, |
453 |
> |
RGBPRIMS pr |
454 |
> |
) |
455 |
|
{ |
456 |
|
COLORMAT wbmat; |
457 |
|
|
458 |
< |
comprgb2xyzmat(mat, pr); |
458 |
> |
if (!comprgb2xyzmat(mat, pr)) |
459 |
> |
return(0); |
460 |
|
if (XYEQ(pr[WHT],xyneu)) |
461 |
< |
return; |
462 |
< |
compxyzWBmat(wbmat, pr[WHT], xyneu); |
461 |
> |
return(1); |
462 |
> |
if (!compxyzWBmat(wbmat, pr[WHT], xyneu)) |
463 |
> |
return(0); |
464 |
|
multcolormat(mat, mat, wbmat); |
465 |
+ |
return(1); |
466 |
|
} |
467 |
|
|
468 |
< |
void |
469 |
< |
comprgb2rgbWBmat(mat, pr1, pr2) /* von Kries conversion from RGB1 to RGB2 */ |
470 |
< |
COLORMAT mat; |
471 |
< |
RGBPRIMS pr1, pr2; |
468 |
> |
int |
469 |
> |
comprgb2rgbWBmat( /* von Kries conversion from RGB1 to RGB2 */ |
470 |
> |
COLORMAT mat, |
471 |
> |
RGBPRIMS pr1, |
472 |
> |
RGBPRIMS pr2 |
473 |
> |
) |
474 |
|
{ |
475 |
|
COLORMAT pr1toxyz, xyztopr2, wbmat; |
476 |
|
|
478 |
|
mat[0][0] = mat[1][1] = mat[2][2] = 1.0; |
479 |
|
mat[0][1] = mat[0][2] = mat[1][0] = |
480 |
|
mat[1][2] = mat[2][0] = mat[2][1] = 0.0; |
481 |
< |
return; |
481 |
> |
return(1); |
482 |
|
} |
483 |
< |
comprgb2xyzmat(pr1toxyz, pr1); |
484 |
< |
compxyzWBmat(wbmat, pr1[WHT], pr2[WHT]); |
485 |
< |
compxyz2rgbmat(xyztopr2, pr2); |
483 |
> |
if (!comprgb2xyzmat(pr1toxyz, pr1)) |
484 |
> |
return(0); |
485 |
> |
if (!compxyzWBmat(wbmat, pr1[WHT], pr2[WHT])) |
486 |
> |
return(0); |
487 |
> |
if (!compxyz2rgbmat(xyztopr2, pr2)) |
488 |
> |
return(0); |
489 |
|
/* combine transforms */ |
490 |
|
multcolormat(mat, pr1toxyz, wbmat); |
491 |
|
multcolormat(mat, mat, xyztopr2); |
492 |
+ |
return(1); |
493 |
|
} |