12cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* 22cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * jquant1.c 32cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * 4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC * This file was part of the Independent JPEG Group's software: 5489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane * Copyright (C) 1991-1996, Thomas G. Lane. 6a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications: 76eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * Copyright (C) 2009, 2015, D. R. Commander. 86eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * For conditions of distribution and use, see the accompanying README.ijg 96eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * file. 102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * 112cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * This file contains 1-pass color quantization (color mapping) routines. 1236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * These routines provide mapping to a fixed color map using equally spaced 1336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * color values. Optional Floyd-Steinberg or ordered dithering is available. 142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 1636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define JPEG_INTERNALS 172cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "jinclude.h" 1836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h" 192cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 202cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#ifdef QUANT_1PASS_SUPPORTED 212cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 222cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* 244a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * The main purpose of 1-pass quantization is to provide a fast, if not very 254a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * high quality, colormapped output capability. A 2-pass quantizer usually 264a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * gives better visual quality; however, for quantized grayscale output this 274a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * quantizer is perfectly adequate. Dithering is highly recommended with this 284a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * quantizer, though you can turn it off if you really want to. 292cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * 304a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * In 1-pass quantization the colormap must be chosen in advance of seeing the 314a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * image. We use a map consisting of all combinations of Ncolors[i] color 324a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * values for the i'th component. The Ncolors[] values are chosen so that 334a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * their product, the total number of colors, is no more than that requested. 344a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * (In most cases, the product will be somewhat less.) 354a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * 364a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Since the colormap is orthogonal, the representative value for each color 374a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * component can be determined without considering the other components; 384a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * then these indexes can be combined into a colormap index by a standard 394a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * N-dimensional-array-subscript calculation. Most of the arithmetic involved 404a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * can be precalculated and stored in the lookup table colorindex[]. 414a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * colorindex[i][j] maps pixel value j in component i to the nearest 424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * representative value (grid plane) for that component; this index is 434a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * multiplied by the array stride for component i, so that the 444a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * index of the colormap entry closest to a given pixel value is just 454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * sum( colorindex[component-number][pixel-component-value] ) 464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Aside from being fast, this scheme allows for variable spacing between 474a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * representative values with no additional lookup cost. 4836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 4936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * If gamma correction has been applied in color conversion, it might be wise 5036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * to adjust the color grid spacing so that the representative colors are 5136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * equidistant in linear space. At this writing, gamma correction is not 5236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * implemented by jdcolor, so nothing is done here. 532cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 542cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 552cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 5636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Declarations for ordered dithering. 5736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 589ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane * We use a standard 16x16 ordered dither array. The basic concept of ordered 5936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * dithering is described in many references, for instance Dale Schumacher's 6036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991). 6136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * In place of Schumacher's comparisons against a "threshold" value, we add a 6236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * "dither" value to the input pixel and then round the result to the nearest 6336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * output value. The dither value is equivalent to (0.5 - threshold) times 6436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the distance between output values. For ordered dithering, we assume that 6536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the output colors are equally spaced; if not, results will probably be 6636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * worse, since the dither may be too much or too little at a given point. 6736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 6836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * The normal calculation would be to form pixel value + dither, range-limit 6936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual. 7036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * We can skip the separate range-limiting step by extending the colorindex 7136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * table in both directions. 724a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */ 734a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 74e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define ODITHER_SIZE 16 /* dimension of dither matrix */ 759ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */ 76e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE) /* # cells in matrix */ 779ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane#define ODITHER_MASK (ODITHER_SIZE-1) /* mask for wrapping around counters */ 7836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 7936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE]; 809ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lanetypedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE]; 819ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane 829ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lanestatic const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = { 83a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane /* Bayer's order-4 dither array. Generated by the code given in 84a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I. 85a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane * The values in this array must range from 0 to ODITHER_CELLS-1. 86a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane */ 87a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 }, 88a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 }, 89a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 }, 90a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 }, 91a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 }, 92a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 }, 93a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 }, 94a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 }, 95a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 }, 96a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 }, 97a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 }, 98a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 }, 99a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 }, 100a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 }, 101a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 }, 102a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 } 1039ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane}; 1042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 1052cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 1062cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* Declarations for Floyd-Steinberg dithering. 1074a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * 108cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * Errors are accumulated into the array fserrors[], at a resolution of 109cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * 1/16th of a pixel count. The error at a given pixel is propagated 110cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * to its not-yet-processed neighbors using the standard F-S fractions, 111e5eaf37440b8e337ab150c017df7c03faf846c51DRC * ... (here) 7/16 112e5eaf37440b8e337ab150c017df7c03faf846c51DRC * 3/16 5/16 1/16 1132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * We work left-to-right on even rows, right-to-left on odd rows. 1142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * 115cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * We can get away with a single array (holding one row's worth of errors) 116cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * by using it to store the current row's errors at pixel columns not yet 117cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * processed, but the next row's errors at columns already processed. We 118cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * need only a few extra variables to hold the errors immediately around the 119cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * current column. (If we are lucky, those variables are in registers, but 120cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * even if not, they're probably cheaper to access than array elements are.) 121cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * 122cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * The fserrors[] array is indexed [component#][position]. 1234a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * We provide (#columns + 2) entries per component; the extra entry at each 1244a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * end saves us from special-casing the first and last pixels. 1252cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 1262cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 12736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#if BITS_IN_JSAMPLE == 8 128e5eaf37440b8e337ab150c017df7c03faf846c51DRCtypedef INT16 FSERROR; /* 16 bits should be enough */ 129e5eaf37440b8e337ab150c017df7c03faf846c51DRCtypedef int LOCFSERROR; /* use 'int' for calculation temps */ 1302cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#else 1316eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidistypedef JLONG FSERROR; /* may need more than 16 bits */ 1326eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidistypedef JLONG LOCFSERROR; /* be sure calculation temps are big enough */ 1332cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#endif 1342cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 1355033f3e19a31e8ad40c1a79700365aefe5664494DRCtypedef FSERROR *FSERRPTR; /* pointer to error array */ 1362cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 13736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 13836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Private subobject */ 13936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 140e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define MAX_Q_COMPS 4 /* max components I can handle */ 14136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 14236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef struct { 14336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane struct jpeg_color_quantizer pub; /* public fields */ 14436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 145bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Initially allocated colormap is saved here */ 146e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */ 147e5eaf37440b8e337ab150c017df7c03faf846c51DRC int sv_actual; /* number of entries in use */ 148bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 149e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY colorindex; /* Precomputed mapping for speed */ 15036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* colorindex[i][j] = index of color closest to pixel value j in component i, 15136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * premultiplied as described above. Since colormap indexes must fit into 15236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * JSAMPLEs, the entries of this array will too. 15336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 154e5eaf37440b8e337ab150c017df7c03faf846c51DRC boolean is_padded; /* is the colorindex padded for odither? */ 155bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 156e5eaf37440b8e337ab150c017df7c03faf846c51DRC int Ncolors[MAX_Q_COMPS]; /* # of values alloced to each component */ 15736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 15836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Variables for ordered dithering */ 159e5eaf37440b8e337ab150c017df7c03faf846c51DRC int row_index; /* cur row's vertical index in dither matrix */ 1609ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */ 16136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 16236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Variables for Floyd-Steinberg dithering */ 16336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */ 164e5eaf37440b8e337ab150c017df7c03faf846c51DRC boolean on_odd_row; /* flag to remember which row we are on */ 16536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} my_cquantizer; 16636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 1676eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidistypedef my_cquantizer *my_cquantize_ptr; 1682cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 1692cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 1702cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* 171bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Policy-making subroutines for create_colormap and create_colorindex. 172bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * These routines determine the colormap to be used. The rest of the module 173bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * only assumes that the colormap is orthogonal. 1744a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * 1754a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * * select_ncolors decides how to divvy up the available colors 1764a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * among the components. 1774a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * * output_value defines the set of representative values for a component. 1784a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * * largest_input_value defines the mapping from input values to 1794a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * representative values for a component. 1804a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Note that the latter two routines may impose different policies for 1814a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * different components, though this is not currently done. 1824a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */ 1834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 1844a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 185489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(int) 18636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneselect_ncolors (j_decompress_ptr cinfo, int Ncolors[]) 1874a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Determine allocation of desired colors to components, */ 1884a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* and fill in Ncolors[] array to indicate choice. */ 1894a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Return value is total number of colors (product of Ncolors[] values). */ 1904a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{ 19136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane int nc = cinfo->out_color_components; /* number of color components */ 1924a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane int max_colors = cinfo->desired_number_of_colors; 19336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane int total_colors, iroot, i, j; 194a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane boolean changed; 1954a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane long temp; 196f25c071eb745268452206bb633561b770c4d62eaDRC int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE }; 197f25c071eb745268452206bb633561b770c4d62eaDRC RGB_order[0] = rgb_green[cinfo->out_color_space]; 198f25c071eb745268452206bb633561b770c4d62eaDRC RGB_order[1] = rgb_red[cinfo->out_color_space]; 199f25c071eb745268452206bb633561b770c4d62eaDRC RGB_order[2] = rgb_blue[cinfo->out_color_space]; 2004a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 2014a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* We can allocate at least the nc'th root of max_colors per component. */ 2024a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* Compute floor(nc'th root of max_colors). */ 2034a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane iroot = 1; 2044a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane do { 2054a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane iroot++; 206e5eaf37440b8e337ab150c017df7c03faf846c51DRC temp = iroot; /* set temp = iroot ** nc */ 2074a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane for (i = 1; i < nc; i++) 2084a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane temp *= iroot; 2094a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */ 210e5eaf37440b8e337ab150c017df7c03faf846c51DRC iroot--; /* now iroot = floor(root) */ 2114a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 2124a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* Must have at least 2 color values per component */ 2134a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane if (iroot < 2) 21436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp); 21536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 21636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Initialize to iroot color values for each component */ 21736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane total_colors = 1; 21836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (i = 0; i < nc; i++) { 21936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane Ncolors[i] = iroot; 22036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane total_colors *= iroot; 22136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 22236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* We may be able to increment the count for one or more components without 22336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * exceeding max_colors, though we know not all can be incremented. 224a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane * Sometimes, the first component can be incremented more than once! 225a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.) 22636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * In RGB colorspace, try to increment G first, then R, then B. 22736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */ 228a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane do { 229a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane changed = FALSE; 230a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane for (i = 0; i < nc; i++) { 231a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i); 232a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane /* calculate new total_colors if Ncolors[j] is incremented */ 233a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane temp = total_colors / Ncolors[j]; 234e5eaf37440b8e337ab150c017df7c03faf846c51DRC temp *= Ncolors[j]+1; /* done in long arith to avoid oflo */ 235a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane if (temp > (long) max_colors) 236e5eaf37440b8e337ab150c017df7c03faf846c51DRC break; /* won't fit, done with this pass */ 237e5eaf37440b8e337ab150c017df7c03faf846c51DRC Ncolors[j]++; /* OK, apply the increment */ 238a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane total_colors = (int) temp; 239a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane changed = TRUE; 240a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane } 241a8b67c4fbbfde9b4b4e03f2dea8f4f0b1900fc33Thomas G. Lane } while (changed); 2424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 2434a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane return total_colors; 2444a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane} 2454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 2464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 247489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(int) 24836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneoutput_value (j_decompress_ptr cinfo, int ci, int j, int maxj) 2494a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Return j'th output value, where j will range from 0 to maxj */ 2504a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* The output values must fall in 0..MAXJSAMPLE in increasing order */ 2514a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{ 2524a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* We always provide values 0 and MAXJSAMPLE for each component; 2534a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * any additional values are equally spaced between these limits. 2544a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * (Forcing the upper and lower values to the limits ensures that 2554a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * dithering can't produce a color outside the selected gamut.) 2564a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */ 2576eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis return (int) (((JLONG) j * MAXJSAMPLE + maxj/2) / maxj); 2584a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane} 2594a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 2604a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 261489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(int) 26236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanelargest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj) 2634a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Return largest input value that should map to j'th output value */ 2644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */ 2654a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{ 2664a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* Breakpoints are halfway between values returned by output_value */ 2676eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis return (int) (((JLONG) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj)); 2684a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane} 2694a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 2704a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 2714a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* 272bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Create the colormap. 2732cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 2742cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 275489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void) 27636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanecreate_colormap (j_decompress_ptr cinfo) 2772cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{ 27836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 279e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY colormap; /* Created colormap */ 280e5eaf37440b8e337ab150c017df7c03faf846c51DRC int total_colors; /* Number of distinct output colors */ 281bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane int i,j,k, nci, blksize, blkdist, ptr, val; 2822cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 2834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* Select number of colors for each component */ 284bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane total_colors = select_ncolors(cinfo, cquantize->Ncolors); 2852cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 2862cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane /* Report selected color counts */ 28736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (cinfo->out_color_components == 3) 28836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS, 289e5eaf37440b8e337ab150c017df7c03faf846c51DRC total_colors, cquantize->Ncolors[0], 290e5eaf37440b8e337ab150c017df7c03faf846c51DRC cquantize->Ncolors[1], cquantize->Ncolors[2]); 2912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane else 29236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors); 29336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 294bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Allocate and fill in the colormap. */ 2952cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane /* The colors are ordered in the map in standard row-major order, */ 2962cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane /* i.e. rightmost (highest-indexed) color changes most rapidly. */ 2972cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 29836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane colormap = (*cinfo->mem->alloc_sarray) 29936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ((j_common_ptr) cinfo, JPOOL_IMAGE, 30036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components); 3012cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 3022cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane /* blksize is number of adjacent repeated entries for a component */ 3032cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane /* blkdist is distance between groups of identical entries for a component */ 3042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane blkdist = total_colors; 3052cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 30636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (i = 0; i < cinfo->out_color_components; i++) { 3072cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane /* fill in colormap entries for i'th color component */ 308bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 3092cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane blksize = blkdist / nci; 3102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (j = 0; j < nci; j++) { 311bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane /* Compute j'th output value (out of nci) for component */ 3124a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane val = output_value(cinfo, i, j, nci-1); 313bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane /* Fill in all colormap entries that have this value of this component */ 3142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) { 315e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* fill in blksize entries beginning at ptr */ 316e5eaf37440b8e337ab150c017df7c03faf846c51DRC for (k = 0; k < blksize; k++) 317e5eaf37440b8e337ab150c017df7c03faf846c51DRC colormap[i][ptr+k] = (JSAMPLE) val; 3182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 3192cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 320e5eaf37440b8e337ab150c017df7c03faf846c51DRC blkdist = blksize; /* blksize of this color is blkdist of next */ 321bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 322bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 323bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Save the colormap in private storage, 324bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * where it will survive color quantization mode changes. 325bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 326bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->sv_colormap = colormap; 327bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->sv_actual = total_colors; 328bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} 329bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 330bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 331bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 332bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Create the color index table. 333bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 334bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 335489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void) 336bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanecreate_colorindex (j_decompress_ptr cinfo) 337bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{ 338bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 339bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane JSAMPROW indexptr; 340bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane int i,j,k, nci, blksize, val, pad; 341bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 342bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* For ordered dither, we pad the color index tables by MAXJSAMPLE in 343bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE). 344bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This is not necessary in the other dithering modes. However, we 345bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * flag whether it was done in case user changes dithering mode. 346bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 347bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->dither_mode == JDITHER_ORDERED) { 348bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane pad = MAXJSAMPLE*2; 349bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->is_padded = TRUE; 350bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } else { 351bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane pad = 0; 352bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->is_padded = FALSE; 353bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 354bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 355bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->colorindex = (*cinfo->mem->alloc_sarray) 356bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane ((j_common_ptr) cinfo, JPOOL_IMAGE, 357bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane (JDIMENSION) (MAXJSAMPLE+1 + pad), 358bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane (JDIMENSION) cinfo->out_color_components); 359bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 360bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* blksize is number of adjacent repeated entries for a component */ 361bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane blksize = cquantize->sv_actual; 362bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 363bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane for (i = 0; i < cinfo->out_color_components; i++) { 364bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* fill in colorindex entries for i'th color component */ 365bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 366bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane blksize = blksize / nci; 3672cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 36836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* adjust colorindex pointers to provide padding at negative indexes. */ 36936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (pad) 37036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cquantize->colorindex[i] += MAXJSAMPLE; 37136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 3724a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* in loop, val = index of current output value, */ 3734a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* and k = largest j that maps to current val */ 37436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane indexptr = cquantize->colorindex[i]; 3754a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane val = 0; 3764a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane k = largest_input_value(cinfo, i, 0, nci-1); 3772cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (j = 0; j <= MAXJSAMPLE; j++) { 378e5eaf37440b8e337ab150c017df7c03faf846c51DRC while (j > k) /* advance val if past boundary */ 379e5eaf37440b8e337ab150c017df7c03faf846c51DRC k = largest_input_value(cinfo, i, ++val, nci-1); 3802cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane /* premultiply so that no multiplication needed in main processing */ 38136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane indexptr[j] = (JSAMPLE) (val * blksize); 3822cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 38336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Pad at both ends if necessary */ 38436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (pad) 38536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (j = 1; j <= MAXJSAMPLE; j++) { 386e5eaf37440b8e337ab150c017df7c03faf846c51DRC indexptr[-j] = indexptr[0]; 387e5eaf37440b8e337ab150c017df7c03faf846c51DRC indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE]; 38836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 3892cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 390bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} 3912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 3924a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 393bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 394bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Create an ordered-dither array for a component having ncolors 395bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * distinct output values. 396bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 397bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 398489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(ODITHER_MATRIX_PTR) 399bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanemake_odither_array (j_decompress_ptr cinfo, int ncolors) 400bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{ 401bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane ODITHER_MATRIX_PTR odither; 402bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane int j,k; 4036eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis JLONG num,den; 404bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 405bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane odither = (ODITHER_MATRIX_PTR) 406bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 4075de454b291f48382648a5d1dc2aa0fca8b5786d4DRC sizeof(ODITHER_MATRIX)); 408bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1). 409bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Hence the dither value for the matrix cell with fill order f 410bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1). 411bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * On 16-bit-int machine, be careful to avoid overflow. 412bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 4136eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis den = 2 * ODITHER_CELLS * ((JLONG) (ncolors - 1)); 414bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane for (j = 0; j < ODITHER_SIZE; j++) { 415bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane for (k = 0; k < ODITHER_SIZE; k++) { 4166eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis num = ((JLONG) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k]))) 417e5eaf37440b8e337ab150c017df7c03faf846c51DRC * MAXJSAMPLE; 418bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Ensure round towards zero despite C's lack of consistency 419bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * about rounding negative values in integer division... 420bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 421bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den); 422bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 423bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 424bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane return odither; 425bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} 426bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 427bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 428bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 429bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Create the ordered-dither tables. 430e5eaf37440b8e337ab150c017df7c03faf846c51DRC * Components having the same number of representative colors may 431bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * share a dither table. 432bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 433bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 434489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void) 435bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanecreate_odither_tables (j_decompress_ptr cinfo) 436bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{ 437bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 438bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane ODITHER_MATRIX_PTR odither; 439bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane int i, j, nci; 440bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 441bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane for (i = 0; i < cinfo->out_color_components; i++) { 442bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ 443e5eaf37440b8e337ab150c017df7c03faf846c51DRC odither = NULL; /* search for matching prior component */ 444bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane for (j = 0; j < i; j++) { 445bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (nci == cquantize->Ncolors[j]) { 446e5eaf37440b8e337ab150c017df7c03faf846c51DRC odither = cquantize->odither[j]; 447e5eaf37440b8e337ab150c017df7c03faf846c51DRC break; 4489ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane } 4494a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane } 450e5eaf37440b8e337ab150c017df7c03faf846c51DRC if (odither == NULL) /* need a new table? */ 451bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane odither = make_odither_array(cinfo, nci); 452bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->odither[i] = odither; 4532cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 4544a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane} 4554a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 4564a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane 4574a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/* 4582cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Map some rows of pixels to the output colormapped representation. 4592cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 4602cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 461489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 46236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanecolor_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 463e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY output_buf, int num_rows) 4642cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* General case, no dithering */ 4652cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{ 46636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 46736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPARRAY colorindex = cquantize->colorindex; 4682cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane register int pixcode, ci; 46936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register JSAMPROW ptrin, ptrout; 4704a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane int row; 47136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION col; 47236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION width = cinfo->output_width; 47336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register int nc = cinfo->out_color_components; 4742cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 4752cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (row = 0; row < num_rows; row++) { 47636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ptrin = input_buf[row]; 47736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ptrout = output_buf[row]; 47836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (col = width; col > 0; col--) { 4792cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane pixcode = 0; 4802cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (ci = 0; ci < nc; ci++) { 481e5eaf37440b8e337ab150c017df7c03faf846c51DRC pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]); 4822cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 4834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *ptrout++ = (JSAMPLE) pixcode; 4842cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 4852cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 4862cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane} 4872cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 4882cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 489489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 49036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanecolor_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 491e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY output_buf, int num_rows) 49236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Fast path for out_color_components==3, no dithering */ 4932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{ 49436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 4952cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane register int pixcode; 49636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register JSAMPROW ptrin, ptrout; 49736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPROW colorindex0 = cquantize->colorindex[0]; 49836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPROW colorindex1 = cquantize->colorindex[1]; 49936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPROW colorindex2 = cquantize->colorindex[2]; 5004a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane int row; 50136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION col; 50236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION width = cinfo->output_width; 5032cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 5042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (row = 0; row < num_rows; row++) { 50536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ptrin = input_buf[row]; 50636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ptrout = output_buf[row]; 5072cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (col = width; col > 0; col--) { 50836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]); 50936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]); 51036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]); 511bd543f030e7e435c2c6a6a7d52ad927ae97cd927Thomas G. Lane *ptrout++ = (JSAMPLE) pixcode; 5122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 5132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 5142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane} 5152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 5162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 517489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 51836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanequantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 519e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY output_buf, int num_rows) 52036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* General case, with ordered dithering */ 52136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{ 52236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 52336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register JSAMPROW input_ptr; 52436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register JSAMPROW output_ptr; 52536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPROW colorindex_ci; 5266eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis int *dither; /* points to active row of dither matrix */ 527e5eaf37440b8e337ab150c017df7c03faf846c51DRC int row_index, col_index; /* current indexes into dither matrix */ 52836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane int nc = cinfo->out_color_components; 52936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane int ci; 53036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane int row; 53136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION col; 53236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION width = cinfo->output_width; 53336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 53436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (row = 0; row < num_rows; row++) { 53536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Initialize output values to 0 so can process components separately */ 5365de454b291f48382648a5d1dc2aa0fca8b5786d4DRC jzero_far((void *) output_buf[row], (size_t) (width * sizeof(JSAMPLE))); 53736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane row_index = cquantize->row_index; 53836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (ci = 0; ci < nc; ci++) { 53936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane input_ptr = input_buf[row] + ci; 54036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane output_ptr = output_buf[row]; 54136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane colorindex_ci = cquantize->colorindex[ci]; 54236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane dither = cquantize->odither[ci][row_index]; 54336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane col_index = 0; 54436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 54536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (col = width; col > 0; col--) { 546e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE, 547e5eaf37440b8e337ab150c017df7c03faf846c51DRC * select output value, accumulate into output code for this pixel. 548e5eaf37440b8e337ab150c017df7c03faf846c51DRC * Range-limiting need not be done explicitly, as we have extended 549e5eaf37440b8e337ab150c017df7c03faf846c51DRC * the colorindex table to produce the right answers for out-of-range 550e5eaf37440b8e337ab150c017df7c03faf846c51DRC * inputs. The maximum dither is +- MAXJSAMPLE; this sets the 551e5eaf37440b8e337ab150c017df7c03faf846c51DRC * required amount of padding. 552e5eaf37440b8e337ab150c017df7c03faf846c51DRC */ 553e5eaf37440b8e337ab150c017df7c03faf846c51DRC *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]]; 554e5eaf37440b8e337ab150c017df7c03faf846c51DRC input_ptr += nc; 555e5eaf37440b8e337ab150c017df7c03faf846c51DRC output_ptr++; 556e5eaf37440b8e337ab150c017df7c03faf846c51DRC col_index = (col_index + 1) & ODITHER_MASK; 55736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 55836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 55936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Advance row index for next row */ 56036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane row_index = (row_index + 1) & ODITHER_MASK; 56136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cquantize->row_index = row_index; 56236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 56336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} 56436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 56536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 566489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 56736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanequantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 568e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY output_buf, int num_rows) 56936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Fast path for out_color_components==3, with ordered dithering */ 57036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{ 57136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 57236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register int pixcode; 57336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register JSAMPROW input_ptr; 57436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane register JSAMPROW output_ptr; 57536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPROW colorindex0 = cquantize->colorindex[0]; 57636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPROW colorindex1 = cquantize->colorindex[1]; 57736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JSAMPROW colorindex2 = cquantize->colorindex[2]; 5786eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis int *dither0; /* points to active row of dither matrix */ 5796eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis int *dither1; 5806eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis int *dither2; 581e5eaf37440b8e337ab150c017df7c03faf846c51DRC int row_index, col_index; /* current indexes into dither matrix */ 58236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane int row; 58336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION col; 58436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION width = cinfo->output_width; 58536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 58636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (row = 0; row < num_rows; row++) { 58736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane row_index = cquantize->row_index; 58836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane input_ptr = input_buf[row]; 58936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane output_ptr = output_buf[row]; 59036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane dither0 = cquantize->odither[0][row_index]; 59136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane dither1 = cquantize->odither[1][row_index]; 59236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane dither2 = cquantize->odither[2][row_index]; 59336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane col_index = 0; 59436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 59536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (col = width; col > 0; col--) { 59636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) + 597e5eaf37440b8e337ab150c017df7c03faf846c51DRC dither0[col_index]]); 59836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) + 599e5eaf37440b8e337ab150c017df7c03faf846c51DRC dither1[col_index]]); 60036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) + 601e5eaf37440b8e337ab150c017df7c03faf846c51DRC dither2[col_index]]); 60236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *output_ptr++ = (JSAMPLE) pixcode; 60336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane col_index = (col_index + 1) & ODITHER_MASK; 60436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 60536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane row_index = (row_index + 1) & ODITHER_MASK; 60636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cquantize->row_index = row_index; 60736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane } 60836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} 60936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 61036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 611489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 61236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanequantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, 613e5eaf37440b8e337ab150c017df7c03faf846c51DRC JSAMPARRAY output_buf, int num_rows) 6142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* General case, with Floyd-Steinberg dithering */ 6152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{ 61636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 617e5eaf37440b8e337ab150c017df7c03faf846c51DRC register LOCFSERROR cur; /* current error or pixel value */ 618e5eaf37440b8e337ab150c017df7c03faf846c51DRC LOCFSERROR belowerr; /* error for pixel below cur */ 619e5eaf37440b8e337ab150c017df7c03faf846c51DRC LOCFSERROR bpreverr; /* error for below/prev col */ 620e5eaf37440b8e337ab150c017df7c03faf846c51DRC LOCFSERROR bnexterr; /* error for below/next col */ 621cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane LOCFSERROR delta; 622e5eaf37440b8e337ab150c017df7c03faf846c51DRC register FSERRPTR errorptr; /* => fserrors[] at column before current */ 6234a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane register JSAMPROW input_ptr; 6244a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane register JSAMPROW output_ptr; 6254a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane JSAMPROW colorindex_ci; 6264a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane JSAMPROW colormap_ci; 627cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane int pixcode; 62836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane int nc = cinfo->out_color_components; 629e5eaf37440b8e337ab150c017df7c03faf846c51DRC int dir; /* 1 for left-to-right, -1 for right-to-left */ 630e5eaf37440b8e337ab150c017df7c03faf846c51DRC int dirnc; /* dir * nc */ 6314a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane int ci; 6324a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane int row; 63336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION col; 63436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane JDIMENSION width = cinfo->output_width; 635cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane JSAMPLE *range_limit = cinfo->sample_range_limit; 63688aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane SHIFT_TEMPS 6372cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 6382cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane for (row = 0; row < num_rows; row++) { 6394a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane /* Initialize output values to 0 so can process components separately */ 6405de454b291f48382648a5d1dc2aa0fca8b5786d4DRC jzero_far((void *) output_buf[row], (size_t) (width * sizeof(JSAMPLE))); 6414a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane for (ci = 0; ci < nc; ci++) { 64236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane input_ptr = input_buf[row] + ci; 64336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane output_ptr = output_buf[row]; 64436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (cquantize->on_odd_row) { 645e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* work right to left in this row */ 646e5eaf37440b8e337ab150c017df7c03faf846c51DRC input_ptr += (width-1) * nc; /* so point to rightmost pixel */ 647e5eaf37440b8e337ab150c017df7c03faf846c51DRC output_ptr += width-1; 648e5eaf37440b8e337ab150c017df7c03faf846c51DRC dir = -1; 649e5eaf37440b8e337ab150c017df7c03faf846c51DRC dirnc = -nc; 650e5eaf37440b8e337ab150c017df7c03faf846c51DRC errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */ 6514a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane } else { 652e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* work left to right in this row */ 653e5eaf37440b8e337ab150c017df7c03faf846c51DRC dir = 1; 654e5eaf37440b8e337ab150c017df7c03faf846c51DRC dirnc = nc; 655e5eaf37440b8e337ab150c017df7c03faf846c51DRC errorptr = cquantize->fserrors[ci]; /* => entry before first column */ 6562cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 65736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane colorindex_ci = cquantize->colorindex[ci]; 658bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane colormap_ci = cquantize->sv_colormap[ci]; 659cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane /* Preset error values: no error propagated to first pixel from left */ 660cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane cur = 0; 661cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane /* and no error propagated to row below yet */ 662cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane belowerr = bpreverr = 0; 663cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane 66436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane for (col = width; col > 0; col--) { 665e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* cur holds the error propagated from the previous pixel on the 666e5eaf37440b8e337ab150c017df7c03faf846c51DRC * current line. Add the error propagated from the previous line 667e5eaf37440b8e337ab150c017df7c03faf846c51DRC * to form the complete error correction term for this pixel, and 668e5eaf37440b8e337ab150c017df7c03faf846c51DRC * round the error term (which is expressed * 16) to an integer. 669e5eaf37440b8e337ab150c017df7c03faf846c51DRC * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct 670e5eaf37440b8e337ab150c017df7c03faf846c51DRC * for either sign of the error value. 671e5eaf37440b8e337ab150c017df7c03faf846c51DRC * Note: errorptr points to *previous* column's array entry. 672e5eaf37440b8e337ab150c017df7c03faf846c51DRC */ 673e5eaf37440b8e337ab150c017df7c03faf846c51DRC cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4); 674e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE. 675e5eaf37440b8e337ab150c017df7c03faf846c51DRC * The maximum error is +- MAXJSAMPLE; this sets the required size 676e5eaf37440b8e337ab150c017df7c03faf846c51DRC * of the range_limit array. 677e5eaf37440b8e337ab150c017df7c03faf846c51DRC */ 678e5eaf37440b8e337ab150c017df7c03faf846c51DRC cur += GETJSAMPLE(*input_ptr); 679e5eaf37440b8e337ab150c017df7c03faf846c51DRC cur = GETJSAMPLE(range_limit[cur]); 680e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* Select output value, accumulate into output code for this pixel */ 681e5eaf37440b8e337ab150c017df7c03faf846c51DRC pixcode = GETJSAMPLE(colorindex_ci[cur]); 682e5eaf37440b8e337ab150c017df7c03faf846c51DRC *output_ptr += (JSAMPLE) pixcode; 683e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* Compute actual representation error at this pixel */ 684e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* Note: we can do this even though we don't have the final */ 685e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* pixel code, because the colormap is orthogonal. */ 686e5eaf37440b8e337ab150c017df7c03faf846c51DRC cur -= GETJSAMPLE(colormap_ci[pixcode]); 687e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* Compute error fractions to be propagated to adjacent pixels. 688e5eaf37440b8e337ab150c017df7c03faf846c51DRC * Add these into the running sums, and simultaneously shift the 689e5eaf37440b8e337ab150c017df7c03faf846c51DRC * next-line error sums left by 1 column. 690e5eaf37440b8e337ab150c017df7c03faf846c51DRC */ 691e5eaf37440b8e337ab150c017df7c03faf846c51DRC bnexterr = cur; 692e5eaf37440b8e337ab150c017df7c03faf846c51DRC delta = cur * 2; 693e5eaf37440b8e337ab150c017df7c03faf846c51DRC cur += delta; /* form error * 3 */ 694e5eaf37440b8e337ab150c017df7c03faf846c51DRC errorptr[0] = (FSERROR) (bpreverr + cur); 695e5eaf37440b8e337ab150c017df7c03faf846c51DRC cur += delta; /* form error * 5 */ 696e5eaf37440b8e337ab150c017df7c03faf846c51DRC bpreverr = belowerr + cur; 697e5eaf37440b8e337ab150c017df7c03faf846c51DRC belowerr = bnexterr; 698e5eaf37440b8e337ab150c017df7c03faf846c51DRC cur += delta; /* form error * 7 */ 699e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* At this point cur contains the 7/16 error value to be propagated 700e5eaf37440b8e337ab150c017df7c03faf846c51DRC * to the next pixel on the current line, and all the errors for the 701e5eaf37440b8e337ab150c017df7c03faf846c51DRC * next line have been shifted over. We are therefore ready to move on. 702e5eaf37440b8e337ab150c017df7c03faf846c51DRC */ 703e5eaf37440b8e337ab150c017df7c03faf846c51DRC input_ptr += dirnc; /* advance input ptr to next column */ 704e5eaf37440b8e337ab150c017df7c03faf846c51DRC output_ptr += dir; /* advance output ptr to next column */ 705e5eaf37440b8e337ab150c017df7c03faf846c51DRC errorptr += dir; /* advance errorptr to current column */ 7062cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 707cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane /* Post-loop cleanup: we must unload the final error value into the 708cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * final fserrors[] entry. Note we need not unload belowerr because 709cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane * it is for the dummy column before or after the actual array. 710cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane */ 711cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */ 7122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 71336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE); 7142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane } 7152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane} 7162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 7172cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 7182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* 719bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Allocate workspace for Floyd-Steinberg errors. 720bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 721bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 722489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void) 723bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanealloc_fs_workspace (j_decompress_ptr cinfo) 724bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{ 725bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 726bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane size_t arraysize; 727bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane int i; 728bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 7295de454b291f48382648a5d1dc2aa0fca8b5786d4DRC arraysize = (size_t) ((cinfo->output_width + 2) * sizeof(FSERROR)); 730bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane for (i = 0; i < cinfo->out_color_components; i++) { 731bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->fserrors[i] = (FSERRPTR) 732bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize); 733bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 734bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} 735bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 736bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 737bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 73836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Initialize for one-pass color quantization. 7392cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 7402cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 741489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 74236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanestart_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan) 7432cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{ 744bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; 745bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane size_t arraysize; 746bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane int i; 747bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 748bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Install my colormap. */ 749bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cinfo->colormap = cquantize->sv_colormap; 750bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cinfo->actual_number_of_colors = cquantize->sv_actual; 751bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 752bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Initialize for desired dithering mode. */ 753bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane switch (cinfo->dither_mode) { 754bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane case JDITHER_NONE: 755bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->out_color_components == 3) 756bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->pub.color_quantize = color_quantize3; 757bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane else 758bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->pub.color_quantize = color_quantize; 759bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane break; 760bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane case JDITHER_ORDERED: 761bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->out_color_components == 3) 762bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->pub.color_quantize = quantize3_ord_dither; 763bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane else 764bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->pub.color_quantize = quantize_ord_dither; 765e5eaf37440b8e337ab150c017df7c03faf846c51DRC cquantize->row_index = 0; /* initialize state for ordered dither */ 766bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* If user changed to ordered dither from another mode, 767bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * we must recreate the color index table with padding. 768bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This will cost extra space, but probably isn't very likely. 769bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 770bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (! cquantize->is_padded) 771bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane create_colorindex(cinfo); 772bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Create ordered-dither tables if we didn't already. */ 773bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cquantize->odither[0] == NULL) 774bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane create_odither_tables(cinfo); 775bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane break; 776bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane case JDITHER_FS: 777bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->pub.color_quantize = quantize_fs_dither; 778bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */ 779bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Allocate Floyd-Steinberg workspace if didn't already. */ 780bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cquantize->fserrors[0] == NULL) 781bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane alloc_fs_workspace(cinfo); 782bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Initialize the propagated errors to zero. */ 7835de454b291f48382648a5d1dc2aa0fca8b5786d4DRC arraysize = (size_t) ((cinfo->output_width + 2) * sizeof(FSERROR)); 784bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane for (i = 0; i < cinfo->out_color_components; i++) 7855033f3e19a31e8ad40c1a79700365aefe5664494DRC jzero_far((void *) cquantize->fserrors[i], arraysize); 786bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane break; 787bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane default: 788bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane ERREXIT(cinfo, JERR_NOT_COMPILED); 789bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane break; 790bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 7912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane} 7922cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 7932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 7942cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* 79536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Finish up at the end of the pass. 7962cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 7972cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 798489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 79936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanefinish_pass_1_quant (j_decompress_ptr cinfo) 8002cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{ 80136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* no work in 1-pass case */ 8022cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane} 8032cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 8042cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 8052cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/* 806bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Switch to a new external colormap between output passes. 807bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Shouldn't get to this module! 808bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 809bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 810489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void) 811bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanenew_color_map_1_quant (j_decompress_ptr cinfo) 812bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{ 813bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane ERREXIT(cinfo, JERR_MODE_CHANGE); 814bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} 815bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 816bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 817bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 81836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Module initialization routine for 1-pass color quantization. 8192cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */ 8202cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 821489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void) 82236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejinit_1pass_quantizer (j_decompress_ptr cinfo) 8232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{ 82436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane my_cquantize_ptr cquantize; 8252cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 82636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cquantize = (my_cquantize_ptr) 82736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 8285de454b291f48382648a5d1dc2aa0fca8b5786d4DRC sizeof(my_cquantizer)); 82936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize; 83036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cquantize->pub.start_pass = start_pass_1_quant; 83136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane cquantize->pub.finish_pass = finish_pass_1_quant; 832bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->pub.new_color_map = new_color_map_1_quant; 833bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */ 834e5eaf37440b8e337ab150c017df7c03faf846c51DRC cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */ 8352cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 83636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Make sure my internal arrays won't overflow */ 83736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (cinfo->out_color_components > MAX_Q_COMPS) 83836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS); 83936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane /* Make sure colormap indexes can be represented by JSAMPLEs */ 84036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1)) 84136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1); 84236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane 843bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Create the colormap and color index table. */ 84436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane create_colormap(cinfo); 845bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane create_colorindex(cinfo); 846bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 847bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Allocate Floyd-Steinberg workspace now if requested. 8485033f3e19a31e8ad40c1a79700365aefe5664494DRC * We do this now since it may affect the memory manager's space 8495033f3e19a31e8ad40c1a79700365aefe5664494DRC * calculations. If the user changes to FS dither mode in a later pass, we 8505033f3e19a31e8ad40c1a79700365aefe5664494DRC * will allocate the space then, and will possibly overrun the 8515033f3e19a31e8ad40c1a79700365aefe5664494DRC * max_memory_to_use setting. 852bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 853bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->dither_mode == JDITHER_FS) 854bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane alloc_fs_workspace(cinfo); 8552cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane} 8562cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane 8572cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#endif /* QUANT_1PASS_SUPPORTED */ 858