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