media/libjpeg/jdcolor.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * jdcolor.c
michael@0 3 *
michael@0 4 * This file was part of the Independent JPEG Group's software:
michael@0 5 * Copyright (C) 1991-1997, Thomas G. Lane.
michael@0 6 * Modified 2011 by Guido Vollbeding.
michael@0 7 * libjpeg-turbo Modifications:
michael@0 8 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
michael@0 9 * Copyright (C) 2009, 2011-2012, D. R. Commander.
michael@0 10 * For conditions of distribution and use, see the accompanying README file.
michael@0 11 *
michael@0 12 * This file contains output colorspace conversion routines.
michael@0 13 */
michael@0 14
michael@0 15 #define JPEG_INTERNALS
michael@0 16 #include "jinclude.h"
michael@0 17 #include "jpeglib.h"
michael@0 18 #include "jsimd.h"
michael@0 19 #include "config.h"
michael@0 20
michael@0 21
michael@0 22 /* Private subobject */
michael@0 23
michael@0 24 typedef struct {
michael@0 25 struct jpeg_color_deconverter pub; /* public fields */
michael@0 26
michael@0 27 /* Private state for YCC->RGB conversion */
michael@0 28 int * Cr_r_tab; /* => table for Cr to R conversion */
michael@0 29 int * Cb_b_tab; /* => table for Cb to B conversion */
michael@0 30 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
michael@0 31 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
michael@0 32
michael@0 33 /* Private state for RGB->Y conversion */
michael@0 34 INT32 * rgb_y_tab; /* => table for RGB to Y conversion */
michael@0 35 } my_color_deconverter;
michael@0 36
michael@0 37 typedef my_color_deconverter * my_cconvert_ptr;
michael@0 38
michael@0 39
michael@0 40 /**************** YCbCr -> RGB conversion: most common case **************/
michael@0 41 /**************** RGB -> Y conversion: less common case **************/
michael@0 42
michael@0 43 /*
michael@0 44 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
michael@0 45 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
michael@0 46 * The conversion equations to be implemented are therefore
michael@0 47 *
michael@0 48 * R = Y + 1.40200 * Cr
michael@0 49 * G = Y - 0.34414 * Cb - 0.71414 * Cr
michael@0 50 * B = Y + 1.77200 * Cb
michael@0 51 *
michael@0 52 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
michael@0 53 *
michael@0 54 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
michael@0 55 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
michael@0 56 *
michael@0 57 * To avoid floating-point arithmetic, we represent the fractional constants
michael@0 58 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
michael@0 59 * the products by 2^16, with appropriate rounding, to get the correct answer.
michael@0 60 * Notice that Y, being an integral input, does not contribute any fraction
michael@0 61 * so it need not participate in the rounding.
michael@0 62 *
michael@0 63 * For even more speed, we avoid doing any multiplications in the inner loop
michael@0 64 * by precalculating the constants times Cb and Cr for all possible values.
michael@0 65 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
michael@0 66 * for 12-bit samples it is still acceptable. It's not very reasonable for
michael@0 67 * 16-bit samples, but if you want lossless storage you shouldn't be changing
michael@0 68 * colorspace anyway.
michael@0 69 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
michael@0 70 * values for the G calculation are left scaled up, since we must add them
michael@0 71 * together before rounding.
michael@0 72 */
michael@0 73
michael@0 74 #define SCALEBITS 16 /* speediest right-shift on some machines */
michael@0 75 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
michael@0 76 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
michael@0 77
michael@0 78 /* We allocate one big table for RGB->Y conversion and divide it up into
michael@0 79 * three parts, instead of doing three alloc_small requests. This lets us
michael@0 80 * use a single table base address, which can be held in a register in the
michael@0 81 * inner loops on many machines (more than can hold all three addresses,
michael@0 82 * anyway).
michael@0 83 */
michael@0 84
michael@0 85 #define R_Y_OFF 0 /* offset to R => Y section */
michael@0 86 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
michael@0 87 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
michael@0 88 #define TABLE_SIZE (3*(MAXJSAMPLE+1))
michael@0 89
michael@0 90
michael@0 91 /* Include inline routines for colorspace extensions */
michael@0 92
michael@0 93 #include "jdcolext.c"
michael@0 94 #undef RGB_RED
michael@0 95 #undef RGB_GREEN
michael@0 96 #undef RGB_BLUE
michael@0 97 #undef RGB_PIXELSIZE
michael@0 98
michael@0 99 #define RGB_RED EXT_RGB_RED
michael@0 100 #define RGB_GREEN EXT_RGB_GREEN
michael@0 101 #define RGB_BLUE EXT_RGB_BLUE
michael@0 102 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
michael@0 103 #define ycc_rgb_convert_internal ycc_extrgb_convert_internal
michael@0 104 #define gray_rgb_convert_internal gray_extrgb_convert_internal
michael@0 105 #define rgb_rgb_convert_internal rgb_extrgb_convert_internal
michael@0 106 #include "jdcolext.c"
michael@0 107 #undef RGB_RED
michael@0 108 #undef RGB_GREEN
michael@0 109 #undef RGB_BLUE
michael@0 110 #undef RGB_PIXELSIZE
michael@0 111 #undef ycc_rgb_convert_internal
michael@0 112 #undef gray_rgb_convert_internal
michael@0 113 #undef rgb_rgb_convert_internal
michael@0 114
michael@0 115 #define RGB_RED EXT_RGBX_RED
michael@0 116 #define RGB_GREEN EXT_RGBX_GREEN
michael@0 117 #define RGB_BLUE EXT_RGBX_BLUE
michael@0 118 #define RGB_ALPHA 3
michael@0 119 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
michael@0 120 #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
michael@0 121 #define gray_rgb_convert_internal gray_extrgbx_convert_internal
michael@0 122 #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
michael@0 123 #include "jdcolext.c"
michael@0 124 #undef RGB_RED
michael@0 125 #undef RGB_GREEN
michael@0 126 #undef RGB_BLUE
michael@0 127 #undef RGB_ALPHA
michael@0 128 #undef RGB_PIXELSIZE
michael@0 129 #undef ycc_rgb_convert_internal
michael@0 130 #undef gray_rgb_convert_internal
michael@0 131 #undef rgb_rgb_convert_internal
michael@0 132
michael@0 133 #define RGB_RED EXT_BGR_RED
michael@0 134 #define RGB_GREEN EXT_BGR_GREEN
michael@0 135 #define RGB_BLUE EXT_BGR_BLUE
michael@0 136 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
michael@0 137 #define ycc_rgb_convert_internal ycc_extbgr_convert_internal
michael@0 138 #define gray_rgb_convert_internal gray_extbgr_convert_internal
michael@0 139 #define rgb_rgb_convert_internal rgb_extbgr_convert_internal
michael@0 140 #include "jdcolext.c"
michael@0 141 #undef RGB_RED
michael@0 142 #undef RGB_GREEN
michael@0 143 #undef RGB_BLUE
michael@0 144 #undef RGB_PIXELSIZE
michael@0 145 #undef ycc_rgb_convert_internal
michael@0 146 #undef gray_rgb_convert_internal
michael@0 147 #undef rgb_rgb_convert_internal
michael@0 148
michael@0 149 #define RGB_RED EXT_BGRX_RED
michael@0 150 #define RGB_GREEN EXT_BGRX_GREEN
michael@0 151 #define RGB_BLUE EXT_BGRX_BLUE
michael@0 152 #define RGB_ALPHA 3
michael@0 153 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
michael@0 154 #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
michael@0 155 #define gray_rgb_convert_internal gray_extbgrx_convert_internal
michael@0 156 #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
michael@0 157 #include "jdcolext.c"
michael@0 158 #undef RGB_RED
michael@0 159 #undef RGB_GREEN
michael@0 160 #undef RGB_BLUE
michael@0 161 #undef RGB_ALPHA
michael@0 162 #undef RGB_PIXELSIZE
michael@0 163 #undef ycc_rgb_convert_internal
michael@0 164 #undef gray_rgb_convert_internal
michael@0 165 #undef rgb_rgb_convert_internal
michael@0 166
michael@0 167 #define RGB_RED EXT_XBGR_RED
michael@0 168 #define RGB_GREEN EXT_XBGR_GREEN
michael@0 169 #define RGB_BLUE EXT_XBGR_BLUE
michael@0 170 #define RGB_ALPHA 0
michael@0 171 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
michael@0 172 #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
michael@0 173 #define gray_rgb_convert_internal gray_extxbgr_convert_internal
michael@0 174 #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
michael@0 175 #include "jdcolext.c"
michael@0 176 #undef RGB_RED
michael@0 177 #undef RGB_GREEN
michael@0 178 #undef RGB_BLUE
michael@0 179 #undef RGB_ALPHA
michael@0 180 #undef RGB_PIXELSIZE
michael@0 181 #undef ycc_rgb_convert_internal
michael@0 182 #undef gray_rgb_convert_internal
michael@0 183 #undef rgb_rgb_convert_internal
michael@0 184
michael@0 185 #define RGB_RED EXT_XRGB_RED
michael@0 186 #define RGB_GREEN EXT_XRGB_GREEN
michael@0 187 #define RGB_BLUE EXT_XRGB_BLUE
michael@0 188 #define RGB_ALPHA 0
michael@0 189 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
michael@0 190 #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
michael@0 191 #define gray_rgb_convert_internal gray_extxrgb_convert_internal
michael@0 192 #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
michael@0 193 #include "jdcolext.c"
michael@0 194 #undef RGB_RED
michael@0 195 #undef RGB_GREEN
michael@0 196 #undef RGB_BLUE
michael@0 197 #undef RGB_ALPHA
michael@0 198 #undef RGB_PIXELSIZE
michael@0 199 #undef ycc_rgb_convert_internal
michael@0 200 #undef gray_rgb_convert_internal
michael@0 201 #undef rgb_rgb_convert_internal
michael@0 202
michael@0 203
michael@0 204 /*
michael@0 205 * Initialize tables for YCC->RGB colorspace conversion.
michael@0 206 */
michael@0 207
michael@0 208 LOCAL(void)
michael@0 209 build_ycc_rgb_table (j_decompress_ptr cinfo)
michael@0 210 {
michael@0 211 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
michael@0 212 int i;
michael@0 213 INT32 x;
michael@0 214 SHIFT_TEMPS
michael@0 215
michael@0 216 cconvert->Cr_r_tab = (int *)
michael@0 217 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 218 (MAXJSAMPLE+1) * SIZEOF(int));
michael@0 219 cconvert->Cb_b_tab = (int *)
michael@0 220 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 221 (MAXJSAMPLE+1) * SIZEOF(int));
michael@0 222 cconvert->Cr_g_tab = (INT32 *)
michael@0 223 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 224 (MAXJSAMPLE+1) * SIZEOF(INT32));
michael@0 225 cconvert->Cb_g_tab = (INT32 *)
michael@0 226 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 227 (MAXJSAMPLE+1) * SIZEOF(INT32));
michael@0 228
michael@0 229 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
michael@0 230 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
michael@0 231 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
michael@0 232 /* Cr=>R value is nearest int to 1.40200 * x */
michael@0 233 cconvert->Cr_r_tab[i] = (int)
michael@0 234 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
michael@0 235 /* Cb=>B value is nearest int to 1.77200 * x */
michael@0 236 cconvert->Cb_b_tab[i] = (int)
michael@0 237 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
michael@0 238 /* Cr=>G value is scaled-up -0.71414 * x */
michael@0 239 cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
michael@0 240 /* Cb=>G value is scaled-up -0.34414 * x */
michael@0 241 /* We also add in ONE_HALF so that need not do it in inner loop */
michael@0 242 cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
michael@0 243 }
michael@0 244 }
michael@0 245
michael@0 246
michael@0 247 /*
michael@0 248 * Convert some rows of samples to the output colorspace.
michael@0 249 */
michael@0 250
michael@0 251 METHODDEF(void)
michael@0 252 ycc_rgb_convert (j_decompress_ptr cinfo,
michael@0 253 JSAMPIMAGE input_buf, JDIMENSION input_row,
michael@0 254 JSAMPARRAY output_buf, int num_rows)
michael@0 255 {
michael@0 256 switch (cinfo->out_color_space) {
michael@0 257 case JCS_EXT_RGB:
michael@0 258 ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 259 num_rows);
michael@0 260 break;
michael@0 261 case JCS_EXT_RGBX:
michael@0 262 case JCS_EXT_RGBA:
michael@0 263 ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 264 num_rows);
michael@0 265 break;
michael@0 266 case JCS_EXT_BGR:
michael@0 267 ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 268 num_rows);
michael@0 269 break;
michael@0 270 case JCS_EXT_BGRX:
michael@0 271 case JCS_EXT_BGRA:
michael@0 272 ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 273 num_rows);
michael@0 274 break;
michael@0 275 case JCS_EXT_XBGR:
michael@0 276 case JCS_EXT_ABGR:
michael@0 277 ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 278 num_rows);
michael@0 279 break;
michael@0 280 case JCS_EXT_XRGB:
michael@0 281 case JCS_EXT_ARGB:
michael@0 282 ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 283 num_rows);
michael@0 284 break;
michael@0 285 default:
michael@0 286 ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 287 num_rows);
michael@0 288 break;
michael@0 289 }
michael@0 290 }
michael@0 291
michael@0 292
michael@0 293 /**************** Cases other than YCbCr -> RGB **************/
michael@0 294
michael@0 295
michael@0 296 /*
michael@0 297 * Initialize for RGB->grayscale colorspace conversion.
michael@0 298 */
michael@0 299
michael@0 300 LOCAL(void)
michael@0 301 build_rgb_y_table (j_decompress_ptr cinfo)
michael@0 302 {
michael@0 303 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
michael@0 304 INT32 * rgb_y_tab;
michael@0 305 INT32 i;
michael@0 306
michael@0 307 /* Allocate and fill in the conversion tables. */
michael@0 308 cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
michael@0 309 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 310 (TABLE_SIZE * SIZEOF(INT32)));
michael@0 311
michael@0 312 for (i = 0; i <= MAXJSAMPLE; i++) {
michael@0 313 rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
michael@0 314 rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
michael@0 315 rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
michael@0 316 }
michael@0 317 }
michael@0 318
michael@0 319
michael@0 320 /*
michael@0 321 * Convert RGB to grayscale.
michael@0 322 */
michael@0 323
michael@0 324 METHODDEF(void)
michael@0 325 rgb_gray_convert (j_decompress_ptr cinfo,
michael@0 326 JSAMPIMAGE input_buf, JDIMENSION input_row,
michael@0 327 JSAMPARRAY output_buf, int num_rows)
michael@0 328 {
michael@0 329 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
michael@0 330 register int r, g, b;
michael@0 331 register INT32 * ctab = cconvert->rgb_y_tab;
michael@0 332 register JSAMPROW outptr;
michael@0 333 register JSAMPROW inptr0, inptr1, inptr2;
michael@0 334 register JDIMENSION col;
michael@0 335 JDIMENSION num_cols = cinfo->output_width;
michael@0 336
michael@0 337 while (--num_rows >= 0) {
michael@0 338 inptr0 = input_buf[0][input_row];
michael@0 339 inptr1 = input_buf[1][input_row];
michael@0 340 inptr2 = input_buf[2][input_row];
michael@0 341 input_row++;
michael@0 342 outptr = *output_buf++;
michael@0 343 for (col = 0; col < num_cols; col++) {
michael@0 344 r = GETJSAMPLE(inptr0[col]);
michael@0 345 g = GETJSAMPLE(inptr1[col]);
michael@0 346 b = GETJSAMPLE(inptr2[col]);
michael@0 347 /* Y */
michael@0 348 outptr[col] = (JSAMPLE)
michael@0 349 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
michael@0 350 >> SCALEBITS);
michael@0 351 }
michael@0 352 }
michael@0 353 }
michael@0 354
michael@0 355
michael@0 356 /*
michael@0 357 * Color conversion for no colorspace change: just copy the data,
michael@0 358 * converting from separate-planes to interleaved representation.
michael@0 359 */
michael@0 360
michael@0 361 METHODDEF(void)
michael@0 362 null_convert (j_decompress_ptr cinfo,
michael@0 363 JSAMPIMAGE input_buf, JDIMENSION input_row,
michael@0 364 JSAMPARRAY output_buf, int num_rows)
michael@0 365 {
michael@0 366 register JSAMPROW inptr, outptr;
michael@0 367 register JDIMENSION count;
michael@0 368 register int num_components = cinfo->num_components;
michael@0 369 JDIMENSION num_cols = cinfo->output_width;
michael@0 370 int ci;
michael@0 371
michael@0 372 while (--num_rows >= 0) {
michael@0 373 for (ci = 0; ci < num_components; ci++) {
michael@0 374 inptr = input_buf[ci][input_row];
michael@0 375 outptr = output_buf[0] + ci;
michael@0 376 for (count = num_cols; count > 0; count--) {
michael@0 377 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
michael@0 378 outptr += num_components;
michael@0 379 }
michael@0 380 }
michael@0 381 input_row++;
michael@0 382 output_buf++;
michael@0 383 }
michael@0 384 }
michael@0 385
michael@0 386
michael@0 387 /*
michael@0 388 * Color conversion for grayscale: just copy the data.
michael@0 389 * This also works for YCbCr -> grayscale conversion, in which
michael@0 390 * we just copy the Y (luminance) component and ignore chrominance.
michael@0 391 */
michael@0 392
michael@0 393 METHODDEF(void)
michael@0 394 grayscale_convert (j_decompress_ptr cinfo,
michael@0 395 JSAMPIMAGE input_buf, JDIMENSION input_row,
michael@0 396 JSAMPARRAY output_buf, int num_rows)
michael@0 397 {
michael@0 398 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
michael@0 399 num_rows, cinfo->output_width);
michael@0 400 }
michael@0 401
michael@0 402
michael@0 403 /*
michael@0 404 * Convert grayscale to RGB
michael@0 405 */
michael@0 406
michael@0 407 METHODDEF(void)
michael@0 408 gray_rgb_convert (j_decompress_ptr cinfo,
michael@0 409 JSAMPIMAGE input_buf, JDIMENSION input_row,
michael@0 410 JSAMPARRAY output_buf, int num_rows)
michael@0 411 {
michael@0 412 switch (cinfo->out_color_space) {
michael@0 413 case JCS_EXT_RGB:
michael@0 414 gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 415 num_rows);
michael@0 416 break;
michael@0 417 case JCS_EXT_RGBX:
michael@0 418 case JCS_EXT_RGBA:
michael@0 419 gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 420 num_rows);
michael@0 421 break;
michael@0 422 case JCS_EXT_BGR:
michael@0 423 gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 424 num_rows);
michael@0 425 break;
michael@0 426 case JCS_EXT_BGRX:
michael@0 427 case JCS_EXT_BGRA:
michael@0 428 gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 429 num_rows);
michael@0 430 break;
michael@0 431 case JCS_EXT_XBGR:
michael@0 432 case JCS_EXT_ABGR:
michael@0 433 gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 434 num_rows);
michael@0 435 break;
michael@0 436 case JCS_EXT_XRGB:
michael@0 437 case JCS_EXT_ARGB:
michael@0 438 gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 439 num_rows);
michael@0 440 break;
michael@0 441 default:
michael@0 442 gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 443 num_rows);
michael@0 444 break;
michael@0 445 }
michael@0 446 }
michael@0 447
michael@0 448
michael@0 449 /*
michael@0 450 * Convert plain RGB to extended RGB
michael@0 451 */
michael@0 452
michael@0 453 METHODDEF(void)
michael@0 454 rgb_rgb_convert (j_decompress_ptr cinfo,
michael@0 455 JSAMPIMAGE input_buf, JDIMENSION input_row,
michael@0 456 JSAMPARRAY output_buf, int num_rows)
michael@0 457 {
michael@0 458 switch (cinfo->out_color_space) {
michael@0 459 case JCS_EXT_RGB:
michael@0 460 rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 461 num_rows);
michael@0 462 break;
michael@0 463 case JCS_EXT_RGBX:
michael@0 464 case JCS_EXT_RGBA:
michael@0 465 rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 466 num_rows);
michael@0 467 break;
michael@0 468 case JCS_EXT_BGR:
michael@0 469 rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 470 num_rows);
michael@0 471 break;
michael@0 472 case JCS_EXT_BGRX:
michael@0 473 case JCS_EXT_BGRA:
michael@0 474 rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 475 num_rows);
michael@0 476 break;
michael@0 477 case JCS_EXT_XBGR:
michael@0 478 case JCS_EXT_ABGR:
michael@0 479 rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 480 num_rows);
michael@0 481 break;
michael@0 482 case JCS_EXT_XRGB:
michael@0 483 case JCS_EXT_ARGB:
michael@0 484 rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 485 num_rows);
michael@0 486 break;
michael@0 487 default:
michael@0 488 rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
michael@0 489 num_rows);
michael@0 490 break;
michael@0 491 }
michael@0 492 }
michael@0 493
michael@0 494
michael@0 495 /*
michael@0 496 * Adobe-style YCCK->CMYK conversion.
michael@0 497 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
michael@0 498 * conversion as above, while passing K (black) unchanged.
michael@0 499 * We assume build_ycc_rgb_table has been called.
michael@0 500 */
michael@0 501
michael@0 502 METHODDEF(void)
michael@0 503 ycck_cmyk_convert (j_decompress_ptr cinfo,
michael@0 504 JSAMPIMAGE input_buf, JDIMENSION input_row,
michael@0 505 JSAMPARRAY output_buf, int num_rows)
michael@0 506 {
michael@0 507 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
michael@0 508 register int y, cb, cr;
michael@0 509 register JSAMPROW outptr;
michael@0 510 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
michael@0 511 register JDIMENSION col;
michael@0 512 JDIMENSION num_cols = cinfo->output_width;
michael@0 513 /* copy these pointers into registers if possible */
michael@0 514 register JSAMPLE * range_limit = cinfo->sample_range_limit;
michael@0 515 register int * Crrtab = cconvert->Cr_r_tab;
michael@0 516 register int * Cbbtab = cconvert->Cb_b_tab;
michael@0 517 register INT32 * Crgtab = cconvert->Cr_g_tab;
michael@0 518 register INT32 * Cbgtab = cconvert->Cb_g_tab;
michael@0 519 SHIFT_TEMPS
michael@0 520
michael@0 521 while (--num_rows >= 0) {
michael@0 522 inptr0 = input_buf[0][input_row];
michael@0 523 inptr1 = input_buf[1][input_row];
michael@0 524 inptr2 = input_buf[2][input_row];
michael@0 525 inptr3 = input_buf[3][input_row];
michael@0 526 input_row++;
michael@0 527 outptr = *output_buf++;
michael@0 528 for (col = 0; col < num_cols; col++) {
michael@0 529 y = GETJSAMPLE(inptr0[col]);
michael@0 530 cb = GETJSAMPLE(inptr1[col]);
michael@0 531 cr = GETJSAMPLE(inptr2[col]);
michael@0 532 /* Range-limiting is essential due to noise introduced by DCT losses. */
michael@0 533 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
michael@0 534 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
michael@0 535 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
michael@0 536 SCALEBITS)))];
michael@0 537 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
michael@0 538 /* K passes through unchanged */
michael@0 539 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
michael@0 540 outptr += 4;
michael@0 541 }
michael@0 542 }
michael@0 543 }
michael@0 544
michael@0 545
michael@0 546 /*
michael@0 547 * Empty method for start_pass.
michael@0 548 */
michael@0 549
michael@0 550 METHODDEF(void)
michael@0 551 start_pass_dcolor (j_decompress_ptr cinfo)
michael@0 552 {
michael@0 553 /* no work needed */
michael@0 554 }
michael@0 555
michael@0 556
michael@0 557 /*
michael@0 558 * Module initialization routine for output colorspace conversion.
michael@0 559 */
michael@0 560
michael@0 561 GLOBAL(void)
michael@0 562 jinit_color_deconverter (j_decompress_ptr cinfo)
michael@0 563 {
michael@0 564 my_cconvert_ptr cconvert;
michael@0 565 int ci;
michael@0 566
michael@0 567 cconvert = (my_cconvert_ptr)
michael@0 568 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 569 SIZEOF(my_color_deconverter));
michael@0 570 cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
michael@0 571 cconvert->pub.start_pass = start_pass_dcolor;
michael@0 572
michael@0 573 /* Make sure num_components agrees with jpeg_color_space */
michael@0 574 switch (cinfo->jpeg_color_space) {
michael@0 575 case JCS_GRAYSCALE:
michael@0 576 if (cinfo->num_components != 1)
michael@0 577 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 578 break;
michael@0 579
michael@0 580 case JCS_RGB:
michael@0 581 case JCS_YCbCr:
michael@0 582 if (cinfo->num_components != 3)
michael@0 583 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 584 break;
michael@0 585
michael@0 586 case JCS_CMYK:
michael@0 587 case JCS_YCCK:
michael@0 588 if (cinfo->num_components != 4)
michael@0 589 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 590 break;
michael@0 591
michael@0 592 default: /* JCS_UNKNOWN can be anything */
michael@0 593 if (cinfo->num_components < 1)
michael@0 594 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 595 break;
michael@0 596 }
michael@0 597
michael@0 598 /* Set out_color_components and conversion method based on requested space.
michael@0 599 * Also clear the component_needed flags for any unused components,
michael@0 600 * so that earlier pipeline stages can avoid useless computation.
michael@0 601 */
michael@0 602
michael@0 603 switch (cinfo->out_color_space) {
michael@0 604 case JCS_GRAYSCALE:
michael@0 605 cinfo->out_color_components = 1;
michael@0 606 if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
michael@0 607 cinfo->jpeg_color_space == JCS_YCbCr) {
michael@0 608 cconvert->pub.color_convert = grayscale_convert;
michael@0 609 /* For color->grayscale conversion, only the Y (0) component is needed */
michael@0 610 for (ci = 1; ci < cinfo->num_components; ci++)
michael@0 611 cinfo->comp_info[ci].component_needed = FALSE;
michael@0 612 } else if (cinfo->jpeg_color_space == JCS_RGB) {
michael@0 613 cconvert->pub.color_convert = rgb_gray_convert;
michael@0 614 build_rgb_y_table(cinfo);
michael@0 615 } else
michael@0 616 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 617 break;
michael@0 618
michael@0 619 case JCS_RGB:
michael@0 620 case JCS_EXT_RGB:
michael@0 621 case JCS_EXT_RGBX:
michael@0 622 case JCS_EXT_BGR:
michael@0 623 case JCS_EXT_BGRX:
michael@0 624 case JCS_EXT_XBGR:
michael@0 625 case JCS_EXT_XRGB:
michael@0 626 case JCS_EXT_RGBA:
michael@0 627 case JCS_EXT_BGRA:
michael@0 628 case JCS_EXT_ABGR:
michael@0 629 case JCS_EXT_ARGB:
michael@0 630 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
michael@0 631 if (cinfo->jpeg_color_space == JCS_YCbCr) {
michael@0 632 if (jsimd_can_ycc_rgb())
michael@0 633 cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
michael@0 634 else {
michael@0 635 cconvert->pub.color_convert = ycc_rgb_convert;
michael@0 636 build_ycc_rgb_table(cinfo);
michael@0 637 }
michael@0 638 } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
michael@0 639 cconvert->pub.color_convert = gray_rgb_convert;
michael@0 640 } else if (cinfo->jpeg_color_space == JCS_RGB) {
michael@0 641 if (rgb_red[cinfo->out_color_space] == 0 &&
michael@0 642 rgb_green[cinfo->out_color_space] == 1 &&
michael@0 643 rgb_blue[cinfo->out_color_space] == 2 &&
michael@0 644 rgb_pixelsize[cinfo->out_color_space] == 3)
michael@0 645 cconvert->pub.color_convert = null_convert;
michael@0 646 else
michael@0 647 cconvert->pub.color_convert = rgb_rgb_convert;
michael@0 648 } else
michael@0 649 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 650 break;
michael@0 651
michael@0 652 case JCS_CMYK:
michael@0 653 cinfo->out_color_components = 4;
michael@0 654 if (cinfo->jpeg_color_space == JCS_YCCK) {
michael@0 655 cconvert->pub.color_convert = ycck_cmyk_convert;
michael@0 656 build_ycc_rgb_table(cinfo);
michael@0 657 } else if (cinfo->jpeg_color_space == JCS_CMYK) {
michael@0 658 cconvert->pub.color_convert = null_convert;
michael@0 659 } else
michael@0 660 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 661 break;
michael@0 662
michael@0 663 default:
michael@0 664 /* Permit null conversion to same output space */
michael@0 665 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
michael@0 666 cinfo->out_color_components = cinfo->num_components;
michael@0 667 cconvert->pub.color_convert = null_convert;
michael@0 668 } else /* unsupported non-null conversion */
michael@0 669 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 670 break;
michael@0 671 }
michael@0 672
michael@0 673 if (cinfo->quantize_colors)
michael@0 674 cinfo->output_components = 1; /* single colormapped output component */
michael@0 675 else
michael@0 676 cinfo->output_components = cinfo->out_color_components;
michael@0 677 }

mercurial