| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
greg |
1.4 |
static const char RCSid[] = "$Id: cvrgb.c,v 1.3 2003/11/15 17:54:06 schorsch Exp $";
|
| 3 |
greg |
1.1 |
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* Convert MGF color to RGB representation defined below.
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
|
|
#include <stdio.h>
|
| 9 |
greg |
1.2 |
#include <stdlib.h>
|
| 10 |
greg |
1.1 |
|
| 11 |
|
|
#include "parser.h"
|
| 12 |
|
|
|
| 13 |
|
|
/* Change the following to suit your standard */
|
| 14 |
|
|
#define CIE_x_r 0.640 /* nominal CRT primaries */
|
| 15 |
|
|
#define CIE_y_r 0.330
|
| 16 |
|
|
#define CIE_x_g 0.290
|
| 17 |
|
|
#define CIE_y_g 0.600
|
| 18 |
|
|
#define CIE_x_b 0.150
|
| 19 |
|
|
#define CIE_y_b 0.060
|
| 20 |
|
|
#define CIE_x_w 0.3333 /* use true white */
|
| 21 |
|
|
#define CIE_y_w 0.3333
|
| 22 |
|
|
|
| 23 |
|
|
#define CIE_C_rD ( (1./CIE_y_w) * \
|
| 24 |
|
|
( CIE_x_w*(CIE_y_g - CIE_y_b) - \
|
| 25 |
|
|
CIE_y_w*(CIE_x_g - CIE_x_b) + \
|
| 26 |
|
|
CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g ) )
|
| 27 |
|
|
#define CIE_C_gD ( (1./CIE_y_w) * \
|
| 28 |
|
|
( CIE_x_w*(CIE_y_b - CIE_y_r) - \
|
| 29 |
|
|
CIE_y_w*(CIE_x_b - CIE_x_r) - \
|
| 30 |
|
|
CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r ) )
|
| 31 |
|
|
#define CIE_C_bD ( (1./CIE_y_w) * \
|
| 32 |
|
|
( CIE_x_w*(CIE_y_r - CIE_y_g) - \
|
| 33 |
|
|
CIE_y_w*(CIE_x_r - CIE_x_g) + \
|
| 34 |
|
|
CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r ) )
|
| 35 |
|
|
|
| 36 |
|
|
|
| 37 |
|
|
float xyz2rgbmat[3][3] = { /* XYZ to RGB conversion matrix */
|
| 38 |
|
|
{(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD,
|
| 39 |
|
|
(CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD,
|
| 40 |
|
|
(CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD},
|
| 41 |
|
|
{(CIE_y_b - CIE_y_r - CIE_y_b*CIE_x_r + CIE_y_r*CIE_x_b)/CIE_C_gD,
|
| 42 |
|
|
(CIE_x_r - CIE_x_b - CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r)/CIE_C_gD,
|
| 43 |
|
|
(CIE_x_b*CIE_y_r - CIE_x_r*CIE_y_b)/CIE_C_gD},
|
| 44 |
|
|
{(CIE_y_r - CIE_y_g - CIE_y_r*CIE_x_g + CIE_y_g*CIE_x_r)/CIE_C_bD,
|
| 45 |
|
|
(CIE_x_g - CIE_x_r - CIE_x_g*CIE_y_r + CIE_x_r*CIE_y_g)/CIE_C_bD,
|
| 46 |
|
|
(CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD}
|
| 47 |
|
|
};
|
| 48 |
|
|
|
| 49 |
|
|
|
| 50 |
schorsch |
1.3 |
void
|
| 51 |
|
|
mgf2rgb( /* convert MGF color to RGB */
|
| 52 |
|
|
register C_COLOR *cin, /* input MGF chrominance */
|
| 53 |
|
|
double intensity, /* input luminance or reflectance */
|
| 54 |
|
|
register float cout[3] /* output RGB color */
|
| 55 |
|
|
)
|
| 56 |
greg |
1.1 |
{
|
| 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 |
|
|
}
|