1 /*
2 * jdcolor.c
3 *
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.
13 *
14 * This file contains output colorspace conversion routines.
15 */
16
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jsimd.h"
21 #include "jconfigint.h"
22
23
24 /* Private subobject */
25
26 typedef struct {
27 struct jpeg_color_deconverter pub; /* public fields */
28
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 */
34
35 /* Private state for RGB->Y conversion */
36 JLONG *rgb_y_tab; /* => table for RGB to Y conversion */
37 } my_color_deconverter;
38
39 typedef my_color_deconverter *my_cconvert_ptr;
40
41
42 /**************** YCbCr -> RGB conversion: most common case **************/
43 /**************** RGB -> Y conversion: less common case **************/
44
45 /*
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
49 *
50 * R = Y + 1.40200 * Cr
51 * G = Y - 0.34414 * Cb - 0.71414 * Cr
52 * B = Y + 1.77200 * Cb
53 *
54 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
55 *
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.)
58 *
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.
64 *
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
70 * colorspace anyway.
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.
74 */
75
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))
79
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,
84 * anyway).
85 */
86
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))
91
92
93 /* Include inline routines for colorspace extensions */
94
95 #include "jdcolext.c"
96 #undef RGB_RED
97 #undef RGB_GREEN
98 #undef RGB_BLUE
99 #undef RGB_PIXELSIZE
100
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"
109 #undef RGB_RED
110 #undef RGB_GREEN
111 #undef RGB_BLUE
112 #undef RGB_PIXELSIZE
113 #undef ycc_rgb_convert_internal
114 #undef gray_rgb_convert_internal
115 #undef rgb_rgb_convert_internal
116
117 #define RGB_RED EXT_RGBX_RED
118 #define RGB_GREEN EXT_RGBX_GREEN
119 #define RGB_BLUE EXT_RGBX_BLUE
120 #define RGB_ALPHA 3
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"
126 #undef RGB_RED
127 #undef RGB_GREEN
128 #undef RGB_BLUE
129 #undef RGB_ALPHA
130 #undef RGB_PIXELSIZE
131 #undef ycc_rgb_convert_internal
132 #undef gray_rgb_convert_internal
133 #undef rgb_rgb_convert_internal
134
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"
143 #undef RGB_RED
144 #undef RGB_GREEN
145 #undef RGB_BLUE
146 #undef RGB_PIXELSIZE
147 #undef ycc_rgb_convert_internal
148 #undef gray_rgb_convert_internal
149 #undef rgb_rgb_convert_internal
150
151 #define RGB_RED EXT_BGRX_RED
152 #define RGB_GREEN EXT_BGRX_GREEN
153 #define RGB_BLUE EXT_BGRX_BLUE
154 #define RGB_ALPHA 3
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"
160 #undef RGB_RED
161 #undef RGB_GREEN
162 #undef RGB_BLUE
163 #undef RGB_ALPHA
164 #undef RGB_PIXELSIZE
165 #undef ycc_rgb_convert_internal
166 #undef gray_rgb_convert_internal
167 #undef rgb_rgb_convert_internal
168
169 #define RGB_RED EXT_XBGR_RED
170 #define RGB_GREEN EXT_XBGR_GREEN
171 #define RGB_BLUE EXT_XBGR_BLUE
172 #define RGB_ALPHA 0
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"
178 #undef RGB_RED
179 #undef RGB_GREEN
180 #undef RGB_BLUE
181 #undef RGB_ALPHA
182 #undef RGB_PIXELSIZE
183 #undef ycc_rgb_convert_internal
184 #undef gray_rgb_convert_internal
185 #undef rgb_rgb_convert_internal
186
187 #define RGB_RED EXT_XRGB_RED
188 #define RGB_GREEN EXT_XRGB_GREEN
189 #define RGB_BLUE EXT_XRGB_BLUE
190 #define RGB_ALPHA 0
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"
196 #undef RGB_RED
197 #undef RGB_GREEN
198 #undef RGB_BLUE
199 #undef RGB_ALPHA
200 #undef RGB_PIXELSIZE
201 #undef ycc_rgb_convert_internal
202 #undef gray_rgb_convert_internal
203 #undef rgb_rgb_convert_internal
204
205
206 /*
207 * Initialize tables for YCC->RGB colorspace conversion.
208 */
209
210 LOCAL(void)
build_ycc_rgb_table(j_decompress_ptr cinfo)211 build_ycc_rgb_table(j_decompress_ptr cinfo)
212 {
213 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
214 int i;
215 JLONG x;
216 SHIFT_TEMPS
217
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));
230
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;
245 }
246 }
247
248
249 /*
250 * Convert some rows of samples to the output colorspace.
251 */
252
253 METHODDEF(void)
ycc_rgb_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)254 ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
255 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
256 {
257 switch (cinfo->out_color_space) {
258 case JCS_EXT_RGB:
259 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
260 num_rows);
261 break;
262 case JCS_EXT_RGBX:
263 case JCS_EXT_RGBA:
264 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
265 num_rows);
266 break;
267 case JCS_EXT_BGR:
268 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
269 num_rows);
270 break;
271 case JCS_EXT_BGRX:
272 case JCS_EXT_BGRA:
273 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
274 num_rows);
275 break;
276 case JCS_EXT_XBGR:
277 case JCS_EXT_ABGR:
278 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
279 num_rows);
280 break;
281 case JCS_EXT_XRGB:
282 case JCS_EXT_ARGB:
283 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
284 num_rows);
285 break;
286 default:
287 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
288 num_rows);
289 break;
290 }
291 }
292
293
294 /**************** Cases other than YCbCr -> RGB **************/
295
296
297 /*
298 * Initialize for RGB->grayscale colorspace conversion.
299 */
300
301 LOCAL(void)
build_rgb_y_table(j_decompress_ptr cinfo)302 build_rgb_y_table(j_decompress_ptr cinfo)
303 {
304 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
305 JLONG *rgb_y_tab;
306 JLONG i;
307
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)));
312
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;
317 }
318 }
319
320
321 /*
322 * Convert RGB to grayscale.
323 */
324
325 METHODDEF(void)
rgb_gray_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)326 rgb_gray_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
327 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
328 {
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;
336
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];
341 input_row++;
342 outptr = *output_buf++;
343 for (col = 0; col < num_cols; col++) {
344 r = inptr0[col];
345 g = inptr1[col];
346 b = inptr2[col];
347 /* Y */
348 outptr[col] = (JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
349 ctab[b + B_Y_OFF]) >> SCALEBITS);
350 }
351 }
352 }
353
354
355 /*
356 * Color conversion for no colorspace change: just copy the data,
357 * converting from separate-planes to interleaved representation.
358 */
359
360 METHODDEF(void)
null_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)361 null_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
362 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
363 {
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;
368 int ci;
369
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];
375 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];
381 }
382 }
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];
389 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];
396 }
397 }
398 } else {
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;
406 }
407 }
408 output_buf++;
409 input_row++;
410 }
411 }
412 }
413
414
415 /*
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.
419 */
420
421 METHODDEF(void)
grayscale_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)422 grayscale_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
423 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
424 {
425 jcopy_sample_rows(input_buf[0], (int)input_row, output_buf, 0, num_rows,
426 cinfo->output_width);
427 }
428
429
430 /*
431 * Convert grayscale to RGB
432 */
433
434 METHODDEF(void)
gray_rgb_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)435 gray_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
436 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
437 {
438 switch (cinfo->out_color_space) {
439 case JCS_EXT_RGB:
440 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
441 num_rows);
442 break;
443 case JCS_EXT_RGBX:
444 case JCS_EXT_RGBA:
445 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
446 num_rows);
447 break;
448 case JCS_EXT_BGR:
449 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
450 num_rows);
451 break;
452 case JCS_EXT_BGRX:
453 case JCS_EXT_BGRA:
454 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
455 num_rows);
456 break;
457 case JCS_EXT_XBGR:
458 case JCS_EXT_ABGR:
459 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
460 num_rows);
461 break;
462 case JCS_EXT_XRGB:
463 case JCS_EXT_ARGB:
464 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
465 num_rows);
466 break;
467 default:
468 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
469 num_rows);
470 break;
471 }
472 }
473
474
475 /*
476 * Convert plain RGB to extended RGB
477 */
478
479 METHODDEF(void)
rgb_rgb_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)480 rgb_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
481 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
482 {
483 switch (cinfo->out_color_space) {
484 case JCS_EXT_RGB:
485 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
486 num_rows);
487 break;
488 case JCS_EXT_RGBX:
489 case JCS_EXT_RGBA:
490 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
491 num_rows);
492 break;
493 case JCS_EXT_BGR:
494 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
495 num_rows);
496 break;
497 case JCS_EXT_BGRX:
498 case JCS_EXT_BGRA:
499 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
500 num_rows);
501 break;
502 case JCS_EXT_XBGR:
503 case JCS_EXT_ABGR:
504 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
505 num_rows);
506 break;
507 case JCS_EXT_XRGB:
508 case JCS_EXT_ARGB:
509 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
510 num_rows);
511 break;
512 default:
513 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
514 num_rows);
515 break;
516 }
517 }
518
519
520 /*
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.
525 */
526
527 METHODDEF(void)
ycck_cmyk_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)528 ycck_cmyk_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
529 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
530 {
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;
543 SHIFT_TEMPS
544
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];
550 input_row++;
551 outptr = *output_buf++;
552 for (col = 0; col < num_cols; col++) {
553 y = inptr0[col];
554 cb = inptr1[col];
555 cr = inptr2[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],
560 SCALEBITS)))];
561 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
562 /* K passes through unchanged */
563 outptr[3] = inptr3[col];
564 outptr += 4;
565 }
566 }
567 }
568
569
570 /*
571 * RGB565 conversion
572 */
573
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))
578
579 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
580 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
581
582 #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
583
584 #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
585
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))
589
590
591 /* Declarations for ordered dithering
592 *
593 * We use a 4x4 ordered dither array packed into 32 bits. This array is
594 * sufficient for dithering RGB888 to RGB565.
595 */
596
597 #define DITHER_MASK 0x3
598 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
599 static const JLONG dither_matrix[4] = {
600 0x0008020A,
601 0x0C040E06,
602 0x030B0109,
603 0x0F070D05
604 };
605
606
is_big_endian(void)607 static INLINE boolean is_big_endian(void)
608 {
609 int test_value = 1;
610 if (*(char *)&test_value != 1)
611 return TRUE;
612 return FALSE;
613 }
614
615
616 /* Include inline routines for RGB565 conversion */
617
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
635
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
653
654
655 METHODDEF(void)
ycc_rgb565_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)656 ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
657 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
658 {
659 if (is_big_endian())
660 ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
661 else
662 ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
663 }
664
665
666 METHODDEF(void)
ycc_rgb565D_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)667 ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
668 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
669 {
670 if (is_big_endian())
671 ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
672 else
673 ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
674 }
675
676
677 METHODDEF(void)
rgb_rgb565_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)678 rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
679 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
680 {
681 if (is_big_endian())
682 rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
683 else
684 rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
685 }
686
687
688 METHODDEF(void)
rgb_rgb565D_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)689 rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
690 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
691 {
692 if (is_big_endian())
693 rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
694 else
695 rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
696 }
697
698
699 METHODDEF(void)
gray_rgb565_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)700 gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
701 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
702 {
703 if (is_big_endian())
704 gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
705 else
706 gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
707 }
708
709
710 METHODDEF(void)
gray_rgb565D_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)711 gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
712 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
713 {
714 if (is_big_endian())
715 gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
716 else
717 gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
718 }
719
720
721 /*
722 * Empty method for start_pass.
723 */
724
725 METHODDEF(void)
start_pass_dcolor(j_decompress_ptr cinfo)726 start_pass_dcolor(j_decompress_ptr cinfo)
727 {
728 /* no work needed */
729 }
730
731
732 /*
733 * Module initialization routine for output colorspace conversion.
734 */
735
736 GLOBAL(void)
jinit_color_deconverter(j_decompress_ptr cinfo)737 jinit_color_deconverter(j_decompress_ptr cinfo)
738 {
739 my_cconvert_ptr cconvert;
740 int ci;
741
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;
747
748 /* Make sure num_components agrees with jpeg_color_space */
749 switch (cinfo->jpeg_color_space) {
750 case JCS_GRAYSCALE:
751 if (cinfo->num_components != 1)
752 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
753 break;
754
755 case JCS_RGB:
756 case JCS_YCbCr:
757 if (cinfo->num_components != 3)
758 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
759 break;
760
761 case JCS_CMYK:
762 case JCS_YCCK:
763 if (cinfo->num_components != 4)
764 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
765 break;
766
767 default: /* JCS_UNKNOWN can be anything */
768 if (cinfo->num_components < 1)
769 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
770 break;
771 }
772
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.
776 */
777
778 switch (cinfo->out_color_space) {
779 case JCS_GRAYSCALE:
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);
790 } else
791 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
792 break;
793
794 case JCS_RGB:
795 case JCS_EXT_RGB:
796 case JCS_EXT_RGBX:
797 case JCS_EXT_BGR:
798 case JCS_EXT_BGRX:
799 case JCS_EXT_XBGR:
800 case JCS_EXT_XRGB:
801 case JCS_EXT_RGBA:
802 case JCS_EXT_BGRA:
803 case JCS_EXT_ABGR:
804 case JCS_EXT_ARGB:
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;
809 else {
810 cconvert->pub.color_convert = ycc_rgb_convert;
811 build_ycc_rgb_table(cinfo);
812 }
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;
821 else
822 cconvert->pub.color_convert = rgb_rgb_convert;
823 } else
824 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
825 break;
826
827 case JCS_RGB565:
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;
833 else {
834 cconvert->pub.color_convert = ycc_rgb565_convert;
835 build_ycc_rgb_table(cinfo);
836 }
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;
841 } else
842 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
843 } else {
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;
852 } else
853 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
854 }
855 break;
856
857 case JCS_CMYK:
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;
864 } else
865 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
866 break;
867
868 default:
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);
875 break;
876 }
877
878 if (cinfo->quantize_colors)
879 cinfo->output_components = 1; /* single colormapped output component */
880 else
881 cinfo->output_components = cinfo->out_color_components;
882 }
883