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, 2014-2015, D. R. Commander.
10 * Copyright (C) 2013, Linaro Limited.
11 * For conditions of distribution and use, see the accompanying README.ijg
14 * This file contains output colorspace conversion routines.
17 #define JPEG_INTERNALS
21 #include "jconfigint.h"
24 /* Private subobject */
27 struct jpeg_color_deconverter pub; /* public fields */
29 /* Private state for YCC->RGB conversion */
30 int *Cr_r_tab; /* => table for Cr to R conversion */
31 int *Cb_b_tab; /* => table for Cb to B conversion */
32 JLONG *Cr_g_tab; /* => table for Cr to G conversion */
33 JLONG *Cb_g_tab; /* => table for Cb to G conversion */
35 /* Private state for RGB->Y conversion */
36 JLONG *rgb_y_tab; /* => table for RGB to Y conversion */
37 } my_color_deconverter;
39 typedef my_color_deconverter *my_cconvert_ptr;
42 /**************** YCbCr -> RGB conversion: most common case **************/
43 /**************** RGB -> Y conversion: less common case **************/
46 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
47 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
48 * The conversion equations to be implemented are therefore
50 * R = Y + 1.40200 * Cr
51 * G = Y - 0.34414 * Cb - 0.71414 * Cr
52 * B = Y + 1.77200 * Cb
54 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
56 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
57 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
59 * To avoid floating-point arithmetic, we represent the fractional constants
60 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
61 * the products by 2^16, with appropriate rounding, to get the correct answer.
62 * Notice that Y, being an integral input, does not contribute any fraction
63 * so it need not participate in the rounding.
65 * For even more speed, we avoid doing any multiplications in the inner loop
66 * by precalculating the constants times Cb and Cr for all possible values.
67 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
68 * for 12-bit samples it is still acceptable. It's not very reasonable for
69 * 16-bit samples, but if you want lossless storage you shouldn't be changing
71 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
72 * values for the G calculation are left scaled up, since we must add them
73 * together before rounding.
76 #define SCALEBITS 16 /* speediest right-shift on some machines */
77 #define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))
78 #define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
80 /* We allocate one big table for RGB->Y conversion and divide it up into
81 * three parts, instead of doing three alloc_small requests. This lets us
82 * use a single table base address, which can be held in a register in the
83 * inner loops on many machines (more than can hold all three addresses,
87 #define R_Y_OFF 0 /* offset to R => Y section */
88 #define G_Y_OFF (1 * (MAXJSAMPLE + 1)) /* offset to G => Y section */
89 #define B_Y_OFF (2 * (MAXJSAMPLE + 1)) /* etc. */
90 #define TABLE_SIZE (3 * (MAXJSAMPLE + 1))
93 /* Include inline routines for colorspace extensions */
101 #define RGB_RED EXT_RGB_RED
102 #define RGB_GREEN EXT_RGB_GREEN
103 #define RGB_BLUE EXT_RGB_BLUE
104 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
105 #define ycc_rgb_convert_internal ycc_extrgb_convert_internal
106 #define gray_rgb_convert_internal gray_extrgb_convert_internal
107 #define rgb_rgb_convert_internal rgb_extrgb_convert_internal
108 #include "jdcolext.c"
113 #undef ycc_rgb_convert_internal
114 #undef gray_rgb_convert_internal
115 #undef rgb_rgb_convert_internal
117 #define RGB_RED EXT_RGBX_RED
118 #define RGB_GREEN EXT_RGBX_GREEN
119 #define RGB_BLUE EXT_RGBX_BLUE
121 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
122 #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
123 #define gray_rgb_convert_internal gray_extrgbx_convert_internal
124 #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
125 #include "jdcolext.c"
131 #undef ycc_rgb_convert_internal
132 #undef gray_rgb_convert_internal
133 #undef rgb_rgb_convert_internal
135 #define RGB_RED EXT_BGR_RED
136 #define RGB_GREEN EXT_BGR_GREEN
137 #define RGB_BLUE EXT_BGR_BLUE
138 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
139 #define ycc_rgb_convert_internal ycc_extbgr_convert_internal
140 #define gray_rgb_convert_internal gray_extbgr_convert_internal
141 #define rgb_rgb_convert_internal rgb_extbgr_convert_internal
142 #include "jdcolext.c"
147 #undef ycc_rgb_convert_internal
148 #undef gray_rgb_convert_internal
149 #undef rgb_rgb_convert_internal
151 #define RGB_RED EXT_BGRX_RED
152 #define RGB_GREEN EXT_BGRX_GREEN
153 #define RGB_BLUE EXT_BGRX_BLUE
155 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
156 #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
157 #define gray_rgb_convert_internal gray_extbgrx_convert_internal
158 #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
159 #include "jdcolext.c"
165 #undef ycc_rgb_convert_internal
166 #undef gray_rgb_convert_internal
167 #undef rgb_rgb_convert_internal
169 #define RGB_RED EXT_XBGR_RED
170 #define RGB_GREEN EXT_XBGR_GREEN
171 #define RGB_BLUE EXT_XBGR_BLUE
173 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
174 #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
175 #define gray_rgb_convert_internal gray_extxbgr_convert_internal
176 #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
177 #include "jdcolext.c"
183 #undef ycc_rgb_convert_internal
184 #undef gray_rgb_convert_internal
185 #undef rgb_rgb_convert_internal
187 #define RGB_RED EXT_XRGB_RED
188 #define RGB_GREEN EXT_XRGB_GREEN
189 #define RGB_BLUE EXT_XRGB_BLUE
191 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
192 #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
193 #define gray_rgb_convert_internal gray_extxrgb_convert_internal
194 #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
195 #include "jdcolext.c"
201 #undef ycc_rgb_convert_internal
202 #undef gray_rgb_convert_internal
203 #undef rgb_rgb_convert_internal
207 * Initialize tables for YCC->RGB colorspace conversion.
211 build_ycc_rgb_table(j_decompress_ptr cinfo)
213 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
218 cconvert->Cr_r_tab = (int *)
219 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
220 (MAXJSAMPLE + 1) * sizeof(int));
221 cconvert->Cb_b_tab = (int *)
222 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
223 (MAXJSAMPLE + 1) * sizeof(int));
224 cconvert->Cr_g_tab = (JLONG *)
225 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
226 (MAXJSAMPLE + 1) * sizeof(JLONG));
227 cconvert->Cb_g_tab = (JLONG *)
228 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
229 (MAXJSAMPLE + 1) * sizeof(JLONG));
231 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
232 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
233 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
234 /* Cr=>R value is nearest int to 1.40200 * x */
235 cconvert->Cr_r_tab[i] = (int)
236 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
237 /* Cb=>B value is nearest int to 1.77200 * x */
238 cconvert->Cb_b_tab[i] = (int)
239 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
240 /* Cr=>G value is scaled-up -0.71414 * x */
241 cconvert->Cr_g_tab[i] = (-FIX(0.71414)) * x;
242 /* Cb=>G value is scaled-up -0.34414 * x */
243 /* We also add in ONE_HALF so that need not do it in inner loop */
244 cconvert->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
250 * Convert some rows of samples to the output colorspace.
254 ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
255 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
257 switch (cinfo->out_color_space) {
259 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
264 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
268 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
273 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
278 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
283 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
287 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
294 /**************** Cases other than YCbCr -> RGB **************/
298 * Initialize for RGB->grayscale colorspace conversion.
302 build_rgb_y_table(j_decompress_ptr cinfo)
304 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
308 /* Allocate and fill in the conversion tables. */
309 cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
310 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
311 (TABLE_SIZE * sizeof(JLONG)));
313 for (i = 0; i <= MAXJSAMPLE; i++) {
314 rgb_y_tab[i + R_Y_OFF] = FIX(0.29900) * i;
315 rgb_y_tab[i + G_Y_OFF] = FIX(0.58700) * i;
316 rgb_y_tab[i + B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
322 * Convert RGB to grayscale.
326 rgb_gray_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
327 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
329 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
330 register int r, g, b;
331 register JLONG *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++) {
348 outptr[col] = (JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
349 ctab[b + B_Y_OFF]) >> SCALEBITS);
356 * Color conversion for no colorspace change: just copy the data,
357 * converting from separate-planes to interleaved representation.
361 null_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
362 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
364 register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
365 register JDIMENSION col;
366 register int num_components = cinfo->num_components;
367 JDIMENSION num_cols = cinfo->output_width;
370 if (num_components == 3) {
371 while (--num_rows >= 0) {
372 inptr0 = input_buf[0][input_row];
373 inptr1 = input_buf[1][input_row];
374 inptr2 = input_buf[2][input_row];
376 outptr = *output_buf++;
377 for (col = 0; col < num_cols; col++) {
378 *outptr++ = inptr0[col];
379 *outptr++ = inptr1[col];
380 *outptr++ = inptr2[col];
383 } else if (num_components == 4) {
384 while (--num_rows >= 0) {
385 inptr0 = input_buf[0][input_row];
386 inptr1 = input_buf[1][input_row];
387 inptr2 = input_buf[2][input_row];
388 inptr3 = input_buf[3][input_row];
390 outptr = *output_buf++;
391 for (col = 0; col < num_cols; col++) {
392 *outptr++ = inptr0[col];
393 *outptr++ = inptr1[col];
394 *outptr++ = inptr2[col];
395 *outptr++ = inptr3[col];
399 while (--num_rows >= 0) {
400 for (ci = 0; ci < num_components; ci++) {
401 inptr = input_buf[ci][input_row];
402 outptr = *output_buf;
403 for (col = 0; col < num_cols; col++) {
404 outptr[ci] = inptr[col];
405 outptr += num_components;
416 * Color conversion for grayscale: just copy the data.
417 * This also works for YCbCr -> grayscale conversion, in which
418 * we just copy the Y (luminance) component and ignore chrominance.
422 grayscale_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
423 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
425 jcopy_sample_rows(input_buf[0], (int)input_row, output_buf, 0, num_rows,
426 cinfo->output_width);
431 * Convert grayscale to RGB
435 gray_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
436 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
438 switch (cinfo->out_color_space) {
440 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
445 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
449 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
454 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
459 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
464 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
468 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
476 * Convert plain RGB to extended RGB
480 rgb_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
481 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
483 switch (cinfo->out_color_space) {
485 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
490 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
494 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
499 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
504 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
509 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
513 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
521 * Adobe-style YCCK->CMYK conversion.
522 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
523 * conversion as above, while passing K (black) unchanged.
524 * We assume build_ycc_rgb_table has been called.
528 ycck_cmyk_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
529 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
531 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
532 register int y, cb, cr;
533 register JSAMPROW outptr;
534 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
535 register JDIMENSION col;
536 JDIMENSION num_cols = cinfo->output_width;
537 /* copy these pointers into registers if possible */
538 register JSAMPLE *range_limit = cinfo->sample_range_limit;
539 register int *Crrtab = cconvert->Cr_r_tab;
540 register int *Cbbtab = cconvert->Cb_b_tab;
541 register JLONG *Crgtab = cconvert->Cr_g_tab;
542 register JLONG *Cbgtab = cconvert->Cb_g_tab;
545 while (--num_rows >= 0) {
546 inptr0 = input_buf[0][input_row];
547 inptr1 = input_buf[1][input_row];
548 inptr2 = input_buf[2][input_row];
549 inptr3 = input_buf[3][input_row];
551 outptr = *output_buf++;
552 for (col = 0; col < num_cols; col++) {
556 /* Range-limiting is essential due to noise introduced by DCT losses. */
557 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
558 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
559 ((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
561 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
562 /* K passes through unchanged */
563 outptr[3] = inptr3[col];
574 #define PACK_SHORT_565_LE(r, g, b) \
575 ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
576 #define PACK_SHORT_565_BE(r, g, b) \
577 (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
579 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
580 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
582 #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
584 #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
586 #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
587 #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
588 #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
591 /* Declarations for ordered dithering
593 * We use a 4x4 ordered dither array packed into 32 bits. This array is
594 * sufficient for dithering RGB888 to RGB565.
597 #define DITHER_MASK 0x3
598 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
599 static const JLONG dither_matrix[4] = {
607 static INLINE boolean is_big_endian(void)
610 if (*(char *)&test_value != 1)
616 /* Include inline routines for RGB565 conversion */
618 #define PACK_SHORT_565 PACK_SHORT_565_LE
619 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
620 #define ycc_rgb565_convert_internal ycc_rgb565_convert_le
621 #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
622 #define rgb_rgb565_convert_internal rgb_rgb565_convert_le
623 #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
624 #define gray_rgb565_convert_internal gray_rgb565_convert_le
625 #define gray_rgb565D_convert_internal gray_rgb565D_convert_le
626 #include "jdcol565.c"
627 #undef PACK_SHORT_565
628 #undef PACK_TWO_PIXELS
629 #undef ycc_rgb565_convert_internal
630 #undef ycc_rgb565D_convert_internal
631 #undef rgb_rgb565_convert_internal
632 #undef rgb_rgb565D_convert_internal
633 #undef gray_rgb565_convert_internal
634 #undef gray_rgb565D_convert_internal
636 #define PACK_SHORT_565 PACK_SHORT_565_BE
637 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
638 #define ycc_rgb565_convert_internal ycc_rgb565_convert_be
639 #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
640 #define rgb_rgb565_convert_internal rgb_rgb565_convert_be
641 #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
642 #define gray_rgb565_convert_internal gray_rgb565_convert_be
643 #define gray_rgb565D_convert_internal gray_rgb565D_convert_be
644 #include "jdcol565.c"
645 #undef PACK_SHORT_565
646 #undef PACK_TWO_PIXELS
647 #undef ycc_rgb565_convert_internal
648 #undef ycc_rgb565D_convert_internal
649 #undef rgb_rgb565_convert_internal
650 #undef rgb_rgb565D_convert_internal
651 #undef gray_rgb565_convert_internal
652 #undef gray_rgb565D_convert_internal
656 ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
657 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
660 ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
662 ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
667 ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
668 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
671 ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
673 ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
678 rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
679 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
682 rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
684 rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
689 rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
690 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
693 rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
695 rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
700 gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
701 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
704 gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
706 gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
711 gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
712 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
715 gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
717 gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
722 * Empty method for start_pass.
726 start_pass_dcolor(j_decompress_ptr cinfo)
733 * Module initialization routine for output colorspace conversion.
737 jinit_color_deconverter(j_decompress_ptr cinfo)
739 my_cconvert_ptr cconvert;
742 cconvert = (my_cconvert_ptr)
743 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
744 sizeof(my_color_deconverter));
745 cinfo->cconvert = (struct jpeg_color_deconverter *)cconvert;
746 cconvert->pub.start_pass = start_pass_dcolor;
748 /* Make sure num_components agrees with jpeg_color_space */
749 switch (cinfo->jpeg_color_space) {
751 if (cinfo->num_components != 1)
752 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
757 if (cinfo->num_components != 3)
758 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
763 if (cinfo->num_components != 4)
764 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
767 default: /* JCS_UNKNOWN can be anything */
768 if (cinfo->num_components < 1)
769 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
773 /* Set out_color_components and conversion method based on requested space.
774 * Also clear the component_needed flags for any unused components,
775 * so that earlier pipeline stages can avoid useless computation.
778 switch (cinfo->out_color_space) {
780 cinfo->out_color_components = 1;
781 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
782 cinfo->jpeg_color_space == JCS_YCbCr) {
783 cconvert->pub.color_convert = grayscale_convert;
784 /* For color->grayscale conversion, only the Y (0) component is needed */
785 for (ci = 1; ci < cinfo->num_components; ci++)
786 cinfo->comp_info[ci].component_needed = FALSE;
787 } else if (cinfo->jpeg_color_space == JCS_RGB) {
788 cconvert->pub.color_convert = rgb_gray_convert;
789 build_rgb_y_table(cinfo);
791 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
805 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
806 if (cinfo->jpeg_color_space == JCS_YCbCr) {
807 if (jsimd_can_ycc_rgb())
808 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
810 cconvert->pub.color_convert = ycc_rgb_convert;
811 build_ycc_rgb_table(cinfo);
813 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
814 cconvert->pub.color_convert = gray_rgb_convert;
815 } else if (cinfo->jpeg_color_space == JCS_RGB) {
816 if (rgb_red[cinfo->out_color_space] == 0 &&
817 rgb_green[cinfo->out_color_space] == 1 &&
818 rgb_blue[cinfo->out_color_space] == 2 &&
819 rgb_pixelsize[cinfo->out_color_space] == 3)
820 cconvert->pub.color_convert = null_convert;
822 cconvert->pub.color_convert = rgb_rgb_convert;
824 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
828 cinfo->out_color_components = 3;
829 if (cinfo->dither_mode == JDITHER_NONE) {
830 if (cinfo->jpeg_color_space == JCS_YCbCr) {
831 if (jsimd_can_ycc_rgb565())
832 cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
834 cconvert->pub.color_convert = ycc_rgb565_convert;
835 build_ycc_rgb_table(cinfo);
837 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
838 cconvert->pub.color_convert = gray_rgb565_convert;
839 } else if (cinfo->jpeg_color_space == JCS_RGB) {
840 cconvert->pub.color_convert = rgb_rgb565_convert;
842 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
844 /* only ordered dithering is supported */
845 if (cinfo->jpeg_color_space == JCS_YCbCr) {
846 cconvert->pub.color_convert = ycc_rgb565D_convert;
847 build_ycc_rgb_table(cinfo);
848 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
849 cconvert->pub.color_convert = gray_rgb565D_convert;
850 } else if (cinfo->jpeg_color_space == JCS_RGB) {
851 cconvert->pub.color_convert = rgb_rgb565D_convert;
853 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
858 cinfo->out_color_components = 4;
859 if (cinfo->jpeg_color_space == JCS_YCCK) {
860 cconvert->pub.color_convert = ycck_cmyk_convert;
861 build_ycc_rgb_table(cinfo);
862 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
863 cconvert->pub.color_convert = null_convert;
865 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
869 /* Permit null conversion to same output space */
870 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
871 cinfo->out_color_components = cinfo->num_components;
872 cconvert->pub.color_convert = null_convert;
873 } else /* unsupported non-null conversion */
874 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
878 if (cinfo->quantize_colors)
879 cinfo->output_components = 1; /* single colormapped output component */
881 cinfo->output_components = cinfo->out_color_components;