Debian Codegraph @

frmts/jpeg/libjpeg/jdcolor.c

raw
1 /*
2 * jdcolor.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains output colorspace conversion routines.
9 */
10
11 #define JPEG_INTERNALS
12 #include "jinclude.h"
13 #include "jpeglib.h"
14
15 #include "cpl_port.h"
16
17 /* Private subobject */
18
19 typedef struct {
20 struct jpeg_color_deconverter pub; /* public fields */
21
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 */
27 } my_color_deconverter;
28
29 typedef my_color_deconverter * my_cconvert_ptr;
30
31
32 /**************** YCbCr -> RGB conversion: most common case **************/
33
34 /*
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 * R = Y + 1.40200 * Cr
39 * G = Y - 0.34414 * Cb - 0.71414 * Cr
40 * B = Y + 1.77200 * Cb
41 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
42 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
43 *
44 * To avoid floating-point arithmetic, we represent the fractional constants
45 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
46 * the products by 2^16, with appropriate rounding, to get the correct answer.
47 * Notice that Y, being an integral input, does not contribute any fraction
48 * so it need not participate in the rounding.
49 *
50 * For even more speed, we avoid doing any multiplications in the inner loop
51 * by precalculating the constants times Cb and Cr for all possible values.
52 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
53 * for 12-bit samples it is still acceptable. It's not very reasonable for
54 * 16-bit samples, but if you want lossless storage you shouldn't be changing
55 * colorspace anyway.
56 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
57 * values for the G calculation are left scaled up, since we must add them
58 * together before rounding.
59 */
60
61 #define SCALEBITS 16 /* speediest right-shift on some machines */
62 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
63 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
64
65
66 /*
67 * Initialize tables for YCC->RGB colorspace conversion.
68 */
69
70 LOCAL(void)
71 build_ycc_rgb_table (j_decompress_ptr cinfo)
72 {
73 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
74 int i;
75 INT32 x;
76 SHIFT_TEMPS
77
78 cconvert->Cr_r_tab = (int *)
79 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
80 (MAXJSAMPLE+1) * SIZEOF(int));
81 cconvert->Cb_b_tab = (int *)
82 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
83 (MAXJSAMPLE+1) * SIZEOF(int));
84 cconvert->Cr_g_tab = (INT32 *)
85 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
86 (MAXJSAMPLE+1) * SIZEOF(INT32));
87 cconvert->Cb_g_tab = (INT32 *)
88 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
89 (MAXJSAMPLE+1) * SIZEOF(INT32));
90
91 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
92 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
93 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
94 /* Cr=>R value is nearest int to 1.40200 * x */
95 cconvert->Cr_r_tab[i] = (int)
96 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
97 /* Cb=>B value is nearest int to 1.77200 * x */
98 cconvert->Cb_b_tab[i] = (int)
99 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
100 /* Cr=>G value is scaled-up -0.71414 * x */
101 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
102 /* Cb=>G value is scaled-up -0.34414 * x */
103 /* We also add in ONE_HALF so that need not do it in inner loop */
104 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
105 }
106 }
107
108
109 /*
110 * Convert some rows of samples to the output colorspace.
111 *
112 * Note that we change from noninterleaved, one-plane-per-component format
113 * to interleaved-pixel format. The output buffer is therefore three times
114 * as wide as the input buffer.
115 * A starting row offset is provided only for the input buffer. The caller
116 * can easily adjust the passed output_buf value to accommodate any row
117 * offset required on that side.
118 */
119
120 METHODDEF(void)
121 ycc_rgb_convert (j_decompress_ptr cinfo,
122 JSAMPIMAGE input_buf, JDIMENSION input_row,
123 JSAMPARRAY output_buf, int num_rows)
124 {
125 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
126 register int y, cb, cr;
127 register JSAMPROW outptr;
128 register JSAMPROW inptr0, inptr1, inptr2;
129 register JDIMENSION col;
130 JDIMENSION num_cols = cinfo->output_width;
131 /* copy these pointers into registers if possible */
132 register JSAMPLE * range_limit = cinfo->sample_range_limit;
133 register int * Crrtab = cconvert->Cr_r_tab;
134 register int * Cbbtab = cconvert->Cb_b_tab;
135 register INT32 * Crgtab = cconvert->Cr_g_tab;
136 register INT32 * Cbgtab = cconvert->Cb_g_tab;
137 SHIFT_TEMPS
138
139 while (--num_rows >= 0) {
140 inptr0 = input_buf[0][input_row];
141 inptr1 = input_buf[1][input_row];
142 inptr2 = input_buf[2][input_row];
143 input_row++;
144 outptr = *output_buf++;
145 for (col = 0; col < num_cols; col++) {
146 y = GETJSAMPLE(inptr0[col]);
147 cb = GETJSAMPLE(inptr1[col]);
148 cr = GETJSAMPLE(inptr2[col]);
149 /* Range-limiting is essential due to noise introduced by DCT losses. */
150 outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
151 outptr[RGB_GREEN] = range_limit[y +
152 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
153 SCALEBITS))];
154 outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
155 outptr += RGB_PIXELSIZE;
156 }
157 }
158 }
159
160
161 /**************** Cases other than YCbCr -> RGB **************/
162
163
164 /*
165 * Color conversion for no colorspace change: just copy the data,
166 * converting from separate-planes to interleaved representation.
167 */
168
169 METHODDEF(void)
170 null_convert (j_decompress_ptr cinfo,
171 JSAMPIMAGE input_buf, JDIMENSION input_row,
172 JSAMPARRAY output_buf, int num_rows)
173 {
174 register JSAMPROW inptr, outptr;
175 register JDIMENSION count;
176 register int num_components = cinfo->num_components;
177 JDIMENSION num_cols = cinfo->output_width;
178 int ci;
179
180 while (--num_rows >= 0) {
181 for (ci = 0; ci < num_components; ci++) {
182 inptr = input_buf[ci][input_row];
183 outptr = output_buf[0] + ci;
184 for (count = num_cols; count > 0; count--) {
185 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
186 outptr += num_components;
187 }
188 }
189 input_row++;
190 output_buf++;
191 }
192 }
193
194
195 /*
196 * Color conversion for grayscale: just copy the data.
197 * This also works for YCbCr -> grayscale conversion, in which
198 * we just copy the Y (luminance) component and ignore chrominance.
199 */
200
201 METHODDEF(void)
202 grayscale_convert (j_decompress_ptr cinfo,
203 JSAMPIMAGE input_buf, JDIMENSION input_row,
204 JSAMPARRAY output_buf, int num_rows)
205 {
206 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
207 num_rows, cinfo->output_width);
208 }
209
210
211 /*
212 * Convert grayscale to RGB: just duplicate the graylevel three times.
213 * This is provided to support applications that don't want to cope
214 * with grayscale as a separate case.
215 */
216
217 METHODDEF(void)
218 gray_rgb_convert (j_decompress_ptr cinfo,
219 JSAMPIMAGE input_buf, JDIMENSION input_row,
220 JSAMPARRAY output_buf, int num_rows)
221 {
222 register JSAMPROW inptr, outptr;
223 register JDIMENSION col;
224 JDIMENSION num_cols = cinfo->output_width;
225
226 while (--num_rows >= 0) {
227 inptr = input_buf[0][input_row++];
228 outptr = *output_buf++;
229 for (col = 0; col < num_cols; col++) {
230 /* We can dispense with GETJSAMPLE() here */
231 outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
232 outptr += RGB_PIXELSIZE;
233 }
234 }
235 }
236
237
238 /*
239 * Adobe-style YCCK->CMYK conversion.
240 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
241 * conversion as above, while passing K (black) unchanged.
242 * We assume build_ycc_rgb_table has been called.
243 */
244
245 METHODDEF(void)
246 ycck_cmyk_convert (j_decompress_ptr cinfo,
247 JSAMPIMAGE input_buf, JDIMENSION input_row,
248 JSAMPARRAY output_buf, int num_rows)
249 {
250 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
251 register int y, cb, cr;
252 register JSAMPROW outptr;
253 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
254 register JDIMENSION col;
255 JDIMENSION num_cols = cinfo->output_width;
256 /* copy these pointers into registers if possible */
257 register JSAMPLE * range_limit = cinfo->sample_range_limit;
258 register int * Crrtab = cconvert->Cr_r_tab;
259 register int * Cbbtab = cconvert->Cb_b_tab;
260 register INT32 * Crgtab = cconvert->Cr_g_tab;
261 register INT32 * Cbgtab = cconvert->Cb_g_tab;
262 SHIFT_TEMPS
263
264 while (--num_rows >= 0) {
265 inptr0 = input_buf[0][input_row];
266 inptr1 = input_buf[1][input_row];
267 inptr2 = input_buf[2][input_row];
268 inptr3 = input_buf[3][input_row];
269 input_row++;
270 outptr = *output_buf++;
271 for (col = 0; col < num_cols; col++) {
272 y = GETJSAMPLE(inptr0[col]);
273 cb = GETJSAMPLE(inptr1[col]);
274 cr = GETJSAMPLE(inptr2[col]);
275 /* Range-limiting is essential due to noise introduced by DCT losses. */
276 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
277 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
278 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
279 SCALEBITS)))];
280 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
281 /* K passes through unchanged */
282 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
283 outptr += 4;
284 }
285 }
286 }
287
288
289 /*
290 * Empty method for start_pass.
291 */
292
293 METHODDEF(void)
294 start_pass_dcolor (CPL_UNUSED j_decompress_ptr cinfo)
295 {
296 /* no work needed */
297 }
298
299
300 /*
301 * Module initialization routine for output colorspace conversion.
302 */
303
304 GLOBAL(void)
305 jinit_color_deconverter (j_decompress_ptr cinfo)
306 {
307 my_cconvert_ptr cconvert;
308 int ci;
309
310 cconvert = (my_cconvert_ptr)
311 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
312 SIZEOF(my_color_deconverter));
313 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
314 cconvert->pub.start_pass = start_pass_dcolor;
315
316 /* Make sure num_components agrees with jpeg_color_space */
317 switch (cinfo->jpeg_color_space) {
318 case JCS_GRAYSCALE:
319 if (cinfo->num_components != 1)
320 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
321 break;
322
323 case JCS_RGB:
324 case JCS_YCbCr:
325 if (cinfo->num_components != 3)
326 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
327 break;
328
329 case JCS_CMYK:
330 case JCS_YCCK:
331 if (cinfo->num_components != 4)
332 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
333 break;
334
335 default: /* JCS_UNKNOWN can be anything */
336 if (cinfo->num_components < 1)
337 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
338 break;
339 }
340
341 /* Set out_color_components and conversion method based on requested space.
342 * Also clear the component_needed flags for any unused components,
343 * so that earlier pipeline stages can avoid useless computation.
344 */
345
346 switch (cinfo->out_color_space) {
347 case JCS_GRAYSCALE:
348 cinfo->out_color_components = 1;
349 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
350 cinfo->jpeg_color_space == JCS_YCbCr) {
351 cconvert->pub.color_convert = grayscale_convert;
352 /* For color->grayscale conversion, only the Y (0) component is needed */
353 for (ci = 1; ci < cinfo->num_components; ci++)
354 cinfo->comp_info[ci].component_needed = FALSE;
355 } else
356 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
357 break;
358
359 case JCS_RGB:
360 cinfo->out_color_components = RGB_PIXELSIZE;
361 if (cinfo->jpeg_color_space == JCS_YCbCr) {
362 cconvert->pub.color_convert = ycc_rgb_convert;
363 build_ycc_rgb_table(cinfo);
364 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
365 cconvert->pub.color_convert = gray_rgb_convert;
366 } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
367 cconvert->pub.color_convert = null_convert;
368 } else
369 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
370 break;
371
372 case JCS_CMYK:
373 cinfo->out_color_components = 4;
374 if (cinfo->jpeg_color_space == JCS_YCCK) {
375 cconvert->pub.color_convert = ycck_cmyk_convert;
376 build_ycc_rgb_table(cinfo);
377 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
378 cconvert->pub.color_convert = null_convert;
379 } else
380 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
381 break;
382
383 default:
384 /* Permit null conversion to same output space */
385 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
386 cinfo->out_color_components = cinfo->num_components;
387 cconvert->pub.color_convert = null_convert;
388 } else /* unsupported non-null conversion */
389 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
390 break;
391 }
392
393 if (cinfo->quantize_colors)
394 cinfo->output_components = 1; /* single colormapped output component */
395 else
396 cinfo->output_components = cinfo->out_color_components;
397 }