1 /*
2 * jcshuff.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 encoding routines for sequential JPEG.
9 *
10 * Much of the complexity here has to do with supporting output suspension.
11 * If the data destination module demands suspension, we want to be able to
12 * back up to the start of the current MCU. To do this, we copy state
13 * variables into local working storage, and update them back to the
14 * permanent JPEG objects only upon successful completion of an MCU.
15 */
16
17 #define JPEG_INTERNALS
18 #include "jinclude8.h"
19 #include "jpeglib8.h"
20 #include "jlossy8.h" /* Private declarations for lossy codec */
21 #include "jchuff8.h" /* Declarations shared with jc*huff.c */
22
23
24 /* Expanded entropy encoder object for Huffman encoding.
25 *
26 * The savable_state subrecord contains fields that change within an MCU,
27 * but must not be updated permanently until we complete the MCU.
28 */
29
30 typedef struct {
31 IJG_INT32 put_buffer; /* current bit-accumulation buffer */
32 int put_bits; /* # of bits now in it */
33 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
34 } savable_state;
35
36 /* This macro is to work around compilers with missing or broken
37 * structure assignment. You'll need to fix this code if you have
38 * such a compiler and you change MAX_COMPS_IN_SCAN.
39 */
40
41 #ifndef NO_STRUCT_ASSIGN
42 #define ASSIGN_STATE(dest,src) ((dest) = (src))
43 #else
44 #if MAX_COMPS_IN_SCAN == 4
45 #define ASSIGN_STATE(dest,src) \
46 ((dest).put_buffer = (src).put_buffer, \
47 (dest).put_bits = (src).put_bits, \
48 (dest).last_dc_val[0] = (src).last_dc_val[0], \
49 (dest).last_dc_val[1] = (src).last_dc_val[1], \
50 (dest).last_dc_val[2] = (src).last_dc_val[2], \
51 (dest).last_dc_val[3] = (src).last_dc_val[3])
52 #endif
53 #endif
54
55
56 typedef struct {
57 savable_state saved; /* Bit buffer & DC state at start of MCU */
58
59 /* These fields are NOT loaded into local working state. */
60 unsigned int restarts_to_go; /* MCUs left in this restart interval */
61 int next_restart_num; /* next restart number to write (0-7) */
62
63 /* Pointers to derived tables (these workspaces have image lifespan) */
64 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
65 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
66
67 #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
68 long * dc_count_ptrs[NUM_HUFF_TBLS];
69 long * ac_count_ptrs[NUM_HUFF_TBLS];
70 #endif
71 } shuff_entropy_encoder;
72
73 typedef shuff_entropy_encoder * shuff_entropy_ptr;
74
75 /* Working state while writing an MCU.
76 * This struct contains all the fields that are needed by subroutines.
77 */
78
79 typedef struct {
80 JOCTET * next_output_byte; /* => next byte to write in buffer */
81 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
82 savable_state cur; /* Current bit buffer & DC state */
83 j_compress_ptr cinfo; /* dump_buffer needs access to this */
84 } working_state;
85
86
87 /* Forward declarations */
88 METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
89 JBLOCKROW *MCU_data));
90 METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
91 #ifdef ENTROPY_OPT_SUPPORTED
92 METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
93 JBLOCKROW *MCU_data));
94 METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
95 #endif
96
97
98 /*
99 * Initialize for a Huffman-compressed scan.
100 * If gather_statistics is TRUE, we do not output anything during the scan,
101 * just count the Huffman symbols used and generate Huffman code tables.
102 */
103
104 METHODDEF(void)
start_pass_huff(j_compress_ptr cinfo,boolean gather_statistics)105 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
106 {
107 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
108 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
109 int ci, dctbl, actbl;
110 jpeg_component_info * compptr;
111
112 if (gather_statistics) {
113 #ifdef ENTROPY_OPT_SUPPORTED
114 lossyc->entropy_encode_mcu = encode_mcu_gather;
115 lossyc->pub.entropy_finish_pass = finish_pass_gather;
116 #else
117 ERREXIT(cinfo, JERR_NOT_COMPILED);
118 #endif
119 } else {
120 lossyc->entropy_encode_mcu = encode_mcu_huff;
121 lossyc->pub.entropy_finish_pass = finish_pass_huff;
122 }
123
124 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
125 compptr = cinfo->cur_comp_info[ci];
126 dctbl = compptr->dc_tbl_no;
127 actbl = compptr->ac_tbl_no;
128 if (gather_statistics) {
129 #ifdef ENTROPY_OPT_SUPPORTED
130 /* Check for invalid table indexes */
131 /* (make_c_derived_tbl does this in the other path) */
132 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
133 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
134 if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
135 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
136 /* Allocate and zero the statistics tables */
137 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
138 if (entropy->dc_count_ptrs[dctbl] == NULL)
139 entropy->dc_count_ptrs[dctbl] = (long *)
140 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
141 257 * SIZEOF(long));
142 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
143 if (entropy->ac_count_ptrs[actbl] == NULL)
144 entropy->ac_count_ptrs[actbl] = (long *)
145 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
146 257 * SIZEOF(long));
147 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
148 #endif
149 } else {
150 /* Compute derived values for Huffman tables */
151 /* We may do this more than once for a table, but it's not expensive */
152 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
153 & entropy->dc_derived_tbls[dctbl]);
154 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
155 & entropy->ac_derived_tbls[actbl]);
156 }
157 /* Initialize DC predictions to 0 */
158 entropy->saved.last_dc_val[ci] = 0;
159 }
160
161 /* Initialize bit buffer to empty */
162 entropy->saved.put_buffer = 0;
163 entropy->saved.put_bits = 0;
164
165 /* Initialize restart stuff */
166 entropy->restarts_to_go = cinfo->restart_interval;
167 entropy->next_restart_num = 0;
168 }
169
170
171 /* Outputting bytes to the file */
172
173 /* Emit a byte, taking 'action' if must suspend. */
174 #define emit_byte(state,val,action) \
175 { *(state)->next_output_byte++ = (JOCTET) (val); \
176 if (--(state)->free_in_buffer == 0) \
177 if (! dump_buffer(state)) \
178 { action; } }
179
180
181 LOCAL(boolean)
dump_buffer(working_state * state)182 dump_buffer (working_state * state)
183 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
184 {
185 struct jpeg_destination_mgr * dest = state->cinfo->dest;
186
187 if (! (*dest->empty_output_buffer) (state->cinfo))
188 return FALSE;
189 /* After a successful buffer dump, must reset buffer pointers */
190 state->next_output_byte = dest->next_output_byte;
191 state->free_in_buffer = dest->free_in_buffer;
192 return TRUE;
193 }
194
195
196 /* Outputting bits to the file */
197
198 /* Only the right 24 bits of put_buffer are used; the valid bits are
199 * left-justified in this part. At most 16 bits can be passed to emit_bits
200 * in one call, and we never retain more than 7 bits in put_buffer
201 * between calls, so 24 bits are sufficient.
202 */
203
204 INLINE
LOCAL(boolean)205 LOCAL(boolean)
206 emit_bits (working_state * state, unsigned int code, int size)
207 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
208 {
209 /* This routine is heavily used, so it's worth coding tightly. */
210 register IJG_INT32 put_buffer = (IJG_INT32) code;
211 register int put_bits = state->cur.put_bits;
212
213 /* if size is 0, caller used an invalid Huffman table entry */
214 if (size == 0)
215 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
216
217 put_buffer &= (((IJG_INT32) 1)<<size) - 1; /* mask off any extra bits in code */
218
219 put_bits += size; /* new number of bits in buffer */
220
221 put_buffer <<= 24 - put_bits; /* align incoming bits */
222
223 put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
224
225 while (put_bits >= 8) {
226 int c = (int) ((put_buffer >> 16) & 0xFF);
227
228 emit_byte(state, c, return FALSE);
229 if (c == 0xFF) { /* need to stuff a zero byte? */
230 emit_byte(state, 0, return FALSE);
231 }
232 put_buffer <<= 8;
233 put_bits -= 8;
234 }
235
236 state->cur.put_buffer = put_buffer; /* update state variables */
237 state->cur.put_bits = put_bits;
238
239 return TRUE;
240 }
241
242
243 LOCAL(boolean)
flush_bits(working_state * state)244 flush_bits (working_state * state)
245 {
246 if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
247 return FALSE;
248 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
249 state->cur.put_bits = 0;
250 return TRUE;
251 }
252
253
254 /* Encode a single block's worth of coefficients */
255
256 LOCAL(boolean)
encode_one_block(working_state * state,const JCOEFPTR block,int last_dc_val,c_derived_tbl * dctbl,c_derived_tbl * actbl)257 encode_one_block (working_state * state, const JCOEFPTR block, int last_dc_val,
258 c_derived_tbl *dctbl, c_derived_tbl *actbl)
259 {
260 register int temp, temp2;
261 register int nbits;
262 register int k, r, i;
263
264 /* Encode the DC coefficient difference per section F.1.2.1 */
265
266 temp = temp2 = block[0] - last_dc_val;
267
268 if (temp < 0) {
269 temp = -temp; /* temp is abs value of input */
270 /* For a negative input, want temp2 = bitwise complement of abs(input) */
271 /* This code assumes we are on a two's complement machine */
272 temp2--;
273 }
274
275 /* Find the number of bits needed for the magnitude of the coefficient */
276 nbits = 0;
277 while (temp) {
278 nbits++;
279 temp >>= 1;
280 }
281 /* Check for out-of-range coefficient values.
282 * Since we're encoding a difference, the range limit is twice as much.
283 */
284 if (nbits > MAX_COEF_BITS+1)
285 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
286
287 /* Emit the Huffman-coded symbol for the number of bits */
288 if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
289 return FALSE;
290
291 /* Emit that number of bits of the value, if positive, */
292 /* or the complement of its magnitude, if negative. */
293 if (nbits) /* emit_bits rejects calls with size 0 */
294 if (! emit_bits(state, (unsigned int) temp2, nbits))
295 return FALSE;
296
297 /* Encode the AC coefficients per section F.1.2.2 */
298
299 r = 0; /* r = run length of zeros */
300
301 for (k = 1; k < DCTSIZE2; k++) {
302 if ((temp = block[jpeg_natural_order[k]]) == 0) {
303 r++;
304 } else {
305 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
306 while (r > 15) {
307 if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
308 return FALSE;
309 r -= 16;
310 }
311
312 temp2 = temp;
313 if (temp < 0) {
314 temp = -temp; /* temp is abs value of input */
315 /* This code assumes we are on a two's complement machine */
316 temp2--;
317 }
318
319 /* Find the number of bits needed for the magnitude of the coefficient */
320 nbits = 1; /* there must be at least one 1 bit */
321 while ((temp >>= 1))
322 nbits++;
323 /* Check for out-of-range coefficient values */
324 if (nbits > MAX_COEF_BITS)
325 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
326
327 /* Emit Huffman symbol for run length / number of bits */
328 i = (r << 4) + nbits;
329 if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
330 return FALSE;
331
332 /* Emit that number of bits of the value, if positive, */
333 /* or the complement of its magnitude, if negative. */
334 if (! emit_bits(state, (unsigned int) temp2, nbits))
335 return FALSE;
336
337 r = 0;
338 }
339 }
340
341 /* If the last coef(s) were zero, emit an end-of-block code */
342 if (r > 0)
343 if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
344 return FALSE;
345
346 return TRUE;
347 }
348
349
350 /*
351 * Emit a restart marker & resynchronize predictions.
352 */
353
354 LOCAL(boolean)
emit_restart(working_state * state,int restart_num)355 emit_restart (working_state * state, int restart_num)
356 {
357 int ci;
358
359 if (! flush_bits(state))
360 return FALSE;
361
362 emit_byte(state, 0xFF, return FALSE);
363 emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
364
365 /* Re-initialize DC predictions to 0 */
366 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
367 state->cur.last_dc_val[ci] = 0;
368
369 /* The restart counter is not updated until we successfully write the MCU. */
370
371 return TRUE;
372 }
373
374
375 /*
376 * Encode and output one MCU's worth of Huffman-compressed coefficients.
377 */
378
379 METHODDEF(boolean)
encode_mcu_huff(j_compress_ptr cinfo,JBLOCKROW * MCU_data)380 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
381 {
382 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
383 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
384 working_state state;
385 int blkn, ci;
386 jpeg_component_info * compptr;
387
388 /* Load up working state */
389 state.next_output_byte = cinfo->dest->next_output_byte;
390 state.free_in_buffer = cinfo->dest->free_in_buffer;
391 ASSIGN_STATE(state.cur, entropy->saved);
392 state.cinfo = cinfo;
393
394 /* Emit restart marker if needed */
395 if (cinfo->restart_interval) {
396 if (entropy->restarts_to_go == 0)
397 if (! emit_restart(&state, entropy->next_restart_num))
398 return FALSE;
399 }
400
401 /* Encode the MCU data blocks */
402 for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
403 ci = cinfo->MCU_membership[blkn];
404 compptr = cinfo->cur_comp_info[ci];
405 if (! encode_one_block(&state,
406 MCU_data[blkn][0], state.cur.last_dc_val[ci],
407 entropy->dc_derived_tbls[compptr->dc_tbl_no],
408 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
409 return FALSE;
410 /* Update last_dc_val */
411 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
412 }
413
414 /* Completed MCU, so update state */
415 cinfo->dest->next_output_byte = state.next_output_byte;
416 cinfo->dest->free_in_buffer = state.free_in_buffer;
417 ASSIGN_STATE(entropy->saved, state.cur);
418
419 /* Update restart-interval state too */
420 if (cinfo->restart_interval) {
421 if (entropy->restarts_to_go == 0) {
422 entropy->restarts_to_go = cinfo->restart_interval;
423 entropy->next_restart_num++;
424 entropy->next_restart_num &= 7;
425 }
426 entropy->restarts_to_go--;
427 }
428
429 return TRUE;
430 }
431
432
433 /*
434 * Finish up at the end of a Huffman-compressed scan.
435 */
436
437 METHODDEF(void)
finish_pass_huff(j_compress_ptr cinfo)438 finish_pass_huff (j_compress_ptr cinfo)
439 {
440 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
441 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
442 working_state state;
443
444 /* Load up working state ... flush_bits needs it */
445 state.next_output_byte = cinfo->dest->next_output_byte;
446 state.free_in_buffer = cinfo->dest->free_in_buffer;
447 ASSIGN_STATE(state.cur, entropy->saved);
448 state.cinfo = cinfo;
449
450 /* Flush out the last data */
451 if (! flush_bits(&state))
452 ERREXIT(cinfo, JERR_CANT_SUSPEND);
453
454 /* Update state */
455 cinfo->dest->next_output_byte = state.next_output_byte;
456 cinfo->dest->free_in_buffer = state.free_in_buffer;
457 ASSIGN_STATE(entropy->saved, state.cur);
458 }
459
460
461 /*
462 * Huffman coding optimization.
463 *
464 * We first scan the supplied data and count the number of uses of each symbol
465 * that is to be Huffman-coded. (This process MUST agree with the code above.)
466 * Then we build a Huffman coding tree for the observed counts.
467 * Symbols which are not needed at all for the particular image are not
468 * assigned any code, which saves space in the DHT marker as well as in
469 * the compressed data.
470 */
471
472 #ifdef ENTROPY_OPT_SUPPORTED
473
474
475 /* Process a single block's worth of coefficients */
476
477 LOCAL(void)
htest_one_block(j_compress_ptr cinfo,const JCOEFPTR block,int last_dc_val,long dc_counts[],long ac_counts[])478 htest_one_block (j_compress_ptr cinfo, const JCOEFPTR block, int last_dc_val,
479 long dc_counts[], long ac_counts[])
480 {
481 register int temp;
482 register int nbits;
483 register int k, r;
484
485 /* Encode the DC coefficient difference per section F.1.2.1 */
486
487 temp = block[0] - last_dc_val;
488 if (temp < 0)
489 temp = -temp;
490
491 /* Find the number of bits needed for the magnitude of the coefficient */
492 nbits = 0;
493 while (temp) {
494 nbits++;
495 temp >>= 1;
496 }
497 /* Check for out-of-range coefficient values.
498 * Since we're encoding a difference, the range limit is twice as much.
499 */
500 if (nbits > MAX_COEF_BITS+1)
501 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
502
503 /* Count the Huffman symbol for the number of bits */
504 dc_counts[nbits]++;
505
506 /* Encode the AC coefficients per section F.1.2.2 */
507
508 r = 0; /* r = run length of zeros */
509
510 for (k = 1; k < DCTSIZE2; k++) {
511 if ((temp = block[jpeg_natural_order[k]]) == 0) {
512 r++;
513 } else {
514 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
515 while (r > 15) {
516 ac_counts[0xF0]++;
517 r -= 16;
518 }
519
520 /* Find the number of bits needed for the magnitude of the coefficient */
521 if (temp < 0)
522 temp = -temp;
523
524 /* Find the number of bits needed for the magnitude of the coefficient */
525 nbits = 1; /* there must be at least one 1 bit */
526 while ((temp >>= 1))
527 nbits++;
528 /* Check for out-of-range coefficient values */
529 if (nbits > MAX_COEF_BITS)
530 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
531
532 /* Count Huffman symbol for run length / number of bits */
533 ac_counts[(r << 4) + nbits]++;
534
535 r = 0;
536 }
537 }
538
539 /* If the last coef(s) were zero, emit an end-of-block code */
540 if (r > 0)
541 ac_counts[0]++;
542 }
543
544
545 /*
546 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
547 * No data is actually output, so no suspension return is possible.
548 */
549
550 METHODDEF(boolean)
encode_mcu_gather(j_compress_ptr cinfo,JBLOCKROW * MCU_data)551 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
552 {
553 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
554 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
555 int blkn, ci;
556 jpeg_component_info * compptr;
557
558 /* Take care of restart intervals if needed */
559 if (cinfo->restart_interval) {
560 if (entropy->restarts_to_go == 0) {
561 /* Re-initialize DC predictions to 0 */
562 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
563 entropy->saved.last_dc_val[ci] = 0;
564 /* Update restart state */
565 entropy->restarts_to_go = cinfo->restart_interval;
566 }
567 entropy->restarts_to_go--;
568 }
569
570 for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
571 ci = cinfo->MCU_membership[blkn];
572 compptr = cinfo->cur_comp_info[ci];
573 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
574 entropy->dc_count_ptrs[compptr->dc_tbl_no],
575 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
576 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
577 }
578
579 return TRUE;
580 }
581
582
583 /*
584 * Finish up a statistics-gathering pass and create the new Huffman tables.
585 */
586
587 METHODDEF(void)
finish_pass_gather(j_compress_ptr cinfo)588 finish_pass_gather (j_compress_ptr cinfo)
589 {
590 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
591 shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
592 int ci, dctbl, actbl;
593 jpeg_component_info * compptr;
594 JHUFF_TBL **htblptr;
595 boolean did_dc[NUM_HUFF_TBLS];
596 boolean did_ac[NUM_HUFF_TBLS];
597
598 /* It's important not to apply jpeg_gen_optimal_table more than once
599 * per table, because it clobbers the input frequency counts!
600 */
601 MEMZERO(did_dc, SIZEOF(did_dc));
602 MEMZERO(did_ac, SIZEOF(did_ac));
603
604 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
605 compptr = cinfo->cur_comp_info[ci];
606 dctbl = compptr->dc_tbl_no;
607 actbl = compptr->ac_tbl_no;
608 if (! did_dc[dctbl]) {
609 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
610 if (*htblptr == NULL)
611 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
612 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
613 did_dc[dctbl] = TRUE;
614 }
615 if (! did_ac[actbl]) {
616 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
617 if (*htblptr == NULL)
618 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
619 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
620 did_ac[actbl] = TRUE;
621 }
622 }
623 }
624
625
626 #endif /* ENTROPY_OPT_SUPPORTED */
627
628
629 METHODDEF(boolean)
need_optimization_pass(j_compress_ptr cinfo)630 need_optimization_pass (j_compress_ptr cinfo)
631 {
632 return TRUE;
633 }
634
635
636 /*
637 * Module initialization routine for Huffman entropy encoding.
638 */
639
640 GLOBAL(void)
jinit_shuff_encoder(j_compress_ptr cinfo)641 jinit_shuff_encoder (j_compress_ptr cinfo)
642 {
643 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
644 shuff_entropy_ptr entropy;
645 int i;
646
647 entropy = (shuff_entropy_ptr)
648 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
649 SIZEOF(shuff_entropy_encoder));
650 lossyc->entropy_private = (struct jpeg_entropy_encoder *) entropy;
651 lossyc->pub.entropy_start_pass = start_pass_huff;
652 lossyc->pub.need_optimization_pass = need_optimization_pass;
653
654 /* Mark tables unallocated */
655 for (i = 0; i < NUM_HUFF_TBLS; i++) {
656 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
657 #ifdef ENTROPY_OPT_SUPPORTED
658 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
659 #endif
660 }
661 }
662