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