Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jccolor.c |
| 3 | * |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 4 | * Copyright (C) 1991-1996, 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 | 5a9d6c3 | 2011-02-27 10:26:08 +0000 | [diff] [blame] | 6 | * Copyright 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 input 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 | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 17 | |
| 18 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 19 | /* Private subobject */ |
| 20 | |
| 21 | typedef struct { |
| 22 | struct jpeg_color_converter pub; /* public fields */ |
| 23 | |
| 24 | /* Private state for RGB->YCC conversion */ |
| 25 | INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ |
| 26 | } my_color_converter; |
| 27 | |
| 28 | typedef my_color_converter * my_cconvert_ptr; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 29 | |
| 30 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 31 | /**************** RGB -> YCbCr conversion: most common case **************/ |
| 32 | |
| 33 | /* |
| 34 | * YCbCr is defined per CCIR 601-1, except that Cb and Cr are |
| 35 | * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. |
| 36 | * The conversion equations to be implemented are therefore |
| 37 | * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 38 | * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE |
| 39 | * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 40 | * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 41 | * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, |
| 42 | * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and |
| 43 | * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) |
| 44 | * were not represented exactly. Now we sacrifice exact representation of |
| 45 | * maximum red and maximum blue in order to get exact grayscales. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 46 | * |
| 47 | * To avoid floating-point arithmetic, we represent the fractional constants |
| 48 | * as integers scaled up by 2^16 (about 4 digits precision); we have to divide |
| 49 | * the products by 2^16, with appropriate rounding, to get the correct answer. |
| 50 | * |
| 51 | * For even more speed, we avoid doing any multiplications in the inner loop |
| 52 | * by precalculating the constants times R,G,B for all possible values. |
| 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 |
| 56 | * colorspace anyway. |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 57 | * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 58 | * in the tables to save adding them separately in the inner loop. |
| 59 | */ |
| 60 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 61 | #define SCALEBITS 16 /* speediest right-shift on some machines */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 62 | #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) |
Thomas G. Lane | 88aeed4 | 1992-12-10 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 | |
| 66 | /* We allocate one big table and divide it up into eight parts, instead of |
| 67 | * doing eight alloc_small requests. This lets us use a single table base |
| 68 | * address, which can be held in a register in the inner loops on many |
| 69 | * machines (more than can hold all eight addresses, anyway). |
| 70 | */ |
| 71 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 72 | #define R_Y_OFF 0 /* offset to R => Y section */ |
| 73 | #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ |
| 74 | #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ |
| 75 | #define R_CB_OFF (3*(MAXJSAMPLE+1)) |
| 76 | #define G_CB_OFF (4*(MAXJSAMPLE+1)) |
| 77 | #define B_CB_OFF (5*(MAXJSAMPLE+1)) |
| 78 | #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ |
| 79 | #define G_CR_OFF (6*(MAXJSAMPLE+1)) |
| 80 | #define B_CR_OFF (7*(MAXJSAMPLE+1)) |
| 81 | #define TABLE_SIZE (8*(MAXJSAMPLE+1)) |
| 82 | |
| 83 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 84 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 85 | * Initialize for RGB->YCC colorspace conversion. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 86 | */ |
| 87 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 88 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 89 | rgb_ycc_start (j_compress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 90 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 91 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| 92 | INT32 * rgb_ycc_tab; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 93 | INT32 i; |
| 94 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 95 | /* Allocate and fill in the conversion tables. */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 96 | cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) |
| 97 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 98 | (TABLE_SIZE * SIZEOF(INT32))); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 99 | |
| 100 | for (i = 0; i <= MAXJSAMPLE; i++) { |
| 101 | rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; |
| 102 | rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; |
| 103 | rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; |
| 104 | rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; |
| 105 | rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 106 | /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. |
| 107 | * This ensures that the maximum output will round to MAXJSAMPLE |
| 108 | * not MAXJSAMPLE+1, and thus that we don't have to range-limit. |
| 109 | */ |
| 110 | rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 111 | /* B=>Cb and R=>Cr tables are the same |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 112 | rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 113 | */ |
| 114 | rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; |
| 115 | rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; |
| 116 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 121 | * Convert some rows of samples to the JPEG colorspace. |
| 122 | * |
| 123 | * Note that we change from the application's interleaved-pixel format |
| 124 | * to our internal noninterleaved, one-plane-per-component format. |
| 125 | * The input buffer is therefore three times as wide as the output buffer. |
| 126 | * |
| 127 | * A starting row offset is provided only for the output buffer. The caller |
| 128 | * can easily adjust the passed input_buf value to accommodate any row |
| 129 | * offset required on that side. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 130 | */ |
| 131 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 132 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 133 | rgb_ycc_convert (j_compress_ptr cinfo, |
| 134 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 135 | JDIMENSION output_row, int num_rows) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 136 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 137 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 138 | register int r, g, b; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 139 | register INT32 * ctab = cconvert->rgb_ycc_tab; |
| 140 | register JSAMPROW inptr; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 141 | register JSAMPROW outptr0, outptr1, outptr2; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 142 | register JDIMENSION col; |
| 143 | JDIMENSION num_cols = cinfo->image_width; |
DRC | 5a9d6c3 | 2011-02-27 10:26:08 +0000 | [diff] [blame] | 144 | int rindex = rgb_red[cinfo->in_color_space]; |
| 145 | int gindex = rgb_green[cinfo->in_color_space]; |
| 146 | int bindex = rgb_blue[cinfo->in_color_space]; |
| 147 | int rgbstride = rgb_pixelsize[cinfo->in_color_space]; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 148 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 149 | while (--num_rows >= 0) { |
| 150 | inptr = *input_buf++; |
| 151 | outptr0 = output_buf[0][output_row]; |
| 152 | outptr1 = output_buf[1][output_row]; |
| 153 | outptr2 = output_buf[2][output_row]; |
| 154 | output_row++; |
| 155 | for (col = 0; col < num_cols; col++) { |
DRC | 5a9d6c3 | 2011-02-27 10:26:08 +0000 | [diff] [blame] | 156 | r = GETJSAMPLE(inptr[rindex]); |
| 157 | g = GETJSAMPLE(inptr[gindex]); |
| 158 | b = GETJSAMPLE(inptr[bindex]); |
| 159 | inptr += rgbstride; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 160 | /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 161 | * must be too; we do not need an explicit range-limiting operation. |
| 162 | * Hence the value being shifted is never negative, and we don't |
| 163 | * need the general RIGHT_SHIFT macro. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 164 | */ |
| 165 | /* Y */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 166 | outptr0[col] = (JSAMPLE) |
| 167 | ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| 168 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 169 | /* Cb */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 170 | outptr1[col] = (JSAMPLE) |
| 171 | ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) |
| 172 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 173 | /* Cr */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 174 | outptr2[col] = (JSAMPLE) |
| 175 | ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) |
| 176 | >> SCALEBITS); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /**************** Cases other than RGB -> YCbCr **************/ |
| 183 | |
| 184 | |
| 185 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 186 | * Convert some rows of samples to the JPEG colorspace. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 187 | * This version handles RGB->grayscale conversion, which is the same |
| 188 | * as the RGB->Y portion of RGB->YCbCr. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 189 | * We assume rgb_ycc_start has been called (we only use the Y tables). |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 190 | */ |
| 191 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 192 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 193 | rgb_gray_convert (j_compress_ptr cinfo, |
| 194 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 195 | JDIMENSION output_row, int num_rows) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 196 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 197 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
DRC | c866633 | 2011-02-18 11:23:45 +0000 | [diff] [blame] | 198 | register int r, g, b; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 199 | register INT32 * ctab = cconvert->rgb_ycc_tab; |
| 200 | register JSAMPROW inptr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 201 | register JSAMPROW outptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 202 | register JDIMENSION col; |
| 203 | JDIMENSION num_cols = cinfo->image_width; |
DRC | 5a9d6c3 | 2011-02-27 10:26:08 +0000 | [diff] [blame] | 204 | int rindex = rgb_red[cinfo->in_color_space]; |
| 205 | int gindex = rgb_green[cinfo->in_color_space]; |
| 206 | int bindex = rgb_blue[cinfo->in_color_space]; |
| 207 | int rgbstride = rgb_pixelsize[cinfo->in_color_space]; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 208 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 209 | while (--num_rows >= 0) { |
| 210 | inptr = *input_buf++; |
| 211 | outptr = output_buf[0][output_row]; |
| 212 | output_row++; |
DRC | c866633 | 2011-02-18 11:23:45 +0000 | [diff] [blame] | 213 | for (col = 0; col < num_cols; col++) { |
DRC | 5a9d6c3 | 2011-02-27 10:26:08 +0000 | [diff] [blame] | 214 | r = GETJSAMPLE(inptr[rindex]); |
| 215 | g = GETJSAMPLE(inptr[gindex]); |
| 216 | b = GETJSAMPLE(inptr[bindex]); |
| 217 | inptr += rgbstride; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 218 | /* Y */ |
DRC | c866633 | 2011-02-18 11:23:45 +0000 | [diff] [blame] | 219 | outptr[col] = (JSAMPLE) |
| 220 | ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| 221 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 228 | * Convert some rows of samples to the JPEG colorspace. |
| 229 | * This version handles Adobe-style CMYK->YCCK conversion, |
| 230 | * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same |
| 231 | * conversion as above, while passing K (black) unchanged. |
| 232 | * We assume rgb_ycc_start has been called. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 233 | */ |
| 234 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 235 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 236 | cmyk_ycck_convert (j_compress_ptr cinfo, |
| 237 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 238 | JDIMENSION output_row, int num_rows) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 239 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 240 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| 241 | register int r, g, b; |
| 242 | register INT32 * ctab = cconvert->rgb_ycc_tab; |
| 243 | register JSAMPROW inptr; |
| 244 | register JSAMPROW outptr0, outptr1, outptr2, outptr3; |
| 245 | register JDIMENSION col; |
| 246 | JDIMENSION num_cols = cinfo->image_width; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 247 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 248 | while (--num_rows >= 0) { |
| 249 | inptr = *input_buf++; |
| 250 | outptr0 = output_buf[0][output_row]; |
| 251 | outptr1 = output_buf[1][output_row]; |
| 252 | outptr2 = output_buf[2][output_row]; |
| 253 | outptr3 = output_buf[3][output_row]; |
| 254 | output_row++; |
| 255 | for (col = 0; col < num_cols; col++) { |
| 256 | r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); |
| 257 | g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); |
| 258 | b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); |
| 259 | /* K passes through as-is */ |
| 260 | outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ |
| 261 | inptr += 4; |
| 262 | /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations |
| 263 | * must be too; we do not need an explicit range-limiting operation. |
| 264 | * Hence the value being shifted is never negative, and we don't |
| 265 | * need the general RIGHT_SHIFT macro. |
| 266 | */ |
| 267 | /* Y */ |
| 268 | outptr0[col] = (JSAMPLE) |
| 269 | ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| 270 | >> SCALEBITS); |
| 271 | /* Cb */ |
| 272 | outptr1[col] = (JSAMPLE) |
| 273 | ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) |
| 274 | >> SCALEBITS); |
| 275 | /* Cr */ |
| 276 | outptr2[col] = (JSAMPLE) |
| 277 | ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) |
| 278 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 285 | * Convert some rows of samples to the JPEG colorspace. |
| 286 | * This version handles grayscale output with no conversion. |
| 287 | * The source can be either plain grayscale or YCbCr (since Y == gray). |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 288 | */ |
| 289 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 290 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 291 | grayscale_convert (j_compress_ptr cinfo, |
| 292 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 293 | JDIMENSION output_row, int num_rows) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 294 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 295 | register JSAMPROW inptr; |
| 296 | register JSAMPROW outptr; |
| 297 | register JDIMENSION col; |
| 298 | JDIMENSION num_cols = cinfo->image_width; |
| 299 | int instride = cinfo->input_components; |
| 300 | |
| 301 | while (--num_rows >= 0) { |
| 302 | inptr = *input_buf++; |
| 303 | outptr = output_buf[0][output_row]; |
| 304 | output_row++; |
| 305 | for (col = 0; col < num_cols; col++) { |
| 306 | outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ |
| 307 | inptr += instride; |
| 308 | } |
| 309 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 314 | * Convert some rows of samples to the JPEG colorspace. |
| 315 | * This version handles multi-component colorspaces without conversion. |
| 316 | * We assume input_components == num_components. |
| 317 | */ |
| 318 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 319 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 320 | null_convert (j_compress_ptr cinfo, |
| 321 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 322 | JDIMENSION output_row, int num_rows) |
| 323 | { |
| 324 | register JSAMPROW inptr; |
| 325 | register JSAMPROW outptr; |
| 326 | register JDIMENSION col; |
| 327 | register int ci; |
| 328 | int nc = cinfo->num_components; |
| 329 | JDIMENSION num_cols = cinfo->image_width; |
| 330 | |
| 331 | while (--num_rows >= 0) { |
| 332 | /* It seems fastest to make a separate pass for each component. */ |
| 333 | for (ci = 0; ci < nc; ci++) { |
| 334 | inptr = *input_buf; |
| 335 | outptr = output_buf[ci][output_row]; |
| 336 | for (col = 0; col < num_cols; col++) { |
| 337 | outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ |
| 338 | inptr += nc; |
| 339 | } |
| 340 | } |
| 341 | input_buf++; |
| 342 | output_row++; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /* |
| 348 | * Empty method for start_pass. |
| 349 | */ |
| 350 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 351 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 352 | null_method (j_compress_ptr cinfo) |
| 353 | { |
| 354 | /* no work needed */ |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /* |
| 359 | * Module initialization routine for input colorspace conversion. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 360 | */ |
| 361 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 362 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 363 | jinit_color_converter (j_compress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 364 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 365 | my_cconvert_ptr cconvert; |
| 366 | |
| 367 | cconvert = (my_cconvert_ptr) |
| 368 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 369 | SIZEOF(my_color_converter)); |
| 370 | cinfo->cconvert = (struct jpeg_color_converter *) cconvert; |
| 371 | /* set start_pass to null method until we find out differently */ |
| 372 | cconvert->pub.start_pass = null_method; |
| 373 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 374 | /* Make sure input_components agrees with in_color_space */ |
| 375 | switch (cinfo->in_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 376 | case JCS_GRAYSCALE: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 377 | if (cinfo->input_components != 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 378 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 379 | break; |
| 380 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 381 | case JCS_RGB: |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 382 | case JCS_EXT_RGB: |
| 383 | case JCS_EXT_RGBX: |
| 384 | case JCS_EXT_BGR: |
| 385 | case JCS_EXT_BGRX: |
| 386 | case JCS_EXT_XBGR: |
| 387 | case JCS_EXT_XRGB: |
| 388 | if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space]) |
| 389 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| 390 | break; |
| 391 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 392 | case JCS_YCbCr: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 393 | if (cinfo->input_components != 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 394 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 395 | break; |
| 396 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 397 | case JCS_CMYK: |
| 398 | case JCS_YCCK: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 399 | if (cinfo->input_components != 4) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 400 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 401 | break; |
| 402 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 403 | default: /* JCS_UNKNOWN can be anything */ |
| 404 | if (cinfo->input_components < 1) |
| 405 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | |
| 409 | /* Check num_components, set conversion method based on requested space */ |
| 410 | switch (cinfo->jpeg_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 411 | case JCS_GRAYSCALE: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 412 | if (cinfo->num_components != 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 413 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| 414 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
| 415 | cconvert->pub.color_convert = grayscale_convert; |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 416 | else if (cinfo->in_color_space == JCS_RGB || |
| 417 | cinfo->in_color_space == JCS_EXT_RGB || |
| 418 | cinfo->in_color_space == JCS_EXT_RGBX || |
| 419 | cinfo->in_color_space == JCS_EXT_BGR || |
| 420 | cinfo->in_color_space == JCS_EXT_BGRX || |
| 421 | cinfo->in_color_space == JCS_EXT_XBGR || |
| 422 | cinfo->in_color_space == JCS_EXT_XRGB) { |
DRC | c866633 | 2011-02-18 11:23:45 +0000 | [diff] [blame] | 423 | if (jsimd_can_rgb_gray()) |
| 424 | cconvert->pub.color_convert = jsimd_rgb_gray_convert; |
| 425 | else { |
| 426 | cconvert->pub.start_pass = rgb_ycc_start; |
| 427 | cconvert->pub.color_convert = rgb_gray_convert; |
| 428 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 429 | } else if (cinfo->in_color_space == JCS_YCbCr) |
| 430 | cconvert->pub.color_convert = grayscale_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 431 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 432 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 433 | break; |
| 434 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 435 | case JCS_RGB: |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 436 | case JCS_EXT_RGB: |
| 437 | case JCS_EXT_RGBX: |
| 438 | case JCS_EXT_BGR: |
| 439 | case JCS_EXT_BGRX: |
| 440 | case JCS_EXT_XBGR: |
| 441 | case JCS_EXT_XRGB: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 442 | if (cinfo->num_components != 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 443 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 444 | if (cinfo->in_color_space == cinfo->jpeg_color_space && |
| 445 | rgb_pixelsize[cinfo->in_color_space] == 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 446 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 447 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 448 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 449 | break; |
| 450 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 451 | case JCS_YCbCr: |
| 452 | if (cinfo->num_components != 3) |
| 453 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 454 | if (cinfo->in_color_space == JCS_RGB || |
| 455 | cinfo->in_color_space == JCS_EXT_RGB || |
| 456 | cinfo->in_color_space == JCS_EXT_RGBX || |
| 457 | cinfo->in_color_space == JCS_EXT_BGR || |
| 458 | cinfo->in_color_space == JCS_EXT_BGRX || |
| 459 | cinfo->in_color_space == JCS_EXT_XBGR || |
| 460 | cinfo->in_color_space == JCS_EXT_XRGB) { |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 461 | if (jsimd_can_rgb_ycc()) |
| 462 | cconvert->pub.color_convert = jsimd_rgb_ycc_convert; |
| 463 | else { |
| 464 | cconvert->pub.start_pass = rgb_ycc_start; |
| 465 | cconvert->pub.color_convert = rgb_ycc_convert; |
| 466 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 467 | } else if (cinfo->in_color_space == JCS_YCbCr) |
| 468 | cconvert->pub.color_convert = null_convert; |
| 469 | else |
| 470 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 471 | break; |
| 472 | |
| 473 | case JCS_CMYK: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 474 | if (cinfo->num_components != 4) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 475 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| 476 | if (cinfo->in_color_space == JCS_CMYK) |
| 477 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 478 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 479 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 480 | break; |
| 481 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 482 | case JCS_YCCK: |
| 483 | if (cinfo->num_components != 4) |
| 484 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| 485 | if (cinfo->in_color_space == JCS_CMYK) { |
| 486 | cconvert->pub.start_pass = rgb_ycc_start; |
| 487 | cconvert->pub.color_convert = cmyk_ycck_convert; |
| 488 | } else if (cinfo->in_color_space == JCS_YCCK) |
| 489 | cconvert->pub.color_convert = null_convert; |
| 490 | else |
| 491 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 492 | break; |
| 493 | |
| 494 | default: /* allow null conversion of JCS_UNKNOWN */ |
| 495 | if (cinfo->jpeg_color_space != cinfo->in_color_space || |
| 496 | cinfo->num_components != cinfo->input_components) |
| 497 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 498 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 499 | break; |
| 500 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 501 | } |