Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * jdhuff.c | 2 * jdhuff.c |
| 3 * | 3 * |
| 4 * Copyright (C) 1991-1997, Thomas G. Lane. | 4 * Copyright (C) 1991-1997, Thomas G. Lane. |
| 5 * This file is part of the Independent JPEG Group's software. | 5 * This file is part of the Independent JPEG Group's software. |
| 6 * For conditions of distribution and use, see the accompanying README file. | 6 * For conditions of distribution and use, see the accompanying README file. |
| 7 * | 7 * |
| 8 * This file contains Huffman entropy decoding routines. | 8 * This file contains Huffman entropy decoding routines. |
| 9 * | 9 * |
| 10 * Much of the complexity here has to do with supporting input suspension. | 10 * Much of the complexity here has to do with supporting input suspension. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 * | 25 * |
| 26 * This library is distributed in the hope that it will be useful, | 26 * This library is distributed in the hope that it will be useful, |
| 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 * wxWindows Library License for more details. | 29 * wxWindows Library License for more details. |
| 30 */ | 30 */ |
| 31 | 31 |
| 32 #define JPEG_INTERNALS | 32 #define JPEG_INTERNALS |
| 33 #include "jinclude.h" | 33 #include "jinclude.h" |
| 34 #include "jpeglib.h" | 34 #include "jpeglib.h" |
| 35 #include "jdhuff.h"» » /* Declarations shared with jdphuff.c */ | 35 #include "jdhuff.h" /* Declarations shared with jdphuff.c */ |
| 36 | 36 |
| 37 | 37 |
| 38 /* | 38 /* |
| 39 * Expanded entropy decoder object for Huffman decoding. | 39 * Expanded entropy decoder object for Huffman decoding. |
| 40 * | 40 * |
| 41 * The savable_state subrecord contains fields that change within an MCU, | 41 * The savable_state subrecord contains fields that change within an MCU, |
| 42 * but must not be updated permanently until we complete the MCU. | 42 * but must not be updated permanently until we complete the MCU. |
| 43 */ | 43 */ |
| 44 | 44 |
| 45 typedef struct { | 45 typedef struct { |
| 46 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ | 46 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
| 47 } savable_state; | 47 } savable_state; |
| 48 | 48 |
| 49 /* This macro is to work around compilers with missing or broken | 49 /* This macro is to work around compilers with missing or broken |
| 50 * structure assignment. You'll need to fix this code if you have | 50 * structure assignment. You'll need to fix this code if you have |
| 51 * such a compiler and you change MAX_COMPS_IN_SCAN. | 51 * such a compiler and you change MAX_COMPS_IN_SCAN. |
| 52 */ | 52 */ |
| 53 | 53 |
| 54 #ifndef NO_STRUCT_ASSIGN | 54 #ifndef NO_STRUCT_ASSIGN |
| 55 #define ASSIGN_STATE(dest,src) ((dest) = (src)) | 55 #define ASSIGN_STATE(dest,src) ((dest) = (src)) |
| 56 #else | 56 #else |
| 57 #if MAX_COMPS_IN_SCAN == 4 | 57 #if MAX_COMPS_IN_SCAN == 4 |
| 58 #define ASSIGN_STATE(dest,src) \ | 58 #define ASSIGN_STATE(dest,src) \ |
| 59 » ((dest).last_dc_val[0] = (src).last_dc_val[0], \ | 59 ((dest).last_dc_val[0] = (src).last_dc_val[0], \ |
| 60 » (dest).last_dc_val[1] = (src).last_dc_val[1], \ | 60 (dest).last_dc_val[1] = (src).last_dc_val[1], \ |
| 61 » (dest).last_dc_val[2] = (src).last_dc_val[2], \ | 61 (dest).last_dc_val[2] = (src).last_dc_val[2], \ |
| 62 » (dest).last_dc_val[3] = (src).last_dc_val[3]) | 62 (dest).last_dc_val[3] = (src).last_dc_val[3]) |
| 63 #endif | 63 #endif |
| 64 #endif | 64 #endif |
| 65 | 65 |
| 66 | 66 |
| 67 typedef struct { | 67 typedef struct { |
| 68 struct jpeg_entropy_decoder pub; /* public fields */ | 68 struct jpeg_entropy_decoder pub; /* public fields */ |
| 69 | 69 |
| 70 /* These fields are loaded into local variables at start of each MCU. | 70 /* These fields are loaded into local variables at start of each MCU. |
| 71 * In case of suspension, we exit WITHOUT updating them. | 71 * In case of suspension, we exit WITHOUT updating them. |
| 72 */ | 72 */ |
| 73 bitread_perm_state bitstate;» /* Bit buffer at start of MCU */ | 73 bitread_perm_state bitstate; /* Bit buffer at start of MCU */ |
| 74 savable_state saved;» » /* Other state at start of MCU */ | 74 savable_state saved; /* Other state at start of MCU */ |
| 75 | 75 |
| 76 /* These fields are NOT loaded into local working state. */ | 76 /* These fields are NOT loaded into local working state. */ |
| 77 unsigned int restarts_to_go;» /* MCUs left in this restart interval */ | 77 unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
| 78 | 78 |
| 79 /* Pointers to derived tables (these workspaces have image lifespan) */ | 79 /* Pointers to derived tables (these workspaces have image lifespan) */ |
| 80 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; | 80 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; |
| 81 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; | 81 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; |
| 82 | 82 |
| 83 /* Precalculated info set up by start_pass for use in decode_mcu: */ | 83 /* Precalculated info set up by start_pass for use in decode_mcu: */ |
| 84 | 84 |
| 85 /* Pointers to derived tables to be used for each block within an MCU */ | 85 /* Pointers to derived tables to be used for each block within an MCU */ |
| 86 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; | 86 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; |
| 87 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; | 87 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 112 cinfo->Ah != 0 || cinfo->Al != 0) | 112 cinfo->Ah != 0 || cinfo->Al != 0) |
| 113 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); | 113 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); |
| 114 | 114 |
| 115 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 115 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 116 compptr = cinfo->cur_comp_info[ci]; | 116 compptr = cinfo->cur_comp_info[ci]; |
| 117 dctbl = compptr->dc_tbl_no; | 117 dctbl = compptr->dc_tbl_no; |
| 118 actbl = compptr->ac_tbl_no; | 118 actbl = compptr->ac_tbl_no; |
| 119 /* Compute derived values for Huffman tables */ | 119 /* Compute derived values for Huffman tables */ |
| 120 /* We may do this more than once for a table, but it's not expensive */ | 120 /* We may do this more than once for a table, but it's not expensive */ |
| 121 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, | 121 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, |
| 122 » » » & entropy->dc_derived_tbls[dctbl]); | 122 & entropy->dc_derived_tbls[dctbl]); |
| 123 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, | 123 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, |
| 124 » » » & entropy->ac_derived_tbls[actbl]); | 124 & entropy->ac_derived_tbls[actbl]); |
| 125 /* Initialize DC predictions to 0 */ | 125 /* Initialize DC predictions to 0 */ |
| 126 entropy->saved.last_dc_val[ci] = 0; | 126 entropy->saved.last_dc_val[ci] = 0; |
| 127 } | 127 } |
| 128 | 128 |
| 129 /* Precalculate decoding info for each block in an MCU of this scan */ | 129 /* Precalculate decoding info for each block in an MCU of this scan */ |
| 130 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 130 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 131 ci = cinfo->MCU_membership[blkn]; | 131 ci = cinfo->MCU_membership[blkn]; |
| 132 compptr = cinfo->cur_comp_info[ci]; | 132 compptr = cinfo->cur_comp_info[ci]; |
| 133 /* Precalculate which table to use for each block */ | 133 /* Precalculate which table to use for each block */ |
| 134 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; | 134 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 155 | 155 |
| 156 /* | 156 /* |
| 157 * Compute the derived values for a Huffman table. | 157 * Compute the derived values for a Huffman table. |
| 158 * This routine also performs some validation checks on the table. | 158 * This routine also performs some validation checks on the table. |
| 159 * | 159 * |
| 160 * Note this is also used by jdphuff.c. | 160 * Note this is also used by jdphuff.c. |
| 161 */ | 161 */ |
| 162 | 162 |
| 163 GLOBAL(void) | 163 GLOBAL(void) |
| 164 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, | 164 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, |
| 165 » » » d_derived_tbl ** pdtbl) | 165 d_derived_tbl ** pdtbl) |
| 166 { | 166 { |
| 167 JHUFF_TBL *htbl; | 167 JHUFF_TBL *htbl; |
| 168 d_derived_tbl *dtbl; | 168 d_derived_tbl *dtbl; |
| 169 int p, i, l, si, numsymbols; | 169 int p, i, l, si, numsymbols; |
| 170 int lookbits, ctr; | 170 int lookbits, ctr; |
| 171 char huffsize[257]; | 171 char huffsize[257]; |
| 172 unsigned int huffcode[257]; | 172 unsigned int huffcode[257]; |
| 173 unsigned int code; | 173 unsigned int code; |
| 174 | 174 |
| 175 /* Note that huffsize[] and huffcode[] are filled in code-length order, | 175 /* Note that huffsize[] and huffcode[] are filled in code-length order, |
| 176 * paralleling the order of the symbols themselves in htbl->huffval[]. | 176 * paralleling the order of the symbols themselves in htbl->huffval[]. |
| 177 */ | 177 */ |
| 178 | 178 |
| 179 /* Find the input Huffman table */ | 179 /* Find the input Huffman table */ |
| 180 if (tblno < 0 || tblno >= NUM_HUFF_TBLS) | 180 if (tblno < 0 || tblno >= NUM_HUFF_TBLS) |
| 181 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); | 181 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
| 182 htbl = | 182 htbl = |
| 183 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; | 183 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; |
| 184 if (htbl == NULL) | 184 if (htbl == NULL) |
| 185 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); | 185 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
| 186 | 186 |
| 187 /* Allocate a workspace if we haven't already done so. */ | 187 /* Allocate a workspace if we haven't already done so. */ |
| 188 if (*pdtbl == NULL) | 188 if (*pdtbl == NULL) |
| 189 *pdtbl = (d_derived_tbl *) | 189 *pdtbl = (d_derived_tbl *) |
| 190 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 190 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 191 » » » » SIZEOF(d_derived_tbl)); | 191 SIZEOF(d_derived_tbl)); |
| 192 dtbl = *pdtbl; | 192 dtbl = *pdtbl; |
| 193 dtbl->pub = htbl;» » /* fill in back link */ | 193 dtbl->pub = htbl; /* fill in back link */ |
| 194 | 194 |
| 195 /* Figure C.1: make table of Huffman code length for each symbol */ | 195 /* Figure C.1: make table of Huffman code length for each symbol */ |
| 196 | 196 |
| 197 p = 0; | 197 p = 0; |
| 198 for (l = 1; l <= 16; l++) { | 198 for (l = 1; l <= 16; l++) { |
| 199 i = (int) htbl->bits[l]; | 199 i = (int) htbl->bits[l]; |
| 200 if (i < 0 || p + i > 256)» /* protect against table overrun */ | 200 if (i < 0 || p + i > 256) /* protect against table overrun */ |
| 201 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); | 201 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 202 while (i--) | 202 while (i--) |
| 203 huffsize[p++] = (char) l; | 203 huffsize[p++] = (char) l; |
| 204 } | 204 } |
| 205 huffsize[p] = 0; | 205 huffsize[p] = 0; |
| 206 numsymbols = p; | 206 numsymbols = p; |
| 207 | 207 |
| 208 /* Figure C.2: generate the codes themselves */ | 208 /* Figure C.2: generate the codes themselves */ |
| 209 /* We also validate that the counts represent a legal Huffman code tree. */ | 209 /* We also validate that the counts represent a legal Huffman code tree. */ |
| 210 | 210 |
| 211 code = 0; | 211 code = 0; |
| 212 si = huffsize[0]; | 212 si = huffsize[0]; |
| 213 p = 0; | 213 p = 0; |
| 214 while (huffsize[p]) { | 214 while (huffsize[p]) { |
| 215 while (((int) huffsize[p]) == si) { | 215 while (((int) huffsize[p]) == si) { |
| 216 huffcode[p++] = code; | 216 huffcode[p++] = code; |
| 217 code++; | 217 code++; |
| 218 } | 218 } |
| 219 /* code is now 1 more than the last code used for codelength si; but | 219 /* code is now 1 more than the last code used for codelength si; but |
| 220 * it must still fit in si bits, since no code is allowed to be all ones. | 220 * it must still fit in si bits, since no code is allowed to be all ones. |
| 221 */ | 221 */ |
| 222 if (((INT32) code) >= (((INT32) 1) << si)) | 222 if (((INT32) code) >= (((INT32) 1) << si)) |
| 223 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); | 223 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 224 code <<= 1; | 224 code <<= 1; |
| 225 si++; | 225 si++; |
| 226 } | 226 } |
| 227 | 227 |
| 228 /* Figure F.15: generate decoding tables for bit-sequential decoding */ | 228 /* Figure F.15: generate decoding tables for bit-sequential decoding */ |
| 229 | 229 |
| 230 p = 0; | 230 p = 0; |
| 231 for (l = 1; l <= 16; l++) { | 231 for (l = 1; l <= 16; l++) { |
| 232 if (htbl->bits[l]) { | 232 if (htbl->bits[l]) { |
| 233 /* valoffset[l] = huffval[] index of 1st symbol of code length l, | 233 /* valoffset[l] = huffval[] index of 1st symbol of code length l, |
| 234 * minus the minimum code of length l | 234 * minus the minimum code of length l |
| 235 */ | 235 */ |
| 236 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; | 236 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; |
| 237 p += htbl->bits[l]; | 237 p += htbl->bits[l]; |
| 238 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ | 238 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ |
| 239 } else { | 239 } else { |
| 240 dtbl->maxcode[l] = -1;» /* -1 if no codes of this length */ | 240 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 dtbl->valoffset[17] = 0; | 243 dtbl->valoffset[17] = 0; |
| 244 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ | 244 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ |
| 245 | 245 |
| 246 /* Compute lookahead tables to speed up decoding. | 246 /* Compute lookahead tables to speed up decoding. |
| 247 * First we set all the table entries to 0, indicating "too long"; | 247 * First we set all the table entries to 0, indicating "too long"; |
| 248 * then we iterate through the Huffman codes that are short enough and | 248 * then we iterate through the Huffman codes that are short enough and |
| 249 * fill in all the entries that correspond to bit sequences starting | 249 * fill in all the entries that correspond to bit sequences starting |
| 250 * with that code. | 250 * with that code. |
| 251 */ | 251 */ |
| 252 | 252 |
| 253 for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++) | 253 for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++) |
| 254 dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD; | 254 dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD; |
| 255 | 255 |
| 256 p = 0; | 256 p = 0; |
| 257 for (l = 1; l <= HUFF_LOOKAHEAD; l++) { | 257 for (l = 1; l <= HUFF_LOOKAHEAD; l++) { |
| 258 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { | 258 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { |
| 259 /* l = current code's length, p = its index in huffcode[] & huffval[]. */ | 259 /* l = current code's length, p = its index in huffcode[] & huffval[]. */ |
| 260 /* Generate left-justified code followed by all possible bit sequences */ | 260 /* Generate left-justified code followed by all possible bit sequences */ |
| 261 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); | 261 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); |
| 262 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { | 262 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { |
| 263 » dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p]; | 263 dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p]; |
| 264 » lookbits++; | 264 lookbits++; |
| 265 } | 265 } |
| 266 } | 266 } |
| 267 } | 267 } |
| 268 | 268 |
| 269 /* Validate symbols as being reasonable. | 269 /* Validate symbols as being reasonable. |
| 270 * For AC tables, we make no check, but accept all byte values 0..255. | 270 * For AC tables, we make no check, but accept all byte values 0..255. |
| 271 * For DC tables, we require the symbols to be in range 0..15. | 271 * For DC tables, we require the symbols to be in range 0..15. |
| 272 * (Tighter bounds could be applied depending on the data depth and mode, | 272 * (Tighter bounds could be applied depending on the data depth and mode, |
| 273 * but this is sufficient to ensure safe decoding.) | 273 * but this is sufficient to ensure safe decoding.) |
| 274 */ | 274 */ |
| 275 if (isDC) { | 275 if (isDC) { |
| 276 for (i = 0; i < numsymbols; i++) { | 276 for (i = 0; i < numsymbols; i++) { |
| 277 int sym = htbl->huffval[i]; | 277 int sym = htbl->huffval[i]; |
| 278 if (sym < 0 || sym > 15) | 278 if (sym < 0 || sym > 15) |
| 279 » ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); | 279 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 280 } | 280 } |
| 281 } | 281 } |
| 282 } | 282 } |
| 283 | 283 |
| 284 | 284 |
| 285 /* | 285 /* |
| 286 * Out-of-line code for bit fetching (shared with jdphuff.c). | 286 * Out-of-line code for bit fetching (shared with jdphuff.c). |
| 287 * See jdhuff.h for info about usage. | 287 * See jdhuff.h for info about usage. |
| 288 * Note: current values of get_buffer and bits_left are passed as parameters, | 288 * Note: current values of get_buffer and bits_left are passed as parameters, |
| 289 * but are returned in the corresponding fields of the state struct. | 289 * but are returned in the corresponding fields of the state struct. |
| 290 * | 290 * |
| 291 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width | 291 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width |
| 292 * of get_buffer to be used. (On machines with wider words, an even larger | 292 * of get_buffer to be used. (On machines with wider words, an even larger |
| 293 * buffer could be used.) However, on some machines 32-bit shifts are | 293 * buffer could be used.) However, on some machines 32-bit shifts are |
| 294 * quite slow and take time proportional to the number of places shifted. | 294 * quite slow and take time proportional to the number of places shifted. |
| 295 * (This is true with most PC compilers, for instance.) In this case it may | 295 * (This is true with most PC compilers, for instance.) In this case it may |
| 296 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the | 296 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the |
| 297 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. | 297 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. |
| 298 */ | 298 */ |
| 299 | 299 |
| 300 #ifdef SLOW_SHIFT_32 | 300 #ifdef SLOW_SHIFT_32 |
| 301 #define MIN_GET_BITS 15» /* minimum allowable value */ | 301 #define MIN_GET_BITS 15 /* minimum allowable value */ |
| 302 #else | 302 #else |
| 303 #define MIN_GET_BITS (BIT_BUF_SIZE-7) | 303 #define MIN_GET_BITS (BIT_BUF_SIZE-7) |
| 304 #endif | 304 #endif |
| 305 | 305 |
| 306 | 306 |
| 307 GLOBAL(boolean) | 307 GLOBAL(boolean) |
| 308 jpeg_fill_bit_buffer (bitread_working_state * state, | 308 jpeg_fill_bit_buffer (bitread_working_state * state, |
| 309 » » register bit_buf_type get_buffer, register int bits_left, | 309 register bit_buf_type get_buffer, register int bits_left, |
| 310 » » int nbits) | 310 int nbits) |
| 311 /* Load up the bit buffer to a depth of at least nbits */ | 311 /* Load up the bit buffer to a depth of at least nbits */ |
| 312 { | 312 { |
| 313 /* Copy heavily used state fields into locals (hopefully registers) */ | 313 /* Copy heavily used state fields into locals (hopefully registers) */ |
| 314 register const JOCTET * next_input_byte = state->next_input_byte; | 314 register const JOCTET * next_input_byte = state->next_input_byte; |
| 315 register size_t bytes_in_buffer = state->bytes_in_buffer; | 315 register size_t bytes_in_buffer = state->bytes_in_buffer; |
| 316 j_decompress_ptr cinfo = state->cinfo; | 316 j_decompress_ptr cinfo = state->cinfo; |
| 317 | 317 |
| 318 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ | 318 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ |
| 319 /* (It is assumed that no request will be for more than that many bits.) */ | 319 /* (It is assumed that no request will be for more than that many bits.) */ |
| 320 /* We fail to do so only if we hit a marker or are forced to suspend. */ | 320 /* We fail to do so only if we hit a marker or are forced to suspend. */ |
| 321 | 321 |
| 322 if (cinfo->unread_marker == 0) {» /* cannot advance past a marker */ | 322 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ |
| 323 while (bits_left < MIN_GET_BITS) { | 323 while (bits_left < MIN_GET_BITS) { |
| 324 register int c; | 324 register int c; |
| 325 | 325 |
| 326 /* Attempt to read a byte */ | 326 /* Attempt to read a byte */ |
| 327 if (bytes_in_buffer == 0) { | 327 if (bytes_in_buffer == 0) { |
| 328 » if (! (*cinfo->src->fill_input_buffer) (cinfo)) | 328 if (! (*cinfo->src->fill_input_buffer) (cinfo)) |
| 329 » return FALSE; | 329 return FALSE; |
| 330 » next_input_byte = cinfo->src->next_input_byte; | 330 next_input_byte = cinfo->src->next_input_byte; |
| 331 » bytes_in_buffer = cinfo->src->bytes_in_buffer; | 331 bytes_in_buffer = cinfo->src->bytes_in_buffer; |
| 332 } | 332 } |
| 333 bytes_in_buffer--; | 333 bytes_in_buffer--; |
| 334 c = GETJOCTET(*next_input_byte++); | 334 c = GETJOCTET(*next_input_byte++); |
| 335 | 335 |
| 336 /* If it's 0xFF, check and discard stuffed zero byte */ | 336 /* If it's 0xFF, check and discard stuffed zero byte */ |
| 337 if (c == 0xFF) { | 337 if (c == 0xFF) { |
| 338 » /* Loop here to discard any padding FF's on terminating marker, | 338 /* Loop here to discard any padding FF's on terminating marker, |
| 339 » * so that we can save a valid unread_marker value. NOTE: we will | 339 * so that we can save a valid unread_marker value. NOTE: we will |
| 340 » * accept multiple FF's followed by a 0 as meaning a single FF data | 340 * accept multiple FF's followed by a 0 as meaning a single FF data |
| 341 » * byte. This data pattern is not valid according to the standard. | 341 * byte. This data pattern is not valid according to the standard. |
| 342 » */ | 342 */ |
| 343 » do { | 343 do { |
| 344 » if (bytes_in_buffer == 0) { | 344 if (bytes_in_buffer == 0) { |
| 345 » if (! (*cinfo->src->fill_input_buffer) (cinfo)) | 345 if (! (*cinfo->src->fill_input_buffer) (cinfo)) |
| 346 » return FALSE; | 346 return FALSE; |
| 347 » next_input_byte = cinfo->src->next_input_byte; | 347 next_input_byte = cinfo->src->next_input_byte; |
| 348 » bytes_in_buffer = cinfo->src->bytes_in_buffer; | 348 bytes_in_buffer = cinfo->src->bytes_in_buffer; |
| 349 » } | 349 } |
| 350 » bytes_in_buffer--; | 350 bytes_in_buffer--; |
| 351 » c = GETJOCTET(*next_input_byte++); | 351 c = GETJOCTET(*next_input_byte++); |
| 352 » } while (c == 0xFF); | 352 } while (c == 0xFF); |
| 353 | 353 |
| 354 » if (c == 0) { | 354 if (c == 0) { |
| 355 » /* Found FF/00, which represents an FF data byte */ | 355 /* Found FF/00, which represents an FF data byte */ |
| 356 » c = 0xFF; | 356 c = 0xFF; |
| 357 » } else { | 357 } else { |
| 358 » /* Oops, it's actually a marker indicating end of compressed data. | 358 /* Oops, it's actually a marker indicating end of compressed data. |
| 359 » * Save the marker code for later use. | 359 * Save the marker code for later use. |
| 360 » * Fine point: it might appear that we should save the marker into | 360 * Fine point: it might appear that we should save the marker into |
| 361 » * bitread working state, not straight into permanent state. But | 361 * bitread working state, not straight into permanent state. But |
| 362 » * once we have hit a marker, we cannot need to suspend within the | 362 * once we have hit a marker, we cannot need to suspend within the |
| 363 » * current MCU, because we will read no more bytes from the data | 363 * current MCU, because we will read no more bytes from the data |
| 364 » * source. So it is OK to update permanent state right away. | 364 * source. So it is OK to update permanent state right away. |
| 365 » */ | 365 */ |
| 366 » cinfo->unread_marker = c; | 366 cinfo->unread_marker = c; |
| 367 » /* See if we need to insert some fake zero bits. */ | 367 /* See if we need to insert some fake zero bits. */ |
| 368 » goto no_more_bytes; | 368 goto no_more_bytes; |
| 369 » } | 369 } |
| 370 } | 370 } |
| 371 | 371 |
| 372 /* OK, load c into get_buffer */ | 372 /* OK, load c into get_buffer */ |
| 373 get_buffer = (get_buffer << 8) | c; | 373 get_buffer = (get_buffer << 8) | c; |
| 374 bits_left += 8; | 374 bits_left += 8; |
| 375 } /* end while */ | 375 } /* end while */ |
| 376 } else { | 376 } else { |
| 377 no_more_bytes: | 377 no_more_bytes: |
| 378 /* We get here if we've read the marker that terminates the compressed | 378 /* We get here if we've read the marker that terminates the compressed |
| 379 * data segment. There should be enough bits in the buffer register | 379 * data segment. There should be enough bits in the buffer register |
| 380 * to satisfy the request; if so, no problem. | 380 * to satisfy the request; if so, no problem. |
| 381 */ | 381 */ |
| 382 if (nbits > bits_left) { | 382 if (nbits > bits_left) { |
| 383 /* Uh-oh. Report corrupted data to user and stuff zeroes into | 383 /* Uh-oh. Report corrupted data to user and stuff zeroes into |
| 384 * the data stream, so that we can produce some kind of image. | 384 * the data stream, so that we can produce some kind of image. |
| 385 * We use a nonvolatile flag to ensure that only one warning message | 385 * We use a nonvolatile flag to ensure that only one warning message |
| 386 * appears per data segment. | 386 * appears per data segment. |
| 387 */ | 387 */ |
| 388 if (! cinfo->entropy->insufficient_data) { | 388 if (! cinfo->entropy->insufficient_data) { |
| 389 » WARNMS(cinfo, JWRN_HIT_MARKER); | 389 WARNMS(cinfo, JWRN_HIT_MARKER); |
| 390 » cinfo->entropy->insufficient_data = TRUE; | 390 cinfo->entropy->insufficient_data = TRUE; |
| 391 } | 391 } |
| 392 /* Fill the buffer with zero bits */ | 392 /* Fill the buffer with zero bits */ |
| 393 get_buffer <<= MIN_GET_BITS - bits_left; | 393 get_buffer <<= MIN_GET_BITS - bits_left; |
| 394 bits_left = MIN_GET_BITS; | 394 bits_left = MIN_GET_BITS; |
| 395 } | 395 } |
| 396 } | 396 } |
| 397 | 397 |
| 398 /* Unload the local registers */ | 398 /* Unload the local registers */ |
| 399 state->next_input_byte = next_input_byte; | 399 state->next_input_byte = next_input_byte; |
| 400 state->bytes_in_buffer = bytes_in_buffer; | 400 state->bytes_in_buffer = bytes_in_buffer; |
| 401 state->get_buffer = get_buffer; | 401 state->get_buffer = get_buffer; |
| 402 state->bits_left = bits_left; | 402 state->bits_left = bits_left; |
| 403 | 403 |
| 404 return TRUE; | 404 return TRUE; |
| 405 } | 405 } |
| 406 | 406 |
| 407 | 407 |
| 408 /* | 408 /* |
| 409 * Out-of-line code for Huffman code decoding. | 409 * Out-of-line code for Huffman code decoding. |
| 410 * See jdhuff.h for info about usage. | 410 * See jdhuff.h for info about usage. |
| 411 */ | 411 */ |
| 412 | 412 |
| 413 GLOBAL(int) | 413 GLOBAL(int) |
| 414 jpeg_huff_decode (bitread_working_state * state, | 414 jpeg_huff_decode (bitread_working_state * state, |
| 415 » » register bit_buf_type get_buffer, register int bits_left, | 415 register bit_buf_type get_buffer, register int bits_left, |
| 416 » » d_derived_tbl * htbl, int min_bits) | 416 d_derived_tbl * htbl, int min_bits) |
| 417 { | 417 { |
| 418 register int l = min_bits; | 418 register int l = min_bits; |
| 419 register INT32 code; | 419 register INT32 code; |
| 420 | 420 |
| 421 /* HUFF_DECODE has determined that the code is at least min_bits */ | 421 /* HUFF_DECODE has determined that the code is at least min_bits */ |
| 422 /* bits long, so fetch that many bits in one swoop. */ | 422 /* bits long, so fetch that many bits in one swoop. */ |
| 423 | 423 |
| 424 CHECK_BIT_BUFFER(*state, l, return -1); | 424 CHECK_BIT_BUFFER(*state, l, return -1); |
| 425 code = GET_BITS(l); | 425 code = GET_BITS(l); |
| 426 | 426 |
| 427 /* Collect the rest of the Huffman code one bit at a time. */ | 427 /* Collect the rest of the Huffman code one bit at a time. */ |
| 428 /* This is per Figure F.16 in the JPEG spec. */ | 428 /* This is per Figure F.16 in the JPEG spec. */ |
| 429 | 429 |
| 430 while (code > htbl->maxcode[l]) { | 430 while (code > htbl->maxcode[l]) { |
| 431 code <<= 1; | 431 code <<= 1; |
| 432 CHECK_BIT_BUFFER(*state, 1, return -1); | 432 CHECK_BIT_BUFFER(*state, 1, return -1); |
| 433 code |= GET_BITS(1); | 433 code |= GET_BITS(1); |
| 434 l++; | 434 l++; |
| 435 } | 435 } |
| 436 | 436 |
| 437 /* Unload the local registers */ | 437 /* Unload the local registers */ |
| 438 state->get_buffer = get_buffer; | 438 state->get_buffer = get_buffer; |
| 439 state->bits_left = bits_left; | 439 state->bits_left = bits_left; |
| 440 | 440 |
| 441 /* With garbage input we may reach the sentinel value l = 17. */ | 441 /* With garbage input we may reach the sentinel value l = 17. */ |
| 442 | 442 |
| 443 if (l > 16) { | 443 if (l > 16) { |
| 444 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); | 444 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); |
| 445 return 0;» » » /* fake a zero as the safest result */ | 445 return 0; /* fake a zero as the safest result */ |
| 446 } | 446 } |
| 447 | 447 |
| 448 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; | 448 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; |
| 449 } | 449 } |
| 450 | 450 |
| 451 | 451 |
| 452 /* | 452 /* |
| 453 * Figure F.12: extend sign bit. | 453 * Figure F.12: extend sign bit. |
| 454 * On some machines, a shift and add will be faster than a table lookup. | 454 * On some machines, a shift and add will be faster than a table lookup. |
| 455 */ | 455 */ |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 555 | 555 |
| 556 if (entropy->ac_needed[blkn]) { | 556 if (entropy->ac_needed[blkn]) { |
| 557 | 557 |
| 558 /* Section F.2.2.2: decode the AC coefficients */ | 558 /* Section F.2.2.2: decode the AC coefficients */ |
| 559 /* Since zeroes are skipped, output area must be cleared beforehand */ | 559 /* Since zeroes are skipped, output area must be cleared beforehand */ |
| 560 for (k = 1; k < DCTSIZE2; k++) { | 560 for (k = 1; k < DCTSIZE2; k++) { |
| 561 HUFF_DECODE(s, br_state, actbl, return FALSE, label2); | 561 HUFF_DECODE(s, br_state, actbl, return FALSE, label2); |
| 562 | 562 |
| 563 r = s >> 4; | 563 r = s >> 4; |
| 564 s &= 15; | 564 s &= 15; |
| 565 | 565 |
| 566 if (s) { | 566 if (s) { |
| 567 k += r; | 567 k += r; |
| 568 CHECK_BIT_BUFFER(br_state, s, return FALSE); | 568 CHECK_BIT_BUFFER(br_state, s, return FALSE); |
| 569 r = GET_BITS(s); | 569 r = GET_BITS(s); |
| 570 s = HUFF_EXTEND(r, s); | 570 s = HUFF_EXTEND(r, s); |
| 571 /* Output coefficient in natural (dezigzagged) order. | 571 /* Output coefficient in natural (dezigzagged) order. |
| 572 * Note: the extra entries in jpeg_natural_order[] will save us | 572 * Note: the extra entries in jpeg_natural_order[] will save us |
| 573 * if k >= DCTSIZE2, which could happen if the data is corrupted. | 573 * if k >= DCTSIZE2, which could happen if the data is corrupted. |
| 574 */ | 574 */ |
| 575 (*block)[jpeg_natural_order[k]] = (JCOEF) s; | 575 (*block)[jpeg_natural_order[k]] = (JCOEF) s; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 698 state.last_dc_val[ci] = s; | 698 state.last_dc_val[ci] = s; |
| 699 (*block)[0] = (JCOEF) s; | 699 (*block)[0] = (JCOEF) s; |
| 700 } | 700 } |
| 701 | 701 |
| 702 if (entropy->ac_needed[blkn]) { | 702 if (entropy->ac_needed[blkn]) { |
| 703 | 703 |
| 704 for (k = 1; k < DCTSIZE2; k++) { | 704 for (k = 1; k < DCTSIZE2; k++) { |
| 705 HUFF_DECODE_FAST(s, l, actbl); | 705 HUFF_DECODE_FAST(s, l, actbl); |
| 706 r = s >> 4; | 706 r = s >> 4; |
| 707 s &= 15; | 707 s &= 15; |
| 708 | 708 |
| 709 if (s) { | 709 if (s) { |
| 710 k += r; | 710 k += r; |
| 711 ENSURE_SHORT | 711 ENSURE_SHORT |
| 712 r = GET_BITS(s); | 712 r = GET_BITS(s); |
| 713 s = HUFF_EXTEND(r, s); | 713 s = HUFF_EXTEND(r, s); |
| 714 (*block)[jpeg_natural_order[k]] = (JCOEF) s; | 714 (*block)[jpeg_natural_order[k]] = (JCOEF) s; |
| 715 } else { | 715 } else { |
| 716 if (r != 15) break; | 716 if (r != 15) break; |
| 717 k += 15; | 717 k += 15; |
| 718 } | 718 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 753 * The i'th block of the MCU is stored into the block pointed to by | 753 * The i'th block of the MCU is stored into the block pointed to by |
| 754 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. | 754 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. |
| 755 * (Wholesale zeroing is usually a little faster than retail...) | 755 * (Wholesale zeroing is usually a little faster than retail...) |
| 756 * | 756 * |
| 757 * Returns FALSE if data source requested suspension. In that case no | 757 * Returns FALSE if data source requested suspension. In that case no |
| 758 * changes have been made to permanent state. (Exception: some output | 758 * changes have been made to permanent state. (Exception: some output |
| 759 * coefficients may already have been assigned. This is harmless for | 759 * coefficients may already have been assigned. This is harmless for |
| 760 * this module, since we'll just re-assign them on the next call.) | 760 * this module, since we'll just re-assign them on the next call.) |
| 761 */ | 761 */ |
| 762 | 762 |
| 763 #define BUFSIZE (DCTSIZE2 * 2) | 763 #define BUFSIZE (DCTSIZE2 * 2u) |
|
fbarchard
2011/07/08 08:11:03
this change avoid a casting warning
| |
| 764 | 764 |
| 765 METHODDEF(boolean) | 765 METHODDEF(boolean) |
| 766 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | 766 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
| 767 { | 767 { |
| 768 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; | 768 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; |
| 769 int usefast = 1; | 769 int usefast = 1; |
| 770 | 770 |
| 771 /* Process restart marker if needed; may have to suspend */ | 771 /* Process restart marker if needed; may have to suspend */ |
| 772 if (cinfo->restart_interval) { | 772 if (cinfo->restart_interval) { |
| 773 if (entropy->restarts_to_go == 0) | 773 if (entropy->restarts_to_go == 0) |
| 774 if (! process_restart(cinfo)) | 774 if (! process_restart(cinfo)) |
| 775 » return FALSE; | 775 return FALSE; |
| 776 usefast = 0; | 776 usefast = 0; |
| 777 } | 777 } |
| 778 | 778 |
| 779 if (cinfo->src->bytes_in_buffer < BUFSIZE * cinfo->blocks_in_MCU) | 779 if (cinfo->src->bytes_in_buffer < BUFSIZE * cinfo->blocks_in_MCU) |
| 780 usefast = 0; | 780 usefast = 0; |
| 781 | 781 |
| 782 /* If we've run out of data, just leave the MCU set to zeroes. | 782 /* If we've run out of data, just leave the MCU set to zeroes. |
| 783 * This way, we return uniform gray for the remainder of the segment. | 783 * This way, we return uniform gray for the remainder of the segment. |
| 784 */ | 784 */ |
| 785 if (! entropy->pub.insufficient_data) { | 785 if (! entropy->pub.insufficient_data) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 805 */ | 805 */ |
| 806 | 806 |
| 807 GLOBAL(void) | 807 GLOBAL(void) |
| 808 jinit_huff_decoder (j_decompress_ptr cinfo) | 808 jinit_huff_decoder (j_decompress_ptr cinfo) |
| 809 { | 809 { |
| 810 huff_entropy_ptr entropy; | 810 huff_entropy_ptr entropy; |
| 811 int i; | 811 int i; |
| 812 | 812 |
| 813 entropy = (huff_entropy_ptr) | 813 entropy = (huff_entropy_ptr) |
| 814 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 814 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 815 » » » » SIZEOF(huff_entropy_decoder)); | 815 SIZEOF(huff_entropy_decoder)); |
| 816 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; | 816 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; |
| 817 entropy->pub.start_pass = start_pass_huff_decoder; | 817 entropy->pub.start_pass = start_pass_huff_decoder; |
| 818 entropy->pub.decode_mcu = decode_mcu; | 818 entropy->pub.decode_mcu = decode_mcu; |
| 819 | 819 |
| 820 /* Mark tables unallocated */ | 820 /* Mark tables unallocated */ |
| 821 for (i = 0; i < NUM_HUFF_TBLS; i++) { | 821 for (i = 0; i < NUM_HUFF_TBLS; i++) { |
| 822 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; | 822 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; |
| 823 } | 823 } |
| 824 } | 824 } |
| OLD | NEW |