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 | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 6 | * Copyright 2009 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; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 144 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 145 | while (--num_rows >= 0) { |
| 146 | inptr = *input_buf++; |
| 147 | outptr0 = output_buf[0][output_row]; |
| 148 | outptr1 = output_buf[1][output_row]; |
| 149 | outptr2 = output_buf[2][output_row]; |
| 150 | output_row++; |
| 151 | for (col = 0; col < num_cols; col++) { |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 152 | r = GETJSAMPLE(inptr[rgb_red[cinfo->in_color_space]]); |
| 153 | g = GETJSAMPLE(inptr[rgb_green[cinfo->in_color_space]]); |
| 154 | b = GETJSAMPLE(inptr[rgb_blue[cinfo->in_color_space]]); |
| 155 | inptr += rgb_pixelsize[cinfo->in_color_space]; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 156 | /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 157 | * must be too; we do not need an explicit range-limiting operation. |
| 158 | * Hence the value being shifted is never negative, and we don't |
| 159 | * need the general RIGHT_SHIFT macro. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 160 | */ |
| 161 | /* Y */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 162 | outptr0[col] = (JSAMPLE) |
| 163 | ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| 164 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 165 | /* Cb */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 166 | outptr1[col] = (JSAMPLE) |
| 167 | ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) |
| 168 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 169 | /* Cr */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 170 | outptr2[col] = (JSAMPLE) |
| 171 | ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) |
| 172 | >> SCALEBITS); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /**************** Cases other than RGB -> YCbCr **************/ |
| 179 | |
| 180 | |
| 181 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 182 | * Convert some rows of samples to the JPEG colorspace. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 183 | * This version handles RGB->grayscale conversion, which is the same |
| 184 | * as the RGB->Y portion of RGB->YCbCr. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 185 | * 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] | 186 | */ |
| 187 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 188 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 189 | rgb_gray_convert (j_compress_ptr cinfo, |
| 190 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 191 | JDIMENSION output_row, int num_rows) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 192 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 193 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 194 | register int r, g, b; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 195 | register INT32 * ctab = cconvert->rgb_ycc_tab; |
| 196 | register JSAMPROW inptr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 197 | register JSAMPROW outptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 198 | register JDIMENSION col; |
| 199 | JDIMENSION num_cols = cinfo->image_width; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 200 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 201 | while (--num_rows >= 0) { |
| 202 | inptr = *input_buf++; |
| 203 | outptr = output_buf[0][output_row]; |
| 204 | output_row++; |
| 205 | for (col = 0; col < num_cols; col++) { |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 206 | r = GETJSAMPLE(inptr[rgb_red[cinfo->in_color_space]]); |
| 207 | g = GETJSAMPLE(inptr[rgb_green[cinfo->in_color_space]]); |
| 208 | b = GETJSAMPLE(inptr[rgb_blue[cinfo->in_color_space]]); |
| 209 | inptr += rgb_pixelsize[cinfo->in_color_space]; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 210 | /* Y */ |
| 211 | outptr[col] = (JSAMPLE) |
| 212 | ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| 213 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 220 | * Convert some rows of samples to the JPEG colorspace. |
| 221 | * This version handles Adobe-style CMYK->YCCK conversion, |
| 222 | * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same |
| 223 | * conversion as above, while passing K (black) unchanged. |
| 224 | * We assume rgb_ycc_start has been called. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 225 | */ |
| 226 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 227 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 228 | cmyk_ycck_convert (j_compress_ptr cinfo, |
| 229 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 230 | JDIMENSION output_row, int num_rows) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 231 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 232 | my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| 233 | register int r, g, b; |
| 234 | register INT32 * ctab = cconvert->rgb_ycc_tab; |
| 235 | register JSAMPROW inptr; |
| 236 | register JSAMPROW outptr0, outptr1, outptr2, outptr3; |
| 237 | register JDIMENSION col; |
| 238 | JDIMENSION num_cols = cinfo->image_width; |
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 | while (--num_rows >= 0) { |
| 241 | inptr = *input_buf++; |
| 242 | outptr0 = output_buf[0][output_row]; |
| 243 | outptr1 = output_buf[1][output_row]; |
| 244 | outptr2 = output_buf[2][output_row]; |
| 245 | outptr3 = output_buf[3][output_row]; |
| 246 | output_row++; |
| 247 | for (col = 0; col < num_cols; col++) { |
| 248 | r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); |
| 249 | g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); |
| 250 | b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); |
| 251 | /* K passes through as-is */ |
| 252 | outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ |
| 253 | inptr += 4; |
| 254 | /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations |
| 255 | * must be too; we do not need an explicit range-limiting operation. |
| 256 | * Hence the value being shifted is never negative, and we don't |
| 257 | * need the general RIGHT_SHIFT macro. |
| 258 | */ |
| 259 | /* Y */ |
| 260 | outptr0[col] = (JSAMPLE) |
| 261 | ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| 262 | >> SCALEBITS); |
| 263 | /* Cb */ |
| 264 | outptr1[col] = (JSAMPLE) |
| 265 | ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) |
| 266 | >> SCALEBITS); |
| 267 | /* Cr */ |
| 268 | outptr2[col] = (JSAMPLE) |
| 269 | ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) |
| 270 | >> SCALEBITS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 277 | * Convert some rows of samples to the JPEG colorspace. |
| 278 | * This version handles grayscale output with no conversion. |
| 279 | * 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] | 280 | */ |
| 281 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 282 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 283 | grayscale_convert (j_compress_ptr cinfo, |
| 284 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 285 | JDIMENSION output_row, int num_rows) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 286 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 287 | register JSAMPROW inptr; |
| 288 | register JSAMPROW outptr; |
| 289 | register JDIMENSION col; |
| 290 | JDIMENSION num_cols = cinfo->image_width; |
| 291 | int instride = cinfo->input_components; |
| 292 | |
| 293 | while (--num_rows >= 0) { |
| 294 | inptr = *input_buf++; |
| 295 | outptr = output_buf[0][output_row]; |
| 296 | output_row++; |
| 297 | for (col = 0; col < num_cols; col++) { |
| 298 | outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ |
| 299 | inptr += instride; |
| 300 | } |
| 301 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 306 | * Convert some rows of samples to the JPEG colorspace. |
| 307 | * This version handles multi-component colorspaces without conversion. |
| 308 | * We assume input_components == num_components. |
| 309 | */ |
| 310 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 311 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 312 | null_convert (j_compress_ptr cinfo, |
| 313 | JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| 314 | JDIMENSION output_row, int num_rows) |
| 315 | { |
| 316 | register JSAMPROW inptr; |
| 317 | register JSAMPROW outptr; |
| 318 | register JDIMENSION col; |
| 319 | register int ci; |
| 320 | int nc = cinfo->num_components; |
| 321 | JDIMENSION num_cols = cinfo->image_width; |
| 322 | |
| 323 | while (--num_rows >= 0) { |
| 324 | /* It seems fastest to make a separate pass for each component. */ |
| 325 | for (ci = 0; ci < nc; ci++) { |
| 326 | inptr = *input_buf; |
| 327 | outptr = output_buf[ci][output_row]; |
| 328 | for (col = 0; col < num_cols; col++) { |
| 329 | outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ |
| 330 | inptr += nc; |
| 331 | } |
| 332 | } |
| 333 | input_buf++; |
| 334 | output_row++; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* |
| 340 | * Empty method for start_pass. |
| 341 | */ |
| 342 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 343 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 344 | null_method (j_compress_ptr cinfo) |
| 345 | { |
| 346 | /* no work needed */ |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /* |
| 351 | * Module initialization routine for input colorspace conversion. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 352 | */ |
| 353 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 354 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 355 | jinit_color_converter (j_compress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 356 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 357 | my_cconvert_ptr cconvert; |
| 358 | |
| 359 | cconvert = (my_cconvert_ptr) |
| 360 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 361 | SIZEOF(my_color_converter)); |
| 362 | cinfo->cconvert = (struct jpeg_color_converter *) cconvert; |
| 363 | /* set start_pass to null method until we find out differently */ |
| 364 | cconvert->pub.start_pass = null_method; |
| 365 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 366 | /* Make sure input_components agrees with in_color_space */ |
| 367 | switch (cinfo->in_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 368 | case JCS_GRAYSCALE: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 369 | if (cinfo->input_components != 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 370 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 371 | break; |
| 372 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 373 | case JCS_RGB: |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 374 | case JCS_EXT_RGB: |
| 375 | case JCS_EXT_RGBX: |
| 376 | case JCS_EXT_BGR: |
| 377 | case JCS_EXT_BGRX: |
| 378 | case JCS_EXT_XBGR: |
| 379 | case JCS_EXT_XRGB: |
| 380 | if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space]) |
| 381 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| 382 | break; |
| 383 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 384 | case JCS_YCbCr: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 385 | if (cinfo->input_components != 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 386 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 387 | break; |
| 388 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 389 | case JCS_CMYK: |
| 390 | case JCS_YCCK: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 391 | if (cinfo->input_components != 4) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 392 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 393 | break; |
| 394 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 395 | default: /* JCS_UNKNOWN can be anything */ |
| 396 | if (cinfo->input_components < 1) |
| 397 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | |
| 401 | /* Check num_components, set conversion method based on requested space */ |
| 402 | switch (cinfo->jpeg_color_space) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 403 | case JCS_GRAYSCALE: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 404 | if (cinfo->num_components != 1) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 405 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| 406 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
| 407 | cconvert->pub.color_convert = grayscale_convert; |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 408 | else if (cinfo->in_color_space == JCS_RGB || |
| 409 | cinfo->in_color_space == JCS_EXT_RGB || |
| 410 | cinfo->in_color_space == JCS_EXT_RGBX || |
| 411 | cinfo->in_color_space == JCS_EXT_BGR || |
| 412 | cinfo->in_color_space == JCS_EXT_BGRX || |
| 413 | cinfo->in_color_space == JCS_EXT_XBGR || |
| 414 | cinfo->in_color_space == JCS_EXT_XRGB) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 415 | cconvert->pub.start_pass = rgb_ycc_start; |
| 416 | cconvert->pub.color_convert = rgb_gray_convert; |
| 417 | } else if (cinfo->in_color_space == JCS_YCbCr) |
| 418 | cconvert->pub.color_convert = grayscale_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 419 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 420 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 421 | break; |
| 422 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 423 | case JCS_RGB: |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 424 | case JCS_EXT_RGB: |
| 425 | case JCS_EXT_RGBX: |
| 426 | case JCS_EXT_BGR: |
| 427 | case JCS_EXT_BGRX: |
| 428 | case JCS_EXT_XBGR: |
| 429 | case JCS_EXT_XRGB: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 430 | if (cinfo->num_components != 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 431 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 432 | if (cinfo->in_color_space == cinfo->jpeg_color_space && |
| 433 | rgb_pixelsize[cinfo->in_color_space] == 3) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 434 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 435 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 436 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 437 | break; |
| 438 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 439 | case JCS_YCbCr: |
| 440 | if (cinfo->num_components != 3) |
| 441 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
DRC | f25c071 | 2009-04-03 12:00:51 +0000 | [diff] [blame] | 442 | if (cinfo->in_color_space == JCS_RGB || |
| 443 | cinfo->in_color_space == JCS_EXT_RGB || |
| 444 | cinfo->in_color_space == JCS_EXT_RGBX || |
| 445 | cinfo->in_color_space == JCS_EXT_BGR || |
| 446 | cinfo->in_color_space == JCS_EXT_BGRX || |
| 447 | cinfo->in_color_space == JCS_EXT_XBGR || |
| 448 | cinfo->in_color_space == JCS_EXT_XRGB) { |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 449 | if (jsimd_can_rgb_ycc()) |
| 450 | cconvert->pub.color_convert = jsimd_rgb_ycc_convert; |
| 451 | else { |
| 452 | cconvert->pub.start_pass = rgb_ycc_start; |
| 453 | cconvert->pub.color_convert = rgb_ycc_convert; |
| 454 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 455 | } else if (cinfo->in_color_space == JCS_YCbCr) |
| 456 | cconvert->pub.color_convert = null_convert; |
| 457 | else |
| 458 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 459 | break; |
| 460 | |
| 461 | case JCS_CMYK: |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 462 | if (cinfo->num_components != 4) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 463 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| 464 | if (cinfo->in_color_space == JCS_CMYK) |
| 465 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 466 | else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 467 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 468 | break; |
| 469 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 470 | case JCS_YCCK: |
| 471 | if (cinfo->num_components != 4) |
| 472 | ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| 473 | if (cinfo->in_color_space == JCS_CMYK) { |
| 474 | cconvert->pub.start_pass = rgb_ycc_start; |
| 475 | cconvert->pub.color_convert = cmyk_ycck_convert; |
| 476 | } else if (cinfo->in_color_space == JCS_YCCK) |
| 477 | cconvert->pub.color_convert = null_convert; |
| 478 | else |
| 479 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 480 | break; |
| 481 | |
| 482 | default: /* allow null conversion of JCS_UNKNOWN */ |
| 483 | if (cinfo->jpeg_color_space != cinfo->in_color_space || |
| 484 | cinfo->num_components != cinfo->input_components) |
| 485 | ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| 486 | cconvert->pub.color_convert = null_convert; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 487 | break; |
| 488 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 489 | } |