shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jdcolor.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1991-1997, Thomas G. Lane.
shun-iwasawa 82a8f5
 * Modified 2011 by Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB</ossman@cendio.se>
shun-iwasawa 82a8f5
 * Copyright (C) 2009, 2011-2012, 2014-2015, D. R. Commander.
shun-iwasawa 82a8f5
 * Copyright (C) 2013, Linaro Limited.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains output colorspace conversion routines.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define JPEG_INTERNALS
shun-iwasawa 82a8f5
#include "jinclude.h"
shun-iwasawa 82a8f5
#include "jpeglib.h"
shun-iwasawa 82a8f5
#include "jsimd.h"
shun-iwasawa 82a8f5
#include "jconfigint.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Private subobject */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {
shun-iwasawa 82a8f5
  struct jpeg_color_deconverter pub; /* public fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Private state for YCC->RGB conversion */
shun-iwasawa 82a8f5
  int *Cr_r_tab;                /* => table for Cr to R conversion */
shun-iwasawa 82a8f5
  int *Cb_b_tab;                /* => table for Cb to B conversion */
shun-iwasawa 82a8f5
  JLONG *Cr_g_tab;              /* => table for Cr to G conversion */
shun-iwasawa 82a8f5
  JLONG *Cb_g_tab;              /* => table for Cb to G conversion */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Private state for RGB->Y conversion */
shun-iwasawa 82a8f5
  JLONG *rgb_y_tab;             /* => table for RGB to Y conversion */
shun-iwasawa 82a8f5
} my_color_deconverter;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef my_color_deconverter *my_cconvert_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/**************** YCbCr -> RGB conversion: most common case **************/
shun-iwasawa 82a8f5
/****************   RGB -> Y   conversion: less common case **************/
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
shun-iwasawa 82a8f5
 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
shun-iwasawa 82a8f5
 * The conversion equations to be implemented are therefore
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 *      R = Y                + 1.40200 * Cr
shun-iwasawa 82a8f5
 *      G = Y - 0.34414 * Cb - 0.71414 * Cr
shun-iwasawa 82a8f5
 *      B = Y + 1.77200 * Cb
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 *      Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
shun-iwasawa 82a8f5
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * To avoid floating-point arithmetic, we represent the fractional constants
shun-iwasawa 82a8f5
 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
shun-iwasawa 82a8f5
 * the products by 2^16, with appropriate rounding, to get the correct answer.
shun-iwasawa 82a8f5
 * Notice that Y, being an integral input, does not contribute any fraction
shun-iwasawa 82a8f5
 * so it need not participate in the rounding.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * For even more speed, we avoid doing any multiplications in the inner loop
shun-iwasawa 82a8f5
 * by precalculating the constants times Cb and Cr for all possible values.
shun-iwasawa 82a8f5
 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
shun-iwasawa 82a8f5
 * for 12-bit samples it is still acceptable.  It's not very reasonable for
shun-iwasawa 82a8f5
 * 16-bit samples, but if you want lossless storage you shouldn't be changing
shun-iwasawa 82a8f5
 * colorspace anyway.
shun-iwasawa 82a8f5
 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
shun-iwasawa 82a8f5
 * values for the G calculation are left scaled up, since we must add them
shun-iwasawa 82a8f5
 * together before rounding.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define SCALEBITS       16      /* speediest right-shift on some machines */
shun-iwasawa 82a8f5
#define ONE_HALF        ((JLONG)1 << (SCALEBITS - 1))
shun-iwasawa 82a8f5
#define FIX(x)          ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* We allocate one big table for RGB->Y conversion and divide it up into
shun-iwasawa 82a8f5
 * three parts, instead of doing three alloc_small requests.  This lets us
shun-iwasawa 82a8f5
 * use a single table base address, which can be held in a register in the
shun-iwasawa 82a8f5
 * inner loops on many machines (more than can hold all three addresses,
shun-iwasawa 82a8f5
 * anyway).
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define R_Y_OFF         0                       /* offset to R => Y section */
shun-iwasawa 82a8f5
#define G_Y_OFF         (1 * (MAXJSAMPLE + 1))  /* offset to G => Y section */
shun-iwasawa 82a8f5
#define B_Y_OFF         (2 * (MAXJSAMPLE + 1))  /* etc. */
shun-iwasawa 82a8f5
#define TABLE_SIZE      (3 * (MAXJSAMPLE + 1))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Include inline routines for colorspace extensions */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "jdcolext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_RGB_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_RGB_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_RGB_BLUE
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_RGB_PIXELSIZE
shun-iwasawa 82a8f5
#define ycc_rgb_convert_internal  ycc_extrgb_convert_internal
shun-iwasawa 82a8f5
#define gray_rgb_convert_internal  gray_extrgb_convert_internal
shun-iwasawa 82a8f5
#define rgb_rgb_convert_internal  rgb_extrgb_convert_internal
shun-iwasawa 82a8f5
#include "jdcolext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef ycc_rgb_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_RGBX_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_RGBX_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_RGBX_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  3
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_RGBX_PIXELSIZE
shun-iwasawa 82a8f5
#define ycc_rgb_convert_internal  ycc_extrgbx_convert_internal
shun-iwasawa 82a8f5
#define gray_rgb_convert_internal  gray_extrgbx_convert_internal
shun-iwasawa 82a8f5
#define rgb_rgb_convert_internal  rgb_extrgbx_convert_internal
shun-iwasawa 82a8f5
#include "jdcolext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef ycc_rgb_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_BGR_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_BGR_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_BGR_BLUE
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_BGR_PIXELSIZE
shun-iwasawa 82a8f5
#define ycc_rgb_convert_internal  ycc_extbgr_convert_internal
shun-iwasawa 82a8f5
#define gray_rgb_convert_internal  gray_extbgr_convert_internal
shun-iwasawa 82a8f5
#define rgb_rgb_convert_internal  rgb_extbgr_convert_internal
shun-iwasawa 82a8f5
#include "jdcolext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef ycc_rgb_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_BGRX_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_BGRX_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_BGRX_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  3
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_BGRX_PIXELSIZE
shun-iwasawa 82a8f5
#define ycc_rgb_convert_internal  ycc_extbgrx_convert_internal
shun-iwasawa 82a8f5
#define gray_rgb_convert_internal  gray_extbgrx_convert_internal
shun-iwasawa 82a8f5
#define rgb_rgb_convert_internal  rgb_extbgrx_convert_internal
shun-iwasawa 82a8f5
#include "jdcolext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef ycc_rgb_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_XBGR_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_XBGR_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_XBGR_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  0
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_XBGR_PIXELSIZE
shun-iwasawa 82a8f5
#define ycc_rgb_convert_internal  ycc_extxbgr_convert_internal
shun-iwasawa 82a8f5
#define gray_rgb_convert_internal  gray_extxbgr_convert_internal
shun-iwasawa 82a8f5
#define rgb_rgb_convert_internal  rgb_extxbgr_convert_internal
shun-iwasawa 82a8f5
#include "jdcolext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef ycc_rgb_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_XRGB_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_XRGB_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_XRGB_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  0
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_XRGB_PIXELSIZE
shun-iwasawa 82a8f5
#define ycc_rgb_convert_internal  ycc_extxrgb_convert_internal
shun-iwasawa 82a8f5
#define gray_rgb_convert_internal  gray_extxrgb_convert_internal
shun-iwasawa 82a8f5
#define rgb_rgb_convert_internal  rgb_extxrgb_convert_internal
shun-iwasawa 82a8f5
#include "jdcolext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef ycc_rgb_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize tables for YCC->RGB colorspace conversion.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
build_ycc_rgb_table(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
  JLONG x;
shun-iwasawa 82a8f5
  SHIFT_TEMPS
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cconvert->Cr_r_tab = (int *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(int));
shun-iwasawa 82a8f5
  cconvert->Cb_b_tab = (int *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(int));
shun-iwasawa 82a8f5
  cconvert->Cr_g_tab = (JLONG *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(JLONG));
shun-iwasawa 82a8f5
  cconvert->Cb_g_tab = (JLONG *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(JLONG));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
shun-iwasawa 82a8f5
    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
shun-iwasawa 82a8f5
    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
shun-iwasawa 82a8f5
    /* Cr=>R value is nearest int to 1.40200 * x */
shun-iwasawa 82a8f5
    cconvert->Cr_r_tab[i] = (int)
shun-iwasawa 82a8f5
                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
shun-iwasawa 82a8f5
    /* Cb=>B value is nearest int to 1.77200 * x */
shun-iwasawa 82a8f5
    cconvert->Cb_b_tab[i] = (int)
shun-iwasawa 82a8f5
                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
shun-iwasawa 82a8f5
    /* Cr=>G value is scaled-up -0.71414 * x */
shun-iwasawa 82a8f5
    cconvert->Cr_g_tab[i] = (-FIX(0.71414)) * x;
shun-iwasawa 82a8f5
    /* Cb=>G value is scaled-up -0.34414 * x */
shun-iwasawa 82a8f5
    /* We also add in ONE_HALF so that need not do it in inner loop */
shun-iwasawa 82a8f5
    cconvert->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Convert some rows of samples to the output colorspace.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  switch (cinfo->out_color_space) {
shun-iwasawa 82a8f5
  case JCS_EXT_RGB:
shun-iwasawa 82a8f5
    ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_RGBX:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBA:
shun-iwasawa 82a8f5
    ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGR:
shun-iwasawa 82a8f5
    ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGRX:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRA:
shun-iwasawa 82a8f5
    ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XBGR:
shun-iwasawa 82a8f5
  case JCS_EXT_ABGR:
shun-iwasawa 82a8f5
    ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XRGB:
shun-iwasawa 82a8f5
  case JCS_EXT_ARGB:
shun-iwasawa 82a8f5
    ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                             num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/**************** Cases other than YCbCr -> RGB **************/
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize for RGB->grayscale colorspace conversion.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
build_rgb_y_table(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
shun-iwasawa 82a8f5
  JLONG *rgb_y_tab;
shun-iwasawa 82a8f5
  JLONG i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Allocate and fill in the conversion tables. */
shun-iwasawa 82a8f5
  cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (TABLE_SIZE * sizeof(JLONG)));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i <= MAXJSAMPLE; i++) {
shun-iwasawa 82a8f5
    rgb_y_tab[i + R_Y_OFF] = FIX(0.29900) * i;
shun-iwasawa 82a8f5
    rgb_y_tab[i + G_Y_OFF] = FIX(0.58700) * i;
shun-iwasawa 82a8f5
    rgb_y_tab[i + B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Convert RGB to grayscale.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
rgb_gray_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
shun-iwasawa 82a8f5
  register int r, g, b;
shun-iwasawa 82a8f5
  register JLONG *ctab = cconvert->rgb_y_tab;
shun-iwasawa 82a8f5
  register JSAMPROW outptr;
shun-iwasawa 82a8f5
  register JSAMPROW inptr0, inptr1, inptr2;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
  JDIMENSION num_cols = cinfo->output_width;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  while (--num_rows >= 0) {
shun-iwasawa 82a8f5
    inptr0 = input_buf[0][input_row];
shun-iwasawa 82a8f5
    inptr1 = input_buf[1][input_row];
shun-iwasawa 82a8f5
    inptr2 = input_buf[2][input_row];
shun-iwasawa 82a8f5
    input_row++;
shun-iwasawa 82a8f5
    outptr = *output_buf++;
shun-iwasawa 82a8f5
    for (col = 0; col < num_cols; col++) {
shun-iwasawa 82a8f5
      r = GETJSAMPLE(inptr0[col]);
shun-iwasawa 82a8f5
      g = GETJSAMPLE(inptr1[col]);
shun-iwasawa 82a8f5
      b = GETJSAMPLE(inptr2[col]);
shun-iwasawa 82a8f5
      /* Y */
shun-iwasawa 82a8f5
      outptr[col] = (JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
shun-iwasawa 82a8f5
                               ctab[b + B_Y_OFF]) >> SCALEBITS);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Color conversion for no colorspace change: just copy the data,
shun-iwasawa 82a8f5
 * converting from separate-planes to interleaved representation.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
null_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
             JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
  register int num_components = cinfo->num_components;
shun-iwasawa 82a8f5
  JDIMENSION num_cols = cinfo->output_width;
shun-iwasawa 82a8f5
  int ci;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (num_components == 3) {
shun-iwasawa 82a8f5
    while (--num_rows >= 0) {
shun-iwasawa 82a8f5
      inptr0 = input_buf[0][input_row];
shun-iwasawa 82a8f5
      inptr1 = input_buf[1][input_row];
shun-iwasawa 82a8f5
      inptr2 = input_buf[2][input_row];
shun-iwasawa 82a8f5
      input_row++;
shun-iwasawa 82a8f5
      outptr = *output_buf++;
shun-iwasawa 82a8f5
      for (col = 0; col < num_cols; col++) {
shun-iwasawa 82a8f5
        *outptr++ = inptr0[col];
shun-iwasawa 82a8f5
        *outptr++ = inptr1[col];
shun-iwasawa 82a8f5
        *outptr++ = inptr2[col];
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else if (num_components == 4) {
shun-iwasawa 82a8f5
    while (--num_rows >= 0) {
shun-iwasawa 82a8f5
      inptr0 = input_buf[0][input_row];
shun-iwasawa 82a8f5
      inptr1 = input_buf[1][input_row];
shun-iwasawa 82a8f5
      inptr2 = input_buf[2][input_row];
shun-iwasawa 82a8f5
      inptr3 = input_buf[3][input_row];
shun-iwasawa 82a8f5
      input_row++;
shun-iwasawa 82a8f5
      outptr = *output_buf++;
shun-iwasawa 82a8f5
      for (col = 0; col < num_cols; col++) {
shun-iwasawa 82a8f5
        *outptr++ = inptr0[col];
shun-iwasawa 82a8f5
        *outptr++ = inptr1[col];
shun-iwasawa 82a8f5
        *outptr++ = inptr2[col];
shun-iwasawa 82a8f5
        *outptr++ = inptr3[col];
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    while (--num_rows >= 0) {
shun-iwasawa 82a8f5
      for (ci = 0; ci < num_components; ci++) {
shun-iwasawa 82a8f5
        inptr = input_buf[ci][input_row];
shun-iwasawa 82a8f5
        outptr = *output_buf;
shun-iwasawa 82a8f5
        for (col = 0; col < num_cols; col++) {
shun-iwasawa 82a8f5
          outptr[ci] = inptr[col];
shun-iwasawa 82a8f5
          outptr += num_components;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      output_buf++;
shun-iwasawa 82a8f5
      input_row++;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Color conversion for grayscale: just copy the data.
shun-iwasawa 82a8f5
 * This also works for YCbCr -> grayscale conversion, in which
shun-iwasawa 82a8f5
 * we just copy the Y (luminance) component and ignore chrominance.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
grayscale_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                  JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jcopy_sample_rows(input_buf[0], (int)input_row, output_buf, 0, num_rows,
shun-iwasawa 82a8f5
                    cinfo->output_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Convert grayscale to RGB
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
gray_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  switch (cinfo->out_color_space) {
shun-iwasawa 82a8f5
  case JCS_EXT_RGB:
shun-iwasawa 82a8f5
    gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_RGBX:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBA:
shun-iwasawa 82a8f5
    gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                  num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGR:
shun-iwasawa 82a8f5
    gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGRX:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRA:
shun-iwasawa 82a8f5
    gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                  num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XBGR:
shun-iwasawa 82a8f5
  case JCS_EXT_ABGR:
shun-iwasawa 82a8f5
    gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                  num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XRGB:
shun-iwasawa 82a8f5
  case JCS_EXT_ARGB:
shun-iwasawa 82a8f5
    gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                  num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                              num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Convert plain RGB to extended RGB
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
rgb_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  switch (cinfo->out_color_space) {
shun-iwasawa 82a8f5
  case JCS_EXT_RGB:
shun-iwasawa 82a8f5
    rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_RGBX:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBA:
shun-iwasawa 82a8f5
    rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGR:
shun-iwasawa 82a8f5
    rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGRX:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRA:
shun-iwasawa 82a8f5
    rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XBGR:
shun-iwasawa 82a8f5
  case JCS_EXT_ABGR:
shun-iwasawa 82a8f5
    rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XRGB:
shun-iwasawa 82a8f5
  case JCS_EXT_ARGB:
shun-iwasawa 82a8f5
    rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                                 num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
shun-iwasawa 82a8f5
                             num_rows);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Adobe-style YCCK->CMYK conversion.
shun-iwasawa 82a8f5
 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
shun-iwasawa 82a8f5
 * conversion as above, while passing K (black) unchanged.
shun-iwasawa 82a8f5
 * We assume build_ycc_rgb_table has been called.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
ycck_cmyk_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                  JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
shun-iwasawa 82a8f5
  register int y, cb, cr;
shun-iwasawa 82a8f5
  register JSAMPROW outptr;
shun-iwasawa 82a8f5
  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
  JDIMENSION num_cols = cinfo->output_width;
shun-iwasawa 82a8f5
  /* copy these pointers into registers if possible */
shun-iwasawa 82a8f5
  register JSAMPLE *range_limit = cinfo->sample_range_limit;
shun-iwasawa 82a8f5
  register int *Crrtab = cconvert->Cr_r_tab;
shun-iwasawa 82a8f5
  register int *Cbbtab = cconvert->Cb_b_tab;
shun-iwasawa 82a8f5
  register JLONG *Crgtab = cconvert->Cr_g_tab;
shun-iwasawa 82a8f5
  register JLONG *Cbgtab = cconvert->Cb_g_tab;
shun-iwasawa 82a8f5
  SHIFT_TEMPS
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  while (--num_rows >= 0) {
shun-iwasawa 82a8f5
    inptr0 = input_buf[0][input_row];
shun-iwasawa 82a8f5
    inptr1 = input_buf[1][input_row];
shun-iwasawa 82a8f5
    inptr2 = input_buf[2][input_row];
shun-iwasawa 82a8f5
    inptr3 = input_buf[3][input_row];
shun-iwasawa 82a8f5
    input_row++;
shun-iwasawa 82a8f5
    outptr = *output_buf++;
shun-iwasawa 82a8f5
    for (col = 0; col < num_cols; col++) {
shun-iwasawa 82a8f5
      y  = GETJSAMPLE(inptr0[col]);
shun-iwasawa 82a8f5
      cb = GETJSAMPLE(inptr1[col]);
shun-iwasawa 82a8f5
      cr = GETJSAMPLE(inptr2[col]);
shun-iwasawa 82a8f5
      /* Range-limiting is essential due to noise introduced by DCT losses. */
shun-iwasawa 82a8f5
      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];   /* red */
shun-iwasawa 82a8f5
      outptr[1] = range_limit[MAXJSAMPLE - (y +                 /* green */
shun-iwasawa 82a8f5
                              ((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
shun-iwasawa 82a8f5
                                                 SCALEBITS)))];
shun-iwasawa 82a8f5
      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];   /* blue */
shun-iwasawa 82a8f5
      /* K passes through unchanged */
shun-iwasawa 82a8f5
      outptr[3] = inptr3[col];  /* don't need GETJSAMPLE here */
shun-iwasawa 82a8f5
      outptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * RGB565 conversion
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_SHORT_565_LE(r, g, b) \
shun-iwasawa 82a8f5
  ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
shun-iwasawa 82a8f5
#define PACK_SHORT_565_BE(r, g, b) \
shun-iwasawa 82a8f5
  (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS_LE(l, r)    ((r << 16) | l)
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS_BE(l, r)    ((l << 16) | r)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_NEED_ALIGNMENT(ptr)    (((size_t)(ptr)) & 3)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define WRITE_TWO_ALIGNED_PIXELS(addr, pixels)  ((*(int *)(addr)) = pixels)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define DITHER_565_R(r, dither)  ((r) + ((dither) & 0xFF))
shun-iwasawa 82a8f5
#define DITHER_565_G(g, dither)  ((g) + (((dither) & 0xFF) >> 1))
shun-iwasawa 82a8f5
#define DITHER_565_B(b, dither)  ((b) + ((dither) & 0xFF))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Declarations for ordered dithering
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * We use a 4x4 ordered dither array packed into 32 bits.  This array is
shun-iwasawa 82a8f5
 * sufficient for dithering RGB888 to RGB565.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define DITHER_MASK       0x3
shun-iwasawa 82a8f5
#define DITHER_ROTATE(x)  ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
shun-iwasawa 82a8f5
static const JLONG dither_matrix[4] = {
shun-iwasawa 82a8f5
  0x0008020A,
shun-iwasawa 82a8f5
  0x0C040E06,
shun-iwasawa 82a8f5
  0x030B0109,
shun-iwasawa 82a8f5
  0x0F070D05
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static INLINE boolean is_big_endian(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int test_value = 1;
shun-iwasawa 82a8f5
  if (*(char *)&test_value != 1)
shun-iwasawa 82a8f5
    return TRUE;
shun-iwasawa 82a8f5
  return FALSE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Include inline routines for RGB565 conversion */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_SHORT_565  PACK_SHORT_565_LE
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS  PACK_TWO_PIXELS_LE
shun-iwasawa 82a8f5
#define ycc_rgb565_convert_internal  ycc_rgb565_convert_le
shun-iwasawa 82a8f5
#define ycc_rgb565D_convert_internal  ycc_rgb565D_convert_le
shun-iwasawa 82a8f5
#define rgb_rgb565_convert_internal  rgb_rgb565_convert_le
shun-iwasawa 82a8f5
#define rgb_rgb565D_convert_internal  rgb_rgb565D_convert_le
shun-iwasawa 82a8f5
#define gray_rgb565_convert_internal  gray_rgb565_convert_le
shun-iwasawa 82a8f5
#define gray_rgb565D_convert_internal  gray_rgb565D_convert_le
shun-iwasawa 82a8f5
#include "jdcol565.c"
shun-iwasawa 82a8f5
#undef PACK_SHORT_565
shun-iwasawa 82a8f5
#undef PACK_TWO_PIXELS
shun-iwasawa 82a8f5
#undef ycc_rgb565_convert_internal
shun-iwasawa 82a8f5
#undef ycc_rgb565D_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb565_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb565D_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb565_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb565D_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_SHORT_565  PACK_SHORT_565_BE
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS  PACK_TWO_PIXELS_BE
shun-iwasawa 82a8f5
#define ycc_rgb565_convert_internal  ycc_rgb565_convert_be
shun-iwasawa 82a8f5
#define ycc_rgb565D_convert_internal  ycc_rgb565D_convert_be
shun-iwasawa 82a8f5
#define rgb_rgb565_convert_internal  rgb_rgb565_convert_be
shun-iwasawa 82a8f5
#define rgb_rgb565D_convert_internal  rgb_rgb565D_convert_be
shun-iwasawa 82a8f5
#define gray_rgb565_convert_internal  gray_rgb565_convert_be
shun-iwasawa 82a8f5
#define gray_rgb565D_convert_internal  gray_rgb565D_convert_be
shun-iwasawa 82a8f5
#include "jdcol565.c"
shun-iwasawa 82a8f5
#undef PACK_SHORT_565
shun-iwasawa 82a8f5
#undef PACK_TWO_PIXELS
shun-iwasawa 82a8f5
#undef ycc_rgb565_convert_internal
shun-iwasawa 82a8f5
#undef ycc_rgb565D_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb565_convert_internal
shun-iwasawa 82a8f5
#undef rgb_rgb565D_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb565_convert_internal
shun-iwasawa 82a8f5
#undef gray_rgb565D_convert_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                   JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                    JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                   JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                    JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                    JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                     JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Empty method for start_pass.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
start_pass_dcolor(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* no work needed */
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Module initialization routine for output colorspace conversion.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jinit_color_deconverter(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_cconvert_ptr cconvert;
shun-iwasawa 82a8f5
  int ci;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cconvert = (my_cconvert_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(my_color_deconverter));
shun-iwasawa 82a8f5
  cinfo->cconvert = (struct jpeg_color_deconverter *)cconvert;
shun-iwasawa 82a8f5
  cconvert->pub.start_pass = start_pass_dcolor;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Make sure num_components agrees with jpeg_color_space */
shun-iwasawa 82a8f5
  switch (cinfo->jpeg_color_space) {
shun-iwasawa 82a8f5
  case JCS_GRAYSCALE:
shun-iwasawa 82a8f5
    if (cinfo->num_components != 1)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case JCS_RGB:
shun-iwasawa 82a8f5
  case JCS_YCbCr:
shun-iwasawa 82a8f5
    if (cinfo->num_components != 3)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case JCS_CMYK:
shun-iwasawa 82a8f5
  case JCS_YCCK:
shun-iwasawa 82a8f5
    if (cinfo->num_components != 4)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  default:                      /* JCS_UNKNOWN can be anything */
shun-iwasawa 82a8f5
    if (cinfo->num_components < 1)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Set out_color_components and conversion method based on requested space.
shun-iwasawa 82a8f5
   * Also clear the component_needed flags for any unused components,
shun-iwasawa 82a8f5
   * so that earlier pipeline stages can avoid useless computation.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  switch (cinfo->out_color_space) {
shun-iwasawa 82a8f5
  case JCS_GRAYSCALE:
shun-iwasawa 82a8f5
    cinfo->out_color_components = 1;
shun-iwasawa 82a8f5
    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
shun-iwasawa 82a8f5
        cinfo->jpeg_color_space == JCS_YCbCr) {
shun-iwasawa 82a8f5
      cconvert->pub.color_convert = grayscale_convert;
shun-iwasawa 82a8f5
      /* For color->grayscale conversion, only the Y (0) component is needed */
shun-iwasawa 82a8f5
      for (ci = 1; ci < cinfo->num_components; ci++)
shun-iwasawa 82a8f5
        cinfo->comp_info[ci].component_needed = FALSE;
shun-iwasawa 82a8f5
    } else if (cinfo->jpeg_color_space == JCS_RGB) {
shun-iwasawa 82a8f5
      cconvert->pub.color_convert = rgb_gray_convert;
shun-iwasawa 82a8f5
      build_rgb_y_table(cinfo);
shun-iwasawa 82a8f5
    } else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case JCS_RGB:
shun-iwasawa 82a8f5
  case JCS_EXT_RGB:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBX:
shun-iwasawa 82a8f5
  case JCS_EXT_BGR:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRX:
shun-iwasawa 82a8f5
  case JCS_EXT_XBGR:
shun-iwasawa 82a8f5
  case JCS_EXT_XRGB:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBA:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRA:
shun-iwasawa 82a8f5
  case JCS_EXT_ABGR:
shun-iwasawa 82a8f5
  case JCS_EXT_ARGB:
shun-iwasawa 82a8f5
    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
shun-iwasawa 82a8f5
    if (cinfo->jpeg_color_space == JCS_YCbCr) {
shun-iwasawa 82a8f5
      if (jsimd_can_ycc_rgb())
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
shun-iwasawa 82a8f5
      else {
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = ycc_rgb_convert;
shun-iwasawa 82a8f5
        build_ycc_rgb_table(cinfo);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
shun-iwasawa 82a8f5
      cconvert->pub.color_convert = gray_rgb_convert;
shun-iwasawa 82a8f5
    } else if (cinfo->jpeg_color_space == JCS_RGB) {
shun-iwasawa 82a8f5
      if (rgb_red[cinfo->out_color_space] == 0 &&
shun-iwasawa 82a8f5
          rgb_green[cinfo->out_color_space] == 1 &&
shun-iwasawa 82a8f5
          rgb_blue[cinfo->out_color_space] == 2 &&
shun-iwasawa 82a8f5
          rgb_pixelsize[cinfo->out_color_space] == 3)
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = null_convert;
shun-iwasawa 82a8f5
      else
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = rgb_rgb_convert;
shun-iwasawa 82a8f5
    } else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case JCS_RGB565:
shun-iwasawa 82a8f5
    cinfo->out_color_components = 3;
shun-iwasawa 82a8f5
    if (cinfo->dither_mode == JDITHER_NONE) {
shun-iwasawa 82a8f5
      if (cinfo->jpeg_color_space == JCS_YCbCr) {
shun-iwasawa 82a8f5
        if (jsimd_can_ycc_rgb565())
shun-iwasawa 82a8f5
          cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
shun-iwasawa 82a8f5
        else {
shun-iwasawa 82a8f5
          cconvert->pub.color_convert = ycc_rgb565_convert;
shun-iwasawa 82a8f5
          build_ycc_rgb_table(cinfo);
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = gray_rgb565_convert;
shun-iwasawa 82a8f5
      } else if (cinfo->jpeg_color_space == JCS_RGB) {
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = rgb_rgb565_convert;
shun-iwasawa 82a8f5
      } else
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      /* only ordered dithering is supported */
shun-iwasawa 82a8f5
      if (cinfo->jpeg_color_space == JCS_YCbCr) {
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = ycc_rgb565D_convert;
shun-iwasawa 82a8f5
        build_ycc_rgb_table(cinfo);
shun-iwasawa 82a8f5
      } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = gray_rgb565D_convert;
shun-iwasawa 82a8f5
      } else if (cinfo->jpeg_color_space == JCS_RGB) {
shun-iwasawa 82a8f5
        cconvert->pub.color_convert = rgb_rgb565D_convert;
shun-iwasawa 82a8f5
      } else
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  case JCS_CMYK:
shun-iwasawa 82a8f5
    cinfo->out_color_components = 4;
shun-iwasawa 82a8f5
    if (cinfo->jpeg_color_space == JCS_YCCK) {
shun-iwasawa 82a8f5
      cconvert->pub.color_convert = ycck_cmyk_convert;
shun-iwasawa 82a8f5
      build_ycc_rgb_table(cinfo);
shun-iwasawa 82a8f5
    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
shun-iwasawa 82a8f5
      cconvert->pub.color_convert = null_convert;
shun-iwasawa 82a8f5
    } else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    /* Permit null conversion to same output space */
shun-iwasawa 82a8f5
    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
shun-iwasawa 82a8f5
      cinfo->out_color_components = cinfo->num_components;
shun-iwasawa 82a8f5
      cconvert->pub.color_convert = null_convert;
shun-iwasawa 82a8f5
    } else                      /* unsupported non-null conversion */
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->quantize_colors)
shun-iwasawa 82a8f5
    cinfo->output_components = 1; /* single colormapped output component */
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    cinfo->output_components = cinfo->out_color_components;
shun-iwasawa 82a8f5
}