jdk
1314 строк · 48.8 Кб
1/*
2* reserved comment block
3* DO NOT REMOVE OR ALTER!
4*/
5/*
6* jquant2.c
7*
8* Copyright (C) 1991-1996, Thomas G. Lane.
9* This file is part of the Independent JPEG Group's software.
10* For conditions of distribution and use, see the accompanying README file.
11*
12* This file contains 2-pass color quantization (color mapping) routines.
13* These routines provide selection of a custom color map for an image,
14* followed by mapping of the image to that color map, with optional
15* Floyd-Steinberg dithering.
16* It is also possible to use just the second pass to map to an arbitrary
17* externally-given color map.
18*
19* Note: ordered dithering is not supported, since there isn't any fast
20* way to compute intercolor distances; it's unclear that ordered dither's
21* fundamental assumptions even hold with an irregularly spaced color map.
22*/
23
24#define JPEG_INTERNALS
25#include "jinclude.h"
26#include "jpeglib.h"
27
28#ifdef QUANT_2PASS_SUPPORTED
29
30
31/*
32* This module implements the well-known Heckbert paradigm for color
33* quantization. Most of the ideas used here can be traced back to
34* Heckbert's seminal paper
35* Heckbert, Paul. "Color Image Quantization for Frame Buffer Display",
36* Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
37*
38* In the first pass over the image, we accumulate a histogram showing the
39* usage count of each possible color. To keep the histogram to a reasonable
40* size, we reduce the precision of the input; typical practice is to retain
41* 5 or 6 bits per color, so that 8 or 4 different input values are counted
42* in the same histogram cell.
43*
44* Next, the color-selection step begins with a box representing the whole
45* color space, and repeatedly splits the "largest" remaining box until we
46* have as many boxes as desired colors. Then the mean color in each
47* remaining box becomes one of the possible output colors.
48*
49* The second pass over the image maps each input pixel to the closest output
50* color (optionally after applying a Floyd-Steinberg dithering correction).
51* This mapping is logically trivial, but making it go fast enough requires
52* considerable care.
53*
54* Heckbert-style quantizers vary a good deal in their policies for choosing
55* the "largest" box and deciding where to cut it. The particular policies
56* used here have proved out well in experimental comparisons, but better ones
57* may yet be found.
58*
59* In earlier versions of the IJG code, this module quantized in YCbCr color
60* space, processing the raw upsampled data without a color conversion step.
61* This allowed the color conversion math to be done only once per colormap
62* entry, not once per pixel. However, that optimization precluded other
63* useful optimizations (such as merging color conversion with upsampling)
64* and it also interfered with desired capabilities such as quantizing to an
65* externally-supplied colormap. We have therefore abandoned that approach.
66* The present code works in the post-conversion color space, typically RGB.
67*
68* To improve the visual quality of the results, we actually work in scaled
69* RGB space, giving G distances more weight than R, and R in turn more than
70* B. To do everything in integer math, we must use integer scale factors.
71* The 2/3/1 scale factors used here correspond loosely to the relative
72* weights of the colors in the NTSC grayscale equation.
73* If you want to use this code to quantize a non-RGB color space, you'll
74* probably need to change these scale factors.
75*/
76
77#define R_SCALE 2 /* scale R distances by this much */
78#define G_SCALE 3 /* scale G distances by this much */
79#define B_SCALE 1 /* and B by this much */
80
81/* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
82* in jmorecfg.h. As the code stands, it will do the right thing for R,G,B
83* and B,G,R orders. If you define some other weird order in jmorecfg.h,
84* you'll get compile errors until you extend this logic. In that case
85* you'll probably want to tweak the histogram sizes too.
86*/
87
88#if RGB_RED == 0
89#define C0_SCALE R_SCALE
90#endif
91#if RGB_BLUE == 0
92#define C0_SCALE B_SCALE
93#endif
94#if RGB_GREEN == 1
95#define C1_SCALE G_SCALE
96#endif
97#if RGB_RED == 2
98#define C2_SCALE R_SCALE
99#endif
100#if RGB_BLUE == 2
101#define C2_SCALE B_SCALE
102#endif
103
104
105/*
106* First we have the histogram data structure and routines for creating it.
107*
108* The number of bits of precision can be adjusted by changing these symbols.
109* We recommend keeping 6 bits for G and 5 each for R and B.
110* If you have plenty of memory and cycles, 6 bits all around gives marginally
111* better results; if you are short of memory, 5 bits all around will save
112* some space but degrade the results.
113* To maintain a fully accurate histogram, we'd need to allocate a "long"
114* (preferably unsigned long) for each cell. In practice this is overkill;
115* we can get by with 16 bits per cell. Few of the cell counts will overflow,
116* and clamping those that do overflow to the maximum value will give close-
117* enough results. This reduces the recommended histogram size from 256Kb
118* to 128Kb, which is a useful savings on PC-class machines.
119* (In the second pass the histogram space is re-used for pixel mapping data;
120* in that capacity, each cell must be able to store zero to the number of
121* desired colors. 16 bits/cell is plenty for that too.)
122* Since the JPEG code is intended to run in small memory model on 80x86
123* machines, we can't just allocate the histogram in one chunk. Instead
124* of a true 3-D array, we use a row of pointers to 2-D arrays. Each
125* pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
126* each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries. Note that
127* on 80x86 machines, the pointer row is in near memory but the actual
128* arrays are in far memory (same arrangement as we use for image arrays).
129*/
130
131#define MAXNUMCOLORS (MAXJSAMPLE+1) /* maximum size of colormap */
132
133/* These will do the right thing for either R,G,B or B,G,R color order,
134* but you may not like the results for other color orders.
135*/
136#define HIST_C0_BITS 5 /* bits of precision in R/B histogram */
137#define HIST_C1_BITS 6 /* bits of precision in G histogram */
138#define HIST_C2_BITS 5 /* bits of precision in B/R histogram */
139
140/* Number of elements along histogram axes. */
141#define HIST_C0_ELEMS (1<<HIST_C0_BITS)
142#define HIST_C1_ELEMS (1<<HIST_C1_BITS)
143#define HIST_C2_ELEMS (1<<HIST_C2_BITS)
144
145/* These are the amounts to shift an input value to get a histogram index. */
146#define C0_SHIFT (BITS_IN_JSAMPLE-HIST_C0_BITS)
147#define C1_SHIFT (BITS_IN_JSAMPLE-HIST_C1_BITS)
148#define C2_SHIFT (BITS_IN_JSAMPLE-HIST_C2_BITS)
149
150
151typedef UINT16 histcell; /* histogram cell; prefer an unsigned type */
152
153typedef histcell FAR * histptr; /* for pointers to histogram cells */
154
155typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
156typedef hist1d FAR * hist2d; /* type for the 2nd-level pointers */
157typedef hist2d * hist3d; /* type for top-level pointer */
158
159
160/* Declarations for Floyd-Steinberg dithering.
161*
162* Errors are accumulated into the array fserrors[], at a resolution of
163* 1/16th of a pixel count. The error at a given pixel is propagated
164* to its not-yet-processed neighbors using the standard F-S fractions,
165* ... (here) 7/16
166* 3/16 5/16 1/16
167* We work left-to-right on even rows, right-to-left on odd rows.
168*
169* We can get away with a single array (holding one row's worth of errors)
170* by using it to store the current row's errors at pixel columns not yet
171* processed, but the next row's errors at columns already processed. We
172* need only a few extra variables to hold the errors immediately around the
173* current column. (If we are lucky, those variables are in registers, but
174* even if not, they're probably cheaper to access than array elements are.)
175*
176* The fserrors[] array has (#columns + 2) entries; the extra entry at
177* each end saves us from special-casing the first and last pixels.
178* Each entry is three values long, one value for each color component.
179*
180* Note: on a wide image, we might not have enough room in a PC's near data
181* segment to hold the error array; so it is allocated with alloc_large.
182*/
183
184#if BITS_IN_JSAMPLE == 8
185typedef INT16 FSERROR; /* 16 bits should be enough */
186typedef int LOCFSERROR; /* use 'int' for calculation temps */
187#else
188typedef INT32 FSERROR; /* may need more than 16 bits */
189typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */
190#endif
191
192typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */
193
194
195/* Private subobject */
196
197typedef struct {
198struct jpeg_color_quantizer pub; /* public fields */
199
200/* Space for the eventually created colormap is stashed here */
201JSAMPARRAY sv_colormap; /* colormap allocated at init time */
202int desired; /* desired # of colors = size of colormap */
203
204/* Variables for accumulating image statistics */
205hist3d histogram; /* pointer to the histogram */
206
207boolean needs_zeroed; /* TRUE if next pass must zero histogram */
208
209/* Variables for Floyd-Steinberg dithering */
210FSERRPTR fserrors; /* accumulated errors */
211boolean on_odd_row; /* flag to remember which row we are on */
212int * error_limiter; /* table for clamping the applied error */
213} my_cquantizer;
214
215typedef my_cquantizer * my_cquantize_ptr;
216
217
218/*
219* Prescan some rows of pixels.
220* In this module the prescan simply updates the histogram, which has been
221* initialized to zeroes by start_pass.
222* An output_buf parameter is required by the method signature, but no data
223* is actually output (in fact the buffer controller is probably passing a
224* NULL pointer).
225*/
226
227METHODDEF(void)
228prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
229JSAMPARRAY output_buf, int num_rows)
230{
231my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
232register JSAMPROW ptr;
233register histptr histp;
234register hist3d histogram = cquantize->histogram;
235int row;
236JDIMENSION col;
237JDIMENSION width = cinfo->output_width;
238
239for (row = 0; row < num_rows; row++) {
240ptr = input_buf[row];
241for (col = width; col > 0; col--) {
242/* get pixel value and index into the histogram */
243histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
244[GETJSAMPLE(ptr[1]) >> C1_SHIFT]
245[GETJSAMPLE(ptr[2]) >> C2_SHIFT];
246/* increment, check for overflow and undo increment if so. */
247if (++(*histp) <= 0)
248(*histp)--;
249ptr += 3;
250}
251}
252}
253
254
255/*
256* Next we have the really interesting routines: selection of a colormap
257* given the completed histogram.
258* These routines work with a list of "boxes", each representing a rectangular
259* subset of the input color space (to histogram precision).
260*/
261
262typedef struct {
263/* The bounds of the box (inclusive); expressed as histogram indexes */
264int c0min, c0max;
265int c1min, c1max;
266int c2min, c2max;
267/* The volume (actually 2-norm) of the box */
268INT32 volume;
269/* The number of nonzero histogram cells within this box */
270long colorcount;
271} box;
272
273typedef box * boxptr;
274
275
276LOCAL(boxptr)
277find_biggest_color_pop (boxptr boxlist, int numboxes)
278/* Find the splittable box with the largest color population */
279/* Returns NULL if no splittable boxes remain */
280{
281register boxptr boxp;
282register int i;
283register long maxc = 0;
284boxptr which = NULL;
285
286for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
287if (boxp->colorcount > maxc && boxp->volume > 0) {
288which = boxp;
289maxc = boxp->colorcount;
290}
291}
292return which;
293}
294
295
296LOCAL(boxptr)
297find_biggest_volume (boxptr boxlist, int numboxes)
298/* Find the splittable box with the largest (scaled) volume */
299/* Returns NULL if no splittable boxes remain */
300{
301register boxptr boxp;
302register int i;
303register INT32 maxv = 0;
304boxptr which = NULL;
305
306for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
307if (boxp->volume > maxv) {
308which = boxp;
309maxv = boxp->volume;
310}
311}
312return which;
313}
314
315
316LOCAL(void)
317update_box (j_decompress_ptr cinfo, boxptr boxp)
318/* Shrink the min/max bounds of a box to enclose only nonzero elements, */
319/* and recompute its volume and population */
320{
321my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
322hist3d histogram = cquantize->histogram;
323histptr histp;
324int c0,c1,c2;
325int c0min,c0max,c1min,c1max,c2min,c2max;
326INT32 dist0,dist1,dist2;
327long ccount;
328
329c0min = boxp->c0min; c0max = boxp->c0max;
330c1min = boxp->c1min; c1max = boxp->c1max;
331c2min = boxp->c2min; c2max = boxp->c2max;
332
333if (c0max > c0min)
334for (c0 = c0min; c0 <= c0max; c0++)
335for (c1 = c1min; c1 <= c1max; c1++) {
336histp = & histogram[c0][c1][c2min];
337for (c2 = c2min; c2 <= c2max; c2++)
338if (*histp++ != 0) {
339boxp->c0min = c0min = c0;
340goto have_c0min;
341}
342}
343have_c0min:
344if (c0max > c0min)
345for (c0 = c0max; c0 >= c0min; c0--)
346for (c1 = c1min; c1 <= c1max; c1++) {
347histp = & histogram[c0][c1][c2min];
348for (c2 = c2min; c2 <= c2max; c2++)
349if (*histp++ != 0) {
350boxp->c0max = c0max = c0;
351goto have_c0max;
352}
353}
354have_c0max:
355if (c1max > c1min)
356for (c1 = c1min; c1 <= c1max; c1++)
357for (c0 = c0min; c0 <= c0max; c0++) {
358histp = & histogram[c0][c1][c2min];
359for (c2 = c2min; c2 <= c2max; c2++)
360if (*histp++ != 0) {
361boxp->c1min = c1min = c1;
362goto have_c1min;
363}
364}
365have_c1min:
366if (c1max > c1min)
367for (c1 = c1max; c1 >= c1min; c1--)
368for (c0 = c0min; c0 <= c0max; c0++) {
369histp = & histogram[c0][c1][c2min];
370for (c2 = c2min; c2 <= c2max; c2++)
371if (*histp++ != 0) {
372boxp->c1max = c1max = c1;
373goto have_c1max;
374}
375}
376have_c1max:
377if (c2max > c2min)
378for (c2 = c2min; c2 <= c2max; c2++)
379for (c0 = c0min; c0 <= c0max; c0++) {
380histp = & histogram[c0][c1min][c2];
381for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
382if (*histp != 0) {
383boxp->c2min = c2min = c2;
384goto have_c2min;
385}
386}
387have_c2min:
388if (c2max > c2min)
389for (c2 = c2max; c2 >= c2min; c2--)
390for (c0 = c0min; c0 <= c0max; c0++) {
391histp = & histogram[c0][c1min][c2];
392for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
393if (*histp != 0) {
394boxp->c2max = c2max = c2;
395goto have_c2max;
396}
397}
398have_c2max:
399
400/* Update box volume.
401* We use 2-norm rather than real volume here; this biases the method
402* against making long narrow boxes, and it has the side benefit that
403* a box is splittable iff norm > 0.
404* Since the differences are expressed in histogram-cell units,
405* we have to shift back to JSAMPLE units to get consistent distances;
406* after which, we scale according to the selected distance scale factors.
407*/
408dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
409dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
410dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
411boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
412
413/* Now scan remaining volume of box and compute population */
414ccount = 0;
415for (c0 = c0min; c0 <= c0max; c0++)
416for (c1 = c1min; c1 <= c1max; c1++) {
417histp = & histogram[c0][c1][c2min];
418for (c2 = c2min; c2 <= c2max; c2++, histp++)
419if (*histp != 0) {
420ccount++;
421}
422}
423boxp->colorcount = ccount;
424}
425
426
427LOCAL(int)
428median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
429int desired_colors)
430/* Repeatedly select and split the largest box until we have enough boxes */
431{
432int n,lb;
433int c0,c1,c2,cmax;
434register boxptr b1,b2;
435
436while (numboxes < desired_colors) {
437/* Select box to split.
438* Current algorithm: by population for first half, then by volume.
439*/
440if (numboxes*2 <= desired_colors) {
441b1 = find_biggest_color_pop(boxlist, numboxes);
442} else {
443b1 = find_biggest_volume(boxlist, numboxes);
444}
445if (b1 == NULL) /* no splittable boxes left! */
446break;
447b2 = &boxlist[numboxes]; /* where new box will go */
448/* Copy the color bounds to the new box. */
449b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
450b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
451/* Choose which axis to split the box on.
452* Current algorithm: longest scaled axis.
453* See notes in update_box about scaling distances.
454*/
455c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
456c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
457c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
458/* We want to break any ties in favor of green, then red, blue last.
459* This code does the right thing for R,G,B or B,G,R color orders only.
460*/
461#if RGB_RED == 0
462cmax = c1; n = 1;
463if (c0 > cmax) { cmax = c0; n = 0; }
464if (c2 > cmax) { n = 2; }
465#else
466cmax = c1; n = 1;
467if (c2 > cmax) { cmax = c2; n = 2; }
468if (c0 > cmax) { n = 0; }
469#endif
470/* Choose split point along selected axis, and update box bounds.
471* Current algorithm: split at halfway point.
472* (Since the box has been shrunk to minimum volume,
473* any split will produce two nonempty subboxes.)
474* Note that lb value is max for lower box, so must be < old max.
475*/
476switch (n) {
477case 0:
478lb = (b1->c0max + b1->c0min) / 2;
479b1->c0max = lb;
480b2->c0min = lb+1;
481break;
482case 1:
483lb = (b1->c1max + b1->c1min) / 2;
484b1->c1max = lb;
485b2->c1min = lb+1;
486break;
487case 2:
488lb = (b1->c2max + b1->c2min) / 2;
489b1->c2max = lb;
490b2->c2min = lb+1;
491break;
492}
493/* Update stats for boxes */
494update_box(cinfo, b1);
495update_box(cinfo, b2);
496numboxes++;
497}
498return numboxes;
499}
500
501
502LOCAL(void)
503compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
504/* Compute representative color for a box, put it in colormap[icolor] */
505{
506/* Current algorithm: mean weighted by pixels (not colors) */
507/* Note it is important to get the rounding correct! */
508my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
509hist3d histogram = cquantize->histogram;
510histptr histp;
511int c0,c1,c2;
512int c0min,c0max,c1min,c1max,c2min,c2max;
513long count;
514long total = 0;
515long c0total = 0;
516long c1total = 0;
517long c2total = 0;
518
519c0min = boxp->c0min; c0max = boxp->c0max;
520c1min = boxp->c1min; c1max = boxp->c1max;
521c2min = boxp->c2min; c2max = boxp->c2max;
522
523for (c0 = c0min; c0 <= c0max; c0++)
524for (c1 = c1min; c1 <= c1max; c1++) {
525histp = & histogram[c0][c1][c2min];
526for (c2 = c2min; c2 <= c2max; c2++) {
527if ((count = *histp++) != 0) {
528total += count;
529c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
530c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
531c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
532}
533}
534}
535
536cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
537cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
538cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
539}
540
541
542LOCAL(void)
543select_colors (j_decompress_ptr cinfo, int desired_colors)
544/* Master routine for color selection */
545{
546boxptr boxlist;
547int numboxes;
548int i;
549
550/* Allocate workspace for box list */
551boxlist = (boxptr) (*cinfo->mem->alloc_small)
552((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box));
553/* Initialize one box containing whole space */
554numboxes = 1;
555boxlist[0].c0min = 0;
556boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
557boxlist[0].c1min = 0;
558boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
559boxlist[0].c2min = 0;
560boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
561/* Shrink it to actually-used volume and set its statistics */
562update_box(cinfo, & boxlist[0]);
563/* Perform median-cut to produce final box list */
564numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors);
565/* Compute the representative color for each box, fill colormap */
566for (i = 0; i < numboxes; i++)
567compute_color(cinfo, & boxlist[i], i);
568cinfo->actual_number_of_colors = numboxes;
569TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
570}
571
572
573/*
574* These routines are concerned with the time-critical task of mapping input
575* colors to the nearest color in the selected colormap.
576*
577* We re-use the histogram space as an "inverse color map", essentially a
578* cache for the results of nearest-color searches. All colors within a
579* histogram cell will be mapped to the same colormap entry, namely the one
580* closest to the cell's center. This may not be quite the closest entry to
581* the actual input color, but it's almost as good. A zero in the cache
582* indicates we haven't found the nearest color for that cell yet; the array
583* is cleared to zeroes before starting the mapping pass. When we find the
584* nearest color for a cell, its colormap index plus one is recorded in the
585* cache for future use. The pass2 scanning routines call fill_inverse_cmap
586* when they need to use an unfilled entry in the cache.
587*
588* Our method of efficiently finding nearest colors is based on the "locally
589* sorted search" idea described by Heckbert and on the incremental distance
590* calculation described by Spencer W. Thomas in chapter III.1 of Graphics
591* Gems II (James Arvo, ed. Academic Press, 1991). Thomas points out that
592* the distances from a given colormap entry to each cell of the histogram can
593* be computed quickly using an incremental method: the differences between
594* distances to adjacent cells themselves differ by a constant. This allows a
595* fairly fast implementation of the "brute force" approach of computing the
596* distance from every colormap entry to every histogram cell. Unfortunately,
597* it needs a work array to hold the best-distance-so-far for each histogram
598* cell (because the inner loop has to be over cells, not colormap entries).
599* The work array elements have to be INT32s, so the work array would need
600* 256Kb at our recommended precision. This is not feasible in DOS machines.
601*
602* To get around these problems, we apply Thomas' method to compute the
603* nearest colors for only the cells within a small subbox of the histogram.
604* The work array need be only as big as the subbox, so the memory usage
605* problem is solved. Furthermore, we need not fill subboxes that are never
606* referenced in pass2; many images use only part of the color gamut, so a
607* fair amount of work is saved. An additional advantage of this
608* approach is that we can apply Heckbert's locality criterion to quickly
609* eliminate colormap entries that are far away from the subbox; typically
610* three-fourths of the colormap entries are rejected by Heckbert's criterion,
611* and we need not compute their distances to individual cells in the subbox.
612* The speed of this approach is heavily influenced by the subbox size: too
613* small means too much overhead, too big loses because Heckbert's criterion
614* can't eliminate as many colormap entries. Empirically the best subbox
615* size seems to be about 1/512th of the histogram (1/8th in each direction).
616*
617* Thomas' article also describes a refined method which is asymptotically
618* faster than the brute-force method, but it is also far more complex and
619* cannot efficiently be applied to small subboxes. It is therefore not
620* useful for programs intended to be portable to DOS machines. On machines
621* with plenty of memory, filling the whole histogram in one shot with Thomas'
622* refined method might be faster than the present code --- but then again,
623* it might not be any faster, and it's certainly more complicated.
624*/
625
626
627/* log2(histogram cells in update box) for each axis; this can be adjusted */
628#define BOX_C0_LOG (HIST_C0_BITS-3)
629#define BOX_C1_LOG (HIST_C1_BITS-3)
630#define BOX_C2_LOG (HIST_C2_BITS-3)
631
632#define BOX_C0_ELEMS (1<<BOX_C0_LOG) /* # of hist cells in update box */
633#define BOX_C1_ELEMS (1<<BOX_C1_LOG)
634#define BOX_C2_ELEMS (1<<BOX_C2_LOG)
635
636#define BOX_C0_SHIFT (C0_SHIFT + BOX_C0_LOG)
637#define BOX_C1_SHIFT (C1_SHIFT + BOX_C1_LOG)
638#define BOX_C2_SHIFT (C2_SHIFT + BOX_C2_LOG)
639
640
641/*
642* The next three routines implement inverse colormap filling. They could
643* all be folded into one big routine, but splitting them up this way saves
644* some stack space (the mindist[] and bestdist[] arrays need not coexist)
645* and may allow some compilers to produce better code by registerizing more
646* inner-loop variables.
647*/
648
649LOCAL(int)
650find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
651JSAMPLE colorlist[])
652/* Locate the colormap entries close enough to an update box to be candidates
653* for the nearest entry to some cell(s) in the update box. The update box
654* is specified by the center coordinates of its first cell. The number of
655* candidate colormap entries is returned, and their colormap indexes are
656* placed in colorlist[].
657* This routine uses Heckbert's "locally sorted search" criterion to select
658* the colors that need further consideration.
659*/
660{
661int numcolors = cinfo->actual_number_of_colors;
662int maxc0, maxc1, maxc2;
663int centerc0, centerc1, centerc2;
664int i, x, ncolors;
665INT32 minmaxdist, min_dist, max_dist, tdist;
666INT32 mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */
667
668/* Compute true coordinates of update box's upper corner and center.
669* Actually we compute the coordinates of the center of the upper-corner
670* histogram cell, which are the upper bounds of the volume we care about.
671* Note that since ">>" rounds down, the "center" values may be closer to
672* min than to max; hence comparisons to them must be "<=", not "<".
673*/
674maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
675centerc0 = (minc0 + maxc0) >> 1;
676maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
677centerc1 = (minc1 + maxc1) >> 1;
678maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
679centerc2 = (minc2 + maxc2) >> 1;
680
681/* For each color in colormap, find:
682* 1. its minimum squared-distance to any point in the update box
683* (zero if color is within update box);
684* 2. its maximum squared-distance to any point in the update box.
685* Both of these can be found by considering only the corners of the box.
686* We save the minimum distance for each color in mindist[];
687* only the smallest maximum distance is of interest.
688*/
689minmaxdist = 0x7FFFFFFFL;
690
691for (i = 0; i < numcolors; i++) {
692/* We compute the squared-c0-distance term, then add in the other two. */
693x = GETJSAMPLE(cinfo->colormap[0][i]);
694if (x < minc0) {
695tdist = (x - minc0) * C0_SCALE;
696min_dist = tdist*tdist;
697tdist = (x - maxc0) * C0_SCALE;
698max_dist = tdist*tdist;
699} else if (x > maxc0) {
700tdist = (x - maxc0) * C0_SCALE;
701min_dist = tdist*tdist;
702tdist = (x - minc0) * C0_SCALE;
703max_dist = tdist*tdist;
704} else {
705/* within cell range so no contribution to min_dist */
706min_dist = 0;
707if (x <= centerc0) {
708tdist = (x - maxc0) * C0_SCALE;
709max_dist = tdist*tdist;
710} else {
711tdist = (x - minc0) * C0_SCALE;
712max_dist = tdist*tdist;
713}
714}
715
716x = GETJSAMPLE(cinfo->colormap[1][i]);
717if (x < minc1) {
718tdist = (x - minc1) * C1_SCALE;
719min_dist += tdist*tdist;
720tdist = (x - maxc1) * C1_SCALE;
721max_dist += tdist*tdist;
722} else if (x > maxc1) {
723tdist = (x - maxc1) * C1_SCALE;
724min_dist += tdist*tdist;
725tdist = (x - minc1) * C1_SCALE;
726max_dist += tdist*tdist;
727} else {
728/* within cell range so no contribution to min_dist */
729if (x <= centerc1) {
730tdist = (x - maxc1) * C1_SCALE;
731max_dist += tdist*tdist;
732} else {
733tdist = (x - minc1) * C1_SCALE;
734max_dist += tdist*tdist;
735}
736}
737
738x = GETJSAMPLE(cinfo->colormap[2][i]);
739if (x < minc2) {
740tdist = (x - minc2) * C2_SCALE;
741min_dist += tdist*tdist;
742tdist = (x - maxc2) * C2_SCALE;
743max_dist += tdist*tdist;
744} else if (x > maxc2) {
745tdist = (x - maxc2) * C2_SCALE;
746min_dist += tdist*tdist;
747tdist = (x - minc2) * C2_SCALE;
748max_dist += tdist*tdist;
749} else {
750/* within cell range so no contribution to min_dist */
751if (x <= centerc2) {
752tdist = (x - maxc2) * C2_SCALE;
753max_dist += tdist*tdist;
754} else {
755tdist = (x - minc2) * C2_SCALE;
756max_dist += tdist*tdist;
757}
758}
759
760mindist[i] = min_dist; /* save away the results */
761if (max_dist < minmaxdist)
762minmaxdist = max_dist;
763}
764
765/* Now we know that no cell in the update box is more than minmaxdist
766* away from some colormap entry. Therefore, only colors that are
767* within minmaxdist of some part of the box need be considered.
768*/
769ncolors = 0;
770for (i = 0; i < numcolors; i++) {
771if (mindist[i] <= minmaxdist)
772colorlist[ncolors++] = (JSAMPLE) i;
773}
774return ncolors;
775}
776
777
778LOCAL(void)
779find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
780int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
781/* Find the closest colormap entry for each cell in the update box,
782* given the list of candidate colors prepared by find_nearby_colors.
783* Return the indexes of the closest entries in the bestcolor[] array.
784* This routine uses Thomas' incremental distance calculation method to
785* find the distance from a colormap entry to successive cells in the box.
786*/
787{
788int ic0, ic1, ic2;
789int i, icolor;
790register INT32 * bptr; /* pointer into bestdist[] array */
791JSAMPLE * cptr; /* pointer into bestcolor[] array */
792INT32 dist0, dist1; /* initial distance values */
793register INT32 dist2; /* current distance in inner loop */
794INT32 xx0, xx1; /* distance increments */
795register INT32 xx2;
796INT32 inc0, inc1, inc2; /* initial values for increments */
797/* This array holds the distance to the nearest-so-far color for each cell */
798INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
799
800/* Initialize best-distance for each cell of the update box */
801bptr = bestdist;
802for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
803*bptr++ = 0x7FFFFFFFL;
804
805/* For each color selected by find_nearby_colors,
806* compute its distance to the center of each cell in the box.
807* If that's less than best-so-far, update best distance and color number.
808*/
809
810/* Nominal steps between cell centers ("x" in Thomas article) */
811#define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE)
812#define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE)
813#define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE)
814
815for (i = 0; i < numcolors; i++) {
816icolor = GETJSAMPLE(colorlist[i]);
817/* Compute (square of) distance from minc0/c1/c2 to this color */
818inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
819dist0 = inc0*inc0;
820inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
821dist0 += inc1*inc1;
822inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
823dist0 += inc2*inc2;
824/* Form the initial difference increments */
825inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
826inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
827inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
828/* Now loop over all cells in box, updating distance per Thomas method */
829bptr = bestdist;
830cptr = bestcolor;
831xx0 = inc0;
832for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
833dist1 = dist0;
834xx1 = inc1;
835for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
836dist2 = dist1;
837xx2 = inc2;
838for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
839if (dist2 < *bptr) {
840*bptr = dist2;
841*cptr = (JSAMPLE) icolor;
842}
843dist2 += xx2;
844xx2 += 2 * STEP_C2 * STEP_C2;
845bptr++;
846cptr++;
847}
848dist1 += xx1;
849xx1 += 2 * STEP_C1 * STEP_C1;
850}
851dist0 += xx0;
852xx0 += 2 * STEP_C0 * STEP_C0;
853}
854}
855}
856
857
858LOCAL(void)
859fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
860/* Fill the inverse-colormap entries in the update box that contains */
861/* histogram cell c0/c1/c2. (Only that one cell MUST be filled, but */
862/* we can fill as many others as we wish.) */
863{
864my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
865hist3d histogram = cquantize->histogram;
866int minc0, minc1, minc2; /* lower left corner of update box */
867int ic0, ic1, ic2;
868register JSAMPLE * cptr; /* pointer into bestcolor[] array */
869register histptr cachep; /* pointer into main cache array */
870/* This array lists the candidate colormap indexes. */
871JSAMPLE colorlist[MAXNUMCOLORS];
872int numcolors; /* number of candidate colors */
873/* This array holds the actually closest colormap index for each cell. */
874JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
875
876/* Convert cell coordinates to update box ID */
877c0 >>= BOX_C0_LOG;
878c1 >>= BOX_C1_LOG;
879c2 >>= BOX_C2_LOG;
880
881/* Compute true coordinates of update box's origin corner.
882* Actually we compute the coordinates of the center of the corner
883* histogram cell, which are the lower bounds of the volume we care about.
884*/
885minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
886minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
887minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
888
889/* Determine which colormap entries are close enough to be candidates
890* for the nearest entry to some cell in the update box.
891*/
892numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
893
894/* Determine the actually nearest colors. */
895find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
896bestcolor);
897
898/* Save the best color numbers (plus 1) in the main cache array */
899c0 <<= BOX_C0_LOG; /* convert ID back to base cell indexes */
900c1 <<= BOX_C1_LOG;
901c2 <<= BOX_C2_LOG;
902cptr = bestcolor;
903for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
904for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
905cachep = & histogram[c0+ic0][c1+ic1][c2];
906for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
907*cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
908}
909}
910}
911}
912
913
914/*
915* Map some rows of pixels to the output colormapped representation.
916*/
917
918METHODDEF(void)
919pass2_no_dither (j_decompress_ptr cinfo,
920JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
921/* This version performs no dithering */
922{
923my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
924hist3d histogram = cquantize->histogram;
925register JSAMPROW inptr, outptr;
926register histptr cachep;
927register int c0, c1, c2;
928int row;
929JDIMENSION col;
930JDIMENSION width = cinfo->output_width;
931
932for (row = 0; row < num_rows; row++) {
933inptr = input_buf[row];
934outptr = output_buf[row];
935for (col = width; col > 0; col--) {
936/* get pixel value and index into the cache */
937c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
938c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
939c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
940cachep = & histogram[c0][c1][c2];
941/* If we have not seen this color before, find nearest colormap entry */
942/* and update the cache */
943if (*cachep == 0)
944fill_inverse_cmap(cinfo, c0,c1,c2);
945/* Now emit the colormap index for this cell */
946*outptr++ = (JSAMPLE) (*cachep - 1);
947}
948}
949}
950
951
952METHODDEF(void)
953pass2_fs_dither (j_decompress_ptr cinfo,
954JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
955/* This version performs Floyd-Steinberg dithering */
956{
957my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
958hist3d histogram = cquantize->histogram;
959register LOCFSERROR cur0, cur1, cur2; /* current error or pixel value */
960LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
961LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
962register FSERRPTR errorptr; /* => fserrors[] at column before current */
963JSAMPROW inptr; /* => current input pixel */
964JSAMPROW outptr; /* => current output pixel */
965histptr cachep;
966int dir; /* +1 or -1 depending on direction */
967int dir3; /* 3*dir, for advancing inptr & errorptr */
968int row;
969JDIMENSION col;
970JDIMENSION width = cinfo->output_width;
971JSAMPLE *range_limit = cinfo->sample_range_limit;
972int *error_limit = cquantize->error_limiter;
973JSAMPROW colormap0 = cinfo->colormap[0];
974JSAMPROW colormap1 = cinfo->colormap[1];
975JSAMPROW colormap2 = cinfo->colormap[2];
976SHIFT_TEMPS
977
978for (row = 0; row < num_rows; row++) {
979inptr = input_buf[row];
980outptr = output_buf[row];
981if (cquantize->on_odd_row) {
982/* work right to left in this row */
983inptr += (width-1) * 3; /* so point to rightmost pixel */
984outptr += width-1;
985dir = -1;
986dir3 = -3;
987errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
988cquantize->on_odd_row = FALSE; /* flip for next time */
989} else {
990/* work left to right in this row */
991dir = 1;
992dir3 = 3;
993errorptr = cquantize->fserrors; /* => entry before first real column */
994cquantize->on_odd_row = TRUE; /* flip for next time */
995}
996/* Preset error values: no error propagated to first pixel from left */
997cur0 = cur1 = cur2 = 0;
998/* and no error propagated to row below yet */
999belowerr0 = belowerr1 = belowerr2 = 0;
1000bpreverr0 = bpreverr1 = bpreverr2 = 0;
1001
1002for (col = width; col > 0; col--) {
1003/* curN holds the error propagated from the previous pixel on the
1004* current line. Add the error propagated from the previous line
1005* to form the complete error correction term for this pixel, and
1006* round the error term (which is expressed * 16) to an integer.
1007* RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
1008* for either sign of the error value.
1009* Note: errorptr points to *previous* column's array entry.
1010*/
1011cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
1012cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
1013cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
1014/* Limit the error using transfer function set by init_error_limit.
1015* See comments with init_error_limit for rationale.
1016*/
1017cur0 = error_limit[cur0];
1018cur1 = error_limit[cur1];
1019cur2 = error_limit[cur2];
1020/* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
1021* The maximum error is +- MAXJSAMPLE (or less with error limiting);
1022* this sets the required size of the range_limit array.
1023*/
1024cur0 += GETJSAMPLE(inptr[0]);
1025cur1 += GETJSAMPLE(inptr[1]);
1026cur2 += GETJSAMPLE(inptr[2]);
1027cur0 = GETJSAMPLE(range_limit[cur0]);
1028cur1 = GETJSAMPLE(range_limit[cur1]);
1029cur2 = GETJSAMPLE(range_limit[cur2]);
1030/* Index into the cache with adjusted pixel value */
1031cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
1032/* If we have not seen this color before, find nearest colormap */
1033/* entry and update the cache */
1034if (*cachep == 0)
1035fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
1036/* Now emit the colormap index for this cell */
1037{ register int pixcode = *cachep - 1;
1038*outptr = (JSAMPLE) pixcode;
1039/* Compute representation error for this pixel */
1040cur0 -= GETJSAMPLE(colormap0[pixcode]);
1041cur1 -= GETJSAMPLE(colormap1[pixcode]);
1042cur2 -= GETJSAMPLE(colormap2[pixcode]);
1043}
1044/* Compute error fractions to be propagated to adjacent pixels.
1045* Add these into the running sums, and simultaneously shift the
1046* next-line error sums left by 1 column.
1047*/
1048{ register LOCFSERROR bnexterr, delta;
1049
1050bnexterr = cur0; /* Process component 0 */
1051delta = cur0 * 2;
1052cur0 += delta; /* form error * 3 */
1053errorptr[0] = (FSERROR) (bpreverr0 + cur0);
1054cur0 += delta; /* form error * 5 */
1055bpreverr0 = belowerr0 + cur0;
1056belowerr0 = bnexterr;
1057cur0 += delta; /* form error * 7 */
1058bnexterr = cur1; /* Process component 1 */
1059delta = cur1 * 2;
1060cur1 += delta; /* form error * 3 */
1061errorptr[1] = (FSERROR) (bpreverr1 + cur1);
1062cur1 += delta; /* form error * 5 */
1063bpreverr1 = belowerr1 + cur1;
1064belowerr1 = bnexterr;
1065cur1 += delta; /* form error * 7 */
1066bnexterr = cur2; /* Process component 2 */
1067delta = cur2 * 2;
1068cur2 += delta; /* form error * 3 */
1069errorptr[2] = (FSERROR) (bpreverr2 + cur2);
1070cur2 += delta; /* form error * 5 */
1071bpreverr2 = belowerr2 + cur2;
1072belowerr2 = bnexterr;
1073cur2 += delta; /* form error * 7 */
1074}
1075/* At this point curN contains the 7/16 error value to be propagated
1076* to the next pixel on the current line, and all the errors for the
1077* next line have been shifted over. We are therefore ready to move on.
1078*/
1079inptr += dir3; /* Advance pixel pointers to next column */
1080outptr += dir;
1081errorptr += dir3; /* advance errorptr to current column */
1082}
1083/* Post-loop cleanup: we must unload the final error values into the
1084* final fserrors[] entry. Note we need not unload belowerrN because
1085* it is for the dummy column before or after the actual array.
1086*/
1087errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
1088errorptr[1] = (FSERROR) bpreverr1;
1089errorptr[2] = (FSERROR) bpreverr2;
1090}
1091}
1092
1093
1094/*
1095* Initialize the error-limiting transfer function (lookup table).
1096* The raw F-S error computation can potentially compute error values of up to
1097* +- MAXJSAMPLE. But we want the maximum correction applied to a pixel to be
1098* much less, otherwise obviously wrong pixels will be created. (Typical
1099* effects include weird fringes at color-area boundaries, isolated bright
1100* pixels in a dark area, etc.) The standard advice for avoiding this problem
1101* is to ensure that the "corners" of the color cube are allocated as output
1102* colors; then repeated errors in the same direction cannot cause cascading
1103* error buildup. However, that only prevents the error from getting
1104* completely out of hand; Aaron Giles reports that error limiting improves
1105* the results even with corner colors allocated.
1106* A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
1107* well, but the smoother transfer function used below is even better. Thanks
1108* to Aaron Giles for this idea.
1109*/
1110
1111LOCAL(void)
1112init_error_limit (j_decompress_ptr cinfo)
1113/* Allocate and fill in the error_limiter table */
1114{
1115my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1116int * table;
1117int in, out;
1118
1119table = (int *) (*cinfo->mem->alloc_small)
1120((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
1121table += MAXJSAMPLE; /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
1122cquantize->error_limiter = table;
1123
1124#define STEPSIZE ((MAXJSAMPLE+1)/16)
1125/* Map errors 1:1 up to +- MAXJSAMPLE/16 */
1126out = 0;
1127for (in = 0; in < STEPSIZE; in++, out++) {
1128table[in] = out; table[-in] = -out;
1129}
1130/* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
1131for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
1132table[in] = out; table[-in] = -out;
1133}
1134/* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
1135for (; in <= MAXJSAMPLE; in++) {
1136table[in] = out; table[-in] = -out;
1137}
1138#undef STEPSIZE
1139}
1140
1141
1142/*
1143* Finish up at the end of each pass.
1144*/
1145
1146METHODDEF(void)
1147finish_pass1 (j_decompress_ptr cinfo)
1148{
1149my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1150
1151/* Select the representative colors and fill in cinfo->colormap */
1152cinfo->colormap = cquantize->sv_colormap;
1153select_colors(cinfo, cquantize->desired);
1154/* Force next pass to zero the color index table */
1155cquantize->needs_zeroed = TRUE;
1156}
1157
1158
1159METHODDEF(void)
1160finish_pass2 (j_decompress_ptr cinfo)
1161{
1162/* no work */
1163}
1164
1165
1166/*
1167* Initialize for each processing pass.
1168*/
1169
1170METHODDEF(void)
1171start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
1172{
1173my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1174hist3d histogram = cquantize->histogram;
1175int i;
1176
1177/* Only F-S dithering or no dithering is supported. */
1178/* If user asks for ordered dither, give him F-S. */
1179if (cinfo->dither_mode != JDITHER_NONE)
1180cinfo->dither_mode = JDITHER_FS;
1181
1182if (is_pre_scan) {
1183/* Set up method pointers */
1184cquantize->pub.color_quantize = prescan_quantize;
1185cquantize->pub.finish_pass = finish_pass1;
1186cquantize->needs_zeroed = TRUE; /* Always zero histogram */
1187} else {
1188/* Set up method pointers */
1189if (cinfo->dither_mode == JDITHER_FS)
1190cquantize->pub.color_quantize = pass2_fs_dither;
1191else
1192cquantize->pub.color_quantize = pass2_no_dither;
1193cquantize->pub.finish_pass = finish_pass2;
1194
1195/* Make sure color count is acceptable */
1196i = cinfo->actual_number_of_colors;
1197if (i < 1)
1198ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
1199if (i > MAXNUMCOLORS)
1200ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
1201
1202if (cinfo->dither_mode == JDITHER_FS) {
1203size_t arraysize = (size_t) ((cinfo->output_width + 2) *
1204(3 * SIZEOF(FSERROR)));
1205/* Allocate Floyd-Steinberg workspace if we didn't already. */
1206if (cquantize->fserrors == NULL)
1207cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
1208((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
1209/* Initialize the propagated errors to zero. */
1210jzero_far((void FAR *) cquantize->fserrors, arraysize);
1211/* Make the error-limit table if we didn't already. */
1212if (cquantize->error_limiter == NULL)
1213init_error_limit(cinfo);
1214cquantize->on_odd_row = FALSE;
1215}
1216
1217}
1218/* Zero the histogram or inverse color map, if necessary */
1219if (cquantize->needs_zeroed) {
1220for (i = 0; i < HIST_C0_ELEMS; i++) {
1221jzero_far((void FAR *) histogram[i],
1222HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
1223}
1224cquantize->needs_zeroed = FALSE;
1225}
1226}
1227
1228
1229/*
1230* Switch to a new external colormap between output passes.
1231*/
1232
1233METHODDEF(void)
1234new_color_map_2_quant (j_decompress_ptr cinfo)
1235{
1236my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1237
1238/* Reset the inverse color map */
1239cquantize->needs_zeroed = TRUE;
1240}
1241
1242
1243/*
1244* Module initialization routine for 2-pass color quantization.
1245*/
1246
1247GLOBAL(void)
1248jinit_2pass_quantizer (j_decompress_ptr cinfo)
1249{
1250my_cquantize_ptr cquantize;
1251int i;
1252
1253cquantize = (my_cquantize_ptr)
1254(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1255SIZEOF(my_cquantizer));
1256cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
1257cquantize->pub.start_pass = start_pass_2_quant;
1258cquantize->pub.new_color_map = new_color_map_2_quant;
1259cquantize->fserrors = NULL; /* flag optional arrays not allocated */
1260cquantize->error_limiter = NULL;
1261
1262/* Make sure jdmaster didn't give me a case I can't handle */
1263if (cinfo->out_color_components != 3)
1264ERREXIT(cinfo, JERR_NOTIMPL);
1265
1266/* Allocate the histogram/inverse colormap storage */
1267cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
1268((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
1269for (i = 0; i < HIST_C0_ELEMS; i++) {
1270cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
1271((j_common_ptr) cinfo, JPOOL_IMAGE,
1272HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
1273}
1274cquantize->needs_zeroed = TRUE; /* histogram is garbage now */
1275
1276/* Allocate storage for the completed colormap, if required.
1277* We do this now since it is FAR storage and may affect
1278* the memory manager's space calculations.
1279*/
1280if (cinfo->enable_2pass_quant) {
1281/* Make sure color count is acceptable */
1282int desired = cinfo->desired_number_of_colors;
1283/* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
1284if (desired < 8)
1285ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
1286/* Make sure colormap indexes can be represented by JSAMPLEs */
1287if (desired > MAXNUMCOLORS)
1288ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
1289cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
1290((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
1291cquantize->desired = desired;
1292} else
1293cquantize->sv_colormap = NULL;
1294
1295/* Only F-S dithering or no dithering is supported. */
1296/* If user asks for ordered dither, give him F-S. */
1297if (cinfo->dither_mode != JDITHER_NONE)
1298cinfo->dither_mode = JDITHER_FS;
1299
1300/* Allocate Floyd-Steinberg workspace if necessary.
1301* This isn't really needed until pass 2, but again it is FAR storage.
1302* Although we will cope with a later change in dither_mode,
1303* we do not promise to honor max_memory_to_use if dither_mode changes.
1304*/
1305if (cinfo->dither_mode == JDITHER_FS) {
1306cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
1307((j_common_ptr) cinfo, JPOOL_IMAGE,
1308(size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
1309/* Might as well create the error-limiting table too. */
1310init_error_limit(cinfo);
1311}
1312}
1313
1314#endif /* QUANT_2PASS_SUPPORTED */
1315