blob: 05cbf4df1fa8a296169289feaf1db820ea1ed067 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdcolor.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Guido Vollbeding5829cb22012-01-15 00:00:00 +00006 * Modified 2011 by Guido Vollbeding.
DRCa6ef2822013-09-28 03:23:49 +00007 * libjpeg-turbo Modifications:
DRCcf763c02013-01-01 09:51:37 +00008 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC0e940252015-01-20 10:33:32 +00009 * Copyright (C) 2009, 2011-2012, 2014-2015, D. R. Commander.
DRC78df2e62014-05-12 09:23:57 +000010 * Copyright (C) 2013, Linaro Limited.
Alex Naidis6eb7d372016-10-16 23:10:08 +020011 * For conditions of distribution and use, see the accompanying README.ijg
12 * file.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000013 *
14 * This file contains output colorspace conversion routines.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000015 */
16
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000018#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000020#include "jsimd.h"
DRCff6961f2014-04-20 09:17:11 +000021#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022
23
24/* Private subobject */
25
26typedef struct {
27 struct jpeg_color_deconverter pub; /* public fields */
28
29 /* Private state for YCC->RGB conversion */
Alex Naidis6eb7d372016-10-16 23:10:08 +020030 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 */
Guido Vollbeding5829cb22012-01-15 00:00:00 +000034
35 /* Private state for RGB->Y conversion */
Alex Naidis6eb7d372016-10-16 23:10:08 +020036 JLONG *rgb_y_tab; /* => table for RGB to Y conversion */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037} my_color_deconverter;
38
Alex Naidis6eb7d372016-10-16 23:10:08 +020039typedef my_color_deconverter *my_cconvert_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000040
41
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000042/**************** YCbCr -> RGB conversion: most common case **************/
Guido Vollbeding5829cb22012-01-15 00:00:00 +000043/**************** RGB -> Y conversion: less common case **************/
Thomas G. Lane4a6b7301992-03-17 00:00:00 +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
Guido Vollbeding5829cb22012-01-15 00:00:00 +000049 *
DRCe5eaf372014-05-09 18:00:32 +000050 * R = Y + 1.40200 * Cr
51 * G = Y - 0.34414 * Cb - 0.71414 * Cr
52 * B = Y + 1.77200 * Cb
Guido Vollbeding5829cb22012-01-15 00:00:00 +000053 *
DRCe5eaf372014-05-09 18:00:32 +000054 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
Guido Vollbeding5829cb22012-01-15 00:00:00 +000055 *
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000056 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000057 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000058 *
59 * To avoid floating-point arithmetic, we represent the fractional constants
Thomas G. Lane88aeed41992-12-10 00:00:00 +000060 * 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.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000062 * 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.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000067 * 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
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000070 * 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
DRCe5eaf372014-05-09 18:00:32 +000076#define SCALEBITS 16 /* speediest right-shift on some machines */
Alex Naidis6eb7d372016-10-16 23:10:08 +020077#define ONE_HALF ((JLONG) 1 << (SCALEBITS-1))
78#define FIX(x) ((JLONG) ((x) * (1L<<SCALEBITS) + 0.5))
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000079
Guido Vollbeding5829cb22012-01-15 00:00: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
DRCe5eaf372014-05-09 18:00:32 +000087#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))
Guido Vollbeding5829cb22012-01-15 00:00:00 +000091
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000092
DRCb4570bb2011-09-07 06:31:00 +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
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
DRCa9b646c2012-03-11 22:06:54 +0000107#define rgb_rgb_convert_internal rgb_extrgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +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
DRCa9b646c2012-03-11 22:06:54 +0000115#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000116
117#define RGB_RED EXT_RGBX_RED
118#define RGB_GREEN EXT_RGBX_GREEN
119#define RGB_BLUE EXT_RGBX_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000120#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +0000121#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
DRCa9b646c2012-03-11 22:06:54 +0000124#define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000125#include "jdcolext.c"
126#undef RGB_RED
127#undef RGB_GREEN
128#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000129#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000130#undef RGB_PIXELSIZE
131#undef ycc_rgb_convert_internal
132#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000133#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000134
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
DRCa9b646c2012-03-11 22:06:54 +0000141#define rgb_rgb_convert_internal rgb_extbgr_convert_internal
DRCb4570bb2011-09-07 06:31:00 +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
DRCa9b646c2012-03-11 22:06:54 +0000149#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000150
151#define RGB_RED EXT_BGRX_RED
152#define RGB_GREEN EXT_BGRX_GREEN
153#define RGB_BLUE EXT_BGRX_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000154#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +0000155#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
DRCa9b646c2012-03-11 22:06:54 +0000158#define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000159#include "jdcolext.c"
160#undef RGB_RED
161#undef RGB_GREEN
162#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000163#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000164#undef RGB_PIXELSIZE
165#undef ycc_rgb_convert_internal
166#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000167#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000168
169#define RGB_RED EXT_XBGR_RED
170#define RGB_GREEN EXT_XBGR_GREEN
171#define RGB_BLUE EXT_XBGR_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000172#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000173#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
DRCa9b646c2012-03-11 22:06:54 +0000176#define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000177#include "jdcolext.c"
178#undef RGB_RED
179#undef RGB_GREEN
180#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000181#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000182#undef RGB_PIXELSIZE
183#undef ycc_rgb_convert_internal
184#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000185#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000186
187#define RGB_RED EXT_XRGB_RED
188#define RGB_GREEN EXT_XRGB_GREEN
189#define RGB_BLUE EXT_XRGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000190#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000191#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
DRCa9b646c2012-03-11 22:06:54 +0000194#define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000195#include "jdcolext.c"
196#undef RGB_RED
197#undef RGB_GREEN
198#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000199#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000200#undef RGB_PIXELSIZE
201#undef ycc_rgb_convert_internal
202#undef gray_rgb_convert_internal
DRCa9b646c2012-03-11 22:06:54 +0000203#undef rgb_rgb_convert_internal
DRCb4570bb2011-09-07 06:31:00 +0000204
205
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000206/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000207 * Initialize tables for YCC->RGB colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000208 */
209
Thomas G. Lane489583f1996-02-07 00:00:00 +0000210LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000211build_ycc_rgb_table (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000212{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000214 int i;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200215 JLONG x;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000216 SHIFT_TEMPS
217
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 cconvert->Cr_r_tab = (int *)
219 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000220 (MAXJSAMPLE+1) * sizeof(int));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000221 cconvert->Cb_b_tab = (int *)
222 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000223 (MAXJSAMPLE+1) * sizeof(int));
Alex Naidis6eb7d372016-10-16 23:10:08 +0200224 cconvert->Cr_g_tab = (JLONG *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Alex Naidis6eb7d372016-10-16 23:10:08 +0200226 (MAXJSAMPLE+1) * sizeof(JLONG));
227 cconvert->Cb_g_tab = (JLONG *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Alex Naidis6eb7d372016-10-16 23:10:08 +0200229 (MAXJSAMPLE+1) * sizeof(JLONG));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000230
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000231 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000232 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000233 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000234 /* Cr=>R value is nearest int to 1.40200 * x */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235 cconvert->Cr_r_tab[i] = (int)
DRCe5eaf372014-05-09 18:00:32 +0000236 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000237 /* Cb=>B value is nearest int to 1.77200 * x */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000238 cconvert->Cb_b_tab[i] = (int)
DRCe5eaf372014-05-09 18:00:32 +0000239 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000240 /* Cr=>G value is scaled-up -0.71414 * x */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000241 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +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 */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000244 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000245 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000246}
247
248
249/*
250 * Convert some rows of samples to the output colorspace.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000251 */
252
Thomas G. Lane489583f1996-02-07 00:00:00 +0000253METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254ycc_rgb_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000255 JSAMPIMAGE input_buf, JDIMENSION input_row,
256 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000257{
DRCb4570bb2011-09-07 06:31:00 +0000258 switch (cinfo->out_color_space) {
259 case JCS_EXT_RGB:
260 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
261 num_rows);
262 break;
263 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000264 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000265 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
266 num_rows);
267 break;
268 case JCS_EXT_BGR:
269 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
270 num_rows);
271 break;
272 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000273 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000274 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
275 num_rows);
276 break;
277 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000278 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000279 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
280 num_rows);
281 break;
282 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000283 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000284 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
285 num_rows);
286 break;
287 default:
288 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
289 num_rows);
290 break;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000291 }
292}
293
294
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000295/**************** Cases other than YCbCr -> RGB **************/
296
297
298/*
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000299 * Initialize for RGB->grayscale colorspace conversion.
300 */
301
302LOCAL(void)
303build_rgb_y_table (j_decompress_ptr cinfo)
304{
305 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200306 JLONG *rgb_y_tab;
307 JLONG i;
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000308
309 /* Allocate and fill in the conversion tables. */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200310 cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000311 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Alex Naidis6eb7d372016-10-16 23:10:08 +0200312 (TABLE_SIZE * sizeof(JLONG)));
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000313
314 for (i = 0; i <= MAXJSAMPLE; i++) {
315 rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
316 rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
317 rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
318 }
319}
320
321
322/*
323 * Convert RGB to grayscale.
324 */
325
326METHODDEF(void)
327rgb_gray_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000328 JSAMPIMAGE input_buf, JDIMENSION input_row,
329 JSAMPARRAY output_buf, int num_rows)
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000330{
331 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
332 register int r, g, b;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200333 register JLONG *ctab = cconvert->rgb_y_tab;
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000334 register JSAMPROW outptr;
335 register JSAMPROW inptr0, inptr1, inptr2;
336 register JDIMENSION col;
337 JDIMENSION num_cols = cinfo->output_width;
338
339 while (--num_rows >= 0) {
340 inptr0 = input_buf[0][input_row];
341 inptr1 = input_buf[1][input_row];
342 inptr2 = input_buf[2][input_row];
343 input_row++;
344 outptr = *output_buf++;
345 for (col = 0; col < num_cols; col++) {
346 r = GETJSAMPLE(inptr0[col]);
347 g = GETJSAMPLE(inptr1[col]);
348 b = GETJSAMPLE(inptr2[col]);
349 /* Y */
350 outptr[col] = (JSAMPLE)
DRCe5eaf372014-05-09 18:00:32 +0000351 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
352 >> SCALEBITS);
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000353 }
354 }
355}
356
357
358/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000359 * Color conversion for no colorspace change: just copy the data,
360 * converting from separate-planes to interleaved representation.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000361 */
362
Thomas G. Lane489583f1996-02-07 00:00:00 +0000363METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364null_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000365 JSAMPIMAGE input_buf, JDIMENSION input_row,
366 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000367{
DRC0e940252015-01-20 10:33:32 +0000368 register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
369 register JDIMENSION col;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000370 register int num_components = cinfo->num_components;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000371 JDIMENSION num_cols = cinfo->output_width;
372 int ci;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000373
DRC0e940252015-01-20 10:33:32 +0000374 if (num_components == 3) {
375 while (--num_rows >= 0) {
376 inptr0 = input_buf[0][input_row];
377 inptr1 = input_buf[1][input_row];
378 inptr2 = input_buf[2][input_row];
379 input_row++;
380 outptr = *output_buf++;
381 for (col = 0; col < num_cols; col++) {
382 *outptr++ = inptr0[col];
383 *outptr++ = inptr1[col];
384 *outptr++ = inptr2[col];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000385 }
386 }
DRC0e940252015-01-20 10:33:32 +0000387 } else if (num_components == 4) {
388 while (--num_rows >= 0) {
389 inptr0 = input_buf[0][input_row];
390 inptr1 = input_buf[1][input_row];
391 inptr2 = input_buf[2][input_row];
392 inptr3 = input_buf[3][input_row];
393 input_row++;
394 outptr = *output_buf++;
395 for (col = 0; col < num_cols; col++) {
396 *outptr++ = inptr0[col];
397 *outptr++ = inptr1[col];
398 *outptr++ = inptr2[col];
399 *outptr++ = inptr3[col];
400 }
401 }
402 } else {
403 while (--num_rows >= 0) {
404 for (ci = 0; ci < num_components; ci++) {
405 inptr = input_buf[ci][input_row];
406 outptr = *output_buf;
407 for (col = 0; col < num_cols; col++) {
408 outptr[ci] = inptr[col];
409 outptr += num_components;
410 }
411 }
412 output_buf++;
413 input_row++;
414 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000415 }
416}
417
418
419/*
420 * Color conversion for grayscale: just copy the data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000421 * This also works for YCbCr -> grayscale conversion, in which
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000422 * we just copy the Y (luminance) component and ignore chrominance.
423 */
424
Thomas G. Lane489583f1996-02-07 00:00:00 +0000425METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000426grayscale_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000427 JSAMPIMAGE input_buf, JDIMENSION input_row,
428 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000429{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000430 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
DRCe5eaf372014-05-09 18:00:32 +0000431 num_rows, cinfo->output_width);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000432}
433
434
435/*
DRCb4570bb2011-09-07 06:31:00 +0000436 * Convert grayscale to RGB
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000437 */
438
439METHODDEF(void)
440gray_rgb_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000441 JSAMPIMAGE input_buf, JDIMENSION input_row,
442 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000443{
DRCb4570bb2011-09-07 06:31:00 +0000444 switch (cinfo->out_color_space) {
445 case JCS_EXT_RGB:
446 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
447 num_rows);
448 break;
449 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000450 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000451 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
452 num_rows);
453 break;
454 case JCS_EXT_BGR:
455 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
456 num_rows);
457 break;
458 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000459 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000460 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
461 num_rows);
462 break;
463 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000464 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000465 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
466 num_rows);
467 break;
468 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000469 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000470 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
471 num_rows);
472 break;
473 default:
474 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
475 num_rows);
476 break;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000477 }
478}
479
480
481/*
DRCa9b646c2012-03-11 22:06:54 +0000482 * Convert plain RGB to extended RGB
483 */
484
485METHODDEF(void)
486rgb_rgb_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000487 JSAMPIMAGE input_buf, JDIMENSION input_row,
488 JSAMPARRAY output_buf, int num_rows)
DRCa9b646c2012-03-11 22:06:54 +0000489{
490 switch (cinfo->out_color_space) {
491 case JCS_EXT_RGB:
492 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
493 num_rows);
494 break;
495 case JCS_EXT_RGBX:
496 case JCS_EXT_RGBA:
497 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
498 num_rows);
499 break;
500 case JCS_EXT_BGR:
501 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
502 num_rows);
503 break;
504 case JCS_EXT_BGRX:
505 case JCS_EXT_BGRA:
506 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
507 num_rows);
508 break;
509 case JCS_EXT_XBGR:
510 case JCS_EXT_ABGR:
511 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
512 num_rows);
513 break;
514 case JCS_EXT_XRGB:
515 case JCS_EXT_ARGB:
516 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
517 num_rows);
518 break;
519 default:
520 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
521 num_rows);
522 break;
523 }
524}
525
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000526
DRCa9b646c2012-03-11 22:06:54 +0000527/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000528 * Adobe-style YCCK->CMYK conversion.
529 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
530 * conversion as above, while passing K (black) unchanged.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000531 * We assume build_ycc_rgb_table has been called.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000532 */
533
Thomas G. Lane489583f1996-02-07 00:00:00 +0000534METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000535ycck_cmyk_convert (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000536 JSAMPIMAGE input_buf, JDIMENSION input_row,
537 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000538{
539 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
540 register int y, cb, cr;
541 register JSAMPROW outptr;
542 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
543 register JDIMENSION col;
544 JDIMENSION num_cols = cinfo->output_width;
545 /* copy these pointers into registers if possible */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200546 register JSAMPLE *range_limit = cinfo->sample_range_limit;
547 register int *Crrtab = cconvert->Cr_r_tab;
548 register int *Cbbtab = cconvert->Cb_b_tab;
549 register JLONG *Crgtab = cconvert->Cr_g_tab;
550 register JLONG *Cbgtab = cconvert->Cb_g_tab;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000551 SHIFT_TEMPS
552
553 while (--num_rows >= 0) {
554 inptr0 = input_buf[0][input_row];
555 inptr1 = input_buf[1][input_row];
556 inptr2 = input_buf[2][input_row];
557 inptr3 = input_buf[3][input_row];
558 input_row++;
559 outptr = *output_buf++;
560 for (col = 0; col < num_cols; col++) {
561 y = GETJSAMPLE(inptr0[col]);
562 cb = GETJSAMPLE(inptr1[col]);
563 cr = GETJSAMPLE(inptr2[col]);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000564 /* Range-limiting is essential due to noise introduced by DCT losses. */
DRCe5eaf372014-05-09 18:00:32 +0000565 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
566 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
567 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
568 SCALEBITS)))];
569 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000570 /* K passes through unchanged */
DRCe5eaf372014-05-09 18:00:32 +0000571 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000572 outptr += 4;
573 }
574 }
575}
576
577
DRC72a3cc02014-08-30 20:37:50 +0000578/*
579 * RGB565 conversion
580 */
581
582#define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
583 (((g) << 3) & 0x7E0) | ((b) >> 3))
584#define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
585 (((g) << 11) & 0xE000) | \
586 (((b) << 5) & 0x1F00))
587
588#define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
589#define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
590
591#define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
592
DRCbbd9b712014-09-04 17:35:22 +0000593#define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
DRC72a3cc02014-08-30 20:37:50 +0000594
595#define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
596#define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
597#define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
598
599
600/* Declarations for ordered dithering
601 *
602 * We use a 4x4 ordered dither array packed into 32 bits. This array is
603 * sufficent for dithering RGB888 to RGB565.
604 */
605
606#define DITHER_MASK 0x3
Alex Naidis6eb7d372016-10-16 23:10:08 +0200607#define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
608static const JLONG dither_matrix[4] = {
DRC72a3cc02014-08-30 20:37:50 +0000609 0x0008020A,
610 0x0C040E06,
611 0x030B0109,
612 0x0F070D05
613};
614
615
616static INLINE boolean is_big_endian(void)
617{
618 int test_value = 1;
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500619 if (*(char *)&test_value != 1)
DRC72a3cc02014-08-30 20:37:50 +0000620 return TRUE;
621 return FALSE;
622}
623
624
625/* Include inline routines for RGB565 conversion */
626
627#define PACK_SHORT_565 PACK_SHORT_565_LE
628#define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
629#define ycc_rgb565_convert_internal ycc_rgb565_convert_le
630#define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
631#define rgb_rgb565_convert_internal rgb_rgb565_convert_le
632#define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
633#define gray_rgb565_convert_internal gray_rgb565_convert_le
634#define gray_rgb565D_convert_internal gray_rgb565D_convert_le
DRC78df2e62014-05-12 09:23:57 +0000635#include "jdcol565.c"
DRC72a3cc02014-08-30 20:37:50 +0000636#undef PACK_SHORT_565
637#undef PACK_TWO_PIXELS
638#undef ycc_rgb565_convert_internal
639#undef ycc_rgb565D_convert_internal
640#undef rgb_rgb565_convert_internal
641#undef rgb_rgb565D_convert_internal
642#undef gray_rgb565_convert_internal
643#undef gray_rgb565D_convert_internal
644
645#define PACK_SHORT_565 PACK_SHORT_565_BE
646#define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
647#define ycc_rgb565_convert_internal ycc_rgb565_convert_be
648#define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
649#define rgb_rgb565_convert_internal rgb_rgb565_convert_be
650#define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
651#define gray_rgb565_convert_internal gray_rgb565_convert_be
652#define gray_rgb565D_convert_internal gray_rgb565D_convert_be
653#include "jdcol565.c"
654#undef PACK_SHORT_565
655#undef PACK_TWO_PIXELS
656#undef ycc_rgb565_convert_internal
657#undef ycc_rgb565D_convert_internal
658#undef rgb_rgb565_convert_internal
659#undef rgb_rgb565D_convert_internal
660#undef gray_rgb565_convert_internal
661#undef gray_rgb565D_convert_internal
662
663
664METHODDEF(void)
665ycc_rgb565_convert (j_decompress_ptr cinfo,
666 JSAMPIMAGE input_buf, JDIMENSION input_row,
667 JSAMPARRAY output_buf, int num_rows)
668{
669 if (is_big_endian())
670 ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
671 else
672 ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
673}
674
675
676METHODDEF(void)
677ycc_rgb565D_convert (j_decompress_ptr cinfo,
678 JSAMPIMAGE input_buf, JDIMENSION input_row,
679 JSAMPARRAY output_buf, int num_rows)
680{
681 if (is_big_endian())
682 ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
683 else
684 ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
685}
686
687
688METHODDEF(void)
689rgb_rgb565_convert (j_decompress_ptr cinfo,
690 JSAMPIMAGE input_buf, JDIMENSION input_row,
691 JSAMPARRAY output_buf, int num_rows)
692{
693 if (is_big_endian())
694 rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
695 else
696 rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
697}
698
699
700METHODDEF(void)
701rgb_rgb565D_convert (j_decompress_ptr cinfo,
702 JSAMPIMAGE input_buf, JDIMENSION input_row,
703 JSAMPARRAY output_buf, int num_rows)
704{
705 if (is_big_endian())
706 rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
707 else
708 rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
709}
710
711
712METHODDEF(void)
713gray_rgb565_convert (j_decompress_ptr cinfo,
714 JSAMPIMAGE input_buf, JDIMENSION input_row,
715 JSAMPARRAY output_buf, int num_rows)
716{
717 if (is_big_endian())
718 gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
719 else
720 gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
721}
722
723
724METHODDEF(void)
725gray_rgb565D_convert (j_decompress_ptr cinfo,
726 JSAMPIMAGE input_buf, JDIMENSION input_row,
727 JSAMPARRAY output_buf, int num_rows)
728{
729 if (is_big_endian())
730 gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
731 else
732 gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
733}
DRC78df2e62014-05-12 09:23:57 +0000734
735
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000736/*
737 * Empty method for start_pass.
738 */
739
Thomas G. Lane489583f1996-02-07 00:00:00 +0000740METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000741start_pass_dcolor (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000742{
743 /* no work needed */
744}
745
746
747/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000748 * Module initialization routine for output colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000749 */
750
Thomas G. Lane489583f1996-02-07 00:00:00 +0000751GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000752jinit_color_deconverter (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000753{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000754 my_cconvert_ptr cconvert;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000755 int ci;
756
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000757 cconvert = (my_cconvert_ptr)
758 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000759 sizeof(my_color_deconverter));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000760 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000761 cconvert->pub.start_pass = start_pass_dcolor;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000762
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000763 /* Make sure num_components agrees with jpeg_color_space */
764 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000765 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000766 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000767 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000768 break;
769
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000770 case JCS_RGB:
771 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000772 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000773 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000774 break;
775
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000776 case JCS_CMYK:
777 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000778 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000779 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000780 break;
781
DRCe5eaf372014-05-09 18:00:32 +0000782 default: /* JCS_UNKNOWN can be anything */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000783 if (cinfo->num_components < 1)
784 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000785 break;
786 }
787
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000788 /* Set out_color_components and conversion method based on requested space.
789 * Also clear the component_needed flags for any unused components,
790 * so that earlier pipeline stages can avoid useless computation.
791 */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000792
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000793 switch (cinfo->out_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000794 case JCS_GRAYSCALE:
795 cinfo->out_color_components = 1;
796 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
DRCe5eaf372014-05-09 18:00:32 +0000797 cinfo->jpeg_color_space == JCS_YCbCr) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000798 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000799 /* For color->grayscale conversion, only the Y (0) component is needed */
800 for (ci = 1; ci < cinfo->num_components; ci++)
DRCe5eaf372014-05-09 18:00:32 +0000801 cinfo->comp_info[ci].component_needed = FALSE;
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000802 } else if (cinfo->jpeg_color_space == JCS_RGB) {
803 cconvert->pub.color_convert = rgb_gray_convert;
804 build_rgb_y_table(cinfo);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000805 } else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000806 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000807 break;
808
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000809 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000810 case JCS_EXT_RGB:
811 case JCS_EXT_RGBX:
812 case JCS_EXT_BGR:
813 case JCS_EXT_BGRX:
814 case JCS_EXT_XBGR:
815 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000816 case JCS_EXT_RGBA:
817 case JCS_EXT_BGRA:
818 case JCS_EXT_ABGR:
819 case JCS_EXT_ARGB:
DRCf25c0712009-04-03 12:00:51 +0000820 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000821 if (cinfo->jpeg_color_space == JCS_YCbCr) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000822 if (jsimd_can_ycc_rgb())
823 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
824 else {
825 cconvert->pub.color_convert = ycc_rgb_convert;
826 build_ycc_rgb_table(cinfo);
827 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000828 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
829 cconvert->pub.color_convert = gray_rgb_convert;
DRCa9b646c2012-03-11 22:06:54 +0000830 } else if (cinfo->jpeg_color_space == JCS_RGB) {
831 if (rgb_red[cinfo->out_color_space] == 0 &&
832 rgb_green[cinfo->out_color_space] == 1 &&
833 rgb_blue[cinfo->out_color_space] == 2 &&
834 rgb_pixelsize[cinfo->out_color_space] == 3)
835 cconvert->pub.color_convert = null_convert;
836 else
837 cconvert->pub.color_convert = rgb_rgb_convert;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000838 } else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000839 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
840 break;
841
DRC78df2e62014-05-12 09:23:57 +0000842 case JCS_RGB565:
843 cinfo->out_color_components = 3;
844 if (cinfo->dither_mode == JDITHER_NONE) {
845 if (cinfo->jpeg_color_space == JCS_YCbCr) {
DRCd729f4d2014-08-23 15:47:51 +0000846 if (jsimd_can_ycc_rgb565())
847 cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
848 else {
849 cconvert->pub.color_convert = ycc_rgb565_convert;
850 build_ycc_rgb_table(cinfo);
851 }
DRC78df2e62014-05-12 09:23:57 +0000852 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
853 cconvert->pub.color_convert = gray_rgb565_convert;
854 } else if (cinfo->jpeg_color_space == JCS_RGB) {
855 cconvert->pub.color_convert = rgb_rgb565_convert;
856 } else
857 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
858 } else {
859 /* only ordered dithering is supported */
860 if (cinfo->jpeg_color_space == JCS_YCbCr) {
861 cconvert->pub.color_convert = ycc_rgb565D_convert;
862 build_ycc_rgb_table(cinfo);
863 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
864 cconvert->pub.color_convert = gray_rgb565D_convert;
865 } else if (cinfo->jpeg_color_space == JCS_RGB) {
866 cconvert->pub.color_convert = rgb_rgb565D_convert;
867 } else
868 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
869 }
870 break;
871
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000872 case JCS_CMYK:
873 cinfo->out_color_components = 4;
874 if (cinfo->jpeg_color_space == JCS_YCCK) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000875 cconvert->pub.color_convert = ycck_cmyk_convert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000876 build_ycc_rgb_table(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000877 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
878 cconvert->pub.color_convert = null_convert;
879 } else
880 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000881 break;
882
883 default:
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000884 /* Permit null conversion to same output space */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000885 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000886 cinfo->out_color_components = cinfo->num_components;
887 cconvert->pub.color_convert = null_convert;
DRCe5eaf372014-05-09 18:00:32 +0000888 } else /* unsupported non-null conversion */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000889 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000890 break;
891 }
892
893 if (cinfo->quantize_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000894 cinfo->output_components = 1; /* single colormapped output component */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000895 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000896 cinfo->output_components = cinfo->out_color_components;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000897}