4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2011-2012 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 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 /* Private state for RGB->Y conversion */
29 INT32
* rgb_y_tab
; /* => table for RGB to Y conversion */
30 } my_color_deconverter
;
32 typedef my_color_deconverter
* my_cconvert_ptr
;
35 /**************** YCbCr -> RGB conversion: most common case **************/
36 /**************** RGB -> Y conversion: less common case **************/
39 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
40 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
41 * The conversion equations to be implemented are therefore
43 * R = Y + 1.40200 * Cr
44 * G = Y - 0.34414 * Cb - 0.71414 * Cr
45 * B = Y + 1.77200 * Cb
47 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
49 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
50 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
52 * To avoid floating-point arithmetic, we represent the fractional constants
53 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
54 * the products by 2^16, with appropriate rounding, to get the correct answer.
55 * Notice that Y, being an integral input, does not contribute any fraction
56 * so it need not participate in the rounding.
58 * For even more speed, we avoid doing any multiplications in the inner loop
59 * by precalculating the constants times Cb and Cr for all possible values.
60 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
61 * for 12-bit samples it is still acceptable. It's not very reasonable for
62 * 16-bit samples, but if you want lossless storage you shouldn't be changing
64 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
65 * values for the G calculation are left scaled up, since we must add them
66 * together before rounding.
69 #define SCALEBITS 16 /* speediest right-shift on some machines */
70 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
71 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
73 /* We allocate one big table for RGB->Y conversion and divide it up into
74 * three parts, instead of doing three alloc_small requests. This lets us
75 * use a single table base address, which can be held in a register in the
76 * inner loops on many machines (more than can hold all three addresses,
80 #define R_Y_OFF 0 /* offset to R => Y section */
81 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
82 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
83 #define TABLE_SIZE (3*(MAXJSAMPLE+1))
87 * Initialize tables for YCC->RGB colorspace conversion.
91 build_ycc_rgb_table (j_decompress_ptr cinfo
)
93 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
98 cconvert
->Cr_r_tab
= (int *)
99 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
100 (MAXJSAMPLE
+1) * SIZEOF(int));
101 cconvert
->Cb_b_tab
= (int *)
102 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
103 (MAXJSAMPLE
+1) * SIZEOF(int));
104 cconvert
->Cr_g_tab
= (INT32
*)
105 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
106 (MAXJSAMPLE
+1) * SIZEOF(INT32
));
107 cconvert
->Cb_g_tab
= (INT32
*)
108 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
109 (MAXJSAMPLE
+1) * SIZEOF(INT32
));
111 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
112 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
113 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
114 /* Cr=>R value is nearest int to 1.40200 * x */
115 cconvert
->Cr_r_tab
[i
] = (int)
116 RIGHT_SHIFT(FIX(1.40200) * x
+ ONE_HALF
, SCALEBITS
);
117 /* Cb=>B value is nearest int to 1.77200 * x */
118 cconvert
->Cb_b_tab
[i
] = (int)
119 RIGHT_SHIFT(FIX(1.77200) * x
+ ONE_HALF
, SCALEBITS
);
120 /* Cr=>G value is scaled-up -0.71414 * x */
121 cconvert
->Cr_g_tab
[i
] = (- FIX(0.71414)) * x
;
122 /* Cb=>G value is scaled-up -0.34414 * x */
123 /* We also add in ONE_HALF so that need not do it in inner loop */
124 cconvert
->Cb_g_tab
[i
] = (- FIX(0.34414)) * x
+ ONE_HALF
;
130 * Convert some rows of samples to the output colorspace.
132 * Note that we change from noninterleaved, one-plane-per-component format
133 * to interleaved-pixel format. The output buffer is therefore three times
134 * as wide as the input buffer.
135 * A starting row offset is provided only for the input buffer. The caller
136 * can easily adjust the passed output_buf value to accommodate any row
137 * offset required on that side.
141 ycc_rgb_convert (j_decompress_ptr cinfo
,
142 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
143 JSAMPARRAY output_buf
, int num_rows
)
145 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
146 register int y
, cb
, cr
;
147 register JSAMPROW outptr
;
148 register JSAMPROW inptr0
, inptr1
, inptr2
;
149 register JDIMENSION col
;
150 JDIMENSION num_cols
= cinfo
->output_width
;
151 /* copy these pointers into registers if possible */
152 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
153 register int * Crrtab
= cconvert
->Cr_r_tab
;
154 register int * Cbbtab
= cconvert
->Cb_b_tab
;
155 register INT32
* Crgtab
= cconvert
->Cr_g_tab
;
156 register INT32
* Cbgtab
= cconvert
->Cb_g_tab
;
159 while (--num_rows
>= 0) {
160 inptr0
= input_buf
[0][input_row
];
161 inptr1
= input_buf
[1][input_row
];
162 inptr2
= input_buf
[2][input_row
];
164 outptr
= *output_buf
++;
165 for (col
= 0; col
< num_cols
; col
++) {
166 y
= GETJSAMPLE(inptr0
[col
]);
167 cb
= GETJSAMPLE(inptr1
[col
]);
168 cr
= GETJSAMPLE(inptr2
[col
]);
169 /* Range-limiting is essential due to noise introduced by DCT losses. */
170 outptr
[RGB_RED
] = range_limit
[y
+ Crrtab
[cr
]];
171 outptr
[RGB_GREEN
] = range_limit
[y
+
172 ((int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
],
174 outptr
[RGB_BLUE
] = range_limit
[y
+ Cbbtab
[cb
]];
175 outptr
+= RGB_PIXELSIZE
;
181 /**************** Cases other than YCbCr -> RGB **************/
185 * Initialize for RGB->grayscale colorspace conversion.
189 build_rgb_y_table (j_decompress_ptr cinfo
)
191 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
195 /* Allocate and fill in the conversion tables. */
196 cconvert
->rgb_y_tab
= rgb_y_tab
= (INT32
*)
197 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
198 (TABLE_SIZE
* SIZEOF(INT32
)));
200 for (i
= 0; i
<= MAXJSAMPLE
; i
++) {
201 rgb_y_tab
[i
+R_Y_OFF
] = FIX(0.29900) * i
;
202 rgb_y_tab
[i
+G_Y_OFF
] = FIX(0.58700) * i
;
203 rgb_y_tab
[i
+B_Y_OFF
] = FIX(0.11400) * i
+ ONE_HALF
;
209 * Convert RGB to grayscale.
213 rgb_gray_convert (j_decompress_ptr cinfo
,
214 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
215 JSAMPARRAY output_buf
, int num_rows
)
217 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
218 register INT32
* ctab
= cconvert
->rgb_y_tab
;
219 register int r
, g
, b
;
220 register JSAMPROW outptr
;
221 register JSAMPROW inptr0
, inptr1
, inptr2
;
222 register JDIMENSION col
;
223 JDIMENSION num_cols
= cinfo
->output_width
;
225 while (--num_rows
>= 0) {
226 inptr0
= input_buf
[0][input_row
];
227 inptr1
= input_buf
[1][input_row
];
228 inptr2
= input_buf
[2][input_row
];
230 outptr
= *output_buf
++;
231 for (col
= 0; col
< num_cols
; col
++) {
232 r
= GETJSAMPLE(inptr0
[col
]);
233 g
= GETJSAMPLE(inptr1
[col
]);
234 b
= GETJSAMPLE(inptr2
[col
]);
236 outptr
[col
] = (JSAMPLE
)
237 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
245 * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
246 * (inverse color transform).
250 rgb1_rgb_convert (j_decompress_ptr cinfo
,
251 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
252 JSAMPARRAY output_buf
, int num_rows
)
254 register int r
, g
, b
;
255 register JSAMPROW outptr
;
256 register JSAMPROW inptr0
, inptr1
, inptr2
;
257 register JDIMENSION col
;
258 JDIMENSION num_cols
= cinfo
->output_width
;
260 while (--num_rows
>= 0) {
261 inptr0
= input_buf
[0][input_row
];
262 inptr1
= input_buf
[1][input_row
];
263 inptr2
= input_buf
[2][input_row
];
265 outptr
= *output_buf
++;
266 for (col
= 0; col
< num_cols
; col
++) {
267 r
= GETJSAMPLE(inptr0
[col
]);
268 g
= GETJSAMPLE(inptr1
[col
]);
269 b
= GETJSAMPLE(inptr2
[col
]);
270 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
271 * (modulo) operator is equivalent to the bitmask operator AND.
273 outptr
[RGB_RED
] = (JSAMPLE
) ((r
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
);
274 outptr
[RGB_GREEN
] = (JSAMPLE
) g
;
275 outptr
[RGB_BLUE
] = (JSAMPLE
) ((b
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
);
276 outptr
+= RGB_PIXELSIZE
;
283 * [R-G,G,B-G] to grayscale conversion with modulo calculation
284 * (inverse color transform).
288 rgb1_gray_convert (j_decompress_ptr cinfo
,
289 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
290 JSAMPARRAY output_buf
, int num_rows
)
292 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
293 register INT32
* ctab
= cconvert
->rgb_y_tab
;
294 register int r
, g
, b
;
295 register JSAMPROW outptr
;
296 register JSAMPROW inptr0
, inptr1
, inptr2
;
297 register JDIMENSION col
;
298 JDIMENSION num_cols
= cinfo
->output_width
;
300 while (--num_rows
>= 0) {
301 inptr0
= input_buf
[0][input_row
];
302 inptr1
= input_buf
[1][input_row
];
303 inptr2
= input_buf
[2][input_row
];
305 outptr
= *output_buf
++;
306 for (col
= 0; col
< num_cols
; col
++) {
307 r
= GETJSAMPLE(inptr0
[col
]);
308 g
= GETJSAMPLE(inptr1
[col
]);
309 b
= GETJSAMPLE(inptr2
[col
]);
310 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
311 * (modulo) operator is equivalent to the bitmask operator AND.
313 r
= (r
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
;
314 b
= (b
+ g
- CENTERJSAMPLE
) & MAXJSAMPLE
;
316 outptr
[col
] = (JSAMPLE
)
317 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
325 * No colorspace change, but conversion from separate-planes
326 * to interleaved representation.
330 rgb_convert (j_decompress_ptr cinfo
,
331 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
332 JSAMPARRAY output_buf
, int num_rows
)
334 register JSAMPROW outptr
;
335 register JSAMPROW inptr0
, inptr1
, inptr2
;
336 register JDIMENSION col
;
337 JDIMENSION num_cols
= cinfo
->output_width
;
339 while (--num_rows
>= 0) {
340 inptr0
= input_buf
[0][input_row
];
341 inptr1
= input_buf
[1][input_row
];
342 inptr2
= input_buf
[2][input_row
];
344 outptr
= *output_buf
++;
345 for (col
= 0; col
< num_cols
; col
++) {
346 /* We can dispense with GETJSAMPLE() here */
347 outptr
[RGB_RED
] = inptr0
[col
];
348 outptr
[RGB_GREEN
] = inptr1
[col
];
349 outptr
[RGB_BLUE
] = inptr2
[col
];
350 outptr
+= RGB_PIXELSIZE
;
357 * Color conversion for no colorspace change: just copy the data,
358 * converting from separate-planes to interleaved representation.
362 null_convert (j_decompress_ptr cinfo
,
363 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
364 JSAMPARRAY output_buf
, int num_rows
)
367 register int nc
= cinfo
->num_components
;
368 register JSAMPROW outptr
;
369 register JSAMPROW inptr
;
370 register JDIMENSION col
;
371 JDIMENSION num_cols
= cinfo
->output_width
;
373 while (--num_rows
>= 0) {
374 for (ci
= 0; ci
< nc
; ci
++) {
375 inptr
= input_buf
[ci
][input_row
];
376 outptr
= output_buf
[0] + ci
;
377 for (col
= 0; col
< num_cols
; col
++) {
378 *outptr
= *inptr
++; /* needn't bother with GETJSAMPLE() here */
389 * Color conversion for grayscale: just copy the data.
390 * This also works for YCbCr -> grayscale conversion, in which
391 * we just copy the Y (luminance) component and ignore chrominance.
395 grayscale_convert (j_decompress_ptr cinfo
,
396 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
397 JSAMPARRAY output_buf
, int num_rows
)
399 jcopy_sample_rows(input_buf
[0], (int) input_row
, output_buf
, 0,
400 num_rows
, cinfo
->output_width
);
405 * Convert grayscale to RGB: just duplicate the graylevel three times.
406 * This is provided to support applications that don't want to cope
407 * with grayscale as a separate case.
411 gray_rgb_convert (j_decompress_ptr cinfo
,
412 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
413 JSAMPARRAY output_buf
, int num_rows
)
415 register JSAMPROW outptr
;
416 register JSAMPROW inptr
;
417 register JDIMENSION col
;
418 JDIMENSION num_cols
= cinfo
->output_width
;
420 while (--num_rows
>= 0) {
421 inptr
= input_buf
[0][input_row
++];
422 outptr
= *output_buf
++;
423 for (col
= 0; col
< num_cols
; col
++) {
424 /* We can dispense with GETJSAMPLE() here */
425 outptr
[RGB_RED
] = outptr
[RGB_GREEN
] = outptr
[RGB_BLUE
] = inptr
[col
];
426 outptr
+= RGB_PIXELSIZE
;
433 * Adobe-style YCCK->CMYK conversion.
434 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
435 * conversion as above, while passing K (black) unchanged.
436 * We assume build_ycc_rgb_table has been called.
440 ycck_cmyk_convert (j_decompress_ptr cinfo
,
441 JSAMPIMAGE input_buf
, JDIMENSION input_row
,
442 JSAMPARRAY output_buf
, int num_rows
)
444 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
445 register int y
, cb
, cr
;
446 register JSAMPROW outptr
;
447 register JSAMPROW inptr0
, inptr1
, inptr2
, inptr3
;
448 register JDIMENSION col
;
449 JDIMENSION num_cols
= cinfo
->output_width
;
450 /* copy these pointers into registers if possible */
451 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
452 register int * Crrtab
= cconvert
->Cr_r_tab
;
453 register int * Cbbtab
= cconvert
->Cb_b_tab
;
454 register INT32
* Crgtab
= cconvert
->Cr_g_tab
;
455 register INT32
* Cbgtab
= cconvert
->Cb_g_tab
;
458 while (--num_rows
>= 0) {
459 inptr0
= input_buf
[0][input_row
];
460 inptr1
= input_buf
[1][input_row
];
461 inptr2
= input_buf
[2][input_row
];
462 inptr3
= input_buf
[3][input_row
];
464 outptr
= *output_buf
++;
465 for (col
= 0; col
< num_cols
; col
++) {
466 y
= GETJSAMPLE(inptr0
[col
]);
467 cb
= GETJSAMPLE(inptr1
[col
]);
468 cr
= GETJSAMPLE(inptr2
[col
]);
469 /* Range-limiting is essential due to noise introduced by DCT losses. */
470 outptr
[0] = range_limit
[MAXJSAMPLE
- (y
+ Crrtab
[cr
])]; /* red */
471 outptr
[1] = range_limit
[MAXJSAMPLE
- (y
+ /* green */
472 ((int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
],
474 outptr
[2] = range_limit
[MAXJSAMPLE
- (y
+ Cbbtab
[cb
])]; /* blue */
475 /* K passes through unchanged */
476 outptr
[3] = inptr3
[col
]; /* don't need GETJSAMPLE here */
484 * Empty method for start_pass.
488 start_pass_dcolor (j_decompress_ptr cinfo
)
495 * Module initialization routine for output colorspace conversion.
499 jinit_color_deconverter (j_decompress_ptr cinfo
)
501 my_cconvert_ptr cconvert
;
504 cconvert
= (my_cconvert_ptr
)
505 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
506 SIZEOF(my_color_deconverter
));
507 cinfo
->cconvert
= &cconvert
->pub
;
508 cconvert
->pub
.start_pass
= start_pass_dcolor
;
510 /* Make sure num_components agrees with jpeg_color_space */
511 switch (cinfo
->jpeg_color_space
) {
513 if (cinfo
->num_components
!= 1)
514 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
519 if (cinfo
->num_components
!= 3)
520 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
525 if (cinfo
->num_components
!= 4)
526 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
529 default: /* JCS_UNKNOWN can be anything */
530 if (cinfo
->num_components
< 1)
531 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
535 /* Support color transform only for RGB colorspace */
536 if (cinfo
->color_transform
&& cinfo
->jpeg_color_space
!= JCS_RGB
)
537 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
539 /* Set out_color_components and conversion method based on requested space.
540 * Also clear the component_needed flags for any unused components,
541 * so that earlier pipeline stages can avoid useless computation.
544 switch (cinfo
->out_color_space
) {
546 cinfo
->out_color_components
= 1;
547 if (cinfo
->jpeg_color_space
== JCS_GRAYSCALE
||
548 cinfo
->jpeg_color_space
== JCS_YCbCr
) {
549 cconvert
->pub
.color_convert
= grayscale_convert
;
550 /* For color->grayscale conversion, only the Y (0) component is needed */
551 for (ci
= 1; ci
< cinfo
->num_components
; ci
++)
552 cinfo
->comp_info
[ci
].component_needed
= FALSE
;
553 } else if (cinfo
->jpeg_color_space
== JCS_RGB
) {
554 switch (cinfo
->color_transform
) {
556 cconvert
->pub
.color_convert
= rgb_gray_convert
;
558 case JCT_SUBTRACT_GREEN
:
559 cconvert
->pub
.color_convert
= rgb1_gray_convert
;
562 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
565 build_rgb_y_table(cinfo
);
567 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
571 cinfo
->out_color_components
= RGB_PIXELSIZE
;
572 if (cinfo
->jpeg_color_space
== JCS_YCbCr
) {
573 cconvert
->pub
.color_convert
= ycc_rgb_convert
;
574 build_ycc_rgb_table(cinfo
);
575 } else if (cinfo
->jpeg_color_space
== JCS_GRAYSCALE
) {
576 cconvert
->pub
.color_convert
= gray_rgb_convert
;
577 } else if (cinfo
->jpeg_color_space
== JCS_RGB
) {
578 switch (cinfo
->color_transform
) {
580 cconvert
->pub
.color_convert
= rgb_convert
;
582 case JCT_SUBTRACT_GREEN
:
583 cconvert
->pub
.color_convert
= rgb1_rgb_convert
;
586 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
590 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
594 cinfo
->out_color_components
= 4;
595 if (cinfo
->jpeg_color_space
== JCS_YCCK
) {
596 cconvert
->pub
.color_convert
= ycck_cmyk_convert
;
597 build_ycc_rgb_table(cinfo
);
598 } else if (cinfo
->jpeg_color_space
== JCS_CMYK
) {
599 cconvert
->pub
.color_convert
= null_convert
;
601 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
605 /* Permit null conversion to same output space */
606 if (cinfo
->out_color_space
== cinfo
->jpeg_color_space
) {
607 cinfo
->out_color_components
= cinfo
->num_components
;
608 cconvert
->pub
.color_convert
= null_convert
;
609 } else /* unsupported non-null conversion */
610 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
614 if (cinfo
->quantize_colors
)
615 cinfo
->output_components
= 1; /* single colormapped output component */
617 cinfo
->output_components
= cinfo
->out_color_components
;