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

darco 56a656
/*
darco 56a656
 * jdhuff.h
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 declarations for Huffman entropy decoding routines
darco 56a656
 * that are shared between the sequential decoder (jdhuff.c) and the
darco 56a656
 * progressive decoder (jdphuff.c).  No other modules need to see these.
darco 56a656
 */
darco 56a656
darco 56a656
/* Short forms of external names for systems with brain-damaged linkers. */
darco 56a656
darco 56a656
#ifdef NEED_SHORT_EXTERNAL_NAMES
darco 56a656
#define jpeg_make_d_derived_tbl	jMkDDerived
darco 56a656
#define jpeg_fill_bit_buffer	jFilBitBuf
darco 56a656
#define jpeg_huff_decode	jHufDecode
darco 56a656
#endif /* NEED_SHORT_EXTERNAL_NAMES */
darco 56a656
darco 56a656
darco 56a656
/* Derived data constructed for each Huffman table */
darco 56a656
darco 56a656
#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
darco 56a656
darco 56a656
typedef struct {
darco 56a656
  /* Basic tables: (element [0] of each array is unused) */
darco 56a656
  INT32 maxcode[18];		/* largest code of length k (-1 if none) */
darco 56a656
  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
darco 56a656
  INT32 valoffset[17];		/* huffval[] offset for codes of length k */
darco 56a656
  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
darco 56a656
   * the smallest code of length k; so given a code of length k, the
darco 56a656
   * corresponding symbol is huffval[code + valoffset[k]]
darco 56a656
   */
darco 56a656
darco 56a656
  /* Link to public Huffman table (needed only in jpeg_huff_decode) */
darco 56a656
  JHUFF_TBL *pub;
darco 56a656
darco 56a656
  /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
darco 56a656
   * the input data stream.  If the next Huffman code is no more
darco 56a656
   * than HUFF_LOOKAHEAD bits long, we can obtain its length and
darco 56a656
   * the corresponding symbol directly from these tables.
darco 56a656
   */
darco 56a656
  int look_nbits[1<
darco 56a656
  UINT8 look_sym[1<
darco 56a656
} d_derived_tbl;
darco 56a656
darco 56a656
/* Expand a Huffman table definition into the derived format */
darco 56a656
EXTERN(void) jpeg_make_d_derived_tbl
darco 56a656
	JPP((j_decompress_ptr cinfo, boolean isDC, int tblno,
darco 56a656
	     d_derived_tbl ** pdtbl));
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Fetching the next N bits from the input stream is a time-critical operation
darco 56a656
 * for the Huffman decoders.  We implement it with a combination of inline
darco 56a656
 * macros and out-of-line subroutines.  Note that N (the number of bits
darco 56a656
 * demanded at one time) never exceeds 15 for JPEG use.
darco 56a656
 *
darco 56a656
 * We read source bytes into get_buffer and dole out bits as needed.
darco 56a656
 * If get_buffer already contains enough bits, they are fetched in-line
darco 56a656
 * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough
darco 56a656
 * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
darco 56a656
 * as full as possible (not just to the number of bits needed; this
darco 56a656
 * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
darco 56a656
 * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
darco 56a656
 * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
darco 56a656
 * at least the requested number of bits --- dummy zeroes are inserted if
darco 56a656
 * necessary.
darco 56a656
 */
darco 56a656
darco 56a656
typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */
darco 56a656
#define BIT_BUF_SIZE  32	/* size of buffer in bits */
darco 56a656
darco 56a656
/* If long is > 32 bits on your machine, and shifting/masking longs is
darco 56a656
 * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
darco 56a656
 * appropriately should be a win.  Unfortunately we can't define the size
darco 56a656
 * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
darco 56a656
 * because not all machines measure sizeof in 8-bit bytes.
darco 56a656
 */
darco 56a656
darco 56a656
typedef struct {		/* Bitreading state saved across MCUs */
darco 56a656
  bit_buf_type get_buffer;	/* current bit-extraction buffer */
darco 56a656
  int bits_left;		/* # of unused bits in it */
darco 56a656
} bitread_perm_state;
darco 56a656
darco 56a656
typedef struct {		/* Bitreading working state within an MCU */
darco 56a656
  /* Current data source location */
darco 56a656
  /* We need a copy, rather than munging the original, in case of suspension */
darco 56a656
  const JOCTET * next_input_byte; /* => next byte to read from source */
darco 56a656
  size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
darco 56a656
  /* Bit input buffer --- note these values are kept in register variables,
darco 56a656
   * not in this struct, inside the inner loops.
darco 56a656
   */
darco 56a656
  bit_buf_type get_buffer;	/* current bit-extraction buffer */
darco 56a656
  int bits_left;		/* # of unused bits in it */
darco 56a656
  /* Pointer needed by jpeg_fill_bit_buffer. */
darco 56a656
  j_decompress_ptr cinfo;	/* back link to decompress master record */
darco 56a656
} bitread_working_state;
darco 56a656
darco 56a656
/* Macros to declare and load/save bitread local variables. */
darco 56a656
#define BITREAD_STATE_VARS  \
darco 56a656
	register bit_buf_type get_buffer;  \
darco 56a656
	register int bits_left;  \
darco 56a656
	bitread_working_state br_state
darco 56a656
darco 56a656
#define BITREAD_LOAD_STATE(cinfop,permstate)  \
darco 56a656
	br_state.cinfo = cinfop; \
darco 56a656
	br_state.next_input_byte = cinfop->src->next_input_byte; \
darco 56a656
	br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
darco 56a656
	get_buffer = permstate.get_buffer; \
darco 56a656
	bits_left = permstate.bits_left;
darco 56a656
darco 56a656
#define BITREAD_SAVE_STATE(cinfop,permstate)  \
darco 56a656
	cinfop->src->next_input_byte = br_state.next_input_byte; \
darco 56a656
	cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
darco 56a656
	permstate.get_buffer = get_buffer; \
darco 56a656
	permstate.bits_left = bits_left
darco 56a656
darco 56a656
/*
darco 56a656
 * These macros provide the in-line portion of bit fetching.
darco 56a656
 * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
darco 56a656
 * before using GET_BITS, PEEK_BITS, or DROP_BITS.
darco 56a656
 * The variables get_buffer and bits_left are assumed to be locals,
darco 56a656
 * but the state struct might not be (jpeg_huff_decode needs this).
darco 56a656
 *	CHECK_BIT_BUFFER(state,n,action);
darco 56a656
 *		Ensure there are N bits in get_buffer; if suspend, take action.
darco 56a656
 *      val = GET_BITS(n);
darco 56a656
 *		Fetch next N bits.
darco 56a656
 *      val = PEEK_BITS(n);
darco 56a656
 *		Fetch next N bits without removing them from the buffer.
darco 56a656
 *	DROP_BITS(n);
darco 56a656
 *		Discard next N bits.
darco 56a656
 * The value N should be a simple variable, not an expression, because it
darco 56a656
 * is evaluated multiple times.
darco 56a656
 */
darco 56a656
darco 56a656
#define CHECK_BIT_BUFFER(state,nbits,action) \
darco 56a656
	{ if (bits_left < (nbits)) {  \
darco 56a656
	    if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
darco 56a656
	      { action; }  \
darco 56a656
	    get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
darco 56a656
darco 56a656
#define GET_BITS(nbits) \
darco 56a656
	(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
darco 56a656
darco 56a656
#define PEEK_BITS(nbits) \
darco 56a656
	(((int) (get_buffer >> (bits_left -  (nbits)))) & ((1<<(nbits))-1))
darco 56a656
darco 56a656
#define DROP_BITS(nbits) \
darco 56a656
	(bits_left -= (nbits))
darco 56a656
darco 56a656
/* Load up the bit buffer to a depth of at least nbits */
darco 56a656
EXTERN(boolean) jpeg_fill_bit_buffer
darco 56a656
	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
darco 56a656
	     register int bits_left, int nbits));
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Code for extracting next Huffman-coded symbol from input bit stream.
darco 56a656
 * Again, this is time-critical and we make the main paths be macros.
darco 56a656
 *
darco 56a656
 * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
darco 56a656
 * without looping.  Usually, more than 95% of the Huffman codes will be 8
darco 56a656
 * or fewer bits long.  The few overlength codes are handled with a loop,
darco 56a656
 * which need not be inline code.
darco 56a656
 *
darco 56a656
 * Notes about the HUFF_DECODE macro:
darco 56a656
 * 1. Near the end of the data segment, we may fail to get enough bits
darco 56a656
 *    for a lookahead.  In that case, we do it the hard way.
darco 56a656
 * 2. If the lookahead table contains no entry, the next code must be
darco 56a656
 *    more than HUFF_LOOKAHEAD bits long.
darco 56a656
 * 3. jpeg_huff_decode returns -1 if forced to suspend.
darco 56a656
 */
darco 56a656
darco 56a656
#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
darco 56a656
{ register int nb, look; \
darco 56a656
  if (bits_left < HUFF_LOOKAHEAD) { \
darco 56a656
    if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
darco 56a656
    get_buffer = state.get_buffer; bits_left = state.bits_left; \
darco 56a656
    if (bits_left < HUFF_LOOKAHEAD) { \
darco 56a656
      nb = 1; goto slowlabel; \
darco 56a656
    } \
darco 56a656
  } \
darco 56a656
  look = PEEK_BITS(HUFF_LOOKAHEAD); \
darco 56a656
  if ((nb = htbl->look_nbits[look]) != 0) { \
darco 56a656
    DROP_BITS(nb); \
darco 56a656
    result = htbl->look_sym[look]; \
darco 56a656
  } else { \
darco 56a656
    nb = HUFF_LOOKAHEAD+1; \
darco 56a656
slowlabel: \
darco 56a656
    if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
darco 56a656
	{ failaction; } \
darco 56a656
    get_buffer = state.get_buffer; bits_left = state.bits_left; \
darco 56a656
  } \
darco 56a656
}
darco 56a656
darco 56a656
/* Out-of-line case for Huffman code fetching */
darco 56a656
EXTERN(int) jpeg_huff_decode
darco 56a656
	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
darco 56a656
	     register int bits_left, d_derived_tbl * htbl, int min_bits));