media/libjpeg/jquant2.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial