Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdcolor.c |
| 3 | * |
DRC | a73e870 | 2012-12-31 02:52:30 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1997, Thomas G. Lane. |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 6 | * Modified 2011 by Guido Vollbeding. |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 7 | * libjpeg-turbo Modifications: |
DRC | cf763c0 | 2013-01-01 09:51:37 +0000 | [diff] [blame] | 8 | * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 9 | * Copyright (C) 2009, 2011-2012, D. R. Commander. |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 10 | * Copyright (C) 2013, Linaro Limited. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 11 | * For conditions of distribution and use, see the accompanying README file. |
| 12 | * |
| 13 | * This file contains output colorspace conversion routines. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 16 | #define JPEG_INTERNALS |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 17 | #include "jinclude.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 18 | #include "jpeglib.h" |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 19 | #include "jsimd.h" |
DRC | ff6961f | 2014-04-20 09:17:11 +0000 | [diff] [blame] | 20 | #include "jconfigint.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | /* Private subobject */ |
| 24 | |
| 25 | typedef struct { |
| 26 | struct jpeg_color_deconverter pub; /* public fields */ |
| 27 | |
| 28 | /* Private state for YCC->RGB conversion */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 29 | int * Cr_r_tab; /* => table for Cr to R conversion */ |
| 30 | int * Cb_b_tab; /* => table for Cb to B conversion */ |
| 31 | INT32 * Cr_g_tab; /* => table for Cr to G conversion */ |
| 32 | INT32 * Cb_g_tab; /* => table for Cb to G conversion */ |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 33 | |
| 34 | /* Private state for RGB->Y conversion */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 35 | INT32 * rgb_y_tab; /* => table for RGB to Y conversion */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 36 | } my_color_deconverter; |
| 37 | |
| 38 | typedef my_color_deconverter * my_cconvert_ptr; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 39 | |
| 40 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 41 | /**************** YCbCr -> RGB conversion: most common case **************/ |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 42 | /**************** RGB -> Y conversion: less common case **************/ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * YCbCr is defined per CCIR 601-1, except that Cb and Cr are |
| 46 | * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. |
| 47 | * The conversion equations to be implemented are therefore |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 48 | * |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 49 | * R = Y + 1.40200 * Cr |
| 50 | * G = Y - 0.34414 * Cb - 0.71414 * Cr |
| 51 | * B = Y + 1.77200 * Cb |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 52 | * |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 53 | * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 54 | * |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 55 | * where Cb and Cr represent the incoming values less CENTERJSAMPLE. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 56 | * (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] | 57 | * |
| 58 | * To avoid floating-point arithmetic, we represent the fractional constants |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 59 | * as integers scaled up by 2^16 (about 4 digits precision); we have to divide |
| 60 | * 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] | 61 | * Notice that Y, being an integral input, does not contribute any fraction |
| 62 | * so it need not participate in the rounding. |
| 63 | * |
| 64 | * For even more speed, we avoid doing any multiplications in the inner loop |
| 65 | * 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] | 66 | * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); |
| 67 | * for 12-bit samples it is still acceptable. It's not very reasonable for |
| 68 | * 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] | 69 | * colorspace anyway. |
| 70 | * The Cr=>R and Cb=>B values can be rounded to integers in advance; the |
| 71 | * values for the G calculation are left scaled up, since we must add them |
| 72 | * together before rounding. |
| 73 | */ |
| 74 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 75 | #define SCALEBITS 16 /* speediest right-shift on some machines */ |
| 76 | #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) |
| 77 | #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 78 | |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 79 | /* We allocate one big table for RGB->Y conversion and divide it up into |
| 80 | * three parts, instead of doing three alloc_small requests. This lets us |
| 81 | * use a single table base address, which can be held in a register in the |
| 82 | * inner loops on many machines (more than can hold all three addresses, |
| 83 | * anyway). |
| 84 | */ |
| 85 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 86 | #define R_Y_OFF 0 /* offset to R => Y section */ |
| 87 | #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ |
| 88 | #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ |
| 89 | #define TABLE_SIZE (3*(MAXJSAMPLE+1)) |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 90 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 91 | |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 92 | /* Include inline routines for colorspace extensions */ |
| 93 | |
| 94 | #include "jdcolext.c" |
| 95 | #undef RGB_RED |
| 96 | #undef RGB_GREEN |
| 97 | #undef RGB_BLUE |
| 98 | #undef RGB_PIXELSIZE |
| 99 | |
| 100 | #define RGB_RED EXT_RGB_RED |
| 101 | #define RGB_GREEN EXT_RGB_GREEN |
| 102 | #define RGB_BLUE EXT_RGB_BLUE |
| 103 | #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE |
| 104 | #define ycc_rgb_convert_internal ycc_extrgb_convert_internal |
| 105 | #define gray_rgb_convert_internal gray_extrgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 106 | #define rgb_rgb_convert_internal rgb_extrgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 107 | #include "jdcolext.c" |
| 108 | #undef RGB_RED |
| 109 | #undef RGB_GREEN |
| 110 | #undef RGB_BLUE |
| 111 | #undef RGB_PIXELSIZE |
| 112 | #undef ycc_rgb_convert_internal |
| 113 | #undef gray_rgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 114 | #undef rgb_rgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 115 | |
| 116 | #define RGB_RED EXT_RGBX_RED |
| 117 | #define RGB_GREEN EXT_RGBX_GREEN |
| 118 | #define RGB_BLUE EXT_RGBX_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 119 | #define RGB_ALPHA 3 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 120 | #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE |
| 121 | #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal |
| 122 | #define gray_rgb_convert_internal gray_extrgbx_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 123 | #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 124 | #include "jdcolext.c" |
| 125 | #undef RGB_RED |
| 126 | #undef RGB_GREEN |
| 127 | #undef RGB_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 128 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 129 | #undef RGB_PIXELSIZE |
| 130 | #undef ycc_rgb_convert_internal |
| 131 | #undef gray_rgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 132 | #undef rgb_rgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 133 | |
| 134 | #define RGB_RED EXT_BGR_RED |
| 135 | #define RGB_GREEN EXT_BGR_GREEN |
| 136 | #define RGB_BLUE EXT_BGR_BLUE |
| 137 | #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE |
| 138 | #define ycc_rgb_convert_internal ycc_extbgr_convert_internal |
| 139 | #define gray_rgb_convert_internal gray_extbgr_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 140 | #define rgb_rgb_convert_internal rgb_extbgr_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 141 | #include "jdcolext.c" |
| 142 | #undef RGB_RED |
| 143 | #undef RGB_GREEN |
| 144 | #undef RGB_BLUE |
| 145 | #undef RGB_PIXELSIZE |
| 146 | #undef ycc_rgb_convert_internal |
| 147 | #undef gray_rgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 148 | #undef rgb_rgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 149 | |
| 150 | #define RGB_RED EXT_BGRX_RED |
| 151 | #define RGB_GREEN EXT_BGRX_GREEN |
| 152 | #define RGB_BLUE EXT_BGRX_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 153 | #define RGB_ALPHA 3 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 154 | #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE |
| 155 | #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal |
| 156 | #define gray_rgb_convert_internal gray_extbgrx_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 157 | #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 158 | #include "jdcolext.c" |
| 159 | #undef RGB_RED |
| 160 | #undef RGB_GREEN |
| 161 | #undef RGB_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 162 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 163 | #undef RGB_PIXELSIZE |
| 164 | #undef ycc_rgb_convert_internal |
| 165 | #undef gray_rgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 166 | #undef rgb_rgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 167 | |
| 168 | #define RGB_RED EXT_XBGR_RED |
| 169 | #define RGB_GREEN EXT_XBGR_GREEN |
| 170 | #define RGB_BLUE EXT_XBGR_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 171 | #define RGB_ALPHA 0 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 172 | #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE |
| 173 | #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal |
| 174 | #define gray_rgb_convert_internal gray_extxbgr_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 175 | #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 176 | #include "jdcolext.c" |
| 177 | #undef RGB_RED |
| 178 | #undef RGB_GREEN |
| 179 | #undef RGB_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 180 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 181 | #undef RGB_PIXELSIZE |
| 182 | #undef ycc_rgb_convert_internal |
| 183 | #undef gray_rgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 184 | #undef rgb_rgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 185 | |
| 186 | #define RGB_RED EXT_XRGB_RED |
| 187 | #define RGB_GREEN EXT_XRGB_GREEN |
| 188 | #define RGB_BLUE EXT_XRGB_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 189 | #define RGB_ALPHA 0 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 190 | #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE |
| 191 | #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal |
| 192 | #define gray_rgb_convert_internal gray_extxrgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 193 | #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 194 | #include "jdcolext.c" |
| 195 | #undef RGB_RED |
| 196 | #undef RGB_GREEN |
| 197 | #undef RGB_BLUE |
DRC | d89e01f | 2011-09-09 18:15:53 +0000 | [diff] [blame] | 198 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 199 | #undef RGB_PIXELSIZE |
| 200 | #undef ycc_rgb_convert_internal |
| 201 | #undef gray_rgb_convert_internal |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 202 | #undef rgb_rgb_convert_internal |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 203 | |
| 204 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 205 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 206 | * Initialize tables for YCC->RGB colorspace conversion. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 207 | */ |
| 208 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 209 | LOCAL(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 210 | build_ycc_rgb_table (j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 211 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 212 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 213 | int i; |
| 214 | INT32 x; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 215 | SHIFT_TEMPS |
| 216 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 217 | cconvert->Cr_r_tab = (int *) |
| 218 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 219 | (MAXJSAMPLE+1) * sizeof(int)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 220 | cconvert->Cb_b_tab = (int *) |
| 221 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 222 | (MAXJSAMPLE+1) * sizeof(int)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 223 | cconvert->Cr_g_tab = (INT32 *) |
| 224 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 225 | (MAXJSAMPLE+1) * sizeof(INT32)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 226 | cconvert->Cb_g_tab = (INT32 *) |
| 227 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 228 | (MAXJSAMPLE+1) * sizeof(INT32)); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 229 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 230 | for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 231 | /* 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] | 232 | /* 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] | 233 | /* Cr=>R value is nearest int to 1.40200 * x */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 234 | cconvert->Cr_r_tab[i] = (int) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 235 | RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 236 | /* Cb=>B value is nearest int to 1.77200 * x */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 237 | cconvert->Cb_b_tab[i] = (int) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 238 | RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 239 | /* Cr=>G value is scaled-up -0.71414 * x */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 240 | cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 241 | /* Cb=>G value is scaled-up -0.34414 * x */ |
| 242 | /* 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] | 243 | cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 244 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | * Convert some rows of samples to the output colorspace. |
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 | ycc_rgb_convert (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 254 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 255 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 256 | { |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 257 | switch (cinfo->out_color_space) { |
| 258 | case JCS_EXT_RGB: |
| 259 | ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 260 | num_rows); |
| 261 | break; |
| 262 | case JCS_EXT_RGBX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 263 | case JCS_EXT_RGBA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 264 | ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 265 | num_rows); |
| 266 | break; |
| 267 | case JCS_EXT_BGR: |
| 268 | ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 269 | num_rows); |
| 270 | break; |
| 271 | case JCS_EXT_BGRX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 272 | case JCS_EXT_BGRA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 273 | ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 274 | num_rows); |
| 275 | break; |
| 276 | case JCS_EXT_XBGR: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 277 | case JCS_EXT_ABGR: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 278 | ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 279 | num_rows); |
| 280 | break; |
| 281 | case JCS_EXT_XRGB: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 282 | case JCS_EXT_ARGB: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 283 | ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 284 | num_rows); |
| 285 | break; |
| 286 | default: |
| 287 | ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 288 | num_rows); |
| 289 | break; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 294 | /**************** Cases other than YCbCr -> RGB **************/ |
| 295 | |
| 296 | |
| 297 | /* |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 298 | * Initialize for RGB->grayscale colorspace conversion. |
| 299 | */ |
| 300 | |
| 301 | LOCAL(void) |
| 302 | build_rgb_y_table (j_decompress_ptr cinfo) |
| 303 | { |
| 304 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| 305 | INT32 * rgb_y_tab; |
| 306 | INT32 i; |
| 307 | |
| 308 | /* Allocate and fill in the conversion tables. */ |
| 309 | cconvert->rgb_y_tab = rgb_y_tab = (INT32 *) |
| 310 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 311 | (TABLE_SIZE * sizeof(INT32))); |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 312 | |
| 313 | for (i = 0; i <= MAXJSAMPLE; i++) { |
| 314 | rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i; |
| 315 | rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i; |
| 316 | rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /* |
| 322 | * Convert RGB to grayscale. |
| 323 | */ |
| 324 | |
| 325 | METHODDEF(void) |
| 326 | rgb_gray_convert (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 327 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 328 | JSAMPARRAY output_buf, int num_rows) |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 329 | { |
| 330 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| 331 | register int r, g, b; |
| 332 | register INT32 * ctab = cconvert->rgb_y_tab; |
| 333 | register JSAMPROW outptr; |
| 334 | register JSAMPROW inptr0, inptr1, inptr2; |
| 335 | register JDIMENSION col; |
| 336 | JDIMENSION num_cols = cinfo->output_width; |
| 337 | |
| 338 | while (--num_rows >= 0) { |
| 339 | inptr0 = input_buf[0][input_row]; |
| 340 | inptr1 = input_buf[1][input_row]; |
| 341 | inptr2 = input_buf[2][input_row]; |
| 342 | input_row++; |
| 343 | outptr = *output_buf++; |
| 344 | for (col = 0; col < num_cols; col++) { |
| 345 | r = GETJSAMPLE(inptr0[col]); |
| 346 | g = GETJSAMPLE(inptr1[col]); |
| 347 | b = GETJSAMPLE(inptr2[col]); |
| 348 | /* Y */ |
| 349 | outptr[col] = (JSAMPLE) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 350 | ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| 351 | >> SCALEBITS); |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 358 | * Color conversion for no colorspace change: just copy the data, |
| 359 | * converting from separate-planes to interleaved representation. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 360 | */ |
| 361 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 362 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 363 | null_convert (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 364 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 365 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 366 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 367 | register JSAMPROW inptr, outptr; |
| 368 | register JDIMENSION count; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 369 | register int num_components = cinfo->num_components; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 370 | JDIMENSION num_cols = cinfo->output_width; |
| 371 | int ci; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 372 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 373 | while (--num_rows >= 0) { |
| 374 | for (ci = 0; ci < num_components; ci++) { |
| 375 | inptr = input_buf[ci][input_row]; |
| 376 | outptr = output_buf[0] + ci; |
| 377 | for (count = num_cols; count > 0; count--) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 378 | *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */ |
| 379 | outptr += num_components; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | input_row++; |
| 383 | output_buf++; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /* |
| 389 | * Color conversion for grayscale: just copy the data. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 390 | * This also works for YCbCr -> grayscale conversion, in which |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 391 | * we just copy the Y (luminance) component and ignore chrominance. |
| 392 | */ |
| 393 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 394 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 395 | grayscale_convert (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 396 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 397 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 398 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 399 | jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 400 | num_rows, cinfo->output_width); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | /* |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 405 | * Convert grayscale to RGB |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 406 | */ |
| 407 | |
| 408 | METHODDEF(void) |
| 409 | gray_rgb_convert (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 410 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 411 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 412 | { |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 413 | switch (cinfo->out_color_space) { |
| 414 | case JCS_EXT_RGB: |
| 415 | gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 416 | num_rows); |
| 417 | break; |
| 418 | case JCS_EXT_RGBX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 419 | case JCS_EXT_RGBA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 420 | gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 421 | num_rows); |
| 422 | break; |
| 423 | case JCS_EXT_BGR: |
| 424 | gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 425 | num_rows); |
| 426 | break; |
| 427 | case JCS_EXT_BGRX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 428 | case JCS_EXT_BGRA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 429 | gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 430 | num_rows); |
| 431 | break; |
| 432 | case JCS_EXT_XBGR: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 433 | case JCS_EXT_ABGR: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 434 | gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 435 | num_rows); |
| 436 | break; |
| 437 | case JCS_EXT_XRGB: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 438 | case JCS_EXT_ARGB: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 439 | gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 440 | num_rows); |
| 441 | break; |
| 442 | default: |
| 443 | gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 444 | num_rows); |
| 445 | break; |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /* |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 451 | * Convert plain RGB to extended RGB |
| 452 | */ |
| 453 | |
| 454 | METHODDEF(void) |
| 455 | rgb_rgb_convert (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 456 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 457 | JSAMPARRAY output_buf, int num_rows) |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 458 | { |
| 459 | switch (cinfo->out_color_space) { |
| 460 | case JCS_EXT_RGB: |
| 461 | rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 462 | num_rows); |
| 463 | break; |
| 464 | case JCS_EXT_RGBX: |
| 465 | case JCS_EXT_RGBA: |
| 466 | rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 467 | num_rows); |
| 468 | break; |
| 469 | case JCS_EXT_BGR: |
| 470 | rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 471 | num_rows); |
| 472 | break; |
| 473 | case JCS_EXT_BGRX: |
| 474 | case JCS_EXT_BGRA: |
| 475 | rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 476 | num_rows); |
| 477 | break; |
| 478 | case JCS_EXT_XBGR: |
| 479 | case JCS_EXT_ABGR: |
| 480 | rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 481 | num_rows); |
| 482 | break; |
| 483 | case JCS_EXT_XRGB: |
| 484 | case JCS_EXT_ARGB: |
| 485 | rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 486 | num_rows); |
| 487 | break; |
| 488 | default: |
| 489 | rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, |
| 490 | num_rows); |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 495 | |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 496 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 497 | * Adobe-style YCCK->CMYK conversion. |
| 498 | * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same |
| 499 | * conversion as above, while passing K (black) unchanged. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 500 | * We assume build_ycc_rgb_table has been called. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 501 | */ |
| 502 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 503 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 504 | ycck_cmyk_convert (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 505 | JSAMPIMAGE input_buf, JDIMENSION input_row, |
| 506 | JSAMPARRAY output_buf, int num_rows) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 507 | { |
| 508 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| 509 | register int y, cb, cr; |
| 510 | register JSAMPROW outptr; |
| 511 | register JSAMPROW inptr0, inptr1, inptr2, inptr3; |
| 512 | register JDIMENSION col; |
| 513 | JDIMENSION num_cols = cinfo->output_width; |
| 514 | /* copy these pointers into registers if possible */ |
| 515 | register JSAMPLE * range_limit = cinfo->sample_range_limit; |
| 516 | register int * Crrtab = cconvert->Cr_r_tab; |
| 517 | register int * Cbbtab = cconvert->Cb_b_tab; |
| 518 | register INT32 * Crgtab = cconvert->Cr_g_tab; |
| 519 | register INT32 * Cbgtab = cconvert->Cb_g_tab; |
| 520 | SHIFT_TEMPS |
| 521 | |
| 522 | while (--num_rows >= 0) { |
| 523 | inptr0 = input_buf[0][input_row]; |
| 524 | inptr1 = input_buf[1][input_row]; |
| 525 | inptr2 = input_buf[2][input_row]; |
| 526 | inptr3 = input_buf[3][input_row]; |
| 527 | input_row++; |
| 528 | outptr = *output_buf++; |
| 529 | for (col = 0; col < num_cols; col++) { |
| 530 | y = GETJSAMPLE(inptr0[col]); |
| 531 | cb = GETJSAMPLE(inptr1[col]); |
| 532 | cr = GETJSAMPLE(inptr2[col]); |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 533 | /* Range-limiting is essential due to noise introduced by DCT losses. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 534 | outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */ |
| 535 | outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */ |
| 536 | ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], |
| 537 | SCALEBITS)))]; |
| 538 | outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 539 | /* K passes through unchanged */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 540 | outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 541 | outptr += 4; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 547 | #include "jdcol565.c" |
| 548 | |
| 549 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 550 | /* |
| 551 | * Empty method for start_pass. |
| 552 | */ |
| 553 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 554 | METHODDEF(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 555 | start_pass_dcolor (j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 556 | { |
| 557 | /* no work needed */ |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 562 | * Module initialization routine for output colorspace conversion. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 563 | */ |
| 564 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 565 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 566 | jinit_color_deconverter (j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 567 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 568 | my_cconvert_ptr cconvert; |
Thomas G. Lane | cc7150e | 1993-02-18 00:00:00 +0000 | [diff] [blame] | 569 | int ci; |
| 570 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 571 | cconvert = (my_cconvert_ptr) |
| 572 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 573 | sizeof(my_color_deconverter)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 574 | cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 575 | cconvert->pub.start_pass = start_pass_dcolor; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 576 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 577 | /* Make sure num_components agrees with jpeg_color_space */ |
| 578 | switch (cinfo->jpeg_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 579 | case JCS_GRAYSCALE: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 580 | if (cinfo->num_components != 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 581 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 582 | break; |
| 583 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 584 | case JCS_RGB: |
| 585 | case JCS_YCbCr: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 586 | if (cinfo->num_components != 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 587 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 588 | break; |
| 589 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 590 | case JCS_CMYK: |
| 591 | case JCS_YCCK: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 592 | if (cinfo->num_components != 4) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 593 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 594 | break; |
| 595 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 596 | default: /* JCS_UNKNOWN can be anything */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 597 | if (cinfo->num_components < 1) |
| 598 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 599 | break; |
| 600 | } |
| 601 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 602 | /* Set out_color_components and conversion method based on requested space. |
| 603 | * Also clear the component_needed flags for any unused components, |
| 604 | * so that earlier pipeline stages can avoid useless computation. |
| 605 | */ |
Thomas G. Lane | cc7150e | 1993-02-18 00:00:00 +0000 | [diff] [blame] | 606 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 607 | switch (cinfo->out_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 608 | case JCS_GRAYSCALE: |
| 609 | cinfo->out_color_components = 1; |
| 610 | if (cinfo->jpeg_color_space == JCS_GRAYSCALE || |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 611 | cinfo->jpeg_color_space == JCS_YCbCr) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 612 | cconvert->pub.color_convert = grayscale_convert; |
Thomas G. Lane | cc7150e | 1993-02-18 00:00:00 +0000 | [diff] [blame] | 613 | /* For color->grayscale conversion, only the Y (0) component is needed */ |
| 614 | for (ci = 1; ci < cinfo->num_components; ci++) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 615 | cinfo->comp_info[ci].component_needed = FALSE; |
Guido Vollbeding | 5829cb2 | 2012-01-15 00:00:00 +0000 | [diff] [blame] | 616 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
| 617 | cconvert->pub.color_convert = rgb_gray_convert; |
| 618 | build_rgb_y_table(cinfo); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 619 | } else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 620 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 621 | break; |
| 622 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 623 | case JCS_RGB: |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 624 | case JCS_EXT_RGB: |
| 625 | case JCS_EXT_RGBX: |
| 626 | case JCS_EXT_BGR: |
| 627 | case JCS_EXT_BGRX: |
| 628 | case JCS_EXT_XBGR: |
| 629 | case JCS_EXT_XRGB: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 630 | case JCS_EXT_RGBA: |
| 631 | case JCS_EXT_BGRA: |
| 632 | case JCS_EXT_ABGR: |
| 633 | case JCS_EXT_ARGB: |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 634 | cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 635 | if (cinfo->jpeg_color_space == JCS_YCbCr) { |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 636 | if (jsimd_can_ycc_rgb()) |
| 637 | cconvert->pub.color_convert = jsimd_ycc_rgb_convert; |
| 638 | else { |
| 639 | cconvert->pub.color_convert = ycc_rgb_convert; |
| 640 | build_ycc_rgb_table(cinfo); |
| 641 | } |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 642 | } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { |
| 643 | cconvert->pub.color_convert = gray_rgb_convert; |
DRC | a9b646c | 2012-03-11 22:06:54 +0000 | [diff] [blame] | 644 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
| 645 | if (rgb_red[cinfo->out_color_space] == 0 && |
| 646 | rgb_green[cinfo->out_color_space] == 1 && |
| 647 | rgb_blue[cinfo->out_color_space] == 2 && |
| 648 | rgb_pixelsize[cinfo->out_color_space] == 3) |
| 649 | cconvert->pub.color_convert = null_convert; |
| 650 | else |
| 651 | cconvert->pub.color_convert = rgb_rgb_convert; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 652 | } else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 653 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 654 | break; |
| 655 | |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 656 | case JCS_RGB565: |
| 657 | cinfo->out_color_components = 3; |
| 658 | if (cinfo->dither_mode == JDITHER_NONE) { |
| 659 | if (cinfo->jpeg_color_space == JCS_YCbCr) { |
| 660 | cconvert->pub.color_convert = ycc_rgb565_convert; |
| 661 | build_ycc_rgb_table(cinfo); |
| 662 | } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { |
| 663 | cconvert->pub.color_convert = gray_rgb565_convert; |
| 664 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
| 665 | cconvert->pub.color_convert = rgb_rgb565_convert; |
| 666 | } else |
| 667 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 668 | } else { |
| 669 | /* only ordered dithering is supported */ |
| 670 | if (cinfo->jpeg_color_space == JCS_YCbCr) { |
| 671 | cconvert->pub.color_convert = ycc_rgb565D_convert; |
| 672 | build_ycc_rgb_table(cinfo); |
| 673 | } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { |
| 674 | cconvert->pub.color_convert = gray_rgb565D_convert; |
| 675 | } else if (cinfo->jpeg_color_space == JCS_RGB) { |
| 676 | cconvert->pub.color_convert = rgb_rgb565D_convert; |
| 677 | } else |
| 678 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 679 | } |
| 680 | break; |
| 681 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 682 | case JCS_CMYK: |
| 683 | cinfo->out_color_components = 4; |
| 684 | if (cinfo->jpeg_color_space == JCS_YCCK) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 685 | cconvert->pub.color_convert = ycck_cmyk_convert; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 686 | build_ycc_rgb_table(cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 687 | } else if (cinfo->jpeg_color_space == JCS_CMYK) { |
| 688 | cconvert->pub.color_convert = null_convert; |
| 689 | } else |
| 690 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 691 | break; |
| 692 | |
| 693 | default: |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 694 | /* Permit null conversion to same output space */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 695 | if (cinfo->out_color_space == cinfo->jpeg_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 696 | cinfo->out_color_components = cinfo->num_components; |
| 697 | cconvert->pub.color_convert = null_convert; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 698 | } else /* unsupported non-null conversion */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 699 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 700 | break; |
| 701 | } |
| 702 | |
| 703 | if (cinfo->quantize_colors) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 704 | cinfo->output_components = 1; /* single colormapped output component */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 705 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 706 | cinfo->output_components = cinfo->out_color_components; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 707 | } |