blob: 2407456c112c51894cb0bddeecceb88d8f44be03 [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
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains input colorspace conversion routines.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000010 */
11
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000012#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000013#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000015#include "jsimd.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000016
17
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018/* Private subobject */
19
20typedef struct {
21 struct jpeg_color_converter pub; /* public fields */
22
23 /* Private state for RGB->YCC conversion */
24 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
25} my_color_converter;
26
27typedef my_color_converter * my_cconvert_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000028
29
Thomas G. Lane88aeed41992-12-10 00:00:00 +000030/**************** RGB -> YCbCr conversion: most common case **************/
31
32/*
33 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
34 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
35 * The conversion equations to be implemented are therefore
36 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000037 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
38 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
Thomas G. Lane88aeed41992-12-10 00:00:00 +000039 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000040 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
41 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
42 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
43 * were not represented exactly. Now we sacrifice exact representation of
44 * maximum red and maximum blue in order to get exact grayscales.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000045 *
46 * To avoid floating-point arithmetic, we represent the fractional constants
47 * 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.
49 *
50 * For even more speed, we avoid doing any multiplications in the inner loop
51 * by precalculating the constants times R,G,B for all possible values.
52 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
53 * for 12-bit samples it is still acceptable. It's not very reasonable for
54 * 16-bit samples, but if you want lossless storage you shouldn't be changing
55 * colorspace anyway.
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000056 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
Thomas G. Lane88aeed41992-12-10 00:00:00 +000057 * in the tables to save adding them separately in the inner loop.
58 */
59
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000060#define SCALEBITS 16 /* speediest right-shift on some machines */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000061#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
Thomas G. Lane88aeed41992-12-10 00:00:00 +000062#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
63#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
64
65/* We allocate one big table and divide it up into eight parts, instead of
66 * doing eight alloc_small requests. This lets us use a single table base
67 * address, which can be held in a register in the inner loops on many
68 * machines (more than can hold all eight addresses, anyway).
69 */
70
Thomas G. Lane88aeed41992-12-10 00:00:00 +000071#define R_Y_OFF 0 /* offset to R => Y section */
72#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
73#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
74#define R_CB_OFF (3*(MAXJSAMPLE+1))
75#define G_CB_OFF (4*(MAXJSAMPLE+1))
76#define B_CB_OFF (5*(MAXJSAMPLE+1))
77#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
78#define G_CR_OFF (6*(MAXJSAMPLE+1))
79#define B_CR_OFF (7*(MAXJSAMPLE+1))
80#define TABLE_SIZE (8*(MAXJSAMPLE+1))
81
82
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000083/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084 * Initialize for RGB->YCC colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000085 */
86
Thomas G. Lane489583f1996-02-07 00:00:00 +000087METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000088rgb_ycc_start (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000089{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
91 INT32 * rgb_ycc_tab;
Thomas G. Lane88aeed41992-12-10 00:00:00 +000092 INT32 i;
93
Thomas G. Lane88aeed41992-12-10 00:00:00 +000094 /* Allocate and fill in the conversion tables. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
96 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
97 (TABLE_SIZE * SIZEOF(INT32)));
Thomas G. Lane88aeed41992-12-10 00:00:00 +000098
99 for (i = 0; i <= MAXJSAMPLE; i++) {
100 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
101 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
102 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
103 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
104 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000105 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
106 * This ensures that the maximum output will round to MAXJSAMPLE
107 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
108 */
109 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000110/* B=>Cb and R=>Cr tables are the same
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000111 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000112*/
113 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
114 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
115 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000116}
117
118
119/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120 * Convert some rows of samples to the JPEG colorspace.
121 *
122 * Note that we change from the application's interleaved-pixel format
123 * to our internal noninterleaved, one-plane-per-component format.
124 * The input buffer is therefore three times as wide as the output buffer.
125 *
126 * A starting row offset is provided only for the output buffer. The caller
127 * can easily adjust the passed input_buf value to accommodate any row
128 * offset required on that side.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000129 */
130
Thomas G. Lane489583f1996-02-07 00:00:00 +0000131METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132rgb_ycc_convert (j_compress_ptr cinfo,
133 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
134 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000135{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000136 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000137 register int r, g, b;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138 register INT32 * ctab = cconvert->rgb_ycc_tab;
139 register JSAMPROW inptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000140 register JSAMPROW outptr0, outptr1, outptr2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141 register JDIMENSION col;
142 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000143
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144 while (--num_rows >= 0) {
145 inptr = *input_buf++;
146 outptr0 = output_buf[0][output_row];
147 outptr1 = output_buf[1][output_row];
148 outptr2 = output_buf[2][output_row];
149 output_row++;
150 for (col = 0; col < num_cols; col++) {
151 r = GETJSAMPLE(inptr[RGB_RED]);
152 g = GETJSAMPLE(inptr[RGB_GREEN]);
153 b = GETJSAMPLE(inptr[RGB_BLUE]);
154 inptr += RGB_PIXELSIZE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000155 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000156 * must be too; we do not need an explicit range-limiting operation.
157 * Hence the value being shifted is never negative, and we don't
158 * need the general RIGHT_SHIFT macro.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000159 */
160 /* Y */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000161 outptr0[col] = (JSAMPLE)
162 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
163 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000164 /* Cb */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000165 outptr1[col] = (JSAMPLE)
166 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
167 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000168 /* Cr */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000169 outptr2[col] = (JSAMPLE)
170 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
171 >> SCALEBITS);
172 }
173 }
174}
175
176
177/**************** Cases other than RGB -> YCbCr **************/
178
179
180/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 * Convert some rows of samples to the JPEG colorspace.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000182 * This version handles RGB->grayscale conversion, which is the same
183 * as the RGB->Y portion of RGB->YCbCr.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000184 * We assume rgb_ycc_start has been called (we only use the Y tables).
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000185 */
186
Thomas G. Lane489583f1996-02-07 00:00:00 +0000187METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188rgb_gray_convert (j_compress_ptr cinfo,
189 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
190 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000191{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000193 register int r, g, b;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 register INT32 * ctab = cconvert->rgb_ycc_tab;
195 register JSAMPROW inptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000196 register JSAMPROW outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197 register JDIMENSION col;
198 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000199
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000200 while (--num_rows >= 0) {
201 inptr = *input_buf++;
202 outptr = output_buf[0][output_row];
203 output_row++;
204 for (col = 0; col < num_cols; col++) {
205 r = GETJSAMPLE(inptr[RGB_RED]);
206 g = GETJSAMPLE(inptr[RGB_GREEN]);
207 b = GETJSAMPLE(inptr[RGB_BLUE]);
208 inptr += RGB_PIXELSIZE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000209 /* Y */
210 outptr[col] = (JSAMPLE)
211 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
212 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000213 }
214 }
215}
216
217
218/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000219 * Convert some rows of samples to the JPEG colorspace.
220 * This version handles Adobe-style CMYK->YCCK conversion,
221 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
222 * conversion as above, while passing K (black) unchanged.
223 * We assume rgb_ycc_start has been called.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000224 */
225
Thomas G. Lane489583f1996-02-07 00:00:00 +0000226METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000227cmyk_ycck_convert (j_compress_ptr cinfo,
228 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
229 JDIMENSION output_row, int num_rows)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000230{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
232 register int r, g, b;
233 register INT32 * ctab = cconvert->rgb_ycc_tab;
234 register JSAMPROW inptr;
235 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
236 register JDIMENSION col;
237 JDIMENSION num_cols = cinfo->image_width;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000238
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239 while (--num_rows >= 0) {
240 inptr = *input_buf++;
241 outptr0 = output_buf[0][output_row];
242 outptr1 = output_buf[1][output_row];
243 outptr2 = output_buf[2][output_row];
244 outptr3 = output_buf[3][output_row];
245 output_row++;
246 for (col = 0; col < num_cols; col++) {
247 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
248 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
249 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
250 /* K passes through as-is */
251 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
252 inptr += 4;
253 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
254 * must be too; we do not need an explicit range-limiting operation.
255 * Hence the value being shifted is never negative, and we don't
256 * need the general RIGHT_SHIFT macro.
257 */
258 /* Y */
259 outptr0[col] = (JSAMPLE)
260 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
261 >> SCALEBITS);
262 /* Cb */
263 outptr1[col] = (JSAMPLE)
264 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
265 >> SCALEBITS);
266 /* Cr */
267 outptr2[col] = (JSAMPLE)
268 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
269 >> SCALEBITS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000270 }
271 }
272}
273
274
275/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000276 * Convert some rows of samples to the JPEG colorspace.
277 * This version handles grayscale output with no conversion.
278 * The source can be either plain grayscale or YCbCr (since Y == gray).
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000279 */
280
Thomas G. Lane489583f1996-02-07 00:00:00 +0000281METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000282grayscale_convert (j_compress_ptr cinfo,
283 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
284 JDIMENSION output_row, int num_rows)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000285{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000286 register JSAMPROW inptr;
287 register JSAMPROW outptr;
288 register JDIMENSION col;
289 JDIMENSION num_cols = cinfo->image_width;
290 int instride = cinfo->input_components;
291
292 while (--num_rows >= 0) {
293 inptr = *input_buf++;
294 outptr = output_buf[0][output_row];
295 output_row++;
296 for (col = 0; col < num_cols; col++) {
297 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
298 inptr += instride;
299 }
300 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000301}
302
303
304/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305 * Convert some rows of samples to the JPEG colorspace.
306 * This version handles multi-component colorspaces without conversion.
307 * We assume input_components == num_components.
308 */
309
Thomas G. Lane489583f1996-02-07 00:00:00 +0000310METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311null_convert (j_compress_ptr cinfo,
312 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
313 JDIMENSION output_row, int num_rows)
314{
315 register JSAMPROW inptr;
316 register JSAMPROW outptr;
317 register JDIMENSION col;
318 register int ci;
319 int nc = cinfo->num_components;
320 JDIMENSION num_cols = cinfo->image_width;
321
322 while (--num_rows >= 0) {
323 /* It seems fastest to make a separate pass for each component. */
324 for (ci = 0; ci < nc; ci++) {
325 inptr = *input_buf;
326 outptr = output_buf[ci][output_row];
327 for (col = 0; col < num_cols; col++) {
328 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
329 inptr += nc;
330 }
331 }
332 input_buf++;
333 output_row++;
334 }
335}
336
337
338/*
339 * Empty method for start_pass.
340 */
341
Thomas G. Lane489583f1996-02-07 00:00:00 +0000342METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000343null_method (j_compress_ptr cinfo)
344{
345 /* no work needed */
346}
347
348
349/*
350 * Module initialization routine for input colorspace conversion.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000351 */
352
Thomas G. Lane489583f1996-02-07 00:00:00 +0000353GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354jinit_color_converter (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000355{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000356 my_cconvert_ptr cconvert;
357
358 cconvert = (my_cconvert_ptr)
359 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
360 SIZEOF(my_color_converter));
361 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
362 /* set start_pass to null method until we find out differently */
363 cconvert->pub.start_pass = null_method;
364
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000365 /* Make sure input_components agrees with in_color_space */
366 switch (cinfo->in_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000368 if (cinfo->input_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000369 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000370 break;
371
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000372 case JCS_RGB:
373#if RGB_PIXELSIZE != 3
374 if (cinfo->input_components != RGB_PIXELSIZE)
375 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
376 break;
377#endif /* else share code with YCbCr */
378
379 case JCS_YCbCr:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000380 if (cinfo->input_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000381 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000382 break;
383
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 case JCS_CMYK:
385 case JCS_YCCK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000386 if (cinfo->input_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000388 break;
389
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000390 default: /* JCS_UNKNOWN can be anything */
391 if (cinfo->input_components < 1)
392 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000393 break;
394 }
395
396 /* Check num_components, set conversion method based on requested space */
397 switch (cinfo->jpeg_color_space) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000398 case JCS_GRAYSCALE:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000399 if (cinfo->num_components != 1)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000400 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
401 if (cinfo->in_color_space == JCS_GRAYSCALE)
402 cconvert->pub.color_convert = grayscale_convert;
403 else if (cinfo->in_color_space == JCS_RGB) {
404 cconvert->pub.start_pass = rgb_ycc_start;
405 cconvert->pub.color_convert = rgb_gray_convert;
406 } else if (cinfo->in_color_space == JCS_YCbCr)
407 cconvert->pub.color_convert = grayscale_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000408 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000409 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000410 break;
411
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000412 case JCS_RGB:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000413 if (cinfo->num_components != 3)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000414 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
415 if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
416 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000417 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000418 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000419 break;
420
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000421 case JCS_YCbCr:
422 if (cinfo->num_components != 3)
423 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
424 if (cinfo->in_color_space == JCS_RGB) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000425 if (jsimd_can_rgb_ycc())
426 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
427 else {
428 cconvert->pub.start_pass = rgb_ycc_start;
429 cconvert->pub.color_convert = rgb_ycc_convert;
430 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431 } else if (cinfo->in_color_space == JCS_YCbCr)
432 cconvert->pub.color_convert = null_convert;
433 else
434 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
435 break;
436
437 case JCS_CMYK:
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000438 if (cinfo->num_components != 4)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000439 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
440 if (cinfo->in_color_space == JCS_CMYK)
441 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000442 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000443 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000444 break;
445
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000446 case JCS_YCCK:
447 if (cinfo->num_components != 4)
448 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
449 if (cinfo->in_color_space == JCS_CMYK) {
450 cconvert->pub.start_pass = rgb_ycc_start;
451 cconvert->pub.color_convert = cmyk_ycck_convert;
452 } else if (cinfo->in_color_space == JCS_YCCK)
453 cconvert->pub.color_convert = null_convert;
454 else
455 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
456 break;
457
458 default: /* allow null conversion of JCS_UNKNOWN */
459 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
460 cinfo->num_components != cinfo->input_components)
461 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
462 cconvert->pub.color_convert = null_convert;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000463 break;
464 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000465}