Blame gtkmm-osx/jpeg-6b/jdhuff.c

darco 56a656
/*
darco 56a656
 * jdhuff.c
darco 56a656
 *
darco 56a656
 * Copyright (C) 1991-1997, Thomas G. Lane.
darco 56a656
 * This file is part of the Independent JPEG Group's software.
darco 56a656
 * For conditions of distribution and use, see the accompanying README file.
darco 56a656
 *
darco 56a656
 * This file contains Huffman entropy decoding routines.
darco 56a656
 *
darco 56a656
 * Much of the complexity here has to do with supporting input suspension.
darco 56a656
 * If the data source module demands suspension, we want to be able to back
darco 56a656
 * up to the start of the current MCU.  To do this, we copy state variables
darco 56a656
 * into local working storage, and update them back to the permanent
darco 56a656
 * storage only upon successful completion of an MCU.
darco 56a656
 */
darco 56a656
darco 56a656
#define JPEG_INTERNALS
darco 56a656
#include "jinclude.h"
darco 56a656
#include "jpeglib.h"
darco 56a656
#include "jdhuff.h"		/* Declarations shared with jdphuff.c */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Expanded entropy decoder object for Huffman decoding.
darco 56a656
 *
darco 56a656
 * The savable_state subrecord contains fields that change within an MCU,
darco 56a656
 * but must not be updated permanently until we complete the MCU.
darco 56a656
 */
darco 56a656
darco 56a656
typedef struct {
darco 56a656
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
darco 56a656
} savable_state;
darco 56a656
darco 56a656
/* This macro is to work around compilers with missing or broken
darco 56a656
 * structure assignment.  You'll need to fix this code if you have
darco 56a656
 * such a compiler and you change MAX_COMPS_IN_SCAN.
darco 56a656
 */
darco 56a656
darco 56a656
#ifndef NO_STRUCT_ASSIGN
darco 56a656
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
darco 56a656
#else
darco 56a656
#if MAX_COMPS_IN_SCAN == 4
darco 56a656
#define ASSIGN_STATE(dest,src)  \
darco 56a656
	((dest).last_dc_val[0] = (src).last_dc_val[0], \
darco 56a656
	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
darco 56a656
	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
darco 56a656
	 (dest).last_dc_val[3] = (src).last_dc_val[3])
darco 56a656
#endif
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
typedef struct {
darco 56a656
  struct jpeg_entropy_decoder pub; /* public fields */
darco 56a656
darco 56a656
  /* These fields are loaded into local variables at start of each MCU.
darco 56a656
   * In case of suspension, we exit WITHOUT updating them.
darco 56a656
   */
darco 56a656
  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
darco 56a656
  savable_state saved;		/* Other state at start of MCU */
darco 56a656
darco 56a656
  /* These fields are NOT loaded into local working state. */
darco 56a656
  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
darco 56a656
darco 56a656
  /* Pointers to derived tables (these workspaces have image lifespan) */
darco 56a656
  d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
darco 56a656
  d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
darco 56a656
darco 56a656
  /* Precalculated info set up by start_pass for use in decode_mcu: */
darco 56a656
darco 56a656
  /* Pointers to derived tables to be used for each block within an MCU */
darco 56a656
  d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
darco 56a656
  d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
darco 56a656
  /* Whether we care about the DC and AC coefficient values for each block */
darco 56a656
  boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
darco 56a656
  boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
darco 56a656
} huff_entropy_decoder;
darco 56a656
darco 56a656
typedef huff_entropy_decoder * huff_entropy_ptr;
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Initialize for a Huffman-compressed scan.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
start_pass_huff_decoder (j_decompress_ptr cinfo)
darco 56a656
{
darco 56a656
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
darco 56a656
  int ci, blkn, dctbl, actbl;
darco 56a656
  jpeg_component_info * compptr;
darco 56a656
darco 56a656
  /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
darco 56a656
   * This ought to be an error condition, but we make it a warning because
darco 56a656
   * there are some baseline files out there with all zeroes in these bytes.
darco 56a656
   */
darco 56a656
  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
darco 56a656
      cinfo->Ah != 0 || cinfo->Al != 0)
darco 56a656
    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
darco 56a656
darco 56a656
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
darco 56a656
    compptr = cinfo->cur_comp_info[ci];
darco 56a656
    dctbl = compptr->dc_tbl_no;
darco 56a656
    actbl = compptr->ac_tbl_no;
darco 56a656
    /* Compute derived values for Huffman tables */
darco 56a656
    /* We may do this more than once for a table, but it's not expensive */
darco 56a656
    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
darco 56a656
			    & entropy->dc_derived_tbls[dctbl]);
darco 56a656
    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
darco 56a656
			    & entropy->ac_derived_tbls[actbl]);
darco 56a656
    /* Initialize DC predictions to 0 */
darco 56a656
    entropy->saved.last_dc_val[ci] = 0;
darco 56a656
  }
darco 56a656
darco 56a656
  /* Precalculate decoding info for each block in an MCU of this scan */
darco 56a656
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
darco 56a656
    ci = cinfo->MCU_membership[blkn];
darco 56a656
    compptr = cinfo->cur_comp_info[ci];
darco 56a656
    /* Precalculate which table to use for each block */
darco 56a656
    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
darco 56a656
    entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
darco 56a656
    /* Decide whether we really care about the coefficient values */
darco 56a656
    if (compptr->component_needed) {
darco 56a656
      entropy->dc_needed[blkn] = TRUE;
darco 56a656
      /* we don't need the ACs if producing a 1/8th-size image */
darco 56a656
      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
darco 56a656
    } else {
darco 56a656
      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
darco 56a656
    }
darco 56a656
  }
darco 56a656
darco 56a656
  /* Initialize bitread state variables */
darco 56a656
  entropy->bitstate.bits_left = 0;
darco 56a656
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
darco 56a656
  entropy->pub.insufficient_data = FALSE;
darco 56a656
darco 56a656
  /* Initialize restart counter */
darco 56a656
  entropy->restarts_to_go = cinfo->restart_interval;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Compute the derived values for a Huffman table.
darco 56a656
 * This routine also performs some validation checks on the table.
darco 56a656
 *
darco 56a656
 * Note this is also used by jdphuff.c.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
darco 56a656
			 d_derived_tbl ** pdtbl)
darco 56a656
{
darco 56a656
  JHUFF_TBL *htbl;
darco 56a656
  d_derived_tbl *dtbl;
darco 56a656
  int p, i, l, si, numsymbols;
darco 56a656
  int lookbits, ctr;
darco 56a656
  char huffsize[257];
darco 56a656
  unsigned int huffcode[257];
darco 56a656
  unsigned int code;
darco 56a656
darco 56a656
  /* Note that huffsize[] and huffcode[] are filled in code-length order,
darco 56a656
   * paralleling the order of the symbols themselves in htbl->huffval[].
darco 56a656
   */
darco 56a656
darco 56a656
  /* Find the input Huffman table */
darco 56a656
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
darco 56a656
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
darco 56a656
  htbl =
darco 56a656
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
darco 56a656
  if (htbl == NULL)
darco 56a656
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
darco 56a656
darco 56a656
  /* Allocate a workspace if we haven't already done so. */
darco 56a656
  if (*pdtbl == NULL)
darco 56a656
    *pdtbl = (d_derived_tbl *)
darco 56a656
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
darco 56a656
				  SIZEOF(d_derived_tbl));
darco 56a656
  dtbl = *pdtbl;
darco 56a656
  dtbl->pub = htbl;		/* fill in back link */
darco 56a656
  
darco 56a656
  /* Figure C.1: make table of Huffman code length for each symbol */
darco 56a656
darco 56a656
  p = 0;
darco 56a656
  for (l = 1; l <= 16; l++) {
darco 56a656
    i = (int) htbl->bits[l];
darco 56a656
    if (i < 0 || p + i > 256)	/* protect against table overrun */
darco 56a656
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
darco 56a656
    while (i--)
darco 56a656
      huffsize[p++] = (char) l;
darco 56a656
  }
darco 56a656
  huffsize[p] = 0;
darco 56a656
  numsymbols = p;
darco 56a656
  
darco 56a656
  /* Figure C.2: generate the codes themselves */
darco 56a656
  /* We also validate that the counts represent a legal Huffman code tree. */
darco 56a656
  
darco 56a656
  code = 0;
darco 56a656
  si = huffsize[0];
darco 56a656
  p = 0;
darco 56a656
  while (huffsize[p]) {
darco 56a656
    while (((int) huffsize[p]) == si) {
darco 56a656
      huffcode[p++] = code;
darco 56a656
      code++;
darco 56a656
    }
darco 56a656
    /* code is now 1 more than the last code used for codelength si; but
darco 56a656
     * it must still fit in si bits, since no code is allowed to be all ones.
darco 56a656
     */
darco 56a656
    if (((INT32) code) >= (((INT32) 1) << si))
darco 56a656
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
darco 56a656
    code <<= 1;
darco 56a656
    si++;
darco 56a656
  }
darco 56a656
darco 56a656
  /* Figure F.15: generate decoding tables for bit-sequential decoding */
darco 56a656
darco 56a656
  p = 0;
darco 56a656
  for (l = 1; l <= 16; l++) {
darco 56a656
    if (htbl->bits[l]) {
darco 56a656
      /* valoffset[l] = huffval[] index of 1st symbol of code length l,
darco 56a656
       * minus the minimum code of length l
darco 56a656
       */
darco 56a656
      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
darco 56a656
      p += htbl->bits[l];
darco 56a656
      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
darco 56a656
    } else {
darco 56a656
      dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
darco 56a656
    }
darco 56a656
  }
darco 56a656
  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
darco 56a656
darco 56a656
  /* Compute lookahead tables to speed up decoding.
darco 56a656
   * First we set all the table entries to 0, indicating "too long";
darco 56a656
   * then we iterate through the Huffman codes that are short enough and
darco 56a656
   * fill in all the entries that correspond to bit sequences starting
darco 56a656
   * with that code.
darco 56a656
   */
darco 56a656
darco 56a656
  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
darco 56a656
darco 56a656
  p = 0;
darco 56a656
  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
darco 56a656
    for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
darco 56a656
      /* l = current code's length, p = its index in huffcode[] & huffval[]. */
darco 56a656
      /* Generate left-justified code followed by all possible bit sequences */
darco 56a656
      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
darco 56a656
      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
darco 56a656
	dtbl->look_nbits[lookbits] = l;
darco 56a656
	dtbl->look_sym[lookbits] = htbl->huffval[p];
darco 56a656
	lookbits++;
darco 56a656
      }
darco 56a656
    }
darco 56a656
  }
darco 56a656
darco 56a656
  /* Validate symbols as being reasonable.
darco 56a656
   * For AC tables, we make no check, but accept all byte values 0..255.
darco 56a656
   * For DC tables, we require the symbols to be in range 0..15.
darco 56a656
   * (Tighter bounds could be applied depending on the data depth and mode,
darco 56a656
   * but this is sufficient to ensure safe decoding.)
darco 56a656
   */
darco 56a656
  if (isDC) {
darco 56a656
    for (i = 0; i < numsymbols; i++) {
darco 56a656
      int sym = htbl->huffval[i];
darco 56a656
      if (sym < 0 || sym > 15)
darco 56a656
	ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
darco 56a656
    }
darco 56a656
  }
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Out-of-line code for bit fetching (shared with jdphuff.c).
darco 56a656
 * See jdhuff.h for info about usage.
darco 56a656
 * Note: current values of get_buffer and bits_left are passed as parameters,
darco 56a656
 * but are returned in the corresponding fields of the state struct.
darco 56a656
 *
darco 56a656
 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
darco 56a656
 * of get_buffer to be used.  (On machines with wider words, an even larger
darco 56a656
 * buffer could be used.)  However, on some machines 32-bit shifts are
darco 56a656
 * quite slow and take time proportional to the number of places shifted.
darco 56a656
 * (This is true with most PC compilers, for instance.)  In this case it may
darco 56a656
 * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
darco 56a656
 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
darco 56a656
 */
darco 56a656
darco 56a656
#ifdef SLOW_SHIFT_32
darco 56a656
#define MIN_GET_BITS  15	/* minimum allowable value */
darco 56a656
#else
darco 56a656
#define MIN_GET_BITS  (BIT_BUF_SIZE-7)
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
GLOBAL(boolean)
darco 56a656
jpeg_fill_bit_buffer (bitread_working_state * state,
darco 56a656
		      register bit_buf_type get_buffer, register int bits_left,
darco 56a656
		      int nbits)
darco 56a656
/* Load up the bit buffer to a depth of at least nbits */
darco 56a656
{
darco 56a656
  /* Copy heavily used state fields into locals (hopefully registers) */
darco 56a656
  register const JOCTET * next_input_byte = state->next_input_byte;
darco 56a656
  register size_t bytes_in_buffer = state->bytes_in_buffer;
darco 56a656
  j_decompress_ptr cinfo = state->cinfo;
darco 56a656
darco 56a656
  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
darco 56a656
  /* (It is assumed that no request will be for more than that many bits.) */
darco 56a656
  /* We fail to do so only if we hit a marker or are forced to suspend. */
darco 56a656
darco 56a656
  if (cinfo->unread_marker == 0) {	/* cannot advance past a marker */
darco 56a656
    while (bits_left < MIN_GET_BITS) {
darco 56a656
      register int c;
darco 56a656
darco 56a656
      /* Attempt to read a byte */
darco 56a656
      if (bytes_in_buffer == 0) {
darco 56a656
	if (! (*cinfo->src->fill_input_buffer) (cinfo))
darco 56a656
	  return FALSE;
darco 56a656
	next_input_byte = cinfo->src->next_input_byte;
darco 56a656
	bytes_in_buffer = cinfo->src->bytes_in_buffer;
darco 56a656
      }
darco 56a656
      bytes_in_buffer--;
darco 56a656
      c = GETJOCTET(*next_input_byte++);
darco 56a656
darco 56a656
      /* If it's 0xFF, check and discard stuffed zero byte */
darco 56a656
      if (c == 0xFF) {
darco 56a656
	/* Loop here to discard any padding FF's on terminating marker,
darco 56a656
	 * so that we can save a valid unread_marker value.  NOTE: we will
darco 56a656
	 * accept multiple FF's followed by a 0 as meaning a single FF data
darco 56a656
	 * byte.  This data pattern is not valid according to the standard.
darco 56a656
	 */
darco 56a656
	do {
darco 56a656
	  if (bytes_in_buffer == 0) {
darco 56a656
	    if (! (*cinfo->src->fill_input_buffer) (cinfo))
darco 56a656
	      return FALSE;
darco 56a656
	    next_input_byte = cinfo->src->next_input_byte;
darco 56a656
	    bytes_in_buffer = cinfo->src->bytes_in_buffer;
darco 56a656
	  }
darco 56a656
	  bytes_in_buffer--;
darco 56a656
	  c = GETJOCTET(*next_input_byte++);
darco 56a656
	} while (c == 0xFF);
darco 56a656
darco 56a656
	if (c == 0) {
darco 56a656
	  /* Found FF/00, which represents an FF data byte */
darco 56a656
	  c = 0xFF;
darco 56a656
	} else {
darco 56a656
	  /* Oops, it's actually a marker indicating end of compressed data.
darco 56a656
	   * Save the marker code for later use.
darco 56a656
	   * Fine point: it might appear that we should save the marker into
darco 56a656
	   * bitread working state, not straight into permanent state.  But
darco 56a656
	   * once we have hit a marker, we cannot need to suspend within the
darco 56a656
	   * current MCU, because we will read no more bytes from the data
darco 56a656
	   * source.  So it is OK to update permanent state right away.
darco 56a656
	   */
darco 56a656
	  cinfo->unread_marker = c;
darco 56a656
	  /* See if we need to insert some fake zero bits. */
darco 56a656
	  goto no_more_bytes;
darco 56a656
	}
darco 56a656
      }
darco 56a656
darco 56a656
      /* OK, load c into get_buffer */
darco 56a656
      get_buffer = (get_buffer << 8) | c;
darco 56a656
      bits_left += 8;
darco 56a656
    } /* end while */
darco 56a656
  } else {
darco 56a656
  no_more_bytes:
darco 56a656
    /* We get here if we've read the marker that terminates the compressed
darco 56a656
     * data segment.  There should be enough bits in the buffer register
darco 56a656
     * to satisfy the request; if so, no problem.
darco 56a656
     */
darco 56a656
    if (nbits > bits_left) {
darco 56a656
      /* Uh-oh.  Report corrupted data to user and stuff zeroes into
darco 56a656
       * the data stream, so that we can produce some kind of image.
darco 56a656
       * We use a nonvolatile flag to ensure that only one warning message
darco 56a656
       * appears per data segment.
darco 56a656
       */
darco 56a656
      if (! cinfo->entropy->insufficient_data) {
darco 56a656
	WARNMS(cinfo, JWRN_HIT_MARKER);
darco 56a656
	cinfo->entropy->insufficient_data = TRUE;
darco 56a656
      }
darco 56a656
      /* Fill the buffer with zero bits */
darco 56a656
      get_buffer <<= MIN_GET_BITS - bits_left;
darco 56a656
      bits_left = MIN_GET_BITS;
darco 56a656
    }
darco 56a656
  }
darco 56a656
darco 56a656
  /* Unload the local registers */
darco 56a656
  state->next_input_byte = next_input_byte;
darco 56a656
  state->bytes_in_buffer = bytes_in_buffer;
darco 56a656
  state->get_buffer = get_buffer;
darco 56a656
  state->bits_left = bits_left;
darco 56a656
darco 56a656
  return TRUE;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Out-of-line code for Huffman code decoding.
darco 56a656
 * See jdhuff.h for info about usage.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(int)
darco 56a656
jpeg_huff_decode (bitread_working_state * state,
darco 56a656
		  register bit_buf_type get_buffer, register int bits_left,
darco 56a656
		  d_derived_tbl * htbl, int min_bits)
darco 56a656
{
darco 56a656
  register int l = min_bits;
darco 56a656
  register INT32 code;
darco 56a656
darco 56a656
  /* HUFF_DECODE has determined that the code is at least min_bits */
darco 56a656
  /* bits long, so fetch that many bits in one swoop. */
darco 56a656
darco 56a656
  CHECK_BIT_BUFFER(*state, l, return -1);
darco 56a656
  code = GET_BITS(l);
darco 56a656
darco 56a656
  /* Collect the rest of the Huffman code one bit at a time. */
darco 56a656
  /* This is per Figure F.16 in the JPEG spec. */
darco 56a656
darco 56a656
  while (code > htbl->maxcode[l]) {
darco 56a656
    code <<= 1;
darco 56a656
    CHECK_BIT_BUFFER(*state, 1, return -1);
darco 56a656
    code |= GET_BITS(1);
darco 56a656
    l++;
darco 56a656
  }
darco 56a656
darco 56a656
  /* Unload the local registers */
darco 56a656
  state->get_buffer = get_buffer;
darco 56a656
  state->bits_left = bits_left;
darco 56a656
darco 56a656
  /* With garbage input we may reach the sentinel value l = 17. */
darco 56a656
darco 56a656
  if (l > 16) {
darco 56a656
    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
darco 56a656
    return 0;			/* fake a zero as the safest result */
darco 56a656
  }
darco 56a656
darco 56a656
  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Figure F.12: extend sign bit.
darco 56a656
 * On some machines, a shift and add will be faster than a table lookup.
darco 56a656
 */
darco 56a656
darco 56a656
#ifdef AVOID_TABLES
darco 56a656
darco 56a656
#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
darco 56a656
darco 56a656
#else
darco 56a656
darco 56a656
#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
darco 56a656
darco 56a656
static const int extend_test[16] =   /* entry n is 2**(n-1) */
darco 56a656
  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
darco 56a656
    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
darco 56a656
darco 56a656
static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
darco 56a656
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
darco 56a656
    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
darco 56a656
    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
darco 56a656
    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
darco 56a656
darco 56a656
#endif /* AVOID_TABLES */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Check for a restart marker & resynchronize decoder.
darco 56a656
 * Returns FALSE if must suspend.
darco 56a656
 */
darco 56a656
darco 56a656
LOCAL(boolean)
darco 56a656
process_restart (j_decompress_ptr cinfo)
darco 56a656
{
darco 56a656
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
darco 56a656
  int ci;
darco 56a656
darco 56a656
  /* Throw away any unused bits remaining in bit buffer; */
darco 56a656
  /* include any full bytes in next_marker's count of discarded bytes */
darco 56a656
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
darco 56a656
  entropy->bitstate.bits_left = 0;
darco 56a656
darco 56a656
  /* Advance past the RSTn marker */
darco 56a656
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
darco 56a656
    return FALSE;
darco 56a656
darco 56a656
  /* Re-initialize DC predictions to 0 */
darco 56a656
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
darco 56a656
    entropy->saved.last_dc_val[ci] = 0;
darco 56a656
darco 56a656
  /* Reset restart counter */
darco 56a656
  entropy->restarts_to_go = cinfo->restart_interval;
darco 56a656
darco 56a656
  /* Reset out-of-data flag, unless read_restart_marker left us smack up
darco 56a656
   * against a marker.  In that case we will end up treating the next data
darco 56a656
   * segment as empty, and we can avoid producing bogus output pixels by
darco 56a656
   * leaving the flag set.
darco 56a656
   */
darco 56a656
  if (cinfo->unread_marker == 0)
darco 56a656
    entropy->pub.insufficient_data = FALSE;
darco 56a656
darco 56a656
  return TRUE;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Decode and return one MCU's worth of Huffman-compressed coefficients.
darco 56a656
 * The coefficients are reordered from zigzag order into natural array order,
darco 56a656
 * but are not dequantized.
darco 56a656
 *
darco 56a656
 * The i'th block of the MCU is stored into the block pointed to by
darco 56a656
 * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
darco 56a656
 * (Wholesale zeroing is usually a little faster than retail...)
darco 56a656
 *
darco 56a656
 * Returns FALSE if data source requested suspension.  In that case no
darco 56a656
 * changes have been made to permanent state.  (Exception: some output
darco 56a656
 * coefficients may already have been assigned.  This is harmless for
darco 56a656
 * this module, since we'll just re-assign them on the next call.)
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(boolean)
darco 56a656
decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
darco 56a656
{
darco 56a656
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
darco 56a656
  int blkn;
darco 56a656
  BITREAD_STATE_VARS;
darco 56a656
  savable_state state;
darco 56a656
darco 56a656
  /* Process restart marker if needed; may have to suspend */
darco 56a656
  if (cinfo->restart_interval) {
darco 56a656
    if (entropy->restarts_to_go == 0)
darco 56a656
      if (! process_restart(cinfo))
darco 56a656
	return FALSE;
darco 56a656
  }
darco 56a656
darco 56a656
  /* If we've run out of data, just leave the MCU set to zeroes.
darco 56a656
   * This way, we return uniform gray for the remainder of the segment.
darco 56a656
   */
darco 56a656
  if (! entropy->pub.insufficient_data) {
darco 56a656
darco 56a656
    /* Load up working state */
darco 56a656
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
darco 56a656
    ASSIGN_STATE(state, entropy->saved);
darco 56a656
darco 56a656
    /* Outer loop handles each block in the MCU */
darco 56a656
darco 56a656
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
darco 56a656
      JBLOCKROW block = MCU_data[blkn];
darco 56a656
      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
darco 56a656
      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
darco 56a656
      register int s, k, r;
darco 56a656
darco 56a656
      /* Decode a single block's worth of coefficients */
darco 56a656
darco 56a656
      /* Section F.2.2.1: decode the DC coefficient difference */
darco 56a656
      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
darco 56a656
      if (s) {
darco 56a656
	CHECK_BIT_BUFFER(br_state, s, return FALSE);
darco 56a656
	r = GET_BITS(s);
darco 56a656
	s = HUFF_EXTEND(r, s);
darco 56a656
      }
darco 56a656
darco 56a656
      if (entropy->dc_needed[blkn]) {
darco 56a656
	/* Convert DC difference to actual value, update last_dc_val */
darco 56a656
	int ci = cinfo->MCU_membership[blkn];
darco 56a656
	s += state.last_dc_val[ci];
darco 56a656
	state.last_dc_val[ci] = s;
darco 56a656
	/* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
darco 56a656
	(*block)[0] = (JCOEF) s;
darco 56a656
      }
darco 56a656
darco 56a656
      if (entropy->ac_needed[blkn]) {
darco 56a656
darco 56a656
	/* Section F.2.2.2: decode the AC coefficients */
darco 56a656
	/* Since zeroes are skipped, output area must be cleared beforehand */
darco 56a656
	for (k = 1; k < DCTSIZE2; k++) {
darco 56a656
	  HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
darco 56a656
      
darco 56a656
	  r = s >> 4;
darco 56a656
	  s &= 15;
darco 56a656
      
darco 56a656
	  if (s) {
darco 56a656
	    k += r;
darco 56a656
	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
darco 56a656
	    r = GET_BITS(s);
darco 56a656
	    s = HUFF_EXTEND(r, s);
darco 56a656
	    /* Output coefficient in natural (dezigzagged) order.
darco 56a656
	     * Note: the extra entries in jpeg_natural_order[] will save us
darco 56a656
	     * if k >= DCTSIZE2, which could happen if the data is corrupted.
darco 56a656
	     */
darco 56a656
	    (*block)[jpeg_natural_order[k]] = (JCOEF) s;
darco 56a656
	  } else {
darco 56a656
	    if (r != 15)
darco 56a656
	      break;
darco 56a656
	    k += 15;
darco 56a656
	  }
darco 56a656
	}
darco 56a656
darco 56a656
      } else {
darco 56a656
darco 56a656
	/* Section F.2.2.2: decode the AC coefficients */
darco 56a656
	/* In this path we just discard the values */
darco 56a656
	for (k = 1; k < DCTSIZE2; k++) {
darco 56a656
	  HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
darco 56a656
      
darco 56a656
	  r = s >> 4;
darco 56a656
	  s &= 15;
darco 56a656
      
darco 56a656
	  if (s) {
darco 56a656
	    k += r;
darco 56a656
	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
darco 56a656
	    DROP_BITS(s);
darco 56a656
	  } else {
darco 56a656
	    if (r != 15)
darco 56a656
	      break;
darco 56a656
	    k += 15;
darco 56a656
	  }
darco 56a656
	}
darco 56a656
darco 56a656
      }
darco 56a656
    }
darco 56a656
darco 56a656
    /* Completed MCU, so update state */
darco 56a656
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
darco 56a656
    ASSIGN_STATE(entropy->saved, state);
darco 56a656
  }
darco 56a656
darco 56a656
  /* Account for restart interval (no-op if not using restarts) */
darco 56a656
  entropy->restarts_to_go--;
darco 56a656
darco 56a656
  return TRUE;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Module initialization routine for Huffman entropy decoding.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jinit_huff_decoder (j_decompress_ptr cinfo)
darco 56a656
{
darco 56a656
  huff_entropy_ptr entropy;
darco 56a656
  int i;
darco 56a656
darco 56a656
  entropy = (huff_entropy_ptr)
darco 56a656
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
darco 56a656
				SIZEOF(huff_entropy_decoder));
darco 56a656
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
darco 56a656
  entropy->pub.start_pass = start_pass_huff_decoder;
darco 56a656
  entropy->pub.decode_mcu = decode_mcu;
darco 56a656
darco 56a656
  /* Mark tables unallocated */
darco 56a656
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
darco 56a656
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
darco 56a656
  }
darco 56a656
}