media/libjpeg/jdcolor.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jdcolor.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,677 @@
     1.4 +/*
     1.5 + * jdcolor.c
     1.6 + *
     1.7 + * This file was part of the Independent JPEG Group's software:
     1.8 + * Copyright (C) 1991-1997, Thomas G. Lane.
     1.9 + * Modified 2011 by Guido Vollbeding.
    1.10 + * libjpeg-turbo Modifications:
    1.11 + * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
    1.12 + * Copyright (C) 2009, 2011-2012, D. R. Commander.
    1.13 + * For conditions of distribution and use, see the accompanying README file.
    1.14 + *
    1.15 + * This file contains output colorspace conversion routines.
    1.16 + */
    1.17 +
    1.18 +#define JPEG_INTERNALS
    1.19 +#include "jinclude.h"
    1.20 +#include "jpeglib.h"
    1.21 +#include "jsimd.h"
    1.22 +#include "config.h"
    1.23 +
    1.24 +
    1.25 +/* Private subobject */
    1.26 +
    1.27 +typedef struct {
    1.28 +  struct jpeg_color_deconverter pub; /* public fields */
    1.29 +
    1.30 +  /* Private state for YCC->RGB conversion */
    1.31 +  int * Cr_r_tab;		/* => table for Cr to R conversion */
    1.32 +  int * Cb_b_tab;		/* => table for Cb to B conversion */
    1.33 +  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
    1.34 +  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
    1.35 +
    1.36 +  /* Private state for RGB->Y conversion */
    1.37 +  INT32 * rgb_y_tab;		/* => table for RGB to Y conversion */
    1.38 +} my_color_deconverter;
    1.39 +
    1.40 +typedef my_color_deconverter * my_cconvert_ptr;
    1.41 +
    1.42 +
    1.43 +/**************** YCbCr -> RGB conversion: most common case **************/
    1.44 +/****************   RGB -> Y   conversion: less common case **************/
    1.45 +
    1.46 +/*
    1.47 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
    1.48 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
    1.49 + * The conversion equations to be implemented are therefore
    1.50 + *
    1.51 + *	R = Y                + 1.40200 * Cr
    1.52 + *	G = Y - 0.34414 * Cb - 0.71414 * Cr
    1.53 + *	B = Y + 1.77200 * Cb
    1.54 + *
    1.55 + *	Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
    1.56 + *
    1.57 + * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
    1.58 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
    1.59 + *
    1.60 + * To avoid floating-point arithmetic, we represent the fractional constants
    1.61 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
    1.62 + * the products by 2^16, with appropriate rounding, to get the correct answer.
    1.63 + * Notice that Y, being an integral input, does not contribute any fraction
    1.64 + * so it need not participate in the rounding.
    1.65 + *
    1.66 + * For even more speed, we avoid doing any multiplications in the inner loop
    1.67 + * by precalculating the constants times Cb and Cr for all possible values.
    1.68 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
    1.69 + * for 12-bit samples it is still acceptable.  It's not very reasonable for
    1.70 + * 16-bit samples, but if you want lossless storage you shouldn't be changing
    1.71 + * colorspace anyway.
    1.72 + * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
    1.73 + * values for the G calculation are left scaled up, since we must add them
    1.74 + * together before rounding.
    1.75 + */
    1.76 +
    1.77 +#define SCALEBITS	16	/* speediest right-shift on some machines */
    1.78 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
    1.79 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
    1.80 +
    1.81 +/* We allocate one big table for RGB->Y conversion and divide it up into
    1.82 + * three parts, instead of doing three alloc_small requests.  This lets us
    1.83 + * use a single table base address, which can be held in a register in the
    1.84 + * inner loops on many machines (more than can hold all three addresses,
    1.85 + * anyway).
    1.86 + */
    1.87 +
    1.88 +#define R_Y_OFF		0			/* offset to R => Y section */
    1.89 +#define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */
    1.90 +#define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */
    1.91 +#define TABLE_SIZE	(3*(MAXJSAMPLE+1))
    1.92 +
    1.93 +
    1.94 +/* Include inline routines for colorspace extensions */
    1.95 +
    1.96 +#include "jdcolext.c"
    1.97 +#undef RGB_RED
    1.98 +#undef RGB_GREEN
    1.99 +#undef RGB_BLUE
   1.100 +#undef RGB_PIXELSIZE
   1.101 +
   1.102 +#define RGB_RED EXT_RGB_RED
   1.103 +#define RGB_GREEN EXT_RGB_GREEN
   1.104 +#define RGB_BLUE EXT_RGB_BLUE
   1.105 +#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
   1.106 +#define ycc_rgb_convert_internal ycc_extrgb_convert_internal
   1.107 +#define gray_rgb_convert_internal gray_extrgb_convert_internal
   1.108 +#define rgb_rgb_convert_internal rgb_extrgb_convert_internal
   1.109 +#include "jdcolext.c"
   1.110 +#undef RGB_RED
   1.111 +#undef RGB_GREEN
   1.112 +#undef RGB_BLUE
   1.113 +#undef RGB_PIXELSIZE
   1.114 +#undef ycc_rgb_convert_internal
   1.115 +#undef gray_rgb_convert_internal
   1.116 +#undef rgb_rgb_convert_internal
   1.117 +
   1.118 +#define RGB_RED EXT_RGBX_RED
   1.119 +#define RGB_GREEN EXT_RGBX_GREEN
   1.120 +#define RGB_BLUE EXT_RGBX_BLUE
   1.121 +#define RGB_ALPHA 3
   1.122 +#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
   1.123 +#define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
   1.124 +#define gray_rgb_convert_internal gray_extrgbx_convert_internal
   1.125 +#define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
   1.126 +#include "jdcolext.c"
   1.127 +#undef RGB_RED
   1.128 +#undef RGB_GREEN
   1.129 +#undef RGB_BLUE
   1.130 +#undef RGB_ALPHA
   1.131 +#undef RGB_PIXELSIZE
   1.132 +#undef ycc_rgb_convert_internal
   1.133 +#undef gray_rgb_convert_internal
   1.134 +#undef rgb_rgb_convert_internal
   1.135 +
   1.136 +#define RGB_RED EXT_BGR_RED
   1.137 +#define RGB_GREEN EXT_BGR_GREEN
   1.138 +#define RGB_BLUE EXT_BGR_BLUE
   1.139 +#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
   1.140 +#define ycc_rgb_convert_internal ycc_extbgr_convert_internal
   1.141 +#define gray_rgb_convert_internal gray_extbgr_convert_internal
   1.142 +#define rgb_rgb_convert_internal rgb_extbgr_convert_internal
   1.143 +#include "jdcolext.c"
   1.144 +#undef RGB_RED
   1.145 +#undef RGB_GREEN
   1.146 +#undef RGB_BLUE
   1.147 +#undef RGB_PIXELSIZE
   1.148 +#undef ycc_rgb_convert_internal
   1.149 +#undef gray_rgb_convert_internal
   1.150 +#undef rgb_rgb_convert_internal
   1.151 +
   1.152 +#define RGB_RED EXT_BGRX_RED
   1.153 +#define RGB_GREEN EXT_BGRX_GREEN
   1.154 +#define RGB_BLUE EXT_BGRX_BLUE
   1.155 +#define RGB_ALPHA 3
   1.156 +#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
   1.157 +#define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
   1.158 +#define gray_rgb_convert_internal gray_extbgrx_convert_internal
   1.159 +#define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
   1.160 +#include "jdcolext.c"
   1.161 +#undef RGB_RED
   1.162 +#undef RGB_GREEN
   1.163 +#undef RGB_BLUE
   1.164 +#undef RGB_ALPHA
   1.165 +#undef RGB_PIXELSIZE
   1.166 +#undef ycc_rgb_convert_internal
   1.167 +#undef gray_rgb_convert_internal
   1.168 +#undef rgb_rgb_convert_internal
   1.169 +
   1.170 +#define RGB_RED EXT_XBGR_RED
   1.171 +#define RGB_GREEN EXT_XBGR_GREEN
   1.172 +#define RGB_BLUE EXT_XBGR_BLUE
   1.173 +#define RGB_ALPHA 0
   1.174 +#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
   1.175 +#define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
   1.176 +#define gray_rgb_convert_internal gray_extxbgr_convert_internal
   1.177 +#define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
   1.178 +#include "jdcolext.c"
   1.179 +#undef RGB_RED
   1.180 +#undef RGB_GREEN
   1.181 +#undef RGB_BLUE
   1.182 +#undef RGB_ALPHA
   1.183 +#undef RGB_PIXELSIZE
   1.184 +#undef ycc_rgb_convert_internal
   1.185 +#undef gray_rgb_convert_internal
   1.186 +#undef rgb_rgb_convert_internal
   1.187 +
   1.188 +#define RGB_RED EXT_XRGB_RED
   1.189 +#define RGB_GREEN EXT_XRGB_GREEN
   1.190 +#define RGB_BLUE EXT_XRGB_BLUE
   1.191 +#define RGB_ALPHA 0
   1.192 +#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
   1.193 +#define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
   1.194 +#define gray_rgb_convert_internal gray_extxrgb_convert_internal
   1.195 +#define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
   1.196 +#include "jdcolext.c"
   1.197 +#undef RGB_RED
   1.198 +#undef RGB_GREEN
   1.199 +#undef RGB_BLUE
   1.200 +#undef RGB_ALPHA
   1.201 +#undef RGB_PIXELSIZE
   1.202 +#undef ycc_rgb_convert_internal
   1.203 +#undef gray_rgb_convert_internal
   1.204 +#undef rgb_rgb_convert_internal
   1.205 +
   1.206 +
   1.207 +/*
   1.208 + * Initialize tables for YCC->RGB colorspace conversion.
   1.209 + */
   1.210 +
   1.211 +LOCAL(void)
   1.212 +build_ycc_rgb_table (j_decompress_ptr cinfo)
   1.213 +{
   1.214 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   1.215 +  int i;
   1.216 +  INT32 x;
   1.217 +  SHIFT_TEMPS
   1.218 +
   1.219 +  cconvert->Cr_r_tab = (int *)
   1.220 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.221 +				(MAXJSAMPLE+1) * SIZEOF(int));
   1.222 +  cconvert->Cb_b_tab = (int *)
   1.223 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.224 +				(MAXJSAMPLE+1) * SIZEOF(int));
   1.225 +  cconvert->Cr_g_tab = (INT32 *)
   1.226 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.227 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
   1.228 +  cconvert->Cb_g_tab = (INT32 *)
   1.229 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.230 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
   1.231 +
   1.232 +  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
   1.233 +    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
   1.234 +    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
   1.235 +    /* Cr=>R value is nearest int to 1.40200 * x */
   1.236 +    cconvert->Cr_r_tab[i] = (int)
   1.237 +		    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
   1.238 +    /* Cb=>B value is nearest int to 1.77200 * x */
   1.239 +    cconvert->Cb_b_tab[i] = (int)
   1.240 +		    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
   1.241 +    /* Cr=>G value is scaled-up -0.71414 * x */
   1.242 +    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
   1.243 +    /* Cb=>G value is scaled-up -0.34414 * x */
   1.244 +    /* We also add in ONE_HALF so that need not do it in inner loop */
   1.245 +    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
   1.246 +  }
   1.247 +}
   1.248 +
   1.249 +
   1.250 +/*
   1.251 + * Convert some rows of samples to the output colorspace.
   1.252 + */
   1.253 +
   1.254 +METHODDEF(void)
   1.255 +ycc_rgb_convert (j_decompress_ptr cinfo,
   1.256 +		 JSAMPIMAGE input_buf, JDIMENSION input_row,
   1.257 +		 JSAMPARRAY output_buf, int num_rows)
   1.258 +{
   1.259 +  switch (cinfo->out_color_space) {
   1.260 +    case JCS_EXT_RGB:
   1.261 +      ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.262 +                                  num_rows);
   1.263 +      break;
   1.264 +    case JCS_EXT_RGBX:
   1.265 +    case JCS_EXT_RGBA:
   1.266 +      ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.267 +                                   num_rows);
   1.268 +      break;
   1.269 +    case JCS_EXT_BGR:
   1.270 +      ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.271 +                                  num_rows);
   1.272 +      break;
   1.273 +    case JCS_EXT_BGRX:
   1.274 +    case JCS_EXT_BGRA:
   1.275 +      ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.276 +                                   num_rows);
   1.277 +      break;
   1.278 +    case JCS_EXT_XBGR:
   1.279 +    case JCS_EXT_ABGR:
   1.280 +      ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.281 +                                   num_rows);
   1.282 +      break;
   1.283 +    case JCS_EXT_XRGB:
   1.284 +    case JCS_EXT_ARGB:
   1.285 +      ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.286 +                                   num_rows);
   1.287 +      break;
   1.288 +    default:
   1.289 +      ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.290 +                               num_rows);
   1.291 +      break;
   1.292 +  }
   1.293 +}
   1.294 +
   1.295 +
   1.296 +/**************** Cases other than YCbCr -> RGB **************/
   1.297 +
   1.298 +
   1.299 +/*
   1.300 + * Initialize for RGB->grayscale colorspace conversion.
   1.301 + */
   1.302 +
   1.303 +LOCAL(void)
   1.304 +build_rgb_y_table (j_decompress_ptr cinfo)
   1.305 +{
   1.306 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   1.307 +  INT32 * rgb_y_tab;
   1.308 +  INT32 i;
   1.309 +
   1.310 +  /* Allocate and fill in the conversion tables. */
   1.311 +  cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
   1.312 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.313 +				(TABLE_SIZE * SIZEOF(INT32)));
   1.314 +
   1.315 +  for (i = 0; i <= MAXJSAMPLE; i++) {
   1.316 +    rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
   1.317 +    rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
   1.318 +    rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
   1.319 +  }
   1.320 +}
   1.321 +
   1.322 +
   1.323 +/*
   1.324 + * Convert RGB to grayscale.
   1.325 + */
   1.326 +
   1.327 +METHODDEF(void)
   1.328 +rgb_gray_convert (j_decompress_ptr cinfo,
   1.329 +		  JSAMPIMAGE input_buf, JDIMENSION input_row,
   1.330 +		  JSAMPARRAY output_buf, int num_rows)
   1.331 +{
   1.332 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   1.333 +  register int r, g, b;
   1.334 +  register INT32 * ctab = cconvert->rgb_y_tab;
   1.335 +  register JSAMPROW outptr;
   1.336 +  register JSAMPROW inptr0, inptr1, inptr2;
   1.337 +  register JDIMENSION col;
   1.338 +  JDIMENSION num_cols = cinfo->output_width;
   1.339 +
   1.340 +  while (--num_rows >= 0) {
   1.341 +    inptr0 = input_buf[0][input_row];
   1.342 +    inptr1 = input_buf[1][input_row];
   1.343 +    inptr2 = input_buf[2][input_row];
   1.344 +    input_row++;
   1.345 +    outptr = *output_buf++;
   1.346 +    for (col = 0; col < num_cols; col++) {
   1.347 +      r = GETJSAMPLE(inptr0[col]);
   1.348 +      g = GETJSAMPLE(inptr1[col]);
   1.349 +      b = GETJSAMPLE(inptr2[col]);
   1.350 +      /* Y */
   1.351 +      outptr[col] = (JSAMPLE)
   1.352 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
   1.353 +		 >> SCALEBITS);
   1.354 +    }
   1.355 +  }
   1.356 +}
   1.357 +
   1.358 +
   1.359 +/*
   1.360 + * Color conversion for no colorspace change: just copy the data,
   1.361 + * converting from separate-planes to interleaved representation.
   1.362 + */
   1.363 +
   1.364 +METHODDEF(void)
   1.365 +null_convert (j_decompress_ptr cinfo,
   1.366 +	      JSAMPIMAGE input_buf, JDIMENSION input_row,
   1.367 +	      JSAMPARRAY output_buf, int num_rows)
   1.368 +{
   1.369 +  register JSAMPROW inptr, outptr;
   1.370 +  register JDIMENSION count;
   1.371 +  register int num_components = cinfo->num_components;
   1.372 +  JDIMENSION num_cols = cinfo->output_width;
   1.373 +  int ci;
   1.374 +
   1.375 +  while (--num_rows >= 0) {
   1.376 +    for (ci = 0; ci < num_components; ci++) {
   1.377 +      inptr = input_buf[ci][input_row];
   1.378 +      outptr = output_buf[0] + ci;
   1.379 +      for (count = num_cols; count > 0; count--) {
   1.380 +	*outptr = *inptr++;	/* needn't bother with GETJSAMPLE() here */
   1.381 +	outptr += num_components;
   1.382 +      }
   1.383 +    }
   1.384 +    input_row++;
   1.385 +    output_buf++;
   1.386 +  }
   1.387 +}
   1.388 +
   1.389 +
   1.390 +/*
   1.391 + * Color conversion for grayscale: just copy the data.
   1.392 + * This also works for YCbCr -> grayscale conversion, in which
   1.393 + * we just copy the Y (luminance) component and ignore chrominance.
   1.394 + */
   1.395 +
   1.396 +METHODDEF(void)
   1.397 +grayscale_convert (j_decompress_ptr cinfo,
   1.398 +		   JSAMPIMAGE input_buf, JDIMENSION input_row,
   1.399 +		   JSAMPARRAY output_buf, int num_rows)
   1.400 +{
   1.401 +  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
   1.402 +		    num_rows, cinfo->output_width);
   1.403 +}
   1.404 +
   1.405 +
   1.406 +/*
   1.407 + * Convert grayscale to RGB
   1.408 + */
   1.409 +
   1.410 +METHODDEF(void)
   1.411 +gray_rgb_convert (j_decompress_ptr cinfo,
   1.412 +		  JSAMPIMAGE input_buf, JDIMENSION input_row,
   1.413 +		  JSAMPARRAY output_buf, int num_rows)
   1.414 +{
   1.415 +  switch (cinfo->out_color_space) {
   1.416 +    case JCS_EXT_RGB:
   1.417 +      gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.418 +                                   num_rows);
   1.419 +      break;
   1.420 +    case JCS_EXT_RGBX:
   1.421 +    case JCS_EXT_RGBA:
   1.422 +      gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.423 +                                    num_rows);
   1.424 +      break;
   1.425 +    case JCS_EXT_BGR:
   1.426 +      gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.427 +                                   num_rows);
   1.428 +      break;
   1.429 +    case JCS_EXT_BGRX:
   1.430 +    case JCS_EXT_BGRA:
   1.431 +      gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.432 +                                    num_rows);
   1.433 +      break;
   1.434 +    case JCS_EXT_XBGR:
   1.435 +    case JCS_EXT_ABGR:
   1.436 +      gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.437 +                                    num_rows);
   1.438 +      break;
   1.439 +    case JCS_EXT_XRGB:
   1.440 +    case JCS_EXT_ARGB:
   1.441 +      gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.442 +                                    num_rows);
   1.443 +      break;
   1.444 +    default:
   1.445 +      gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.446 +                                num_rows);
   1.447 +      break;
   1.448 +  }
   1.449 +}
   1.450 +
   1.451 +
   1.452 +/*
   1.453 + * Convert plain RGB to extended RGB
   1.454 + */
   1.455 +
   1.456 +METHODDEF(void)
   1.457 +rgb_rgb_convert (j_decompress_ptr cinfo,
   1.458 +		  JSAMPIMAGE input_buf, JDIMENSION input_row,
   1.459 +		  JSAMPARRAY output_buf, int num_rows)
   1.460 +{
   1.461 +  switch (cinfo->out_color_space) {
   1.462 +    case JCS_EXT_RGB:
   1.463 +      rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.464 +                                  num_rows);
   1.465 +      break;
   1.466 +    case JCS_EXT_RGBX:
   1.467 +    case JCS_EXT_RGBA:
   1.468 +      rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.469 +                                   num_rows);
   1.470 +      break;
   1.471 +    case JCS_EXT_BGR:
   1.472 +      rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.473 +                                  num_rows);
   1.474 +      break;
   1.475 +    case JCS_EXT_BGRX:
   1.476 +    case JCS_EXT_BGRA:
   1.477 +      rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.478 +                                   num_rows);
   1.479 +      break;
   1.480 +    case JCS_EXT_XBGR:
   1.481 +    case JCS_EXT_ABGR:
   1.482 +      rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.483 +                                   num_rows);
   1.484 +      break;
   1.485 +    case JCS_EXT_XRGB:
   1.486 +    case JCS_EXT_ARGB:
   1.487 +      rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.488 +                                   num_rows);
   1.489 +      break;
   1.490 +    default:
   1.491 +      rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
   1.492 +                               num_rows);
   1.493 +      break;
   1.494 +  }
   1.495 +}
   1.496 +
   1.497 +
   1.498 +/*
   1.499 + * Adobe-style YCCK->CMYK conversion.
   1.500 + * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
   1.501 + * conversion as above, while passing K (black) unchanged.
   1.502 + * We assume build_ycc_rgb_table has been called.
   1.503 + */
   1.504 +
   1.505 +METHODDEF(void)
   1.506 +ycck_cmyk_convert (j_decompress_ptr cinfo,
   1.507 +		   JSAMPIMAGE input_buf, JDIMENSION input_row,
   1.508 +		   JSAMPARRAY output_buf, int num_rows)
   1.509 +{
   1.510 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   1.511 +  register int y, cb, cr;
   1.512 +  register JSAMPROW outptr;
   1.513 +  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
   1.514 +  register JDIMENSION col;
   1.515 +  JDIMENSION num_cols = cinfo->output_width;
   1.516 +  /* copy these pointers into registers if possible */
   1.517 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
   1.518 +  register int * Crrtab = cconvert->Cr_r_tab;
   1.519 +  register int * Cbbtab = cconvert->Cb_b_tab;
   1.520 +  register INT32 * Crgtab = cconvert->Cr_g_tab;
   1.521 +  register INT32 * Cbgtab = cconvert->Cb_g_tab;
   1.522 +  SHIFT_TEMPS
   1.523 +
   1.524 +  while (--num_rows >= 0) {
   1.525 +    inptr0 = input_buf[0][input_row];
   1.526 +    inptr1 = input_buf[1][input_row];
   1.527 +    inptr2 = input_buf[2][input_row];
   1.528 +    inptr3 = input_buf[3][input_row];
   1.529 +    input_row++;
   1.530 +    outptr = *output_buf++;
   1.531 +    for (col = 0; col < num_cols; col++) {
   1.532 +      y  = GETJSAMPLE(inptr0[col]);
   1.533 +      cb = GETJSAMPLE(inptr1[col]);
   1.534 +      cr = GETJSAMPLE(inptr2[col]);
   1.535 +      /* Range-limiting is essential due to noise introduced by DCT losses. */
   1.536 +      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];	/* red */
   1.537 +      outptr[1] = range_limit[MAXJSAMPLE - (y +			/* green */
   1.538 +			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
   1.539 +						 SCALEBITS)))];
   1.540 +      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];	/* blue */
   1.541 +      /* K passes through unchanged */
   1.542 +      outptr[3] = inptr3[col];	/* don't need GETJSAMPLE here */
   1.543 +      outptr += 4;
   1.544 +    }
   1.545 +  }
   1.546 +}
   1.547 +
   1.548 +
   1.549 +/*
   1.550 + * Empty method for start_pass.
   1.551 + */
   1.552 +
   1.553 +METHODDEF(void)
   1.554 +start_pass_dcolor (j_decompress_ptr cinfo)
   1.555 +{
   1.556 +  /* no work needed */
   1.557 +}
   1.558 +
   1.559 +
   1.560 +/*
   1.561 + * Module initialization routine for output colorspace conversion.
   1.562 + */
   1.563 +
   1.564 +GLOBAL(void)
   1.565 +jinit_color_deconverter (j_decompress_ptr cinfo)
   1.566 +{
   1.567 +  my_cconvert_ptr cconvert;
   1.568 +  int ci;
   1.569 +
   1.570 +  cconvert = (my_cconvert_ptr)
   1.571 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.572 +				SIZEOF(my_color_deconverter));
   1.573 +  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
   1.574 +  cconvert->pub.start_pass = start_pass_dcolor;
   1.575 +
   1.576 +  /* Make sure num_components agrees with jpeg_color_space */
   1.577 +  switch (cinfo->jpeg_color_space) {
   1.578 +  case JCS_GRAYSCALE:
   1.579 +    if (cinfo->num_components != 1)
   1.580 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
   1.581 +    break;
   1.582 +
   1.583 +  case JCS_RGB:
   1.584 +  case JCS_YCbCr:
   1.585 +    if (cinfo->num_components != 3)
   1.586 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
   1.587 +    break;
   1.588 +
   1.589 +  case JCS_CMYK:
   1.590 +  case JCS_YCCK:
   1.591 +    if (cinfo->num_components != 4)
   1.592 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
   1.593 +    break;
   1.594 +
   1.595 +  default:			/* JCS_UNKNOWN can be anything */
   1.596 +    if (cinfo->num_components < 1)
   1.597 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
   1.598 +    break;
   1.599 +  }
   1.600 +
   1.601 +  /* Set out_color_components and conversion method based on requested space.
   1.602 +   * Also clear the component_needed flags for any unused components,
   1.603 +   * so that earlier pipeline stages can avoid useless computation.
   1.604 +   */
   1.605 +
   1.606 +  switch (cinfo->out_color_space) {
   1.607 +  case JCS_GRAYSCALE:
   1.608 +    cinfo->out_color_components = 1;
   1.609 +    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
   1.610 +	cinfo->jpeg_color_space == JCS_YCbCr) {
   1.611 +      cconvert->pub.color_convert = grayscale_convert;
   1.612 +      /* For color->grayscale conversion, only the Y (0) component is needed */
   1.613 +      for (ci = 1; ci < cinfo->num_components; ci++)
   1.614 +	cinfo->comp_info[ci].component_needed = FALSE;
   1.615 +    } else if (cinfo->jpeg_color_space == JCS_RGB) {
   1.616 +      cconvert->pub.color_convert = rgb_gray_convert;
   1.617 +      build_rgb_y_table(cinfo);
   1.618 +    } else
   1.619 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
   1.620 +    break;
   1.621 +
   1.622 +  case JCS_RGB:
   1.623 +  case JCS_EXT_RGB:
   1.624 +  case JCS_EXT_RGBX:
   1.625 +  case JCS_EXT_BGR:
   1.626 +  case JCS_EXT_BGRX:
   1.627 +  case JCS_EXT_XBGR:
   1.628 +  case JCS_EXT_XRGB:
   1.629 +  case JCS_EXT_RGBA:
   1.630 +  case JCS_EXT_BGRA:
   1.631 +  case JCS_EXT_ABGR:
   1.632 +  case JCS_EXT_ARGB:
   1.633 +    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
   1.634 +    if (cinfo->jpeg_color_space == JCS_YCbCr) {
   1.635 +      if (jsimd_can_ycc_rgb())
   1.636 +        cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
   1.637 +      else {
   1.638 +        cconvert->pub.color_convert = ycc_rgb_convert;
   1.639 +        build_ycc_rgb_table(cinfo);
   1.640 +      }
   1.641 +    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
   1.642 +      cconvert->pub.color_convert = gray_rgb_convert;
   1.643 +    } else if (cinfo->jpeg_color_space == JCS_RGB) {
   1.644 +      if (rgb_red[cinfo->out_color_space] == 0 &&
   1.645 +          rgb_green[cinfo->out_color_space] == 1 &&
   1.646 +          rgb_blue[cinfo->out_color_space] == 2 &&
   1.647 +          rgb_pixelsize[cinfo->out_color_space] == 3)
   1.648 +        cconvert->pub.color_convert = null_convert;
   1.649 +      else
   1.650 +        cconvert->pub.color_convert = rgb_rgb_convert;
   1.651 +    } else
   1.652 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
   1.653 +    break;
   1.654 +
   1.655 +  case JCS_CMYK:
   1.656 +    cinfo->out_color_components = 4;
   1.657 +    if (cinfo->jpeg_color_space == JCS_YCCK) {
   1.658 +      cconvert->pub.color_convert = ycck_cmyk_convert;
   1.659 +      build_ycc_rgb_table(cinfo);
   1.660 +    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
   1.661 +      cconvert->pub.color_convert = null_convert;
   1.662 +    } else
   1.663 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
   1.664 +    break;
   1.665 +
   1.666 +  default:
   1.667 +    /* Permit null conversion to same output space */
   1.668 +    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
   1.669 +      cinfo->out_color_components = cinfo->num_components;
   1.670 +      cconvert->pub.color_convert = null_convert;
   1.671 +    } else			/* unsupported non-null conversion */
   1.672 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
   1.673 +    break;
   1.674 +  }
   1.675 +
   1.676 +  if (cinfo->quantize_colors)
   1.677 +    cinfo->output_components = 1; /* single colormapped output component */
   1.678 +  else
   1.679 +    cinfo->output_components = cinfo->out_color_components;
   1.680 +}

mercurial