1 .\" $Id: TIFFcolor.3tiff,v 1.3 2006/03/23 14:54:02 dron Exp $
3 .\" Copyright (c) 2003, Andrey Kiselev <dron@ak4719.spb.edu>
5 .\" Permission to use, copy, modify, distribute, and sell this software and
6 .\" its documentation for any purpose is hereby granted without fee, provided
7 .\" that (i) the above copyright notices and this permission notice appear in
8 .\" all copies of the software and related documentation, and (ii) the names of
9 .\" Sam Leffler and Silicon Graphics may not be used in any advertising or
10 .\" publicity relating to the software without the specific, prior written
11 .\" permission of Sam Leffler and Silicon Graphics.
13 .\" THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 .\" EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 .\" WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 .\" IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 .\" ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 .\" OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 .\" WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 .\" LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 .TH COLOR 3TIFF "December 21, 2003" "libtiff"
27 TIFFYCbCrToRGBInit, TIFFYCbCrtoRGB, TIFFCIELabToRGBInit, TIFFCIELabToXYZ,
28 TIFFXYZToRGB \- color conversion routines.
30 .B "#include <tiffio.h>"
32 .BI "int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *" ycbcr ", float *" luma ", float *"refBlackWhite" );"
34 .BI "void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *" ycbcr ", uint32 " Y ", int32 " Cb ", int32 " Cr ", uint32 *" R ", uint32 *" G ", uint32 *" B " );"
36 .BI "int TIFFCIELabToRGBInit(TIFFCIELabToRGB *" cielab ", TIFFDisplay *" display ", float *" refWhite ");"
38 .BI "void TIFFCIELabToXYZ(TIFFCIELabToRGB *" cielab ", uint32 " L ", int32 " a ", int32 " b ", float *" X ", float *" Y ", float *" Z ");"
40 .BI "void TIFFXYZToRGB(TIFFCIELabToRGB *" cielab ", float " X ", float " Y ", float " Z" , uint32 *" R ", uint32 *" G ", uint32 *" B ");"
42 TIFF supports several color spaces for images stored in that format. There is
43 usually a problem of application to handle the data properly and convert
44 between different colorspaces for displaying and printing purposes. To
45 simplify this task libtiff implements several color conversion routines
46 itself. In particular, these routines used in
47 .B TIFFRGBAImage(3TIFF)
50 .B TIFFYCbCrToRGBInit()
55 conversion state. Allocating and freeing of the
57 structure belongs to programmer.
65 typedef struct { /* YCbCr->RGB support */
66 TIFFRGBValue* clamptab; /* range clamping table */
77 is a float array of three values representing proportions of the red, green
78 and blue in luminance, Y (see section 21 of the TIFF 6.0 specification, where
79 the YCbCr images discussed).
80 .I TIFFTAG_YCBCRCOEFFICIENTS
81 holds that values in TIFF file.
83 is a float array of 6 values which specifies a pair of headroom and footroom
84 image data values (codes) for each image component (see section 20 of the
85 TIFF 6.0 specification where the colorinmetry fields discussed).
86 .I TIFFTAG_REFERENCEBLACKWHITE
87 is responsible for storing these values in TIFF file. Following code snippet
88 should helps to understand the the technique:
92 float *luma, *refBlackWhite;
95 /* Initialize structures */
96 ycbcr = (TIFFYCbCrToRGB*)
97 _TIFFmalloc(TIFFroundup(sizeof(TIFFYCbCrToRGB), sizeof(long))
98 + 4*256*sizeof(TIFFRGBValue)
100 + 3*256*sizeof(int32));
102 TIFFError("YCbCr->RGB",
103 "No space for YCbCr->RGB conversion state");
107 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
108 TIFFGetFieldDefaulted(tif, TIFFTAG_REFERENCEBLACKWHITE, &refBlackWhite);
109 if (TIFFYCbCrToRGBInit(ycbcr, luma, refBlackWhite) < 0)
112 /* Start conversion */
117 for each pixel in image
118 TIFFYCbCrtoRGB(img->ycbcr, Y, Cb, Cr, &r, &g, &b);
120 /* Free state structure */
127 .B TIFFCIELabToRGBInit()
138 #define CIELABTORGB_TABLE_RANGE 1500
140 typedef struct { /* CIE Lab 1976->RGB support */
141 int range; /* Size of conversion table */
142 float rstep, gstep, bstep;
143 float X0, Y0, Z0; /* Reference white point */
145 float Yr2r[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yr to r */
146 float Yg2g[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yg to g */
147 float Yb2b[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yb to b */
153 is a display device description, declared as
158 float d_mat[3][3]; /* XYZ -> luminance matrix */
159 float d_YCR; /* Light o/p for reference white */
162 uint32 d_Vrwr; /* Pixel values for ref. white */
165 float d_Y0R; /* Residual light for black pixel */
168 float d_gammaR; /* Gamma values for the three guns */
175 For example, the one can use sRGB device, which has the following parameters:
179 TIFFDisplay display_sRGB = {
180 { /* XYZ -> luminance matrix */
181 { 3.2410F, -1.5374F, -0.4986F },
182 { -0.9692F, 1.8760F, 0.0416F },
183 { 0.0556F, -0.2040F, 1.0570F }
185 100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
186 255, 255, 255, /* Pixel values for ref. white */
187 1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
188 2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
194 is a color temperature of the reference white. The
195 .I TIFFTAG_WHITEPOINT
196 contains the chromaticity of the white point of the image from where the
197 reference white can be calculated using following formulae:
202 refWhite_X = whitePoint_x / whitePoint_y * refWhite_Y
204 refWhite_Z = (1.0 - whitePoint_x - whitePoint_y) / whitePoint_y * refWhite_X
208 The conversion itself performed in two steps: at the first one we will convert
214 routine, and at the second step we will convert
220 Look at the code sample below:
227 /* Initialize structures */
228 img->cielab = (TIFFCIELabToRGB *)
229 _TIFFmalloc(sizeof(TIFFCIELabToRGB));
231 TIFFError("CIE L*a*b*->RGB",
232 "No space for CIE L*a*b*->RGB conversion state.");
236 TIFFGetFieldDefaulted(tif, TIFFTAG_WHITEPOINT, &whitePoint);
237 refWhite[1] = 100.0F;
238 refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
239 refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
240 / whitePoint[1] * refWhite[1];
241 if (TIFFCIELabToRGBInit(cielab, &display_sRGB, refWhite) < 0) {
242 TIFFError("CIE L*a*b*->RGB",
243 "Failed to initialize CIE L*a*b*->RGB conversion state.");
248 /* Now we can start to convert */
254 for each pixel in image
255 TIFFCIELabToXYZ(cielab, L, a, b, &X, &Y, &Z);
256 TIFFXYZToRGB(cielab, X, Y, Z, &r, &g, &b);
258 /* Don't forget to free the state structure */
264 .BR TIFFRGBAImage (3TIFF)
267 Libtiff library home page:
268 .BR http://www.remotesensing.org/libtiff/