3dphotoshoot

annotate libs/libjpeg/jdcolor.c @ 14:06dc8b9b4f89

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