blob: 05d389a587ff205e1618d97075f7da58b21c31e8 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdcolor.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00005 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCb4570bb2011-09-07 06:31:00 +00006 * Copyright (C) 2009, 2011, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains output colorspace conversion routines.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000011 */
12
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000013#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000014#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000016#include "jsimd.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017
18
19/* Private subobject */
20
21typedef struct {
22 struct jpeg_color_deconverter pub; /* public fields */
23
24 /* Private state for YCC->RGB conversion */
25 int * Cr_r_tab; /* => table for Cr to R conversion */
26 int * Cb_b_tab; /* => table for Cb to B conversion */
27 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
28 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
29} my_color_deconverter;
30
31typedef my_color_deconverter * my_cconvert_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000032
33
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000034/**************** YCbCr -> RGB conversion: most common case **************/
35
36/*
37 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
38 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
39 * The conversion equations to be implemented are therefore
40 * R = Y + 1.40200 * Cr
41 * G = Y - 0.34414 * Cb - 0.71414 * Cr
42 * B = Y + 1.77200 * Cb
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000043 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000044 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000045 *
46 * To avoid floating-point arithmetic, we represent the fractional constants
Thomas G. Lane88aeed41992-12-10 00:00:00 +000047 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
48 * the products by 2^16, with appropriate rounding, to get the correct answer.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000049 * Notice that Y, being an integral input, does not contribute any fraction
50 * so it need not participate in the rounding.
51 *
52 * For even more speed, we avoid doing any multiplications in the inner loop
53 * by precalculating the constants times Cb and Cr for all possible values.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000054 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
55 * for 12-bit samples it is still acceptable. It's not very reasonable for
56 * 16-bit samples, but if you want lossless storage you shouldn't be changing
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000057 * colorspace anyway.
58 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
59 * values for the G calculation are left scaled up, since we must add them
60 * together before rounding.
61 */
62
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063#define SCALEBITS 16 /* speediest right-shift on some machines */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000064#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
65#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
66
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000067
DRCb4570bb2011-09-07 06:31:00 +000068/* Include inline routines for colorspace extensions */
69
70#include "jdcolext.c"
71#undef RGB_RED
72#undef RGB_GREEN
73#undef RGB_BLUE
74#undef RGB_PIXELSIZE
75
76#define RGB_RED EXT_RGB_RED
77#define RGB_GREEN EXT_RGB_GREEN
78#define RGB_BLUE EXT_RGB_BLUE
79#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
80#define ycc_rgb_convert_internal ycc_extrgb_convert_internal
81#define gray_rgb_convert_internal gray_extrgb_convert_internal
82#include "jdcolext.c"
83#undef RGB_RED
84#undef RGB_GREEN
85#undef RGB_BLUE
86#undef RGB_PIXELSIZE
87#undef ycc_rgb_convert_internal
88#undef gray_rgb_convert_internal
89
90#define RGB_RED EXT_RGBX_RED
91#define RGB_GREEN EXT_RGBX_GREEN
92#define RGB_BLUE EXT_RGBX_BLUE
DRCd89e01f2011-09-09 18:15:53 +000093#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +000094#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
95#define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
96#define gray_rgb_convert_internal gray_extrgbx_convert_internal
97#include "jdcolext.c"
98#undef RGB_RED
99#undef RGB_GREEN
100#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000101#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000102#undef RGB_PIXELSIZE
103#undef ycc_rgb_convert_internal
104#undef gray_rgb_convert_internal
105
106#define RGB_RED EXT_BGR_RED
107#define RGB_GREEN EXT_BGR_GREEN
108#define RGB_BLUE EXT_BGR_BLUE
109#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
110#define ycc_rgb_convert_internal ycc_extbgr_convert_internal
111#define gray_rgb_convert_internal gray_extbgr_convert_internal
112#include "jdcolext.c"
113#undef RGB_RED
114#undef RGB_GREEN
115#undef RGB_BLUE
116#undef RGB_PIXELSIZE
117#undef ycc_rgb_convert_internal
118#undef gray_rgb_convert_internal
119
120#define RGB_RED EXT_BGRX_RED
121#define RGB_GREEN EXT_BGRX_GREEN
122#define RGB_BLUE EXT_BGRX_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000123#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +0000124#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
125#define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
126#define gray_rgb_convert_internal gray_extbgrx_convert_internal
127#include "jdcolext.c"
128#undef RGB_RED
129#undef RGB_GREEN
130#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000131#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000132#undef RGB_PIXELSIZE
133#undef ycc_rgb_convert_internal
134#undef gray_rgb_convert_internal
135
136#define RGB_RED EXT_XBGR_RED
137#define RGB_GREEN EXT_XBGR_GREEN
138#define RGB_BLUE EXT_XBGR_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000139#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000140#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
141#define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
142#define gray_rgb_convert_internal gray_extxbgr_convert_internal
143#include "jdcolext.c"
144#undef RGB_RED
145#undef RGB_GREEN
146#undef RGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000147#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000148#undef RGB_PIXELSIZE
149#undef ycc_rgb_convert_internal
150#undef gray_rgb_convert_internal
151
152#define RGB_RED EXT_XRGB_RED
153#define RGB_GREEN EXT_XRGB_GREEN
154#define RGB_BLUE EXT_XRGB_BLUE
DRCd89e01f2011-09-09 18:15:53 +0000155#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000156#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
157#define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
158#define gray_rgb_convert_internal gray_extxrgb_convert_internal
159#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
167
168
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000169/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000170 * Initialize tables for YCC->RGB colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000171 */
172
Thomas G. Lane489583f1996-02-07 00:00:00 +0000173LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000174build_ycc_rgb_table (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000175{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000177 int i;
178 INT32 x;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000179 SHIFT_TEMPS
180
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 cconvert->Cr_r_tab = (int *)
182 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
183 (MAXJSAMPLE+1) * SIZEOF(int));
184 cconvert->Cb_b_tab = (int *)
185 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
186 (MAXJSAMPLE+1) * SIZEOF(int));
187 cconvert->Cr_g_tab = (INT32 *)
188 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
189 (MAXJSAMPLE+1) * SIZEOF(INT32));
190 cconvert->Cb_g_tab = (INT32 *)
191 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
192 (MAXJSAMPLE+1) * SIZEOF(INT32));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000193
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000194 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000195 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000196 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000197 /* Cr=>R value is nearest int to 1.40200 * x */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198 cconvert->Cr_r_tab[i] = (int)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000199 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000200 /* Cb=>B value is nearest int to 1.77200 * x */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000201 cconvert->Cb_b_tab[i] = (int)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000202 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000203 /* Cr=>G value is scaled-up -0.71414 * x */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000204 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000205 /* Cb=>G value is scaled-up -0.34414 * x */
206 /* We also add in ONE_HALF so that need not do it in inner loop */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000207 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000208 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000209}
210
211
212/*
213 * Convert some rows of samples to the output colorspace.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000214 */
215
Thomas G. Lane489583f1996-02-07 00:00:00 +0000216METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000217ycc_rgb_convert (j_decompress_ptr cinfo,
218 JSAMPIMAGE input_buf, JDIMENSION input_row,
219 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000220{
DRCb4570bb2011-09-07 06:31:00 +0000221 switch (cinfo->out_color_space) {
222 case JCS_EXT_RGB:
223 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
224 num_rows);
225 break;
226 case JCS_EXT_RGBX:
227 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
228 num_rows);
229 break;
230 case JCS_EXT_BGR:
231 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
232 num_rows);
233 break;
234 case JCS_EXT_BGRX:
235 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
236 num_rows);
237 break;
238 case JCS_EXT_XBGR:
239 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
240 num_rows);
241 break;
242 case JCS_EXT_XRGB:
243 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
244 num_rows);
245 break;
246 default:
247 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
248 num_rows);
249 break;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000250 }
251}
252
253
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000254/**************** Cases other than YCbCr -> RGB **************/
255
256
257/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000258 * Color conversion for no colorspace change: just copy the data,
259 * converting from separate-planes to interleaved representation.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000260 */
261
Thomas G. Lane489583f1996-02-07 00:00:00 +0000262METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263null_convert (j_decompress_ptr cinfo,
264 JSAMPIMAGE input_buf, JDIMENSION input_row,
265 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000266{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000267 register JSAMPROW inptr, outptr;
268 register JDIMENSION count;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000269 register int num_components = cinfo->num_components;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 JDIMENSION num_cols = cinfo->output_width;
271 int ci;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000272
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000273 while (--num_rows >= 0) {
274 for (ci = 0; ci < num_components; ci++) {
275 inptr = input_buf[ci][input_row];
276 outptr = output_buf[0] + ci;
277 for (count = num_cols; count > 0; count--) {
278 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
279 outptr += num_components;
280 }
281 }
282 input_row++;
283 output_buf++;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000284 }
285}
286
287
288/*
289 * Color conversion for grayscale: just copy the data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 * This also works for YCbCr -> grayscale conversion, in which
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000291 * we just copy the Y (luminance) component and ignore chrominance.
292 */
293
Thomas G. Lane489583f1996-02-07 00:00:00 +0000294METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000295grayscale_convert (j_decompress_ptr cinfo,
296 JSAMPIMAGE input_buf, JDIMENSION input_row,
297 JSAMPARRAY output_buf, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000298{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
300 num_rows, cinfo->output_width);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000301}
302
303
304/*
DRCb4570bb2011-09-07 06:31:00 +0000305 * Convert grayscale to RGB
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000306 */
307
308METHODDEF(void)
309gray_rgb_convert (j_decompress_ptr cinfo,
310 JSAMPIMAGE input_buf, JDIMENSION input_row,
311 JSAMPARRAY output_buf, int num_rows)
312{
DRCb4570bb2011-09-07 06:31:00 +0000313 switch (cinfo->out_color_space) {
314 case JCS_EXT_RGB:
315 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
316 num_rows);
317 break;
318 case JCS_EXT_RGBX:
319 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
320 num_rows);
321 break;
322 case JCS_EXT_BGR:
323 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
324 num_rows);
325 break;
326 case JCS_EXT_BGRX:
327 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
328 num_rows);
329 break;
330 case JCS_EXT_XBGR:
331 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
332 num_rows);
333 break;
334 case JCS_EXT_XRGB:
335 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
336 num_rows);
337 break;
338 default:
339 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
340 num_rows);
341 break;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000342 }
343}
344
345
346/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347 * Adobe-style YCCK->CMYK conversion.
348 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
349 * conversion as above, while passing K (black) unchanged.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000350 * We assume build_ycc_rgb_table has been called.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000351 */
352
Thomas G. Lane489583f1996-02-07 00:00:00 +0000353METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354ycck_cmyk_convert (j_decompress_ptr cinfo,
355 JSAMPIMAGE input_buf, JDIMENSION input_row,
356 JSAMPARRAY output_buf, int num_rows)
357{
358 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
359 register int y, cb, cr;
360 register JSAMPROW outptr;
361 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
362 register JDIMENSION col;
363 JDIMENSION num_cols = cinfo->output_width;
364 /* copy these pointers into registers if possible */
365 register JSAMPLE * range_limit = cinfo->sample_range_limit;
366 register int * Crrtab = cconvert->Cr_r_tab;
367 register int * Cbbtab = cconvert->Cb_b_tab;
368 register INT32 * Crgtab = cconvert->Cr_g_tab;
369 register INT32 * Cbgtab = cconvert->Cb_g_tab;
370 SHIFT_TEMPS
371
372 while (--num_rows >= 0) {
373 inptr0 = input_buf[0][input_row];
374 inptr1 = input_buf[1][input_row];
375 inptr2 = input_buf[2][input_row];
376 inptr3 = input_buf[3][input_row];
377 input_row++;
378 outptr = *output_buf++;
379 for (col = 0; col < num_cols; col++) {
380 y = GETJSAMPLE(inptr0[col]);
381 cb = GETJSAMPLE(inptr1[col]);
382 cr = GETJSAMPLE(inptr2[col]);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000383 /* Range-limiting is essential due to noise introduced by DCT losses. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
385 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
386 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
387 SCALEBITS)))];
388 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
389 /* K passes through unchanged */
390 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
391 outptr += 4;
392 }
393 }
394}
395
396
397/*
398 * Empty method for start_pass.
399 */
400
Thomas G. Lane489583f1996-02-07 00:00:00 +0000401METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000402start_pass_dcolor (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000403{
404 /* no work needed */
405}
406
407
408/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000409 * Module initialization routine for output colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000410 */
411
Thomas G. Lane489583f1996-02-07 00:00:00 +0000412GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000413jinit_color_deconverter (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000414{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000415 my_cconvert_ptr cconvert;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000416 int ci;
417
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000418 cconvert = (my_cconvert_ptr)
419 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
420 SIZEOF(my_color_deconverter));
421 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000422 cconvert->pub.start_pass = start_pass_dcolor;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000423
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000424 /* Make sure num_components agrees with jpeg_color_space */
425 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000426 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000427 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000428 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000429 break;
430
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431 case JCS_RGB:
432 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000433 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000435 break;
436
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000437 case JCS_CMYK:
438 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000439 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000440 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000441 break;
442
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000443 default: /* JCS_UNKNOWN can be anything */
444 if (cinfo->num_components < 1)
445 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000446 break;
447 }
448
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000449 /* Set out_color_components and conversion method based on requested space.
450 * Also clear the component_needed flags for any unused components,
451 * so that earlier pipeline stages can avoid useless computation.
452 */
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000453
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000454 switch (cinfo->out_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000455 case JCS_GRAYSCALE:
456 cinfo->out_color_components = 1;
457 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
458 cinfo->jpeg_color_space == JCS_YCbCr) {
459 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lanecc7150e1993-02-18 00:00:00 +0000460 /* For color->grayscale conversion, only the Y (0) component is needed */
461 for (ci = 1; ci < cinfo->num_components; ci++)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000462 cinfo->comp_info[ci].component_needed = FALSE;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000463 } else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000464 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000465 break;
466
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000468 case JCS_EXT_RGB:
469 case JCS_EXT_RGBX:
470 case JCS_EXT_BGR:
471 case JCS_EXT_BGRX:
472 case JCS_EXT_XBGR:
473 case JCS_EXT_XRGB:
474 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475 if (cinfo->jpeg_color_space == JCS_YCbCr) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000476 if (jsimd_can_ycc_rgb())
477 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
478 else {
479 cconvert->pub.color_convert = ycc_rgb_convert;
480 build_ycc_rgb_table(cinfo);
481 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000482 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
483 cconvert->pub.color_convert = gray_rgb_convert;
DRCf25c0712009-04-03 12:00:51 +0000484 } else if (cinfo->jpeg_color_space == cinfo->out_color_space &&
485 rgb_pixelsize[cinfo->out_color_space] == 3) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000486 cconvert->pub.color_convert = null_convert;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000487 } else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000488 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
489 break;
490
491 case JCS_CMYK:
492 cinfo->out_color_components = 4;
493 if (cinfo->jpeg_color_space == JCS_YCCK) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000494 cconvert->pub.color_convert = ycck_cmyk_convert;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000495 build_ycc_rgb_table(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
497 cconvert->pub.color_convert = null_convert;
498 } else
499 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000500 break;
501
502 default:
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000503 /* Permit null conversion to same output space */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000504 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000505 cinfo->out_color_components = cinfo->num_components;
506 cconvert->pub.color_convert = null_convert;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000507 } else /* unsupported non-null conversion */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000508 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000509 break;
510 }
511
512 if (cinfo->quantize_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000513 cinfo->output_components = 1; /* single colormapped output component */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000514 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000515 cinfo->output_components = cinfo->out_color_components;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000516}