1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker * jquant1.c
3*dfc6aa5cSAndroid Build Coastguard Worker *
4*dfc6aa5cSAndroid Build Coastguard Worker * This file was part of the Independent JPEG Group's software:
5*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 1991-1996, Thomas G. Lane.
6*dfc6aa5cSAndroid Build Coastguard Worker * libjpeg-turbo Modifications:
7*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 2009, 2015, D. R. Commander.
8*dfc6aa5cSAndroid Build Coastguard Worker * For conditions of distribution and use, see the accompanying README.ijg
9*dfc6aa5cSAndroid Build Coastguard Worker * file.
10*dfc6aa5cSAndroid Build Coastguard Worker *
11*dfc6aa5cSAndroid Build Coastguard Worker * This file contains 1-pass color quantization (color mapping) routines.
12*dfc6aa5cSAndroid Build Coastguard Worker * These routines provide mapping to a fixed color map using equally spaced
13*dfc6aa5cSAndroid Build Coastguard Worker * color values. Optional Floyd-Steinberg or ordered dithering is available.
14*dfc6aa5cSAndroid Build Coastguard Worker */
15*dfc6aa5cSAndroid Build Coastguard Worker
16*dfc6aa5cSAndroid Build Coastguard Worker #define JPEG_INTERNALS
17*dfc6aa5cSAndroid Build Coastguard Worker #include "jinclude.h"
18*dfc6aa5cSAndroid Build Coastguard Worker #include "jpeglib.h"
19*dfc6aa5cSAndroid Build Coastguard Worker
20*dfc6aa5cSAndroid Build Coastguard Worker #ifdef QUANT_1PASS_SUPPORTED
21*dfc6aa5cSAndroid Build Coastguard Worker
22*dfc6aa5cSAndroid Build Coastguard Worker
23*dfc6aa5cSAndroid Build Coastguard Worker /*
24*dfc6aa5cSAndroid Build Coastguard Worker * The main purpose of 1-pass quantization is to provide a fast, if not very
25*dfc6aa5cSAndroid Build Coastguard Worker * high quality, colormapped output capability. A 2-pass quantizer usually
26*dfc6aa5cSAndroid Build Coastguard Worker * gives better visual quality; however, for quantized grayscale output this
27*dfc6aa5cSAndroid Build Coastguard Worker * quantizer is perfectly adequate. Dithering is highly recommended with this
28*dfc6aa5cSAndroid Build Coastguard Worker * quantizer, though you can turn it off if you really want to.
29*dfc6aa5cSAndroid Build Coastguard Worker *
30*dfc6aa5cSAndroid Build Coastguard Worker * In 1-pass quantization the colormap must be chosen in advance of seeing the
31*dfc6aa5cSAndroid Build Coastguard Worker * image. We use a map consisting of all combinations of Ncolors[i] color
32*dfc6aa5cSAndroid Build Coastguard Worker * values for the i'th component. The Ncolors[] values are chosen so that
33*dfc6aa5cSAndroid Build Coastguard Worker * their product, the total number of colors, is no more than that requested.
34*dfc6aa5cSAndroid Build Coastguard Worker * (In most cases, the product will be somewhat less.)
35*dfc6aa5cSAndroid Build Coastguard Worker *
36*dfc6aa5cSAndroid Build Coastguard Worker * Since the colormap is orthogonal, the representative value for each color
37*dfc6aa5cSAndroid Build Coastguard Worker * component can be determined without considering the other components;
38*dfc6aa5cSAndroid Build Coastguard Worker * then these indexes can be combined into a colormap index by a standard
39*dfc6aa5cSAndroid Build Coastguard Worker * N-dimensional-array-subscript calculation. Most of the arithmetic involved
40*dfc6aa5cSAndroid Build Coastguard Worker * can be precalculated and stored in the lookup table colorindex[].
41*dfc6aa5cSAndroid Build Coastguard Worker * colorindex[i][j] maps pixel value j in component i to the nearest
42*dfc6aa5cSAndroid Build Coastguard Worker * representative value (grid plane) for that component; this index is
43*dfc6aa5cSAndroid Build Coastguard Worker * multiplied by the array stride for component i, so that the
44*dfc6aa5cSAndroid Build Coastguard Worker * index of the colormap entry closest to a given pixel value is just
45*dfc6aa5cSAndroid Build Coastguard Worker * sum( colorindex[component-number][pixel-component-value] )
46*dfc6aa5cSAndroid Build Coastguard Worker * Aside from being fast, this scheme allows for variable spacing between
47*dfc6aa5cSAndroid Build Coastguard Worker * representative values with no additional lookup cost.
48*dfc6aa5cSAndroid Build Coastguard Worker *
49*dfc6aa5cSAndroid Build Coastguard Worker * If gamma correction has been applied in color conversion, it might be wise
50*dfc6aa5cSAndroid Build Coastguard Worker * to adjust the color grid spacing so that the representative colors are
51*dfc6aa5cSAndroid Build Coastguard Worker * equidistant in linear space. At this writing, gamma correction is not
52*dfc6aa5cSAndroid Build Coastguard Worker * implemented by jdcolor, so nothing is done here.
53*dfc6aa5cSAndroid Build Coastguard Worker */
54*dfc6aa5cSAndroid Build Coastguard Worker
55*dfc6aa5cSAndroid Build Coastguard Worker
56*dfc6aa5cSAndroid Build Coastguard Worker /* Declarations for ordered dithering.
57*dfc6aa5cSAndroid Build Coastguard Worker *
58*dfc6aa5cSAndroid Build Coastguard Worker * We use a standard 16x16 ordered dither array. The basic concept of ordered
59*dfc6aa5cSAndroid Build Coastguard Worker * dithering is described in many references, for instance Dale Schumacher's
60*dfc6aa5cSAndroid Build Coastguard Worker * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991).
61*dfc6aa5cSAndroid Build Coastguard Worker * In place of Schumacher's comparisons against a "threshold" value, we add a
62*dfc6aa5cSAndroid Build Coastguard Worker * "dither" value to the input pixel and then round the result to the nearest
63*dfc6aa5cSAndroid Build Coastguard Worker * output value. The dither value is equivalent to (0.5 - threshold) times
64*dfc6aa5cSAndroid Build Coastguard Worker * the distance between output values. For ordered dithering, we assume that
65*dfc6aa5cSAndroid Build Coastguard Worker * the output colors are equally spaced; if not, results will probably be
66*dfc6aa5cSAndroid Build Coastguard Worker * worse, since the dither may be too much or too little at a given point.
67*dfc6aa5cSAndroid Build Coastguard Worker *
68*dfc6aa5cSAndroid Build Coastguard Worker * The normal calculation would be to form pixel value + dither, range-limit
69*dfc6aa5cSAndroid Build Coastguard Worker * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual.
70*dfc6aa5cSAndroid Build Coastguard Worker * We can skip the separate range-limiting step by extending the colorindex
71*dfc6aa5cSAndroid Build Coastguard Worker * table in both directions.
72*dfc6aa5cSAndroid Build Coastguard Worker */
73*dfc6aa5cSAndroid Build Coastguard Worker
74*dfc6aa5cSAndroid Build Coastguard Worker #define ODITHER_SIZE 16 /* dimension of dither matrix */
75*dfc6aa5cSAndroid Build Coastguard Worker /* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */
76*dfc6aa5cSAndroid Build Coastguard Worker #define ODITHER_CELLS (ODITHER_SIZE * ODITHER_SIZE) /* # cells in matrix */
77*dfc6aa5cSAndroid Build Coastguard Worker #define ODITHER_MASK (ODITHER_SIZE - 1) /* mask for wrapping around
78*dfc6aa5cSAndroid Build Coastguard Worker counters */
79*dfc6aa5cSAndroid Build Coastguard Worker
80*dfc6aa5cSAndroid Build Coastguard Worker typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
81*dfc6aa5cSAndroid Build Coastguard Worker typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];
82*dfc6aa5cSAndroid Build Coastguard Worker
83*dfc6aa5cSAndroid Build Coastguard Worker static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
84*dfc6aa5cSAndroid Build Coastguard Worker /* Bayer's order-4 dither array. Generated by the code given in
85*dfc6aa5cSAndroid Build Coastguard Worker * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I.
86*dfc6aa5cSAndroid Build Coastguard Worker * The values in this array must range from 0 to ODITHER_CELLS-1.
87*dfc6aa5cSAndroid Build Coastguard Worker */
88*dfc6aa5cSAndroid Build Coastguard Worker { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
89*dfc6aa5cSAndroid Build Coastguard Worker { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
90*dfc6aa5cSAndroid Build Coastguard Worker { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
91*dfc6aa5cSAndroid Build Coastguard Worker { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
92*dfc6aa5cSAndroid Build Coastguard Worker { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
93*dfc6aa5cSAndroid Build Coastguard Worker { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
94*dfc6aa5cSAndroid Build Coastguard Worker { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
95*dfc6aa5cSAndroid Build Coastguard Worker { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
96*dfc6aa5cSAndroid Build Coastguard Worker { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
97*dfc6aa5cSAndroid Build Coastguard Worker { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
98*dfc6aa5cSAndroid Build Coastguard Worker { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
99*dfc6aa5cSAndroid Build Coastguard Worker { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
100*dfc6aa5cSAndroid Build Coastguard Worker { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
101*dfc6aa5cSAndroid Build Coastguard Worker { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
102*dfc6aa5cSAndroid Build Coastguard Worker { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
103*dfc6aa5cSAndroid Build Coastguard Worker { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
104*dfc6aa5cSAndroid Build Coastguard Worker };
105*dfc6aa5cSAndroid Build Coastguard Worker
106*dfc6aa5cSAndroid Build Coastguard Worker
107*dfc6aa5cSAndroid Build Coastguard Worker /* Declarations for Floyd-Steinberg dithering.
108*dfc6aa5cSAndroid Build Coastguard Worker *
109*dfc6aa5cSAndroid Build Coastguard Worker * Errors are accumulated into the array fserrors[], at a resolution of
110*dfc6aa5cSAndroid Build Coastguard Worker * 1/16th of a pixel count. The error at a given pixel is propagated
111*dfc6aa5cSAndroid Build Coastguard Worker * to its not-yet-processed neighbors using the standard F-S fractions,
112*dfc6aa5cSAndroid Build Coastguard Worker * ... (here) 7/16
113*dfc6aa5cSAndroid Build Coastguard Worker * 3/16 5/16 1/16
114*dfc6aa5cSAndroid Build Coastguard Worker * We work left-to-right on even rows, right-to-left on odd rows.
115*dfc6aa5cSAndroid Build Coastguard Worker *
116*dfc6aa5cSAndroid Build Coastguard Worker * We can get away with a single array (holding one row's worth of errors)
117*dfc6aa5cSAndroid Build Coastguard Worker * by using it to store the current row's errors at pixel columns not yet
118*dfc6aa5cSAndroid Build Coastguard Worker * processed, but the next row's errors at columns already processed. We
119*dfc6aa5cSAndroid Build Coastguard Worker * need only a few extra variables to hold the errors immediately around the
120*dfc6aa5cSAndroid Build Coastguard Worker * current column. (If we are lucky, those variables are in registers, but
121*dfc6aa5cSAndroid Build Coastguard Worker * even if not, they're probably cheaper to access than array elements are.)
122*dfc6aa5cSAndroid Build Coastguard Worker *
123*dfc6aa5cSAndroid Build Coastguard Worker * The fserrors[] array is indexed [component#][position].
124*dfc6aa5cSAndroid Build Coastguard Worker * We provide (#columns + 2) entries per component; the extra entry at each
125*dfc6aa5cSAndroid Build Coastguard Worker * end saves us from special-casing the first and last pixels.
126*dfc6aa5cSAndroid Build Coastguard Worker */
127*dfc6aa5cSAndroid Build Coastguard Worker
128*dfc6aa5cSAndroid Build Coastguard Worker #if BITS_IN_JSAMPLE == 8
129*dfc6aa5cSAndroid Build Coastguard Worker typedef INT16 FSERROR; /* 16 bits should be enough */
130*dfc6aa5cSAndroid Build Coastguard Worker typedef int LOCFSERROR; /* use 'int' for calculation temps */
131*dfc6aa5cSAndroid Build Coastguard Worker #else
132*dfc6aa5cSAndroid Build Coastguard Worker typedef JLONG FSERROR; /* may need more than 16 bits */
133*dfc6aa5cSAndroid Build Coastguard Worker typedef JLONG LOCFSERROR; /* be sure calculation temps are big enough */
134*dfc6aa5cSAndroid Build Coastguard Worker #endif
135*dfc6aa5cSAndroid Build Coastguard Worker
136*dfc6aa5cSAndroid Build Coastguard Worker typedef FSERROR *FSERRPTR; /* pointer to error array */
137*dfc6aa5cSAndroid Build Coastguard Worker
138*dfc6aa5cSAndroid Build Coastguard Worker
139*dfc6aa5cSAndroid Build Coastguard Worker /* Private subobject */
140*dfc6aa5cSAndroid Build Coastguard Worker
141*dfc6aa5cSAndroid Build Coastguard Worker #define MAX_Q_COMPS 4 /* max components I can handle */
142*dfc6aa5cSAndroid Build Coastguard Worker
143*dfc6aa5cSAndroid Build Coastguard Worker typedef struct {
144*dfc6aa5cSAndroid Build Coastguard Worker struct jpeg_color_quantizer pub; /* public fields */
145*dfc6aa5cSAndroid Build Coastguard Worker
146*dfc6aa5cSAndroid Build Coastguard Worker /* Initially allocated colormap is saved here */
147*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */
148*dfc6aa5cSAndroid Build Coastguard Worker int sv_actual; /* number of entries in use */
149*dfc6aa5cSAndroid Build Coastguard Worker
150*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY colorindex; /* Precomputed mapping for speed */
151*dfc6aa5cSAndroid Build Coastguard Worker /* colorindex[i][j] = index of color closest to pixel value j in component i,
152*dfc6aa5cSAndroid Build Coastguard Worker * premultiplied as described above. Since colormap indexes must fit into
153*dfc6aa5cSAndroid Build Coastguard Worker * JSAMPLEs, the entries of this array will too.
154*dfc6aa5cSAndroid Build Coastguard Worker */
155*dfc6aa5cSAndroid Build Coastguard Worker boolean is_padded; /* is the colorindex padded for odither? */
156*dfc6aa5cSAndroid Build Coastguard Worker
157*dfc6aa5cSAndroid Build Coastguard Worker int Ncolors[MAX_Q_COMPS]; /* # of values allocated to each component */
158*dfc6aa5cSAndroid Build Coastguard Worker
159*dfc6aa5cSAndroid Build Coastguard Worker /* Variables for ordered dithering */
160*dfc6aa5cSAndroid Build Coastguard Worker int row_index; /* cur row's vertical index in dither matrix */
161*dfc6aa5cSAndroid Build Coastguard Worker ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
162*dfc6aa5cSAndroid Build Coastguard Worker
163*dfc6aa5cSAndroid Build Coastguard Worker /* Variables for Floyd-Steinberg dithering */
164*dfc6aa5cSAndroid Build Coastguard Worker FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
165*dfc6aa5cSAndroid Build Coastguard Worker boolean on_odd_row; /* flag to remember which row we are on */
166*dfc6aa5cSAndroid Build Coastguard Worker } my_cquantizer;
167*dfc6aa5cSAndroid Build Coastguard Worker
168*dfc6aa5cSAndroid Build Coastguard Worker typedef my_cquantizer *my_cquantize_ptr;
169*dfc6aa5cSAndroid Build Coastguard Worker
170*dfc6aa5cSAndroid Build Coastguard Worker
171*dfc6aa5cSAndroid Build Coastguard Worker /*
172*dfc6aa5cSAndroid Build Coastguard Worker * Policy-making subroutines for create_colormap and create_colorindex.
173*dfc6aa5cSAndroid Build Coastguard Worker * These routines determine the colormap to be used. The rest of the module
174*dfc6aa5cSAndroid Build Coastguard Worker * only assumes that the colormap is orthogonal.
175*dfc6aa5cSAndroid Build Coastguard Worker *
176*dfc6aa5cSAndroid Build Coastguard Worker * * select_ncolors decides how to divvy up the available colors
177*dfc6aa5cSAndroid Build Coastguard Worker * among the components.
178*dfc6aa5cSAndroid Build Coastguard Worker * * output_value defines the set of representative values for a component.
179*dfc6aa5cSAndroid Build Coastguard Worker * * largest_input_value defines the mapping from input values to
180*dfc6aa5cSAndroid Build Coastguard Worker * representative values for a component.
181*dfc6aa5cSAndroid Build Coastguard Worker * Note that the latter two routines may impose different policies for
182*dfc6aa5cSAndroid Build Coastguard Worker * different components, though this is not currently done.
183*dfc6aa5cSAndroid Build Coastguard Worker */
184*dfc6aa5cSAndroid Build Coastguard Worker
185*dfc6aa5cSAndroid Build Coastguard Worker
186*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(int)
select_ncolors(j_decompress_ptr cinfo,int Ncolors[])187*dfc6aa5cSAndroid Build Coastguard Worker select_ncolors(j_decompress_ptr cinfo, int Ncolors[])
188*dfc6aa5cSAndroid Build Coastguard Worker /* Determine allocation of desired colors to components, */
189*dfc6aa5cSAndroid Build Coastguard Worker /* and fill in Ncolors[] array to indicate choice. */
190*dfc6aa5cSAndroid Build Coastguard Worker /* Return value is total number of colors (product of Ncolors[] values). */
191*dfc6aa5cSAndroid Build Coastguard Worker {
192*dfc6aa5cSAndroid Build Coastguard Worker int nc = cinfo->out_color_components; /* number of color components */
193*dfc6aa5cSAndroid Build Coastguard Worker int max_colors = cinfo->desired_number_of_colors;
194*dfc6aa5cSAndroid Build Coastguard Worker int total_colors, iroot, i, j;
195*dfc6aa5cSAndroid Build Coastguard Worker boolean changed;
196*dfc6aa5cSAndroid Build Coastguard Worker long temp;
197*dfc6aa5cSAndroid Build Coastguard Worker int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
198*dfc6aa5cSAndroid Build Coastguard Worker RGB_order[0] = rgb_green[cinfo->out_color_space];
199*dfc6aa5cSAndroid Build Coastguard Worker RGB_order[1] = rgb_red[cinfo->out_color_space];
200*dfc6aa5cSAndroid Build Coastguard Worker RGB_order[2] = rgb_blue[cinfo->out_color_space];
201*dfc6aa5cSAndroid Build Coastguard Worker
202*dfc6aa5cSAndroid Build Coastguard Worker /* We can allocate at least the nc'th root of max_colors per component. */
203*dfc6aa5cSAndroid Build Coastguard Worker /* Compute floor(nc'th root of max_colors). */
204*dfc6aa5cSAndroid Build Coastguard Worker iroot = 1;
205*dfc6aa5cSAndroid Build Coastguard Worker do {
206*dfc6aa5cSAndroid Build Coastguard Worker iroot++;
207*dfc6aa5cSAndroid Build Coastguard Worker temp = iroot; /* set temp = iroot ** nc */
208*dfc6aa5cSAndroid Build Coastguard Worker for (i = 1; i < nc; i++)
209*dfc6aa5cSAndroid Build Coastguard Worker temp *= iroot;
210*dfc6aa5cSAndroid Build Coastguard Worker } while (temp <= (long)max_colors); /* repeat till iroot exceeds root */
211*dfc6aa5cSAndroid Build Coastguard Worker iroot--; /* now iroot = floor(root) */
212*dfc6aa5cSAndroid Build Coastguard Worker
213*dfc6aa5cSAndroid Build Coastguard Worker /* Must have at least 2 color values per component */
214*dfc6aa5cSAndroid Build Coastguard Worker if (iroot < 2)
215*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int)temp);
216*dfc6aa5cSAndroid Build Coastguard Worker
217*dfc6aa5cSAndroid Build Coastguard Worker /* Initialize to iroot color values for each component */
218*dfc6aa5cSAndroid Build Coastguard Worker total_colors = 1;
219*dfc6aa5cSAndroid Build Coastguard Worker for (i = 0; i < nc; i++) {
220*dfc6aa5cSAndroid Build Coastguard Worker Ncolors[i] = iroot;
221*dfc6aa5cSAndroid Build Coastguard Worker total_colors *= iroot;
222*dfc6aa5cSAndroid Build Coastguard Worker }
223*dfc6aa5cSAndroid Build Coastguard Worker /* We may be able to increment the count for one or more components without
224*dfc6aa5cSAndroid Build Coastguard Worker * exceeding max_colors, though we know not all can be incremented.
225*dfc6aa5cSAndroid Build Coastguard Worker * Sometimes, the first component can be incremented more than once!
226*dfc6aa5cSAndroid Build Coastguard Worker * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.)
227*dfc6aa5cSAndroid Build Coastguard Worker * In RGB colorspace, try to increment G first, then R, then B.
228*dfc6aa5cSAndroid Build Coastguard Worker */
229*dfc6aa5cSAndroid Build Coastguard Worker do {
230*dfc6aa5cSAndroid Build Coastguard Worker changed = FALSE;
231*dfc6aa5cSAndroid Build Coastguard Worker for (i = 0; i < nc; i++) {
232*dfc6aa5cSAndroid Build Coastguard Worker j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
233*dfc6aa5cSAndroid Build Coastguard Worker /* calculate new total_colors if Ncolors[j] is incremented */
234*dfc6aa5cSAndroid Build Coastguard Worker temp = total_colors / Ncolors[j];
235*dfc6aa5cSAndroid Build Coastguard Worker temp *= Ncolors[j] + 1; /* done in long arith to avoid oflo */
236*dfc6aa5cSAndroid Build Coastguard Worker if (temp > (long)max_colors)
237*dfc6aa5cSAndroid Build Coastguard Worker break; /* won't fit, done with this pass */
238*dfc6aa5cSAndroid Build Coastguard Worker Ncolors[j]++; /* OK, apply the increment */
239*dfc6aa5cSAndroid Build Coastguard Worker total_colors = (int)temp;
240*dfc6aa5cSAndroid Build Coastguard Worker changed = TRUE;
241*dfc6aa5cSAndroid Build Coastguard Worker }
242*dfc6aa5cSAndroid Build Coastguard Worker } while (changed);
243*dfc6aa5cSAndroid Build Coastguard Worker
244*dfc6aa5cSAndroid Build Coastguard Worker return total_colors;
245*dfc6aa5cSAndroid Build Coastguard Worker }
246*dfc6aa5cSAndroid Build Coastguard Worker
247*dfc6aa5cSAndroid Build Coastguard Worker
248*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(int)
output_value(j_decompress_ptr cinfo,int ci,int j,int maxj)249*dfc6aa5cSAndroid Build Coastguard Worker output_value(j_decompress_ptr cinfo, int ci, int j, int maxj)
250*dfc6aa5cSAndroid Build Coastguard Worker /* Return j'th output value, where j will range from 0 to maxj */
251*dfc6aa5cSAndroid Build Coastguard Worker /* The output values must fall in 0..MAXJSAMPLE in increasing order */
252*dfc6aa5cSAndroid Build Coastguard Worker {
253*dfc6aa5cSAndroid Build Coastguard Worker /* We always provide values 0 and MAXJSAMPLE for each component;
254*dfc6aa5cSAndroid Build Coastguard Worker * any additional values are equally spaced between these limits.
255*dfc6aa5cSAndroid Build Coastguard Worker * (Forcing the upper and lower values to the limits ensures that
256*dfc6aa5cSAndroid Build Coastguard Worker * dithering can't produce a color outside the selected gamut.)
257*dfc6aa5cSAndroid Build Coastguard Worker */
258*dfc6aa5cSAndroid Build Coastguard Worker return (int)(((JLONG)j * MAXJSAMPLE + maxj / 2) / maxj);
259*dfc6aa5cSAndroid Build Coastguard Worker }
260*dfc6aa5cSAndroid Build Coastguard Worker
261*dfc6aa5cSAndroid Build Coastguard Worker
262*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(int)
largest_input_value(j_decompress_ptr cinfo,int ci,int j,int maxj)263*dfc6aa5cSAndroid Build Coastguard Worker largest_input_value(j_decompress_ptr cinfo, int ci, int j, int maxj)
264*dfc6aa5cSAndroid Build Coastguard Worker /* Return largest input value that should map to j'th output value */
265*dfc6aa5cSAndroid Build Coastguard Worker /* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
266*dfc6aa5cSAndroid Build Coastguard Worker {
267*dfc6aa5cSAndroid Build Coastguard Worker /* Breakpoints are halfway between values returned by output_value */
268*dfc6aa5cSAndroid Build Coastguard Worker return (int)(((JLONG)(2 * j + 1) * MAXJSAMPLE + maxj) / (2 * maxj));
269*dfc6aa5cSAndroid Build Coastguard Worker }
270*dfc6aa5cSAndroid Build Coastguard Worker
271*dfc6aa5cSAndroid Build Coastguard Worker
272*dfc6aa5cSAndroid Build Coastguard Worker /*
273*dfc6aa5cSAndroid Build Coastguard Worker * Create the colormap.
274*dfc6aa5cSAndroid Build Coastguard Worker */
275*dfc6aa5cSAndroid Build Coastguard Worker
276*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
create_colormap(j_decompress_ptr cinfo)277*dfc6aa5cSAndroid Build Coastguard Worker create_colormap(j_decompress_ptr cinfo)
278*dfc6aa5cSAndroid Build Coastguard Worker {
279*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
280*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY colormap; /* Created colormap */
281*dfc6aa5cSAndroid Build Coastguard Worker int total_colors; /* Number of distinct output colors */
282*dfc6aa5cSAndroid Build Coastguard Worker int i, j, k, nci, blksize, blkdist, ptr, val;
283*dfc6aa5cSAndroid Build Coastguard Worker
284*dfc6aa5cSAndroid Build Coastguard Worker /* Select number of colors for each component */
285*dfc6aa5cSAndroid Build Coastguard Worker total_colors = select_ncolors(cinfo, cquantize->Ncolors);
286*dfc6aa5cSAndroid Build Coastguard Worker
287*dfc6aa5cSAndroid Build Coastguard Worker /* Report selected color counts */
288*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->out_color_components == 3)
289*dfc6aa5cSAndroid Build Coastguard Worker TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS, total_colors,
290*dfc6aa5cSAndroid Build Coastguard Worker cquantize->Ncolors[0], cquantize->Ncolors[1],
291*dfc6aa5cSAndroid Build Coastguard Worker cquantize->Ncolors[2]);
292*dfc6aa5cSAndroid Build Coastguard Worker else
293*dfc6aa5cSAndroid Build Coastguard Worker TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
294*dfc6aa5cSAndroid Build Coastguard Worker
295*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate and fill in the colormap. */
296*dfc6aa5cSAndroid Build Coastguard Worker /* The colors are ordered in the map in standard row-major order, */
297*dfc6aa5cSAndroid Build Coastguard Worker /* i.e. rightmost (highest-indexed) color changes most rapidly. */
298*dfc6aa5cSAndroid Build Coastguard Worker
299*dfc6aa5cSAndroid Build Coastguard Worker colormap = (*cinfo->mem->alloc_sarray)
300*dfc6aa5cSAndroid Build Coastguard Worker ((j_common_ptr)cinfo, JPOOL_IMAGE,
301*dfc6aa5cSAndroid Build Coastguard Worker (JDIMENSION)total_colors, (JDIMENSION)cinfo->out_color_components);
302*dfc6aa5cSAndroid Build Coastguard Worker
303*dfc6aa5cSAndroid Build Coastguard Worker /* blksize is number of adjacent repeated entries for a component */
304*dfc6aa5cSAndroid Build Coastguard Worker /* blkdist is distance between groups of identical entries for a component */
305*dfc6aa5cSAndroid Build Coastguard Worker blkdist = total_colors;
306*dfc6aa5cSAndroid Build Coastguard Worker
307*dfc6aa5cSAndroid Build Coastguard Worker for (i = 0; i < cinfo->out_color_components; i++) {
308*dfc6aa5cSAndroid Build Coastguard Worker /* fill in colormap entries for i'th color component */
309*dfc6aa5cSAndroid Build Coastguard Worker nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
310*dfc6aa5cSAndroid Build Coastguard Worker blksize = blkdist / nci;
311*dfc6aa5cSAndroid Build Coastguard Worker for (j = 0; j < nci; j++) {
312*dfc6aa5cSAndroid Build Coastguard Worker /* Compute j'th output value (out of nci) for component */
313*dfc6aa5cSAndroid Build Coastguard Worker val = output_value(cinfo, i, j, nci - 1);
314*dfc6aa5cSAndroid Build Coastguard Worker /* Fill in all colormap entries that have this value of this component */
315*dfc6aa5cSAndroid Build Coastguard Worker for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
316*dfc6aa5cSAndroid Build Coastguard Worker /* fill in blksize entries beginning at ptr */
317*dfc6aa5cSAndroid Build Coastguard Worker for (k = 0; k < blksize; k++)
318*dfc6aa5cSAndroid Build Coastguard Worker colormap[i][ptr + k] = (JSAMPLE)val;
319*dfc6aa5cSAndroid Build Coastguard Worker }
320*dfc6aa5cSAndroid Build Coastguard Worker }
321*dfc6aa5cSAndroid Build Coastguard Worker blkdist = blksize; /* blksize of this color is blkdist of next */
322*dfc6aa5cSAndroid Build Coastguard Worker }
323*dfc6aa5cSAndroid Build Coastguard Worker
324*dfc6aa5cSAndroid Build Coastguard Worker /* Save the colormap in private storage,
325*dfc6aa5cSAndroid Build Coastguard Worker * where it will survive color quantization mode changes.
326*dfc6aa5cSAndroid Build Coastguard Worker */
327*dfc6aa5cSAndroid Build Coastguard Worker cquantize->sv_colormap = colormap;
328*dfc6aa5cSAndroid Build Coastguard Worker cquantize->sv_actual = total_colors;
329*dfc6aa5cSAndroid Build Coastguard Worker }
330*dfc6aa5cSAndroid Build Coastguard Worker
331*dfc6aa5cSAndroid Build Coastguard Worker
332*dfc6aa5cSAndroid Build Coastguard Worker /*
333*dfc6aa5cSAndroid Build Coastguard Worker * Create the color index table.
334*dfc6aa5cSAndroid Build Coastguard Worker */
335*dfc6aa5cSAndroid Build Coastguard Worker
336*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
create_colorindex(j_decompress_ptr cinfo)337*dfc6aa5cSAndroid Build Coastguard Worker create_colorindex(j_decompress_ptr cinfo)
338*dfc6aa5cSAndroid Build Coastguard Worker {
339*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
340*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW indexptr;
341*dfc6aa5cSAndroid Build Coastguard Worker int i, j, k, nci, blksize, val, pad;
342*dfc6aa5cSAndroid Build Coastguard Worker
343*dfc6aa5cSAndroid Build Coastguard Worker /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
344*dfc6aa5cSAndroid Build Coastguard Worker * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
345*dfc6aa5cSAndroid Build Coastguard Worker * This is not necessary in the other dithering modes. However, we
346*dfc6aa5cSAndroid Build Coastguard Worker * flag whether it was done in case user changes dithering mode.
347*dfc6aa5cSAndroid Build Coastguard Worker */
348*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->dither_mode == JDITHER_ORDERED) {
349*dfc6aa5cSAndroid Build Coastguard Worker pad = MAXJSAMPLE * 2;
350*dfc6aa5cSAndroid Build Coastguard Worker cquantize->is_padded = TRUE;
351*dfc6aa5cSAndroid Build Coastguard Worker } else {
352*dfc6aa5cSAndroid Build Coastguard Worker pad = 0;
353*dfc6aa5cSAndroid Build Coastguard Worker cquantize->is_padded = FALSE;
354*dfc6aa5cSAndroid Build Coastguard Worker }
355*dfc6aa5cSAndroid Build Coastguard Worker
356*dfc6aa5cSAndroid Build Coastguard Worker cquantize->colorindex = (*cinfo->mem->alloc_sarray)
357*dfc6aa5cSAndroid Build Coastguard Worker ((j_common_ptr)cinfo, JPOOL_IMAGE,
358*dfc6aa5cSAndroid Build Coastguard Worker (JDIMENSION)(MAXJSAMPLE + 1 + pad),
359*dfc6aa5cSAndroid Build Coastguard Worker (JDIMENSION)cinfo->out_color_components);
360*dfc6aa5cSAndroid Build Coastguard Worker
361*dfc6aa5cSAndroid Build Coastguard Worker /* blksize is number of adjacent repeated entries for a component */
362*dfc6aa5cSAndroid Build Coastguard Worker blksize = cquantize->sv_actual;
363*dfc6aa5cSAndroid Build Coastguard Worker
364*dfc6aa5cSAndroid Build Coastguard Worker for (i = 0; i < cinfo->out_color_components; i++) {
365*dfc6aa5cSAndroid Build Coastguard Worker /* fill in colorindex entries for i'th color component */
366*dfc6aa5cSAndroid Build Coastguard Worker nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
367*dfc6aa5cSAndroid Build Coastguard Worker blksize = blksize / nci;
368*dfc6aa5cSAndroid Build Coastguard Worker
369*dfc6aa5cSAndroid Build Coastguard Worker /* adjust colorindex pointers to provide padding at negative indexes. */
370*dfc6aa5cSAndroid Build Coastguard Worker if (pad)
371*dfc6aa5cSAndroid Build Coastguard Worker cquantize->colorindex[i] += MAXJSAMPLE;
372*dfc6aa5cSAndroid Build Coastguard Worker
373*dfc6aa5cSAndroid Build Coastguard Worker /* in loop, val = index of current output value, */
374*dfc6aa5cSAndroid Build Coastguard Worker /* and k = largest j that maps to current val */
375*dfc6aa5cSAndroid Build Coastguard Worker indexptr = cquantize->colorindex[i];
376*dfc6aa5cSAndroid Build Coastguard Worker val = 0;
377*dfc6aa5cSAndroid Build Coastguard Worker k = largest_input_value(cinfo, i, 0, nci - 1);
378*dfc6aa5cSAndroid Build Coastguard Worker for (j = 0; j <= MAXJSAMPLE; j++) {
379*dfc6aa5cSAndroid Build Coastguard Worker while (j > k) /* advance val if past boundary */
380*dfc6aa5cSAndroid Build Coastguard Worker k = largest_input_value(cinfo, i, ++val, nci - 1);
381*dfc6aa5cSAndroid Build Coastguard Worker /* premultiply so that no multiplication needed in main processing */
382*dfc6aa5cSAndroid Build Coastguard Worker indexptr[j] = (JSAMPLE)(val * blksize);
383*dfc6aa5cSAndroid Build Coastguard Worker }
384*dfc6aa5cSAndroid Build Coastguard Worker /* Pad at both ends if necessary */
385*dfc6aa5cSAndroid Build Coastguard Worker if (pad)
386*dfc6aa5cSAndroid Build Coastguard Worker for (j = 1; j <= MAXJSAMPLE; j++) {
387*dfc6aa5cSAndroid Build Coastguard Worker indexptr[-j] = indexptr[0];
388*dfc6aa5cSAndroid Build Coastguard Worker indexptr[MAXJSAMPLE + j] = indexptr[MAXJSAMPLE];
389*dfc6aa5cSAndroid Build Coastguard Worker }
390*dfc6aa5cSAndroid Build Coastguard Worker }
391*dfc6aa5cSAndroid Build Coastguard Worker }
392*dfc6aa5cSAndroid Build Coastguard Worker
393*dfc6aa5cSAndroid Build Coastguard Worker
394*dfc6aa5cSAndroid Build Coastguard Worker /*
395*dfc6aa5cSAndroid Build Coastguard Worker * Create an ordered-dither array for a component having ncolors
396*dfc6aa5cSAndroid Build Coastguard Worker * distinct output values.
397*dfc6aa5cSAndroid Build Coastguard Worker */
398*dfc6aa5cSAndroid Build Coastguard Worker
399*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(ODITHER_MATRIX_PTR)
make_odither_array(j_decompress_ptr cinfo,int ncolors)400*dfc6aa5cSAndroid Build Coastguard Worker make_odither_array(j_decompress_ptr cinfo, int ncolors)
401*dfc6aa5cSAndroid Build Coastguard Worker {
402*dfc6aa5cSAndroid Build Coastguard Worker ODITHER_MATRIX_PTR odither;
403*dfc6aa5cSAndroid Build Coastguard Worker int j, k;
404*dfc6aa5cSAndroid Build Coastguard Worker JLONG num, den;
405*dfc6aa5cSAndroid Build Coastguard Worker
406*dfc6aa5cSAndroid Build Coastguard Worker odither = (ODITHER_MATRIX_PTR)
407*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
408*dfc6aa5cSAndroid Build Coastguard Worker sizeof(ODITHER_MATRIX));
409*dfc6aa5cSAndroid Build Coastguard Worker /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
410*dfc6aa5cSAndroid Build Coastguard Worker * Hence the dither value for the matrix cell with fill order f
411*dfc6aa5cSAndroid Build Coastguard Worker * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
412*dfc6aa5cSAndroid Build Coastguard Worker * On 16-bit-int machine, be careful to avoid overflow.
413*dfc6aa5cSAndroid Build Coastguard Worker */
414*dfc6aa5cSAndroid Build Coastguard Worker den = 2 * ODITHER_CELLS * ((JLONG)(ncolors - 1));
415*dfc6aa5cSAndroid Build Coastguard Worker for (j = 0; j < ODITHER_SIZE; j++) {
416*dfc6aa5cSAndroid Build Coastguard Worker for (k = 0; k < ODITHER_SIZE; k++) {
417*dfc6aa5cSAndroid Build Coastguard Worker num = ((JLONG)(ODITHER_CELLS - 1 -
418*dfc6aa5cSAndroid Build Coastguard Worker 2 * ((int)base_dither_matrix[j][k]))) * MAXJSAMPLE;
419*dfc6aa5cSAndroid Build Coastguard Worker /* Ensure round towards zero despite C's lack of consistency
420*dfc6aa5cSAndroid Build Coastguard Worker * about rounding negative values in integer division...
421*dfc6aa5cSAndroid Build Coastguard Worker */
422*dfc6aa5cSAndroid Build Coastguard Worker odither[j][k] = (int)(num < 0 ? -((-num) / den) : num / den);
423*dfc6aa5cSAndroid Build Coastguard Worker }
424*dfc6aa5cSAndroid Build Coastguard Worker }
425*dfc6aa5cSAndroid Build Coastguard Worker return odither;
426*dfc6aa5cSAndroid Build Coastguard Worker }
427*dfc6aa5cSAndroid Build Coastguard Worker
428*dfc6aa5cSAndroid Build Coastguard Worker
429*dfc6aa5cSAndroid Build Coastguard Worker /*
430*dfc6aa5cSAndroid Build Coastguard Worker * Create the ordered-dither tables.
431*dfc6aa5cSAndroid Build Coastguard Worker * Components having the same number of representative colors may
432*dfc6aa5cSAndroid Build Coastguard Worker * share a dither table.
433*dfc6aa5cSAndroid Build Coastguard Worker */
434*dfc6aa5cSAndroid Build Coastguard Worker
435*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
create_odither_tables(j_decompress_ptr cinfo)436*dfc6aa5cSAndroid Build Coastguard Worker create_odither_tables(j_decompress_ptr cinfo)
437*dfc6aa5cSAndroid Build Coastguard Worker {
438*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
439*dfc6aa5cSAndroid Build Coastguard Worker ODITHER_MATRIX_PTR odither;
440*dfc6aa5cSAndroid Build Coastguard Worker int i, j, nci;
441*dfc6aa5cSAndroid Build Coastguard Worker
442*dfc6aa5cSAndroid Build Coastguard Worker for (i = 0; i < cinfo->out_color_components; i++) {
443*dfc6aa5cSAndroid Build Coastguard Worker nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
444*dfc6aa5cSAndroid Build Coastguard Worker odither = NULL; /* search for matching prior component */
445*dfc6aa5cSAndroid Build Coastguard Worker for (j = 0; j < i; j++) {
446*dfc6aa5cSAndroid Build Coastguard Worker if (nci == cquantize->Ncolors[j]) {
447*dfc6aa5cSAndroid Build Coastguard Worker odither = cquantize->odither[j];
448*dfc6aa5cSAndroid Build Coastguard Worker break;
449*dfc6aa5cSAndroid Build Coastguard Worker }
450*dfc6aa5cSAndroid Build Coastguard Worker }
451*dfc6aa5cSAndroid Build Coastguard Worker if (odither == NULL) /* need a new table? */
452*dfc6aa5cSAndroid Build Coastguard Worker odither = make_odither_array(cinfo, nci);
453*dfc6aa5cSAndroid Build Coastguard Worker cquantize->odither[i] = odither;
454*dfc6aa5cSAndroid Build Coastguard Worker }
455*dfc6aa5cSAndroid Build Coastguard Worker }
456*dfc6aa5cSAndroid Build Coastguard Worker
457*dfc6aa5cSAndroid Build Coastguard Worker
458*dfc6aa5cSAndroid Build Coastguard Worker /*
459*dfc6aa5cSAndroid Build Coastguard Worker * Map some rows of pixels to the output colormapped representation.
460*dfc6aa5cSAndroid Build Coastguard Worker */
461*dfc6aa5cSAndroid Build Coastguard Worker
462*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
color_quantize(j_decompress_ptr cinfo,JSAMPARRAY input_buf,JSAMPARRAY output_buf,int num_rows)463*dfc6aa5cSAndroid Build Coastguard Worker color_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
464*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, int num_rows)
465*dfc6aa5cSAndroid Build Coastguard Worker /* General case, no dithering */
466*dfc6aa5cSAndroid Build Coastguard Worker {
467*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
468*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY colorindex = cquantize->colorindex;
469*dfc6aa5cSAndroid Build Coastguard Worker register int pixcode, ci;
470*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW ptrin, ptrout;
471*dfc6aa5cSAndroid Build Coastguard Worker int row;
472*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION col;
473*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION width = cinfo->output_width;
474*dfc6aa5cSAndroid Build Coastguard Worker register int nc = cinfo->out_color_components;
475*dfc6aa5cSAndroid Build Coastguard Worker
476*dfc6aa5cSAndroid Build Coastguard Worker for (row = 0; row < num_rows; row++) {
477*dfc6aa5cSAndroid Build Coastguard Worker ptrin = input_buf[row];
478*dfc6aa5cSAndroid Build Coastguard Worker ptrout = output_buf[row];
479*dfc6aa5cSAndroid Build Coastguard Worker for (col = width; col > 0; col--) {
480*dfc6aa5cSAndroid Build Coastguard Worker pixcode = 0;
481*dfc6aa5cSAndroid Build Coastguard Worker for (ci = 0; ci < nc; ci++) {
482*dfc6aa5cSAndroid Build Coastguard Worker pixcode += colorindex[ci][*ptrin++];
483*dfc6aa5cSAndroid Build Coastguard Worker }
484*dfc6aa5cSAndroid Build Coastguard Worker *ptrout++ = (JSAMPLE)pixcode;
485*dfc6aa5cSAndroid Build Coastguard Worker }
486*dfc6aa5cSAndroid Build Coastguard Worker }
487*dfc6aa5cSAndroid Build Coastguard Worker }
488*dfc6aa5cSAndroid Build Coastguard Worker
489*dfc6aa5cSAndroid Build Coastguard Worker
490*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
color_quantize3(j_decompress_ptr cinfo,JSAMPARRAY input_buf,JSAMPARRAY output_buf,int num_rows)491*dfc6aa5cSAndroid Build Coastguard Worker color_quantize3(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
492*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, int num_rows)
493*dfc6aa5cSAndroid Build Coastguard Worker /* Fast path for out_color_components==3, no dithering */
494*dfc6aa5cSAndroid Build Coastguard Worker {
495*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
496*dfc6aa5cSAndroid Build Coastguard Worker register int pixcode;
497*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW ptrin, ptrout;
498*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex0 = cquantize->colorindex[0];
499*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex1 = cquantize->colorindex[1];
500*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex2 = cquantize->colorindex[2];
501*dfc6aa5cSAndroid Build Coastguard Worker int row;
502*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION col;
503*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION width = cinfo->output_width;
504*dfc6aa5cSAndroid Build Coastguard Worker
505*dfc6aa5cSAndroid Build Coastguard Worker for (row = 0; row < num_rows; row++) {
506*dfc6aa5cSAndroid Build Coastguard Worker ptrin = input_buf[row];
507*dfc6aa5cSAndroid Build Coastguard Worker ptrout = output_buf[row];
508*dfc6aa5cSAndroid Build Coastguard Worker for (col = width; col > 0; col--) {
509*dfc6aa5cSAndroid Build Coastguard Worker pixcode = colorindex0[*ptrin++];
510*dfc6aa5cSAndroid Build Coastguard Worker pixcode += colorindex1[*ptrin++];
511*dfc6aa5cSAndroid Build Coastguard Worker pixcode += colorindex2[*ptrin++];
512*dfc6aa5cSAndroid Build Coastguard Worker *ptrout++ = (JSAMPLE)pixcode;
513*dfc6aa5cSAndroid Build Coastguard Worker }
514*dfc6aa5cSAndroid Build Coastguard Worker }
515*dfc6aa5cSAndroid Build Coastguard Worker }
516*dfc6aa5cSAndroid Build Coastguard Worker
517*dfc6aa5cSAndroid Build Coastguard Worker
518*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
quantize_ord_dither(j_decompress_ptr cinfo,JSAMPARRAY input_buf,JSAMPARRAY output_buf,int num_rows)519*dfc6aa5cSAndroid Build Coastguard Worker quantize_ord_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
520*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, int num_rows)
521*dfc6aa5cSAndroid Build Coastguard Worker /* General case, with ordered dithering */
522*dfc6aa5cSAndroid Build Coastguard Worker {
523*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
524*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW input_ptr;
525*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW output_ptr;
526*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex_ci;
527*dfc6aa5cSAndroid Build Coastguard Worker int *dither; /* points to active row of dither matrix */
528*dfc6aa5cSAndroid Build Coastguard Worker int row_index, col_index; /* current indexes into dither matrix */
529*dfc6aa5cSAndroid Build Coastguard Worker int nc = cinfo->out_color_components;
530*dfc6aa5cSAndroid Build Coastguard Worker int ci;
531*dfc6aa5cSAndroid Build Coastguard Worker int row;
532*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION col;
533*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION width = cinfo->output_width;
534*dfc6aa5cSAndroid Build Coastguard Worker
535*dfc6aa5cSAndroid Build Coastguard Worker for (row = 0; row < num_rows; row++) {
536*dfc6aa5cSAndroid Build Coastguard Worker /* Initialize output values to 0 so can process components separately */
537*dfc6aa5cSAndroid Build Coastguard Worker jzero_far((void *)output_buf[row], (size_t)(width * sizeof(JSAMPLE)));
538*dfc6aa5cSAndroid Build Coastguard Worker row_index = cquantize->row_index;
539*dfc6aa5cSAndroid Build Coastguard Worker for (ci = 0; ci < nc; ci++) {
540*dfc6aa5cSAndroid Build Coastguard Worker input_ptr = input_buf[row] + ci;
541*dfc6aa5cSAndroid Build Coastguard Worker output_ptr = output_buf[row];
542*dfc6aa5cSAndroid Build Coastguard Worker colorindex_ci = cquantize->colorindex[ci];
543*dfc6aa5cSAndroid Build Coastguard Worker dither = cquantize->odither[ci][row_index];
544*dfc6aa5cSAndroid Build Coastguard Worker col_index = 0;
545*dfc6aa5cSAndroid Build Coastguard Worker
546*dfc6aa5cSAndroid Build Coastguard Worker for (col = width; col > 0; col--) {
547*dfc6aa5cSAndroid Build Coastguard Worker /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
548*dfc6aa5cSAndroid Build Coastguard Worker * select output value, accumulate into output code for this pixel.
549*dfc6aa5cSAndroid Build Coastguard Worker * Range-limiting need not be done explicitly, as we have extended
550*dfc6aa5cSAndroid Build Coastguard Worker * the colorindex table to produce the right answers for out-of-range
551*dfc6aa5cSAndroid Build Coastguard Worker * inputs. The maximum dither is +- MAXJSAMPLE; this sets the
552*dfc6aa5cSAndroid Build Coastguard Worker * required amount of padding.
553*dfc6aa5cSAndroid Build Coastguard Worker */
554*dfc6aa5cSAndroid Build Coastguard Worker *output_ptr +=
555*dfc6aa5cSAndroid Build Coastguard Worker colorindex_ci[*input_ptr + dither[col_index]];
556*dfc6aa5cSAndroid Build Coastguard Worker input_ptr += nc;
557*dfc6aa5cSAndroid Build Coastguard Worker output_ptr++;
558*dfc6aa5cSAndroid Build Coastguard Worker col_index = (col_index + 1) & ODITHER_MASK;
559*dfc6aa5cSAndroid Build Coastguard Worker }
560*dfc6aa5cSAndroid Build Coastguard Worker }
561*dfc6aa5cSAndroid Build Coastguard Worker /* Advance row index for next row */
562*dfc6aa5cSAndroid Build Coastguard Worker row_index = (row_index + 1) & ODITHER_MASK;
563*dfc6aa5cSAndroid Build Coastguard Worker cquantize->row_index = row_index;
564*dfc6aa5cSAndroid Build Coastguard Worker }
565*dfc6aa5cSAndroid Build Coastguard Worker }
566*dfc6aa5cSAndroid Build Coastguard Worker
567*dfc6aa5cSAndroid Build Coastguard Worker
568*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
quantize3_ord_dither(j_decompress_ptr cinfo,JSAMPARRAY input_buf,JSAMPARRAY output_buf,int num_rows)569*dfc6aa5cSAndroid Build Coastguard Worker quantize3_ord_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
570*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, int num_rows)
571*dfc6aa5cSAndroid Build Coastguard Worker /* Fast path for out_color_components==3, with ordered dithering */
572*dfc6aa5cSAndroid Build Coastguard Worker {
573*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
574*dfc6aa5cSAndroid Build Coastguard Worker register int pixcode;
575*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW input_ptr;
576*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW output_ptr;
577*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex0 = cquantize->colorindex[0];
578*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex1 = cquantize->colorindex[1];
579*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex2 = cquantize->colorindex[2];
580*dfc6aa5cSAndroid Build Coastguard Worker int *dither0; /* points to active row of dither matrix */
581*dfc6aa5cSAndroid Build Coastguard Worker int *dither1;
582*dfc6aa5cSAndroid Build Coastguard Worker int *dither2;
583*dfc6aa5cSAndroid Build Coastguard Worker int row_index, col_index; /* current indexes into dither matrix */
584*dfc6aa5cSAndroid Build Coastguard Worker int row;
585*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION col;
586*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION width = cinfo->output_width;
587*dfc6aa5cSAndroid Build Coastguard Worker
588*dfc6aa5cSAndroid Build Coastguard Worker for (row = 0; row < num_rows; row++) {
589*dfc6aa5cSAndroid Build Coastguard Worker row_index = cquantize->row_index;
590*dfc6aa5cSAndroid Build Coastguard Worker input_ptr = input_buf[row];
591*dfc6aa5cSAndroid Build Coastguard Worker output_ptr = output_buf[row];
592*dfc6aa5cSAndroid Build Coastguard Worker dither0 = cquantize->odither[0][row_index];
593*dfc6aa5cSAndroid Build Coastguard Worker dither1 = cquantize->odither[1][row_index];
594*dfc6aa5cSAndroid Build Coastguard Worker dither2 = cquantize->odither[2][row_index];
595*dfc6aa5cSAndroid Build Coastguard Worker col_index = 0;
596*dfc6aa5cSAndroid Build Coastguard Worker
597*dfc6aa5cSAndroid Build Coastguard Worker for (col = width; col > 0; col--) {
598*dfc6aa5cSAndroid Build Coastguard Worker pixcode = colorindex0[(*input_ptr++) + dither0[col_index]];
599*dfc6aa5cSAndroid Build Coastguard Worker pixcode += colorindex1[(*input_ptr++) + dither1[col_index]];
600*dfc6aa5cSAndroid Build Coastguard Worker pixcode += colorindex2[(*input_ptr++) + dither2[col_index]];
601*dfc6aa5cSAndroid Build Coastguard Worker *output_ptr++ = (JSAMPLE)pixcode;
602*dfc6aa5cSAndroid Build Coastguard Worker col_index = (col_index + 1) & ODITHER_MASK;
603*dfc6aa5cSAndroid Build Coastguard Worker }
604*dfc6aa5cSAndroid Build Coastguard Worker row_index = (row_index + 1) & ODITHER_MASK;
605*dfc6aa5cSAndroid Build Coastguard Worker cquantize->row_index = row_index;
606*dfc6aa5cSAndroid Build Coastguard Worker }
607*dfc6aa5cSAndroid Build Coastguard Worker }
608*dfc6aa5cSAndroid Build Coastguard Worker
609*dfc6aa5cSAndroid Build Coastguard Worker
610*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
quantize_fs_dither(j_decompress_ptr cinfo,JSAMPARRAY input_buf,JSAMPARRAY output_buf,int num_rows)611*dfc6aa5cSAndroid Build Coastguard Worker quantize_fs_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
612*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_buf, int num_rows)
613*dfc6aa5cSAndroid Build Coastguard Worker /* General case, with Floyd-Steinberg dithering */
614*dfc6aa5cSAndroid Build Coastguard Worker {
615*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
616*dfc6aa5cSAndroid Build Coastguard Worker register LOCFSERROR cur; /* current error or pixel value */
617*dfc6aa5cSAndroid Build Coastguard Worker LOCFSERROR belowerr; /* error for pixel below cur */
618*dfc6aa5cSAndroid Build Coastguard Worker LOCFSERROR bpreverr; /* error for below/prev col */
619*dfc6aa5cSAndroid Build Coastguard Worker LOCFSERROR bnexterr; /* error for below/next col */
620*dfc6aa5cSAndroid Build Coastguard Worker LOCFSERROR delta;
621*dfc6aa5cSAndroid Build Coastguard Worker register FSERRPTR errorptr; /* => fserrors[] at column before current */
622*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW input_ptr;
623*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW output_ptr;
624*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colorindex_ci;
625*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW colormap_ci;
626*dfc6aa5cSAndroid Build Coastguard Worker int pixcode;
627*dfc6aa5cSAndroid Build Coastguard Worker int nc = cinfo->out_color_components;
628*dfc6aa5cSAndroid Build Coastguard Worker int dir; /* 1 for left-to-right, -1 for right-to-left */
629*dfc6aa5cSAndroid Build Coastguard Worker int dirnc; /* dir * nc */
630*dfc6aa5cSAndroid Build Coastguard Worker int ci;
631*dfc6aa5cSAndroid Build Coastguard Worker int row;
632*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION col;
633*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION width = cinfo->output_width;
634*dfc6aa5cSAndroid Build Coastguard Worker JSAMPLE *range_limit = cinfo->sample_range_limit;
635*dfc6aa5cSAndroid Build Coastguard Worker SHIFT_TEMPS
636*dfc6aa5cSAndroid Build Coastguard Worker
637*dfc6aa5cSAndroid Build Coastguard Worker for (row = 0; row < num_rows; row++) {
638*dfc6aa5cSAndroid Build Coastguard Worker /* Initialize output values to 0 so can process components separately */
639*dfc6aa5cSAndroid Build Coastguard Worker jzero_far((void *)output_buf[row], (size_t)(width * sizeof(JSAMPLE)));
640*dfc6aa5cSAndroid Build Coastguard Worker for (ci = 0; ci < nc; ci++) {
641*dfc6aa5cSAndroid Build Coastguard Worker input_ptr = input_buf[row] + ci;
642*dfc6aa5cSAndroid Build Coastguard Worker output_ptr = output_buf[row];
643*dfc6aa5cSAndroid Build Coastguard Worker if (cquantize->on_odd_row) {
644*dfc6aa5cSAndroid Build Coastguard Worker /* work right to left in this row */
645*dfc6aa5cSAndroid Build Coastguard Worker input_ptr += (width - 1) * nc; /* so point to rightmost pixel */
646*dfc6aa5cSAndroid Build Coastguard Worker output_ptr += width - 1;
647*dfc6aa5cSAndroid Build Coastguard Worker dir = -1;
648*dfc6aa5cSAndroid Build Coastguard Worker dirnc = -nc;
649*dfc6aa5cSAndroid Build Coastguard Worker errorptr = cquantize->fserrors[ci] + (width + 1); /* => entry after last column */
650*dfc6aa5cSAndroid Build Coastguard Worker } else {
651*dfc6aa5cSAndroid Build Coastguard Worker /* work left to right in this row */
652*dfc6aa5cSAndroid Build Coastguard Worker dir = 1;
653*dfc6aa5cSAndroid Build Coastguard Worker dirnc = nc;
654*dfc6aa5cSAndroid Build Coastguard Worker errorptr = cquantize->fserrors[ci]; /* => entry before first column */
655*dfc6aa5cSAndroid Build Coastguard Worker }
656*dfc6aa5cSAndroid Build Coastguard Worker colorindex_ci = cquantize->colorindex[ci];
657*dfc6aa5cSAndroid Build Coastguard Worker colormap_ci = cquantize->sv_colormap[ci];
658*dfc6aa5cSAndroid Build Coastguard Worker /* Preset error values: no error propagated to first pixel from left */
659*dfc6aa5cSAndroid Build Coastguard Worker cur = 0;
660*dfc6aa5cSAndroid Build Coastguard Worker /* and no error propagated to row below yet */
661*dfc6aa5cSAndroid Build Coastguard Worker belowerr = bpreverr = 0;
662*dfc6aa5cSAndroid Build Coastguard Worker
663*dfc6aa5cSAndroid Build Coastguard Worker for (col = width; col > 0; col--) {
664*dfc6aa5cSAndroid Build Coastguard Worker /* cur holds the error propagated from the previous pixel on the
665*dfc6aa5cSAndroid Build Coastguard Worker * current line. Add the error propagated from the previous line
666*dfc6aa5cSAndroid Build Coastguard Worker * to form the complete error correction term for this pixel, and
667*dfc6aa5cSAndroid Build Coastguard Worker * round the error term (which is expressed * 16) to an integer.
668*dfc6aa5cSAndroid Build Coastguard Worker * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
669*dfc6aa5cSAndroid Build Coastguard Worker * for either sign of the error value.
670*dfc6aa5cSAndroid Build Coastguard Worker * Note: errorptr points to *previous* column's array entry.
671*dfc6aa5cSAndroid Build Coastguard Worker */
672*dfc6aa5cSAndroid Build Coastguard Worker cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
673*dfc6aa5cSAndroid Build Coastguard Worker /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
674*dfc6aa5cSAndroid Build Coastguard Worker * The maximum error is +- MAXJSAMPLE; this sets the required size
675*dfc6aa5cSAndroid Build Coastguard Worker * of the range_limit array.
676*dfc6aa5cSAndroid Build Coastguard Worker */
677*dfc6aa5cSAndroid Build Coastguard Worker cur += *input_ptr;
678*dfc6aa5cSAndroid Build Coastguard Worker cur = range_limit[cur];
679*dfc6aa5cSAndroid Build Coastguard Worker /* Select output value, accumulate into output code for this pixel */
680*dfc6aa5cSAndroid Build Coastguard Worker pixcode = colorindex_ci[cur];
681*dfc6aa5cSAndroid Build Coastguard Worker *output_ptr += (JSAMPLE)pixcode;
682*dfc6aa5cSAndroid Build Coastguard Worker /* Compute actual representation error at this pixel */
683*dfc6aa5cSAndroid Build Coastguard Worker /* Note: we can do this even though we don't have the final */
684*dfc6aa5cSAndroid Build Coastguard Worker /* pixel code, because the colormap is orthogonal. */
685*dfc6aa5cSAndroid Build Coastguard Worker cur -= colormap_ci[pixcode];
686*dfc6aa5cSAndroid Build Coastguard Worker /* Compute error fractions to be propagated to adjacent pixels.
687*dfc6aa5cSAndroid Build Coastguard Worker * Add these into the running sums, and simultaneously shift the
688*dfc6aa5cSAndroid Build Coastguard Worker * next-line error sums left by 1 column.
689*dfc6aa5cSAndroid Build Coastguard Worker */
690*dfc6aa5cSAndroid Build Coastguard Worker bnexterr = cur;
691*dfc6aa5cSAndroid Build Coastguard Worker delta = cur * 2;
692*dfc6aa5cSAndroid Build Coastguard Worker cur += delta; /* form error * 3 */
693*dfc6aa5cSAndroid Build Coastguard Worker errorptr[0] = (FSERROR)(bpreverr + cur);
694*dfc6aa5cSAndroid Build Coastguard Worker cur += delta; /* form error * 5 */
695*dfc6aa5cSAndroid Build Coastguard Worker bpreverr = belowerr + cur;
696*dfc6aa5cSAndroid Build Coastguard Worker belowerr = bnexterr;
697*dfc6aa5cSAndroid Build Coastguard Worker cur += delta; /* form error * 7 */
698*dfc6aa5cSAndroid Build Coastguard Worker /* At this point cur contains the 7/16 error value to be propagated
699*dfc6aa5cSAndroid Build Coastguard Worker * to the next pixel on the current line, and all the errors for the
700*dfc6aa5cSAndroid Build Coastguard Worker * next line have been shifted over. We are therefore ready to move on.
701*dfc6aa5cSAndroid Build Coastguard Worker */
702*dfc6aa5cSAndroid Build Coastguard Worker input_ptr += dirnc; /* advance input ptr to next column */
703*dfc6aa5cSAndroid Build Coastguard Worker output_ptr += dir; /* advance output ptr to next column */
704*dfc6aa5cSAndroid Build Coastguard Worker errorptr += dir; /* advance errorptr to current column */
705*dfc6aa5cSAndroid Build Coastguard Worker }
706*dfc6aa5cSAndroid Build Coastguard Worker /* Post-loop cleanup: we must unload the final error value into the
707*dfc6aa5cSAndroid Build Coastguard Worker * final fserrors[] entry. Note we need not unload belowerr because
708*dfc6aa5cSAndroid Build Coastguard Worker * it is for the dummy column before or after the actual array.
709*dfc6aa5cSAndroid Build Coastguard Worker */
710*dfc6aa5cSAndroid Build Coastguard Worker errorptr[0] = (FSERROR)bpreverr; /* unload prev err into array */
711*dfc6aa5cSAndroid Build Coastguard Worker }
712*dfc6aa5cSAndroid Build Coastguard Worker cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
713*dfc6aa5cSAndroid Build Coastguard Worker }
714*dfc6aa5cSAndroid Build Coastguard Worker }
715*dfc6aa5cSAndroid Build Coastguard Worker
716*dfc6aa5cSAndroid Build Coastguard Worker
717*dfc6aa5cSAndroid Build Coastguard Worker /*
718*dfc6aa5cSAndroid Build Coastguard Worker * Allocate workspace for Floyd-Steinberg errors.
719*dfc6aa5cSAndroid Build Coastguard Worker */
720*dfc6aa5cSAndroid Build Coastguard Worker
721*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
alloc_fs_workspace(j_decompress_ptr cinfo)722*dfc6aa5cSAndroid Build Coastguard Worker alloc_fs_workspace(j_decompress_ptr cinfo)
723*dfc6aa5cSAndroid Build Coastguard Worker {
724*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
725*dfc6aa5cSAndroid Build Coastguard Worker size_t arraysize;
726*dfc6aa5cSAndroid Build Coastguard Worker int i;
727*dfc6aa5cSAndroid Build Coastguard Worker
728*dfc6aa5cSAndroid Build Coastguard Worker arraysize = (size_t)((cinfo->output_width + 2) * sizeof(FSERROR));
729*dfc6aa5cSAndroid Build Coastguard Worker for (i = 0; i < cinfo->out_color_components; i++) {
730*dfc6aa5cSAndroid Build Coastguard Worker cquantize->fserrors[i] = (FSERRPTR)
731*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE, arraysize);
732*dfc6aa5cSAndroid Build Coastguard Worker }
733*dfc6aa5cSAndroid Build Coastguard Worker }
734*dfc6aa5cSAndroid Build Coastguard Worker
735*dfc6aa5cSAndroid Build Coastguard Worker
736*dfc6aa5cSAndroid Build Coastguard Worker /*
737*dfc6aa5cSAndroid Build Coastguard Worker * Initialize for one-pass color quantization.
738*dfc6aa5cSAndroid Build Coastguard Worker */
739*dfc6aa5cSAndroid Build Coastguard Worker
740*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
start_pass_1_quant(j_decompress_ptr cinfo,boolean is_pre_scan)741*dfc6aa5cSAndroid Build Coastguard Worker start_pass_1_quant(j_decompress_ptr cinfo, boolean is_pre_scan)
742*dfc6aa5cSAndroid Build Coastguard Worker {
743*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
744*dfc6aa5cSAndroid Build Coastguard Worker size_t arraysize;
745*dfc6aa5cSAndroid Build Coastguard Worker int i;
746*dfc6aa5cSAndroid Build Coastguard Worker
747*dfc6aa5cSAndroid Build Coastguard Worker /* Install my colormap. */
748*dfc6aa5cSAndroid Build Coastguard Worker cinfo->colormap = cquantize->sv_colormap;
749*dfc6aa5cSAndroid Build Coastguard Worker cinfo->actual_number_of_colors = cquantize->sv_actual;
750*dfc6aa5cSAndroid Build Coastguard Worker
751*dfc6aa5cSAndroid Build Coastguard Worker /* Initialize for desired dithering mode. */
752*dfc6aa5cSAndroid Build Coastguard Worker switch (cinfo->dither_mode) {
753*dfc6aa5cSAndroid Build Coastguard Worker case JDITHER_NONE:
754*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->out_color_components == 3)
755*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.color_quantize = color_quantize3;
756*dfc6aa5cSAndroid Build Coastguard Worker else
757*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.color_quantize = color_quantize;
758*dfc6aa5cSAndroid Build Coastguard Worker break;
759*dfc6aa5cSAndroid Build Coastguard Worker case JDITHER_ORDERED:
760*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->out_color_components == 3)
761*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.color_quantize = quantize3_ord_dither;
762*dfc6aa5cSAndroid Build Coastguard Worker else
763*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.color_quantize = quantize_ord_dither;
764*dfc6aa5cSAndroid Build Coastguard Worker cquantize->row_index = 0; /* initialize state for ordered dither */
765*dfc6aa5cSAndroid Build Coastguard Worker /* If user changed to ordered dither from another mode,
766*dfc6aa5cSAndroid Build Coastguard Worker * we must recreate the color index table with padding.
767*dfc6aa5cSAndroid Build Coastguard Worker * This will cost extra space, but probably isn't very likely.
768*dfc6aa5cSAndroid Build Coastguard Worker */
769*dfc6aa5cSAndroid Build Coastguard Worker if (!cquantize->is_padded)
770*dfc6aa5cSAndroid Build Coastguard Worker create_colorindex(cinfo);
771*dfc6aa5cSAndroid Build Coastguard Worker /* Create ordered-dither tables if we didn't already. */
772*dfc6aa5cSAndroid Build Coastguard Worker if (cquantize->odither[0] == NULL)
773*dfc6aa5cSAndroid Build Coastguard Worker create_odither_tables(cinfo);
774*dfc6aa5cSAndroid Build Coastguard Worker break;
775*dfc6aa5cSAndroid Build Coastguard Worker case JDITHER_FS:
776*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.color_quantize = quantize_fs_dither;
777*dfc6aa5cSAndroid Build Coastguard Worker cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
778*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate Floyd-Steinberg workspace if didn't already. */
779*dfc6aa5cSAndroid Build Coastguard Worker if (cquantize->fserrors[0] == NULL)
780*dfc6aa5cSAndroid Build Coastguard Worker alloc_fs_workspace(cinfo);
781*dfc6aa5cSAndroid Build Coastguard Worker /* Initialize the propagated errors to zero. */
782*dfc6aa5cSAndroid Build Coastguard Worker arraysize = (size_t)((cinfo->output_width + 2) * sizeof(FSERROR));
783*dfc6aa5cSAndroid Build Coastguard Worker for (i = 0; i < cinfo->out_color_components; i++)
784*dfc6aa5cSAndroid Build Coastguard Worker jzero_far((void *)cquantize->fserrors[i], arraysize);
785*dfc6aa5cSAndroid Build Coastguard Worker break;
786*dfc6aa5cSAndroid Build Coastguard Worker default:
787*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT(cinfo, JERR_NOT_COMPILED);
788*dfc6aa5cSAndroid Build Coastguard Worker break;
789*dfc6aa5cSAndroid Build Coastguard Worker }
790*dfc6aa5cSAndroid Build Coastguard Worker }
791*dfc6aa5cSAndroid Build Coastguard Worker
792*dfc6aa5cSAndroid Build Coastguard Worker
793*dfc6aa5cSAndroid Build Coastguard Worker /*
794*dfc6aa5cSAndroid Build Coastguard Worker * Finish up at the end of the pass.
795*dfc6aa5cSAndroid Build Coastguard Worker */
796*dfc6aa5cSAndroid Build Coastguard Worker
797*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
finish_pass_1_quant(j_decompress_ptr cinfo)798*dfc6aa5cSAndroid Build Coastguard Worker finish_pass_1_quant(j_decompress_ptr cinfo)
799*dfc6aa5cSAndroid Build Coastguard Worker {
800*dfc6aa5cSAndroid Build Coastguard Worker /* no work in 1-pass case */
801*dfc6aa5cSAndroid Build Coastguard Worker }
802*dfc6aa5cSAndroid Build Coastguard Worker
803*dfc6aa5cSAndroid Build Coastguard Worker
804*dfc6aa5cSAndroid Build Coastguard Worker /*
805*dfc6aa5cSAndroid Build Coastguard Worker * Switch to a new external colormap between output passes.
806*dfc6aa5cSAndroid Build Coastguard Worker * Shouldn't get to this module!
807*dfc6aa5cSAndroid Build Coastguard Worker */
808*dfc6aa5cSAndroid Build Coastguard Worker
809*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
new_color_map_1_quant(j_decompress_ptr cinfo)810*dfc6aa5cSAndroid Build Coastguard Worker new_color_map_1_quant(j_decompress_ptr cinfo)
811*dfc6aa5cSAndroid Build Coastguard Worker {
812*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT(cinfo, JERR_MODE_CHANGE);
813*dfc6aa5cSAndroid Build Coastguard Worker }
814*dfc6aa5cSAndroid Build Coastguard Worker
815*dfc6aa5cSAndroid Build Coastguard Worker
816*dfc6aa5cSAndroid Build Coastguard Worker /*
817*dfc6aa5cSAndroid Build Coastguard Worker * Module initialization routine for 1-pass color quantization.
818*dfc6aa5cSAndroid Build Coastguard Worker */
819*dfc6aa5cSAndroid Build Coastguard Worker
820*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
jinit_1pass_quantizer(j_decompress_ptr cinfo)821*dfc6aa5cSAndroid Build Coastguard Worker jinit_1pass_quantizer(j_decompress_ptr cinfo)
822*dfc6aa5cSAndroid Build Coastguard Worker {
823*dfc6aa5cSAndroid Build Coastguard Worker my_cquantize_ptr cquantize;
824*dfc6aa5cSAndroid Build Coastguard Worker
825*dfc6aa5cSAndroid Build Coastguard Worker cquantize = (my_cquantize_ptr)
826*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
827*dfc6aa5cSAndroid Build Coastguard Worker sizeof(my_cquantizer));
828*dfc6aa5cSAndroid Build Coastguard Worker cinfo->cquantize = (struct jpeg_color_quantizer *)cquantize;
829*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.start_pass = start_pass_1_quant;
830*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.finish_pass = finish_pass_1_quant;
831*dfc6aa5cSAndroid Build Coastguard Worker cquantize->pub.new_color_map = new_color_map_1_quant;
832*dfc6aa5cSAndroid Build Coastguard Worker cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
833*dfc6aa5cSAndroid Build Coastguard Worker cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */
834*dfc6aa5cSAndroid Build Coastguard Worker
835*dfc6aa5cSAndroid Build Coastguard Worker /* Make sure my internal arrays won't overflow */
836*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->out_color_components > MAX_Q_COMPS)
837*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
838*dfc6aa5cSAndroid Build Coastguard Worker /* Make sure colormap indexes can be represented by JSAMPLEs */
839*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->desired_number_of_colors > (MAXJSAMPLE + 1))
840*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE + 1);
841*dfc6aa5cSAndroid Build Coastguard Worker
842*dfc6aa5cSAndroid Build Coastguard Worker /* Create the colormap and color index table. */
843*dfc6aa5cSAndroid Build Coastguard Worker create_colormap(cinfo);
844*dfc6aa5cSAndroid Build Coastguard Worker create_colorindex(cinfo);
845*dfc6aa5cSAndroid Build Coastguard Worker
846*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate Floyd-Steinberg workspace now if requested.
847*dfc6aa5cSAndroid Build Coastguard Worker * We do this now since it may affect the memory manager's space
848*dfc6aa5cSAndroid Build Coastguard Worker * calculations. If the user changes to FS dither mode in a later pass, we
849*dfc6aa5cSAndroid Build Coastguard Worker * will allocate the space then, and will possibly overrun the
850*dfc6aa5cSAndroid Build Coastguard Worker * max_memory_to_use setting.
851*dfc6aa5cSAndroid Build Coastguard Worker */
852*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->dither_mode == JDITHER_FS)
853*dfc6aa5cSAndroid Build Coastguard Worker alloc_fs_workspace(cinfo);
854*dfc6aa5cSAndroid Build Coastguard Worker }
855*dfc6aa5cSAndroid Build Coastguard Worker
856*dfc6aa5cSAndroid Build Coastguard Worker #endif /* QUANT_1PASS_SUPPORTED */
857