jccolor.c revision 4a795dda3d916c591206d2c37d6b583098204108
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#endif  /* ANDROID_RGB */
223
224/**************** Cases other than RGB -> YCbCr **************/
225
226
227/*
228 * Convert some rows of samples to the JPEG colorspace.
229 * This version handles RGB->grayscale conversion, which is the same
230 * as the RGB->Y portion of RGB->YCbCr.
231 * We assume rgb_ycc_start has been called (we only use the Y tables).
232 */
233
234METHODDEF(void)
235rgb_gray_convert (j_compress_ptr cinfo,
236		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
237		  JDIMENSION output_row, int num_rows)
238{
239  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
240  register int r, g, b;
241  register INT32 * ctab = cconvert->rgb_ycc_tab;
242  register JSAMPROW inptr;
243  register JSAMPROW outptr;
244  register JDIMENSION col;
245  JDIMENSION num_cols = cinfo->image_width;
246
247  while (--num_rows >= 0) {
248    inptr = *input_buf++;
249    outptr = output_buf[0][output_row];
250    output_row++;
251    for (col = 0; col < num_cols; col++) {
252      r = GETJSAMPLE(inptr[RGB_RED]);
253      g = GETJSAMPLE(inptr[RGB_GREEN]);
254      b = GETJSAMPLE(inptr[RGB_BLUE]);
255      inptr += RGB_PIXELSIZE;
256      /* Y */
257      outptr[col] = (JSAMPLE)
258		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
259		 >> SCALEBITS);
260    }
261  }
262}
263
264
265/*
266 * Convert some rows of samples to the JPEG colorspace.
267 * This version handles Adobe-style CMYK->YCCK conversion,
268 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
269 * conversion as above, while passing K (black) unchanged.
270 * We assume rgb_ycc_start has been called.
271 */
272
273METHODDEF(void)
274cmyk_ycck_convert (j_compress_ptr cinfo,
275		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
276		   JDIMENSION output_row, int num_rows)
277{
278  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
279  register int r, g, b;
280  register INT32 * ctab = cconvert->rgb_ycc_tab;
281  register JSAMPROW inptr;
282  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
283  register JDIMENSION col;
284  JDIMENSION num_cols = cinfo->image_width;
285
286  while (--num_rows >= 0) {
287    inptr = *input_buf++;
288    outptr0 = output_buf[0][output_row];
289    outptr1 = output_buf[1][output_row];
290    outptr2 = output_buf[2][output_row];
291    outptr3 = output_buf[3][output_row];
292    output_row++;
293    for (col = 0; col < num_cols; col++) {
294      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
295      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
296      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
297      /* K passes through as-is */
298      outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
299      inptr += 4;
300      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
301       * must be too; we do not need an explicit range-limiting operation.
302       * Hence the value being shifted is never negative, and we don't
303       * need the general RIGHT_SHIFT macro.
304       */
305      /* Y */
306      outptr0[col] = (JSAMPLE)
307		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
308		 >> SCALEBITS);
309      /* Cb */
310      outptr1[col] = (JSAMPLE)
311		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
312		 >> SCALEBITS);
313      /* Cr */
314      outptr2[col] = (JSAMPLE)
315		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
316		 >> SCALEBITS);
317    }
318  }
319}
320
321
322/*
323 * Convert some rows of samples to the JPEG colorspace.
324 * This version handles grayscale output with no conversion.
325 * The source can be either plain grayscale or YCbCr (since Y == gray).
326 */
327
328METHODDEF(void)
329grayscale_convert (j_compress_ptr cinfo,
330		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
331		   JDIMENSION output_row, int num_rows)
332{
333  register JSAMPROW inptr;
334  register JSAMPROW outptr;
335  register JDIMENSION col;
336  JDIMENSION num_cols = cinfo->image_width;
337  int instride = cinfo->input_components;
338
339  while (--num_rows >= 0) {
340    inptr = *input_buf++;
341    outptr = output_buf[0][output_row];
342    output_row++;
343    for (col = 0; col < num_cols; col++) {
344      outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
345      inptr += instride;
346    }
347  }
348}
349
350#ifdef ENABLE_ANDROID_NULL_CONVERT
351
352typedef unsigned long UINT32;
353
354#define B0(n)   ((n) & 0xFF)
355#define B1(n)   (((n) >> 8) & 0xFF)
356#define B2(n)   (((n) >> 16) & 0xFF)
357#define B3(n)   ((n) >> 24)
358
359#define PACK(a, b, c, d)    ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
360
361static int ptr_is_quad(const void* p)
362{
363    return (((const char*)p - (const char*)0) & 3) == 0;
364}
365
366static void copyquads(const UINT32 in[], UINT32 out0[], UINT32 out1[], UINT32 out2[], int col4)
367{
368    do {
369        UINT32 src0 = *in++;
370        UINT32 src1 = *in++;
371        UINT32 src2 = *in++;
372        // LEndian
373        *out0++ = PACK(B0(src0), B3(src0), B2(src1), B1(src2));
374        *out1++ = PACK(B1(src0), B0(src1), B3(src1), B2(src2));
375        *out2++ = PACK(B2(src0), B1(src1), B0(src2), B3(src2));
376    } while (--col4 != 0);
377}
378
379#endif
380
381/*
382 * Convert some rows of samples to the JPEG colorspace.
383 * This version handles multi-component colorspaces without conversion.
384 * We assume input_components == num_components.
385 */
386
387METHODDEF(void)
388null_convert (j_compress_ptr cinfo,
389	      JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
390	      JDIMENSION output_row, int num_rows)
391{
392  register JSAMPROW inptr;
393  register JSAMPROW outptr;
394  register JDIMENSION col;
395  register int ci;
396  int nc = cinfo->num_components;
397  JDIMENSION num_cols = cinfo->image_width;
398
399#ifdef ENABLE_ANDROID_NULL_CONVERT
400    if (1 == num_rows && 3 == nc && num_cols > 0) {
401        JSAMPROW inptr = *input_buf;
402        JSAMPROW outptr0 = output_buf[0][output_row];
403        JSAMPROW outptr1 = output_buf[1][output_row];
404        JSAMPROW outptr2 = output_buf[2][output_row];
405
406        int col = num_cols;
407        int col4 = col >> 2;
408        if (col4 > 0 && ptr_is_quad(inptr) && ptr_is_quad(outptr0) &&
409                        ptr_is_quad(outptr1) && ptr_is_quad(outptr2)) {
410
411            const UINT32* in = (const UINT32*)inptr;
412            UINT32* out0 = (UINT32*)outptr0;
413            UINT32* out1 = (UINT32*)outptr1;
414            UINT32* out2 = (UINT32*)outptr2;
415            copyquads(in, out0, out1, out2, col4);
416            col &= 3;
417            if (0 == col)
418                return;
419            col4 <<= 2;
420            inptr += col4 * 3;  /* we read this 3 times per in copyquads */
421            outptr0 += col4;
422            outptr1 += col4;
423            outptr2 += col4;
424            /* fall through to while-loop */
425        }
426        do {
427            *outptr0++ = *inptr++;
428            *outptr1++ = *inptr++;
429            *outptr2++ = *inptr++;
430        } while (--col != 0);
431        return;
432    }
433SLOW:
434#endif
435  while (--num_rows >= 0) {
436    /* It seems fastest to make a separate pass for each component. */
437    for (ci = 0; ci < nc; ci++) {
438      inptr = *input_buf;
439      outptr = output_buf[ci][output_row];
440      for (col = 0; col < num_cols; col++) {
441	outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
442	inptr += nc;
443      }
444    }
445    input_buf++;
446    output_row++;
447  }
448}
449
450
451/*
452 * Empty method for start_pass.
453 */
454
455METHODDEF(void)
456null_method (j_compress_ptr cinfo)
457{
458  /* no work needed */
459}
460
461
462/*
463 * Module initialization routine for input colorspace conversion.
464 */
465
466GLOBAL(void)
467jinit_color_converter (j_compress_ptr cinfo)
468{
469  my_cconvert_ptr cconvert;
470
471  cconvert = (my_cconvert_ptr)
472    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
473				SIZEOF(my_color_converter));
474  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
475  /* set start_pass to null method until we find out differently */
476  cconvert->pub.start_pass = null_method;
477
478  /* Make sure input_components agrees with in_color_space */
479  switch (cinfo->in_color_space) {
480  case JCS_GRAYSCALE:
481    if (cinfo->input_components != 1)
482      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
483    break;
484
485  case JCS_RGB:
486#if RGB_PIXELSIZE != 3
487    if (cinfo->input_components != RGB_PIXELSIZE)
488      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
489    break;
490#endif /* else share code with YCbCr */
491
492  case JCS_YCbCr:
493    if (cinfo->input_components != 3)
494      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
495    break;
496
497  case JCS_CMYK:
498  case JCS_YCCK:
499    if (cinfo->input_components != 4)
500      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
501    break;
502
503#ifdef ANDROID_RGB
504  case JCS_RGB_565:
505    if (cinfo->input_components != 2)
506      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
507    break;
508#endif  /* ANDROID_RGB */
509
510  default:			/* JCS_UNKNOWN can be anything */
511    if (cinfo->input_components < 1)
512      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
513    break;
514  }
515
516  /* Check num_components, set conversion method based on requested space */
517  switch (cinfo->jpeg_color_space) {
518  case JCS_GRAYSCALE:
519    if (cinfo->num_components != 1)
520      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
521    if (cinfo->in_color_space == JCS_GRAYSCALE)
522      cconvert->pub.color_convert = grayscale_convert;
523    else if (cinfo->in_color_space == JCS_RGB) {
524      cconvert->pub.start_pass = rgb_ycc_start;
525      cconvert->pub.color_convert = rgb_gray_convert;
526    } else if (cinfo->in_color_space == JCS_YCbCr)
527      cconvert->pub.color_convert = grayscale_convert;
528    else
529      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
530    break;
531
532  case JCS_RGB:
533    if (cinfo->num_components != 3)
534      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
535    if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
536      cconvert->pub.color_convert = null_convert;
537    else
538      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
539    break;
540
541  case JCS_YCbCr:
542    if (cinfo->num_components != 3)
543      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
544    if (cinfo->in_color_space == JCS_RGB) {
545      cconvert->pub.start_pass = rgb_ycc_start;
546      cconvert->pub.color_convert = rgb_ycc_convert;
547    } else if (cinfo->in_color_space == JCS_YCbCr) {
548      cconvert->pub.color_convert = null_convert;
549    }
550#ifdef ANDROID_RGB
551    else if (cinfo->in_color_space == JCS_RGB_565) {
552      cconvert->pub.start_pass = rgb_ycc_start;
553      cconvert->pub.color_convert = rgb565_ycc_convert;
554    }
555#endif  /* ANDROID_RGB */
556    else
557      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
558    break;
559
560  case JCS_CMYK:
561    if (cinfo->num_components != 4)
562      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
563    if (cinfo->in_color_space == JCS_CMYK)
564      cconvert->pub.color_convert = null_convert;
565    else
566      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
567    break;
568
569  case JCS_YCCK:
570    if (cinfo->num_components != 4)
571      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
572    if (cinfo->in_color_space == JCS_CMYK) {
573      cconvert->pub.start_pass = rgb_ycc_start;
574      cconvert->pub.color_convert = cmyk_ycck_convert;
575    } else if (cinfo->in_color_space == JCS_YCCK)
576      cconvert->pub.color_convert = null_convert;
577    else
578      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
579    break;
580
581  default:			/* allow null conversion of JCS_UNKNOWN */
582    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
583	cinfo->num_components != cinfo->input_components)
584      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
585    cconvert->pub.color_convert = null_convert;
586    break;
587  }
588}
589