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