1 |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Convert MGF color to RGB representation defined below. |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
|
13 |
#include "parser.h" |
14 |
|
15 |
/* Change the following to suit your standard */ |
16 |
#define CIE_x_r 0.640 /* nominal CRT primaries */ |
17 |
#define CIE_y_r 0.330 |
18 |
#define CIE_x_g 0.290 |
19 |
#define CIE_y_g 0.600 |
20 |
#define CIE_x_b 0.150 |
21 |
#define CIE_y_b 0.060 |
22 |
#define CIE_x_w 0.3333 /* use true white */ |
23 |
#define CIE_y_w 0.3333 |
24 |
|
25 |
#define CIE_C_rD ( (1./CIE_y_w) * \ |
26 |
( CIE_x_w*(CIE_y_g - CIE_y_b) - \ |
27 |
CIE_y_w*(CIE_x_g - CIE_x_b) + \ |
28 |
CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g ) ) |
29 |
#define CIE_C_gD ( (1./CIE_y_w) * \ |
30 |
( CIE_x_w*(CIE_y_b - CIE_y_r) - \ |
31 |
CIE_y_w*(CIE_x_b - CIE_x_r) - \ |
32 |
CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r ) ) |
33 |
#define CIE_C_bD ( (1./CIE_y_w) * \ |
34 |
( CIE_x_w*(CIE_y_r - CIE_y_g) - \ |
35 |
CIE_y_w*(CIE_x_r - CIE_x_g) + \ |
36 |
CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r ) ) |
37 |
|
38 |
|
39 |
float xyz2rgbmat[3][3] = { /* XYZ to RGB conversion matrix */ |
40 |
{(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD, |
41 |
(CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD, |
42 |
(CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD}, |
43 |
{(CIE_y_b - CIE_y_r - CIE_y_b*CIE_x_r + CIE_y_r*CIE_x_b)/CIE_C_gD, |
44 |
(CIE_x_r - CIE_x_b - CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r)/CIE_C_gD, |
45 |
(CIE_x_b*CIE_y_r - CIE_x_r*CIE_y_b)/CIE_C_gD}, |
46 |
{(CIE_y_r - CIE_y_g - CIE_y_r*CIE_x_g + CIE_y_g*CIE_x_r)/CIE_C_bD, |
47 |
(CIE_x_g - CIE_x_r - CIE_x_g*CIE_y_r + CIE_x_r*CIE_y_g)/CIE_C_bD, |
48 |
(CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD} |
49 |
}; |
50 |
|
51 |
|
52 |
mgf2rgb(cin, intensity, cout) /* convert MGF color to RGB */ |
53 |
register C_COLOR *cin; /* input MGF chrominance */ |
54 |
double intensity; /* input luminance or reflectance */ |
55 |
register float cout[3]; /* output RGB color */ |
56 |
{ |
57 |
static double cie[3]; |
58 |
/* get CIE XYZ representation */ |
59 |
c_ccvt(cin, C_CSXY); |
60 |
cie[0] = intensity*cin->cx/cin->cy; |
61 |
cie[1] = intensity; |
62 |
cie[2] = intensity*(1./cin->cy - 1.) - cie[0]; |
63 |
/* convert to RGB */ |
64 |
cout[0] = xyz2rgbmat[0][0]*cie[0] + xyz2rgbmat[0][1]*cie[1] |
65 |
+ xyz2rgbmat[0][2]*cie[2]; |
66 |
if(cout[0] < 0.) cout[0] = 0.; |
67 |
cout[1] = xyz2rgbmat[1][0]*cie[0] + xyz2rgbmat[1][1]*cie[1] |
68 |
+ xyz2rgbmat[1][2]*cie[2]; |
69 |
if(cout[1] < 0.) cout[1] = 0.; |
70 |
cout[2] = xyz2rgbmat[2][0]*cie[0] + xyz2rgbmat[2][1]*cie[1] |
71 |
+ xyz2rgbmat[2][2]*cie[2]; |
72 |
if(cout[2] < 0.) cout[2] = 0.; |
73 |
} |