kusano 7d535a
/*
kusano 7d535a
 * jquant1.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2011 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 1-pass color quantization (color mapping) routines.
kusano 7d535a
 * These routines provide mapping to a fixed color map using equally spaced
kusano 7d535a
 * color values.  Optional Floyd-Steinberg or ordered dithering is available.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
#ifdef QUANT_1PASS_SUPPORTED
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The main purpose of 1-pass quantization is to provide a fast, if not very
kusano 7d535a
 * high quality, colormapped output capability.  A 2-pass quantizer usually
kusano 7d535a
 * gives better visual quality; however, for quantized grayscale output this
kusano 7d535a
 * quantizer is perfectly adequate.  Dithering is highly recommended with this
kusano 7d535a
 * quantizer, though you can turn it off if you really want to.
kusano 7d535a
 *
kusano 7d535a
 * In 1-pass quantization the colormap must be chosen in advance of seeing the
kusano 7d535a
 * image.  We use a map consisting of all combinations of Ncolors[i] color
kusano 7d535a
 * values for the i'th component.  The Ncolors[] values are chosen so that
kusano 7d535a
 * their product, the total number of colors, is no more than that requested.
kusano 7d535a
 * (In most cases, the product will be somewhat less.)
kusano 7d535a
 *
kusano 7d535a
 * Since the colormap is orthogonal, the representative value for each color
kusano 7d535a
 * component can be determined without considering the other components;
kusano 7d535a
 * then these indexes can be combined into a colormap index by a standard
kusano 7d535a
 * N-dimensional-array-subscript calculation.  Most of the arithmetic involved
kusano 7d535a
 * can be precalculated and stored in the lookup table colorindex[].
kusano 7d535a
 * colorindex[i][j] maps pixel value j in component i to the nearest
kusano 7d535a
 * representative value (grid plane) for that component; this index is
kusano 7d535a
 * multiplied by the array stride for component i, so that the
kusano 7d535a
 * index of the colormap entry closest to a given pixel value is just
kusano 7d535a
 *    sum( colorindex[component-number][pixel-component-value] )
kusano 7d535a
 * Aside from being fast, this scheme allows for variable spacing between
kusano 7d535a
 * representative values with no additional lookup cost.
kusano 7d535a
 *
kusano 7d535a
 * If gamma correction has been applied in color conversion, it might be wise
kusano 7d535a
 * to adjust the color grid spacing so that the representative colors are
kusano 7d535a
 * equidistant in linear space.  At this writing, gamma correction is not
kusano 7d535a
 * implemented by jdcolor, so nothing is done here.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Declarations for ordered dithering.
kusano 7d535a
 *
kusano 7d535a
 * We use a standard 16x16 ordered dither array.  The basic concept of ordered
kusano 7d535a
 * dithering is described in many references, for instance Dale Schumacher's
kusano 7d535a
 * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991).
kusano 7d535a
 * In place of Schumacher's comparisons against a "threshold" value, we add a
kusano 7d535a
 * "dither" value to the input pixel and then round the result to the nearest
kusano 7d535a
 * output value.  The dither value is equivalent to (0.5 - threshold) times
kusano 7d535a
 * the distance between output values.  For ordered dithering, we assume that
kusano 7d535a
 * the output colors are equally spaced; if not, results will probably be
kusano 7d535a
 * worse, since the dither may be too much or too little at a given point.
kusano 7d535a
 *
kusano 7d535a
 * The normal calculation would be to form pixel value + dither, range-limit
kusano 7d535a
 * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual.
kusano 7d535a
 * We can skip the separate range-limiting step by extending the colorindex
kusano 7d535a
 * table in both directions.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define ODITHER_SIZE  16	/* dimension of dither matrix */
kusano 7d535a
/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */
kusano 7d535a
#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE)	/* # cells in matrix */
kusano 7d535a
#define ODITHER_MASK  (ODITHER_SIZE-1) /* mask for wrapping around counters */
kusano 7d535a
kusano 7d535a
typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
kusano 7d535a
typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];
kusano 7d535a
kusano 7d535a
static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
kusano 7d535a
  /* Bayer's order-4 dither array.  Generated by the code given in
kusano 7d535a
   * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I.
kusano 7d535a
   * The values in this array must range from 0 to ODITHER_CELLS-1.
kusano 7d535a
   */
kusano 7d535a
  {   0,192, 48,240, 12,204, 60,252,  3,195, 51,243, 15,207, 63,255 },
kusano 7d535a
  { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
kusano 7d535a
  {  32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
kusano 7d535a
  { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
kusano 7d535a
  {   8,200, 56,248,  4,196, 52,244, 11,203, 59,251,  7,199, 55,247 },
kusano 7d535a
  { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
kusano 7d535a
  {  40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
kusano 7d535a
  { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
kusano 7d535a
  {   2,194, 50,242, 14,206, 62,254,  1,193, 49,241, 13,205, 61,253 },
kusano 7d535a
  { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
kusano 7d535a
  {  34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
kusano 7d535a
  { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
kusano 7d535a
  {  10,202, 58,250,  6,198, 54,246,  9,201, 57,249,  5,197, 53,245 },
kusano 7d535a
  { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
kusano 7d535a
  {  42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
kusano 7d535a
  { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Declarations for Floyd-Steinberg dithering.
kusano 7d535a
 *
kusano 7d535a
 * Errors are accumulated into the array fserrors[], at a resolution of
kusano 7d535a
 * 1/16th of a pixel count.  The error at a given pixel is propagated
kusano 7d535a
 * to its not-yet-processed neighbors using the standard F-S fractions,
kusano 7d535a
 *		...	(here)	7/16
kusano 7d535a
 *		3/16	5/16	1/16
kusano 7d535a
 * We work left-to-right on even rows, right-to-left on odd rows.
kusano 7d535a
 *
kusano 7d535a
 * We can get away with a single array (holding one row's worth of errors)
kusano 7d535a
 * by using it to store the current row's errors at pixel columns not yet
kusano 7d535a
 * processed, but the next row's errors at columns already processed.  We
kusano 7d535a
 * need only a few extra variables to hold the errors immediately around the
kusano 7d535a
 * current column.  (If we are lucky, those variables are in registers, but
kusano 7d535a
 * even if not, they're probably cheaper to access than array elements are.)
kusano 7d535a
 *
kusano 7d535a
 * The fserrors[] array is indexed [component#][position].
kusano 7d535a
 * We provide (#columns + 2) entries per component; the extra entry at each
kusano 7d535a
 * end saves us from special-casing the first and last pixels.
kusano 7d535a
 *
kusano 7d535a
 * Note: on a wide image, we might not have enough room in a PC's near data
kusano 7d535a
 * segment to hold the error array; so it is allocated with alloc_large.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#if BITS_IN_JSAMPLE == 8
kusano 7d535a
typedef INT16 FSERROR;		/* 16 bits should be enough */
kusano 7d535a
typedef int LOCFSERROR;		/* use 'int' for calculation temps */
kusano 7d535a
#else
kusano 7d535a
typedef INT32 FSERROR;		/* may need more than 16 bits */
kusano 7d535a
typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private subobject */
kusano 7d535a
kusano 7d535a
#define MAX_Q_COMPS 4		/* max components I can handle */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_color_quantizer pub; /* public fields */
kusano 7d535a
kusano 7d535a
  /* Initially allocated colormap is saved here */
kusano 7d535a
  JSAMPARRAY sv_colormap;	/* The color map as a 2-D pixel array */
kusano 7d535a
  int sv_actual;		/* number of entries in use */
kusano 7d535a
kusano 7d535a
  JSAMPARRAY colorindex;	/* Precomputed mapping for speed */
kusano 7d535a
  /* colorindex[i][j] = index of color closest to pixel value j in component i,
kusano 7d535a
   * premultiplied as described above.  Since colormap indexes must fit into
kusano 7d535a
   * JSAMPLEs, the entries of this array will too.
kusano 7d535a
   */
kusano 7d535a
  boolean is_padded;		/* is the colorindex padded for odither? */
kusano 7d535a
kusano 7d535a
  int Ncolors[MAX_Q_COMPS];	/* # of values alloced to each component */
kusano 7d535a
kusano 7d535a
  /* Variables for ordered dithering */
kusano 7d535a
  int row_index;		/* cur row's vertical index in dither matrix */
kusano 7d535a
  ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
kusano 7d535a
kusano 7d535a
  /* Variables for Floyd-Steinberg dithering */
kusano 7d535a
  FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
kusano 7d535a
  boolean on_odd_row;		/* flag to remember which row we are on */
kusano 7d535a
} my_cquantizer;
kusano 7d535a
kusano 7d535a
typedef my_cquantizer * my_cquantize_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Policy-making subroutines for create_colormap and create_colorindex.
kusano 7d535a
 * These routines determine the colormap to be used.  The rest of the module
kusano 7d535a
 * only assumes that the colormap is orthogonal.
kusano 7d535a
 *
kusano 7d535a
 *  * select_ncolors decides how to divvy up the available colors
kusano 7d535a
 *    among the components.
kusano 7d535a
 *  * output_value defines the set of representative values for a component.
kusano 7d535a
 *  * largest_input_value defines the mapping from input values to
kusano 7d535a
 *    representative values for a component.
kusano 7d535a
 * Note that the latter two routines may impose different policies for
kusano 7d535a
 * different components, though this is not currently done.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
kusano 7d535a
/* Determine allocation of desired colors to components, */
kusano 7d535a
/* and fill in Ncolors[] array to indicate choice. */
kusano 7d535a
/* Return value is total number of colors (product of Ncolors[] values). */
kusano 7d535a
{
kusano 7d535a
  int nc = cinfo->out_color_components; /* number of color components */
kusano 7d535a
  int max_colors = cinfo->desired_number_of_colors;
kusano 7d535a
  int total_colors, iroot, i, j;
kusano 7d535a
  boolean changed;
kusano 7d535a
  long temp;
kusano 7d535a
  static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
kusano 7d535a
kusano 7d535a
  /* We can allocate at least the nc'th root of max_colors per component. */
kusano 7d535a
  /* Compute floor(nc'th root of max_colors). */
kusano 7d535a
  iroot = 1;
kusano 7d535a
  do {
kusano 7d535a
    iroot++;
kusano 7d535a
    temp = iroot;		/* set temp = iroot ** nc */
kusano 7d535a
    for (i = 1; i < nc; i++)
kusano 7d535a
      temp *= iroot;
kusano 7d535a
  } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
kusano 7d535a
  iroot--;			/* now iroot = floor(root) */
kusano 7d535a
kusano 7d535a
  /* Must have at least 2 color values per component */
kusano 7d535a
  if (iroot < 2)
kusano 7d535a
    ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp);
kusano 7d535a
kusano 7d535a
  /* Initialize to iroot color values for each component */
kusano 7d535a
  total_colors = 1;
kusano 7d535a
  for (i = 0; i < nc; i++) {
kusano 7d535a
    Ncolors[i] = iroot;
kusano 7d535a
    total_colors *= iroot;
kusano 7d535a
  }
kusano 7d535a
  /* We may be able to increment the count for one or more components without
kusano 7d535a
   * exceeding max_colors, though we know not all can be incremented.
kusano 7d535a
   * Sometimes, the first component can be incremented more than once!
kusano 7d535a
   * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.)
kusano 7d535a
   * In RGB colorspace, try to increment G first, then R, then B.
kusano 7d535a
   */
kusano 7d535a
  do {
kusano 7d535a
    changed = FALSE;
kusano 7d535a
    for (i = 0; i < nc; i++) {
kusano 7d535a
      j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
kusano 7d535a
      /* calculate new total_colors if Ncolors[j] is incremented */
kusano 7d535a
      temp = total_colors / Ncolors[j];
kusano 7d535a
      temp *= Ncolors[j]+1;	/* done in long arith to avoid oflo */
kusano 7d535a
      if (temp > (long) max_colors)
kusano 7d535a
	break;			/* won't fit, done with this pass */
kusano 7d535a
      Ncolors[j]++;		/* OK, apply the increment */
kusano 7d535a
      total_colors = (int) temp;
kusano 7d535a
      changed = TRUE;
kusano 7d535a
    }
kusano 7d535a
  } while (changed);
kusano 7d535a
kusano 7d535a
  return total_colors;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
output_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
kusano 7d535a
/* Return j'th output value, where j will range from 0 to maxj */
kusano 7d535a
/* The output values must fall in 0..MAXJSAMPLE in increasing order */
kusano 7d535a
{
kusano 7d535a
  /* We always provide values 0 and MAXJSAMPLE for each component;
kusano 7d535a
   * any additional values are equally spaced between these limits.
kusano 7d535a
   * (Forcing the upper and lower values to the limits ensures that
kusano 7d535a
   * dithering can't produce a color outside the selected gamut.)
kusano 7d535a
   */
kusano 7d535a
  return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
kusano 7d535a
/* Return largest input value that should map to j'th output value */
kusano 7d535a
/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
kusano 7d535a
{
kusano 7d535a
  /* Breakpoints are halfway between values returned by output_value */
kusano 7d535a
  return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj));
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Create the colormap.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
create_colormap (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  JSAMPARRAY colormap;		/* Created colormap */
kusano 7d535a
  int total_colors;		/* Number of distinct output colors */
kusano 7d535a
  int i,j,k, nci, blksize, blkdist, ptr, val;
kusano 7d535a
kusano 7d535a
  /* Select number of colors for each component */
kusano 7d535a
  total_colors = select_ncolors(cinfo, cquantize->Ncolors);
kusano 7d535a
kusano 7d535a
  /* Report selected color counts */
kusano 7d535a
  if (cinfo->out_color_components == 3)
kusano 7d535a
    TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
kusano 7d535a
	     total_colors, cquantize->Ncolors[0],
kusano 7d535a
	     cquantize->Ncolors[1], cquantize->Ncolors[2]);
kusano 7d535a
  else
kusano 7d535a
    TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
kusano 7d535a
kusano 7d535a
  /* Allocate and fill in the colormap. */
kusano 7d535a
  /* The colors are ordered in the map in standard row-major order, */
kusano 7d535a
  /* i.e. rightmost (highest-indexed) color changes most rapidly. */
kusano 7d535a
kusano 7d535a
  colormap = (*cinfo->mem->alloc_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
     (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);
kusano 7d535a
kusano 7d535a
  /* blksize is number of adjacent repeated entries for a component */
kusano 7d535a
  /* blkdist is distance between groups of identical entries for a component */
kusano 7d535a
  blkdist = total_colors;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < cinfo->out_color_components; i++) {
kusano 7d535a
    /* fill in colormap entries for i'th color component */
kusano 7d535a
    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
kusano 7d535a
    blksize = blkdist / nci;
kusano 7d535a
    for (j = 0; j < nci; j++) {
kusano 7d535a
      /* Compute j'th output value (out of nci) for component */
kusano 7d535a
      val = output_value(cinfo, i, j, nci-1);
kusano 7d535a
      /* Fill in all colormap entries that have this value of this component */
kusano 7d535a
      for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
kusano 7d535a
	/* fill in blksize entries beginning at ptr */
kusano 7d535a
	for (k = 0; k < blksize; k++)
kusano 7d535a
	  colormap[i][ptr+k] = (JSAMPLE) val;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    blkdist = blksize;		/* blksize of this color is blkdist of next */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Save the colormap in private storage,
kusano 7d535a
   * where it will survive color quantization mode changes.
kusano 7d535a
   */
kusano 7d535a
  cquantize->sv_colormap = colormap;
kusano 7d535a
  cquantize->sv_actual = total_colors;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Create the color index table.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
create_colorindex (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  JSAMPROW indexptr;
kusano 7d535a
  int i,j,k, nci, blksize, val, pad;
kusano 7d535a
kusano 7d535a
  /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
kusano 7d535a
   * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
kusano 7d535a
   * This is not necessary in the other dithering modes.  However, we
kusano 7d535a
   * flag whether it was done in case user changes dithering mode.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->dither_mode == JDITHER_ORDERED) {
kusano 7d535a
    pad = MAXJSAMPLE*2;
kusano 7d535a
    cquantize->is_padded = TRUE;
kusano 7d535a
  } else {
kusano 7d535a
    pad = 0;
kusano 7d535a
    cquantize->is_padded = FALSE;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  cquantize->colorindex = (*cinfo->mem->alloc_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
     (JDIMENSION) (MAXJSAMPLE+1 + pad),
kusano 7d535a
     (JDIMENSION) cinfo->out_color_components);
kusano 7d535a
kusano 7d535a
  /* blksize is number of adjacent repeated entries for a component */
kusano 7d535a
  blksize = cquantize->sv_actual;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < cinfo->out_color_components; i++) {
kusano 7d535a
    /* fill in colorindex entries for i'th color component */
kusano 7d535a
    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
kusano 7d535a
    blksize = blksize / nci;
kusano 7d535a
kusano 7d535a
    /* adjust colorindex pointers to provide padding at negative indexes. */
kusano 7d535a
    if (pad)
kusano 7d535a
      cquantize->colorindex[i] += MAXJSAMPLE;
kusano 7d535a
kusano 7d535a
    /* in loop, val = index of current output value, */
kusano 7d535a
    /* and k = largest j that maps to current val */
kusano 7d535a
    indexptr = cquantize->colorindex[i];
kusano 7d535a
    val = 0;
kusano 7d535a
    k = largest_input_value(cinfo, i, 0, nci-1);
kusano 7d535a
    for (j = 0; j <= MAXJSAMPLE; j++) {
kusano 7d535a
      while (j > k)		/* advance val if past boundary */
kusano 7d535a
	k = largest_input_value(cinfo, i, ++val, nci-1);
kusano 7d535a
      /* premultiply so that no multiplication needed in main processing */
kusano 7d535a
      indexptr[j] = (JSAMPLE) (val * blksize);
kusano 7d535a
    }
kusano 7d535a
    /* Pad at both ends if necessary */
kusano 7d535a
    if (pad)
kusano 7d535a
      for (j = 1; j <= MAXJSAMPLE; j++) {
kusano 7d535a
	indexptr[-j] = indexptr[0];
kusano 7d535a
	indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
kusano 7d535a
      }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Create an ordered-dither array for a component having ncolors
kusano 7d535a
 * distinct output values.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(ODITHER_MATRIX_PTR)
kusano 7d535a
make_odither_array (j_decompress_ptr cinfo, int ncolors)
kusano 7d535a
{
kusano 7d535a
  ODITHER_MATRIX_PTR odither;
kusano 7d535a
  int j,k;
kusano 7d535a
  INT32 num,den;
kusano 7d535a
kusano 7d535a
  odither = (ODITHER_MATRIX_PTR)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(ODITHER_MATRIX));
kusano 7d535a
  /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
kusano 7d535a
   * Hence the dither value for the matrix cell with fill order f
kusano 7d535a
   * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
kusano 7d535a
   * On 16-bit-int machine, be careful to avoid overflow.
kusano 7d535a
   */
kusano 7d535a
  den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1));
kusano 7d535a
  for (j = 0; j < ODITHER_SIZE; j++) {
kusano 7d535a
    for (k = 0; k < ODITHER_SIZE; k++) {
kusano 7d535a
      num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k])))
kusano 7d535a
	    * MAXJSAMPLE;
kusano 7d535a
      /* Ensure round towards zero despite C's lack of consistency
kusano 7d535a
       * about rounding negative values in integer division...
kusano 7d535a
       */
kusano 7d535a
      odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
  return odither;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Create the ordered-dither tables.
kusano 7d535a
 * Components having the same number of representative colors may 
kusano 7d535a
 * share a dither table.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
create_odither_tables (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  ODITHER_MATRIX_PTR odither;
kusano 7d535a
  int i, j, nci;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < cinfo->out_color_components; i++) {
kusano 7d535a
    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
kusano 7d535a
    odither = NULL;		/* search for matching prior component */
kusano 7d535a
    for (j = 0; j < i; j++) {
kusano 7d535a
      if (nci == cquantize->Ncolors[j]) {
kusano 7d535a
	odither = cquantize->odither[j];
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    if (odither == NULL)	/* need a new table? */
kusano 7d535a
      odither = make_odither_array(cinfo, nci);
kusano 7d535a
    cquantize->odither[i] = odither;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Map some rows of pixels to the output colormapped representation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
kusano 7d535a
		JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
/* General case, no dithering */
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  JSAMPARRAY colorindex = cquantize->colorindex;
kusano 7d535a
  register int pixcode, ci;
kusano 7d535a
  register JSAMPROW ptrin, ptrout;
kusano 7d535a
  int row;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
  JDIMENSION width = cinfo->output_width;
kusano 7d535a
  register int nc = cinfo->out_color_components;
kusano 7d535a
kusano 7d535a
  for (row = 0; row < num_rows; row++) {
kusano 7d535a
    ptrin = input_buf[row];
kusano 7d535a
    ptrout = output_buf[row];
kusano 7d535a
    for (col = width; col > 0; col--) {
kusano 7d535a
      pixcode = 0;
kusano 7d535a
      for (ci = 0; ci < nc; ci++) {
kusano 7d535a
	pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
kusano 7d535a
      }
kusano 7d535a
      *ptrout++ = (JSAMPLE) pixcode;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
kusano 7d535a
		 JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
/* Fast path for out_color_components==3, no dithering */
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  register int pixcode;
kusano 7d535a
  register JSAMPROW ptrin, ptrout;
kusano 7d535a
  JSAMPROW colorindex0 = cquantize->colorindex[0];
kusano 7d535a
  JSAMPROW colorindex1 = cquantize->colorindex[1];
kusano 7d535a
  JSAMPROW colorindex2 = cquantize->colorindex[2];
kusano 7d535a
  int row;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
  JDIMENSION width = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  for (row = 0; row < num_rows; row++) {
kusano 7d535a
    ptrin = input_buf[row];
kusano 7d535a
    ptrout = output_buf[row];
kusano 7d535a
    for (col = width; col > 0; col--) {
kusano 7d535a
      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
kusano 7d535a
      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
kusano 7d535a
      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
kusano 7d535a
      *ptrout++ = (JSAMPLE) pixcode;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
kusano 7d535a
		     JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
/* General case, with ordered dithering */
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  register JSAMPROW input_ptr;
kusano 7d535a
  register JSAMPROW output_ptr;
kusano 7d535a
  JSAMPROW colorindex_ci;
kusano 7d535a
  int * dither;			/* points to active row of dither matrix */
kusano 7d535a
  int row_index, col_index;	/* current indexes into dither matrix */
kusano 7d535a
  int nc = cinfo->out_color_components;
kusano 7d535a
  int ci;
kusano 7d535a
  int row;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
  JDIMENSION width = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  for (row = 0; row < num_rows; row++) {
kusano 7d535a
    /* Initialize output values to 0 so can process components separately */
kusano 7d535a
    FMEMZERO((void FAR *) output_buf[row],
kusano 7d535a
	     (size_t) (width * SIZEOF(JSAMPLE)));
kusano 7d535a
    row_index = cquantize->row_index;
kusano 7d535a
    for (ci = 0; ci < nc; ci++) {
kusano 7d535a
      input_ptr = input_buf[row] + ci;
kusano 7d535a
      output_ptr = output_buf[row];
kusano 7d535a
      colorindex_ci = cquantize->colorindex[ci];
kusano 7d535a
      dither = cquantize->odither[ci][row_index];
kusano 7d535a
      col_index = 0;
kusano 7d535a
kusano 7d535a
      for (col = width; col > 0; col--) {
kusano 7d535a
	/* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
kusano 7d535a
	 * select output value, accumulate into output code for this pixel.
kusano 7d535a
	 * Range-limiting need not be done explicitly, as we have extended
kusano 7d535a
	 * the colorindex table to produce the right answers for out-of-range
kusano 7d535a
	 * inputs.  The maximum dither is +- MAXJSAMPLE; this sets the
kusano 7d535a
	 * required amount of padding.
kusano 7d535a
	 */
kusano 7d535a
	*output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
kusano 7d535a
	input_ptr += nc;
kusano 7d535a
	output_ptr++;
kusano 7d535a
	col_index = (col_index + 1) & ODITHER_MASK;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Advance row index for next row */
kusano 7d535a
    row_index = (row_index + 1) & ODITHER_MASK;
kusano 7d535a
    cquantize->row_index = row_index;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
kusano 7d535a
		      JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
/* Fast path for out_color_components==3, with ordered dithering */
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  register int pixcode;
kusano 7d535a
  register JSAMPROW input_ptr;
kusano 7d535a
  register JSAMPROW output_ptr;
kusano 7d535a
  JSAMPROW colorindex0 = cquantize->colorindex[0];
kusano 7d535a
  JSAMPROW colorindex1 = cquantize->colorindex[1];
kusano 7d535a
  JSAMPROW colorindex2 = cquantize->colorindex[2];
kusano 7d535a
  int * dither0;		/* points to active row of dither matrix */
kusano 7d535a
  int * dither1;
kusano 7d535a
  int * dither2;
kusano 7d535a
  int row_index, col_index;	/* current indexes into dither matrix */
kusano 7d535a
  int row;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
  JDIMENSION width = cinfo->output_width;
kusano 7d535a
kusano 7d535a
  for (row = 0; row < num_rows; row++) {
kusano 7d535a
    row_index = cquantize->row_index;
kusano 7d535a
    input_ptr = input_buf[row];
kusano 7d535a
    output_ptr = output_buf[row];
kusano 7d535a
    dither0 = cquantize->odither[0][row_index];
kusano 7d535a
    dither1 = cquantize->odither[1][row_index];
kusano 7d535a
    dither2 = cquantize->odither[2][row_index];
kusano 7d535a
    col_index = 0;
kusano 7d535a
kusano 7d535a
    for (col = width; col > 0; col--) {
kusano 7d535a
      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
kusano 7d535a
					dither0[col_index]]);
kusano 7d535a
      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
kusano 7d535a
					dither1[col_index]]);
kusano 7d535a
      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
kusano 7d535a
					dither2[col_index]]);
kusano 7d535a
      *output_ptr++ = (JSAMPLE) pixcode;
kusano 7d535a
      col_index = (col_index + 1) & ODITHER_MASK;
kusano 7d535a
    }
kusano 7d535a
    row_index = (row_index + 1) & ODITHER_MASK;
kusano 7d535a
    cquantize->row_index = row_index;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
kusano 7d535a
		    JSAMPARRAY output_buf, int num_rows)
kusano 7d535a
/* General case, with Floyd-Steinberg dithering */
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  register LOCFSERROR cur;	/* current error or pixel value */
kusano 7d535a
  LOCFSERROR belowerr;		/* error for pixel below cur */
kusano 7d535a
  LOCFSERROR bpreverr;		/* error for below/prev col */
kusano 7d535a
  LOCFSERROR bnexterr;		/* error for below/next col */
kusano 7d535a
  LOCFSERROR delta;
kusano 7d535a
  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
kusano 7d535a
  register JSAMPROW input_ptr;
kusano 7d535a
  register JSAMPROW output_ptr;
kusano 7d535a
  JSAMPROW colorindex_ci;
kusano 7d535a
  JSAMPROW colormap_ci;
kusano 7d535a
  int pixcode;
kusano 7d535a
  int nc = cinfo->out_color_components;
kusano 7d535a
  int dir;			/* 1 for left-to-right, -1 for right-to-left */
kusano 7d535a
  int dirnc;			/* dir * nc */
kusano 7d535a
  int ci;
kusano 7d535a
  int row;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
  JDIMENSION width = cinfo->output_width;
kusano 7d535a
  JSAMPLE *range_limit = cinfo->sample_range_limit;
kusano 7d535a
  SHIFT_TEMPS
kusano 7d535a
kusano 7d535a
  for (row = 0; row < num_rows; row++) {
kusano 7d535a
    /* Initialize output values to 0 so can process components separately */
kusano 7d535a
    FMEMZERO((void FAR *) output_buf[row],
kusano 7d535a
	     (size_t) (width * SIZEOF(JSAMPLE)));
kusano 7d535a
    for (ci = 0; ci < nc; ci++) {
kusano 7d535a
      input_ptr = input_buf[row] + ci;
kusano 7d535a
      output_ptr = output_buf[row];
kusano 7d535a
      if (cquantize->on_odd_row) {
kusano 7d535a
	/* work right to left in this row */
kusano 7d535a
	input_ptr += (width-1) * nc; /* so point to rightmost pixel */
kusano 7d535a
	output_ptr += width-1;
kusano 7d535a
	dir = -1;
kusano 7d535a
	dirnc = -nc;
kusano 7d535a
	errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
kusano 7d535a
      } else {
kusano 7d535a
	/* work left to right in this row */
kusano 7d535a
	dir = 1;
kusano 7d535a
	dirnc = nc;
kusano 7d535a
	errorptr = cquantize->fserrors[ci]; /* => entry before first column */
kusano 7d535a
      }
kusano 7d535a
      colorindex_ci = cquantize->colorindex[ci];
kusano 7d535a
      colormap_ci = cquantize->sv_colormap[ci];
kusano 7d535a
      /* Preset error values: no error propagated to first pixel from left */
kusano 7d535a
      cur = 0;
kusano 7d535a
      /* and no error propagated to row below yet */
kusano 7d535a
      belowerr = bpreverr = 0;
kusano 7d535a
kusano 7d535a
      for (col = width; col > 0; col--) {
kusano 7d535a
	/* cur holds the error propagated from the previous pixel on the
kusano 7d535a
	 * current line.  Add the error propagated from the previous line
kusano 7d535a
	 * to form the complete error correction term for this pixel, and
kusano 7d535a
	 * round the error term (which is expressed * 16) to an integer.
kusano 7d535a
	 * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
kusano 7d535a
	 * for either sign of the error value.
kusano 7d535a
	 * Note: errorptr points to *previous* column's array entry.
kusano 7d535a
	 */
kusano 7d535a
	cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
kusano 7d535a
	/* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
kusano 7d535a
	 * The maximum error is +- MAXJSAMPLE; this sets the required size
kusano 7d535a
	 * of the range_limit array.
kusano 7d535a
	 */
kusano 7d535a
	cur += GETJSAMPLE(*input_ptr);
kusano 7d535a
	cur = GETJSAMPLE(range_limit[cur]);
kusano 7d535a
	/* Select output value, accumulate into output code for this pixel */
kusano 7d535a
	pixcode = GETJSAMPLE(colorindex_ci[cur]);
kusano 7d535a
	*output_ptr += (JSAMPLE) pixcode;
kusano 7d535a
	/* Compute actual representation error at this pixel */
kusano 7d535a
	/* Note: we can do this even though we don't have the final */
kusano 7d535a
	/* pixel code, because the colormap is orthogonal. */
kusano 7d535a
	cur -= GETJSAMPLE(colormap_ci[pixcode]);
kusano 7d535a
	/* Compute error fractions to be propagated to adjacent pixels.
kusano 7d535a
	 * Add these into the running sums, and simultaneously shift the
kusano 7d535a
	 * next-line error sums left by 1 column.
kusano 7d535a
	 */
kusano 7d535a
	bnexterr = cur;
kusano 7d535a
	delta = cur * 2;
kusano 7d535a
	cur += delta;		/* form error * 3 */
kusano 7d535a
	errorptr[0] = (FSERROR) (bpreverr + cur);
kusano 7d535a
	cur += delta;		/* form error * 5 */
kusano 7d535a
	bpreverr = belowerr + cur;
kusano 7d535a
	belowerr = bnexterr;
kusano 7d535a
	cur += delta;		/* form error * 7 */
kusano 7d535a
	/* At this point cur contains the 7/16 error value to be propagated
kusano 7d535a
	 * to the next pixel on the current line, and all the errors for the
kusano 7d535a
	 * next line have been shifted over. We are therefore ready to move on.
kusano 7d535a
	 */
kusano 7d535a
	input_ptr += dirnc;	/* advance input ptr to next column */
kusano 7d535a
	output_ptr += dir;	/* advance output ptr to next column */
kusano 7d535a
	errorptr += dir;	/* advance errorptr to current column */
kusano 7d535a
      }
kusano 7d535a
      /* Post-loop cleanup: we must unload the final error value into the
kusano 7d535a
       * final fserrors[] entry.  Note we need not unload belowerr because
kusano 7d535a
       * it is for the dummy column before or after the actual array.
kusano 7d535a
       */
kusano 7d535a
      errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
kusano 7d535a
    }
kusano 7d535a
    cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Allocate workspace for Floyd-Steinberg errors.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
alloc_fs_workspace (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  size_t arraysize;
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
kusano 7d535a
  for (i = 0; i < cinfo->out_color_components; i++) {
kusano 7d535a
    cquantize->fserrors[i] = (FSERRPTR)
kusano 7d535a
      (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for one-pass color quantization.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
kusano 7d535a
  size_t arraysize;
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  /* Install my colormap. */
kusano 7d535a
  cinfo->colormap = cquantize->sv_colormap;
kusano 7d535a
  cinfo->actual_number_of_colors = cquantize->sv_actual;
kusano 7d535a
kusano 7d535a
  /* Initialize for desired dithering mode. */
kusano 7d535a
  switch (cinfo->dither_mode) {
kusano 7d535a
  case JDITHER_NONE:
kusano 7d535a
    if (cinfo->out_color_components == 3)
kusano 7d535a
      cquantize->pub.color_quantize = color_quantize3;
kusano 7d535a
    else
kusano 7d535a
      cquantize->pub.color_quantize = color_quantize;
kusano 7d535a
    break;
kusano 7d535a
  case JDITHER_ORDERED:
kusano 7d535a
    if (cinfo->out_color_components == 3)
kusano 7d535a
      cquantize->pub.color_quantize = quantize3_ord_dither;
kusano 7d535a
    else
kusano 7d535a
      cquantize->pub.color_quantize = quantize_ord_dither;
kusano 7d535a
    cquantize->row_index = 0;	/* initialize state for ordered dither */
kusano 7d535a
    /* If user changed to ordered dither from another mode,
kusano 7d535a
     * we must recreate the color index table with padding.
kusano 7d535a
     * This will cost extra space, but probably isn't very likely.
kusano 7d535a
     */
kusano 7d535a
    if (! cquantize->is_padded)
kusano 7d535a
      create_colorindex(cinfo);
kusano 7d535a
    /* Create ordered-dither tables if we didn't already. */
kusano 7d535a
    if (cquantize->odither[0] == NULL)
kusano 7d535a
      create_odither_tables(cinfo);
kusano 7d535a
    break;
kusano 7d535a
  case JDITHER_FS:
kusano 7d535a
    cquantize->pub.color_quantize = quantize_fs_dither;
kusano 7d535a
    cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
kusano 7d535a
    /* Allocate Floyd-Steinberg workspace if didn't already. */
kusano 7d535a
    if (cquantize->fserrors[0] == NULL)
kusano 7d535a
      alloc_fs_workspace(cinfo);
kusano 7d535a
    /* Initialize the propagated errors to zero. */
kusano 7d535a
    arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
kusano 7d535a
    for (i = 0; i < cinfo->out_color_components; i++)
kusano 7d535a
      FMEMZERO((void FAR *) cquantize->fserrors[i], arraysize);
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up at the end of the pass.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
finish_pass_1_quant (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work in 1-pass case */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Switch to a new external colormap between output passes.
kusano 7d535a
 * Shouldn't get to this module!
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
new_color_map_1_quant (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  ERREXIT(cinfo, JERR_MODE_CHANGE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Module initialization routine for 1-pass color quantization.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_1pass_quantizer (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_cquantize_ptr cquantize;
kusano 7d535a
kusano 7d535a
  cquantize = (my_cquantize_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_cquantizer));
kusano 7d535a
  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
kusano 7d535a
  cquantize->pub.start_pass = start_pass_1_quant;
kusano 7d535a
  cquantize->pub.finish_pass = finish_pass_1_quant;
kusano 7d535a
  cquantize->pub.new_color_map = new_color_map_1_quant;
kusano 7d535a
  cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
kusano 7d535a
  cquantize->odither[0] = NULL;	/* Also flag odither arrays not allocated */
kusano 7d535a
kusano 7d535a
  /* Make sure my internal arrays won't overflow */
kusano 7d535a
  if (cinfo->out_color_components > MAX_Q_COMPS)
kusano 7d535a
    ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
kusano 7d535a
  /* Make sure colormap indexes can be represented by JSAMPLEs */
kusano 7d535a
  if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
kusano 7d535a
    ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1);
kusano 7d535a
kusano 7d535a
  /* Create the colormap and color index table. */
kusano 7d535a
  create_colormap(cinfo);
kusano 7d535a
  create_colorindex(cinfo);
kusano 7d535a
kusano 7d535a
  /* Allocate Floyd-Steinberg workspace now if requested.
kusano 7d535a
   * We do this now since it is FAR storage and may affect the memory
kusano 7d535a
   * manager's space calculations.  If the user changes to FS dither
kusano 7d535a
   * mode in a later pass, we will allocate the space then, and will
kusano 7d535a
   * possibly overrun the max_memory_to_use setting.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->dither_mode == JDITHER_FS)
kusano 7d535a
    alloc_fs_workspace(cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* QUANT_1PASS_SUPPORTED */