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,
255 JSAMPIMAGE input_buf, JDIMENSION input_row,
256 JSAMPARRAY output_buf, int num_rows)
258 switch (cinfo->out_color_space) {
260 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
265 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
269 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
274 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
279 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
284 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
288 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
295 /**************** Cases other than YCbCr -> RGB **************/
299 * Initialize for RGB->grayscale colorspace conversion.
303 build_rgb_y_table (j_decompress_ptr cinfo)
305 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
309 /* Allocate and fill in the conversion tables. */
310 cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
311 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
312 (TABLE_SIZE * sizeof(JLONG)));
314 for (i = 0; i <= MAXJSAMPLE; i++) {
315 rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
316 rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
317 rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
323 * Convert RGB to grayscale.
327 rgb_gray_convert (j_decompress_ptr cinfo,
328 JSAMPIMAGE input_buf, JDIMENSION input_row,
329 JSAMPARRAY output_buf, int num_rows)
331 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
332 register int r, g, b;
333 register JLONG *ctab = cconvert->rgb_y_tab;
334 register JSAMPROW outptr;
335 register JSAMPROW inptr0, inptr1, inptr2;
336 register JDIMENSION col;
337 JDIMENSION num_cols = cinfo->output_width;
339 while (--num_rows >= 0) {
340 inptr0 = input_buf[0][input_row];
341 inptr1 = input_buf[1][input_row];
342 inptr2 = input_buf[2][input_row];
344 outptr = *output_buf++;
345 for (col = 0; col < num_cols; col++) {
346 r = GETJSAMPLE(inptr0[col]);
347 g = GETJSAMPLE(inptr1[col]);
348 b = GETJSAMPLE(inptr2[col]);
350 outptr[col] = (JSAMPLE)
351 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
359 * Color conversion for no colorspace change: just copy the data,
360 * converting from separate-planes to interleaved representation.
364 null_convert (j_decompress_ptr cinfo,
365 JSAMPIMAGE input_buf, JDIMENSION input_row,
366 JSAMPARRAY output_buf, int num_rows)
368 register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
369 register JDIMENSION col;
370 register int num_components = cinfo->num_components;
371 JDIMENSION num_cols = cinfo->output_width;
374 if (num_components == 3) {
375 while (--num_rows >= 0) {
376 inptr0 = input_buf[0][input_row];
377 inptr1 = input_buf[1][input_row];
378 inptr2 = input_buf[2][input_row];
380 outptr = *output_buf++;
381 for (col = 0; col < num_cols; col++) {
382 *outptr++ = inptr0[col];
383 *outptr++ = inptr1[col];
384 *outptr++ = inptr2[col];
387 } else if (num_components == 4) {
388 while (--num_rows >= 0) {
389 inptr0 = input_buf[0][input_row];
390 inptr1 = input_buf[1][input_row];
391 inptr2 = input_buf[2][input_row];
392 inptr3 = input_buf[3][input_row];
394 outptr = *output_buf++;
395 for (col = 0; col < num_cols; col++) {
396 *outptr++ = inptr0[col];
397 *outptr++ = inptr1[col];
398 *outptr++ = inptr2[col];
399 *outptr++ = inptr3[col];
403 while (--num_rows >= 0) {
404 for (ci = 0; ci < num_components; ci++) {
405 inptr = input_buf[ci][input_row];
406 outptr = *output_buf;
407 for (col = 0; col < num_cols; col++) {
408 outptr[ci] = inptr[col];
409 outptr += num_components;
420 * Color conversion for grayscale: just copy the data.
421 * This also works for YCbCr -> grayscale conversion, in which
422 * we just copy the Y (luminance) component and ignore chrominance.
426 grayscale_convert (j_decompress_ptr cinfo,
427 JSAMPIMAGE input_buf, JDIMENSION input_row,
428 JSAMPARRAY output_buf, int num_rows)
430 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
431 num_rows, cinfo->output_width);
436 * Convert grayscale to RGB
440 gray_rgb_convert (j_decompress_ptr cinfo,
441 JSAMPIMAGE input_buf, JDIMENSION input_row,
442 JSAMPARRAY output_buf, int num_rows)
444 switch (cinfo->out_color_space) {
446 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
451 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
455 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
460 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
465 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
470 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
474 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
482 * Convert plain RGB to extended RGB
486 rgb_rgb_convert (j_decompress_ptr cinfo,
487 JSAMPIMAGE input_buf, JDIMENSION input_row,
488 JSAMPARRAY output_buf, int num_rows)
490 switch (cinfo->out_color_space) {
492 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
497 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
501 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
506 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
511 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
516 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
520 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
528 * Adobe-style YCCK->CMYK conversion.
529 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
530 * conversion as above, while passing K (black) unchanged.
531 * We assume build_ycc_rgb_table has been called.
535 ycck_cmyk_convert (j_decompress_ptr cinfo,
536 JSAMPIMAGE input_buf, JDIMENSION input_row,
537 JSAMPARRAY output_buf, int num_rows)
539 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
540 register int y, cb, cr;
541 register JSAMPROW outptr;
542 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
543 register JDIMENSION col;
544 JDIMENSION num_cols = cinfo->output_width;
545 /* copy these pointers into registers if possible */
546 register JSAMPLE *range_limit = cinfo->sample_range_limit;
547 register int *Crrtab = cconvert->Cr_r_tab;
548 register int *Cbbtab = cconvert->Cb_b_tab;
549 register JLONG *Crgtab = cconvert->Cr_g_tab;
550 register JLONG *Cbgtab = cconvert->Cb_g_tab;
553 while (--num_rows >= 0) {
554 inptr0 = input_buf[0][input_row];
555 inptr1 = input_buf[1][input_row];
556 inptr2 = input_buf[2][input_row];
557 inptr3 = input_buf[3][input_row];
559 outptr = *output_buf++;
560 for (col = 0; col < num_cols; col++) {
561 y = GETJSAMPLE(inptr0[col]);
562 cb = GETJSAMPLE(inptr1[col]);
563 cr = GETJSAMPLE(inptr2[col]);
564 /* Range-limiting is essential due to noise introduced by DCT losses. */
565 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
566 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
567 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
569 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
570 /* K passes through unchanged */
571 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
582 #define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
583 (((g) << 3) & 0x7E0) | ((b) >> 3))
584 #define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
585 (((g) << 11) & 0xE000) | \
586 (((b) << 5) & 0x1F00))
588 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
589 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
591 #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
593 #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
595 #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
596 #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
597 #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
600 /* Declarations for ordered dithering
602 * We use a 4x4 ordered dither array packed into 32 bits. This array is
603 * sufficent for dithering RGB888 to RGB565.
606 #define DITHER_MASK 0x3
607 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
608 static const JLONG dither_matrix[4] = {
616 static INLINE boolean is_big_endian(void)
619 if (*(char *)&test_value != 1)
625 /* Include inline routines for RGB565 conversion */
627 #define PACK_SHORT_565 PACK_SHORT_565_LE
628 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
629 #define ycc_rgb565_convert_internal ycc_rgb565_convert_le
630 #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
631 #define rgb_rgb565_convert_internal rgb_rgb565_convert_le
632 #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
633 #define gray_rgb565_convert_internal gray_rgb565_convert_le
634 #define gray_rgb565D_convert_internal gray_rgb565D_convert_le
635 #include "jdcol565.c"
636 #undef PACK_SHORT_565
637 #undef PACK_TWO_PIXELS
638 #undef ycc_rgb565_convert_internal
639 #undef ycc_rgb565D_convert_internal
640 #undef rgb_rgb565_convert_internal
641 #undef rgb_rgb565D_convert_internal
642 #undef gray_rgb565_convert_internal
643 #undef gray_rgb565D_convert_internal
645 #define PACK_SHORT_565 PACK_SHORT_565_BE
646 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
647 #define ycc_rgb565_convert_internal ycc_rgb565_convert_be
648 #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
649 #define rgb_rgb565_convert_internal rgb_rgb565_convert_be
650 #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
651 #define gray_rgb565_convert_internal gray_rgb565_convert_be
652 #define gray_rgb565D_convert_internal gray_rgb565D_convert_be
653 #include "jdcol565.c"
654 #undef PACK_SHORT_565
655 #undef PACK_TWO_PIXELS
656 #undef ycc_rgb565_convert_internal
657 #undef ycc_rgb565D_convert_internal
658 #undef rgb_rgb565_convert_internal
659 #undef rgb_rgb565D_convert_internal
660 #undef gray_rgb565_convert_internal
661 #undef gray_rgb565D_convert_internal
665 ycc_rgb565_convert (j_decompress_ptr cinfo,
666 JSAMPIMAGE input_buf, JDIMENSION input_row,
667 JSAMPARRAY output_buf, int num_rows)
670 ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
672 ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
677 ycc_rgb565D_convert (j_decompress_ptr cinfo,
678 JSAMPIMAGE input_buf, JDIMENSION input_row,
679 JSAMPARRAY output_buf, int num_rows)
682 ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
684 ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
689 rgb_rgb565_convert (j_decompress_ptr cinfo,
690 JSAMPIMAGE input_buf, JDIMENSION input_row,
691 JSAMPARRAY output_buf, int num_rows)
694 rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
696 rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
701 rgb_rgb565D_convert (j_decompress_ptr cinfo,
702 JSAMPIMAGE input_buf, JDIMENSION input_row,
703 JSAMPARRAY output_buf, int num_rows)
706 rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
708 rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
713 gray_rgb565_convert (j_decompress_ptr cinfo,
714 JSAMPIMAGE input_buf, JDIMENSION input_row,
715 JSAMPARRAY output_buf, int num_rows)
718 gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
720 gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
725 gray_rgb565D_convert (j_decompress_ptr cinfo,
726 JSAMPIMAGE input_buf, JDIMENSION input_row,
727 JSAMPARRAY output_buf, int num_rows)
730 gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
732 gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
737 * Empty method for start_pass.
741 start_pass_dcolor (j_decompress_ptr cinfo)
748 * Module initialization routine for output colorspace conversion.
752 jinit_color_deconverter (j_decompress_ptr cinfo)
754 my_cconvert_ptr cconvert;
757 cconvert = (my_cconvert_ptr)
758 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
759 sizeof(my_color_deconverter));
760 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
761 cconvert->pub.start_pass = start_pass_dcolor;
763 /* Make sure num_components agrees with jpeg_color_space */
764 switch (cinfo->jpeg_color_space) {
766 if (cinfo->num_components != 1)
767 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
772 if (cinfo->num_components != 3)
773 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
778 if (cinfo->num_components != 4)
779 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
782 default: /* JCS_UNKNOWN can be anything */
783 if (cinfo->num_components < 1)
784 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
788 /* Set out_color_components and conversion method based on requested space.
789 * Also clear the component_needed flags for any unused components,
790 * so that earlier pipeline stages can avoid useless computation.
793 switch (cinfo->out_color_space) {
795 cinfo->out_color_components = 1;
796 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
797 cinfo->jpeg_color_space == JCS_YCbCr) {
798 cconvert->pub.color_convert = grayscale_convert;
799 /* For color->grayscale conversion, only the Y (0) component is needed */
800 for (ci = 1; ci < cinfo->num_components; ci++)
801 cinfo->comp_info[ci].component_needed = FALSE;
802 } else if (cinfo->jpeg_color_space == JCS_RGB) {
803 cconvert->pub.color_convert = rgb_gray_convert;
804 build_rgb_y_table(cinfo);
806 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
820 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
821 if (cinfo->jpeg_color_space == JCS_YCbCr) {
822 if (jsimd_can_ycc_rgb())
823 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
825 cconvert->pub.color_convert = ycc_rgb_convert;
826 build_ycc_rgb_table(cinfo);
828 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
829 cconvert->pub.color_convert = gray_rgb_convert;
830 } else if (cinfo->jpeg_color_space == JCS_RGB) {
831 if (rgb_red[cinfo->out_color_space] == 0 &&
832 rgb_green[cinfo->out_color_space] == 1 &&
833 rgb_blue[cinfo->out_color_space] == 2 &&
834 rgb_pixelsize[cinfo->out_color_space] == 3)
835 cconvert->pub.color_convert = null_convert;
837 cconvert->pub.color_convert = rgb_rgb_convert;
839 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
843 cinfo->out_color_components = 3;
844 if (cinfo->dither_mode == JDITHER_NONE) {
845 if (cinfo->jpeg_color_space == JCS_YCbCr) {
846 if (jsimd_can_ycc_rgb565())
847 cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
849 cconvert->pub.color_convert = ycc_rgb565_convert;
850 build_ycc_rgb_table(cinfo);
852 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
853 cconvert->pub.color_convert = gray_rgb565_convert;
854 } else if (cinfo->jpeg_color_space == JCS_RGB) {
855 cconvert->pub.color_convert = rgb_rgb565_convert;
857 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
859 /* only ordered dithering is supported */
860 if (cinfo->jpeg_color_space == JCS_YCbCr) {
861 cconvert->pub.color_convert = ycc_rgb565D_convert;
862 build_ycc_rgb_table(cinfo);
863 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
864 cconvert->pub.color_convert = gray_rgb565D_convert;
865 } else if (cinfo->jpeg_color_space == JCS_RGB) {
866 cconvert->pub.color_convert = rgb_rgb565D_convert;
868 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
873 cinfo->out_color_components = 4;
874 if (cinfo->jpeg_color_space == JCS_YCCK) {
875 cconvert->pub.color_convert = ycck_cmyk_convert;
876 build_ycc_rgb_table(cinfo);
877 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
878 cconvert->pub.color_convert = null_convert;
880 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
884 /* Permit null conversion to same output space */
885 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
886 cinfo->out_color_components = cinfo->num_components;
887 cconvert->pub.color_convert = null_convert;
888 } else /* unsupported non-null conversion */
889 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
893 if (cinfo->quantize_colors)
894 cinfo->output_components = 1; /* single colormapped output component */
896 cinfo->output_components = cinfo->out_color_components;