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