media/libjpeg/jccolor.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 * jccolor.c
michael@0 3 *
michael@0 4 * This file was part of the Independent JPEG Group's software:
michael@0 5 * Copyright (C) 1991-1996, Thomas G. Lane.
michael@0 6 * libjpeg-turbo Modifications:
michael@0 7 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
michael@0 8 * Copyright (C) 2009-2012, D. R. Commander.
michael@0 9 * For conditions of distribution and use, see the accompanying README file.
michael@0 10 *
michael@0 11 * This file contains input colorspace conversion routines.
michael@0 12 */
michael@0 13
michael@0 14 #define JPEG_INTERNALS
michael@0 15 #include "jinclude.h"
michael@0 16 #include "jpeglib.h"
michael@0 17 #include "jsimd.h"
michael@0 18 #include "config.h"
michael@0 19
michael@0 20
michael@0 21 /* Private subobject */
michael@0 22
michael@0 23 typedef struct {
michael@0 24 struct jpeg_color_converter pub; /* public fields */
michael@0 25
michael@0 26 /* Private state for RGB->YCC conversion */
michael@0 27 INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
michael@0 28 } my_color_converter;
michael@0 29
michael@0 30 typedef my_color_converter * my_cconvert_ptr;
michael@0 31
michael@0 32
michael@0 33 /**************** RGB -> YCbCr conversion: most common case **************/
michael@0 34
michael@0 35 /*
michael@0 36 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
michael@0 37 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
michael@0 38 * The conversion equations to be implemented are therefore
michael@0 39 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
michael@0 40 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
michael@0 41 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
michael@0 42 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
michael@0 43 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
michael@0 44 * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
michael@0 45 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
michael@0 46 * were not represented exactly. Now we sacrifice exact representation of
michael@0 47 * maximum red and maximum blue in order to get exact grayscales.
michael@0 48 *
michael@0 49 * To avoid floating-point arithmetic, we represent the fractional constants
michael@0 50 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
michael@0 51 * the products by 2^16, with appropriate rounding, to get the correct answer.
michael@0 52 *
michael@0 53 * For even more speed, we avoid doing any multiplications in the inner loop
michael@0 54 * by precalculating the constants times R,G,B for all possible values.
michael@0 55 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
michael@0 56 * for 12-bit samples it is still acceptable. It's not very reasonable for
michael@0 57 * 16-bit samples, but if you want lossless storage you shouldn't be changing
michael@0 58 * colorspace anyway.
michael@0 59 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
michael@0 60 * in the tables to save adding them separately in the inner loop.
michael@0 61 */
michael@0 62
michael@0 63 #define SCALEBITS 16 /* speediest right-shift on some machines */
michael@0 64 #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
michael@0 65 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
michael@0 66 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
michael@0 67
michael@0 68 /* We allocate one big table and divide it up into eight parts, instead of
michael@0 69 * doing eight alloc_small requests. This lets us use a single table base
michael@0 70 * address, which can be held in a register in the inner loops on many
michael@0 71 * machines (more than can hold all eight addresses, anyway).
michael@0 72 */
michael@0 73
michael@0 74 #define R_Y_OFF 0 /* offset to R => Y section */
michael@0 75 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
michael@0 76 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
michael@0 77 #define R_CB_OFF (3*(MAXJSAMPLE+1))
michael@0 78 #define G_CB_OFF (4*(MAXJSAMPLE+1))
michael@0 79 #define B_CB_OFF (5*(MAXJSAMPLE+1))
michael@0 80 #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
michael@0 81 #define G_CR_OFF (6*(MAXJSAMPLE+1))
michael@0 82 #define B_CR_OFF (7*(MAXJSAMPLE+1))
michael@0 83 #define TABLE_SIZE (8*(MAXJSAMPLE+1))
michael@0 84
michael@0 85
michael@0 86 /* Include inline routines for colorspace extensions */
michael@0 87
michael@0 88 #include "jccolext.c"
michael@0 89 #undef RGB_RED
michael@0 90 #undef RGB_GREEN
michael@0 91 #undef RGB_BLUE
michael@0 92 #undef RGB_PIXELSIZE
michael@0 93
michael@0 94 #define RGB_RED EXT_RGB_RED
michael@0 95 #define RGB_GREEN EXT_RGB_GREEN
michael@0 96 #define RGB_BLUE EXT_RGB_BLUE
michael@0 97 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
michael@0 98 #define rgb_ycc_convert_internal extrgb_ycc_convert_internal
michael@0 99 #define rgb_gray_convert_internal extrgb_gray_convert_internal
michael@0 100 #define rgb_rgb_convert_internal extrgb_rgb_convert_internal
michael@0 101 #include "jccolext.c"
michael@0 102 #undef RGB_RED
michael@0 103 #undef RGB_GREEN
michael@0 104 #undef RGB_BLUE
michael@0 105 #undef RGB_PIXELSIZE
michael@0 106 #undef rgb_ycc_convert_internal
michael@0 107 #undef rgb_gray_convert_internal
michael@0 108 #undef rgb_rgb_convert_internal
michael@0 109
michael@0 110 #define RGB_RED EXT_RGBX_RED
michael@0 111 #define RGB_GREEN EXT_RGBX_GREEN
michael@0 112 #define RGB_BLUE EXT_RGBX_BLUE
michael@0 113 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
michael@0 114 #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal
michael@0 115 #define rgb_gray_convert_internal extrgbx_gray_convert_internal
michael@0 116 #define rgb_rgb_convert_internal extrgbx_rgb_convert_internal
michael@0 117 #include "jccolext.c"
michael@0 118 #undef RGB_RED
michael@0 119 #undef RGB_GREEN
michael@0 120 #undef RGB_BLUE
michael@0 121 #undef RGB_PIXELSIZE
michael@0 122 #undef rgb_ycc_convert_internal
michael@0 123 #undef rgb_gray_convert_internal
michael@0 124 #undef rgb_rgb_convert_internal
michael@0 125
michael@0 126 #define RGB_RED EXT_BGR_RED
michael@0 127 #define RGB_GREEN EXT_BGR_GREEN
michael@0 128 #define RGB_BLUE EXT_BGR_BLUE
michael@0 129 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
michael@0 130 #define rgb_ycc_convert_internal extbgr_ycc_convert_internal
michael@0 131 #define rgb_gray_convert_internal extbgr_gray_convert_internal
michael@0 132 #define rgb_rgb_convert_internal extbgr_rgb_convert_internal
michael@0 133 #include "jccolext.c"
michael@0 134 #undef RGB_RED
michael@0 135 #undef RGB_GREEN
michael@0 136 #undef RGB_BLUE
michael@0 137 #undef RGB_PIXELSIZE
michael@0 138 #undef rgb_ycc_convert_internal
michael@0 139 #undef rgb_gray_convert_internal
michael@0 140 #undef rgb_rgb_convert_internal
michael@0 141
michael@0 142 #define RGB_RED EXT_BGRX_RED
michael@0 143 #define RGB_GREEN EXT_BGRX_GREEN
michael@0 144 #define RGB_BLUE EXT_BGRX_BLUE
michael@0 145 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
michael@0 146 #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal
michael@0 147 #define rgb_gray_convert_internal extbgrx_gray_convert_internal
michael@0 148 #define rgb_rgb_convert_internal extbgrx_rgb_convert_internal
michael@0 149 #include "jccolext.c"
michael@0 150 #undef RGB_RED
michael@0 151 #undef RGB_GREEN
michael@0 152 #undef RGB_BLUE
michael@0 153 #undef RGB_PIXELSIZE
michael@0 154 #undef rgb_ycc_convert_internal
michael@0 155 #undef rgb_gray_convert_internal
michael@0 156 #undef rgb_rgb_convert_internal
michael@0 157
michael@0 158 #define RGB_RED EXT_XBGR_RED
michael@0 159 #define RGB_GREEN EXT_XBGR_GREEN
michael@0 160 #define RGB_BLUE EXT_XBGR_BLUE
michael@0 161 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
michael@0 162 #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal
michael@0 163 #define rgb_gray_convert_internal extxbgr_gray_convert_internal
michael@0 164 #define rgb_rgb_convert_internal extxbgr_rgb_convert_internal
michael@0 165 #include "jccolext.c"
michael@0 166 #undef RGB_RED
michael@0 167 #undef RGB_GREEN
michael@0 168 #undef RGB_BLUE
michael@0 169 #undef RGB_PIXELSIZE
michael@0 170 #undef rgb_ycc_convert_internal
michael@0 171 #undef rgb_gray_convert_internal
michael@0 172 #undef rgb_rgb_convert_internal
michael@0 173
michael@0 174 #define RGB_RED EXT_XRGB_RED
michael@0 175 #define RGB_GREEN EXT_XRGB_GREEN
michael@0 176 #define RGB_BLUE EXT_XRGB_BLUE
michael@0 177 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
michael@0 178 #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal
michael@0 179 #define rgb_gray_convert_internal extxrgb_gray_convert_internal
michael@0 180 #define rgb_rgb_convert_internal extxrgb_rgb_convert_internal
michael@0 181 #include "jccolext.c"
michael@0 182 #undef RGB_RED
michael@0 183 #undef RGB_GREEN
michael@0 184 #undef RGB_BLUE
michael@0 185 #undef RGB_PIXELSIZE
michael@0 186 #undef rgb_ycc_convert_internal
michael@0 187 #undef rgb_gray_convert_internal
michael@0 188 #undef rgb_rgb_convert_internal
michael@0 189
michael@0 190
michael@0 191 /*
michael@0 192 * Initialize for RGB->YCC colorspace conversion.
michael@0 193 */
michael@0 194
michael@0 195 METHODDEF(void)
michael@0 196 rgb_ycc_start (j_compress_ptr cinfo)
michael@0 197 {
michael@0 198 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
michael@0 199 INT32 * rgb_ycc_tab;
michael@0 200 INT32 i;
michael@0 201
michael@0 202 /* Allocate and fill in the conversion tables. */
michael@0 203 cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
michael@0 204 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 205 (TABLE_SIZE * SIZEOF(INT32)));
michael@0 206
michael@0 207 for (i = 0; i <= MAXJSAMPLE; i++) {
michael@0 208 rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
michael@0 209 rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
michael@0 210 rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
michael@0 211 rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
michael@0 212 rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
michael@0 213 /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
michael@0 214 * This ensures that the maximum output will round to MAXJSAMPLE
michael@0 215 * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
michael@0 216 */
michael@0 217 rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
michael@0 218 /* B=>Cb and R=>Cr tables are the same
michael@0 219 rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
michael@0 220 */
michael@0 221 rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
michael@0 222 rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
michael@0 223 }
michael@0 224 }
michael@0 225
michael@0 226
michael@0 227 /*
michael@0 228 * Convert some rows of samples to the JPEG colorspace.
michael@0 229 */
michael@0 230
michael@0 231 METHODDEF(void)
michael@0 232 rgb_ycc_convert (j_compress_ptr cinfo,
michael@0 233 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
michael@0 234 JDIMENSION output_row, int num_rows)
michael@0 235 {
michael@0 236 switch (cinfo->in_color_space) {
michael@0 237 case JCS_EXT_RGB:
michael@0 238 extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 239 num_rows);
michael@0 240 break;
michael@0 241 case JCS_EXT_RGBX:
michael@0 242 case JCS_EXT_RGBA:
michael@0 243 extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 244 num_rows);
michael@0 245 break;
michael@0 246 case JCS_EXT_BGR:
michael@0 247 extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 248 num_rows);
michael@0 249 break;
michael@0 250 case JCS_EXT_BGRX:
michael@0 251 case JCS_EXT_BGRA:
michael@0 252 extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 253 num_rows);
michael@0 254 break;
michael@0 255 case JCS_EXT_XBGR:
michael@0 256 case JCS_EXT_ABGR:
michael@0 257 extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 258 num_rows);
michael@0 259 break;
michael@0 260 case JCS_EXT_XRGB:
michael@0 261 case JCS_EXT_ARGB:
michael@0 262 extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 263 num_rows);
michael@0 264 break;
michael@0 265 default:
michael@0 266 rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 267 num_rows);
michael@0 268 break;
michael@0 269 }
michael@0 270 }
michael@0 271
michael@0 272
michael@0 273 /**************** Cases other than RGB -> YCbCr **************/
michael@0 274
michael@0 275
michael@0 276 /*
michael@0 277 * Convert some rows of samples to the JPEG colorspace.
michael@0 278 */
michael@0 279
michael@0 280 METHODDEF(void)
michael@0 281 rgb_gray_convert (j_compress_ptr cinfo,
michael@0 282 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
michael@0 283 JDIMENSION output_row, int num_rows)
michael@0 284 {
michael@0 285 switch (cinfo->in_color_space) {
michael@0 286 case JCS_EXT_RGB:
michael@0 287 extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 288 num_rows);
michael@0 289 break;
michael@0 290 case JCS_EXT_RGBX:
michael@0 291 case JCS_EXT_RGBA:
michael@0 292 extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 293 num_rows);
michael@0 294 break;
michael@0 295 case JCS_EXT_BGR:
michael@0 296 extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 297 num_rows);
michael@0 298 break;
michael@0 299 case JCS_EXT_BGRX:
michael@0 300 case JCS_EXT_BGRA:
michael@0 301 extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 302 num_rows);
michael@0 303 break;
michael@0 304 case JCS_EXT_XBGR:
michael@0 305 case JCS_EXT_ABGR:
michael@0 306 extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 307 num_rows);
michael@0 308 break;
michael@0 309 case JCS_EXT_XRGB:
michael@0 310 case JCS_EXT_ARGB:
michael@0 311 extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 312 num_rows);
michael@0 313 break;
michael@0 314 default:
michael@0 315 rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 316 num_rows);
michael@0 317 break;
michael@0 318 }
michael@0 319 }
michael@0 320
michael@0 321
michael@0 322 /*
michael@0 323 * Extended RGB to plain RGB conversion
michael@0 324 */
michael@0 325
michael@0 326 METHODDEF(void)
michael@0 327 rgb_rgb_convert (j_compress_ptr cinfo,
michael@0 328 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
michael@0 329 JDIMENSION output_row, int num_rows)
michael@0 330 {
michael@0 331 switch (cinfo->in_color_space) {
michael@0 332 case JCS_EXT_RGB:
michael@0 333 extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 334 num_rows);
michael@0 335 break;
michael@0 336 case JCS_EXT_RGBX:
michael@0 337 case JCS_EXT_RGBA:
michael@0 338 extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 339 num_rows);
michael@0 340 break;
michael@0 341 case JCS_EXT_BGR:
michael@0 342 extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 343 num_rows);
michael@0 344 break;
michael@0 345 case JCS_EXT_BGRX:
michael@0 346 case JCS_EXT_BGRA:
michael@0 347 extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 348 num_rows);
michael@0 349 break;
michael@0 350 case JCS_EXT_XBGR:
michael@0 351 case JCS_EXT_ABGR:
michael@0 352 extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 353 num_rows);
michael@0 354 break;
michael@0 355 case JCS_EXT_XRGB:
michael@0 356 case JCS_EXT_ARGB:
michael@0 357 extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 358 num_rows);
michael@0 359 break;
michael@0 360 default:
michael@0 361 rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
michael@0 362 num_rows);
michael@0 363 break;
michael@0 364 }
michael@0 365 }
michael@0 366
michael@0 367
michael@0 368 /*
michael@0 369 * Convert some rows of samples to the JPEG colorspace.
michael@0 370 * This version handles Adobe-style CMYK->YCCK conversion,
michael@0 371 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
michael@0 372 * conversion as above, while passing K (black) unchanged.
michael@0 373 * We assume rgb_ycc_start has been called.
michael@0 374 */
michael@0 375
michael@0 376 METHODDEF(void)
michael@0 377 cmyk_ycck_convert (j_compress_ptr cinfo,
michael@0 378 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
michael@0 379 JDIMENSION output_row, int num_rows)
michael@0 380 {
michael@0 381 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
michael@0 382 register int r, g, b;
michael@0 383 register INT32 * ctab = cconvert->rgb_ycc_tab;
michael@0 384 register JSAMPROW inptr;
michael@0 385 register JSAMPROW outptr0, outptr1, outptr2, outptr3;
michael@0 386 register JDIMENSION col;
michael@0 387 JDIMENSION num_cols = cinfo->image_width;
michael@0 388
michael@0 389 while (--num_rows >= 0) {
michael@0 390 inptr = *input_buf++;
michael@0 391 outptr0 = output_buf[0][output_row];
michael@0 392 outptr1 = output_buf[1][output_row];
michael@0 393 outptr2 = output_buf[2][output_row];
michael@0 394 outptr3 = output_buf[3][output_row];
michael@0 395 output_row++;
michael@0 396 for (col = 0; col < num_cols; col++) {
michael@0 397 r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
michael@0 398 g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
michael@0 399 b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
michael@0 400 /* K passes through as-is */
michael@0 401 outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
michael@0 402 inptr += 4;
michael@0 403 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
michael@0 404 * must be too; we do not need an explicit range-limiting operation.
michael@0 405 * Hence the value being shifted is never negative, and we don't
michael@0 406 * need the general RIGHT_SHIFT macro.
michael@0 407 */
michael@0 408 /* Y */
michael@0 409 outptr0[col] = (JSAMPLE)
michael@0 410 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
michael@0 411 >> SCALEBITS);
michael@0 412 /* Cb */
michael@0 413 outptr1[col] = (JSAMPLE)
michael@0 414 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
michael@0 415 >> SCALEBITS);
michael@0 416 /* Cr */
michael@0 417 outptr2[col] = (JSAMPLE)
michael@0 418 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
michael@0 419 >> SCALEBITS);
michael@0 420 }
michael@0 421 }
michael@0 422 }
michael@0 423
michael@0 424
michael@0 425 /*
michael@0 426 * Convert some rows of samples to the JPEG colorspace.
michael@0 427 * This version handles grayscale output with no conversion.
michael@0 428 * The source can be either plain grayscale or YCbCr (since Y == gray).
michael@0 429 */
michael@0 430
michael@0 431 METHODDEF(void)
michael@0 432 grayscale_convert (j_compress_ptr cinfo,
michael@0 433 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
michael@0 434 JDIMENSION output_row, int num_rows)
michael@0 435 {
michael@0 436 register JSAMPROW inptr;
michael@0 437 register JSAMPROW outptr;
michael@0 438 register JDIMENSION col;
michael@0 439 JDIMENSION num_cols = cinfo->image_width;
michael@0 440 int instride = cinfo->input_components;
michael@0 441
michael@0 442 while (--num_rows >= 0) {
michael@0 443 inptr = *input_buf++;
michael@0 444 outptr = output_buf[0][output_row];
michael@0 445 output_row++;
michael@0 446 for (col = 0; col < num_cols; col++) {
michael@0 447 outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
michael@0 448 inptr += instride;
michael@0 449 }
michael@0 450 }
michael@0 451 }
michael@0 452
michael@0 453
michael@0 454 /*
michael@0 455 * Convert some rows of samples to the JPEG colorspace.
michael@0 456 * This version handles multi-component colorspaces without conversion.
michael@0 457 * We assume input_components == num_components.
michael@0 458 */
michael@0 459
michael@0 460 METHODDEF(void)
michael@0 461 null_convert (j_compress_ptr cinfo,
michael@0 462 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
michael@0 463 JDIMENSION output_row, int num_rows)
michael@0 464 {
michael@0 465 register JSAMPROW inptr;
michael@0 466 register JSAMPROW outptr;
michael@0 467 register JDIMENSION col;
michael@0 468 register int ci;
michael@0 469 int nc = cinfo->num_components;
michael@0 470 JDIMENSION num_cols = cinfo->image_width;
michael@0 471
michael@0 472 while (--num_rows >= 0) {
michael@0 473 /* It seems fastest to make a separate pass for each component. */
michael@0 474 for (ci = 0; ci < nc; ci++) {
michael@0 475 inptr = *input_buf;
michael@0 476 outptr = output_buf[ci][output_row];
michael@0 477 for (col = 0; col < num_cols; col++) {
michael@0 478 outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
michael@0 479 inptr += nc;
michael@0 480 }
michael@0 481 }
michael@0 482 input_buf++;
michael@0 483 output_row++;
michael@0 484 }
michael@0 485 }
michael@0 486
michael@0 487
michael@0 488 /*
michael@0 489 * Empty method for start_pass.
michael@0 490 */
michael@0 491
michael@0 492 METHODDEF(void)
michael@0 493 null_method (j_compress_ptr cinfo)
michael@0 494 {
michael@0 495 /* no work needed */
michael@0 496 }
michael@0 497
michael@0 498
michael@0 499 /*
michael@0 500 * Module initialization routine for input colorspace conversion.
michael@0 501 */
michael@0 502
michael@0 503 GLOBAL(void)
michael@0 504 jinit_color_converter (j_compress_ptr cinfo)
michael@0 505 {
michael@0 506 my_cconvert_ptr cconvert;
michael@0 507
michael@0 508 cconvert = (my_cconvert_ptr)
michael@0 509 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 510 SIZEOF(my_color_converter));
michael@0 511 cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
michael@0 512 /* set start_pass to null method until we find out differently */
michael@0 513 cconvert->pub.start_pass = null_method;
michael@0 514
michael@0 515 /* Make sure input_components agrees with in_color_space */
michael@0 516 switch (cinfo->in_color_space) {
michael@0 517 case JCS_GRAYSCALE:
michael@0 518 if (cinfo->input_components != 1)
michael@0 519 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
michael@0 520 break;
michael@0 521
michael@0 522 case JCS_RGB:
michael@0 523 case JCS_EXT_RGB:
michael@0 524 case JCS_EXT_RGBX:
michael@0 525 case JCS_EXT_BGR:
michael@0 526 case JCS_EXT_BGRX:
michael@0 527 case JCS_EXT_XBGR:
michael@0 528 case JCS_EXT_XRGB:
michael@0 529 case JCS_EXT_RGBA:
michael@0 530 case JCS_EXT_BGRA:
michael@0 531 case JCS_EXT_ABGR:
michael@0 532 case JCS_EXT_ARGB:
michael@0 533 if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
michael@0 534 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
michael@0 535 break;
michael@0 536
michael@0 537 case JCS_YCbCr:
michael@0 538 if (cinfo->input_components != 3)
michael@0 539 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
michael@0 540 break;
michael@0 541
michael@0 542 case JCS_CMYK:
michael@0 543 case JCS_YCCK:
michael@0 544 if (cinfo->input_components != 4)
michael@0 545 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
michael@0 546 break;
michael@0 547
michael@0 548 default: /* JCS_UNKNOWN can be anything */
michael@0 549 if (cinfo->input_components < 1)
michael@0 550 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
michael@0 551 break;
michael@0 552 }
michael@0 553
michael@0 554 /* Check num_components, set conversion method based on requested space */
michael@0 555 switch (cinfo->jpeg_color_space) {
michael@0 556 case JCS_GRAYSCALE:
michael@0 557 if (cinfo->num_components != 1)
michael@0 558 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 559 if (cinfo->in_color_space == JCS_GRAYSCALE)
michael@0 560 cconvert->pub.color_convert = grayscale_convert;
michael@0 561 else if (cinfo->in_color_space == JCS_RGB ||
michael@0 562 cinfo->in_color_space == JCS_EXT_RGB ||
michael@0 563 cinfo->in_color_space == JCS_EXT_RGBX ||
michael@0 564 cinfo->in_color_space == JCS_EXT_BGR ||
michael@0 565 cinfo->in_color_space == JCS_EXT_BGRX ||
michael@0 566 cinfo->in_color_space == JCS_EXT_XBGR ||
michael@0 567 cinfo->in_color_space == JCS_EXT_XRGB ||
michael@0 568 cinfo->in_color_space == JCS_EXT_RGBA ||
michael@0 569 cinfo->in_color_space == JCS_EXT_BGRA ||
michael@0 570 cinfo->in_color_space == JCS_EXT_ABGR ||
michael@0 571 cinfo->in_color_space == JCS_EXT_ARGB) {
michael@0 572 if (jsimd_can_rgb_gray())
michael@0 573 cconvert->pub.color_convert = jsimd_rgb_gray_convert;
michael@0 574 else {
michael@0 575 cconvert->pub.start_pass = rgb_ycc_start;
michael@0 576 cconvert->pub.color_convert = rgb_gray_convert;
michael@0 577 }
michael@0 578 } else if (cinfo->in_color_space == JCS_YCbCr)
michael@0 579 cconvert->pub.color_convert = grayscale_convert;
michael@0 580 else
michael@0 581 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 582 break;
michael@0 583
michael@0 584 case JCS_RGB:
michael@0 585 if (cinfo->num_components != 3)
michael@0 586 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 587 if (rgb_red[cinfo->in_color_space] == 0 &&
michael@0 588 rgb_green[cinfo->in_color_space] == 1 &&
michael@0 589 rgb_blue[cinfo->in_color_space] == 2 &&
michael@0 590 rgb_pixelsize[cinfo->in_color_space] == 3)
michael@0 591 cconvert->pub.color_convert = null_convert;
michael@0 592 else if (cinfo->in_color_space == JCS_RGB ||
michael@0 593 cinfo->in_color_space == JCS_EXT_RGB ||
michael@0 594 cinfo->in_color_space == JCS_EXT_RGBX ||
michael@0 595 cinfo->in_color_space == JCS_EXT_BGR ||
michael@0 596 cinfo->in_color_space == JCS_EXT_BGRX ||
michael@0 597 cinfo->in_color_space == JCS_EXT_XBGR ||
michael@0 598 cinfo->in_color_space == JCS_EXT_XRGB ||
michael@0 599 cinfo->in_color_space == JCS_EXT_RGBA ||
michael@0 600 cinfo->in_color_space == JCS_EXT_BGRA ||
michael@0 601 cinfo->in_color_space == JCS_EXT_ABGR ||
michael@0 602 cinfo->in_color_space == JCS_EXT_ARGB)
michael@0 603 cconvert->pub.color_convert = rgb_rgb_convert;
michael@0 604 else
michael@0 605 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 606 break;
michael@0 607
michael@0 608 case JCS_YCbCr:
michael@0 609 if (cinfo->num_components != 3)
michael@0 610 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 611 if (cinfo->in_color_space == JCS_RGB ||
michael@0 612 cinfo->in_color_space == JCS_EXT_RGB ||
michael@0 613 cinfo->in_color_space == JCS_EXT_RGBX ||
michael@0 614 cinfo->in_color_space == JCS_EXT_BGR ||
michael@0 615 cinfo->in_color_space == JCS_EXT_BGRX ||
michael@0 616 cinfo->in_color_space == JCS_EXT_XBGR ||
michael@0 617 cinfo->in_color_space == JCS_EXT_XRGB ||
michael@0 618 cinfo->in_color_space == JCS_EXT_RGBA ||
michael@0 619 cinfo->in_color_space == JCS_EXT_BGRA ||
michael@0 620 cinfo->in_color_space == JCS_EXT_ABGR ||
michael@0 621 cinfo->in_color_space == JCS_EXT_ARGB) {
michael@0 622 if (jsimd_can_rgb_ycc())
michael@0 623 cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
michael@0 624 else {
michael@0 625 cconvert->pub.start_pass = rgb_ycc_start;
michael@0 626 cconvert->pub.color_convert = rgb_ycc_convert;
michael@0 627 }
michael@0 628 } else if (cinfo->in_color_space == JCS_YCbCr)
michael@0 629 cconvert->pub.color_convert = null_convert;
michael@0 630 else
michael@0 631 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 632 break;
michael@0 633
michael@0 634 case JCS_CMYK:
michael@0 635 if (cinfo->num_components != 4)
michael@0 636 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 637 if (cinfo->in_color_space == JCS_CMYK)
michael@0 638 cconvert->pub.color_convert = null_convert;
michael@0 639 else
michael@0 640 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 641 break;
michael@0 642
michael@0 643 case JCS_YCCK:
michael@0 644 if (cinfo->num_components != 4)
michael@0 645 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
michael@0 646 if (cinfo->in_color_space == JCS_CMYK) {
michael@0 647 cconvert->pub.start_pass = rgb_ycc_start;
michael@0 648 cconvert->pub.color_convert = cmyk_ycck_convert;
michael@0 649 } else if (cinfo->in_color_space == JCS_YCCK)
michael@0 650 cconvert->pub.color_convert = null_convert;
michael@0 651 else
michael@0 652 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 653 break;
michael@0 654
michael@0 655 default: /* allow null conversion of JCS_UNKNOWN */
michael@0 656 if (cinfo->jpeg_color_space != cinfo->in_color_space ||
michael@0 657 cinfo->num_components != cinfo->input_components)
michael@0 658 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
michael@0 659 cconvert->pub.color_convert = null_convert;
michael@0 660 break;
michael@0 661 }
michael@0 662 }

mercurial