istereo2

annotate libs/libjpeg/jquant2.c @ 27:f0da8b2b61ec

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