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 | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009, 2011, 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 | |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 93 | #define RGB_ALPHA 3 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 101 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 123 | #define RGB_ALPHA 3 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 131 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 139 | #define RGB_ALPHA 0 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 147 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 155 | #define RGB_ALPHA 0 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 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 |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 163 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 164 | #undef RGB_PIXELSIZE |
| 165 | #undef ycc_rgb_convert_internal |
| 166 | #undef gray_rgb_convert_internal |
| 167 | |
| 168 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 169 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 170 | * Initialize tables for YCC->RGB colorspace conversion. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 171 | */ |
| 172 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 173 | LOCAL(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 174 | build_ycc_rgb_table (j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 175 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 176 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 177 | int i; |
| 178 | INT32 x; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 179 | SHIFT_TEMPS |
| 180 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 181 | cconvert->Cr_r_tab = (int *) |
| 182 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 183 | (MAXJSAMPLE+1) * SIZEOF(int)); |
| 184 | cconvert->Cb_b_tab = (int *) |
| 185 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 186 | (MAXJSAMPLE+1) * SIZEOF(int)); |
| 187 | cconvert->Cr_g_tab = (INT32 *) |
| 188 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 189 | (MAXJSAMPLE+1) * SIZEOF(INT32)); |
| 190 | cconvert->Cb_g_tab = (INT32 *) |
| 191 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 192 | (MAXJSAMPLE+1) * SIZEOF(INT32)); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 193 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 194 | for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 195 | /* 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] | 196 | /* 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] | 197 | /* Cr=>R value is nearest int to 1.40200 * x */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 198 | cconvert->Cr_r_tab[i] = (int) |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 199 | RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 200 | /* Cb=>B value is nearest int to 1.77200 * x */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 201 | cconvert->Cb_b_tab[i] = (int) |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 202 | RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 203 | /* Cr=>G value is scaled-up -0.71414 * x */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 204 | cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 205 | /* Cb=>G value is scaled-up -0.34414 * x */ |
| 206 | /* 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] | 207 | cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 208 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
| 212 | /* |
| 213 | * Convert some rows of samples to the output colorspace. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 214 | */ |
| 215 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 216 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 217 | ycc_rgb_convert (j_decompress_ptr cinfo, |
| 218 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 219 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 220 | { |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 221 | switch (cinfo->out_color_space) { |
| 222 | case JCS_EXT_RGB: |
| 223 | ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 224 | num_rows); |
| 225 | break; |
| 226 | case JCS_EXT_RGBX: |
| 227 | ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 228 | num_rows); |
| 229 | break; |
| 230 | case JCS_EXT_BGR: |
| 231 | ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 232 | num_rows); |
| 233 | break; |
| 234 | case JCS_EXT_BGRX: |
| 235 | ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 236 | num_rows); |
| 237 | break; |
| 238 | case JCS_EXT_XBGR: |
| 239 | ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 240 | num_rows); |
| 241 | break; |
| 242 | case JCS_EXT_XRGB: |
| 243 | ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 244 | num_rows); |
| 245 | break; |
| 246 | default: |
| 247 | ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 248 | num_rows); |
| 249 | break; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 254 | /**************** Cases other than YCbCr -> RGB **************/ |
| 255 | |
| 256 | |
| 257 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 258 | * Color conversion for no colorspace change: just copy the data, |
| 259 | * converting from separate-planes to interleaved representation. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 260 | */ |
| 261 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 262 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 263 | null_convert (j_decompress_ptr cinfo, |
| 264 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 265 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 266 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 267 | register JSAMPROW inptr, outptr; |
| 268 | register JDIMENSION count; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 269 | register int num_components = cinfo->num_components; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 270 | JDIMENSION num_cols = cinfo->output_width; |
| 271 | int ci; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 272 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 273 | while (--num_rows >= 0) { |
| 274 | for (ci = 0; ci < num_components; ci++) { |
| 275 | inptr = input_buf[ci][input_row]; |
| 276 | outptr = output_buf[0] + ci; |
| 277 | for (count = num_cols; count > 0; count--) { |
| 278 | *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */ |
| 279 | outptr += num_components; |
| 280 | } |
| 281 | } |
| 282 | input_row++; |
| 283 | output_buf++; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /* |
| 289 | * Color conversion for grayscale: just copy the data. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 290 | * This also works for YCbCr -> grayscale conversion, in which |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 291 | * we just copy the Y (luminance) component and ignore chrominance. |
| 292 | */ |
| 293 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 294 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 295 | grayscale_convert (j_decompress_ptr cinfo, |
| 296 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 297 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 298 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 299 | jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0, |
| 300 | num_rows, cinfo->output_width); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
| 304 | /* |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 305 | * Convert grayscale to RGB |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 306 | */ |
| 307 | |
| 308 | METHODDEF(void) |
| 309 | gray_rgb_convert (j_decompress_ptr cinfo, |
| 310 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 311 | JSAMPARRAY output_buf, int num_rows) |
| 312 | { |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 313 | switch (cinfo->out_color_space) { |
| 314 | case JCS_EXT_RGB: |
| 315 | gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 316 | num_rows); |
| 317 | break; |
| 318 | case JCS_EXT_RGBX: |
| 319 | gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 320 | num_rows); |
| 321 | break; |
| 322 | case JCS_EXT_BGR: |
| 323 | gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 324 | num_rows); |
| 325 | break; |
| 326 | case JCS_EXT_BGRX: |
| 327 | gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 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; |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 347 | * Adobe-style YCCK->CMYK conversion. |
| 348 | * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same |
| 349 | * conversion as above, while passing K (black) unchanged. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 350 | * We assume build_ycc_rgb_table has been called. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 351 | */ |
| 352 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 353 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 354 | ycck_cmyk_convert (j_decompress_ptr cinfo, |
| 355 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 356 | JSAMPARRAY output_buf, int num_rows) |
| 357 | { |
| 358 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| 359 | register int y, cb, cr; |
| 360 | register JSAMPROW outptr; |
| 361 | register JSAMPROW inptr0, inptr1, inptr2, inptr3; |
| 362 | register JDIMENSION col; |
| 363 | JDIMENSION num_cols = cinfo->output_width; |
| 364 | /* copy these pointers into registers if possible */ |
| 365 | register JSAMPLE * range_limit = cinfo->sample_range_limit; |
| 366 | register int * Crrtab = cconvert->Cr_r_tab; |
| 367 | register int * Cbbtab = cconvert->Cb_b_tab; |
| 368 | register INT32 * Crgtab = cconvert->Cr_g_tab; |
| 369 | register INT32 * Cbgtab = cconvert->Cb_g_tab; |
| 370 | SHIFT_TEMPS |
| 371 | |
| 372 | while (--num_rows >= 0) { |
| 373 | inptr0 = input_buf[0][input_row]; |
| 374 | inptr1 = input_buf[1][input_row]; |
| 375 | inptr2 = input_buf[2][input_row]; |
| 376 | inptr3 = input_buf[3][input_row]; |
| 377 | input_row++; |
| 378 | outptr = *output_buf++; |
| 379 | for (col = 0; col < num_cols; col++) { |
| 380 | y = GETJSAMPLE(inptr0[col]); |
| 381 | cb = GETJSAMPLE(inptr1[col]); |
| 382 | cr = GETJSAMPLE(inptr2[col]); |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 383 | /* Range-limiting is essential due to noise introduced by DCT losses. */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 384 | outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */ |
| 385 | outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */ |
| 386 | ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], |
| 387 | SCALEBITS)))]; |
| 388 | outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */ |
| 389 | /* K passes through unchanged */ |
| 390 | outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */ |
| 391 | outptr += 4; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /* |
| 398 | * Empty method for start_pass. |
| 399 | */ |
| 400 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 401 | METHODDEF(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 402 | start_pass_dcolor (j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 403 | { |
| 404 | /* no work needed */ |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 409 | * Module initialization routine for output colorspace conversion. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 410 | */ |
| 411 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 412 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 413 | jinit_color_deconverter (j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 414 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 415 | my_cconvert_ptr cconvert; |
Thomas G. Lane | cc7150e | 1993-02-18 00:00:00 +0000 | [diff] [blame] | 416 | int ci; |
| 417 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 418 | cconvert = (my_cconvert_ptr) |
| 419 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 420 | SIZEOF(my_color_deconverter)); |
| 421 | cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 422 | cconvert->pub.start_pass = start_pass_dcolor; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 423 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 424 | /* Make sure num_components agrees with jpeg_color_space */ |
| 425 | switch (cinfo->jpeg_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 426 | case JCS_GRAYSCALE: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 427 | if (cinfo->num_components != 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 428 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 429 | break; |
| 430 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 431 | case JCS_RGB: |
| 432 | case JCS_YCbCr: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 433 | if (cinfo->num_components != 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 434 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 435 | break; |
| 436 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 437 | case JCS_CMYK: |
| 438 | case JCS_YCCK: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 439 | if (cinfo->num_components != 4) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 440 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 441 | break; |
| 442 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 443 | default: /* JCS_UNKNOWN can be anything */ |
| 444 | if (cinfo->num_components < 1) |
| 445 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 449 | /* Set out_color_components and conversion method based on requested space. |
| 450 | * Also clear the component_needed flags for any unused components, |
| 451 | * so that earlier pipeline stages can avoid useless computation. |
| 452 | */ |
Thomas G. Lane | cc7150e | 1993-02-18 00:00:00 +0000 | [diff] [blame] | 453 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 454 | switch (cinfo->out_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 455 | case JCS_GRAYSCALE: |
| 456 | cinfo->out_color_components = 1; |
| 457 | if (cinfo->jpeg_color_space == JCS_GRAYSCALE || |
| 458 | cinfo->jpeg_color_space == JCS_YCbCr) { |
| 459 | cconvert->pub.color_convert = grayscale_convert; |
Thomas G. Lane | cc7150e | 1993-02-18 00:00:00 +0000 | [diff] [blame] | 460 | /* For color->grayscale conversion, only the Y (0) component is needed */ |
| 461 | for (ci = 1; ci < cinfo->num_components; ci++) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 462 | cinfo->comp_info[ci].component_needed = FALSE; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 463 | } else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 464 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 465 | break; |
| 466 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 467 | case JCS_RGB: |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 468 | case JCS_EXT_RGB: |
| 469 | case JCS_EXT_RGBX: |
| 470 | case JCS_EXT_BGR: |
| 471 | case JCS_EXT_BGRX: |
| 472 | case JCS_EXT_XBGR: |
| 473 | case JCS_EXT_XRGB: |
| 474 | cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 475 | if (cinfo->jpeg_color_space == JCS_YCbCr) { |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 476 | if (jsimd_can_ycc_rgb()) |
| 477 | cconvert->pub.color_convert = jsimd_ycc_rgb_convert; |
| 478 | else { |
| 479 | cconvert->pub.color_convert = ycc_rgb_convert; |
| 480 | build_ycc_rgb_table(cinfo); |
| 481 | } |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 482 | } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { |
| 483 | cconvert->pub.color_convert = gray_rgb_convert; |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 484 | } else if (cinfo->jpeg_color_space == cinfo->out_color_space && |
| 485 | rgb_pixelsize[cinfo->out_color_space] == 3) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 486 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 487 | } else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 488 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 489 | break; |
| 490 | |
| 491 | case JCS_CMYK: |
| 492 | cinfo->out_color_components = 4; |
| 493 | if (cinfo->jpeg_color_space == JCS_YCCK) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 494 | cconvert->pub.color_convert = ycck_cmyk_convert; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 495 | build_ycc_rgb_table(cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 496 | } else if (cinfo->jpeg_color_space == JCS_CMYK) { |
| 497 | cconvert->pub.color_convert = null_convert; |
| 498 | } else |
| 499 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 500 | break; |
| 501 | |
| 502 | default: |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 503 | /* Permit null conversion to same output space */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 504 | if (cinfo->out_color_space == cinfo->jpeg_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 505 | cinfo->out_color_components = cinfo->num_components; |
| 506 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 507 | } else /* unsupported non-null conversion */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 508 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | |
| 512 | if (cinfo->quantize_colors) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 513 | cinfo->output_components = 1; /* single colormapped output component */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 514 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 515 | cinfo->output_components = cinfo->out_color_components; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 516 | } |