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