src/3rdparty/libjpeg/jdhuff.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     1 /*
     1 /*
     2  * jdhuff.c
     2  * jdhuff.c
     3  *
     3  *
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
       
     5  * Modified 2006-2009 by Guido Vollbeding.
     5  * This file is part of the Independent JPEG Group's software.
     6  * This file is part of the Independent JPEG Group's software.
     6  * For conditions of distribution and use, see the accompanying README file.
     7  * For conditions of distribution and use, see the accompanying README file.
     7  *
     8  *
     8  * This file contains Huffman entropy decoding routines.
     9  * This file contains Huffman entropy decoding routines.
       
    10  * Both sequential and progressive modes are supported in this single module.
     9  *
    11  *
    10  * Much of the complexity here has to do with supporting input suspension.
    12  * Much of the complexity here has to do with supporting input suspension.
    11  * If the data source module demands suspension, we want to be able to back
    13  * If the data source module demands suspension, we want to be able to back
    12  * up to the start of the current MCU.  To do this, we copy state variables
    14  * up to the start of the current MCU.  To do this, we copy state variables
    13  * into local working storage, and update them back to the permanent
    15  * into local working storage, and update them back to the permanent
    15  */
    17  */
    16 
    18 
    17 #define JPEG_INTERNALS
    19 #define JPEG_INTERNALS
    18 #include "jinclude.h"
    20 #include "jinclude.h"
    19 #include "jpeglib.h"
    21 #include "jpeglib.h"
    20 #include "jdhuff.h"		/* Declarations shared with jdphuff.c */
    22 
       
    23 
       
    24 /* Derived data constructed for each Huffman table */
       
    25 
       
    26 #define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
       
    27 
       
    28 typedef struct {
       
    29   /* Basic tables: (element [0] of each array is unused) */
       
    30   INT32 maxcode[18];		/* largest code of length k (-1 if none) */
       
    31   /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
       
    32   INT32 valoffset[17];		/* huffval[] offset for codes of length k */
       
    33   /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
       
    34    * the smallest code of length k; so given a code of length k, the
       
    35    * corresponding symbol is huffval[code + valoffset[k]]
       
    36    */
       
    37 
       
    38   /* Link to public Huffman table (needed only in jpeg_huff_decode) */
       
    39   JHUFF_TBL *pub;
       
    40 
       
    41   /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
       
    42    * the input data stream.  If the next Huffman code is no more
       
    43    * than HUFF_LOOKAHEAD bits long, we can obtain its length and
       
    44    * the corresponding symbol directly from these tables.
       
    45    */
       
    46   int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
       
    47   UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
       
    48 } d_derived_tbl;
       
    49 
       
    50 
       
    51 /*
       
    52  * Fetching the next N bits from the input stream is a time-critical operation
       
    53  * for the Huffman decoders.  We implement it with a combination of inline
       
    54  * macros and out-of-line subroutines.  Note that N (the number of bits
       
    55  * demanded at one time) never exceeds 15 for JPEG use.
       
    56  *
       
    57  * We read source bytes into get_buffer and dole out bits as needed.
       
    58  * If get_buffer already contains enough bits, they are fetched in-line
       
    59  * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough
       
    60  * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
       
    61  * as full as possible (not just to the number of bits needed; this
       
    62  * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
       
    63  * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
       
    64  * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
       
    65  * at least the requested number of bits --- dummy zeroes are inserted if
       
    66  * necessary.
       
    67  */
       
    68 
       
    69 typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */
       
    70 #define BIT_BUF_SIZE  32	/* size of buffer in bits */
       
    71 
       
    72 /* If long is > 32 bits on your machine, and shifting/masking longs is
       
    73  * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
       
    74  * appropriately should be a win.  Unfortunately we can't define the size
       
    75  * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
       
    76  * because not all machines measure sizeof in 8-bit bytes.
       
    77  */
       
    78 
       
    79 typedef struct {		/* Bitreading state saved across MCUs */
       
    80   bit_buf_type get_buffer;	/* current bit-extraction buffer */
       
    81   int bits_left;		/* # of unused bits in it */
       
    82 } bitread_perm_state;
       
    83 
       
    84 typedef struct {		/* Bitreading working state within an MCU */
       
    85   /* Current data source location */
       
    86   /* We need a copy, rather than munging the original, in case of suspension */
       
    87   const JOCTET * next_input_byte; /* => next byte to read from source */
       
    88   size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
       
    89   /* Bit input buffer --- note these values are kept in register variables,
       
    90    * not in this struct, inside the inner loops.
       
    91    */
       
    92   bit_buf_type get_buffer;	/* current bit-extraction buffer */
       
    93   int bits_left;		/* # of unused bits in it */
       
    94   /* Pointer needed by jpeg_fill_bit_buffer. */
       
    95   j_decompress_ptr cinfo;	/* back link to decompress master record */
       
    96 } bitread_working_state;
       
    97 
       
    98 /* Macros to declare and load/save bitread local variables. */
       
    99 #define BITREAD_STATE_VARS  \
       
   100 	register bit_buf_type get_buffer;  \
       
   101 	register int bits_left;  \
       
   102 	bitread_working_state br_state
       
   103 
       
   104 #define BITREAD_LOAD_STATE(cinfop,permstate)  \
       
   105 	br_state.cinfo = cinfop; \
       
   106 	br_state.next_input_byte = cinfop->src->next_input_byte; \
       
   107 	br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
       
   108 	get_buffer = permstate.get_buffer; \
       
   109 	bits_left = permstate.bits_left;
       
   110 
       
   111 #define BITREAD_SAVE_STATE(cinfop,permstate)  \
       
   112 	cinfop->src->next_input_byte = br_state.next_input_byte; \
       
   113 	cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
       
   114 	permstate.get_buffer = get_buffer; \
       
   115 	permstate.bits_left = bits_left
       
   116 
       
   117 /*
       
   118  * These macros provide the in-line portion of bit fetching.
       
   119  * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
       
   120  * before using GET_BITS, PEEK_BITS, or DROP_BITS.
       
   121  * The variables get_buffer and bits_left are assumed to be locals,
       
   122  * but the state struct might not be (jpeg_huff_decode needs this).
       
   123  *	CHECK_BIT_BUFFER(state,n,action);
       
   124  *		Ensure there are N bits in get_buffer; if suspend, take action.
       
   125  *      val = GET_BITS(n);
       
   126  *		Fetch next N bits.
       
   127  *      val = PEEK_BITS(n);
       
   128  *		Fetch next N bits without removing them from the buffer.
       
   129  *	DROP_BITS(n);
       
   130  *		Discard next N bits.
       
   131  * The value N should be a simple variable, not an expression, because it
       
   132  * is evaluated multiple times.
       
   133  */
       
   134 
       
   135 #define CHECK_BIT_BUFFER(state,nbits,action) \
       
   136 	{ if (bits_left < (nbits)) {  \
       
   137 	    if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
       
   138 	      { action; }  \
       
   139 	    get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
       
   140 
       
   141 #define GET_BITS(nbits) \
       
   142 	(((int) (get_buffer >> (bits_left -= (nbits)))) & BIT_MASK(nbits))
       
   143 
       
   144 #define PEEK_BITS(nbits) \
       
   145 	(((int) (get_buffer >> (bits_left -  (nbits)))) & BIT_MASK(nbits))
       
   146 
       
   147 #define DROP_BITS(nbits) \
       
   148 	(bits_left -= (nbits))
       
   149 
       
   150 
       
   151 /*
       
   152  * Code for extracting next Huffman-coded symbol from input bit stream.
       
   153  * Again, this is time-critical and we make the main paths be macros.
       
   154  *
       
   155  * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
       
   156  * without looping.  Usually, more than 95% of the Huffman codes will be 8
       
   157  * or fewer bits long.  The few overlength codes are handled with a loop,
       
   158  * which need not be inline code.
       
   159  *
       
   160  * Notes about the HUFF_DECODE macro:
       
   161  * 1. Near the end of the data segment, we may fail to get enough bits
       
   162  *    for a lookahead.  In that case, we do it the hard way.
       
   163  * 2. If the lookahead table contains no entry, the next code must be
       
   164  *    more than HUFF_LOOKAHEAD bits long.
       
   165  * 3. jpeg_huff_decode returns -1 if forced to suspend.
       
   166  */
       
   167 
       
   168 #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
       
   169 { register int nb, look; \
       
   170   if (bits_left < HUFF_LOOKAHEAD) { \
       
   171     if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
       
   172     get_buffer = state.get_buffer; bits_left = state.bits_left; \
       
   173     if (bits_left < HUFF_LOOKAHEAD) { \
       
   174       nb = 1; goto slowlabel; \
       
   175     } \
       
   176   } \
       
   177   look = PEEK_BITS(HUFF_LOOKAHEAD); \
       
   178   if ((nb = htbl->look_nbits[look]) != 0) { \
       
   179     DROP_BITS(nb); \
       
   180     result = htbl->look_sym[look]; \
       
   181   } else { \
       
   182     nb = HUFF_LOOKAHEAD+1; \
       
   183 slowlabel: \
       
   184     if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
       
   185 	{ failaction; } \
       
   186     get_buffer = state.get_buffer; bits_left = state.bits_left; \
       
   187   } \
       
   188 }
    21 
   189 
    22 
   190 
    23 /*
   191 /*
    24  * Expanded entropy decoder object for Huffman decoding.
   192  * Expanded entropy decoder object for Huffman decoding.
    25  *
   193  *
    26  * The savable_state subrecord contains fields that change within an MCU,
   194  * The savable_state subrecord contains fields that change within an MCU,
    27  * but must not be updated permanently until we complete the MCU.
   195  * but must not be updated permanently until we complete the MCU.
    28  */
   196  */
    29 
   197 
    30 typedef struct {
   198 typedef struct {
    31   int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   199   unsigned int EOBRUN;			/* remaining EOBs in EOBRUN */
       
   200   int last_dc_val[MAX_COMPS_IN_SCAN];	/* last DC coef for each component */
    32 } savable_state;
   201 } savable_state;
    33 
   202 
    34 /* This macro is to work around compilers with missing or broken
   203 /* This macro is to work around compilers with missing or broken
    35  * structure assignment.  You'll need to fix this code if you have
   204  * structure assignment.  You'll need to fix this code if you have
    36  * such a compiler and you change MAX_COMPS_IN_SCAN.
   205  * such a compiler and you change MAX_COMPS_IN_SCAN.
    39 #ifndef NO_STRUCT_ASSIGN
   208 #ifndef NO_STRUCT_ASSIGN
    40 #define ASSIGN_STATE(dest,src)  ((dest) = (src))
   209 #define ASSIGN_STATE(dest,src)  ((dest) = (src))
    41 #else
   210 #else
    42 #if MAX_COMPS_IN_SCAN == 4
   211 #if MAX_COMPS_IN_SCAN == 4
    43 #define ASSIGN_STATE(dest,src)  \
   212 #define ASSIGN_STATE(dest,src)  \
    44 	((dest).last_dc_val[0] = (src).last_dc_val[0], \
   213 	((dest).EOBRUN = (src).EOBRUN, \
       
   214 	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
    45 	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   215 	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
    46 	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   216 	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
    47 	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   217 	 (dest).last_dc_val[3] = (src).last_dc_val[3])
    48 #endif
   218 #endif
    49 #endif
   219 #endif
    57    */
   227    */
    58   bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
   228   bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
    59   savable_state saved;		/* Other state at start of MCU */
   229   savable_state saved;		/* Other state at start of MCU */
    60 
   230 
    61   /* These fields are NOT loaded into local working state. */
   231   /* These fields are NOT loaded into local working state. */
       
   232   boolean insufficient_data;	/* set TRUE after emitting warning */
    62   unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   233   unsigned int restarts_to_go;	/* MCUs left in this restart interval */
       
   234 
       
   235   /* Following two fields used only in progressive mode */
       
   236 
       
   237   /* Pointers to derived tables (these workspaces have image lifespan) */
       
   238   d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
       
   239 
       
   240   d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
       
   241 
       
   242   /* Following fields used only in sequential mode */
    63 
   243 
    64   /* Pointers to derived tables (these workspaces have image lifespan) */
   244   /* Pointers to derived tables (these workspaces have image lifespan) */
    65   d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
   245   d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
    66   d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
   246   d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
    67 
   247 
    69 
   249 
    70   /* Pointers to derived tables to be used for each block within an MCU */
   250   /* Pointers to derived tables to be used for each block within an MCU */
    71   d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
   251   d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
    72   d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
   252   d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
    73   /* Whether we care about the DC and AC coefficient values for each block */
   253   /* Whether we care about the DC and AC coefficient values for each block */
    74   boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
   254   int coef_limit[D_MAX_BLOCKS_IN_MCU];
    75   boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
       
    76 } huff_entropy_decoder;
   255 } huff_entropy_decoder;
    77 
   256 
    78 typedef huff_entropy_decoder * huff_entropy_ptr;
   257 typedef huff_entropy_decoder * huff_entropy_ptr;
    79 
   258 
    80 
   259 
    81 /*
   260 static const int jpeg_zigzag_order[8][8] = {
    82  * Initialize for a Huffman-compressed scan.
   261   {  0,  1,  5,  6, 14, 15, 27, 28 },
    83  */
   262   {  2,  4,  7, 13, 16, 26, 29, 42 },
    84 
   263   {  3,  8, 12, 17, 25, 30, 41, 43 },
    85 METHODDEF(void)
   264   {  9, 11, 18, 24, 31, 40, 44, 53 },
    86 start_pass_huff_decoder (j_decompress_ptr cinfo)
   265   { 10, 19, 23, 32, 39, 45, 52, 54 },
    87 {
   266   { 20, 22, 33, 38, 46, 51, 55, 60 },
    88   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   267   { 21, 34, 37, 47, 50, 56, 59, 61 },
    89   int ci, blkn, dctbl, actbl;
   268   { 35, 36, 48, 49, 57, 58, 62, 63 }
    90   jpeg_component_info * compptr;
   269 };
    91 
   270 
    92   /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
   271 static const int jpeg_zigzag_order7[7][7] = {
    93    * This ought to be an error condition, but we make it a warning because
   272   {  0,  1,  5,  6, 14, 15, 27 },
    94    * there are some baseline files out there with all zeroes in these bytes.
   273   {  2,  4,  7, 13, 16, 26, 28 },
    95    */
   274   {  3,  8, 12, 17, 25, 29, 38 },
    96   if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
   275   {  9, 11, 18, 24, 30, 37, 39 },
    97       cinfo->Ah != 0 || cinfo->Al != 0)
   276   { 10, 19, 23, 31, 36, 40, 45 },
    98     WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
   277   { 20, 22, 32, 35, 41, 44, 46 },
    99 
   278   { 21, 33, 34, 42, 43, 47, 48 }
   100   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   279 };
   101     compptr = cinfo->cur_comp_info[ci];
   280 
   102     dctbl = compptr->dc_tbl_no;
   281 static const int jpeg_zigzag_order6[6][6] = {
   103     actbl = compptr->ac_tbl_no;
   282   {  0,  1,  5,  6, 14, 15 },
   104     /* Compute derived values for Huffman tables */
   283   {  2,  4,  7, 13, 16, 25 },
   105     /* We may do this more than once for a table, but it's not expensive */
   284   {  3,  8, 12, 17, 24, 26 },
   106     jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
   285   {  9, 11, 18, 23, 27, 32 },
   107 			    & entropy->dc_derived_tbls[dctbl]);
   286   { 10, 19, 22, 28, 31, 33 },
   108     jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
   287   { 20, 21, 29, 30, 34, 35 }
   109 			    & entropy->ac_derived_tbls[actbl]);
   288 };
   110     /* Initialize DC predictions to 0 */
   289 
   111     entropy->saved.last_dc_val[ci] = 0;
   290 static const int jpeg_zigzag_order5[5][5] = {
   112   }
   291   {  0,  1,  5,  6, 14 },
   113 
   292   {  2,  4,  7, 13, 15 },
   114   /* Precalculate decoding info for each block in an MCU of this scan */
   293   {  3,  8, 12, 16, 21 },
   115   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   294   {  9, 11, 17, 20, 22 },
   116     ci = cinfo->MCU_membership[blkn];
   295   { 10, 18, 19, 23, 24 }
   117     compptr = cinfo->cur_comp_info[ci];
   296 };
   118     /* Precalculate which table to use for each block */
   297 
   119     entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
   298 static const int jpeg_zigzag_order4[4][4] = {
   120     entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
   299   { 0,  1,  5,  6 },
   121     /* Decide whether we really care about the coefficient values */
   300   { 2,  4,  7, 12 },
   122     if (compptr->component_needed) {
   301   { 3,  8, 11, 13 },
   123       entropy->dc_needed[blkn] = TRUE;
   302   { 9, 10, 14, 15 }
   124       /* we don't need the ACs if producing a 1/8th-size image */
   303 };
   125       entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
   304 
   126     } else {
   305 static const int jpeg_zigzag_order3[3][3] = {
   127       entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
   306   { 0, 1, 5 },
   128     }
   307   { 2, 4, 6 },
   129   }
   308   { 3, 7, 8 }
   130 
   309 };
   131   /* Initialize bitread state variables */
   310 
   132   entropy->bitstate.bits_left = 0;
   311 static const int jpeg_zigzag_order2[2][2] = {
   133   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
   312   { 0, 1 },
   134   entropy->pub.insufficient_data = FALSE;
   313   { 2, 3 }
   135 
   314 };
   136   /* Initialize restart counter */
       
   137   entropy->restarts_to_go = cinfo->restart_interval;
       
   138 }
       
   139 
   315 
   140 
   316 
   141 /*
   317 /*
   142  * Compute the derived values for a Huffman table.
   318  * Compute the derived values for a Huffman table.
   143  * This routine also performs some validation checks on the table.
   319  * This routine also performs some validation checks on the table.
   144  *
   320  */
   145  * Note this is also used by jdphuff.c.
   321 
   146  */
   322 LOCAL(void)
   147 
       
   148 GLOBAL(void)
       
   149 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
   323 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
   150 			 d_derived_tbl ** pdtbl)
   324 			 d_derived_tbl ** pdtbl)
   151 {
   325 {
   152   JHUFF_TBL *htbl;
   326   JHUFF_TBL *htbl;
   153   d_derived_tbl *dtbl;
   327   d_derived_tbl *dtbl;
   265   }
   439   }
   266 }
   440 }
   267 
   441 
   268 
   442 
   269 /*
   443 /*
   270  * Out-of-line code for bit fetching (shared with jdphuff.c).
   444  * Out-of-line code for bit fetching.
   271  * See jdhuff.h for info about usage.
       
   272  * Note: current values of get_buffer and bits_left are passed as parameters,
   445  * Note: current values of get_buffer and bits_left are passed as parameters,
   273  * but are returned in the corresponding fields of the state struct.
   446  * but are returned in the corresponding fields of the state struct.
   274  *
   447  *
   275  * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
   448  * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
   276  * of get_buffer to be used.  (On machines with wider words, an even larger
   449  * of get_buffer to be used.  (On machines with wider words, an even larger
   286 #else
   459 #else
   287 #define MIN_GET_BITS  (BIT_BUF_SIZE-7)
   460 #define MIN_GET_BITS  (BIT_BUF_SIZE-7)
   288 #endif
   461 #endif
   289 
   462 
   290 
   463 
   291 GLOBAL(boolean)
   464 LOCAL(boolean)
   292 jpeg_fill_bit_buffer (bitread_working_state * state,
   465 jpeg_fill_bit_buffer (bitread_working_state * state,
   293 		      register bit_buf_type get_buffer, register int bits_left,
   466 		      register bit_buf_type get_buffer, register int bits_left,
   294 		      int nbits)
   467 		      int nbits)
   295 /* Load up the bit buffer to a depth of at least nbits */
   468 /* Load up the bit buffer to a depth of at least nbits */
   296 {
   469 {
   367       /* Uh-oh.  Report corrupted data to user and stuff zeroes into
   540       /* Uh-oh.  Report corrupted data to user and stuff zeroes into
   368        * the data stream, so that we can produce some kind of image.
   541        * the data stream, so that we can produce some kind of image.
   369        * We use a nonvolatile flag to ensure that only one warning message
   542        * We use a nonvolatile flag to ensure that only one warning message
   370        * appears per data segment.
   543        * appears per data segment.
   371        */
   544        */
   372       if (! cinfo->entropy->insufficient_data) {
   545       if (! ((huff_entropy_ptr) cinfo->entropy)->insufficient_data) {
   373 	WARNMS(cinfo, JWRN_HIT_MARKER);
   546 	WARNMS(cinfo, JWRN_HIT_MARKER);
   374 	cinfo->entropy->insufficient_data = TRUE;
   547 	((huff_entropy_ptr) cinfo->entropy)->insufficient_data = TRUE;
   375       }
   548       }
   376       /* Fill the buffer with zero bits */
   549       /* Fill the buffer with zero bits */
   377       get_buffer <<= MIN_GET_BITS - bits_left;
   550       get_buffer <<= MIN_GET_BITS - bits_left;
   378       bits_left = MIN_GET_BITS;
   551       bits_left = MIN_GET_BITS;
   379     }
   552     }
   388   return TRUE;
   561   return TRUE;
   389 }
   562 }
   390 
   563 
   391 
   564 
   392 /*
   565 /*
       
   566  * Figure F.12: extend sign bit.
       
   567  * On some machines, a shift and sub will be faster than a table lookup.
       
   568  */
       
   569 
       
   570 #ifdef AVOID_TABLES
       
   571 
       
   572 #define BIT_MASK(nbits)   ((1<<(nbits))-1)
       
   573 #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) - ((1<<(s))-1) : (x))
       
   574 
       
   575 #else
       
   576 
       
   577 #define BIT_MASK(nbits)   bmask[nbits]
       
   578 #define HUFF_EXTEND(x,s)  ((x) <= bmask[(s) - 1] ? (x) - bmask[s] : (x))
       
   579 
       
   580 static const int bmask[16] =	/* bmask[n] is mask for n rightmost bits */
       
   581   { 0, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,
       
   582     0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF };
       
   583 
       
   584 #endif /* AVOID_TABLES */
       
   585 
       
   586 
       
   587 /*
   393  * Out-of-line code for Huffman code decoding.
   588  * Out-of-line code for Huffman code decoding.
   394  * See jdhuff.h for info about usage.
   589  */
   395  */
   590 
   396 
   591 LOCAL(int)
   397 GLOBAL(int)
       
   398 jpeg_huff_decode (bitread_working_state * state,
   592 jpeg_huff_decode (bitread_working_state * state,
   399 		  register bit_buf_type get_buffer, register int bits_left,
   593 		  register bit_buf_type get_buffer, register int bits_left,
   400 		  d_derived_tbl * htbl, int min_bits)
   594 		  d_derived_tbl * htbl, int min_bits)
   401 {
   595 {
   402   register int l = min_bits;
   596   register int l = min_bits;
   432   return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
   626   return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
   433 }
   627 }
   434 
   628 
   435 
   629 
   436 /*
   630 /*
   437  * Figure F.12: extend sign bit.
       
   438  * On some machines, a shift and add will be faster than a table lookup.
       
   439  */
       
   440 
       
   441 #ifdef AVOID_TABLES
       
   442 
       
   443 #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
       
   444 
       
   445 #else
       
   446 
       
   447 #define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
       
   448 
       
   449 static const int extend_test[16] =   /* entry n is 2**(n-1) */
       
   450   { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
       
   451     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
       
   452 
       
   453 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
       
   454   { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
       
   455     ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
       
   456     ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
       
   457     ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
       
   458 
       
   459 #endif /* AVOID_TABLES */
       
   460 
       
   461 
       
   462 /*
       
   463  * Check for a restart marker & resynchronize decoder.
   631  * Check for a restart marker & resynchronize decoder.
   464  * Returns FALSE if must suspend.
   632  * Returns FALSE if must suspend.
   465  */
   633  */
   466 
   634 
   467 LOCAL(boolean)
   635 LOCAL(boolean)
   480     return FALSE;
   648     return FALSE;
   481 
   649 
   482   /* Re-initialize DC predictions to 0 */
   650   /* Re-initialize DC predictions to 0 */
   483   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
   651   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
   484     entropy->saved.last_dc_val[ci] = 0;
   652     entropy->saved.last_dc_val[ci] = 0;
       
   653   /* Re-init EOB run count, too */
       
   654   entropy->saved.EOBRUN = 0;
   485 
   655 
   486   /* Reset restart counter */
   656   /* Reset restart counter */
   487   entropy->restarts_to_go = cinfo->restart_interval;
   657   entropy->restarts_to_go = cinfo->restart_interval;
   488 
   658 
   489   /* Reset out-of-data flag, unless read_restart_marker left us smack up
   659   /* Reset out-of-data flag, unless read_restart_marker left us smack up
   490    * against a marker.  In that case we will end up treating the next data
   660    * against a marker.  In that case we will end up treating the next data
   491    * segment as empty, and we can avoid producing bogus output pixels by
   661    * segment as empty, and we can avoid producing bogus output pixels by
   492    * leaving the flag set.
   662    * leaving the flag set.
   493    */
   663    */
   494   if (cinfo->unread_marker == 0)
   664   if (cinfo->unread_marker == 0)
   495     entropy->pub.insufficient_data = FALSE;
   665     entropy->insufficient_data = FALSE;
   496 
   666 
   497   return TRUE;
   667   return TRUE;
   498 }
   668 }
   499 
   669 
   500 
   670 
   501 /*
   671 /*
   502  * Decode and return one MCU's worth of Huffman-compressed coefficients.
   672  * Huffman MCU decoding.
       
   673  * Each of these routines decodes and returns one MCU's worth of
       
   674  * Huffman-compressed coefficients. 
   503  * The coefficients are reordered from zigzag order into natural array order,
   675  * The coefficients are reordered from zigzag order into natural array order,
   504  * but are not dequantized.
   676  * but are not dequantized.
   505  *
   677  *
   506  * The i'th block of the MCU is stored into the block pointed to by
   678  * The i'th block of the MCU is stored into the block pointed to by
   507  * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
   679  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
   508  * (Wholesale zeroing is usually a little faster than retail...)
   680  * (Wholesale zeroing is usually a little faster than retail...)
   509  *
   681  *
   510  * Returns FALSE if data source requested suspension.  In that case no
   682  * We return FALSE if data source requested suspension.  In that case no
   511  * changes have been made to permanent state.  (Exception: some output
   683  * changes have been made to permanent state.  (Exception: some output
   512  * coefficients may already have been assigned.  This is harmless for
   684  * coefficients may already have been assigned.  This is harmless for
   513  * this module, since we'll just re-assign them on the next call.)
   685  * spectral selection, since we'll just re-assign them on the next call.
       
   686  * Successive approximation AC refinement has to be more careful, however.)
       
   687  */
       
   688 
       
   689 /*
       
   690  * MCU decoding for DC initial scan (either spectral selection,
       
   691  * or first pass of successive approximation).
       
   692  */
       
   693 
       
   694 METHODDEF(boolean)
       
   695 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       
   696 {   
       
   697   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
       
   698   int Al = cinfo->Al;
       
   699   register int s, r;
       
   700   int blkn, ci;
       
   701   JBLOCKROW block;
       
   702   BITREAD_STATE_VARS;
       
   703   savable_state state;
       
   704   d_derived_tbl * tbl;
       
   705   jpeg_component_info * compptr;
       
   706 
       
   707   /* Process restart marker if needed; may have to suspend */
       
   708   if (cinfo->restart_interval) {
       
   709     if (entropy->restarts_to_go == 0)
       
   710       if (! process_restart(cinfo))
       
   711 	return FALSE;
       
   712   }
       
   713 
       
   714   /* If we've run out of data, just leave the MCU set to zeroes.
       
   715    * This way, we return uniform gray for the remainder of the segment.
       
   716    */
       
   717   if (! entropy->insufficient_data) {
       
   718 
       
   719     /* Load up working state */
       
   720     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
       
   721     ASSIGN_STATE(state, entropy->saved);
       
   722 
       
   723     /* Outer loop handles each block in the MCU */
       
   724 
       
   725     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
       
   726       block = MCU_data[blkn];
       
   727       ci = cinfo->MCU_membership[blkn];
       
   728       compptr = cinfo->cur_comp_info[ci];
       
   729       tbl = entropy->derived_tbls[compptr->dc_tbl_no];
       
   730 
       
   731       /* Decode a single block's worth of coefficients */
       
   732 
       
   733       /* Section F.2.2.1: decode the DC coefficient difference */
       
   734       HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
       
   735       if (s) {
       
   736 	CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
   737 	r = GET_BITS(s);
       
   738 	s = HUFF_EXTEND(r, s);
       
   739       }
       
   740 
       
   741       /* Convert DC difference to actual value, update last_dc_val */
       
   742       s += state.last_dc_val[ci];
       
   743       state.last_dc_val[ci] = s;
       
   744       /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
       
   745       (*block)[0] = (JCOEF) (s << Al);
       
   746     }
       
   747 
       
   748     /* Completed MCU, so update state */
       
   749     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
       
   750     ASSIGN_STATE(entropy->saved, state);
       
   751   }
       
   752 
       
   753   /* Account for restart interval (no-op if not using restarts) */
       
   754   entropy->restarts_to_go--;
       
   755 
       
   756   return TRUE;
       
   757 }
       
   758 
       
   759 
       
   760 /*
       
   761  * MCU decoding for AC initial scan (either spectral selection,
       
   762  * or first pass of successive approximation).
       
   763  */
       
   764 
       
   765 METHODDEF(boolean)
       
   766 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       
   767 {   
       
   768   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
       
   769   register int s, k, r;
       
   770   unsigned int EOBRUN;
       
   771   int Se, Al;
       
   772   const int * natural_order;
       
   773   JBLOCKROW block;
       
   774   BITREAD_STATE_VARS;
       
   775   d_derived_tbl * tbl;
       
   776 
       
   777   /* Process restart marker if needed; may have to suspend */
       
   778   if (cinfo->restart_interval) {
       
   779     if (entropy->restarts_to_go == 0)
       
   780       if (! process_restart(cinfo))
       
   781 	return FALSE;
       
   782   }
       
   783 
       
   784   /* If we've run out of data, just leave the MCU set to zeroes.
       
   785    * This way, we return uniform gray for the remainder of the segment.
       
   786    */
       
   787   if (! entropy->insufficient_data) {
       
   788 
       
   789     Se = cinfo->Se;
       
   790     Al = cinfo->Al;
       
   791     natural_order = cinfo->natural_order;
       
   792 
       
   793     /* Load up working state.
       
   794      * We can avoid loading/saving bitread state if in an EOB run.
       
   795      */
       
   796     EOBRUN = entropy->saved.EOBRUN;	/* only part of saved state we need */
       
   797 
       
   798     /* There is always only one block per MCU */
       
   799 
       
   800     if (EOBRUN > 0)		/* if it's a band of zeroes... */
       
   801       EOBRUN--;			/* ...process it now (we do nothing) */
       
   802     else {
       
   803       BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
       
   804       block = MCU_data[0];
       
   805       tbl = entropy->ac_derived_tbl;
       
   806 
       
   807       for (k = cinfo->Ss; k <= Se; k++) {
       
   808 	HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
       
   809 	r = s >> 4;
       
   810 	s &= 15;
       
   811 	if (s) {
       
   812 	  k += r;
       
   813 	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
   814 	  r = GET_BITS(s);
       
   815 	  s = HUFF_EXTEND(r, s);
       
   816 	  /* Scale and output coefficient in natural (dezigzagged) order */
       
   817 	  (*block)[natural_order[k]] = (JCOEF) (s << Al);
       
   818 	} else {
       
   819 	  if (r == 15) {	/* ZRL */
       
   820 	    k += 15;		/* skip 15 zeroes in band */
       
   821 	  } else {		/* EOBr, run length is 2^r + appended bits */
       
   822 	    EOBRUN = 1 << r;
       
   823 	    if (r) {		/* EOBr, r > 0 */
       
   824 	      CHECK_BIT_BUFFER(br_state, r, return FALSE);
       
   825 	      r = GET_BITS(r);
       
   826 	      EOBRUN += r;
       
   827 	    }
       
   828 	    EOBRUN--;		/* this band is processed at this moment */
       
   829 	    break;		/* force end-of-band */
       
   830 	  }
       
   831 	}
       
   832       }
       
   833 
       
   834       BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
       
   835     }
       
   836 
       
   837     /* Completed MCU, so update state */
       
   838     entropy->saved.EOBRUN = EOBRUN;	/* only part of saved state we need */
       
   839   }
       
   840 
       
   841   /* Account for restart interval (no-op if not using restarts) */
       
   842   entropy->restarts_to_go--;
       
   843 
       
   844   return TRUE;
       
   845 }
       
   846 
       
   847 
       
   848 /*
       
   849  * MCU decoding for DC successive approximation refinement scan.
       
   850  * Note: we assume such scans can be multi-component, although the spec
       
   851  * is not very clear on the point.
       
   852  */
       
   853 
       
   854 METHODDEF(boolean)
       
   855 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       
   856 {   
       
   857   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
       
   858   int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
       
   859   int blkn;
       
   860   JBLOCKROW block;
       
   861   BITREAD_STATE_VARS;
       
   862 
       
   863   /* Process restart marker if needed; may have to suspend */
       
   864   if (cinfo->restart_interval) {
       
   865     if (entropy->restarts_to_go == 0)
       
   866       if (! process_restart(cinfo))
       
   867 	return FALSE;
       
   868   }
       
   869 
       
   870   /* Not worth the cycles to check insufficient_data here,
       
   871    * since we will not change the data anyway if we read zeroes.
       
   872    */
       
   873 
       
   874   /* Load up working state */
       
   875   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
       
   876 
       
   877   /* Outer loop handles each block in the MCU */
       
   878 
       
   879   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
       
   880     block = MCU_data[blkn];
       
   881 
       
   882     /* Encoded data is simply the next bit of the two's-complement DC value */
       
   883     CHECK_BIT_BUFFER(br_state, 1, return FALSE);
       
   884     if (GET_BITS(1))
       
   885       (*block)[0] |= p1;
       
   886     /* Note: since we use |=, repeating the assignment later is safe */
       
   887   }
       
   888 
       
   889   /* Completed MCU, so update state */
       
   890   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
       
   891 
       
   892   /* Account for restart interval (no-op if not using restarts) */
       
   893   entropy->restarts_to_go--;
       
   894 
       
   895   return TRUE;
       
   896 }
       
   897 
       
   898 
       
   899 /*
       
   900  * MCU decoding for AC successive approximation refinement scan.
       
   901  */
       
   902 
       
   903 METHODDEF(boolean)
       
   904 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       
   905 {   
       
   906   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
       
   907   register int s, k, r;
       
   908   unsigned int EOBRUN;
       
   909   int Se, p1, m1;
       
   910   const int * natural_order;
       
   911   JBLOCKROW block;
       
   912   JCOEFPTR thiscoef;
       
   913   BITREAD_STATE_VARS;
       
   914   d_derived_tbl * tbl;
       
   915   int num_newnz;
       
   916   int newnz_pos[DCTSIZE2];
       
   917 
       
   918   /* Process restart marker if needed; may have to suspend */
       
   919   if (cinfo->restart_interval) {
       
   920     if (entropy->restarts_to_go == 0)
       
   921       if (! process_restart(cinfo))
       
   922 	return FALSE;
       
   923   }
       
   924 
       
   925   /* If we've run out of data, don't modify the MCU.
       
   926    */
       
   927   if (! entropy->insufficient_data) {
       
   928 
       
   929     Se = cinfo->Se;
       
   930     p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
       
   931     m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
       
   932     natural_order = cinfo->natural_order;
       
   933 
       
   934     /* Load up working state */
       
   935     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
       
   936     EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
       
   937 
       
   938     /* There is always only one block per MCU */
       
   939     block = MCU_data[0];
       
   940     tbl = entropy->ac_derived_tbl;
       
   941 
       
   942     /* If we are forced to suspend, we must undo the assignments to any newly
       
   943      * nonzero coefficients in the block, because otherwise we'd get confused
       
   944      * next time about which coefficients were already nonzero.
       
   945      * But we need not undo addition of bits to already-nonzero coefficients;
       
   946      * instead, we can test the current bit to see if we already did it.
       
   947      */
       
   948     num_newnz = 0;
       
   949 
       
   950     /* initialize coefficient loop counter to start of band */
       
   951     k = cinfo->Ss;
       
   952 
       
   953     if (EOBRUN == 0) {
       
   954       for (; k <= Se; k++) {
       
   955 	HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
       
   956 	r = s >> 4;
       
   957 	s &= 15;
       
   958 	if (s) {
       
   959 	  if (s != 1)		/* size of new coef should always be 1 */
       
   960 	    WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
       
   961 	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
       
   962 	  if (GET_BITS(1))
       
   963 	    s = p1;		/* newly nonzero coef is positive */
       
   964 	  else
       
   965 	    s = m1;		/* newly nonzero coef is negative */
       
   966 	} else {
       
   967 	  if (r != 15) {
       
   968 	    EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */
       
   969 	    if (r) {
       
   970 	      CHECK_BIT_BUFFER(br_state, r, goto undoit);
       
   971 	      r = GET_BITS(r);
       
   972 	      EOBRUN += r;
       
   973 	    }
       
   974 	    break;		/* rest of block is handled by EOB logic */
       
   975 	  }
       
   976 	  /* note s = 0 for processing ZRL */
       
   977 	}
       
   978 	/* Advance over already-nonzero coefs and r still-zero coefs,
       
   979 	 * appending correction bits to the nonzeroes.  A correction bit is 1
       
   980 	 * if the absolute value of the coefficient must be increased.
       
   981 	 */
       
   982 	do {
       
   983 	  thiscoef = *block + natural_order[k];
       
   984 	  if (*thiscoef != 0) {
       
   985 	    CHECK_BIT_BUFFER(br_state, 1, goto undoit);
       
   986 	    if (GET_BITS(1)) {
       
   987 	      if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
       
   988 		if (*thiscoef >= 0)
       
   989 		  *thiscoef += p1;
       
   990 		else
       
   991 		  *thiscoef += m1;
       
   992 	      }
       
   993 	    }
       
   994 	  } else {
       
   995 	    if (--r < 0)
       
   996 	      break;		/* reached target zero coefficient */
       
   997 	  }
       
   998 	  k++;
       
   999 	} while (k <= Se);
       
  1000 	if (s) {
       
  1001 	  int pos = natural_order[k];
       
  1002 	  /* Output newly nonzero coefficient */
       
  1003 	  (*block)[pos] = (JCOEF) s;
       
  1004 	  /* Remember its position in case we have to suspend */
       
  1005 	  newnz_pos[num_newnz++] = pos;
       
  1006 	}
       
  1007       }
       
  1008     }
       
  1009 
       
  1010     if (EOBRUN > 0) {
       
  1011       /* Scan any remaining coefficient positions after the end-of-band
       
  1012        * (the last newly nonzero coefficient, if any).  Append a correction
       
  1013        * bit to each already-nonzero coefficient.  A correction bit is 1
       
  1014        * if the absolute value of the coefficient must be increased.
       
  1015        */
       
  1016       for (; k <= Se; k++) {
       
  1017 	thiscoef = *block + natural_order[k];
       
  1018 	if (*thiscoef != 0) {
       
  1019 	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
       
  1020 	  if (GET_BITS(1)) {
       
  1021 	    if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
       
  1022 	      if (*thiscoef >= 0)
       
  1023 		*thiscoef += p1;
       
  1024 	      else
       
  1025 		*thiscoef += m1;
       
  1026 	    }
       
  1027 	  }
       
  1028 	}
       
  1029       }
       
  1030       /* Count one block completed in EOB run */
       
  1031       EOBRUN--;
       
  1032     }
       
  1033 
       
  1034     /* Completed MCU, so update state */
       
  1035     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
       
  1036     entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
       
  1037   }
       
  1038 
       
  1039   /* Account for restart interval (no-op if not using restarts) */
       
  1040   entropy->restarts_to_go--;
       
  1041 
       
  1042   return TRUE;
       
  1043 
       
  1044 undoit:
       
  1045   /* Re-zero any output coefficients that we made newly nonzero */
       
  1046   while (num_newnz > 0)
       
  1047     (*block)[newnz_pos[--num_newnz]] = 0;
       
  1048 
       
  1049   return FALSE;
       
  1050 }
       
  1051 
       
  1052 
       
  1053 /*
       
  1054  * Decode one MCU's worth of Huffman-compressed coefficients,
       
  1055  * partial blocks.
       
  1056  */
       
  1057 
       
  1058 METHODDEF(boolean)
       
  1059 decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       
  1060 {
       
  1061   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
       
  1062   const int * natural_order;
       
  1063   int Se, blkn;
       
  1064   BITREAD_STATE_VARS;
       
  1065   savable_state state;
       
  1066 
       
  1067   /* Process restart marker if needed; may have to suspend */
       
  1068   if (cinfo->restart_interval) {
       
  1069     if (entropy->restarts_to_go == 0)
       
  1070       if (! process_restart(cinfo))
       
  1071 	return FALSE;
       
  1072   }
       
  1073 
       
  1074   /* If we've run out of data, just leave the MCU set to zeroes.
       
  1075    * This way, we return uniform gray for the remainder of the segment.
       
  1076    */
       
  1077   if (! entropy->insufficient_data) {
       
  1078 
       
  1079     natural_order = cinfo->natural_order;
       
  1080     Se = cinfo->lim_Se;
       
  1081 
       
  1082     /* Load up working state */
       
  1083     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
       
  1084     ASSIGN_STATE(state, entropy->saved);
       
  1085 
       
  1086     /* Outer loop handles each block in the MCU */
       
  1087 
       
  1088     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
       
  1089       JBLOCKROW block = MCU_data[blkn];
       
  1090       d_derived_tbl * htbl;
       
  1091       register int s, k, r;
       
  1092       int coef_limit, ci;
       
  1093 
       
  1094       /* Decode a single block's worth of coefficients */
       
  1095 
       
  1096       /* Section F.2.2.1: decode the DC coefficient difference */
       
  1097       htbl = entropy->dc_cur_tbls[blkn];
       
  1098       HUFF_DECODE(s, br_state, htbl, return FALSE, label1);
       
  1099 
       
  1100       htbl = entropy->ac_cur_tbls[blkn];
       
  1101       k = 1;
       
  1102       coef_limit = entropy->coef_limit[blkn];
       
  1103       if (coef_limit) {
       
  1104 	/* Convert DC difference to actual value, update last_dc_val */
       
  1105 	if (s) {
       
  1106 	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
  1107 	  r = GET_BITS(s);
       
  1108 	  s = HUFF_EXTEND(r, s);
       
  1109 	}
       
  1110 	ci = cinfo->MCU_membership[blkn];
       
  1111 	s += state.last_dc_val[ci];
       
  1112 	state.last_dc_val[ci] = s;
       
  1113 	/* Output the DC coefficient */
       
  1114 	(*block)[0] = (JCOEF) s;
       
  1115 
       
  1116 	/* Section F.2.2.2: decode the AC coefficients */
       
  1117 	/* Since zeroes are skipped, output area must be cleared beforehand */
       
  1118 	for (; k < coef_limit; k++) {
       
  1119 	  HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
       
  1120 
       
  1121 	  r = s >> 4;
       
  1122 	  s &= 15;
       
  1123 
       
  1124 	  if (s) {
       
  1125 	    k += r;
       
  1126 	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
  1127 	    r = GET_BITS(s);
       
  1128 	    s = HUFF_EXTEND(r, s);
       
  1129 	    /* Output coefficient in natural (dezigzagged) order.
       
  1130 	     * Note: the extra entries in natural_order[] will save us
       
  1131 	     * if k > Se, which could happen if the data is corrupted.
       
  1132 	     */
       
  1133 	    (*block)[natural_order[k]] = (JCOEF) s;
       
  1134 	  } else {
       
  1135 	    if (r != 15)
       
  1136 	      goto EndOfBlock;
       
  1137 	    k += 15;
       
  1138 	  }
       
  1139 	}
       
  1140       } else {
       
  1141 	if (s) {
       
  1142 	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
  1143 	  DROP_BITS(s);
       
  1144 	}
       
  1145       }
       
  1146 
       
  1147       /* Section F.2.2.2: decode the AC coefficients */
       
  1148       /* In this path we just discard the values */
       
  1149       for (; k <= Se; k++) {
       
  1150 	HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
       
  1151 
       
  1152 	r = s >> 4;
       
  1153 	s &= 15;
       
  1154 
       
  1155 	if (s) {
       
  1156 	  k += r;
       
  1157 	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
  1158 	  DROP_BITS(s);
       
  1159 	} else {
       
  1160 	  if (r != 15)
       
  1161 	    break;
       
  1162 	  k += 15;
       
  1163 	}
       
  1164       }
       
  1165 
       
  1166       EndOfBlock: ;
       
  1167     }
       
  1168 
       
  1169     /* Completed MCU, so update state */
       
  1170     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
       
  1171     ASSIGN_STATE(entropy->saved, state);
       
  1172   }
       
  1173 
       
  1174   /* Account for restart interval (no-op if not using restarts) */
       
  1175   entropy->restarts_to_go--;
       
  1176 
       
  1177   return TRUE;
       
  1178 }
       
  1179 
       
  1180 
       
  1181 /*
       
  1182  * Decode one MCU's worth of Huffman-compressed coefficients,
       
  1183  * full-size blocks.
   514  */
  1184  */
   515 
  1185 
   516 METHODDEF(boolean)
  1186 METHODDEF(boolean)
   517 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  1187 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   518 {
  1188 {
   529   }
  1199   }
   530 
  1200 
   531   /* If we've run out of data, just leave the MCU set to zeroes.
  1201   /* If we've run out of data, just leave the MCU set to zeroes.
   532    * This way, we return uniform gray for the remainder of the segment.
  1202    * This way, we return uniform gray for the remainder of the segment.
   533    */
  1203    */
   534   if (! entropy->pub.insufficient_data) {
  1204   if (! entropy->insufficient_data) {
   535 
  1205 
   536     /* Load up working state */
  1206     /* Load up working state */
   537     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  1207     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   538     ASSIGN_STATE(state, entropy->saved);
  1208     ASSIGN_STATE(state, entropy->saved);
   539 
  1209 
   540     /* Outer loop handles each block in the MCU */
  1210     /* Outer loop handles each block in the MCU */
   541 
  1211 
   542     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  1212     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   543       JBLOCKROW block = MCU_data[blkn];
  1213       JBLOCKROW block = MCU_data[blkn];
   544       d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
  1214       d_derived_tbl * htbl;
   545       d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
       
   546       register int s, k, r;
  1215       register int s, k, r;
       
  1216       int coef_limit, ci;
   547 
  1217 
   548       /* Decode a single block's worth of coefficients */
  1218       /* Decode a single block's worth of coefficients */
   549 
  1219 
   550       /* Section F.2.2.1: decode the DC coefficient difference */
  1220       /* Section F.2.2.1: decode the DC coefficient difference */
   551       HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
  1221       htbl = entropy->dc_cur_tbls[blkn];
   552       if (s) {
  1222       HUFF_DECODE(s, br_state, htbl, return FALSE, label1);
   553 	CHECK_BIT_BUFFER(br_state, s, return FALSE);
  1223 
   554 	r = GET_BITS(s);
  1224       htbl = entropy->ac_cur_tbls[blkn];
   555 	s = HUFF_EXTEND(r, s);
  1225       k = 1;
   556       }
  1226       coef_limit = entropy->coef_limit[blkn];
   557 
  1227       if (coef_limit) {
   558       if (entropy->dc_needed[blkn]) {
       
   559 	/* Convert DC difference to actual value, update last_dc_val */
  1228 	/* Convert DC difference to actual value, update last_dc_val */
   560 	int ci = cinfo->MCU_membership[blkn];
  1229 	if (s) {
       
  1230 	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
  1231 	  r = GET_BITS(s);
       
  1232 	  s = HUFF_EXTEND(r, s);
       
  1233 	}
       
  1234 	ci = cinfo->MCU_membership[blkn];
   561 	s += state.last_dc_val[ci];
  1235 	s += state.last_dc_val[ci];
   562 	state.last_dc_val[ci] = s;
  1236 	state.last_dc_val[ci] = s;
   563 	/* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
  1237 	/* Output the DC coefficient */
   564 	(*block)[0] = (JCOEF) s;
  1238 	(*block)[0] = (JCOEF) s;
   565       }
       
   566 
       
   567       if (entropy->ac_needed[blkn]) {
       
   568 
  1239 
   569 	/* Section F.2.2.2: decode the AC coefficients */
  1240 	/* Section F.2.2.2: decode the AC coefficients */
   570 	/* Since zeroes are skipped, output area must be cleared beforehand */
  1241 	/* Since zeroes are skipped, output area must be cleared beforehand */
   571 	for (k = 1; k < DCTSIZE2; k++) {
  1242 	for (; k < coef_limit; k++) {
   572 	  HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
  1243 	  HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
   573       
  1244 
   574 	  r = s >> 4;
  1245 	  r = s >> 4;
   575 	  s &= 15;
  1246 	  s &= 15;
   576       
  1247 
   577 	  if (s) {
  1248 	  if (s) {
   578 	    k += r;
  1249 	    k += r;
   579 	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
  1250 	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
   580 	    r = GET_BITS(s);
  1251 	    r = GET_BITS(s);
   581 	    s = HUFF_EXTEND(r, s);
  1252 	    s = HUFF_EXTEND(r, s);
   584 	     * if k >= DCTSIZE2, which could happen if the data is corrupted.
  1255 	     * if k >= DCTSIZE2, which could happen if the data is corrupted.
   585 	     */
  1256 	     */
   586 	    (*block)[jpeg_natural_order[k]] = (JCOEF) s;
  1257 	    (*block)[jpeg_natural_order[k]] = (JCOEF) s;
   587 	  } else {
  1258 	  } else {
   588 	    if (r != 15)
  1259 	    if (r != 15)
   589 	      break;
  1260 	      goto EndOfBlock;
   590 	    k += 15;
  1261 	    k += 15;
   591 	  }
  1262 	  }
   592 	}
  1263 	}
   593 
       
   594       } else {
  1264       } else {
   595 
  1265 	if (s) {
   596 	/* Section F.2.2.2: decode the AC coefficients */
  1266 	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
   597 	/* In this path we just discard the values */
  1267 	  DROP_BITS(s);
   598 	for (k = 1; k < DCTSIZE2; k++) {
       
   599 	  HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
       
   600       
       
   601 	  r = s >> 4;
       
   602 	  s &= 15;
       
   603       
       
   604 	  if (s) {
       
   605 	    k += r;
       
   606 	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
   607 	    DROP_BITS(s);
       
   608 	  } else {
       
   609 	    if (r != 15)
       
   610 	      break;
       
   611 	    k += 15;
       
   612 	  }
       
   613 	}
  1268 	}
   614 
       
   615       }
  1269       }
       
  1270 
       
  1271       /* Section F.2.2.2: decode the AC coefficients */
       
  1272       /* In this path we just discard the values */
       
  1273       for (; k < DCTSIZE2; k++) {
       
  1274 	HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
       
  1275 
       
  1276 	r = s >> 4;
       
  1277 	s &= 15;
       
  1278 
       
  1279 	if (s) {
       
  1280 	  k += r;
       
  1281 	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
       
  1282 	  DROP_BITS(s);
       
  1283 	} else {
       
  1284 	  if (r != 15)
       
  1285 	    break;
       
  1286 	  k += 15;
       
  1287 	}
       
  1288       }
       
  1289 
       
  1290       EndOfBlock: ;
   616     }
  1291     }
   617 
  1292 
   618     /* Completed MCU, so update state */
  1293     /* Completed MCU, so update state */
   619     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  1294     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   620     ASSIGN_STATE(entropy->saved, state);
  1295     ASSIGN_STATE(entropy->saved, state);
   622 
  1297 
   623   /* Account for restart interval (no-op if not using restarts) */
  1298   /* Account for restart interval (no-op if not using restarts) */
   624   entropy->restarts_to_go--;
  1299   entropy->restarts_to_go--;
   625 
  1300 
   626   return TRUE;
  1301   return TRUE;
       
  1302 }
       
  1303 
       
  1304 
       
  1305 /*
       
  1306  * Initialize for a Huffman-compressed scan.
       
  1307  */
       
  1308 
       
  1309 METHODDEF(void)
       
  1310 start_pass_huff_decoder (j_decompress_ptr cinfo)
       
  1311 {
       
  1312   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
       
  1313   int ci, blkn, tbl, i;
       
  1314   jpeg_component_info * compptr;
       
  1315 
       
  1316   if (cinfo->progressive_mode) {
       
  1317     /* Validate progressive scan parameters */
       
  1318     if (cinfo->Ss == 0) {
       
  1319       if (cinfo->Se != 0)
       
  1320 	goto bad;
       
  1321     } else {
       
  1322       /* need not check Ss/Se < 0 since they came from unsigned bytes */
       
  1323       if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
       
  1324 	goto bad;
       
  1325       /* AC scans may have only one component */
       
  1326       if (cinfo->comps_in_scan != 1)
       
  1327 	goto bad;
       
  1328     }
       
  1329     if (cinfo->Ah != 0) {
       
  1330       /* Successive approximation refinement scan: must have Al = Ah-1. */
       
  1331       if (cinfo->Ah-1 != cinfo->Al)
       
  1332 	goto bad;
       
  1333     }
       
  1334     if (cinfo->Al > 13) {	/* need not check for < 0 */
       
  1335       /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
       
  1336        * but the spec doesn't say so, and we try to be liberal about what we
       
  1337        * accept.  Note: large Al values could result in out-of-range DC
       
  1338        * coefficients during early scans, leading to bizarre displays due to
       
  1339        * overflows in the IDCT math.  But we won't crash.
       
  1340        */
       
  1341       bad:
       
  1342       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
       
  1343 	       cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
       
  1344     }
       
  1345     /* Update progression status, and verify that scan order is legal.
       
  1346      * Note that inter-scan inconsistencies are treated as warnings
       
  1347      * not fatal errors ... not clear if this is right way to behave.
       
  1348      */
       
  1349     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
       
  1350       int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
       
  1351       int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
       
  1352       if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
       
  1353 	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
       
  1354       for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
       
  1355 	int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
       
  1356 	if (cinfo->Ah != expected)
       
  1357 	  WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
       
  1358 	coef_bit_ptr[coefi] = cinfo->Al;
       
  1359       }
       
  1360     }
       
  1361 
       
  1362     /* Select MCU decoding routine */
       
  1363     if (cinfo->Ah == 0) {
       
  1364       if (cinfo->Ss == 0)
       
  1365 	entropy->pub.decode_mcu = decode_mcu_DC_first;
       
  1366       else
       
  1367 	entropy->pub.decode_mcu = decode_mcu_AC_first;
       
  1368     } else {
       
  1369       if (cinfo->Ss == 0)
       
  1370 	entropy->pub.decode_mcu = decode_mcu_DC_refine;
       
  1371       else
       
  1372 	entropy->pub.decode_mcu = decode_mcu_AC_refine;
       
  1373     }
       
  1374 
       
  1375     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
       
  1376       compptr = cinfo->cur_comp_info[ci];
       
  1377       /* Make sure requested tables are present, and compute derived tables.
       
  1378        * We may build same derived table more than once, but it's not expensive.
       
  1379        */
       
  1380       if (cinfo->Ss == 0) {
       
  1381 	if (cinfo->Ah == 0) {	/* DC refinement needs no table */
       
  1382 	  tbl = compptr->dc_tbl_no;
       
  1383 	  jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
       
  1384 				  & entropy->derived_tbls[tbl]);
       
  1385 	}
       
  1386       } else {
       
  1387 	tbl = compptr->ac_tbl_no;
       
  1388 	jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
       
  1389 				& entropy->derived_tbls[tbl]);
       
  1390 	/* remember the single active table */
       
  1391 	entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
       
  1392       }
       
  1393       /* Initialize DC predictions to 0 */
       
  1394       entropy->saved.last_dc_val[ci] = 0;
       
  1395     }
       
  1396 
       
  1397     /* Initialize private state variables */
       
  1398     entropy->saved.EOBRUN = 0;
       
  1399   } else {
       
  1400     /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
       
  1401      * This ought to be an error condition, but we make it a warning because
       
  1402      * there are some baseline files out there with all zeroes in these bytes.
       
  1403      */
       
  1404     if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
       
  1405 	((cinfo->is_baseline || cinfo->Se < DCTSIZE2) &&
       
  1406 	cinfo->Se != cinfo->lim_Se))
       
  1407       WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
       
  1408 
       
  1409     /* Select MCU decoding routine */
       
  1410     /* We retain the hard-coded case for full-size blocks.
       
  1411      * This is not necessary, but it appears that this version is slightly
       
  1412      * more performant in the given implementation.
       
  1413      * With an improved implementation we would prefer a single optimized
       
  1414      * function.
       
  1415      */
       
  1416     if (cinfo->lim_Se != DCTSIZE2-1)
       
  1417       entropy->pub.decode_mcu = decode_mcu_sub;
       
  1418     else
       
  1419       entropy->pub.decode_mcu = decode_mcu;
       
  1420 
       
  1421     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
       
  1422       compptr = cinfo->cur_comp_info[ci];
       
  1423       /* Compute derived values for Huffman tables */
       
  1424       /* We may do this more than once for a table, but it's not expensive */
       
  1425       tbl = compptr->dc_tbl_no;
       
  1426       jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
       
  1427 			      & entropy->dc_derived_tbls[tbl]);
       
  1428       if (cinfo->lim_Se) {	/* AC needs no table when not present */
       
  1429 	tbl = compptr->ac_tbl_no;
       
  1430 	jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
       
  1431 				& entropy->ac_derived_tbls[tbl]);
       
  1432       }
       
  1433       /* Initialize DC predictions to 0 */
       
  1434       entropy->saved.last_dc_val[ci] = 0;
       
  1435     }
       
  1436 
       
  1437     /* Precalculate decoding info for each block in an MCU of this scan */
       
  1438     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
       
  1439       ci = cinfo->MCU_membership[blkn];
       
  1440       compptr = cinfo->cur_comp_info[ci];
       
  1441       /* Precalculate which table to use for each block */
       
  1442       entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
       
  1443       entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
       
  1444       /* Decide whether we really care about the coefficient values */
       
  1445       if (compptr->component_needed) {
       
  1446 	ci = compptr->DCT_v_scaled_size;
       
  1447 	i = compptr->DCT_h_scaled_size;
       
  1448 	switch (cinfo->lim_Se) {
       
  1449 	case (1*1-1):
       
  1450 	  entropy->coef_limit[blkn] = 1;
       
  1451 	  break;
       
  1452 	case (2*2-1):
       
  1453 	  if (ci <= 0 || ci > 2) ci = 2;
       
  1454 	  if (i <= 0 || i > 2) i = 2;
       
  1455 	  entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order2[ci - 1][i - 1];
       
  1456 	  break;
       
  1457 	case (3*3-1):
       
  1458 	  if (ci <= 0 || ci > 3) ci = 3;
       
  1459 	  if (i <= 0 || i > 3) i = 3;
       
  1460 	  entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order3[ci - 1][i - 1];
       
  1461 	  break;
       
  1462 	case (4*4-1):
       
  1463 	  if (ci <= 0 || ci > 4) ci = 4;
       
  1464 	  if (i <= 0 || i > 4) i = 4;
       
  1465 	  entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order4[ci - 1][i - 1];
       
  1466 	  break;
       
  1467 	case (5*5-1):
       
  1468 	  if (ci <= 0 || ci > 5) ci = 5;
       
  1469 	  if (i <= 0 || i > 5) i = 5;
       
  1470 	  entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order5[ci - 1][i - 1];
       
  1471 	  break;
       
  1472 	case (6*6-1):
       
  1473 	  if (ci <= 0 || ci > 6) ci = 6;
       
  1474 	  if (i <= 0 || i > 6) i = 6;
       
  1475 	  entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order6[ci - 1][i - 1];
       
  1476 	  break;
       
  1477 	case (7*7-1):
       
  1478 	  if (ci <= 0 || ci > 7) ci = 7;
       
  1479 	  if (i <= 0 || i > 7) i = 7;
       
  1480 	  entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order7[ci - 1][i - 1];
       
  1481 	  break;
       
  1482 	default:
       
  1483 	  if (ci <= 0 || ci > 8) ci = 8;
       
  1484 	  if (i <= 0 || i > 8) i = 8;
       
  1485 	  entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1];
       
  1486 	  break;
       
  1487 	}
       
  1488       } else {
       
  1489 	entropy->coef_limit[blkn] = 0;
       
  1490       }
       
  1491     }
       
  1492   }
       
  1493 
       
  1494   /* Initialize bitread state variables */
       
  1495   entropy->bitstate.bits_left = 0;
       
  1496   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
       
  1497   entropy->insufficient_data = FALSE;
       
  1498 
       
  1499   /* Initialize restart counter */
       
  1500   entropy->restarts_to_go = cinfo->restart_interval;
   627 }
  1501 }
   628 
  1502 
   629 
  1503 
   630 /*
  1504 /*
   631  * Module initialization routine for Huffman entropy decoding.
  1505  * Module initialization routine for Huffman entropy decoding.
   640   entropy = (huff_entropy_ptr)
  1514   entropy = (huff_entropy_ptr)
   641     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1515     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   642 				SIZEOF(huff_entropy_decoder));
  1516 				SIZEOF(huff_entropy_decoder));
   643   cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  1517   cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
   644   entropy->pub.start_pass = start_pass_huff_decoder;
  1518   entropy->pub.start_pass = start_pass_huff_decoder;
   645   entropy->pub.decode_mcu = decode_mcu;
  1519 
   646 
  1520   if (cinfo->progressive_mode) {
   647   /* Mark tables unallocated */
  1521     /* Create progression status table */
   648   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  1522     int *coef_bit_ptr, ci;
   649     entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  1523     cinfo->coef_bits = (int (*)[DCTSIZE2])
       
  1524       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
       
  1525 				  cinfo->num_components*DCTSIZE2*SIZEOF(int));
       
  1526     coef_bit_ptr = & cinfo->coef_bits[0][0];
       
  1527     for (ci = 0; ci < cinfo->num_components; ci++)
       
  1528       for (i = 0; i < DCTSIZE2; i++)
       
  1529 	*coef_bit_ptr++ = -1;
       
  1530 
       
  1531     /* Mark derived tables unallocated */
       
  1532     for (i = 0; i < NUM_HUFF_TBLS; i++) {
       
  1533       entropy->derived_tbls[i] = NULL;
       
  1534     }
       
  1535   } else {
       
  1536     /* Mark tables unallocated */
       
  1537     for (i = 0; i < NUM_HUFF_TBLS; i++) {
       
  1538       entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
       
  1539     }
   650   }
  1540   }
   651 }
  1541 }