dbf-halloween2015

annotate libs/libjpeg/jdcolor.c @ 3:c37fe5d8a4ed

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