Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /*
shoaib_ahmed 0:791a779d6220 2 * jquant1.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1991-1996, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2011 by Guido Vollbeding.
shoaib_ahmed 0:791a779d6220 6 * This file is part of the Independent JPEG Group's software.
shoaib_ahmed 0:791a779d6220 7 * For conditions of distribution and use, see the accompanying README file.
shoaib_ahmed 0:791a779d6220 8 *
shoaib_ahmed 0:791a779d6220 9 * This file contains 1-pass color quantization (color mapping) routines.
shoaib_ahmed 0:791a779d6220 10 * These routines provide mapping to a fixed color map using equally spaced
shoaib_ahmed 0:791a779d6220 11 * color values. Optional Floyd-Steinberg or ordered dithering is available.
shoaib_ahmed 0:791a779d6220 12 */
shoaib_ahmed 0:791a779d6220 13
shoaib_ahmed 0:791a779d6220 14 #define JPEG_INTERNALS
shoaib_ahmed 0:791a779d6220 15 #include "jinclude.h"
shoaib_ahmed 0:791a779d6220 16 #include "jpeglib.h"
shoaib_ahmed 0:791a779d6220 17
shoaib_ahmed 0:791a779d6220 18 #ifdef QUANT_1PASS_SUPPORTED
shoaib_ahmed 0:791a779d6220 19
shoaib_ahmed 0:791a779d6220 20
shoaib_ahmed 0:791a779d6220 21 /*
shoaib_ahmed 0:791a779d6220 22 * The main purpose of 1-pass quantization is to provide a fast, if not very
shoaib_ahmed 0:791a779d6220 23 * high quality, colormapped output capability. A 2-pass quantizer usually
shoaib_ahmed 0:791a779d6220 24 * gives better visual quality; however, for quantized grayscale output this
shoaib_ahmed 0:791a779d6220 25 * quantizer is perfectly adequate. Dithering is highly recommended with this
shoaib_ahmed 0:791a779d6220 26 * quantizer, though you can turn it off if you really want to.
shoaib_ahmed 0:791a779d6220 27 *
shoaib_ahmed 0:791a779d6220 28 * In 1-pass quantization the colormap must be chosen in advance of seeing the
shoaib_ahmed 0:791a779d6220 29 * image. We use a map consisting of all combinations of Ncolors[i] color
shoaib_ahmed 0:791a779d6220 30 * values for the i'th component. The Ncolors[] values are chosen so that
shoaib_ahmed 0:791a779d6220 31 * their product, the total number of colors, is no more than that requested.
shoaib_ahmed 0:791a779d6220 32 * (In most cases, the product will be somewhat less.)
shoaib_ahmed 0:791a779d6220 33 *
shoaib_ahmed 0:791a779d6220 34 * Since the colormap is orthogonal, the representative value for each color
shoaib_ahmed 0:791a779d6220 35 * component can be determined without considering the other components;
shoaib_ahmed 0:791a779d6220 36 * then these indexes can be combined into a colormap index by a standard
shoaib_ahmed 0:791a779d6220 37 * N-dimensional-array-subscript calculation. Most of the arithmetic involved
shoaib_ahmed 0:791a779d6220 38 * can be precalculated and stored in the lookup table colorindex[].
shoaib_ahmed 0:791a779d6220 39 * colorindex[i][j] maps pixel value j in component i to the nearest
shoaib_ahmed 0:791a779d6220 40 * representative value (grid plane) for that component; this index is
shoaib_ahmed 0:791a779d6220 41 * multiplied by the array stride for component i, so that the
shoaib_ahmed 0:791a779d6220 42 * index of the colormap entry closest to a given pixel value is just
shoaib_ahmed 0:791a779d6220 43 * sum( colorindex[component-number][pixel-component-value] )
shoaib_ahmed 0:791a779d6220 44 * Aside from being fast, this scheme allows for variable spacing between
shoaib_ahmed 0:791a779d6220 45 * representative values with no additional lookup cost.
shoaib_ahmed 0:791a779d6220 46 *
shoaib_ahmed 0:791a779d6220 47 * If gamma correction has been applied in color conversion, it might be wise
shoaib_ahmed 0:791a779d6220 48 * to adjust the color grid spacing so that the representative colors are
shoaib_ahmed 0:791a779d6220 49 * equidistant in linear space. At this writing, gamma correction is not
shoaib_ahmed 0:791a779d6220 50 * implemented by jdcolor, so nothing is done here.
shoaib_ahmed 0:791a779d6220 51 */
shoaib_ahmed 0:791a779d6220 52
shoaib_ahmed 0:791a779d6220 53
shoaib_ahmed 0:791a779d6220 54 /* Declarations for ordered dithering.
shoaib_ahmed 0:791a779d6220 55 *
shoaib_ahmed 0:791a779d6220 56 * We use a standard 16x16 ordered dither array. The basic concept of ordered
shoaib_ahmed 0:791a779d6220 57 * dithering is described in many references, for instance Dale Schumacher's
shoaib_ahmed 0:791a779d6220 58 * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991).
shoaib_ahmed 0:791a779d6220 59 * In place of Schumacher's comparisons against a "threshold" value, we add a
shoaib_ahmed 0:791a779d6220 60 * "dither" value to the input pixel and then round the result to the nearest
shoaib_ahmed 0:791a779d6220 61 * output value. The dither value is equivalent to (0.5 - threshold) times
shoaib_ahmed 0:791a779d6220 62 * the distance between output values. For ordered dithering, we assume that
shoaib_ahmed 0:791a779d6220 63 * the output colors are equally spaced; if not, results will probably be
shoaib_ahmed 0:791a779d6220 64 * worse, since the dither may be too much or too little at a given point.
shoaib_ahmed 0:791a779d6220 65 *
shoaib_ahmed 0:791a779d6220 66 * The normal calculation would be to form pixel value + dither, range-limit
shoaib_ahmed 0:791a779d6220 67 * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual.
shoaib_ahmed 0:791a779d6220 68 * We can skip the separate range-limiting step by extending the colorindex
shoaib_ahmed 0:791a779d6220 69 * table in both directions.
shoaib_ahmed 0:791a779d6220 70 */
shoaib_ahmed 0:791a779d6220 71
shoaib_ahmed 0:791a779d6220 72 #define ODITHER_SIZE 16 /* dimension of dither matrix */
shoaib_ahmed 0:791a779d6220 73 /* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */
shoaib_ahmed 0:791a779d6220 74 #define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE) /* # cells in matrix */
shoaib_ahmed 0:791a779d6220 75 #define ODITHER_MASK (ODITHER_SIZE-1) /* mask for wrapping around counters */
shoaib_ahmed 0:791a779d6220 76
shoaib_ahmed 0:791a779d6220 77 typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
shoaib_ahmed 0:791a779d6220 78 typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];
shoaib_ahmed 0:791a779d6220 79
shoaib_ahmed 0:791a779d6220 80 static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
shoaib_ahmed 0:791a779d6220 81 /* Bayer's order-4 dither array. Generated by the code given in
shoaib_ahmed 0:791a779d6220 82 * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I.
shoaib_ahmed 0:791a779d6220 83 * The values in this array must range from 0 to ODITHER_CELLS-1.
shoaib_ahmed 0:791a779d6220 84 */
shoaib_ahmed 0:791a779d6220 85 { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
shoaib_ahmed 0:791a779d6220 86 { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
shoaib_ahmed 0:791a779d6220 87 { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
shoaib_ahmed 0:791a779d6220 88 { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
shoaib_ahmed 0:791a779d6220 89 { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
shoaib_ahmed 0:791a779d6220 90 { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
shoaib_ahmed 0:791a779d6220 91 { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
shoaib_ahmed 0:791a779d6220 92 { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
shoaib_ahmed 0:791a779d6220 93 { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
shoaib_ahmed 0:791a779d6220 94 { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
shoaib_ahmed 0:791a779d6220 95 { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
shoaib_ahmed 0:791a779d6220 96 { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
shoaib_ahmed 0:791a779d6220 97 { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
shoaib_ahmed 0:791a779d6220 98 { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
shoaib_ahmed 0:791a779d6220 99 { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
shoaib_ahmed 0:791a779d6220 100 { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
shoaib_ahmed 0:791a779d6220 101 };
shoaib_ahmed 0:791a779d6220 102
shoaib_ahmed 0:791a779d6220 103
shoaib_ahmed 0:791a779d6220 104 /* Declarations for Floyd-Steinberg dithering.
shoaib_ahmed 0:791a779d6220 105 *
shoaib_ahmed 0:791a779d6220 106 * Errors are accumulated into the array fserrors[], at a resolution of
shoaib_ahmed 0:791a779d6220 107 * 1/16th of a pixel count. The error at a given pixel is propagated
shoaib_ahmed 0:791a779d6220 108 * to its not-yet-processed neighbors using the standard F-S fractions,
shoaib_ahmed 0:791a779d6220 109 * ... (here) 7/16
shoaib_ahmed 0:791a779d6220 110 * 3/16 5/16 1/16
shoaib_ahmed 0:791a779d6220 111 * We work left-to-right on even rows, right-to-left on odd rows.
shoaib_ahmed 0:791a779d6220 112 *
shoaib_ahmed 0:791a779d6220 113 * We can get away with a single array (holding one row's worth of errors)
shoaib_ahmed 0:791a779d6220 114 * by using it to store the current row's errors at pixel columns not yet
shoaib_ahmed 0:791a779d6220 115 * processed, but the next row's errors at columns already processed. We
shoaib_ahmed 0:791a779d6220 116 * need only a few extra variables to hold the errors immediately around the
shoaib_ahmed 0:791a779d6220 117 * current column. (If we are lucky, those variables are in registers, but
shoaib_ahmed 0:791a779d6220 118 * even if not, they're probably cheaper to access than array elements are.)
shoaib_ahmed 0:791a779d6220 119 *
shoaib_ahmed 0:791a779d6220 120 * The fserrors[] array is indexed [component#][position].
shoaib_ahmed 0:791a779d6220 121 * We provide (#columns + 2) entries per component; the extra entry at each
shoaib_ahmed 0:791a779d6220 122 * end saves us from special-casing the first and last pixels.
shoaib_ahmed 0:791a779d6220 123 *
shoaib_ahmed 0:791a779d6220 124 * Note: on a wide image, we might not have enough room in a PC's near data
shoaib_ahmed 0:791a779d6220 125 * segment to hold the error array; so it is allocated with alloc_large.
shoaib_ahmed 0:791a779d6220 126 */
shoaib_ahmed 0:791a779d6220 127
shoaib_ahmed 0:791a779d6220 128 #if BITS_IN_JSAMPLE == 8
shoaib_ahmed 0:791a779d6220 129 typedef INT16 FSERROR; /* 16 bits should be enough */
shoaib_ahmed 0:791a779d6220 130 typedef int LOCFSERROR; /* use 'int' for calculation temps */
shoaib_ahmed 0:791a779d6220 131 #else
shoaib_ahmed 0:791a779d6220 132 typedef INT32 FSERROR; /* may need more than 16 bits */
shoaib_ahmed 0:791a779d6220 133 typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */
shoaib_ahmed 0:791a779d6220 134 #endif
shoaib_ahmed 0:791a779d6220 135
shoaib_ahmed 0:791a779d6220 136 typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */
shoaib_ahmed 0:791a779d6220 137
shoaib_ahmed 0:791a779d6220 138
shoaib_ahmed 0:791a779d6220 139 /* Private subobject */
shoaib_ahmed 0:791a779d6220 140
shoaib_ahmed 0:791a779d6220 141 #define MAX_Q_COMPS 4 /* max components I can handle */
shoaib_ahmed 0:791a779d6220 142
shoaib_ahmed 0:791a779d6220 143 typedef struct {
shoaib_ahmed 0:791a779d6220 144 struct jpeg_color_quantizer pub; /* public fields */
shoaib_ahmed 0:791a779d6220 145
shoaib_ahmed 0:791a779d6220 146 /* Initially allocated colormap is saved here */
shoaib_ahmed 0:791a779d6220 147 JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */
shoaib_ahmed 0:791a779d6220 148 int sv_actual; /* number of entries in use */
shoaib_ahmed 0:791a779d6220 149
shoaib_ahmed 0:791a779d6220 150 JSAMPARRAY colorindex; /* Precomputed mapping for speed */
shoaib_ahmed 0:791a779d6220 151 /* colorindex[i][j] = index of color closest to pixel value j in component i,
shoaib_ahmed 0:791a779d6220 152 * premultiplied as described above. Since colormap indexes must fit into
shoaib_ahmed 0:791a779d6220 153 * JSAMPLEs, the entries of this array will too.
shoaib_ahmed 0:791a779d6220 154 */
shoaib_ahmed 0:791a779d6220 155 boolean is_padded; /* is the colorindex padded for odither? */
shoaib_ahmed 0:791a779d6220 156
shoaib_ahmed 0:791a779d6220 157 int Ncolors[MAX_Q_COMPS]; /* # of values alloced to each component */
shoaib_ahmed 0:791a779d6220 158
shoaib_ahmed 0:791a779d6220 159 /* Variables for ordered dithering */
shoaib_ahmed 0:791a779d6220 160 int row_index; /* cur row's vertical index in dither matrix */
shoaib_ahmed 0:791a779d6220 161 ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
shoaib_ahmed 0:791a779d6220 162
shoaib_ahmed 0:791a779d6220 163 /* Variables for Floyd-Steinberg dithering */
shoaib_ahmed 0:791a779d6220 164 FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
shoaib_ahmed 0:791a779d6220 165 boolean on_odd_row; /* flag to remember which row we are on */
shoaib_ahmed 0:791a779d6220 166 } my_cquantizer;
shoaib_ahmed 0:791a779d6220 167
shoaib_ahmed 0:791a779d6220 168 typedef my_cquantizer * my_cquantize_ptr;
shoaib_ahmed 0:791a779d6220 169
shoaib_ahmed 0:791a779d6220 170
shoaib_ahmed 0:791a779d6220 171 /*
shoaib_ahmed 0:791a779d6220 172 * Policy-making subroutines for create_colormap and create_colorindex.
shoaib_ahmed 0:791a779d6220 173 * These routines determine the colormap to be used. The rest of the module
shoaib_ahmed 0:791a779d6220 174 * only assumes that the colormap is orthogonal.
shoaib_ahmed 0:791a779d6220 175 *
shoaib_ahmed 0:791a779d6220 176 * * select_ncolors decides how to divvy up the available colors
shoaib_ahmed 0:791a779d6220 177 * among the components.
shoaib_ahmed 0:791a779d6220 178 * * output_value defines the set of representative values for a component.
shoaib_ahmed 0:791a779d6220 179 * * largest_input_value defines the mapping from input values to
shoaib_ahmed 0:791a779d6220 180 * representative values for a component.
shoaib_ahmed 0:791a779d6220 181 * Note that the latter two routines may impose different policies for
shoaib_ahmed 0:791a779d6220 182 * different components, though this is not currently done.
shoaib_ahmed 0:791a779d6220 183 */
shoaib_ahmed 0:791a779d6220 184
shoaib_ahmed 0:791a779d6220 185
shoaib_ahmed 0:791a779d6220 186 LOCAL(int)
shoaib_ahmed 0:791a779d6220 187 select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
shoaib_ahmed 0:791a779d6220 188 /* Determine allocation of desired colors to components, */
shoaib_ahmed 0:791a779d6220 189 /* and fill in Ncolors[] array to indicate choice. */
shoaib_ahmed 0:791a779d6220 190 /* Return value is total number of colors (product of Ncolors[] values). */
shoaib_ahmed 0:791a779d6220 191 {
shoaib_ahmed 0:791a779d6220 192 int nc = cinfo->out_color_components; /* number of color components */
shoaib_ahmed 0:791a779d6220 193 int max_colors = cinfo->desired_number_of_colors;
shoaib_ahmed 0:791a779d6220 194 int total_colors, iroot, i, j;
shoaib_ahmed 0:791a779d6220 195 boolean changed;
shoaib_ahmed 0:791a779d6220 196 long temp;
shoaib_ahmed 0:791a779d6220 197 static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
shoaib_ahmed 0:791a779d6220 198
shoaib_ahmed 0:791a779d6220 199 /* We can allocate at least the nc'th root of max_colors per component. */
shoaib_ahmed 0:791a779d6220 200 /* Compute floor(nc'th root of max_colors). */
shoaib_ahmed 0:791a779d6220 201 iroot = 1;
shoaib_ahmed 0:791a779d6220 202 do {
shoaib_ahmed 0:791a779d6220 203 iroot++;
shoaib_ahmed 0:791a779d6220 204 temp = iroot; /* set temp = iroot ** nc */
shoaib_ahmed 0:791a779d6220 205 for (i = 1; i < nc; i++)
shoaib_ahmed 0:791a779d6220 206 temp *= iroot;
shoaib_ahmed 0:791a779d6220 207 } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
shoaib_ahmed 0:791a779d6220 208 iroot--; /* now iroot = floor(root) */
shoaib_ahmed 0:791a779d6220 209
shoaib_ahmed 0:791a779d6220 210 /* Must have at least 2 color values per component */
shoaib_ahmed 0:791a779d6220 211 if (iroot < 2)
shoaib_ahmed 0:791a779d6220 212 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp);
shoaib_ahmed 0:791a779d6220 213
shoaib_ahmed 0:791a779d6220 214 /* Initialize to iroot color values for each component */
shoaib_ahmed 0:791a779d6220 215 total_colors = 1;
shoaib_ahmed 0:791a779d6220 216 for (i = 0; i < nc; i++) {
shoaib_ahmed 0:791a779d6220 217 Ncolors[i] = iroot;
shoaib_ahmed 0:791a779d6220 218 total_colors *= iroot;
shoaib_ahmed 0:791a779d6220 219 }
shoaib_ahmed 0:791a779d6220 220 /* We may be able to increment the count for one or more components without
shoaib_ahmed 0:791a779d6220 221 * exceeding max_colors, though we know not all can be incremented.
shoaib_ahmed 0:791a779d6220 222 * Sometimes, the first component can be incremented more than once!
shoaib_ahmed 0:791a779d6220 223 * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.)
shoaib_ahmed 0:791a779d6220 224 * In RGB colorspace, try to increment G first, then R, then B.
shoaib_ahmed 0:791a779d6220 225 */
shoaib_ahmed 0:791a779d6220 226 do {
shoaib_ahmed 0:791a779d6220 227 changed = FALSE;
shoaib_ahmed 0:791a779d6220 228 for (i = 0; i < nc; i++) {
shoaib_ahmed 0:791a779d6220 229 j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
shoaib_ahmed 0:791a779d6220 230 /* calculate new total_colors if Ncolors[j] is incremented */
shoaib_ahmed 0:791a779d6220 231 temp = total_colors / Ncolors[j];
shoaib_ahmed 0:791a779d6220 232 temp *= Ncolors[j]+1; /* done in long arith to avoid oflo */
shoaib_ahmed 0:791a779d6220 233 if (temp > (long) max_colors)
shoaib_ahmed 0:791a779d6220 234 break; /* won't fit, done with this pass */
shoaib_ahmed 0:791a779d6220 235 Ncolors[j]++; /* OK, apply the increment */
shoaib_ahmed 0:791a779d6220 236 total_colors = (int) temp;
shoaib_ahmed 0:791a779d6220 237 changed = TRUE;
shoaib_ahmed 0:791a779d6220 238 }
shoaib_ahmed 0:791a779d6220 239 } while (changed);
shoaib_ahmed 0:791a779d6220 240
shoaib_ahmed 0:791a779d6220 241 return total_colors;
shoaib_ahmed 0:791a779d6220 242 }
shoaib_ahmed 0:791a779d6220 243
shoaib_ahmed 0:791a779d6220 244
shoaib_ahmed 0:791a779d6220 245 LOCAL(int)
shoaib_ahmed 0:791a779d6220 246 output_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
shoaib_ahmed 0:791a779d6220 247 /* Return j'th output value, where j will range from 0 to maxj */
shoaib_ahmed 0:791a779d6220 248 /* The output values must fall in 0..MAXJSAMPLE in increasing order */
shoaib_ahmed 0:791a779d6220 249 {
shoaib_ahmed 0:791a779d6220 250 /* We always provide values 0 and MAXJSAMPLE for each component;
shoaib_ahmed 0:791a779d6220 251 * any additional values are equally spaced between these limits.
shoaib_ahmed 0:791a779d6220 252 * (Forcing the upper and lower values to the limits ensures that
shoaib_ahmed 0:791a779d6220 253 * dithering can't produce a color outside the selected gamut.)
shoaib_ahmed 0:791a779d6220 254 */
shoaib_ahmed 0:791a779d6220 255 return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
shoaib_ahmed 0:791a779d6220 256 }
shoaib_ahmed 0:791a779d6220 257
shoaib_ahmed 0:791a779d6220 258
shoaib_ahmed 0:791a779d6220 259 LOCAL(int)
shoaib_ahmed 0:791a779d6220 260 largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
shoaib_ahmed 0:791a779d6220 261 /* Return largest input value that should map to j'th output value */
shoaib_ahmed 0:791a779d6220 262 /* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
shoaib_ahmed 0:791a779d6220 263 {
shoaib_ahmed 0:791a779d6220 264 /* Breakpoints are halfway between values returned by output_value */
shoaib_ahmed 0:791a779d6220 265 return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj));
shoaib_ahmed 0:791a779d6220 266 }
shoaib_ahmed 0:791a779d6220 267
shoaib_ahmed 0:791a779d6220 268
shoaib_ahmed 0:791a779d6220 269 /*
shoaib_ahmed 0:791a779d6220 270 * Create the colormap.
shoaib_ahmed 0:791a779d6220 271 */
shoaib_ahmed 0:791a779d6220 272
shoaib_ahmed 0:791a779d6220 273 LOCAL(void)
shoaib_ahmed 0:791a779d6220 274 create_colormap (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 275 {
shoaib_ahmed 0:791a779d6220 276 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 277 JSAMPARRAY colormap; /* Created colormap */
shoaib_ahmed 0:791a779d6220 278 int total_colors; /* Number of distinct output colors */
shoaib_ahmed 0:791a779d6220 279 int i,j,k, nci, blksize, blkdist, ptr, val;
shoaib_ahmed 0:791a779d6220 280
shoaib_ahmed 0:791a779d6220 281 /* Select number of colors for each component */
shoaib_ahmed 0:791a779d6220 282 total_colors = select_ncolors(cinfo, cquantize->Ncolors);
shoaib_ahmed 0:791a779d6220 283
shoaib_ahmed 0:791a779d6220 284 /* Report selected color counts */
shoaib_ahmed 0:791a779d6220 285 if (cinfo->out_color_components == 3)
shoaib_ahmed 0:791a779d6220 286 TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
shoaib_ahmed 0:791a779d6220 287 total_colors, cquantize->Ncolors[0],
shoaib_ahmed 0:791a779d6220 288 cquantize->Ncolors[1], cquantize->Ncolors[2]);
shoaib_ahmed 0:791a779d6220 289 else
shoaib_ahmed 0:791a779d6220 290 TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
shoaib_ahmed 0:791a779d6220 291
shoaib_ahmed 0:791a779d6220 292 /* Allocate and fill in the colormap. */
shoaib_ahmed 0:791a779d6220 293 /* The colors are ordered in the map in standard row-major order, */
shoaib_ahmed 0:791a779d6220 294 /* i.e. rightmost (highest-indexed) color changes most rapidly. */
shoaib_ahmed 0:791a779d6220 295
shoaib_ahmed 0:791a779d6220 296 colormap = (*cinfo->mem->alloc_sarray)
shoaib_ahmed 0:791a779d6220 297 ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 298 (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);
shoaib_ahmed 0:791a779d6220 299
shoaib_ahmed 0:791a779d6220 300 /* blksize is number of adjacent repeated entries for a component */
shoaib_ahmed 0:791a779d6220 301 /* blkdist is distance between groups of identical entries for a component */
shoaib_ahmed 0:791a779d6220 302 blkdist = total_colors;
shoaib_ahmed 0:791a779d6220 303
shoaib_ahmed 0:791a779d6220 304 for (i = 0; i < cinfo->out_color_components; i++) {
shoaib_ahmed 0:791a779d6220 305 /* fill in colormap entries for i'th color component */
shoaib_ahmed 0:791a779d6220 306 nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
shoaib_ahmed 0:791a779d6220 307 blksize = blkdist / nci;
shoaib_ahmed 0:791a779d6220 308 for (j = 0; j < nci; j++) {
shoaib_ahmed 0:791a779d6220 309 /* Compute j'th output value (out of nci) for component */
shoaib_ahmed 0:791a779d6220 310 val = output_value(cinfo, i, j, nci-1);
shoaib_ahmed 0:791a779d6220 311 /* Fill in all colormap entries that have this value of this component */
shoaib_ahmed 0:791a779d6220 312 for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
shoaib_ahmed 0:791a779d6220 313 /* fill in blksize entries beginning at ptr */
shoaib_ahmed 0:791a779d6220 314 for (k = 0; k < blksize; k++)
shoaib_ahmed 0:791a779d6220 315 colormap[i][ptr+k] = (JSAMPLE) val;
shoaib_ahmed 0:791a779d6220 316 }
shoaib_ahmed 0:791a779d6220 317 }
shoaib_ahmed 0:791a779d6220 318 blkdist = blksize; /* blksize of this color is blkdist of next */
shoaib_ahmed 0:791a779d6220 319 }
shoaib_ahmed 0:791a779d6220 320
shoaib_ahmed 0:791a779d6220 321 /* Save the colormap in private storage,
shoaib_ahmed 0:791a779d6220 322 * where it will survive color quantization mode changes.
shoaib_ahmed 0:791a779d6220 323 */
shoaib_ahmed 0:791a779d6220 324 cquantize->sv_colormap = colormap;
shoaib_ahmed 0:791a779d6220 325 cquantize->sv_actual = total_colors;
shoaib_ahmed 0:791a779d6220 326 }
shoaib_ahmed 0:791a779d6220 327
shoaib_ahmed 0:791a779d6220 328
shoaib_ahmed 0:791a779d6220 329 /*
shoaib_ahmed 0:791a779d6220 330 * Create the color index table.
shoaib_ahmed 0:791a779d6220 331 */
shoaib_ahmed 0:791a779d6220 332
shoaib_ahmed 0:791a779d6220 333 LOCAL(void)
shoaib_ahmed 0:791a779d6220 334 create_colorindex (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 335 {
shoaib_ahmed 0:791a779d6220 336 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 337 JSAMPROW indexptr;
shoaib_ahmed 0:791a779d6220 338 int i,j,k, nci, blksize, val, pad;
shoaib_ahmed 0:791a779d6220 339
shoaib_ahmed 0:791a779d6220 340 /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
shoaib_ahmed 0:791a779d6220 341 * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
shoaib_ahmed 0:791a779d6220 342 * This is not necessary in the other dithering modes. However, we
shoaib_ahmed 0:791a779d6220 343 * flag whether it was done in case user changes dithering mode.
shoaib_ahmed 0:791a779d6220 344 */
shoaib_ahmed 0:791a779d6220 345 if (cinfo->dither_mode == JDITHER_ORDERED) {
shoaib_ahmed 0:791a779d6220 346 pad = MAXJSAMPLE*2;
shoaib_ahmed 0:791a779d6220 347 cquantize->is_padded = TRUE;
shoaib_ahmed 0:791a779d6220 348 } else {
shoaib_ahmed 0:791a779d6220 349 pad = 0;
shoaib_ahmed 0:791a779d6220 350 cquantize->is_padded = FALSE;
shoaib_ahmed 0:791a779d6220 351 }
shoaib_ahmed 0:791a779d6220 352
shoaib_ahmed 0:791a779d6220 353 cquantize->colorindex = (*cinfo->mem->alloc_sarray)
shoaib_ahmed 0:791a779d6220 354 ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 355 (JDIMENSION) (MAXJSAMPLE+1 + pad),
shoaib_ahmed 0:791a779d6220 356 (JDIMENSION) cinfo->out_color_components);
shoaib_ahmed 0:791a779d6220 357
shoaib_ahmed 0:791a779d6220 358 /* blksize is number of adjacent repeated entries for a component */
shoaib_ahmed 0:791a779d6220 359 blksize = cquantize->sv_actual;
shoaib_ahmed 0:791a779d6220 360
shoaib_ahmed 0:791a779d6220 361 for (i = 0; i < cinfo->out_color_components; i++) {
shoaib_ahmed 0:791a779d6220 362 /* fill in colorindex entries for i'th color component */
shoaib_ahmed 0:791a779d6220 363 nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
shoaib_ahmed 0:791a779d6220 364 blksize = blksize / nci;
shoaib_ahmed 0:791a779d6220 365
shoaib_ahmed 0:791a779d6220 366 /* adjust colorindex pointers to provide padding at negative indexes. */
shoaib_ahmed 0:791a779d6220 367 if (pad)
shoaib_ahmed 0:791a779d6220 368 cquantize->colorindex[i] += MAXJSAMPLE;
shoaib_ahmed 0:791a779d6220 369
shoaib_ahmed 0:791a779d6220 370 /* in loop, val = index of current output value, */
shoaib_ahmed 0:791a779d6220 371 /* and k = largest j that maps to current val */
shoaib_ahmed 0:791a779d6220 372 indexptr = cquantize->colorindex[i];
shoaib_ahmed 0:791a779d6220 373 val = 0;
shoaib_ahmed 0:791a779d6220 374 k = largest_input_value(cinfo, i, 0, nci-1);
shoaib_ahmed 0:791a779d6220 375 for (j = 0; j <= MAXJSAMPLE; j++) {
shoaib_ahmed 0:791a779d6220 376 while (j > k) /* advance val if past boundary */
shoaib_ahmed 0:791a779d6220 377 k = largest_input_value(cinfo, i, ++val, nci-1);
shoaib_ahmed 0:791a779d6220 378 /* premultiply so that no multiplication needed in main processing */
shoaib_ahmed 0:791a779d6220 379 indexptr[j] = (JSAMPLE) (val * blksize);
shoaib_ahmed 0:791a779d6220 380 }
shoaib_ahmed 0:791a779d6220 381 /* Pad at both ends if necessary */
shoaib_ahmed 0:791a779d6220 382 if (pad)
shoaib_ahmed 0:791a779d6220 383 for (j = 1; j <= MAXJSAMPLE; j++) {
shoaib_ahmed 0:791a779d6220 384 indexptr[-j] = indexptr[0];
shoaib_ahmed 0:791a779d6220 385 indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
shoaib_ahmed 0:791a779d6220 386 }
shoaib_ahmed 0:791a779d6220 387 }
shoaib_ahmed 0:791a779d6220 388 }
shoaib_ahmed 0:791a779d6220 389
shoaib_ahmed 0:791a779d6220 390
shoaib_ahmed 0:791a779d6220 391 /*
shoaib_ahmed 0:791a779d6220 392 * Create an ordered-dither array for a component having ncolors
shoaib_ahmed 0:791a779d6220 393 * distinct output values.
shoaib_ahmed 0:791a779d6220 394 */
shoaib_ahmed 0:791a779d6220 395
shoaib_ahmed 0:791a779d6220 396 LOCAL(ODITHER_MATRIX_PTR)
shoaib_ahmed 0:791a779d6220 397 make_odither_array (j_decompress_ptr cinfo, int ncolors)
shoaib_ahmed 0:791a779d6220 398 {
shoaib_ahmed 0:791a779d6220 399 ODITHER_MATRIX_PTR odither;
shoaib_ahmed 0:791a779d6220 400 int j,k;
shoaib_ahmed 0:791a779d6220 401 INT32 num,den;
shoaib_ahmed 0:791a779d6220 402
shoaib_ahmed 0:791a779d6220 403 odither = (ODITHER_MATRIX_PTR)
shoaib_ahmed 0:791a779d6220 404 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 405 SIZEOF(ODITHER_MATRIX));
shoaib_ahmed 0:791a779d6220 406 /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
shoaib_ahmed 0:791a779d6220 407 * Hence the dither value for the matrix cell with fill order f
shoaib_ahmed 0:791a779d6220 408 * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
shoaib_ahmed 0:791a779d6220 409 * On 16-bit-int machine, be careful to avoid overflow.
shoaib_ahmed 0:791a779d6220 410 */
shoaib_ahmed 0:791a779d6220 411 den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1));
shoaib_ahmed 0:791a779d6220 412 for (j = 0; j < ODITHER_SIZE; j++) {
shoaib_ahmed 0:791a779d6220 413 for (k = 0; k < ODITHER_SIZE; k++) {
shoaib_ahmed 0:791a779d6220 414 num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k])))
shoaib_ahmed 0:791a779d6220 415 * MAXJSAMPLE;
shoaib_ahmed 0:791a779d6220 416 /* Ensure round towards zero despite C's lack of consistency
shoaib_ahmed 0:791a779d6220 417 * about rounding negative values in integer division...
shoaib_ahmed 0:791a779d6220 418 */
shoaib_ahmed 0:791a779d6220 419 odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den);
shoaib_ahmed 0:791a779d6220 420 }
shoaib_ahmed 0:791a779d6220 421 }
shoaib_ahmed 0:791a779d6220 422 return odither;
shoaib_ahmed 0:791a779d6220 423 }
shoaib_ahmed 0:791a779d6220 424
shoaib_ahmed 0:791a779d6220 425
shoaib_ahmed 0:791a779d6220 426 /*
shoaib_ahmed 0:791a779d6220 427 * Create the ordered-dither tables.
shoaib_ahmed 0:791a779d6220 428 * Components having the same number of representative colors may
shoaib_ahmed 0:791a779d6220 429 * share a dither table.
shoaib_ahmed 0:791a779d6220 430 */
shoaib_ahmed 0:791a779d6220 431
shoaib_ahmed 0:791a779d6220 432 LOCAL(void)
shoaib_ahmed 0:791a779d6220 433 create_odither_tables (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 434 {
shoaib_ahmed 0:791a779d6220 435 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 436 ODITHER_MATRIX_PTR odither;
shoaib_ahmed 0:791a779d6220 437 int i, j, nci;
shoaib_ahmed 0:791a779d6220 438
shoaib_ahmed 0:791a779d6220 439 for (i = 0; i < cinfo->out_color_components; i++) {
shoaib_ahmed 0:791a779d6220 440 nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
shoaib_ahmed 0:791a779d6220 441 odither = NULL; /* search for matching prior component */
shoaib_ahmed 0:791a779d6220 442 for (j = 0; j < i; j++) {
shoaib_ahmed 0:791a779d6220 443 if (nci == cquantize->Ncolors[j]) {
shoaib_ahmed 0:791a779d6220 444 odither = cquantize->odither[j];
shoaib_ahmed 0:791a779d6220 445 break;
shoaib_ahmed 0:791a779d6220 446 }
shoaib_ahmed 0:791a779d6220 447 }
shoaib_ahmed 0:791a779d6220 448 if (odither == NULL) /* need a new table? */
shoaib_ahmed 0:791a779d6220 449 odither = make_odither_array(cinfo, nci);
shoaib_ahmed 0:791a779d6220 450 cquantize->odither[i] = odither;
shoaib_ahmed 0:791a779d6220 451 }
shoaib_ahmed 0:791a779d6220 452 }
shoaib_ahmed 0:791a779d6220 453
shoaib_ahmed 0:791a779d6220 454
shoaib_ahmed 0:791a779d6220 455 /*
shoaib_ahmed 0:791a779d6220 456 * Map some rows of pixels to the output colormapped representation.
shoaib_ahmed 0:791a779d6220 457 */
shoaib_ahmed 0:791a779d6220 458
shoaib_ahmed 0:791a779d6220 459 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 460 color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
shoaib_ahmed 0:791a779d6220 461 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 462 /* General case, no dithering */
shoaib_ahmed 0:791a779d6220 463 {
shoaib_ahmed 0:791a779d6220 464 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 465 JSAMPARRAY colorindex = cquantize->colorindex;
shoaib_ahmed 0:791a779d6220 466 register int pixcode, ci;
shoaib_ahmed 0:791a779d6220 467 register JSAMPROW ptrin, ptrout;
shoaib_ahmed 0:791a779d6220 468 int row;
shoaib_ahmed 0:791a779d6220 469 JDIMENSION col;
shoaib_ahmed 0:791a779d6220 470 JDIMENSION width = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 471 register int nc = cinfo->out_color_components;
shoaib_ahmed 0:791a779d6220 472
shoaib_ahmed 0:791a779d6220 473 for (row = 0; row < num_rows; row++) {
shoaib_ahmed 0:791a779d6220 474 ptrin = input_buf[row];
shoaib_ahmed 0:791a779d6220 475 ptrout = output_buf[row];
shoaib_ahmed 0:791a779d6220 476 for (col = width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 477 pixcode = 0;
shoaib_ahmed 0:791a779d6220 478 for (ci = 0; ci < nc; ci++) {
shoaib_ahmed 0:791a779d6220 479 pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
shoaib_ahmed 0:791a779d6220 480 }
shoaib_ahmed 0:791a779d6220 481 *ptrout++ = (JSAMPLE) pixcode;
shoaib_ahmed 0:791a779d6220 482 }
shoaib_ahmed 0:791a779d6220 483 }
shoaib_ahmed 0:791a779d6220 484 }
shoaib_ahmed 0:791a779d6220 485
shoaib_ahmed 0:791a779d6220 486
shoaib_ahmed 0:791a779d6220 487 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 488 color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
shoaib_ahmed 0:791a779d6220 489 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 490 /* Fast path for out_color_components==3, no dithering */
shoaib_ahmed 0:791a779d6220 491 {
shoaib_ahmed 0:791a779d6220 492 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 493 register int pixcode;
shoaib_ahmed 0:791a779d6220 494 register JSAMPROW ptrin, ptrout;
shoaib_ahmed 0:791a779d6220 495 JSAMPROW colorindex0 = cquantize->colorindex[0];
shoaib_ahmed 0:791a779d6220 496 JSAMPROW colorindex1 = cquantize->colorindex[1];
shoaib_ahmed 0:791a779d6220 497 JSAMPROW colorindex2 = cquantize->colorindex[2];
shoaib_ahmed 0:791a779d6220 498 int row;
shoaib_ahmed 0:791a779d6220 499 JDIMENSION col;
shoaib_ahmed 0:791a779d6220 500 JDIMENSION width = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 501
shoaib_ahmed 0:791a779d6220 502 for (row = 0; row < num_rows; row++) {
shoaib_ahmed 0:791a779d6220 503 ptrin = input_buf[row];
shoaib_ahmed 0:791a779d6220 504 ptrout = output_buf[row];
shoaib_ahmed 0:791a779d6220 505 for (col = width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 506 pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
shoaib_ahmed 0:791a779d6220 507 pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
shoaib_ahmed 0:791a779d6220 508 pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
shoaib_ahmed 0:791a779d6220 509 *ptrout++ = (JSAMPLE) pixcode;
shoaib_ahmed 0:791a779d6220 510 }
shoaib_ahmed 0:791a779d6220 511 }
shoaib_ahmed 0:791a779d6220 512 }
shoaib_ahmed 0:791a779d6220 513
shoaib_ahmed 0:791a779d6220 514
shoaib_ahmed 0:791a779d6220 515 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 516 quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
shoaib_ahmed 0:791a779d6220 517 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 518 /* General case, with ordered dithering */
shoaib_ahmed 0:791a779d6220 519 {
shoaib_ahmed 0:791a779d6220 520 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 521 register JSAMPROW input_ptr;
shoaib_ahmed 0:791a779d6220 522 register JSAMPROW output_ptr;
shoaib_ahmed 0:791a779d6220 523 JSAMPROW colorindex_ci;
shoaib_ahmed 0:791a779d6220 524 int * dither; /* points to active row of dither matrix */
shoaib_ahmed 0:791a779d6220 525 int row_index, col_index; /* current indexes into dither matrix */
shoaib_ahmed 0:791a779d6220 526 int nc = cinfo->out_color_components;
shoaib_ahmed 0:791a779d6220 527 int ci;
shoaib_ahmed 0:791a779d6220 528 int row;
shoaib_ahmed 0:791a779d6220 529 JDIMENSION col;
shoaib_ahmed 0:791a779d6220 530 JDIMENSION width = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 531
shoaib_ahmed 0:791a779d6220 532 for (row = 0; row < num_rows; row++) {
shoaib_ahmed 0:791a779d6220 533 /* Initialize output values to 0 so can process components separately */
shoaib_ahmed 0:791a779d6220 534 FMEMZERO((void FAR *) output_buf[row],
shoaib_ahmed 0:791a779d6220 535 (size_t) (width * SIZEOF(JSAMPLE)));
shoaib_ahmed 0:791a779d6220 536 row_index = cquantize->row_index;
shoaib_ahmed 0:791a779d6220 537 for (ci = 0; ci < nc; ci++) {
shoaib_ahmed 0:791a779d6220 538 input_ptr = input_buf[row] + ci;
shoaib_ahmed 0:791a779d6220 539 output_ptr = output_buf[row];
shoaib_ahmed 0:791a779d6220 540 colorindex_ci = cquantize->colorindex[ci];
shoaib_ahmed 0:791a779d6220 541 dither = cquantize->odither[ci][row_index];
shoaib_ahmed 0:791a779d6220 542 col_index = 0;
shoaib_ahmed 0:791a779d6220 543
shoaib_ahmed 0:791a779d6220 544 for (col = width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 545 /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
shoaib_ahmed 0:791a779d6220 546 * select output value, accumulate into output code for this pixel.
shoaib_ahmed 0:791a779d6220 547 * Range-limiting need not be done explicitly, as we have extended
shoaib_ahmed 0:791a779d6220 548 * the colorindex table to produce the right answers for out-of-range
shoaib_ahmed 0:791a779d6220 549 * inputs. The maximum dither is +- MAXJSAMPLE; this sets the
shoaib_ahmed 0:791a779d6220 550 * required amount of padding.
shoaib_ahmed 0:791a779d6220 551 */
shoaib_ahmed 0:791a779d6220 552 *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
shoaib_ahmed 0:791a779d6220 553 input_ptr += nc;
shoaib_ahmed 0:791a779d6220 554 output_ptr++;
shoaib_ahmed 0:791a779d6220 555 col_index = (col_index + 1) & ODITHER_MASK;
shoaib_ahmed 0:791a779d6220 556 }
shoaib_ahmed 0:791a779d6220 557 }
shoaib_ahmed 0:791a779d6220 558 /* Advance row index for next row */
shoaib_ahmed 0:791a779d6220 559 row_index = (row_index + 1) & ODITHER_MASK;
shoaib_ahmed 0:791a779d6220 560 cquantize->row_index = row_index;
shoaib_ahmed 0:791a779d6220 561 }
shoaib_ahmed 0:791a779d6220 562 }
shoaib_ahmed 0:791a779d6220 563
shoaib_ahmed 0:791a779d6220 564
shoaib_ahmed 0:791a779d6220 565 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 566 quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
shoaib_ahmed 0:791a779d6220 567 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 568 /* Fast path for out_color_components==3, with ordered dithering */
shoaib_ahmed 0:791a779d6220 569 {
shoaib_ahmed 0:791a779d6220 570 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 571 register int pixcode;
shoaib_ahmed 0:791a779d6220 572 register JSAMPROW input_ptr;
shoaib_ahmed 0:791a779d6220 573 register JSAMPROW output_ptr;
shoaib_ahmed 0:791a779d6220 574 JSAMPROW colorindex0 = cquantize->colorindex[0];
shoaib_ahmed 0:791a779d6220 575 JSAMPROW colorindex1 = cquantize->colorindex[1];
shoaib_ahmed 0:791a779d6220 576 JSAMPROW colorindex2 = cquantize->colorindex[2];
shoaib_ahmed 0:791a779d6220 577 int * dither0; /* points to active row of dither matrix */
shoaib_ahmed 0:791a779d6220 578 int * dither1;
shoaib_ahmed 0:791a779d6220 579 int * dither2;
shoaib_ahmed 0:791a779d6220 580 int row_index, col_index; /* current indexes into dither matrix */
shoaib_ahmed 0:791a779d6220 581 int row;
shoaib_ahmed 0:791a779d6220 582 JDIMENSION col;
shoaib_ahmed 0:791a779d6220 583 JDIMENSION width = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 584
shoaib_ahmed 0:791a779d6220 585 for (row = 0; row < num_rows; row++) {
shoaib_ahmed 0:791a779d6220 586 row_index = cquantize->row_index;
shoaib_ahmed 0:791a779d6220 587 input_ptr = input_buf[row];
shoaib_ahmed 0:791a779d6220 588 output_ptr = output_buf[row];
shoaib_ahmed 0:791a779d6220 589 dither0 = cquantize->odither[0][row_index];
shoaib_ahmed 0:791a779d6220 590 dither1 = cquantize->odither[1][row_index];
shoaib_ahmed 0:791a779d6220 591 dither2 = cquantize->odither[2][row_index];
shoaib_ahmed 0:791a779d6220 592 col_index = 0;
shoaib_ahmed 0:791a779d6220 593
shoaib_ahmed 0:791a779d6220 594 for (col = width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 595 pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
shoaib_ahmed 0:791a779d6220 596 dither0[col_index]]);
shoaib_ahmed 0:791a779d6220 597 pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
shoaib_ahmed 0:791a779d6220 598 dither1[col_index]]);
shoaib_ahmed 0:791a779d6220 599 pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
shoaib_ahmed 0:791a779d6220 600 dither2[col_index]]);
shoaib_ahmed 0:791a779d6220 601 *output_ptr++ = (JSAMPLE) pixcode;
shoaib_ahmed 0:791a779d6220 602 col_index = (col_index + 1) & ODITHER_MASK;
shoaib_ahmed 0:791a779d6220 603 }
shoaib_ahmed 0:791a779d6220 604 row_index = (row_index + 1) & ODITHER_MASK;
shoaib_ahmed 0:791a779d6220 605 cquantize->row_index = row_index;
shoaib_ahmed 0:791a779d6220 606 }
shoaib_ahmed 0:791a779d6220 607 }
shoaib_ahmed 0:791a779d6220 608
shoaib_ahmed 0:791a779d6220 609
shoaib_ahmed 0:791a779d6220 610 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 611 quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
shoaib_ahmed 0:791a779d6220 612 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 613 /* General case, with Floyd-Steinberg dithering */
shoaib_ahmed 0:791a779d6220 614 {
shoaib_ahmed 0:791a779d6220 615 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 616 register LOCFSERROR cur; /* current error or pixel value */
shoaib_ahmed 0:791a779d6220 617 LOCFSERROR belowerr; /* error for pixel below cur */
shoaib_ahmed 0:791a779d6220 618 LOCFSERROR bpreverr; /* error for below/prev col */
shoaib_ahmed 0:791a779d6220 619 LOCFSERROR bnexterr; /* error for below/next col */
shoaib_ahmed 0:791a779d6220 620 LOCFSERROR delta;
shoaib_ahmed 0:791a779d6220 621 register FSERRPTR errorptr; /* => fserrors[] at column before current */
shoaib_ahmed 0:791a779d6220 622 register JSAMPROW input_ptr;
shoaib_ahmed 0:791a779d6220 623 register JSAMPROW output_ptr;
shoaib_ahmed 0:791a779d6220 624 JSAMPROW colorindex_ci;
shoaib_ahmed 0:791a779d6220 625 JSAMPROW colormap_ci;
shoaib_ahmed 0:791a779d6220 626 int pixcode;
shoaib_ahmed 0:791a779d6220 627 int nc = cinfo->out_color_components;
shoaib_ahmed 0:791a779d6220 628 int dir; /* 1 for left-to-right, -1 for right-to-left */
shoaib_ahmed 0:791a779d6220 629 int dirnc; /* dir * nc */
shoaib_ahmed 0:791a779d6220 630 int ci;
shoaib_ahmed 0:791a779d6220 631 int row;
shoaib_ahmed 0:791a779d6220 632 JDIMENSION col;
shoaib_ahmed 0:791a779d6220 633 JDIMENSION width = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 634 JSAMPLE *range_limit = cinfo->sample_range_limit;
shoaib_ahmed 0:791a779d6220 635 SHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 636
shoaib_ahmed 0:791a779d6220 637 for (row = 0; row < num_rows; row++) {
shoaib_ahmed 0:791a779d6220 638 /* Initialize output values to 0 so can process components separately */
shoaib_ahmed 0:791a779d6220 639 FMEMZERO((void FAR *) output_buf[row],
shoaib_ahmed 0:791a779d6220 640 (size_t) (width * SIZEOF(JSAMPLE)));
shoaib_ahmed 0:791a779d6220 641 for (ci = 0; ci < nc; ci++) {
shoaib_ahmed 0:791a779d6220 642 input_ptr = input_buf[row] + ci;
shoaib_ahmed 0:791a779d6220 643 output_ptr = output_buf[row];
shoaib_ahmed 0:791a779d6220 644 if (cquantize->on_odd_row) {
shoaib_ahmed 0:791a779d6220 645 /* work right to left in this row */
shoaib_ahmed 0:791a779d6220 646 input_ptr += (width-1) * nc; /* so point to rightmost pixel */
shoaib_ahmed 0:791a779d6220 647 output_ptr += width-1;
shoaib_ahmed 0:791a779d6220 648 dir = -1;
shoaib_ahmed 0:791a779d6220 649 dirnc = -nc;
shoaib_ahmed 0:791a779d6220 650 errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
shoaib_ahmed 0:791a779d6220 651 } else {
shoaib_ahmed 0:791a779d6220 652 /* work left to right in this row */
shoaib_ahmed 0:791a779d6220 653 dir = 1;
shoaib_ahmed 0:791a779d6220 654 dirnc = nc;
shoaib_ahmed 0:791a779d6220 655 errorptr = cquantize->fserrors[ci]; /* => entry before first column */
shoaib_ahmed 0:791a779d6220 656 }
shoaib_ahmed 0:791a779d6220 657 colorindex_ci = cquantize->colorindex[ci];
shoaib_ahmed 0:791a779d6220 658 colormap_ci = cquantize->sv_colormap[ci];
shoaib_ahmed 0:791a779d6220 659 /* Preset error values: no error propagated to first pixel from left */
shoaib_ahmed 0:791a779d6220 660 cur = 0;
shoaib_ahmed 0:791a779d6220 661 /* and no error propagated to row below yet */
shoaib_ahmed 0:791a779d6220 662 belowerr = bpreverr = 0;
shoaib_ahmed 0:791a779d6220 663
shoaib_ahmed 0:791a779d6220 664 for (col = width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 665 /* cur holds the error propagated from the previous pixel on the
shoaib_ahmed 0:791a779d6220 666 * current line. Add the error propagated from the previous line
shoaib_ahmed 0:791a779d6220 667 * to form the complete error correction term for this pixel, and
shoaib_ahmed 0:791a779d6220 668 * round the error term (which is expressed * 16) to an integer.
shoaib_ahmed 0:791a779d6220 669 * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
shoaib_ahmed 0:791a779d6220 670 * for either sign of the error value.
shoaib_ahmed 0:791a779d6220 671 * Note: errorptr points to *previous* column's array entry.
shoaib_ahmed 0:791a779d6220 672 */
shoaib_ahmed 0:791a779d6220 673 cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
shoaib_ahmed 0:791a779d6220 674 /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
shoaib_ahmed 0:791a779d6220 675 * The maximum error is +- MAXJSAMPLE; this sets the required size
shoaib_ahmed 0:791a779d6220 676 * of the range_limit array.
shoaib_ahmed 0:791a779d6220 677 */
shoaib_ahmed 0:791a779d6220 678 cur += GETJSAMPLE(*input_ptr);
shoaib_ahmed 0:791a779d6220 679 cur = GETJSAMPLE(range_limit[cur]);
shoaib_ahmed 0:791a779d6220 680 /* Select output value, accumulate into output code for this pixel */
shoaib_ahmed 0:791a779d6220 681 pixcode = GETJSAMPLE(colorindex_ci[cur]);
shoaib_ahmed 0:791a779d6220 682 *output_ptr += (JSAMPLE) pixcode;
shoaib_ahmed 0:791a779d6220 683 /* Compute actual representation error at this pixel */
shoaib_ahmed 0:791a779d6220 684 /* Note: we can do this even though we don't have the final */
shoaib_ahmed 0:791a779d6220 685 /* pixel code, because the colormap is orthogonal. */
shoaib_ahmed 0:791a779d6220 686 cur -= GETJSAMPLE(colormap_ci[pixcode]);
shoaib_ahmed 0:791a779d6220 687 /* Compute error fractions to be propagated to adjacent pixels.
shoaib_ahmed 0:791a779d6220 688 * Add these into the running sums, and simultaneously shift the
shoaib_ahmed 0:791a779d6220 689 * next-line error sums left by 1 column.
shoaib_ahmed 0:791a779d6220 690 */
shoaib_ahmed 0:791a779d6220 691 bnexterr = cur;
shoaib_ahmed 0:791a779d6220 692 delta = cur * 2;
shoaib_ahmed 0:791a779d6220 693 cur += delta; /* form error * 3 */
shoaib_ahmed 0:791a779d6220 694 errorptr[0] = (FSERROR) (bpreverr + cur);
shoaib_ahmed 0:791a779d6220 695 cur += delta; /* form error * 5 */
shoaib_ahmed 0:791a779d6220 696 bpreverr = belowerr + cur;
shoaib_ahmed 0:791a779d6220 697 belowerr = bnexterr;
shoaib_ahmed 0:791a779d6220 698 cur += delta; /* form error * 7 */
shoaib_ahmed 0:791a779d6220 699 /* At this point cur contains the 7/16 error value to be propagated
shoaib_ahmed 0:791a779d6220 700 * to the next pixel on the current line, and all the errors for the
shoaib_ahmed 0:791a779d6220 701 * next line have been shifted over. We are therefore ready to move on.
shoaib_ahmed 0:791a779d6220 702 */
shoaib_ahmed 0:791a779d6220 703 input_ptr += dirnc; /* advance input ptr to next column */
shoaib_ahmed 0:791a779d6220 704 output_ptr += dir; /* advance output ptr to next column */
shoaib_ahmed 0:791a779d6220 705 errorptr += dir; /* advance errorptr to current column */
shoaib_ahmed 0:791a779d6220 706 }
shoaib_ahmed 0:791a779d6220 707 /* Post-loop cleanup: we must unload the final error value into the
shoaib_ahmed 0:791a779d6220 708 * final fserrors[] entry. Note we need not unload belowerr because
shoaib_ahmed 0:791a779d6220 709 * it is for the dummy column before or after the actual array.
shoaib_ahmed 0:791a779d6220 710 */
shoaib_ahmed 0:791a779d6220 711 errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
shoaib_ahmed 0:791a779d6220 712 }
shoaib_ahmed 0:791a779d6220 713 cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
shoaib_ahmed 0:791a779d6220 714 }
shoaib_ahmed 0:791a779d6220 715 }
shoaib_ahmed 0:791a779d6220 716
shoaib_ahmed 0:791a779d6220 717
shoaib_ahmed 0:791a779d6220 718 /*
shoaib_ahmed 0:791a779d6220 719 * Allocate workspace for Floyd-Steinberg errors.
shoaib_ahmed 0:791a779d6220 720 */
shoaib_ahmed 0:791a779d6220 721
shoaib_ahmed 0:791a779d6220 722 LOCAL(void)
shoaib_ahmed 0:791a779d6220 723 alloc_fs_workspace (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 724 {
shoaib_ahmed 0:791a779d6220 725 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 726 size_t arraysize;
shoaib_ahmed 0:791a779d6220 727 int i;
shoaib_ahmed 0:791a779d6220 728
shoaib_ahmed 0:791a779d6220 729 arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
shoaib_ahmed 0:791a779d6220 730 for (i = 0; i < cinfo->out_color_components; i++) {
shoaib_ahmed 0:791a779d6220 731 cquantize->fserrors[i] = (FSERRPTR)
shoaib_ahmed 0:791a779d6220 732 (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
shoaib_ahmed 0:791a779d6220 733 }
shoaib_ahmed 0:791a779d6220 734 }
shoaib_ahmed 0:791a779d6220 735
shoaib_ahmed 0:791a779d6220 736
shoaib_ahmed 0:791a779d6220 737 /*
shoaib_ahmed 0:791a779d6220 738 * Initialize for one-pass color quantization.
shoaib_ahmed 0:791a779d6220 739 */
shoaib_ahmed 0:791a779d6220 740
shoaib_ahmed 0:791a779d6220 741 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 742 start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
shoaib_ahmed 0:791a779d6220 743 {
shoaib_ahmed 0:791a779d6220 744 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
shoaib_ahmed 0:791a779d6220 745 size_t arraysize;
shoaib_ahmed 0:791a779d6220 746 int i;
shoaib_ahmed 0:791a779d6220 747
shoaib_ahmed 0:791a779d6220 748 /* Install my colormap. */
shoaib_ahmed 0:791a779d6220 749 cinfo->colormap = cquantize->sv_colormap;
shoaib_ahmed 0:791a779d6220 750 cinfo->actual_number_of_colors = cquantize->sv_actual;
shoaib_ahmed 0:791a779d6220 751
shoaib_ahmed 0:791a779d6220 752 /* Initialize for desired dithering mode. */
shoaib_ahmed 0:791a779d6220 753 switch (cinfo->dither_mode) {
shoaib_ahmed 0:791a779d6220 754 case JDITHER_NONE:
shoaib_ahmed 0:791a779d6220 755 if (cinfo->out_color_components == 3)
shoaib_ahmed 0:791a779d6220 756 cquantize->pub.color_quantize = color_quantize3;
shoaib_ahmed 0:791a779d6220 757 else
shoaib_ahmed 0:791a779d6220 758 cquantize->pub.color_quantize = color_quantize;
shoaib_ahmed 0:791a779d6220 759 break;
shoaib_ahmed 0:791a779d6220 760 case JDITHER_ORDERED:
shoaib_ahmed 0:791a779d6220 761 if (cinfo->out_color_components == 3)
shoaib_ahmed 0:791a779d6220 762 cquantize->pub.color_quantize = quantize3_ord_dither;
shoaib_ahmed 0:791a779d6220 763 else
shoaib_ahmed 0:791a779d6220 764 cquantize->pub.color_quantize = quantize_ord_dither;
shoaib_ahmed 0:791a779d6220 765 cquantize->row_index = 0; /* initialize state for ordered dither */
shoaib_ahmed 0:791a779d6220 766 /* If user changed to ordered dither from another mode,
shoaib_ahmed 0:791a779d6220 767 * we must recreate the color index table with padding.
shoaib_ahmed 0:791a779d6220 768 * This will cost extra space, but probably isn't very likely.
shoaib_ahmed 0:791a779d6220 769 */
shoaib_ahmed 0:791a779d6220 770 if (! cquantize->is_padded)
shoaib_ahmed 0:791a779d6220 771 create_colorindex(cinfo);
shoaib_ahmed 0:791a779d6220 772 /* Create ordered-dither tables if we didn't already. */
shoaib_ahmed 0:791a779d6220 773 if (cquantize->odither[0] == NULL)
shoaib_ahmed 0:791a779d6220 774 create_odither_tables(cinfo);
shoaib_ahmed 0:791a779d6220 775 break;
shoaib_ahmed 0:791a779d6220 776 case JDITHER_FS:
shoaib_ahmed 0:791a779d6220 777 cquantize->pub.color_quantize = quantize_fs_dither;
shoaib_ahmed 0:791a779d6220 778 cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
shoaib_ahmed 0:791a779d6220 779 /* Allocate Floyd-Steinberg workspace if didn't already. */
shoaib_ahmed 0:791a779d6220 780 if (cquantize->fserrors[0] == NULL)
shoaib_ahmed 0:791a779d6220 781 alloc_fs_workspace(cinfo);
shoaib_ahmed 0:791a779d6220 782 /* Initialize the propagated errors to zero. */
shoaib_ahmed 0:791a779d6220 783 arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
shoaib_ahmed 0:791a779d6220 784 for (i = 0; i < cinfo->out_color_components; i++)
shoaib_ahmed 0:791a779d6220 785 FMEMZERO((void FAR *) cquantize->fserrors[i], arraysize);
shoaib_ahmed 0:791a779d6220 786 break;
shoaib_ahmed 0:791a779d6220 787 default:
shoaib_ahmed 0:791a779d6220 788 ERREXIT(cinfo, JERR_NOT_COMPILED);
shoaib_ahmed 0:791a779d6220 789 break;
shoaib_ahmed 0:791a779d6220 790 }
shoaib_ahmed 0:791a779d6220 791 }
shoaib_ahmed 0:791a779d6220 792
shoaib_ahmed 0:791a779d6220 793
shoaib_ahmed 0:791a779d6220 794 /*
shoaib_ahmed 0:791a779d6220 795 * Finish up at the end of the pass.
shoaib_ahmed 0:791a779d6220 796 */
shoaib_ahmed 0:791a779d6220 797
shoaib_ahmed 0:791a779d6220 798 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 799 finish_pass_1_quant (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 800 {
shoaib_ahmed 0:791a779d6220 801 /* no work in 1-pass case */
shoaib_ahmed 0:791a779d6220 802 }
shoaib_ahmed 0:791a779d6220 803
shoaib_ahmed 0:791a779d6220 804
shoaib_ahmed 0:791a779d6220 805 /*
shoaib_ahmed 0:791a779d6220 806 * Switch to a new external colormap between output passes.
shoaib_ahmed 0:791a779d6220 807 * Shouldn't get to this module!
shoaib_ahmed 0:791a779d6220 808 */
shoaib_ahmed 0:791a779d6220 809
shoaib_ahmed 0:791a779d6220 810 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 811 new_color_map_1_quant (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 812 {
shoaib_ahmed 0:791a779d6220 813 ERREXIT(cinfo, JERR_MODE_CHANGE);
shoaib_ahmed 0:791a779d6220 814 }
shoaib_ahmed 0:791a779d6220 815
shoaib_ahmed 0:791a779d6220 816
shoaib_ahmed 0:791a779d6220 817 /*
shoaib_ahmed 0:791a779d6220 818 * Module initialization routine for 1-pass color quantization.
shoaib_ahmed 0:791a779d6220 819 */
shoaib_ahmed 0:791a779d6220 820
shoaib_ahmed 0:791a779d6220 821 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 822 jinit_1pass_quantizer (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 823 {
shoaib_ahmed 0:791a779d6220 824 my_cquantize_ptr cquantize;
shoaib_ahmed 0:791a779d6220 825
shoaib_ahmed 0:791a779d6220 826 cquantize = (my_cquantize_ptr)
shoaib_ahmed 0:791a779d6220 827 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 828 SIZEOF(my_cquantizer));
shoaib_ahmed 0:791a779d6220 829 cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
shoaib_ahmed 0:791a779d6220 830 cquantize->pub.start_pass = start_pass_1_quant;
shoaib_ahmed 0:791a779d6220 831 cquantize->pub.finish_pass = finish_pass_1_quant;
shoaib_ahmed 0:791a779d6220 832 cquantize->pub.new_color_map = new_color_map_1_quant;
shoaib_ahmed 0:791a779d6220 833 cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
shoaib_ahmed 0:791a779d6220 834 cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */
shoaib_ahmed 0:791a779d6220 835
shoaib_ahmed 0:791a779d6220 836 /* Make sure my internal arrays won't overflow */
shoaib_ahmed 0:791a779d6220 837 if (cinfo->out_color_components > MAX_Q_COMPS)
shoaib_ahmed 0:791a779d6220 838 ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
shoaib_ahmed 0:791a779d6220 839 /* Make sure colormap indexes can be represented by JSAMPLEs */
shoaib_ahmed 0:791a779d6220 840 if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
shoaib_ahmed 0:791a779d6220 841 ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1);
shoaib_ahmed 0:791a779d6220 842
shoaib_ahmed 0:791a779d6220 843 /* Create the colormap and color index table. */
shoaib_ahmed 0:791a779d6220 844 create_colormap(cinfo);
shoaib_ahmed 0:791a779d6220 845 create_colorindex(cinfo);
shoaib_ahmed 0:791a779d6220 846
shoaib_ahmed 0:791a779d6220 847 /* Allocate Floyd-Steinberg workspace now if requested.
shoaib_ahmed 0:791a779d6220 848 * We do this now since it is FAR storage and may affect the memory
shoaib_ahmed 0:791a779d6220 849 * manager's space calculations. If the user changes to FS dither
shoaib_ahmed 0:791a779d6220 850 * mode in a later pass, we will allocate the space then, and will
shoaib_ahmed 0:791a779d6220 851 * possibly overrun the max_memory_to_use setting.
shoaib_ahmed 0:791a779d6220 852 */
shoaib_ahmed 0:791a779d6220 853 if (cinfo->dither_mode == JDITHER_FS)
shoaib_ahmed 0:791a779d6220 854 alloc_fs_workspace(cinfo);
shoaib_ahmed 0:791a779d6220 855 }
shoaib_ahmed 0:791a779d6220 856
shoaib_ahmed 0:791a779d6220 857 #endif /* QUANT_1PASS_SUPPORTED */