Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jquant1.c |
| 3 | * |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 4 | * Copyright (C) 1991, 1992, Thomas G. Lane. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 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 1-pass color quantization (color mapping) routines. |
| 9 | * These routines are invoked via the methods color_quantize |
| 10 | * and color_quant_init/term. |
| 11 | */ |
| 12 | |
| 13 | #include "jinclude.h" |
| 14 | |
| 15 | #ifdef QUANT_1PASS_SUPPORTED |
| 16 | |
| 17 | |
| 18 | /* |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 19 | * The main purpose of 1-pass quantization is to provide a fast, if not very |
| 20 | * high quality, colormapped output capability. A 2-pass quantizer usually |
| 21 | * gives better visual quality; however, for quantized grayscale output this |
| 22 | * quantizer is perfectly adequate. Dithering is highly recommended with this |
| 23 | * quantizer, though you can turn it off if you really want to. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 24 | * |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 25 | * This implementation quantizes in the output colorspace. This has a couple |
| 26 | * of disadvantages: each pixel must be individually color-converted, and if |
| 27 | * the color conversion includes gamma correction then quantization is done in |
| 28 | * a nonlinear space, which is less desirable. The major advantage is that |
| 29 | * with the usual output color spaces (RGB, grayscale) an orthogonal grid of |
| 30 | * representative colors can be used, thus permitting the very simple and fast |
| 31 | * color lookup scheme used here. The standard JPEG colorspace (YCbCr) cannot |
| 32 | * be effectively handled this way, because only about a quarter of an |
| 33 | * orthogonal grid would fall within the gamut of realizable colors. Another |
| 34 | * advantage is that when the user wants quantized grayscale output from a |
| 35 | * color JPEG file, this quantizer can provide a high-quality result with no |
| 36 | * special hacking. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 37 | * |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 38 | * The gamma-correction problem could be eliminated by adjusting the grid |
| 39 | * spacing to counteract the gamma correction applied by color_convert. |
| 40 | * At this writing, gamma correction is not implemented by jdcolor, so |
| 41 | * nothing is done here. |
| 42 | * |
| 43 | * In 1-pass quantization the colormap must be chosen in advance of seeing the |
| 44 | * image. We use a map consisting of all combinations of Ncolors[i] color |
| 45 | * values for the i'th component. The Ncolors[] values are chosen so that |
| 46 | * their product, the total number of colors, is no more than that requested. |
| 47 | * (In most cases, the product will be somewhat less.) |
| 48 | * |
| 49 | * Since the colormap is orthogonal, the representative value for each color |
| 50 | * component can be determined without considering the other components; |
| 51 | * then these indexes can be combined into a colormap index by a standard |
| 52 | * N-dimensional-array-subscript calculation. Most of the arithmetic involved |
| 53 | * can be precalculated and stored in the lookup table colorindex[]. |
| 54 | * colorindex[i][j] maps pixel value j in component i to the nearest |
| 55 | * representative value (grid plane) for that component; this index is |
| 56 | * multiplied by the array stride for component i, so that the |
| 57 | * index of the colormap entry closest to a given pixel value is just |
| 58 | * sum( colorindex[component-number][pixel-component-value] ) |
| 59 | * Aside from being fast, this scheme allows for variable spacing between |
| 60 | * representative values with no additional lookup cost. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 61 | */ |
| 62 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 63 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 64 | #define MAX_COMPONENTS 4 /* max components I can handle */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 65 | |
| 66 | static JSAMPARRAY colormap; /* The actual color map */ |
| 67 | /* colormap[i][j] = value of i'th color component for output pixel value j */ |
| 68 | |
| 69 | static JSAMPARRAY colorindex; /* Precomputed mapping for speed */ |
| 70 | /* colorindex[i][j] = index of color closest to pixel value j in component i, |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 71 | * premultiplied as described above. Since colormap indexes must fit into |
| 72 | * JSAMPLEs, the entries of this array will too. |
| 73 | */ |
| 74 | |
| 75 | static JSAMPARRAY input_buffer; /* color conversion workspace */ |
| 76 | /* Since our input data is presented in the JPEG colorspace, we have to call |
| 77 | * color_convert to get it into the output colorspace. input_buffer is a |
| 78 | * one-row-high workspace for the result of color_convert. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 79 | */ |
| 80 | |
| 81 | |
| 82 | /* Declarations for Floyd-Steinberg dithering. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 83 | * |
| 84 | * Errors are accumulated into the arrays evenrowerrs[] and oddrowerrs[]. |
| 85 | * These have resolutions of 1/16th of a pixel count. The error at a given |
| 86 | * pixel is propagated to its unprocessed neighbors using the standard F-S |
| 87 | * fractions, |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 88 | * ... (here) 7/16 |
| 89 | * 3/16 5/16 1/16 |
| 90 | * We work left-to-right on even rows, right-to-left on odd rows. |
| 91 | * |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 92 | * In each of the xxxrowerrs[] arrays, indexing is [component#][position]. |
| 93 | * We provide (#columns + 2) entries per component; the extra entry at each |
| 94 | * end saves us from special-casing the first and last pixels. |
| 95 | * In evenrowerrs[], the entries for a component are stored left-to-right, but |
| 96 | * in oddrowerrs[] they are stored right-to-left. This means we always |
| 97 | * process the current row's error entries in increasing order and the next |
| 98 | * row's error entries in decreasing order, regardless of whether we are |
| 99 | * working L-to-R or R-to-L in the pixel data! |
| 100 | * |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 101 | * Note: on a wide image, we might not have enough room in a PC's near data |
| 102 | * segment to hold the error arrays; so they are allocated with alloc_medium. |
| 103 | */ |
| 104 | |
| 105 | #ifdef EIGHT_BIT_SAMPLES |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 106 | typedef INT16 FSERROR; /* 16 bits should be enough */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 107 | #else |
| 108 | typedef INT32 FSERROR; /* may need more than 16 bits? */ |
| 109 | #endif |
| 110 | |
| 111 | typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */ |
| 112 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 113 | static FSERRPTR evenrowerrs[MAX_COMPONENTS]; /* errors for even rows */ |
| 114 | static FSERRPTR oddrowerrs[MAX_COMPONENTS]; /* errors for odd rows */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 115 | static boolean on_odd_row; /* flag to remember which row we are on */ |
| 116 | |
| 117 | |
| 118 | /* |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 119 | * Policy-making subroutines for color_quant_init: these routines determine |
| 120 | * the colormap to be used. The rest of the module only assumes that the |
| 121 | * colormap is orthogonal. |
| 122 | * |
| 123 | * * select_ncolors decides how to divvy up the available colors |
| 124 | * among the components. |
| 125 | * * output_value defines the set of representative values for a component. |
| 126 | * * largest_input_value defines the mapping from input values to |
| 127 | * representative values for a component. |
| 128 | * Note that the latter two routines may impose different policies for |
| 129 | * different components, though this is not currently done. |
| 130 | */ |
| 131 | |
| 132 | |
| 133 | LOCAL int |
| 134 | select_ncolors (decompress_info_ptr cinfo, int Ncolors[]) |
| 135 | /* Determine allocation of desired colors to components, */ |
| 136 | /* and fill in Ncolors[] array to indicate choice. */ |
| 137 | /* Return value is total number of colors (product of Ncolors[] values). */ |
| 138 | { |
| 139 | int nc = cinfo->color_out_comps; /* number of color components */ |
| 140 | int max_colors = cinfo->desired_number_of_colors; |
| 141 | int total_colors, iroot, i; |
| 142 | long temp; |
| 143 | boolean changed; |
| 144 | |
| 145 | /* We can allocate at least the nc'th root of max_colors per component. */ |
| 146 | /* Compute floor(nc'th root of max_colors). */ |
| 147 | iroot = 1; |
| 148 | do { |
| 149 | iroot++; |
| 150 | temp = iroot; /* set temp = iroot ** nc */ |
| 151 | for (i = 1; i < nc; i++) |
| 152 | temp *= iroot; |
| 153 | } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */ |
| 154 | iroot--; /* now iroot = floor(root) */ |
| 155 | |
| 156 | /* Must have at least 2 color values per component */ |
| 157 | if (iroot < 2) |
| 158 | ERREXIT1(cinfo->emethods, "Cannot quantize to fewer than %d colors", |
| 159 | (int) temp); |
| 160 | |
| 161 | if (cinfo->out_color_space == CS_RGB && nc == 3) { |
| 162 | /* We provide a special policy for quantizing in RGB space. |
| 163 | * If 256 colors are requested, we allocate 8 red, 8 green, 4 blue levels; |
| 164 | * this corresponds to the common 3/3/2-bit scheme. For other totals, |
| 165 | * the counts are set so that the number of colors allocated to each |
| 166 | * component are roughly in the proportion R 3, G 4, B 2. |
| 167 | * For low color counts, it's easier to hardwire the optimal choices |
| 168 | * than try to tweak the algorithm to generate them. |
| 169 | */ |
| 170 | if (max_colors == 256) { |
| 171 | Ncolors[0] = 8; Ncolors[1] = 8; Ncolors[2] = 4; |
| 172 | return 256; |
| 173 | } |
| 174 | if (max_colors < 12) { |
| 175 | /* Fixed mapping for 8 colors */ |
| 176 | Ncolors[0] = Ncolors[1] = Ncolors[2] = 2; |
| 177 | } else if (max_colors < 18) { |
| 178 | /* Fixed mapping for 12 colors */ |
| 179 | Ncolors[0] = 2; Ncolors[1] = 3; Ncolors[2] = 2; |
| 180 | } else if (max_colors < 24) { |
| 181 | /* Fixed mapping for 18 colors */ |
| 182 | Ncolors[0] = 3; Ncolors[1] = 3; Ncolors[2] = 2; |
| 183 | } else if (max_colors < 27) { |
| 184 | /* Fixed mapping for 24 colors */ |
| 185 | Ncolors[0] = 3; Ncolors[1] = 4; Ncolors[2] = 2; |
| 186 | } else if (max_colors < 36) { |
| 187 | /* Fixed mapping for 27 colors */ |
| 188 | Ncolors[0] = 3; Ncolors[1] = 3; Ncolors[2] = 3; |
| 189 | } else { |
| 190 | /* these weights are readily derived with a little algebra */ |
| 191 | Ncolors[0] = (iroot * 266) >> 8; /* R weight is 1.0400 */ |
| 192 | Ncolors[1] = (iroot * 355) >> 8; /* G weight is 1.3867 */ |
| 193 | Ncolors[2] = (iroot * 177) >> 8; /* B weight is 0.6934 */ |
| 194 | } |
| 195 | total_colors = Ncolors[0] * Ncolors[1] * Ncolors[2]; |
| 196 | /* The above computation produces "floor" values, so we may be able to |
| 197 | * increment the count for one or more components without exceeding |
| 198 | * max_colors. We try in the order B, G, R. |
| 199 | */ |
| 200 | do { |
| 201 | changed = FALSE; |
| 202 | for (i = 2; i >= 0; i--) { |
| 203 | /* calculate new total_colors if Ncolors[i] is incremented */ |
| 204 | temp = total_colors / Ncolors[i]; |
| 205 | temp *= Ncolors[i]+1; /* done in long arith to avoid oflo */ |
| 206 | if (temp <= (long) max_colors) { |
| 207 | Ncolors[i]++; /* OK, apply the increment */ |
| 208 | total_colors = (int) temp; |
| 209 | changed = TRUE; |
| 210 | } |
| 211 | } |
| 212 | } while (changed); /* loop until no increment is possible */ |
| 213 | } else { |
| 214 | /* For any colorspace besides RGB, treat all the components equally. */ |
| 215 | |
| 216 | /* Initialize to iroot color values for each component */ |
| 217 | total_colors = 1; |
| 218 | for (i = 0; i < nc; i++) { |
| 219 | Ncolors[i] = iroot; |
| 220 | total_colors *= iroot; |
| 221 | } |
| 222 | /* We may be able to increment the count for one or more components without |
| 223 | * exceeding max_colors, though we know not all can be incremented. |
| 224 | */ |
| 225 | for (i = 0; i < nc; i++) { |
| 226 | /* calculate new total_colors if Ncolors[i] is incremented */ |
| 227 | temp = total_colors / Ncolors[i]; |
| 228 | temp *= Ncolors[i]+1; /* done in long arith to avoid oflo */ |
| 229 | if (temp > (long) max_colors) |
| 230 | break; /* won't fit, done */ |
| 231 | Ncolors[i]++; /* OK, apply the increment */ |
| 232 | total_colors = (int) temp; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return total_colors; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | LOCAL int |
| 241 | output_value (decompress_info_ptr cinfo, int ci, int j, int maxj) |
| 242 | /* Return j'th output value, where j will range from 0 to maxj */ |
| 243 | /* The output values must fall in 0..MAXJSAMPLE in increasing order */ |
| 244 | { |
| 245 | /* We always provide values 0 and MAXJSAMPLE for each component; |
| 246 | * any additional values are equally spaced between these limits. |
| 247 | * (Forcing the upper and lower values to the limits ensures that |
| 248 | * dithering can't produce a color outside the selected gamut.) |
| 249 | */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 250 | return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
| 254 | LOCAL int |
| 255 | largest_input_value (decompress_info_ptr cinfo, int ci, int j, int maxj) |
| 256 | /* Return largest input value that should map to j'th output value */ |
| 257 | /* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */ |
| 258 | { |
| 259 | /* Breakpoints are halfway between values returned by output_value */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 260 | return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj)); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
| 264 | /* |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 265 | * Initialize for one-pass color quantization. |
| 266 | */ |
| 267 | |
| 268 | METHODDEF void |
| 269 | color_quant_init (decompress_info_ptr cinfo) |
| 270 | { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 271 | int total_colors; /* Number of distinct output colors */ |
| 272 | int Ncolors[MAX_COMPONENTS]; /* # of values alloced to each component */ |
| 273 | int i,j,k, nci, blksize, blkdist, ptr, val; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 274 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 275 | /* Make sure my internal arrays won't overflow */ |
| 276 | if (cinfo->num_components > MAX_COMPONENTS || |
| 277 | cinfo->color_out_comps > MAX_COMPONENTS) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 278 | ERREXIT1(cinfo->emethods, "Cannot quantize more than %d color components", |
| 279 | MAX_COMPONENTS); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 280 | /* Make sure colormap indexes can be represented by JSAMPLEs */ |
| 281 | if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1)) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 282 | ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors", |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 283 | MAXJSAMPLE+1); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 284 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 285 | /* Select number of colors for each component */ |
| 286 | total_colors = select_ncolors(cinfo, Ncolors); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 287 | |
| 288 | /* Report selected color counts */ |
| 289 | if (cinfo->color_out_comps == 3) |
| 290 | TRACEMS4(cinfo->emethods, 1, "Quantizing to %d = %d*%d*%d colors", |
| 291 | total_colors, Ncolors[0], Ncolors[1], Ncolors[2]); |
| 292 | else |
| 293 | TRACEMS1(cinfo->emethods, 1, "Quantizing to %d colors", total_colors); |
| 294 | |
| 295 | /* Allocate and fill in the colormap and color index. */ |
| 296 | /* The colors are ordered in the map in standard row-major order, */ |
| 297 | /* i.e. rightmost (highest-indexed) color changes most rapidly. */ |
| 298 | |
| 299 | colormap = (*cinfo->emethods->alloc_small_sarray) |
| 300 | ((long) total_colors, (long) cinfo->color_out_comps); |
| 301 | colorindex = (*cinfo->emethods->alloc_small_sarray) |
| 302 | ((long) (MAXJSAMPLE+1), (long) cinfo->color_out_comps); |
| 303 | |
| 304 | /* blksize is number of adjacent repeated entries for a component */ |
| 305 | /* blkdist is distance between groups of identical entries for a component */ |
| 306 | blkdist = total_colors; |
| 307 | |
| 308 | for (i = 0; i < cinfo->color_out_comps; i++) { |
| 309 | /* fill in colormap entries for i'th color component */ |
| 310 | nci = Ncolors[i]; /* # of distinct values for this color */ |
| 311 | blksize = blkdist / nci; |
| 312 | for (j = 0; j < nci; j++) { |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 313 | /* Compute j'th output value (out of nci) for component */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 314 | val = output_value(cinfo, i, j, nci-1); |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 315 | /* Fill in all colormap entries that have this value of this component */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 316 | for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) { |
| 317 | /* fill in blksize entries beginning at ptr */ |
| 318 | for (k = 0; k < blksize; k++) |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 319 | colormap[i][ptr+k] = (JSAMPLE) val; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | blkdist = blksize; /* blksize of this color is blkdist of next */ |
| 323 | |
| 324 | /* fill in colorindex entries for i'th color component */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 325 | /* in loop, val = index of current output value, */ |
| 326 | /* and k = largest j that maps to current val */ |
| 327 | val = 0; |
| 328 | k = largest_input_value(cinfo, i, 0, nci-1); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 329 | for (j = 0; j <= MAXJSAMPLE; j++) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 330 | while (j > k) /* advance val if past boundary */ |
| 331 | k = largest_input_value(cinfo, i, ++val, nci-1); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 332 | /* premultiply so that no multiplication needed in main processing */ |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 333 | colorindex[i][j] = (JSAMPLE) (val * blksize); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 337 | /* Pass the colormap to the output module. */ |
| 338 | /* NB: the output module may continue to use the colormap until shutdown. */ |
| 339 | cinfo->colormap = colormap; |
| 340 | cinfo->actual_number_of_colors = total_colors; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 341 | (*cinfo->methods->put_color_map) (cinfo, total_colors, colormap); |
| 342 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 343 | /* Allocate workspace to hold one row of color-converted data */ |
| 344 | input_buffer = (*cinfo->emethods->alloc_small_sarray) |
| 345 | (cinfo->image_width, (long) cinfo->color_out_comps); |
| 346 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 347 | /* Allocate Floyd-Steinberg workspace if necessary */ |
| 348 | if (cinfo->use_dithering) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 349 | size_t arraysize = (size_t) ((cinfo->image_width + 2L) * SIZEOF(FSERROR)); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 350 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 351 | for (i = 0; i < cinfo->color_out_comps; i++) { |
| 352 | evenrowerrs[i] = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize); |
| 353 | oddrowerrs[i] = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize); |
| 354 | /* we only need to zero the forward contribution for current row. */ |
| 355 | jzero_far((void FAR *) evenrowerrs[i], arraysize); |
| 356 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 357 | on_odd_row = FALSE; |
| 358 | } |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 359 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 360 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * Subroutines for color conversion methods. |
| 364 | */ |
| 365 | |
| 366 | LOCAL void |
| 367 | do_color_conversion (decompress_info_ptr cinfo, JSAMPIMAGE input_data, int row) |
| 368 | /* Convert the indicated row of the input data into output colorspace */ |
| 369 | /* in input_buffer. This requires a little trickery since color_convert */ |
| 370 | /* expects to deal with 3-D arrays; fortunately we can fake it out */ |
| 371 | /* at fairly low cost. */ |
| 372 | { |
| 373 | short ci; |
| 374 | JSAMPARRAY input_hack[MAX_COMPONENTS]; |
| 375 | JSAMPARRAY output_hack[MAX_COMPONENTS]; |
| 376 | |
| 377 | /* create JSAMPIMAGE pointing at specified row of input_data */ |
| 378 | for (ci = 0; ci < cinfo->num_components; ci++) |
| 379 | input_hack[ci] = input_data[ci] + row; |
| 380 | /* Create JSAMPIMAGE pointing at input_buffer */ |
| 381 | for (ci = 0; ci < cinfo->color_out_comps; ci++) |
| 382 | output_hack[ci] = &(input_buffer[ci]); |
| 383 | |
| 384 | (*cinfo->methods->color_convert) (cinfo, 1, cinfo->image_width, |
| 385 | input_hack, output_hack); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | |
| 389 | /* |
| 390 | * Map some rows of pixels to the output colormapped representation. |
| 391 | */ |
| 392 | |
| 393 | METHODDEF void |
| 394 | color_quantize (decompress_info_ptr cinfo, int num_rows, |
| 395 | JSAMPIMAGE input_data, JSAMPARRAY output_data) |
| 396 | /* General case, no dithering */ |
| 397 | { |
| 398 | register int pixcode, ci; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 399 | register JSAMPROW ptrout; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 400 | register long col; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 401 | int row; |
| 402 | long width = cinfo->image_width; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 403 | register int nc = cinfo->color_out_comps; |
| 404 | |
| 405 | for (row = 0; row < num_rows; row++) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 406 | do_color_conversion(cinfo, input_data, row); |
| 407 | ptrout = output_data[row]; |
| 408 | for (col = 0; col < width; col++) { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 409 | pixcode = 0; |
| 410 | for (ci = 0; ci < nc; ci++) { |
| 411 | pixcode += GETJSAMPLE(colorindex[ci] |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 412 | [GETJSAMPLE(input_buffer[ci][col])]); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 413 | } |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 414 | *ptrout++ = (JSAMPLE) pixcode; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | |
| 420 | METHODDEF void |
| 421 | color_quantize3 (decompress_info_ptr cinfo, int num_rows, |
| 422 | JSAMPIMAGE input_data, JSAMPARRAY output_data) |
| 423 | /* Fast path for color_out_comps==3, no dithering */ |
| 424 | { |
| 425 | register int pixcode; |
| 426 | register JSAMPROW ptr0, ptr1, ptr2, ptrout; |
| 427 | register long col; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 428 | int row; |
| 429 | long width = cinfo->image_width; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 430 | |
| 431 | for (row = 0; row < num_rows; row++) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 432 | do_color_conversion(cinfo, input_data, row); |
| 433 | ptr0 = input_buffer[0]; |
| 434 | ptr1 = input_buffer[1]; |
| 435 | ptr2 = input_buffer[2]; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 436 | ptrout = output_data[row]; |
| 437 | for (col = width; col > 0; col--) { |
| 438 | pixcode = GETJSAMPLE(colorindex[0][GETJSAMPLE(*ptr0++)]); |
| 439 | pixcode += GETJSAMPLE(colorindex[1][GETJSAMPLE(*ptr1++)]); |
| 440 | pixcode += GETJSAMPLE(colorindex[2][GETJSAMPLE(*ptr2++)]); |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 441 | *ptrout++ = (JSAMPLE) pixcode; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | |
| 447 | METHODDEF void |
| 448 | color_quantize_dither (decompress_info_ptr cinfo, int num_rows, |
| 449 | JSAMPIMAGE input_data, JSAMPARRAY output_data) |
| 450 | /* General case, with Floyd-Steinberg dithering */ |
| 451 | { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 452 | register FSERROR val; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 453 | FSERROR two_val; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 454 | register FSERRPTR thisrowerr, nextrowerr; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 455 | register JSAMPROW input_ptr; |
| 456 | register JSAMPROW output_ptr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 457 | JSAMPLE *range_limit = cinfo->sample_range_limit; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 458 | JSAMPROW colorindex_ci; |
| 459 | JSAMPROW colormap_ci; |
| 460 | register int pixcode; |
| 461 | int dir; /* 1 for left-to-right, -1 for right-to-left */ |
| 462 | int ci; |
| 463 | int nc = cinfo->color_out_comps; |
| 464 | int row; |
| 465 | long col_counter; |
| 466 | long width = cinfo->image_width; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 467 | SHIFT_TEMPS |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 468 | |
| 469 | for (row = 0; row < num_rows; row++) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 470 | do_color_conversion(cinfo, input_data, row); |
| 471 | /* Initialize output values to 0 so can process components separately */ |
| 472 | jzero_far((void FAR *) output_data[row], |
| 473 | (size_t) (width * SIZEOF(JSAMPLE))); |
| 474 | for (ci = 0; ci < nc; ci++) { |
| 475 | if (on_odd_row) { |
| 476 | /* work right to left in this row */ |
| 477 | dir = -1; |
| 478 | input_ptr = input_buffer[ci] + (width-1); |
| 479 | output_ptr = output_data[row] + (width-1); |
| 480 | thisrowerr = oddrowerrs[ci] + 1; |
| 481 | nextrowerr = evenrowerrs[ci] + width; |
| 482 | } else { |
| 483 | /* work left to right in this row */ |
| 484 | dir = 1; |
| 485 | input_ptr = input_buffer[ci]; |
| 486 | output_ptr = output_data[row]; |
| 487 | thisrowerr = evenrowerrs[ci] + 1; |
| 488 | nextrowerr = oddrowerrs[ci] + width; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 489 | } |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 490 | colorindex_ci = colorindex[ci]; |
| 491 | colormap_ci = colormap[ci]; |
| 492 | *nextrowerr = 0; /* need only initialize this one entry */ |
| 493 | for (col_counter = width; col_counter > 0; col_counter--) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 494 | /* Get accumulated error for this component, round to integer. |
| 495 | * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct |
| 496 | * for either sign of the error value. |
| 497 | */ |
| 498 | val = RIGHT_SHIFT(*thisrowerr + 8, 4); |
| 499 | /* Compute pixel value + error compensation, range-limit to |
| 500 | * 0..MAXJSAMPLE. Note max error value is +- MAXJSAMPLE. |
| 501 | */ |
| 502 | val = GETJSAMPLE(range_limit[GETJSAMPLE(*input_ptr) + val]); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 503 | /* Select output value, accumulate into output code for this pixel */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 504 | pixcode = GETJSAMPLE(*output_ptr) + GETJSAMPLE(colorindex_ci[val]); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 505 | *output_ptr = (JSAMPLE) pixcode; |
| 506 | /* Compute actual representation error at this pixel */ |
| 507 | /* Note: we can do this even though we don't yet have the final */ |
| 508 | /* value of pixcode, because the colormap is orthogonal. */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame^] | 509 | val -= GETJSAMPLE(colormap_ci[pixcode]); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 510 | /* Propagate error to (same component of) adjacent pixels */ |
| 511 | /* Remember that nextrowerr entries are in reverse order! */ |
| 512 | two_val = val * 2; |
| 513 | nextrowerr[-1] = val; /* not +=, since not initialized yet */ |
| 514 | val += two_val; /* form error * 3 */ |
| 515 | nextrowerr[ 1] += val; |
| 516 | val += two_val; /* form error * 5 */ |
| 517 | nextrowerr[ 0] += val; |
| 518 | val += two_val; /* form error * 7 */ |
| 519 | thisrowerr[ 1] += val; |
| 520 | input_ptr += dir; /* advance input ptr to next column */ |
| 521 | output_ptr += dir; /* advance output ptr to next column */ |
| 522 | thisrowerr++; /* cur-row error ptr advances to right */ |
| 523 | nextrowerr--; /* next-row error ptr advances to left */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 524 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 525 | } |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 526 | on_odd_row = (on_odd_row ? FALSE : TRUE); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* |
| 532 | * Finish up at the end of the file. |
| 533 | */ |
| 534 | |
| 535 | METHODDEF void |
| 536 | color_quant_term (decompress_info_ptr cinfo) |
| 537 | { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 538 | /* no work (we let free_all release the workspace) */ |
| 539 | /* Note that we *mustn't* free the colormap before free_all, */ |
| 540 | /* since output module may use it! */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | |
| 544 | /* |
| 545 | * Prescan some rows of pixels. |
| 546 | * Not used in one-pass case. |
| 547 | */ |
| 548 | |
| 549 | METHODDEF void |
| 550 | color_quant_prescan (decompress_info_ptr cinfo, int num_rows, |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 551 | JSAMPIMAGE image_data, JSAMPARRAY workspace) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 552 | { |
| 553 | ERREXIT(cinfo->emethods, "Should not get here!"); |
| 554 | } |
| 555 | |
| 556 | |
| 557 | /* |
| 558 | * Do two-pass quantization. |
| 559 | * Not used in one-pass case. |
| 560 | */ |
| 561 | |
| 562 | METHODDEF void |
| 563 | color_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method) |
| 564 | { |
| 565 | ERREXIT(cinfo->emethods, "Should not get here!"); |
| 566 | } |
| 567 | |
| 568 | |
| 569 | /* |
| 570 | * The method selection routine for 1-pass color quantization. |
| 571 | */ |
| 572 | |
| 573 | GLOBAL void |
| 574 | jsel1quantize (decompress_info_ptr cinfo) |
| 575 | { |
| 576 | if (! cinfo->two_pass_quantize) { |
| 577 | cinfo->methods->color_quant_init = color_quant_init; |
| 578 | if (cinfo->use_dithering) { |
| 579 | cinfo->methods->color_quantize = color_quantize_dither; |
| 580 | } else { |
| 581 | if (cinfo->color_out_comps == 3) |
| 582 | cinfo->methods->color_quantize = color_quantize3; |
| 583 | else |
| 584 | cinfo->methods->color_quantize = color_quantize; |
| 585 | } |
| 586 | cinfo->methods->color_quant_prescan = color_quant_prescan; |
| 587 | cinfo->methods->color_quant_doit = color_quant_doit; |
| 588 | cinfo->methods->color_quant_term = color_quant_term; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | #endif /* QUANT_1PASS_SUPPORTED */ |