--- ray/src/common/color.h 1991/07/17 11:52:25 1.12
+++ ray/src/common/color.h 2003/02/22 02:07:22 2.18
@@ -1,11 +1,8 @@
-/* Copyright (c) 1986 Regents of the University of California */
-
-/* SCCSid "$SunId$ LBL" */
-
+/* RCSid: $Id: color.h,v 2.18 2003/02/22 02:07:22 greg Exp $ */
/*
* color.h - header for routines using pixel color values.
*
- * 12/31/85
+ * Must be included after X11 headers, since they declare a BYTE type.
*
* Two color representations are used, one for calculation and
* another for storage. Calculation is done with three floats
@@ -13,21 +10,95 @@
* three single byte mantissas and a common exponent.
*/
+/* ====================================================================
+ * The Radiance Software License, Version 1.0
+ *
+ * Copyright (c) 1990 - 2002 The Regents of the University of California,
+ * through Lawrence Berkeley National Laboratory. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes Radiance software
+ * (http://radsite.lbl.gov/)
+ * developed by the Lawrence Berkeley National Laboratory
+ * (http://www.lbl.gov/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
+ * and "The Regents of the University of California" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact radiance@radsite.lbl.gov.
+ *
+ * 5. Products derived from this software may not be called "Radiance",
+ * nor may "Radiance" appear in their name, without prior written
+ * permission of Lawrence Berkeley National Laboratory.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of Lawrence Berkeley National Laboratory. For more
+ * information on Lawrence Berkeley National Laboratory, please see
+ * .
+ */
+
+#include
+#include
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define RED 0
#define GRN 1
#define BLU 2
-#define EXP 3
+#define CIEX 0 /* or, if input is XYZ... */
+#define CIEY 1
+#define CIEZ 2
+#define EXP 3 /* exponent same for either format */
#define COLXS 128 /* excess used for exponent */
+#define WHT 3 /* used for RGBPRIMS type */
-typedef unsigned char BYTE; /* 8-bit unsigned integer */
+#undef BYTE
+#define BYTE unsigned char /* 8-bit unsigned integer */
-typedef BYTE COLR[4]; /* red, green, blue, exponent */
+typedef BYTE COLR[4]; /* red, green, blue (or X,Y,Z), exponent */
+typedef float COLOR[3]; /* red, green, blue (or X,Y,Z) */
+
+typedef float RGBPRIMS[4][2]; /* (x,y) chromaticities for RGBW */
+typedef float (*RGBPRIMP)[2]; /* pointer to RGBPRIMS array */
+
+typedef float COLORMAT[3][3]; /* color coordinate conversion matrix */
+
#define copycolr(c1,c2) (c1[0]=c2[0],c1[1]=c2[1], \
c1[2]=c2[2],c1[3]=c2[3])
-typedef float COLOR[3]; /* red, green, blue */
-
#define colval(col,pri) ((col)[pri])
#define setcolor(col,r,g,b) ((col)[RED]=(r),(col)[GRN]=(g),(col)[BLU]=(b))
@@ -41,15 +112,70 @@ typedef float COLOR[3]; /* red, green, blue */
#define multcolor(c1,c2) ((c1)[0]*=(c2)[0],(c1)[1]*=(c2)[1],(c1)[2]*=(c2)[2])
#ifdef NTSC
-#define bright(col) (.295*(col)[RED]+.636*(col)[GRN]+.070*(col)[BLU])
-#define normbright(c) (int)((74L*(c)[RED]+164L*(c)[GRN]+18L*(c)[BLU])/256)
+#define CIE_x_r 0.670 /* standard NTSC primaries */
+#define CIE_y_r 0.330
+#define CIE_x_g 0.210
+#define CIE_y_g 0.710
+#define CIE_x_b 0.140
+#define CIE_y_b 0.080
+#define CIE_x_w 0.3333 /* use true white */
+#define CIE_y_w 0.3333
#else
-#define bright(col) (.263*(col)[RED]+.655*(col)[GRN]+.082*(col)[BLU])
-#define normbright(c) (int)((67L*(c)[RED]+168L*(c)[GRN]+21L*(c)[BLU])/256)
+#define CIE_x_r 0.640 /* nominal CRT primaries */
+#define CIE_y_r 0.330
+#define CIE_x_g 0.290
+#define CIE_y_g 0.600
+#define CIE_x_b 0.150
+#define CIE_y_b 0.060
+#define CIE_x_w 0.3333 /* use true white */
+#define CIE_y_w 0.3333
#endif
-#define luminance(col) (470. * bright(col))
+#define STDPRIMS {{CIE_x_r,CIE_y_r},{CIE_x_g,CIE_y_g}, \
+ {CIE_x_b,CIE_y_b},{CIE_x_w,CIE_y_w}}
+#define CIE_D ( CIE_x_r*(CIE_y_g - CIE_y_b) + \
+ CIE_x_g*(CIE_y_b - CIE_y_r) + \
+ CIE_x_b*(CIE_y_r - CIE_y_g) )
+#define CIE_C_rD ( (1./CIE_y_w) * \
+ ( CIE_x_w*(CIE_y_g - CIE_y_b) - \
+ CIE_y_w*(CIE_x_g - CIE_x_b) + \
+ CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g ) )
+#define CIE_C_gD ( (1./CIE_y_w) * \
+ ( CIE_x_w*(CIE_y_b - CIE_y_r) - \
+ CIE_y_w*(CIE_x_b - CIE_x_r) - \
+ CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r ) )
+#define CIE_C_bD ( (1./CIE_y_w) * \
+ ( CIE_x_w*(CIE_y_r - CIE_y_g) - \
+ CIE_y_w*(CIE_x_r - CIE_x_g) + \
+ CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r ) )
+
+#define CIE_rf (CIE_y_r*CIE_C_rD/CIE_D)
+#define CIE_gf (CIE_y_g*CIE_C_gD/CIE_D)
+#define CIE_bf (CIE_y_b*CIE_C_bD/CIE_D)
+
+/* As of 9-94, CIE_rf=.265074126, CIE_gf=.670114631 and CIE_bf=.064811243 */
+
+/***** The following definitions are valid for RGB colors only... *****/
+
+#define bright(col) (CIE_rf*(col)[RED]+CIE_gf*(col)[GRN]+CIE_bf*(col)[BLU])
+#define normbright(c) ( ( (long)(CIE_rf*256.+.5)*(c)[RED] + \
+ (long)(CIE_gf*256.+.5)*(c)[GRN] + \
+ (long)(CIE_bf*256.+.5)*(c)[BLU] ) >> 8 )
+
+ /* luminous efficacies over visible spectrum */
+#define MAXEFFICACY 683. /* defined maximum at 550 nm */
+#define WHTEFFICACY 179. /* uniform white light */
+#define D65EFFICACY 203. /* standard illuminant D65 */
+#define INCEFFICACY 160. /* illuminant A (incand.) */
+#define SUNEFFICACY 208. /* illuminant B (solar dir.) */
+#define SKYEFFICACY D65EFFICACY /* skylight */
+#define DAYEFFICACY D65EFFICACY /* combined sky and solar */
+
+#define luminance(col) (WHTEFFICACY * bright(col))
+
+/***** ...end of stuff specific to RGB colors *****/
+
#define intens(col) ( (col)[0] > (col)[1] \
? (col)[0] > (col)[2] ? (col)[0] : (col)[2] \
: (col)[1] > (col)[2] ? (col)[1] : (col)[2] )
@@ -63,13 +189,11 @@ typedef float COLOR[3]; /* red, green, blue */
#define WHTCOLR {128,128,128,COLXS+1}
#define BLKCOLR {0,0,0,0}
- /* definitions for resolution header */
-#define XDECR 1
-#define YDECR 2
-#define YMAJOR 4
-
/* picture format identifier */
#define COLRFMT "32-bit_rle_rgbe"
+#define CIEFMT "32-bit_rle_xyze"
+#define PICFMT "32-bit_rle_???e" /* matches either */
+#define LPICFMT 15 /* max format id len */
/* macros for exposures */
#define EXPOSSTR "EXPOSURE="
@@ -85,6 +209,24 @@ typedef float COLOR[3]; /* red, green, blue */
#define aspectval(hl) atof((hl)+LASPECTSTR)
#define fputaspect(pa,fp) fprintf(fp,"%s%f\n",ASPECTSTR,pa)
+ /* macros for primary specifications */
+#define PRIMARYSTR "PRIMARIES="
+#define LPRIMARYSTR 10
+#define isprims(hl) (!strncmp(hl,PRIMARYSTR,LPRIMARYSTR))
+#define primsval(p,hl) sscanf(hl+LPRIMARYSTR, \
+ "%f %f %f %f %f %f %f %f", \
+ &(p)[RED][CIEX],&(p)[RED][CIEY], \
+ &(p)[GRN][CIEX],&(p)[GRN][CIEY], \
+ &(p)[BLU][CIEX],&(p)[BLU][CIEY], \
+ &(p)[WHT][CIEX],&(p)[WHT][CIEY])
+#define fputprims(p,fp) fprintf(fp, \
+ "%s %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\n",\
+ PRIMARYSTR, \
+ (p)[RED][CIEX],(p)[RED][CIEY], \
+ (p)[GRN][CIEX],(p)[GRN][CIEY], \
+ (p)[BLU][CIEX],(p)[BLU][CIEY], \
+ (p)[WHT][CIEX],(p)[WHT][CIEY])
+
/* macros for color correction */
#define COLCORSTR "COLORCORR="
#define LCOLCORSTR 10
@@ -94,4 +236,100 @@ typedef float COLOR[3]; /* red, green, blue */
#define fputcolcor(cc,fp) fprintf(fp,"%s %f %f %f\n",COLCORSTR, \
(cc)[RED],(cc)[GRN],(cc)[BLU])
-extern double ldexp(), atof();
+/*
+ * Conversions to and from XYZ space generally don't apply WHTEFFICACY.
+ * If you need Y to be luminance (cd/m^2), this must be applied when
+ * converting from radiance (watts/sr/m^2).
+ */
+
+extern RGBPRIMS stdprims; /* standard primary chromaticities */
+extern COLORMAT rgb2xyzmat; /* RGB to XYZ conversion matrix */
+extern COLORMAT xyz2rgbmat; /* XYZ to RGB conversion matrix */
+extern COLOR cblack, cwhite; /* black (0,0,0) and white (1,1,1) */
+
+#define CGAMUT_LOWER 01
+#define CGAMUT_UPPER 02
+#define CGAMUT (CGAMUT_LOWER|CGAMUT_UPPER)
+
+#define rgb_cie(xyz,rgb) colortrans(xyz,rgb2xyzmat,rgb)
+
+#ifdef BSD
+#define cpcolormat(md,ms) bcopy((char *)ms,(char *)md,sizeof(COLORMAT))
+#else
+#define cpcolormat(md,ms) memcpy((char *)md,(char *)ms,sizeof(COLORMAT))
+#endif
+
+#ifdef NOPROTO
+ /* defined in color.c */
+extern char *tempbuffer();
+extern int fwritecolrs();
+extern int freadcolrs();
+extern int fwritescan();
+extern int freadscan();
+extern void setcolr();
+extern void colr_color();
+extern int bigdiff();
+ /* defined in spec_rgb.c */
+extern void spec_rgb();
+extern void spec_cie();
+extern void cie_rgb();
+extern int clipgamut();
+extern void colortrans();
+extern void multcolormat();
+extern void compxyz2rgbmat();
+extern void comprgb2xyzmat();
+extern void comprgb2rgbmat();
+extern void compxyzWBmat();
+extern void compxyz2rgbWBmat();
+extern void comprgb2xyzWBmat();
+extern void comprgb2rgbWBmat();
+ /* defined in colrops.c */
+extern int setcolrcor();
+extern int setcolrinv();
+extern int setcolrgam();
+extern int colrs_gambs();
+extern int gambs_colrs();
+extern void shiftcolrs();
+extern void normcolrs();
+
+#else
+ /* defined in color.c */
+extern char *tempbuffer(unsigned int len);
+extern int fwritecolrs(COLR *scanline, int len, FILE *fp);
+extern int freadcolrs(COLR *scanline, int len, FILE *fp);
+extern int fwritescan(COLOR *scanline, int len, FILE *fp);
+extern int freadscan(COLOR *scanline, int len, FILE *fp);
+extern void setcolr(COLR clr, double r, double g, double b);
+extern void colr_color(COLOR col, COLR clr);
+extern int bigdiff(COLOR c1, COLOR c2, double md);
+ /* defined in spec_rgb.c */
+extern void spec_rgb(COLOR col, int s, int e);
+extern void spec_cie(COLOR col, int s, int e);
+extern void cie_rgb(COLOR rgb, COLOR xyz);
+extern int clipgamut(COLOR col, double brt, int gamut,
+ COLOR lower, COLOR upper);
+extern void colortrans(COLOR c2, COLORMAT mat, COLOR c1);
+extern void multcolormat(COLORMAT m3, COLORMAT m2,
+ COLORMAT m1);
+extern void compxyz2rgbmat(COLORMAT mat, RGBPRIMS pr);
+extern void comprgb2xyzmat(COLORMAT mat, RGBPRIMS pr);
+extern void comprgb2rgbmat(COLORMAT mat, RGBPRIMS pr1, RGBPRIMS pr2);
+extern void compxyzWBmat(COLORMAT mat, float wht1[2],
+ float wht2[2]);
+extern void compxyz2rgbWBmat(COLORMAT mat, RGBPRIMS pr);
+extern void comprgb2xyzWBmat(COLORMAT mat, RGBPRIMS pr);
+extern void comprgb2rgbWBmat(COLORMAT mat, RGBPRIMS pr1, RGBPRIMS pr2);
+ /* defined in colrops.c */
+extern int setcolrcor(double (*f)(), double a2);
+extern int setcolrinv(double (*f)(), double a2);
+extern int setcolrgam(double g);
+extern int colrs_gambs(COLR *scan, int len);
+extern int gambs_colrs(COLR *scan, int len);
+extern void shiftcolrs(COLR *scan, int len, int adjust);
+extern void normcolrs(COLR *scan, int len, int adjust);
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif