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.
9 * This file contains output colorspace conversion routines.
12 #define JPEG_INTERNALS
17 /* Private subobject */
20 struct jpeg_color_deconverter pub
; /* public fields */
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 */
28 JSAMPLE
* range_limit
; /* pointer to normal sample range limit table, */
29 /* or extended sample range limit table for BG_YCC */
31 /* Private state for RGB->Y conversion */
32 INT32
* rgb_y_tab
; /* => table for RGB to Y conversion */
33 } my_color_deconverter
;
35 typedef my_color_deconverter
* my_cconvert_ptr
;
38 /*************** YCbCr -> RGB conversion: most common case **************/
39 /*************** BG_YCC -> RGB conversion: less common case **************/
40 /*************** RGB -> Y conversion: less common case **************/
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
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
58 * Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
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
65 * G = Y - 0.344136286 * Cb - 0.714136286 * Cr
68 * Y = 0.299 * R + 0.587 * G + 0.114 * B
70 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
71 * For bg-sYCC, with K = 4, the equations are
74 * G = Y - 0.688272572 * Cb - 1.428272572 * Cr
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.
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.
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))
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,
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))
112 * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
116 build_ycc_rgb_table (j_decompress_ptr cinfo
)
117 /* Normal case, sYCC */
119 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
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
));
137 cconvert
->range_limit
= cinfo
->sample_range_limit
;
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
;
158 build_bg_ycc_rgb_table (j_decompress_ptr cinfo
)
159 /* Wide gamut case, bg-sYCC */
161 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
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
));
179 cconvert
->range_limit
= (JSAMPLE
*)
180 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
181 5 * (MAXJSAMPLE
+1) * SIZEOF(JSAMPLE
));
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
;
199 /* Cb and Cr portions can extend to double range in wide gamut case,
200 * so we prepare an appropriate extended range limit table.
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
;
216 * Convert some rows of samples to the output colorspace.
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.
227 ycc_rgb_convert (j_decompress_ptr cinfo
,
228 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
229 JSAMPARRAY output_buf
, int num_rows
)
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
;
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
];
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.
258 outptr
[RGB_RED
] = range_limit
[y
+ Crrtab
[cr
]];
259 outptr
[RGB_GREEN
] = range_limit
[y
+
260 ((int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
],
262 outptr
[RGB_BLUE
] = range_limit
[y
+ Cbbtab
[cb
]];
263 outptr
+= RGB_PIXELSIZE
;
269 /**************** Cases other than YCC -> RGB ****************/
273 * Initialize for RGB->grayscale colorspace conversion.
277 build_rgb_y_table (j_decompress_ptr cinfo
)
279 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
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
)));
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
;
297 * Convert RGB to grayscale.
301 rgb_gray_convert (j_decompress_ptr cinfo
,
302 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
303 JSAMPARRAY output_buf
, int num_rows
)
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
;
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
];
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
]);
324 outptr
[col
] = (JSAMPLE
)
325 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
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.
341 rgb1_rgb_convert (j_decompress_ptr cinfo
,
342 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
343 JSAMPARRAY output_buf
, int num_rows
)
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
;
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
];
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.
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
;
374 * [R-G,G,B-G] to grayscale conversion with modulo calculation
375 * (inverse color transform).
379 rgb1_gray_convert (j_decompress_ptr cinfo
,
380 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
381 JSAMPARRAY output_buf
, int num_rows
)
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
;
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
];
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.
404 r
= (r
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
;
405 b
= (b
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
;
407 outptr
[col
] = (JSAMPLE
)
408 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
416 * No colorspace change, but conversion from separate-planes
417 * to interleaved representation.
421 rgb_convert (j_decompress_ptr cinfo
,
422 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
423 JSAMPARRAY output_buf
, int num_rows
)
425 register JSAMPROW outptr
;
426 register JSAMPROW inptr0
, inptr1
, inptr2
;
427 register JDIMENSION col
;
428 JDIMENSION num_cols
= cinfo
->output_width
;
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
];
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
;
448 * Color conversion for no colorspace change: just copy the data,
449 * converting from separate-planes to interleaved representation.
453 null_convert (j_decompress_ptr cinfo
,
454 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
455 JSAMPARRAY output_buf
, int num_rows
)
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
;
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 */
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.
486 grayscale_convert (j_decompress_ptr cinfo
,
487 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
488 JSAMPARRAY output_buf
, int num_rows
)
490 jcopy_sample_rows(input_buf
[0], (int) input_row
, output_buf
, 0,
491 num_rows
, cinfo
->output_width
);
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.
502 gray_rgb_convert (j_decompress_ptr cinfo
,
503 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
504 JSAMPARRAY output_buf
, int num_rows
)
506 register JSAMPROW outptr
;
507 register JSAMPROW inptr
;
508 register JDIMENSION col
;
509 JDIMENSION num_cols
= cinfo
->output_width
;
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
;
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.
531 ycck_cmyk_convert (j_decompress_ptr cinfo
,
532 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
533 JSAMPARRAY output_buf
, int num_rows
)
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
;
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
];
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).
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
],
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 */
577 * Empty method for start_pass.
581 start_pass_dcolor (j_decompress_ptr cinfo
)
588 * Module initialization routine for output colorspace conversion.
592 jinit_color_deconverter (j_decompress_ptr cinfo
)
594 my_cconvert_ptr cconvert
;
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
;
603 /* Make sure num_components agrees with jpeg_color_space */
604 switch (cinfo
->jpeg_color_space
) {
606 if (cinfo
->num_components
!= 1)
607 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
614 if (cinfo
->num_components
!= 3)
615 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
620 if (cinfo
->num_components
!= 4)
621 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
624 default: /* JCS_UNKNOWN can be anything */
625 if (cinfo
->num_components
< 1)
626 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
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
);
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.
641 switch (cinfo
->out_color_space
) {
643 cinfo
->out_color_components
= 1;
644 switch (cinfo
->jpeg_color_space
) {
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
;
654 switch (cinfo
->color_transform
) {
656 cconvert
->pub
.color_convert
= rgb_gray_convert
;
658 case JCT_SUBTRACT_GREEN
:
659 cconvert
->pub
.color_convert
= rgb1_gray_convert
;
662 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
664 build_rgb_y_table(cinfo
);
667 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
672 cinfo
->out_color_components
= RGB_PIXELSIZE
;
673 switch (cinfo
->jpeg_color_space
) {
675 cconvert
->pub
.color_convert
= gray_rgb_convert
;
678 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
679 build_ycc_rgb_table(cinfo
);
682 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
683 build_bg_ycc_rgb_table(cinfo
);
686 switch (cinfo
->color_transform
) {
688 cconvert
->pub
.color_convert
= rgb_convert
;
690 case JCT_SUBTRACT_GREEN
:
691 cconvert
->pub
.color_convert
= rgb1_rgb_convert
;
694 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
698 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
703 cinfo
->out_color_components
= RGB_PIXELSIZE
;
704 if (cinfo
->jpeg_color_space
== JCS_BG_RGB
) {
705 switch (cinfo
->color_transform
) {
707 cconvert
->pub
.color_convert
= rgb_convert
;
709 case JCT_SUBTRACT_GREEN
:
710 cconvert
->pub
.color_convert
= rgb1_rgb_convert
;
713 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
716 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
720 cinfo
->out_color_components
= 4;
721 switch (cinfo
->jpeg_color_space
) {
723 cconvert
->pub
.color_convert
= ycck_cmyk_convert
;
724 build_ycc_rgb_table(cinfo
);
727 cconvert
->pub
.color_convert
= null_convert
;
730 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
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
);
744 if (cinfo
->quantize_colors
)
745 cinfo
->output_components
= 1; /* single colormapped output component */
747 cinfo
->output_components
= cinfo
->out_color_components
;