]> Creatis software - gdcm.git/blob - src/gdcmjpeg/jdlhuff.c
ENH: Final -hopefully- change to jpeg lib. In order to match ITK structure, and be...
[gdcm.git] / src / gdcmjpeg / jdlhuff.c
1 /*
2  * jdlhuff.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 for lossless JPEG.
9  *
10  * Much of the complexity here has to do with supporting input suspension.
11  * If the data source module demands suspension, we want to be able to back
12  * up to the start of the current MCU.  To do this, we copy state variables
13  * into local working storage, and update them back to the permanent
14  * storage only upon successful completion of an MCU.
15  */
16
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jlossls.h"    /* Private declarations for lossless codec */
21 #include "jdhuff.h"    /* Declarations shared with jd*huff.c */
22
23
24 #ifdef D_LOSSLESS_SUPPORTED
25
26 typedef struct {
27   int ci, yoffset, MCU_width;
28 } lhd_output_ptr_info;
29
30 /*
31  * Private entropy decoder object for lossless Huffman decoding.
32  */
33
34 typedef struct {
35   huffd_common_fields;    /* Fields shared with other entropy decoders */
36
37   /* Pointers to derived tables (these workspaces have image lifespan) */
38   d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
39
40   /* Precalculated info set up by start_pass for use in decode_mcus: */
41
42   /* Pointers to derived tables to be used for each data unit within an MCU */
43   d_derived_tbl * cur_tbls[D_MAX_DATA_UNITS_IN_MCU];
44
45   /* Pointers to the proper output difference row for each group of data units
46    * within an MCU.  For each component, there are Vi groups of Hi data units.
47    */
48   JDIFFROW output_ptr[D_MAX_DATA_UNITS_IN_MCU];
49
50   /* Number of output pointers in use for the current MCU.  This is the sum
51    * of all Vi in the MCU.
52    */
53   int num_output_ptrs;
54
55   /* Information used for positioning the output pointers within the output
56    * difference rows.
57    */
58   lhd_output_ptr_info output_ptr_info[D_MAX_DATA_UNITS_IN_MCU];
59
60   /* Index of the proper output pointer for each data unit within an MCU */
61   int output_ptr_index[D_MAX_DATA_UNITS_IN_MCU];
62
63 } lhuff_entropy_decoder;
64
65 typedef lhuff_entropy_decoder * lhuff_entropy_ptr;
66
67
68 /*
69  * Initialize for a Huffman-compressed scan.
70  */
71
72 METHODDEF(void)
73 start_pass_lhuff_decoder (j_decompress_ptr cinfo)
74 {
75   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
76   lhuff_entropy_ptr entropy = (lhuff_entropy_ptr) losslsd->entropy_private;
77   int ci, dctbl, sampn, ptrn, yoffset, xoffset;
78   jpeg_component_info * compptr;
79
80   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
81     compptr = cinfo->cur_comp_info[ci];
82     dctbl = compptr->dc_tbl_no;
83     /* Make sure requested tables are present */
84     if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS ||
85   cinfo->dc_huff_tbl_ptrs[dctbl] == NULL)
86       ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
87     /* Compute derived values for Huffman tables */
88     /* We may do this more than once for a table, but it's not expensive */
89     jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
90           & entropy->derived_tbls[dctbl]);
91   }
92
93   /* Precalculate decoding info for each sample in an MCU of this scan */
94   for (sampn = 0, ptrn = 0; sampn < cinfo->data_units_in_MCU;) {
95     compptr = cinfo->cur_comp_info[cinfo->MCU_membership[sampn]];
96     ci = compptr->component_index;
97     for (yoffset = 0; yoffset < compptr->MCU_height; yoffset++, ptrn++) {
98       /* Precalculate the setup info for each output pointer */
99       entropy->output_ptr_info[ptrn].ci = ci;
100       entropy->output_ptr_info[ptrn].yoffset = yoffset;
101       entropy->output_ptr_info[ptrn].MCU_width = compptr->MCU_width;
102       for (xoffset = 0; xoffset < compptr->MCU_width; xoffset++, sampn++) {
103   /* Precalculate the output pointer index for each sample */
104   entropy->output_ptr_index[sampn] = ptrn;
105   /* Precalculate which table to use for each sample */
106   entropy->cur_tbls[sampn] = entropy->derived_tbls[compptr->dc_tbl_no];
107       }
108     }
109   }
110   entropy->num_output_ptrs = ptrn;
111
112   /* Initialize bitread state variables */
113   entropy->bitstate.bits_left = 0;
114   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
115   entropy->insufficient_data = FALSE;
116 }
117
118
119 /*
120  * Figure F.12: extend sign bit.
121  * On some machines, a shift and add will be faster than a table lookup.
122  */
123
124 #ifdef AVOID_TABLES
125
126 #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
127
128 #else
129
130 #define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
131
132 static const int extend_test[16] =   /* entry n is 2**(n-1) */
133   { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
134     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
135
136 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
137   { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
138     ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
139     ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
140     ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
141
142 #endif /* AVOID_TABLES */
143
144
145 /*
146  * Check for a restart marker & resynchronize decoder.
147  * Returns FALSE if must suspend.
148  */
149
150 METHODDEF(boolean)
151 process_restart (j_decompress_ptr cinfo)
152 {
153   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
154   lhuff_entropy_ptr entropy = (lhuff_entropy_ptr) losslsd->entropy_private;
155   /* int ci; */
156
157   /* Throw away any unused bits remaining in bit buffer; */
158   /* include any full bytes in next_marker's count of discarded bytes */
159   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
160   entropy->bitstate.bits_left = 0;
161
162   /* Advance past the RSTn marker */
163   if (! (*cinfo->marker->read_restart_marker) (cinfo))
164     return FALSE;
165
166   /* Reset out-of-data flag, unless read_restart_marker left us smack up
167    * against a marker.  In that case we will end up treating the next data
168    * segment as empty, and we can avoid producing bogus output pixels by
169    * leaving the flag set.
170    */
171   if (cinfo->unread_marker == 0)
172     entropy->insufficient_data = FALSE;
173
174   return TRUE;
175 }
176
177
178 /*
179  * Decode and return nMCU's worth of Huffman-compressed differences.
180  * Each MCU is also disassembled and placed accordingly in diff_buf.
181  *
182  * MCU_col_num specifies the column of the first MCU being requested within
183  * the MCU-row.  This tells us where to position the output row pointers in
184  * diff_buf.
185  *
186  * Returns the number of MCUs decoded.  This may be less than nMCU if data
187  * source requested suspension.  In that case no changes have been made to
188  * permanent state.  (Exception: some output differences may already have
189  * been assigned.  This is harmless for this module, since we'll just
190  * re-assign them on the next call.)
191  */
192
193 METHODDEF(JDIMENSION)
194 decode_mcus (j_decompress_ptr cinfo, JDIFFIMAGE diff_buf,
195        JDIMENSION MCU_row_num, JDIMENSION MCU_col_num, JDIMENSION nMCU)
196 {
197   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
198   lhuff_entropy_ptr entropy = (lhuff_entropy_ptr) losslsd->entropy_private;
199   unsigned int mcu_num;
200   int sampn, ci, yoffset, MCU_width, ptrn;
201   BITREAD_STATE_VARS;
202
203   /* Set output pointer locations based on MCU_col_num */
204   for (ptrn = 0; ptrn < entropy->num_output_ptrs; ptrn++) {
205     ci = entropy->output_ptr_info[ptrn].ci;
206     yoffset = entropy->output_ptr_info[ptrn].yoffset;
207     MCU_width = entropy->output_ptr_info[ptrn].MCU_width;
208     entropy->output_ptr[ptrn] =
209       diff_buf[ci][MCU_row_num + yoffset] + (MCU_col_num * MCU_width);
210   }
211
212   /*
213    * If we've run out of data, zero out the buffers and return.
214    * By resetting the undifferencer, the output samples will be CENTERJSAMPLE.
215    *
216    * NB: We should find a way to do this without interacting with the
217    * undifferencer module directly.
218    */
219   if (entropy->insufficient_data) {
220     for (ptrn = 0; ptrn < entropy->num_output_ptrs; ptrn++)
221       jzero_far((void FAR *) entropy->output_ptr[ptrn],
222     nMCU * entropy->output_ptr_info[ptrn].MCU_width * SIZEOF(JDIFF));
223
224     (*losslsd->predict_process_restart) (cinfo);
225   }
226
227   else {
228
229     /* Load up working state */
230     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
231
232     /* Outer loop handles the number of MCU requested */
233
234     for (mcu_num = 0; mcu_num < nMCU; mcu_num++) {
235
236       /* Inner loop handles the samples in the MCU */
237       for (sampn = 0; sampn < cinfo->data_units_in_MCU; sampn++) {
238   d_derived_tbl * dctbl = entropy->cur_tbls[sampn];
239   register int s, r;
240
241   /* Section H.2.2: decode the sample difference */
242   HUFF_DECODE(s, br_state, dctbl, return mcu_num, label1);
243   if (s) {
244     if (s == 16)  /* special case: always output 32768 */
245       s = 32768;
246     else {  /* normal case: fetch subsequent bits */
247       CHECK_BIT_BUFFER(br_state, s, return mcu_num);
248       r = GET_BITS(s);
249       s = HUFF_EXTEND(r, s);
250     }
251   }
252
253   /* Output the sample difference */
254   *entropy->output_ptr[entropy->output_ptr_index[sampn]]++ = (JDIFF) s;
255       }
256
257       /* Completed MCU, so update state */
258       BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
259     }
260   }
261
262  return nMCU;
263 }
264
265
266 /*
267  * Module initialization routine for lossless Huffman entropy decoding.
268  */
269
270 GLOBAL(void)
271 jinit_lhuff_decoder (j_decompress_ptr cinfo)
272 {
273   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
274   lhuff_entropy_ptr entropy;
275   int i;
276
277   entropy = (lhuff_entropy_ptr)
278     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
279         SIZEOF(lhuff_entropy_decoder));
280   losslsd->entropy_private = (void *) entropy;
281   losslsd->entropy_start_pass = start_pass_lhuff_decoder;
282   losslsd->entropy_process_restart = process_restart;
283   losslsd->entropy_decode_mcus = decode_mcus;
284
285   /* Mark tables unallocated */
286   for (i = 0; i < NUM_HUFF_TBLS; i++) {
287     entropy->derived_tbls[i] = NULL;
288   }
289 }
290
291 #endif /* D_LOSSLESS_SUPPORTED */