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