3a0772bb5a77223e99930d642942a7857f074d8c
[platform/framework/web/crosswalk.git] / src / third_party / libjpeg_turbo / jccolor.c
1 /*
2  * jccolor.c
3  *
4  * Copyright (C) 1991-1996, Thomas G. Lane.
5  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
6  * Copyright (C) 2009-2012, D. R. Commander.
7  * 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.
11  */
12
13 #define JPEG_INTERNALS
14 #include "jinclude.h"
15 #include "jpeglib.h"
16 #include "jsimd.h"
17 #include "config.h"
18
19
20 /* Private subobject */
21
22 typedef struct {
23   struct jpeg_color_converter pub; /* public fields */
24
25   /* Private state for RGB->YCC conversion */
26   INT32 * rgb_ycc_tab;          /* => table for RGB to YCbCr conversion */
27 } my_color_converter;
28
29 typedef my_color_converter * my_cconvert_ptr;
30
31
32 /**************** RGB -> YCbCr conversion: most common case **************/
33
34 /*
35  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
36  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
37  * The conversion equations to be implemented are therefore
38  *      Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
39  *      Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
40  *      Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
41  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
42  * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
43  * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
44  * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
45  * were not represented exactly.  Now we sacrifice exact representation of
46  * maximum red and maximum blue in order to get exact grayscales.
47  *
48  * To avoid floating-point arithmetic, we represent the fractional constants
49  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
50  * the products by 2^16, with appropriate rounding, to get the correct answer.
51  *
52  * For even more speed, we avoid doing any multiplications in the inner loop
53  * by precalculating the constants times R,G,B for all possible values.
54  * 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
57  * colorspace anyway.
58  * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
59  * in the tables to save adding them separately in the inner loop.
60  */
61
62 #define SCALEBITS       16      /* speediest right-shift on some machines */
63 #define CBCR_OFFSET     ((INT32) CENTERJSAMPLE << SCALEBITS)
64 #define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))
65 #define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
66
67 /* We allocate one big table and divide it up into eight parts, instead of
68  * doing eight alloc_small requests.  This lets us use a single table base
69  * address, which can be held in a register in the inner loops on many
70  * machines (more than can hold all eight addresses, anyway).
71  */
72
73 #define R_Y_OFF         0                       /* offset to R => Y section */
74 #define G_Y_OFF         (1*(MAXJSAMPLE+1))      /* offset to G => Y section */
75 #define B_Y_OFF         (2*(MAXJSAMPLE+1))      /* etc. */
76 #define R_CB_OFF        (3*(MAXJSAMPLE+1))
77 #define G_CB_OFF        (4*(MAXJSAMPLE+1))
78 #define B_CB_OFF        (5*(MAXJSAMPLE+1))
79 #define R_CR_OFF        B_CB_OFF                /* B=>Cb, R=>Cr are the same */
80 #define G_CR_OFF        (6*(MAXJSAMPLE+1))
81 #define B_CR_OFF        (7*(MAXJSAMPLE+1))
82 #define TABLE_SIZE      (8*(MAXJSAMPLE+1))
83
84
85 /* Include inline routines for colorspace extensions */
86
87 #include "jccolext.c"
88 #undef RGB_RED
89 #undef RGB_GREEN
90 #undef RGB_BLUE
91 #undef RGB_PIXELSIZE
92
93 #define RGB_RED EXT_RGB_RED
94 #define RGB_GREEN EXT_RGB_GREEN
95 #define RGB_BLUE EXT_RGB_BLUE
96 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
97 #define rgb_ycc_convert_internal extrgb_ycc_convert_internal
98 #define rgb_gray_convert_internal extrgb_gray_convert_internal
99 #define rgb_rgb_convert_internal extrgb_rgb_convert_internal
100 #include "jccolext.c"
101 #undef RGB_RED
102 #undef RGB_GREEN
103 #undef RGB_BLUE
104 #undef RGB_PIXELSIZE
105 #undef rgb_ycc_convert_internal
106 #undef rgb_gray_convert_internal
107 #undef rgb_rgb_convert_internal
108
109 #define RGB_RED EXT_RGBX_RED
110 #define RGB_GREEN EXT_RGBX_GREEN
111 #define RGB_BLUE EXT_RGBX_BLUE
112 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
113 #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal
114 #define rgb_gray_convert_internal extrgbx_gray_convert_internal
115 #define rgb_rgb_convert_internal extrgbx_rgb_convert_internal
116 #include "jccolext.c"
117 #undef RGB_RED
118 #undef RGB_GREEN
119 #undef RGB_BLUE
120 #undef RGB_PIXELSIZE
121 #undef rgb_ycc_convert_internal
122 #undef rgb_gray_convert_internal
123 #undef rgb_rgb_convert_internal
124
125 #define RGB_RED EXT_BGR_RED
126 #define RGB_GREEN EXT_BGR_GREEN
127 #define RGB_BLUE EXT_BGR_BLUE
128 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
129 #define rgb_ycc_convert_internal extbgr_ycc_convert_internal
130 #define rgb_gray_convert_internal extbgr_gray_convert_internal
131 #define rgb_rgb_convert_internal extbgr_rgb_convert_internal
132 #include "jccolext.c"
133 #undef RGB_RED
134 #undef RGB_GREEN
135 #undef RGB_BLUE
136 #undef RGB_PIXELSIZE
137 #undef rgb_ycc_convert_internal
138 #undef rgb_gray_convert_internal
139 #undef rgb_rgb_convert_internal
140
141 #define RGB_RED EXT_BGRX_RED
142 #define RGB_GREEN EXT_BGRX_GREEN
143 #define RGB_BLUE EXT_BGRX_BLUE
144 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
145 #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal
146 #define rgb_gray_convert_internal extbgrx_gray_convert_internal
147 #define rgb_rgb_convert_internal extbgrx_rgb_convert_internal
148 #include "jccolext.c"
149 #undef RGB_RED
150 #undef RGB_GREEN
151 #undef RGB_BLUE
152 #undef RGB_PIXELSIZE
153 #undef rgb_ycc_convert_internal
154 #undef rgb_gray_convert_internal
155 #undef rgb_rgb_convert_internal
156
157 #define RGB_RED EXT_XBGR_RED
158 #define RGB_GREEN EXT_XBGR_GREEN
159 #define RGB_BLUE EXT_XBGR_BLUE
160 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
161 #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal
162 #define rgb_gray_convert_internal extxbgr_gray_convert_internal
163 #define rgb_rgb_convert_internal extxbgr_rgb_convert_internal
164 #include "jccolext.c"
165 #undef RGB_RED
166 #undef RGB_GREEN
167 #undef RGB_BLUE
168 #undef RGB_PIXELSIZE
169 #undef rgb_ycc_convert_internal
170 #undef rgb_gray_convert_internal
171 #undef rgb_rgb_convert_internal
172
173 #define RGB_RED EXT_XRGB_RED
174 #define RGB_GREEN EXT_XRGB_GREEN
175 #define RGB_BLUE EXT_XRGB_BLUE
176 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
177 #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal
178 #define rgb_gray_convert_internal extxrgb_gray_convert_internal
179 #define rgb_rgb_convert_internal extxrgb_rgb_convert_internal
180 #include "jccolext.c"
181 #undef RGB_RED
182 #undef RGB_GREEN
183 #undef RGB_BLUE
184 #undef RGB_PIXELSIZE
185 #undef rgb_ycc_convert_internal
186 #undef rgb_gray_convert_internal
187 #undef rgb_rgb_convert_internal
188
189
190 /*
191  * Initialize for RGB->YCC colorspace conversion.
192  */
193
194 METHODDEF(void)
195 rgb_ycc_start (j_compress_ptr cinfo)
196 {
197   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
198   INT32 * rgb_ycc_tab;
199   INT32 i;
200
201   /* Allocate and fill in the conversion tables. */
202   cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
203     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
204                                 (TABLE_SIZE * SIZEOF(INT32)));
205
206   for (i = 0; i <= MAXJSAMPLE; i++) {
207     rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
208     rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
209     rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
210     rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
211     rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
212     /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
213      * This ensures that the maximum output will round to MAXJSAMPLE
214      * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
215      */
216     rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
217 /*  B=>Cb and R=>Cr tables are the same
218     rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
219 */
220     rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
221     rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
222   }
223 }
224
225
226 /*
227  * Convert some rows of samples to the JPEG colorspace.
228  */
229
230 METHODDEF(void)
231 rgb_ycc_convert (j_compress_ptr cinfo,
232                  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
233                  JDIMENSION output_row, int num_rows)
234 {
235   switch (cinfo->in_color_space) {
236     case JCS_EXT_RGB:
237       extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
238                                   num_rows);
239       break;
240     case JCS_EXT_RGBX:
241     case JCS_EXT_RGBA:
242       extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
243                                    num_rows);
244       break;
245     case JCS_EXT_BGR:
246       extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
247                                   num_rows);
248       break;
249     case JCS_EXT_BGRX:
250     case JCS_EXT_BGRA:
251       extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
252                                    num_rows);
253       break;
254     case JCS_EXT_XBGR:
255     case JCS_EXT_ABGR:
256       extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
257                                    num_rows);
258       break;
259     case JCS_EXT_XRGB:
260     case JCS_EXT_ARGB:
261       extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
262                                    num_rows);
263       break;
264     default:
265       rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
266                                num_rows);
267       break;
268   }
269 }
270
271
272 /**************** Cases other than RGB -> YCbCr **************/
273
274
275 /*
276  * Convert some rows of samples to the JPEG colorspace.
277  */
278
279 METHODDEF(void)
280 rgb_gray_convert (j_compress_ptr cinfo,
281                   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
282                   JDIMENSION output_row, int num_rows)
283 {
284   switch (cinfo->in_color_space) {
285     case JCS_EXT_RGB:
286       extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
287                                    num_rows);
288       break;
289     case JCS_EXT_RGBX:
290     case JCS_EXT_RGBA:
291       extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
292                                     num_rows);
293       break;
294     case JCS_EXT_BGR:
295       extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
296                                    num_rows);
297       break;
298     case JCS_EXT_BGRX:
299     case JCS_EXT_BGRA:
300       extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
301                                     num_rows);
302       break;
303     case JCS_EXT_XBGR:
304     case JCS_EXT_ABGR:
305       extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
306                                     num_rows);
307       break;
308     case JCS_EXT_XRGB:
309     case JCS_EXT_ARGB:
310       extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
311                                     num_rows);
312       break;
313     default:
314       rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
315                                 num_rows);
316       break;
317   }
318 }
319
320
321 /*
322  * Extended RGB to plain RGB conversion
323  */
324
325 METHODDEF(void)
326 rgb_rgb_convert (j_compress_ptr cinfo,
327                   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
328                   JDIMENSION output_row, int num_rows)
329 {
330   switch (cinfo->in_color_space) {
331     case JCS_EXT_RGB:
332       extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
333                                   num_rows);
334       break;
335     case JCS_EXT_RGBX:
336     case JCS_EXT_RGBA:
337       extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
338                                    num_rows);
339       break;
340     case JCS_EXT_BGR:
341       extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
342                                   num_rows);
343       break;
344     case JCS_EXT_BGRX:
345     case JCS_EXT_BGRA:
346       extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
347                                    num_rows);
348       break;
349     case JCS_EXT_XBGR:
350     case JCS_EXT_ABGR:
351       extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
352                                    num_rows);
353       break;
354     case JCS_EXT_XRGB:
355     case JCS_EXT_ARGB:
356       extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
357                                    num_rows);
358       break;
359     default:
360       rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
361                                num_rows);
362       break;
363   }
364 }
365
366
367 /*
368  * Convert some rows of samples to the JPEG colorspace.
369  * This version handles Adobe-style CMYK->YCCK conversion,
370  * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
371  * conversion as above, while passing K (black) unchanged.
372  * We assume rgb_ycc_start has been called.
373  */
374
375 METHODDEF(void)
376 cmyk_ycck_convert (j_compress_ptr cinfo,
377                    JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
378                    JDIMENSION output_row, int num_rows)
379 {
380   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
381   register int r, g, b;
382   register INT32 * ctab = cconvert->rgb_ycc_tab;
383   register JSAMPROW inptr;
384   register JSAMPROW outptr0, outptr1, outptr2, outptr3;
385   register JDIMENSION col;
386   JDIMENSION num_cols = cinfo->image_width;
387
388   while (--num_rows >= 0) {
389     inptr = *input_buf++;
390     outptr0 = output_buf[0][output_row];
391     outptr1 = output_buf[1][output_row];
392     outptr2 = output_buf[2][output_row];
393     outptr3 = output_buf[3][output_row];
394     output_row++;
395     for (col = 0; col < num_cols; col++) {
396       r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
397       g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
398       b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
399       /* K passes through as-is */
400       outptr3[col] = inptr[3];  /* don't need GETJSAMPLE here */
401       inptr += 4;
402       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
403        * must be too; we do not need an explicit range-limiting operation.
404        * Hence the value being shifted is never negative, and we don't
405        * need the general RIGHT_SHIFT macro.
406        */
407       /* Y */
408       outptr0[col] = (JSAMPLE)
409                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
410                  >> SCALEBITS);
411       /* Cb */
412       outptr1[col] = (JSAMPLE)
413                 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
414                  >> SCALEBITS);
415       /* Cr */
416       outptr2[col] = (JSAMPLE)
417                 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
418                  >> SCALEBITS);
419     }
420   }
421 }
422
423
424 /*
425  * Convert some rows of samples to the JPEG colorspace.
426  * This version handles grayscale output with no conversion.
427  * The source can be either plain grayscale or YCbCr (since Y == gray).
428  */
429
430 METHODDEF(void)
431 grayscale_convert (j_compress_ptr cinfo,
432                    JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
433                    JDIMENSION output_row, int num_rows)
434 {
435   register JSAMPROW inptr;
436   register JSAMPROW outptr;
437   register JDIMENSION col;
438   JDIMENSION num_cols = cinfo->image_width;
439   int instride = cinfo->input_components;
440
441   while (--num_rows >= 0) {
442     inptr = *input_buf++;
443     outptr = output_buf[0][output_row];
444     output_row++;
445     for (col = 0; col < num_cols; col++) {
446       outptr[col] = inptr[0];   /* don't need GETJSAMPLE() here */
447       inptr += instride;
448     }
449   }
450 }
451
452
453 /*
454  * Convert some rows of samples to the JPEG colorspace.
455  * This version handles multi-component colorspaces without conversion.
456  * We assume input_components == num_components.
457  */
458
459 METHODDEF(void)
460 null_convert (j_compress_ptr cinfo,
461               JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
462               JDIMENSION output_row, int num_rows)
463 {
464   register JSAMPROW inptr;
465   register JSAMPROW outptr;
466   register JDIMENSION col;
467   register int ci;
468   int nc = cinfo->num_components;
469   JDIMENSION num_cols = cinfo->image_width;
470
471   while (--num_rows >= 0) {
472     /* It seems fastest to make a separate pass for each component. */
473     for (ci = 0; ci < nc; ci++) {
474       inptr = *input_buf;
475       outptr = output_buf[ci][output_row];
476       for (col = 0; col < num_cols; col++) {
477         outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
478         inptr += nc;
479       }
480     }
481     input_buf++;
482     output_row++;
483   }
484 }
485
486
487 /*
488  * Empty method for start_pass.
489  */
490
491 METHODDEF(void)
492 null_method (j_compress_ptr cinfo)
493 {
494   /* no work needed */
495 }
496
497
498 /*
499  * Module initialization routine for input colorspace conversion.
500  */
501
502 GLOBAL(void)
503 jinit_color_converter (j_compress_ptr cinfo)
504 {
505   my_cconvert_ptr cconvert;
506
507   cconvert = (my_cconvert_ptr)
508     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
509                                 SIZEOF(my_color_converter));
510   cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
511   /* set start_pass to null method until we find out differently */
512   cconvert->pub.start_pass = null_method;
513
514   /* Make sure input_components agrees with in_color_space */
515   switch (cinfo->in_color_space) {
516   case JCS_GRAYSCALE:
517     if (cinfo->input_components != 1)
518       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
519     break;
520
521   case JCS_RGB:
522   case JCS_EXT_RGB:
523   case JCS_EXT_RGBX:
524   case JCS_EXT_BGR:
525   case JCS_EXT_BGRX:
526   case JCS_EXT_XBGR:
527   case JCS_EXT_XRGB:
528   case JCS_EXT_RGBA:
529   case JCS_EXT_BGRA:
530   case JCS_EXT_ABGR:
531   case JCS_EXT_ARGB:
532     if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
533       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
534     break;
535
536   case JCS_YCbCr:
537     if (cinfo->input_components != 3)
538       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
539     break;
540
541   case JCS_CMYK:
542   case JCS_YCCK:
543     if (cinfo->input_components != 4)
544       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
545     break;
546
547   default:                      /* JCS_UNKNOWN can be anything */
548     if (cinfo->input_components < 1)
549       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
550     break;
551   }
552
553   /* Check num_components, set conversion method based on requested space */
554   switch (cinfo->jpeg_color_space) {
555   case JCS_GRAYSCALE:
556     if (cinfo->num_components != 1)
557       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
558     if (cinfo->in_color_space == JCS_GRAYSCALE)
559       cconvert->pub.color_convert = grayscale_convert;
560     else if (cinfo->in_color_space == JCS_RGB ||
561              cinfo->in_color_space == JCS_EXT_RGB ||
562              cinfo->in_color_space == JCS_EXT_RGBX ||
563              cinfo->in_color_space == JCS_EXT_BGR ||
564              cinfo->in_color_space == JCS_EXT_BGRX ||
565              cinfo->in_color_space == JCS_EXT_XBGR ||
566              cinfo->in_color_space == JCS_EXT_XRGB ||
567              cinfo->in_color_space == JCS_EXT_RGBA ||
568              cinfo->in_color_space == JCS_EXT_BGRA ||
569              cinfo->in_color_space == JCS_EXT_ABGR ||
570              cinfo->in_color_space == JCS_EXT_ARGB) {
571       if (jsimd_can_rgb_gray())
572         cconvert->pub.color_convert = jsimd_rgb_gray_convert;
573       else {
574         cconvert->pub.start_pass = rgb_ycc_start;
575         cconvert->pub.color_convert = rgb_gray_convert;
576       }
577     } else if (cinfo->in_color_space == JCS_YCbCr)
578       cconvert->pub.color_convert = grayscale_convert;
579     else
580       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
581     break;
582
583   case JCS_RGB:
584     if (cinfo->num_components != 3)
585       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
586     if (rgb_red[cinfo->in_color_space] == 0 &&
587         rgb_green[cinfo->in_color_space] == 1 &&
588         rgb_blue[cinfo->in_color_space] == 2 &&
589         rgb_pixelsize[cinfo->in_color_space] == 3)
590       cconvert->pub.color_convert = null_convert;
591     else if (cinfo->in_color_space == JCS_RGB ||
592              cinfo->in_color_space == JCS_EXT_RGB ||
593              cinfo->in_color_space == JCS_EXT_RGBX ||
594              cinfo->in_color_space == JCS_EXT_BGR ||
595              cinfo->in_color_space == JCS_EXT_BGRX ||
596              cinfo->in_color_space == JCS_EXT_XBGR ||
597              cinfo->in_color_space == JCS_EXT_XRGB ||
598              cinfo->in_color_space == JCS_EXT_RGBA ||
599              cinfo->in_color_space == JCS_EXT_BGRA ||
600              cinfo->in_color_space == JCS_EXT_ABGR ||
601              cinfo->in_color_space == JCS_EXT_ARGB)
602       cconvert->pub.color_convert = rgb_rgb_convert;
603     else
604       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
605     break;
606
607   case JCS_YCbCr:
608     if (cinfo->num_components != 3)
609       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
610     if (cinfo->in_color_space == JCS_RGB ||
611         cinfo->in_color_space == JCS_EXT_RGB ||
612         cinfo->in_color_space == JCS_EXT_RGBX ||
613         cinfo->in_color_space == JCS_EXT_BGR ||
614         cinfo->in_color_space == JCS_EXT_BGRX ||
615         cinfo->in_color_space == JCS_EXT_XBGR ||
616         cinfo->in_color_space == JCS_EXT_XRGB ||
617         cinfo->in_color_space == JCS_EXT_RGBA ||
618         cinfo->in_color_space == JCS_EXT_BGRA ||
619         cinfo->in_color_space == JCS_EXT_ABGR ||
620         cinfo->in_color_space == JCS_EXT_ARGB) {
621       if (jsimd_can_rgb_ycc())
622         cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
623       else {
624         cconvert->pub.start_pass = rgb_ycc_start;
625         cconvert->pub.color_convert = rgb_ycc_convert;
626       }
627     } else if (cinfo->in_color_space == JCS_YCbCr)
628       cconvert->pub.color_convert = null_convert;
629     else
630       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
631     break;
632
633   case JCS_CMYK:
634     if (cinfo->num_components != 4)
635       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
636     if (cinfo->in_color_space == JCS_CMYK)
637       cconvert->pub.color_convert = null_convert;
638     else
639       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
640     break;
641
642   case JCS_YCCK:
643     if (cinfo->num_components != 4)
644       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
645     if (cinfo->in_color_space == JCS_CMYK) {
646       cconvert->pub.start_pass = rgb_ycc_start;
647       cconvert->pub.color_convert = cmyk_ycck_convert;
648     } else if (cinfo->in_color_space == JCS_YCCK)
649       cconvert->pub.color_convert = null_convert;
650     else
651       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
652     break;
653
654   default:                      /* allow null conversion of JCS_UNKNOWN */
655     if (cinfo->jpeg_color_space != cinfo->in_color_space ||
656         cinfo->num_components != cinfo->input_components)
657       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
658     cconvert->pub.color_convert = null_convert;
659     break;
660   }
661 }