Back to index

lightning-sunbird  0.9+nobinonly
jdphuff.c
Go to the documentation of this file.
00001 /*
00002  * jdphuff.c
00003  *
00004  * Copyright (C) 1995-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains Huffman entropy decoding routines for progressive JPEG.
00009  *
00010  * Much of the complexity here has to do with supporting input suspension.
00011  * If the data source module demands suspension, we want to be able to back
00012  * up to the start of the current MCU.  To do this, we copy state variables
00013  * into local working storage, and update them back to the permanent
00014  * storage only upon successful completion of an MCU.
00015  */
00016 
00017 #define JPEG_INTERNALS
00018 #include "jinclude.h"
00019 #include "jpeglib.h"
00020 #include "jdhuff.h"         /* Declarations shared with jdhuff.c */
00021 
00022 
00023 #ifdef D_PROGRESSIVE_SUPPORTED
00024 
00025 /*
00026  * Expanded entropy decoder object for progressive Huffman decoding.
00027  *
00028  * The savable_state subrecord contains fields that change within an MCU,
00029  * but must not be updated permanently until we complete the MCU.
00030  */
00031 
00032 typedef struct {
00033   unsigned int EOBRUN;                    /* remaining EOBs in EOBRUN */
00034   int last_dc_val[MAX_COMPS_IN_SCAN];     /* last DC coef for each component */
00035 } savable_state;
00036 
00037 /* This macro is to work around compilers with missing or broken
00038  * structure assignment.  You'll need to fix this code if you have
00039  * such a compiler and you change MAX_COMPS_IN_SCAN.
00040  */
00041 
00042 #ifndef NO_STRUCT_ASSIGN
00043 #define ASSIGN_STATE(dest,src)  ((dest) = (src))
00044 #else
00045 #if MAX_COMPS_IN_SCAN == 4
00046 #define ASSIGN_STATE(dest,src)  \
00047        ((dest).EOBRUN = (src).EOBRUN, \
00048         (dest).last_dc_val[0] = (src).last_dc_val[0], \
00049         (dest).last_dc_val[1] = (src).last_dc_val[1], \
00050         (dest).last_dc_val[2] = (src).last_dc_val[2], \
00051         (dest).last_dc_val[3] = (src).last_dc_val[3])
00052 #endif
00053 #endif
00054 
00055 
00056 typedef struct {
00057   struct jpeg_entropy_decoder pub; /* public fields */
00058 
00059   /* These fields are loaded into local variables at start of each MCU.
00060    * In case of suspension, we exit WITHOUT updating them.
00061    */
00062   bitread_perm_state bitstate;     /* Bit buffer at start of MCU */
00063   savable_state saved;             /* Other state at start of MCU */
00064 
00065   /* These fields are NOT loaded into local working state. */
00066   unsigned int restarts_to_go;     /* MCUs left in this restart interval */
00067 
00068   /* Pointers to derived tables (these workspaces have image lifespan) */
00069   d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
00070 
00071   d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
00072 } phuff_entropy_decoder;
00073 
00074 typedef phuff_entropy_decoder * phuff_entropy_ptr;
00075 
00076 /* Forward declarations */
00077 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
00078                                        JBLOCKROW *MCU_data));
00079 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
00080                                        JBLOCKROW *MCU_data));
00081 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
00082                                         JBLOCKROW *MCU_data));
00083 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
00084                                         JBLOCKROW *MCU_data));
00085 
00086 
00087 /*
00088  * Initialize for a Huffman-compressed scan.
00089  */
00090 
00091 METHODDEF(void)
00092 start_pass_phuff_decoder (j_decompress_ptr cinfo)
00093 {
00094   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00095   boolean is_DC_band, bad;
00096   int ci, coefi, tbl;
00097   int *coef_bit_ptr;
00098   jpeg_component_info * compptr;
00099 
00100   is_DC_band = (cinfo->Ss == 0);
00101 
00102   /* Validate scan parameters */
00103   bad = FALSE;
00104   if (is_DC_band) {
00105     if (cinfo->Se != 0)
00106       bad = TRUE;
00107   } else {
00108     /* need not check Ss/Se < 0 since they came from unsigned bytes */
00109     if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
00110       bad = TRUE;
00111     /* AC scans may have only one component */
00112     if (cinfo->comps_in_scan != 1)
00113       bad = TRUE;
00114   }
00115   if (cinfo->Ah != 0) {
00116     /* Successive approximation refinement scan: must have Al = Ah-1. */
00117     if (cinfo->Al != cinfo->Ah-1)
00118       bad = TRUE;
00119   }
00120   if (cinfo->Al > 13)              /* need not check for < 0 */
00121     bad = TRUE;
00122   /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
00123    * but the spec doesn't say so, and we try to be liberal about what we
00124    * accept.  Note: large Al values could result in out-of-range DC
00125    * coefficients during early scans, leading to bizarre displays due to
00126    * overflows in the IDCT math.  But we won't crash.
00127    */
00128   if (bad)
00129     ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
00130             cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
00131   /* Update progression status, and verify that scan order is legal.
00132    * Note that inter-scan inconsistencies are treated as warnings
00133    * not fatal errors ... not clear if this is right way to behave.
00134    */
00135   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00136     int cindex = cinfo->cur_comp_info[ci]->component_index;
00137     coef_bit_ptr = & cinfo->coef_bits[cindex][0];
00138     if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
00139       WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
00140     for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
00141       int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
00142       if (cinfo->Ah != expected)
00143        WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
00144       coef_bit_ptr[coefi] = cinfo->Al;
00145     }
00146   }
00147 
00148   /* Select MCU decoding routine */
00149   if (cinfo->Ah == 0) {
00150     if (is_DC_band)
00151       entropy->pub.decode_mcu = decode_mcu_DC_first;
00152     else
00153       entropy->pub.decode_mcu = decode_mcu_AC_first;
00154   } else {
00155     if (is_DC_band)
00156       entropy->pub.decode_mcu = decode_mcu_DC_refine;
00157     else
00158       entropy->pub.decode_mcu = decode_mcu_AC_refine;
00159   }
00160 
00161   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00162     compptr = cinfo->cur_comp_info[ci];
00163     /* Make sure requested tables are present, and compute derived tables.
00164      * We may build same derived table more than once, but it's not expensive.
00165      */
00166     if (is_DC_band) {
00167       if (cinfo->Ah == 0) { /* DC refinement needs no table */
00168        tbl = compptr->dc_tbl_no;
00169        jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
00170                             & entropy->derived_tbls[tbl]);
00171       }
00172     } else {
00173       tbl = compptr->ac_tbl_no;
00174       jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
00175                            & entropy->derived_tbls[tbl]);
00176       /* remember the single active table */
00177       entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
00178     }
00179     /* Initialize DC predictions to 0 */
00180     entropy->saved.last_dc_val[ci] = 0;
00181   }
00182 
00183   /* Initialize bitread state variables */
00184   entropy->bitstate.bits_left = 0;
00185   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
00186   entropy->pub.insufficient_data = FALSE;
00187 
00188   /* Initialize private state variables */
00189   entropy->saved.EOBRUN = 0;
00190 
00191   /* Initialize restart counter */
00192   entropy->restarts_to_go = cinfo->restart_interval;
00193 }
00194 
00195 
00196 /*
00197  * Figure F.12: extend sign bit.
00198  * On some machines, a shift and add will be faster than a table lookup.
00199  */
00200 
00201 #ifdef AVOID_TABLES
00202 
00203 #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
00204 
00205 #else
00206 
00207 #define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
00208 
00209 static const int extend_test[16] =   /* entry n is 2**(n-1) */
00210   { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
00211     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
00212 
00213 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
00214   { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
00215     ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
00216     ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
00217     ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
00218 
00219 #endif /* AVOID_TABLES */
00220 
00221 
00222 /*
00223  * Check for a restart marker & resynchronize decoder.
00224  * Returns FALSE if must suspend.
00225  */
00226 
00227 LOCAL(boolean)
00228 process_restart (j_decompress_ptr cinfo)
00229 {
00230   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00231   int ci;
00232 
00233   /* Throw away any unused bits remaining in bit buffer; */
00234   /* include any full bytes in next_marker's count of discarded bytes */
00235   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
00236   entropy->bitstate.bits_left = 0;
00237 
00238   /* Advance past the RSTn marker */
00239   if (! (*cinfo->marker->read_restart_marker) (cinfo))
00240     return FALSE;
00241 
00242   /* Re-initialize DC predictions to 0 */
00243   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
00244     entropy->saved.last_dc_val[ci] = 0;
00245   /* Re-init EOB run count, too */
00246   entropy->saved.EOBRUN = 0;
00247 
00248   /* Reset restart counter */
00249   entropy->restarts_to_go = cinfo->restart_interval;
00250 
00251   /* Reset out-of-data flag, unless read_restart_marker left us smack up
00252    * against a marker.  In that case we will end up treating the next data
00253    * segment as empty, and we can avoid producing bogus output pixels by
00254    * leaving the flag set.
00255    */
00256   if (cinfo->unread_marker == 0)
00257     entropy->pub.insufficient_data = FALSE;
00258 
00259   return TRUE;
00260 }
00261 
00262 
00263 /*
00264  * Huffman MCU decoding.
00265  * Each of these routines decodes and returns one MCU's worth of
00266  * Huffman-compressed coefficients. 
00267  * The coefficients are reordered from zigzag order into natural array order,
00268  * but are not dequantized.
00269  *
00270  * The i'th block of the MCU is stored into the block pointed to by
00271  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
00272  *
00273  * We return FALSE if data source requested suspension.  In that case no
00274  * changes have been made to permanent state.  (Exception: some output
00275  * coefficients may already have been assigned.  This is harmless for
00276  * spectral selection, since we'll just re-assign them on the next call.
00277  * Successive approximation AC refinement has to be more careful, however.)
00278  */
00279 
00280 /*
00281  * MCU decoding for DC initial scan (either spectral selection,
00282  * or first pass of successive approximation).
00283  */
00284 
00285 METHODDEF(boolean)
00286 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00287 {   
00288   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00289   int Al = cinfo->Al;
00290   register int s, r;
00291   int blkn, ci;
00292   JBLOCKROW block;
00293   BITREAD_STATE_VARS;
00294   savable_state state;
00295   d_derived_tbl * tbl;
00296   jpeg_component_info * compptr;
00297 
00298   /* Process restart marker if needed; may have to suspend */
00299   if (cinfo->restart_interval) {
00300     if (entropy->restarts_to_go == 0)
00301       if (! process_restart(cinfo))
00302        return FALSE;
00303   }
00304 
00305   /* If we've run out of data, just leave the MCU set to zeroes.
00306    * This way, we return uniform gray for the remainder of the segment.
00307    */
00308   if (! entropy->pub.insufficient_data) {
00309 
00310     /* Load up working state */
00311     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00312     ASSIGN_STATE(state, entropy->saved);
00313 
00314     /* Outer loop handles each block in the MCU */
00315 
00316     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00317       block = MCU_data[blkn];
00318       ci = cinfo->MCU_membership[blkn];
00319       compptr = cinfo->cur_comp_info[ci];
00320       tbl = entropy->derived_tbls[compptr->dc_tbl_no];
00321 
00322       /* Decode a single block's worth of coefficients */
00323 
00324       /* Section F.2.2.1: decode the DC coefficient difference */
00325       HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
00326       if (s) {
00327        CHECK_BIT_BUFFER(br_state, s, return FALSE);
00328        r = GET_BITS(s);
00329        s = HUFF_EXTEND(r, s);
00330       }
00331 
00332       /* Convert DC difference to actual value, update last_dc_val */
00333       s += state.last_dc_val[ci];
00334       state.last_dc_val[ci] = s;
00335       /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
00336       (*block)[0] = (JCOEF) (s << Al);
00337     }
00338 
00339     /* Completed MCU, so update state */
00340     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00341     ASSIGN_STATE(entropy->saved, state);
00342   }
00343 
00344   /* Account for restart interval (no-op if not using restarts) */
00345   entropy->restarts_to_go--;
00346 
00347   return TRUE;
00348 }
00349 
00350 
00351 /*
00352  * MCU decoding for AC initial scan (either spectral selection,
00353  * or first pass of successive approximation).
00354  */
00355 
00356 METHODDEF(boolean)
00357 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00358 {   
00359   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00360   int Se = cinfo->Se;
00361   int Al = cinfo->Al;
00362   register int s, k, r;
00363   unsigned int EOBRUN;
00364   JBLOCKROW block;
00365   BITREAD_STATE_VARS;
00366   d_derived_tbl * tbl;
00367 
00368   /* Process restart marker if needed; may have to suspend */
00369   if (cinfo->restart_interval) {
00370     if (entropy->restarts_to_go == 0)
00371       if (! process_restart(cinfo))
00372        return FALSE;
00373   }
00374 
00375   /* If we've run out of data, just leave the MCU set to zeroes.
00376    * This way, we return uniform gray for the remainder of the segment.
00377    */
00378   if (! entropy->pub.insufficient_data) {
00379 
00380     /* Load up working state.
00381      * We can avoid loading/saving bitread state if in an EOB run.
00382      */
00383     EOBRUN = entropy->saved.EOBRUN;       /* only part of saved state we need */
00384 
00385     /* There is always only one block per MCU */
00386 
00387     if (EOBRUN > 0)         /* if it's a band of zeroes... */
00388       EOBRUN--;                    /* ...process it now (we do nothing) */
00389     else {
00390       BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00391       block = MCU_data[0];
00392       tbl = entropy->ac_derived_tbl;
00393 
00394       for (k = cinfo->Ss; k <= Se; k++) {
00395        HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
00396        r = s >> 4;
00397        s &= 15;
00398        if (s) {
00399          k += r;
00400          CHECK_BIT_BUFFER(br_state, s, return FALSE);
00401          r = GET_BITS(s);
00402          s = HUFF_EXTEND(r, s);
00403          /* Scale and output coefficient in natural (dezigzagged) order */
00404          (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
00405        } else {
00406          if (r == 15) {     /* ZRL */
00407            k += 15;         /* skip 15 zeroes in band */
00408          } else {           /* EOBr, run length is 2^r + appended bits */
00409            EOBRUN = 1 << r;
00410            if (r) {         /* EOBr, r > 0 */
00411              CHECK_BIT_BUFFER(br_state, r, return FALSE);
00412              r = GET_BITS(r);
00413              EOBRUN += r;
00414            }
00415            EOBRUN--;        /* this band is processed at this moment */
00416            break;           /* force end-of-band */
00417          }
00418        }
00419       }
00420 
00421       BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00422     }
00423 
00424     /* Completed MCU, so update state */
00425     entropy->saved.EOBRUN = EOBRUN;       /* only part of saved state we need */
00426   }
00427 
00428   /* Account for restart interval (no-op if not using restarts) */
00429   entropy->restarts_to_go--;
00430 
00431   return TRUE;
00432 }
00433 
00434 
00435 /*
00436  * MCU decoding for DC successive approximation refinement scan.
00437  * Note: we assume such scans can be multi-component, although the spec
00438  * is not very clear on the point.
00439  */
00440 
00441 METHODDEF(boolean)
00442 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00443 {   
00444   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00445   int p1 = 1 << cinfo->Al;  /* 1 in the bit position being coded */
00446   int blkn;
00447   JBLOCKROW block;
00448   BITREAD_STATE_VARS;
00449 
00450   /* Process restart marker if needed; may have to suspend */
00451   if (cinfo->restart_interval) {
00452     if (entropy->restarts_to_go == 0)
00453       if (! process_restart(cinfo))
00454        return FALSE;
00455   }
00456 
00457   /* Not worth the cycles to check insufficient_data here,
00458    * since we will not change the data anyway if we read zeroes.
00459    */
00460 
00461   /* Load up working state */
00462   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00463 
00464   /* Outer loop handles each block in the MCU */
00465 
00466   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00467     block = MCU_data[blkn];
00468 
00469     /* Encoded data is simply the next bit of the two's-complement DC value */
00470     CHECK_BIT_BUFFER(br_state, 1, return FALSE);
00471     if (GET_BITS(1))
00472       (*block)[0] |= p1;
00473     /* Note: since we use |=, repeating the assignment later is safe */
00474   }
00475 
00476   /* Completed MCU, so update state */
00477   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00478 
00479   /* Account for restart interval (no-op if not using restarts) */
00480   entropy->restarts_to_go--;
00481 
00482   return TRUE;
00483 }
00484 
00485 
00486 /*
00487  * MCU decoding for AC successive approximation refinement scan.
00488  */
00489 
00490 METHODDEF(boolean)
00491 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
00492 {   
00493   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
00494   int Se = cinfo->Se;
00495   int p1 = 1 << cinfo->Al;  /* 1 in the bit position being coded */
00496   int m1 = (-1) << cinfo->Al;      /* -1 in the bit position being coded */
00497   register int s, k, r;
00498   unsigned int EOBRUN;
00499   JBLOCKROW block;
00500   JCOEFPTR thiscoef;
00501   BITREAD_STATE_VARS;
00502   d_derived_tbl * tbl;
00503   int num_newnz;
00504   int newnz_pos[DCTSIZE2];
00505 
00506   /* Process restart marker if needed; may have to suspend */
00507   if (cinfo->restart_interval) {
00508     if (entropy->restarts_to_go == 0)
00509       if (! process_restart(cinfo))
00510        return FALSE;
00511   }
00512 
00513   /* If we've run out of data, don't modify the MCU.
00514    */
00515   if (! entropy->pub.insufficient_data) {
00516 
00517     /* Load up working state */
00518     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
00519     EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
00520 
00521     /* There is always only one block per MCU */
00522     block = MCU_data[0];
00523     tbl = entropy->ac_derived_tbl;
00524 
00525     /* If we are forced to suspend, we must undo the assignments to any newly
00526      * nonzero coefficients in the block, because otherwise we'd get confused
00527      * next time about which coefficients were already nonzero.
00528      * But we need not undo addition of bits to already-nonzero coefficients;
00529      * instead, we can test the current bit to see if we already did it.
00530      */
00531     num_newnz = 0;
00532 
00533     /* initialize coefficient loop counter to start of band */
00534     k = cinfo->Ss;
00535 
00536     if (EOBRUN == 0) {
00537       for (; k <= Se; k++) {
00538        HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
00539        r = s >> 4;
00540        s &= 15;
00541        if (s) {
00542          if (s != 1)        /* size of new coef should always be 1 */
00543            WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
00544          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
00545          if (GET_BITS(1))
00546            s = p1;          /* newly nonzero coef is positive */
00547          else
00548            s = m1;          /* newly nonzero coef is negative */
00549        } else {
00550          if (r != 15) {
00551            EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
00552            if (r) {
00553              CHECK_BIT_BUFFER(br_state, r, goto undoit);
00554              r = GET_BITS(r);
00555              EOBRUN += r;
00556            }
00557            break;           /* rest of block is handled by EOB logic */
00558          }
00559          /* note s = 0 for processing ZRL */
00560        }
00561        /* Advance over already-nonzero coefs and r still-zero coefs,
00562         * appending correction bits to the nonzeroes.  A correction bit is 1
00563         * if the absolute value of the coefficient must be increased.
00564         */
00565        do {
00566          thiscoef = *block + jpeg_natural_order[k];
00567          if (*thiscoef != 0) {
00568            CHECK_BIT_BUFFER(br_state, 1, goto undoit);
00569            if (GET_BITS(1)) {
00570              if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
00571               if (*thiscoef >= 0)
00572                 *thiscoef += p1;
00573               else
00574                 *thiscoef += m1;
00575              }
00576            }
00577          } else {
00578            if (--r < 0)
00579              break;         /* reached target zero coefficient */
00580          }
00581          k++;
00582        } while (k <= Se);
00583        if (s) {
00584          int pos = jpeg_natural_order[k];
00585          /* Output newly nonzero coefficient */
00586          (*block)[pos] = (JCOEF) s;
00587          /* Remember its position in case we have to suspend */
00588          newnz_pos[num_newnz++] = pos;
00589        }
00590       }
00591     }
00592 
00593     if (EOBRUN > 0) {
00594       /* Scan any remaining coefficient positions after the end-of-band
00595        * (the last newly nonzero coefficient, if any).  Append a correction
00596        * bit to each already-nonzero coefficient.  A correction bit is 1
00597        * if the absolute value of the coefficient must be increased.
00598        */
00599       for (; k <= Se; k++) {
00600        thiscoef = *block + jpeg_natural_order[k];
00601        if (*thiscoef != 0) {
00602          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
00603          if (GET_BITS(1)) {
00604            if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
00605              if (*thiscoef >= 0)
00606               *thiscoef += p1;
00607              else
00608               *thiscoef += m1;
00609            }
00610          }
00611        }
00612       }
00613       /* Count one block completed in EOB run */
00614       EOBRUN--;
00615     }
00616 
00617     /* Completed MCU, so update state */
00618     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
00619     entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
00620   }
00621 
00622   /* Account for restart interval (no-op if not using restarts) */
00623   entropy->restarts_to_go--;
00624 
00625   return TRUE;
00626 
00627 undoit:
00628   /* Re-zero any output coefficients that we made newly nonzero */
00629   while (num_newnz > 0)
00630     (*block)[newnz_pos[--num_newnz]] = 0;
00631 
00632   return FALSE;
00633 }
00634 
00635 
00636 /*
00637  * Module initialization routine for progressive Huffman entropy decoding.
00638  */
00639 
00640 GLOBAL(void)
00641 jinit_phuff_decoder (j_decompress_ptr cinfo)
00642 {
00643   phuff_entropy_ptr entropy;
00644   int *coef_bit_ptr;
00645   int ci, i;
00646 
00647   entropy = (phuff_entropy_ptr)
00648     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00649                             SIZEOF(phuff_entropy_decoder));
00650   cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
00651   entropy->pub.start_pass = start_pass_phuff_decoder;
00652 
00653   /* Mark derived tables unallocated */
00654   for (i = 0; i < NUM_HUFF_TBLS; i++) {
00655     entropy->derived_tbls[i] = NULL;
00656   }
00657 
00658   /* Create progression status table */
00659   cinfo->coef_bits = (int (*)[DCTSIZE2])
00660     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00661                             cinfo->num_components*DCTSIZE2*SIZEOF(int));
00662   coef_bit_ptr = & cinfo->coef_bits[0][0];
00663   for (ci = 0; ci < cinfo->num_components; ci++) 
00664     for (i = 0; i < DCTSIZE2; i++)
00665       *coef_bit_ptr++ = -1;
00666 }
00667 
00668 #endif /* D_PROGRESSIVE_SUPPORTED */