home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / viewers / hv12 / src / jquant1.c < prev    next >
C/C++ Source or Header  |  1992-07-17  |  23KB  |  597 lines

  1. /*
  2.  * jquant1.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains 1-pass color quantization (color mapping) routines.
  9.  * These routines are invoked via the methods color_quantize
  10.  * and color_quant_init/term.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15. #ifdef QUANT_1PASS_SUPPORTED
  16.  
  17.  
  18. /*
  19.  * The main purpose of 1-pass quantization is to provide a fast, if not very
  20.  * high quality, colormapped output capability.  A 2-pass quantizer usually
  21.  * gives better visual quality; however, for quantized grayscale output this
  22.  * quantizer is perfectly adequate.  Dithering is highly recommended with this
  23.  * quantizer, though you can turn it off if you really want to.
  24.  *
  25.  * This implementation quantizes in the output colorspace.  This has a couple
  26.  * of disadvantages: each pixel must be individually color-converted, and if
  27.  * the color conversion includes gamma correction then quantization is done in
  28.  * a nonlinear space, which is less desirable.  The major advantage is that
  29.  * with the usual output color spaces (RGB, grayscale) an orthogonal grid of
  30.  * representative colors can be used, thus permitting the very simple and fast
  31.  * color lookup scheme used here.  The standard JPEG colorspace (YCbCr) cannot
  32.  * be effectively handled this way, because only about a quarter of an
  33.  * orthogonal grid would fall within the gamut of realizable colors.  Another
  34.  * advantage is that when the user wants quantized grayscale output from a
  35.  * color JPEG file, this quantizer can provide a high-quality result with no
  36.  * special hacking.
  37.  *
  38.  * The gamma-correction problem could be eliminated by adjusting the grid
  39.  * spacing to counteract the gamma correction applied by color_convert.
  40.  * At this writing, gamma correction is not implemented by jdcolor, so
  41.  * nothing is done here.
  42.  *
  43.  * In 1-pass quantization the colormap must be chosen in advance of seeing the
  44.  * image.  We use a map consisting of all combinations of Ncolors[i] color
  45.  * values for the i'th component.  The Ncolors[] values are chosen so that
  46.  * their product, the total number of colors, is no more than that requested.
  47.  * (In most cases, the product will be somewhat less.)
  48.  *
  49.  * Since the colormap is orthogonal, the representative value for each color
  50.  * component can be determined without considering the other components;
  51.  * then these indexes can be combined into a colormap index by a standard
  52.  * N-dimensional-array-subscript calculation.  Most of the arithmetic involved
  53.  * can be precalculated and stored in the lookup table colorindex[].
  54.  * colorindex[i][j] maps pixel value j in component i to the nearest
  55.  * representative value (grid plane) for that component; this index is
  56.  * multiplied by the array stride for component i, so that the
  57.  * index of the colormap entry closest to a given pixel value is just
  58.  *    sum( colorindex[component-number][pixel-component-value] )
  59.  * Aside from being fast, this scheme allows for variable spacing between
  60.  * representative values with no additional lookup cost.
  61.  */
  62.  
  63.  
  64. #define MAX_COMPONENTS 4    /* max components I can handle */
  65.  
  66. static JSAMPARRAY colormap;    /* The actual color map */
  67. /* colormap[i][j] = value of i'th color component for output pixel value j */
  68.  
  69. static JSAMPARRAY colorindex;    /* Precomputed mapping for speed */
  70. /* colorindex[i][j] = index of color closest to pixel value j in component i,
  71.  * premultiplied as described above.  Since colormap indexes must fit into
  72.  * JSAMPLEs, the entries of this array will too.
  73.  */
  74.  
  75. static JSAMPARRAY input_buffer;    /* color conversion workspace */
  76. /* Since our input data is presented in the JPEG colorspace, we have to call
  77.  * color_convert to get it into the output colorspace.  input_buffer is a
  78.  * one-row-high workspace for the result of color_convert.
  79.  */
  80.  
  81.  
  82. /* Declarations for Floyd-Steinberg dithering.
  83.  *
  84.  * Errors are accumulated into the arrays evenrowerrs[] and oddrowerrs[].
  85.  * These have resolutions of 1/16th of a pixel count.  The error at a given
  86.  * pixel is propagated to its unprocessed neighbors using the standard F-S
  87.  * fractions,
  88.  *        ...    (here)    7/16
  89.  *        3/16    5/16    1/16
  90.  * We work left-to-right on even rows, right-to-left on odd rows.
  91.  *
  92.  * In each of the xxxrowerrs[] arrays, indexing is [component#][position].
  93.  * We provide (#columns + 2) entries per component; the extra entry at each
  94.  * end saves us from special-casing the first and last pixels.
  95.  * In evenrowerrs[], the entries for a component are stored left-to-right, but
  96.  * in oddrowerrs[] they are stored right-to-left.  This means we always
  97.  * process the current row's error entries in increasing order and the next
  98.  * row's error entries in decreasing order, regardless of whether we are
  99.  * working L-to-R or R-to-L in the pixel data!
  100.  *
  101.  * Note: on a wide image, we might not have enough room in a PC's near data
  102.  * segment to hold the error arrays; so they are allocated with alloc_medium.
  103.  */
  104.  
  105. #ifdef OLD_CODE
  106. #ifdef EIGHT_BIT_SAMPLES
  107. typedef INT16 FSERROR;        /* 16 bits should be enough */
  108. #else
  109. typedef INT32 FSERROR;        /* may need more than 16 bits? */
  110. #endif
  111. #endif
  112.  
  113. typedef int FSERROR; 
  114.  
  115. typedef FSERROR FAR *FSERRPTR;    /* pointer to error array (in FAR storage!) */
  116.  
  117. static FSERRPTR evenrowerrs[MAX_COMPONENTS]; /* errors for even rows */
  118. static FSERRPTR oddrowerrs[MAX_COMPONENTS];  /* errors for odd rows */
  119. static boolean on_odd_row;    /* flag to remember which row we are on */
  120.  
  121.  
  122. /*
  123.  * Policy-making subroutines for color_quant_init: these routines determine
  124.  * the colormap to be used.  The rest of the module only assumes that the
  125.  * colormap is orthogonal.
  126.  *
  127.  *  * select_ncolors decides how to divvy up the available colors
  128.  *    among the components.
  129.  *  * output_value defines the set of representative values for a component.
  130.  *  * largest_input_value defines the mapping from input values to
  131.  *    representative values for a component.
  132.  * Note that the latter two routines may impose different policies for
  133.  * different components, though this is not currently done.
  134.  */
  135.  
  136.  
  137. LOCAL int
  138. select_ncolors (decompress_info_ptr cinfo, int Ncolors[])
  139. /* Determine allocation of desired colors to components, */
  140. /* and fill in Ncolors[] array to indicate choice. */
  141. /* Return value is total number of colors (product of Ncolors[] values). */
  142. {
  143.   int nc = cinfo->color_out_comps; /* number of color components */
  144.   int max_colors = cinfo->desired_number_of_colors;
  145.   int total_colors, iroot, i;
  146.   long temp;
  147.   boolean changed;
  148.  
  149.   /* We can allocate at least the nc'th root of max_colors per component. */
  150.   /* Compute floor(nc'th root of max_colors). */
  151.   iroot = 1;
  152.   do {
  153.     iroot++;
  154.     temp = iroot;        /* set temp = iroot ** nc */
  155.     for (i = 1; i < nc; i++)
  156.       temp *= iroot;
  157.   } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
  158.   iroot--;            /* now iroot = floor(root) */
  159.  
  160.   /* Must have at least 2 color values per component */
  161.   if (iroot < 2)
  162.     ERREXIT1(cinfo->emethods, "Cannot quantize to fewer than %d colors",
  163.          (int) temp);
  164.  
  165.   if (cinfo->out_color_space == CS_RGB && nc == 3) {
  166.     /* We provide a special policy for quantizing in RGB space.
  167.      * If 256 colors are requested, we allocate 8 red, 8 green, 4 blue levels;
  168.      * this corresponds to the common 3/3/2-bit scheme.  For other totals,
  169.      * the counts are set so that the number of colors allocated to each
  170.      * component are roughly in the proportion R 3, G 4, B 2.
  171.      * For low color counts, it's easier to hardwire the optimal choices
  172.      * than try to tweak the algorithm to generate them.
  173.      */
  174.     if (max_colors == 256) {
  175.       Ncolors[0] = 8;  Ncolors[1] = 8;  Ncolors[2] = 4;
  176.       return 256;
  177.     }
  178.     if (max_colors < 12) {
  179.       /* Fixed mapping for 8 colors */
  180.       Ncolors[0] = Ncolors[1] = Ncolors[2] = 2;
  181.     } else if (max_colors < 18) {
  182.       /* Fixed mapping for 12 colors */
  183.       Ncolors[0] = 2;  Ncolors[1] = 3;  Ncolors[2] = 2;
  184.     } else if (max_colors < 24) {
  185.       /* Fixed mapping for 18 colors */
  186.       Ncolors[0] = 3;  Ncolors[1] = 3;  Ncolors[2] = 2;
  187.     } else if (max_colors < 27) {
  188.       /* Fixed mapping for 24 colors */
  189.       Ncolors[0] = 3;  Ncolors[1] = 4;  Ncolors[2] = 2;
  190.     } else if (max_colors < 36) {
  191.       /* Fixed mapping for 27 colors */
  192.       Ncolors[0] = 3;  Ncolors[1] = 3;  Ncolors[2] = 3;
  193.     } else {
  194.       /* these weights are readily derived with a little algebra */
  195.       Ncolors[0] = (iroot * 266) >> 8; /* R weight is 1.0400 */
  196.       Ncolors[1] = (iroot * 355) >> 8; /* G weight is 1.3867 */
  197.       Ncolors[2] = (iroot * 177) >> 8; /* B weight is 0.6934 */
  198.     }
  199.     total_colors = Ncolors[0] * Ncolors[1] * Ncolors[2];
  200.     /* The above computation produces "floor" values, so we may be able to
  201.      * increment the count for one or more components without exceeding
  202.      * max_colors.  We try in the order B, G, R.
  203.      */
  204.     do {
  205.       changed = FALSE;
  206.       for (i = 2; i >= 0; i--) {
  207.     /* calculate new total_colors if Ncolors[i] is incremented */
  208.     temp = total_colors / Ncolors[i];
  209.     temp *= Ncolors[i]+1;    /* done in long arith to avoid oflo */
  210.     if (temp <= (long) max_colors) {
  211.       Ncolors[i]++;        /* OK, apply the increment */
  212.       total_colors = (int) temp;
  213.       changed = TRUE;
  214.     }
  215.       }
  216.     } while (changed);        /* loop until no increment is possible */
  217.   } else {
  218.     /* For any colorspace besides RGB, treat all the components equally. */
  219.  
  220.     /* Initialize to iroot color values for each component */
  221.     total_colors = 1;
  222.     for (i = 0; i < nc; i++) {
  223.       Ncolors[i] = iroot;
  224.       total_colors *= iroot;
  225.     }
  226.     /* We may be able to increment the count for one or more components without
  227.      * exceeding max_colors, though we know not all can be incremented.
  228.      */
  229.     for (i = 0; i < nc; i++) {
  230.       /* calculate new total_colors if Ncolors[i] is incremented */
  231.       temp = total_colors / Ncolors[i];
  232.       temp *= Ncolors[i]+1;    /* done in long arith to avoid oflo */
  233.       if (temp > (long) max_colors)
  234.     break;            /* won't fit, done */
  235.       Ncolors[i]++;        /* OK, apply the increment */
  236.       total_colors = (int) temp;
  237.     }
  238.   }
  239.  
  240.   return total_colors;
  241. }
  242.  
  243.  
  244. LOCAL int
  245. output_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
  246. /* Return j'th output value, where j will range from 0 to maxj */
  247. /* The output values must fall in 0..MAXJSAMPLE in increasing order */
  248. {
  249.   /* We always provide values 0 and MAXJSAMPLE for each component;
  250.    * any additional values are equally spaced between these limits.
  251.    * (Forcing the upper and lower values to the limits ensures that
  252.    * dithering can't produce a color outside the selected gamut.)
  253.    */
  254.   return (j * MAXJSAMPLE + maxj/2) / maxj;
  255. }
  256.  
  257.  
  258. LOCAL int
  259. largest_input_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
  260. /* Return largest input value that should map to j'th output value */
  261. /* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
  262. {
  263.   /* Breakpoints are halfway between values returned by output_value */
  264.   return ((2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj);
  265. }
  266.  
  267.  
  268. /*
  269.  * Initialize for one-pass color quantization.
  270.  */
  271.  
  272. METHODDEF void
  273. color_quant_init (decompress_info_ptr cinfo)
  274. {
  275.   int total_colors;        /* Number of distinct output colors */
  276.   int Ncolors[MAX_COMPONENTS];    /* # of values alloced to each component */
  277.   int i,j,k, nci, blksize, blkdist, ptr, val;
  278.  
  279.   /* Make sure my internal arrays won't overflow */
  280.   if (cinfo->num_components > MAX_COMPONENTS ||
  281.       cinfo->color_out_comps > MAX_COMPONENTS)
  282.     ERREXIT1(cinfo->emethods, "Cannot quantize more than %d color components",
  283.          MAX_COMPONENTS);
  284.   /* Make sure colormap indexes can be represented by JSAMPLEs */
  285.   if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
  286.     ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors",
  287.          MAXJSAMPLE+1);
  288.  
  289.   /* Select number of colors for each component */
  290.   total_colors = select_ncolors(cinfo, Ncolors);
  291.  
  292.   /* Report selected color counts */
  293.   if (cinfo->color_out_comps == 3)
  294.     TRACEMS4(cinfo->emethods, 1, "Quantizing to %d = %d*%d*%d colors",
  295.          total_colors, Ncolors[0], Ncolors[1], Ncolors[2]);
  296.   else
  297.     TRACEMS1(cinfo->emethods, 1, "Quantizing to %d colors", total_colors);
  298.  
  299.   /* Allocate and fill in the colormap and color index. */
  300.   /* The colors are ordered in the map in standard row-major order, */
  301.   /* i.e. rightmost (highest-indexed) color changes most rapidly. */
  302.  
  303.   colormap = (*cinfo->emethods->alloc_small_sarray)
  304.         ((long) total_colors, (long) cinfo->color_out_comps);
  305.   colorindex = (*cinfo->emethods->alloc_small_sarray)
  306.         ((long) (MAXJSAMPLE+1), (long) cinfo->color_out_comps);
  307.  
  308.   /* blksize is number of adjacent repeated entries for a component */
  309.   /* blkdist is distance between groups of identical entries for a component */
  310.   blkdist = total_colors;
  311.  
  312.   for (i = 0; i < cinfo->color_out_comps; i++) {
  313.     /* fill in colormap entries for i'th color component */
  314.     nci = Ncolors[i];        /* # of distinct values for this color */
  315.     blksize = blkdist / nci;
  316.     for (j = 0; j < nci; j++) {
  317.       /* Compute j'th output value (out of nci) for component */
  318.       val = output_value(cinfo, i, j, nci-1);
  319.       /* Fill in all colormap entries that have this value of this component */
  320.       for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
  321.     /* fill in blksize entries beginning at ptr */
  322.     for (k = 0; k < blksize; k++)
  323.       colormap[i][ptr+k] = (JSAMPLE) val;
  324.       }
  325.     }
  326.     blkdist = blksize;        /* blksize of this color is blkdist of next */
  327.  
  328.     /* fill in colorindex entries for i'th color component */
  329.     /* in loop, val = index of current output value, */
  330.     /* and k = largest j that maps to current val */
  331.     val = 0;
  332.     k = largest_input_value(cinfo, i, 0, nci-1);
  333.     for (j = 0; j <= MAXJSAMPLE; j++) {
  334.       while (j > k)        /* advance val if past boundary */
  335.     k = largest_input_value(cinfo, i, ++val, nci-1);
  336.       /* premultiply so that no multiplication needed in main processing */
  337.       colorindex[i][j] = (JSAMPLE) (val * blksize);
  338.     }
  339.   }
  340.  
  341.   /* Pass the colormap to the output module. */
  342.   /* NB: the output module may continue to use the colormap until shutdown. */
  343.   cinfo->colormap = colormap;
  344.   cinfo->actual_number_of_colors = total_colors;
  345.   (*cinfo->methods->put_color_map) (cinfo, total_colors, colormap);
  346.  
  347.   /* Allocate workspace to hold one row of color-converted data */
  348.   input_buffer = (*cinfo->emethods->alloc_small_sarray)
  349.             (cinfo->image_width, (long) cinfo->color_out_comps);
  350.  
  351.   /* Allocate Floyd-Steinberg workspace if necessary */
  352.   if (cinfo->use_dithering) {
  353.     size_t arraysize = (size_t) ((cinfo->image_width + 2L) * SIZEOF(FSERROR));
  354.  
  355.     for (i = 0; i < cinfo->color_out_comps; i++) {
  356.       evenrowerrs[i] = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
  357.       oddrowerrs[i]  = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
  358.       /* we only need to zero the forward contribution for current row. */
  359.       jzero_far((void FAR *) evenrowerrs[i], arraysize);
  360.     }
  361.     on_odd_row = FALSE;
  362.   }
  363. }
  364.  
  365.  
  366. /*
  367.  * Subroutines for color conversion methods.
  368.  */
  369.  
  370. LOCAL void
  371. do_color_conversion (decompress_info_ptr cinfo, JSAMPIMAGE input_data, int row)
  372. /* Convert the indicated row of the input data into output colorspace */
  373. /* in input_buffer.  This requires a little trickery since color_convert */
  374. /* expects to deal with 3-D arrays; fortunately we can fake it out */
  375. /* at fairly low cost. */
  376. {
  377.   short ci;
  378.   JSAMPARRAY input_hack[MAX_COMPONENTS];
  379.   JSAMPARRAY output_hack[MAX_COMPONENTS];
  380.  
  381.   /* create JSAMPIMAGE pointing at specified row of input_data */
  382.   for (ci = 0; ci < cinfo->num_components; ci++)
  383.     input_hack[ci] = input_data[ci] + row;
  384.   /* Create JSAMPIMAGE pointing at input_buffer */
  385.   for (ci = 0; ci < cinfo->color_out_comps; ci++)
  386.     output_hack[ci] = &(input_buffer[ci]);
  387.  
  388.   (*cinfo->methods->color_convert) (cinfo, 1, cinfo->image_width,
  389.                     input_hack, output_hack);
  390. }
  391.  
  392.  
  393. /*
  394.  * Map some rows of pixels to the output colormapped representation.
  395.  */
  396.  
  397. METHODDEF void
  398. color_quantize (decompress_info_ptr cinfo, int num_rows,
  399.         JSAMPIMAGE input_data, JSAMPARRAY output_data)
  400. /* General case, no dithering */
  401. {
  402.   register int pixcode, ci;
  403.   register JSAMPROW ptrout;
  404.   register long col;
  405.   int row;
  406.   long width = cinfo->image_width;
  407.   register int nc = cinfo->color_out_comps;  
  408.  
  409.   for (row = 0; row < num_rows; row++) {
  410.     do_color_conversion(cinfo, input_data, row);
  411.     ptrout = output_data[row];
  412.     for (col = 0; col < width; col++) {
  413.       pixcode = 0;
  414.       for (ci = 0; ci < nc; ci++) {
  415.     pixcode += GETJSAMPLE(colorindex[ci]
  416.                   [GETJSAMPLE(input_buffer[ci][col])]);
  417.       }
  418.       *ptrout++ = (JSAMPLE) pixcode;
  419.     }
  420.   }
  421. }
  422.  
  423.  
  424. METHODDEF void
  425. color_quantize3 (decompress_info_ptr cinfo, int num_rows,
  426.          JSAMPIMAGE input_data, JSAMPARRAY output_data)
  427. /* Fast path for color_out_comps==3, no dithering */
  428. {
  429.   register int pixcode;
  430.   register JSAMPROW ptr0, ptr1, ptr2, ptrout;
  431.   register long col;
  432.   int row;
  433.   long width = cinfo->image_width;
  434.  
  435.   for (row = 0; row < num_rows; row++) {
  436.     do_color_conversion(cinfo, input_data, row);
  437.     ptr0 = input_buffer[0];
  438.     ptr1 = input_buffer[1];
  439.     ptr2 = input_buffer[2];
  440.     ptrout = output_data[row];
  441.     for (col = width; col > 0; col--) {
  442.       pixcode  = GETJSAMPLE(colorindex[0][GETJSAMPLE(*ptr0++)]);
  443.       pixcode += GETJSAMPLE(colorindex[1][GETJSAMPLE(*ptr1++)]);
  444.       pixcode += GETJSAMPLE(colorindex[2][GETJSAMPLE(*ptr2++)]);
  445.       *ptrout++ = (JSAMPLE) pixcode;
  446.     }
  447.   }
  448. }
  449.  
  450.  
  451. METHODDEF void
  452. color_quantize_dither (decompress_info_ptr cinfo, int num_rows,
  453.                JSAMPIMAGE input_data, JSAMPARRAY output_data)
  454. /* General case, with Floyd-Steinberg dithering */
  455. {
  456.   register FSERROR val;
  457.   FSERROR two_val;
  458.   register FSERRPTR thisrowerr, nextrowerr;
  459.   register JSAMPROW input_ptr;
  460.   register JSAMPROW output_ptr;
  461.   JSAMPROW colorindex_ci;
  462.   JSAMPROW colormap_ci;
  463.   register int pixcode;
  464.   int dir;            /* 1 for left-to-right, -1 for right-to-left */
  465.   int ci;
  466.   int nc = cinfo->color_out_comps;
  467.   int row;
  468.   long col_counter;
  469.   long width = cinfo->image_width;
  470.  
  471.   for (row = 0; row < num_rows; row++) {
  472.     do_color_conversion(cinfo, input_data, row);
  473.     /* Initialize output values to 0 so can process components separately */
  474.     jzero_far((void FAR *) output_data[row],
  475.           (size_t) (width * SIZEOF(JSAMPLE)));
  476.     for (ci = 0; ci < nc; ci++) {
  477.       if (on_odd_row) {
  478.     /* work right to left in this row */
  479.     dir = -1;
  480.     input_ptr = input_buffer[ci] + (width-1);
  481.     output_ptr = output_data[row] + (width-1);
  482.     thisrowerr = oddrowerrs[ci] + 1;
  483.     nextrowerr = evenrowerrs[ci] + width;
  484.       } else {
  485.     /* work left to right in this row */
  486.     dir = 1;
  487.     input_ptr = input_buffer[ci];
  488.     output_ptr = output_data[row];
  489.     thisrowerr = evenrowerrs[ci] + 1;
  490.     nextrowerr = oddrowerrs[ci] + width;
  491.       }
  492.       colorindex_ci = colorindex[ci];
  493.       colormap_ci = colormap[ci];
  494.       *nextrowerr = 0;        /* need only initialize this one entry */
  495.       for (col_counter = width; col_counter > 0; col_counter--) {
  496.     /* Compute pixel value + accumulated error for this component */
  497.     val = (((FSERROR) GETJSAMPLE(*input_ptr)) << 4) + *thisrowerr;
  498.     if (val < 0) val = 0;    /* must watch for range overflow! */
  499.     else {
  500. #ifdef OLD_CODE
  501.       val += 8;     /* divide by 16 with proper rounding */
  502. #endif
  503.       val >>= 4;
  504.       if (val > MAXJSAMPLE) val = MAXJSAMPLE;
  505.     }
  506.     /* Select output value, accumulate into output code for this pixel */
  507.     pixcode = GETJSAMPLE(*output_ptr);
  508.     pixcode += GETJSAMPLE(colorindex_ci[val]);
  509.     *output_ptr = (JSAMPLE) pixcode;
  510.     /* Compute actual representation error at this pixel */
  511.     /* Note: we can do this even though we don't yet have the final */
  512.     /* value of pixcode, because the colormap is orthogonal. */
  513.     val -= (FSERROR) GETJSAMPLE(colormap_ci[pixcode]);
  514.     /* Propagate error to (same component of) adjacent pixels */
  515.     /* Remember that nextrowerr entries are in reverse order! */
  516.     two_val = val * 2;
  517.     nextrowerr[-1]  = val; /* not +=, since not initialized yet */
  518.     val += two_val;        /* form error * 3 */
  519.     nextrowerr[ 1] += val;
  520.     val += two_val;        /* form error * 5 */
  521.     nextrowerr[ 0] += val;
  522.     val += two_val;        /* form error * 7 */
  523.     thisrowerr[ 1] += val;
  524.     input_ptr += dir;    /* advance input ptr to next column */
  525.     output_ptr += dir;    /* advance output ptr to next column */
  526.     thisrowerr++;        /* cur-row error ptr advances to right */
  527.     nextrowerr--;        /* next-row error ptr advances to left */
  528.       }
  529.     }
  530.     on_odd_row = (on_odd_row ? FALSE : TRUE);
  531.   }
  532. }
  533.  
  534.  
  535. /*
  536.  * Finish up at the end of the file.
  537.  */
  538.  
  539. METHODDEF void
  540. color_quant_term (decompress_info_ptr cinfo)
  541. {
  542.   /* no work (we let free_all release the workspace) */
  543.   /* Note that we *mustn't* free the colormap before free_all, */
  544.   /* since output module may use it! */
  545. }
  546.  
  547.  
  548. /*
  549.  * Prescan some rows of pixels.
  550.  * Not used in one-pass case.
  551.  */
  552.  
  553. METHODDEF void
  554. color_quant_prescan (decompress_info_ptr cinfo, int num_rows,
  555.              JSAMPIMAGE image_data, JSAMPARRAY workspace)
  556. {
  557.   ERREXIT(cinfo->emethods, "Should not get here!");
  558. }
  559.  
  560.  
  561. /*
  562.  * Do two-pass quantization.
  563.  * Not used in one-pass case.
  564.  */
  565.  
  566. METHODDEF void
  567. color_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method)
  568. {
  569.   ERREXIT(cinfo->emethods, "Should not get here!");
  570. }
  571.  
  572.  
  573. /*
  574.  * The method selection routine for 1-pass color quantization.
  575.  */
  576.  
  577. GLOBAL void
  578. jsel1quantize (decompress_info_ptr cinfo)
  579. {
  580.   if (! cinfo->two_pass_quantize) {
  581.     cinfo->methods->color_quant_init = color_quant_init;
  582.     if (cinfo->use_dithering) {
  583.       cinfo->methods->color_quantize = color_quantize_dither;
  584.     } else {
  585.       if (cinfo->color_out_comps == 3)
  586.     cinfo->methods->color_quantize = color_quantize3;
  587.       else
  588.     cinfo->methods->color_quantize = color_quantize;
  589.     }
  590.     cinfo->methods->color_quant_prescan = color_quant_prescan;
  591.     cinfo->methods->color_quant_doit = color_quant_doit;
  592.     cinfo->methods->color_quant_term = color_quant_term;
  593.   }
  594. }
  595.  
  596. #endif /* QUANT_1PASS_SUPPORTED */
  597.