home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jdphuff.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  21.5 KB  |  689 lines

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