kusano 7d535a
/*
kusano 7d535a
 * jdcolor.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1997, Thomas G. Lane.
kusano 7d535a
 * Modified 2011-2012 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains output colorspace conversion routines.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private subobject */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_color_deconverter pub; /* public fields */
kusano 7d535a
kusano 7d535a
  /* Private state for YCC->RGB conversion */
kusano 7d535a
  int * Cr_r_tab;		/* => table for Cr to R conversion */
kusano 7d535a
  int * Cb_b_tab;		/* => table for Cb to B conversion */
kusano 7d535a
  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
kusano 7d535a
  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
kusano 7d535a
kusano 7d535a
  /* Private state for RGB->Y conversion */
kusano 7d535a
  INT32 * rgb_y_tab;		/* => table for RGB to Y conversion */
kusano 7d535a
} my_color_deconverter;
kusano 7d535a
kusano 7d535a
typedef my_color_deconverter * my_cconvert_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************** YCbCr -> RGB conversion: most common case **************/
kusano 7d535a
/****************   RGB -> Y   conversion: less common case **************/
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
kusano 7d535a
 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
kusano 7d535a
 * The conversion equations to be implemented are therefore
kusano 7d535a
 *
kusano 7d535a
 *	R = Y                + 1.40200 * Cr
kusano 7d535a
 *	G = Y - 0.34414 * Cb - 0.71414 * Cr
kusano 7d535a
 *	B = Y + 1.77200 * Cb
kusano 7d535a
 *
kusano 7d535a
 *	Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
kusano 7d535a
 *
kusano 7d535a
 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
kusano 7d535a
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
kusano 7d535a
 *
kusano 7d535a
 * To avoid floating-point arithmetic, we represent the fractional constants
kusano 7d535a
 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
kusano 7d535a
 * the products by 2^16, with appropriate rounding, to get the correct answer.
kusano 7d535a
 * Notice that Y, being an integral input, does not contribute any fraction
kusano 7d535a
 * so it need not participate in the rounding.
kusano 7d535a
 *
kusano 7d535a
 * For even more speed, we avoid doing any multiplications in the inner loop
kusano 7d535a
 * by precalculating the constants times Cb and Cr for all possible values.
kusano 7d535a
 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
kusano 7d535a
 * for 12-bit samples it is still acceptable.  It's not very reasonable for
kusano 7d535a
 * 16-bit samples, but if you want lossless storage you shouldn't be changing
kusano 7d535a
 * colorspace anyway.
kusano 7d535a
 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
kusano 7d535a
 * values for the G calculation are left scaled up, since we must add them
kusano 7d535a
 * together before rounding.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define SCALEBITS	16	/* speediest right-shift on some machines */
kusano 7d535a
#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
kusano 7d535a
#define FIX(x)		((INT32) ((x) * (1L<
kusano 7d535a
kusano 7d535a
/* We allocate one big table for RGB->Y conversion and divide it up into
kusano 7d535a
 * three parts, instead of doing three alloc_small requests.  This lets us
kusano 7d535a
 * use a single table base address, which can be held in a register in the
kusano 7d535a
 * inner loops on many machines (more than can hold all three addresses,
kusano 7d535a
 * anyway).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define R_Y_OFF		0			/* offset to R => Y section */
kusano 7d535a
#define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */
kusano 7d535a
#define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */
kusano 7d535a
#define TABLE_SIZE	(3*(MAXJSAMPLE+1))
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize tables for YCC->RGB colorspace conversion.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
build_ycc_rgb_table (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  int i;
kusano 7d535a
  INT32 x;
kusano 7d535a
  SHIFT_TEMPS
kusano 7d535a
kusano 7d535a
  cconvert->Cr_r_tab = (int *)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				(MAXJSAMPLE+1) * SIZEOF(int));
kusano 7d535a
  cconvert->Cb_b_tab = (int *)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				(MAXJSAMPLE+1) * SIZEOF(int));
kusano 7d535a
  cconvert->Cr_g_tab = (INT32 *)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				(MAXJSAMPLE+1) * SIZEOF(INT32));
kusano 7d535a
  cconvert->Cb_g_tab = (INT32 *)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				(MAXJSAMPLE+1) * SIZEOF(INT32));
kusano 7d535a
kusano 7d535a
  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
kusano 7d535a
    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
kusano 7d535a
    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
kusano 7d535a
    /* Cr=>R value is nearest int to 1.40200 * x */
kusano 7d535a
    cconvert->Cr_r_tab[i] = (int)
kusano 7d535a
		    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
kusano 7d535a
    /* Cb=>B value is nearest int to 1.77200 * x */
kusano 7d535a
    cconvert->Cb_b_tab[i] = (int)
kusano 7d535a
		    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
kusano 7d535a
    /* Cr=>G value is scaled-up -0.71414 * x */
kusano 7d535a
    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
kusano 7d535a
    /* Cb=>G value is scaled-up -0.34414 * x */
kusano 7d535a
    /* We also add in ONE_HALF so that need not do it in inner loop */
kusano 7d535a
    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the output colorspace.
kusano 7d535a
 *
kusano 7d535a
 * Note that we change from noninterleaved, one-plane-per-component format
kusano 7d535a
 * to interleaved-pixel format.  The output buffer is therefore three times
kusano 7d535a
 * as wide as the input buffer.
kusano 7d535a
 * A starting row offset is provided only for the input buffer.  The caller
kusano 7d535a
 * can easily adjust the passed output_buf value to accommodate any row
kusano 7d535a
 * offset required on that side.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
ycc_rgb_convert (j_decompress_ptr cinfo,
kusano 7d535a
		 JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
		 JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  register int y, cb, cr;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr0, inptr1, inptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
  /* copy these pointers into registers if possible */
kusano 7d535a
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
kusano 7d535a
  register int * Crrtab = cconvert->Cr_r_tab;
kusano 7d535a
  register int * Cbbtab = cconvert->Cb_b_tab;
kusano 7d535a
  register INT32 * Crgtab = cconvert->Cr_g_tab;
kusano 7d535a
  register INT32 * Cbgtab = cconvert->Cb_g_tab;
kusano 7d535a
  SHIFT_TEMPS
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr0 = input_buf[0][input_row];
kusano 7d535a
    inptr1 = input_buf[1][input_row];
kusano 7d535a
    inptr2 = input_buf[2][input_row];
kusano 7d535a
    input_row++;
kusano 7d535a
    outptr = *output_buf++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      y  = GETJSAMPLE(inptr0[col]);
kusano 7d535a
      cb = GETJSAMPLE(inptr1[col]);
kusano 7d535a
      cr = GETJSAMPLE(inptr2[col]);
kusano 7d535a
      /* Range-limiting is essential due to noise introduced by DCT losses. */
kusano 7d535a
      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
kusano 7d535a
      outptr[RGB_GREEN] = range_limit[y +
kusano 7d535a
			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
kusano 7d535a
						 SCALEBITS))];
kusano 7d535a
      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
kusano 7d535a
      outptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************** Cases other than YCbCr -> RGB **************/
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for RGB->grayscale colorspace conversion.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
build_rgb_y_table (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  INT32 * rgb_y_tab;
kusano 7d535a
  INT32 i;
kusano 7d535a
kusano 7d535a
  /* Allocate and fill in the conversion tables. */
kusano 7d535a
  cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				(TABLE_SIZE * SIZEOF(INT32)));
kusano 7d535a
kusano 7d535a
  for (i = 0; i <= MAXJSAMPLE; i++) {
kusano 7d535a
    rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
kusano 7d535a
    rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
kusano 7d535a
    rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert RGB to grayscale.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb_gray_convert (j_decompress_ptr cinfo,
kusano 7d535a
		  JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
		  JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  register INT32 * ctab = cconvert->rgb_y_tab;
kusano 7d535a
  register int r, g, b;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr0, inptr1, inptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr0 = input_buf[0][input_row];
kusano 7d535a
    inptr1 = input_buf[1][input_row];
kusano 7d535a
    inptr2 = input_buf[2][input_row];
kusano 7d535a
    input_row++;
kusano 7d535a
    outptr = *output_buf++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      r = GETJSAMPLE(inptr0[col]);
kusano 7d535a
      g = GETJSAMPLE(inptr1[col]);
kusano 7d535a
      b = GETJSAMPLE(inptr2[col]);
kusano 7d535a
      /* Y */
kusano 7d535a
      outptr[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
kusano 7d535a
 * (inverse color transform).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb1_rgb_convert (j_decompress_ptr cinfo,
kusano 7d535a
		  JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
		  JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  register int r, g, b;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr0, inptr1, inptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr0 = input_buf[0][input_row];
kusano 7d535a
    inptr1 = input_buf[1][input_row];
kusano 7d535a
    inptr2 = input_buf[2][input_row];
kusano 7d535a
    input_row++;
kusano 7d535a
    outptr = *output_buf++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      r = GETJSAMPLE(inptr0[col]);
kusano 7d535a
      g = GETJSAMPLE(inptr1[col]);
kusano 7d535a
      b = GETJSAMPLE(inptr2[col]);
kusano 7d535a
      /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
kusano 7d535a
       * (modulo) operator is equivalent to the bitmask operator AND.
kusano 7d535a
       */
kusano 7d535a
      outptr[RGB_RED]   = (JSAMPLE) ((r + g - CENTERJSAMPLE) & MAXJSAMPLE);
kusano 7d535a
      outptr[RGB_GREEN] = (JSAMPLE) g;
kusano 7d535a
      outptr[RGB_BLUE]  = (JSAMPLE) ((b + g - CENTERJSAMPLE) & MAXJSAMPLE);
kusano 7d535a
      outptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * [R-G,G,B-G] to grayscale conversion with modulo calculation
kusano 7d535a
 * (inverse color transform).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb1_gray_convert (j_decompress_ptr cinfo,
kusano 7d535a
		   JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
		   JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  register INT32 * ctab = cconvert->rgb_y_tab;
kusano 7d535a
  register int r, g, b;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr0, inptr1, inptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr0 = input_buf[0][input_row];
kusano 7d535a
    inptr1 = input_buf[1][input_row];
kusano 7d535a
    inptr2 = input_buf[2][input_row];
kusano 7d535a
    input_row++;
kusano 7d535a
    outptr = *output_buf++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      r = GETJSAMPLE(inptr0[col]);
kusano 7d535a
      g = GETJSAMPLE(inptr1[col]);
kusano 7d535a
      b = GETJSAMPLE(inptr2[col]);
kusano 7d535a
      /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
kusano 7d535a
       * (modulo) operator is equivalent to the bitmask operator AND.
kusano 7d535a
       */
kusano 7d535a
      r = (r + g - CENTERJSAMPLE) & MAXJSAMPLE;
kusano 7d535a
      b = (b + g - CENTERJSAMPLE) & MAXJSAMPLE;
kusano 7d535a
      /* Y */
kusano 7d535a
      outptr[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * No colorspace change, but conversion from separate-planes
kusano 7d535a
 * to interleaved representation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb_convert (j_decompress_ptr cinfo,
kusano 7d535a
	     JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
	     JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr0, inptr1, inptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr0 = input_buf[0][input_row];
kusano 7d535a
    inptr1 = input_buf[1][input_row];
kusano 7d535a
    inptr2 = input_buf[2][input_row];
kusano 7d535a
    input_row++;
kusano 7d535a
    outptr = *output_buf++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      /* We can dispense with GETJSAMPLE() here */
kusano 7d535a
      outptr[RGB_RED]   = inptr0[col];
kusano 7d535a
      outptr[RGB_GREEN] = inptr1[col];
kusano 7d535a
      outptr[RGB_BLUE]  = inptr2[col];
kusano 7d535a
      outptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Color conversion for no colorspace change: just copy the data,
kusano 7d535a
 * converting from separate-planes to interleaved representation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
null_convert (j_decompress_ptr cinfo,
kusano 7d535a
	      JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
	      JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  int ci;
kusano 7d535a
  register int nc = cinfo->num_components;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    for (ci = 0; ci < nc; ci++) {
kusano 7d535a
      inptr = input_buf[ci][input_row];
kusano 7d535a
      outptr = output_buf[0] + ci;
kusano 7d535a
      for (col = 0; col < num_cols; col++) {
kusano 7d535a
	*outptr = *inptr++;	/* needn't bother with GETJSAMPLE() here */
kusano 7d535a
	outptr += nc;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    input_row++;
kusano 7d535a
    output_buf++;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Color conversion for grayscale: just copy the data.
kusano 7d535a
 * This also works for YCbCr -> grayscale conversion, in which
kusano 7d535a
 * we just copy the Y (luminance) component and ignore chrominance.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
grayscale_convert (j_decompress_ptr cinfo,
kusano 7d535a
		   JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
		   JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
kusano 7d535a
		    num_rows, cinfo->output_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert grayscale to RGB: just duplicate the graylevel three times.
kusano 7d535a
 * This is provided to support applications that don't want to cope
kusano 7d535a
 * with grayscale as a separate case.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
gray_rgb_convert (j_decompress_ptr cinfo,
kusano 7d535a
		  JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
		  JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr = input_buf[0][input_row++];
kusano 7d535a
    outptr = *output_buf++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      /* We can dispense with GETJSAMPLE() here */
kusano 7d535a
      outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
kusano 7d535a
      outptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Adobe-style YCCK->CMYK conversion.
kusano 7d535a
 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
kusano 7d535a
 * conversion as above, while passing K (black) unchanged.
kusano 7d535a
 * We assume build_ycc_rgb_table has been called.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
ycck_cmyk_convert (j_decompress_ptr cinfo,
kusano 7d535a
		   JSAMPIMAGE input_buf, JDIMENSION input_row,
kusano 7d535a
		   JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  register int y, cb, cr;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->output_width;
kusano 7d535a
  /* copy these pointers into registers if possible */
kusano 7d535a
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
kusano 7d535a
  register int * Crrtab = cconvert->Cr_r_tab;
kusano 7d535a
  register int * Cbbtab = cconvert->Cb_b_tab;
kusano 7d535a
  register INT32 * Crgtab = cconvert->Cr_g_tab;
kusano 7d535a
  register INT32 * Cbgtab = cconvert->Cb_g_tab;
kusano 7d535a
  SHIFT_TEMPS
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr0 = input_buf[0][input_row];
kusano 7d535a
    inptr1 = input_buf[1][input_row];
kusano 7d535a
    inptr2 = input_buf[2][input_row];
kusano 7d535a
    inptr3 = input_buf[3][input_row];
kusano 7d535a
    input_row++;
kusano 7d535a
    outptr = *output_buf++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      y  = GETJSAMPLE(inptr0[col]);
kusano 7d535a
      cb = GETJSAMPLE(inptr1[col]);
kusano 7d535a
      cr = GETJSAMPLE(inptr2[col]);
kusano 7d535a
      /* Range-limiting is essential due to noise introduced by DCT losses. */
kusano 7d535a
      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];	/* red */
kusano 7d535a
      outptr[1] = range_limit[MAXJSAMPLE - (y +			/* green */
kusano 7d535a
			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
kusano 7d535a
						 SCALEBITS)))];
kusano 7d535a
      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];	/* blue */
kusano 7d535a
      /* K passes through unchanged */
kusano 7d535a
      outptr[3] = inptr3[col];	/* don't need GETJSAMPLE here */
kusano 7d535a
      outptr += 4;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Empty method for start_pass.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_pass_dcolor (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work needed */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Module initialization routine for output colorspace conversion.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_color_deconverter (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert;
kusano 7d535a
  int ci;
kusano 7d535a
kusano 7d535a
  cconvert = (my_cconvert_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_color_deconverter));
kusano 7d535a
  cinfo->cconvert = &cconvert->pub;
kusano 7d535a
  cconvert->pub.start_pass = start_pass_dcolor;
kusano 7d535a
kusano 7d535a
  /* Make sure num_components agrees with jpeg_color_space */
kusano 7d535a
  switch (cinfo->jpeg_color_space) {
kusano 7d535a
  case JCS_GRAYSCALE:
kusano 7d535a
    if (cinfo->num_components != 1)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_RGB:
kusano 7d535a
  case JCS_YCbCr:
kusano 7d535a
    if (cinfo->num_components != 3)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_CMYK:
kusano 7d535a
  case JCS_YCCK:
kusano 7d535a
    if (cinfo->num_components != 4)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  default:			/* JCS_UNKNOWN can be anything */
kusano 7d535a
    if (cinfo->num_components < 1)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Support color transform only for RGB colorspace */
kusano 7d535a
  if (cinfo->color_transform && cinfo->jpeg_color_space != JCS_RGB)
kusano 7d535a
    ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
kusano 7d535a
  /* Set out_color_components and conversion method based on requested space.
kusano 7d535a
   * Also clear the component_needed flags for any unused components,
kusano 7d535a
   * so that earlier pipeline stages can avoid useless computation.
kusano 7d535a
   */
kusano 7d535a
kusano 7d535a
  switch (cinfo->out_color_space) {
kusano 7d535a
  case JCS_GRAYSCALE:
kusano 7d535a
    cinfo->out_color_components = 1;
kusano 7d535a
    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
kusano 7d535a
	cinfo->jpeg_color_space == JCS_YCbCr) {
kusano 7d535a
      cconvert->pub.color_convert = grayscale_convert;
kusano 7d535a
      /* For color->grayscale conversion, only the Y (0) component is needed */
kusano 7d535a
      for (ci = 1; ci < cinfo->num_components; ci++)
kusano 7d535a
	cinfo->comp_info[ci].component_needed = FALSE;
kusano 7d535a
    } else if (cinfo->jpeg_color_space == JCS_RGB) {
kusano 7d535a
      switch (cinfo->color_transform) {
kusano 7d535a
      case JCT_NONE:
kusano 7d535a
	cconvert->pub.color_convert = rgb_gray_convert;
kusano 7d535a
	break;
kusano 7d535a
      case JCT_SUBTRACT_GREEN:
kusano 7d535a
	cconvert->pub.color_convert = rgb1_gray_convert;
kusano 7d535a
	break;
kusano 7d535a
      default:
kusano 7d535a
	ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
      build_rgb_y_table(cinfo);
kusano 7d535a
    } else
kusano 7d535a
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_RGB:
kusano 7d535a
    cinfo->out_color_components = RGB_PIXELSIZE;
kusano 7d535a
    if (cinfo->jpeg_color_space == JCS_YCbCr) {
kusano 7d535a
      cconvert->pub.color_convert = ycc_rgb_convert;
kusano 7d535a
      build_ycc_rgb_table(cinfo);
kusano 7d535a
    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
kusano 7d535a
      cconvert->pub.color_convert = gray_rgb_convert;
kusano 7d535a
    } else if (cinfo->jpeg_color_space == JCS_RGB) {
kusano 7d535a
      switch (cinfo->color_transform) {
kusano 7d535a
      case JCT_NONE:
kusano 7d535a
	cconvert->pub.color_convert = rgb_convert;
kusano 7d535a
	break;
kusano 7d535a
      case JCT_SUBTRACT_GREEN:
kusano 7d535a
	cconvert->pub.color_convert = rgb1_rgb_convert;
kusano 7d535a
	break;
kusano 7d535a
      default:
kusano 7d535a
	ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
    } else
kusano 7d535a
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_CMYK:
kusano 7d535a
    cinfo->out_color_components = 4;
kusano 7d535a
    if (cinfo->jpeg_color_space == JCS_YCCK) {
kusano 7d535a
      cconvert->pub.color_convert = ycck_cmyk_convert;
kusano 7d535a
      build_ycc_rgb_table(cinfo);
kusano 7d535a
    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
kusano 7d535a
      cconvert->pub.color_convert = null_convert;
kusano 7d535a
    } else
kusano 7d535a
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  default:
kusano 7d535a
    /* Permit null conversion to same output space */
kusano 7d535a
    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
kusano 7d535a
      cinfo->out_color_components = cinfo->num_components;
kusano 7d535a
      cconvert->pub.color_convert = null_convert;
kusano 7d535a
    } else			/* unsupported non-null conversion */
kusano 7d535a
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (cinfo->quantize_colors)
kusano 7d535a
    cinfo->output_components = 1; /* single colormapped output component */
kusano 7d535a
  else
kusano 7d535a
    cinfo->output_components = cinfo->out_color_components;
kusano 7d535a
}