jdphuff.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:19k
源码类别:

浏览器

开发平台:

Unix_Linux

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