4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2011-2020 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 * Note: Omit uninteresting components in output buffer.
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 out_comps
= cinfo
->out_color_components
;
435 JDIMENSION num_cols
= cinfo
->output_width
;
438 jpeg_component_info
*compptr
;
440 while (--num_rows
>= 0) {
441 /* It seems fastest to make a separate pass for each component. */
442 startptr
= *output_buf
++;
443 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
445 if (! compptr
->component_needed
)
446 continue; /* skip uninteresting component */
447 inptr
= input_buf
[ci
][input_row
];
449 for (count
= num_cols
; count
> 0; count
--) {
450 *outptr
= *inptr
++; /* don't need GETJSAMPLE() here */
460 * Color conversion for grayscale: just copy the data.
461 * This also works for YCC -> grayscale conversion, in which
462 * we just copy the Y (luminance) component and ignore chrominance.
466 grayscale_convert (j_decompress_ptr cinfo
,
467 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
468 JSAMPARRAY output_buf
, int num_rows
)
470 jcopy_sample_rows(input_buf
[0] + input_row
, output_buf
,
471 num_rows
, cinfo
->output_width
);
476 * Convert grayscale to RGB: just duplicate the graylevel three times.
477 * This is provided to support applications that don't want to cope
478 * with grayscale as a separate case.
482 gray_rgb_convert (j_decompress_ptr cinfo
,
483 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
484 JSAMPARRAY output_buf
, int num_rows
)
486 register JSAMPROW outptr
;
487 register JSAMPROW inptr
;
488 register JDIMENSION col
;
489 JDIMENSION num_cols
= cinfo
->output_width
;
491 while (--num_rows
>= 0) {
492 inptr
= input_buf
[0][input_row
++];
493 outptr
= *output_buf
++;
494 for (col
= 0; col
< num_cols
; col
++) {
495 /* We can dispense with GETJSAMPLE() here */
496 outptr
[RGB_RED
] = outptr
[RGB_GREEN
] = outptr
[RGB_BLUE
] = inptr
[col
];
497 outptr
+= RGB_PIXELSIZE
;
504 * Convert some rows of samples to the output colorspace.
505 * This version handles Adobe-style YCCK->CMYK conversion,
506 * where we convert YCbCr to R=1-C, G=1-M, and B=1-Y using the
507 * same conversion as above, while passing K (black) unchanged.
508 * We assume build_ycc_rgb_table has been called.
512 ycck_cmyk_convert (j_decompress_ptr cinfo
,
513 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
514 JSAMPARRAY output_buf
, int num_rows
)
516 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
517 register int y
, cb
, cr
;
518 register JSAMPROW outptr
;
519 register JSAMPROW inptr0
, inptr1
, inptr2
, inptr3
;
520 register JDIMENSION col
;
521 JDIMENSION num_cols
= cinfo
->output_width
;
522 /* copy these pointers into registers if possible */
523 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
524 register int * Crrtab
= cconvert
->Cr_r_tab
;
525 register int * Cbbtab
= cconvert
->Cb_b_tab
;
526 register INT32
* Crgtab
= cconvert
->Cr_g_tab
;
527 register INT32
* Cbgtab
= cconvert
->Cb_g_tab
;
530 while (--num_rows
>= 0) {
531 inptr0
= input_buf
[0][input_row
];
532 inptr1
= input_buf
[1][input_row
];
533 inptr2
= input_buf
[2][input_row
];
534 inptr3
= input_buf
[3][input_row
];
536 outptr
= *output_buf
++;
537 for (col
= 0; col
< num_cols
; col
++) {
538 y
= GETJSAMPLE(inptr0
[col
]);
539 cb
= GETJSAMPLE(inptr1
[col
]);
540 cr
= GETJSAMPLE(inptr2
[col
]);
541 /* Range-limiting is essential due to noise introduced by DCT losses,
542 * and for extended gamut encodings (sYCC).
544 outptr
[0] = range_limit
[MAXJSAMPLE
- (y
+ Crrtab
[cr
])]; /* red */
545 outptr
[1] = range_limit
[MAXJSAMPLE
- (y
+ /* green */
546 ((int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
],
548 outptr
[2] = range_limit
[MAXJSAMPLE
- (y
+ Cbbtab
[cb
])]; /* blue */
549 /* K passes through unchanged */
550 outptr
[3] = inptr3
[col
]; /* don't need GETJSAMPLE here */
558 * Convert CMYK to YK part of YCCK for colorless output.
559 * We assume build_rgb_y_table has been called.
563 cmyk_yk_convert (j_decompress_ptr cinfo
,
564 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
565 JSAMPARRAY output_buf
, int num_rows
)
567 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
568 register int r
, g
, b
;
569 register INT32
* ctab
= cconvert
->rgb_y_tab
;
570 register JSAMPROW outptr
;
571 register JSAMPROW inptr0
, inptr1
, inptr2
, inptr3
;
572 register JDIMENSION col
;
573 JDIMENSION num_cols
= cinfo
->output_width
;
575 while (--num_rows
>= 0) {
576 inptr0
= input_buf
[0][input_row
];
577 inptr1
= input_buf
[1][input_row
];
578 inptr2
= input_buf
[2][input_row
];
579 inptr3
= input_buf
[3][input_row
];
581 outptr
= *output_buf
++;
582 for (col
= 0; col
< num_cols
; col
++) {
583 r
= MAXJSAMPLE
- GETJSAMPLE(inptr0
[col
]);
584 g
= MAXJSAMPLE
- GETJSAMPLE(inptr1
[col
]);
585 b
= MAXJSAMPLE
- GETJSAMPLE(inptr2
[col
]);
587 outptr
[0] = (JSAMPLE
)
588 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
590 /* K passes through unchanged */
591 outptr
[1] = inptr3
[col
]; /* don't need GETJSAMPLE here */
599 * Empty method for start_pass.
603 start_pass_dcolor (j_decompress_ptr cinfo
)
610 * Module initialization routine for output colorspace conversion.
614 jinit_color_deconverter (j_decompress_ptr cinfo
)
616 my_cconvert_ptr cconvert
;
619 cconvert
= (my_cconvert_ptr
) (*cinfo
->mem
->alloc_small
)
620 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, SIZEOF(my_color_deconverter
));
621 cinfo
->cconvert
= &cconvert
->pub
;
622 cconvert
->pub
.start_pass
= start_pass_dcolor
;
624 /* Make sure num_components agrees with jpeg_color_space */
625 switch (cinfo
->jpeg_color_space
) {
627 if (cinfo
->num_components
!= 1)
628 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
635 if (cinfo
->num_components
!= 3)
636 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
641 if (cinfo
->num_components
!= 4)
642 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
645 default: /* JCS_UNKNOWN can be anything */
646 if (cinfo
->num_components
< 1)
647 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
650 /* Support color transform only for RGB colorspaces */
651 if (cinfo
->color_transform
&&
652 cinfo
->jpeg_color_space
!= JCS_RGB
&&
653 cinfo
->jpeg_color_space
!= JCS_BG_RGB
)
654 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
656 /* Set out_color_components and conversion method based on requested space.
657 * Also adjust the component_needed flags for any unused components,
658 * so that earlier pipeline stages can avoid useless computation.
661 switch (cinfo
->out_color_space
) {
663 cinfo
->out_color_components
= 1;
664 switch (cinfo
->jpeg_color_space
) {
668 cconvert
->pub
.color_convert
= grayscale_convert
;
669 /* For color->grayscale conversion, only the Y (0) component is needed */
670 for (ci
= 1; ci
< cinfo
->num_components
; ci
++)
671 cinfo
->comp_info
[ci
].component_needed
= FALSE
;
674 switch (cinfo
->color_transform
) {
676 cconvert
->pub
.color_convert
= rgb_gray_convert
;
678 case JCT_SUBTRACT_GREEN
:
679 cconvert
->pub
.color_convert
= rgb1_gray_convert
;
682 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
684 build_rgb_y_table(cinfo
);
687 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
692 cinfo
->out_color_components
= RGB_PIXELSIZE
;
693 switch (cinfo
->jpeg_color_space
) {
695 cconvert
->pub
.color_convert
= gray_rgb_convert
;
698 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
699 build_ycc_rgb_table(cinfo
);
702 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
703 build_bg_ycc_rgb_table(cinfo
);
706 switch (cinfo
->color_transform
) {
708 cconvert
->pub
.color_convert
= rgb_convert
;
710 case JCT_SUBTRACT_GREEN
:
711 cconvert
->pub
.color_convert
= rgb1_rgb_convert
;
714 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
718 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
723 if (cinfo
->jpeg_color_space
!= JCS_BG_RGB
)
724 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
725 cinfo
->out_color_components
= RGB_PIXELSIZE
;
726 switch (cinfo
->color_transform
) {
728 cconvert
->pub
.color_convert
= rgb_convert
;
730 case JCT_SUBTRACT_GREEN
:
731 cconvert
->pub
.color_convert
= rgb1_rgb_convert
;
734 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
739 if (cinfo
->jpeg_color_space
!= JCS_YCCK
)
741 cinfo
->out_color_components
= 4;
742 cconvert
->pub
.color_convert
= ycck_cmyk_convert
;
743 build_ycc_rgb_table(cinfo
);
747 if (cinfo
->jpeg_color_space
!= JCS_CMYK
||
748 /* Support only YK part of YCCK for colorless output */
749 ! cinfo
->comp_info
[0].component_needed
||
750 cinfo
->comp_info
[1].component_needed
||
751 cinfo
->comp_info
[2].component_needed
||
752 ! cinfo
->comp_info
[3].component_needed
)
754 cinfo
->out_color_components
= 2;
755 /* Need all components on input side */
756 cinfo
->comp_info
[1].component_needed
= TRUE
;
757 cinfo
->comp_info
[2].component_needed
= TRUE
;
758 cconvert
->pub
.color_convert
= cmyk_yk_convert
;
759 build_rgb_y_table(cinfo
);
762 default: def_label
: /* permit null conversion to same output space */
763 if (cinfo
->out_color_space
!= cinfo
->jpeg_color_space
)
764 /* unsupported non-null conversion */
765 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
767 for (ci
= 0; ci
< cinfo
->num_components
; ci
++)
768 if (cinfo
->comp_info
[ci
].component_needed
)
769 i
++; /* count output color components */
770 cinfo
->out_color_components
= i
;
771 cconvert
->pub
.color_convert
= null_convert
;
774 if (cinfo
->quantize_colors
)
775 cinfo
->output_components
= 1; /* single colormapped output component */
777 cinfo
->output_components
= cinfo
->out_color_components
;