7 * Copyright (C) 1991-1995, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
11 * For conditions of distribution and use, see the accompanying README file.
15 * This file contains output colorspace conversion routines.
21 #define JPEG_INTERNALS
25 #include "radiant_jpeglib.h"
31 /* Private subobject */
37 struct jpeg_color_deconverter pub; /* public fields */
41 /* Private state for YCC->RGB conversion */
43 int * Cr_r_tab; /* => table for Cr to R conversion */
45 int * Cb_b_tab; /* => table for Cb to B conversion */
47 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
49 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
51 } my_color_deconverter;
55 typedef my_color_deconverter * my_cconvert_ptr;
61 /**************** YCbCr -> RGB conversion: most common case **************/
67 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
69 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
71 * The conversion equations to be implemented are therefore
73 * R = Y + 1.40200 * Cr
75 * G = Y - 0.34414 * Cb - 0.71414 * Cr
77 * B = Y + 1.77200 * Cb
79 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
81 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
85 * To avoid floating-point arithmetic, we represent the fractional constants
87 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
89 * the products by 2^16, with appropriate rounding, to get the correct answer.
91 * Notice that Y, being an integral input, does not contribute any fraction
93 * so it need not participate in the rounding.
97 * For even more speed, we avoid doing any multiplications in the inner loop
99 * by precalculating the constants times Cb and Cr for all possible values.
101 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
103 * for 12-bit samples it is still acceptable. It's not very reasonable for
105 * 16-bit samples, but if you want lossless storage you shouldn't be changing
109 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
111 * values for the G calculation are left scaled up, since we must add them
113 * together before rounding.
119 #define SCALEBITS 16 /* speediest right-shift on some machines */
121 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
123 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
131 * Initialize tables for YCC->RGB colorspace conversion.
139 build_ycc_rgb_table (j_decompress_ptr cinfo)
143 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
153 cconvert->Cr_r_tab = (int *)
155 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
157 (MAXJSAMPLE+1) * SIZEOF(int));
159 cconvert->Cb_b_tab = (int *)
161 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
163 (MAXJSAMPLE+1) * SIZEOF(int));
165 cconvert->Cr_g_tab = (INT32 *)
167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
169 (MAXJSAMPLE+1) * SIZEOF(INT32));
171 cconvert->Cb_g_tab = (INT32 *)
173 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
175 (MAXJSAMPLE+1) * SIZEOF(INT32));
179 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
181 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
183 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
185 /* Cr=>R value is nearest int to 1.40200 * x */
187 cconvert->Cr_r_tab[i] = (int)
189 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
191 /* Cb=>B value is nearest int to 1.77200 * x */
193 cconvert->Cb_b_tab[i] = (int)
195 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
197 /* Cr=>G value is scaled-up -0.71414 * x */
199 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
201 /* Cb=>G value is scaled-up -0.34414 * x */
203 /* We also add in ONE_HALF so that need not do it in inner loop */
205 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
217 * Convert some rows of samples to the output colorspace.
221 * Note that we change from noninterleaved, one-plane-per-component format
223 * to interleaved-pixel format. The output buffer is therefore three times
225 * as wide as the input buffer.
227 * A starting row offset is provided only for the input buffer. The caller
229 * can easily adjust the passed output_buf value to accommodate any row
231 * offset required on that side.
239 ycc_rgb_convert (j_decompress_ptr cinfo,
241 JSAMPIMAGE input_buf, JDIMENSION input_row,
243 JSAMPARRAY output_buf, int num_rows)
247 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
249 register int y, cb, cr;
251 register JSAMPROW outptr;
253 register JSAMPROW inptr0, inptr1, inptr2;
255 register JDIMENSION col;
257 JDIMENSION num_cols = cinfo->output_width;
259 /* copy these pointers into registers if possible */
261 register JSAMPLE * range_limit = cinfo->sample_range_limit;
263 register int * Crrtab = cconvert->Cr_r_tab;
265 register int * Cbbtab = cconvert->Cb_b_tab;
267 register INT32 * Crgtab = cconvert->Cr_g_tab;
269 register INT32 * Cbgtab = cconvert->Cb_g_tab;
275 while (--num_rows >= 0) {
277 inptr0 = input_buf[0][input_row];
279 inptr1 = input_buf[1][input_row];
281 inptr2 = input_buf[2][input_row];
285 outptr = *output_buf++;
287 for (col = 0; col < num_cols; col++) {
289 y = GETJSAMPLE(inptr0[col]);
291 cb = GETJSAMPLE(inptr1[col]);
293 cr = GETJSAMPLE(inptr2[col]);
295 /* Range-limiting is essential due to noise introduced by DCT losses. */
297 outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
299 outptr[RGB_GREEN] = range_limit[y +
301 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
305 outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
307 outptr += RGB_PIXELSIZE;
319 /**************** Cases other than YCbCr -> RGB **************/
327 * Color conversion for no colorspace change: just copy the data,
329 * converting from separate-planes to interleaved representation.
337 null_convert (j_decompress_ptr cinfo,
339 JSAMPIMAGE input_buf, JDIMENSION input_row,
341 JSAMPARRAY output_buf, int num_rows)
345 register JSAMPROW inptr, outptr;
347 register JDIMENSION count;
349 register int num_components = cinfo->num_components;
351 JDIMENSION num_cols = cinfo->output_width;
357 while (--num_rows >= 0) {
359 for (ci = 0; ci < num_components; ci++) {
361 inptr = input_buf[ci][input_row];
363 outptr = output_buf[0] + ci;
365 for (count = num_cols; count > 0; count--) {
367 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
369 outptr += num_components;
389 * Color conversion for grayscale: just copy the data.
391 * This also works for YCbCr -> grayscale conversion, in which
393 * we just copy the Y (luminance) component and ignore chrominance.
401 grayscale_convert (j_decompress_ptr cinfo,
403 JSAMPIMAGE input_buf, JDIMENSION input_row,
405 JSAMPARRAY output_buf, int num_rows)
409 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
411 num_rows, cinfo->output_width);
421 * Adobe-style YCCK->CMYK conversion.
423 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
425 * conversion as above, while passing K (black) unchanged.
427 * We assume build_ycc_rgb_table has been called.
435 ycck_cmyk_convert (j_decompress_ptr cinfo,
437 JSAMPIMAGE input_buf, JDIMENSION input_row,
439 JSAMPARRAY output_buf, int num_rows)
443 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
445 register int y, cb, cr;
447 register JSAMPROW outptr;
449 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
451 register JDIMENSION col;
453 JDIMENSION num_cols = cinfo->output_width;
455 /* copy these pointers into registers if possible */
457 register JSAMPLE * range_limit = cinfo->sample_range_limit;
459 register int * Crrtab = cconvert->Cr_r_tab;
461 register int * Cbbtab = cconvert->Cb_b_tab;
463 register INT32 * Crgtab = cconvert->Cr_g_tab;
465 register INT32 * Cbgtab = cconvert->Cb_g_tab;
471 while (--num_rows >= 0) {
473 inptr0 = input_buf[0][input_row];
475 inptr1 = input_buf[1][input_row];
477 inptr2 = input_buf[2][input_row];
479 inptr3 = input_buf[3][input_row];
483 outptr = *output_buf++;
485 for (col = 0; col < num_cols; col++) {
487 y = GETJSAMPLE(inptr0[col]);
489 cb = GETJSAMPLE(inptr1[col]);
491 cr = GETJSAMPLE(inptr2[col]);
493 /* Range-limiting is essential due to noise introduced by DCT losses. */
495 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
497 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
499 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
503 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
505 /* K passes through unchanged */
507 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
523 * Empty method for start_pass.
531 start_pass_dcolor (j_decompress_ptr cinfo)
545 * Module initialization routine for output colorspace conversion.
553 jinit_color_deconverter (j_decompress_ptr cinfo)
557 my_cconvert_ptr cconvert;
563 cconvert = (my_cconvert_ptr)
565 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
567 SIZEOF(my_color_deconverter));
569 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
571 cconvert->pub.start_pass = start_pass_dcolor;
575 /* Make sure num_components agrees with jpeg_color_space */
577 switch (cinfo->jpeg_color_space) {
581 if (cinfo->num_components != 1)
583 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
593 if (cinfo->num_components != 3)
595 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
605 if (cinfo->num_components != 4)
607 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
613 default: /* JCS_UNKNOWN can be anything */
615 if (cinfo->num_components < 1)
617 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
625 /* Set out_color_components and conversion method based on requested space.
627 * Also clear the component_needed flags for any unused components,
629 * so that earlier pipeline stages can avoid useless computation.
635 switch (cinfo->out_color_space) {
639 cinfo->out_color_components = 1;
641 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
643 cinfo->jpeg_color_space == JCS_YCbCr) {
645 cconvert->pub.color_convert = grayscale_convert;
647 /* For color->grayscale conversion, only the Y (0) component is needed */
649 for (ci = 1; ci < cinfo->num_components; ci++)
651 cinfo->comp_info[ci].component_needed = FALSE;
655 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
663 cinfo->out_color_components = RGB_PIXELSIZE;
665 if (cinfo->jpeg_color_space == JCS_YCbCr) {
667 cconvert->pub.color_convert = ycc_rgb_convert;
669 build_ycc_rgb_table(cinfo);
671 } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
673 cconvert->pub.color_convert = null_convert;
677 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685 cinfo->out_color_components = 4;
687 if (cinfo->jpeg_color_space == JCS_YCCK) {
689 cconvert->pub.color_convert = ycck_cmyk_convert;
691 build_ycc_rgb_table(cinfo);
693 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
695 cconvert->pub.color_convert = null_convert;
699 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
707 /* Permit null conversion to same output space */
709 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
711 cinfo->out_color_components = cinfo->num_components;
713 cconvert->pub.color_convert = null_convert;
715 } else /* unsupported non-null conversion */
717 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
725 if (cinfo->quantize_colors)
727 cinfo->output_components = 1; /* single colormapped output component */
731 cinfo->output_components = cinfo->out_color_components;