no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / media / libjpeg / jdcolor.c
blob735190b700099a0f3555835caf242138ad34ccff
1 /*
2 * jdcolor.c
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
12 * file.
14 * This file contains output colorspace conversion routines.
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jsimd.h"
23 /* Private subobject */
25 typedef struct {
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
69 * colorspace anyway.
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,
83 * anyway).
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 */
94 #include "jdcolext.c"
95 #undef RGB_RED
96 #undef RGB_GREEN
97 #undef RGB_BLUE
98 #undef RGB_PIXELSIZE
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"
108 #undef RGB_RED
109 #undef RGB_GREEN
110 #undef RGB_BLUE
111 #undef RGB_PIXELSIZE
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
119 #define RGB_ALPHA 3
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"
125 #undef RGB_RED
126 #undef RGB_GREEN
127 #undef RGB_BLUE
128 #undef RGB_ALPHA
129 #undef RGB_PIXELSIZE
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"
142 #undef RGB_RED
143 #undef RGB_GREEN
144 #undef RGB_BLUE
145 #undef RGB_PIXELSIZE
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
153 #define RGB_ALPHA 3
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"
159 #undef RGB_RED
160 #undef RGB_GREEN
161 #undef RGB_BLUE
162 #undef RGB_ALPHA
163 #undef RGB_PIXELSIZE
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
171 #define RGB_ALPHA 0
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"
177 #undef RGB_RED
178 #undef RGB_GREEN
179 #undef RGB_BLUE
180 #undef RGB_ALPHA
181 #undef RGB_PIXELSIZE
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
189 #define RGB_ALPHA 0
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"
195 #undef RGB_RED
196 #undef RGB_GREEN
197 #undef RGB_BLUE
198 #undef RGB_ALPHA
199 #undef RGB_PIXELSIZE
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.
209 LOCAL(void)
210 build_ycc_rgb_table(j_decompress_ptr cinfo)
212 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
213 int i;
214 JLONG x;
215 SHIFT_TEMPS
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.
252 METHODDEF(void)
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) {
257 case JCS_EXT_RGB:
258 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
259 num_rows);
260 break;
261 case JCS_EXT_RGBX:
262 case JCS_EXT_RGBA:
263 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
264 num_rows);
265 break;
266 case JCS_EXT_BGR:
267 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
268 num_rows);
269 break;
270 case JCS_EXT_BGRX:
271 case JCS_EXT_BGRA:
272 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
273 num_rows);
274 break;
275 case JCS_EXT_XBGR:
276 case JCS_EXT_ABGR:
277 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
278 num_rows);
279 break;
280 case JCS_EXT_XRGB:
281 case JCS_EXT_ARGB:
282 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
283 num_rows);
284 break;
285 default:
286 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
287 num_rows);
288 break;
293 /**************** Cases other than YCbCr -> RGB **************/
297 * Initialize for RGB->grayscale colorspace conversion.
300 LOCAL(void)
301 build_rgb_y_table(j_decompress_ptr cinfo)
303 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
304 JLONG *rgb_y_tab;
305 JLONG i;
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.
324 METHODDEF(void)
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];
340 input_row++;
341 outptr = *output_buf++;
342 for (col = 0; col < num_cols; col++) {
343 r = inptr0[col];
344 g = inptr1[col];
345 b = inptr2[col];
346 /* Y */
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.
359 METHODDEF(void)
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;
367 int ci;
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];
374 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];
388 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];
397 } else {
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;
407 output_buf++;
408 input_row++;
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.
420 METHODDEF(void)
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
433 METHODDEF(void)
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) {
438 case JCS_EXT_RGB:
439 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
440 num_rows);
441 break;
442 case JCS_EXT_RGBX:
443 case JCS_EXT_RGBA:
444 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
445 num_rows);
446 break;
447 case JCS_EXT_BGR:
448 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
449 num_rows);
450 break;
451 case JCS_EXT_BGRX:
452 case JCS_EXT_BGRA:
453 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
454 num_rows);
455 break;
456 case JCS_EXT_XBGR:
457 case JCS_EXT_ABGR:
458 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
459 num_rows);
460 break;
461 case JCS_EXT_XRGB:
462 case JCS_EXT_ARGB:
463 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
464 num_rows);
465 break;
466 default:
467 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
468 num_rows);
469 break;
475 * Convert plain RGB to extended RGB
478 METHODDEF(void)
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) {
483 case JCS_EXT_RGB:
484 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
485 num_rows);
486 break;
487 case JCS_EXT_RGBX:
488 case JCS_EXT_RGBA:
489 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
490 num_rows);
491 break;
492 case JCS_EXT_BGR:
493 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
494 num_rows);
495 break;
496 case JCS_EXT_BGRX:
497 case JCS_EXT_BGRA:
498 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
499 num_rows);
500 break;
501 case JCS_EXT_XBGR:
502 case JCS_EXT_ABGR:
503 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
504 num_rows);
505 break;
506 case JCS_EXT_XRGB:
507 case JCS_EXT_ARGB:
508 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
509 num_rows);
510 break;
511 default:
512 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
513 num_rows);
514 break;
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.
526 METHODDEF(void)
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;
542 SHIFT_TEMPS
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];
549 input_row++;
550 outptr = *output_buf++;
551 for (col = 0; col < num_cols; col++) {
552 y = inptr0[col];
553 cb = inptr1[col];
554 cr = inptr2[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],
559 SCALEBITS)))];
560 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
561 /* K passes through unchanged */
562 outptr[3] = inptr3[col];
563 outptr += 4;
570 * RGB565 conversion
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] = {
599 0x0008020A,
600 0x0C040E06,
601 0x030B0109,
602 0x0F070D05
606 static INLINE boolean is_big_endian(void)
608 int test_value = 1;
609 if (*(char *)&test_value != 1)
610 return TRUE;
611 return FALSE;
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
654 METHODDEF(void)
655 ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
656 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
658 if (is_big_endian())
659 ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
660 else
661 ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
665 METHODDEF(void)
666 ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
667 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
669 if (is_big_endian())
670 ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
671 else
672 ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
676 METHODDEF(void)
677 rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
678 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
680 if (is_big_endian())
681 rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
682 else
683 rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
687 METHODDEF(void)
688 rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
689 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
691 if (is_big_endian())
692 rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
693 else
694 rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
698 METHODDEF(void)
699 gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
700 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
702 if (is_big_endian())
703 gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
704 else
705 gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
709 METHODDEF(void)
710 gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
711 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
713 if (is_big_endian())
714 gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
715 else
716 gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
721 * Empty method for start_pass.
724 METHODDEF(void)
725 start_pass_dcolor(j_decompress_ptr cinfo)
727 /* no work needed */
732 * Module initialization routine for output colorspace conversion.
735 GLOBAL(void)
736 jinit_color_deconverter(j_decompress_ptr cinfo)
738 my_cconvert_ptr cconvert;
739 int ci;
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) {
749 case JCS_GRAYSCALE:
750 if (cinfo->num_components != 1)
751 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
752 break;
754 case JCS_RGB:
755 case JCS_YCbCr:
756 if (cinfo->num_components != 3)
757 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
758 break;
760 case JCS_CMYK:
761 case JCS_YCCK:
762 if (cinfo->num_components != 4)
763 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
764 break;
766 default: /* JCS_UNKNOWN can be anything */
767 if (cinfo->num_components < 1)
768 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
769 break;
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) {
778 case JCS_GRAYSCALE:
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);
789 } else
790 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
791 break;
793 case JCS_RGB:
794 case JCS_EXT_RGB:
795 case JCS_EXT_RGBX:
796 case JCS_EXT_BGR:
797 case JCS_EXT_BGRX:
798 case JCS_EXT_XBGR:
799 case JCS_EXT_XRGB:
800 case JCS_EXT_RGBA:
801 case JCS_EXT_BGRA:
802 case JCS_EXT_ABGR:
803 case JCS_EXT_ARGB:
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;
808 else {
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;
820 else
821 cconvert->pub.color_convert = rgb_rgb_convert;
822 } else
823 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
824 break;
826 case JCS_RGB565:
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;
832 else {
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;
840 } else
841 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
842 } else {
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;
851 } else
852 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
854 break;
856 case JCS_CMYK:
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;
863 } else
864 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
865 break;
867 default:
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);
874 break;
877 if (cinfo->quantize_colors)
878 cinfo->output_components = 1; /* single colormapped output component */
879 else
880 cinfo->output_components = cinfo->out_color_components;