[TRANSLATIONS] Update the email address and add a note in the Turkish translation...
[reactos.git] / dll / 3rdparty / libjpeg / jdcolor.c
1 /*
2 * jdcolor.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2011-2015 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 /* Private subobject */
18
19 typedef struct {
20 struct jpeg_color_deconverter pub; /* public fields */
21
22 /* Private state for YCbCr->RGB and BG_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
28 /* Private state for RGB->Y conversion */
29 INT32 * rgb_y_tab; /* => table for RGB to Y conversion */
30 } my_color_deconverter;
31
32 typedef my_color_deconverter * my_cconvert_ptr;
33
34
35 /*************** YCbCr -> RGB conversion: most common case **************/
36 /*************** BG_YCC -> RGB conversion: less common case **************/
37 /*************** RGB -> Y conversion: less common case **************/
38
39 /*
40 * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
41 * previously known as Recommendation CCIR 601-1, except that Cb and Cr
42 * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
43 * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
44 * sYCC (standard luma-chroma-chroma color space with extended gamut)
45 * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
46 * bg-sRGB and bg-sYCC (big gamut standard color spaces)
47 * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
48 * Note that the derived conversion coefficients given in some of these
49 * documents are imprecise. The general conversion equations are
50 *
51 * R = Y + K * (1 - Kr) * Cr
52 * G = Y - K * (Kb * (1 - Kb) * Cb + Kr * (1 - Kr) * Cr) / (1 - Kr - Kb)
53 * B = Y + K * (1 - Kb) * Cb
54 *
55 * Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
56 *
57 * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
58 * from the 1953 FCC NTSC primaries and CIE Illuminant C), K = 2 for sYCC,
59 * the conversion equations to be implemented are therefore
60 *
61 * R = Y + 1.402 * Cr
62 * G = Y - 0.344136286 * Cb - 0.714136286 * Cr
63 * B = Y + 1.772 * Cb
64 *
65 * Y = 0.299 * R + 0.587 * G + 0.114 * B
66 *
67 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
68 * For bg-sYCC, with K = 4, the equations are
69 *
70 * R = Y + 2.804 * Cr
71 * G = Y - 0.688272572 * Cb - 1.428272572 * Cr
72 * B = Y + 3.544 * Cb
73 *
74 * To avoid floating-point arithmetic, we represent the fractional constants
75 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
76 * the products by 2^16, with appropriate rounding, to get the correct answer.
77 * Notice that Y, being an integral input, does not contribute any fraction
78 * so it need not participate in the rounding.
79 *
80 * For even more speed, we avoid doing any multiplications in the inner loop
81 * by precalculating the constants times Cb and Cr for all possible values.
82 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
83 * for 9-bit to 12-bit samples it is still acceptable. It's not very
84 * reasonable for 16-bit samples, but if you want lossless storage you
85 * shouldn't be changing colorspace anyway.
86 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
87 * values for the G calculation are left scaled up, since we must add them
88 * together before rounding.
89 */
90
91 #define SCALEBITS 16 /* speediest right-shift on some machines */
92 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
93 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
94
95 /* We allocate one big table for RGB->Y conversion and divide it up into
96 * three parts, instead of doing three alloc_small requests. This lets us
97 * use a single table base address, which can be held in a register in the
98 * inner loops on many machines (more than can hold all three addresses,
99 * anyway).
100 */
101
102 #define R_Y_OFF 0 /* offset to R => Y section */
103 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
104 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
105 #define TABLE_SIZE (3*(MAXJSAMPLE+1))
106
107
108 /*
109 * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
110 */
111
112 LOCAL(void)
113 build_ycc_rgb_table (j_decompress_ptr cinfo)
114 /* Normal case, sYCC */
115 {
116 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
117 int i;
118 INT32 x;
119 SHIFT_TEMPS
120
121 cconvert->Cr_r_tab = (int *)
122 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
123 (MAXJSAMPLE+1) * SIZEOF(int));
124 cconvert->Cb_b_tab = (int *)
125 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
126 (MAXJSAMPLE+1) * SIZEOF(int));
127 cconvert->Cr_g_tab = (INT32 *)
128 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
129 (MAXJSAMPLE+1) * SIZEOF(INT32));
130 cconvert->Cb_g_tab = (INT32 *)
131 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
132 (MAXJSAMPLE+1) * SIZEOF(INT32));
133
134 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
135 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
136 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
137 /* Cr=>R value is nearest int to 1.402 * x */
138 cconvert->Cr_r_tab[i] = (int)
139 RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
140 /* Cb=>B value is nearest int to 1.772 * x */
141 cconvert->Cb_b_tab[i] = (int)
142 RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, 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 *)
162 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
163 (MAXJSAMPLE+1) * SIZEOF(int));
164 cconvert->Cb_b_tab = (int *)
165 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
166 (MAXJSAMPLE+1) * SIZEOF(int));
167 cconvert->Cr_g_tab = (INT32 *)
168 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
169 (MAXJSAMPLE+1) * SIZEOF(INT32));
170 cconvert->Cb_g_tab = (INT32 *)
171 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
172 (MAXJSAMPLE+1) * SIZEOF(INT32));
173
174 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
175 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
176 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
177 /* Cr=>R value is nearest int to 2.804 * x */
178 cconvert->Cr_r_tab[i] = (int)
179 RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
180 /* Cb=>B value is nearest int to 3.544 * x */
181 cconvert->Cb_b_tab[i] = (int)
182 RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
183 /* Cr=>G value is scaled-up -1.428272572 * x */
184 cconvert->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
185 /* Cb=>G value is scaled-up -0.688272572 * x */
186 /* We also add in ONE_HALF so that need not do it in inner loop */
187 cconvert->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
188 }
189 }
190
191
192 /*
193 * Convert some rows of samples to the output colorspace.
194 *
195 * Note that we change from noninterleaved, one-plane-per-component format
196 * to interleaved-pixel format. The output buffer is therefore three times
197 * as wide as the input buffer.
198 * A starting row offset is provided only for the input buffer. The caller
199 * can easily adjust the passed output_buf value to accommodate any row
200 * offset required on that side.
201 */
202
203 METHODDEF(void)
204 ycc_rgb_convert (j_decompress_ptr cinfo,
205 JSAMPIMAGE input_buf, JDIMENSION input_row,
206 JSAMPARRAY output_buf, int num_rows)
207 {
208 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
209 register int y, cb, cr;
210 register JSAMPROW outptr;
211 register JSAMPROW inptr0, inptr1, inptr2;
212 register JDIMENSION col;
213 JDIMENSION num_cols = cinfo->output_width;
214 /* copy these pointers into registers if possible */
215 register JSAMPLE * range_limit = cinfo->sample_range_limit;
216 register int * Crrtab = cconvert->Cr_r_tab;
217 register int * Cbbtab = cconvert->Cb_b_tab;
218 register INT32 * Crgtab = cconvert->Cr_g_tab;
219 register INT32 * Cbgtab = cconvert->Cb_g_tab;
220 SHIFT_TEMPS
221
222 while (--num_rows >= 0) {
223 inptr0 = input_buf[0][input_row];
224 inptr1 = input_buf[1][input_row];
225 inptr2 = input_buf[2][input_row];
226 input_row++;
227 outptr = *output_buf++;
228 for (col = 0; col < num_cols; col++) {
229 y = GETJSAMPLE(inptr0[col]);
230 cb = GETJSAMPLE(inptr1[col]);
231 cr = GETJSAMPLE(inptr2[col]);
232 /* Range-limiting is essential due to noise introduced by DCT losses,
233 * for extended gamut (sYCC) and wide gamut (bg-sYCC) encodings.
234 */
235 outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
236 outptr[RGB_GREEN] = range_limit[y +
237 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
238 SCALEBITS))];
239 outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
240 outptr += RGB_PIXELSIZE;
241 }
242 }
243 }
244
245
246 /**************** Cases other than YCC -> RGB ****************/
247
248
249 /*
250 * Initialize for RGB->grayscale colorspace conversion.
251 */
252
253 LOCAL(void)
254 build_rgb_y_table (j_decompress_ptr cinfo)
255 {
256 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
257 INT32 * rgb_y_tab;
258 INT32 i;
259
260 /* Allocate and fill in the conversion tables. */
261 cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
262 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
263 (TABLE_SIZE * SIZEOF(INT32)));
264
265 for (i = 0; i <= MAXJSAMPLE; i++) {
266 rgb_y_tab[i+R_Y_OFF] = FIX(0.299) * i;
267 rgb_y_tab[i+G_Y_OFF] = FIX(0.587) * i;
268 rgb_y_tab[i+B_Y_OFF] = FIX(0.114) * i + ONE_HALF;
269 }
270 }
271
272
273 /*
274 * Convert RGB to grayscale.
275 */
276
277 METHODDEF(void)
278 rgb_gray_convert (j_decompress_ptr cinfo,
279 JSAMPIMAGE input_buf, JDIMENSION input_row,
280 JSAMPARRAY output_buf, int num_rows)
281 {
282 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
283 register INT32 * ctab = cconvert->rgb_y_tab;
284 register int r, g, b;
285 register JSAMPROW outptr;
286 register JSAMPROW inptr0, inptr1, inptr2;
287 register JDIMENSION col;
288 JDIMENSION num_cols = cinfo->output_width;
289
290 while (--num_rows >= 0) {
291 inptr0 = input_buf[0][input_row];
292 inptr1 = input_buf[1][input_row];
293 inptr2 = input_buf[2][input_row];
294 input_row++;
295 outptr = *output_buf++;
296 for (col = 0; col < num_cols; col++) {
297 r = GETJSAMPLE(inptr0[col]);
298 g = GETJSAMPLE(inptr1[col]);
299 b = GETJSAMPLE(inptr2[col]);
300 /* Y */
301 outptr[col] = (JSAMPLE)
302 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
303 >> SCALEBITS);
304 }
305 }
306 }
307
308
309 /*
310 * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
311 * (inverse color transform).
312 * This can be seen as an adaption of the general YCbCr->RGB
313 * conversion equation with Kr = Kb = 0, while replacing the
314 * normalization by modulo calculation.
315 */
316
317 METHODDEF(void)
318 rgb1_rgb_convert (j_decompress_ptr cinfo,
319 JSAMPIMAGE input_buf, JDIMENSION input_row,
320 JSAMPARRAY output_buf, int num_rows)
321 {
322 register int r, g, b;
323 register JSAMPROW outptr;
324 register JSAMPROW inptr0, inptr1, inptr2;
325 register JDIMENSION col;
326 JDIMENSION num_cols = cinfo->output_width;
327
328 while (--num_rows >= 0) {
329 inptr0 = input_buf[0][input_row];
330 inptr1 = input_buf[1][input_row];
331 inptr2 = input_buf[2][input_row];
332 input_row++;
333 outptr = *output_buf++;
334 for (col = 0; col < num_cols; col++) {
335 r = GETJSAMPLE(inptr0[col]);
336 g = GETJSAMPLE(inptr1[col]);
337 b = GETJSAMPLE(inptr2[col]);
338 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
339 * (modulo) operator is equivalent to the bitmask operator AND.
340 */
341 outptr[RGB_RED] = (JSAMPLE) ((r + g - CENTERJSAMPLE) & MAXJSAMPLE);
342 outptr[RGB_GREEN] = (JSAMPLE) g;
343 outptr[RGB_BLUE] = (JSAMPLE) ((b + g - CENTERJSAMPLE) & MAXJSAMPLE);
344 outptr += RGB_PIXELSIZE;
345 }
346 }
347 }
348
349
350 /*
351 * [R-G,G,B-G] to grayscale conversion with modulo calculation
352 * (inverse color transform).
353 */
354
355 METHODDEF(void)
356 rgb1_gray_convert (j_decompress_ptr cinfo,
357 JSAMPIMAGE input_buf, JDIMENSION input_row,
358 JSAMPARRAY output_buf, int num_rows)
359 {
360 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
361 register INT32 * ctab = cconvert->rgb_y_tab;
362 register int r, g, b;
363 register JSAMPROW outptr;
364 register JSAMPROW inptr0, inptr1, inptr2;
365 register JDIMENSION col;
366 JDIMENSION num_cols = cinfo->output_width;
367
368 while (--num_rows >= 0) {
369 inptr0 = input_buf[0][input_row];
370 inptr1 = input_buf[1][input_row];
371 inptr2 = input_buf[2][input_row];
372 input_row++;
373 outptr = *output_buf++;
374 for (col = 0; col < num_cols; col++) {
375 r = GETJSAMPLE(inptr0[col]);
376 g = GETJSAMPLE(inptr1[col]);
377 b = GETJSAMPLE(inptr2[col]);
378 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
379 * (modulo) operator is equivalent to the bitmask operator AND.
380 */
381 r = (r + g - CENTERJSAMPLE) & MAXJSAMPLE;
382 b = (b + g - CENTERJSAMPLE) & MAXJSAMPLE;
383 /* Y */
384 outptr[col] = (JSAMPLE)
385 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
386 >> SCALEBITS);
387 }
388 }
389 }
390
391
392 /*
393 * No colorspace change, but conversion from separate-planes
394 * to interleaved representation.
395 */
396
397 METHODDEF(void)
398 rgb_convert (j_decompress_ptr cinfo,
399 JSAMPIMAGE input_buf, JDIMENSION input_row,
400 JSAMPARRAY output_buf, int num_rows)
401 {
402 register JSAMPROW outptr;
403 register JSAMPROW inptr0, inptr1, inptr2;
404 register JDIMENSION col;
405 JDIMENSION num_cols = cinfo->output_width;
406
407 while (--num_rows >= 0) {
408 inptr0 = input_buf[0][input_row];
409 inptr1 = input_buf[1][input_row];
410 inptr2 = input_buf[2][input_row];
411 input_row++;
412 outptr = *output_buf++;
413 for (col = 0; col < num_cols; col++) {
414 /* We can dispense with GETJSAMPLE() here */
415 outptr[RGB_RED] = inptr0[col];
416 outptr[RGB_GREEN] = inptr1[col];
417 outptr[RGB_BLUE] = inptr2[col];
418 outptr += RGB_PIXELSIZE;
419 }
420 }
421 }
422
423
424 /*
425 * Color conversion for no colorspace change: just copy the data,
426 * converting from separate-planes to interleaved representation.
427 */
428
429 METHODDEF(void)
430 null_convert (j_decompress_ptr cinfo,
431 JSAMPIMAGE input_buf, JDIMENSION input_row,
432 JSAMPARRAY output_buf, int num_rows)
433 {
434 int ci;
435 register int nc = cinfo->num_components;
436 register JSAMPROW outptr;
437 register JSAMPROW inptr;
438 register JDIMENSION col;
439 JDIMENSION num_cols = cinfo->output_width;
440
441 while (--num_rows >= 0) {
442 for (ci = 0; ci < nc; ci++) {
443 inptr = input_buf[ci][input_row];
444 outptr = output_buf[0] + ci;
445 for (col = 0; col < num_cols; col++) {
446 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
447 outptr += nc;
448 }
449 }
450 input_row++;
451 output_buf++;
452 }
453 }
454
455
456 /*
457 * Color conversion for grayscale: just copy the data.
458 * This also works for YCC -> grayscale conversion, in which
459 * we just copy the Y (luminance) component and ignore chrominance.
460 */
461
462 METHODDEF(void)
463 grayscale_convert (j_decompress_ptr cinfo,
464 JSAMPIMAGE input_buf, JDIMENSION input_row,
465 JSAMPARRAY output_buf, int num_rows)
466 {
467 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
468 num_rows, cinfo->output_width);
469 }
470
471
472 /*
473 * Convert grayscale to RGB: just duplicate the graylevel three times.
474 * This is provided to support applications that don't want to cope
475 * with grayscale as a separate case.
476 */
477
478 METHODDEF(void)
479 gray_rgb_convert (j_decompress_ptr cinfo,
480 JSAMPIMAGE input_buf, JDIMENSION input_row,
481 JSAMPARRAY output_buf, int num_rows)
482 {
483 register JSAMPROW outptr;
484 register JSAMPROW inptr;
485 register JDIMENSION col;
486 JDIMENSION num_cols = cinfo->output_width;
487
488 while (--num_rows >= 0) {
489 inptr = input_buf[0][input_row++];
490 outptr = *output_buf++;
491 for (col = 0; col < num_cols; col++) {
492 /* We can dispense with GETJSAMPLE() here */
493 outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
494 outptr += RGB_PIXELSIZE;
495 }
496 }
497 }
498
499
500 /*
501 * Adobe-style YCCK->CMYK conversion.
502 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
503 * conversion as above, while passing K (black) unchanged.
504 * We assume build_ycc_rgb_table has been called.
505 */
506
507 METHODDEF(void)
508 ycck_cmyk_convert (j_decompress_ptr cinfo,
509 JSAMPIMAGE input_buf, JDIMENSION input_row,
510 JSAMPARRAY output_buf, int num_rows)
511 {
512 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
513 register int y, cb, cr;
514 register JSAMPROW outptr;
515 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
516 register JDIMENSION col;
517 JDIMENSION num_cols = cinfo->output_width;
518 /* copy these pointers into registers if possible */
519 register JSAMPLE * range_limit = cinfo->sample_range_limit;
520 register int * Crrtab = cconvert->Cr_r_tab;
521 register int * Cbbtab = cconvert->Cb_b_tab;
522 register INT32 * Crgtab = cconvert->Cr_g_tab;
523 register INT32 * Cbgtab = cconvert->Cb_g_tab;
524 SHIFT_TEMPS
525
526 while (--num_rows >= 0) {
527 inptr0 = input_buf[0][input_row];
528 inptr1 = input_buf[1][input_row];
529 inptr2 = input_buf[2][input_row];
530 inptr3 = input_buf[3][input_row];
531 input_row++;
532 outptr = *output_buf++;
533 for (col = 0; col < num_cols; col++) {
534 y = GETJSAMPLE(inptr0[col]);
535 cb = GETJSAMPLE(inptr1[col]);
536 cr = GETJSAMPLE(inptr2[col]);
537 /* Range-limiting is essential due to noise introduced by DCT losses,
538 * and for extended gamut encodings (sYCC).
539 */
540 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
541 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
542 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
543 SCALEBITS)))];
544 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
545 /* K passes through unchanged */
546 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
547 outptr += 4;
548 }
549 }
550 }
551
552
553 /*
554 * Empty method for start_pass.
555 */
556
557 METHODDEF(void)
558 start_pass_dcolor (j_decompress_ptr cinfo)
559 {
560 /* no work needed */
561 }
562
563
564 /*
565 * Module initialization routine for output colorspace conversion.
566 */
567
568 GLOBAL(void)
569 jinit_color_deconverter (j_decompress_ptr cinfo)
570 {
571 my_cconvert_ptr cconvert;
572 int ci;
573
574 cconvert = (my_cconvert_ptr)
575 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
576 SIZEOF(my_color_deconverter));
577 cinfo->cconvert = &cconvert->pub;
578 cconvert->pub.start_pass = start_pass_dcolor;
579
580 /* Make sure num_components agrees with jpeg_color_space */
581 switch (cinfo->jpeg_color_space) {
582 case JCS_GRAYSCALE:
583 if (cinfo->num_components != 1)
584 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
585 break;
586
587 case JCS_RGB:
588 case JCS_YCbCr:
589 case JCS_BG_RGB:
590 case JCS_BG_YCC:
591 if (cinfo->num_components != 3)
592 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
593 break;
594
595 case JCS_CMYK:
596 case JCS_YCCK:
597 if (cinfo->num_components != 4)
598 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
599 break;
600
601 default: /* JCS_UNKNOWN can be anything */
602 if (cinfo->num_components < 1)
603 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
604 break;
605 }
606
607 /* Support color transform only for RGB colorspaces */
608 if (cinfo->color_transform &&
609 cinfo->jpeg_color_space != JCS_RGB &&
610 cinfo->jpeg_color_space != JCS_BG_RGB)
611 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
612
613 /* Set out_color_components and conversion method based on requested space.
614 * Also clear the component_needed flags for any unused components,
615 * so that earlier pipeline stages can avoid useless computation.
616 */
617
618 switch (cinfo->out_color_space) {
619 case JCS_GRAYSCALE:
620 cinfo->out_color_components = 1;
621 switch (cinfo->jpeg_color_space) {
622 case JCS_GRAYSCALE:
623 case JCS_YCbCr:
624 case JCS_BG_YCC:
625 cconvert->pub.color_convert = grayscale_convert;
626 /* For color->grayscale conversion, only the Y (0) component is needed */
627 for (ci = 1; ci < cinfo->num_components; ci++)
628 cinfo->comp_info[ci].component_needed = FALSE;
629 break;
630 case JCS_RGB:
631 switch (cinfo->color_transform) {
632 case JCT_NONE:
633 cconvert->pub.color_convert = rgb_gray_convert;
634 break;
635 case JCT_SUBTRACT_GREEN:
636 cconvert->pub.color_convert = rgb1_gray_convert;
637 break;
638 default:
639 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
640 }
641 build_rgb_y_table(cinfo);
642 break;
643 default:
644 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
645 }
646 break;
647
648 case JCS_RGB:
649 cinfo->out_color_components = RGB_PIXELSIZE;
650 switch (cinfo->jpeg_color_space) {
651 case JCS_GRAYSCALE:
652 cconvert->pub.color_convert = gray_rgb_convert;
653 break;
654 case JCS_YCbCr:
655 cconvert->pub.color_convert = ycc_rgb_convert;
656 build_ycc_rgb_table(cinfo);
657 break;
658 case JCS_BG_YCC:
659 cconvert->pub.color_convert = ycc_rgb_convert;
660 build_bg_ycc_rgb_table(cinfo);
661 break;
662 case JCS_RGB:
663 switch (cinfo->color_transform) {
664 case JCT_NONE:
665 cconvert->pub.color_convert = rgb_convert;
666 break;
667 case JCT_SUBTRACT_GREEN:
668 cconvert->pub.color_convert = rgb1_rgb_convert;
669 break;
670 default:
671 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
672 }
673 break;
674 default:
675 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
676 }
677 break;
678
679 case JCS_BG_RGB:
680 cinfo->out_color_components = RGB_PIXELSIZE;
681 if (cinfo->jpeg_color_space == JCS_BG_RGB) {
682 switch (cinfo->color_transform) {
683 case JCT_NONE:
684 cconvert->pub.color_convert = rgb_convert;
685 break;
686 case JCT_SUBTRACT_GREEN:
687 cconvert->pub.color_convert = rgb1_rgb_convert;
688 break;
689 default:
690 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
691 }
692 } else
693 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
694 break;
695
696 case JCS_CMYK:
697 cinfo->out_color_components = 4;
698 switch (cinfo->jpeg_color_space) {
699 case JCS_YCCK:
700 cconvert->pub.color_convert = ycck_cmyk_convert;
701 build_ycc_rgb_table(cinfo);
702 break;
703 case JCS_CMYK:
704 cconvert->pub.color_convert = null_convert;
705 break;
706 default:
707 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
708 }
709 break;
710
711 default:
712 /* Permit null conversion to same output space */
713 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
714 cinfo->out_color_components = cinfo->num_components;
715 cconvert->pub.color_convert = null_convert;
716 } else /* unsupported non-null conversion */
717 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
718 break;
719 }
720
721 if (cinfo->quantize_colors)
722 cinfo->output_components = 1; /* single colormapped output component */
723 else
724 cinfo->output_components = cinfo->out_color_components;
725 }