Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/doc/functions/TIFFcolor.rst

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