8 * Copyright (C) 1991-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains input colorspace conversion routines.
15 #define JPEG_INTERNALS
20 /* Private subobject */
23 struct jpeg_color_converter pub
; /* public fields */
25 /* Private state for RGB->YCC conversion */
26 INT32
* rgb_ycc_tab
; /* => table for RGB to YCbCr conversion */
29 typedef my_color_converter
* my_cconvert_ptr
;
32 /**************** RGB -> YCbCr conversion: most common case **************/
35 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
36 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
37 * The conversion equations to be implemented are therefore
38 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
39 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
40 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
41 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
42 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
43 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
44 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
45 * were not represented exactly. Now we sacrifice exact representation of
46 * maximum red and maximum blue in order to get exact grayscales.
48 * To avoid floating-point arithmetic, we represent the fractional constants
49 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
50 * the products by 2^16, with appropriate rounding, to get the correct answer.
52 * For even more speed, we avoid doing any multiplications in the inner loop
53 * by precalculating the constants times R,G,B for all possible values.
54 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
55 * for 12-bit samples it is still acceptable. It's not very reasonable for
56 * 16-bit samples, but if you want lossless storage you shouldn't be changing
58 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
59 * in the tables to save adding them separately in the inner loop.
62 #define SCALEBITS 16 /* speediest right-shift on some machines */
63 #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
64 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
65 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
67 /* We allocate one big table and divide it up into eight parts, instead of
68 * doing eight alloc_small requests. This lets us use a single table base
69 * address, which can be held in a register in the inner loops on many
70 * machines (more than can hold all eight addresses, anyway).
73 #define R_Y_OFF 0 /* offset to R => Y section */
74 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
75 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
76 #define R_CB_OFF (3*(MAXJSAMPLE+1))
77 #define G_CB_OFF (4*(MAXJSAMPLE+1))
78 #define B_CB_OFF (5*(MAXJSAMPLE+1))
79 #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
80 #define G_CR_OFF (6*(MAXJSAMPLE+1))
81 #define B_CR_OFF (7*(MAXJSAMPLE+1))
82 #define TABLE_SIZE (8*(MAXJSAMPLE+1))
86 * Initialize for RGB->YCC colorspace conversion.
90 rgb_ycc_start (j_compress_ptr cinfo
)
92 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
96 /* Allocate and fill in the conversion tables. */
97 cconvert
->rgb_ycc_tab
= rgb_ycc_tab
= (INT32
*)
98 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
99 (TABLE_SIZE
* SIZEOF(INT32
)));
101 for (i
= 0; i
<= MAXJSAMPLE
; i
++) {
102 rgb_ycc_tab
[i
+R_Y_OFF
] = FIX(0.29900) * i
;
103 rgb_ycc_tab
[i
+G_Y_OFF
] = FIX(0.58700) * i
;
104 rgb_ycc_tab
[i
+B_Y_OFF
] = FIX(0.11400) * i
+ ONE_HALF
;
105 rgb_ycc_tab
[i
+R_CB_OFF
] = (-FIX(0.16874)) * i
;
106 rgb_ycc_tab
[i
+G_CB_OFF
] = (-FIX(0.33126)) * i
;
107 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
108 * This ensures that the maximum output will round to MAXJSAMPLE
109 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
111 rgb_ycc_tab
[i
+B_CB_OFF
] = FIX(0.50000) * i
+ CBCR_OFFSET
+ ONE_HALF
-1;
112 /* B=>Cb and R=>Cr tables are the same
113 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
115 rgb_ycc_tab
[i
+G_CR_OFF
] = (-FIX(0.41869)) * i
;
116 rgb_ycc_tab
[i
+B_CR_OFF
] = (-FIX(0.08131)) * i
;
122 * Convert some rows of samples to the JPEG colorspace.
124 * Note that we change from the application's interleaved-pixel format
125 * to our internal noninterleaved, one-plane-per-component format.
126 * The input buffer is therefore three times as wide as the output buffer.
128 * A starting row offset is provided only for the output buffer. The caller
129 * can easily adjust the passed input_buf value to accommodate any row
130 * offset required on that side.
134 rgb_ycc_convert (j_compress_ptr cinfo
,
135 JSAMPARRAY input_buf
, JSAMPIMAGE output_buf
,
136 JDIMENSION output_row
, int num_rows
)
138 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
139 register int r
, g
, b
;
140 register INT32
* ctab
= cconvert
->rgb_ycc_tab
;
141 register JSAMPROW inptr
;
142 register JSAMPROW outptr0
, outptr1
, outptr2
;
143 register JDIMENSION col
;
144 JDIMENSION num_cols
= cinfo
->image_width
;
146 while (--num_rows
>= 0) {
147 inptr
= *input_buf
++;
148 outptr0
= output_buf
[0][output_row
];
149 outptr1
= output_buf
[1][output_row
];
150 outptr2
= output_buf
[2][output_row
];
152 for (col
= 0; col
< num_cols
; col
++) {
153 r
= GETJSAMPLE(inptr
[RGB_RED
]);
154 g
= GETJSAMPLE(inptr
[RGB_GREEN
]);
155 b
= GETJSAMPLE(inptr
[RGB_BLUE
]);
156 inptr
+= RGB_PIXELSIZE
;
157 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
158 * must be too; we do not need an explicit range-limiting operation.
159 * Hence the value being shifted is never negative, and we don't
160 * need the general RIGHT_SHIFT macro.
163 outptr0
[col
] = (JSAMPLE
)
164 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
167 outptr1
[col
] = (JSAMPLE
)
168 ((ctab
[r
+R_CB_OFF
] + ctab
[g
+G_CB_OFF
] + ctab
[b
+B_CB_OFF
])
171 outptr2
[col
] = (JSAMPLE
)
172 ((ctab
[r
+R_CR_OFF
] + ctab
[g
+G_CR_OFF
] + ctab
[b
+B_CR_OFF
])
179 /**************** Cases other than RGB -> YCbCr **************/
183 * Convert some rows of samples to the JPEG colorspace.
184 * This version handles RGB->grayscale conversion, which is the same
185 * as the RGB->Y portion of RGB->YCbCr.
186 * We assume rgb_ycc_start has been called (we only use the Y tables).
190 rgb_gray_convert (j_compress_ptr cinfo
,
191 JSAMPARRAY input_buf
, JSAMPIMAGE output_buf
,
192 JDIMENSION output_row
, int num_rows
)
194 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
195 register int r
, g
, b
;
196 register INT32
* ctab
= cconvert
->rgb_ycc_tab
;
197 register JSAMPROW inptr
;
198 register JSAMPROW outptr
;
199 register JDIMENSION col
;
200 JDIMENSION num_cols
= cinfo
->image_width
;
202 while (--num_rows
>= 0) {
203 inptr
= *input_buf
++;
204 outptr
= output_buf
[0][output_row
];
206 for (col
= 0; col
< num_cols
; col
++) {
207 r
= GETJSAMPLE(inptr
[RGB_RED
]);
208 g
= GETJSAMPLE(inptr
[RGB_GREEN
]);
209 b
= GETJSAMPLE(inptr
[RGB_BLUE
]);
210 inptr
+= RGB_PIXELSIZE
;
212 outptr
[col
] = (JSAMPLE
)
213 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
221 * Convert some rows of samples to the JPEG colorspace.
222 * This version handles Adobe-style CMYK->YCCK conversion,
223 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
224 * conversion as above, while passing K (black) unchanged.
225 * We assume rgb_ycc_start has been called.
229 cmyk_ycck_convert (j_compress_ptr cinfo
,
230 JSAMPARRAY input_buf
, JSAMPIMAGE output_buf
,
231 JDIMENSION output_row
, int num_rows
)
233 my_cconvert_ptr cconvert
= (my_cconvert_ptr
) cinfo
->cconvert
;
234 register int r
, g
, b
;
235 register INT32
* ctab
= cconvert
->rgb_ycc_tab
;
236 register JSAMPROW inptr
;
237 register JSAMPROW outptr0
, outptr1
, outptr2
, outptr3
;
238 register JDIMENSION col
;
239 JDIMENSION num_cols
= cinfo
->image_width
;
241 while (--num_rows
>= 0) {
242 inptr
= *input_buf
++;
243 outptr0
= output_buf
[0][output_row
];
244 outptr1
= output_buf
[1][output_row
];
245 outptr2
= output_buf
[2][output_row
];
246 outptr3
= output_buf
[3][output_row
];
248 for (col
= 0; col
< num_cols
; col
++) {
249 r
= MAXJSAMPLE
- GETJSAMPLE(inptr
[0]);
250 g
= MAXJSAMPLE
- GETJSAMPLE(inptr
[1]);
251 b
= MAXJSAMPLE
- GETJSAMPLE(inptr
[2]);
252 /* K passes through as-is */
253 outptr3
[col
] = inptr
[3]; /* don't need GETJSAMPLE here */
255 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
256 * must be too; we do not need an explicit range-limiting operation.
257 * Hence the value being shifted is never negative, and we don't
258 * need the general RIGHT_SHIFT macro.
261 outptr0
[col
] = (JSAMPLE
)
262 ((ctab
[r
+R_Y_OFF
] + ctab
[g
+G_Y_OFF
] + ctab
[b
+B_Y_OFF
])
265 outptr1
[col
] = (JSAMPLE
)
266 ((ctab
[r
+R_CB_OFF
] + ctab
[g
+G_CB_OFF
] + ctab
[b
+B_CB_OFF
])
269 outptr2
[col
] = (JSAMPLE
)
270 ((ctab
[r
+R_CR_OFF
] + ctab
[g
+G_CR_OFF
] + ctab
[b
+B_CR_OFF
])
278 * Convert some rows of samples to the JPEG colorspace.
279 * This version handles grayscale output with no conversion.
280 * The source can be either plain grayscale or YCbCr (since Y == gray).
284 grayscale_convert (j_compress_ptr cinfo
,
285 JSAMPARRAY input_buf
, JSAMPIMAGE output_buf
,
286 JDIMENSION output_row
, int num_rows
)
288 register JSAMPROW inptr
;
289 register JSAMPROW outptr
;
290 register JDIMENSION col
;
291 JDIMENSION num_cols
= cinfo
->image_width
;
292 int instride
= cinfo
->input_components
;
294 while (--num_rows
>= 0) {
295 inptr
= *input_buf
++;
296 outptr
= output_buf
[0][output_row
];
298 for (col
= 0; col
< num_cols
; col
++) {
299 outptr
[col
] = inptr
[0]; /* don't need GETJSAMPLE() here */
307 * Convert some rows of samples to the JPEG colorspace.
308 * This version handles multi-component colorspaces without conversion.
309 * We assume input_components == num_components.
313 null_convert (j_compress_ptr cinfo
,
314 JSAMPARRAY input_buf
, JSAMPIMAGE output_buf
,
315 JDIMENSION output_row
, int num_rows
)
317 register JSAMPROW inptr
;
318 register JSAMPROW outptr
;
319 register JDIMENSION col
;
321 int nc
= cinfo
->num_components
;
322 JDIMENSION num_cols
= cinfo
->image_width
;
324 while (--num_rows
>= 0) {
325 /* It seems fastest to make a separate pass for each component. */
326 for (ci
= 0; ci
< nc
; ci
++) {
328 outptr
= output_buf
[ci
][output_row
];
329 for (col
= 0; col
< num_cols
; col
++) {
330 outptr
[col
] = inptr
[ci
]; /* don't need GETJSAMPLE() here */
341 * Empty method for start_pass.
345 null_method (j_compress_ptr cinfo
)
352 * Module initialization routine for input colorspace conversion.
356 jinit_color_converter (j_compress_ptr cinfo
)
358 my_cconvert_ptr cconvert
;
360 cconvert
= (my_cconvert_ptr
)
361 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
362 SIZEOF(my_color_converter
));
363 cinfo
->cconvert
= (struct jpeg_color_converter
*) cconvert
;
364 /* set start_pass to null method until we find out differently */
365 cconvert
->pub
.start_pass
= null_method
;
367 /* Make sure input_components agrees with in_color_space */
368 switch (cinfo
->in_color_space
) {
370 if (cinfo
->input_components
!= 1)
371 ERREXIT(cinfo
, JERR_BAD_IN_COLORSPACE
);
375 #if RGB_PIXELSIZE != 3
376 if (cinfo
->input_components
!= RGB_PIXELSIZE
)
377 ERREXIT(cinfo
, JERR_BAD_IN_COLORSPACE
);
379 #endif /* else share code with YCbCr */
382 if (cinfo
->input_components
!= 3)
383 ERREXIT(cinfo
, JERR_BAD_IN_COLORSPACE
);
388 if (cinfo
->input_components
!= 4)
389 ERREXIT(cinfo
, JERR_BAD_IN_COLORSPACE
);
392 default: /* JCS_UNKNOWN can be anything */
393 if (cinfo
->input_components
< 1)
394 ERREXIT(cinfo
, JERR_BAD_IN_COLORSPACE
);
398 /* Check num_components, set conversion method based on requested space */
399 switch (cinfo
->jpeg_color_space
) {
401 if (cinfo
->num_components
!= 1)
402 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
403 if (cinfo
->in_color_space
== JCS_GRAYSCALE
)
404 cconvert
->pub
.color_convert
= grayscale_convert
;
405 else if (cinfo
->in_color_space
== JCS_RGB
) {
406 cconvert
->pub
.start_pass
= rgb_ycc_start
;
407 cconvert
->pub
.color_convert
= rgb_gray_convert
;
408 } else if (cinfo
->in_color_space
== JCS_YCbCr
)
409 cconvert
->pub
.color_convert
= grayscale_convert
;
411 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
415 if (cinfo
->num_components
!= 3)
416 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
417 if (cinfo
->in_color_space
== JCS_RGB
&& RGB_PIXELSIZE
== 3)
418 cconvert
->pub
.color_convert
= null_convert
;
420 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
424 if (cinfo
->num_components
!= 3)
425 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
426 if (cinfo
->in_color_space
== JCS_RGB
) {
427 cconvert
->pub
.start_pass
= rgb_ycc_start
;
428 cconvert
->pub
.color_convert
= rgb_ycc_convert
;
429 } else if (cinfo
->in_color_space
== JCS_YCbCr
)
430 cconvert
->pub
.color_convert
= null_convert
;
432 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
436 if (cinfo
->num_components
!= 4)
437 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
438 if (cinfo
->in_color_space
== JCS_CMYK
)
439 cconvert
->pub
.color_convert
= null_convert
;
441 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
445 if (cinfo
->num_components
!= 4)
446 ERREXIT(cinfo
, JERR_BAD_J_COLORSPACE
);
447 if (cinfo
->in_color_space
== JCS_CMYK
) {
448 cconvert
->pub
.start_pass
= rgb_ycc_start
;
449 cconvert
->pub
.color_convert
= cmyk_ycck_convert
;
450 } else if (cinfo
->in_color_space
== JCS_YCCK
)
451 cconvert
->pub
.color_convert
= null_convert
;
453 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
456 default: /* allow null conversion of JCS_UNKNOWN */
457 if (cinfo
->jpeg_color_space
!= cinfo
->in_color_space
||
458 cinfo
->num_components
!= cinfo
->input_components
)
459 ERREXIT(cinfo
, JERR_CONVERSION_NOTIMPL
);
460 cconvert
->pub
.color_convert
= null_convert
;