jquant1.c revision cc7150e281999ac2642e21f13e2c160f68b1d675
12cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
22cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * jquant1.c
32cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
4cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
52cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * This file is part of the Independent JPEG Group's software.
62cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * For conditions of distribution and use, see the accompanying README file.
72cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
82cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * This file contains 1-pass color quantization (color mapping) routines.
92cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * These routines are invoked via the methods color_quantize
102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * and color_quant_init/term.
112cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "jinclude.h"
142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#ifdef QUANT_1PASS_SUPPORTED
162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
172cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
194a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * The main purpose of 1-pass quantization is to provide a fast, if not very
204a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * high quality, colormapped output capability.  A 2-pass quantizer usually
214a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * gives better visual quality; however, for quantized grayscale output this
224a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * quantizer is perfectly adequate.  Dithering is highly recommended with this
234a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * quantizer, though you can turn it off if you really want to.
242cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
254a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * This implementation quantizes in the output colorspace.  This has a couple
264a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * of disadvantages: each pixel must be individually color-converted, and if
274a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * the color conversion includes gamma correction then quantization is done in
284a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * a nonlinear space, which is less desirable.  The major advantage is that
294a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * with the usual output color spaces (RGB, grayscale) an orthogonal grid of
304a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * representative colors can be used, thus permitting the very simple and fast
314a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * color lookup scheme used here.  The standard JPEG colorspace (YCbCr) cannot
324a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * be effectively handled this way, because only about a quarter of an
334a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * orthogonal grid would fall within the gamut of realizable colors.  Another
344a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * advantage is that when the user wants quantized grayscale output from a
354a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * color JPEG file, this quantizer can provide a high-quality result with no
364a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * special hacking.
372cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
384a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * The gamma-correction problem could be eliminated by adjusting the grid
394a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * spacing to counteract the gamma correction applied by color_convert.
404a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * At this writing, gamma correction is not implemented by jdcolor, so
414a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * nothing is done here.
424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *
434a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * In 1-pass quantization the colormap must be chosen in advance of seeing the
444a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * image.  We use a map consisting of all combinations of Ncolors[i] color
454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * values for the i'th component.  The Ncolors[] values are chosen so that
464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * their product, the total number of colors, is no more than that requested.
474a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * (In most cases, the product will be somewhat less.)
484a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *
494a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Since the colormap is orthogonal, the representative value for each color
504a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * component can be determined without considering the other components;
514a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * then these indexes can be combined into a colormap index by a standard
524a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * N-dimensional-array-subscript calculation.  Most of the arithmetic involved
534a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * can be precalculated and stored in the lookup table colorindex[].
544a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * colorindex[i][j] maps pixel value j in component i to the nearest
554a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * representative value (grid plane) for that component; this index is
564a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * multiplied by the array stride for component i, so that the
574a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * index of the colormap entry closest to a given pixel value is just
584a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *    sum( colorindex[component-number][pixel-component-value] )
594a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Aside from being fast, this scheme allows for variable spacing between
604a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * representative values with no additional lookup cost.
612cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
622cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
632cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane#define MAX_COMPONENTS 4	/* max components I can handle */
652cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
662cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanestatic JSAMPARRAY colormap;	/* The actual color map */
672cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* colormap[i][j] = value of i'th color component for output pixel value j */
682cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
692cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanestatic JSAMPARRAY colorindex;	/* Precomputed mapping for speed */
702cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* colorindex[i][j] = index of color closest to pixel value j in component i,
714a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * premultiplied as described above.  Since colormap indexes must fit into
724a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * JSAMPLEs, the entries of this array will too.
734a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */
744a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
754a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lanestatic JSAMPARRAY input_buffer;	/* color conversion workspace */
764a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Since our input data is presented in the JPEG colorspace, we have to call
774a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * color_convert to get it into the output colorspace.  input_buffer is a
784a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * one-row-high workspace for the result of color_convert.
792cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
802cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
812cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
822cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* Declarations for Floyd-Steinberg dithering.
834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *
84cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * Errors are accumulated into the array fserrors[], at a resolution of
85cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * 1/16th of a pixel count.  The error at a given pixel is propagated
86cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * to its not-yet-processed neighbors using the standard F-S fractions,
872cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *		...	(here)	7/16
882cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *		3/16	5/16	1/16
892cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * We work left-to-right on even rows, right-to-left on odd rows.
902cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
91cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * We can get away with a single array (holding one row's worth of errors)
92cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * by using it to store the current row's errors at pixel columns not yet
93cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * processed, but the next row's errors at columns already processed.  We
94cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * need only a few extra variables to hold the errors immediately around the
95cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * current column.  (If we are lucky, those variables are in registers, but
96cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * even if not, they're probably cheaper to access than array elements are.)
97cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane *
98cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * The fserrors[] array is indexed [component#][position].
994a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * We provide (#columns + 2) entries per component; the extra entry at each
1004a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * end saves us from special-casing the first and last pixels.
1014a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *
1022cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Note: on a wide image, we might not have enough room in a PC's near data
103cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * segment to hold the error array; so it is allocated with alloc_medium.
1042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
1052cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
1062cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#ifdef EIGHT_BIT_SAMPLES
107bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lanetypedef INT16 FSERROR;		/* 16 bits should be enough */
108cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lanetypedef int LOCFSERROR;		/* use 'int' for calculation temps */
1092cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#else
110cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lanetypedef INT32 FSERROR;		/* may need more than 16 bits */
111cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lanetypedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
1122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#endif
1132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
1142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanetypedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
1152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
116cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lanestatic FSERRPTR fserrors[MAX_COMPONENTS]; /* accumulated errors */
1172cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanestatic boolean on_odd_row;	/* flag to remember which row we are on */
1182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
1192cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
1202cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
1214a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Policy-making subroutines for color_quant_init: these routines determine
1224a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * the colormap to be used.  The rest of the module only assumes that the
1234a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * colormap is orthogonal.
1244a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *
1254a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *  * select_ncolors decides how to divvy up the available colors
1264a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *    among the components.
1274a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *  * output_value defines the set of representative values for a component.
1284a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *  * largest_input_value defines the mapping from input values to
1294a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *    representative values for a component.
1304a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Note that the latter two routines may impose different policies for
1314a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * different components, though this is not currently done.
1324a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */
1334a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
1344a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
1354a6b7303643714d495b9d26742d8a156fd120936Thomas G. LaneLOCAL int
1364a6b7303643714d495b9d26742d8a156fd120936Thomas G. Laneselect_ncolors (decompress_info_ptr cinfo, int Ncolors[])
1374a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Determine allocation of desired colors to components, */
1384a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* and fill in Ncolors[] array to indicate choice. */
1394a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Return value is total number of colors (product of Ncolors[] values). */
1404a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{
1414a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int nc = cinfo->color_out_comps; /* number of color components */
1424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int max_colors = cinfo->desired_number_of_colors;
1434a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int total_colors, iroot, i;
1444a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  long temp;
1454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  boolean changed;
1464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
1474a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* We can allocate at least the nc'th root of max_colors per component. */
1484a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Compute floor(nc'th root of max_colors). */
1494a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  iroot = 1;
1504a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  do {
1514a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    iroot++;
1524a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    temp = iroot;		/* set temp = iroot ** nc */
1534a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    for (i = 1; i < nc; i++)
1544a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      temp *= iroot;
1554a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
1564a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  iroot--;			/* now iroot = floor(root) */
1574a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
1584a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Must have at least 2 color values per component */
1594a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  if (iroot < 2)
1604a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    ERREXIT1(cinfo->emethods, "Cannot quantize to fewer than %d colors",
1614a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	     (int) temp);
1624a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
1634a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  if (cinfo->out_color_space == CS_RGB && nc == 3) {
1644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* We provide a special policy for quantizing in RGB space.
1654a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * If 256 colors are requested, we allocate 8 red, 8 green, 4 blue levels;
1664a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * this corresponds to the common 3/3/2-bit scheme.  For other totals,
1674a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * the counts are set so that the number of colors allocated to each
1684a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * component are roughly in the proportion R 3, G 4, B 2.
1694a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * For low color counts, it's easier to hardwire the optimal choices
1704a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * than try to tweak the algorithm to generate them.
1714a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     */
1724a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    if (max_colors == 256) {
1734a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[0] = 8;  Ncolors[1] = 8;  Ncolors[2] = 4;
1744a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      return 256;
1754a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    }
1764a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    if (max_colors < 12) {
1774a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      /* Fixed mapping for 8 colors */
1784a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[0] = Ncolors[1] = Ncolors[2] = 2;
1794a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } else if (max_colors < 18) {
1804a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      /* Fixed mapping for 12 colors */
1814a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[0] = 2;  Ncolors[1] = 3;  Ncolors[2] = 2;
1824a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } else if (max_colors < 24) {
1834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      /* Fixed mapping for 18 colors */
1844a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[0] = 3;  Ncolors[1] = 3;  Ncolors[2] = 2;
1854a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } else if (max_colors < 27) {
1864a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      /* Fixed mapping for 24 colors */
1874a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[0] = 3;  Ncolors[1] = 4;  Ncolors[2] = 2;
1884a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } else if (max_colors < 36) {
1894a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      /* Fixed mapping for 27 colors */
1904a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[0] = 3;  Ncolors[1] = 3;  Ncolors[2] = 3;
1914a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } else {
1924a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      /* these weights are readily derived with a little algebra */
1934a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[0] = (iroot * 266) >> 8; /* R weight is 1.0400 */
1944a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[1] = (iroot * 355) >> 8; /* G weight is 1.3867 */
1954a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[2] = (iroot * 177) >> 8; /* B weight is 0.6934 */
1964a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    }
1974a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    total_colors = Ncolors[0] * Ncolors[1] * Ncolors[2];
1984a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* The above computation produces "floor" values, so we may be able to
1994a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * increment the count for one or more components without exceeding
2004a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * max_colors.  We try in the order B, G, R.
2014a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     */
2024a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    do {
2034a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      changed = FALSE;
2044a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      for (i = 2; i >= 0; i--) {
2054a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	/* calculate new total_colors if Ncolors[i] is incremented */
2064a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	temp = total_colors / Ncolors[i];
2074a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	temp *= Ncolors[i]+1;	/* done in long arith to avoid oflo */
2084a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	if (temp <= (long) max_colors) {
2094a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	  Ncolors[i]++;		/* OK, apply the increment */
2104a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	  total_colors = (int) temp;
2114a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	  changed = TRUE;
2124a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	}
2134a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      }
2144a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } while (changed);		/* loop until no increment is possible */
2154a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  } else {
2164a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* For any colorspace besides RGB, treat all the components equally. */
2174a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2184a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* Initialize to iroot color values for each component */
2194a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    total_colors = 1;
2204a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    for (i = 0; i < nc; i++) {
2214a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[i] = iroot;
2224a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      total_colors *= iroot;
2234a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    }
2244a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* We may be able to increment the count for one or more components without
2254a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     * exceeding max_colors, though we know not all can be incremented.
2264a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane     */
2274a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    for (i = 0; i < nc; i++) {
2284a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      /* calculate new total_colors if Ncolors[i] is incremented */
2294a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      temp = total_colors / Ncolors[i];
2304a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      temp *= Ncolors[i]+1;	/* done in long arith to avoid oflo */
2314a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      if (temp > (long) max_colors)
2324a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	break;			/* won't fit, done */
2334a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      Ncolors[i]++;		/* OK, apply the increment */
2344a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      total_colors = (int) temp;
2354a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    }
2364a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  }
2374a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2384a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  return total_colors;
2394a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane}
2404a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2414a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2424a6b7303643714d495b9d26742d8a156fd120936Thomas G. LaneLOCAL int
2434a6b7303643714d495b9d26742d8a156fd120936Thomas G. Laneoutput_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
2444a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Return j'th output value, where j will range from 0 to maxj */
2454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* The output values must fall in 0..MAXJSAMPLE in increasing order */
2464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{
2474a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* We always provide values 0 and MAXJSAMPLE for each component;
2484a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane   * any additional values are equally spaced between these limits.
2494a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane   * (Forcing the upper and lower values to the limits ensures that
2504a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane   * dithering can't produce a color outside the selected gamut.)
2514a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane   */
25288aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane  return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
2534a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane}
2544a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2554a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2564a6b7303643714d495b9d26742d8a156fd120936Thomas G. LaneLOCAL int
2574a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lanelargest_input_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
2584a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Return largest input value that should map to j'th output value */
2594a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
2604a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{
2614a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Breakpoints are halfway between values returned by output_value */
26288aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane  return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj));
2634a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane}
2644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2654a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2664a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/*
2672cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Initialize for one-pass color quantization.
2682cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
2692cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2702cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneMETHODDEF void
2712cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanecolor_quant_init (decompress_info_ptr cinfo)
2722cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
2734a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int total_colors;		/* Number of distinct output colors */
2744a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int Ncolors[MAX_COMPONENTS];	/* # of values alloced to each component */
2754a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int i,j,k, nci, blksize, blkdist, ptr, val;
2762cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2774a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Make sure my internal arrays won't overflow */
2784a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  if (cinfo->num_components > MAX_COMPONENTS ||
2794a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      cinfo->color_out_comps > MAX_COMPONENTS)
2802cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    ERREXIT1(cinfo->emethods, "Cannot quantize more than %d color components",
2812cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane	     MAX_COMPONENTS);
2824a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Make sure colormap indexes can be represented by JSAMPLEs */
2834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
2842cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors",
2854a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	     MAXJSAMPLE+1);
2862cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2874a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Select number of colors for each component */
2884a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  total_colors = select_ncolors(cinfo, Ncolors);
2892cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2902cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* Report selected color counts */
2912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  if (cinfo->color_out_comps == 3)
2922cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    TRACEMS4(cinfo->emethods, 1, "Quantizing to %d = %d*%d*%d colors",
2932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane	     total_colors, Ncolors[0], Ncolors[1], Ncolors[2]);
2942cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  else
2952cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    TRACEMS1(cinfo->emethods, 1, "Quantizing to %d colors", total_colors);
2962cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2972cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* Allocate and fill in the colormap and color index. */
2982cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* The colors are ordered in the map in standard row-major order, */
2992cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* i.e. rightmost (highest-indexed) color changes most rapidly. */
3002cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3012cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  colormap = (*cinfo->emethods->alloc_small_sarray)
3022cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane		((long) total_colors, (long) cinfo->color_out_comps);
3032cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  colorindex = (*cinfo->emethods->alloc_small_sarray)
3042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane		((long) (MAXJSAMPLE+1), (long) cinfo->color_out_comps);
3052cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3062cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* blksize is number of adjacent repeated entries for a component */
3072cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* blkdist is distance between groups of identical entries for a component */
3082cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  blkdist = total_colors;
3092cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  for (i = 0; i < cinfo->color_out_comps; i++) {
3112cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    /* fill in colormap entries for i'th color component */
3122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    nci = Ncolors[i];		/* # of distinct values for this color */
3132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    blksize = blkdist / nci;
3142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    for (j = 0; j < nci; j++) {
315bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane      /* Compute j'th output value (out of nci) for component */
3164a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      val = output_value(cinfo, i, j, nci-1);
317bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane      /* Fill in all colormap entries that have this value of this component */
3182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
3192cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane	/* fill in blksize entries beginning at ptr */
3202cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane	for (k = 0; k < blksize; k++)
321bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane	  colormap[i][ptr+k] = (JSAMPLE) val;
3222cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      }
3232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    }
3242cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    blkdist = blksize;		/* blksize of this color is blkdist of next */
3252cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3262cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    /* fill in colorindex entries for i'th color component */
3274a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* in loop, val = index of current output value, */
3284a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* and k = largest j that maps to current val */
3294a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    val = 0;
3304a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    k = largest_input_value(cinfo, i, 0, nci-1);
3312cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    for (j = 0; j <= MAXJSAMPLE; j++) {
3324a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      while (j > k)		/* advance val if past boundary */
3334a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	k = largest_input_value(cinfo, i, ++val, nci-1);
3342cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      /* premultiply so that no multiplication needed in main processing */
335bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane      colorindex[i][j] = (JSAMPLE) (val * blksize);
3362cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    }
3372cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
3382cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3394a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Pass the colormap to the output module. */
3404a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* NB: the output module may continue to use the colormap until shutdown. */
3414a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  cinfo->colormap = colormap;
3424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  cinfo->actual_number_of_colors = total_colors;
3432cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  (*cinfo->methods->put_color_map) (cinfo, total_colors, colormap);
3442cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Allocate workspace to hold one row of color-converted data */
3464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  input_buffer = (*cinfo->emethods->alloc_small_sarray)
3474a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane			(cinfo->image_width, (long) cinfo->color_out_comps);
3484a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
3492cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* Allocate Floyd-Steinberg workspace if necessary */
3502cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  if (cinfo->use_dithering) {
3514a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    size_t arraysize = (size_t) ((cinfo->image_width + 2L) * SIZEOF(FSERROR));
3522cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3534a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    for (i = 0; i < cinfo->color_out_comps; i++) {
354cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      fserrors[i] = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
355cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      /* Initialize the propagated errors to zero. */
356cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      jzero_far((void FAR *) fserrors[i], arraysize);
3574a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    }
3582cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    on_odd_row = FALSE;
3592cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
3604a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane}
3614a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
3624a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
3634a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/*
3644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Subroutines for color conversion methods.
3654a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */
3662cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3674a6b7303643714d495b9d26742d8a156fd120936Thomas G. LaneLOCAL void
3684a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lanedo_color_conversion (decompress_info_ptr cinfo, JSAMPIMAGE input_data, int row)
3694a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Convert the indicated row of the input data into output colorspace */
3704a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* in input_buffer.  This requires a little trickery since color_convert */
3714a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* expects to deal with 3-D arrays; fortunately we can fake it out */
3724a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* at fairly low cost. */
3734a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{
3744a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  short ci;
3754a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  JSAMPARRAY input_hack[MAX_COMPONENTS];
3764a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  JSAMPARRAY output_hack[MAX_COMPONENTS];
3774a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
3784a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* create JSAMPIMAGE pointing at specified row of input_data */
3794a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  for (ci = 0; ci < cinfo->num_components; ci++)
3804a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    input_hack[ci] = input_data[ci] + row;
3814a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Create JSAMPIMAGE pointing at input_buffer */
3824a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  for (ci = 0; ci < cinfo->color_out_comps; ci++)
3834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    output_hack[ci] = &(input_buffer[ci]);
3844a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
3854a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  (*cinfo->methods->color_convert) (cinfo, 1, cinfo->image_width,
3864a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane				    input_hack, output_hack);
3872cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
3882cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3892cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3902cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
3912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Map some rows of pixels to the output colormapped representation.
3922cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
3932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3942cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneMETHODDEF void
3952cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanecolor_quantize (decompress_info_ptr cinfo, int num_rows,
3962cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane		JSAMPIMAGE input_data, JSAMPARRAY output_data)
3972cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* General case, no dithering */
3982cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
3992cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  register int pixcode, ci;
4004a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  register JSAMPROW ptrout;
4012cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  register long col;
4024a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int row;
4034a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  long width = cinfo->image_width;
4042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  register int nc = cinfo->color_out_comps;
4052cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4062cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  for (row = 0; row < num_rows; row++) {
4074a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    do_color_conversion(cinfo, input_data, row);
4084a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    ptrout = output_data[row];
4094a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    for (col = 0; col < width; col++) {
4102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      pixcode = 0;
4112cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      for (ci = 0; ci < nc; ci++) {
4122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane	pixcode += GETJSAMPLE(colorindex[ci]
4134a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane			      [GETJSAMPLE(input_buffer[ci][col])]);
4142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      }
4154a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      *ptrout++ = (JSAMPLE) pixcode;
4162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    }
4172cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
4182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
4192cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4202cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4212cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneMETHODDEF void
4222cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanecolor_quantize3 (decompress_info_ptr cinfo, int num_rows,
4232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane		 JSAMPIMAGE input_data, JSAMPARRAY output_data)
4242cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* Fast path for color_out_comps==3, no dithering */
4252cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
4262cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  register int pixcode;
4272cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  register JSAMPROW ptr0, ptr1, ptr2, ptrout;
4282cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  register long col;
4294a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int row;
430cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  JSAMPROW colorindex0 = colorindex[0];
431cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  JSAMPROW colorindex1 = colorindex[1];
432cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  JSAMPROW colorindex2 = colorindex[2];
4334a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  long width = cinfo->image_width;
4342cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4352cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  for (row = 0; row < num_rows; row++) {
4364a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    do_color_conversion(cinfo, input_data, row);
4374a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    ptr0 = input_buffer[0];
4384a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    ptr1 = input_buffer[1];
4394a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    ptr2 = input_buffer[2];
4402cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    ptrout = output_data[row];
4412cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    for (col = width; col > 0; col--) {
442cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptr0++)]);
443cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptr1++)]);
444cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptr2++)]);
445bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane      *ptrout++ = (JSAMPLE) pixcode;
4462cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    }
4472cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
4482cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
4492cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4502cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4512cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneMETHODDEF void
4522cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanecolor_quantize_dither (decompress_info_ptr cinfo, int num_rows,
4532cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane		       JSAMPIMAGE input_data, JSAMPARRAY output_data)
4542cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* General case, with Floyd-Steinberg dithering */
4552cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
456cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  register LOCFSERROR cur;	/* current error or pixel value */
457cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  LOCFSERROR belowerr;		/* error for pixel below cur */
458cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  LOCFSERROR bpreverr;		/* error for below/prev col */
459cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  LOCFSERROR bnexterr;		/* error for below/next col */
460cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  LOCFSERROR delta;
461cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
4624a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  register JSAMPROW input_ptr;
4634a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  register JSAMPROW output_ptr;
4644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  JSAMPROW colorindex_ci;
4654a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  JSAMPROW colormap_ci;
466cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  int pixcode;
4674a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int dir;			/* 1 for left-to-right, -1 for right-to-left */
4684a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int ci;
4694a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int nc = cinfo->color_out_comps;
4704a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  int row;
4714a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  long col_counter;
4724a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  long width = cinfo->image_width;
473cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  JSAMPLE *range_limit = cinfo->sample_range_limit;
47488aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane  SHIFT_TEMPS
4752cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4762cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  for (row = 0; row < num_rows; row++) {
4774a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    do_color_conversion(cinfo, input_data, row);
4784a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* Initialize output values to 0 so can process components separately */
4794a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    jzero_far((void FAR *) output_data[row],
4804a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	      (size_t) (width * SIZEOF(JSAMPLE)));
4814a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    for (ci = 0; ci < nc; ci++) {
482cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      input_ptr = input_buffer[ci];
483cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      output_ptr = output_data[row];
4844a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      if (on_odd_row) {
4854a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	/* work right to left in this row */
486cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	input_ptr += width - 1;	/* so point to rightmost pixel */
487cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	output_ptr += width - 1;
4884a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	dir = -1;
489cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	errorptr = fserrors[ci] + (width+1); /* point to entry after last column */
4904a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      } else {
4914a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	/* work left to right in this row */
4924a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	dir = 1;
493cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	errorptr = fserrors[ci]; /* point to entry before first real column */
4942cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      }
4954a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      colorindex_ci = colorindex[ci];
4964a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      colormap_ci = colormap[ci];
497cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      /* Preset error values: no error propagated to first pixel from left */
498cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      cur = 0;
499cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      /* and no error propagated to row below yet */
500cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      belowerr = bpreverr = 0;
501cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane
5024a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane      for (col_counter = width; col_counter > 0; col_counter--) {
503cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	/* cur holds the error propagated from the previous pixel on the
504cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * current line.  Add the error propagated from the previous line
505cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * to form the complete error correction term for this pixel, and
506cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * round the error term (which is expressed * 16) to an integer.
50788aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane	 * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
50888aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane	 * for either sign of the error value.
509cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * Note: errorptr points to *previous* column's array entry.
51088aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane	 */
511cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
512cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	/* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
513cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * The maximum error is +- MAXJSAMPLE; this sets the required size
514cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * of the range_limit array.
51588aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane	 */
516cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	cur += GETJSAMPLE(*input_ptr);
517cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	cur = GETJSAMPLE(range_limit[cur]);
5184a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	/* Select output value, accumulate into output code for this pixel */
519cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	pixcode = GETJSAMPLE(colorindex_ci[cur]);
520cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	*output_ptr += (JSAMPLE) pixcode;
5214a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	/* Compute actual representation error at this pixel */
522cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	/* Note: we can do this even though we don't have the final */
523cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	/* pixel code, because the colormap is orthogonal. */
524cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	cur -= GETJSAMPLE(colormap_ci[pixcode]);
525cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	/* Compute error fractions to be propagated to adjacent pixels.
526cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * Add these into the running sums, and simultaneously shift the
527cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * next-line error sums left by 1 column.
528cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 */
529cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	bnexterr = cur;
530cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	delta = cur * 2;
531cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	cur += delta;		/* form error * 3 */
532cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	errorptr[0] = (FSERROR) (bpreverr + cur);
533cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	cur += delta;		/* form error * 5 */
534cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	bpreverr = belowerr + cur;
535cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	belowerr = bnexterr;
536cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	cur += delta;		/* form error * 7 */
537cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	/* At this point cur contains the 7/16 error value to be propagated
538cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * to the next pixel on the current line, and all the errors for the
539cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 * next line have been shifted over. We are therefore ready to move on.
540cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	 */
5414a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	input_ptr += dir;	/* advance input ptr to next column */
5424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane	output_ptr += dir;	/* advance output ptr to next column */
543cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane	errorptr += dir;	/* advance errorptr to current column */
5442cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      }
545cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      /* Post-loop cleanup: we must unload the final error value into the
546cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane       * final fserrors[] entry.  Note we need not unload belowerr because
547cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane       * it is for the dummy column before or after the actual array.
548cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane       */
549cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
5502cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    }
5514a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    on_odd_row = (on_odd_row ? FALSE : TRUE);
5522cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
5532cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
5542cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5552cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5562cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
5572cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Finish up at the end of the file.
5582cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
5592cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5602cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneMETHODDEF void
5612cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanecolor_quant_term (decompress_info_ptr cinfo)
5622cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
5634a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* no work (we let free_all release the workspace) */
5644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* Note that we *mustn't* free the colormap before free_all, */
5654a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  /* since output module may use it! */
5662cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
5672cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5682cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5692cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
5702cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Prescan some rows of pixels.
5712cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Not used in one-pass case.
5722cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
5732cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5742cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneMETHODDEF void
5752cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanecolor_quant_prescan (decompress_info_ptr cinfo, int num_rows,
5764a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane		     JSAMPIMAGE image_data, JSAMPARRAY workspace)
5772cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
5782cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  ERREXIT(cinfo->emethods, "Should not get here!");
5792cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
5802cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5812cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5822cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
5832cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Do two-pass quantization.
5842cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Not used in one-pass case.
5852cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
5862cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5872cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneMETHODDEF void
5882cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanecolor_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method)
5892cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
5902cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  ERREXIT(cinfo->emethods, "Should not get here!");
5912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
5922cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5942cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
5952cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * The method selection routine for 1-pass color quantization.
5962cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
5972cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5982cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. LaneGLOBAL void
5992cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lanejsel1quantize (decompress_info_ptr cinfo)
6002cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
6012cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  if (! cinfo->two_pass_quantize) {
6022cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    cinfo->methods->color_quant_init = color_quant_init;
6032cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    if (cinfo->use_dithering) {
6042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      cinfo->methods->color_quantize = color_quantize_dither;
6052cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    } else {
6062cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      if (cinfo->color_out_comps == 3)
6072cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane	cinfo->methods->color_quantize = color_quantize3;
6082cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane      else
6092cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane	cinfo->methods->color_quantize = color_quantize;
6102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    }
6112cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    cinfo->methods->color_quant_prescan = color_quant_prescan;
6122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    cinfo->methods->color_quant_doit = color_quant_doit;
6132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    cinfo->methods->color_quant_term = color_quant_term;
6142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
6152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
6162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
6172cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#endif /* QUANT_1PASS_SUPPORTED */
618