rev |
line source |
nuclear@1
|
1 /*
|
nuclear@1
|
2 * jchuff.c
|
nuclear@1
|
3 *
|
nuclear@1
|
4 * Copyright (C) 1991-1997, Thomas G. Lane.
|
nuclear@1
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@1
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@1
|
7 *
|
nuclear@1
|
8 * This file contains Huffman entropy encoding routines.
|
nuclear@1
|
9 *
|
nuclear@1
|
10 * Much of the complexity here has to do with supporting output suspension.
|
nuclear@1
|
11 * If the data destination module demands suspension, we want to be able to
|
nuclear@1
|
12 * back up to the start of the current MCU. To do this, we copy state
|
nuclear@1
|
13 * variables into local working storage, and update them back to the
|
nuclear@1
|
14 * permanent JPEG objects only upon successful completion of an MCU.
|
nuclear@1
|
15 */
|
nuclear@1
|
16
|
nuclear@1
|
17 #define JPEG_INTERNALS
|
nuclear@1
|
18 #include "jinclude.h"
|
nuclear@1
|
19 #include "jpeglib.h"
|
nuclear@1
|
20 #include "jchuff.h" /* Declarations shared with jcphuff.c */
|
nuclear@1
|
21
|
nuclear@1
|
22
|
nuclear@1
|
23 /* Expanded entropy encoder object for Huffman encoding.
|
nuclear@1
|
24 *
|
nuclear@1
|
25 * The savable_state subrecord contains fields that change within an MCU,
|
nuclear@1
|
26 * but must not be updated permanently until we complete the MCU.
|
nuclear@1
|
27 */
|
nuclear@1
|
28
|
nuclear@1
|
29 typedef struct {
|
nuclear@1
|
30 INT32 put_buffer; /* current bit-accumulation buffer */
|
nuclear@1
|
31 int put_bits; /* # of bits now in it */
|
nuclear@1
|
32 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
nuclear@1
|
33 } savable_state;
|
nuclear@1
|
34
|
nuclear@1
|
35 /* This macro is to work around compilers with missing or broken
|
nuclear@1
|
36 * structure assignment. You'll need to fix this code if you have
|
nuclear@1
|
37 * such a compiler and you change MAX_COMPS_IN_SCAN.
|
nuclear@1
|
38 */
|
nuclear@1
|
39
|
nuclear@1
|
40 #ifndef NO_STRUCT_ASSIGN
|
nuclear@1
|
41 #define ASSIGN_STATE(dest,src) ((dest) = (src))
|
nuclear@1
|
42 #else
|
nuclear@1
|
43 #if MAX_COMPS_IN_SCAN == 4
|
nuclear@1
|
44 #define ASSIGN_STATE(dest,src) \
|
nuclear@1
|
45 ((dest).put_buffer = (src).put_buffer, \
|
nuclear@1
|
46 (dest).put_bits = (src).put_bits, \
|
nuclear@1
|
47 (dest).last_dc_val[0] = (src).last_dc_val[0], \
|
nuclear@1
|
48 (dest).last_dc_val[1] = (src).last_dc_val[1], \
|
nuclear@1
|
49 (dest).last_dc_val[2] = (src).last_dc_val[2], \
|
nuclear@1
|
50 (dest).last_dc_val[3] = (src).last_dc_val[3])
|
nuclear@1
|
51 #endif
|
nuclear@1
|
52 #endif
|
nuclear@1
|
53
|
nuclear@1
|
54
|
nuclear@1
|
55 typedef struct {
|
nuclear@1
|
56 struct jpeg_entropy_encoder pub; /* public fields */
|
nuclear@1
|
57
|
nuclear@1
|
58 savable_state saved; /* Bit buffer & DC state at start of MCU */
|
nuclear@1
|
59
|
nuclear@1
|
60 /* These fields are NOT loaded into local working state. */
|
nuclear@1
|
61 unsigned int restarts_to_go; /* MCUs left in this restart interval */
|
nuclear@1
|
62 int next_restart_num; /* next restart number to write (0-7) */
|
nuclear@1
|
63
|
nuclear@1
|
64 /* Pointers to derived tables (these workspaces have image lifespan) */
|
nuclear@1
|
65 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
|
nuclear@1
|
66 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
|
nuclear@1
|
67
|
nuclear@1
|
68 #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
|
nuclear@1
|
69 long * dc_count_ptrs[NUM_HUFF_TBLS];
|
nuclear@1
|
70 long * ac_count_ptrs[NUM_HUFF_TBLS];
|
nuclear@1
|
71 #endif
|
nuclear@1
|
72 } huff_entropy_encoder;
|
nuclear@1
|
73
|
nuclear@1
|
74 typedef huff_entropy_encoder * huff_entropy_ptr;
|
nuclear@1
|
75
|
nuclear@1
|
76 /* Working state while writing an MCU.
|
nuclear@1
|
77 * This struct contains all the fields that are needed by subroutines.
|
nuclear@1
|
78 */
|
nuclear@1
|
79
|
nuclear@1
|
80 typedef struct {
|
nuclear@1
|
81 JOCTET * next_output_byte; /* => next byte to write in buffer */
|
nuclear@1
|
82 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
|
nuclear@1
|
83 savable_state cur; /* Current bit buffer & DC state */
|
nuclear@1
|
84 j_compress_ptr cinfo; /* dump_buffer needs access to this */
|
nuclear@1
|
85 } working_state;
|
nuclear@1
|
86
|
nuclear@1
|
87
|
nuclear@1
|
88 /* Forward declarations */
|
nuclear@1
|
89 METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
|
nuclear@1
|
90 JBLOCKROW *MCU_data));
|
nuclear@1
|
91 METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
|
nuclear@1
|
92 #ifdef ENTROPY_OPT_SUPPORTED
|
nuclear@1
|
93 METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
|
nuclear@1
|
94 JBLOCKROW *MCU_data));
|
nuclear@1
|
95 METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
|
nuclear@1
|
96 #endif
|
nuclear@1
|
97
|
nuclear@1
|
98
|
nuclear@1
|
99 /*
|
nuclear@1
|
100 * Initialize for a Huffman-compressed scan.
|
nuclear@1
|
101 * If gather_statistics is TRUE, we do not output anything during the scan,
|
nuclear@1
|
102 * just count the Huffman symbols used and generate Huffman code tables.
|
nuclear@1
|
103 */
|
nuclear@1
|
104
|
nuclear@1
|
105 METHODDEF(void)
|
nuclear@1
|
106 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
|
nuclear@1
|
107 {
|
nuclear@1
|
108 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
109 int ci, dctbl, actbl;
|
nuclear@1
|
110 jpeg_component_info * compptr;
|
nuclear@1
|
111
|
nuclear@1
|
112 if (gather_statistics) {
|
nuclear@1
|
113 #ifdef ENTROPY_OPT_SUPPORTED
|
nuclear@1
|
114 entropy->pub.encode_mcu = encode_mcu_gather;
|
nuclear@1
|
115 entropy->pub.finish_pass = finish_pass_gather;
|
nuclear@1
|
116 #else
|
nuclear@1
|
117 ERREXIT(cinfo, JERR_NOT_COMPILED);
|
nuclear@1
|
118 #endif
|
nuclear@1
|
119 } else {
|
nuclear@1
|
120 entropy->pub.encode_mcu = encode_mcu_huff;
|
nuclear@1
|
121 entropy->pub.finish_pass = finish_pass_huff;
|
nuclear@1
|
122 }
|
nuclear@1
|
123
|
nuclear@1
|
124 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
nuclear@1
|
125 compptr = cinfo->cur_comp_info[ci];
|
nuclear@1
|
126 dctbl = compptr->dc_tbl_no;
|
nuclear@1
|
127 actbl = compptr->ac_tbl_no;
|
nuclear@1
|
128 if (gather_statistics) {
|
nuclear@1
|
129 #ifdef ENTROPY_OPT_SUPPORTED
|
nuclear@1
|
130 /* Check for invalid table indexes */
|
nuclear@1
|
131 /* (make_c_derived_tbl does this in the other path) */
|
nuclear@1
|
132 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
|
nuclear@1
|
133 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
|
nuclear@1
|
134 if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
|
nuclear@1
|
135 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
|
nuclear@1
|
136 /* Allocate and zero the statistics tables */
|
nuclear@1
|
137 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
|
nuclear@1
|
138 if (entropy->dc_count_ptrs[dctbl] == NULL)
|
nuclear@1
|
139 entropy->dc_count_ptrs[dctbl] = (long *)
|
nuclear@1
|
140 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@1
|
141 257 * SIZEOF(long));
|
nuclear@1
|
142 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
|
nuclear@1
|
143 if (entropy->ac_count_ptrs[actbl] == NULL)
|
nuclear@1
|
144 entropy->ac_count_ptrs[actbl] = (long *)
|
nuclear@1
|
145 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@1
|
146 257 * SIZEOF(long));
|
nuclear@1
|
147 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
|
nuclear@1
|
148 #endif
|
nuclear@1
|
149 } else {
|
nuclear@1
|
150 /* Compute derived values for Huffman tables */
|
nuclear@1
|
151 /* We may do this more than once for a table, but it's not expensive */
|
nuclear@1
|
152 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
|
nuclear@1
|
153 & entropy->dc_derived_tbls[dctbl]);
|
nuclear@1
|
154 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
|
nuclear@1
|
155 & entropy->ac_derived_tbls[actbl]);
|
nuclear@1
|
156 }
|
nuclear@1
|
157 /* Initialize DC predictions to 0 */
|
nuclear@1
|
158 entropy->saved.last_dc_val[ci] = 0;
|
nuclear@1
|
159 }
|
nuclear@1
|
160
|
nuclear@1
|
161 /* Initialize bit buffer to empty */
|
nuclear@1
|
162 entropy->saved.put_buffer = 0;
|
nuclear@1
|
163 entropy->saved.put_bits = 0;
|
nuclear@1
|
164
|
nuclear@1
|
165 /* Initialize restart stuff */
|
nuclear@1
|
166 entropy->restarts_to_go = cinfo->restart_interval;
|
nuclear@1
|
167 entropy->next_restart_num = 0;
|
nuclear@1
|
168 }
|
nuclear@1
|
169
|
nuclear@1
|
170
|
nuclear@1
|
171 /*
|
nuclear@1
|
172 * Compute the derived values for a Huffman table.
|
nuclear@1
|
173 * This routine also performs some validation checks on the table.
|
nuclear@1
|
174 *
|
nuclear@1
|
175 * Note this is also used by jcphuff.c.
|
nuclear@1
|
176 */
|
nuclear@1
|
177
|
nuclear@1
|
178 GLOBAL(void)
|
nuclear@1
|
179 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
|
nuclear@1
|
180 c_derived_tbl ** pdtbl)
|
nuclear@1
|
181 {
|
nuclear@1
|
182 JHUFF_TBL *htbl;
|
nuclear@1
|
183 c_derived_tbl *dtbl;
|
nuclear@1
|
184 int p, i, l, lastp, si, maxsymbol;
|
nuclear@1
|
185 char huffsize[257];
|
nuclear@1
|
186 unsigned int huffcode[257];
|
nuclear@1
|
187 unsigned int code;
|
nuclear@1
|
188
|
nuclear@1
|
189 /* Note that huffsize[] and huffcode[] are filled in code-length order,
|
nuclear@1
|
190 * paralleling the order of the symbols themselves in htbl->huffval[].
|
nuclear@1
|
191 */
|
nuclear@1
|
192
|
nuclear@1
|
193 /* Find the input Huffman table */
|
nuclear@1
|
194 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
|
nuclear@1
|
195 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
nuclear@1
|
196 htbl =
|
nuclear@1
|
197 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
|
nuclear@1
|
198 if (htbl == NULL)
|
nuclear@1
|
199 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
nuclear@1
|
200
|
nuclear@1
|
201 /* Allocate a workspace if we haven't already done so. */
|
nuclear@1
|
202 if (*pdtbl == NULL)
|
nuclear@1
|
203 *pdtbl = (c_derived_tbl *)
|
nuclear@1
|
204 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@1
|
205 SIZEOF(c_derived_tbl));
|
nuclear@1
|
206 dtbl = *pdtbl;
|
nuclear@1
|
207
|
nuclear@1
|
208 /* Figure C.1: make table of Huffman code length for each symbol */
|
nuclear@1
|
209
|
nuclear@1
|
210 p = 0;
|
nuclear@1
|
211 for (l = 1; l <= 16; l++) {
|
nuclear@1
|
212 i = (int) htbl->bits[l];
|
nuclear@1
|
213 if (i < 0 || p + i > 256) /* protect against table overrun */
|
nuclear@1
|
214 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
nuclear@1
|
215 while (i--)
|
nuclear@1
|
216 huffsize[p++] = (char) l;
|
nuclear@1
|
217 }
|
nuclear@1
|
218 huffsize[p] = 0;
|
nuclear@1
|
219 lastp = p;
|
nuclear@1
|
220
|
nuclear@1
|
221 /* Figure C.2: generate the codes themselves */
|
nuclear@1
|
222 /* We also validate that the counts represent a legal Huffman code tree. */
|
nuclear@1
|
223
|
nuclear@1
|
224 code = 0;
|
nuclear@1
|
225 si = huffsize[0];
|
nuclear@1
|
226 p = 0;
|
nuclear@1
|
227 while (huffsize[p]) {
|
nuclear@1
|
228 while (((int) huffsize[p]) == si) {
|
nuclear@1
|
229 huffcode[p++] = code;
|
nuclear@1
|
230 code++;
|
nuclear@1
|
231 }
|
nuclear@1
|
232 /* code is now 1 more than the last code used for codelength si; but
|
nuclear@1
|
233 * it must still fit in si bits, since no code is allowed to be all ones.
|
nuclear@1
|
234 */
|
nuclear@1
|
235 if (((INT32) code) >= (((INT32) 1) << si))
|
nuclear@1
|
236 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
nuclear@1
|
237 code <<= 1;
|
nuclear@1
|
238 si++;
|
nuclear@1
|
239 }
|
nuclear@1
|
240
|
nuclear@1
|
241 /* Figure C.3: generate encoding tables */
|
nuclear@1
|
242 /* These are code and size indexed by symbol value */
|
nuclear@1
|
243
|
nuclear@1
|
244 /* Set all codeless symbols to have code length 0;
|
nuclear@1
|
245 * this lets us detect duplicate VAL entries here, and later
|
nuclear@1
|
246 * allows emit_bits to detect any attempt to emit such symbols.
|
nuclear@1
|
247 */
|
nuclear@1
|
248 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
|
nuclear@1
|
249
|
nuclear@1
|
250 /* This is also a convenient place to check for out-of-range
|
nuclear@1
|
251 * and duplicated VAL entries. We allow 0..255 for AC symbols
|
nuclear@1
|
252 * but only 0..15 for DC. (We could constrain them further
|
nuclear@1
|
253 * based on data depth and mode, but this seems enough.)
|
nuclear@1
|
254 */
|
nuclear@1
|
255 maxsymbol = isDC ? 15 : 255;
|
nuclear@1
|
256
|
nuclear@1
|
257 for (p = 0; p < lastp; p++) {
|
nuclear@1
|
258 i = htbl->huffval[p];
|
nuclear@1
|
259 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
|
nuclear@1
|
260 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
nuclear@1
|
261 dtbl->ehufco[i] = huffcode[p];
|
nuclear@1
|
262 dtbl->ehufsi[i] = huffsize[p];
|
nuclear@1
|
263 }
|
nuclear@1
|
264 }
|
nuclear@1
|
265
|
nuclear@1
|
266
|
nuclear@1
|
267 /* Outputting bytes to the file */
|
nuclear@1
|
268
|
nuclear@1
|
269 /* Emit a byte, taking 'action' if must suspend. */
|
nuclear@1
|
270 #define emit_byte(state,val,action) \
|
nuclear@1
|
271 { *(state)->next_output_byte++ = (JOCTET) (val); \
|
nuclear@1
|
272 if (--(state)->free_in_buffer == 0) \
|
nuclear@1
|
273 if (! dump_buffer(state)) \
|
nuclear@1
|
274 { action; } }
|
nuclear@1
|
275
|
nuclear@1
|
276
|
nuclear@1
|
277 LOCAL(boolean)
|
nuclear@1
|
278 dump_buffer (working_state * state)
|
nuclear@1
|
279 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
|
nuclear@1
|
280 {
|
nuclear@1
|
281 struct jpeg_destination_mgr * dest = state->cinfo->dest;
|
nuclear@1
|
282
|
nuclear@1
|
283 if (! (*dest->empty_output_buffer) (state->cinfo))
|
nuclear@1
|
284 return FALSE;
|
nuclear@1
|
285 /* After a successful buffer dump, must reset buffer pointers */
|
nuclear@1
|
286 state->next_output_byte = dest->next_output_byte;
|
nuclear@1
|
287 state->free_in_buffer = dest->free_in_buffer;
|
nuclear@1
|
288 return TRUE;
|
nuclear@1
|
289 }
|
nuclear@1
|
290
|
nuclear@1
|
291
|
nuclear@1
|
292 /* Outputting bits to the file */
|
nuclear@1
|
293
|
nuclear@1
|
294 /* Only the right 24 bits of put_buffer are used; the valid bits are
|
nuclear@1
|
295 * left-justified in this part. At most 16 bits can be passed to emit_bits
|
nuclear@1
|
296 * in one call, and we never retain more than 7 bits in put_buffer
|
nuclear@1
|
297 * between calls, so 24 bits are sufficient.
|
nuclear@1
|
298 */
|
nuclear@1
|
299
|
nuclear@1
|
300 INLINE
|
nuclear@1
|
301 LOCAL(boolean)
|
nuclear@1
|
302 emit_bits (working_state * state, unsigned int code, int size)
|
nuclear@1
|
303 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
|
nuclear@1
|
304 {
|
nuclear@1
|
305 /* This routine is heavily used, so it's worth coding tightly. */
|
nuclear@1
|
306 register INT32 put_buffer = (INT32) code;
|
nuclear@1
|
307 register int put_bits = state->cur.put_bits;
|
nuclear@1
|
308
|
nuclear@1
|
309 /* if size is 0, caller used an invalid Huffman table entry */
|
nuclear@1
|
310 if (size == 0)
|
nuclear@1
|
311 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
|
nuclear@1
|
312
|
nuclear@1
|
313 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
|
nuclear@1
|
314
|
nuclear@1
|
315 put_bits += size; /* new number of bits in buffer */
|
nuclear@1
|
316
|
nuclear@1
|
317 put_buffer <<= 24 - put_bits; /* align incoming bits */
|
nuclear@1
|
318
|
nuclear@1
|
319 put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
|
nuclear@1
|
320
|
nuclear@1
|
321 while (put_bits >= 8) {
|
nuclear@1
|
322 int c = (int) ((put_buffer >> 16) & 0xFF);
|
nuclear@1
|
323
|
nuclear@1
|
324 emit_byte(state, c, return FALSE);
|
nuclear@1
|
325 if (c == 0xFF) { /* need to stuff a zero byte? */
|
nuclear@1
|
326 emit_byte(state, 0, return FALSE);
|
nuclear@1
|
327 }
|
nuclear@1
|
328 put_buffer <<= 8;
|
nuclear@1
|
329 put_bits -= 8;
|
nuclear@1
|
330 }
|
nuclear@1
|
331
|
nuclear@1
|
332 state->cur.put_buffer = put_buffer; /* update state variables */
|
nuclear@1
|
333 state->cur.put_bits = put_bits;
|
nuclear@1
|
334
|
nuclear@1
|
335 return TRUE;
|
nuclear@1
|
336 }
|
nuclear@1
|
337
|
nuclear@1
|
338
|
nuclear@1
|
339 LOCAL(boolean)
|
nuclear@1
|
340 flush_bits (working_state * state)
|
nuclear@1
|
341 {
|
nuclear@1
|
342 if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
|
nuclear@1
|
343 return FALSE;
|
nuclear@1
|
344 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
|
nuclear@1
|
345 state->cur.put_bits = 0;
|
nuclear@1
|
346 return TRUE;
|
nuclear@1
|
347 }
|
nuclear@1
|
348
|
nuclear@1
|
349
|
nuclear@1
|
350 /* Encode a single block's worth of coefficients */
|
nuclear@1
|
351
|
nuclear@1
|
352 LOCAL(boolean)
|
nuclear@1
|
353 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
|
nuclear@1
|
354 c_derived_tbl *dctbl, c_derived_tbl *actbl)
|
nuclear@1
|
355 {
|
nuclear@1
|
356 register int temp, temp2;
|
nuclear@1
|
357 register int nbits;
|
nuclear@1
|
358 register int k, r, i;
|
nuclear@1
|
359
|
nuclear@1
|
360 /* Encode the DC coefficient difference per section F.1.2.1 */
|
nuclear@1
|
361
|
nuclear@1
|
362 temp = temp2 = block[0] - last_dc_val;
|
nuclear@1
|
363
|
nuclear@1
|
364 if (temp < 0) {
|
nuclear@1
|
365 temp = -temp; /* temp is abs value of input */
|
nuclear@1
|
366 /* For a negative input, want temp2 = bitwise complement of abs(input) */
|
nuclear@1
|
367 /* This code assumes we are on a two's complement machine */
|
nuclear@1
|
368 temp2--;
|
nuclear@1
|
369 }
|
nuclear@1
|
370
|
nuclear@1
|
371 /* Find the number of bits needed for the magnitude of the coefficient */
|
nuclear@1
|
372 nbits = 0;
|
nuclear@1
|
373 while (temp) {
|
nuclear@1
|
374 nbits++;
|
nuclear@1
|
375 temp >>= 1;
|
nuclear@1
|
376 }
|
nuclear@1
|
377 /* Check for out-of-range coefficient values.
|
nuclear@1
|
378 * Since we're encoding a difference, the range limit is twice as much.
|
nuclear@1
|
379 */
|
nuclear@1
|
380 if (nbits > MAX_COEF_BITS+1)
|
nuclear@1
|
381 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
|
nuclear@1
|
382
|
nuclear@1
|
383 /* Emit the Huffman-coded symbol for the number of bits */
|
nuclear@1
|
384 if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
|
nuclear@1
|
385 return FALSE;
|
nuclear@1
|
386
|
nuclear@1
|
387 /* Emit that number of bits of the value, if positive, */
|
nuclear@1
|
388 /* or the complement of its magnitude, if negative. */
|
nuclear@1
|
389 if (nbits) /* emit_bits rejects calls with size 0 */
|
nuclear@1
|
390 if (! emit_bits(state, (unsigned int) temp2, nbits))
|
nuclear@1
|
391 return FALSE;
|
nuclear@1
|
392
|
nuclear@1
|
393 /* Encode the AC coefficients per section F.1.2.2 */
|
nuclear@1
|
394
|
nuclear@1
|
395 r = 0; /* r = run length of zeros */
|
nuclear@1
|
396
|
nuclear@1
|
397 for (k = 1; k < DCTSIZE2; k++) {
|
nuclear@1
|
398 if ((temp = block[jpeg_natural_order[k]]) == 0) {
|
nuclear@1
|
399 r++;
|
nuclear@1
|
400 } else {
|
nuclear@1
|
401 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
|
nuclear@1
|
402 while (r > 15) {
|
nuclear@1
|
403 if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
|
nuclear@1
|
404 return FALSE;
|
nuclear@1
|
405 r -= 16;
|
nuclear@1
|
406 }
|
nuclear@1
|
407
|
nuclear@1
|
408 temp2 = temp;
|
nuclear@1
|
409 if (temp < 0) {
|
nuclear@1
|
410 temp = -temp; /* temp is abs value of input */
|
nuclear@1
|
411 /* This code assumes we are on a two's complement machine */
|
nuclear@1
|
412 temp2--;
|
nuclear@1
|
413 }
|
nuclear@1
|
414
|
nuclear@1
|
415 /* Find the number of bits needed for the magnitude of the coefficient */
|
nuclear@1
|
416 nbits = 1; /* there must be at least one 1 bit */
|
nuclear@1
|
417 while ((temp >>= 1))
|
nuclear@1
|
418 nbits++;
|
nuclear@1
|
419 /* Check for out-of-range coefficient values */
|
nuclear@1
|
420 if (nbits > MAX_COEF_BITS)
|
nuclear@1
|
421 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
|
nuclear@1
|
422
|
nuclear@1
|
423 /* Emit Huffman symbol for run length / number of bits */
|
nuclear@1
|
424 i = (r << 4) + nbits;
|
nuclear@1
|
425 if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
|
nuclear@1
|
426 return FALSE;
|
nuclear@1
|
427
|
nuclear@1
|
428 /* Emit that number of bits of the value, if positive, */
|
nuclear@1
|
429 /* or the complement of its magnitude, if negative. */
|
nuclear@1
|
430 if (! emit_bits(state, (unsigned int) temp2, nbits))
|
nuclear@1
|
431 return FALSE;
|
nuclear@1
|
432
|
nuclear@1
|
433 r = 0;
|
nuclear@1
|
434 }
|
nuclear@1
|
435 }
|
nuclear@1
|
436
|
nuclear@1
|
437 /* If the last coef(s) were zero, emit an end-of-block code */
|
nuclear@1
|
438 if (r > 0)
|
nuclear@1
|
439 if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
|
nuclear@1
|
440 return FALSE;
|
nuclear@1
|
441
|
nuclear@1
|
442 return TRUE;
|
nuclear@1
|
443 }
|
nuclear@1
|
444
|
nuclear@1
|
445
|
nuclear@1
|
446 /*
|
nuclear@1
|
447 * Emit a restart marker & resynchronize predictions.
|
nuclear@1
|
448 */
|
nuclear@1
|
449
|
nuclear@1
|
450 LOCAL(boolean)
|
nuclear@1
|
451 emit_restart (working_state * state, int restart_num)
|
nuclear@1
|
452 {
|
nuclear@1
|
453 int ci;
|
nuclear@1
|
454
|
nuclear@1
|
455 if (! flush_bits(state))
|
nuclear@1
|
456 return FALSE;
|
nuclear@1
|
457
|
nuclear@1
|
458 emit_byte(state, 0xFF, return FALSE);
|
nuclear@1
|
459 emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
|
nuclear@1
|
460
|
nuclear@1
|
461 /* Re-initialize DC predictions to 0 */
|
nuclear@1
|
462 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
|
nuclear@1
|
463 state->cur.last_dc_val[ci] = 0;
|
nuclear@1
|
464
|
nuclear@1
|
465 /* The restart counter is not updated until we successfully write the MCU. */
|
nuclear@1
|
466
|
nuclear@1
|
467 return TRUE;
|
nuclear@1
|
468 }
|
nuclear@1
|
469
|
nuclear@1
|
470
|
nuclear@1
|
471 /*
|
nuclear@1
|
472 * Encode and output one MCU's worth of Huffman-compressed coefficients.
|
nuclear@1
|
473 */
|
nuclear@1
|
474
|
nuclear@1
|
475 METHODDEF(boolean)
|
nuclear@1
|
476 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
nuclear@1
|
477 {
|
nuclear@1
|
478 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
479 working_state state;
|
nuclear@1
|
480 int blkn, ci;
|
nuclear@1
|
481 jpeg_component_info * compptr;
|
nuclear@1
|
482
|
nuclear@1
|
483 /* Load up working state */
|
nuclear@1
|
484 state.next_output_byte = cinfo->dest->next_output_byte;
|
nuclear@1
|
485 state.free_in_buffer = cinfo->dest->free_in_buffer;
|
nuclear@1
|
486 ASSIGN_STATE(state.cur, entropy->saved);
|
nuclear@1
|
487 state.cinfo = cinfo;
|
nuclear@1
|
488
|
nuclear@1
|
489 /* Emit restart marker if needed */
|
nuclear@1
|
490 if (cinfo->restart_interval) {
|
nuclear@1
|
491 if (entropy->restarts_to_go == 0)
|
nuclear@1
|
492 if (! emit_restart(&state, entropy->next_restart_num))
|
nuclear@1
|
493 return FALSE;
|
nuclear@1
|
494 }
|
nuclear@1
|
495
|
nuclear@1
|
496 /* Encode the MCU data blocks */
|
nuclear@1
|
497 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
nuclear@1
|
498 ci = cinfo->MCU_membership[blkn];
|
nuclear@1
|
499 compptr = cinfo->cur_comp_info[ci];
|
nuclear@1
|
500 if (! encode_one_block(&state,
|
nuclear@1
|
501 MCU_data[blkn][0], state.cur.last_dc_val[ci],
|
nuclear@1
|
502 entropy->dc_derived_tbls[compptr->dc_tbl_no],
|
nuclear@1
|
503 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
|
nuclear@1
|
504 return FALSE;
|
nuclear@1
|
505 /* Update last_dc_val */
|
nuclear@1
|
506 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
|
nuclear@1
|
507 }
|
nuclear@1
|
508
|
nuclear@1
|
509 /* Completed MCU, so update state */
|
nuclear@1
|
510 cinfo->dest->next_output_byte = state.next_output_byte;
|
nuclear@1
|
511 cinfo->dest->free_in_buffer = state.free_in_buffer;
|
nuclear@1
|
512 ASSIGN_STATE(entropy->saved, state.cur);
|
nuclear@1
|
513
|
nuclear@1
|
514 /* Update restart-interval state too */
|
nuclear@1
|
515 if (cinfo->restart_interval) {
|
nuclear@1
|
516 if (entropy->restarts_to_go == 0) {
|
nuclear@1
|
517 entropy->restarts_to_go = cinfo->restart_interval;
|
nuclear@1
|
518 entropy->next_restart_num++;
|
nuclear@1
|
519 entropy->next_restart_num &= 7;
|
nuclear@1
|
520 }
|
nuclear@1
|
521 entropy->restarts_to_go--;
|
nuclear@1
|
522 }
|
nuclear@1
|
523
|
nuclear@1
|
524 return TRUE;
|
nuclear@1
|
525 }
|
nuclear@1
|
526
|
nuclear@1
|
527
|
nuclear@1
|
528 /*
|
nuclear@1
|
529 * Finish up at the end of a Huffman-compressed scan.
|
nuclear@1
|
530 */
|
nuclear@1
|
531
|
nuclear@1
|
532 METHODDEF(void)
|
nuclear@1
|
533 finish_pass_huff (j_compress_ptr cinfo)
|
nuclear@1
|
534 {
|
nuclear@1
|
535 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
536 working_state state;
|
nuclear@1
|
537
|
nuclear@1
|
538 /* Load up working state ... flush_bits needs it */
|
nuclear@1
|
539 state.next_output_byte = cinfo->dest->next_output_byte;
|
nuclear@1
|
540 state.free_in_buffer = cinfo->dest->free_in_buffer;
|
nuclear@1
|
541 ASSIGN_STATE(state.cur, entropy->saved);
|
nuclear@1
|
542 state.cinfo = cinfo;
|
nuclear@1
|
543
|
nuclear@1
|
544 /* Flush out the last data */
|
nuclear@1
|
545 if (! flush_bits(&state))
|
nuclear@1
|
546 ERREXIT(cinfo, JERR_CANT_SUSPEND);
|
nuclear@1
|
547
|
nuclear@1
|
548 /* Update state */
|
nuclear@1
|
549 cinfo->dest->next_output_byte = state.next_output_byte;
|
nuclear@1
|
550 cinfo->dest->free_in_buffer = state.free_in_buffer;
|
nuclear@1
|
551 ASSIGN_STATE(entropy->saved, state.cur);
|
nuclear@1
|
552 }
|
nuclear@1
|
553
|
nuclear@1
|
554
|
nuclear@1
|
555 /*
|
nuclear@1
|
556 * Huffman coding optimization.
|
nuclear@1
|
557 *
|
nuclear@1
|
558 * We first scan the supplied data and count the number of uses of each symbol
|
nuclear@1
|
559 * that is to be Huffman-coded. (This process MUST agree with the code above.)
|
nuclear@1
|
560 * Then we build a Huffman coding tree for the observed counts.
|
nuclear@1
|
561 * Symbols which are not needed at all for the particular image are not
|
nuclear@1
|
562 * assigned any code, which saves space in the DHT marker as well as in
|
nuclear@1
|
563 * the compressed data.
|
nuclear@1
|
564 */
|
nuclear@1
|
565
|
nuclear@1
|
566 #ifdef ENTROPY_OPT_SUPPORTED
|
nuclear@1
|
567
|
nuclear@1
|
568
|
nuclear@1
|
569 /* Process a single block's worth of coefficients */
|
nuclear@1
|
570
|
nuclear@1
|
571 LOCAL(void)
|
nuclear@1
|
572 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
|
nuclear@1
|
573 long dc_counts[], long ac_counts[])
|
nuclear@1
|
574 {
|
nuclear@1
|
575 register int temp;
|
nuclear@1
|
576 register int nbits;
|
nuclear@1
|
577 register int k, r;
|
nuclear@1
|
578
|
nuclear@1
|
579 /* Encode the DC coefficient difference per section F.1.2.1 */
|
nuclear@1
|
580
|
nuclear@1
|
581 temp = block[0] - last_dc_val;
|
nuclear@1
|
582 if (temp < 0)
|
nuclear@1
|
583 temp = -temp;
|
nuclear@1
|
584
|
nuclear@1
|
585 /* Find the number of bits needed for the magnitude of the coefficient */
|
nuclear@1
|
586 nbits = 0;
|
nuclear@1
|
587 while (temp) {
|
nuclear@1
|
588 nbits++;
|
nuclear@1
|
589 temp >>= 1;
|
nuclear@1
|
590 }
|
nuclear@1
|
591 /* Check for out-of-range coefficient values.
|
nuclear@1
|
592 * Since we're encoding a difference, the range limit is twice as much.
|
nuclear@1
|
593 */
|
nuclear@1
|
594 if (nbits > MAX_COEF_BITS+1)
|
nuclear@1
|
595 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
|
nuclear@1
|
596
|
nuclear@1
|
597 /* Count the Huffman symbol for the number of bits */
|
nuclear@1
|
598 dc_counts[nbits]++;
|
nuclear@1
|
599
|
nuclear@1
|
600 /* Encode the AC coefficients per section F.1.2.2 */
|
nuclear@1
|
601
|
nuclear@1
|
602 r = 0; /* r = run length of zeros */
|
nuclear@1
|
603
|
nuclear@1
|
604 for (k = 1; k < DCTSIZE2; k++) {
|
nuclear@1
|
605 if ((temp = block[jpeg_natural_order[k]]) == 0) {
|
nuclear@1
|
606 r++;
|
nuclear@1
|
607 } else {
|
nuclear@1
|
608 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
|
nuclear@1
|
609 while (r > 15) {
|
nuclear@1
|
610 ac_counts[0xF0]++;
|
nuclear@1
|
611 r -= 16;
|
nuclear@1
|
612 }
|
nuclear@1
|
613
|
nuclear@1
|
614 /* Find the number of bits needed for the magnitude of the coefficient */
|
nuclear@1
|
615 if (temp < 0)
|
nuclear@1
|
616 temp = -temp;
|
nuclear@1
|
617
|
nuclear@1
|
618 /* Find the number of bits needed for the magnitude of the coefficient */
|
nuclear@1
|
619 nbits = 1; /* there must be at least one 1 bit */
|
nuclear@1
|
620 while ((temp >>= 1))
|
nuclear@1
|
621 nbits++;
|
nuclear@1
|
622 /* Check for out-of-range coefficient values */
|
nuclear@1
|
623 if (nbits > MAX_COEF_BITS)
|
nuclear@1
|
624 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
|
nuclear@1
|
625
|
nuclear@1
|
626 /* Count Huffman symbol for run length / number of bits */
|
nuclear@1
|
627 ac_counts[(r << 4) + nbits]++;
|
nuclear@1
|
628
|
nuclear@1
|
629 r = 0;
|
nuclear@1
|
630 }
|
nuclear@1
|
631 }
|
nuclear@1
|
632
|
nuclear@1
|
633 /* If the last coef(s) were zero, emit an end-of-block code */
|
nuclear@1
|
634 if (r > 0)
|
nuclear@1
|
635 ac_counts[0]++;
|
nuclear@1
|
636 }
|
nuclear@1
|
637
|
nuclear@1
|
638
|
nuclear@1
|
639 /*
|
nuclear@1
|
640 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
|
nuclear@1
|
641 * No data is actually output, so no suspension return is possible.
|
nuclear@1
|
642 */
|
nuclear@1
|
643
|
nuclear@1
|
644 METHODDEF(boolean)
|
nuclear@1
|
645 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
nuclear@1
|
646 {
|
nuclear@1
|
647 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
648 int blkn, ci;
|
nuclear@1
|
649 jpeg_component_info * compptr;
|
nuclear@1
|
650
|
nuclear@1
|
651 /* Take care of restart intervals if needed */
|
nuclear@1
|
652 if (cinfo->restart_interval) {
|
nuclear@1
|
653 if (entropy->restarts_to_go == 0) {
|
nuclear@1
|
654 /* Re-initialize DC predictions to 0 */
|
nuclear@1
|
655 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
|
nuclear@1
|
656 entropy->saved.last_dc_val[ci] = 0;
|
nuclear@1
|
657 /* Update restart state */
|
nuclear@1
|
658 entropy->restarts_to_go = cinfo->restart_interval;
|
nuclear@1
|
659 }
|
nuclear@1
|
660 entropy->restarts_to_go--;
|
nuclear@1
|
661 }
|
nuclear@1
|
662
|
nuclear@1
|
663 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
nuclear@1
|
664 ci = cinfo->MCU_membership[blkn];
|
nuclear@1
|
665 compptr = cinfo->cur_comp_info[ci];
|
nuclear@1
|
666 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
|
nuclear@1
|
667 entropy->dc_count_ptrs[compptr->dc_tbl_no],
|
nuclear@1
|
668 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
|
nuclear@1
|
669 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
|
nuclear@1
|
670 }
|
nuclear@1
|
671
|
nuclear@1
|
672 return TRUE;
|
nuclear@1
|
673 }
|
nuclear@1
|
674
|
nuclear@1
|
675
|
nuclear@1
|
676 /*
|
nuclear@1
|
677 * Generate the best Huffman code table for the given counts, fill htbl.
|
nuclear@1
|
678 * Note this is also used by jcphuff.c.
|
nuclear@1
|
679 *
|
nuclear@1
|
680 * The JPEG standard requires that no symbol be assigned a codeword of all
|
nuclear@1
|
681 * one bits (so that padding bits added at the end of a compressed segment
|
nuclear@1
|
682 * can't look like a valid code). Because of the canonical ordering of
|
nuclear@1
|
683 * codewords, this just means that there must be an unused slot in the
|
nuclear@1
|
684 * longest codeword length category. Section K.2 of the JPEG spec suggests
|
nuclear@1
|
685 * reserving such a slot by pretending that symbol 256 is a valid symbol
|
nuclear@1
|
686 * with count 1. In theory that's not optimal; giving it count zero but
|
nuclear@1
|
687 * including it in the symbol set anyway should give a better Huffman code.
|
nuclear@1
|
688 * But the theoretically better code actually seems to come out worse in
|
nuclear@1
|
689 * practice, because it produces more all-ones bytes (which incur stuffed
|
nuclear@1
|
690 * zero bytes in the final file). In any case the difference is tiny.
|
nuclear@1
|
691 *
|
nuclear@1
|
692 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
|
nuclear@1
|
693 * If some symbols have a very small but nonzero probability, the Huffman tree
|
nuclear@1
|
694 * must be adjusted to meet the code length restriction. We currently use
|
nuclear@1
|
695 * the adjustment method suggested in JPEG section K.2. This method is *not*
|
nuclear@1
|
696 * optimal; it may not choose the best possible limited-length code. But
|
nuclear@1
|
697 * typically only very-low-frequency symbols will be given less-than-optimal
|
nuclear@1
|
698 * lengths, so the code is almost optimal. Experimental comparisons against
|
nuclear@1
|
699 * an optimal limited-length-code algorithm indicate that the difference is
|
nuclear@1
|
700 * microscopic --- usually less than a hundredth of a percent of total size.
|
nuclear@1
|
701 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
|
nuclear@1
|
702 */
|
nuclear@1
|
703
|
nuclear@1
|
704 GLOBAL(void)
|
nuclear@1
|
705 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
nuclear@1
|
706 {
|
nuclear@1
|
707 #define MAX_CLEN 32 /* assumed maximum initial code length */
|
nuclear@1
|
708 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
|
nuclear@1
|
709 int codesize[257]; /* codesize[k] = code length of symbol k */
|
nuclear@1
|
710 int others[257]; /* next symbol in current branch of tree */
|
nuclear@1
|
711 int c1, c2;
|
nuclear@1
|
712 int p, i, j;
|
nuclear@1
|
713 long v;
|
nuclear@1
|
714
|
nuclear@1
|
715 /* This algorithm is explained in section K.2 of the JPEG standard */
|
nuclear@1
|
716
|
nuclear@1
|
717 MEMZERO(bits, SIZEOF(bits));
|
nuclear@1
|
718 MEMZERO(codesize, SIZEOF(codesize));
|
nuclear@1
|
719 for (i = 0; i < 257; i++)
|
nuclear@1
|
720 others[i] = -1; /* init links to empty */
|
nuclear@1
|
721
|
nuclear@1
|
722 freq[256] = 1; /* make sure 256 has a nonzero count */
|
nuclear@1
|
723 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
|
nuclear@1
|
724 * that no real symbol is given code-value of all ones, because 256
|
nuclear@1
|
725 * will be placed last in the largest codeword category.
|
nuclear@1
|
726 */
|
nuclear@1
|
727
|
nuclear@1
|
728 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
|
nuclear@1
|
729
|
nuclear@1
|
730 for (;;) {
|
nuclear@1
|
731 /* Find the smallest nonzero frequency, set c1 = its symbol */
|
nuclear@1
|
732 /* In case of ties, take the larger symbol number */
|
nuclear@1
|
733 c1 = -1;
|
nuclear@1
|
734 v = 1000000000L;
|
nuclear@1
|
735 for (i = 0; i <= 256; i++) {
|
nuclear@1
|
736 if (freq[i] && freq[i] <= v) {
|
nuclear@1
|
737 v = freq[i];
|
nuclear@1
|
738 c1 = i;
|
nuclear@1
|
739 }
|
nuclear@1
|
740 }
|
nuclear@1
|
741
|
nuclear@1
|
742 /* Find the next smallest nonzero frequency, set c2 = its symbol */
|
nuclear@1
|
743 /* In case of ties, take the larger symbol number */
|
nuclear@1
|
744 c2 = -1;
|
nuclear@1
|
745 v = 1000000000L;
|
nuclear@1
|
746 for (i = 0; i <= 256; i++) {
|
nuclear@1
|
747 if (freq[i] && freq[i] <= v && i != c1) {
|
nuclear@1
|
748 v = freq[i];
|
nuclear@1
|
749 c2 = i;
|
nuclear@1
|
750 }
|
nuclear@1
|
751 }
|
nuclear@1
|
752
|
nuclear@1
|
753 /* Done if we've merged everything into one frequency */
|
nuclear@1
|
754 if (c2 < 0)
|
nuclear@1
|
755 break;
|
nuclear@1
|
756
|
nuclear@1
|
757 /* Else merge the two counts/trees */
|
nuclear@1
|
758 freq[c1] += freq[c2];
|
nuclear@1
|
759 freq[c2] = 0;
|
nuclear@1
|
760
|
nuclear@1
|
761 /* Increment the codesize of everything in c1's tree branch */
|
nuclear@1
|
762 codesize[c1]++;
|
nuclear@1
|
763 while (others[c1] >= 0) {
|
nuclear@1
|
764 c1 = others[c1];
|
nuclear@1
|
765 codesize[c1]++;
|
nuclear@1
|
766 }
|
nuclear@1
|
767
|
nuclear@1
|
768 others[c1] = c2; /* chain c2 onto c1's tree branch */
|
nuclear@1
|
769
|
nuclear@1
|
770 /* Increment the codesize of everything in c2's tree branch */
|
nuclear@1
|
771 codesize[c2]++;
|
nuclear@1
|
772 while (others[c2] >= 0) {
|
nuclear@1
|
773 c2 = others[c2];
|
nuclear@1
|
774 codesize[c2]++;
|
nuclear@1
|
775 }
|
nuclear@1
|
776 }
|
nuclear@1
|
777
|
nuclear@1
|
778 /* Now count the number of symbols of each code length */
|
nuclear@1
|
779 for (i = 0; i <= 256; i++) {
|
nuclear@1
|
780 if (codesize[i]) {
|
nuclear@1
|
781 /* The JPEG standard seems to think that this can't happen, */
|
nuclear@1
|
782 /* but I'm paranoid... */
|
nuclear@1
|
783 if (codesize[i] > MAX_CLEN)
|
nuclear@1
|
784 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
|
nuclear@1
|
785
|
nuclear@1
|
786 bits[codesize[i]]++;
|
nuclear@1
|
787 }
|
nuclear@1
|
788 }
|
nuclear@1
|
789
|
nuclear@1
|
790 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
|
nuclear@1
|
791 * Huffman procedure assigned any such lengths, we must adjust the coding.
|
nuclear@1
|
792 * Here is what the JPEG spec says about how this next bit works:
|
nuclear@1
|
793 * Since symbols are paired for the longest Huffman code, the symbols are
|
nuclear@1
|
794 * removed from this length category two at a time. The prefix for the pair
|
nuclear@1
|
795 * (which is one bit shorter) is allocated to one of the pair; then,
|
nuclear@1
|
796 * skipping the BITS entry for that prefix length, a code word from the next
|
nuclear@1
|
797 * shortest nonzero BITS entry is converted into a prefix for two code words
|
nuclear@1
|
798 * one bit longer.
|
nuclear@1
|
799 */
|
nuclear@1
|
800
|
nuclear@1
|
801 for (i = MAX_CLEN; i > 16; i--) {
|
nuclear@1
|
802 while (bits[i] > 0) {
|
nuclear@1
|
803 j = i - 2; /* find length of new prefix to be used */
|
nuclear@1
|
804 while (bits[j] == 0)
|
nuclear@1
|
805 j--;
|
nuclear@1
|
806
|
nuclear@1
|
807 bits[i] -= 2; /* remove two symbols */
|
nuclear@1
|
808 bits[i-1]++; /* one goes in this length */
|
nuclear@1
|
809 bits[j+1] += 2; /* two new symbols in this length */
|
nuclear@1
|
810 bits[j]--; /* symbol of this length is now a prefix */
|
nuclear@1
|
811 }
|
nuclear@1
|
812 }
|
nuclear@1
|
813
|
nuclear@1
|
814 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
|
nuclear@1
|
815 while (bits[i] == 0) /* find largest codelength still in use */
|
nuclear@1
|
816 i--;
|
nuclear@1
|
817 bits[i]--;
|
nuclear@1
|
818
|
nuclear@1
|
819 /* Return final symbol counts (only for lengths 0..16) */
|
nuclear@1
|
820 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
|
nuclear@1
|
821
|
nuclear@1
|
822 /* Return a list of the symbols sorted by code length */
|
nuclear@1
|
823 /* It's not real clear to me why we don't need to consider the codelength
|
nuclear@1
|
824 * changes made above, but the JPEG spec seems to think this works.
|
nuclear@1
|
825 */
|
nuclear@1
|
826 p = 0;
|
nuclear@1
|
827 for (i = 1; i <= MAX_CLEN; i++) {
|
nuclear@1
|
828 for (j = 0; j <= 255; j++) {
|
nuclear@1
|
829 if (codesize[j] == i) {
|
nuclear@1
|
830 htbl->huffval[p] = (UINT8) j;
|
nuclear@1
|
831 p++;
|
nuclear@1
|
832 }
|
nuclear@1
|
833 }
|
nuclear@1
|
834 }
|
nuclear@1
|
835
|
nuclear@1
|
836 /* Set sent_table FALSE so updated table will be written to JPEG file. */
|
nuclear@1
|
837 htbl->sent_table = FALSE;
|
nuclear@1
|
838 }
|
nuclear@1
|
839
|
nuclear@1
|
840
|
nuclear@1
|
841 /*
|
nuclear@1
|
842 * Finish up a statistics-gathering pass and create the new Huffman tables.
|
nuclear@1
|
843 */
|
nuclear@1
|
844
|
nuclear@1
|
845 METHODDEF(void)
|
nuclear@1
|
846 finish_pass_gather (j_compress_ptr cinfo)
|
nuclear@1
|
847 {
|
nuclear@1
|
848 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
nuclear@1
|
849 int ci, dctbl, actbl;
|
nuclear@1
|
850 jpeg_component_info * compptr;
|
nuclear@1
|
851 JHUFF_TBL **htblptr;
|
nuclear@1
|
852 boolean did_dc[NUM_HUFF_TBLS];
|
nuclear@1
|
853 boolean did_ac[NUM_HUFF_TBLS];
|
nuclear@1
|
854
|
nuclear@1
|
855 /* It's important not to apply jpeg_gen_optimal_table more than once
|
nuclear@1
|
856 * per table, because it clobbers the input frequency counts!
|
nuclear@1
|
857 */
|
nuclear@1
|
858 MEMZERO(did_dc, SIZEOF(did_dc));
|
nuclear@1
|
859 MEMZERO(did_ac, SIZEOF(did_ac));
|
nuclear@1
|
860
|
nuclear@1
|
861 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
nuclear@1
|
862 compptr = cinfo->cur_comp_info[ci];
|
nuclear@1
|
863 dctbl = compptr->dc_tbl_no;
|
nuclear@1
|
864 actbl = compptr->ac_tbl_no;
|
nuclear@1
|
865 if (! did_dc[dctbl]) {
|
nuclear@1
|
866 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
|
nuclear@1
|
867 if (*htblptr == NULL)
|
nuclear@1
|
868 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
|
nuclear@1
|
869 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
|
nuclear@1
|
870 did_dc[dctbl] = TRUE;
|
nuclear@1
|
871 }
|
nuclear@1
|
872 if (! did_ac[actbl]) {
|
nuclear@1
|
873 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
|
nuclear@1
|
874 if (*htblptr == NULL)
|
nuclear@1
|
875 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
|
nuclear@1
|
876 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
|
nuclear@1
|
877 did_ac[actbl] = TRUE;
|
nuclear@1
|
878 }
|
nuclear@1
|
879 }
|
nuclear@1
|
880 }
|
nuclear@1
|
881
|
nuclear@1
|
882
|
nuclear@1
|
883 #endif /* ENTROPY_OPT_SUPPORTED */
|
nuclear@1
|
884
|
nuclear@1
|
885
|
nuclear@1
|
886 /*
|
nuclear@1
|
887 * Module initialization routine for Huffman entropy encoding.
|
nuclear@1
|
888 */
|
nuclear@1
|
889
|
nuclear@1
|
890 GLOBAL(void)
|
nuclear@1
|
891 jinit_huff_encoder (j_compress_ptr cinfo)
|
nuclear@1
|
892 {
|
nuclear@1
|
893 huff_entropy_ptr entropy;
|
nuclear@1
|
894 int i;
|
nuclear@1
|
895
|
nuclear@1
|
896 entropy = (huff_entropy_ptr)
|
nuclear@1
|
897 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@1
|
898 SIZEOF(huff_entropy_encoder));
|
nuclear@1
|
899 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
|
nuclear@1
|
900 entropy->pub.start_pass = start_pass_huff;
|
nuclear@1
|
901
|
nuclear@1
|
902 /* Mark tables unallocated */
|
nuclear@1
|
903 for (i = 0; i < NUM_HUFF_TBLS; i++) {
|
nuclear@1
|
904 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
|
nuclear@1
|
905 #ifdef ENTROPY_OPT_SUPPORTED
|
nuclear@1
|
906 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
|
nuclear@1
|
907 #endif
|
nuclear@1
|
908 }
|
nuclear@1
|
909 }
|