]> Creatis software - gdcm.git/blob - src/gdcmjpeg/jdhuff.c
Fix mistypings
[gdcm.git] / src / gdcmjpeg / jdhuff.c
1 /*
2  * jdhuff.c
3  *
4  * Copyright (C) 1991-1998, Thomas G. Lane.
5  * This file is part of the Independent JPEG Group's software.
6  * For conditions of distribution and use, see the accompanying README file.
7  *
8  * This file contains Huffman entropy decoding routines which are shared
9  * by the sequential, progressive and lossless decoders.
10  */
11
12 #define JPEG_INTERNALS
13 #include "jinclude.h"
14 #include "jpeglib.h"
15 #include "jlossy.h"    /* Private declarations for lossy codec */
16 #include "jlossls.h"    /* Private declarations for lossless codec */
17 #include "jdhuff.h"    /* Declarations shared with jd*huff.c */
18
19
20 /*
21  * Compute the derived values for a Huffman table.
22  * This routine also performs some validation checks on the table.
23  */
24
25 GLOBAL(void)
26 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
27        d_derived_tbl ** pdtbl)
28 {
29   JHUFF_TBL *htbl;
30   d_derived_tbl *dtbl;
31   int p, i, l, si, numsymbols;
32   int lookbits, ctr;
33   char huffsize[257];
34   unsigned int huffcode[257];
35   unsigned int code;
36
37   /* Note that huffsize[] and huffcode[] are filled in code-length order,
38    * paralleling the order of the symbols themselves in htbl->huffval[].
39    */
40
41   /* Find the input Huffman table */
42   if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
43     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
44   htbl =
45     isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
46   if (htbl == NULL)
47     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
48
49   /* Allocate a workspace if we haven't already done so. */
50   if (*pdtbl == NULL)
51     *pdtbl = (d_derived_tbl *)
52       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
53           SIZEOF(d_derived_tbl));
54   dtbl = *pdtbl;
55   dtbl->pub = htbl;    /* fill in back link */
56   
57   /* Figure C.1: make table of Huffman code length for each symbol */
58
59   p = 0;
60   for (l = 1; l <= 16; l++) {
61     i = (int) htbl->bits[l];
62     if (i < 0 || p + i > 256)  /* protect against table overrun */
63       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
64     while (i--)
65       huffsize[p++] = (char) l;
66   }
67   huffsize[p] = 0;
68   numsymbols = p;
69   
70   /* Figure C.2: generate the codes themselves */
71   /* We also validate that the counts represent a legal Huffman code tree. */
72   
73   code = 0;
74   si = huffsize[0];
75   p = 0;
76   while (huffsize[p]) {
77     while (((int) huffsize[p]) == si) {
78       huffcode[p++] = code;
79       code++;
80     }
81     /* code is now 1 more than the last code used for codelength si; but
82      * it must still fit in si bits, since no code is allowed to be all ones.
83      * BUG FIX 2001-09-03: Comparison must be >, not >=
84      */
85     if (((INT32) code) > (((INT32) 1) << si))
86       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
87     code <<= 1;
88     si++;
89   }
90
91   /* Figure F.15: generate decoding tables for bit-sequential decoding */
92
93   p = 0;
94   for (l = 1; l <= 16; l++) {
95     if (htbl->bits[l]) {
96       /* valoffset[l] = huffval[] index of 1st symbol of code length l,
97        * minus the minimum code of length l
98        */
99       dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
100       p += htbl->bits[l];
101       dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
102     } else {
103       dtbl->maxcode[l] = -1;  /* -1 if no codes of this length */
104     }
105   }
106   dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
107
108   /* Compute lookahead tables to speed up decoding.
109    * First we set all the table entries to 0, indicating "too long";
110    * then we iterate through the Huffman codes that are short enough and
111    * fill in all the entries that correspond to bit sequences starting
112    * with that code.
113    */
114
115   MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
116
117   p = 0;
118   for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
119     for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
120       /* l = current code's length, p = its index in huffcode[] & huffval[]. */
121       /* Generate left-justified code followed by all possible bit sequences */
122       lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
123       for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
124   dtbl->look_nbits[lookbits] = l;
125   dtbl->look_sym[lookbits] = htbl->huffval[p];
126   lookbits++;
127       }
128     }
129   }
130
131   /* Validate symbols as being reasonable.
132    * For AC tables, we make no check, but accept all byte values 0..255.
133    * For DC tables, we require the symbols to be in range 0..16.
134    * (Tighter bounds could be applied depending on the data depth and mode,
135    * but this is sufficient to ensure safe decoding.)
136    */
137   if (isDC) {
138     for (i = 0; i < numsymbols; i++) {
139       int sym = htbl->huffval[i];
140       if (sym < 0 || sym > 16)
141 /* The following is needed to be able to read certain Philips DICOM MRI images */
142 #if BITS_IN_JSAMPLE == 12
143         htbl->huffval[i]=15;
144 #else
145   ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
146 #endif /* BITS_IN_JSAMPLE == 12 */
147     }
148   }
149 }
150
151
152 /*
153  * Out-of-line code for bit fetching.
154  * See jdhuff.h for info about usage.
155  * Note: current values of get_buffer and bits_left are passed as parameters,
156  * but are returned in the corresponding fields of the state struct.
157  *
158  * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
159  * of get_buffer to be used.  (On machines with wider words, an even larger
160  * buffer could be used.)  However, on some machines 32-bit shifts are
161  * quite slow and take time proportional to the number of places shifted.
162  * (This is true with most PC compilers, for instance.)  In this case it may
163  * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
164  * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
165  */
166
167 #ifdef SLOW_SHIFT_32
168 #define MIN_GET_BITS  15  /* minimum allowable value */
169 #else
170 #define MIN_GET_BITS  (BIT_BUF_SIZE-7)
171 #endif
172
173
174 GLOBAL(boolean)
175 jpeg_fill_bit_buffer (bitread_working_state * state,
176           register bit_buf_type get_buffer, register int bits_left,
177           int nbits)
178 /* Load up the bit buffer to a depth of at least nbits */
179 {
180   /* Copy heavily used state fields into locals (hopefully registers) */
181   register const JOCTET * next_input_byte = state->next_input_byte;
182   register size_t bytes_in_buffer = state->bytes_in_buffer;
183   j_decompress_ptr cinfo = state->cinfo;
184
185   /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
186   /* (It is assumed that no request will be for more than that many bits.) */
187   /* We fail to do so only if we hit a marker or are forced to suspend. */
188
189   if (cinfo->unread_marker == 0) {  /* cannot advance past a marker */
190     while (bits_left < MIN_GET_BITS) {
191       register int c;
192
193       /* Attempt to read a byte */
194       if (bytes_in_buffer == 0) {
195   if (! (*cinfo->src->fill_input_buffer) (cinfo))
196     return FALSE;
197   next_input_byte = cinfo->src->next_input_byte;
198   bytes_in_buffer = cinfo->src->bytes_in_buffer;
199       }
200       bytes_in_buffer--;
201       c = GETJOCTET(*next_input_byte++);
202
203       /* If it's 0xFF, check and discard stuffed zero byte */
204       if (c == 0xFF) {
205   /* Loop here to discard any padding FF's on terminating marker,
206    * so that we can save a valid unread_marker value.  NOTE: we will
207    * accept multiple FF's followed by a 0 as meaning a single FF data
208    * byte.  This data pattern is not valid according to the standard.
209    */
210   do {
211     if (bytes_in_buffer == 0) {
212       if (! (*cinfo->src->fill_input_buffer) (cinfo))
213         return FALSE;
214       next_input_byte = cinfo->src->next_input_byte;
215       bytes_in_buffer = cinfo->src->bytes_in_buffer;
216     }
217     bytes_in_buffer--;
218     c = GETJOCTET(*next_input_byte++);
219   } while (c == 0xFF);
220
221   if (c == 0) {
222     /* Found FF/00, which represents an FF data byte */
223     c = 0xFF;
224   } else {
225     /* Oops, it's actually a marker indicating end of compressed data.
226      * Save the marker code for later use.
227      * Fine point: it might appear that we should save the marker into
228      * bitread working state, not straight into permanent state.  But
229      * once we have hit a marker, we cannot need to suspend within the
230      * current MCU, because we will read no more bytes from the data
231      * source.  So it is OK to update permanent state right away.
232      */
233     cinfo->unread_marker = c;
234     /* See if we need to insert some fake zero bits. */
235     goto no_more_bytes;
236   }
237       }
238
239       /* OK, load c into get_buffer */
240       get_buffer = (get_buffer << 8) | c;
241       bits_left += 8;
242     } /* end while */
243   } else {
244   no_more_bytes:
245     /* We get here if we've read the marker that terminates the compressed
246      * data segment.  There should be enough bits in the buffer register
247      * to satisfy the request; if so, no problem.
248      */
249     if (nbits > bits_left) {
250       /* Uh-oh.  Report corrupted data to user and stuff zeroes into
251        * the data stream, so that we can produce some kind of image.
252        * We use a nonvolatile flag to ensure that only one warning message
253        * appears per data segment.
254        */
255       huffd_common_ptr huffd;
256       if (cinfo->process == JPROC_LOSSLESS)
257   huffd = (huffd_common_ptr) ((j_lossless_d_ptr) cinfo->codec)->entropy_private;
258       else
259   huffd = (huffd_common_ptr) ((j_lossy_d_ptr) cinfo->codec)->entropy_private;
260       if (! huffd->insufficient_data) {
261   WARNMS(cinfo, JWRN_HIT_MARKER);
262   huffd->insufficient_data = TRUE;
263       }
264       /* Fill the buffer with zero bits */
265       get_buffer <<= MIN_GET_BITS - bits_left;
266       bits_left = MIN_GET_BITS;
267     }
268   }
269
270   /* Unload the local registers */
271   state->next_input_byte = next_input_byte;
272   state->bytes_in_buffer = bytes_in_buffer;
273   state->get_buffer = get_buffer;
274   state->bits_left = bits_left;
275
276   return TRUE;
277 }
278
279
280 /*
281  * Out-of-line code for Huffman code decoding.
282  * See jdhuff.h for info about usage.
283  */
284
285 GLOBAL(int)
286 jpeg_huff_decode (bitread_working_state * state,
287       register bit_buf_type get_buffer, register int bits_left,
288       d_derived_tbl * htbl, int min_bits)
289 {
290   register int l = min_bits;
291   register INT32 code;
292
293   /* HUFF_DECODE has determined that the code is at least min_bits */
294   /* bits long, so fetch that many bits in one swoop. */
295
296   CHECK_BIT_BUFFER(*state, l, return -1);
297   code = GET_BITS(l);
298
299   /* Collect the rest of the Huffman code one bit at a time. */
300   /* This is per Figure F.16 in the JPEG spec. */
301
302   while (code > htbl->maxcode[l]) {
303     code <<= 1;
304     CHECK_BIT_BUFFER(*state, 1, return -1);
305     code |= GET_BITS(1);
306     l++;
307   }
308
309   /* Unload the local registers */
310   state->get_buffer = get_buffer;
311   state->bits_left = bits_left;
312
313   /* With garbage input we may reach the sentinel value l = 17. */
314
315   if (l > 16) {
316     WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
317     return 0;      /* fake a zero as the safest result */
318   }
319
320   return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
321 }