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
23 /* Private subobject */
26 struct jpeg_color_deconverter pub
; /* public fields */
28 /* Private state for YCC->RGB conversion */
29 int *Cr_r_tab
; /* => table for Cr to R conversion */
30 int *Cb_b_tab
; /* => table for Cb to B conversion */
31 JLONG
*Cr_g_tab
; /* => table for Cr to G conversion */
32 JLONG
*Cb_g_tab
; /* => table for Cb to G conversion */
34 /* Private state for RGB->Y conversion */
35 JLONG
*rgb_y_tab
; /* => table for RGB to Y conversion */
36 } my_color_deconverter
;
38 typedef my_color_deconverter
*my_cconvert_ptr
;
41 /**************** YCbCr -> RGB conversion: most common case **************/
42 /**************** RGB -> Y conversion: less common case **************/
45 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
46 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
47 * The conversion equations to be implemented are therefore
49 * R = Y + 1.40200 * Cr
50 * G = Y - 0.34414 * Cb - 0.71414 * Cr
51 * B = Y + 1.77200 * Cb
53 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
55 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
56 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
58 * To avoid floating-point arithmetic, we represent the fractional constants
59 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
60 * the products by 2^16, with appropriate rounding, to get the correct answer.
61 * Notice that Y, being an integral input, does not contribute any fraction
62 * so it need not participate in the rounding.
64 * For even more speed, we avoid doing any multiplications in the inner loop
65 * by precalculating the constants times Cb and Cr for all possible values.
66 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
67 * for 12-bit samples it is still acceptable. It's not very reasonable for
68 * 16-bit samples, but if you want lossless storage you shouldn't be changing
70 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
71 * values for the G calculation are left scaled up, since we must add them
72 * together before rounding.
75 #define SCALEBITS 16 /* speediest right-shift on some machines */
76 #define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))
77 #define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
79 /* We allocate one big table for RGB->Y conversion and divide it up into
80 * three parts, instead of doing three alloc_small requests. This lets us
81 * use a single table base address, which can be held in a register in the
82 * inner loops on many machines (more than can hold all three addresses,
86 #define R_Y_OFF 0 /* offset to R => Y section */
87 #define G_Y_OFF (1 * (MAXJSAMPLE + 1)) /* offset to G => Y section */
88 #define B_Y_OFF (2 * (MAXJSAMPLE + 1)) /* etc. */
89 #define TABLE_SIZE (3 * (MAXJSAMPLE + 1))
92 /* Include inline routines for colorspace extensions */
100 #define RGB_RED EXT_RGB_RED
101 #define RGB_GREEN EXT_RGB_GREEN
102 #define RGB_BLUE EXT_RGB_BLUE
103 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
104 #define ycc_rgb_convert_internal ycc_extrgb_convert_internal
105 #define gray_rgb_convert_internal gray_extrgb_convert_internal
106 #define rgb_rgb_convert_internal rgb_extrgb_convert_internal
107 #include "jdcolext.c"
112 #undef ycc_rgb_convert_internal
113 #undef gray_rgb_convert_internal
114 #undef rgb_rgb_convert_internal
116 #define RGB_RED EXT_RGBX_RED
117 #define RGB_GREEN EXT_RGBX_GREEN
118 #define RGB_BLUE EXT_RGBX_BLUE
120 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
121 #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
122 #define gray_rgb_convert_internal gray_extrgbx_convert_internal
123 #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
124 #include "jdcolext.c"
130 #undef ycc_rgb_convert_internal
131 #undef gray_rgb_convert_internal
132 #undef rgb_rgb_convert_internal
134 #define RGB_RED EXT_BGR_RED
135 #define RGB_GREEN EXT_BGR_GREEN
136 #define RGB_BLUE EXT_BGR_BLUE
137 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
138 #define ycc_rgb_convert_internal ycc_extbgr_convert_internal
139 #define gray_rgb_convert_internal gray_extbgr_convert_internal
140 #define rgb_rgb_convert_internal rgb_extbgr_convert_internal
141 #include "jdcolext.c"
146 #undef ycc_rgb_convert_internal
147 #undef gray_rgb_convert_internal
148 #undef rgb_rgb_convert_internal
150 #define RGB_RED EXT_BGRX_RED
151 #define RGB_GREEN EXT_BGRX_GREEN
152 #define RGB_BLUE EXT_BGRX_BLUE
154 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
155 #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
156 #define gray_rgb_convert_internal gray_extbgrx_convert_internal
157 #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
158 #include "jdcolext.c"
164 #undef ycc_rgb_convert_internal
165 #undef gray_rgb_convert_internal
166 #undef rgb_rgb_convert_internal
168 #define RGB_RED EXT_XBGR_RED
169 #define RGB_GREEN EXT_XBGR_GREEN
170 #define RGB_BLUE EXT_XBGR_BLUE
172 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
173 #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
174 #define gray_rgb_convert_internal gray_extxbgr_convert_internal
175 #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
176 #include "jdcolext.c"
182 #undef ycc_rgb_convert_internal
183 #undef gray_rgb_convert_internal
184 #undef rgb_rgb_convert_internal
186 #define RGB_RED EXT_XRGB_RED
187 #define RGB_GREEN EXT_XRGB_GREEN
188 #define RGB_BLUE EXT_XRGB_BLUE
190 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
191 #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
192 #define gray_rgb_convert_internal gray_extxrgb_convert_internal
193 #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
194 #include "jdcolext.c"
200 #undef ycc_rgb_convert_internal
201 #undef gray_rgb_convert_internal
202 #undef rgb_rgb_convert_internal
206 * Initialize tables for YCC->RGB colorspace conversion.
210 build_ycc_rgb_table(j_decompress_ptr cinfo
)
212 my_cconvert_ptr cconvert
= (my_cconvert_ptr
)cinfo
->cconvert
;
217 cconvert
->Cr_r_tab
= (int *)
218 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
219 (MAXJSAMPLE
+ 1) * sizeof(int));
220 cconvert
->Cb_b_tab
= (int *)
221 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
222 (MAXJSAMPLE
+ 1) * sizeof(int));
223 cconvert
->Cr_g_tab
= (JLONG
*)
224 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
225 (MAXJSAMPLE
+ 1) * sizeof(JLONG
));
226 cconvert
->Cb_g_tab
= (JLONG
*)
227 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
228 (MAXJSAMPLE
+ 1) * sizeof(JLONG
));
230 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
231 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
232 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
233 /* Cr=>R value is nearest int to 1.40200 * x */
234 cconvert
->Cr_r_tab
[i
] = (int)
235 RIGHT_SHIFT(FIX(1.40200) * x
+ ONE_HALF
, SCALEBITS
);
236 /* Cb=>B value is nearest int to 1.77200 * x */
237 cconvert
->Cb_b_tab
[i
] = (int)
238 RIGHT_SHIFT(FIX(1.77200) * x
+ ONE_HALF
, SCALEBITS
);
239 /* Cr=>G value is scaled-up -0.71414 * x */
240 cconvert
->Cr_g_tab
[i
] = (-FIX(0.71414)) * x
;
241 /* Cb=>G value is scaled-up -0.34414 * x */
242 /* We also add in ONE_HALF so that need not do it in inner loop */
243 cconvert
->Cb_g_tab
[i
] = (-FIX(0.34414)) * x
+ ONE_HALF
;
249 * Convert some rows of samples to the output colorspace.
253 ycc_rgb_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
254 JDIMENSION input_row
, 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
= (JLONG
*)
309 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
310 (TABLE_SIZE
* sizeof(JLONG
)));
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
, JSAMPIMAGE input_buf
,
326 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
328 my_cconvert_ptr cconvert
= (my_cconvert_ptr
)cinfo
->cconvert
;
329 register int r
, g
, b
;
330 register JLONG
*ctab
= cconvert
->rgb_y_tab
;
331 register JSAMPROW outptr
;
332 register JSAMPROW inptr0
, inptr1
, inptr2
;
333 register JDIMENSION col
;
334 JDIMENSION num_cols
= cinfo
->output_width
;
336 while (--num_rows
>= 0) {
337 inptr0
= input_buf
[0][input_row
];
338 inptr1
= input_buf
[1][input_row
];
339 inptr2
= input_buf
[2][input_row
];
341 outptr
= *output_buf
++;
342 for (col
= 0; col
< num_cols
; col
++) {
347 outptr
[col
] = (JSAMPLE
)((ctab
[r
+ R_Y_OFF
] + ctab
[g
+ G_Y_OFF
] +
348 ctab
[b
+ B_Y_OFF
]) >> SCALEBITS
);
355 * Color conversion for no colorspace change: just copy the data,
356 * converting from separate-planes to interleaved representation.
360 null_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
361 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
363 register JSAMPROW inptr
, inptr0
, inptr1
, inptr2
, inptr3
, outptr
;
364 register JDIMENSION col
;
365 register int num_components
= cinfo
->num_components
;
366 JDIMENSION num_cols
= cinfo
->output_width
;
369 if (num_components
== 3) {
370 while (--num_rows
>= 0) {
371 inptr0
= input_buf
[0][input_row
];
372 inptr1
= input_buf
[1][input_row
];
373 inptr2
= input_buf
[2][input_row
];
375 outptr
= *output_buf
++;
376 for (col
= 0; col
< num_cols
; col
++) {
377 *outptr
++ = inptr0
[col
];
378 *outptr
++ = inptr1
[col
];
379 *outptr
++ = inptr2
[col
];
382 } else if (num_components
== 4) {
383 while (--num_rows
>= 0) {
384 inptr0
= input_buf
[0][input_row
];
385 inptr1
= input_buf
[1][input_row
];
386 inptr2
= input_buf
[2][input_row
];
387 inptr3
= input_buf
[3][input_row
];
389 outptr
= *output_buf
++;
390 for (col
= 0; col
< num_cols
; col
++) {
391 *outptr
++ = inptr0
[col
];
392 *outptr
++ = inptr1
[col
];
393 *outptr
++ = inptr2
[col
];
394 *outptr
++ = inptr3
[col
];
398 while (--num_rows
>= 0) {
399 for (ci
= 0; ci
< num_components
; ci
++) {
400 inptr
= input_buf
[ci
][input_row
];
401 outptr
= *output_buf
;
402 for (col
= 0; col
< num_cols
; col
++) {
403 outptr
[ci
] = inptr
[col
];
404 outptr
+= num_components
;
415 * Color conversion for grayscale: just copy the data.
416 * This also works for YCbCr -> grayscale conversion, in which
417 * we just copy the Y (luminance) component and ignore chrominance.
421 grayscale_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
422 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
424 jcopy_sample_rows(input_buf
[0], (int)input_row
, output_buf
, 0, num_rows
,
425 cinfo
->output_width
);
430 * Convert grayscale to RGB
434 gray_rgb_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
435 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
437 switch (cinfo
->out_color_space
) {
439 gray_extrgb_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
444 gray_extrgbx_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
448 gray_extbgr_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
453 gray_extbgrx_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
458 gray_extxbgr_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
463 gray_extxrgb_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
467 gray_rgb_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
475 * Convert plain RGB to extended RGB
479 rgb_rgb_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
480 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
482 switch (cinfo
->out_color_space
) {
484 rgb_extrgb_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
489 rgb_extrgbx_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
493 rgb_extbgr_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
498 rgb_extbgrx_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
503 rgb_extxbgr_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
508 rgb_extxrgb_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
512 rgb_rgb_convert_internal(cinfo
, input_buf
, input_row
, output_buf
,
520 * Adobe-style YCCK->CMYK conversion.
521 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
522 * conversion as above, while passing K (black) unchanged.
523 * We assume build_ycc_rgb_table has been called.
527 ycck_cmyk_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
528 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
530 my_cconvert_ptr cconvert
= (my_cconvert_ptr
)cinfo
->cconvert
;
531 register int y
, cb
, cr
;
532 register JSAMPROW outptr
;
533 register JSAMPROW inptr0
, inptr1
, inptr2
, inptr3
;
534 register JDIMENSION col
;
535 JDIMENSION num_cols
= cinfo
->output_width
;
536 /* copy these pointers into registers if possible */
537 register JSAMPLE
*range_limit
= cinfo
->sample_range_limit
;
538 register int *Crrtab
= cconvert
->Cr_r_tab
;
539 register int *Cbbtab
= cconvert
->Cb_b_tab
;
540 register JLONG
*Crgtab
= cconvert
->Cr_g_tab
;
541 register JLONG
*Cbgtab
= cconvert
->Cb_g_tab
;
544 while (--num_rows
>= 0) {
545 inptr0
= input_buf
[0][input_row
];
546 inptr1
= input_buf
[1][input_row
];
547 inptr2
= input_buf
[2][input_row
];
548 inptr3
= input_buf
[3][input_row
];
550 outptr
= *output_buf
++;
551 for (col
= 0; col
< num_cols
; col
++) {
555 /* Range-limiting is essential due to noise introduced by DCT losses. */
556 outptr
[0] = range_limit
[MAXJSAMPLE
- (y
+ Crrtab
[cr
])]; /* red */
557 outptr
[1] = range_limit
[MAXJSAMPLE
- (y
+ /* green */
558 ((int)RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
],
560 outptr
[2] = range_limit
[MAXJSAMPLE
- (y
+ Cbbtab
[cb
])]; /* blue */
561 /* K passes through unchanged */
562 outptr
[3] = inptr3
[col
];
573 #define PACK_SHORT_565_LE(r, g, b) \
574 ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
575 #define PACK_SHORT_565_BE(r, g, b) \
576 (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
578 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
579 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
581 #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
583 #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
585 #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
586 #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
587 #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
590 /* Declarations for ordered dithering
592 * We use a 4x4 ordered dither array packed into 32 bits. This array is
593 * sufficient for dithering RGB888 to RGB565.
596 #define DITHER_MASK 0x3
597 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
598 static const JLONG dither_matrix
[4] = {
606 static INLINE boolean
is_big_endian(void)
609 if (*(char *)&test_value
!= 1)
615 /* Include inline routines for RGB565 conversion */
617 #define PACK_SHORT_565 PACK_SHORT_565_LE
618 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
619 #define ycc_rgb565_convert_internal ycc_rgb565_convert_le
620 #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
621 #define rgb_rgb565_convert_internal rgb_rgb565_convert_le
622 #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
623 #define gray_rgb565_convert_internal gray_rgb565_convert_le
624 #define gray_rgb565D_convert_internal gray_rgb565D_convert_le
625 #include "jdcol565.c"
626 #undef PACK_SHORT_565
627 #undef PACK_TWO_PIXELS
628 #undef ycc_rgb565_convert_internal
629 #undef ycc_rgb565D_convert_internal
630 #undef rgb_rgb565_convert_internal
631 #undef rgb_rgb565D_convert_internal
632 #undef gray_rgb565_convert_internal
633 #undef gray_rgb565D_convert_internal
635 #define PACK_SHORT_565 PACK_SHORT_565_BE
636 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
637 #define ycc_rgb565_convert_internal ycc_rgb565_convert_be
638 #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
639 #define rgb_rgb565_convert_internal rgb_rgb565_convert_be
640 #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
641 #define gray_rgb565_convert_internal gray_rgb565_convert_be
642 #define gray_rgb565D_convert_internal gray_rgb565D_convert_be
643 #include "jdcol565.c"
644 #undef PACK_SHORT_565
645 #undef PACK_TWO_PIXELS
646 #undef ycc_rgb565_convert_internal
647 #undef ycc_rgb565D_convert_internal
648 #undef rgb_rgb565_convert_internal
649 #undef rgb_rgb565D_convert_internal
650 #undef gray_rgb565_convert_internal
651 #undef gray_rgb565D_convert_internal
655 ycc_rgb565_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
656 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
659 ycc_rgb565_convert_be(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
661 ycc_rgb565_convert_le(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
666 ycc_rgb565D_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
667 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
670 ycc_rgb565D_convert_be(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
672 ycc_rgb565D_convert_le(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
677 rgb_rgb565_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
678 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
681 rgb_rgb565_convert_be(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
683 rgb_rgb565_convert_le(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
688 rgb_rgb565D_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
689 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
692 rgb_rgb565D_convert_be(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
694 rgb_rgb565D_convert_le(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
699 gray_rgb565_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
700 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
703 gray_rgb565_convert_be(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
705 gray_rgb565_convert_le(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
710 gray_rgb565D_convert(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
711 JDIMENSION input_row
, JSAMPARRAY output_buf
, int num_rows
)
714 gray_rgb565D_convert_be(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
716 gray_rgb565D_convert_le(cinfo
, input_buf
, input_row
, output_buf
, num_rows
);
721 * Empty method for start_pass.
725 start_pass_dcolor(j_decompress_ptr cinfo
)
732 * Module initialization routine for output colorspace conversion.
736 jinit_color_deconverter(j_decompress_ptr cinfo
)
738 my_cconvert_ptr cconvert
;
741 cconvert
= (my_cconvert_ptr
)
742 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
743 sizeof(my_color_deconverter
));
744 cinfo
->cconvert
= (struct jpeg_color_deconverter
*)cconvert
;
745 cconvert
->pub
.start_pass
= start_pass_dcolor
;
747 /* Make sure num_components agrees with jpeg_color_space */
748 switch (cinfo
->jpeg_color_space
) {
750 if (cinfo
->num_components
!= 1)
751 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
756 if (cinfo
->num_components
!= 3)
757 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
762 if (cinfo
->num_components
!= 4)
763 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
766 default: /* JCS_UNKNOWN can be anything */
767 if (cinfo
->num_components
< 1)
768 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
772 /* Set out_color_components and conversion method based on requested space.
773 * Also clear the component_needed flags for any unused components,
774 * so that earlier pipeline stages can avoid useless computation.
777 switch (cinfo
->out_color_space
) {
779 cinfo
->out_color_components
= 1;
780 if (cinfo
->jpeg_color_space
== JCS_GRAYSCALE
||
781 cinfo
->jpeg_color_space
== JCS_YCbCr
) {
782 cconvert
->pub
.color_convert
= grayscale_convert
;
783 /* For color->grayscale conversion, only the Y (0) component is needed */
784 for (ci
= 1; ci
< cinfo
->num_components
; ci
++)
785 cinfo
->comp_info
[ci
].component_needed
= FALSE
;
786 } else if (cinfo
->jpeg_color_space
== JCS_RGB
) {
787 cconvert
->pub
.color_convert
= rgb_gray_convert
;
788 build_rgb_y_table(cinfo
);
790 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
804 cinfo
->out_color_components
= rgb_pixelsize
[cinfo
->out_color_space
];
805 if (cinfo
->jpeg_color_space
== JCS_YCbCr
) {
806 if (jsimd_can_ycc_rgb())
807 cconvert
->pub
.color_convert
= jsimd_ycc_rgb_convert
;
809 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
810 build_ycc_rgb_table(cinfo
);
812 } else if (cinfo
->jpeg_color_space
== JCS_GRAYSCALE
) {
813 cconvert
->pub
.color_convert
= gray_rgb_convert
;
814 } else if (cinfo
->jpeg_color_space
== JCS_RGB
) {
815 if (rgb_red
[cinfo
->out_color_space
] == 0 &&
816 rgb_green
[cinfo
->out_color_space
] == 1 &&
817 rgb_blue
[cinfo
->out_color_space
] == 2 &&
818 rgb_pixelsize
[cinfo
->out_color_space
] == 3)
819 cconvert
->pub
.color_convert
= null_convert
;
821 cconvert
->pub
.color_convert
= rgb_rgb_convert
;
823 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
827 cinfo
->out_color_components
= 3;
828 if (cinfo
->dither_mode
== JDITHER_NONE
) {
829 if (cinfo
->jpeg_color_space
== JCS_YCbCr
) {
830 if (jsimd_can_ycc_rgb565())
831 cconvert
->pub
.color_convert
= jsimd_ycc_rgb565_convert
;
833 cconvert
->pub
.color_convert
= ycc_rgb565_convert
;
834 build_ycc_rgb_table(cinfo
);
836 } else if (cinfo
->jpeg_color_space
== JCS_GRAYSCALE
) {
837 cconvert
->pub
.color_convert
= gray_rgb565_convert
;
838 } else if (cinfo
->jpeg_color_space
== JCS_RGB
) {
839 cconvert
->pub
.color_convert
= rgb_rgb565_convert
;
841 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
843 /* only ordered dithering is supported */
844 if (cinfo
->jpeg_color_space
== JCS_YCbCr
) {
845 cconvert
->pub
.color_convert
= ycc_rgb565D_convert
;
846 build_ycc_rgb_table(cinfo
);
847 } else if (cinfo
->jpeg_color_space
== JCS_GRAYSCALE
) {
848 cconvert
->pub
.color_convert
= gray_rgb565D_convert
;
849 } else if (cinfo
->jpeg_color_space
== JCS_RGB
) {
850 cconvert
->pub
.color_convert
= rgb_rgb565D_convert
;
852 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
857 cinfo
->out_color_components
= 4;
858 if (cinfo
->jpeg_color_space
== JCS_YCCK
) {
859 cconvert
->pub
.color_convert
= ycck_cmyk_convert
;
860 build_ycc_rgb_table(cinfo
);
861 } else if (cinfo
->jpeg_color_space
== JCS_CMYK
) {
862 cconvert
->pub
.color_convert
= null_convert
;
864 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
868 /* Permit null conversion to same output space */
869 if (cinfo
->out_color_space
== cinfo
->jpeg_color_space
) {
870 cinfo
->out_color_components
= cinfo
->num_components
;
871 cconvert
->pub
.color_convert
= null_convert
;
872 } else /* unsupported non-null conversion */
873 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
877 if (cinfo
->quantize_colors
)
878 cinfo
->output_components
= 1; /* single colormapped output component */
880 cinfo
->output_components
= cinfo
->out_color_components
;