blob: 8da2b4eaf2e9194115e6f78e9e1085d24081931c [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * jdcolor.c
3 *
noel@chromium.org3395bcc2014-04-14 06:56:00 +00004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
noel@chromium.org3395bcc2014-04-14 06:56:00 +00006 * Modified 2011 by Guido Vollbeding.
7 * libjpeg-turbo Modifications:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00008 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
Tom Hudson0d47d2d2016-05-04 13:22:56 -04009 * Copyright (C) 2009, 2011-2012, 2014-2015, D. R. Commander.
Aaron Gablefeec46f2015-08-06 09:54:48 -070010 * Copyright (C) 2013, Linaro Limited.
Tom Hudson0d47d2d2016-05-04 13:22:56 -040011 * For conditions of distribution and use, see the accompanying README.ijg
12 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000013 *
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"
Tom Hudson0d47d2d2016-05-04 13:22:56 -040021#include "jconfigint.h"
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000022
23
24/* Private subobject */
25
26typedef struct {
27 struct jpeg_color_deconverter pub; /* public fields */
28
29 /* Private state for YCC->RGB conversion */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040030 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 */
noel@chromium.org3395bcc2014-04-14 06:56:00 +000034
35 /* Private state for RGB->Y conversion */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040036 JLONG *rgb_y_tab; /* => table for RGB to Y conversion */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000037} my_color_deconverter;
38
Tom Hudson0d47d2d2016-05-04 13:22:56 -040039typedef my_color_deconverter *my_cconvert_ptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000040
41
42/**************** YCbCr -> RGB conversion: most common case **************/
noel@chromium.org3395bcc2014-04-14 06:56:00 +000043/**************** RGB -> Y conversion: less common case **************/
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000044
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
noel@chromium.org3395bcc2014-04-14 06:56:00 +000049 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -040050 * R = Y + 1.40200 * Cr
51 * G = Y - 0.34414 * Cb - 0.71414 * Cr
52 * B = Y + 1.77200 * Cb
noel@chromium.org3395bcc2014-04-14 06:56:00 +000053 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -040054 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
noel@chromium.org3395bcc2014-04-14 06:56:00 +000055 *
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000056 * 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -040076#define SCALEBITS 16 /* speediest right-shift on some machines */
Chris Blumecca8c4d2019-03-01 01:09:50 -080077#define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))
78#define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000079
noel@chromium.org3395bcc2014-04-14 06:56:00 +000080/* 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -040087#define R_Y_OFF 0 /* offset to R => Y section */
Chris Blumecca8c4d2019-03-01 01:09:50 -080088#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))
noel@chromium.org3395bcc2014-04-14 06:56:00 +000091
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000092
hbono@chromium.orgc6beb742011-11-29 05:16:26 +000093/* 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
Chris Blumecca8c4d2019-03-01 01:09:50 -0800101#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
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000108#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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000115#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000116
Chris Blumecca8c4d2019-03-01 01:09:50 -0800117#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
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000125#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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000133#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000134
Chris Blumecca8c4d2019-03-01 01:09:50 -0800135#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
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000142#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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000149#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000150
Chris Blumecca8c4d2019-03-01 01:09:50 -0800151#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
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000159#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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000167#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000168
Chris Blumecca8c4d2019-03-01 01:09:50 -0800169#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
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000177#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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000185#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000186
Chris Blumecca8c4d2019-03-01 01:09:50 -0800187#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
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000195#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
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000203#undef rgb_rgb_convert_internal
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000204
205
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000206/*
207 * Initialize tables for YCC->RGB colorspace conversion.
208 */
209
210LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800211build_ycc_rgb_table(j_decompress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000212{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800213 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000214 int i;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400215 JLONG x;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000216 SHIFT_TEMPS
217
218 cconvert->Cr_r_tab = (int *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800219 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
220 (MAXJSAMPLE + 1) * sizeof(int));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000221 cconvert->Cb_b_tab = (int *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800222 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
223 (MAXJSAMPLE + 1) * sizeof(int));
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400224 cconvert->Cr_g_tab = (JLONG *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800225 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
226 (MAXJSAMPLE + 1) * sizeof(JLONG));
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400227 cconvert->Cb_g_tab = (JLONG *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800228 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
229 (MAXJSAMPLE + 1) * sizeof(JLONG));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000230
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)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400236 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000237 /* Cb=>B value is nearest int to 1.77200 * x */
238 cconvert->Cb_b_tab[i] = (int)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400239 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000240 /* Cr=>G value is scaled-up -0.71414 * x */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800241 cconvert->Cr_g_tab[i] = (-FIX(0.71414)) * x;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000242 /* 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 */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800244 cconvert->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000245 }
246}
247
248
249/*
250 * Convert some rows of samples to the output colorspace.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000251 */
252
253METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800254ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
255 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000256{
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000257 switch (cinfo->out_color_space) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800258 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;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000290 }
291}
292
293
294/**************** Cases other than YCbCr -> RGB **************/
295
296
297/*
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000298 * Initialize for RGB->grayscale colorspace conversion.
299 */
300
301LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800302build_rgb_y_table(j_decompress_ptr cinfo)
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000303{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800304 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400305 JLONG *rgb_y_tab;
306 JLONG i;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000307
308 /* Allocate and fill in the conversion tables. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400309 cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800310 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400311 (TABLE_SIZE * sizeof(JLONG)));
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000312
313 for (i = 0; i <= MAXJSAMPLE; i++) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800314 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;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000317 }
318}
319
320
321/*
322 * Convert RGB to grayscale.
323 */
324
325METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800326rgb_gray_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
327 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000328{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800329 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000330 register int r, g, b;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400331 register JLONG *ctab = cconvert->rgb_y_tab;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000332 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++) {
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000344 r = inptr0[col];
345 g = inptr1[col];
346 b = inptr2[col];
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000347 /* Y */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800348 outptr[col] = (JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
349 ctab[b + B_Y_OFF]) >> SCALEBITS);
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000350 }
351 }
352}
353
354
355/*
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000356 * Color conversion for no colorspace change: just copy the data,
357 * converting from separate-planes to interleaved representation.
358 */
359
360METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800361null_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
362 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000363{
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400364 register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
365 register JDIMENSION col;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000366 register int num_components = cinfo->num_components;
367 JDIMENSION num_cols = cinfo->output_width;
368 int ci;
369
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400370 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];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000381 }
382 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400383 } 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 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000411 }
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
421METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800422grayscale_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
423 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000424{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800425 jcopy_sample_rows(input_buf[0], (int)input_row, output_buf, 0, num_rows,
426 cinfo->output_width);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000427}
428
429
430/*
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000431 * Convert grayscale to RGB
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000432 */
433
434METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800435gray_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
436 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000437{
hbono@chromium.orgc6beb742011-11-29 05:16:26 +0000438 switch (cinfo->out_color_space) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800439 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;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000471 }
472}
473
474
475/*
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000476 * Convert plain RGB to extended RGB
477 */
478
479METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800480rgb_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
481 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000482{
483 switch (cinfo->out_color_space) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800484 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;
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000516 }
517}
518
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000519
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000520/*
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000521 * 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
527METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800528ycck_cmyk_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
529 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000530{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800531 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000532 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 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400538 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;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000543 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++) {
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000553 y = inptr0[col];
554 cb = inptr1[col];
555 cr = inptr2[col];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000556 /* Range-limiting is essential due to noise introduced by DCT losses. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400557 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
558 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800559 ((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400560 SCALEBITS)))];
561 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000562 /* K passes through unchanged */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000563 outptr[3] = inptr3[col];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000564 outptr += 4;
565 }
566 }
567}
568
569
570/*
Aaron Gablefeec46f2015-08-06 09:54:48 -0700571 * RGB565 conversion
572 */
573
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000574#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))
Aaron Gablefeec46f2015-08-06 09:54:48 -0700578
Chris Blumecca8c4d2019-03-01 01:09:50 -0800579#define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
580#define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700581
Chris Blumecca8c4d2019-03-01 01:09:50 -0800582#define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700583
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
Chris Blumecca8c4d2019-03-01 01:09:50 -0800594 * sufficient for dithering RGB888 to RGB565.
Aaron Gablefeec46f2015-08-06 09:54:48 -0700595 */
596
597#define DITHER_MASK 0x3
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400598#define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
599static const JLONG dither_matrix[4] = {
Aaron Gablefeec46f2015-08-06 09:54:48 -0700600 0x0008020A,
601 0x0C040E06,
602 0x030B0109,
603 0x0F070D05
604};
605
606
607static INLINE boolean is_big_endian(void)
608{
609 int test_value = 1;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800610 if (*(char *)&test_value != 1)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700611 return TRUE;
612 return FALSE;
613}
614
615
616/* Include inline routines for RGB565 conversion */
617
Chris Blumecca8c4d2019-03-01 01:09:50 -0800618#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
Aaron Gablefeec46f2015-08-06 09:54:48 -0700626#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
Chris Blumecca8c4d2019-03-01 01:09:50 -0800636#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
Aaron Gablefeec46f2015-08-06 09:54:48 -0700644#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
655METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800656ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
657 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700658{
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
666METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800667ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
668 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700669{
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
677METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800678rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
679 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700680{
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
688METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800689rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
690 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700691{
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
699METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800700gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
701 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700702{
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
710METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800711gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
712 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
Aaron Gablefeec46f2015-08-06 09:54:48 -0700713{
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/*
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000722 * Empty method for start_pass.
723 */
724
725METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800726start_pass_dcolor(j_decompress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000727{
728 /* no work needed */
729}
730
731
732/*
733 * Module initialization routine for output colorspace conversion.
734 */
735
736GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800737jinit_color_deconverter(j_decompress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000738{
739 my_cconvert_ptr cconvert;
740 int ci;
741
742 cconvert = (my_cconvert_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800743 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400744 sizeof(my_color_deconverter));
Chris Blumecca8c4d2019-03-01 01:09:50 -0800745 cinfo->cconvert = (struct jpeg_color_deconverter *)cconvert;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000746 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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400767 default: /* JCS_UNKNOWN can be anything */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000768 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 ||
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400782 cinfo->jpeg_color_space == JCS_YCbCr) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000783 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++)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400786 cinfo->comp_info[ci].component_needed = FALSE;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000787 } else if (cinfo->jpeg_color_space == JCS_RGB) {
788 cconvert->pub.color_convert = rgb_gray_convert;
789 build_rgb_y_table(cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000790 } 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:
hbono@chromium.org0ec930e2012-01-18 07:01:04 +0000801 case JCS_EXT_RGBA:
802 case JCS_EXT_BGRA:
803 case JCS_EXT_ABGR:
804 case JCS_EXT_ARGB:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000805 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;
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000815 } 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;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000823 } else
824 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
825 break;
826
Aaron Gablefeec46f2015-08-06 09:54:48 -0700827 case JCS_RGB565:
828 cinfo->out_color_components = 3;
829 if (cinfo->dither_mode == JDITHER_NONE) {
830 if (cinfo->jpeg_color_space == JCS_YCbCr) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800831 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);
Aaron Gablefeec46f2015-08-06 09:54:48 -0700836 }
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
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400857 case JCS_CMYK:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000858 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;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400873 } else /* unsupported non-null conversion */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000874 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}