4 * Various utility routines used in the jpeg encoder/decoder. Large parts
5 * are stolen from the IJG code
8 * $Id: jpegutil.c,v 1.2 2004/08/18 02:26:08 malaterre Exp $
18 * To fix memory leaks, memory is allocated once for the mcu buffers.
19 * Enough memory is reserved to accomodate up to 1024-wide images
20 * with up to 4 components.
22 static char mcuROW1Memory[1024 * sizeof(MCU)];
23 static char mcuROW2Memory[1024 * sizeof(MCU)];
24 static char buf1Memory[1024 * 4 * sizeof(ComponentType)];
25 static char buf2Memory[1024 * 4 * sizeof(ComponentType)];
28 unsigned int bitMask[] = { 0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff,
29 0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff,
30 0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff,
31 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff,
32 0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff,
33 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
34 0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f,
35 0x0000000f, 0x00000007, 0x00000003, 0x00000001};
37 *--------------------------------------------------------------
41 * Compute a rounded up to next multiple of b; a >= 0, b > 0
49 *--------------------------------------------------------------
51 int JroundUp (int a, int b)
58 *--------------------------------------------------------------
60 * DecoderStructInit --
62 * Initalize the rest of the fields in the decompression
71 *--------------------------------------------------------------
74 void DecoderStructInit (DecompressInfo *dcPtr)
78 JpegComponentInfo *compPtr;
82 * Check sampling factor validity.
84 for (ci = 0; ci < dcPtr->numComponents; ci++) {
85 compPtr = &dcPtr->compInfo[ci];
86 if ((compPtr->hSampFactor != 1) || (compPtr->vSampFactor != 1)) {
87 fprintf (stderr, "Error: Downsampling is not supported.\n");
89 dcPtr->error = -1; return;
94 * Prepare array describing MCU composition
96 if (dcPtr->compsInScan == 1) {
97 dcPtr->MCUmembership[0] = 0;
101 if (dcPtr->compsInScan > 4) {
102 fprintf (stderr, "Too many components for interleaved scan\n");
104 dcPtr->error = -1; return;
107 for (ci = 0; ci < dcPtr->compsInScan; ci++) {
108 dcPtr->MCUmembership[ci] = ci;
113 * Initialize mucROW1 and mcuROW2 which buffer two rows of
114 * pixels for predictor calculation.
117 mcuROW1 = (MCU *) mcuROW1Memory;
118 mcuROW2 = (MCU *) mcuROW2Memory;
120 mcuSize=dcPtr->compsInScan * sizeof(ComponentType);
125 for (i=0;i<dcPtr->imageWidth;i++) {
126 mcuROW1[i]=(MCU)(buf1+i*mcuSize);
127 mcuROW2[i]=(MCU)(buf2+i*mcuSize);
132 }/*endof DecoderStructInit*/
136 *--------------------------------------------------------------
140 * Compute derived values for a Huffman table one the DHT marker
141 * has been processed. This generates both the encoding and
150 *--------------------------------------------------------------
152 void FixHuffTbl (HuffmanTable *htbl)
154 int p, i, l, lastp, si;
156 Ushort huffcode[257];
162 * Figure C.1: make table of Huffman code length for each symbol
163 * Note that this is in code-length order.
166 for (l = 1; l <= 16; l++) {
167 for (i = 1; i <= (int)htbl->bits[l]; i++)
168 huffsize[p++] = (char)l;
175 * Figure C.2: generate the codes themselves
176 * Note that this is in code-length order.
181 while (huffsize[p]) {
182 while (((int)huffsize[p]) == si) {
183 huffcode[p++] = code;
191 * Figure C.3: generate encoding tables
192 * These are code and size indexed by symbol value
193 * Set any codeless symbols to have code length 0; this allows
194 * EmitBits to detect any attempt to emit such symbols.
196 MEMSET(htbl->ehufsi, 0, sizeof(htbl->ehufsi));
198 for (p = 0; p < lastp; p++) {
199 htbl->ehufco[htbl->huffval[p]] = huffcode[p];
200 htbl->ehufsi[htbl->huffval[p]] = huffsize[p];
204 * Figure F.15: generate decoding tables
207 for (l = 1; l <= 16; l++) {
210 htbl->mincode[l] = huffcode[p];
212 htbl->maxcode[l] = huffcode[p - 1];
214 htbl->maxcode[l] = -1;
219 * We put in this value to ensure HuffDecode terminates.
221 htbl->maxcode[17] = 0xFFFFFL;
224 * Build the numbits, value lookup tables.
225 * These table allow us to gather 8 bits from the bits stream,
226 * and immediately lookup the size and value of the huffman codes.
227 * If size is zero, it means that more than 8 bits are in the huffman
228 * code (this happens about 3-4% of the time).
230 /*bzero (htbl->numbits, sizeof(htbl->numbits));*/
231 memset(htbl->numbits, 0, sizeof(htbl->numbits));
233 for (p=0; p<lastp; p++) {
236 value = htbl->huffval[p];
238 ll = code << (8-size);
240 ul = ll | bitMask[24+size];
244 for (i=ll; i<=ul; i++) {
245 htbl->numbits[i] = size;
246 htbl->value[i] = value;