istereo

annotate libs/libjpeg/jdcolor.c @ 39:ff055bff6a15

copyright statements and stuff
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 11 Sep 2011 09:03:18 +0300
parents
children
rev   line source
nuclear@26 1 /*
nuclear@26 2 * jdcolor.c
nuclear@26 3 *
nuclear@26 4 * Copyright (C) 1991-1997, Thomas G. Lane.
nuclear@26 5 * This file is part of the Independent JPEG Group's software.
nuclear@26 6 * For conditions of distribution and use, see the accompanying README file.
nuclear@26 7 *
nuclear@26 8 * This file contains output colorspace conversion routines.
nuclear@26 9 */
nuclear@26 10
nuclear@26 11 #define JPEG_INTERNALS
nuclear@26 12 #include "jinclude.h"
nuclear@26 13 #include "jpeglib.h"
nuclear@26 14
nuclear@26 15
nuclear@26 16 /* Private subobject */
nuclear@26 17
nuclear@26 18 typedef struct {
nuclear@26 19 struct jpeg_color_deconverter pub; /* public fields */
nuclear@26 20
nuclear@26 21 /* Private state for YCC->RGB conversion */
nuclear@26 22 int * Cr_r_tab; /* => table for Cr to R conversion */
nuclear@26 23 int * Cb_b_tab; /* => table for Cb to B conversion */
nuclear@26 24 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
nuclear@26 25 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
nuclear@26 26 } my_color_deconverter;
nuclear@26 27
nuclear@26 28 typedef my_color_deconverter * my_cconvert_ptr;
nuclear@26 29
nuclear@26 30
nuclear@26 31 /**************** YCbCr -> RGB conversion: most common case **************/
nuclear@26 32
nuclear@26 33 /*
nuclear@26 34 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
nuclear@26 35 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
nuclear@26 36 * The conversion equations to be implemented are therefore
nuclear@26 37 * R = Y + 1.40200 * Cr
nuclear@26 38 * G = Y - 0.34414 * Cb - 0.71414 * Cr
nuclear@26 39 * B = Y + 1.77200 * Cb
nuclear@26 40 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
nuclear@26 41 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
nuclear@26 42 *
nuclear@26 43 * To avoid floating-point arithmetic, we represent the fractional constants
nuclear@26 44 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
nuclear@26 45 * the products by 2^16, with appropriate rounding, to get the correct answer.
nuclear@26 46 * Notice that Y, being an integral input, does not contribute any fraction
nuclear@26 47 * so it need not participate in the rounding.
nuclear@26 48 *
nuclear@26 49 * For even more speed, we avoid doing any multiplications in the inner loop
nuclear@26 50 * by precalculating the constants times Cb and Cr for all possible values.
nuclear@26 51 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
nuclear@26 52 * for 12-bit samples it is still acceptable. It's not very reasonable for
nuclear@26 53 * 16-bit samples, but if you want lossless storage you shouldn't be changing
nuclear@26 54 * colorspace anyway.
nuclear@26 55 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
nuclear@26 56 * values for the G calculation are left scaled up, since we must add them
nuclear@26 57 * together before rounding.
nuclear@26 58 */
nuclear@26 59
nuclear@26 60 #define SCALEBITS 16 /* speediest right-shift on some machines */
nuclear@26 61 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
nuclear@26 62 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
nuclear@26 63
nuclear@26 64
nuclear@26 65 /*
nuclear@26 66 * Initialize tables for YCC->RGB colorspace conversion.
nuclear@26 67 */
nuclear@26 68
nuclear@26 69 LOCAL(void)
nuclear@26 70 build_ycc_rgb_table (j_decompress_ptr cinfo)
nuclear@26 71 {
nuclear@26 72 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
nuclear@26 73 int i;
nuclear@26 74 INT32 x;
nuclear@26 75 SHIFT_TEMPS
nuclear@26 76
nuclear@26 77 cconvert->Cr_r_tab = (int *)
nuclear@26 78 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 79 (MAXJSAMPLE+1) * SIZEOF(int));
nuclear@26 80 cconvert->Cb_b_tab = (int *)
nuclear@26 81 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 82 (MAXJSAMPLE+1) * SIZEOF(int));
nuclear@26 83 cconvert->Cr_g_tab = (INT32 *)
nuclear@26 84 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 85 (MAXJSAMPLE+1) * SIZEOF(INT32));
nuclear@26 86 cconvert->Cb_g_tab = (INT32 *)
nuclear@26 87 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 88 (MAXJSAMPLE+1) * SIZEOF(INT32));
nuclear@26 89
nuclear@26 90 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
nuclear@26 91 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
nuclear@26 92 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
nuclear@26 93 /* Cr=>R value is nearest int to 1.40200 * x */
nuclear@26 94 cconvert->Cr_r_tab[i] = (int)
nuclear@26 95 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
nuclear@26 96 /* Cb=>B value is nearest int to 1.77200 * x */
nuclear@26 97 cconvert->Cb_b_tab[i] = (int)
nuclear@26 98 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
nuclear@26 99 /* Cr=>G value is scaled-up -0.71414 * x */
nuclear@26 100 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
nuclear@26 101 /* Cb=>G value is scaled-up -0.34414 * x */
nuclear@26 102 /* We also add in ONE_HALF so that need not do it in inner loop */
nuclear@26 103 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
nuclear@26 104 }
nuclear@26 105 }
nuclear@26 106
nuclear@26 107
nuclear@26 108 /*
nuclear@26 109 * Convert some rows of samples to the output colorspace.
nuclear@26 110 *
nuclear@26 111 * Note that we change from noninterleaved, one-plane-per-component format
nuclear@26 112 * to interleaved-pixel format. The output buffer is therefore three times
nuclear@26 113 * as wide as the input buffer.
nuclear@26 114 * A starting row offset is provided only for the input buffer. The caller
nuclear@26 115 * can easily adjust the passed output_buf value to accommodate any row
nuclear@26 116 * offset required on that side.
nuclear@26 117 */
nuclear@26 118
nuclear@26 119 METHODDEF(void)
nuclear@26 120 ycc_rgb_convert (j_decompress_ptr cinfo,
nuclear@26 121 JSAMPIMAGE input_buf, JDIMENSION input_row,
nuclear@26 122 JSAMPARRAY output_buf, int num_rows)
nuclear@26 123 {
nuclear@26 124 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
nuclear@26 125 register int y, cb, cr;
nuclear@26 126 register JSAMPROW outptr;
nuclear@26 127 register JSAMPROW inptr0, inptr1, inptr2;
nuclear@26 128 register JDIMENSION col;
nuclear@26 129 JDIMENSION num_cols = cinfo->output_width;
nuclear@26 130 /* copy these pointers into registers if possible */
nuclear@26 131 register JSAMPLE * range_limit = cinfo->sample_range_limit;
nuclear@26 132 register int * Crrtab = cconvert->Cr_r_tab;
nuclear@26 133 register int * Cbbtab = cconvert->Cb_b_tab;
nuclear@26 134 register INT32 * Crgtab = cconvert->Cr_g_tab;
nuclear@26 135 register INT32 * Cbgtab = cconvert->Cb_g_tab;
nuclear@26 136 SHIFT_TEMPS
nuclear@26 137
nuclear@26 138 while (--num_rows >= 0) {
nuclear@26 139 inptr0 = input_buf[0][input_row];
nuclear@26 140 inptr1 = input_buf[1][input_row];
nuclear@26 141 inptr2 = input_buf[2][input_row];
nuclear@26 142 input_row++;
nuclear@26 143 outptr = *output_buf++;
nuclear@26 144 for (col = 0; col < num_cols; col++) {
nuclear@26 145 y = GETJSAMPLE(inptr0[col]);
nuclear@26 146 cb = GETJSAMPLE(inptr1[col]);
nuclear@26 147 cr = GETJSAMPLE(inptr2[col]);
nuclear@26 148 /* Range-limiting is essential due to noise introduced by DCT losses. */
nuclear@26 149 outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
nuclear@26 150 outptr[RGB_GREEN] = range_limit[y +
nuclear@26 151 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
nuclear@26 152 SCALEBITS))];
nuclear@26 153 outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
nuclear@26 154 outptr += RGB_PIXELSIZE;
nuclear@26 155 }
nuclear@26 156 }
nuclear@26 157 }
nuclear@26 158
nuclear@26 159
nuclear@26 160 /**************** Cases other than YCbCr -> RGB **************/
nuclear@26 161
nuclear@26 162
nuclear@26 163 /*
nuclear@26 164 * Color conversion for no colorspace change: just copy the data,
nuclear@26 165 * converting from separate-planes to interleaved representation.
nuclear@26 166 */
nuclear@26 167
nuclear@26 168 METHODDEF(void)
nuclear@26 169 null_convert (j_decompress_ptr cinfo,
nuclear@26 170 JSAMPIMAGE input_buf, JDIMENSION input_row,
nuclear@26 171 JSAMPARRAY output_buf, int num_rows)
nuclear@26 172 {
nuclear@26 173 register JSAMPROW inptr, outptr;
nuclear@26 174 register JDIMENSION count;
nuclear@26 175 register int num_components = cinfo->num_components;
nuclear@26 176 JDIMENSION num_cols = cinfo->output_width;
nuclear@26 177 int ci;
nuclear@26 178
nuclear@26 179 while (--num_rows >= 0) {
nuclear@26 180 for (ci = 0; ci < num_components; ci++) {
nuclear@26 181 inptr = input_buf[ci][input_row];
nuclear@26 182 outptr = output_buf[0] + ci;
nuclear@26 183 for (count = num_cols; count > 0; count--) {
nuclear@26 184 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
nuclear@26 185 outptr += num_components;
nuclear@26 186 }
nuclear@26 187 }
nuclear@26 188 input_row++;
nuclear@26 189 output_buf++;
nuclear@26 190 }
nuclear@26 191 }
nuclear@26 192
nuclear@26 193
nuclear@26 194 /*
nuclear@26 195 * Color conversion for grayscale: just copy the data.
nuclear@26 196 * This also works for YCbCr -> grayscale conversion, in which
nuclear@26 197 * we just copy the Y (luminance) component and ignore chrominance.
nuclear@26 198 */
nuclear@26 199
nuclear@26 200 METHODDEF(void)
nuclear@26 201 grayscale_convert (j_decompress_ptr cinfo,
nuclear@26 202 JSAMPIMAGE input_buf, JDIMENSION input_row,
nuclear@26 203 JSAMPARRAY output_buf, int num_rows)
nuclear@26 204 {
nuclear@26 205 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
nuclear@26 206 num_rows, cinfo->output_width);
nuclear@26 207 }
nuclear@26 208
nuclear@26 209
nuclear@26 210 /*
nuclear@26 211 * Convert grayscale to RGB: just duplicate the graylevel three times.
nuclear@26 212 * This is provided to support applications that don't want to cope
nuclear@26 213 * with grayscale as a separate case.
nuclear@26 214 */
nuclear@26 215
nuclear@26 216 METHODDEF(void)
nuclear@26 217 gray_rgb_convert (j_decompress_ptr cinfo,
nuclear@26 218 JSAMPIMAGE input_buf, JDIMENSION input_row,
nuclear@26 219 JSAMPARRAY output_buf, int num_rows)
nuclear@26 220 {
nuclear@26 221 register JSAMPROW inptr, outptr;
nuclear@26 222 register JDIMENSION col;
nuclear@26 223 JDIMENSION num_cols = cinfo->output_width;
nuclear@26 224
nuclear@26 225 while (--num_rows >= 0) {
nuclear@26 226 inptr = input_buf[0][input_row++];
nuclear@26 227 outptr = *output_buf++;
nuclear@26 228 for (col = 0; col < num_cols; col++) {
nuclear@26 229 /* We can dispense with GETJSAMPLE() here */
nuclear@26 230 outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
nuclear@26 231 outptr += RGB_PIXELSIZE;
nuclear@26 232 }
nuclear@26 233 }
nuclear@26 234 }
nuclear@26 235
nuclear@26 236
nuclear@26 237 /*
nuclear@26 238 * Adobe-style YCCK->CMYK conversion.
nuclear@26 239 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
nuclear@26 240 * conversion as above, while passing K (black) unchanged.
nuclear@26 241 * We assume build_ycc_rgb_table has been called.
nuclear@26 242 */
nuclear@26 243
nuclear@26 244 METHODDEF(void)
nuclear@26 245 ycck_cmyk_convert (j_decompress_ptr cinfo,
nuclear@26 246 JSAMPIMAGE input_buf, JDIMENSION input_row,
nuclear@26 247 JSAMPARRAY output_buf, int num_rows)
nuclear@26 248 {
nuclear@26 249 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
nuclear@26 250 register int y, cb, cr;
nuclear@26 251 register JSAMPROW outptr;
nuclear@26 252 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
nuclear@26 253 register JDIMENSION col;
nuclear@26 254 JDIMENSION num_cols = cinfo->output_width;
nuclear@26 255 /* copy these pointers into registers if possible */
nuclear@26 256 register JSAMPLE * range_limit = cinfo->sample_range_limit;
nuclear@26 257 register int * Crrtab = cconvert->Cr_r_tab;
nuclear@26 258 register int * Cbbtab = cconvert->Cb_b_tab;
nuclear@26 259 register INT32 * Crgtab = cconvert->Cr_g_tab;
nuclear@26 260 register INT32 * Cbgtab = cconvert->Cb_g_tab;
nuclear@26 261 SHIFT_TEMPS
nuclear@26 262
nuclear@26 263 while (--num_rows >= 0) {
nuclear@26 264 inptr0 = input_buf[0][input_row];
nuclear@26 265 inptr1 = input_buf[1][input_row];
nuclear@26 266 inptr2 = input_buf[2][input_row];
nuclear@26 267 inptr3 = input_buf[3][input_row];
nuclear@26 268 input_row++;
nuclear@26 269 outptr = *output_buf++;
nuclear@26 270 for (col = 0; col < num_cols; col++) {
nuclear@26 271 y = GETJSAMPLE(inptr0[col]);
nuclear@26 272 cb = GETJSAMPLE(inptr1[col]);
nuclear@26 273 cr = GETJSAMPLE(inptr2[col]);
nuclear@26 274 /* Range-limiting is essential due to noise introduced by DCT losses. */
nuclear@26 275 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
nuclear@26 276 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
nuclear@26 277 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
nuclear@26 278 SCALEBITS)))];
nuclear@26 279 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
nuclear@26 280 /* K passes through unchanged */
nuclear@26 281 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
nuclear@26 282 outptr += 4;
nuclear@26 283 }
nuclear@26 284 }
nuclear@26 285 }
nuclear@26 286
nuclear@26 287
nuclear@26 288 /*
nuclear@26 289 * Empty method for start_pass.
nuclear@26 290 */
nuclear@26 291
nuclear@26 292 METHODDEF(void)
nuclear@26 293 start_pass_dcolor (j_decompress_ptr cinfo)
nuclear@26 294 {
nuclear@26 295 /* no work needed */
nuclear@26 296 }
nuclear@26 297
nuclear@26 298
nuclear@26 299 /*
nuclear@26 300 * Module initialization routine for output colorspace conversion.
nuclear@26 301 */
nuclear@26 302
nuclear@26 303 GLOBAL(void)
nuclear@26 304 jinit_color_deconverter (j_decompress_ptr cinfo)
nuclear@26 305 {
nuclear@26 306 my_cconvert_ptr cconvert;
nuclear@26 307 int ci;
nuclear@26 308
nuclear@26 309 cconvert = (my_cconvert_ptr)
nuclear@26 310 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@26 311 SIZEOF(my_color_deconverter));
nuclear@26 312 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
nuclear@26 313 cconvert->pub.start_pass = start_pass_dcolor;
nuclear@26 314
nuclear@26 315 /* Make sure num_components agrees with jpeg_color_space */
nuclear@26 316 switch (cinfo->jpeg_color_space) {
nuclear@26 317 case JCS_GRAYSCALE:
nuclear@26 318 if (cinfo->num_components != 1)
nuclear@26 319 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
nuclear@26 320 break;
nuclear@26 321
nuclear@26 322 case JCS_RGB:
nuclear@26 323 case JCS_YCbCr:
nuclear@26 324 if (cinfo->num_components != 3)
nuclear@26 325 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
nuclear@26 326 break;
nuclear@26 327
nuclear@26 328 case JCS_CMYK:
nuclear@26 329 case JCS_YCCK:
nuclear@26 330 if (cinfo->num_components != 4)
nuclear@26 331 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
nuclear@26 332 break;
nuclear@26 333
nuclear@26 334 default: /* JCS_UNKNOWN can be anything */
nuclear@26 335 if (cinfo->num_components < 1)
nuclear@26 336 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
nuclear@26 337 break;
nuclear@26 338 }
nuclear@26 339
nuclear@26 340 /* Set out_color_components and conversion method based on requested space.
nuclear@26 341 * Also clear the component_needed flags for any unused components,
nuclear@26 342 * so that earlier pipeline stages can avoid useless computation.
nuclear@26 343 */
nuclear@26 344
nuclear@26 345 switch (cinfo->out_color_space) {
nuclear@26 346 case JCS_GRAYSCALE:
nuclear@26 347 cinfo->out_color_components = 1;
nuclear@26 348 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
nuclear@26 349 cinfo->jpeg_color_space == JCS_YCbCr) {
nuclear@26 350 cconvert->pub.color_convert = grayscale_convert;
nuclear@26 351 /* For color->grayscale conversion, only the Y (0) component is needed */
nuclear@26 352 for (ci = 1; ci < cinfo->num_components; ci++)
nuclear@26 353 cinfo->comp_info[ci].component_needed = FALSE;
nuclear@26 354 } else
nuclear@26 355 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
nuclear@26 356 break;
nuclear@26 357
nuclear@26 358 case JCS_RGB:
nuclear@26 359 cinfo->out_color_components = RGB_PIXELSIZE;
nuclear@26 360 if (cinfo->jpeg_color_space == JCS_YCbCr) {
nuclear@26 361 cconvert->pub.color_convert = ycc_rgb_convert;
nuclear@26 362 build_ycc_rgb_table(cinfo);
nuclear@26 363 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
nuclear@26 364 cconvert->pub.color_convert = gray_rgb_convert;
nuclear@26 365 } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
nuclear@26 366 cconvert->pub.color_convert = null_convert;
nuclear@26 367 } else
nuclear@26 368 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
nuclear@26 369 break;
nuclear@26 370
nuclear@26 371 case JCS_CMYK:
nuclear@26 372 cinfo->out_color_components = 4;
nuclear@26 373 if (cinfo->jpeg_color_space == JCS_YCCK) {
nuclear@26 374 cconvert->pub.color_convert = ycck_cmyk_convert;
nuclear@26 375 build_ycc_rgb_table(cinfo);
nuclear@26 376 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
nuclear@26 377 cconvert->pub.color_convert = null_convert;
nuclear@26 378 } else
nuclear@26 379 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
nuclear@26 380 break;
nuclear@26 381
nuclear@26 382 default:
nuclear@26 383 /* Permit null conversion to same output space */
nuclear@26 384 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
nuclear@26 385 cinfo->out_color_components = cinfo->num_components;
nuclear@26 386 cconvert->pub.color_convert = null_convert;
nuclear@26 387 } else /* unsupported non-null conversion */
nuclear@26 388 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
nuclear@26 389 break;
nuclear@26 390 }
nuclear@26 391
nuclear@26 392 if (cinfo->quantize_colors)
nuclear@26 393 cinfo->output_components = 1; /* single colormapped output component */
nuclear@26 394 else
nuclear@26 395 cinfo->output_components = cinfo->out_color_components;
nuclear@26 396 }