blob: 9559aa599e5fa53db8b52d8bcc4247d1ed92b272 [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
DRC5a9d6c32011-02-27 10:26:08 +00006 * Copyright 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 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;
DRC5a9d6c32011-02-27 10:26:08 +0000144 int rindex = rgb_red[cinfo->in_color_space];
145 int gindex = rgb_green[cinfo->in_color_space];
146 int bindex = rgb_blue[cinfo->in_color_space];
147 int rgbstride = rgb_pixelsize[cinfo->in_color_space];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000148
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000149 while (--num_rows >= 0) {
150 inptr = *input_buf++;
151 outptr0 = output_buf[0][output_row];
152 outptr1 = output_buf[1][output_row];
153 outptr2 = output_buf[2][output_row];
154 output_row++;
155 for (col = 0; col < num_cols; col++) {
DRC5a9d6c32011-02-27 10:26:08 +0000156 r = GETJSAMPLE(inptr[rindex]);
157 g = GETJSAMPLE(inptr[gindex]);
158 b = GETJSAMPLE(inptr[bindex]);
159 inptr += rgbstride;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000160 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000161 * must be too; we do not need an explicit range-limiting operation.
162 * Hence the value being shifted is never negative, and we don't
163 * need the general RIGHT_SHIFT macro.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000164 */
165 /* Y */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000166 outptr0[col] = (JSAMPLE)
167 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
168 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000169 /* Cb */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000170 outptr1[col] = (JSAMPLE)
171 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
172 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000173 /* Cr */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000174 outptr2[col] = (JSAMPLE)
175 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
176 >> SCALEBITS);
177 }
178 }
179}
180
181
182/**************** Cases other than RGB -> YCbCr **************/
183
184
185/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 * Convert some rows of samples to the JPEG colorspace.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000187 * This version handles RGB->grayscale conversion, which is the same
188 * as the RGB->Y portion of RGB->YCbCr.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189 * We assume rgb_ycc_start has been called (we only use the Y tables).
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000190 */
191
Thomas G. Lane489583f1996-02-07 00:00:00 +0000192METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193rgb_gray_convert (j_compress_ptr cinfo,
194 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
195 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000196{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
DRCc8666332011-02-18 11:23:45 +0000198 register int r, g, b;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000199 register INT32 * ctab = cconvert->rgb_ycc_tab;
200 register JSAMPROW inptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000201 register JSAMPROW outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 register JDIMENSION col;
203 JDIMENSION num_cols = cinfo->image_width;
DRC5a9d6c32011-02-27 10:26:08 +0000204 int rindex = rgb_red[cinfo->in_color_space];
205 int gindex = rgb_green[cinfo->in_color_space];
206 int bindex = rgb_blue[cinfo->in_color_space];
207 int rgbstride = rgb_pixelsize[cinfo->in_color_space];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000208
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209 while (--num_rows >= 0) {
210 inptr = *input_buf++;
211 outptr = output_buf[0][output_row];
212 output_row++;
DRCc8666332011-02-18 11:23:45 +0000213 for (col = 0; col < num_cols; col++) {
DRC5a9d6c32011-02-27 10:26:08 +0000214 r = GETJSAMPLE(inptr[rindex]);
215 g = GETJSAMPLE(inptr[gindex]);
216 b = GETJSAMPLE(inptr[bindex]);
217 inptr += rgbstride;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000218 /* Y */
DRCc8666332011-02-18 11:23:45 +0000219 outptr[col] = (JSAMPLE)
220 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
221 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000222 }
223 }
224}
225
226
227/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 * Convert some rows of samples to the JPEG colorspace.
229 * This version handles Adobe-style CMYK->YCCK conversion,
230 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
231 * conversion as above, while passing K (black) unchanged.
232 * We assume rgb_ycc_start has been called.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000233 */
234
Thomas G. Lane489583f1996-02-07 00:00:00 +0000235METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000236cmyk_ycck_convert (j_compress_ptr cinfo,
237 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
238 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000239{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
241 register int r, g, b;
242 register INT32 * ctab = cconvert->rgb_ycc_tab;
243 register JSAMPROW inptr;
244 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
245 register JDIMENSION col;
246 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000247
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000248 while (--num_rows >= 0) {
249 inptr = *input_buf++;
250 outptr0 = output_buf[0][output_row];
251 outptr1 = output_buf[1][output_row];
252 outptr2 = output_buf[2][output_row];
253 outptr3 = output_buf[3][output_row];
254 output_row++;
255 for (col = 0; col < num_cols; col++) {
256 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
257 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
258 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
259 /* K passes through as-is */
260 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
261 inptr += 4;
262 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
263 * must be too; we do not need an explicit range-limiting operation.
264 * Hence the value being shifted is never negative, and we don't
265 * need the general RIGHT_SHIFT macro.
266 */
267 /* Y */
268 outptr0[col] = (JSAMPLE)
269 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
270 >> SCALEBITS);
271 /* Cb */
272 outptr1[col] = (JSAMPLE)
273 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
274 >> SCALEBITS);
275 /* Cr */
276 outptr2[col] = (JSAMPLE)
277 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
278 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000279 }
280 }
281}
282
283
284/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000285 * Convert some rows of samples to the JPEG colorspace.
286 * This version handles grayscale output with no conversion.
287 * The source can be either plain grayscale or YCbCr (since Y == gray).
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000288 */
289
Thomas G. Lane489583f1996-02-07 00:00:00 +0000290METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000291grayscale_convert (j_compress_ptr cinfo,
292 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
293 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000294{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000295 register JSAMPROW inptr;
296 register JSAMPROW outptr;
297 register JDIMENSION col;
298 JDIMENSION num_cols = cinfo->image_width;
299 int instride = cinfo->input_components;
300
301 while (--num_rows >= 0) {
302 inptr = *input_buf++;
303 outptr = output_buf[0][output_row];
304 output_row++;
305 for (col = 0; col < num_cols; col++) {
306 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
307 inptr += instride;
308 }
309 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000310}
311
312
313/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000314 * Convert some rows of samples to the JPEG colorspace.
315 * This version handles multi-component colorspaces without conversion.
316 * We assume input_components == num_components.
317 */
318
Thomas G. Lane489583f1996-02-07 00:00:00 +0000319METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320null_convert (j_compress_ptr cinfo,
321 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
322 JDIMENSION output_row, int num_rows)
323{
324 register JSAMPROW inptr;
325 register JSAMPROW outptr;
326 register JDIMENSION col;
327 register int ci;
328 int nc = cinfo->num_components;
329 JDIMENSION num_cols = cinfo->image_width;
330
331 while (--num_rows >= 0) {
332 /* It seems fastest to make a separate pass for each component. */
333 for (ci = 0; ci < nc; ci++) {
334 inptr = *input_buf;
335 outptr = output_buf[ci][output_row];
336 for (col = 0; col < num_cols; col++) {
337 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
338 inptr += nc;
339 }
340 }
341 input_buf++;
342 output_row++;
343 }
344}
345
346
347/*
348 * Empty method for start_pass.
349 */
350
Thomas G. Lane489583f1996-02-07 00:00:00 +0000351METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352null_method (j_compress_ptr cinfo)
353{
354 /* no work needed */
355}
356
357
358/*
359 * Module initialization routine for input colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000360 */
361
Thomas G. Lane489583f1996-02-07 00:00:00 +0000362GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363jinit_color_converter (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000364{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000365 my_cconvert_ptr cconvert;
366
367 cconvert = (my_cconvert_ptr)
368 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
369 SIZEOF(my_color_converter));
370 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
371 /* set start_pass to null method until we find out differently */
372 cconvert->pub.start_pass = null_method;
373
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000374 /* Make sure input_components agrees with in_color_space */
375 switch (cinfo->in_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000377 if (cinfo->input_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000378 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000379 break;
380
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000381 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000382 case JCS_EXT_RGB:
383 case JCS_EXT_RGBX:
384 case JCS_EXT_BGR:
385 case JCS_EXT_BGRX:
386 case JCS_EXT_XBGR:
387 case JCS_EXT_XRGB:
388 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
389 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
390 break;
391
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000392 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000393 if (cinfo->input_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000394 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000395 break;
396
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397 case JCS_CMYK:
398 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000399 if (cinfo->input_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000400 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000401 break;
402
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403 default: /* JCS_UNKNOWN can be anything */
404 if (cinfo->input_components < 1)
405 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000406 break;
407 }
408
409 /* Check num_components, set conversion method based on requested space */
410 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000411 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000412 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000413 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
414 if (cinfo->in_color_space == JCS_GRAYSCALE)
415 cconvert->pub.color_convert = grayscale_convert;
DRCf25c0712009-04-03 12:00:51 +0000416 else if (cinfo->in_color_space == JCS_RGB ||
417 cinfo->in_color_space == JCS_EXT_RGB ||
418 cinfo->in_color_space == JCS_EXT_RGBX ||
419 cinfo->in_color_space == JCS_EXT_BGR ||
420 cinfo->in_color_space == JCS_EXT_BGRX ||
421 cinfo->in_color_space == JCS_EXT_XBGR ||
422 cinfo->in_color_space == JCS_EXT_XRGB) {
DRCc8666332011-02-18 11:23:45 +0000423 if (jsimd_can_rgb_gray())
424 cconvert->pub.color_convert = jsimd_rgb_gray_convert;
425 else {
426 cconvert->pub.start_pass = rgb_ycc_start;
427 cconvert->pub.color_convert = rgb_gray_convert;
428 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000429 } else if (cinfo->in_color_space == JCS_YCbCr)
430 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000431 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000432 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000433 break;
434
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000435 case JCS_RGB:
DRCf25c0712009-04-03 12:00:51 +0000436 case JCS_EXT_RGB:
437 case JCS_EXT_RGBX:
438 case JCS_EXT_BGR:
439 case JCS_EXT_BGRX:
440 case JCS_EXT_XBGR:
441 case JCS_EXT_XRGB:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000442 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000443 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCf25c0712009-04-03 12:00:51 +0000444 if (cinfo->in_color_space == cinfo->jpeg_color_space &&
445 rgb_pixelsize[cinfo->in_color_space] == 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000446 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000447 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000448 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000449 break;
450
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000451 case JCS_YCbCr:
452 if (cinfo->num_components != 3)
453 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
DRCf25c0712009-04-03 12:00:51 +0000454 if (cinfo->in_color_space == JCS_RGB ||
455 cinfo->in_color_space == JCS_EXT_RGB ||
456 cinfo->in_color_space == JCS_EXT_RGBX ||
457 cinfo->in_color_space == JCS_EXT_BGR ||
458 cinfo->in_color_space == JCS_EXT_BGRX ||
459 cinfo->in_color_space == JCS_EXT_XBGR ||
460 cinfo->in_color_space == JCS_EXT_XRGB) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000461 if (jsimd_can_rgb_ycc())
462 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
463 else {
464 cconvert->pub.start_pass = rgb_ycc_start;
465 cconvert->pub.color_convert = rgb_ycc_convert;
466 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 } else if (cinfo->in_color_space == JCS_YCbCr)
468 cconvert->pub.color_convert = null_convert;
469 else
470 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
471 break;
472
473 case JCS_CMYK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000474 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
476 if (cinfo->in_color_space == JCS_CMYK)
477 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000478 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000479 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000480 break;
481
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000482 case JCS_YCCK:
483 if (cinfo->num_components != 4)
484 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
485 if (cinfo->in_color_space == JCS_CMYK) {
486 cconvert->pub.start_pass = rgb_ycc_start;
487 cconvert->pub.color_convert = cmyk_ycck_convert;
488 } else if (cinfo->in_color_space == JCS_YCCK)
489 cconvert->pub.color_convert = null_convert;
490 else
491 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
492 break;
493
494 default: /* allow null conversion of JCS_UNKNOWN */
495 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
496 cinfo->num_components != cinfo->input_components)
497 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
498 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000499 break;
500 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000501}