blob: 8fdb2a3a9ce40182be12adaed8beb45cdcdfd5b0 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jquant1.c
3 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00004 * Copyright (C) 1991, 1992, Thomas G. Lane.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00005 * 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/*
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000019 * 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.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000024 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000025 * 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.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000037 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000038 * 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.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000061 */
62
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000063
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000064#define MAX_COMPONENTS 4 /* max components I can handle */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000065
66static JSAMPARRAY colormap; /* The actual color map */
67/* colormap[i][j] = value of i'th color component for output pixel value j */
68
69static JSAMPARRAY colorindex; /* Precomputed mapping for speed */
70/* colorindex[i][j] = index of color closest to pixel value j in component i,
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000071 * premultiplied as described above. Since colormap indexes must fit into
72 * JSAMPLEs, the entries of this array will too.
73 */
74
75static 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.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000079 */
80
81
82/* Declarations for Floyd-Steinberg dithering.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000083 *
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,
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000088 * ... (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 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000092 * 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 *
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000101 * 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 EIGHT_BIT_SAMPLES
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000106typedef INT16 FSERROR; /* 16 bits should be enough */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000107#else
108typedef INT32 FSERROR; /* may need more than 16 bits? */
109#endif
110
111typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */
112
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000113static FSERRPTR evenrowerrs[MAX_COMPONENTS]; /* errors for even rows */
114static FSERRPTR oddrowerrs[MAX_COMPONENTS]; /* errors for odd rows */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000115static boolean on_odd_row; /* flag to remember which row we are on */
116
117
118/*
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000119 * Policy-making subroutines for color_quant_init: these routines determine
120 * the colormap to be used. The rest of the module only assumes that the
121 * colormap is orthogonal.
122 *
123 * * select_ncolors decides how to divvy up the available colors
124 * among the components.
125 * * output_value defines the set of representative values for a component.
126 * * largest_input_value defines the mapping from input values to
127 * representative values for a component.
128 * Note that the latter two routines may impose different policies for
129 * different components, though this is not currently done.
130 */
131
132
133LOCAL int
134select_ncolors (decompress_info_ptr cinfo, int Ncolors[])
135/* Determine allocation of desired colors to components, */
136/* and fill in Ncolors[] array to indicate choice. */
137/* Return value is total number of colors (product of Ncolors[] values). */
138{
139 int nc = cinfo->color_out_comps; /* number of color components */
140 int max_colors = cinfo->desired_number_of_colors;
141 int total_colors, iroot, i;
142 long temp;
143 boolean changed;
144
145 /* We can allocate at least the nc'th root of max_colors per component. */
146 /* Compute floor(nc'th root of max_colors). */
147 iroot = 1;
148 do {
149 iroot++;
150 temp = iroot; /* set temp = iroot ** nc */
151 for (i = 1; i < nc; i++)
152 temp *= iroot;
153 } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
154 iroot--; /* now iroot = floor(root) */
155
156 /* Must have at least 2 color values per component */
157 if (iroot < 2)
158 ERREXIT1(cinfo->emethods, "Cannot quantize to fewer than %d colors",
159 (int) temp);
160
161 if (cinfo->out_color_space == CS_RGB && nc == 3) {
162 /* We provide a special policy for quantizing in RGB space.
163 * If 256 colors are requested, we allocate 8 red, 8 green, 4 blue levels;
164 * this corresponds to the common 3/3/2-bit scheme. For other totals,
165 * the counts are set so that the number of colors allocated to each
166 * component are roughly in the proportion R 3, G 4, B 2.
167 * For low color counts, it's easier to hardwire the optimal choices
168 * than try to tweak the algorithm to generate them.
169 */
170 if (max_colors == 256) {
171 Ncolors[0] = 8; Ncolors[1] = 8; Ncolors[2] = 4;
172 return 256;
173 }
174 if (max_colors < 12) {
175 /* Fixed mapping for 8 colors */
176 Ncolors[0] = Ncolors[1] = Ncolors[2] = 2;
177 } else if (max_colors < 18) {
178 /* Fixed mapping for 12 colors */
179 Ncolors[0] = 2; Ncolors[1] = 3; Ncolors[2] = 2;
180 } else if (max_colors < 24) {
181 /* Fixed mapping for 18 colors */
182 Ncolors[0] = 3; Ncolors[1] = 3; Ncolors[2] = 2;
183 } else if (max_colors < 27) {
184 /* Fixed mapping for 24 colors */
185 Ncolors[0] = 3; Ncolors[1] = 4; Ncolors[2] = 2;
186 } else if (max_colors < 36) {
187 /* Fixed mapping for 27 colors */
188 Ncolors[0] = 3; Ncolors[1] = 3; Ncolors[2] = 3;
189 } else {
190 /* these weights are readily derived with a little algebra */
191 Ncolors[0] = (iroot * 266) >> 8; /* R weight is 1.0400 */
192 Ncolors[1] = (iroot * 355) >> 8; /* G weight is 1.3867 */
193 Ncolors[2] = (iroot * 177) >> 8; /* B weight is 0.6934 */
194 }
195 total_colors = Ncolors[0] * Ncolors[1] * Ncolors[2];
196 /* The above computation produces "floor" values, so we may be able to
197 * increment the count for one or more components without exceeding
198 * max_colors. We try in the order B, G, R.
199 */
200 do {
201 changed = FALSE;
202 for (i = 2; i >= 0; i--) {
203 /* calculate new total_colors if Ncolors[i] is incremented */
204 temp = total_colors / Ncolors[i];
205 temp *= Ncolors[i]+1; /* done in long arith to avoid oflo */
206 if (temp <= (long) max_colors) {
207 Ncolors[i]++; /* OK, apply the increment */
208 total_colors = (int) temp;
209 changed = TRUE;
210 }
211 }
212 } while (changed); /* loop until no increment is possible */
213 } else {
214 /* For any colorspace besides RGB, treat all the components equally. */
215
216 /* Initialize to iroot color values for each component */
217 total_colors = 1;
218 for (i = 0; i < nc; i++) {
219 Ncolors[i] = iroot;
220 total_colors *= iroot;
221 }
222 /* We may be able to increment the count for one or more components without
223 * exceeding max_colors, though we know not all can be incremented.
224 */
225 for (i = 0; i < nc; i++) {
226 /* calculate new total_colors if Ncolors[i] is incremented */
227 temp = total_colors / Ncolors[i];
228 temp *= Ncolors[i]+1; /* done in long arith to avoid oflo */
229 if (temp > (long) max_colors)
230 break; /* won't fit, done */
231 Ncolors[i]++; /* OK, apply the increment */
232 total_colors = (int) temp;
233 }
234 }
235
236 return total_colors;
237}
238
239
240LOCAL int
241output_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
242/* Return j'th output value, where j will range from 0 to maxj */
243/* The output values must fall in 0..MAXJSAMPLE in increasing order */
244{
245 /* We always provide values 0 and MAXJSAMPLE for each component;
246 * any additional values are equally spaced between these limits.
247 * (Forcing the upper and lower values to the limits ensures that
248 * dithering can't produce a color outside the selected gamut.)
249 */
250 return (j * MAXJSAMPLE + maxj/2) / maxj;
251}
252
253
254LOCAL int
255largest_input_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
256/* Return largest input value that should map to j'th output value */
257/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
258{
259 /* Breakpoints are halfway between values returned by output_value */
260 return ((2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj);
261}
262
263
264/*
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000265 * Initialize for one-pass color quantization.
266 */
267
268METHODDEF void
269color_quant_init (decompress_info_ptr cinfo)
270{
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000271 int total_colors; /* Number of distinct output colors */
272 int Ncolors[MAX_COMPONENTS]; /* # of values alloced to each component */
273 int i,j,k, nci, blksize, blkdist, ptr, val;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000274
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000275 /* Make sure my internal arrays won't overflow */
276 if (cinfo->num_components > MAX_COMPONENTS ||
277 cinfo->color_out_comps > MAX_COMPONENTS)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000278 ERREXIT1(cinfo->emethods, "Cannot quantize more than %d color components",
279 MAX_COMPONENTS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000280 /* Make sure colormap indexes can be represented by JSAMPLEs */
281 if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000282 ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors",
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000283 MAXJSAMPLE+1);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000284
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000285 /* Select number of colors for each component */
286 total_colors = select_ncolors(cinfo, Ncolors);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000287
288 /* Report selected color counts */
289 if (cinfo->color_out_comps == 3)
290 TRACEMS4(cinfo->emethods, 1, "Quantizing to %d = %d*%d*%d colors",
291 total_colors, Ncolors[0], Ncolors[1], Ncolors[2]);
292 else
293 TRACEMS1(cinfo->emethods, 1, "Quantizing to %d colors", total_colors);
294
295 /* Allocate and fill in the colormap and color index. */
296 /* The colors are ordered in the map in standard row-major order, */
297 /* i.e. rightmost (highest-indexed) color changes most rapidly. */
298
299 colormap = (*cinfo->emethods->alloc_small_sarray)
300 ((long) total_colors, (long) cinfo->color_out_comps);
301 colorindex = (*cinfo->emethods->alloc_small_sarray)
302 ((long) (MAXJSAMPLE+1), (long) cinfo->color_out_comps);
303
304 /* blksize is number of adjacent repeated entries for a component */
305 /* blkdist is distance between groups of identical entries for a component */
306 blkdist = total_colors;
307
308 for (i = 0; i < cinfo->color_out_comps; i++) {
309 /* fill in colormap entries for i'th color component */
310 nci = Ncolors[i]; /* # of distinct values for this color */
311 blksize = blkdist / nci;
312 for (j = 0; j < nci; j++) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000313 /* Compute j'th output value (out of nci) for component */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000314 val = output_value(cinfo, i, j, nci-1);
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000315 /* Fill in all colormap entries that have this value of this component */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000316 for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
317 /* fill in blksize entries beginning at ptr */
318 for (k = 0; k < blksize; k++)
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000319 colormap[i][ptr+k] = (JSAMPLE) val;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000320 }
321 }
322 blkdist = blksize; /* blksize of this color is blkdist of next */
323
324 /* fill in colorindex entries for i'th color component */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000325 /* in loop, val = index of current output value, */
326 /* and k = largest j that maps to current val */
327 val = 0;
328 k = largest_input_value(cinfo, i, 0, nci-1);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000329 for (j = 0; j <= MAXJSAMPLE; j++) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000330 while (j > k) /* advance val if past boundary */
331 k = largest_input_value(cinfo, i, ++val, nci-1);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000332 /* premultiply so that no multiplication needed in main processing */
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000333 colorindex[i][j] = (JSAMPLE) (val * blksize);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000334 }
335 }
336
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000337 /* Pass the colormap to the output module. */
338 /* NB: the output module may continue to use the colormap until shutdown. */
339 cinfo->colormap = colormap;
340 cinfo->actual_number_of_colors = total_colors;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000341 (*cinfo->methods->put_color_map) (cinfo, total_colors, colormap);
342
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000343 /* Allocate workspace to hold one row of color-converted data */
344 input_buffer = (*cinfo->emethods->alloc_small_sarray)
345 (cinfo->image_width, (long) cinfo->color_out_comps);
346
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000347 /* Allocate Floyd-Steinberg workspace if necessary */
348 if (cinfo->use_dithering) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000349 size_t arraysize = (size_t) ((cinfo->image_width + 2L) * SIZEOF(FSERROR));
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000350
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000351 for (i = 0; i < cinfo->color_out_comps; i++) {
352 evenrowerrs[i] = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
353 oddrowerrs[i] = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
354 /* we only need to zero the forward contribution for current row. */
355 jzero_far((void FAR *) evenrowerrs[i], arraysize);
356 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000357 on_odd_row = FALSE;
358 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000359}
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000360
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000361
362/*
363 * Subroutines for color conversion methods.
364 */
365
366LOCAL void
367do_color_conversion (decompress_info_ptr cinfo, JSAMPIMAGE input_data, int row)
368/* Convert the indicated row of the input data into output colorspace */
369/* in input_buffer. This requires a little trickery since color_convert */
370/* expects to deal with 3-D arrays; fortunately we can fake it out */
371/* at fairly low cost. */
372{
373 short ci;
374 JSAMPARRAY input_hack[MAX_COMPONENTS];
375 JSAMPARRAY output_hack[MAX_COMPONENTS];
376
377 /* create JSAMPIMAGE pointing at specified row of input_data */
378 for (ci = 0; ci < cinfo->num_components; ci++)
379 input_hack[ci] = input_data[ci] + row;
380 /* Create JSAMPIMAGE pointing at input_buffer */
381 for (ci = 0; ci < cinfo->color_out_comps; ci++)
382 output_hack[ci] = &(input_buffer[ci]);
383
384 (*cinfo->methods->color_convert) (cinfo, 1, cinfo->image_width,
385 input_hack, output_hack);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000386}
387
388
389/*
390 * Map some rows of pixels to the output colormapped representation.
391 */
392
393METHODDEF void
394color_quantize (decompress_info_ptr cinfo, int num_rows,
395 JSAMPIMAGE input_data, JSAMPARRAY output_data)
396/* General case, no dithering */
397{
398 register int pixcode, ci;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000399 register JSAMPROW ptrout;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000400 register long col;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000401 int row;
402 long width = cinfo->image_width;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000403 register int nc = cinfo->color_out_comps;
404
405 for (row = 0; row < num_rows; row++) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000406 do_color_conversion(cinfo, input_data, row);
407 ptrout = output_data[row];
408 for (col = 0; col < width; col++) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000409 pixcode = 0;
410 for (ci = 0; ci < nc; ci++) {
411 pixcode += GETJSAMPLE(colorindex[ci]
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000412 [GETJSAMPLE(input_buffer[ci][col])]);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000413 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000414 *ptrout++ = (JSAMPLE) pixcode;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000415 }
416 }
417}
418
419
420METHODDEF void
421color_quantize3 (decompress_info_ptr cinfo, int num_rows,
422 JSAMPIMAGE input_data, JSAMPARRAY output_data)
423/* Fast path for color_out_comps==3, no dithering */
424{
425 register int pixcode;
426 register JSAMPROW ptr0, ptr1, ptr2, ptrout;
427 register long col;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000428 int row;
429 long width = cinfo->image_width;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000430
431 for (row = 0; row < num_rows; row++) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000432 do_color_conversion(cinfo, input_data, row);
433 ptr0 = input_buffer[0];
434 ptr1 = input_buffer[1];
435 ptr2 = input_buffer[2];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000436 ptrout = output_data[row];
437 for (col = width; col > 0; col--) {
438 pixcode = GETJSAMPLE(colorindex[0][GETJSAMPLE(*ptr0++)]);
439 pixcode += GETJSAMPLE(colorindex[1][GETJSAMPLE(*ptr1++)]);
440 pixcode += GETJSAMPLE(colorindex[2][GETJSAMPLE(*ptr2++)]);
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000441 *ptrout++ = (JSAMPLE) pixcode;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000442 }
443 }
444}
445
446
447METHODDEF void
448color_quantize_dither (decompress_info_ptr cinfo, int num_rows,
449 JSAMPIMAGE input_data, JSAMPARRAY output_data)
450/* General case, with Floyd-Steinberg dithering */
451{
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000452 register FSERROR val;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000453 FSERROR two_val;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000454 register FSERRPTR thisrowerr, nextrowerr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000455 register JSAMPROW input_ptr;
456 register JSAMPROW output_ptr;
457 JSAMPROW colorindex_ci;
458 JSAMPROW colormap_ci;
459 register int pixcode;
460 int dir; /* 1 for left-to-right, -1 for right-to-left */
461 int ci;
462 int nc = cinfo->color_out_comps;
463 int row;
464 long col_counter;
465 long width = cinfo->image_width;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000466
467 for (row = 0; row < num_rows; row++) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000468 do_color_conversion(cinfo, input_data, row);
469 /* Initialize output values to 0 so can process components separately */
470 jzero_far((void FAR *) output_data[row],
471 (size_t) (width * SIZEOF(JSAMPLE)));
472 for (ci = 0; ci < nc; ci++) {
473 if (on_odd_row) {
474 /* work right to left in this row */
475 dir = -1;
476 input_ptr = input_buffer[ci] + (width-1);
477 output_ptr = output_data[row] + (width-1);
478 thisrowerr = oddrowerrs[ci] + 1;
479 nextrowerr = evenrowerrs[ci] + width;
480 } else {
481 /* work left to right in this row */
482 dir = 1;
483 input_ptr = input_buffer[ci];
484 output_ptr = output_data[row];
485 thisrowerr = evenrowerrs[ci] + 1;
486 nextrowerr = oddrowerrs[ci] + width;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000487 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000488 colorindex_ci = colorindex[ci];
489 colormap_ci = colormap[ci];
490 *nextrowerr = 0; /* need only initialize this one entry */
491 for (col_counter = width; col_counter > 0; col_counter--) {
492 /* Compute pixel value + accumulated error for this component */
493 val = (((FSERROR) GETJSAMPLE(*input_ptr)) << 4) + *thisrowerr;
494 if (val < 0) val = 0; /* must watch for range overflow! */
495 else {
496 val += 8; /* divide by 16 with proper rounding */
497 val >>= 4;
498 if (val > MAXJSAMPLE) val = MAXJSAMPLE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000499 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000500 /* Select output value, accumulate into output code for this pixel */
501 pixcode = GETJSAMPLE(*output_ptr);
502 pixcode += GETJSAMPLE(colorindex_ci[val]);
503 *output_ptr = (JSAMPLE) pixcode;
504 /* Compute actual representation error at this pixel */
505 /* Note: we can do this even though we don't yet have the final */
506 /* value of pixcode, because the colormap is orthogonal. */
507 val -= (FSERROR) GETJSAMPLE(colormap_ci[pixcode]);
508 /* Propagate error to (same component of) adjacent pixels */
509 /* Remember that nextrowerr entries are in reverse order! */
510 two_val = val * 2;
511 nextrowerr[-1] = val; /* not +=, since not initialized yet */
512 val += two_val; /* form error * 3 */
513 nextrowerr[ 1] += val;
514 val += two_val; /* form error * 5 */
515 nextrowerr[ 0] += val;
516 val += two_val; /* form error * 7 */
517 thisrowerr[ 1] += val;
518 input_ptr += dir; /* advance input ptr to next column */
519 output_ptr += dir; /* advance output ptr to next column */
520 thisrowerr++; /* cur-row error ptr advances to right */
521 nextrowerr--; /* next-row error ptr advances to left */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000522 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000523 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000524 on_odd_row = (on_odd_row ? FALSE : TRUE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000525 }
526}
527
528
529/*
530 * Finish up at the end of the file.
531 */
532
533METHODDEF void
534color_quant_term (decompress_info_ptr cinfo)
535{
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000536 /* no work (we let free_all release the workspace) */
537 /* Note that we *mustn't* free the colormap before free_all, */
538 /* since output module may use it! */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000539}
540
541
542/*
543 * Prescan some rows of pixels.
544 * Not used in one-pass case.
545 */
546
547METHODDEF void
548color_quant_prescan (decompress_info_ptr cinfo, int num_rows,
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000549 JSAMPIMAGE image_data, JSAMPARRAY workspace)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000550{
551 ERREXIT(cinfo->emethods, "Should not get here!");
552}
553
554
555/*
556 * Do two-pass quantization.
557 * Not used in one-pass case.
558 */
559
560METHODDEF void
561color_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method)
562{
563 ERREXIT(cinfo->emethods, "Should not get here!");
564}
565
566
567/*
568 * The method selection routine for 1-pass color quantization.
569 */
570
571GLOBAL void
572jsel1quantize (decompress_info_ptr cinfo)
573{
574 if (! cinfo->two_pass_quantize) {
575 cinfo->methods->color_quant_init = color_quant_init;
576 if (cinfo->use_dithering) {
577 cinfo->methods->color_quantize = color_quantize_dither;
578 } else {
579 if (cinfo->color_out_comps == 3)
580 cinfo->methods->color_quantize = color_quantize3;
581 else
582 cinfo->methods->color_quantize = color_quantize;
583 }
584 cinfo->methods->color_quant_prescan = color_quant_prescan;
585 cinfo->methods->color_quant_doit = color_quant_doit;
586 cinfo->methods->color_quant_term = color_quant_term;
587 }
588}
589
590#endif /* QUANT_1PASS_SUPPORTED */