1COLOR(3TIFF)                                                      COLOR(3TIFF)
2
3
4

NAME

6       TIFFYCbCrToRGBInit,    TIFFYCbCrtoRGB,    TIFFCIELabToRGBInit,    TIFF‐
7       CIELabToXYZ, TIFFXYZToRGB - color conversion routines.
8

SYNOPSIS

10       #include <tiffio.h>
11
12       int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *ycbcr, float *luma, float  *ref‐
13       BlackWhite");"
14       void  TIFFYCbCrtoRGB(TIFFYCbCrToRGB  *ycbcr,  uint32 Y, int32 Cb, int32
15       Cr, uint32 *R, uint32 *G, uint32 *B );
16
17       int  TIFFCIELabToRGBInit(TIFFCIELabToRGB  *cielab,  const   TIFFDisplay
18       *display, float *refWhite);
19       void  TIFFCIELabToXYZ(TIFFCIELabToRGB *cielab, uint32 L, int32 a, int32
20       b, float *X, float *Y, float *Z);
21       void TIFFXYZToRGB(TIFFCIELabToRGB *cielab,  float  X,  float  Y,  float
22       Z",uint32*"R, uint32 *G, uint32 *B);
23

DESCRIPTION

25       TIFF  supports  several  color spaces for images stored in that format.
26       There is usually a problem of application to handle the  data  properly
27       and  convert  between different colorspaces for displaying and printing
28       purposes. To simplify this task libtiff implements several  color  con‐
29       version  routines itself. In particular, these routines used in TIFFRG‐
30       BAImage(3TIFF) interface.
31
32       TIFFYCbCrToRGBInit() used to initialize YCbCr to RGB conversion  state.
33       Allocating  and  freeing  of the ycbcr structure belongs to programmer.
34       TIFFYCbCrToRGB defined in tiffio.h as
35
36              typedef struct {                /* YCbCr->RGB support */
37                      TIFFRGBValue* clamptab; /* range clamping table */
38                      int*         Cr_r_tab;
39                      int*         Cb_b_tab;
40                      int32*       Cr_g_tab;
41                      int32*       Cb_g_tab;
42                      int32*        Y_tab;
43              } TIFFYCbCrToRGB;
44
45       luma is a float array of three values representing proportions  of  the
46       red,  green  and  blue  in luminance, Y (see section 21 of the TIFF 6.0
47       specification, where the YCbCr images discussed).  TIFFTAG_YCBCRCOEFFI‐
48       CIENTS  holds that values in TIFF file.  refBlackWhite is a float array
49       of 6 values which specifies a pair of headroom and footroom image  data
50       values (codes) for each image component (see section 20 of the TIFF 6.0
51       specification where the colorinmetry fields discussed).  TIFFTAG_REFER‐
52       ENCEBLACKWHITE  is  responsible  for storing these values in TIFF file.
53       Following code snippet should helps to understand the the technique:
54
55              float *luma, *refBlackWhite;
56              uint16 hs, vs;
57
58              /* Initialize structures */
59              ycbcr = (TIFFYCbCrToRGB*)
60                   _TIFFmalloc(TIFFroundup(sizeof(TIFFYCbCrToRGB), sizeof(long))
61                        + 4*256*sizeof(TIFFRGBValue)
62                        + 2*256*sizeof(int)
63                        + 3*256*sizeof(int32));
64              if (ycbcr == NULL) {
65                      TIFFError("YCbCr->RGB",
66                        "No space for YCbCr->RGB conversion state");
67                      exit(0);
68              }
69
70              TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
71              TIFFGetFieldDefaulted(tif, TIFFTAG_REFERENCEBLACKWHITE, &refBlackWhite);
72              if (TIFFYCbCrToRGBInit(ycbcr, luma, refBlackWhite) < 0)
73                   exit(0);
74
75              /* Start conversion */
76              uint32 r, g, b;
77              uint32 Y;
78              int32 Cb, Cr;
79
80              for each pixel in image
81                   TIFFYCbCrtoRGB(img->ycbcr, Y, Cb, Cr, &r, &g, &b);
82
83              /* Free state structure */
84              _TIFFfree(ycbcr);
85
86       TIFFCIELabToRGBInit() initializes the CIE L*a*b* 1976 to RGB conversion
87       state.  TIFFCIELabToRGB defined as
88
89              #define CIELABTORGB_TABLE_RANGE 1500
90
91              typedef struct {              /* CIE Lab 1976->RGB support */
92                   int  range;              /* Size of conversion table */
93                   float     rstep, gstep, bstep;
94                   float     X0, Y0, Z0;         /* Reference white point */
95                   TIFFDisplay display;
96                   float     Yr2r[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yr to r */
97                   float     Yg2g[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yg to g */
98                   float     Yb2b[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yb to b */
99              } TIFFCIELabToRGB;
100
101       display is a display device description, declared as
102
103              typedef struct {
104                   float d_mat[3][3]; /* XYZ -> luminance matrix */
105                   float d_YCR;       /* Light o/p for reference white */
106                   float d_YCG;
107                   float d_YCB;
108                   uint32 d_Vrwr;     /* Pixel values for ref. white */
109                   uint32 d_Vrwg;
110                   uint32 d_Vrwb;
111                   float d_Y0R;       /* Residual light for black pixel */
112                   float d_Y0G;
113                   float d_Y0B;
114                   float d_gammaR;    /* Gamma values for the three guns */
115                   float d_gammaG;
116                   float d_gammaB;
117              } TIFFDisplay;
118
119       For  example,  the  one  can  use  sRGB device, which has the following
120       parameters:
121
122              TIFFDisplay display_sRGB = {
123                   {       /* XYZ -> luminance matrix */
124                        {  3.2410F, -1.5374F, -0.4986F },
125                        {  -0.9692F, 1.8760F, 0.0416F },
126                        {  0.0556F, -0.2040F, 1.0570F }
127                   },
128                   100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
129                   255, 255, 255,      /* Pixel values for ref. white */
130                   1.0F, 1.0F, 1.0F,   /* Residual light o/p for black pixel */
131                   2.4F, 2.4F, 2.4F,   /* Gamma values for the three guns */
132              };
133
134       refWhite is a color temperature  of  the  reference  white.  The  TIFF‐
135       TAG_WHITEPOINT  contains  the  chromaticity  of  the white point of the
136       image from where the reference white can be calculated using  following
137       formulae:
138
139              refWhite_Y = 100.0
140              refWhite_X = whitePoint_x / whitePoint_y * refWhite_Y
141              refWhite_Z  = (1.0 - whitePoint_x - whitePoint_y) / whitePoint_y
142              * refWhite_X
143
144       The conversion itself performed in two steps: at the first one we  will
145       convert CIE L*a*b* 1976 to CIE XYZ using TIFFCIELabToXYZ() routine, and
146       at the second step we will convert CIE XYZ to RGB using TIFFXYZToRGB().
147       Look at the code sample below:
148
149              float   *whitePoint;
150              float   refWhite[3];
151
152              /* Initialize structures */
153              img->cielab = (TIFFCIELabToRGB *)
154                   _TIFFmalloc(sizeof(TIFFCIELabToRGB));
155              if (!cielab) {
156                   TIFFError("CIE L*a*b*->RGB",
157                        "No space for CIE L*a*b*->RGB conversion state.");
158                   exit(0);
159              }
160
161              TIFFGetFieldDefaulted(tif, TIFFTAG_WHITEPOINT, &whitePoint);
162              refWhite[1] = 100.0F;
163              refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
164              refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
165                         / whitePoint[1] * refWhite[1];
166              if (TIFFCIELabToRGBInit(cielab, &display_sRGB, refWhite) < 0) {
167                   TIFFError("CIE L*a*b*->RGB",
168                        "Failed to initialize CIE L*a*b*->RGB conversion state.");
169                   _TIFFfree(cielab);
170                   exit(0);
171              }
172
173              /* Now we can start to convert */
174              uint32 r, g, b;
175              uint32 L;
176              int32 a, b;
177              float X, Y, Z;
178
179              for each pixel in image
180                   TIFFCIELabToXYZ(cielab, L, a, b, &X, &Y, &Z);
181                   TIFFXYZToRGB(cielab, X, Y, Z, &r, &g, &b);
182
183              /* Don't forget to free the state structure */
184              _TIFFfree(cielab);
185

SEE ALSO

187       TIFFRGBAImage(3TIFF) libtiff(3TIFF),
188
189       Libtiff library home page: http://www.simplesystems.org/libtiff/
190
191
192
193libtiff                        December 21, 2003                  COLOR(3TIFF)
Impressum