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