1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ccyrgb.c,v 3.4 2012/05/17 17:11:33 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Convert MGF color to Radiance RGB and spectral representations |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include "color.h" |
10 |
#include "ccolor.h" |
11 |
|
12 |
void |
13 |
ccy2rgb( /* convert MGF color to RGB */ |
14 |
C_COLOR *cin, /* input MGF chrominance */ |
15 |
double cieY, /* input luminance or reflectance */ |
16 |
COLOR cout /* output RGB color */ |
17 |
) |
18 |
{ |
19 |
double d; |
20 |
COLOR xyz; |
21 |
/* get CIE XYZ representation */ |
22 |
c_ccvt(cin, C_CSXY); |
23 |
d = cin->cx/cin->cy; |
24 |
xyz[CIEX] = d * cieY; |
25 |
xyz[CIEY] = cieY; |
26 |
xyz[CIEZ] = (1./cin->cy - d - 1.) * cieY; |
27 |
cie_rgb(cout, xyz); |
28 |
} |
29 |
|
30 |
/* Convert RGB to MGF color and value */ |
31 |
double |
32 |
rgb2ccy(COLOR cin, C_COLOR *cout) |
33 |
{ |
34 |
COLOR xyz; |
35 |
double df; |
36 |
|
37 |
rgb_cie(xyz, cin); |
38 |
*cout = c_dfcolor; |
39 |
df = xyz[CIEX] + xyz[CIEY] + xyz[CIEZ]; |
40 |
if (df <= .0) |
41 |
return(.0); |
42 |
df = 1./df; |
43 |
cout->cx = xyz[CIEX]*df; |
44 |
cout->cy = xyz[CIEZ]*df; |
45 |
cout->flags = (C_CSXY|C_CDXY); |
46 |
|
47 |
return(xyz[CIEY]); |
48 |
} |
49 |
|
50 |
void |
51 |
ccy2scolor( /* convert MGF color, Y to spectral color */ |
52 |
C_COLOR *cin, /* input MGF color or spectrum */ |
53 |
double cieY, /* input luminance or reflectance */ |
54 |
SCOLOR sco /* output spectral color */ |
55 |
) |
56 |
{ |
57 |
COLORV nf, sorig[C_CNSS]; |
58 |
int i; |
59 |
|
60 |
if (cin->flags & C_CDXY || NCSAMP <= 3) { |
61 |
COLOR col; /* tristimulus in or out, so... */ |
62 |
ccy2rgb(cin, cieY, col); |
63 |
setscolor(sco, colval(col,RED), colval(col,GRN), colval(col,BLU)); |
64 |
return; |
65 |
} |
66 |
c_ccvt(cin, C_CSXY); |
67 |
nf = cieY*C_CNSS/(cin->ssum*3*cin->cy); |
68 |
for (i = C_CNSS; i--; ) /* may as well reverse order */ |
69 |
sorig[C_CNSS-1-i] = nf*(COLORV)cin->ssamp[i]; |
70 |
|
71 |
convertscolor(sco, NCSAMP, WLPART[0], WLPART[3], |
72 |
sorig, C_CNSS, C_CMAXWL+.5*C_CWLI, C_CMINWL-.5*C_CWLI); |
73 |
} |
74 |
|
75 |
/* Convert spectral color to MGF color and Y value */ |
76 |
double |
77 |
scolor2ccy(SCOLOR sci, C_COLOR *cout) |
78 |
{ |
79 |
float hinc, cspec[MAXCSAMP]; |
80 |
int i = NCSAMP; |
81 |
|
82 |
if (i <= 3) /* defined as RGB, so... */ |
83 |
return(rgb2ccy(sci, cout)); |
84 |
|
85 |
while (i-- > 0) /* need to reverse array */ |
86 |
cspec[i] = sci[NCSAMP-1-i]; |
87 |
|
88 |
hinc = (WLPART[0]-WLPART[3])/(float)(2*NCSAMP); |
89 |
|
90 |
return(c_sset(cout, WLPART[3]+hinc, WLPART[0]-hinc, cspec, NCSAMP)); |
91 |
} |