170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * jquant1.c 370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Copyright (C) 1991-1996, Thomas G. Lane. 570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file is part of the Independent JPEG Group's software. 670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * For conditions of distribution and use, see the accompanying README file. 770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file contains 1-pass color quantization (color mapping) routines. 970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * These routines provide mapping to a fixed color map using equally spaced 1070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * color values. Optional Floyd-Steinberg or ordered dithering is available. 1170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 1270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 1370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define JPEG_INTERNALS 1470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jinclude.h" 1570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jpeglib.h" 1670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 1770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef QUANT_1PASS_SUPPORTED 1870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 1970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 2070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 2170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The main purpose of 1-pass quantization is to provide a fast, if not very 2270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * high quality, colormapped output capability. A 2-pass quantizer usually 2370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * gives better visual quality; however, for quantized grayscale output this 2470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * quantizer is perfectly adequate. Dithering is highly recommended with this 2570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * quantizer, though you can turn it off if you really want to. 2670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 2770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * In 1-pass quantization the colormap must be chosen in advance of seeing the 2870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * image. We use a map consisting of all combinations of Ncolors[i] color 2970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * values for the i'th component. The Ncolors[] values are chosen so that 3070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * their product, the total number of colors, is no more than that requested. 3170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (In most cases, the product will be somewhat less.) 3270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 3370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Since the colormap is orthogonal, the representative value for each color 3470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * component can be determined without considering the other components; 3570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * then these indexes can be combined into a colormap index by a standard 3670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * N-dimensional-array-subscript calculation. Most of the arithmetic involved 3770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * can be precalculated and stored in the lookup table colorindex[]. 3870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * colorindex[i][j] maps pixel value j in component i to the nearest 3970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * representative value (grid plane) for that component; this index is 4070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * multiplied by the array stride for component i, so that the 4170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * index of the colormap entry closest to a given pixel value is just 4270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * sum( colorindex[component-number][pixel-component-value] ) 4370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Aside from being fast, this scheme allows for variable spacing between 4470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * representative values with no additional lookup cost. 4570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 4670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * If gamma correction has been applied in color conversion, it might be wise 4770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * to adjust the color grid spacing so that the representative colors are 4870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * equidistant in linear space. At this writing, gamma correction is not 4970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * implemented by jdcolor, so nothing is done here. 5070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 5170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 5270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 5370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Declarations for ordered dithering. 5470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 5570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We use a standard 16x16 ordered dither array. The basic concept of ordered 5670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * dithering is described in many references, for instance Dale Schumacher's 5770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991). 5870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * In place of Schumacher's comparisons against a "threshold" value, we add a 5970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * "dither" value to the input pixel and then round the result to the nearest 6070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * output value. The dither value is equivalent to (0.5 - threshold) times 6170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the distance between output values. For ordered dithering, we assume that 6270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the output colors are equally spaced; if not, results will probably be 6370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * worse, since the dither may be too much or too little at a given point. 6470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 6570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The normal calculation would be to form pixel value + dither, range-limit 6670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual. 6770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We can skip the separate range-limiting step by extending the colorindex 6870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * table in both directions. 6970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 7070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 7170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define ODITHER_SIZE 16 /* dimension of dither matrix */ 7270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */ 7370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE) /* # cells in matrix */ 7470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define ODITHER_MASK (ODITHER_SIZE-1) /* mask for wrapping around counters */ 7570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 7670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE]; 7770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE]; 7870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 7970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinestatic const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = { 8070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Bayer's order-4 dither array. Generated by the code given in 8170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I. 8270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The values in this array must range from 0 to ODITHER_CELLS-1. 8370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 8470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 }, 8570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 }, 8670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 }, 8770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 }, 8870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 }, 8970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 }, 9070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 }, 9170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 }, 9270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 }, 9370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 }, 9470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 }, 9570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 }, 9670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 }, 9770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 }, 9870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 }, 9970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 } 10070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}; 10170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 10270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 10370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Declarations for Floyd-Steinberg dithering. 10470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 10570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Errors are accumulated into the array fserrors[], at a resolution of 10670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 1/16th of a pixel count. The error at a given pixel is propagated 10770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * to its not-yet-processed neighbors using the standard F-S fractions, 10870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * ... (here) 7/16 10970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 3/16 5/16 1/16 11070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We work left-to-right on even rows, right-to-left on odd rows. 11170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 11270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We can get away with a single array (holding one row's worth of errors) 11370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * by using it to store the current row's errors at pixel columns not yet 11470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * processed, but the next row's errors at columns already processed. We 11570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * need only a few extra variables to hold the errors immediately around the 11670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * current column. (If we are lucky, those variables are in registers, but 11770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * even if not, they're probably cheaper to access than array elements are.) 11870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 11970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The fserrors[] array is indexed [component#][position]. 12070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We provide (#columns + 2) entries per component; the extra entry at each 12170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * end saves us from special-casing the first and last pixels. 12270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 12370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Note: on a wide image, we might not have enough room in a PC's near data 12470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * segment to hold the error array; so it is allocated with alloc_large. 12570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 12670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 12770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#if BITS_IN_JSAMPLE == 8 12870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef INT16 FSERROR; /* 16 bits should be enough */ 12970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef int LOCFSERROR; /* use 'int' for calculation temps */ 13070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#else 13170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef INT32 FSERROR; /* may need more than 16 bits */ 13270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */ 13370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif 13470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 13570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */ 13670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 13770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 13870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Private subobject */ 13970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 14070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define MAX_Q_COMPS 4 /* max components I can handle */ 14170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 14270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef struct { 14370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine struct jpeg_color_quantizer pub; /* public fields */ 14470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 14570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Initially allocated colormap is saved here */ 14670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */ 14770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int sv_actual; /* number of entries in use */ 14870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 14970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY colorindex; /* Precomputed mapping for speed */ 15070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* colorindex[i][j] = index of color closest to pixel value j in component i, 15170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * premultiplied as described above. Since colormap indexes must fit into 15270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * JSAMPLEs, the entries of this array will too. 15370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 15470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine boolean is_padded; /* is the colorindex padded for odither? */ 15570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 15670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int Ncolors[MAX_Q_COMPS]; /* # of values alloced to each component */ 15770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 15870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Variables for ordered dithering */ 15970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row_index; /* cur row's vertical index in dither matrix */ 16070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */ 16170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 16270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Variables for Floyd-Steinberg dithering */ 16370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */ 16470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine boolean on_odd_row; /* flag to remember which row we are on */ 16570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} my_cquantizer; 16670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 16770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef my_cquantizer * my_cquantize_ptr; 16870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 16970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 17070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 17170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Policy-making subroutines for create_colormap and create_colorindex. 17270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * These routines determine the colormap to be used. The rest of the module 17370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * only assumes that the colormap is orthogonal. 17470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * 17570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * * select_ncolors decides how to divvy up the available colors 17670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * among the components. 17770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * * output_value defines the set of representative values for a component. 17870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * * largest_input_value defines the mapping from input values to 17970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * representative values for a component. 18070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Note that the latter two routines may impose different policies for 18170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * different components, though this is not currently done. 18270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 18370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 18470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 18570a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(int) 18670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineselect_ncolors (j_decompress_ptr cinfo, int Ncolors[]) 18770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Determine allocation of desired colors to components, */ 18870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* and fill in Ncolors[] array to indicate choice. */ 18970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Return value is total number of colors (product of Ncolors[] values). */ 19070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 19170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int nc = cinfo->out_color_components; /* number of color components */ 19270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int max_colors = cinfo->desired_number_of_colors; 19370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int total_colors, iroot, i, j; 19470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine boolean changed; 19570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine long temp; 19670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE }; 19770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 19870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* We can allocate at least the nc'th root of max_colors per component. */ 19970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Compute floor(nc'th root of max_colors). */ 20070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine iroot = 1; 20170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine do { 20270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine iroot++; 20370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine temp = iroot; /* set temp = iroot ** nc */ 20470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 1; i < nc; i++) 20570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine temp *= iroot; 20670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */ 20770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine iroot--; /* now iroot = floor(root) */ 20870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 20970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Must have at least 2 color values per component */ 21070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (iroot < 2) 21170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp); 21270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 21370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Initialize to iroot color values for each component */ 21470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine total_colors = 1; 21570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 0; i < nc; i++) { 21670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine Ncolors[i] = iroot; 21770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine total_colors *= iroot; 21870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 21970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* We may be able to increment the count for one or more components without 22070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * exceeding max_colors, though we know not all can be incremented. 22170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Sometimes, the first component can be incremented more than once! 22270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.) 22370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * In RGB colorspace, try to increment G first, then R, then B. 22470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 22570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine do { 22670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine changed = FALSE; 22770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 0; i < nc; i++) { 22870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i); 22970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* calculate new total_colors if Ncolors[j] is incremented */ 23070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine temp = total_colors / Ncolors[j]; 23170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine temp *= Ncolors[j]+1; /* done in long arith to avoid oflo */ 23270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (temp > (long) max_colors) 23370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine break; /* won't fit, done with this pass */ 23470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine Ncolors[j]++; /* OK, apply the increment */ 23570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine total_colors = (int) temp; 23670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine changed = TRUE; 23770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 23870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } while (changed); 23970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 24070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine return total_colors; 24170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 24270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 24370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 24470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(int) 24570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineoutput_value (j_decompress_ptr cinfo, int ci, int j, int maxj) 24670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Return j'th output value, where j will range from 0 to maxj */ 24770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* The output values must fall in 0..MAXJSAMPLE in increasing order */ 24870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 24970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* We always provide values 0 and MAXJSAMPLE for each component; 25070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * any additional values are equally spaced between these limits. 25170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (Forcing the upper and lower values to the limits ensures that 25270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * dithering can't produce a color outside the selected gamut.) 25370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 25470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj); 25570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 25670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 25770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 25870a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(int) 25970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinelargest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj) 26070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Return largest input value that should map to j'th output value */ 26170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */ 26270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 26370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Breakpoints are halfway between values returned by output_value */ 26470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj)); 26570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 26670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 26770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 26870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 26970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Create the colormap. 27070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 27170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 27270a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(void) 27370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecreate_colormap (j_decompress_ptr cinfo) 27470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 27570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 27670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY colormap; /* Created colormap */ 27770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int total_colors; /* Number of distinct output colors */ 27870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int i,j,k, nci, blksize, blkdist, ptr, val; 27970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 28070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Select number of colors for each component */ 28170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine total_colors = select_ncolors(cinfo, cquantize->Ncolors); 28270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 28370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Report selected color counts */ 28470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cinfo->out_color_components == 3) 28570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS, 28670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine total_colors, cquantize->Ncolors[0], 28770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->Ncolors[1], cquantize->Ncolors[2]); 28870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine else 28970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors); 29070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 29170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Allocate and fill in the colormap. */ 29270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* The colors are ordered in the map in standard row-major order, */ 29370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* i.e. rightmost (highest-indexed) color changes most rapidly. */ 29470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 29570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine colormap = (*cinfo->mem->alloc_sarray) 29670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ((j_common_ptr) cinfo, JPOOL_IMAGE, 29770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components); 29870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 29970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* blksize is number of adjacent repeated entries for a component */ 30070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* blkdist is distance between groups of identical entries for a component */ 30170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine blkdist = total_colors; 30270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 30370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 0; i < cinfo->out_color_components; i++) { 30470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* fill in colormap entries for i'th color component */ 30570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 30670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine blksize = blkdist / nci; 30770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (j = 0; j < nci; j++) { 30870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Compute j'th output value (out of nci) for component */ 30970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine val = output_value(cinfo, i, j, nci-1); 31070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Fill in all colormap entries that have this value of this component */ 31170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) { 31270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* fill in blksize entries beginning at ptr */ 31370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (k = 0; k < blksize; k++) 31470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine colormap[i][ptr+k] = (JSAMPLE) val; 31570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 31670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 31770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine blkdist = blksize; /* blksize of this color is blkdist of next */ 31870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 31970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 32070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Save the colormap in private storage, 32170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * where it will survive color quantization mode changes. 32270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 32370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->sv_colormap = colormap; 32470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->sv_actual = total_colors; 32570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 32670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 32770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 32870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 32970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Create the color index table. 33070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 33170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 33270a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(void) 33370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecreate_colorindex (j_decompress_ptr cinfo) 33470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 33570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 33670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW indexptr; 33770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int i,j,k, nci, blksize, val, pad; 33870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 33970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* For ordered dither, we pad the color index tables by MAXJSAMPLE in 34070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE). 34170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This is not necessary in the other dithering modes. However, we 34270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * flag whether it was done in case user changes dithering mode. 34370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 34470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cinfo->dither_mode == JDITHER_ORDERED) { 34570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pad = MAXJSAMPLE*2; 34670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->is_padded = TRUE; 34770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } else { 34870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pad = 0; 34970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->is_padded = FALSE; 35070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 35170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 35270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->colorindex = (*cinfo->mem->alloc_sarray) 35370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ((j_common_ptr) cinfo, JPOOL_IMAGE, 35470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (JDIMENSION) (MAXJSAMPLE+1 + pad), 35570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (JDIMENSION) cinfo->out_color_components); 35670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 35770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* blksize is number of adjacent repeated entries for a component */ 35870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine blksize = cquantize->sv_actual; 35970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 36070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 0; i < cinfo->out_color_components; i++) { 36170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* fill in colorindex entries for i'th color component */ 36270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 36370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine blksize = blksize / nci; 36470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 36570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* adjust colorindex pointers to provide padding at negative indexes. */ 36670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (pad) 36770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->colorindex[i] += MAXJSAMPLE; 36870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 36970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* in loop, val = index of current output value, */ 37070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* and k = largest j that maps to current val */ 37170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine indexptr = cquantize->colorindex[i]; 37270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine val = 0; 37370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine k = largest_input_value(cinfo, i, 0, nci-1); 37470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (j = 0; j <= MAXJSAMPLE; j++) { 37570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine while (j > k) /* advance val if past boundary */ 37670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine k = largest_input_value(cinfo, i, ++val, nci-1); 37770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* premultiply so that no multiplication needed in main processing */ 37870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine indexptr[j] = (JSAMPLE) (val * blksize); 37970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 38070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Pad at both ends if necessary */ 38170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (pad) 38270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (j = 1; j <= MAXJSAMPLE; j++) { 38370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine indexptr[-j] = indexptr[0]; 38470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE]; 38570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 38670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 38770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 38870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 38970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 39070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 39170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Create an ordered-dither array for a component having ncolors 39270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * distinct output values. 39370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 39470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 39570a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(ODITHER_MATRIX_PTR) 39670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinemake_odither_array (j_decompress_ptr cinfo, int ncolors) 39770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 39870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ODITHER_MATRIX_PTR odither; 39970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int j,k; 40070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine INT32 num,den; 40170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 40270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine odither = (ODITHER_MATRIX_PTR) 40370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 40470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine SIZEOF(ODITHER_MATRIX)); 40570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1). 40670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Hence the dither value for the matrix cell with fill order f 40770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1). 40870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * On 16-bit-int machine, be careful to avoid overflow. 40970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 41070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1)); 41170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (j = 0; j < ODITHER_SIZE; j++) { 41270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (k = 0; k < ODITHER_SIZE; k++) { 41370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k]))) 41470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * MAXJSAMPLE; 41570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Ensure round towards zero despite C's lack of consistency 41670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * about rounding negative values in integer division... 41770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 41870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den); 41970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 42070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 42170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine return odither; 42270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 42370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 42470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 42570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 42670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Create the ordered-dither tables. 42770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Components having the same number of representative colors may 42870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * share a dither table. 42970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 43070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 43170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(void) 43270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecreate_odither_tables (j_decompress_ptr cinfo) 43370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 43470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 43570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ODITHER_MATRIX_PTR odither; 43670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int i, j, nci; 43770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 43870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 0; i < cinfo->out_color_components; i++) { 43970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 44070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine odither = NULL; /* search for matching prior component */ 44170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (j = 0; j < i; j++) { 44270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (nci == cquantize->Ncolors[j]) { 44370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine odither = cquantize->odither[j]; 44470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine break; 44570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 44670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 44770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (odither == NULL) /* need a new table? */ 44870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine odither = make_odither_array(cinfo, nci); 44970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->odither[i] = odither; 45070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 45170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 45270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 45370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 45470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 45570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Map some rows of pixels to the output colormapped representation. 45670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 45770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 45870a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 45970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecolor_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 46070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY output_buf, int num_rows) 46170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* General case, no dithering */ 46270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 46370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 46470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY colorindex = cquantize->colorindex; 46570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register int pixcode, ci; 46670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW ptrin, ptrout; 46770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row; 46870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION col; 46970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION width = cinfo->output_width; 47070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register int nc = cinfo->out_color_components; 47170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 47270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (row = 0; row < num_rows; row++) { 47370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ptrin = input_buf[row]; 47470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ptrout = output_buf[row]; 47570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (col = width; col > 0; col--) { 47670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode = 0; 47770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (ci = 0; ci < nc; ci++) { 47870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]); 47970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 48070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *ptrout++ = (JSAMPLE) pixcode; 48170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 48270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 48370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 48470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 48570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 48670a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 48770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecolor_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 48870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY output_buf, int num_rows) 48970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Fast path for out_color_components==3, no dithering */ 49070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 49170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 49270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register int pixcode; 49370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW ptrin, ptrout; 49470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex0 = cquantize->colorindex[0]; 49570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex1 = cquantize->colorindex[1]; 49670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex2 = cquantize->colorindex[2]; 49770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row; 49870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION col; 49970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION width = cinfo->output_width; 50070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 50170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (row = 0; row < num_rows; row++) { 50270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ptrin = input_buf[row]; 50370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ptrout = output_buf[row]; 50470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (col = width; col > 0; col--) { 50570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]); 50670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]); 50770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]); 50870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *ptrout++ = (JSAMPLE) pixcode; 50970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 51070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 51170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 51270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 51370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 51470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 51570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinequantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 51670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY output_buf, int num_rows) 51770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* General case, with ordered dithering */ 51870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 51970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 52070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW input_ptr; 52170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW output_ptr; 52270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex_ci; 52370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int * dither; /* points to active row of dither matrix */ 52470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row_index, col_index; /* current indexes into dither matrix */ 52570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int nc = cinfo->out_color_components; 52670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int ci; 52770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row; 52870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION col; 52970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION width = cinfo->output_width; 53070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 53170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (row = 0; row < num_rows; row++) { 53270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Initialize output values to 0 so can process components separately */ 53370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine jzero_far((void FAR *) output_buf[row], 53470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (size_t) (width * SIZEOF(JSAMPLE))); 53570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine row_index = cquantize->row_index; 53670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (ci = 0; ci < nc; ci++) { 53770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine input_ptr = input_buf[row] + ci; 53870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine output_ptr = output_buf[row]; 53970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine colorindex_ci = cquantize->colorindex[ci]; 54070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dither = cquantize->odither[ci][row_index]; 54170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine col_index = 0; 54270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 54370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (col = width; col > 0; col--) { 54470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE, 54570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * select output value, accumulate into output code for this pixel. 54670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Range-limiting need not be done explicitly, as we have extended 54770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the colorindex table to produce the right answers for out-of-range 54870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * inputs. The maximum dither is +- MAXJSAMPLE; this sets the 54970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * required amount of padding. 55070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 55170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]]; 55270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine input_ptr += nc; 55370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine output_ptr++; 55470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine col_index = (col_index + 1) & ODITHER_MASK; 55570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 55670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 55770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Advance row index for next row */ 55870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine row_index = (row_index + 1) & ODITHER_MASK; 55970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->row_index = row_index; 56070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 56170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 56270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 56370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 56470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 56570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinequantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 56670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY output_buf, int num_rows) 56770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Fast path for out_color_components==3, with ordered dithering */ 56870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 56970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 57070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register int pixcode; 57170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW input_ptr; 57270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW output_ptr; 57370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex0 = cquantize->colorindex[0]; 57470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex1 = cquantize->colorindex[1]; 57570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex2 = cquantize->colorindex[2]; 57670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int * dither0; /* points to active row of dither matrix */ 57770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int * dither1; 57870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int * dither2; 57970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row_index, col_index; /* current indexes into dither matrix */ 58070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row; 58170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION col; 58270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION width = cinfo->output_width; 58370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 58470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (row = 0; row < num_rows; row++) { 58570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine row_index = cquantize->row_index; 58670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine input_ptr = input_buf[row]; 58770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine output_ptr = output_buf[row]; 58870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dither0 = cquantize->odither[0][row_index]; 58970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dither1 = cquantize->odither[1][row_index]; 59070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dither2 = cquantize->odither[2][row_index]; 59170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine col_index = 0; 59270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 59370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (col = width; col > 0; col--) { 59470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) + 59570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dither0[col_index]]); 59670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) + 59770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dither1[col_index]]); 59870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) + 59970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dither2[col_index]]); 60070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *output_ptr++ = (JSAMPLE) pixcode; 60170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine col_index = (col_index + 1) & ODITHER_MASK; 60270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 60370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine row_index = (row_index + 1) & ODITHER_MASK; 60470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->row_index = row_index; 60570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 60670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 60770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 60870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 60970a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 61070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinequantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 61170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPARRAY output_buf, int num_rows) 61270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* General case, with Floyd-Steinberg dithering */ 61370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 61470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 61570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register LOCFSERROR cur; /* current error or pixel value */ 61670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine LOCFSERROR belowerr; /* error for pixel below cur */ 61770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine LOCFSERROR bpreverr; /* error for below/prev col */ 61870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine LOCFSERROR bnexterr; /* error for below/next col */ 61970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine LOCFSERROR delta; 62070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register FSERRPTR errorptr; /* => fserrors[] at column before current */ 62170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW input_ptr; 62270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine register JSAMPROW output_ptr; 62370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colorindex_ci; 62470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPROW colormap_ci; 62570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int pixcode; 62670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int nc = cinfo->out_color_components; 62770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int dir; /* 1 for left-to-right, -1 for right-to-left */ 62870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int dirnc; /* dir * nc */ 62970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int ci; 63070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int row; 63170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION col; 63270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JDIMENSION width = cinfo->output_width; 63370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine JSAMPLE *range_limit = cinfo->sample_range_limit; 63470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine SHIFT_TEMPS 63570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 63670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (row = 0; row < num_rows; row++) { 63770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Initialize output values to 0 so can process components separately */ 63870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine jzero_far((void FAR *) output_buf[row], 63970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (size_t) (width * SIZEOF(JSAMPLE))); 64070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (ci = 0; ci < nc; ci++) { 64170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine input_ptr = input_buf[row] + ci; 64270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine output_ptr = output_buf[row]; 64370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cquantize->on_odd_row) { 64470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* work right to left in this row */ 64570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine input_ptr += (width-1) * nc; /* so point to rightmost pixel */ 64670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine output_ptr += width-1; 64770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dir = -1; 64870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dirnc = -nc; 64970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */ 65070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } else { 65170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* work left to right in this row */ 65270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dir = 1; 65370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine dirnc = nc; 65470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine errorptr = cquantize->fserrors[ci]; /* => entry before first column */ 65570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 65670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine colorindex_ci = cquantize->colorindex[ci]; 65770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine colormap_ci = cquantize->sv_colormap[ci]; 65870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Preset error values: no error propagated to first pixel from left */ 65970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur = 0; 66070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* and no error propagated to row below yet */ 66170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine belowerr = bpreverr = 0; 66270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 66370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (col = width; col > 0; col--) { 66470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* cur holds the error propagated from the previous pixel on the 66570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * current line. Add the error propagated from the previous line 66670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * to form the complete error correction term for this pixel, and 66770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * round the error term (which is expressed * 16) to an integer. 66870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct 66970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * for either sign of the error value. 67070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Note: errorptr points to *previous* column's array entry. 67170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 67270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4); 67370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE. 67470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The maximum error is +- MAXJSAMPLE; this sets the required size 67570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * of the range_limit array. 67670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 67770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur += GETJSAMPLE(*input_ptr); 67870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur = GETJSAMPLE(range_limit[cur]); 67970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Select output value, accumulate into output code for this pixel */ 68070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine pixcode = GETJSAMPLE(colorindex_ci[cur]); 68170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *output_ptr += (JSAMPLE) pixcode; 68270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Compute actual representation error at this pixel */ 68370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Note: we can do this even though we don't have the final */ 68470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* pixel code, because the colormap is orthogonal. */ 68570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur -= GETJSAMPLE(colormap_ci[pixcode]); 68670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Compute error fractions to be propagated to adjacent pixels. 68770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Add these into the running sums, and simultaneously shift the 68870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * next-line error sums left by 1 column. 68970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 69070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine bnexterr = cur; 69170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine delta = cur * 2; 69270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur += delta; /* form error * 3 */ 69370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine errorptr[0] = (FSERROR) (bpreverr + cur); 69470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur += delta; /* form error * 5 */ 69570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine bpreverr = belowerr + cur; 69670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine belowerr = bnexterr; 69770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cur += delta; /* form error * 7 */ 69870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* At this point cur contains the 7/16 error value to be propagated 69970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * to the next pixel on the current line, and all the errors for the 70070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * next line have been shifted over. We are therefore ready to move on. 70170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 70270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine input_ptr += dirnc; /* advance input ptr to next column */ 70370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine output_ptr += dir; /* advance output ptr to next column */ 70470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine errorptr += dir; /* advance errorptr to current column */ 70570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 70670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Post-loop cleanup: we must unload the final error value into the 70770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * final fserrors[] entry. Note we need not unload belowerr because 70870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * it is for the dummy column before or after the actual array. 70970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 71070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */ 71170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 71270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE); 71370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 71470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 71570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 71670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 71770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 71870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Allocate workspace for Floyd-Steinberg errors. 71970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 72070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 72170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(void) 72270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinealloc_fs_workspace (j_decompress_ptr cinfo) 72370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 72470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 72570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine size_t arraysize; 72670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int i; 72770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 72870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); 72970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 0; i < cinfo->out_color_components; i++) { 73070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->fserrors[i] = (FSERRPTR) 73170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize); 73270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 73370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 73470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 73570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 73670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 73770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Initialize for one-pass color quantization. 73870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 73970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 74070a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 74170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinestart_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan) 74270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 74370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 74470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine size_t arraysize; 74570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine int i; 74670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 74770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Install my colormap. */ 74870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cinfo->colormap = cquantize->sv_colormap; 74970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cinfo->actual_number_of_colors = cquantize->sv_actual; 75070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 75170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Initialize for desired dithering mode. */ 75270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine switch (cinfo->dither_mode) { 75370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine case JDITHER_NONE: 75470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cinfo->out_color_components == 3) 75570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.color_quantize = color_quantize3; 75670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine else 75770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.color_quantize = color_quantize; 75870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine break; 75970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine case JDITHER_ORDERED: 76070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cinfo->out_color_components == 3) 76170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.color_quantize = quantize3_ord_dither; 76270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine else 76370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.color_quantize = quantize_ord_dither; 76470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->row_index = 0; /* initialize state for ordered dither */ 76570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* If user changed to ordered dither from another mode, 76670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * we must recreate the color index table with padding. 76770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This will cost extra space, but probably isn't very likely. 76870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 76970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (! cquantize->is_padded) 77070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine create_colorindex(cinfo); 77170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Create ordered-dither tables if we didn't already. */ 77270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cquantize->odither[0] == NULL) 77370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine create_odither_tables(cinfo); 77470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine break; 77570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine case JDITHER_FS: 77670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.color_quantize = quantize_fs_dither; 77770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */ 77870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Allocate Floyd-Steinberg workspace if didn't already. */ 77970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cquantize->fserrors[0] == NULL) 78070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine alloc_fs_workspace(cinfo); 78170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Initialize the propagated errors to zero. */ 78270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); 78370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine for (i = 0; i < cinfo->out_color_components; i++) 78470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine jzero_far((void FAR *) cquantize->fserrors[i], arraysize); 78570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine break; 78670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine default: 78770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ERREXIT(cinfo, JERR_NOT_COMPILED); 78870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine break; 78970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine } 79070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 79170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 79270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 79370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 79470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Finish up at the end of the pass. 79570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 79670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 79770a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 79870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinefinish_pass_1_quant (j_decompress_ptr cinfo) 79970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 80070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* no work in 1-pass case */ 80170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 80270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 80370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 80470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 80570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Switch to a new external colormap between output passes. 80670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Shouldn't get to this module! 80770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 80870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 80970a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) 81070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinenew_color_map_1_quant (j_decompress_ptr cinfo) 81170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 81270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ERREXIT(cinfo, JERR_MODE_CHANGE); 81370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 81470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 81570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 81670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* 81770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Module initialization routine for 1-pass color quantization. 81870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 81970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 82070a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void) 82170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejinit_1pass_quantizer (j_decompress_ptr cinfo) 82270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{ 82370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine my_cquantize_ptr cquantize; 82470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 82570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize = (my_cquantize_ptr) 82670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 82770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine SIZEOF(my_cquantizer)); 82870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize; 82970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.start_pass = start_pass_1_quant; 83070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.finish_pass = finish_pass_1_quant; 83170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->pub.new_color_map = new_color_map_1_quant; 83270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */ 83370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */ 83470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 83570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Make sure my internal arrays won't overflow */ 83670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cinfo->out_color_components > MAX_Q_COMPS) 83770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS); 83870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Make sure colormap indexes can be represented by JSAMPLEs */ 83970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1)) 84070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1); 84170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 84270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Create the colormap and color index table. */ 84370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine create_colormap(cinfo); 84470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine create_colorindex(cinfo); 84570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 84670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine /* Allocate Floyd-Steinberg workspace now if requested. 84770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We do this now since it is FAR storage and may affect the memory 84870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * manager's space calculations. If the user changes to FS dither 84970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * mode in a later pass, we will allocate the space then, and will 85070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * possibly overrun the max_memory_to_use setting. 85170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */ 85270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine if (cinfo->dither_mode == JDITHER_FS) 85370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine alloc_fs_workspace(cinfo); 85470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} 85570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine 85670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif /* QUANT_1PASS_SUPPORTED */ 857