4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1991-1997, Thomas G. Lane.
6 * Modified 2011 by Guido Vollbeding.
7 * libjpeg-turbo Modifications:
8 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 * Copyright (C) 2009, 2011-2012, D. R. Commander.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains output colorspace conversion routines.
15 #define JPEG_INTERNALS
22 /* Private subobject */
25 struct jpeg_color_deconverter pub; /* public fields */
27 /* Private state for YCC->RGB conversion */
28 int * Cr_r_tab; /* => table for Cr to R conversion */
29 int * Cb_b_tab; /* => table for Cb to B conversion */
30 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
31 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
33 /* Private state for RGB->Y conversion */
34 INT32 * rgb_y_tab; /* => table for RGB to Y conversion */
35 } my_color_deconverter;
37 typedef my_color_deconverter * my_cconvert_ptr;
40 /**************** YCbCr -> RGB conversion: most common case **************/
41 /**************** RGB -> Y conversion: less common case **************/
44 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
45 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
46 * The conversion equations to be implemented are therefore
48 * R = Y + 1.40200 * Cr
49 * G = Y - 0.34414 * Cb - 0.71414 * Cr
50 * B = Y + 1.77200 * Cb
52 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
54 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
55 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
57 * To avoid floating-point arithmetic, we represent the fractional constants
58 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
59 * the products by 2^16, with appropriate rounding, to get the correct answer.
60 * Notice that Y, being an integral input, does not contribute any fraction
61 * so it need not participate in the rounding.
63 * For even more speed, we avoid doing any multiplications in the inner loop
64 * by precalculating the constants times Cb and Cr for all possible values.
65 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
66 * for 12-bit samples it is still acceptable. It's not very reasonable for
67 * 16-bit samples, but if you want lossless storage you shouldn't be changing
69 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
70 * values for the G calculation are left scaled up, since we must add them
71 * together before rounding.
74 #define SCALEBITS 16 /* speediest right-shift on some machines */
75 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
76 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
78 /* We allocate one big table for RGB->Y conversion and divide it up into
79 * three parts, instead of doing three alloc_small requests. This lets us
80 * use a single table base address, which can be held in a register in the
81 * inner loops on many machines (more than can hold all three addresses,
85 #define R_Y_OFF 0 /* offset to R => Y section */
86 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
87 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
88 #define TABLE_SIZE (3*(MAXJSAMPLE+1))
91 /* Include inline routines for colorspace extensions */
99 #define RGB_RED EXT_RGB_RED
100 #define RGB_GREEN EXT_RGB_GREEN
101 #define RGB_BLUE EXT_RGB_BLUE
102 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
103 #define ycc_rgb_convert_internal ycc_extrgb_convert_internal
104 #define gray_rgb_convert_internal gray_extrgb_convert_internal
105 #define rgb_rgb_convert_internal rgb_extrgb_convert_internal
106 #include "jdcolext.c"
111 #undef ycc_rgb_convert_internal
112 #undef gray_rgb_convert_internal
113 #undef rgb_rgb_convert_internal
115 #define RGB_RED EXT_RGBX_RED
116 #define RGB_GREEN EXT_RGBX_GREEN
117 #define RGB_BLUE EXT_RGBX_BLUE
119 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
120 #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
121 #define gray_rgb_convert_internal gray_extrgbx_convert_internal
122 #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
123 #include "jdcolext.c"
129 #undef ycc_rgb_convert_internal
130 #undef gray_rgb_convert_internal
131 #undef rgb_rgb_convert_internal
133 #define RGB_RED EXT_BGR_RED
134 #define RGB_GREEN EXT_BGR_GREEN
135 #define RGB_BLUE EXT_BGR_BLUE
136 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
137 #define ycc_rgb_convert_internal ycc_extbgr_convert_internal
138 #define gray_rgb_convert_internal gray_extbgr_convert_internal
139 #define rgb_rgb_convert_internal rgb_extbgr_convert_internal
140 #include "jdcolext.c"
145 #undef ycc_rgb_convert_internal
146 #undef gray_rgb_convert_internal
147 #undef rgb_rgb_convert_internal
149 #define RGB_RED EXT_BGRX_RED
150 #define RGB_GREEN EXT_BGRX_GREEN
151 #define RGB_BLUE EXT_BGRX_BLUE
153 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
154 #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
155 #define gray_rgb_convert_internal gray_extbgrx_convert_internal
156 #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
157 #include "jdcolext.c"
163 #undef ycc_rgb_convert_internal
164 #undef gray_rgb_convert_internal
165 #undef rgb_rgb_convert_internal
167 #define RGB_RED EXT_XBGR_RED
168 #define RGB_GREEN EXT_XBGR_GREEN
169 #define RGB_BLUE EXT_XBGR_BLUE
171 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
172 #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
173 #define gray_rgb_convert_internal gray_extxbgr_convert_internal
174 #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
175 #include "jdcolext.c"
181 #undef ycc_rgb_convert_internal
182 #undef gray_rgb_convert_internal
183 #undef rgb_rgb_convert_internal
185 #define RGB_RED EXT_XRGB_RED
186 #define RGB_GREEN EXT_XRGB_GREEN
187 #define RGB_BLUE EXT_XRGB_BLUE
189 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
190 #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
191 #define gray_rgb_convert_internal gray_extxrgb_convert_internal
192 #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
193 #include "jdcolext.c"
199 #undef ycc_rgb_convert_internal
200 #undef gray_rgb_convert_internal
201 #undef rgb_rgb_convert_internal
205 * Initialize tables for YCC->RGB colorspace conversion.
209 build_ycc_rgb_table (j_decompress_ptr cinfo)
211 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
216 cconvert->Cr_r_tab = (int *)
217 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
218 (MAXJSAMPLE+1) * SIZEOF(int));
219 cconvert->Cb_b_tab = (int *)
220 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
221 (MAXJSAMPLE+1) * SIZEOF(int));
222 cconvert->Cr_g_tab = (INT32 *)
223 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
224 (MAXJSAMPLE+1) * SIZEOF(INT32));
225 cconvert->Cb_g_tab = (INT32 *)
226 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
227 (MAXJSAMPLE+1) * SIZEOF(INT32));
229 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
230 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
231 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
232 /* Cr=>R value is nearest int to 1.40200 * x */
233 cconvert->Cr_r_tab[i] = (int)
234 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
235 /* Cb=>B value is nearest int to 1.77200 * x */
236 cconvert->Cb_b_tab[i] = (int)
237 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
238 /* Cr=>G value is scaled-up -0.71414 * x */
239 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
240 /* Cb=>G value is scaled-up -0.34414 * x */
241 /* We also add in ONE_HALF so that need not do it in inner loop */
242 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
248 * Convert some rows of samples to the output colorspace.
252 ycc_rgb_convert (j_decompress_ptr cinfo,
253 JSAMPIMAGE input_buf, JDIMENSION input_row,
254 JSAMPARRAY output_buf, int num_rows)
256 switch (cinfo->out_color_space) {
258 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
263 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
267 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
272 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
277 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
282 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
286 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
293 /**************** Cases other than YCbCr -> RGB **************/
297 * Initialize for RGB->grayscale colorspace conversion.
301 build_rgb_y_table (j_decompress_ptr cinfo)
303 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
307 /* Allocate and fill in the conversion tables. */
308 cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
309 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
310 (TABLE_SIZE * SIZEOF(INT32)));
312 for (i = 0; i <= MAXJSAMPLE; i++) {
313 rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
314 rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
315 rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
321 * Convert RGB to grayscale.
325 rgb_gray_convert (j_decompress_ptr cinfo,
326 JSAMPIMAGE input_buf, JDIMENSION input_row,
327 JSAMPARRAY output_buf, int num_rows)
329 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
330 register int r, g, b;
331 register INT32 * ctab = cconvert->rgb_y_tab;
332 register JSAMPROW outptr;
333 register JSAMPROW inptr0, inptr1, inptr2;
334 register JDIMENSION col;
335 JDIMENSION num_cols = cinfo->output_width;
337 while (--num_rows >= 0) {
338 inptr0 = input_buf[0][input_row];
339 inptr1 = input_buf[1][input_row];
340 inptr2 = input_buf[2][input_row];
342 outptr = *output_buf++;
343 for (col = 0; col < num_cols; col++) {
344 r = GETJSAMPLE(inptr0[col]);
345 g = GETJSAMPLE(inptr1[col]);
346 b = GETJSAMPLE(inptr2[col]);
348 outptr[col] = (JSAMPLE)
349 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
357 * Color conversion for no colorspace change: just copy the data,
358 * converting from separate-planes to interleaved representation.
362 null_convert (j_decompress_ptr cinfo,
363 JSAMPIMAGE input_buf, JDIMENSION input_row,
364 JSAMPARRAY output_buf, int num_rows)
366 register JSAMPROW inptr, outptr;
367 register JDIMENSION count;
368 register int num_components = cinfo->num_components;
369 JDIMENSION num_cols = cinfo->output_width;
372 while (--num_rows >= 0) {
373 for (ci = 0; ci < num_components; ci++) {
374 inptr = input_buf[ci][input_row];
375 outptr = output_buf[0] + ci;
376 for (count = num_cols; count > 0; count--) {
377 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
378 outptr += num_components;
388 * Color conversion for grayscale: just copy the data.
389 * This also works for YCbCr -> grayscale conversion, in which
390 * we just copy the Y (luminance) component and ignore chrominance.
394 grayscale_convert (j_decompress_ptr cinfo,
395 JSAMPIMAGE input_buf, JDIMENSION input_row,
396 JSAMPARRAY output_buf, int num_rows)
398 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
399 num_rows, cinfo->output_width);
404 * Convert grayscale to RGB
408 gray_rgb_convert (j_decompress_ptr cinfo,
409 JSAMPIMAGE input_buf, JDIMENSION input_row,
410 JSAMPARRAY output_buf, int num_rows)
412 switch (cinfo->out_color_space) {
414 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
419 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
423 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
428 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
433 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
438 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
442 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
450 * Convert plain RGB to extended RGB
454 rgb_rgb_convert (j_decompress_ptr cinfo,
455 JSAMPIMAGE input_buf, JDIMENSION input_row,
456 JSAMPARRAY output_buf, int num_rows)
458 switch (cinfo->out_color_space) {
460 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
465 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
469 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
474 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
479 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
484 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
488 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
496 * Adobe-style YCCK->CMYK conversion.
497 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
498 * conversion as above, while passing K (black) unchanged.
499 * We assume build_ycc_rgb_table has been called.
503 ycck_cmyk_convert (j_decompress_ptr cinfo,
504 JSAMPIMAGE input_buf, JDIMENSION input_row,
505 JSAMPARRAY output_buf, int num_rows)
507 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
508 register int y, cb, cr;
509 register JSAMPROW outptr;
510 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
511 register JDIMENSION col;
512 JDIMENSION num_cols = cinfo->output_width;
513 /* copy these pointers into registers if possible */
514 register JSAMPLE * range_limit = cinfo->sample_range_limit;
515 register int * Crrtab = cconvert->Cr_r_tab;
516 register int * Cbbtab = cconvert->Cb_b_tab;
517 register INT32 * Crgtab = cconvert->Cr_g_tab;
518 register INT32 * Cbgtab = cconvert->Cb_g_tab;
521 while (--num_rows >= 0) {
522 inptr0 = input_buf[0][input_row];
523 inptr1 = input_buf[1][input_row];
524 inptr2 = input_buf[2][input_row];
525 inptr3 = input_buf[3][input_row];
527 outptr = *output_buf++;
528 for (col = 0; col < num_cols; col++) {
529 y = GETJSAMPLE(inptr0[col]);
530 cb = GETJSAMPLE(inptr1[col]);
531 cr = GETJSAMPLE(inptr2[col]);
532 /* Range-limiting is essential due to noise introduced by DCT losses. */
533 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
534 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
535 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
537 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
538 /* K passes through unchanged */
539 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
547 * Empty method for start_pass.
551 start_pass_dcolor (j_decompress_ptr cinfo)
558 * Module initialization routine for output colorspace conversion.
562 jinit_color_deconverter (j_decompress_ptr cinfo)
564 my_cconvert_ptr cconvert;
567 cconvert = (my_cconvert_ptr)
568 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
569 SIZEOF(my_color_deconverter));
570 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
571 cconvert->pub.start_pass = start_pass_dcolor;
573 /* Make sure num_components agrees with jpeg_color_space */
574 switch (cinfo->jpeg_color_space) {
576 if (cinfo->num_components != 1)
577 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
582 if (cinfo->num_components != 3)
583 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
588 if (cinfo->num_components != 4)
589 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
592 default: /* JCS_UNKNOWN can be anything */
593 if (cinfo->num_components < 1)
594 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
598 /* Set out_color_components and conversion method based on requested space.
599 * Also clear the component_needed flags for any unused components,
600 * so that earlier pipeline stages can avoid useless computation.
603 switch (cinfo->out_color_space) {
605 cinfo->out_color_components = 1;
606 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
607 cinfo->jpeg_color_space == JCS_YCbCr) {
608 cconvert->pub.color_convert = grayscale_convert;
609 /* For color->grayscale conversion, only the Y (0) component is needed */
610 for (ci = 1; ci < cinfo->num_components; ci++)
611 cinfo->comp_info[ci].component_needed = FALSE;
612 } else if (cinfo->jpeg_color_space == JCS_RGB) {
613 cconvert->pub.color_convert = rgb_gray_convert;
614 build_rgb_y_table(cinfo);
616 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
630 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
631 if (cinfo->jpeg_color_space == JCS_YCbCr) {
632 if (jsimd_can_ycc_rgb())
633 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
635 cconvert->pub.color_convert = ycc_rgb_convert;
636 build_ycc_rgb_table(cinfo);
638 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
639 cconvert->pub.color_convert = gray_rgb_convert;
640 } else if (cinfo->jpeg_color_space == JCS_RGB) {
641 if (rgb_red[cinfo->out_color_space] == 0 &&
642 rgb_green[cinfo->out_color_space] == 1 &&
643 rgb_blue[cinfo->out_color_space] == 2 &&
644 rgb_pixelsize[cinfo->out_color_space] == 3)
645 cconvert->pub.color_convert = null_convert;
647 cconvert->pub.color_convert = rgb_rgb_convert;
649 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
653 cinfo->out_color_components = 4;
654 if (cinfo->jpeg_color_space == JCS_YCCK) {
655 cconvert->pub.color_convert = ycck_cmyk_convert;
656 build_ycc_rgb_table(cinfo);
657 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
658 cconvert->pub.color_convert = null_convert;
660 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
664 /* Permit null conversion to same output space */
665 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
666 cinfo->out_color_components = cinfo->num_components;
667 cconvert->pub.color_convert = null_convert;
668 } else /* unsupported non-null conversion */
669 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
673 if (cinfo->quantize_colors)
674 cinfo->output_components = 1; /* single colormapped output component */
676 cinfo->output_components = cinfo->out_color_components;