1 |
/* RCSid: $Id$ */ |
2 |
/* |
3 |
* color.h - header for routines using pixel color values. |
4 |
* |
5 |
* Must be included after X11 headers, since they declare a BYTE type. |
6 |
* |
7 |
* Two color representations are used, one for calculation and |
8 |
* another for storage. Calculation is done with three floats |
9 |
* for speed. Stored color values use 4 bytes which contain |
10 |
* three single byte mantissas and a common exponent. |
11 |
*/ |
12 |
|
13 |
/* ==================================================================== |
14 |
* The Radiance Software License, Version 1.0 |
15 |
* |
16 |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
17 |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
18 |
* |
19 |
* Redistribution and use in source and binary forms, with or without |
20 |
* modification, are permitted provided that the following conditions |
21 |
* are met: |
22 |
* |
23 |
* 1. Redistributions of source code must retain the above copyright |
24 |
* notice, this list of conditions and the following disclaimer. |
25 |
* |
26 |
* 2. Redistributions in binary form must reproduce the above copyright |
27 |
* notice, this list of conditions and the following disclaimer in |
28 |
* the documentation and/or other materials provided with the |
29 |
* distribution. |
30 |
* |
31 |
* 3. The end-user documentation included with the redistribution, |
32 |
* if any, must include the following acknowledgment: |
33 |
* "This product includes Radiance software |
34 |
* (http://radsite.lbl.gov/) |
35 |
* developed by the Lawrence Berkeley National Laboratory |
36 |
* (http://www.lbl.gov/)." |
37 |
* Alternately, this acknowledgment may appear in the software itself, |
38 |
* if and wherever such third-party acknowledgments normally appear. |
39 |
* |
40 |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
41 |
* and "The Regents of the University of California" must |
42 |
* not be used to endorse or promote products derived from this |
43 |
* software without prior written permission. For written |
44 |
* permission, please contact [email protected]. |
45 |
* |
46 |
* 5. Products derived from this software may not be called "Radiance", |
47 |
* nor may "Radiance" appear in their name, without prior written |
48 |
* permission of Lawrence Berkeley National Laboratory. |
49 |
* |
50 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
51 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
52 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
53 |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
54 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
55 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
56 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
57 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
58 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
59 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
60 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
61 |
* SUCH DAMAGE. |
62 |
* ==================================================================== |
63 |
* |
64 |
* This software consists of voluntary contributions made by many |
65 |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
66 |
* information on Lawrence Berkeley National Laboratory, please see |
67 |
* <http://www.lbl.gov/>. |
68 |
*/ |
69 |
|
70 |
#include <stdio.h> |
71 |
#include <stdlib.h> |
72 |
|
73 |
#ifdef __cplusplus |
74 |
extern "C" { |
75 |
#endif |
76 |
|
77 |
#define RED 0 |
78 |
#define GRN 1 |
79 |
#define BLU 2 |
80 |
#define CIEX 0 /* or, if input is XYZ... */ |
81 |
#define CIEY 1 |
82 |
#define CIEZ 2 |
83 |
#define EXP 3 /* exponent same for either format */ |
84 |
#define COLXS 128 /* excess used for exponent */ |
85 |
#define WHT 3 /* used for RGBPRIMS type */ |
86 |
|
87 |
#undef BYTE |
88 |
#define BYTE unsigned char /* 8-bit unsigned integer */ |
89 |
|
90 |
typedef BYTE COLR[4]; /* red, green, blue (or X,Y,Z), exponent */ |
91 |
|
92 |
typedef float COLOR[3]; /* red, green, blue (or X,Y,Z) */ |
93 |
|
94 |
typedef float RGBPRIMS[4][2]; /* (x,y) chromaticities for RGBW */ |
95 |
typedef float (*RGBPRIMP)[2]; /* pointer to RGBPRIMS array */ |
96 |
|
97 |
typedef float COLORMAT[3][3]; /* color coordinate conversion matrix */ |
98 |
|
99 |
#define copycolr(c1,c2) (c1[0]=c2[0],c1[1]=c2[1], \ |
100 |
c1[2]=c2[2],c1[3]=c2[3]) |
101 |
|
102 |
#define colval(col,pri) ((col)[pri]) |
103 |
|
104 |
#define setcolor(col,r,g,b) ((col)[RED]=(r),(col)[GRN]=(g),(col)[BLU]=(b)) |
105 |
|
106 |
#define copycolor(c1,c2) ((c1)[0]=(c2)[0],(c1)[1]=(c2)[1],(c1)[2]=(c2)[2]) |
107 |
|
108 |
#define scalecolor(col,sf) ((col)[0]*=(sf),(col)[1]*=(sf),(col)[2]*=(sf)) |
109 |
|
110 |
#define addcolor(c1,c2) ((c1)[0]+=(c2)[0],(c1)[1]+=(c2)[1],(c1)[2]+=(c2)[2]) |
111 |
|
112 |
#define multcolor(c1,c2) ((c1)[0]*=(c2)[0],(c1)[1]*=(c2)[1],(c1)[2]*=(c2)[2]) |
113 |
|
114 |
#ifdef NTSC |
115 |
#define CIE_x_r 0.670 /* standard NTSC primaries */ |
116 |
#define CIE_y_r 0.330 |
117 |
#define CIE_x_g 0.210 |
118 |
#define CIE_y_g 0.710 |
119 |
#define CIE_x_b 0.140 |
120 |
#define CIE_y_b 0.080 |
121 |
#define CIE_x_w 0.3333 /* use true white */ |
122 |
#define CIE_y_w 0.3333 |
123 |
#else |
124 |
#define CIE_x_r 0.640 /* nominal CRT primaries */ |
125 |
#define CIE_y_r 0.330 |
126 |
#define CIE_x_g 0.290 |
127 |
#define CIE_y_g 0.600 |
128 |
#define CIE_x_b 0.150 |
129 |
#define CIE_y_b 0.060 |
130 |
#define CIE_x_w 0.3333 /* use true white */ |
131 |
#define CIE_y_w 0.3333 |
132 |
#endif |
133 |
|
134 |
#define STDPRIMS {{CIE_x_r,CIE_y_r},{CIE_x_g,CIE_y_g}, \ |
135 |
{CIE_x_b,CIE_y_b},{CIE_x_w,CIE_y_w}} |
136 |
|
137 |
#define CIE_D ( CIE_x_r*(CIE_y_g - CIE_y_b) + \ |
138 |
CIE_x_g*(CIE_y_b - CIE_y_r) + \ |
139 |
CIE_x_b*(CIE_y_r - CIE_y_g) ) |
140 |
#define CIE_C_rD ( (1./CIE_y_w) * \ |
141 |
( CIE_x_w*(CIE_y_g - CIE_y_b) - \ |
142 |
CIE_y_w*(CIE_x_g - CIE_x_b) + \ |
143 |
CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g ) ) |
144 |
#define CIE_C_gD ( (1./CIE_y_w) * \ |
145 |
( CIE_x_w*(CIE_y_b - CIE_y_r) - \ |
146 |
CIE_y_w*(CIE_x_b - CIE_x_r) - \ |
147 |
CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r ) ) |
148 |
#define CIE_C_bD ( (1./CIE_y_w) * \ |
149 |
( CIE_x_w*(CIE_y_r - CIE_y_g) - \ |
150 |
CIE_y_w*(CIE_x_r - CIE_x_g) + \ |
151 |
CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r ) ) |
152 |
|
153 |
#define CIE_rf (CIE_y_r*CIE_C_rD/CIE_D) |
154 |
#define CIE_gf (CIE_y_g*CIE_C_gD/CIE_D) |
155 |
#define CIE_bf (CIE_y_b*CIE_C_bD/CIE_D) |
156 |
|
157 |
/* As of 9-94, CIE_rf=.265074126, CIE_gf=.670114631 and CIE_bf=.064811243 */ |
158 |
|
159 |
/***** The following definitions are valid for RGB colors only... *****/ |
160 |
|
161 |
#define bright(col) (CIE_rf*(col)[RED]+CIE_gf*(col)[GRN]+CIE_bf*(col)[BLU]) |
162 |
#define normbright(c) ( ( (long)(CIE_rf*256.+.5)*(c)[RED] + \ |
163 |
(long)(CIE_gf*256.+.5)*(c)[GRN] + \ |
164 |
(long)(CIE_bf*256.+.5)*(c)[BLU] ) >> 8 ) |
165 |
|
166 |
/* luminous efficacies over visible spectrum */ |
167 |
#define MAXEFFICACY 683. /* defined maximum at 550 nm */ |
168 |
#define WHTEFFICACY 179. /* uniform white light */ |
169 |
#define D65EFFICACY 203. /* standard illuminant D65 */ |
170 |
#define INCEFFICACY 160. /* illuminant A (incand.) */ |
171 |
#define SUNEFFICACY 208. /* illuminant B (solar dir.) */ |
172 |
#define SKYEFFICACY D65EFFICACY /* skylight */ |
173 |
#define DAYEFFICACY D65EFFICACY /* combined sky and solar */ |
174 |
|
175 |
#define luminance(col) (WHTEFFICACY * bright(col)) |
176 |
|
177 |
/***** ...end of stuff specific to RGB colors *****/ |
178 |
|
179 |
#define intens(col) ( (col)[0] > (col)[1] \ |
180 |
? (col)[0] > (col)[2] ? (col)[0] : (col)[2] \ |
181 |
: (col)[1] > (col)[2] ? (col)[1] : (col)[2] ) |
182 |
|
183 |
#define colrval(c,p) ( (c)[EXP] ? \ |
184 |
ldexp((c)[p]+.5,(int)(c)[EXP]-(COLXS+8)) : \ |
185 |
0. ) |
186 |
|
187 |
#define WHTCOLOR {1.0,1.0,1.0} |
188 |
#define BLKCOLOR {0.0,0.0,0.0} |
189 |
#define WHTCOLR {128,128,128,COLXS+1} |
190 |
#define BLKCOLR {0,0,0,0} |
191 |
|
192 |
/* picture format identifier */ |
193 |
#define COLRFMT "32-bit_rle_rgbe" |
194 |
#define CIEFMT "32-bit_rle_xyze" |
195 |
#define PICFMT "32-bit_rle_???e" /* matches either */ |
196 |
#define LPICFMT 15 /* max format id len */ |
197 |
|
198 |
/* macros for exposures */ |
199 |
#define EXPOSSTR "EXPOSURE=" |
200 |
#define LEXPOSSTR 9 |
201 |
#define isexpos(hl) (!strncmp(hl,EXPOSSTR,LEXPOSSTR)) |
202 |
#define exposval(hl) atof((hl)+LEXPOSSTR) |
203 |
#define fputexpos(ex,fp) fprintf(fp,"%s%e\n",EXPOSSTR,ex) |
204 |
|
205 |
/* macros for pixel aspect ratios */ |
206 |
#define ASPECTSTR "PIXASPECT=" |
207 |
#define LASPECTSTR 10 |
208 |
#define isaspect(hl) (!strncmp(hl,ASPECTSTR,LASPECTSTR)) |
209 |
#define aspectval(hl) atof((hl)+LASPECTSTR) |
210 |
#define fputaspect(pa,fp) fprintf(fp,"%s%f\n",ASPECTSTR,pa) |
211 |
|
212 |
/* macros for primary specifications */ |
213 |
#define PRIMARYSTR "PRIMARIES=" |
214 |
#define LPRIMARYSTR 10 |
215 |
#define isprims(hl) (!strncmp(hl,PRIMARYSTR,LPRIMARYSTR)) |
216 |
#define primsval(p,hl) sscanf(hl+LPRIMARYSTR, \ |
217 |
"%f %f %f %f %f %f %f %f", \ |
218 |
&(p)[RED][CIEX],&(p)[RED][CIEY], \ |
219 |
&(p)[GRN][CIEX],&(p)[GRN][CIEY], \ |
220 |
&(p)[BLU][CIEX],&(p)[BLU][CIEY], \ |
221 |
&(p)[WHT][CIEX],&(p)[WHT][CIEY]) |
222 |
#define fputprims(p,fp) fprintf(fp, \ |
223 |
"%s %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\n",\ |
224 |
PRIMARYSTR, \ |
225 |
(p)[RED][CIEX],(p)[RED][CIEY], \ |
226 |
(p)[GRN][CIEX],(p)[GRN][CIEY], \ |
227 |
(p)[BLU][CIEX],(p)[BLU][CIEY], \ |
228 |
(p)[WHT][CIEX],(p)[WHT][CIEY]) |
229 |
|
230 |
/* macros for color correction */ |
231 |
#define COLCORSTR "COLORCORR=" |
232 |
#define LCOLCORSTR 10 |
233 |
#define iscolcor(hl) (!strncmp(hl,COLCORSTR,LCOLCORSTR)) |
234 |
#define colcorval(cc,hl) sscanf(hl+LCOLCORSTR,"%f %f %f", \ |
235 |
&(cc)[RED],&(cc)[GRN],&(cc)[BLU]) |
236 |
#define fputcolcor(cc,fp) fprintf(fp,"%s %f %f %f\n",COLCORSTR, \ |
237 |
(cc)[RED],(cc)[GRN],(cc)[BLU]) |
238 |
|
239 |
/* |
240 |
* Conversions to and from XYZ space generally don't apply WHTEFFICACY. |
241 |
* If you need Y to be luminance (cd/m^2), this must be applied when |
242 |
* converting from radiance (watts/sr/m^2). |
243 |
*/ |
244 |
|
245 |
extern RGBPRIMS stdprims; /* standard primary chromaticities */ |
246 |
extern COLORMAT rgb2xyzmat; /* RGB to XYZ conversion matrix */ |
247 |
extern COLORMAT xyz2rgbmat; /* XYZ to RGB conversion matrix */ |
248 |
extern COLOR cblack, cwhite; /* black (0,0,0) and white (1,1,1) */ |
249 |
|
250 |
#define CGAMUT_LOWER 01 |
251 |
#define CGAMUT_UPPER 02 |
252 |
#define CGAMUT (CGAMUT_LOWER|CGAMUT_UPPER) |
253 |
|
254 |
#define rgb_cie(xyz,rgb) colortrans(xyz,rgb2xyzmat,rgb) |
255 |
|
256 |
#ifdef BSD |
257 |
#define cpcolormat(md,ms) bcopy((char *)ms,(char *)md,sizeof(COLORMAT)) |
258 |
#else |
259 |
#define cpcolormat(md,ms) memcpy((char *)md,(char *)ms,sizeof(COLORMAT)) |
260 |
#endif |
261 |
|
262 |
#ifdef NOPROTO |
263 |
/* defined in color.c */ |
264 |
extern char *tempbuffer(); |
265 |
extern int fwritecolrs(); |
266 |
extern int freadcolrs(); |
267 |
extern int fwritescan(); |
268 |
extern int freadscan(); |
269 |
extern void setcolr(); |
270 |
extern void colr_color(); |
271 |
extern int bigdiff(); |
272 |
/* defined in spec_rgb.c */ |
273 |
extern void spec_rgb(); |
274 |
extern void spec_cie(); |
275 |
extern void cie_rgb(); |
276 |
extern int clipgamut(); |
277 |
extern void colortrans(); |
278 |
extern void multcolormat(); |
279 |
extern void compxyz2rgbmat(); |
280 |
extern void comprgb2xyzmat(); |
281 |
extern void comprgb2rgbmat(); |
282 |
extern void compxyzWBmat(); |
283 |
extern void compxyz2rgbWBmat(); |
284 |
extern void comprgb2xyzWBmat(); |
285 |
extern void comprgb2rgbWBmat(); |
286 |
/* defined in colrops.c */ |
287 |
extern int setcolrcor(); |
288 |
extern int setcolrinv(); |
289 |
extern int setcolrgam(); |
290 |
extern int colrs_gambs(); |
291 |
extern int gambs_colrs(); |
292 |
extern void shiftcolrs(); |
293 |
extern void normcolrs(); |
294 |
|
295 |
#else |
296 |
/* defined in color.c */ |
297 |
extern char *tempbuffer(unsigned int len); |
298 |
extern int fwritecolrs(COLR *scanline, int len, FILE *fp); |
299 |
extern int freadcolrs(COLR *scanline, int len, FILE *fp); |
300 |
extern int fwritescan(COLOR *scanline, int len, FILE *fp); |
301 |
extern int freadscan(COLOR *scanline, int len, FILE *fp); |
302 |
extern void setcolr(COLR clr, double r, double g, double b); |
303 |
extern void colr_color(COLOR col, COLR clr); |
304 |
extern int bigdiff(COLOR c1, COLOR c2, double md); |
305 |
/* defined in spec_rgb.c */ |
306 |
extern void spec_rgb(COLOR col, int s, int e); |
307 |
extern void spec_cie(COLOR col, int s, int e); |
308 |
extern void cie_rgb(COLOR rgb, COLOR xyz); |
309 |
extern int clipgamut(COLOR col, double brt, int gamut, |
310 |
COLOR lower, COLOR upper); |
311 |
extern void colortrans(COLOR c2, COLORMAT mat, COLOR c1); |
312 |
extern void multcolormat(COLORMAT m3, COLORMAT m2, |
313 |
COLORMAT m1); |
314 |
extern void compxyz2rgbmat(COLORMAT mat, RGBPRIMS pr); |
315 |
extern void comprgb2xyzmat(COLORMAT mat, RGBPRIMS pr); |
316 |
extern void comprgb2rgbmat(COLORMAT mat, RGBPRIMS pr1, RGBPRIMS pr2); |
317 |
extern void compxyzWBmat(COLORMAT mat, float wht1[2], |
318 |
float wht2[2]); |
319 |
extern void compxyz2rgbWBmat(COLORMAT mat, RGBPRIMS pr); |
320 |
extern void comprgb2xyzWBmat(COLORMAT mat, RGBPRIMS pr); |
321 |
extern void comprgb2rgbWBmat(COLORMAT mat, RGBPRIMS pr1, RGBPRIMS pr2); |
322 |
/* defined in colrops.c */ |
323 |
extern int setcolrcor(double (*f)(), double a2); |
324 |
extern int setcolrinv(double (*f)(), double a2); |
325 |
extern int setcolrgam(double g); |
326 |
extern int colrs_gambs(COLR *scan, int len); |
327 |
extern int gambs_colrs(COLR *scan, int len); |
328 |
extern void shiftcolrs(COLR *scan, int len, int adjust); |
329 |
extern void normcolrs(COLR *scan, int len, int adjust); |
330 |
|
331 |
#endif |
332 |
|
333 |
#ifdef __cplusplus |
334 |
} |
335 |
#endif |