4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
6 * Copyright (C) 2009, 2011, D. R. Commander.
7 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
10 * This file contains output colorspace conversion routines.
13 #define JPEG_INTERNALS
20 /* Private subobject */
23 struct jpeg_color_deconverter pub; /* public fields */
25 /* Private state for YCC->RGB conversion */
26 int * Cr_r_tab; /* => table for Cr to R conversion */
27 int * Cb_b_tab; /* => table for Cb to B conversion */
28 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
29 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
30 } my_color_deconverter;
32 typedef my_color_deconverter * my_cconvert_ptr;
35 /**************** YCbCr -> RGB conversion: most common case **************/
38 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
39 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
40 * The conversion equations to be implemented are therefore
41 * R = Y + 1.40200 * Cr
42 * G = Y - 0.34414 * Cb - 0.71414 * Cr
43 * B = Y + 1.77200 * Cb
44 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
45 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
47 * To avoid floating-point arithmetic, we represent the fractional constants
48 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
49 * the products by 2^16, with appropriate rounding, to get the correct answer.
50 * Notice that Y, being an integral input, does not contribute any fraction
51 * so it need not participate in the rounding.
53 * For even more speed, we avoid doing any multiplications in the inner loop
54 * by precalculating the constants times Cb and Cr for all possible values.
55 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
56 * for 12-bit samples it is still acceptable. It's not very reasonable for
57 * 16-bit samples, but if you want lossless storage you shouldn't be changing
59 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
60 * values for the G calculation are left scaled up, since we must add them
61 * together before rounding.
64 #define SCALEBITS 16 /* speediest right-shift on some machines */
65 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
66 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
69 /* Include inline routines for colorspace extensions */
77 #define RGB_RED EXT_RGB_RED
78 #define RGB_GREEN EXT_RGB_GREEN
79 #define RGB_BLUE EXT_RGB_BLUE
80 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
81 #define ycc_rgb_convert_internal ycc_extrgb_convert_internal
82 #define gray_rgb_convert_internal gray_extrgb_convert_internal
88 #undef ycc_rgb_convert_internal
89 #undef gray_rgb_convert_internal
91 #define RGB_RED EXT_RGBX_RED
92 #define RGB_GREEN EXT_RGBX_GREEN
93 #define RGB_BLUE EXT_RGBX_BLUE
95 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
96 #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
97 #define gray_rgb_convert_internal gray_extrgbx_convert_internal
104 #undef ycc_rgb_convert_internal
105 #undef gray_rgb_convert_internal
107 #define RGB_RED EXT_BGR_RED
108 #define RGB_GREEN EXT_BGR_GREEN
109 #define RGB_BLUE EXT_BGR_BLUE
110 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
111 #define ycc_rgb_convert_internal ycc_extbgr_convert_internal
112 #define gray_rgb_convert_internal gray_extbgr_convert_internal
113 #include "jdcolext.c"
118 #undef ycc_rgb_convert_internal
119 #undef gray_rgb_convert_internal
121 #define RGB_RED EXT_BGRX_RED
122 #define RGB_GREEN EXT_BGRX_GREEN
123 #define RGB_BLUE EXT_BGRX_BLUE
125 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
126 #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
127 #define gray_rgb_convert_internal gray_extbgrx_convert_internal
128 #include "jdcolext.c"
134 #undef ycc_rgb_convert_internal
135 #undef gray_rgb_convert_internal
137 #define RGB_RED EXT_XBGR_RED
138 #define RGB_GREEN EXT_XBGR_GREEN
139 #define RGB_BLUE EXT_XBGR_BLUE
141 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
142 #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
143 #define gray_rgb_convert_internal gray_extxbgr_convert_internal
144 #include "jdcolext.c"
150 #undef ycc_rgb_convert_internal
151 #undef gray_rgb_convert_internal
153 #define RGB_RED EXT_XRGB_RED
154 #define RGB_GREEN EXT_XRGB_GREEN
155 #define RGB_BLUE EXT_XRGB_BLUE
157 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
158 #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
159 #define gray_rgb_convert_internal gray_extxrgb_convert_internal
160 #include "jdcolext.c"
166 #undef ycc_rgb_convert_internal
167 #undef gray_rgb_convert_internal
171 * Initialize tables for YCC->RGB colorspace conversion.
175 build_ycc_rgb_table (j_decompress_ptr cinfo)
177 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
182 cconvert->Cr_r_tab = (int *)
183 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
184 (MAXJSAMPLE+1) * SIZEOF(int));
185 cconvert->Cb_b_tab = (int *)
186 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
187 (MAXJSAMPLE+1) * SIZEOF(int));
188 cconvert->Cr_g_tab = (INT32 *)
189 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
190 (MAXJSAMPLE+1) * SIZEOF(INT32));
191 cconvert->Cb_g_tab = (INT32 *)
192 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
193 (MAXJSAMPLE+1) * SIZEOF(INT32));
195 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
196 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
197 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
198 /* Cr=>R value is nearest int to 1.40200 * x */
199 cconvert->Cr_r_tab[i] = (int)
200 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
201 /* Cb=>B value is nearest int to 1.77200 * x */
202 cconvert->Cb_b_tab[i] = (int)
203 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
204 /* Cr=>G value is scaled-up -0.71414 * x */
205 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
206 /* Cb=>G value is scaled-up -0.34414 * x */
207 /* We also add in ONE_HALF so that need not do it in inner loop */
208 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
214 * Convert some rows of samples to the output colorspace.
218 ycc_rgb_convert (j_decompress_ptr cinfo,
219 JSAMPIMAGE input_buf, JDIMENSION input_row,
220 JSAMPARRAY output_buf, int num_rows)
222 switch (cinfo->out_color_space) {
224 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
229 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
233 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
238 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
243 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
248 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
252 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
259 /**************** Cases other than YCbCr -> RGB **************/
263 * Color conversion for no colorspace change: just copy the data,
264 * converting from separate-planes to interleaved representation.
268 null_convert (j_decompress_ptr cinfo,
269 JSAMPIMAGE input_buf, JDIMENSION input_row,
270 JSAMPARRAY output_buf, int num_rows)
272 register JSAMPROW inptr, outptr;
273 register JDIMENSION count;
274 register int num_components = cinfo->num_components;
275 JDIMENSION num_cols = cinfo->output_width;
278 while (--num_rows >= 0) {
279 for (ci = 0; ci < num_components; ci++) {
280 inptr = input_buf[ci][input_row];
281 outptr = output_buf[0] + ci;
282 for (count = num_cols; count > 0; count--) {
283 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
284 outptr += num_components;
294 * Color conversion for grayscale: just copy the data.
295 * This also works for YCbCr -> grayscale conversion, in which
296 * we just copy the Y (luminance) component and ignore chrominance.
300 grayscale_convert (j_decompress_ptr cinfo,
301 JSAMPIMAGE input_buf, JDIMENSION input_row,
302 JSAMPARRAY output_buf, int num_rows)
304 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
305 num_rows, cinfo->output_width);
310 * Convert grayscale to RGB
314 gray_rgb_convert (j_decompress_ptr cinfo,
315 JSAMPIMAGE input_buf, JDIMENSION input_row,
316 JSAMPARRAY output_buf, int num_rows)
318 switch (cinfo->out_color_space) {
320 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
325 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
329 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
334 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
339 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
344 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
348 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
356 * Adobe-style YCCK->CMYK conversion.
357 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
358 * conversion as above, while passing K (black) unchanged.
359 * We assume build_ycc_rgb_table has been called.
363 ycck_cmyk_convert (j_decompress_ptr cinfo,
364 JSAMPIMAGE input_buf, JDIMENSION input_row,
365 JSAMPARRAY output_buf, int num_rows)
367 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
368 register int y, cb, cr;
369 register JSAMPROW outptr;
370 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
371 register JDIMENSION col;
372 JDIMENSION num_cols = cinfo->output_width;
373 /* copy these pointers into registers if possible */
374 register JSAMPLE * range_limit = cinfo->sample_range_limit;
375 register int * Crrtab = cconvert->Cr_r_tab;
376 register int * Cbbtab = cconvert->Cb_b_tab;
377 register INT32 * Crgtab = cconvert->Cr_g_tab;
378 register INT32 * Cbgtab = cconvert->Cb_g_tab;
381 while (--num_rows >= 0) {
382 inptr0 = input_buf[0][input_row];
383 inptr1 = input_buf[1][input_row];
384 inptr2 = input_buf[2][input_row];
385 inptr3 = input_buf[3][input_row];
387 outptr = *output_buf++;
388 for (col = 0; col < num_cols; col++) {
389 y = GETJSAMPLE(inptr0[col]);
390 cb = GETJSAMPLE(inptr1[col]);
391 cr = GETJSAMPLE(inptr2[col]);
392 /* Range-limiting is essential due to noise introduced by DCT losses. */
393 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
394 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
395 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
397 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
398 /* K passes through unchanged */
399 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
407 * Empty method for start_pass.
411 start_pass_dcolor (j_decompress_ptr cinfo)
418 * Module initialization routine for output colorspace conversion.
422 jinit_color_deconverter (j_decompress_ptr cinfo)
424 my_cconvert_ptr cconvert;
427 cconvert = (my_cconvert_ptr)
428 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
429 SIZEOF(my_color_deconverter));
430 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
431 cconvert->pub.start_pass = start_pass_dcolor;
433 /* Make sure num_components agrees with jpeg_color_space */
434 switch (cinfo->jpeg_color_space) {
436 if (cinfo->num_components != 1)
437 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
442 if (cinfo->num_components != 3)
443 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
448 if (cinfo->num_components != 4)
449 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
452 default: /* JCS_UNKNOWN can be anything */
453 if (cinfo->num_components < 1)
454 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
458 /* Set out_color_components and conversion method based on requested space.
459 * Also clear the component_needed flags for any unused components,
460 * so that earlier pipeline stages can avoid useless computation.
463 switch (cinfo->out_color_space) {
465 cinfo->out_color_components = 1;
466 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
467 cinfo->jpeg_color_space == JCS_YCbCr) {
468 cconvert->pub.color_convert = grayscale_convert;
469 /* For color->grayscale conversion, only the Y (0) component is needed */
470 for (ci = 1; ci < cinfo->num_components; ci++)
471 cinfo->comp_info[ci].component_needed = FALSE;
473 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
487 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
488 if (cinfo->jpeg_color_space == JCS_YCbCr) {
489 if (jsimd_can_ycc_rgb())
490 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
492 cconvert->pub.color_convert = ycc_rgb_convert;
493 build_ycc_rgb_table(cinfo);
495 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
496 cconvert->pub.color_convert = gray_rgb_convert;
497 } else if (cinfo->jpeg_color_space == cinfo->out_color_space &&
498 rgb_pixelsize[cinfo->out_color_space] == 3) {
499 cconvert->pub.color_convert = null_convert;
501 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
505 cinfo->out_color_components = 4;
506 if (cinfo->jpeg_color_space == JCS_YCCK) {
507 cconvert->pub.color_convert = ycck_cmyk_convert;
508 build_ycc_rgb_table(cinfo);
509 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
510 cconvert->pub.color_convert = null_convert;
512 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
516 /* Permit null conversion to same output space */
517 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
518 cinfo->out_color_components = cinfo->num_components;
519 cconvert->pub.color_convert = null_convert;
520 } else /* unsupported non-null conversion */
521 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
525 if (cinfo->quantize_colors)
526 cinfo->output_components = 1; /* single colormapped output component */
528 cinfo->output_components = cinfo->out_color_components;