istereo2

annotate libs/libjpeg/jdcolor.c @ 2:81d35769f546

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