deps/jpeg-6b -> jpeg-9a
[sdk] / deps / jpeg-9a / jdcolor.c
1 /*
2  * jdcolor.c
3  *
4  * Copyright (C) 1991-1997, Thomas G. Lane.
5  * Modified 2011-2013 by Guido Vollbeding.
6  * 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 output colorspace conversion routines.
10  */
11
12 #define JPEG_INTERNALS
13 #include "jinclude.h"
14 #include "jpeglib.h"
15
16
17 /* Private subobject */
18
19 typedef struct {
20   struct jpeg_color_deconverter pub; /* public fields */
21
22   /* Private state for YCbCr->RGB and BG_YCC->RGB conversion */
23   int * Cr_r_tab;               /* => table for Cr to R conversion */
24   int * Cb_b_tab;               /* => table for Cb to B conversion */
25   INT32 * Cr_g_tab;             /* => table for Cr to G conversion */
26   INT32 * Cb_g_tab;             /* => table for Cb to G conversion */
27
28   JSAMPLE * range_limit; /* pointer to normal sample range limit table, */
29                      /* or extended sample range limit table for BG_YCC */
30
31   /* Private state for RGB->Y conversion */
32   INT32 * rgb_y_tab;            /* => table for RGB to Y conversion */
33 } my_color_deconverter;
34
35 typedef my_color_deconverter * my_cconvert_ptr;
36
37
38 /***************  YCbCr -> RGB conversion: most common case **************/
39 /*************** BG_YCC -> RGB conversion: less common case **************/
40 /***************    RGB -> Y   conversion: less common case **************/
41
42 /*
43  * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
44  * previously known as Recommendation CCIR 601-1, except that Cb and Cr
45  * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
46  * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
47  * sYCC (standard luma-chroma-chroma color space with extended gamut)
48  * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
49  * bg-sRGB and bg-sYCC (big gamut standard color spaces)
50  * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
51  * Note that the derived conversion coefficients given in some of these
52  * documents are imprecise.  The general conversion equations are
53  *
54  *      R = Y + K * (1 - Kr) * Cr
55  *      G = Y - K * (Kb * (1 - Kb) * Cb + Kr * (1 - Kr) * Cr) / (1 - Kr - Kb)
56  *      B = Y + K * (1 - Kb) * Cb
57  *
58  *      Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
59  *
60  * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
61  * from the 1953 FCC NTSC primaries and CIE Illuminant C), K = 2 for sYCC,
62  * the conversion equations to be implemented are therefore
63  *
64  *      R = Y + 1.402 * Cr
65  *      G = Y - 0.344136286 * Cb - 0.714136286 * Cr
66  *      B = Y + 1.772 * Cb
67  *
68  *      Y = 0.299 * R + 0.587 * G + 0.114 * B
69  *
70  * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
71  * For bg-sYCC, with K = 4, the equations are
72  *
73  *      R = Y + 2.804 * Cr
74  *      G = Y - 0.688272572 * Cb - 1.428272572 * Cr
75  *      B = Y + 3.544 * Cb
76  *
77  * To avoid floating-point arithmetic, we represent the fractional constants
78  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
79  * the products by 2^16, with appropriate rounding, to get the correct answer.
80  * Notice that Y, being an integral input, does not contribute any fraction
81  * so it need not participate in the rounding.
82  *
83  * For even more speed, we avoid doing any multiplications in the inner loop
84  * by precalculating the constants times Cb and Cr for all possible values.
85  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
86  * for 9-bit to 12-bit samples it is still acceptable.  It's not very
87  * reasonable for 16-bit samples, but if you want lossless storage you
88  * shouldn't be changing colorspace anyway.
89  * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
90  * values for the G calculation are left scaled up, since we must add them
91  * together before rounding.
92  */
93
94 #define SCALEBITS       16      /* speediest right-shift on some machines */
95 #define ONE_HALF        ((INT32) 1 << (SCALEBITS-1))
96 #define FIX(x)          ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
97
98 /* We allocate one big table for RGB->Y conversion and divide it up into
99  * three parts, instead of doing three alloc_small requests.  This lets us
100  * use a single table base address, which can be held in a register in the
101  * inner loops on many machines (more than can hold all three addresses,
102  * anyway).
103  */
104
105 #define R_Y_OFF         0                       /* offset to R => Y section */
106 #define G_Y_OFF         (1*(MAXJSAMPLE+1))      /* offset to G => Y section */
107 #define B_Y_OFF         (2*(MAXJSAMPLE+1))      /* etc. */
108 #define TABLE_SIZE      (3*(MAXJSAMPLE+1))
109
110
111 /*
112  * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
113  */
114
115 LOCAL(void)
116 build_ycc_rgb_table (j_decompress_ptr cinfo)
117 /* Normal case, sYCC */
118 {
119   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
120   int i;
121   INT32 x;
122   SHIFT_TEMPS
123
124   cconvert->Cr_r_tab = (int *)
125     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
126                                 (MAXJSAMPLE+1) * SIZEOF(int));
127   cconvert->Cb_b_tab = (int *)
128     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
129                                 (MAXJSAMPLE+1) * SIZEOF(int));
130   cconvert->Cr_g_tab = (INT32 *)
131     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
132                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
133   cconvert->Cb_g_tab = (INT32 *)
134     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
135                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
136
137   cconvert->range_limit = cinfo->sample_range_limit;
138
139   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
140     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
141     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
142     /* Cr=>R value is nearest int to 1.402 * x */
143     cconvert->Cr_r_tab[i] = (int)
144                     RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
145     /* Cb=>B value is nearest int to 1.772 * x */
146     cconvert->Cb_b_tab[i] = (int)
147                     RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
148     /* Cr=>G value is scaled-up -0.714136286 * x */
149     cconvert->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
150     /* Cb=>G value is scaled-up -0.344136286 * x */
151     /* We also add in ONE_HALF so that need not do it in inner loop */
152     cconvert->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
153   }
154 }
155
156
157 LOCAL(void)
158 build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
159 /* Wide gamut case, bg-sYCC */
160 {
161   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
162   int i;
163   INT32 x;
164   SHIFT_TEMPS
165
166   cconvert->Cr_r_tab = (int *)
167     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
168                                 (MAXJSAMPLE+1) * SIZEOF(int));
169   cconvert->Cb_b_tab = (int *)
170     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
171                                 (MAXJSAMPLE+1) * SIZEOF(int));
172   cconvert->Cr_g_tab = (INT32 *)
173     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
174                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
175   cconvert->Cb_g_tab = (INT32 *)
176     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
177                                 (MAXJSAMPLE+1) * SIZEOF(INT32));
178
179   cconvert->range_limit = (JSAMPLE *)
180     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
181                                 5 * (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
182
183   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
184     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
185     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
186     /* Cr=>R value is nearest int to 2.804 * x */
187     cconvert->Cr_r_tab[i] = (int)
188                     RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
189     /* Cb=>B value is nearest int to 3.544 * x */
190     cconvert->Cb_b_tab[i] = (int)
191                     RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
192     /* Cr=>G value is scaled-up -1.428272572 * x */
193     cconvert->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
194     /* Cb=>G value is scaled-up -0.688272572 * x */
195     /* We also add in ONE_HALF so that need not do it in inner loop */
196     cconvert->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
197   }
198
199   /* Cb and Cr portions can extend to double range in wide gamut case,
200    * so we prepare an appropriate extended range limit table.
201    */
202
203   /* First segment of range limit table: limit[x] = 0 for x < 0 */
204   MEMZERO(cconvert->range_limit, 2 * (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
205   cconvert->range_limit += 2 * (MAXJSAMPLE+1);
206   /* Main part of range limit table: limit[x] = x */
207   for (i = 0; i <= MAXJSAMPLE; i++)
208     cconvert->range_limit[i] = (JSAMPLE) i;
209   /* End of range limit table: limit[x] = MAXJSAMPLE for x > MAXJSAMPLE */
210   for (; i < 3 * (MAXJSAMPLE+1); i++)
211     cconvert->range_limit[i] = MAXJSAMPLE;
212 }
213
214
215 /*
216  * Convert some rows of samples to the output colorspace.
217  *
218  * Note that we change from noninterleaved, one-plane-per-component format
219  * to interleaved-pixel format.  The output buffer is therefore three times
220  * as wide as the input buffer.
221  * A starting row offset is provided only for the input buffer.  The caller
222  * can easily adjust the passed output_buf value to accommodate any row
223  * offset required on that side.
224  */
225
226 METHODDEF(void)
227 ycc_rgb_convert (j_decompress_ptr cinfo,
228                  JSAMPIMAGE input_buf, JDIMENSION input_row,
229                  JSAMPARRAY output_buf, int num_rows)
230 {
231   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
232   register int y, cb, cr;
233   register JSAMPROW outptr;
234   register JSAMPROW inptr0, inptr1, inptr2;
235   register JDIMENSION col;
236   JDIMENSION num_cols = cinfo->output_width;
237   /* copy these pointers into registers if possible */
238   register JSAMPLE * range_limit = cconvert->range_limit;
239   register int * Crrtab = cconvert->Cr_r_tab;
240   register int * Cbbtab = cconvert->Cb_b_tab;
241   register INT32 * Crgtab = cconvert->Cr_g_tab;
242   register INT32 * Cbgtab = cconvert->Cb_g_tab;
243   SHIFT_TEMPS
244
245   while (--num_rows >= 0) {
246     inptr0 = input_buf[0][input_row];
247     inptr1 = input_buf[1][input_row];
248     inptr2 = input_buf[2][input_row];
249     input_row++;
250     outptr = *output_buf++;
251     for (col = 0; col < num_cols; col++) {
252       y  = GETJSAMPLE(inptr0[col]);
253       cb = GETJSAMPLE(inptr1[col]);
254       cr = GETJSAMPLE(inptr2[col]);
255       /* Range-limiting is essential due to noise introduced by DCT losses,
256        * for extended gamut (sYCC) and wide gamut (bg-sYCC) encodings.
257        */
258       outptr[RGB_RED]   = range_limit[y + Crrtab[cr]];
259       outptr[RGB_GREEN] = range_limit[y +
260                               ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
261                                                  SCALEBITS))];
262       outptr[RGB_BLUE]  = range_limit[y + Cbbtab[cb]];
263       outptr += RGB_PIXELSIZE;
264     }
265   }
266 }
267
268
269 /**************** Cases other than YCC -> RGB ****************/
270
271
272 /*
273  * Initialize for RGB->grayscale colorspace conversion.
274  */
275
276 LOCAL(void)
277 build_rgb_y_table (j_decompress_ptr cinfo)
278 {
279   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
280   INT32 * rgb_y_tab;
281   INT32 i;
282
283   /* Allocate and fill in the conversion tables. */
284   cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
285     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
286                                 (TABLE_SIZE * SIZEOF(INT32)));
287
288   for (i = 0; i <= MAXJSAMPLE; i++) {
289     rgb_y_tab[i+R_Y_OFF] = FIX(0.299) * i;
290     rgb_y_tab[i+G_Y_OFF] = FIX(0.587) * i;
291     rgb_y_tab[i+B_Y_OFF] = FIX(0.114) * i + ONE_HALF;
292   }
293 }
294
295
296 /*
297  * Convert RGB to grayscale.
298  */
299
300 METHODDEF(void)
301 rgb_gray_convert (j_decompress_ptr cinfo,
302                   JSAMPIMAGE input_buf, JDIMENSION input_row,
303                   JSAMPARRAY output_buf, int num_rows)
304 {
305   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
306   register INT32 * ctab = cconvert->rgb_y_tab;
307   register int r, g, b;
308   register JSAMPROW outptr;
309   register JSAMPROW inptr0, inptr1, inptr2;
310   register JDIMENSION col;
311   JDIMENSION num_cols = cinfo->output_width;
312
313   while (--num_rows >= 0) {
314     inptr0 = input_buf[0][input_row];
315     inptr1 = input_buf[1][input_row];
316     inptr2 = input_buf[2][input_row];
317     input_row++;
318     outptr = *output_buf++;
319     for (col = 0; col < num_cols; col++) {
320       r = GETJSAMPLE(inptr0[col]);
321       g = GETJSAMPLE(inptr1[col]);
322       b = GETJSAMPLE(inptr2[col]);
323       /* Y */
324       outptr[col] = (JSAMPLE)
325                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
326                  >> SCALEBITS);
327     }
328   }
329 }
330
331
332 /*
333  * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
334  * (inverse color transform).
335  * This can be seen as an adaption of the general YCbCr->RGB
336  * conversion equation with Kr = Kb = 0, while replacing the
337  * normalization by modulo calculation.
338  */
339
340 METHODDEF(void)
341 rgb1_rgb_convert (j_decompress_ptr cinfo,
342                   JSAMPIMAGE input_buf, JDIMENSION input_row,
343                   JSAMPARRAY output_buf, int num_rows)
344 {
345   register int r, g, b;
346   register JSAMPROW outptr;
347   register JSAMPROW inptr0, inptr1, inptr2;
348   register JDIMENSION col;
349   JDIMENSION num_cols = cinfo->output_width;
350
351   while (--num_rows >= 0) {
352     inptr0 = input_buf[0][input_row];
353     inptr1 = input_buf[1][input_row];
354     inptr2 = input_buf[2][input_row];
355     input_row++;
356     outptr = *output_buf++;
357     for (col = 0; col < num_cols; col++) {
358       r = GETJSAMPLE(inptr0[col]);
359       g = GETJSAMPLE(inptr1[col]);
360       b = GETJSAMPLE(inptr2[col]);
361       /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
362        * (modulo) operator is equivalent to the bitmask operator AND.
363        */
364       outptr[RGB_RED]   = (JSAMPLE) ((r + g - CENTERJSAMPLE) & MAXJSAMPLE);
365       outptr[RGB_GREEN] = (JSAMPLE) g;
366       outptr[RGB_BLUE]  = (JSAMPLE) ((b + g - CENTERJSAMPLE) & MAXJSAMPLE);
367       outptr += RGB_PIXELSIZE;
368     }
369   }
370 }
371
372
373 /*
374  * [R-G,G,B-G] to grayscale conversion with modulo calculation
375  * (inverse color transform).
376  */
377
378 METHODDEF(void)
379 rgb1_gray_convert (j_decompress_ptr cinfo,
380                    JSAMPIMAGE input_buf, JDIMENSION input_row,
381                    JSAMPARRAY output_buf, int num_rows)
382 {
383   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
384   register INT32 * ctab = cconvert->rgb_y_tab;
385   register int r, g, b;
386   register JSAMPROW outptr;
387   register JSAMPROW inptr0, inptr1, inptr2;
388   register JDIMENSION col;
389   JDIMENSION num_cols = cinfo->output_width;
390
391   while (--num_rows >= 0) {
392     inptr0 = input_buf[0][input_row];
393     inptr1 = input_buf[1][input_row];
394     inptr2 = input_buf[2][input_row];
395     input_row++;
396     outptr = *output_buf++;
397     for (col = 0; col < num_cols; col++) {
398       r = GETJSAMPLE(inptr0[col]);
399       g = GETJSAMPLE(inptr1[col]);
400       b = GETJSAMPLE(inptr2[col]);
401       /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
402        * (modulo) operator is equivalent to the bitmask operator AND.
403        */
404       r = (r + g - CENTERJSAMPLE) & MAXJSAMPLE;
405       b = (b + g - CENTERJSAMPLE) & MAXJSAMPLE;
406       /* Y */
407       outptr[col] = (JSAMPLE)
408                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
409                  >> SCALEBITS);
410     }
411   }
412 }
413
414
415 /*
416  * No colorspace change, but conversion from separate-planes
417  * to interleaved representation.
418  */
419
420 METHODDEF(void)
421 rgb_convert (j_decompress_ptr cinfo,
422              JSAMPIMAGE input_buf, JDIMENSION input_row,
423              JSAMPARRAY output_buf, int num_rows)
424 {
425   register JSAMPROW outptr;
426   register JSAMPROW inptr0, inptr1, inptr2;
427   register JDIMENSION col;
428   JDIMENSION num_cols = cinfo->output_width;
429
430   while (--num_rows >= 0) {
431     inptr0 = input_buf[0][input_row];
432     inptr1 = input_buf[1][input_row];
433     inptr2 = input_buf[2][input_row];
434     input_row++;
435     outptr = *output_buf++;
436     for (col = 0; col < num_cols; col++) {
437       /* We can dispense with GETJSAMPLE() here */
438       outptr[RGB_RED]   = inptr0[col];
439       outptr[RGB_GREEN] = inptr1[col];
440       outptr[RGB_BLUE]  = inptr2[col];
441       outptr += RGB_PIXELSIZE;
442     }
443   }
444 }
445
446
447 /*
448  * Color conversion for no colorspace change: just copy the data,
449  * converting from separate-planes to interleaved representation.
450  */
451
452 METHODDEF(void)
453 null_convert (j_decompress_ptr cinfo,
454               JSAMPIMAGE input_buf, JDIMENSION input_row,
455               JSAMPARRAY output_buf, int num_rows)
456 {
457   int ci;
458   register int nc = cinfo->num_components;
459   register JSAMPROW outptr;
460   register JSAMPROW inptr;
461   register JDIMENSION col;
462   JDIMENSION num_cols = cinfo->output_width;
463
464   while (--num_rows >= 0) {
465     for (ci = 0; ci < nc; ci++) {
466       inptr = input_buf[ci][input_row];
467       outptr = output_buf[0] + ci;
468       for (col = 0; col < num_cols; col++) {
469         *outptr = *inptr++;     /* needn't bother with GETJSAMPLE() here */
470         outptr += nc;
471       }
472     }
473     input_row++;
474     output_buf++;
475   }
476 }
477
478
479 /*
480  * Color conversion for grayscale: just copy the data.
481  * This also works for YCC -> grayscale conversion, in which
482  * we just copy the Y (luminance) component and ignore chrominance.
483  */
484
485 METHODDEF(void)
486 grayscale_convert (j_decompress_ptr cinfo,
487                    JSAMPIMAGE input_buf, JDIMENSION input_row,
488                    JSAMPARRAY output_buf, int num_rows)
489 {
490   jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
491                     num_rows, cinfo->output_width);
492 }
493
494
495 /*
496  * Convert grayscale to RGB: just duplicate the graylevel three times.
497  * This is provided to support applications that don't want to cope
498  * with grayscale as a separate case.
499  */
500
501 METHODDEF(void)
502 gray_rgb_convert (j_decompress_ptr cinfo,
503                   JSAMPIMAGE input_buf, JDIMENSION input_row,
504                   JSAMPARRAY output_buf, int num_rows)
505 {
506   register JSAMPROW outptr;
507   register JSAMPROW inptr;
508   register JDIMENSION col;
509   JDIMENSION num_cols = cinfo->output_width;
510
511   while (--num_rows >= 0) {
512     inptr = input_buf[0][input_row++];
513     outptr = *output_buf++;
514     for (col = 0; col < num_cols; col++) {
515       /* We can dispense with GETJSAMPLE() here */
516       outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
517       outptr += RGB_PIXELSIZE;
518     }
519   }
520 }
521
522
523 /*
524  * Adobe-style YCCK->CMYK conversion.
525  * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
526  * conversion as above, while passing K (black) unchanged.
527  * We assume build_ycc_rgb_table has been called.
528  */
529
530 METHODDEF(void)
531 ycck_cmyk_convert (j_decompress_ptr cinfo,
532                    JSAMPIMAGE input_buf, JDIMENSION input_row,
533                    JSAMPARRAY output_buf, int num_rows)
534 {
535   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
536   register int y, cb, cr;
537   register JSAMPROW outptr;
538   register JSAMPROW inptr0, inptr1, inptr2, inptr3;
539   register JDIMENSION col;
540   JDIMENSION num_cols = cinfo->output_width;
541   /* copy these pointers into registers if possible */
542   register JSAMPLE * range_limit = cinfo->sample_range_limit;
543   register int * Crrtab = cconvert->Cr_r_tab;
544   register int * Cbbtab = cconvert->Cb_b_tab;
545   register INT32 * Crgtab = cconvert->Cr_g_tab;
546   register INT32 * Cbgtab = cconvert->Cb_g_tab;
547   SHIFT_TEMPS
548
549   while (--num_rows >= 0) {
550     inptr0 = input_buf[0][input_row];
551     inptr1 = input_buf[1][input_row];
552     inptr2 = input_buf[2][input_row];
553     inptr3 = input_buf[3][input_row];
554     input_row++;
555     outptr = *output_buf++;
556     for (col = 0; col < num_cols; col++) {
557       y  = GETJSAMPLE(inptr0[col]);
558       cb = GETJSAMPLE(inptr1[col]);
559       cr = GETJSAMPLE(inptr2[col]);
560       /* Range-limiting is essential due to noise introduced by DCT losses,
561        * and for extended gamut encodings (sYCC).
562        */
563       outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];   /* red */
564       outptr[1] = range_limit[MAXJSAMPLE - (y +                 /* green */
565                               ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
566                                                  SCALEBITS)))];
567       outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];   /* blue */
568       /* K passes through unchanged */
569       outptr[3] = inptr3[col];  /* don't need GETJSAMPLE here */
570       outptr += 4;
571     }
572   }
573 }
574
575
576 /*
577  * Empty method for start_pass.
578  */
579
580 METHODDEF(void)
581 start_pass_dcolor (j_decompress_ptr cinfo)
582 {
583   /* no work needed */
584 }
585
586
587 /*
588  * Module initialization routine for output colorspace conversion.
589  */
590
591 GLOBAL(void)
592 jinit_color_deconverter (j_decompress_ptr cinfo)
593 {
594   my_cconvert_ptr cconvert;
595   int ci;
596
597   cconvert = (my_cconvert_ptr)
598     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
599                                 SIZEOF(my_color_deconverter));
600   cinfo->cconvert = &cconvert->pub;
601   cconvert->pub.start_pass = start_pass_dcolor;
602
603   /* Make sure num_components agrees with jpeg_color_space */
604   switch (cinfo->jpeg_color_space) {
605   case JCS_GRAYSCALE:
606     if (cinfo->num_components != 1)
607       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
608     break;
609
610   case JCS_RGB:
611   case JCS_YCbCr:
612   case JCS_BG_RGB:
613   case JCS_BG_YCC:
614     if (cinfo->num_components != 3)
615       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
616     break;
617
618   case JCS_CMYK:
619   case JCS_YCCK:
620     if (cinfo->num_components != 4)
621       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
622     break;
623
624   default:                      /* JCS_UNKNOWN can be anything */
625     if (cinfo->num_components < 1)
626       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
627     break;
628   }
629
630   /* Support color transform only for RGB colorspaces */
631   if (cinfo->color_transform &&
632       cinfo->jpeg_color_space != JCS_RGB &&
633       cinfo->jpeg_color_space != JCS_BG_RGB)
634     ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
635
636   /* Set out_color_components and conversion method based on requested space.
637    * Also clear the component_needed flags for any unused components,
638    * so that earlier pipeline stages can avoid useless computation.
639    */
640
641   switch (cinfo->out_color_space) {
642   case JCS_GRAYSCALE:
643     cinfo->out_color_components = 1;
644     switch (cinfo->jpeg_color_space) {
645     case JCS_GRAYSCALE:
646     case JCS_YCbCr:
647     case JCS_BG_YCC:
648       cconvert->pub.color_convert = grayscale_convert;
649       /* For color->grayscale conversion, only the Y (0) component is needed */
650       for (ci = 1; ci < cinfo->num_components; ci++)
651         cinfo->comp_info[ci].component_needed = FALSE;
652       break;
653     case JCS_RGB:
654       switch (cinfo->color_transform) {
655       case JCT_NONE:
656         cconvert->pub.color_convert = rgb_gray_convert;
657         break;
658       case JCT_SUBTRACT_GREEN:
659         cconvert->pub.color_convert = rgb1_gray_convert;
660         break;
661       default:
662         ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
663       }
664       build_rgb_y_table(cinfo);
665       break;
666     default:
667       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
668     }
669     break;
670
671   case JCS_RGB:
672     cinfo->out_color_components = RGB_PIXELSIZE;
673     switch (cinfo->jpeg_color_space) {
674     case JCS_GRAYSCALE:
675       cconvert->pub.color_convert = gray_rgb_convert;
676       break;
677     case JCS_YCbCr:
678       cconvert->pub.color_convert = ycc_rgb_convert;
679       build_ycc_rgb_table(cinfo);
680       break;
681     case JCS_BG_YCC:
682       cconvert->pub.color_convert = ycc_rgb_convert;
683       build_bg_ycc_rgb_table(cinfo);
684       break;
685     case JCS_RGB:
686       switch (cinfo->color_transform) {
687       case JCT_NONE:
688         cconvert->pub.color_convert = rgb_convert;
689         break;
690       case JCT_SUBTRACT_GREEN:
691         cconvert->pub.color_convert = rgb1_rgb_convert;
692         break;
693       default:
694         ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
695       }
696       break;
697     default:
698       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
699     }
700     break;
701
702   case JCS_BG_RGB:
703     cinfo->out_color_components = RGB_PIXELSIZE;
704     if (cinfo->jpeg_color_space == JCS_BG_RGB) {
705       switch (cinfo->color_transform) {
706       case JCT_NONE:
707         cconvert->pub.color_convert = rgb_convert;
708         break;
709       case JCT_SUBTRACT_GREEN:
710         cconvert->pub.color_convert = rgb1_rgb_convert;
711         break;
712       default:
713         ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
714       }
715     } else
716       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
717     break;
718
719   case JCS_CMYK:
720     cinfo->out_color_components = 4;
721     switch (cinfo->jpeg_color_space) {
722     case JCS_YCCK:
723       cconvert->pub.color_convert = ycck_cmyk_convert;
724       build_ycc_rgb_table(cinfo);
725       break;
726     case JCS_CMYK:
727       cconvert->pub.color_convert = null_convert;
728       break;
729     default:
730       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
731     }
732     break;
733
734   default:
735     /* Permit null conversion to same output space */
736     if (cinfo->out_color_space == cinfo->jpeg_color_space) {
737       cinfo->out_color_components = cinfo->num_components;
738       cconvert->pub.color_convert = null_convert;
739     } else                      /* unsupported non-null conversion */
740       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
741     break;
742   }
743
744   if (cinfo->quantize_colors)
745     cinfo->output_components = 1; /* single colormapped output component */
746   else
747     cinfo->output_components = cinfo->out_color_components;
748 }