1 |
/* RCSid $Id: color.h,v 2.33 2013/09/11 16:00:13 greg Exp $ */ |
2 |
/* |
3 |
* color.h - header for routines using pixel color values. |
4 |
* |
5 |
* Two color representations are used, one for calculation and |
6 |
* another for storage. Calculation is done with three floats |
7 |
* for speed. Stored color values use 4 bytes which contain |
8 |
* three single byte mantissas and a common exponent. |
9 |
*/ |
10 |
#ifndef _RAD_COLOR_H_ |
11 |
#define _RAD_COLOR_H_ |
12 |
|
13 |
#include <stdio.h> |
14 |
#include <stdlib.h> |
15 |
|
16 |
#ifdef __cplusplus |
17 |
extern "C" { |
18 |
#endif |
19 |
|
20 |
#define RED 0 |
21 |
#define GRN 1 |
22 |
#define BLU 2 |
23 |
#define CIEX 0 /* or, if input is XYZ... */ |
24 |
#define CIEY 1 |
25 |
#define CIEZ 2 |
26 |
#define EXP 3 /* exponent same for either format */ |
27 |
#define COLXS 128 /* excess used for exponent */ |
28 |
#define WHT 3 /* used for RGBPRIMS type */ |
29 |
|
30 |
#undef uby8 |
31 |
#define uby8 unsigned char /* 8-bit unsigned integer */ |
32 |
|
33 |
typedef uby8 COLR[4]; /* red, green, blue (or X,Y,Z), exponent */ |
34 |
|
35 |
typedef float COLORV; |
36 |
typedef COLORV COLOR[3]; /* red, green, blue (or X,Y,Z) */ |
37 |
|
38 |
typedef float RGBPRIMS[4][2]; /* (x,y) chromaticities for RGBW */ |
39 |
typedef float (*RGBPRIMP)[2]; /* pointer to RGBPRIMS array */ |
40 |
|
41 |
typedef float COLORMAT[3][3]; /* color coordinate conversion matrix */ |
42 |
|
43 |
#define copycolr(c1,c2) (c1[0]=c2[0],c1[1]=c2[1], \ |
44 |
c1[2]=c2[2],c1[3]=c2[3]) |
45 |
|
46 |
#define colval(col,pri) ((col)[pri]) |
47 |
|
48 |
#define setcolor(col,r,g,b) ((col)[RED]=(r),(col)[GRN]=(g),(col)[BLU]=(b)) |
49 |
|
50 |
#define copycolor(c1,c2) ((c1)[0]=(c2)[0],(c1)[1]=(c2)[1],(c1)[2]=(c2)[2]) |
51 |
|
52 |
#define scalecolor(col,sf) ((col)[0]*=(sf),(col)[1]*=(sf),(col)[2]*=(sf)) |
53 |
|
54 |
#define addcolor(c1,c2) ((c1)[0]+=(c2)[0],(c1)[1]+=(c2)[1],(c1)[2]+=(c2)[2]) |
55 |
|
56 |
#define multcolor(c1,c2) ((c1)[0]*=(c2)[0],(c1)[1]*=(c2)[1],(c1)[2]*=(c2)[2]) |
57 |
|
58 |
#ifdef NTSC |
59 |
#define CIE_x_r 0.670 /* standard NTSC primaries */ |
60 |
#define CIE_y_r 0.330 |
61 |
#define CIE_x_g 0.210 |
62 |
#define CIE_y_g 0.710 |
63 |
#define CIE_x_b 0.140 |
64 |
#define CIE_y_b 0.080 |
65 |
#define CIE_x_w (1./3.) /* use true white */ |
66 |
#define CIE_y_w (1./3.) |
67 |
#else |
68 |
#define CIE_x_r 0.640 /* nominal CRT primaries */ |
69 |
#define CIE_y_r 0.330 |
70 |
#define CIE_x_g 0.290 |
71 |
#define CIE_y_g 0.600 |
72 |
#define CIE_x_b 0.150 |
73 |
#define CIE_y_b 0.060 |
74 |
#define CIE_x_w (1./3.) /* use true white */ |
75 |
#define CIE_y_w (1./3.) |
76 |
#endif |
77 |
|
78 |
#define STDPRIMS {{CIE_x_r,CIE_y_r},{CIE_x_g,CIE_y_g}, \ |
79 |
{CIE_x_b,CIE_y_b},{CIE_x_w,CIE_y_w}} |
80 |
|
81 |
#define CIE_D ( CIE_x_r*(CIE_y_g - CIE_y_b) + \ |
82 |
CIE_x_g*(CIE_y_b - CIE_y_r) + \ |
83 |
CIE_x_b*(CIE_y_r - CIE_y_g) ) |
84 |
#define CIE_C_rD ( (1./CIE_y_w) * \ |
85 |
( CIE_x_w*(CIE_y_g - CIE_y_b) - \ |
86 |
CIE_y_w*(CIE_x_g - CIE_x_b) + \ |
87 |
CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g ) ) |
88 |
#define CIE_C_gD ( (1./CIE_y_w) * \ |
89 |
( CIE_x_w*(CIE_y_b - CIE_y_r) - \ |
90 |
CIE_y_w*(CIE_x_b - CIE_x_r) - \ |
91 |
CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r ) ) |
92 |
#define CIE_C_bD ( (1./CIE_y_w) * \ |
93 |
( CIE_x_w*(CIE_y_r - CIE_y_g) - \ |
94 |
CIE_y_w*(CIE_x_r - CIE_x_g) + \ |
95 |
CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r ) ) |
96 |
|
97 |
#define CIE_rf (CIE_y_r*CIE_C_rD/CIE_D) |
98 |
#define CIE_gf (CIE_y_g*CIE_C_gD/CIE_D) |
99 |
#define CIE_bf (CIE_y_b*CIE_C_bD/CIE_D) |
100 |
|
101 |
/* As of 9-94, CIE_rf=.265074126, CIE_gf=.670114631 and CIE_bf=.064811243 */ |
102 |
|
103 |
/***** The following definitions are valid for RGB colors only... *****/ |
104 |
|
105 |
#define bright(col) (CIE_rf*(col)[RED]+CIE_gf*(col)[GRN]+CIE_bf*(col)[BLU]) |
106 |
#define normbright(c) ( ( (long)(CIE_rf*256.+.5)*(c)[RED] + \ |
107 |
(long)(CIE_gf*256.+.5)*(c)[GRN] + \ |
108 |
(long)(CIE_bf*256.+.5)*(c)[BLU] ) >> 8 ) |
109 |
|
110 |
/* luminous efficacies over visible spectrum */ |
111 |
#define MAXEFFICACY 683. /* defined maximum at 550 nm */ |
112 |
#define WHTEFFICACY 179. /* uniform white light */ |
113 |
#define D65EFFICACY 203. /* standard illuminant D65 */ |
114 |
#define INCEFFICACY 160. /* illuminant A (incand.) */ |
115 |
#define SUNEFFICACY 208. /* illuminant B (solar dir.) */ |
116 |
#define SKYEFFICACY D65EFFICACY /* skylight (should be 110) */ |
117 |
#define DAYEFFICACY D65EFFICACY /* combined sky and solar */ |
118 |
|
119 |
#define luminance(col) (WHTEFFICACY * bright(col)) |
120 |
|
121 |
/***** ...end of stuff specific to RGB colors *****/ |
122 |
|
123 |
#define intens(col) ( (col)[0] > (col)[1] \ |
124 |
? (col)[0] > (col)[2] ? (col)[0] : (col)[2] \ |
125 |
: (col)[1] > (col)[2] ? (col)[1] : (col)[2] ) |
126 |
|
127 |
#define colrval(c,p) ( (c)[EXP] ? \ |
128 |
ldexp((c)[p]+.5,(int)(c)[EXP]-(COLXS+8)) : \ |
129 |
0. ) |
130 |
|
131 |
#define WHTCOLOR {1.0,1.0,1.0} |
132 |
#define BLKCOLOR {0.0,0.0,0.0} |
133 |
#define WHTCOLR {128,128,128,COLXS+1} |
134 |
#define BLKCOLR {0,0,0,0} |
135 |
|
136 |
/* picture format identifier */ |
137 |
#define COLRFMT "32-bit_rle_rgbe" |
138 |
#define CIEFMT "32-bit_rle_xyze" |
139 |
#define PICFMT "32-bit_rle_???e" /* matches either */ |
140 |
#define LPICFMT 15 /* max format id len */ |
141 |
|
142 |
/* macros for exposures */ |
143 |
#define EXPOSSTR "EXPOSURE=" |
144 |
#define LEXPOSSTR 9 |
145 |
#define isexpos(hl) (!strncmp(hl,EXPOSSTR,LEXPOSSTR)) |
146 |
#define exposval(hl) atof((hl)+LEXPOSSTR) |
147 |
#define fputexpos(ex,fp) fprintf(fp,"%s%e\n",EXPOSSTR,ex) |
148 |
|
149 |
/* macros for pixel aspect ratios */ |
150 |
#define ASPECTSTR "PIXASPECT=" |
151 |
#define LASPECTSTR 10 |
152 |
#define isaspect(hl) (!strncmp(hl,ASPECTSTR,LASPECTSTR)) |
153 |
#define aspectval(hl) atof((hl)+LASPECTSTR) |
154 |
#define fputaspect(pa,fp) fprintf(fp,"%s%f\n",ASPECTSTR,pa) |
155 |
|
156 |
/* macros for primary specifications */ |
157 |
#define PRIMARYSTR "PRIMARIES=" |
158 |
#define LPRIMARYSTR 10 |
159 |
#define isprims(hl) (!strncmp(hl,PRIMARYSTR,LPRIMARYSTR)) |
160 |
#define primsval(p,hl) (sscanf((hl)+LPRIMARYSTR, \ |
161 |
"%f %f %f %f %f %f %f %f", \ |
162 |
&(p)[RED][CIEX],&(p)[RED][CIEY], \ |
163 |
&(p)[GRN][CIEX],&(p)[GRN][CIEY], \ |
164 |
&(p)[BLU][CIEX],&(p)[BLU][CIEY], \ |
165 |
&(p)[WHT][CIEX],&(p)[WHT][CIEY]) == 8) |
166 |
#define fputprims(p,fp) fprintf(fp, \ |
167 |
"%s %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\n",\ |
168 |
PRIMARYSTR, \ |
169 |
(p)[RED][CIEX],(p)[RED][CIEY], \ |
170 |
(p)[GRN][CIEX],(p)[GRN][CIEY], \ |
171 |
(p)[BLU][CIEX],(p)[BLU][CIEY], \ |
172 |
(p)[WHT][CIEX],(p)[WHT][CIEY]) |
173 |
|
174 |
/* macros for color correction */ |
175 |
#define COLCORSTR "COLORCORR=" |
176 |
#define LCOLCORSTR 10 |
177 |
#define iscolcor(hl) (!strncmp(hl,COLCORSTR,LCOLCORSTR)) |
178 |
#define colcorval(cc,hl) sscanf((hl)+LCOLCORSTR,"%f %f %f", \ |
179 |
&(cc)[RED],&(cc)[GRN],&(cc)[BLU]) |
180 |
#define fputcolcor(cc,fp) fprintf(fp,"%s %f %f %f\n",COLCORSTR, \ |
181 |
(cc)[RED],(cc)[GRN],(cc)[BLU]) |
182 |
|
183 |
/* |
184 |
* Conversions to and from XYZ space generally don't apply WHTEFFICACY. |
185 |
* If you need Y to be luminance (cd/m^2), this must be applied when |
186 |
* converting from radiance (watts/sr/m^2). |
187 |
*/ |
188 |
|
189 |
extern RGBPRIMS stdprims; /* standard primary chromaticities */ |
190 |
extern COLORMAT rgb2xyzmat; /* RGB to XYZ conversion matrix */ |
191 |
extern COLORMAT xyz2rgbmat; /* XYZ to RGB conversion matrix */ |
192 |
extern COLOR cblack, cwhite; /* black (0,0,0) and white (1,1,1) */ |
193 |
|
194 |
#define CGAMUT_LOWER 01 |
195 |
#define CGAMUT_UPPER 02 |
196 |
#define CGAMUT (CGAMUT_LOWER|CGAMUT_UPPER) |
197 |
|
198 |
#define rgb_cie(xyz,rgb) colortrans(xyz,rgb2xyzmat,rgb) |
199 |
|
200 |
#define cpcolormat(md,ms) memcpy((void *)md,(void *)ms,sizeof(COLORMAT)) |
201 |
|
202 |
/* defined in color.c */ |
203 |
extern char *tempbuffer(unsigned int len); |
204 |
extern int fwritecolrs(COLR *scanline, int len, FILE *fp); |
205 |
extern int freadcolrs(COLR *scanline, int len, FILE *fp); |
206 |
extern int fwritescan(COLOR *scanline, int len, FILE *fp); |
207 |
extern int freadscan(COLOR *scanline, int len, FILE *fp); |
208 |
extern void setcolr(COLR clr, double r, double g, double b); |
209 |
extern void colr_color(COLOR col, COLR clr); |
210 |
extern int bigdiff(COLOR c1, COLOR c2, double md); |
211 |
/* defined in spec_rgb.c */ |
212 |
extern void spec_rgb(COLOR col, int s, int e); |
213 |
extern void spec_cie(COLOR col, int s, int e); |
214 |
extern void cie_rgb(COLOR rgb, COLOR xyz); |
215 |
extern int clipgamut(COLOR col, double brt, int gamut, |
216 |
COLOR lower, COLOR upper); |
217 |
extern void colortrans(COLOR c2, COLORMAT mat, COLOR c1); |
218 |
extern void multcolormat(COLORMAT m3, COLORMAT m2, |
219 |
COLORMAT m1); |
220 |
extern int colorprimsOK(RGBPRIMS pr); |
221 |
extern int compxyz2rgbmat(COLORMAT mat, RGBPRIMS pr); |
222 |
extern int comprgb2xyzmat(COLORMAT mat, RGBPRIMS pr); |
223 |
extern int comprgb2rgbmat(COLORMAT mat, RGBPRIMS pr1, RGBPRIMS pr2); |
224 |
extern int compxyzWBmat(COLORMAT mat, float wht1[2], |
225 |
float wht2[2]); |
226 |
extern int compxyz2rgbWBmat(COLORMAT mat, RGBPRIMS pr); |
227 |
extern int comprgb2xyzWBmat(COLORMAT mat, RGBPRIMS pr); |
228 |
extern int comprgb2rgbWBmat(COLORMAT mat, RGBPRIMS pr1, RGBPRIMS pr2); |
229 |
/* defined in colrops.c */ |
230 |
extern int setcolrcor(double (*f)(double, double), double a2); |
231 |
extern int setcolrinv(double (*f)(double, double), double a2); |
232 |
extern int setcolrgam(double g); |
233 |
extern int colrs_gambs(COLR *scan, int len); |
234 |
extern int gambs_colrs(COLR *scan, int len); |
235 |
extern void shiftcolrs(COLR *scan, int len, int adjust); |
236 |
extern void normcolrs(COLR *scan, int len, int adjust); |
237 |
|
238 |
|
239 |
#ifdef __cplusplus |
240 |
} |
241 |
#endif |
242 |
#endif /* _RAD_COLOR_H_ */ |
243 |
|