1/*
2 * jccolor.c
3 *
4 * Copyright (C) 1991-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains input colorspace conversion routines.
9 */
10
11#define JPEG_INTERNALS
12#include "jinclude.h"
13#include "jpeglib.h"
14
15// this enables unrolling null_convert's loop, and reading/write ints for speed
16#define ENABLE_ANDROID_NULL_CONVERT
17
18/* 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;
28
29
30/**************** 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
37 *	Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
38 *	Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
39 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
40 * 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.
45 *
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.
56 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
57 * in the tables to save adding them separately in the inner loop.
58 */
59
60#define SCALEBITS	16	/* speediest right-shift on some machines */
61#define CBCR_OFFSET	((INT32) CENTERJSAMPLE << SCALEBITS)
62#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
71#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
83/*
84 * Initialize for RGB->YCC colorspace conversion.
85 */
86
87METHODDEF(void)
88rgb_ycc_start (j_compress_ptr cinfo)
89{
90  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
91  INT32 * rgb_ycc_tab;
92  INT32 i;
93
94  /* Allocate and fill in the conversion tables. */
95  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
96    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
97				(TABLE_SIZE * SIZEOF(INT32)));
98
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;
105    /* 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;
110/*  B=>Cb and R=>Cr tables are the same
111    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
112*/
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  }
116}
117
118
119/*
120 * 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.
129 */
130
131METHODDEF(void)
132rgb_ycc_convert (j_compress_ptr cinfo,
133		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
134		 JDIMENSION output_row, int num_rows)
135{
136  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
137  register int r, g, b;
138  register INT32 * ctab = cconvert->rgb_ycc_tab;
139  register JSAMPROW inptr;
140  register JSAMPROW outptr0, outptr1, outptr2;
141  register JDIMENSION col;
142  JDIMENSION num_cols = cinfo->image_width;
143
144  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;
155      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
156       * 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.
159       */
160      /* Y */
161      outptr0[col] = (JSAMPLE)
162		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
163		 >> SCALEBITS);
164      /* Cb */
165      outptr1[col] = (JSAMPLE)
166		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
167		 >> SCALEBITS);
168      /* Cr */
169      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#ifdef ANDROID_RGB
177/* Converts RGB565 row into YCbCr */
178METHODDEF(void)
179rgb565_ycc_convert (j_compress_ptr cinfo,
180		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
181		 JDIMENSION output_row, int num_rows)
182{
183  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
184  register int r, g, b;
185  register INT32 * ctab = cconvert->rgb_ycc_tab;
186  register unsigned short* inptr;
187  register JSAMPROW outptr0, outptr1, outptr2;
188  register JDIMENSION col;
189  JDIMENSION num_cols = cinfo->image_width;
190
191  while (--num_rows >= 0) {
192    inptr = (unsigned short*)(*input_buf++);
193    outptr0 = output_buf[0][output_row];
194    outptr1 = output_buf[1][output_row];
195    outptr2 = output_buf[2][output_row];
196    output_row++;
197    for (col = 0; col < num_cols; col++) {
198      register const unsigned short color = inptr[col];
199      r = ((color & 0xf800) >> 8) | ((color & 0xf800) >> 14);
200      g = ((color & 0x7e0) >> 3) | ((color & 0x7e0) >> 9);
201      b = ((color & 0x1f) << 3) | ((color & 0x1f) >> 2);
202      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
203       * must be too; we do not need an explicit range-limiting operation.
204       * Hence the value being shifted is never negative, and we don't
205       * need the general RIGHT_SHIFT macro.
206       */
207      /* Y */
208      outptr0[col] = (JSAMPLE)
209		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
210		 >> SCALEBITS);
211      /* Cb */
212      outptr1[col] = (JSAMPLE)
213		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
214		 >> SCALEBITS);
215      /* Cr */
216      outptr2[col] = (JSAMPLE)
217		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
218		 >> SCALEBITS);
219    }
220  }
221}
222
223/* Converts RGBA8888 row into YCbCr */
224METHODDEF(void)
225rgba8888_ycc_convert (j_compress_ptr cinfo,
226		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
227		 JDIMENSION output_row, int num_rows)
228{
229  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
230  register int r, g, b;
231  register INT32 * ctab = cconvert->rgb_ycc_tab;
232  register INT32* inptr;
233  register JSAMPROW outptr0, outptr1, outptr2;
234  register JDIMENSION col;
235  JDIMENSION num_cols = cinfo->image_width;
236
237  while (--num_rows >= 0) {
238    inptr = (INT32*)(*input_buf++);
239    outptr0 = output_buf[0][output_row];
240    outptr1 = output_buf[1][output_row];
241    outptr2 = output_buf[2][output_row];
242    output_row++;
243    for (col = 0; col < num_cols; col++) {
244      register const unsigned char* color = (unsigned char*)(inptr + col);
245      r = (*color) & 0xff; color++;
246      g = (*color) & 0xff; color++;
247      b = (*color) & 0xff; color++;
248      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
249       * must be too; we do not need an explicit range-limiting operation.
250       * Hence the value being shifted is never negative, and we don't
251       * need the general RIGHT_SHIFT macro.
252       */
253      /* Y */
254      outptr0[col] = (JSAMPLE)
255		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
256		 >> SCALEBITS);
257      /* Cb */
258      outptr1[col] = (JSAMPLE)
259		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
260		 >> SCALEBITS);
261      /* Cr */
262      outptr2[col] = (JSAMPLE)
263		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
264		 >> SCALEBITS);
265    }
266  }
267}
268#endif  /* ANDROID_RGB */
269
270/**************** Cases other than RGB -> YCbCr **************/
271
272
273/*
274 * Convert some rows of samples to the JPEG colorspace.
275 * This version handles RGB->grayscale conversion, which is the same
276 * as the RGB->Y portion of RGB->YCbCr.
277 * We assume rgb_ycc_start has been called (we only use the Y tables).
278 */
279
280METHODDEF(void)
281rgb_gray_convert (j_compress_ptr cinfo,
282		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
283		  JDIMENSION output_row, int num_rows)
284{
285  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
286  register int r, g, b;
287  register INT32 * ctab = cconvert->rgb_ycc_tab;
288  register JSAMPROW inptr;
289  register JSAMPROW outptr;
290  register JDIMENSION col;
291  JDIMENSION num_cols = cinfo->image_width;
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      r = GETJSAMPLE(inptr[RGB_RED]);
299      g = GETJSAMPLE(inptr[RGB_GREEN]);
300      b = GETJSAMPLE(inptr[RGB_BLUE]);
301      inptr += RGB_PIXELSIZE;
302      /* Y */
303      outptr[col] = (JSAMPLE)
304		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
305		 >> SCALEBITS);
306    }
307  }
308}
309
310
311/*
312 * Convert some rows of samples to the JPEG colorspace.
313 * This version handles Adobe-style CMYK->YCCK conversion,
314 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
315 * conversion as above, while passing K (black) unchanged.
316 * We assume rgb_ycc_start has been called.
317 */
318
319METHODDEF(void)
320cmyk_ycck_convert (j_compress_ptr cinfo,
321		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
322		   JDIMENSION output_row, int num_rows)
323{
324  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
325  register int r, g, b;
326  register INT32 * ctab = cconvert->rgb_ycc_tab;
327  register JSAMPROW inptr;
328  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
329  register JDIMENSION col;
330  JDIMENSION num_cols = cinfo->image_width;
331
332  while (--num_rows >= 0) {
333    inptr = *input_buf++;
334    outptr0 = output_buf[0][output_row];
335    outptr1 = output_buf[1][output_row];
336    outptr2 = output_buf[2][output_row];
337    outptr3 = output_buf[3][output_row];
338    output_row++;
339    for (col = 0; col < num_cols; col++) {
340      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
341      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
342      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
343      /* K passes through as-is */
344      outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
345      inptr += 4;
346      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
347       * must be too; we do not need an explicit range-limiting operation.
348       * Hence the value being shifted is never negative, and we don't
349       * need the general RIGHT_SHIFT macro.
350       */
351      /* Y */
352      outptr0[col] = (JSAMPLE)
353		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
354		 >> SCALEBITS);
355      /* Cb */
356      outptr1[col] = (JSAMPLE)
357		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
358		 >> SCALEBITS);
359      /* Cr */
360      outptr2[col] = (JSAMPLE)
361		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
362		 >> SCALEBITS);
363    }
364  }
365}
366
367
368/*
369 * Convert some rows of samples to the JPEG colorspace.
370 * This version handles grayscale output with no conversion.
371 * The source can be either plain grayscale or YCbCr (since Y == gray).
372 */
373
374METHODDEF(void)
375grayscale_convert (j_compress_ptr cinfo,
376		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
377		   JDIMENSION output_row, int num_rows)
378{
379  register JSAMPROW inptr;
380  register JSAMPROW outptr;
381  register JDIMENSION col;
382  JDIMENSION num_cols = cinfo->image_width;
383  int instride = cinfo->input_components;
384
385  while (--num_rows >= 0) {
386    inptr = *input_buf++;
387    outptr = output_buf[0][output_row];
388    output_row++;
389    for (col = 0; col < num_cols; col++) {
390      outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
391      inptr += instride;
392    }
393  }
394}
395
396#ifdef ENABLE_ANDROID_NULL_CONVERT
397
398typedef unsigned long UINT32;
399
400#define B0(n)   ((n) & 0xFF)
401#define B1(n)   (((n) >> 8) & 0xFF)
402#define B2(n)   (((n) >> 16) & 0xFF)
403#define B3(n)   ((n) >> 24)
404
405#define PACK(a, b, c, d)    ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
406
407static int ptr_is_quad(const void* p)
408{
409    return (((const char*)p - (const char*)0) & 3) == 0;
410}
411
412static void copyquads(const UINT32 in[], UINT32 out0[], UINT32 out1[], UINT32 out2[], int col4)
413{
414    do {
415        UINT32 src0 = *in++;
416        UINT32 src1 = *in++;
417        UINT32 src2 = *in++;
418        // LEndian
419        *out0++ = PACK(B0(src0), B3(src0), B2(src1), B1(src2));
420        *out1++ = PACK(B1(src0), B0(src1), B3(src1), B2(src2));
421        *out2++ = PACK(B2(src0), B1(src1), B0(src2), B3(src2));
422    } while (--col4 != 0);
423}
424
425#endif
426
427/*
428 * Convert some rows of samples to the JPEG colorspace.
429 * This version handles multi-component colorspaces without conversion.
430 * We assume input_components == num_components.
431 */
432
433METHODDEF(void)
434null_convert (j_compress_ptr cinfo,
435	      JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
436	      JDIMENSION output_row, int num_rows)
437{
438  register JSAMPROW inptr;
439  register JSAMPROW outptr;
440  register JDIMENSION col;
441  register int ci;
442  int nc = cinfo->num_components;
443  JDIMENSION num_cols = cinfo->image_width;
444
445#ifdef ENABLE_ANDROID_NULL_CONVERT
446    if (1 == num_rows && 3 == nc && num_cols > 0) {
447        JSAMPROW inptr = *input_buf;
448        JSAMPROW outptr0 = output_buf[0][output_row];
449        JSAMPROW outptr1 = output_buf[1][output_row];
450        JSAMPROW outptr2 = output_buf[2][output_row];
451
452        int col = num_cols;
453        int col4 = col >> 2;
454        if (col4 > 0 && ptr_is_quad(inptr) && ptr_is_quad(outptr0) &&
455                        ptr_is_quad(outptr1) && ptr_is_quad(outptr2)) {
456
457            const UINT32* in = (const UINT32*)inptr;
458            UINT32* out0 = (UINT32*)outptr0;
459            UINT32* out1 = (UINT32*)outptr1;
460            UINT32* out2 = (UINT32*)outptr2;
461            copyquads(in, out0, out1, out2, col4);
462            col &= 3;
463            if (0 == col)
464                return;
465            col4 <<= 2;
466            inptr += col4 * 3;  /* we read this 3 times per in copyquads */
467            outptr0 += col4;
468            outptr1 += col4;
469            outptr2 += col4;
470            /* fall through to while-loop */
471        }
472        do {
473            *outptr0++ = *inptr++;
474            *outptr1++ = *inptr++;
475            *outptr2++ = *inptr++;
476        } while (--col != 0);
477        return;
478    }
479SLOW:
480#endif
481  while (--num_rows >= 0) {
482    /* It seems fastest to make a separate pass for each component. */
483    for (ci = 0; ci < nc; ci++) {
484      inptr = *input_buf;
485      outptr = output_buf[ci][output_row];
486      for (col = 0; col < num_cols; col++) {
487	outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
488	inptr += nc;
489      }
490    }
491    input_buf++;
492    output_row++;
493  }
494}
495
496
497/*
498 * Empty method for start_pass.
499 */
500
501METHODDEF(void)
502null_method (j_compress_ptr cinfo)
503{
504  /* no work needed */
505}
506
507
508/*
509 * Module initialization routine for input colorspace conversion.
510 */
511
512GLOBAL(void)
513jinit_color_converter (j_compress_ptr cinfo)
514{
515  my_cconvert_ptr cconvert;
516
517  cconvert = (my_cconvert_ptr)
518    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
519				SIZEOF(my_color_converter));
520  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
521  /* set start_pass to null method until we find out differently */
522  cconvert->pub.start_pass = null_method;
523
524  /* Make sure input_components agrees with in_color_space */
525  switch (cinfo->in_color_space) {
526  case JCS_GRAYSCALE:
527    if (cinfo->input_components != 1)
528      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
529    break;
530
531  case JCS_RGB:
532#if RGB_PIXELSIZE != 3
533    if (cinfo->input_components != RGB_PIXELSIZE)
534      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
535    break;
536#endif /* else share code with YCbCr */
537
538  case JCS_YCbCr:
539    if (cinfo->input_components != 3)
540      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
541    break;
542
543  case JCS_CMYK:
544  case JCS_YCCK:
545    if (cinfo->input_components != 4)
546      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
547    break;
548
549#ifdef ANDROID_RGB
550  case JCS_RGB_565:
551    if (cinfo->input_components != 2)
552      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
553    break;
554  case JCS_RGBA_8888:
555    if (cinfo->input_components != 4)
556      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
557    break;
558#endif  /* ANDROID_RGB */
559
560  default:			/* JCS_UNKNOWN can be anything */
561    if (cinfo->input_components < 1)
562      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
563    break;
564  }
565
566  /* Check num_components, set conversion method based on requested space */
567  switch (cinfo->jpeg_color_space) {
568  case JCS_GRAYSCALE:
569    if (cinfo->num_components != 1)
570      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
571    if (cinfo->in_color_space == JCS_GRAYSCALE)
572      cconvert->pub.color_convert = grayscale_convert;
573    else if (cinfo->in_color_space == JCS_RGB) {
574      cconvert->pub.start_pass = rgb_ycc_start;
575      cconvert->pub.color_convert = rgb_gray_convert;
576    } else if (cinfo->in_color_space == JCS_YCbCr)
577      cconvert->pub.color_convert = grayscale_convert;
578    else
579      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
580    break;
581
582  case JCS_RGB:
583    if (cinfo->num_components != 3)
584      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
585    if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
586      cconvert->pub.color_convert = null_convert;
587    else
588      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
589    break;
590
591  case JCS_YCbCr:
592    if (cinfo->num_components != 3)
593      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
594    if (cinfo->in_color_space == JCS_RGB) {
595      cconvert->pub.start_pass = rgb_ycc_start;
596      cconvert->pub.color_convert = rgb_ycc_convert;
597    } else if (cinfo->in_color_space == JCS_YCbCr) {
598      cconvert->pub.color_convert = null_convert;
599    }
600#ifdef ANDROID_RGB
601    else if (cinfo->in_color_space == JCS_RGB_565) {
602      cconvert->pub.start_pass = rgb_ycc_start;
603      cconvert->pub.color_convert = rgb565_ycc_convert;
604    } else if (cinfo->in_color_space == JCS_RGBA_8888) {
605      cconvert->pub.start_pass = rgb_ycc_start;
606      cconvert->pub.color_convert = rgba8888_ycc_convert;
607    }
608#endif  /* ANDROID_RGB */
609    else
610      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
611    break;
612
613  case JCS_CMYK:
614    if (cinfo->num_components != 4)
615      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
616    if (cinfo->in_color_space == JCS_CMYK)
617      cconvert->pub.color_convert = null_convert;
618    else
619      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
620    break;
621
622  case JCS_YCCK:
623    if (cinfo->num_components != 4)
624      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
625    if (cinfo->in_color_space == JCS_CMYK) {
626      cconvert->pub.start_pass = rgb_ycc_start;
627      cconvert->pub.color_convert = cmyk_ycck_convert;
628    } else if (cinfo->in_color_space == JCS_YCCK)
629      cconvert->pub.color_convert = null_convert;
630    else
631      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
632    break;
633
634  default:			/* allow null conversion of JCS_UNKNOWN */
635    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
636	cinfo->num_components != cinfo->input_components)
637      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
638    cconvert->pub.color_convert = null_convert;
639    break;
640  }
641}
642