vrshoot

diff libs/libjpeg/jdphuff.c @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jdphuff.c	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,668 @@
     1.4 +/*
     1.5 + * jdphuff.c
     1.6 + *
     1.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains Huffman entropy decoding routines for progressive JPEG.
    1.12 + *
    1.13 + * Much of the complexity here has to do with supporting input suspension.
    1.14 + * If the data source module demands suspension, we want to be able to back
    1.15 + * up to the start of the current MCU.  To do this, we copy state variables
    1.16 + * into local working storage, and update them back to the permanent
    1.17 + * storage only upon successful completion of an MCU.
    1.18 + */
    1.19 +
    1.20 +#define JPEG_INTERNALS
    1.21 +#include "jinclude.h"
    1.22 +#include "jpeglib.h"
    1.23 +#include "jdhuff.h"		/* Declarations shared with jdhuff.c */
    1.24 +
    1.25 +
    1.26 +#ifdef D_PROGRESSIVE_SUPPORTED
    1.27 +
    1.28 +/*
    1.29 + * Expanded entropy decoder object for progressive Huffman decoding.
    1.30 + *
    1.31 + * The savable_state subrecord contains fields that change within an MCU,
    1.32 + * but must not be updated permanently until we complete the MCU.
    1.33 + */
    1.34 +
    1.35 +typedef struct {
    1.36 +  unsigned int EOBRUN;			/* remaining EOBs in EOBRUN */
    1.37 +  int last_dc_val[MAX_COMPS_IN_SCAN];	/* last DC coef for each component */
    1.38 +} savable_state;
    1.39 +
    1.40 +/* This macro is to work around compilers with missing or broken
    1.41 + * structure assignment.  You'll need to fix this code if you have
    1.42 + * such a compiler and you change MAX_COMPS_IN_SCAN.
    1.43 + */
    1.44 +
    1.45 +#ifndef NO_STRUCT_ASSIGN
    1.46 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
    1.47 +#else
    1.48 +#if MAX_COMPS_IN_SCAN == 4
    1.49 +#define ASSIGN_STATE(dest,src)  \
    1.50 +	((dest).EOBRUN = (src).EOBRUN, \
    1.51 +	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
    1.52 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
    1.53 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
    1.54 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
    1.55 +#endif
    1.56 +#endif
    1.57 +
    1.58 +
    1.59 +typedef struct {
    1.60 +  struct jpeg_entropy_decoder pub; /* public fields */
    1.61 +
    1.62 +  /* These fields are loaded into local variables at start of each MCU.
    1.63 +   * In case of suspension, we exit WITHOUT updating them.
    1.64 +   */
    1.65 +  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
    1.66 +  savable_state saved;		/* Other state at start of MCU */
    1.67 +
    1.68 +  /* These fields are NOT loaded into local working state. */
    1.69 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
    1.70 +
    1.71 +  /* Pointers to derived tables (these workspaces have image lifespan) */
    1.72 +  d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
    1.73 +
    1.74 +  d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
    1.75 +} phuff_entropy_decoder;
    1.76 +
    1.77 +typedef phuff_entropy_decoder * phuff_entropy_ptr;
    1.78 +
    1.79 +/* Forward declarations */
    1.80 +METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
    1.81 +					    JBLOCKROW *MCU_data));
    1.82 +METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
    1.83 +					    JBLOCKROW *MCU_data));
    1.84 +METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
    1.85 +					     JBLOCKROW *MCU_data));
    1.86 +METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
    1.87 +					     JBLOCKROW *MCU_data));
    1.88 +
    1.89 +
    1.90 +/*
    1.91 + * Initialize for a Huffman-compressed scan.
    1.92 + */
    1.93 +
    1.94 +METHODDEF(void)
    1.95 +start_pass_phuff_decoder (j_decompress_ptr cinfo)
    1.96 +{
    1.97 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
    1.98 +  boolean is_DC_band, bad;
    1.99 +  int ci, coefi, tbl;
   1.100 +  int *coef_bit_ptr;
   1.101 +  jpeg_component_info * compptr;
   1.102 +
   1.103 +  is_DC_band = (cinfo->Ss == 0);
   1.104 +
   1.105 +  /* Validate scan parameters */
   1.106 +  bad = FALSE;
   1.107 +  if (is_DC_band) {
   1.108 +    if (cinfo->Se != 0)
   1.109 +      bad = TRUE;
   1.110 +  } else {
   1.111 +    /* need not check Ss/Se < 0 since they came from unsigned bytes */
   1.112 +    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
   1.113 +      bad = TRUE;
   1.114 +    /* AC scans may have only one component */
   1.115 +    if (cinfo->comps_in_scan != 1)
   1.116 +      bad = TRUE;
   1.117 +  }
   1.118 +  if (cinfo->Ah != 0) {
   1.119 +    /* Successive approximation refinement scan: must have Al = Ah-1. */
   1.120 +    if (cinfo->Al != cinfo->Ah-1)
   1.121 +      bad = TRUE;
   1.122 +  }
   1.123 +  if (cinfo->Al > 13)		/* need not check for < 0 */
   1.124 +    bad = TRUE;
   1.125 +  /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
   1.126 +   * but the spec doesn't say so, and we try to be liberal about what we
   1.127 +   * accept.  Note: large Al values could result in out-of-range DC
   1.128 +   * coefficients during early scans, leading to bizarre displays due to
   1.129 +   * overflows in the IDCT math.  But we won't crash.
   1.130 +   */
   1.131 +  if (bad)
   1.132 +    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
   1.133 +	     cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
   1.134 +  /* Update progression status, and verify that scan order is legal.
   1.135 +   * Note that inter-scan inconsistencies are treated as warnings
   1.136 +   * not fatal errors ... not clear if this is right way to behave.
   1.137 +   */
   1.138 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.139 +    int cindex = cinfo->cur_comp_info[ci]->component_index;
   1.140 +    coef_bit_ptr = & cinfo->coef_bits[cindex][0];
   1.141 +    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
   1.142 +      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
   1.143 +    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
   1.144 +      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
   1.145 +      if (cinfo->Ah != expected)
   1.146 +	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
   1.147 +      coef_bit_ptr[coefi] = cinfo->Al;
   1.148 +    }
   1.149 +  }
   1.150 +
   1.151 +  /* Select MCU decoding routine */
   1.152 +  if (cinfo->Ah == 0) {
   1.153 +    if (is_DC_band)
   1.154 +      entropy->pub.decode_mcu = decode_mcu_DC_first;
   1.155 +    else
   1.156 +      entropy->pub.decode_mcu = decode_mcu_AC_first;
   1.157 +  } else {
   1.158 +    if (is_DC_band)
   1.159 +      entropy->pub.decode_mcu = decode_mcu_DC_refine;
   1.160 +    else
   1.161 +      entropy->pub.decode_mcu = decode_mcu_AC_refine;
   1.162 +  }
   1.163 +
   1.164 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.165 +    compptr = cinfo->cur_comp_info[ci];
   1.166 +    /* Make sure requested tables are present, and compute derived tables.
   1.167 +     * We may build same derived table more than once, but it's not expensive.
   1.168 +     */
   1.169 +    if (is_DC_band) {
   1.170 +      if (cinfo->Ah == 0) {	/* DC refinement needs no table */
   1.171 +	tbl = compptr->dc_tbl_no;
   1.172 +	jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
   1.173 +				& entropy->derived_tbls[tbl]);
   1.174 +      }
   1.175 +    } else {
   1.176 +      tbl = compptr->ac_tbl_no;
   1.177 +      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
   1.178 +			      & entropy->derived_tbls[tbl]);
   1.179 +      /* remember the single active table */
   1.180 +      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
   1.181 +    }
   1.182 +    /* Initialize DC predictions to 0 */
   1.183 +    entropy->saved.last_dc_val[ci] = 0;
   1.184 +  }
   1.185 +
   1.186 +  /* Initialize bitread state variables */
   1.187 +  entropy->bitstate.bits_left = 0;
   1.188 +  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
   1.189 +  entropy->pub.insufficient_data = FALSE;
   1.190 +
   1.191 +  /* Initialize private state variables */
   1.192 +  entropy->saved.EOBRUN = 0;
   1.193 +
   1.194 +  /* Initialize restart counter */
   1.195 +  entropy->restarts_to_go = cinfo->restart_interval;
   1.196 +}
   1.197 +
   1.198 +
   1.199 +/*
   1.200 + * Figure F.12: extend sign bit.
   1.201 + * On some machines, a shift and add will be faster than a table lookup.
   1.202 + */
   1.203 +
   1.204 +#ifdef AVOID_TABLES
   1.205 +
   1.206 +#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
   1.207 +
   1.208 +#else
   1.209 +
   1.210 +#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
   1.211 +
   1.212 +static const int extend_test[16] =   /* entry n is 2**(n-1) */
   1.213 +  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
   1.214 +    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
   1.215 +
   1.216 +static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
   1.217 +  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
   1.218 +    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
   1.219 +    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
   1.220 +    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
   1.221 +
   1.222 +#endif /* AVOID_TABLES */
   1.223 +
   1.224 +
   1.225 +/*
   1.226 + * Check for a restart marker & resynchronize decoder.
   1.227 + * Returns FALSE if must suspend.
   1.228 + */
   1.229 +
   1.230 +LOCAL(boolean)
   1.231 +process_restart (j_decompress_ptr cinfo)
   1.232 +{
   1.233 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
   1.234 +  int ci;
   1.235 +
   1.236 +  /* Throw away any unused bits remaining in bit buffer; */
   1.237 +  /* include any full bytes in next_marker's count of discarded bytes */
   1.238 +  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
   1.239 +  entropy->bitstate.bits_left = 0;
   1.240 +
   1.241 +  /* Advance past the RSTn marker */
   1.242 +  if (! (*cinfo->marker->read_restart_marker) (cinfo))
   1.243 +    return FALSE;
   1.244 +
   1.245 +  /* Re-initialize DC predictions to 0 */
   1.246 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
   1.247 +    entropy->saved.last_dc_val[ci] = 0;
   1.248 +  /* Re-init EOB run count, too */
   1.249 +  entropy->saved.EOBRUN = 0;
   1.250 +
   1.251 +  /* Reset restart counter */
   1.252 +  entropy->restarts_to_go = cinfo->restart_interval;
   1.253 +
   1.254 +  /* Reset out-of-data flag, unless read_restart_marker left us smack up
   1.255 +   * against a marker.  In that case we will end up treating the next data
   1.256 +   * segment as empty, and we can avoid producing bogus output pixels by
   1.257 +   * leaving the flag set.
   1.258 +   */
   1.259 +  if (cinfo->unread_marker == 0)
   1.260 +    entropy->pub.insufficient_data = FALSE;
   1.261 +
   1.262 +  return TRUE;
   1.263 +}
   1.264 +
   1.265 +
   1.266 +/*
   1.267 + * Huffman MCU decoding.
   1.268 + * Each of these routines decodes and returns one MCU's worth of
   1.269 + * Huffman-compressed coefficients. 
   1.270 + * The coefficients are reordered from zigzag order into natural array order,
   1.271 + * but are not dequantized.
   1.272 + *
   1.273 + * The i'th block of the MCU is stored into the block pointed to by
   1.274 + * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
   1.275 + *
   1.276 + * We return FALSE if data source requested suspension.  In that case no
   1.277 + * changes have been made to permanent state.  (Exception: some output
   1.278 + * coefficients may already have been assigned.  This is harmless for
   1.279 + * spectral selection, since we'll just re-assign them on the next call.
   1.280 + * Successive approximation AC refinement has to be more careful, however.)
   1.281 + */
   1.282 +
   1.283 +/*
   1.284 + * MCU decoding for DC initial scan (either spectral selection,
   1.285 + * or first pass of successive approximation).
   1.286 + */
   1.287 +
   1.288 +METHODDEF(boolean)
   1.289 +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.290 +{   
   1.291 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
   1.292 +  int Al = cinfo->Al;
   1.293 +  register int s, r;
   1.294 +  int blkn, ci;
   1.295 +  JBLOCKROW block;
   1.296 +  BITREAD_STATE_VARS;
   1.297 +  savable_state state;
   1.298 +  d_derived_tbl * tbl;
   1.299 +  jpeg_component_info * compptr;
   1.300 +
   1.301 +  /* Process restart marker if needed; may have to suspend */
   1.302 +  if (cinfo->restart_interval) {
   1.303 +    if (entropy->restarts_to_go == 0)
   1.304 +      if (! process_restart(cinfo))
   1.305 +	return FALSE;
   1.306 +  }
   1.307 +
   1.308 +  /* If we've run out of data, just leave the MCU set to zeroes.
   1.309 +   * This way, we return uniform gray for the remainder of the segment.
   1.310 +   */
   1.311 +  if (! entropy->pub.insufficient_data) {
   1.312 +
   1.313 +    /* Load up working state */
   1.314 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   1.315 +    ASSIGN_STATE(state, entropy->saved);
   1.316 +
   1.317 +    /* Outer loop handles each block in the MCU */
   1.318 +
   1.319 +    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1.320 +      block = MCU_data[blkn];
   1.321 +      ci = cinfo->MCU_membership[blkn];
   1.322 +      compptr = cinfo->cur_comp_info[ci];
   1.323 +      tbl = entropy->derived_tbls[compptr->dc_tbl_no];
   1.324 +
   1.325 +      /* Decode a single block's worth of coefficients */
   1.326 +
   1.327 +      /* Section F.2.2.1: decode the DC coefficient difference */
   1.328 +      HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
   1.329 +      if (s) {
   1.330 +	CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1.331 +	r = GET_BITS(s);
   1.332 +	s = HUFF_EXTEND(r, s);
   1.333 +      }
   1.334 +
   1.335 +      /* Convert DC difference to actual value, update last_dc_val */
   1.336 +      s += state.last_dc_val[ci];
   1.337 +      state.last_dc_val[ci] = s;
   1.338 +      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
   1.339 +      (*block)[0] = (JCOEF) (s << Al);
   1.340 +    }
   1.341 +
   1.342 +    /* Completed MCU, so update state */
   1.343 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1.344 +    ASSIGN_STATE(entropy->saved, state);
   1.345 +  }
   1.346 +
   1.347 +  /* Account for restart interval (no-op if not using restarts) */
   1.348 +  entropy->restarts_to_go--;
   1.349 +
   1.350 +  return TRUE;
   1.351 +}
   1.352 +
   1.353 +
   1.354 +/*
   1.355 + * MCU decoding for AC initial scan (either spectral selection,
   1.356 + * or first pass of successive approximation).
   1.357 + */
   1.358 +
   1.359 +METHODDEF(boolean)
   1.360 +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.361 +{   
   1.362 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
   1.363 +  int Se = cinfo->Se;
   1.364 +  int Al = cinfo->Al;
   1.365 +  register int s, k, r;
   1.366 +  unsigned int EOBRUN;
   1.367 +  JBLOCKROW block;
   1.368 +  BITREAD_STATE_VARS;
   1.369 +  d_derived_tbl * tbl;
   1.370 +
   1.371 +  /* Process restart marker if needed; may have to suspend */
   1.372 +  if (cinfo->restart_interval) {
   1.373 +    if (entropy->restarts_to_go == 0)
   1.374 +      if (! process_restart(cinfo))
   1.375 +	return FALSE;
   1.376 +  }
   1.377 +
   1.378 +  /* If we've run out of data, just leave the MCU set to zeroes.
   1.379 +   * This way, we return uniform gray for the remainder of the segment.
   1.380 +   */
   1.381 +  if (! entropy->pub.insufficient_data) {
   1.382 +
   1.383 +    /* Load up working state.
   1.384 +     * We can avoid loading/saving bitread state if in an EOB run.
   1.385 +     */
   1.386 +    EOBRUN = entropy->saved.EOBRUN;	/* only part of saved state we need */
   1.387 +
   1.388 +    /* There is always only one block per MCU */
   1.389 +
   1.390 +    if (EOBRUN > 0)		/* if it's a band of zeroes... */
   1.391 +      EOBRUN--;			/* ...process it now (we do nothing) */
   1.392 +    else {
   1.393 +      BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   1.394 +      block = MCU_data[0];
   1.395 +      tbl = entropy->ac_derived_tbl;
   1.396 +
   1.397 +      for (k = cinfo->Ss; k <= Se; k++) {
   1.398 +	HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
   1.399 +	r = s >> 4;
   1.400 +	s &= 15;
   1.401 +	if (s) {
   1.402 +	  k += r;
   1.403 +	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1.404 +	  r = GET_BITS(s);
   1.405 +	  s = HUFF_EXTEND(r, s);
   1.406 +	  /* Scale and output coefficient in natural (dezigzagged) order */
   1.407 +	  (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
   1.408 +	} else {
   1.409 +	  if (r == 15) {	/* ZRL */
   1.410 +	    k += 15;		/* skip 15 zeroes in band */
   1.411 +	  } else {		/* EOBr, run length is 2^r + appended bits */
   1.412 +	    EOBRUN = 1 << r;
   1.413 +	    if (r) {		/* EOBr, r > 0 */
   1.414 +	      CHECK_BIT_BUFFER(br_state, r, return FALSE);
   1.415 +	      r = GET_BITS(r);
   1.416 +	      EOBRUN += r;
   1.417 +	    }
   1.418 +	    EOBRUN--;		/* this band is processed at this moment */
   1.419 +	    break;		/* force end-of-band */
   1.420 +	  }
   1.421 +	}
   1.422 +      }
   1.423 +
   1.424 +      BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1.425 +    }
   1.426 +
   1.427 +    /* Completed MCU, so update state */
   1.428 +    entropy->saved.EOBRUN = EOBRUN;	/* only part of saved state we need */
   1.429 +  }
   1.430 +
   1.431 +  /* Account for restart interval (no-op if not using restarts) */
   1.432 +  entropy->restarts_to_go--;
   1.433 +
   1.434 +  return TRUE;
   1.435 +}
   1.436 +
   1.437 +
   1.438 +/*
   1.439 + * MCU decoding for DC successive approximation refinement scan.
   1.440 + * Note: we assume such scans can be multi-component, although the spec
   1.441 + * is not very clear on the point.
   1.442 + */
   1.443 +
   1.444 +METHODDEF(boolean)
   1.445 +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.446 +{   
   1.447 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
   1.448 +  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
   1.449 +  int blkn;
   1.450 +  JBLOCKROW block;
   1.451 +  BITREAD_STATE_VARS;
   1.452 +
   1.453 +  /* Process restart marker if needed; may have to suspend */
   1.454 +  if (cinfo->restart_interval) {
   1.455 +    if (entropy->restarts_to_go == 0)
   1.456 +      if (! process_restart(cinfo))
   1.457 +	return FALSE;
   1.458 +  }
   1.459 +
   1.460 +  /* Not worth the cycles to check insufficient_data here,
   1.461 +   * since we will not change the data anyway if we read zeroes.
   1.462 +   */
   1.463 +
   1.464 +  /* Load up working state */
   1.465 +  BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   1.466 +
   1.467 +  /* Outer loop handles each block in the MCU */
   1.468 +
   1.469 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1.470 +    block = MCU_data[blkn];
   1.471 +
   1.472 +    /* Encoded data is simply the next bit of the two's-complement DC value */
   1.473 +    CHECK_BIT_BUFFER(br_state, 1, return FALSE);
   1.474 +    if (GET_BITS(1))
   1.475 +      (*block)[0] |= p1;
   1.476 +    /* Note: since we use |=, repeating the assignment later is safe */
   1.477 +  }
   1.478 +
   1.479 +  /* Completed MCU, so update state */
   1.480 +  BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1.481 +
   1.482 +  /* Account for restart interval (no-op if not using restarts) */
   1.483 +  entropy->restarts_to_go--;
   1.484 +
   1.485 +  return TRUE;
   1.486 +}
   1.487 +
   1.488 +
   1.489 +/*
   1.490 + * MCU decoding for AC successive approximation refinement scan.
   1.491 + */
   1.492 +
   1.493 +METHODDEF(boolean)
   1.494 +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.495 +{   
   1.496 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
   1.497 +  int Se = cinfo->Se;
   1.498 +  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
   1.499 +  int m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
   1.500 +  register int s, k, r;
   1.501 +  unsigned int EOBRUN;
   1.502 +  JBLOCKROW block;
   1.503 +  JCOEFPTR thiscoef;
   1.504 +  BITREAD_STATE_VARS;
   1.505 +  d_derived_tbl * tbl;
   1.506 +  int num_newnz;
   1.507 +  int newnz_pos[DCTSIZE2];
   1.508 +
   1.509 +  /* Process restart marker if needed; may have to suspend */
   1.510 +  if (cinfo->restart_interval) {
   1.511 +    if (entropy->restarts_to_go == 0)
   1.512 +      if (! process_restart(cinfo))
   1.513 +	return FALSE;
   1.514 +  }
   1.515 +
   1.516 +  /* If we've run out of data, don't modify the MCU.
   1.517 +   */
   1.518 +  if (! entropy->pub.insufficient_data) {
   1.519 +
   1.520 +    /* Load up working state */
   1.521 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   1.522 +    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
   1.523 +
   1.524 +    /* There is always only one block per MCU */
   1.525 +    block = MCU_data[0];
   1.526 +    tbl = entropy->ac_derived_tbl;
   1.527 +
   1.528 +    /* If we are forced to suspend, we must undo the assignments to any newly
   1.529 +     * nonzero coefficients in the block, because otherwise we'd get confused
   1.530 +     * next time about which coefficients were already nonzero.
   1.531 +     * But we need not undo addition of bits to already-nonzero coefficients;
   1.532 +     * instead, we can test the current bit to see if we already did it.
   1.533 +     */
   1.534 +    num_newnz = 0;
   1.535 +
   1.536 +    /* initialize coefficient loop counter to start of band */
   1.537 +    k = cinfo->Ss;
   1.538 +
   1.539 +    if (EOBRUN == 0) {
   1.540 +      for (; k <= Se; k++) {
   1.541 +	HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
   1.542 +	r = s >> 4;
   1.543 +	s &= 15;
   1.544 +	if (s) {
   1.545 +	  if (s != 1)		/* size of new coef should always be 1 */
   1.546 +	    WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
   1.547 +	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
   1.548 +	  if (GET_BITS(1))
   1.549 +	    s = p1;		/* newly nonzero coef is positive */
   1.550 +	  else
   1.551 +	    s = m1;		/* newly nonzero coef is negative */
   1.552 +	} else {
   1.553 +	  if (r != 15) {
   1.554 +	    EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */
   1.555 +	    if (r) {
   1.556 +	      CHECK_BIT_BUFFER(br_state, r, goto undoit);
   1.557 +	      r = GET_BITS(r);
   1.558 +	      EOBRUN += r;
   1.559 +	    }
   1.560 +	    break;		/* rest of block is handled by EOB logic */
   1.561 +	  }
   1.562 +	  /* note s = 0 for processing ZRL */
   1.563 +	}
   1.564 +	/* Advance over already-nonzero coefs and r still-zero coefs,
   1.565 +	 * appending correction bits to the nonzeroes.  A correction bit is 1
   1.566 +	 * if the absolute value of the coefficient must be increased.
   1.567 +	 */
   1.568 +	do {
   1.569 +	  thiscoef = *block + jpeg_natural_order[k];
   1.570 +	  if (*thiscoef != 0) {
   1.571 +	    CHECK_BIT_BUFFER(br_state, 1, goto undoit);
   1.572 +	    if (GET_BITS(1)) {
   1.573 +	      if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
   1.574 +		if (*thiscoef >= 0)
   1.575 +		  *thiscoef += p1;
   1.576 +		else
   1.577 +		  *thiscoef += m1;
   1.578 +	      }
   1.579 +	    }
   1.580 +	  } else {
   1.581 +	    if (--r < 0)
   1.582 +	      break;		/* reached target zero coefficient */
   1.583 +	  }
   1.584 +	  k++;
   1.585 +	} while (k <= Se);
   1.586 +	if (s) {
   1.587 +	  int pos = jpeg_natural_order[k];
   1.588 +	  /* Output newly nonzero coefficient */
   1.589 +	  (*block)[pos] = (JCOEF) s;
   1.590 +	  /* Remember its position in case we have to suspend */
   1.591 +	  newnz_pos[num_newnz++] = pos;
   1.592 +	}
   1.593 +      }
   1.594 +    }
   1.595 +
   1.596 +    if (EOBRUN > 0) {
   1.597 +      /* Scan any remaining coefficient positions after the end-of-band
   1.598 +       * (the last newly nonzero coefficient, if any).  Append a correction
   1.599 +       * bit to each already-nonzero coefficient.  A correction bit is 1
   1.600 +       * if the absolute value of the coefficient must be increased.
   1.601 +       */
   1.602 +      for (; k <= Se; k++) {
   1.603 +	thiscoef = *block + jpeg_natural_order[k];
   1.604 +	if (*thiscoef != 0) {
   1.605 +	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
   1.606 +	  if (GET_BITS(1)) {
   1.607 +	    if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
   1.608 +	      if (*thiscoef >= 0)
   1.609 +		*thiscoef += p1;
   1.610 +	      else
   1.611 +		*thiscoef += m1;
   1.612 +	    }
   1.613 +	  }
   1.614 +	}
   1.615 +      }
   1.616 +      /* Count one block completed in EOB run */
   1.617 +      EOBRUN--;
   1.618 +    }
   1.619 +
   1.620 +    /* Completed MCU, so update state */
   1.621 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1.622 +    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
   1.623 +  }
   1.624 +
   1.625 +  /* Account for restart interval (no-op if not using restarts) */
   1.626 +  entropy->restarts_to_go--;
   1.627 +
   1.628 +  return TRUE;
   1.629 +
   1.630 +undoit:
   1.631 +  /* Re-zero any output coefficients that we made newly nonzero */
   1.632 +  while (num_newnz > 0)
   1.633 +    (*block)[newnz_pos[--num_newnz]] = 0;
   1.634 +
   1.635 +  return FALSE;
   1.636 +}
   1.637 +
   1.638 +
   1.639 +/*
   1.640 + * Module initialization routine for progressive Huffman entropy decoding.
   1.641 + */
   1.642 +
   1.643 +GLOBAL(void)
   1.644 +jinit_phuff_decoder (j_decompress_ptr cinfo)
   1.645 +{
   1.646 +  phuff_entropy_ptr entropy;
   1.647 +  int *coef_bit_ptr;
   1.648 +  int ci, i;
   1.649 +
   1.650 +  entropy = (phuff_entropy_ptr)
   1.651 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.652 +				SIZEOF(phuff_entropy_decoder));
   1.653 +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
   1.654 +  entropy->pub.start_pass = start_pass_phuff_decoder;
   1.655 +
   1.656 +  /* Mark derived tables unallocated */
   1.657 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   1.658 +    entropy->derived_tbls[i] = NULL;
   1.659 +  }
   1.660 +
   1.661 +  /* Create progression status table */
   1.662 +  cinfo->coef_bits = (int (*)[DCTSIZE2])
   1.663 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.664 +				cinfo->num_components*DCTSIZE2*SIZEOF(int));
   1.665 +  coef_bit_ptr = & cinfo->coef_bits[0][0];
   1.666 +  for (ci = 0; ci < cinfo->num_components; ci++) 
   1.667 +    for (i = 0; i < DCTSIZE2; i++)
   1.668 +      *coef_bit_ptr++ = -1;
   1.669 +}
   1.670 +
   1.671 +#endif /* D_PROGRESSIVE_SUPPORTED */