blob: 8d3feb8d9cb7af64ecaa6455489fcef024169043 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jccolor.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1991-1996, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00005 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCf25c0712009-04-03 12:00:51 +00006 * Copyright 2009 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 input 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. Lane2cbeb8a1991-10-07 00:00:00 +000017
18
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019/* Private subobject */
20
21typedef struct {
22 struct jpeg_color_converter pub; /* public fields */
23
24 /* Private state for RGB->YCC conversion */
25 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
26} my_color_converter;
27
28typedef my_color_converter * my_cconvert_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000029
30
Thomas G. Lane88aeed41992-12-10 00:00:00 +000031/**************** RGB -> YCbCr conversion: most common case **************/
32
33/*
34 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
35 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
36 * The conversion equations to be implemented are therefore
37 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000038 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
39 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
Thomas G. Lane88aeed41992-12-10 00:00:00 +000040 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000041 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
42 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
43 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
44 * were not represented exactly. Now we sacrifice exact representation of
45 * maximum red and maximum blue in order to get exact grayscales.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000046 *
47 * To avoid floating-point arithmetic, we represent the fractional constants
48 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
49 * the products by 2^16, with appropriate rounding, to get the correct answer.
50 *
51 * For even more speed, we avoid doing any multiplications in the inner loop
52 * by precalculating the constants times R,G,B for all possible values.
53 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
54 * for 12-bit samples it is still acceptable. It's not very reasonable for
55 * 16-bit samples, but if you want lossless storage you shouldn't be changing
56 * colorspace anyway.
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000057 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
Thomas G. Lane88aeed41992-12-10 00:00:00 +000058 * in the tables to save adding them separately in the inner loop.
59 */
60
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061#define SCALEBITS 16 /* speediest right-shift on some machines */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000062#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
Thomas G. Lane88aeed41992-12-10 00:00:00 +000063#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
64#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
65
66/* We allocate one big table and divide it up into eight parts, instead of
67 * doing eight alloc_small requests. This lets us use a single table base
68 * address, which can be held in a register in the inner loops on many
69 * machines (more than can hold all eight addresses, anyway).
70 */
71
Thomas G. Lane88aeed41992-12-10 00:00:00 +000072#define R_Y_OFF 0 /* offset to R => Y section */
73#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
74#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
75#define R_CB_OFF (3*(MAXJSAMPLE+1))
76#define G_CB_OFF (4*(MAXJSAMPLE+1))
77#define B_CB_OFF (5*(MAXJSAMPLE+1))
78#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
79#define G_CR_OFF (6*(MAXJSAMPLE+1))
80#define B_CR_OFF (7*(MAXJSAMPLE+1))
81#define TABLE_SIZE (8*(MAXJSAMPLE+1))
82
83
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000084/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085 * Initialize for RGB->YCC colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000086 */
87
Thomas G. Lane489583f1996-02-07 00:00:00 +000088METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089rgb_ycc_start (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000090{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
92 INT32 * rgb_ycc_tab;
Thomas G. Lane88aeed41992-12-10 00:00:00 +000093 INT32 i;
94
Thomas G. Lane88aeed41992-12-10 00:00:00 +000095 /* Allocate and fill in the conversion tables. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
97 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
98 (TABLE_SIZE * SIZEOF(INT32)));
Thomas G. Lane88aeed41992-12-10 00:00:00 +000099
100 for (i = 0; i <= MAXJSAMPLE; i++) {
101 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
102 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
103 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
104 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
105 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000106 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
107 * This ensures that the maximum output will round to MAXJSAMPLE
108 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
109 */
110 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000111/* B=>Cb and R=>Cr tables are the same
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000112 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000113*/
114 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
115 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
116 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000117}
118
119
120/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000121 * Convert some rows of samples to the JPEG colorspace.
122 *
123 * Note that we change from the application's interleaved-pixel format
124 * to our internal noninterleaved, one-plane-per-component format.
125 * The input buffer is therefore three times as wide as the output buffer.
126 *
127 * A starting row offset is provided only for the output buffer. The caller
128 * can easily adjust the passed input_buf value to accommodate any row
129 * offset required on that side.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000130 */
131
Thomas G. Lane489583f1996-02-07 00:00:00 +0000132METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000133rgb_ycc_convert (j_compress_ptr cinfo,
134 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
135 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000136{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000137 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000138 register int r, g, b;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000139 register INT32 * ctab = cconvert->rgb_ycc_tab;
140 register JSAMPROW inptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000141 register JSAMPROW outptr0, outptr1, outptr2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142 register JDIMENSION col;
143 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000144
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145 while (--num_rows >= 0) {
146 inptr = *input_buf++;
147 outptr0 = output_buf[0][output_row];
148 outptr1 = output_buf[1][output_row];
149 outptr2 = output_buf[2][output_row];
150 output_row++;
151 for (col = 0; col < num_cols; col++) {
DRCf25c0712009-04-03 12:00:51 +0000152 r = GETJSAMPLE(inptr[rgb_red[cinfo->in_color_space]]);
153 g = GETJSAMPLE(inptr[rgb_green[cinfo->in_color_space]]);
154 b = GETJSAMPLE(inptr[rgb_blue[cinfo->in_color_space]]);
155 inptr += rgb_pixelsize[cinfo->in_color_space];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000156 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000157 * must be too; we do not need an explicit range-limiting operation.
158 * Hence the value being shifted is never negative, and we don't
159 * need the general RIGHT_SHIFT macro.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000160 */
161 /* Y */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000162 outptr0[col] = (JSAMPLE)
163 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
164 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000165 /* Cb */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000166 outptr1[col] = (JSAMPLE)
167 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
168 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000169 /* Cr */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000170 outptr2[col] = (JSAMPLE)
171 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
172 >> SCALEBITS);
173 }
174 }
175}
176
177
178/**************** Cases other than RGB -> YCbCr **************/
179
180
181/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000182 * Convert some rows of samples to the JPEG colorspace.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000183 * This version handles RGB->grayscale conversion, which is the same
184 * as the RGB->Y portion of RGB->YCbCr.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000185 * We assume rgb_ycc_start has been called (we only use the Y tables).
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000186 */
187
Thomas G. Lane489583f1996-02-07 00:00:00 +0000188METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189rgb_gray_convert (j_compress_ptr cinfo,
190 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
191 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000192{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000194 register int r, g, b;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000195 register INT32 * ctab = cconvert->rgb_ycc_tab;
196 register JSAMPROW inptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000197 register JSAMPROW outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198 register JDIMENSION col;
199 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000200
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000201 while (--num_rows >= 0) {
202 inptr = *input_buf++;
203 outptr = output_buf[0][output_row];
204 output_row++;
205 for (col = 0; col < num_cols; col++) {
DRCf25c0712009-04-03 12:00:51 +0000206 r = GETJSAMPLE(inptr[rgb_red[cinfo->in_color_space]]);
207 g = GETJSAMPLE(inptr[rgb_green[cinfo->in_color_space]]);
208 b = GETJSAMPLE(inptr[rgb_blue[cinfo->in_color_space]]);
209 inptr += rgb_pixelsize[cinfo->in_color_space];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000210 /* Y */
211 outptr[col] = (JSAMPLE)
212 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
213 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000214 }
215 }
216}
217
218
219/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 * Convert some rows of samples to the JPEG colorspace.
221 * This version handles Adobe-style CMYK->YCCK conversion,
222 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
223 * conversion as above, while passing K (black) unchanged.
224 * We assume rgb_ycc_start has been called.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000225 */
226
Thomas G. Lane489583f1996-02-07 00:00:00 +0000227METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228cmyk_ycck_convert (j_compress_ptr cinfo,
229 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
230 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000231{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000232 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
233 register int r, g, b;
234 register INT32 * ctab = cconvert->rgb_ycc_tab;
235 register JSAMPROW inptr;
236 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
237 register JDIMENSION col;
238 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000239
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 while (--num_rows >= 0) {
241 inptr = *input_buf++;
242 outptr0 = output_buf[0][output_row];
243 outptr1 = output_buf[1][output_row];
244 outptr2 = output_buf[2][output_row];
245 outptr3 = output_buf[3][output_row];
246 output_row++;
247 for (col = 0; col < num_cols; col++) {
248 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
249 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
250 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
251 /* K passes through as-is */
252 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
253 inptr += 4;
254 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
255 * must be too; we do not need an explicit range-limiting operation.
256 * Hence the value being shifted is never negative, and we don't
257 * need the general RIGHT_SHIFT macro.
258 */
259 /* Y */
260 outptr0[col] = (JSAMPLE)
261 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
262 >> SCALEBITS);
263 /* Cb */
264 outptr1[col] = (JSAMPLE)
265 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
266 >> SCALEBITS);
267 /* Cr */
268 outptr2[col] = (JSAMPLE)
269 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
270 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000271 }
272 }
273}
274
275
276/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277 * Convert some rows of samples to the JPEG colorspace.
278 * This version handles grayscale output with no conversion.
279 * The source can be either plain grayscale or YCbCr (since Y == gray).
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000280 */
281
Thomas G. Lane489583f1996-02-07 00:00:00 +0000282METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000283grayscale_convert (j_compress_ptr cinfo,
284 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
285 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000286{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000287 register JSAMPROW inptr;
288 register JSAMPROW outptr;
289 register JDIMENSION col;
290 JDIMENSION num_cols = cinfo->image_width;
291 int instride = cinfo->input_components;
292
293 while (--num_rows >= 0) {
294 inptr = *input_buf++;
295 outptr = output_buf[0][output_row];
296 output_row++;
297 for (col = 0; col < num_cols; col++) {
298 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
299 inptr += instride;
300 }
301 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000302}
303
304
305/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000306 * Convert some rows of samples to the JPEG colorspace.
307 * This version handles multi-component colorspaces without conversion.
308 * We assume input_components == num_components.
309 */
310
Thomas G. Lane489583f1996-02-07 00:00:00 +0000311METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000312null_convert (j_compress_ptr cinfo,
313 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
314 JDIMENSION output_row, int num_rows)
315{
316 register JSAMPROW inptr;
317 register JSAMPROW outptr;
318 register JDIMENSION col;
319 register int ci;
320 int nc = cinfo->num_components;
321 JDIMENSION num_cols = cinfo->image_width;
322
323 while (--num_rows >= 0) {
324 /* It seems fastest to make a separate pass for each component. */
325 for (ci = 0; ci < nc; ci++) {
326 inptr = *input_buf;
327 outptr = output_buf[ci][output_row];
328 for (col = 0; col < num_cols; col++) {
329 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
330 inptr += nc;
331 }
332 }
333 input_buf++;
334 output_row++;
335 }
336}
337
338
339/*
340 * Empty method for start_pass.
341 */
342
Thomas G. Lane489583f1996-02-07 00:00:00 +0000343METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344null_method (j_compress_ptr cinfo)
345{
346 /* no work needed */
347}
348
349
350/*
351 * Module initialization routine for input colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000352 */
353
Thomas G. Lane489583f1996-02-07 00:00:00 +0000354GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000355jinit_color_converter (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000356{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000357 my_cconvert_ptr cconvert;
358
359 cconvert = (my_cconvert_ptr)
360 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
361 SIZEOF(my_color_converter));
362 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
363 /* set start_pass to null method until we find out differently */
364 cconvert->pub.start_pass = null_method;
365
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000366 /* Make sure input_components agrees with in_color_space */
367 switch (cinfo->in_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000368 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000369 if (cinfo->input_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000371 break;
372
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000374 case JCS_EXT_RGB:
375 case JCS_EXT_RGBX:
376 case JCS_EXT_BGR:
377 case JCS_EXT_BGRX:
378 case JCS_EXT_XBGR:
379 case JCS_EXT_XRGB:
380 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
381 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
382 break;
383
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000385 if (cinfo->input_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000386 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000387 break;
388
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000389 case JCS_CMYK:
390 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000391 if (cinfo->input_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000392 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000393 break;
394
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000395 default: /* JCS_UNKNOWN can be anything */
396 if (cinfo->input_components < 1)
397 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000398 break;
399 }
400
401 /* Check num_components, set conversion method based on requested space */
402 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000404 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
406 if (cinfo->in_color_space == JCS_GRAYSCALE)
407 cconvert->pub.color_convert = grayscale_convert;
DRCf25c0712009-04-03 12:00:51 +0000408 else if (cinfo->in_color_space == JCS_RGB ||
409 cinfo->in_color_space == JCS_EXT_RGB ||
410 cinfo->in_color_space == JCS_EXT_RGBX ||
411 cinfo->in_color_space == JCS_EXT_BGR ||
412 cinfo->in_color_space == JCS_EXT_BGRX ||
413 cinfo->in_color_space == JCS_EXT_XBGR ||
414 cinfo->in_color_space == JCS_EXT_XRGB) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000415 cconvert->pub.start_pass = rgb_ycc_start;
416 cconvert->pub.color_convert = rgb_gray_convert;
417 } else if (cinfo->in_color_space == JCS_YCbCr)
418 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000419 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000420 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000421 break;
422
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000423 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000424 case JCS_EXT_RGB:
425 case JCS_EXT_RGBX:
426 case JCS_EXT_BGR:
427 case JCS_EXT_BGRX:
428 case JCS_EXT_XBGR:
429 case JCS_EXT_XRGB:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000430 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCf25c0712009-04-03 12:00:51 +0000432 if (cinfo->in_color_space == cinfo->jpeg_color_space &&
433 rgb_pixelsize[cinfo->in_color_space] == 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000435 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000437 break;
438
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000439 case JCS_YCbCr:
440 if (cinfo->num_components != 3)
441 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCf25c0712009-04-03 12:00:51 +0000442 if (cinfo->in_color_space == JCS_RGB ||
443 cinfo->in_color_space == JCS_EXT_RGB ||
444 cinfo->in_color_space == JCS_EXT_RGBX ||
445 cinfo->in_color_space == JCS_EXT_BGR ||
446 cinfo->in_color_space == JCS_EXT_BGRX ||
447 cinfo->in_color_space == JCS_EXT_XBGR ||
448 cinfo->in_color_space == JCS_EXT_XRGB) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000449 if (jsimd_can_rgb_ycc())
450 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
451 else {
452 cconvert->pub.start_pass = rgb_ycc_start;
453 cconvert->pub.color_convert = rgb_ycc_convert;
454 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000455 } else if (cinfo->in_color_space == JCS_YCbCr)
456 cconvert->pub.color_convert = null_convert;
457 else
458 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
459 break;
460
461 case JCS_CMYK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000462 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
464 if (cinfo->in_color_space == JCS_CMYK)
465 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000466 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000468 break;
469
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000470 case JCS_YCCK:
471 if (cinfo->num_components != 4)
472 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
473 if (cinfo->in_color_space == JCS_CMYK) {
474 cconvert->pub.start_pass = rgb_ycc_start;
475 cconvert->pub.color_convert = cmyk_ycck_convert;
476 } else if (cinfo->in_color_space == JCS_YCCK)
477 cconvert->pub.color_convert = null_convert;
478 else
479 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
480 break;
481
482 default: /* allow null conversion of JCS_UNKNOWN */
483 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
484 cinfo->num_components != cinfo->input_components)
485 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
486 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000487 break;
488 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000489}