kusano 7d535a
/*
kusano 7d535a
 * jccolor.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1996, 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 input 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_converter pub; /* public fields */
kusano 7d535a
kusano 7d535a
  /* Private state for RGB->YCC conversion */
kusano 7d535a
  INT32 * rgb_ycc_tab;		/* => table for RGB to YCbCr conversion */
kusano 7d535a
} my_color_converter;
kusano 7d535a
kusano 7d535a
typedef my_color_converter * my_cconvert_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************** RGB -> YCbCr conversion: most 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
 *	Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
kusano 7d535a
 *	Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
kusano 7d535a
 *	Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
kusano 7d535a
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
kusano 7d535a
 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
kusano 7d535a
 * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
kusano 7d535a
 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
kusano 7d535a
 * were not represented exactly.  Now we sacrifice exact representation of
kusano 7d535a
 * maximum red and maximum blue in order to get exact grayscales.
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
 *
kusano 7d535a
 * For even more speed, we avoid doing any multiplications in the inner loop
kusano 7d535a
 * by precalculating the constants times R,G,B 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 CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
kusano 7d535a
 * in the tables to save adding them separately in the inner loop.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define SCALEBITS	16	/* speediest right-shift on some machines */
kusano 7d535a
#define CBCR_OFFSET	((INT32) CENTERJSAMPLE << SCALEBITS)
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 and divide it up into eight parts, instead of
kusano 7d535a
 * doing eight alloc_small requests.  This lets us use a single table base
kusano 7d535a
 * address, which can be held in a register in the inner loops on many
kusano 7d535a
 * machines (more than can hold all eight addresses, 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 R_CB_OFF	(3*(MAXJSAMPLE+1))
kusano 7d535a
#define G_CB_OFF	(4*(MAXJSAMPLE+1))
kusano 7d535a
#define B_CB_OFF	(5*(MAXJSAMPLE+1))
kusano 7d535a
#define R_CR_OFF	B_CB_OFF		/* B=>Cb, R=>Cr are the same */
kusano 7d535a
#define G_CR_OFF	(6*(MAXJSAMPLE+1))
kusano 7d535a
#define B_CR_OFF	(7*(MAXJSAMPLE+1))
kusano 7d535a
#define TABLE_SIZE	(8*(MAXJSAMPLE+1))
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for RGB->YCC colorspace conversion.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb_ycc_start (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  INT32 * rgb_ycc_tab;
kusano 7d535a
  INT32 i;
kusano 7d535a
kusano 7d535a
  /* Allocate and fill in the conversion tables. */
kusano 7d535a
  cconvert->rgb_ycc_tab = rgb_ycc_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_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
kusano 7d535a
    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
kusano 7d535a
    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
kusano 7d535a
    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
kusano 7d535a
    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
kusano 7d535a
    /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
kusano 7d535a
     * This ensures that the maximum output will round to MAXJSAMPLE
kusano 7d535a
     * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
kusano 7d535a
     */
kusano 7d535a
    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
kusano 7d535a
/*  B=>Cb and R=>Cr tables are the same
kusano 7d535a
    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
kusano 7d535a
*/
kusano 7d535a
    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
kusano 7d535a
    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the JPEG colorspace.
kusano 7d535a
 *
kusano 7d535a
 * Note that we change from the application's interleaved-pixel format
kusano 7d535a
 * to our internal noninterleaved, one-plane-per-component format.
kusano 7d535a
 * The input buffer is therefore three times as wide as the output buffer.
kusano 7d535a
 *
kusano 7d535a
 * A starting row offset is provided only for the output buffer.  The caller
kusano 7d535a
 * can easily adjust the passed input_buf value to accommodate any row
kusano 7d535a
 * offset required on that side.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb_ycc_convert (j_compress_ptr cinfo,
kusano 7d535a
		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
kusano 7d535a
		 JDIMENSION output_row, int num_rows)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  register INT32 * ctab = cconvert->rgb_ycc_tab;
kusano 7d535a
  register int r, g, b;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JSAMPROW outptr0, outptr1, outptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->image_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr = *input_buf++;
kusano 7d535a
    outptr0 = output_buf[0][output_row];
kusano 7d535a
    outptr1 = output_buf[1][output_row];
kusano 7d535a
    outptr2 = output_buf[2][output_row];
kusano 7d535a
    output_row++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      r = GETJSAMPLE(inptr[RGB_RED]);
kusano 7d535a
      g = GETJSAMPLE(inptr[RGB_GREEN]);
kusano 7d535a
      b = GETJSAMPLE(inptr[RGB_BLUE]);
kusano 7d535a
      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
kusano 7d535a
       * must be too; we do not need an explicit range-limiting operation.
kusano 7d535a
       * Hence the value being shifted is never negative, and we don't
kusano 7d535a
       * need the general RIGHT_SHIFT macro.
kusano 7d535a
       */
kusano 7d535a
      /* Y */
kusano 7d535a
      outptr0[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
      /* Cb */
kusano 7d535a
      outptr1[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
      /* Cr */
kusano 7d535a
      outptr2[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
      inptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************** Cases other than RGB -> YCbCr **************/
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the JPEG colorspace.
kusano 7d535a
 * This version handles RGB->grayscale conversion, which is the same
kusano 7d535a
 * as the RGB->Y portion of RGB->YCbCr.
kusano 7d535a
 * We assume rgb_ycc_start has been called (we only use the Y tables).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb_gray_convert (j_compress_ptr cinfo,
kusano 7d535a
		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
kusano 7d535a
		  JDIMENSION output_row, int num_rows)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  register INT32 * ctab = cconvert->rgb_ycc_tab;
kusano 7d535a
  register int r, g, b;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->image_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr = *input_buf++;
kusano 7d535a
    outptr = output_buf[0][output_row++];
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      r = GETJSAMPLE(inptr[RGB_RED]);
kusano 7d535a
      g = GETJSAMPLE(inptr[RGB_GREEN]);
kusano 7d535a
      b = GETJSAMPLE(inptr[RGB_BLUE]);
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
      inptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the JPEG colorspace.
kusano 7d535a
 * This version handles Adobe-style CMYK->YCCK conversion,
kusano 7d535a
 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
kusano 7d535a
 * conversion as above, while passing K (black) unchanged.
kusano 7d535a
 * We assume rgb_ycc_start has been called.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
cmyk_ycck_convert (j_compress_ptr cinfo,
kusano 7d535a
		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
kusano 7d535a
		   JDIMENSION output_row, int num_rows)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
kusano 7d535a
  register INT32 * ctab = cconvert->rgb_ycc_tab;
kusano 7d535a
  register int r, g, b;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->image_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr = *input_buf++;
kusano 7d535a
    outptr0 = output_buf[0][output_row];
kusano 7d535a
    outptr1 = output_buf[1][output_row];
kusano 7d535a
    outptr2 = output_buf[2][output_row];
kusano 7d535a
    outptr3 = output_buf[3][output_row];
kusano 7d535a
    output_row++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
kusano 7d535a
      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
kusano 7d535a
      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
kusano 7d535a
      /* K passes through as-is */
kusano 7d535a
      outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
kusano 7d535a
      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
kusano 7d535a
       * must be too; we do not need an explicit range-limiting operation.
kusano 7d535a
       * Hence the value being shifted is never negative, and we don't
kusano 7d535a
       * need the general RIGHT_SHIFT macro.
kusano 7d535a
       */
kusano 7d535a
      /* Y */
kusano 7d535a
      outptr0[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
      /* Cb */
kusano 7d535a
      outptr1[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
      /* Cr */
kusano 7d535a
      outptr2[col] = (JSAMPLE)
kusano 7d535a
		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
kusano 7d535a
		 >> SCALEBITS);
kusano 7d535a
      inptr += 4;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the JPEG colorspace.
kusano 7d535a
 * [R,G,B] to [R-G,G,B-G] conversion with modulo calculation
kusano 7d535a
 * (forward reversible color transform).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb_rgb1_convert (j_compress_ptr cinfo,
kusano 7d535a
		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
kusano 7d535a
		  JDIMENSION output_row, int num_rows)
kusano 7d535a
{
kusano 7d535a
  register int r, g, b;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JSAMPROW outptr0, outptr1, outptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->image_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr = *input_buf++;
kusano 7d535a
    outptr0 = output_buf[0][output_row];
kusano 7d535a
    outptr1 = output_buf[1][output_row];
kusano 7d535a
    outptr2 = output_buf[2][output_row];
kusano 7d535a
    output_row++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      r = GETJSAMPLE(inptr[RGB_RED]);
kusano 7d535a
      g = GETJSAMPLE(inptr[RGB_GREEN]);
kusano 7d535a
      b = GETJSAMPLE(inptr[RGB_BLUE]);
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
      outptr0[col] = (JSAMPLE) ((r - g + CENTERJSAMPLE) & MAXJSAMPLE);
kusano 7d535a
      outptr1[col] = (JSAMPLE) g;
kusano 7d535a
      outptr2[col] = (JSAMPLE) ((b - g + CENTERJSAMPLE) & MAXJSAMPLE);
kusano 7d535a
      inptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the JPEG colorspace.
kusano 7d535a
 * This version handles grayscale output with no conversion.
kusano 7d535a
 * The source can be either plain grayscale or YCbCr (since Y == gray).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
grayscale_convert (j_compress_ptr cinfo,
kusano 7d535a
		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
kusano 7d535a
		   JDIMENSION output_row, int num_rows)
kusano 7d535a
{
kusano 7d535a
  int instride = cinfo->input_components;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->image_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr = *input_buf++;
kusano 7d535a
    outptr = output_buf[0][output_row++];
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
kusano 7d535a
      inptr += instride;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the JPEG colorspace.
kusano 7d535a
 * No colorspace conversion, but change from interleaved
kusano 7d535a
 * to separate-planes representation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
rgb_convert (j_compress_ptr cinfo,
kusano 7d535a
	     JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
kusano 7d535a
	     JDIMENSION output_row, int num_rows)
kusano 7d535a
{
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JSAMPROW outptr0, outptr1, outptr2;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->image_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    inptr = *input_buf++;
kusano 7d535a
    outptr0 = output_buf[0][output_row];
kusano 7d535a
    outptr1 = output_buf[1][output_row];
kusano 7d535a
    outptr2 = output_buf[2][output_row];
kusano 7d535a
    output_row++;
kusano 7d535a
    for (col = 0; col < num_cols; col++) {
kusano 7d535a
      /* We can dispense with GETJSAMPLE() here */
kusano 7d535a
      outptr0[col] = inptr[RGB_RED];
kusano 7d535a
      outptr1[col] = inptr[RGB_GREEN];
kusano 7d535a
      outptr2[col] = inptr[RGB_BLUE];
kusano 7d535a
      inptr += RGB_PIXELSIZE;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert some rows of samples to the JPEG colorspace.
kusano 7d535a
 * This version handles multi-component colorspaces without conversion.
kusano 7d535a
 * We assume input_components == num_components.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
null_convert (j_compress_ptr cinfo,
kusano 7d535a
	      JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
kusano 7d535a
	      JDIMENSION output_row, int num_rows)
kusano 7d535a
{
kusano 7d535a
  int ci;
kusano 7d535a
  register int nc = cinfo->num_components;
kusano 7d535a
  register JSAMPROW inptr;
kusano 7d535a
  register JSAMPROW outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  JDIMENSION num_cols = cinfo->image_width;
kusano 7d535a
kusano 7d535a
  while (--num_rows >= 0) {
kusano 7d535a
    /* It seems fastest to make a separate pass for each component. */
kusano 7d535a
    for (ci = 0; ci < nc; ci++) {
kusano 7d535a
      inptr = input_buf[0] + ci;
kusano 7d535a
      outptr = output_buf[ci][output_row];
kusano 7d535a
      for (col = 0; col < num_cols; col++) {
kusano 7d535a
	*outptr++ = *inptr;	/* don't need GETJSAMPLE() here */
kusano 7d535a
	inptr += nc;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    input_buf++;
kusano 7d535a
    output_row++;
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
null_method (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work needed */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Module initialization routine for input colorspace conversion.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_color_converter (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cconvert_ptr cconvert;
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_converter));
kusano 7d535a
  cinfo->cconvert = &cconvert->pub;
kusano 7d535a
  /* set start_pass to null method until we find out differently */
kusano 7d535a
  cconvert->pub.start_pass = null_method;
kusano 7d535a
kusano 7d535a
  /* Make sure input_components agrees with in_color_space */
kusano 7d535a
  switch (cinfo->in_color_space) {
kusano 7d535a
  case JCS_GRAYSCALE:
kusano 7d535a
    if (cinfo->input_components != 1)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_RGB:
kusano 7d535a
    if (cinfo->input_components != RGB_PIXELSIZE)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_YCbCr:
kusano 7d535a
    if (cinfo->input_components != 3)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_CMYK:
kusano 7d535a
  case JCS_YCCK:
kusano 7d535a
    if (cinfo->input_components != 4)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  default:			/* JCS_UNKNOWN can be anything */
kusano 7d535a
    if (cinfo->input_components < 1)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_IN_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
  /* Check num_components, set conversion method based on requested 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
    if (cinfo->in_color_space == JCS_GRAYSCALE ||
kusano 7d535a
	cinfo->in_color_space == JCS_YCbCr)
kusano 7d535a
      cconvert->pub.color_convert = grayscale_convert;
kusano 7d535a
    else if (cinfo->in_color_space == JCS_RGB) {
kusano 7d535a
      cconvert->pub.start_pass = rgb_ycc_start;
kusano 7d535a
      cconvert->pub.color_convert = rgb_gray_convert;
kusano 7d535a
    } else
kusano 7d535a
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case JCS_RGB:
kusano 7d535a
    if (cinfo->num_components != 3)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    if (cinfo->in_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 = rgb_rgb1_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_YCbCr:
kusano 7d535a
    if (cinfo->num_components != 3)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    if (cinfo->in_color_space == JCS_RGB) {
kusano 7d535a
      cconvert->pub.start_pass = rgb_ycc_start;
kusano 7d535a
      cconvert->pub.color_convert = rgb_ycc_convert;
kusano 7d535a
    } else if (cinfo->in_color_space == JCS_YCbCr)
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
  case JCS_CMYK:
kusano 7d535a
    if (cinfo->num_components != 4)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    if (cinfo->in_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
  case JCS_YCCK:
kusano 7d535a
    if (cinfo->num_components != 4)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
kusano 7d535a
    if (cinfo->in_color_space == JCS_CMYK) {
kusano 7d535a
      cconvert->pub.start_pass = rgb_ycc_start;
kusano 7d535a
      cconvert->pub.color_convert = cmyk_ycck_convert;
kusano 7d535a
    } else if (cinfo->in_color_space == JCS_YCCK)
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:			/* allow null conversion of JCS_UNKNOWN */
kusano 7d535a
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
kusano 7d535a
	cinfo->num_components != cinfo->input_components)
kusano 7d535a
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
    cconvert->pub.color_convert = null_convert;
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
}