[SHELL32_APITEST] Follow-up to #6796 (25e2f5f)
[reactos.git] / dll / 3rdparty / libjpeg / jdcolor.c
1 /*
2 * jdcolor.c
3 *
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.
8 *
9 * This file contains output colorspace conversion routines.
10 */
11
12 #define JPEG_INTERNALS
13 #include "jinclude.h"
14 #include "jpeglib.h"
15
16
17 #if RANGE_BITS < 2
18 /* Deliberate syntax err */
19 Sorry, this code requires 2 or more range extension bits.
20 #endif
21
22
23 /* Private subobject */
24
25 typedef struct {
26 struct jpeg_color_deconverter pub; /* public fields */
27
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 */
33
34 /* Private state for RGB->Y conversion */
35 INT32 * rgb_y_tab; /* => table for RGB to Y conversion */
36 } my_color_deconverter;
37
38 typedef my_color_deconverter * my_cconvert_ptr;
39
40
41 /*************** YCbCr -> RGB conversion: most common case **************/
42 /*************** BG_YCC -> RGB conversion: less common case **************/
43 /*************** RGB -> Y conversion: less common case **************/
44
45 /*
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
56 *
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
60 *
61 * Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
62 *
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
66 *
67 * R = Y + 1.402 * Cr
68 * G = Y - 0.344136286 * Cb - 0.714136286 * Cr
69 * B = Y + 1.772 * Cb
70 *
71 * Y = 0.299 * R + 0.587 * G + 0.114 * B
72 *
73 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
74 * For bg-sYCC, with K = 4, the equations are
75 *
76 * R = Y + 2.804 * Cr
77 * G = Y - 0.688272572 * Cb - 1.428272572 * Cr
78 * B = Y + 3.544 * Cb
79 *
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.
85 *
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.
95 */
96
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))
100
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,
105 * anyway).
106 */
107
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))
112
113
114 /*
115 * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
116 */
117
118 LOCAL(void)
119 build_ycc_rgb_table (j_decompress_ptr cinfo)
120 /* Normal case, sYCC */
121 {
122 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
123 int i;
124 INT32 x;
125 SHIFT_TEMPS
126
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));
135
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;
148 }
149 }
150
151
152 LOCAL(void)
153 build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
154 /* Wide gamut case, bg-sYCC */
155 {
156 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
157 int i;
158 INT32 x;
159 SHIFT_TEMPS
160
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));
169
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;
182 }
183 }
184
185
186 /*
187 * Convert some rows of samples to the output colorspace.
188 *
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.
192 *
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.
196 */
197
198 METHODDEF(void)
199 ycc_rgb_convert (j_decompress_ptr cinfo,
200 JSAMPIMAGE input_buf, JDIMENSION input_row,
201 JSAMPARRAY output_buf, int num_rows)
202 {
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;
215 SHIFT_TEMPS
216
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];
221 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.
229 */
230 outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
231 outptr[RGB_GREEN] = range_limit[y +
232 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
233 SCALEBITS))];
234 outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
235 outptr += RGB_PIXELSIZE;
236 }
237 }
238 }
239
240
241 /**************** Cases other than YCC -> RGB ****************/
242
243
244 /*
245 * Initialize for RGB->grayscale colorspace conversion.
246 */
247
248 LOCAL(void)
249 build_rgb_y_table (j_decompress_ptr cinfo)
250 {
251 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
252 INT32 * rgb_y_tab;
253 INT32 i;
254
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));
258
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;
263 }
264 }
265
266
267 /*
268 * Convert RGB to grayscale.
269 */
270
271 METHODDEF(void)
272 rgb_gray_convert (j_decompress_ptr cinfo,
273 JSAMPIMAGE input_buf, JDIMENSION input_row,
274 JSAMPARRAY output_buf, int num_rows)
275 {
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;
283
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];
288 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]);
294 /* Y */
295 outptr[col] = (JSAMPLE)
296 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
297 >> SCALEBITS);
298 }
299 }
300 }
301
302
303 /*
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.
310 */
311
312 METHODDEF(void)
313 rgb1_rgb_convert (j_decompress_ptr cinfo,
314 JSAMPIMAGE input_buf, JDIMENSION input_row,
315 JSAMPARRAY output_buf, int num_rows)
316 {
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;
322
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];
327 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.
335 */
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;
340 }
341 }
342 }
343
344
345 /*
346 * [R-G,G,B-G] to grayscale conversion with modulo calculation
347 * (inverse color transform).
348 */
349
350 METHODDEF(void)
351 rgb1_gray_convert (j_decompress_ptr cinfo,
352 JSAMPIMAGE input_buf, JDIMENSION input_row,
353 JSAMPARRAY output_buf, int num_rows)
354 {
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;
362
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];
367 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.
375 */
376 r = (r + g - CENTERJSAMPLE) & MAXJSAMPLE;
377 b = (b + g - CENTERJSAMPLE) & MAXJSAMPLE;
378 /* Y */
379 outptr[col] = (JSAMPLE)
380 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
381 >> SCALEBITS);
382 }
383 }
384 }
385
386
387 /*
388 * Convert some rows of samples to the output colorspace.
389 * No colorspace change, but conversion from separate-planes
390 * to interleaved representation.
391 */
392
393 METHODDEF(void)
394 rgb_convert (j_decompress_ptr cinfo,
395 JSAMPIMAGE input_buf, JDIMENSION input_row,
396 JSAMPARRAY output_buf, int num_rows)
397 {
398 register JSAMPROW outptr;
399 register JSAMPROW inptr0, inptr1, inptr2;
400 register JDIMENSION col;
401 JDIMENSION num_cols = cinfo->output_width;
402
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];
407 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;
415 }
416 }
417 }
418
419
420 /*
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.
424 */
425
426 METHODDEF(void)
427 null_convert (j_decompress_ptr cinfo,
428 JSAMPIMAGE input_buf, JDIMENSION input_row,
429 JSAMPARRAY output_buf, int num_rows)
430 {
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;
436 int ci;
437
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 */
445 outptr += num_comps;
446 }
447 }
448 input_row++;
449 output_buf++;
450 }
451 }
452
453
454 /*
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.
458 */
459
460 METHODDEF(void)
461 grayscale_convert (j_decompress_ptr cinfo,
462 JSAMPIMAGE input_buf, JDIMENSION input_row,
463 JSAMPARRAY output_buf, int num_rows)
464 {
465 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
466 num_rows, cinfo->output_width);
467 }
468
469
470 /*
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.
474 */
475
476 METHODDEF(void)
477 gray_rgb_convert (j_decompress_ptr cinfo,
478 JSAMPIMAGE input_buf, JDIMENSION input_row,
479 JSAMPARRAY output_buf, int num_rows)
480 {
481 register JSAMPROW outptr;
482 register JSAMPROW inptr;
483 register JDIMENSION col;
484 JDIMENSION num_cols = cinfo->output_width;
485
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;
493 }
494 }
495 }
496
497
498 /*
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.
504 */
505
506 METHODDEF(void)
507 ycck_cmyk_convert (j_decompress_ptr cinfo,
508 JSAMPIMAGE input_buf, JDIMENSION input_row,
509 JSAMPARRAY output_buf, int num_rows)
510 {
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;
523 SHIFT_TEMPS
524
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];
530 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).
538 */
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],
542 SCALEBITS)))];
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 */
546 outptr += 4;
547 }
548 }
549 }
550
551
552 /*
553 * Empty method for start_pass.
554 */
555
556 METHODDEF(void)
557 start_pass_dcolor (j_decompress_ptr cinfo)
558 {
559 /* no work needed */
560 }
561
562
563 /*
564 * Module initialization routine for output colorspace conversion.
565 */
566
567 GLOBAL(void)
568 jinit_color_deconverter (j_decompress_ptr cinfo)
569 {
570 my_cconvert_ptr cconvert;
571 int ci;
572
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;
577
578 /* Make sure num_components agrees with jpeg_color_space */
579 switch (cinfo->jpeg_color_space) {
580 case JCS_GRAYSCALE:
581 if (cinfo->num_components != 1)
582 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
583 break;
584
585 case JCS_RGB:
586 case JCS_YCbCr:
587 case JCS_BG_RGB:
588 case JCS_BG_YCC:
589 if (cinfo->num_components != 3)
590 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
591 break;
592
593 case JCS_CMYK:
594 case JCS_YCCK:
595 if (cinfo->num_components != 4)
596 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
597 break;
598
599 default: /* JCS_UNKNOWN can be anything */
600 if (cinfo->num_components < 1)
601 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
602 }
603
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);
609
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.
613 */
614
615 switch (cinfo->out_color_space) {
616 case JCS_GRAYSCALE:
617 cinfo->out_color_components = 1;
618 switch (cinfo->jpeg_color_space) {
619 case JCS_GRAYSCALE:
620 case JCS_YCbCr:
621 case JCS_BG_YCC:
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;
626 break;
627 case JCS_RGB:
628 switch (cinfo->color_transform) {
629 case JCT_NONE:
630 cconvert->pub.color_convert = rgb_gray_convert;
631 break;
632 case JCT_SUBTRACT_GREEN:
633 cconvert->pub.color_convert = rgb1_gray_convert;
634 break;
635 default:
636 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
637 }
638 build_rgb_y_table(cinfo);
639 break;
640 default:
641 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
642 }
643 break;
644
645 case JCS_RGB:
646 cinfo->out_color_components = RGB_PIXELSIZE;
647 switch (cinfo->jpeg_color_space) {
648 case JCS_GRAYSCALE:
649 cconvert->pub.color_convert = gray_rgb_convert;
650 break;
651 case JCS_YCbCr:
652 cconvert->pub.color_convert = ycc_rgb_convert;
653 build_ycc_rgb_table(cinfo);
654 break;
655 case JCS_BG_YCC:
656 cconvert->pub.color_convert = ycc_rgb_convert;
657 build_bg_ycc_rgb_table(cinfo);
658 break;
659 case JCS_RGB:
660 switch (cinfo->color_transform) {
661 case JCT_NONE:
662 cconvert->pub.color_convert = rgb_convert;
663 break;
664 case JCT_SUBTRACT_GREEN:
665 cconvert->pub.color_convert = rgb1_rgb_convert;
666 break;
667 default:
668 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
669 }
670 break;
671 default:
672 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
673 }
674 break;
675
676 case JCS_BG_RGB:
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) {
681 case JCT_NONE:
682 cconvert->pub.color_convert = rgb_convert;
683 break;
684 case JCT_SUBTRACT_GREEN:
685 cconvert->pub.color_convert = rgb1_rgb_convert;
686 break;
687 default:
688 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
689 }
690 break;
691
692 case JCS_CMYK:
693 cinfo->out_color_components = 4;
694 switch (cinfo->jpeg_color_space) {
695 case JCS_YCCK:
696 cconvert->pub.color_convert = ycck_cmyk_convert;
697 build_ycc_rgb_table(cinfo);
698 break;
699 case JCS_CMYK:
700 cconvert->pub.color_convert = null_convert;
701 break;
702 default:
703 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
704 }
705 break;
706
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;
713 }
714
715 if (cinfo->quantize_colors)
716 cinfo->output_components = 1; /* single colormapped output component */
717 else
718 cinfo->output_components = cinfo->out_color_components;
719 }