LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_color.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
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.
12 *
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.
16 *
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
22 * OF THIS SOFTWARE.
23 */
24
25/*
26 * CIE L*a*b* to CIE XYZ and CIE XYZ to RGB conversion routines are taken
27 * from the VIPS library (http://www.vips.ecs.soton.ac.uk) with
28 * the permission of John Cupitt, the VIPS author.
29 */
30
31/*
32 * TIFF Library.
33 *
34 * Color space conversion routines.
35 */
36
43#include "tiffiop.h"
44#include <math.h>
45
46/*
47 * Convert color value from the CIE L*a*b* 1976 space to CIE XYZ.
48 */
50void TIFFCIELabToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a, int32_t b,
51 float *X, float *Y, float *Z)
52{
53 /*
54 * Keep the historical signed a * 256 and b * 256 scaling. Out-of-range
55 * callers may trigger UBSan signed-overflow reports here, but this
56 * per-pixel path intentionally does not add extra handling; see !926 for
57 * details.
58 */
59 TIFFCIELab16ToXYZ(cielab, l * 257U, a * 256, b * 256, X, Y, Z);
60}
61
62/*
63 * For CIELab encoded in 16 bits, L is an unsigned integer range [0,65535].
64 * The a* and b* components are signed integers range [-32768,32767]. The 16
65 * bit chrominance values are encoded as 256 times the 1976 CIE a* and b*
66 * values
67 */
68void TIFFCIELab16ToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a,
69 int32_t b, float *X, float *Y, float *Z)
70{
71 float L = (float)l * 100.0f / 65535.0f;
72 float cby, tmp;
73
74 if (L < 8.856f)
75 {
76 *Y = (L * cielab->Y0) / 903.292f;
77 cby = 7.787f * (*Y / cielab->Y0) + 16.0f / 116.0f;
78 }
79 else
80 {
81 cby = (L + 16.0f) / 116.0f;
82 *Y = cielab->Y0 * cby * cby * cby;
83 }
84
85 tmp = (float)a / 256.0f / 500.0f + cby;
86 if (tmp < 0.2069f)
87 *X = cielab->X0 * (tmp - 0.13793f) / 7.787f;
88 else
89 *X = cielab->X0 * tmp * tmp * tmp;
90
91 tmp = cby - (float)b / 256.0f / 200.0f;
92 if (tmp < 0.2069f)
93 *Z = cielab->Z0 * (tmp - 0.13793f) / 7.787f;
94 else
95 *Z = cielab->Z0 * tmp * tmp * tmp;
96}
97
108#define RINT(R) ((uint32_t)((R) > 0 ? ((R) + 0.5f) : ((R) - 0.5f)))
109/*
110 * Convert color value from the XYZ space to RGB.
111 */
112void TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z,
113 uint32_t *r, uint32_t *g, uint32_t *b)
114{
115 size_t i;
116 float Yr, Yg, Yb;
117 float *matrix = &cielab->display.d_mat[0][0];
118
119 /* Multiply through the matrix to get luminosity values. */
120 Yr = matrix[0] * X + matrix[1] * Y + matrix[2] * Z;
121 Yg = matrix[3] * X + matrix[4] * Y + matrix[5] * Z;
122 Yb = matrix[6] * X + matrix[7] * Y + matrix[8] * Z;
123
124 /* Clip input */
125 Yr = TIFFmax(Yr, cielab->display.d_Y0R);
126 Yg = TIFFmax(Yg, cielab->display.d_Y0G);
127 Yb = TIFFmax(Yb, cielab->display.d_Y0B);
128
129 /* Avoid overflow in case of wrong input values */
130 Yr = TIFFmin(Yr, cielab->display.d_YCR);
131 Yg = TIFFmin(Yg, cielab->display.d_YCG);
132 Yb = TIFFmin(Yb, cielab->display.d_YCB);
133
134 /* Turn luminosity to colour value. */
135 i = (size_t)((Yr - cielab->display.d_Y0R) / cielab->rstep);
136 i = TIFFmin((size_t)cielab->range, i);
137 *r = RINT(cielab->Yr2r[i]);
138
139 i = (size_t)((Yg - cielab->display.d_Y0G) / cielab->gstep);
140 i = TIFFmin((size_t)cielab->range, i);
141 *g = RINT(cielab->Yg2g[i]);
142
143 i = (size_t)((Yb - cielab->display.d_Y0B) / cielab->bstep);
144 i = TIFFmin((size_t)cielab->range, i);
145 *b = RINT(cielab->Yb2b[i]);
146
147 /* Clip output. */
148 *r = TIFFmin(*r, cielab->display.d_Vrwr);
149 *g = TIFFmin(*g, cielab->display.d_Vrwg);
150 *b = TIFFmin(*b, cielab->display.d_Vrwb);
151}
152#undef RINT
153
154/*
155 * Allocate conversion state structures and make look_up tables for
156 * the Yr,Yb,Yg <=> r,g,b conversions.
157 *
158 * Note: gstep and bstep are currently computed from the red display values
159 * (d_YCR - d_Y0R), the same as rstep, rather than from the green and blue
160 * values. This is a known discrepancy, recorded for a separate code fix.
161 */
163 float *refWhite)
164{
165 size_t i;
166 double dfGamma;
167
169
170 _TIFFmemcpy(&cielab->display, display, sizeof(TIFFDisplay));
171
172 /* Red */
173 dfGamma = 1.0 / (double)cielab->display.d_gammaR;
174 cielab->rstep =
175 (cielab->display.d_YCR - cielab->display.d_Y0R) / (float)cielab->range;
176 for (i = 0; i <= (size_t)cielab->range; i++)
177 {
178 cielab->Yr2r[i] = (float)cielab->display.d_Vrwr *
179 ((float)pow((double)i / cielab->range, dfGamma));
180 }
181
182 /* Green */
183 dfGamma = 1.0 / (double)cielab->display.d_gammaG;
184 cielab->gstep =
185 (cielab->display.d_YCR - cielab->display.d_Y0R) / (float)cielab->range;
186 for (i = 0; i <= (size_t)cielab->range; i++)
187 {
188 cielab->Yg2g[i] = (float)cielab->display.d_Vrwg *
189 ((float)pow((double)i / cielab->range, dfGamma));
190 }
191
192 /* Blue */
193 dfGamma = 1.0 / (double)cielab->display.d_gammaB;
194 cielab->bstep =
195 (cielab->display.d_YCR - cielab->display.d_Y0R) / (float)cielab->range;
196 for (i = 0; i <= (size_t)cielab->range; i++)
197 {
198 cielab->Yb2b[i] = (float)cielab->display.d_Vrwb *
199 ((float)pow((double)i / cielab->range, dfGamma));
200 }
201
202 /* Init reference white point */
203 cielab->X0 = refWhite[0];
204 cielab->Y0 = refWhite[1];
205 cielab->Z0 = refWhite[2];
206
207 return 0;
208}
209
210/*
211 * Convert color value from the YCbCr space to RGB.
212 * The colorspace conversion algorithm comes from the IJG v5a code;
213 * see below for more information on how it works.
214 */
222#define SHIFT 16
233#define FIX(x) ((int32_t)((double)(x) * (1L << SHIFT) + 0.5))
241#define ONE_HALF ((int32_t)(1 << (SHIFT - 1)))
258#define Code2V(c, RB, RW, CR) \
259 (((float)((c) - (int32_t)(RB)) * (float)(CR)) / \
260 ((!TIFF_FLOAT_EQ((RW), (RB))) ? ((RW) - (RB)) : 1.0f))
261/* !((f)>=(min)) written that way to deal with NaN */
274#define CLAMP(f, min, max) \
275 ((!((f) >= (min))) ? (min) : (f) > (max) ? (max) : (f))
287#define HICLAMP(f, max) ((f) > (max) ? (max) : (f))
288
289void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *ycbcr, uint32_t Y, int32_t Cb, int32_t Cr,
290 uint32_t *r, uint32_t *g, uint32_t *b)
291{
292 int32_t i;
293
294 /* XXX: Only 8-bit YCbCr input supported for now */
295 Y = HICLAMP(Y, 255);
296 Cb = CLAMP(Cb, 0, 255);
297 Cr = CLAMP(Cr, 0, 255);
298
299 i = ycbcr->Y_tab[Y] + ycbcr->Cr_r_tab[Cr];
300 *r = (uint32_t)CLAMP(i, 0, 255);
301 i = ycbcr->Y_tab[Y] +
302 ((ycbcr->Cb_g_tab[Cb] + ycbcr->Cr_g_tab[Cr]) >> SHIFT);
303 *g = (uint32_t)CLAMP(i, 0, 255);
304 i = ycbcr->Y_tab[Y] + ycbcr->Cb_b_tab[Cb];
305 *b = (uint32_t)CLAMP(i, 0, 255);
306}
307
308/* Clamp function for sanitization purposes. Normally clamping should not */
309/* occur for well behaved chroma and refBlackWhite coefficients */
323static float CLAMPw(float v, float vmin, float vmax)
324{
325 if (v < vmin)
326 {
327 /* printf("%f clamped to %f\n", v, vmin); */
328 return vmin;
329 }
330 if (v > vmax)
331 {
332 /* printf("%f clamped to %f\n", v, vmax); */
333 return vmax;
334 }
335 return v;
336}
337
338/*
339 * Initialize the YCbCr->RGB conversion tables. The conversion
340 * is done according to the 6.0 spec:
341 *
342 * R = Y + Cr*(2 - 2*LumaRed)
343 * B = Y + Cb*(2 - 2*LumaBlue)
344 * G = Y
345 * - LumaBlue*Cb*(2-2*LumaBlue)/LumaGreen
346 * - LumaRed*Cr*(2-2*LumaRed)/LumaGreen
347 *
348 * To avoid floating point arithmetic the fractional constants that
349 * come out of the equations are represented as fixed point values
350 * in the range 0...2^16. We also eliminate multiplications by
351 * pre-calculating possible values indexed by Cb and Cr (this code
352 * assumes conversion is being done for 8-bit samples).
353 */
354int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *ycbcr, float *luma, float *refBlackWhite)
355{
356 TIFFRGBValue *clamptab;
357 int i;
358
359#define LumaRed luma[0]
360#define LumaGreen luma[1]
361#define LumaBlue luma[2]
362
363 clamptab =
364 (uint8_t *)ycbcr + TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long));
365 _TIFFmemset(clamptab, 0, 256); /* v < 0 => 0 */
366 ycbcr->clamptab = (clamptab += 256);
367 for (i = 0; i < 256; i++)
368 clamptab[i] = (TIFFRGBValue)i;
369 _TIFFmemset(clamptab + 256, 255, 2 * 256); /* v > 255 => 255 */
370 ycbcr->Cr_r_tab = (int *)(clamptab + 3 * 256);
371 ycbcr->Cb_b_tab = ycbcr->Cr_r_tab + 256;
372 ycbcr->Cr_g_tab = (int32_t *)(ycbcr->Cb_b_tab + 256);
373 ycbcr->Cb_g_tab = ycbcr->Cr_g_tab + 256;
374 ycbcr->Y_tab = ycbcr->Cb_g_tab + 256;
375
376 {
377 float f1 = 2 - 2 * LumaRed;
378 int32_t D1 = FIX(CLAMP(f1, 0.0f, 2.0f));
379 float f2 = LumaRed * f1 / LumaGreen;
380 int32_t D2 = -FIX(CLAMP(f2, 0.0f, 2.0f));
381 float f3 = 2 - 2 * LumaBlue;
382 int32_t D3 = FIX(CLAMP(f3, 0.0f, 2.0f));
383 float f4 = LumaBlue * f3 / LumaGreen;
384 int32_t D4 = -FIX(CLAMP(f4, 0.0f, 2.0f));
385 int x;
386
387#undef LumaBlue
388#undef LumaGreen
389#undef LumaRed
390
391 /*
392 * i is the actual input pixel value in the range 0..255
393 * Cb and Cr values are in the range -128..127 (actually
394 * they are in a range defined by the ReferenceBlackWhite
395 * tag) so there is some range shifting to do here when
396 * constructing tables indexed by the raw pixel data.
397 */
398 for (i = 0, x = -128; i < 256; i++, x++)
399 {
400 int32_t Cr = (int32_t)CLAMPw(Code2V(x, refBlackWhite[4] - 128.0f,
401 refBlackWhite[5] - 128.0f, 127),
402 -128.0f * 32, 128.0f * 32);
403 int32_t Cb = (int32_t)CLAMPw(Code2V(x, refBlackWhite[2] - 128.0f,
404 refBlackWhite[3] - 128.0f, 127),
405 -128.0f * 32, 128.0f * 32);
406
407 ycbcr->Cr_r_tab[i] = (int32_t)((D1 * Cr + ONE_HALF) >> SHIFT);
408 ycbcr->Cb_b_tab[i] = (int32_t)((D3 * Cb + ONE_HALF) >> SHIFT);
409 ycbcr->Cr_g_tab[i] = D2 * Cr;
410 ycbcr->Cb_g_tab[i] = D4 * Cb + ONE_HALF;
411 ycbcr->Y_tab[i] = (int32_t)CLAMPw(
412 Code2V(x + 128, refBlackWhite[0], refBlackWhite[1], 255),
413 -128.0f * 32, 128.0f * 32);
414 }
415 }
416
417 return 0;
418}
419#undef HICLAMP
420#undef CLAMP
421#undef Code2V
422#undef SHIFT
423#undef ONE_HALF
424#undef FIX
425
void TIFFCIELab16ToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a, int32_t b, float *X, float *Y, float *Z)
Convert 16-bit TIFF CIE L*a*b* samples to XYZ.
Definition tif_color.c:68
static float CLAMPw(float v, float vmin, float vmax)
Clamp a finite coefficient to an inclusive interval.
Definition tif_color.c:323
#define LumaRed
Read the red coefficient from the local luma array.
#define LumaBlue
Read the blue coefficient from the local luma array.
#define FIX(x)
Convert a bounded coefficient to fixed-point form.
Definition tif_color.c:233
#define RINT(R)
Round a nonnegative table value to an unsigned channel.
Definition tif_color.c:108
#define Code2V(c, RB, RW, CR)
Scale a code between reference black and white endpoints.
Definition tif_color.c:258
#define CLAMP(f, min, max)
Clamp a value and map NaN to the lower bound.
Definition tif_color.c:274
#define ONE_HALF
Represent a half-unit rounding bias in fixed-point form.
Definition tif_color.c:241
#define SHIFT
Select sixteen fractional bits for YCbCr contributions.
Definition tif_color.c:222
#define LumaGreen
Read the green coefficient from the local luma array.
#define HICLAMP(f, max)
Apply only an upper bound.
Definition tif_color.c:287
#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
Suppress unsigned-overflow instrumentation on supported Clang functions.
Definition tiffiop.h:1484
#define TIFFroundup_32(x, y)
Round a 32-bit value up to a multiple using the guarded quotient.
Definition tiffiop.h:1102
#define TIFFmax(A, B)
Select the first operand if it is greater, otherwise the second.
Definition tiffiop.h:1213
#define TIFFmin(A, B)
Select the first operand if it is smaller, otherwise the second.
Definition tiffiop.h:1226
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW void TIFFCIELabToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a, int32_t b, float *X, float *Y, float *Z)
Convert 8-bit TIFF CIE L*a*b* samples to XYZ.
Definition tif_color.c:50
void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *ycbcr, uint32_t Y, int32_t Cb, int32_t Cr, uint32_t *r, uint32_t *g, uint32_t *b)
Convert an 8-bit YCbCr sample triplet to clipped RGB.
Definition tif_color.c:289
int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *ycbcr, float *luma, float *refBlackWhite)
Initialise caller-allocated 8-bit YCbCr conversion tables.
Definition tif_color.c:354
int TIFFCIELabToRGBInit(TIFFCIELabToRGB *cielab, const TIFFDisplay *display, float *refWhite)
Initialise caller-owned CIE Lab conversion tables and reference white.
Definition tif_color.c:162
void TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z, uint32_t *r, uint32_t *g, uint32_t *b)
Convert XYZ tristimulus values to display RGB channels.
Definition tif_color.c:112
unsigned char TIFFRGBValue
Represent an 8-bit RGB channel value.
Definition tiffio.h:276
#define CIELABTORGB_TABLE_RANGE
Define the largest CIE L*a*b*-to-RGB lookup-table index.
Definition tiffio.h:370
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
Hold CIE L*a*b*-to-RGB conversion parameters and tables.
Definition tiffio.h:361
int range
Largest table index; initialised to CIELABTORGB_TABLE_RANGE.
Definition tiffio.h:363
float Yb2b[CIELABTORGB_TABLE_RANGE+1]
Blue luminance-to-code table, including both endpoints.
Definition tiffio.h:398
float bstep
Blue luminance increment per lookup-table interval. Currently initialised from the red-channel d_YCR/...
Definition tiffio.h:384
float X0
Reference-white X coordinate.
Definition tiffio.h:386
float Z0
Reference-white Z coordinate.
Definition tiffio.h:390
TIFFDisplay display
Inline copy of the display description.
Definition tiffio.h:392
float rstep
Red luminance increment per lookup-table interval.
Definition tiffio.h:372
float gstep
Green luminance increment per lookup-table interval. Currently initialised from the red-channel d_YCR...
Definition tiffio.h:378
float Yr2r[CIELABTORGB_TABLE_RANGE+1]
Red luminance-to-code table, including both endpoints.
Definition tiffio.h:394
float Yg2g[CIELABTORGB_TABLE_RANGE+1]
Green luminance-to-code table, including both endpoints.
Definition tiffio.h:396
float Y0
Reference-white Y coordinate.
Definition tiffio.h:388
Describe the matrix, luminance, and gamma of an RGB display.
Definition tiffio.h:287
float d_mat[3][3]
3 by 3 matrix mapping XYZ to red, green, and blue luminance.
Definition tiffio.h:289
float d_gammaG
Green-channel gamma.
Definition tiffio.h:311
float d_YCB
Blue-channel reference-white luminance.
Definition tiffio.h:295
uint32_t d_Vrwr
Red-channel code value at reference white.
Definition tiffio.h:297
float d_Y0R
Red-channel residual luminance at black.
Definition tiffio.h:303
float d_gammaR
Red-channel gamma.
Definition tiffio.h:309
float d_YCG
Green-channel reference-white luminance.
Definition tiffio.h:293
float d_YCR
Red-channel reference-white luminance.
Definition tiffio.h:291
float d_Y0G
Green-channel residual luminance at black.
Definition tiffio.h:305
float d_Y0B
Blue-channel residual luminance at black.
Definition tiffio.h:307
uint32_t d_Vrwg
Green-channel code value at reference white.
Definition tiffio.h:299
float d_gammaB
Blue-channel gamma.
Definition tiffio.h:313
uint32_t d_Vrwb
Blue-channel code value at reference white.
Definition tiffio.h:301
Hold lookup-table pointers for 8-bit YCbCr-to-RGB conversion.
Definition tiffio.h:334
int * Cr_r_tab
256-entry table of Cr contributions to red.
Definition tiffio.h:341
int32_t * Cb_g_tab
256-entry fixed-point table of Cb contributions to green.
Definition tiffio.h:347
int32_t * Y_tab
256-entry table of scaled luminance contributions.
Definition tiffio.h:349
TIFFRGBValue * clamptab
Biased table pointer for clamping channel results to 0 through 255.
Definition tiffio.h:339
int * Cb_b_tab
256-entry table of Cb contributions to blue.
Definition tiffio.h:343
int32_t * Cr_g_tab
256-entry fixed-point table of Cr contributions to green.
Definition tiffio.h:345
Declare private library state, callbacks, and implementation helpers.