]> Creatis software - gdcm.git/blob - src/jpeg/ljpg/jpegutil.c
FIX: Compilation on Mac OSX is fine now + remove tabs
[gdcm.git] / src / jpeg / ljpg / jpegutil.c
1 /*
2  * jpegutil.c --
3  *
4  * Various utility routines used in the jpeg encoder/decoder.  Large parts
5  * are stolen from the IJG code
6  */
7 /*
8  * $Id: jpegutil.c,v 1.2 2004/08/18 02:26:08 malaterre Exp $
9  */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "jpeg.h"
14 #include "mcu.h"
15 #include "proto.h"
16
17 /*
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.
21  */
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)];
26
27
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};
36 /*
37  *--------------------------------------------------------------
38  *
39  * JroundUp --
40  *
41  *        Compute a rounded up to next multiple of b; a >= 0, b > 0 
42  *
43  * Results:
44  *        Rounded up value.
45  *
46  * Side effects:
47  *        None.
48  *
49  *--------------------------------------------------------------
50  */
51 int JroundUp (int a, int b)
52 {
53     a += b - 1;
54     return a - (a % b);
55 }
56
57 /*
58  *--------------------------------------------------------------
59  *
60  * DecoderStructInit --
61  *
62  *        Initalize the rest of the fields in the decompression
63  *        structure.
64  *
65  * Results:
66  *        None.
67  *
68  * Side effects:
69  *        None.
70  *
71  *--------------------------------------------------------------
72  */
73
74 void DecoderStructInit (DecompressInfo *dcPtr)
75 {
76     char *buf1,*buf2;
77     short ci,i;
78     JpegComponentInfo *compPtr;
79     int mcuSize;
80
81     /*
82      * Check sampling factor validity.
83      */
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");
88            /* exit(-1); */
89            dcPtr->error = -1; return; 
90         }
91     }
92
93     /*
94      * Prepare array describing MCU composition
95      */
96     if (dcPtr->compsInScan == 1) {
97         dcPtr->MCUmembership[0] = 0;
98     } else {
99         short ci;
100
101         if (dcPtr->compsInScan > 4) {
102             fprintf (stderr, "Too many components for interleaved scan\n");
103             /* exit (1); */
104             dcPtr->error = -1; return;
105         }
106
107         for (ci = 0; ci < dcPtr->compsInScan; ci++) {
108             dcPtr->MCUmembership[ci] = ci;
109         }
110     }
111
112     /*
113      * Initialize mucROW1 and mcuROW2 which buffer two rows of
114      * pixels for predictor calculation.
115      */
116
117     mcuROW1 = (MCU *) mcuROW1Memory;
118     mcuROW2 = (MCU *) mcuROW2Memory;
119
120     mcuSize=dcPtr->compsInScan * sizeof(ComponentType);
121
122     buf1 = buf1Memory;
123     buf2 = buf2Memory;
124
125     for (i=0;i<dcPtr->imageWidth;i++) {
126         mcuROW1[i]=(MCU)(buf1+i*mcuSize);
127         mcuROW2[i]=(MCU)(buf2+i*mcuSize);
128     }
129
130     dcPtr->error = 0;
131
132 }/*endof DecoderStructInit*/
133
134
135 /*
136  *--------------------------------------------------------------
137  *
138  * FixHuffTbl --
139  *
140  *      Compute derived values for a Huffman table one the DHT marker
141  *      has been processed.  This generates both the encoding and
142  *      decoding tables.
143  *
144  * Results:
145  *      None.
146  *
147  * Side effects:
148  *      None.
149  *
150  *--------------------------------------------------------------
151  */
152 void FixHuffTbl (HuffmanTable *htbl)
153 {
154     int p, i, l, lastp, si;
155     char huffsize[257];
156     Ushort huffcode[257];
157     Ushort code;
158     int size;
159     int value, ll, ul;
160
161     /*
162      * Figure C.1: make table of Huffman code length for each symbol
163      * Note that this is in code-length order.
164      */
165     p = 0;
166     for (l = 1; l <= 16; l++) {
167         for (i = 1; i <= (int)htbl->bits[l]; i++)
168             huffsize[p++] = (char)l;
169     }
170     huffsize[p] = 0;
171     lastp = p;
172
173
174     /*
175      * Figure C.2: generate the codes themselves
176      * Note that this is in code-length order.
177      */
178     code = 0;
179     si = huffsize[0];
180     p = 0;
181     while (huffsize[p]) {
182         while (((int)huffsize[p]) == si) {
183             huffcode[p++] = code;
184             code++;
185         }
186         code <<= 1;
187         si++;
188     }
189
190     /*
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.
195      */
196     MEMSET(htbl->ehufsi, 0, sizeof(htbl->ehufsi));
197
198     for (p = 0; p < lastp; p++) {
199         htbl->ehufco[htbl->huffval[p]] = huffcode[p];
200         htbl->ehufsi[htbl->huffval[p]] = huffsize[p];
201     }
202
203     /*
204      * Figure F.15: generate decoding tables
205      */
206     p = 0;
207     for (l = 1; l <= 16; l++) {
208         if (htbl->bits[l]) {
209             htbl->valptr[l] = p;
210             htbl->mincode[l] = huffcode[p];
211             p += htbl->bits[l];
212             htbl->maxcode[l] = huffcode[p - 1];
213         } else {
214             htbl->maxcode[l] = -1;
215         }
216     }
217
218     /*
219      * We put in this value to ensure HuffDecode terminates.
220      */
221     htbl->maxcode[17] = 0xFFFFFL;
222
223     /*
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).
229      */
230     /*bzero (htbl->numbits, sizeof(htbl->numbits));*/
231     memset(htbl->numbits, 0, sizeof(htbl->numbits));
232
233     for (p=0; p<lastp; p++) {
234         size = huffsize[p];
235         if (size <= 8) {
236             value = htbl->huffval[p];
237             code = huffcode[p];
238             ll = code << (8-size);
239             if (size < 8) {
240                 ul = ll | bitMask[24+size];
241             } else {
242                 ul = ll;
243             }
244             for (i=ll; i<=ul; i++) {
245                 htbl->numbits[i] = size;
246                 htbl->value[i] = value;
247             }
248         }
249     }
250 }
251
252