OLD | NEW |
1 /* | 1 /* |
2 * jdcolor.c | 2 * jdcolor.c |
3 * | 3 * |
4 * Copyright (C) 1991-1997, Thomas G. Lane. | 4 * Copyright (C) 1991-1997, Thomas G. Lane. |
5 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB | 5 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB |
6 * Copyright (C) 2009, D. R. Commander. | 6 * Copyright (C) 2009, 2011, D. R. Commander. |
7 * This file is part of the Independent JPEG Group's software. | 7 * This file is part of the Independent JPEG Group's software. |
8 * For conditions of distribution and use, see the accompanying README file. | 8 * For conditions of distribution and use, see the accompanying README file. |
9 * | 9 * |
10 * This file contains output colorspace conversion routines. | 10 * This file contains output colorspace conversion routines. |
11 */ | 11 */ |
12 | 12 |
13 #define JPEG_INTERNALS | 13 #define JPEG_INTERNALS |
14 #include "jinclude.h" | 14 #include "jinclude.h" |
15 #include "jpeglib.h" | 15 #include "jpeglib.h" |
16 #include "jsimd.h" | 16 #include "jsimd.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the | 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 | 59 * values for the G calculation are left scaled up, since we must add them |
60 * together before rounding. | 60 * together before rounding. |
61 */ | 61 */ |
62 | 62 |
63 #define SCALEBITS 16 /* speediest right-shift on some machines */ | 63 #define SCALEBITS 16 /* speediest right-shift on some machines */ |
64 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) | 64 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) |
65 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) | 65 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) |
66 | 66 |
67 | 67 |
| 68 /* Include inline routines for colorspace extensions */ |
| 69 |
| 70 #include "jdcolext.c" |
| 71 #undef RGB_RED |
| 72 #undef RGB_GREEN |
| 73 #undef RGB_BLUE |
| 74 #undef RGB_PIXELSIZE |
| 75 |
| 76 #define RGB_RED EXT_RGB_RED |
| 77 #define RGB_GREEN EXT_RGB_GREEN |
| 78 #define RGB_BLUE EXT_RGB_BLUE |
| 79 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE |
| 80 #define ycc_rgb_convert_internal ycc_extrgb_convert_internal |
| 81 #define gray_rgb_convert_internal gray_extrgb_convert_internal |
| 82 #include "jdcolext.c" |
| 83 #undef RGB_RED |
| 84 #undef RGB_GREEN |
| 85 #undef RGB_BLUE |
| 86 #undef RGB_PIXELSIZE |
| 87 #undef ycc_rgb_convert_internal |
| 88 #undef gray_rgb_convert_internal |
| 89 |
| 90 #define RGB_RED EXT_RGBX_RED |
| 91 #define RGB_GREEN EXT_RGBX_GREEN |
| 92 #define RGB_BLUE EXT_RGBX_BLUE |
| 93 #define RGB_ALPHA 3 |
| 94 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE |
| 95 #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal |
| 96 #define gray_rgb_convert_internal gray_extrgbx_convert_internal |
| 97 #include "jdcolext.c" |
| 98 #undef RGB_RED |
| 99 #undef RGB_GREEN |
| 100 #undef RGB_BLUE |
| 101 #undef RGB_ALPHA |
| 102 #undef RGB_PIXELSIZE |
| 103 #undef ycc_rgb_convert_internal |
| 104 #undef gray_rgb_convert_internal |
| 105 |
| 106 #define RGB_RED EXT_BGR_RED |
| 107 #define RGB_GREEN EXT_BGR_GREEN |
| 108 #define RGB_BLUE EXT_BGR_BLUE |
| 109 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE |
| 110 #define ycc_rgb_convert_internal ycc_extbgr_convert_internal |
| 111 #define gray_rgb_convert_internal gray_extbgr_convert_internal |
| 112 #include "jdcolext.c" |
| 113 #undef RGB_RED |
| 114 #undef RGB_GREEN |
| 115 #undef RGB_BLUE |
| 116 #undef RGB_PIXELSIZE |
| 117 #undef ycc_rgb_convert_internal |
| 118 #undef gray_rgb_convert_internal |
| 119 |
| 120 #define RGB_RED EXT_BGRX_RED |
| 121 #define RGB_GREEN EXT_BGRX_GREEN |
| 122 #define RGB_BLUE EXT_BGRX_BLUE |
| 123 #define RGB_ALPHA 3 |
| 124 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE |
| 125 #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal |
| 126 #define gray_rgb_convert_internal gray_extbgrx_convert_internal |
| 127 #include "jdcolext.c" |
| 128 #undef RGB_RED |
| 129 #undef RGB_GREEN |
| 130 #undef RGB_BLUE |
| 131 #undef RGB_ALPHA |
| 132 #undef RGB_PIXELSIZE |
| 133 #undef ycc_rgb_convert_internal |
| 134 #undef gray_rgb_convert_internal |
| 135 |
| 136 #define RGB_RED EXT_XBGR_RED |
| 137 #define RGB_GREEN EXT_XBGR_GREEN |
| 138 #define RGB_BLUE EXT_XBGR_BLUE |
| 139 #define RGB_ALPHA 0 |
| 140 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE |
| 141 #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal |
| 142 #define gray_rgb_convert_internal gray_extxbgr_convert_internal |
| 143 #include "jdcolext.c" |
| 144 #undef RGB_RED |
| 145 #undef RGB_GREEN |
| 146 #undef RGB_BLUE |
| 147 #undef RGB_ALPHA |
| 148 #undef RGB_PIXELSIZE |
| 149 #undef ycc_rgb_convert_internal |
| 150 #undef gray_rgb_convert_internal |
| 151 |
| 152 #define RGB_RED EXT_XRGB_RED |
| 153 #define RGB_GREEN EXT_XRGB_GREEN |
| 154 #define RGB_BLUE EXT_XRGB_BLUE |
| 155 #define RGB_ALPHA 0 |
| 156 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE |
| 157 #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal |
| 158 #define gray_rgb_convert_internal gray_extxrgb_convert_internal |
| 159 #include "jdcolext.c" |
| 160 #undef RGB_RED |
| 161 #undef RGB_GREEN |
| 162 #undef RGB_BLUE |
| 163 #undef RGB_ALPHA |
| 164 #undef RGB_PIXELSIZE |
| 165 #undef ycc_rgb_convert_internal |
| 166 #undef gray_rgb_convert_internal |
| 167 |
| 168 |
68 /* | 169 /* |
69 * Initialize tables for YCC->RGB colorspace conversion. | 170 * Initialize tables for YCC->RGB colorspace conversion. |
70 */ | 171 */ |
71 | 172 |
72 LOCAL(void) | 173 LOCAL(void) |
73 build_ycc_rgb_table (j_decompress_ptr cinfo) | 174 build_ycc_rgb_table (j_decompress_ptr cinfo) |
74 { | 175 { |
75 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; | 176 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
76 int i; | 177 int i; |
77 INT32 x; | 178 INT32 x; |
(...skipping 25 matching lines...) Expand all Loading... |
103 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x; | 204 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x; |
104 /* Cb=>G value is scaled-up -0.34414 * x */ | 205 /* 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 */ | 206 /* We also add in ONE_HALF so that need not do it in inner loop */ |
106 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; | 207 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; |
107 } | 208 } |
108 } | 209 } |
109 | 210 |
110 | 211 |
111 /* | 212 /* |
112 * Convert some rows of samples to the output colorspace. | 213 * Convert some rows of samples to the output colorspace. |
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. | |
120 */ | 214 */ |
121 | 215 |
122 METHODDEF(void) | 216 METHODDEF(void) |
123 ycc_rgb_convert (j_decompress_ptr cinfo, | 217 ycc_rgb_convert (j_decompress_ptr cinfo, |
124 JSAMPIMAGE input_buf, JDIMENSION input_row, | 218 JSAMPIMAGE input_buf, JDIMENSION input_row, |
125 JSAMPARRAY output_buf, int num_rows) | 219 JSAMPARRAY output_buf, int num_rows) |
126 { | 220 { |
127 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; | 221 switch (cinfo->out_color_space) { |
128 register int y, cb, cr; | 222 case JCS_EXT_RGB: |
129 register JSAMPROW outptr; | 223 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
130 register JSAMPROW inptr0, inptr1, inptr2; | 224 num_rows); |
131 register JDIMENSION col; | 225 break; |
132 JDIMENSION num_cols = cinfo->output_width; | 226 case JCS_EXT_RGBX: |
133 int rindex = rgb_red[cinfo->out_color_space]; | 227 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
134 int gindex = rgb_green[cinfo->out_color_space]; | 228 num_rows); |
135 int bindex = rgb_blue[cinfo->out_color_space]; | 229 break; |
136 int rgbstride = rgb_pixelsize[cinfo->out_color_space]; | 230 case JCS_EXT_BGR: |
137 /* copy these pointers into registers if possible */ | 231 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
138 register JSAMPLE * range_limit = cinfo->sample_range_limit; | 232 num_rows); |
139 register int * Crrtab = cconvert->Cr_r_tab; | 233 break; |
140 register int * Cbbtab = cconvert->Cb_b_tab; | 234 case JCS_EXT_BGRX: |
141 register INT32 * Crgtab = cconvert->Cr_g_tab; | 235 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
142 register INT32 * Cbgtab = cconvert->Cb_g_tab; | 236 num_rows); |
143 SHIFT_TEMPS | 237 break; |
144 | 238 case JCS_EXT_XBGR: |
145 while (--num_rows >= 0) { | 239 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
146 inptr0 = input_buf[0][input_row]; | 240 num_rows); |
147 inptr1 = input_buf[1][input_row]; | 241 break; |
148 inptr2 = input_buf[2][input_row]; | 242 case JCS_EXT_XRGB: |
149 input_row++; | 243 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
150 outptr = *output_buf++; | 244 num_rows); |
151 for (col = 0; col < num_cols; col++) { | 245 break; |
152 y = GETJSAMPLE(inptr0[col]); | 246 default: |
153 cb = GETJSAMPLE(inptr1[col]); | 247 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
154 cr = GETJSAMPLE(inptr2[col]); | 248 num_rows); |
155 /* Range-limiting is essential due to noise introduced by DCT losses. */ | 249 break; |
156 outptr[rindex] = range_limit[y + Crrtab[cr]]; | |
157 outptr[gindex] = range_limit[y + | |
158 » » » ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], | |
159 » » » » » » SCALEBITS))]; | |
160 outptr[bindex] = range_limit[y + Cbbtab[cb]]; | |
161 outptr += rgbstride; | |
162 } | |
163 } | 250 } |
164 } | 251 } |
165 | 252 |
166 | 253 |
167 /**************** Cases other than YCbCr -> RGB **************/ | 254 /**************** Cases other than YCbCr -> RGB **************/ |
168 | 255 |
169 | 256 |
170 /* | 257 /* |
171 * Color conversion for no colorspace change: just copy the data, | 258 * Color conversion for no colorspace change: just copy the data, |
172 * converting from separate-planes to interleaved representation. | 259 * converting from separate-planes to interleaved representation. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 grayscale_convert (j_decompress_ptr cinfo, | 295 grayscale_convert (j_decompress_ptr cinfo, |
209 JSAMPIMAGE input_buf, JDIMENSION input_row, | 296 JSAMPIMAGE input_buf, JDIMENSION input_row, |
210 JSAMPARRAY output_buf, int num_rows) | 297 JSAMPARRAY output_buf, int num_rows) |
211 { | 298 { |
212 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0, | 299 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0, |
213 num_rows, cinfo->output_width); | 300 num_rows, cinfo->output_width); |
214 } | 301 } |
215 | 302 |
216 | 303 |
217 /* | 304 /* |
218 * Convert grayscale to RGB: just duplicate the graylevel three times. | 305 * Convert grayscale to RGB |
219 * This is provided to support applications that don't want to cope | |
220 * with grayscale as a separate case. | |
221 */ | 306 */ |
222 | 307 |
223 METHODDEF(void) | 308 METHODDEF(void) |
224 gray_rgb_convert (j_decompress_ptr cinfo, | 309 gray_rgb_convert (j_decompress_ptr cinfo, |
225 JSAMPIMAGE input_buf, JDIMENSION input_row, | 310 JSAMPIMAGE input_buf, JDIMENSION input_row, |
226 JSAMPARRAY output_buf, int num_rows) | 311 JSAMPARRAY output_buf, int num_rows) |
227 { | 312 { |
228 register JSAMPROW inptr, outptr; | 313 switch (cinfo->out_color_space) { |
229 JSAMPLE *maxinptr; | 314 case JCS_EXT_RGB: |
230 JDIMENSION num_cols = cinfo->output_width; | 315 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
231 int rindex = rgb_red[cinfo->out_color_space]; | 316 num_rows); |
232 int gindex = rgb_green[cinfo->out_color_space]; | 317 break; |
233 int bindex = rgb_blue[cinfo->out_color_space]; | 318 case JCS_EXT_RGBX: |
234 int rgbstride = rgb_pixelsize[cinfo->out_color_space]; | 319 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
235 | 320 num_rows); |
236 while (--num_rows >= 0) { | 321 break; |
237 inptr = input_buf[0][input_row++]; | 322 case JCS_EXT_BGR: |
238 maxinptr = &inptr[num_cols]; | 323 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
239 outptr = *output_buf++; | 324 num_rows); |
240 for (; inptr < maxinptr; inptr++, outptr += rgbstride) { | 325 break; |
241 /* We can dispense with GETJSAMPLE() here */ | 326 case JCS_EXT_BGRX: |
242 outptr[rindex] = outptr[gindex] = outptr[bindex] = *inptr; | 327 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
243 } | 328 num_rows); |
| 329 break; |
| 330 case JCS_EXT_XBGR: |
| 331 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 332 num_rows); |
| 333 break; |
| 334 case JCS_EXT_XRGB: |
| 335 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 336 num_rows); |
| 337 break; |
| 338 default: |
| 339 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 340 num_rows); |
| 341 break; |
244 } | 342 } |
245 } | 343 } |
246 | 344 |
247 | 345 |
248 /* | 346 /* |
249 * Adobe-style YCCK->CMYK conversion. | 347 * Adobe-style YCCK->CMYK conversion. |
250 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same | 348 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same |
251 * conversion as above, while passing K (black) unchanged. | 349 * conversion as above, while passing K (black) unchanged. |
252 * We assume build_ycc_rgb_table has been called. | 350 * We assume build_ycc_rgb_table has been called. |
253 */ | 351 */ |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 } else /* unsupported non-null conversion */ | 507 } else /* unsupported non-null conversion */ |
410 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); | 508 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
411 break; | 509 break; |
412 } | 510 } |
413 | 511 |
414 if (cinfo->quantize_colors) | 512 if (cinfo->quantize_colors) |
415 cinfo->output_components = 1; /* single colormapped output component */ | 513 cinfo->output_components = 1; /* single colormapped output component */ |
416 else | 514 else |
417 cinfo->output_components = cinfo->out_color_components; | 515 cinfo->output_components = cinfo->out_color_components; |
418 } | 516 } |
OLD | NEW |