Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / DtHelp / jpeg / jdphuff.c
1 /* $XConsortium: jdphuff.c /main/2 1996/05/09 03:49:25 drk $ */
2 /*
3  * jdphuff.c
4  *
5  * Copyright (C) 1995-1996, Thomas G. Lane.
6  * This file is part of the Independent JPEG Group's software.
7  * For conditions of distribution and use, see the accompanying README file.
8  *
9  * This file contains Huffman entropy decoding routines for progressive JPEG.
10  *
11  * Much of the complexity here has to do with supporting input suspension.
12  * If the data source module demands suspension, we want to be able to back
13  * up to the start of the current MCU.  To do this, we copy state variables
14  * into local working storage, and update them back to the permanent
15  * storage only upon successful completion of an MCU.
16  */
17
18 #define JPEG_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jdhuff.h"             /* Declarations shared with jdhuff.c */
22
23
24 #ifdef D_PROGRESSIVE_SUPPORTED
25
26 /*
27  * Expanded entropy decoder object for progressive Huffman decoding.
28  *
29  * The savable_state subrecord contains fields that change within an MCU,
30  * but must not be updated permanently until we complete the MCU.
31  */
32
33 typedef struct {
34   unsigned int EOBRUN;                  /* remaining EOBs in EOBRUN */
35   int last_dc_val[MAX_COMPS_IN_SCAN];   /* last DC coef for each component */
36 } savable_state;
37
38 /* This macro is to work around compilers with missing or broken
39  * structure assignment.  You'll need to fix this code if you have
40  * such a compiler and you change MAX_COMPS_IN_SCAN.
41  */
42
43 #ifndef NO_STRUCT_ASSIGN
44 #define ASSIGN_STATE(dest,src)  ((dest) = (src))
45 #else
46 #if MAX_COMPS_IN_SCAN == 4
47 #define ASSIGN_STATE(dest,src)  \
48         ((dest).EOBRUN = (src).EOBRUN, \
49          (dest).last_dc_val[0] = (src).last_dc_val[0], \
50          (dest).last_dc_val[1] = (src).last_dc_val[1], \
51          (dest).last_dc_val[2] = (src).last_dc_val[2], \
52          (dest).last_dc_val[3] = (src).last_dc_val[3])
53 #endif
54 #endif
55
56
57 typedef struct {
58   struct jpeg_entropy_decoder pub; /* public fields */
59
60   /* These fields are loaded into local variables at start of each MCU.
61    * In case of suspension, we exit WITHOUT updating them.
62    */
63   bitread_perm_state bitstate;  /* Bit buffer at start of MCU */
64   savable_state saved;          /* Other state at start of MCU */
65
66   /* These fields are NOT loaded into local working state. */
67   unsigned int restarts_to_go;  /* MCUs left in this restart interval */
68
69   /* Pointers to derived tables (these workspaces have image lifespan) */
70   d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
71
72   d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
73 } phuff_entropy_decoder;
74
75 typedef phuff_entropy_decoder * phuff_entropy_ptr;
76
77 /* Forward declarations */
78 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
79                                             JBLOCKROW *MCU_data));
80 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
81                                             JBLOCKROW *MCU_data));
82 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
83                                              JBLOCKROW *MCU_data));
84 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
85                                              JBLOCKROW *MCU_data));
86
87
88 /*
89  * Initialize for a Huffman-compressed scan.
90  */
91
92 METHODDEF(void)
93 start_pass_phuff_decoder (j_decompress_ptr cinfo)
94 {
95   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
96   boolean is_DC_band, bad;
97   int ci, coefi, tbl;
98   int *coef_bit_ptr;
99   jpeg_component_info * compptr;
100
101   is_DC_band = (cinfo->Ss == 0);
102
103   /* Validate scan parameters */
104   bad = FALSE;
105   if (is_DC_band) {
106     if (cinfo->Se != 0)
107       bad = TRUE;
108   } else {
109     /* need not check Ss/Se < 0 since they came from unsigned bytes */
110     if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
111       bad = TRUE;
112     /* AC scans may have only one component */
113     if (cinfo->comps_in_scan != 1)
114       bad = TRUE;
115   }
116   if (cinfo->Ah != 0) {
117     /* Successive approximation refinement scan: must have Al = Ah-1. */
118     if (cinfo->Al != cinfo->Ah-1)
119       bad = TRUE;
120   }
121   if (cinfo->Al > 13)           /* need not check for < 0 */
122     bad = TRUE;
123   if (bad)
124     ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
125              cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
126   /* Update progression status, and verify that scan order is legal.
127    * Note that inter-scan inconsistencies are treated as warnings
128    * not fatal errors ... not clear if this is right way to behave.
129    */
130   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
131     int cindex = cinfo->cur_comp_info[ci]->component_index;
132     coef_bit_ptr = & cinfo->coef_bits[cindex][0];
133     if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
134       WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
135     for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
136       int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
137       if (cinfo->Ah != expected)
138         WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
139       coef_bit_ptr[coefi] = cinfo->Al;
140     }
141   }
142
143   /* Select MCU decoding routine */
144   if (cinfo->Ah == 0) {
145     if (is_DC_band)
146       entropy->pub.decode_mcu = decode_mcu_DC_first;
147     else
148       entropy->pub.decode_mcu = decode_mcu_AC_first;
149   } else {
150     if (is_DC_band)
151       entropy->pub.decode_mcu = decode_mcu_DC_refine;
152     else
153       entropy->pub.decode_mcu = decode_mcu_AC_refine;
154   }
155
156   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
157     compptr = cinfo->cur_comp_info[ci];
158     /* Make sure requested tables are present, and compute derived tables.
159      * We may build same derived table more than once, but it's not expensive.
160      */
161     if (is_DC_band) {
162       if (cinfo->Ah == 0) {     /* DC refinement needs no table */
163         tbl = compptr->dc_tbl_no;
164         if (tbl < 0 || tbl >= NUM_HUFF_TBLS ||
165             cinfo->dc_huff_tbl_ptrs[tbl] == NULL)
166           ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
167         jpeg_make_d_derived_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[tbl],
168                                 & entropy->derived_tbls[tbl]);
169       }
170     } else {
171       tbl = compptr->ac_tbl_no;
172       if (tbl < 0 || tbl >= NUM_HUFF_TBLS ||
173           cinfo->ac_huff_tbl_ptrs[tbl] == NULL)
174         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
175       jpeg_make_d_derived_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[tbl],
176                               & entropy->derived_tbls[tbl]);
177       /* remember the single active table */
178       entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
179     }
180     /* Initialize DC predictions to 0 */
181     entropy->saved.last_dc_val[ci] = 0;
182   }
183
184   /* Initialize bitread state variables */
185   entropy->bitstate.bits_left = 0;
186   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
187   entropy->bitstate.printed_eod = FALSE;
188
189   /* Initialize private state variables */
190   entropy->saved.EOBRUN = 0;
191
192   /* Initialize restart counter */
193   entropy->restarts_to_go = cinfo->restart_interval;
194 }
195
196
197 /*
198  * Figure F.12: extend sign bit.
199  * On some machines, a shift and add will be faster than a table lookup.
200  */
201
202 #ifdef AVOID_TABLES
203
204 #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
205
206 #else
207
208 #define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
209
210 static const int extend_test[16] =   /* entry n is 2**(n-1) */
211   { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
212     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
213
214 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
215   { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
216     ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
217     ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
218     ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
219
220 #endif /* AVOID_TABLES */
221
222
223 /*
224  * Check for a restart marker & resynchronize decoder.
225  * Returns FALSE if must suspend.
226  */
227
228 LOCAL(boolean)
229 process_restart (j_decompress_ptr cinfo)
230 {
231   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
232   int ci;
233
234   /* Throw away any unused bits remaining in bit buffer; */
235   /* include any full bytes in next_marker's count of discarded bytes */
236   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
237   entropy->bitstate.bits_left = 0;
238
239   /* Advance past the RSTn marker */
240   if (! (*cinfo->marker->read_restart_marker) (cinfo))
241     return FALSE;
242
243   /* Re-initialize DC predictions to 0 */
244   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
245     entropy->saved.last_dc_val[ci] = 0;
246   /* Re-init EOB run count, too */
247   entropy->saved.EOBRUN = 0;
248
249   /* Reset restart counter */
250   entropy->restarts_to_go = cinfo->restart_interval;
251
252   /* Next segment can get another out-of-data warning */
253   entropy->bitstate.printed_eod = FALSE;
254
255   return TRUE;
256 }
257
258
259 /*
260  * Huffman MCU decoding.
261  * Each of these routines decodes and returns one MCU's worth of
262  * Huffman-compressed coefficients. 
263  * The coefficients are reordered from zigzag order into natural array order,
264  * but are not dequantized.
265  *
266  * The i'th block of the MCU is stored into the block pointed to by
267  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
268  *
269  * We return FALSE if data source requested suspension.  In that case no
270  * changes have been made to permanent state.  (Exception: some output
271  * coefficients may already have been assigned.  This is harmless for
272  * spectral selection, since we'll just re-assign them on the next call.
273  * Successive approximation AC refinement has to be more careful, however.)
274  */
275
276 /*
277  * MCU decoding for DC initial scan (either spectral selection,
278  * or first pass of successive approximation).
279  */
280
281 METHODDEF(boolean)
282 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
283 {   
284   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
285   int Al = cinfo->Al;
286   register int s, r;
287   int blkn, ci;
288   JBLOCKROW block;
289   BITREAD_STATE_VARS;
290   savable_state state;
291   d_derived_tbl * tbl;
292   jpeg_component_info * compptr;
293
294   /* Process restart marker if needed; may have to suspend */
295   if (cinfo->restart_interval) {
296     if (entropy->restarts_to_go == 0)
297       if (! process_restart(cinfo))
298         return FALSE;
299   }
300
301   /* Load up working state */
302   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
303   ASSIGN_STATE(state, entropy->saved);
304
305   /* Outer loop handles each block in the MCU */
306
307   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
308     block = MCU_data[blkn];
309     ci = cinfo->MCU_membership[blkn];
310     compptr = cinfo->cur_comp_info[ci];
311     tbl = entropy->derived_tbls[compptr->dc_tbl_no];
312
313     /* Decode a single block's worth of coefficients */
314
315     /* Section F.2.2.1: decode the DC coefficient difference */
316     HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
317     if (s) {
318       CHECK_BIT_BUFFER(br_state, s, return FALSE);
319       r = GET_BITS(s);
320       s = HUFF_EXTEND(r, s);
321     }
322
323     /* Convert DC difference to actual value, update last_dc_val */
324     s += state.last_dc_val[ci];
325     state.last_dc_val[ci] = s;
326     /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
327     (*block)[0] = (JCOEF) (s << Al);
328   }
329
330   /* Completed MCU, so update state */
331   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
332   ASSIGN_STATE(entropy->saved, state);
333
334   /* Account for restart interval (no-op if not using restarts) */
335   entropy->restarts_to_go--;
336
337   return TRUE;
338 }
339
340
341 /*
342  * MCU decoding for AC initial scan (either spectral selection,
343  * or first pass of successive approximation).
344  */
345
346 METHODDEF(boolean)
347 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
348 {   
349   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
350   int Se = cinfo->Se;
351   int Al = cinfo->Al;
352   register int s, k, r;
353   unsigned int EOBRUN;
354   JBLOCKROW block;
355   BITREAD_STATE_VARS;
356   d_derived_tbl * tbl;
357
358   /* Process restart marker if needed; may have to suspend */
359   if (cinfo->restart_interval) {
360     if (entropy->restarts_to_go == 0)
361       if (! process_restart(cinfo))
362         return FALSE;
363   }
364
365   /* Load up working state.
366    * We can avoid loading/saving bitread state if in an EOB run.
367    */
368   EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we care about */
369
370   /* There is always only one block per MCU */
371
372   if (EOBRUN > 0)               /* if it's a band of zeroes... */
373     EOBRUN--;                   /* ...process it now (we do nothing) */
374   else {
375     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
376     block = MCU_data[0];
377     tbl = entropy->ac_derived_tbl;
378
379     for (k = cinfo->Ss; k <= Se; k++) {
380       HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
381       r = s >> 4;
382       s &= 15;
383       if (s) {
384         k += r;
385         CHECK_BIT_BUFFER(br_state, s, return FALSE);
386         r = GET_BITS(s);
387         s = HUFF_EXTEND(r, s);
388         /* Scale and output coefficient in natural (dezigzagged) order */
389         (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
390       } else {
391         if (r == 15) {          /* ZRL */
392           k += 15;              /* skip 15 zeroes in band */
393         } else {                /* EOBr, run length is 2^r + appended bits */
394           EOBRUN = 1 << r;
395           if (r) {              /* EOBr, r > 0 */
396             CHECK_BIT_BUFFER(br_state, r, return FALSE);
397             r = GET_BITS(r);
398             EOBRUN += r;
399           }
400           EOBRUN--;             /* this band is processed at this moment */
401           break;                /* force end-of-band */
402         }
403       }
404     }
405
406     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
407   }
408
409   /* Completed MCU, so update state */
410   entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we care about */
411
412   /* Account for restart interval (no-op if not using restarts) */
413   entropy->restarts_to_go--;
414
415   return TRUE;
416 }
417
418
419 /*
420  * MCU decoding for DC successive approximation refinement scan.
421  * Note: we assume such scans can be multi-component, although the spec
422  * is not very clear on the point.
423  */
424
425 METHODDEF(boolean)
426 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
427 {   
428   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
429   int p1 = 1 << cinfo->Al;      /* 1 in the bit position being coded */
430   int blkn;
431   JBLOCKROW block;
432   BITREAD_STATE_VARS;
433
434   /* Process restart marker if needed; may have to suspend */
435   if (cinfo->restart_interval) {
436     if (entropy->restarts_to_go == 0)
437       if (! process_restart(cinfo))
438         return FALSE;
439   }
440
441   /* Load up working state */
442   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
443
444   /* Outer loop handles each block in the MCU */
445
446   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
447     block = MCU_data[blkn];
448
449     /* Encoded data is simply the next bit of the two's-complement DC value */
450     CHECK_BIT_BUFFER(br_state, 1, return FALSE);
451     if (GET_BITS(1))
452       (*block)[0] |= p1;
453     /* Note: since we use |=, repeating the assignment later is safe */
454   }
455
456   /* Completed MCU, so update state */
457   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
458
459   /* Account for restart interval (no-op if not using restarts) */
460   entropy->restarts_to_go--;
461
462   return TRUE;
463 }
464
465
466 /*
467  * MCU decoding for AC successive approximation refinement scan.
468  */
469
470 METHODDEF(boolean)
471 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
472 {   
473   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
474   int Se = cinfo->Se;
475   int p1 = 1 << cinfo->Al;      /* 1 in the bit position being coded */
476   int m1 = (-1) << cinfo->Al;   /* -1 in the bit position being coded */
477   register int s, k, r;
478   unsigned int EOBRUN;
479   JBLOCKROW block;
480   JCOEFPTR thiscoef;
481   BITREAD_STATE_VARS;
482   d_derived_tbl * tbl;
483   int num_newnz;
484   int newnz_pos[DCTSIZE2];
485
486   /* Process restart marker if needed; may have to suspend */
487   if (cinfo->restart_interval) {
488     if (entropy->restarts_to_go == 0)
489       if (! process_restart(cinfo))
490         return FALSE;
491   }
492
493   /* Load up working state */
494   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
495   EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we care about */
496
497   /* There is always only one block per MCU */
498   block = MCU_data[0];
499   tbl = entropy->ac_derived_tbl;
500
501   /* If we are forced to suspend, we must undo the assignments to any newly
502    * nonzero coefficients in the block, because otherwise we'd get confused
503    * next time about which coefficients were already nonzero.
504    * But we need not undo addition of bits to already-nonzero coefficients;
505    * instead, we can test the current bit position to see if we already did it.
506    */
507   num_newnz = 0;
508
509   /* initialize coefficient loop counter to start of band */
510   k = cinfo->Ss;
511
512   if (EOBRUN == 0) {
513     for (; k <= Se; k++) {
514       HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
515       r = s >> 4;
516       s &= 15;
517       if (s) {
518         if (s != 1)             /* size of new coef should always be 1 */
519           WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
520         CHECK_BIT_BUFFER(br_state, 1, goto undoit);
521         if (GET_BITS(1))
522           s = p1;               /* newly nonzero coef is positive */
523         else
524           s = m1;               /* newly nonzero coef is negative */
525       } else {
526         if (r != 15) {
527           EOBRUN = 1 << r;      /* EOBr, run length is 2^r + appended bits */
528           if (r) {
529             CHECK_BIT_BUFFER(br_state, r, goto undoit);
530             r = GET_BITS(r);
531             EOBRUN += r;
532           }
533           break;                /* rest of block is handled by EOB logic */
534         }
535         /* note s = 0 for processing ZRL */
536       }
537       /* Advance over already-nonzero coefs and r still-zero coefs,
538        * appending correction bits to the nonzeroes.  A correction bit is 1
539        * if the absolute value of the coefficient must be increased.
540        */
541       do {
542         thiscoef = *block + jpeg_natural_order[k];
543         if (*thiscoef != 0) {
544           CHECK_BIT_BUFFER(br_state, 1, goto undoit);
545           if (GET_BITS(1)) {
546             if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
547               if (*thiscoef >= 0)
548                 *thiscoef += p1;
549               else
550                 *thiscoef += m1;
551             }
552           }
553         } else {
554           if (--r < 0)
555             break;              /* reached target zero coefficient */
556         }
557         k++;
558       } while (k <= Se);
559       if (s) {
560         int pos = jpeg_natural_order[k];
561         /* Output newly nonzero coefficient */
562         (*block)[pos] = (JCOEF) s;
563         /* Remember its position in case we have to suspend */
564         newnz_pos[num_newnz++] = pos;
565       }
566     }
567   }
568
569   if (EOBRUN > 0) {
570     /* Scan any remaining coefficient positions after the end-of-band
571      * (the last newly nonzero coefficient, if any).  Append a correction
572      * bit to each already-nonzero coefficient.  A correction bit is 1
573      * if the absolute value of the coefficient must be increased.
574      */
575     for (; k <= Se; k++) {
576       thiscoef = *block + jpeg_natural_order[k];
577       if (*thiscoef != 0) {
578         CHECK_BIT_BUFFER(br_state, 1, goto undoit);
579         if (GET_BITS(1)) {
580           if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
581             if (*thiscoef >= 0)
582               *thiscoef += p1;
583             else
584               *thiscoef += m1;
585           }
586         }
587       }
588     }
589     /* Count one block completed in EOB run */
590     EOBRUN--;
591   }
592
593   /* Completed MCU, so update state */
594   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
595   entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we care about */
596
597   /* Account for restart interval (no-op if not using restarts) */
598   entropy->restarts_to_go--;
599
600   return TRUE;
601
602 undoit:
603   /* Re-zero any output coefficients that we made newly nonzero */
604   while (num_newnz > 0)
605     (*block)[newnz_pos[--num_newnz]] = 0;
606
607   return FALSE;
608 }
609
610
611 /*
612  * Module initialization routine for progressive Huffman entropy decoding.
613  */
614
615 GLOBAL(void)
616 jinit_phuff_decoder (j_decompress_ptr cinfo)
617 {
618   phuff_entropy_ptr entropy;
619   int *coef_bit_ptr;
620   int ci, i;
621
622   entropy = (phuff_entropy_ptr)
623     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
624                                 SIZEOF(phuff_entropy_decoder));
625   cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
626   entropy->pub.start_pass = start_pass_phuff_decoder;
627
628   /* Mark derived tables unallocated */
629   for (i = 0; i < NUM_HUFF_TBLS; i++) {
630     entropy->derived_tbls[i] = NULL;
631   }
632
633   /* Create progression status table */
634   cinfo->coef_bits = (int (*)[DCTSIZE2])
635     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
636                                 cinfo->num_components*DCTSIZE2*SIZEOF(int));
637   coef_bit_ptr = & cinfo->coef_bits[0][0];
638   for (ci = 0; ci < cinfo->num_components; ci++) 
639     for (i = 0; i < DCTSIZE2; i++)
640       *coef_bit_ptr++ = -1;
641 }
642
643 #endif /* D_PROGRESSIVE_SUPPORTED */