1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jquant1.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,861 @@ 1.4 +/* 1.5 + * jquant1.c 1.6 + * 1.7 + * This file was part of the Independent JPEG Group's software: 1.8 + * Copyright (C) 1991-1996, Thomas G. Lane. 1.9 + * libjpeg-turbo Modifications: 1.10 + * Copyright (C) 2009, D. R. Commander 1.11 + * For conditions of distribution and use, see the accompanying README file. 1.12 + * 1.13 + * This file contains 1-pass color quantization (color mapping) routines. 1.14 + * These routines provide mapping to a fixed color map using equally spaced 1.15 + * color values. Optional Floyd-Steinberg or ordered dithering is available. 1.16 + */ 1.17 + 1.18 +#define JPEG_INTERNALS 1.19 +#include "jinclude.h" 1.20 +#include "jpeglib.h" 1.21 + 1.22 +#ifdef QUANT_1PASS_SUPPORTED 1.23 + 1.24 + 1.25 +/* 1.26 + * The main purpose of 1-pass quantization is to provide a fast, if not very 1.27 + * high quality, colormapped output capability. A 2-pass quantizer usually 1.28 + * gives better visual quality; however, for quantized grayscale output this 1.29 + * quantizer is perfectly adequate. Dithering is highly recommended with this 1.30 + * quantizer, though you can turn it off if you really want to. 1.31 + * 1.32 + * In 1-pass quantization the colormap must be chosen in advance of seeing the 1.33 + * image. We use a map consisting of all combinations of Ncolors[i] color 1.34 + * values for the i'th component. The Ncolors[] values are chosen so that 1.35 + * their product, the total number of colors, is no more than that requested. 1.36 + * (In most cases, the product will be somewhat less.) 1.37 + * 1.38 + * Since the colormap is orthogonal, the representative value for each color 1.39 + * component can be determined without considering the other components; 1.40 + * then these indexes can be combined into a colormap index by a standard 1.41 + * N-dimensional-array-subscript calculation. Most of the arithmetic involved 1.42 + * can be precalculated and stored in the lookup table colorindex[]. 1.43 + * colorindex[i][j] maps pixel value j in component i to the nearest 1.44 + * representative value (grid plane) for that component; this index is 1.45 + * multiplied by the array stride for component i, so that the 1.46 + * index of the colormap entry closest to a given pixel value is just 1.47 + * sum( colorindex[component-number][pixel-component-value] ) 1.48 + * Aside from being fast, this scheme allows for variable spacing between 1.49 + * representative values with no additional lookup cost. 1.50 + * 1.51 + * If gamma correction has been applied in color conversion, it might be wise 1.52 + * to adjust the color grid spacing so that the representative colors are 1.53 + * equidistant in linear space. At this writing, gamma correction is not 1.54 + * implemented by jdcolor, so nothing is done here. 1.55 + */ 1.56 + 1.57 + 1.58 +/* Declarations for ordered dithering. 1.59 + * 1.60 + * We use a standard 16x16 ordered dither array. The basic concept of ordered 1.61 + * dithering is described in many references, for instance Dale Schumacher's 1.62 + * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991). 1.63 + * In place of Schumacher's comparisons against a "threshold" value, we add a 1.64 + * "dither" value to the input pixel and then round the result to the nearest 1.65 + * output value. The dither value is equivalent to (0.5 - threshold) times 1.66 + * the distance between output values. For ordered dithering, we assume that 1.67 + * the output colors are equally spaced; if not, results will probably be 1.68 + * worse, since the dither may be too much or too little at a given point. 1.69 + * 1.70 + * The normal calculation would be to form pixel value + dither, range-limit 1.71 + * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual. 1.72 + * We can skip the separate range-limiting step by extending the colorindex 1.73 + * table in both directions. 1.74 + */ 1.75 + 1.76 +#define ODITHER_SIZE 16 /* dimension of dither matrix */ 1.77 +/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */ 1.78 +#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE) /* # cells in matrix */ 1.79 +#define ODITHER_MASK (ODITHER_SIZE-1) /* mask for wrapping around counters */ 1.80 + 1.81 +typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE]; 1.82 +typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE]; 1.83 + 1.84 +static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = { 1.85 + /* Bayer's order-4 dither array. Generated by the code given in 1.86 + * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I. 1.87 + * The values in this array must range from 0 to ODITHER_CELLS-1. 1.88 + */ 1.89 + { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 }, 1.90 + { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 }, 1.91 + { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 }, 1.92 + { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 }, 1.93 + { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 }, 1.94 + { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 }, 1.95 + { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 }, 1.96 + { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 }, 1.97 + { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 }, 1.98 + { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 }, 1.99 + { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 }, 1.100 + { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 }, 1.101 + { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 }, 1.102 + { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 }, 1.103 + { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 }, 1.104 + { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 } 1.105 +}; 1.106 + 1.107 + 1.108 +/* Declarations for Floyd-Steinberg dithering. 1.109 + * 1.110 + * Errors are accumulated into the array fserrors[], at a resolution of 1.111 + * 1/16th of a pixel count. The error at a given pixel is propagated 1.112 + * to its not-yet-processed neighbors using the standard F-S fractions, 1.113 + * ... (here) 7/16 1.114 + * 3/16 5/16 1/16 1.115 + * We work left-to-right on even rows, right-to-left on odd rows. 1.116 + * 1.117 + * We can get away with a single array (holding one row's worth of errors) 1.118 + * by using it to store the current row's errors at pixel columns not yet 1.119 + * processed, but the next row's errors at columns already processed. We 1.120 + * need only a few extra variables to hold the errors immediately around the 1.121 + * current column. (If we are lucky, those variables are in registers, but 1.122 + * even if not, they're probably cheaper to access than array elements are.) 1.123 + * 1.124 + * The fserrors[] array is indexed [component#][position]. 1.125 + * We provide (#columns + 2) entries per component; the extra entry at each 1.126 + * end saves us from special-casing the first and last pixels. 1.127 + * 1.128 + * Note: on a wide image, we might not have enough room in a PC's near data 1.129 + * segment to hold the error array; so it is allocated with alloc_large. 1.130 + */ 1.131 + 1.132 +#if BITS_IN_JSAMPLE == 8 1.133 +typedef INT16 FSERROR; /* 16 bits should be enough */ 1.134 +typedef int LOCFSERROR; /* use 'int' for calculation temps */ 1.135 +#else 1.136 +typedef INT32 FSERROR; /* may need more than 16 bits */ 1.137 +typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */ 1.138 +#endif 1.139 + 1.140 +typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */ 1.141 + 1.142 + 1.143 +/* Private subobject */ 1.144 + 1.145 +#define MAX_Q_COMPS 4 /* max components I can handle */ 1.146 + 1.147 +typedef struct { 1.148 + struct jpeg_color_quantizer pub; /* public fields */ 1.149 + 1.150 + /* Initially allocated colormap is saved here */ 1.151 + JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */ 1.152 + int sv_actual; /* number of entries in use */ 1.153 + 1.154 + JSAMPARRAY colorindex; /* Precomputed mapping for speed */ 1.155 + /* colorindex[i][j] = index of color closest to pixel value j in component i, 1.156 + * premultiplied as described above. Since colormap indexes must fit into 1.157 + * JSAMPLEs, the entries of this array will too. 1.158 + */ 1.159 + boolean is_padded; /* is the colorindex padded for odither? */ 1.160 + 1.161 + int Ncolors[MAX_Q_COMPS]; /* # of values alloced to each component */ 1.162 + 1.163 + /* Variables for ordered dithering */ 1.164 + int row_index; /* cur row's vertical index in dither matrix */ 1.165 + ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */ 1.166 + 1.167 + /* Variables for Floyd-Steinberg dithering */ 1.168 + FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */ 1.169 + boolean on_odd_row; /* flag to remember which row we are on */ 1.170 +} my_cquantizer; 1.171 + 1.172 +typedef my_cquantizer * my_cquantize_ptr; 1.173 + 1.174 + 1.175 +/* 1.176 + * Policy-making subroutines for create_colormap and create_colorindex. 1.177 + * These routines determine the colormap to be used. The rest of the module 1.178 + * only assumes that the colormap is orthogonal. 1.179 + * 1.180 + * * select_ncolors decides how to divvy up the available colors 1.181 + * among the components. 1.182 + * * output_value defines the set of representative values for a component. 1.183 + * * largest_input_value defines the mapping from input values to 1.184 + * representative values for a component. 1.185 + * Note that the latter two routines may impose different policies for 1.186 + * different components, though this is not currently done. 1.187 + */ 1.188 + 1.189 + 1.190 +LOCAL(int) 1.191 +select_ncolors (j_decompress_ptr cinfo, int Ncolors[]) 1.192 +/* Determine allocation of desired colors to components, */ 1.193 +/* and fill in Ncolors[] array to indicate choice. */ 1.194 +/* Return value is total number of colors (product of Ncolors[] values). */ 1.195 +{ 1.196 + int nc = cinfo->out_color_components; /* number of color components */ 1.197 + int max_colors = cinfo->desired_number_of_colors; 1.198 + int total_colors, iroot, i, j; 1.199 + boolean changed; 1.200 + long temp; 1.201 + int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE }; 1.202 + RGB_order[0] = rgb_green[cinfo->out_color_space]; 1.203 + RGB_order[1] = rgb_red[cinfo->out_color_space]; 1.204 + RGB_order[2] = rgb_blue[cinfo->out_color_space]; 1.205 + 1.206 + /* We can allocate at least the nc'th root of max_colors per component. */ 1.207 + /* Compute floor(nc'th root of max_colors). */ 1.208 + iroot = 1; 1.209 + do { 1.210 + iroot++; 1.211 + temp = iroot; /* set temp = iroot ** nc */ 1.212 + for (i = 1; i < nc; i++) 1.213 + temp *= iroot; 1.214 + } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */ 1.215 + iroot--; /* now iroot = floor(root) */ 1.216 + 1.217 + /* Must have at least 2 color values per component */ 1.218 + if (iroot < 2) 1.219 + ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp); 1.220 + 1.221 + /* Initialize to iroot color values for each component */ 1.222 + total_colors = 1; 1.223 + for (i = 0; i < nc; i++) { 1.224 + Ncolors[i] = iroot; 1.225 + total_colors *= iroot; 1.226 + } 1.227 + /* We may be able to increment the count for one or more components without 1.228 + * exceeding max_colors, though we know not all can be incremented. 1.229 + * Sometimes, the first component can be incremented more than once! 1.230 + * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.) 1.231 + * In RGB colorspace, try to increment G first, then R, then B. 1.232 + */ 1.233 + do { 1.234 + changed = FALSE; 1.235 + for (i = 0; i < nc; i++) { 1.236 + j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i); 1.237 + /* calculate new total_colors if Ncolors[j] is incremented */ 1.238 + temp = total_colors / Ncolors[j]; 1.239 + temp *= Ncolors[j]+1; /* done in long arith to avoid oflo */ 1.240 + if (temp > (long) max_colors) 1.241 + break; /* won't fit, done with this pass */ 1.242 + Ncolors[j]++; /* OK, apply the increment */ 1.243 + total_colors = (int) temp; 1.244 + changed = TRUE; 1.245 + } 1.246 + } while (changed); 1.247 + 1.248 + return total_colors; 1.249 +} 1.250 + 1.251 + 1.252 +LOCAL(int) 1.253 +output_value (j_decompress_ptr cinfo, int ci, int j, int maxj) 1.254 +/* Return j'th output value, where j will range from 0 to maxj */ 1.255 +/* The output values must fall in 0..MAXJSAMPLE in increasing order */ 1.256 +{ 1.257 + /* We always provide values 0 and MAXJSAMPLE for each component; 1.258 + * any additional values are equally spaced between these limits. 1.259 + * (Forcing the upper and lower values to the limits ensures that 1.260 + * dithering can't produce a color outside the selected gamut.) 1.261 + */ 1.262 + return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj); 1.263 +} 1.264 + 1.265 + 1.266 +LOCAL(int) 1.267 +largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj) 1.268 +/* Return largest input value that should map to j'th output value */ 1.269 +/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */ 1.270 +{ 1.271 + /* Breakpoints are halfway between values returned by output_value */ 1.272 + return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj)); 1.273 +} 1.274 + 1.275 + 1.276 +/* 1.277 + * Create the colormap. 1.278 + */ 1.279 + 1.280 +LOCAL(void) 1.281 +create_colormap (j_decompress_ptr cinfo) 1.282 +{ 1.283 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.284 + JSAMPARRAY colormap; /* Created colormap */ 1.285 + int total_colors; /* Number of distinct output colors */ 1.286 + int i,j,k, nci, blksize, blkdist, ptr, val; 1.287 + 1.288 + /* Select number of colors for each component */ 1.289 + total_colors = select_ncolors(cinfo, cquantize->Ncolors); 1.290 + 1.291 + /* Report selected color counts */ 1.292 + if (cinfo->out_color_components == 3) 1.293 + TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS, 1.294 + total_colors, cquantize->Ncolors[0], 1.295 + cquantize->Ncolors[1], cquantize->Ncolors[2]); 1.296 + else 1.297 + TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors); 1.298 + 1.299 + /* Allocate and fill in the colormap. */ 1.300 + /* The colors are ordered in the map in standard row-major order, */ 1.301 + /* i.e. rightmost (highest-indexed) color changes most rapidly. */ 1.302 + 1.303 + colormap = (*cinfo->mem->alloc_sarray) 1.304 + ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.305 + (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components); 1.306 + 1.307 + /* blksize is number of adjacent repeated entries for a component */ 1.308 + /* blkdist is distance between groups of identical entries for a component */ 1.309 + blkdist = total_colors; 1.310 + 1.311 + for (i = 0; i < cinfo->out_color_components; i++) { 1.312 + /* fill in colormap entries for i'th color component */ 1.313 + nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 1.314 + blksize = blkdist / nci; 1.315 + for (j = 0; j < nci; j++) { 1.316 + /* Compute j'th output value (out of nci) for component */ 1.317 + val = output_value(cinfo, i, j, nci-1); 1.318 + /* Fill in all colormap entries that have this value of this component */ 1.319 + for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) { 1.320 + /* fill in blksize entries beginning at ptr */ 1.321 + for (k = 0; k < blksize; k++) 1.322 + colormap[i][ptr+k] = (JSAMPLE) val; 1.323 + } 1.324 + } 1.325 + blkdist = blksize; /* blksize of this color is blkdist of next */ 1.326 + } 1.327 + 1.328 + /* Save the colormap in private storage, 1.329 + * where it will survive color quantization mode changes. 1.330 + */ 1.331 + cquantize->sv_colormap = colormap; 1.332 + cquantize->sv_actual = total_colors; 1.333 +} 1.334 + 1.335 + 1.336 +/* 1.337 + * Create the color index table. 1.338 + */ 1.339 + 1.340 +LOCAL(void) 1.341 +create_colorindex (j_decompress_ptr cinfo) 1.342 +{ 1.343 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.344 + JSAMPROW indexptr; 1.345 + int i,j,k, nci, blksize, val, pad; 1.346 + 1.347 + /* For ordered dither, we pad the color index tables by MAXJSAMPLE in 1.348 + * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE). 1.349 + * This is not necessary in the other dithering modes. However, we 1.350 + * flag whether it was done in case user changes dithering mode. 1.351 + */ 1.352 + if (cinfo->dither_mode == JDITHER_ORDERED) { 1.353 + pad = MAXJSAMPLE*2; 1.354 + cquantize->is_padded = TRUE; 1.355 + } else { 1.356 + pad = 0; 1.357 + cquantize->is_padded = FALSE; 1.358 + } 1.359 + 1.360 + cquantize->colorindex = (*cinfo->mem->alloc_sarray) 1.361 + ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.362 + (JDIMENSION) (MAXJSAMPLE+1 + pad), 1.363 + (JDIMENSION) cinfo->out_color_components); 1.364 + 1.365 + /* blksize is number of adjacent repeated entries for a component */ 1.366 + blksize = cquantize->sv_actual; 1.367 + 1.368 + for (i = 0; i < cinfo->out_color_components; i++) { 1.369 + /* fill in colorindex entries for i'th color component */ 1.370 + nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 1.371 + blksize = blksize / nci; 1.372 + 1.373 + /* adjust colorindex pointers to provide padding at negative indexes. */ 1.374 + if (pad) 1.375 + cquantize->colorindex[i] += MAXJSAMPLE; 1.376 + 1.377 + /* in loop, val = index of current output value, */ 1.378 + /* and k = largest j that maps to current val */ 1.379 + indexptr = cquantize->colorindex[i]; 1.380 + val = 0; 1.381 + k = largest_input_value(cinfo, i, 0, nci-1); 1.382 + for (j = 0; j <= MAXJSAMPLE; j++) { 1.383 + while (j > k) /* advance val if past boundary */ 1.384 + k = largest_input_value(cinfo, i, ++val, nci-1); 1.385 + /* premultiply so that no multiplication needed in main processing */ 1.386 + indexptr[j] = (JSAMPLE) (val * blksize); 1.387 + } 1.388 + /* Pad at both ends if necessary */ 1.389 + if (pad) 1.390 + for (j = 1; j <= MAXJSAMPLE; j++) { 1.391 + indexptr[-j] = indexptr[0]; 1.392 + indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE]; 1.393 + } 1.394 + } 1.395 +} 1.396 + 1.397 + 1.398 +/* 1.399 + * Create an ordered-dither array for a component having ncolors 1.400 + * distinct output values. 1.401 + */ 1.402 + 1.403 +LOCAL(ODITHER_MATRIX_PTR) 1.404 +make_odither_array (j_decompress_ptr cinfo, int ncolors) 1.405 +{ 1.406 + ODITHER_MATRIX_PTR odither; 1.407 + int j,k; 1.408 + INT32 num,den; 1.409 + 1.410 + odither = (ODITHER_MATRIX_PTR) 1.411 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.412 + SIZEOF(ODITHER_MATRIX)); 1.413 + /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1). 1.414 + * Hence the dither value for the matrix cell with fill order f 1.415 + * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1). 1.416 + * On 16-bit-int machine, be careful to avoid overflow. 1.417 + */ 1.418 + den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1)); 1.419 + for (j = 0; j < ODITHER_SIZE; j++) { 1.420 + for (k = 0; k < ODITHER_SIZE; k++) { 1.421 + num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k]))) 1.422 + * MAXJSAMPLE; 1.423 + /* Ensure round towards zero despite C's lack of consistency 1.424 + * about rounding negative values in integer division... 1.425 + */ 1.426 + odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den); 1.427 + } 1.428 + } 1.429 + return odither; 1.430 +} 1.431 + 1.432 + 1.433 +/* 1.434 + * Create the ordered-dither tables. 1.435 + * Components having the same number of representative colors may 1.436 + * share a dither table. 1.437 + */ 1.438 + 1.439 +LOCAL(void) 1.440 +create_odither_tables (j_decompress_ptr cinfo) 1.441 +{ 1.442 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.443 + ODITHER_MATRIX_PTR odither; 1.444 + int i, j, nci; 1.445 + 1.446 + for (i = 0; i < cinfo->out_color_components; i++) { 1.447 + nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 1.448 + odither = NULL; /* search for matching prior component */ 1.449 + for (j = 0; j < i; j++) { 1.450 + if (nci == cquantize->Ncolors[j]) { 1.451 + odither = cquantize->odither[j]; 1.452 + break; 1.453 + } 1.454 + } 1.455 + if (odither == NULL) /* need a new table? */ 1.456 + odither = make_odither_array(cinfo, nci); 1.457 + cquantize->odither[i] = odither; 1.458 + } 1.459 +} 1.460 + 1.461 + 1.462 +/* 1.463 + * Map some rows of pixels to the output colormapped representation. 1.464 + */ 1.465 + 1.466 +METHODDEF(void) 1.467 +color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 1.468 + JSAMPARRAY output_buf, int num_rows) 1.469 +/* General case, no dithering */ 1.470 +{ 1.471 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.472 + JSAMPARRAY colorindex = cquantize->colorindex; 1.473 + register int pixcode, ci; 1.474 + register JSAMPROW ptrin, ptrout; 1.475 + int row; 1.476 + JDIMENSION col; 1.477 + JDIMENSION width = cinfo->output_width; 1.478 + register int nc = cinfo->out_color_components; 1.479 + 1.480 + for (row = 0; row < num_rows; row++) { 1.481 + ptrin = input_buf[row]; 1.482 + ptrout = output_buf[row]; 1.483 + for (col = width; col > 0; col--) { 1.484 + pixcode = 0; 1.485 + for (ci = 0; ci < nc; ci++) { 1.486 + pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]); 1.487 + } 1.488 + *ptrout++ = (JSAMPLE) pixcode; 1.489 + } 1.490 + } 1.491 +} 1.492 + 1.493 + 1.494 +METHODDEF(void) 1.495 +color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 1.496 + JSAMPARRAY output_buf, int num_rows) 1.497 +/* Fast path for out_color_components==3, no dithering */ 1.498 +{ 1.499 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.500 + register int pixcode; 1.501 + register JSAMPROW ptrin, ptrout; 1.502 + JSAMPROW colorindex0 = cquantize->colorindex[0]; 1.503 + JSAMPROW colorindex1 = cquantize->colorindex[1]; 1.504 + JSAMPROW colorindex2 = cquantize->colorindex[2]; 1.505 + int row; 1.506 + JDIMENSION col; 1.507 + JDIMENSION width = cinfo->output_width; 1.508 + 1.509 + for (row = 0; row < num_rows; row++) { 1.510 + ptrin = input_buf[row]; 1.511 + ptrout = output_buf[row]; 1.512 + for (col = width; col > 0; col--) { 1.513 + pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]); 1.514 + pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]); 1.515 + pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]); 1.516 + *ptrout++ = (JSAMPLE) pixcode; 1.517 + } 1.518 + } 1.519 +} 1.520 + 1.521 + 1.522 +METHODDEF(void) 1.523 +quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 1.524 + JSAMPARRAY output_buf, int num_rows) 1.525 +/* General case, with ordered dithering */ 1.526 +{ 1.527 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.528 + register JSAMPROW input_ptr; 1.529 + register JSAMPROW output_ptr; 1.530 + JSAMPROW colorindex_ci; 1.531 + int * dither; /* points to active row of dither matrix */ 1.532 + int row_index, col_index; /* current indexes into dither matrix */ 1.533 + int nc = cinfo->out_color_components; 1.534 + int ci; 1.535 + int row; 1.536 + JDIMENSION col; 1.537 + JDIMENSION width = cinfo->output_width; 1.538 + 1.539 + for (row = 0; row < num_rows; row++) { 1.540 + /* Initialize output values to 0 so can process components separately */ 1.541 + jzero_far((void FAR *) output_buf[row], 1.542 + (size_t) (width * SIZEOF(JSAMPLE))); 1.543 + row_index = cquantize->row_index; 1.544 + for (ci = 0; ci < nc; ci++) { 1.545 + input_ptr = input_buf[row] + ci; 1.546 + output_ptr = output_buf[row]; 1.547 + colorindex_ci = cquantize->colorindex[ci]; 1.548 + dither = cquantize->odither[ci][row_index]; 1.549 + col_index = 0; 1.550 + 1.551 + for (col = width; col > 0; col--) { 1.552 + /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE, 1.553 + * select output value, accumulate into output code for this pixel. 1.554 + * Range-limiting need not be done explicitly, as we have extended 1.555 + * the colorindex table to produce the right answers for out-of-range 1.556 + * inputs. The maximum dither is +- MAXJSAMPLE; this sets the 1.557 + * required amount of padding. 1.558 + */ 1.559 + *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]]; 1.560 + input_ptr += nc; 1.561 + output_ptr++; 1.562 + col_index = (col_index + 1) & ODITHER_MASK; 1.563 + } 1.564 + } 1.565 + /* Advance row index for next row */ 1.566 + row_index = (row_index + 1) & ODITHER_MASK; 1.567 + cquantize->row_index = row_index; 1.568 + } 1.569 +} 1.570 + 1.571 + 1.572 +METHODDEF(void) 1.573 +quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 1.574 + JSAMPARRAY output_buf, int num_rows) 1.575 +/* Fast path for out_color_components==3, with ordered dithering */ 1.576 +{ 1.577 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.578 + register int pixcode; 1.579 + register JSAMPROW input_ptr; 1.580 + register JSAMPROW output_ptr; 1.581 + JSAMPROW colorindex0 = cquantize->colorindex[0]; 1.582 + JSAMPROW colorindex1 = cquantize->colorindex[1]; 1.583 + JSAMPROW colorindex2 = cquantize->colorindex[2]; 1.584 + int * dither0; /* points to active row of dither matrix */ 1.585 + int * dither1; 1.586 + int * dither2; 1.587 + int row_index, col_index; /* current indexes into dither matrix */ 1.588 + int row; 1.589 + JDIMENSION col; 1.590 + JDIMENSION width = cinfo->output_width; 1.591 + 1.592 + for (row = 0; row < num_rows; row++) { 1.593 + row_index = cquantize->row_index; 1.594 + input_ptr = input_buf[row]; 1.595 + output_ptr = output_buf[row]; 1.596 + dither0 = cquantize->odither[0][row_index]; 1.597 + dither1 = cquantize->odither[1][row_index]; 1.598 + dither2 = cquantize->odither[2][row_index]; 1.599 + col_index = 0; 1.600 + 1.601 + for (col = width; col > 0; col--) { 1.602 + pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) + 1.603 + dither0[col_index]]); 1.604 + pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) + 1.605 + dither1[col_index]]); 1.606 + pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) + 1.607 + dither2[col_index]]); 1.608 + *output_ptr++ = (JSAMPLE) pixcode; 1.609 + col_index = (col_index + 1) & ODITHER_MASK; 1.610 + } 1.611 + row_index = (row_index + 1) & ODITHER_MASK; 1.612 + cquantize->row_index = row_index; 1.613 + } 1.614 +} 1.615 + 1.616 + 1.617 +METHODDEF(void) 1.618 +quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 1.619 + JSAMPARRAY output_buf, int num_rows) 1.620 +/* General case, with Floyd-Steinberg dithering */ 1.621 +{ 1.622 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.623 + register LOCFSERROR cur; /* current error or pixel value */ 1.624 + LOCFSERROR belowerr; /* error for pixel below cur */ 1.625 + LOCFSERROR bpreverr; /* error for below/prev col */ 1.626 + LOCFSERROR bnexterr; /* error for below/next col */ 1.627 + LOCFSERROR delta; 1.628 + register FSERRPTR errorptr; /* => fserrors[] at column before current */ 1.629 + register JSAMPROW input_ptr; 1.630 + register JSAMPROW output_ptr; 1.631 + JSAMPROW colorindex_ci; 1.632 + JSAMPROW colormap_ci; 1.633 + int pixcode; 1.634 + int nc = cinfo->out_color_components; 1.635 + int dir; /* 1 for left-to-right, -1 for right-to-left */ 1.636 + int dirnc; /* dir * nc */ 1.637 + int ci; 1.638 + int row; 1.639 + JDIMENSION col; 1.640 + JDIMENSION width = cinfo->output_width; 1.641 + JSAMPLE *range_limit = cinfo->sample_range_limit; 1.642 + SHIFT_TEMPS 1.643 + 1.644 + for (row = 0; row < num_rows; row++) { 1.645 + /* Initialize output values to 0 so can process components separately */ 1.646 + jzero_far((void FAR *) output_buf[row], 1.647 + (size_t) (width * SIZEOF(JSAMPLE))); 1.648 + for (ci = 0; ci < nc; ci++) { 1.649 + input_ptr = input_buf[row] + ci; 1.650 + output_ptr = output_buf[row]; 1.651 + if (cquantize->on_odd_row) { 1.652 + /* work right to left in this row */ 1.653 + input_ptr += (width-1) * nc; /* so point to rightmost pixel */ 1.654 + output_ptr += width-1; 1.655 + dir = -1; 1.656 + dirnc = -nc; 1.657 + errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */ 1.658 + } else { 1.659 + /* work left to right in this row */ 1.660 + dir = 1; 1.661 + dirnc = nc; 1.662 + errorptr = cquantize->fserrors[ci]; /* => entry before first column */ 1.663 + } 1.664 + colorindex_ci = cquantize->colorindex[ci]; 1.665 + colormap_ci = cquantize->sv_colormap[ci]; 1.666 + /* Preset error values: no error propagated to first pixel from left */ 1.667 + cur = 0; 1.668 + /* and no error propagated to row below yet */ 1.669 + belowerr = bpreverr = 0; 1.670 + 1.671 + for (col = width; col > 0; col--) { 1.672 + /* cur holds the error propagated from the previous pixel on the 1.673 + * current line. Add the error propagated from the previous line 1.674 + * to form the complete error correction term for this pixel, and 1.675 + * round the error term (which is expressed * 16) to an integer. 1.676 + * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct 1.677 + * for either sign of the error value. 1.678 + * Note: errorptr points to *previous* column's array entry. 1.679 + */ 1.680 + cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4); 1.681 + /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE. 1.682 + * The maximum error is +- MAXJSAMPLE; this sets the required size 1.683 + * of the range_limit array. 1.684 + */ 1.685 + cur += GETJSAMPLE(*input_ptr); 1.686 + cur = GETJSAMPLE(range_limit[cur]); 1.687 + /* Select output value, accumulate into output code for this pixel */ 1.688 + pixcode = GETJSAMPLE(colorindex_ci[cur]); 1.689 + *output_ptr += (JSAMPLE) pixcode; 1.690 + /* Compute actual representation error at this pixel */ 1.691 + /* Note: we can do this even though we don't have the final */ 1.692 + /* pixel code, because the colormap is orthogonal. */ 1.693 + cur -= GETJSAMPLE(colormap_ci[pixcode]); 1.694 + /* Compute error fractions to be propagated to adjacent pixels. 1.695 + * Add these into the running sums, and simultaneously shift the 1.696 + * next-line error sums left by 1 column. 1.697 + */ 1.698 + bnexterr = cur; 1.699 + delta = cur * 2; 1.700 + cur += delta; /* form error * 3 */ 1.701 + errorptr[0] = (FSERROR) (bpreverr + cur); 1.702 + cur += delta; /* form error * 5 */ 1.703 + bpreverr = belowerr + cur; 1.704 + belowerr = bnexterr; 1.705 + cur += delta; /* form error * 7 */ 1.706 + /* At this point cur contains the 7/16 error value to be propagated 1.707 + * to the next pixel on the current line, and all the errors for the 1.708 + * next line have been shifted over. We are therefore ready to move on. 1.709 + */ 1.710 + input_ptr += dirnc; /* advance input ptr to next column */ 1.711 + output_ptr += dir; /* advance output ptr to next column */ 1.712 + errorptr += dir; /* advance errorptr to current column */ 1.713 + } 1.714 + /* Post-loop cleanup: we must unload the final error value into the 1.715 + * final fserrors[] entry. Note we need not unload belowerr because 1.716 + * it is for the dummy column before or after the actual array. 1.717 + */ 1.718 + errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */ 1.719 + } 1.720 + cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE); 1.721 + } 1.722 +} 1.723 + 1.724 + 1.725 +/* 1.726 + * Allocate workspace for Floyd-Steinberg errors. 1.727 + */ 1.728 + 1.729 +LOCAL(void) 1.730 +alloc_fs_workspace (j_decompress_ptr cinfo) 1.731 +{ 1.732 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.733 + size_t arraysize; 1.734 + int i; 1.735 + 1.736 + arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); 1.737 + for (i = 0; i < cinfo->out_color_components; i++) { 1.738 + cquantize->fserrors[i] = (FSERRPTR) 1.739 + (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize); 1.740 + } 1.741 +} 1.742 + 1.743 + 1.744 +/* 1.745 + * Initialize for one-pass color quantization. 1.746 + */ 1.747 + 1.748 +METHODDEF(void) 1.749 +start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan) 1.750 +{ 1.751 + my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 1.752 + size_t arraysize; 1.753 + int i; 1.754 + 1.755 + /* Install my colormap. */ 1.756 + cinfo->colormap = cquantize->sv_colormap; 1.757 + cinfo->actual_number_of_colors = cquantize->sv_actual; 1.758 + 1.759 + /* Initialize for desired dithering mode. */ 1.760 + switch (cinfo->dither_mode) { 1.761 + case JDITHER_NONE: 1.762 + if (cinfo->out_color_components == 3) 1.763 + cquantize->pub.color_quantize = color_quantize3; 1.764 + else 1.765 + cquantize->pub.color_quantize = color_quantize; 1.766 + break; 1.767 + case JDITHER_ORDERED: 1.768 + if (cinfo->out_color_components == 3) 1.769 + cquantize->pub.color_quantize = quantize3_ord_dither; 1.770 + else 1.771 + cquantize->pub.color_quantize = quantize_ord_dither; 1.772 + cquantize->row_index = 0; /* initialize state for ordered dither */ 1.773 + /* If user changed to ordered dither from another mode, 1.774 + * we must recreate the color index table with padding. 1.775 + * This will cost extra space, but probably isn't very likely. 1.776 + */ 1.777 + if (! cquantize->is_padded) 1.778 + create_colorindex(cinfo); 1.779 + /* Create ordered-dither tables if we didn't already. */ 1.780 + if (cquantize->odither[0] == NULL) 1.781 + create_odither_tables(cinfo); 1.782 + break; 1.783 + case JDITHER_FS: 1.784 + cquantize->pub.color_quantize = quantize_fs_dither; 1.785 + cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */ 1.786 + /* Allocate Floyd-Steinberg workspace if didn't already. */ 1.787 + if (cquantize->fserrors[0] == NULL) 1.788 + alloc_fs_workspace(cinfo); 1.789 + /* Initialize the propagated errors to zero. */ 1.790 + arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); 1.791 + for (i = 0; i < cinfo->out_color_components; i++) 1.792 + jzero_far((void FAR *) cquantize->fserrors[i], arraysize); 1.793 + break; 1.794 + default: 1.795 + ERREXIT(cinfo, JERR_NOT_COMPILED); 1.796 + break; 1.797 + } 1.798 +} 1.799 + 1.800 + 1.801 +/* 1.802 + * Finish up at the end of the pass. 1.803 + */ 1.804 + 1.805 +METHODDEF(void) 1.806 +finish_pass_1_quant (j_decompress_ptr cinfo) 1.807 +{ 1.808 + /* no work in 1-pass case */ 1.809 +} 1.810 + 1.811 + 1.812 +/* 1.813 + * Switch to a new external colormap between output passes. 1.814 + * Shouldn't get to this module! 1.815 + */ 1.816 + 1.817 +METHODDEF(void) 1.818 +new_color_map_1_quant (j_decompress_ptr cinfo) 1.819 +{ 1.820 + ERREXIT(cinfo, JERR_MODE_CHANGE); 1.821 +} 1.822 + 1.823 + 1.824 +/* 1.825 + * Module initialization routine for 1-pass color quantization. 1.826 + */ 1.827 + 1.828 +GLOBAL(void) 1.829 +jinit_1pass_quantizer (j_decompress_ptr cinfo) 1.830 +{ 1.831 + my_cquantize_ptr cquantize; 1.832 + 1.833 + cquantize = (my_cquantize_ptr) 1.834 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.835 + SIZEOF(my_cquantizer)); 1.836 + cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize; 1.837 + cquantize->pub.start_pass = start_pass_1_quant; 1.838 + cquantize->pub.finish_pass = finish_pass_1_quant; 1.839 + cquantize->pub.new_color_map = new_color_map_1_quant; 1.840 + cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */ 1.841 + cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */ 1.842 + 1.843 + /* Make sure my internal arrays won't overflow */ 1.844 + if (cinfo->out_color_components > MAX_Q_COMPS) 1.845 + ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS); 1.846 + /* Make sure colormap indexes can be represented by JSAMPLEs */ 1.847 + if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1)) 1.848 + ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1); 1.849 + 1.850 + /* Create the colormap and color index table. */ 1.851 + create_colormap(cinfo); 1.852 + create_colorindex(cinfo); 1.853 + 1.854 + /* Allocate Floyd-Steinberg workspace now if requested. 1.855 + * We do this now since it is FAR storage and may affect the memory 1.856 + * manager's space calculations. If the user changes to FS dither 1.857 + * mode in a later pass, we will allocate the space then, and will 1.858 + * possibly overrun the max_memory_to_use setting. 1.859 + */ 1.860 + if (cinfo->dither_mode == JDITHER_FS) 1.861 + alloc_fs_workspace(cinfo); 1.862 +} 1.863 + 1.864 +#endif /* QUANT_1PASS_SUPPORTED */