]> Creatis software - gdcm.git/blob - src/gdcmjpeg/jdphuff.c
ENH: Final -hopefully- change to jpeg lib. In order to match ITK structure, and be...
[gdcm.git] / src / gdcmjpeg / jdphuff.c
1 /*
2  * jdphuff.c
3  *
4  * Copyright (C) 1995-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 progressive 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 "jlossy.h"    /* Private declarations for lossy subsystem */
21 #include "jdhuff.h"    /* Declarations shared with jd*huff.c */
22
23
24 #ifdef D_PROGRESSIVE_SUPPORTED
25
26 /*
27  * Private 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   huffd_common_fields;    /* Fields shared with other entropy decoders */
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   savable_state saved;    /* Other state at start of MCU */
64
65   /* These fields are NOT loaded into local working state. */
66   unsigned int restarts_to_go;  /* MCUs left in this restart interval */
67
68   /* Pointers to derived tables (these workspaces have image lifespan) */
69   d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
70
71   d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
72 } phuff_entropy_decoder;
73
74 typedef phuff_entropy_decoder * phuff_entropy_ptr;
75
76 /* Forward declarations */
77 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
78               JBLOCKROW *MCU_data));
79 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
80               JBLOCKROW *MCU_data));
81 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
82                JBLOCKROW *MCU_data));
83 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
84                JBLOCKROW *MCU_data));
85
86
87 /*
88  * Initialize for a Huffman-compressed scan.
89  */
90
91 METHODDEF(void)
92 start_pass_phuff_decoder (j_decompress_ptr cinfo)
93 {
94   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
95   phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
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   /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
124    * but the spec doesn't say so, and we try to be liberal about what we
125    * accept.  Note: large Al values could result in out-of-range DC
126    * coefficients during early scans, leading to bizarre displays due to
127    * overflows in the IDCT math.  But we won't crash.
128    */
129   if (bad)
130     ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
131        cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
132   /* Update progression status, and verify that scan order is legal.
133    * Note that inter-scan inconsistencies are treated as warnings
134    * not fatal errors ... not clear if this is right way to behave.
135    */
136   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
137     int cindex = cinfo->cur_comp_info[ci]->component_index;
138     coef_bit_ptr = & cinfo->coef_bits[cindex][0];
139     if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
140       WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
141     for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
142       int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
143       if (cinfo->Ah != expected)
144   WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
145       coef_bit_ptr[coefi] = cinfo->Al;
146     }
147   }
148
149   /* Select MCU decoding routine */
150   if (cinfo->Ah == 0) {
151     if (is_DC_band)
152       lossyd->entropy_decode_mcu = decode_mcu_DC_first;
153     else
154       lossyd->entropy_decode_mcu = decode_mcu_AC_first;
155   } else {
156     if (is_DC_band)
157       lossyd->entropy_decode_mcu = decode_mcu_DC_refine;
158     else
159       lossyd->entropy_decode_mcu = decode_mcu_AC_refine;
160   }
161
162   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
163     compptr = cinfo->cur_comp_info[ci];
164     /* Make sure requested tables are present, and compute derived tables.
165      * We may build same derived table more than once, but it's not expensive.
166      */
167     if (is_DC_band) {
168       if (cinfo->Ah == 0) {  /* DC refinement needs no table */
169   tbl = compptr->dc_tbl_no;
170   jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
171         & entropy->derived_tbls[tbl]);
172       }
173     } else {
174       tbl = compptr->ac_tbl_no;
175       jpeg_make_d_derived_tbl(cinfo, FALSE, 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->insufficient_data = 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   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
232   phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
233   int ci;
234
235   /* Throw away any unused bits remaining in bit buffer; */
236   /* include any full bytes in next_marker's count of discarded bytes */
237   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
238   entropy->bitstate.bits_left = 0;
239
240   /* Advance past the RSTn marker */
241   if (! (*cinfo->marker->read_restart_marker) (cinfo))
242     return FALSE;
243
244   /* Re-initialize DC predictions to 0 */
245   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
246     entropy->saved.last_dc_val[ci] = 0;
247   /* Re-init EOB run count, too */
248   entropy->saved.EOBRUN = 0;
249
250   /* Reset restart counter */
251   entropy->restarts_to_go = cinfo->restart_interval;
252
253   /* Reset out-of-data flag, unless read_restart_marker left us smack up
254    * against a marker.  In that case we will end up treating the next data
255    * segment as empty, and we can avoid producing bogus output pixels by
256    * leaving the flag set.
257    */
258   if (cinfo->unread_marker == 0)
259     entropy->insufficient_data = FALSE;
260
261   return TRUE;
262 }
263
264
265 /*
266  * Huffman MCU decoding.
267  * Each of these routines decodes and returns one MCU's worth of
268  * Huffman-compressed coefficients. 
269  * The coefficients are reordered from zigzag order into natural array order,
270  * but are not dequantized.
271  *
272  * The i'th block of the MCU is stored into the block pointed to by
273  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
274  *
275  * We return FALSE if data source requested suspension.  In that case no
276  * changes have been made to permanent state.  (Exception: some output
277  * coefficients may already have been assigned.  This is harmless for
278  * spectral selection, since we'll just re-assign them on the next call.
279  * Successive approximation AC refinement has to be more careful, however.)
280  */
281
282 /*
283  * MCU decoding for DC initial scan (either spectral selection,
284  * or first pass of successive approximation).
285  */
286
287 METHODDEF(boolean)
288 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
289 {   
290   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
291   phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
292   int Al = cinfo->Al;
293   register int s, r;
294   int blkn, ci;
295   JBLOCKROW block;
296   BITREAD_STATE_VARS;
297   savable_state state;
298   d_derived_tbl * tbl;
299   jpeg_component_info * compptr;
300
301   /* Process restart marker if needed; may have to suspend */
302   if (cinfo->restart_interval) {
303     if (entropy->restarts_to_go == 0)
304       if (! process_restart(cinfo))
305   return FALSE;
306   }
307
308   /* If we've run out of data, just leave the MCU set to zeroes.
309    * This way, we return uniform gray for the remainder of the segment.
310    */
311   if (! entropy->insufficient_data) {
312
313     /* Load up working state */
314     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
315     ASSIGN_STATE(state, entropy->saved);
316
317     /* Outer loop handles each block in the MCU */
318
319     for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
320       block = MCU_data[blkn];
321       ci = cinfo->MCU_membership[blkn];
322       compptr = cinfo->cur_comp_info[ci];
323       tbl = entropy->derived_tbls[compptr->dc_tbl_no];
324
325       /* Decode a single block's worth of coefficients */
326
327       /* Section F.2.2.1: decode the DC coefficient difference */
328       HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
329       if (s) {
330   CHECK_BIT_BUFFER(br_state, s, return FALSE);
331   r = GET_BITS(s);
332   s = HUFF_EXTEND(r, s);
333       }
334
335       /* Convert DC difference to actual value, update last_dc_val */
336       s += state.last_dc_val[ci];
337       state.last_dc_val[ci] = s;
338       /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
339       (*block)[0] = (JCOEF) (s << Al);
340     }
341
342     /* Completed MCU, so update state */
343     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
344     ASSIGN_STATE(entropy->saved, state);
345   }
346
347   /* Account for restart interval (no-op if not using restarts) */
348   entropy->restarts_to_go--;
349
350   return TRUE;
351 }
352
353
354 /*
355  * MCU decoding for AC initial scan (either spectral selection,
356  * or first pass of successive approximation).
357  */
358
359 METHODDEF(boolean)
360 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
361 {   
362   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
363   phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
364   int Se = cinfo->Se;
365   int Al = cinfo->Al;
366   register int s, k, r;
367   unsigned int EOBRUN;
368   JBLOCKROW block;
369   BITREAD_STATE_VARS;
370   d_derived_tbl * tbl;
371
372   /* Process restart marker if needed; may have to suspend */
373   if (cinfo->restart_interval) {
374     if (entropy->restarts_to_go == 0)
375       if (! process_restart(cinfo))
376   return FALSE;
377   }
378
379   /* If we've run out of data, just leave the MCU set to zeroes.
380    * This way, we return uniform gray for the remainder of the segment.
381    */
382   if (! entropy->insufficient_data) {
383
384     /* Load up working state.
385      * We can avoid loading/saving bitread state if in an EOB run.
386      */
387     EOBRUN = entropy->saved.EOBRUN;  /* only part of saved state we need */
388
389     /* There is always only one block per MCU */
390
391     if (EOBRUN > 0)    /* if it's a band of zeroes... */
392       EOBRUN--;      /* ...process it now (we do nothing) */
393     else {
394       BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
395       block = MCU_data[0];
396       tbl = entropy->ac_derived_tbl;
397
398       for (k = cinfo->Ss; k <= Se; k++) {
399   HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
400   r = s >> 4;
401   s &= 15;
402   if (s) {
403     k += r;
404     CHECK_BIT_BUFFER(br_state, s, return FALSE);
405     r = GET_BITS(s);
406     s = HUFF_EXTEND(r, s);
407     /* Scale and output coefficient in natural (dezigzagged) order */
408     (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
409   } else {
410     if (r == 15) {  /* ZRL */
411       k += 15;    /* skip 15 zeroes in band */
412     } else {    /* EOBr, run length is 2^r + appended bits */
413       EOBRUN = 1 << r;
414       if (r) {    /* EOBr, r > 0 */
415         CHECK_BIT_BUFFER(br_state, r, return FALSE);
416         r = GET_BITS(r);
417         EOBRUN += r;
418       }
419       EOBRUN--;    /* this band is processed at this moment */
420       break;    /* force end-of-band */
421     }
422   }
423       }
424
425       BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
426     }
427
428     /* Completed MCU, so update state */
429     entropy->saved.EOBRUN = EOBRUN;  /* only part of saved state we need */
430   }
431
432   /* Account for restart interval (no-op if not using restarts) */
433   entropy->restarts_to_go--;
434
435   return TRUE;
436 }
437
438
439 /*
440  * MCU decoding for DC successive approximation refinement scan.
441  * Note: we assume such scans can be multi-component, although the spec
442  * is not very clear on the point.
443  */
444
445 METHODDEF(boolean)
446 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
447 {   
448   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
449   phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
450   int p1 = 1 << cinfo->Al;  /* 1 in the bit position being coded */
451   int blkn;
452   JBLOCKROW block;
453   BITREAD_STATE_VARS;
454
455   /* Process restart marker if needed; may have to suspend */
456   if (cinfo->restart_interval) {
457     if (entropy->restarts_to_go == 0)
458       if (! process_restart(cinfo))
459   return FALSE;
460   }
461
462   /* Not worth the cycles to check insufficient_data here,
463    * since we will not change the data anyway if we read zeroes.
464    */
465
466   /* Load up working state */
467   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
468
469   /* Outer loop handles each block in the MCU */
470
471   for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
472     block = MCU_data[blkn];
473
474     /* Encoded data is simply the next bit of the two's-complement DC value */
475     CHECK_BIT_BUFFER(br_state, 1, return FALSE);
476     if (GET_BITS(1))
477       (*block)[0] |= p1;
478     /* Note: since we use |=, repeating the assignment later is safe */
479   }
480
481   /* Completed MCU, so update state */
482   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
483
484   /* Account for restart interval (no-op if not using restarts) */
485   entropy->restarts_to_go--;
486
487   return TRUE;
488 }
489
490
491 /*
492  * MCU decoding for AC successive approximation refinement scan.
493  */
494
495 METHODDEF(boolean)
496 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
497 {   
498   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
499   phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
500   int Se = cinfo->Se;
501   int p1 = 1 << cinfo->Al;  /* 1 in the bit position being coded */
502   int m1 = (-1) << cinfo->Al;  /* -1 in the bit position being coded */
503   register int s, k, r;
504   unsigned int EOBRUN;
505   JBLOCKROW block;
506   JCOEFPTR thiscoef;
507   BITREAD_STATE_VARS;
508   d_derived_tbl * tbl;
509   int num_newnz;
510   int newnz_pos[DCTSIZE2];
511
512   /* Process restart marker if needed; may have to suspend */
513   if (cinfo->restart_interval) {
514     if (entropy->restarts_to_go == 0)
515       if (! process_restart(cinfo))
516   return FALSE;
517   }
518
519   /* If we've run out of data, don't modify the MCU.
520    */
521   if (! entropy->insufficient_data) {
522
523     /* Load up working state */
524     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
525     EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
526
527     /* There is always only one block per MCU */
528     block = MCU_data[0];
529     tbl = entropy->ac_derived_tbl;
530
531     /* If we are forced to suspend, we must undo the assignments to any newly
532      * nonzero coefficients in the block, because otherwise we'd get confused
533      * next time about which coefficients were already nonzero.
534      * But we need not undo addition of bits to already-nonzero coefficients;
535      * instead, we can test the current bit to see if we already did it.
536      */
537     num_newnz = 0;
538
539     /* initialize coefficient loop counter to start of band */
540     k = cinfo->Ss;
541
542     if (EOBRUN == 0) {
543       for (; k <= Se; k++) {
544   HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
545   r = s >> 4;
546   s &= 15;
547   if (s) {
548     if (s != 1)    /* size of new coef should always be 1 */
549       WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
550     CHECK_BIT_BUFFER(br_state, 1, goto undoit);
551     if (GET_BITS(1))
552       s = p1;    /* newly nonzero coef is positive */
553     else
554       s = m1;    /* newly nonzero coef is negative */
555   } else {
556     if (r != 15) {
557       EOBRUN = 1 << r;  /* EOBr, run length is 2^r + appended bits */
558       if (r) {
559         CHECK_BIT_BUFFER(br_state, r, goto undoit);
560         r = GET_BITS(r);
561         EOBRUN += r;
562       }
563       break;    /* rest of block is handled by EOB logic */
564     }
565     /* note s = 0 for processing ZRL */
566   }
567   /* Advance over already-nonzero coefs and r still-zero coefs,
568    * appending correction bits to the nonzeroes.  A correction bit is 1
569    * if the absolute value of the coefficient must be increased.
570    */
571   do {
572     thiscoef = *block + jpeg_natural_order[k];
573     if (*thiscoef != 0) {
574       CHECK_BIT_BUFFER(br_state, 1, goto undoit);
575       if (GET_BITS(1)) {
576         if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
577     if (*thiscoef >= 0)
578       *thiscoef += p1;
579     else
580       *thiscoef += m1;
581         }
582       }
583     } else {
584       if (--r < 0)
585         break;    /* reached target zero coefficient */
586     }
587     k++;
588   } while (k <= Se);
589   if (s) {
590     int pos = jpeg_natural_order[k];
591     /* Output newly nonzero coefficient */
592     (*block)[pos] = (JCOEF) s;
593     /* Remember its position in case we have to suspend */
594     newnz_pos[num_newnz++] = pos;
595   }
596       }
597     }
598
599     if (EOBRUN > 0) {
600       /* Scan any remaining coefficient positions after the end-of-band
601        * (the last newly nonzero coefficient, if any).  Append a correction
602        * bit to each already-nonzero coefficient.  A correction bit is 1
603        * if the absolute value of the coefficient must be increased.
604        */
605       for (; k <= Se; k++) {
606   thiscoef = *block + jpeg_natural_order[k];
607   if (*thiscoef != 0) {
608     CHECK_BIT_BUFFER(br_state, 1, goto undoit);
609     if (GET_BITS(1)) {
610       if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
611         if (*thiscoef >= 0)
612     *thiscoef += p1;
613         else
614     *thiscoef += m1;
615       }
616     }
617   }
618       }
619       /* Count one block completed in EOB run */
620       EOBRUN--;
621     }
622
623     /* Completed MCU, so update state */
624     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
625     entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
626   }
627
628   /* Account for restart interval (no-op if not using restarts) */
629   entropy->restarts_to_go--;
630
631   return TRUE;
632
633 undoit:
634   /* Re-zero any output coefficients that we made newly nonzero */
635   while (num_newnz > 0)
636     (*block)[newnz_pos[--num_newnz]] = 0;
637
638   return FALSE;
639 }
640
641
642 /*
643  * Module initialization routine for progressive Huffman entropy decoding.
644  */
645
646 GLOBAL(void)
647 jinit_phuff_decoder (j_decompress_ptr cinfo)
648 {
649   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
650   phuff_entropy_ptr entropy;
651   int *coef_bit_ptr;
652   int ci, i;
653
654   entropy = (phuff_entropy_ptr)
655     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
656         SIZEOF(phuff_entropy_decoder));
657   lossyd->entropy_private = (void *) entropy;
658   lossyd->entropy_start_pass = start_pass_phuff_decoder;
659
660   /* Mark derived tables unallocated */
661   for (i = 0; i < NUM_HUFF_TBLS; i++) {
662     entropy->derived_tbls[i] = NULL;
663   }
664
665   /* Create progression status table */
666   cinfo->coef_bits = (int (*)[DCTSIZE2])
667     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
668         cinfo->num_components*DCTSIZE2*SIZEOF(int));
669   coef_bit_ptr = & cinfo->coef_bits[0][0];
670   for (ci = 0; ci < cinfo->num_components; ci++) 
671     for (i = 0; i < DCTSIZE2; i++)
672       *coef_bit_ptr++ = -1;
673 }
674
675 #endif /* D_PROGRESSIVE_SUPPORTED */