vrshoot

annotate libs/libjpeg/jdcolor.c @ 2:334d17aed7de

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