3dphotoshoot

diff libs/libjpeg/jdhuff.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jdhuff.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,651 @@
     1.4 +/*
     1.5 + * jdhuff.c
     1.6 + *
     1.7 + * Copyright (C) 1991-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.
    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 jdphuff.c */
    1.24 +
    1.25 +
    1.26 +/*
    1.27 + * Expanded entropy decoder object for Huffman decoding.
    1.28 + *
    1.29 + * The savable_state subrecord contains fields that change within an MCU,
    1.30 + * but must not be updated permanently until we complete the MCU.
    1.31 + */
    1.32 +
    1.33 +typedef struct {
    1.34 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
    1.35 +} savable_state;
    1.36 +
    1.37 +/* This macro is to work around compilers with missing or broken
    1.38 + * structure assignment.  You'll need to fix this code if you have
    1.39 + * such a compiler and you change MAX_COMPS_IN_SCAN.
    1.40 + */
    1.41 +
    1.42 +#ifndef NO_STRUCT_ASSIGN
    1.43 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
    1.44 +#else
    1.45 +#if MAX_COMPS_IN_SCAN == 4
    1.46 +#define ASSIGN_STATE(dest,src)  \
    1.47 +	((dest).last_dc_val[0] = (src).last_dc_val[0], \
    1.48 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
    1.49 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
    1.50 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
    1.51 +#endif
    1.52 +#endif
    1.53 +
    1.54 +
    1.55 +typedef struct {
    1.56 +  struct jpeg_entropy_decoder pub; /* public fields */
    1.57 +
    1.58 +  /* These fields are loaded into local variables at start of each MCU.
    1.59 +   * In case of suspension, we exit WITHOUT updating them.
    1.60 +   */
    1.61 +  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
    1.62 +  savable_state saved;		/* Other state at start of MCU */
    1.63 +
    1.64 +  /* These fields are NOT loaded into local working state. */
    1.65 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
    1.66 +
    1.67 +  /* Pointers to derived tables (these workspaces have image lifespan) */
    1.68 +  d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
    1.69 +  d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
    1.70 +
    1.71 +  /* Precalculated info set up by start_pass for use in decode_mcu: */
    1.72 +
    1.73 +  /* Pointers to derived tables to be used for each block within an MCU */
    1.74 +  d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
    1.75 +  d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
    1.76 +  /* Whether we care about the DC and AC coefficient values for each block */
    1.77 +  boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
    1.78 +  boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
    1.79 +} huff_entropy_decoder;
    1.80 +
    1.81 +typedef huff_entropy_decoder * huff_entropy_ptr;
    1.82 +
    1.83 +
    1.84 +/*
    1.85 + * Initialize for a Huffman-compressed scan.
    1.86 + */
    1.87 +
    1.88 +METHODDEF(void)
    1.89 +start_pass_huff_decoder (j_decompress_ptr cinfo)
    1.90 +{
    1.91 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    1.92 +  int ci, blkn, dctbl, actbl;
    1.93 +  jpeg_component_info * compptr;
    1.94 +
    1.95 +  /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
    1.96 +   * This ought to be an error condition, but we make it a warning because
    1.97 +   * there are some baseline files out there with all zeroes in these bytes.
    1.98 +   */
    1.99 +  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
   1.100 +      cinfo->Ah != 0 || cinfo->Al != 0)
   1.101 +    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
   1.102 +
   1.103 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.104 +    compptr = cinfo->cur_comp_info[ci];
   1.105 +    dctbl = compptr->dc_tbl_no;
   1.106 +    actbl = compptr->ac_tbl_no;
   1.107 +    /* Compute derived values for Huffman tables */
   1.108 +    /* We may do this more than once for a table, but it's not expensive */
   1.109 +    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
   1.110 +			    & entropy->dc_derived_tbls[dctbl]);
   1.111 +    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
   1.112 +			    & entropy->ac_derived_tbls[actbl]);
   1.113 +    /* Initialize DC predictions to 0 */
   1.114 +    entropy->saved.last_dc_val[ci] = 0;
   1.115 +  }
   1.116 +
   1.117 +  /* Precalculate decoding info for each block in an MCU of this scan */
   1.118 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1.119 +    ci = cinfo->MCU_membership[blkn];
   1.120 +    compptr = cinfo->cur_comp_info[ci];
   1.121 +    /* Precalculate which table to use for each block */
   1.122 +    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
   1.123 +    entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
   1.124 +    /* Decide whether we really care about the coefficient values */
   1.125 +    if (compptr->component_needed) {
   1.126 +      entropy->dc_needed[blkn] = TRUE;
   1.127 +      /* we don't need the ACs if producing a 1/8th-size image */
   1.128 +      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
   1.129 +    } else {
   1.130 +      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
   1.131 +    }
   1.132 +  }
   1.133 +
   1.134 +  /* Initialize bitread state variables */
   1.135 +  entropy->bitstate.bits_left = 0;
   1.136 +  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
   1.137 +  entropy->pub.insufficient_data = FALSE;
   1.138 +
   1.139 +  /* Initialize restart counter */
   1.140 +  entropy->restarts_to_go = cinfo->restart_interval;
   1.141 +}
   1.142 +
   1.143 +
   1.144 +/*
   1.145 + * Compute the derived values for a Huffman table.
   1.146 + * This routine also performs some validation checks on the table.
   1.147 + *
   1.148 + * Note this is also used by jdphuff.c.
   1.149 + */
   1.150 +
   1.151 +GLOBAL(void)
   1.152 +jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
   1.153 +			 d_derived_tbl ** pdtbl)
   1.154 +{
   1.155 +  JHUFF_TBL *htbl;
   1.156 +  d_derived_tbl *dtbl;
   1.157 +  int p, i, l, si, numsymbols;
   1.158 +  int lookbits, ctr;
   1.159 +  char huffsize[257];
   1.160 +  unsigned int huffcode[257];
   1.161 +  unsigned int code;
   1.162 +
   1.163 +  /* Note that huffsize[] and huffcode[] are filled in code-length order,
   1.164 +   * paralleling the order of the symbols themselves in htbl->huffval[].
   1.165 +   */
   1.166 +
   1.167 +  /* Find the input Huffman table */
   1.168 +  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
   1.169 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
   1.170 +  htbl =
   1.171 +    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
   1.172 +  if (htbl == NULL)
   1.173 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
   1.174 +
   1.175 +  /* Allocate a workspace if we haven't already done so. */
   1.176 +  if (*pdtbl == NULL)
   1.177 +    *pdtbl = (d_derived_tbl *)
   1.178 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.179 +				  SIZEOF(d_derived_tbl));
   1.180 +  dtbl = *pdtbl;
   1.181 +  dtbl->pub = htbl;		/* fill in back link */
   1.182 +  
   1.183 +  /* Figure C.1: make table of Huffman code length for each symbol */
   1.184 +
   1.185 +  p = 0;
   1.186 +  for (l = 1; l <= 16; l++) {
   1.187 +    i = (int) htbl->bits[l];
   1.188 +    if (i < 0 || p + i > 256)	/* protect against table overrun */
   1.189 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
   1.190 +    while (i--)
   1.191 +      huffsize[p++] = (char) l;
   1.192 +  }
   1.193 +  huffsize[p] = 0;
   1.194 +  numsymbols = p;
   1.195 +  
   1.196 +  /* Figure C.2: generate the codes themselves */
   1.197 +  /* We also validate that the counts represent a legal Huffman code tree. */
   1.198 +  
   1.199 +  code = 0;
   1.200 +  si = huffsize[0];
   1.201 +  p = 0;
   1.202 +  while (huffsize[p]) {
   1.203 +    while (((int) huffsize[p]) == si) {
   1.204 +      huffcode[p++] = code;
   1.205 +      code++;
   1.206 +    }
   1.207 +    /* code is now 1 more than the last code used for codelength si; but
   1.208 +     * it must still fit in si bits, since no code is allowed to be all ones.
   1.209 +     */
   1.210 +    if (((INT32) code) >= (((INT32) 1) << si))
   1.211 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
   1.212 +    code <<= 1;
   1.213 +    si++;
   1.214 +  }
   1.215 +
   1.216 +  /* Figure F.15: generate decoding tables for bit-sequential decoding */
   1.217 +
   1.218 +  p = 0;
   1.219 +  for (l = 1; l <= 16; l++) {
   1.220 +    if (htbl->bits[l]) {
   1.221 +      /* valoffset[l] = huffval[] index of 1st symbol of code length l,
   1.222 +       * minus the minimum code of length l
   1.223 +       */
   1.224 +      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
   1.225 +      p += htbl->bits[l];
   1.226 +      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
   1.227 +    } else {
   1.228 +      dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
   1.229 +    }
   1.230 +  }
   1.231 +  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
   1.232 +
   1.233 +  /* Compute lookahead tables to speed up decoding.
   1.234 +   * First we set all the table entries to 0, indicating "too long";
   1.235 +   * then we iterate through the Huffman codes that are short enough and
   1.236 +   * fill in all the entries that correspond to bit sequences starting
   1.237 +   * with that code.
   1.238 +   */
   1.239 +
   1.240 +  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
   1.241 +
   1.242 +  p = 0;
   1.243 +  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
   1.244 +    for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
   1.245 +      /* l = current code's length, p = its index in huffcode[] & huffval[]. */
   1.246 +      /* Generate left-justified code followed by all possible bit sequences */
   1.247 +      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
   1.248 +      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
   1.249 +	dtbl->look_nbits[lookbits] = l;
   1.250 +	dtbl->look_sym[lookbits] = htbl->huffval[p];
   1.251 +	lookbits++;
   1.252 +      }
   1.253 +    }
   1.254 +  }
   1.255 +
   1.256 +  /* Validate symbols as being reasonable.
   1.257 +   * For AC tables, we make no check, but accept all byte values 0..255.
   1.258 +   * For DC tables, we require the symbols to be in range 0..15.
   1.259 +   * (Tighter bounds could be applied depending on the data depth and mode,
   1.260 +   * but this is sufficient to ensure safe decoding.)
   1.261 +   */
   1.262 +  if (isDC) {
   1.263 +    for (i = 0; i < numsymbols; i++) {
   1.264 +      int sym = htbl->huffval[i];
   1.265 +      if (sym < 0 || sym > 15)
   1.266 +	ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
   1.267 +    }
   1.268 +  }
   1.269 +}
   1.270 +
   1.271 +
   1.272 +/*
   1.273 + * Out-of-line code for bit fetching (shared with jdphuff.c).
   1.274 + * See jdhuff.h for info about usage.
   1.275 + * Note: current values of get_buffer and bits_left are passed as parameters,
   1.276 + * but are returned in the corresponding fields of the state struct.
   1.277 + *
   1.278 + * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
   1.279 + * of get_buffer to be used.  (On machines with wider words, an even larger
   1.280 + * buffer could be used.)  However, on some machines 32-bit shifts are
   1.281 + * quite slow and take time proportional to the number of places shifted.
   1.282 + * (This is true with most PC compilers, for instance.)  In this case it may
   1.283 + * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
   1.284 + * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
   1.285 + */
   1.286 +
   1.287 +#ifdef SLOW_SHIFT_32
   1.288 +#define MIN_GET_BITS  15	/* minimum allowable value */
   1.289 +#else
   1.290 +#define MIN_GET_BITS  (BIT_BUF_SIZE-7)
   1.291 +#endif
   1.292 +
   1.293 +
   1.294 +GLOBAL(boolean)
   1.295 +jpeg_fill_bit_buffer (bitread_working_state * state,
   1.296 +		      register bit_buf_type get_buffer, register int bits_left,
   1.297 +		      int nbits)
   1.298 +/* Load up the bit buffer to a depth of at least nbits */
   1.299 +{
   1.300 +  /* Copy heavily used state fields into locals (hopefully registers) */
   1.301 +  register const JOCTET * next_input_byte = state->next_input_byte;
   1.302 +  register size_t bytes_in_buffer = state->bytes_in_buffer;
   1.303 +  j_decompress_ptr cinfo = state->cinfo;
   1.304 +
   1.305 +  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
   1.306 +  /* (It is assumed that no request will be for more than that many bits.) */
   1.307 +  /* We fail to do so only if we hit a marker or are forced to suspend. */
   1.308 +
   1.309 +  if (cinfo->unread_marker == 0) {	/* cannot advance past a marker */
   1.310 +    while (bits_left < MIN_GET_BITS) {
   1.311 +      register int c;
   1.312 +
   1.313 +      /* Attempt to read a byte */
   1.314 +      if (bytes_in_buffer == 0) {
   1.315 +	if (! (*cinfo->src->fill_input_buffer) (cinfo))
   1.316 +	  return FALSE;
   1.317 +	next_input_byte = cinfo->src->next_input_byte;
   1.318 +	bytes_in_buffer = cinfo->src->bytes_in_buffer;
   1.319 +      }
   1.320 +      bytes_in_buffer--;
   1.321 +      c = GETJOCTET(*next_input_byte++);
   1.322 +
   1.323 +      /* If it's 0xFF, check and discard stuffed zero byte */
   1.324 +      if (c == 0xFF) {
   1.325 +	/* Loop here to discard any padding FF's on terminating marker,
   1.326 +	 * so that we can save a valid unread_marker value.  NOTE: we will
   1.327 +	 * accept multiple FF's followed by a 0 as meaning a single FF data
   1.328 +	 * byte.  This data pattern is not valid according to the standard.
   1.329 +	 */
   1.330 +	do {
   1.331 +	  if (bytes_in_buffer == 0) {
   1.332 +	    if (! (*cinfo->src->fill_input_buffer) (cinfo))
   1.333 +	      return FALSE;
   1.334 +	    next_input_byte = cinfo->src->next_input_byte;
   1.335 +	    bytes_in_buffer = cinfo->src->bytes_in_buffer;
   1.336 +	  }
   1.337 +	  bytes_in_buffer--;
   1.338 +	  c = GETJOCTET(*next_input_byte++);
   1.339 +	} while (c == 0xFF);
   1.340 +
   1.341 +	if (c == 0) {
   1.342 +	  /* Found FF/00, which represents an FF data byte */
   1.343 +	  c = 0xFF;
   1.344 +	} else {
   1.345 +	  /* Oops, it's actually a marker indicating end of compressed data.
   1.346 +	   * Save the marker code for later use.
   1.347 +	   * Fine point: it might appear that we should save the marker into
   1.348 +	   * bitread working state, not straight into permanent state.  But
   1.349 +	   * once we have hit a marker, we cannot need to suspend within the
   1.350 +	   * current MCU, because we will read no more bytes from the data
   1.351 +	   * source.  So it is OK to update permanent state right away.
   1.352 +	   */
   1.353 +	  cinfo->unread_marker = c;
   1.354 +	  /* See if we need to insert some fake zero bits. */
   1.355 +	  goto no_more_bytes;
   1.356 +	}
   1.357 +      }
   1.358 +
   1.359 +      /* OK, load c into get_buffer */
   1.360 +      get_buffer = (get_buffer << 8) | c;
   1.361 +      bits_left += 8;
   1.362 +    } /* end while */
   1.363 +  } else {
   1.364 +  no_more_bytes:
   1.365 +    /* We get here if we've read the marker that terminates the compressed
   1.366 +     * data segment.  There should be enough bits in the buffer register
   1.367 +     * to satisfy the request; if so, no problem.
   1.368 +     */
   1.369 +    if (nbits > bits_left) {
   1.370 +      /* Uh-oh.  Report corrupted data to user and stuff zeroes into
   1.371 +       * the data stream, so that we can produce some kind of image.
   1.372 +       * We use a nonvolatile flag to ensure that only one warning message
   1.373 +       * appears per data segment.
   1.374 +       */
   1.375 +      if (! cinfo->entropy->insufficient_data) {
   1.376 +	WARNMS(cinfo, JWRN_HIT_MARKER);
   1.377 +	cinfo->entropy->insufficient_data = TRUE;
   1.378 +      }
   1.379 +      /* Fill the buffer with zero bits */
   1.380 +      get_buffer <<= MIN_GET_BITS - bits_left;
   1.381 +      bits_left = MIN_GET_BITS;
   1.382 +    }
   1.383 +  }
   1.384 +
   1.385 +  /* Unload the local registers */
   1.386 +  state->next_input_byte = next_input_byte;
   1.387 +  state->bytes_in_buffer = bytes_in_buffer;
   1.388 +  state->get_buffer = get_buffer;
   1.389 +  state->bits_left = bits_left;
   1.390 +
   1.391 +  return TRUE;
   1.392 +}
   1.393 +
   1.394 +
   1.395 +/*
   1.396 + * Out-of-line code for Huffman code decoding.
   1.397 + * See jdhuff.h for info about usage.
   1.398 + */
   1.399 +
   1.400 +GLOBAL(int)
   1.401 +jpeg_huff_decode (bitread_working_state * state,
   1.402 +		  register bit_buf_type get_buffer, register int bits_left,
   1.403 +		  d_derived_tbl * htbl, int min_bits)
   1.404 +{
   1.405 +  register int l = min_bits;
   1.406 +  register INT32 code;
   1.407 +
   1.408 +  /* HUFF_DECODE has determined that the code is at least min_bits */
   1.409 +  /* bits long, so fetch that many bits in one swoop. */
   1.410 +
   1.411 +  CHECK_BIT_BUFFER(*state, l, return -1);
   1.412 +  code = GET_BITS(l);
   1.413 +
   1.414 +  /* Collect the rest of the Huffman code one bit at a time. */
   1.415 +  /* This is per Figure F.16 in the JPEG spec. */
   1.416 +
   1.417 +  while (code > htbl->maxcode[l]) {
   1.418 +    code <<= 1;
   1.419 +    CHECK_BIT_BUFFER(*state, 1, return -1);
   1.420 +    code |= GET_BITS(1);
   1.421 +    l++;
   1.422 +  }
   1.423 +
   1.424 +  /* Unload the local registers */
   1.425 +  state->get_buffer = get_buffer;
   1.426 +  state->bits_left = bits_left;
   1.427 +
   1.428 +  /* With garbage input we may reach the sentinel value l = 17. */
   1.429 +
   1.430 +  if (l > 16) {
   1.431 +    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
   1.432 +    return 0;			/* fake a zero as the safest result */
   1.433 +  }
   1.434 +
   1.435 +  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
   1.436 +}
   1.437 +
   1.438 +
   1.439 +/*
   1.440 + * Figure F.12: extend sign bit.
   1.441 + * On some machines, a shift and add will be faster than a table lookup.
   1.442 + */
   1.443 +
   1.444 +#ifdef AVOID_TABLES
   1.445 +
   1.446 +#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
   1.447 +
   1.448 +#else
   1.449 +
   1.450 +#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
   1.451 +
   1.452 +static const int extend_test[16] =   /* entry n is 2**(n-1) */
   1.453 +  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
   1.454 +    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
   1.455 +
   1.456 +static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
   1.457 +  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
   1.458 +    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
   1.459 +    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
   1.460 +    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
   1.461 +
   1.462 +#endif /* AVOID_TABLES */
   1.463 +
   1.464 +
   1.465 +/*
   1.466 + * Check for a restart marker & resynchronize decoder.
   1.467 + * Returns FALSE if must suspend.
   1.468 + */
   1.469 +
   1.470 +LOCAL(boolean)
   1.471 +process_restart (j_decompress_ptr cinfo)
   1.472 +{
   1.473 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   1.474 +  int ci;
   1.475 +
   1.476 +  /* Throw away any unused bits remaining in bit buffer; */
   1.477 +  /* include any full bytes in next_marker's count of discarded bytes */
   1.478 +  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
   1.479 +  entropy->bitstate.bits_left = 0;
   1.480 +
   1.481 +  /* Advance past the RSTn marker */
   1.482 +  if (! (*cinfo->marker->read_restart_marker) (cinfo))
   1.483 +    return FALSE;
   1.484 +
   1.485 +  /* Re-initialize DC predictions to 0 */
   1.486 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
   1.487 +    entropy->saved.last_dc_val[ci] = 0;
   1.488 +
   1.489 +  /* Reset restart counter */
   1.490 +  entropy->restarts_to_go = cinfo->restart_interval;
   1.491 +
   1.492 +  /* Reset out-of-data flag, unless read_restart_marker left us smack up
   1.493 +   * against a marker.  In that case we will end up treating the next data
   1.494 +   * segment as empty, and we can avoid producing bogus output pixels by
   1.495 +   * leaving the flag set.
   1.496 +   */
   1.497 +  if (cinfo->unread_marker == 0)
   1.498 +    entropy->pub.insufficient_data = FALSE;
   1.499 +
   1.500 +  return TRUE;
   1.501 +}
   1.502 +
   1.503 +
   1.504 +/*
   1.505 + * Decode and return one MCU's worth of Huffman-compressed coefficients.
   1.506 + * The coefficients are reordered from zigzag order into natural array order,
   1.507 + * but are not dequantized.
   1.508 + *
   1.509 + * The i'th block of the MCU is stored into the block pointed to by
   1.510 + * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
   1.511 + * (Wholesale zeroing is usually a little faster than retail...)
   1.512 + *
   1.513 + * Returns FALSE if data source requested suspension.  In that case no
   1.514 + * changes have been made to permanent state.  (Exception: some output
   1.515 + * coefficients may already have been assigned.  This is harmless for
   1.516 + * this module, since we'll just re-assign them on the next call.)
   1.517 + */
   1.518 +
   1.519 +METHODDEF(boolean)
   1.520 +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1.521 +{
   1.522 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   1.523 +  int blkn;
   1.524 +  BITREAD_STATE_VARS;
   1.525 +  savable_state state;
   1.526 +
   1.527 +  /* Process restart marker if needed; may have to suspend */
   1.528 +  if (cinfo->restart_interval) {
   1.529 +    if (entropy->restarts_to_go == 0)
   1.530 +      if (! process_restart(cinfo))
   1.531 +	return FALSE;
   1.532 +  }
   1.533 +
   1.534 +  /* If we've run out of data, just leave the MCU set to zeroes.
   1.535 +   * This way, we return uniform gray for the remainder of the segment.
   1.536 +   */
   1.537 +  if (! entropy->pub.insufficient_data) {
   1.538 +
   1.539 +    /* Load up working state */
   1.540 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   1.541 +    ASSIGN_STATE(state, entropy->saved);
   1.542 +
   1.543 +    /* Outer loop handles each block in the MCU */
   1.544 +
   1.545 +    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1.546 +      JBLOCKROW block = MCU_data[blkn];
   1.547 +      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
   1.548 +      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
   1.549 +      register int s, k, r;
   1.550 +
   1.551 +      /* Decode a single block's worth of coefficients */
   1.552 +
   1.553 +      /* Section F.2.2.1: decode the DC coefficient difference */
   1.554 +      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
   1.555 +      if (s) {
   1.556 +	CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1.557 +	r = GET_BITS(s);
   1.558 +	s = HUFF_EXTEND(r, s);
   1.559 +      }
   1.560 +
   1.561 +      if (entropy->dc_needed[blkn]) {
   1.562 +	/* Convert DC difference to actual value, update last_dc_val */
   1.563 +	int ci = cinfo->MCU_membership[blkn];
   1.564 +	s += state.last_dc_val[ci];
   1.565 +	state.last_dc_val[ci] = s;
   1.566 +	/* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
   1.567 +	(*block)[0] = (JCOEF) s;
   1.568 +      }
   1.569 +
   1.570 +      if (entropy->ac_needed[blkn]) {
   1.571 +
   1.572 +	/* Section F.2.2.2: decode the AC coefficients */
   1.573 +	/* Since zeroes are skipped, output area must be cleared beforehand */
   1.574 +	for (k = 1; k < DCTSIZE2; k++) {
   1.575 +	  HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
   1.576 +      
   1.577 +	  r = s >> 4;
   1.578 +	  s &= 15;
   1.579 +      
   1.580 +	  if (s) {
   1.581 +	    k += r;
   1.582 +	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1.583 +	    r = GET_BITS(s);
   1.584 +	    s = HUFF_EXTEND(r, s);
   1.585 +	    /* Output coefficient in natural (dezigzagged) order.
   1.586 +	     * Note: the extra entries in jpeg_natural_order[] will save us
   1.587 +	     * if k >= DCTSIZE2, which could happen if the data is corrupted.
   1.588 +	     */
   1.589 +	    (*block)[jpeg_natural_order[k]] = (JCOEF) s;
   1.590 +	  } else {
   1.591 +	    if (r != 15)
   1.592 +	      break;
   1.593 +	    k += 15;
   1.594 +	  }
   1.595 +	}
   1.596 +
   1.597 +      } else {
   1.598 +
   1.599 +	/* Section F.2.2.2: decode the AC coefficients */
   1.600 +	/* In this path we just discard the values */
   1.601 +	for (k = 1; k < DCTSIZE2; k++) {
   1.602 +	  HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
   1.603 +      
   1.604 +	  r = s >> 4;
   1.605 +	  s &= 15;
   1.606 +      
   1.607 +	  if (s) {
   1.608 +	    k += r;
   1.609 +	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1.610 +	    DROP_BITS(s);
   1.611 +	  } else {
   1.612 +	    if (r != 15)
   1.613 +	      break;
   1.614 +	    k += 15;
   1.615 +	  }
   1.616 +	}
   1.617 +
   1.618 +      }
   1.619 +    }
   1.620 +
   1.621 +    /* Completed MCU, so update state */
   1.622 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1.623 +    ASSIGN_STATE(entropy->saved, state);
   1.624 +  }
   1.625 +
   1.626 +  /* Account for restart interval (no-op if not using restarts) */
   1.627 +  entropy->restarts_to_go--;
   1.628 +
   1.629 +  return TRUE;
   1.630 +}
   1.631 +
   1.632 +
   1.633 +/*
   1.634 + * Module initialization routine for Huffman entropy decoding.
   1.635 + */
   1.636 +
   1.637 +GLOBAL(void)
   1.638 +jinit_huff_decoder (j_decompress_ptr cinfo)
   1.639 +{
   1.640 +  huff_entropy_ptr entropy;
   1.641 +  int i;
   1.642 +
   1.643 +  entropy = (huff_entropy_ptr)
   1.644 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.645 +				SIZEOF(huff_entropy_decoder));
   1.646 +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
   1.647 +  entropy->pub.start_pass = start_pass_huff_decoder;
   1.648 +  entropy->pub.decode_mcu = decode_mcu;
   1.649 +
   1.650 +  /* Mark tables unallocated */
   1.651 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   1.652 +    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
   1.653 +  }
   1.654 +}