4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2011-2019 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
18 /* Deliberate syntax err */
19 Sorry
, this code requires
2 or more range extension bits
.
23 /* Private subobject */
26 struct jpeg_color_deconverter pub
; /* public fields */
28 /* Private state for YCbCr->RGB and BG_YCC->RGB conversion */
29 int * Cr_r_tab
; /* => table for Cr to R conversion */
30 int * Cb_b_tab
; /* => table for Cb to B conversion */
31 INT32
* Cr_g_tab
; /* => table for Cr to G conversion */
32 INT32
* Cb_g_tab
; /* => table for Cb to G conversion */
34 /* Private state for RGB->Y conversion */
35 INT32
* rgb_y_tab
; /* => table for RGB to Y conversion */
36 } my_color_deconverter
;
38 typedef my_color_deconverter
* my_cconvert_ptr
;
41 /*************** YCbCr -> RGB conversion: most common case **************/
42 /*************** BG_YCC -> RGB conversion: less common case **************/
43 /*************** RGB -> Y conversion: less common case **************/
46 * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
47 * previously known as Recommendation CCIR 601-1, except that Cb and Cr
48 * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
49 * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
50 * sYCC (standard luma-chroma-chroma color space with extended gamut)
51 * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
52 * bg-sRGB and bg-sYCC (big gamut standard color spaces)
53 * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
54 * Note that the derived conversion coefficients given in some of these
55 * documents are imprecise. The general conversion equations are
57 * R = Y + K * (1 - Kr) * Cr
58 * G = Y - K * (Kb * (1 - Kb) * Cb + Kr * (1 - Kr) * Cr) / (1 - Kr - Kb)
59 * B = Y + K * (1 - Kb) * Cb
61 * Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
63 * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
64 * from the 1953 FCC NTSC primaries and CIE Illuminant C), K = 2 for sYCC,
65 * the conversion equations to be implemented are therefore
68 * G = Y - 0.344136286 * Cb - 0.714136286 * Cr
71 * Y = 0.299 * R + 0.587 * G + 0.114 * B
73 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
74 * For bg-sYCC, with K = 4, the equations are
77 * G = Y - 0.688272572 * Cb - 1.428272572 * Cr
80 * To avoid floating-point arithmetic, we represent the fractional constants
81 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
82 * the products by 2^16, with appropriate rounding, to get the correct answer.
83 * Notice that Y, being an integral input, does not contribute any fraction
84 * so it need not participate in the rounding.
86 * For even more speed, we avoid doing any multiplications in the inner loop
87 * by precalculating the constants times Cb and Cr for all possible values.
88 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
89 * for 9-bit to 12-bit samples it is still acceptable. It's not very
90 * reasonable for 16-bit samples, but if you want lossless storage you
91 * shouldn't be changing colorspace anyway.
92 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
93 * values for the G calculation are left scaled up, since we must add them
94 * together before rounding.
97 #define SCALEBITS 16 /* speediest right-shift on some machines */
98 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
99 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
101 /* We allocate one big table for RGB->Y conversion and divide it up into
102 * three parts, instead of doing three alloc_small requests. This lets us
103 * use a single table base address, which can be held in a register in the
104 * inner loops on many machines (more than can hold all three addresses,
108 #define R_Y_OFF 0 /* offset to R => Y section */
109 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
110 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
111 #define TABLE_SIZE (3*(MAXJSAMPLE+1))
115 * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
119 build_ycc_rgb_table (j_decompress_ptr cinfo
)
120 /* Normal case, sYCC */
122 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
127 cconvert
->Cr_r_tab
= (int *) (*cinfo
->mem
->alloc_small
)
128 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
129 cconvert
->Cb_b_tab
= (int *) (*cinfo
->mem
->alloc_small
)
130 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
131 cconvert
->Cr_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
132 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
133 cconvert
->Cb_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
134 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
136 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
137 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
138 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
139 /* Cr=>R value is nearest int to 1.402 * x */
140 cconvert
->Cr_r_tab
[i
] = (int) DESCALE(FIX(1.402) * x
, SCALEBITS
);
141 /* Cb=>B value is nearest int to 1.772 * x */
142 cconvert
->Cb_b_tab
[i
] = (int) DESCALE(FIX(1.772) * x
, SCALEBITS
);
143 /* Cr=>G value is scaled-up -0.714136286 * x */
144 cconvert
->Cr_g_tab
[i
] = (- FIX(0.714136286)) * x
;
145 /* Cb=>G value is scaled-up -0.344136286 * x */
146 /* We also add in ONE_HALF so that need not do it in inner loop */
147 cconvert
->Cb_g_tab
[i
] = (- FIX(0.344136286)) * x
+ ONE_HALF
;
153 build_bg_ycc_rgb_table (j_decompress_ptr cinfo
)
154 /* Wide gamut case, bg-sYCC */
156 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
161 cconvert
->Cr_r_tab
= (int *) (*cinfo
->mem
->alloc_small
)
162 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
163 cconvert
->Cb_b_tab
= (int *) (*cinfo
->mem
->alloc_small
)
164 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
165 cconvert
->Cr_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
166 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
167 cconvert
->Cb_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
168 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
170 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
171 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
172 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
173 /* Cr=>R value is nearest int to 2.804 * x */
174 cconvert
->Cr_r_tab
[i
] = (int) DESCALE(FIX(2.804) * x
, SCALEBITS
);
175 /* Cb=>B value is nearest int to 3.544 * x */
176 cconvert
->Cb_b_tab
[i
] = (int) DESCALE(FIX(3.544) * x
, SCALEBITS
);
177 /* Cr=>G value is scaled-up -1.428272572 * x */
178 cconvert
->Cr_g_tab
[i
] = (- FIX(1.428272572)) * x
;
179 /* Cb=>G value is scaled-up -0.688272572 * x */
180 /* We also add in ONE_HALF so that need not do it in inner loop */
181 cconvert
->Cb_g_tab
[i
] = (- FIX(0.688272572)) * x
+ ONE_HALF
;
187 * Convert some rows of samples to the output colorspace.
189 * Note that we change from noninterleaved, one-plane-per-component format
190 * to interleaved-pixel format. The output buffer is therefore three times
191 * as wide as the input buffer.
193 * A starting row offset is provided only for the input buffer. The caller
194 * can easily adjust the passed output_buf value to accommodate any row
195 * offset required on that side.
199 ycc_rgb_convert (j_decompress_ptr cinfo
,
200 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
201 JSAMPARRAY output_buf
, int num_rows
)
203 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
204 register int y
, cb
, cr
;
205 register JSAMPROW outptr
;
206 register JSAMPROW inptr0
, inptr1
, inptr2
;
207 register JDIMENSION col
;
208 JDIMENSION num_cols
= cinfo
->output_width
;
209 /* copy these pointers into registers if possible */
210 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
211 register int * Crrtab
= cconvert
->Cr_r_tab
;
212 register int * Cbbtab
= cconvert
->Cb_b_tab
;
213 register INT32
* Crgtab
= cconvert
->Cr_g_tab
;
214 register INT32
* Cbgtab
= cconvert
->Cb_g_tab
;
217 while (--num_rows
>= 0) {
218 inptr0
= input_buf
[0][input_row
];
219 inptr1
= input_buf
[1][input_row
];
220 inptr2
= input_buf
[2][input_row
];
222 outptr
= *output_buf
++;
223 for (col
= 0; col
< num_cols
; col
++) {
224 y
= GETJSAMPLE(inptr0
[col
]);
225 cb
= GETJSAMPLE(inptr1
[col
]);
226 cr
= GETJSAMPLE(inptr2
[col
]);
227 /* Range-limiting is essential due to noise introduced by DCT losses,
228 * for extended gamut (sYCC) and wide gamut (bg-sYCC) encodings.
230 outptr
[RGB_RED
] = range_limit
[y
+ Crrtab
[cr
]];
231 outptr
[RGB_GREEN
] = range_limit
[y
+
232 ((int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
],
234 outptr
[RGB_BLUE
] = range_limit
[y
+ Cbbtab
[cb
]];
235 outptr
+= RGB_PIXELSIZE
;
241 /**************** Cases other than YCC -> RGB ****************/
245 * Initialize for RGB->grayscale colorspace conversion.
249 build_rgb_y_table (j_decompress_ptr cinfo
)
251 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
255 /* Allocate and fill in the conversion tables. */
256 cconvert
->rgb_y_tab
= rgb_y_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
257 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, TABLE_SIZE
* SIZEOF(INT32
));
259 for (i
= 0; i
<= MAXJSAMPLE
; i
++) {
260 rgb_y_tab
[i
+R_Y_OFF
] = FIX(0.299) * i
;
261 rgb_y_tab
[i
+G_Y_OFF
] = FIX(0.587) * i
;
262 rgb_y_tab
[i
+B_Y_OFF
] = FIX(0.114) * i
+ ONE_HALF
;
268 * Convert RGB to grayscale.
272 rgb_gray_convert (j_decompress_ptr cinfo
,
273 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
274 JSAMPARRAY output_buf
, int num_rows
)
276 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
277 register int r
, g
, b
;
278 register INT32
* ctab
= cconvert
->rgb_y_tab
;
279 register JSAMPROW outptr
;
280 register JSAMPROW inptr0
, inptr1
, inptr2
;
281 register JDIMENSION col
;
282 JDIMENSION num_cols
= cinfo
->output_width
;
284 while (--num_rows
>= 0) {
285 inptr0
= input_buf
[0][input_row
];
286 inptr1
= input_buf
[1][input_row
];
287 inptr2
= input_buf
[2][input_row
];
289 outptr
= *output_buf
++;
290 for (col
= 0; col
< num_cols
; col
++) {
291 r
= GETJSAMPLE(inptr0
[col
]);
292 g
= GETJSAMPLE(inptr1
[col
]);
293 b
= GETJSAMPLE(inptr2
[col
]);
295 outptr
[col
] = (JSAMPLE
)
296 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
304 * Convert some rows of samples to the output colorspace.
305 * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
306 * (inverse color transform).
307 * This can be seen as an adaption of the general YCbCr->RGB
308 * conversion equation with Kr = Kb = 0, while replacing the
309 * normalization by modulo calculation.
313 rgb1_rgb_convert (j_decompress_ptr cinfo
,
314 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
315 JSAMPARRAY output_buf
, int num_rows
)
317 register int r
, g
, b
;
318 register JSAMPROW outptr
;
319 register JSAMPROW inptr0
, inptr1
, inptr2
;
320 register JDIMENSION col
;
321 JDIMENSION num_cols
= cinfo
->output_width
;
323 while (--num_rows
>= 0) {
324 inptr0
= input_buf
[0][input_row
];
325 inptr1
= input_buf
[1][input_row
];
326 inptr2
= input_buf
[2][input_row
];
328 outptr
= *output_buf
++;
329 for (col
= 0; col
< num_cols
; col
++) {
330 r
= GETJSAMPLE(inptr0
[col
]);
331 g
= GETJSAMPLE(inptr1
[col
]);
332 b
= GETJSAMPLE(inptr2
[col
]);
333 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
334 * (modulo) operator is equivalent to the bitmask operator AND.
336 outptr
[RGB_RED
] = (JSAMPLE
) ((r
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
);
337 outptr
[RGB_GREEN
] = (JSAMPLE
) g
;
338 outptr
[RGB_BLUE
] = (JSAMPLE
) ((b
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
);
339 outptr
+= RGB_PIXELSIZE
;
346 * [R-G,G,B-G] to grayscale conversion with modulo calculation
347 * (inverse color transform).
351 rgb1_gray_convert (j_decompress_ptr cinfo
,
352 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
353 JSAMPARRAY output_buf
, int num_rows
)
355 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
356 register int r
, g
, b
;
357 register INT32
* ctab
= cconvert
->rgb_y_tab
;
358 register JSAMPROW outptr
;
359 register JSAMPROW inptr0
, inptr1
, inptr2
;
360 register JDIMENSION col
;
361 JDIMENSION num_cols
= cinfo
->output_width
;
363 while (--num_rows
>= 0) {
364 inptr0
= input_buf
[0][input_row
];
365 inptr1
= input_buf
[1][input_row
];
366 inptr2
= input_buf
[2][input_row
];
368 outptr
= *output_buf
++;
369 for (col
= 0; col
< num_cols
; col
++) {
370 r
= GETJSAMPLE(inptr0
[col
]);
371 g
= GETJSAMPLE(inptr1
[col
]);
372 b
= GETJSAMPLE(inptr2
[col
]);
373 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
374 * (modulo) operator is equivalent to the bitmask operator AND.
376 r
= (r
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
;
377 b
= (b
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
;
379 outptr
[col
] = (JSAMPLE
)
380 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
388 * Convert some rows of samples to the output colorspace.
389 * No colorspace change, but conversion from separate-planes
390 * to interleaved representation.
394 rgb_convert (j_decompress_ptr cinfo
,
395 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
396 JSAMPARRAY output_buf
, int num_rows
)
398 register JSAMPROW outptr
;
399 register JSAMPROW inptr0
, inptr1
, inptr2
;
400 register JDIMENSION col
;
401 JDIMENSION num_cols
= cinfo
->output_width
;
403 while (--num_rows
>= 0) {
404 inptr0
= input_buf
[0][input_row
];
405 inptr1
= input_buf
[1][input_row
];
406 inptr2
= input_buf
[2][input_row
];
408 outptr
= *output_buf
++;
409 for (col
= 0; col
< num_cols
; col
++) {
410 /* We can dispense with GETJSAMPLE() here */
411 outptr
[RGB_RED
] = inptr0
[col
];
412 outptr
[RGB_GREEN
] = inptr1
[col
];
413 outptr
[RGB_BLUE
] = inptr2
[col
];
414 outptr
+= RGB_PIXELSIZE
;
421 * Color conversion for no colorspace change: just copy the data,
422 * converting from separate-planes to interleaved representation.
423 * We assume out_color_components == num_components.
427 null_convert (j_decompress_ptr cinfo
,
428 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
429 JSAMPARRAY output_buf
, int num_rows
)
431 register JSAMPROW outptr
;
432 register JSAMPROW inptr
;
433 register JDIMENSION count
;
434 register int num_comps
= cinfo
->num_components
;
435 JDIMENSION num_cols
= cinfo
->output_width
;
438 while (--num_rows
>= 0) {
439 /* It seems fastest to make a separate pass for each component. */
440 for (ci
= 0; ci
< num_comps
; ci
++) {
441 inptr
= input_buf
[ci
][input_row
];
442 outptr
= output_buf
[0] + ci
;
443 for (count
= num_cols
; count
> 0; count
--) {
444 *outptr
= *inptr
++; /* don't need GETJSAMPLE() here */
455 * Color conversion for grayscale: just copy the data.
456 * This also works for YCC -> grayscale conversion, in which
457 * we just copy the Y (luminance) component and ignore chrominance.
461 grayscale_convert (j_decompress_ptr cinfo
,
462 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
463 JSAMPARRAY output_buf
, int num_rows
)
465 jcopy_sample_rows(input_buf
[0], (int) input_row
, output_buf
, 0,
466 num_rows
, cinfo
->output_width
);
471 * Convert grayscale to RGB: just duplicate the graylevel three times.
472 * This is provided to support applications that don't want to cope
473 * with grayscale as a separate case.
477 gray_rgb_convert (j_decompress_ptr cinfo
,
478 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
479 JSAMPARRAY output_buf
, int num_rows
)
481 register JSAMPROW outptr
;
482 register JSAMPROW inptr
;
483 register JDIMENSION col
;
484 JDIMENSION num_cols
= cinfo
->output_width
;
486 while (--num_rows
>= 0) {
487 inptr
= input_buf
[0][input_row
++];
488 outptr
= *output_buf
++;
489 for (col
= 0; col
< num_cols
; col
++) {
490 /* We can dispense with GETJSAMPLE() here */
491 outptr
[RGB_RED
] = outptr
[RGB_GREEN
] = outptr
[RGB_BLUE
] = inptr
[col
];
492 outptr
+= RGB_PIXELSIZE
;
499 * Convert some rows of samples to the output colorspace.
500 * This version handles Adobe-style YCCK->CMYK conversion,
501 * where we convert YCbCr to R=1-C, G=1-M, and B=1-Y using the
502 * same conversion as above, while passing K (black) unchanged.
503 * We assume build_ycc_rgb_table has been called.
507 ycck_cmyk_convert (j_decompress_ptr cinfo
,
508 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
509 JSAMPARRAY output_buf
, int num_rows
)
511 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
512 register int y
, cb
, cr
;
513 register JSAMPROW outptr
;
514 register JSAMPROW inptr0
, inptr1
, inptr2
, inptr3
;
515 register JDIMENSION col
;
516 JDIMENSION num_cols
= cinfo
->output_width
;
517 /* copy these pointers into registers if possible */
518 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
519 register int * Crrtab
= cconvert
->Cr_r_tab
;
520 register int * Cbbtab
= cconvert
->Cb_b_tab
;
521 register INT32
* Crgtab
= cconvert
->Cr_g_tab
;
522 register INT32
* Cbgtab
= cconvert
->Cb_g_tab
;
525 while (--num_rows
>= 0) {
526 inptr0
= input_buf
[0][input_row
];
527 inptr1
= input_buf
[1][input_row
];
528 inptr2
= input_buf
[2][input_row
];
529 inptr3
= input_buf
[3][input_row
];
531 outptr
= *output_buf
++;
532 for (col
= 0; col
< num_cols
; col
++) {
533 y
= GETJSAMPLE(inptr0
[col
]);
534 cb
= GETJSAMPLE(inptr1
[col
]);
535 cr
= GETJSAMPLE(inptr2
[col
]);
536 /* Range-limiting is essential due to noise introduced by DCT losses,
537 * and for extended gamut encodings (sYCC).
539 outptr
[0] = range_limit
[MAXJSAMPLE
- (y
+ Crrtab
[cr
])]; /* red */
540 outptr
[1] = range_limit
[MAXJSAMPLE
- (y
+ /* green */
541 ((int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
],
543 outptr
[2] = range_limit
[MAXJSAMPLE
- (y
+ Cbbtab
[cb
])]; /* blue */
544 /* K passes through unchanged */
545 outptr
[3] = inptr3
[col
]; /* don't need GETJSAMPLE here */
553 * Empty method for start_pass.
557 start_pass_dcolor (j_decompress_ptr cinfo
)
564 * Module initialization routine for output colorspace conversion.
568 jinit_color_deconverter (j_decompress_ptr cinfo
)
570 my_cconvert_ptr cconvert
;
573 cconvert
= (my_cconvert_ptr
) (*cinfo
->mem
->alloc_small
)
574 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, SIZEOF(my_color_deconverter
));
575 cinfo
->cconvert
= &cconvert
->pub
;
576 cconvert
->pub
.start_pass
= start_pass_dcolor
;
578 /* Make sure num_components agrees with jpeg_color_space */
579 switch (cinfo
->jpeg_color_space
) {
581 if (cinfo
->num_components
!= 1)
582 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
589 if (cinfo
->num_components
!= 3)
590 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
595 if (cinfo
->num_components
!= 4)
596 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
599 default: /* JCS_UNKNOWN can be anything */
600 if (cinfo
->num_components
< 1)
601 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
604 /* Support color transform only for RGB colorspaces */
605 if (cinfo
->color_transform
&&
606 cinfo
->jpeg_color_space
!= JCS_RGB
&&
607 cinfo
->jpeg_color_space
!= JCS_BG_RGB
)
608 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
610 /* Set out_color_components and conversion method based on requested space.
611 * Also clear the component_needed flags for any unused components,
612 * so that earlier pipeline stages can avoid useless computation.
615 switch (cinfo
->out_color_space
) {
617 cinfo
->out_color_components
= 1;
618 switch (cinfo
->jpeg_color_space
) {
622 cconvert
->pub
.color_convert
= grayscale_convert
;
623 /* For color->grayscale conversion, only the Y (0) component is needed */
624 for (ci
= 1; ci
< cinfo
->num_components
; ci
++)
625 cinfo
->comp_info
[ci
].component_needed
= FALSE
;
628 switch (cinfo
->color_transform
) {
630 cconvert
->pub
.color_convert
= rgb_gray_convert
;
632 case JCT_SUBTRACT_GREEN
:
633 cconvert
->pub
.color_convert
= rgb1_gray_convert
;
636 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
638 build_rgb_y_table(cinfo
);
641 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
646 cinfo
->out_color_components
= RGB_PIXELSIZE
;
647 switch (cinfo
->jpeg_color_space
) {
649 cconvert
->pub
.color_convert
= gray_rgb_convert
;
652 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
653 build_ycc_rgb_table(cinfo
);
656 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
657 build_bg_ycc_rgb_table(cinfo
);
660 switch (cinfo
->color_transform
) {
662 cconvert
->pub
.color_convert
= rgb_convert
;
664 case JCT_SUBTRACT_GREEN
:
665 cconvert
->pub
.color_convert
= rgb1_rgb_convert
;
668 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
672 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
677 cinfo
->out_color_components
= RGB_PIXELSIZE
;
678 if (cinfo
->jpeg_color_space
!= JCS_BG_RGB
)
679 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
680 switch (cinfo
->color_transform
) {
682 cconvert
->pub
.color_convert
= rgb_convert
;
684 case JCT_SUBTRACT_GREEN
:
685 cconvert
->pub
.color_convert
= rgb1_rgb_convert
;
688 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
693 cinfo
->out_color_components
= 4;
694 switch (cinfo
->jpeg_color_space
) {
696 cconvert
->pub
.color_convert
= ycck_cmyk_convert
;
697 build_ycc_rgb_table(cinfo
);
700 cconvert
->pub
.color_convert
= null_convert
;
703 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
707 default: /* permit null conversion to same output space */
708 if (cinfo
->out_color_space
!= cinfo
->jpeg_color_space
)
709 /* unsupported non-null conversion */
710 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
711 cinfo
->out_color_components
= cinfo
->num_components
;
712 cconvert
->pub
.color_convert
= null_convert
;
715 if (cinfo
->quantize_colors
)
716 cinfo
->output_components
= 1; /* single colormapped output component */
718 cinfo
->output_components
= cinfo
->out_color_components
;