dbf-halloween2015

annotate libs/libjpeg/jdphuff.c @ 3:c37fe5d8a4ed

windows port
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:04:28 +0200
parents
children
rev   line source
nuclear@1 1 /*
nuclear@1 2 * jdphuff.c
nuclear@1 3 *
nuclear@1 4 * Copyright (C) 1995-1997, Thomas G. Lane.
nuclear@1 5 * This file is part of the Independent JPEG Group's software.
nuclear@1 6 * For conditions of distribution and use, see the accompanying README file.
nuclear@1 7 *
nuclear@1 8 * This file contains Huffman entropy decoding routines for progressive JPEG.
nuclear@1 9 *
nuclear@1 10 * Much of the complexity here has to do with supporting input suspension.
nuclear@1 11 * If the data source module demands suspension, we want to be able to back
nuclear@1 12 * up to the start of the current MCU. To do this, we copy state variables
nuclear@1 13 * into local working storage, and update them back to the permanent
nuclear@1 14 * storage only upon successful completion of an MCU.
nuclear@1 15 */
nuclear@1 16
nuclear@1 17 #define JPEG_INTERNALS
nuclear@1 18 #include "jinclude.h"
nuclear@1 19 #include "jpeglib.h"
nuclear@1 20 #include "jdhuff.h" /* Declarations shared with jdhuff.c */
nuclear@1 21
nuclear@1 22
nuclear@1 23 #ifdef D_PROGRESSIVE_SUPPORTED
nuclear@1 24
nuclear@1 25 /*
nuclear@1 26 * Expanded entropy decoder object for progressive Huffman decoding.
nuclear@1 27 *
nuclear@1 28 * The savable_state subrecord contains fields that change within an MCU,
nuclear@1 29 * but must not be updated permanently until we complete the MCU.
nuclear@1 30 */
nuclear@1 31
nuclear@1 32 typedef struct {
nuclear@1 33 unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
nuclear@1 34 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
nuclear@1 35 } savable_state;
nuclear@1 36
nuclear@1 37 /* This macro is to work around compilers with missing or broken
nuclear@1 38 * structure assignment. You'll need to fix this code if you have
nuclear@1 39 * such a compiler and you change MAX_COMPS_IN_SCAN.
nuclear@1 40 */
nuclear@1 41
nuclear@1 42 #ifndef NO_STRUCT_ASSIGN
nuclear@1 43 #define ASSIGN_STATE(dest,src) ((dest) = (src))
nuclear@1 44 #else
nuclear@1 45 #if MAX_COMPS_IN_SCAN == 4
nuclear@1 46 #define ASSIGN_STATE(dest,src) \
nuclear@1 47 ((dest).EOBRUN = (src).EOBRUN, \
nuclear@1 48 (dest).last_dc_val[0] = (src).last_dc_val[0], \
nuclear@1 49 (dest).last_dc_val[1] = (src).last_dc_val[1], \
nuclear@1 50 (dest).last_dc_val[2] = (src).last_dc_val[2], \
nuclear@1 51 (dest).last_dc_val[3] = (src).last_dc_val[3])
nuclear@1 52 #endif
nuclear@1 53 #endif
nuclear@1 54
nuclear@1 55
nuclear@1 56 typedef struct {
nuclear@1 57 struct jpeg_entropy_decoder pub; /* public fields */
nuclear@1 58
nuclear@1 59 /* These fields are loaded into local variables at start of each MCU.
nuclear@1 60 * In case of suspension, we exit WITHOUT updating them.
nuclear@1 61 */
nuclear@1 62 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
nuclear@1 63 savable_state saved; /* Other state at start of MCU */
nuclear@1 64
nuclear@1 65 /* These fields are NOT loaded into local working state. */
nuclear@1 66 unsigned int restarts_to_go; /* MCUs left in this restart interval */
nuclear@1 67
nuclear@1 68 /* Pointers to derived tables (these workspaces have image lifespan) */
nuclear@1 69 d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
nuclear@1 70
nuclear@1 71 d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
nuclear@1 72 } phuff_entropy_decoder;
nuclear@1 73
nuclear@1 74 typedef phuff_entropy_decoder * phuff_entropy_ptr;
nuclear@1 75
nuclear@1 76 /* Forward declarations */
nuclear@1 77 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
nuclear@1 78 JBLOCKROW *MCU_data));
nuclear@1 79 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
nuclear@1 80 JBLOCKROW *MCU_data));
nuclear@1 81 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
nuclear@1 82 JBLOCKROW *MCU_data));
nuclear@1 83 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
nuclear@1 84 JBLOCKROW *MCU_data));
nuclear@1 85
nuclear@1 86
nuclear@1 87 /*
nuclear@1 88 * Initialize for a Huffman-compressed scan.
nuclear@1 89 */
nuclear@1 90
nuclear@1 91 METHODDEF(void)
nuclear@1 92 start_pass_phuff_decoder (j_decompress_ptr cinfo)
nuclear@1 93 {
nuclear@1 94 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 95 boolean is_DC_band, bad;
nuclear@1 96 int ci, coefi, tbl;
nuclear@1 97 int *coef_bit_ptr;
nuclear@1 98 jpeg_component_info * compptr;
nuclear@1 99
nuclear@1 100 is_DC_band = (cinfo->Ss == 0);
nuclear@1 101
nuclear@1 102 /* Validate scan parameters */
nuclear@1 103 bad = FALSE;
nuclear@1 104 if (is_DC_band) {
nuclear@1 105 if (cinfo->Se != 0)
nuclear@1 106 bad = TRUE;
nuclear@1 107 } else {
nuclear@1 108 /* need not check Ss/Se < 0 since they came from unsigned bytes */
nuclear@1 109 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
nuclear@1 110 bad = TRUE;
nuclear@1 111 /* AC scans may have only one component */
nuclear@1 112 if (cinfo->comps_in_scan != 1)
nuclear@1 113 bad = TRUE;
nuclear@1 114 }
nuclear@1 115 if (cinfo->Ah != 0) {
nuclear@1 116 /* Successive approximation refinement scan: must have Al = Ah-1. */
nuclear@1 117 if (cinfo->Al != cinfo->Ah-1)
nuclear@1 118 bad = TRUE;
nuclear@1 119 }
nuclear@1 120 if (cinfo->Al > 13) /* need not check for < 0 */
nuclear@1 121 bad = TRUE;
nuclear@1 122 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
nuclear@1 123 * but the spec doesn't say so, and we try to be liberal about what we
nuclear@1 124 * accept. Note: large Al values could result in out-of-range DC
nuclear@1 125 * coefficients during early scans, leading to bizarre displays due to
nuclear@1 126 * overflows in the IDCT math. But we won't crash.
nuclear@1 127 */
nuclear@1 128 if (bad)
nuclear@1 129 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
nuclear@1 130 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
nuclear@1 131 /* Update progression status, and verify that scan order is legal.
nuclear@1 132 * Note that inter-scan inconsistencies are treated as warnings
nuclear@1 133 * not fatal errors ... not clear if this is right way to behave.
nuclear@1 134 */
nuclear@1 135 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
nuclear@1 136 int cindex = cinfo->cur_comp_info[ci]->component_index;
nuclear@1 137 coef_bit_ptr = & cinfo->coef_bits[cindex][0];
nuclear@1 138 if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
nuclear@1 139 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
nuclear@1 140 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
nuclear@1 141 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
nuclear@1 142 if (cinfo->Ah != expected)
nuclear@1 143 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
nuclear@1 144 coef_bit_ptr[coefi] = cinfo->Al;
nuclear@1 145 }
nuclear@1 146 }
nuclear@1 147
nuclear@1 148 /* Select MCU decoding routine */
nuclear@1 149 if (cinfo->Ah == 0) {
nuclear@1 150 if (is_DC_band)
nuclear@1 151 entropy->pub.decode_mcu = decode_mcu_DC_first;
nuclear@1 152 else
nuclear@1 153 entropy->pub.decode_mcu = decode_mcu_AC_first;
nuclear@1 154 } else {
nuclear@1 155 if (is_DC_band)
nuclear@1 156 entropy->pub.decode_mcu = decode_mcu_DC_refine;
nuclear@1 157 else
nuclear@1 158 entropy->pub.decode_mcu = decode_mcu_AC_refine;
nuclear@1 159 }
nuclear@1 160
nuclear@1 161 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
nuclear@1 162 compptr = cinfo->cur_comp_info[ci];
nuclear@1 163 /* Make sure requested tables are present, and compute derived tables.
nuclear@1 164 * We may build same derived table more than once, but it's not expensive.
nuclear@1 165 */
nuclear@1 166 if (is_DC_band) {
nuclear@1 167 if (cinfo->Ah == 0) { /* DC refinement needs no table */
nuclear@1 168 tbl = compptr->dc_tbl_no;
nuclear@1 169 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
nuclear@1 170 & entropy->derived_tbls[tbl]);
nuclear@1 171 }
nuclear@1 172 } else {
nuclear@1 173 tbl = compptr->ac_tbl_no;
nuclear@1 174 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
nuclear@1 175 & entropy->derived_tbls[tbl]);
nuclear@1 176 /* remember the single active table */
nuclear@1 177 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
nuclear@1 178 }
nuclear@1 179 /* Initialize DC predictions to 0 */
nuclear@1 180 entropy->saved.last_dc_val[ci] = 0;
nuclear@1 181 }
nuclear@1 182
nuclear@1 183 /* Initialize bitread state variables */
nuclear@1 184 entropy->bitstate.bits_left = 0;
nuclear@1 185 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
nuclear@1 186 entropy->pub.insufficient_data = FALSE;
nuclear@1 187
nuclear@1 188 /* Initialize private state variables */
nuclear@1 189 entropy->saved.EOBRUN = 0;
nuclear@1 190
nuclear@1 191 /* Initialize restart counter */
nuclear@1 192 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@1 193 }
nuclear@1 194
nuclear@1 195
nuclear@1 196 /*
nuclear@1 197 * Figure F.12: extend sign bit.
nuclear@1 198 * On some machines, a shift and add will be faster than a table lookup.
nuclear@1 199 */
nuclear@1 200
nuclear@1 201 #ifdef AVOID_TABLES
nuclear@1 202
nuclear@1 203 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
nuclear@1 204
nuclear@1 205 #else
nuclear@1 206
nuclear@1 207 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
nuclear@1 208
nuclear@1 209 static const int extend_test[16] = /* entry n is 2**(n-1) */
nuclear@1 210 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
nuclear@1 211 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
nuclear@1 212
nuclear@1 213 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
nuclear@1 214 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
nuclear@1 215 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
nuclear@1 216 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
nuclear@1 217 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
nuclear@1 218
nuclear@1 219 #endif /* AVOID_TABLES */
nuclear@1 220
nuclear@1 221
nuclear@1 222 /*
nuclear@1 223 * Check for a restart marker & resynchronize decoder.
nuclear@1 224 * Returns FALSE if must suspend.
nuclear@1 225 */
nuclear@1 226
nuclear@1 227 LOCAL(boolean)
nuclear@1 228 process_restart (j_decompress_ptr cinfo)
nuclear@1 229 {
nuclear@1 230 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 231 int ci;
nuclear@1 232
nuclear@1 233 /* Throw away any unused bits remaining in bit buffer; */
nuclear@1 234 /* include any full bytes in next_marker's count of discarded bytes */
nuclear@1 235 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
nuclear@1 236 entropy->bitstate.bits_left = 0;
nuclear@1 237
nuclear@1 238 /* Advance past the RSTn marker */
nuclear@1 239 if (! (*cinfo->marker->read_restart_marker) (cinfo))
nuclear@1 240 return FALSE;
nuclear@1 241
nuclear@1 242 /* Re-initialize DC predictions to 0 */
nuclear@1 243 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
nuclear@1 244 entropy->saved.last_dc_val[ci] = 0;
nuclear@1 245 /* Re-init EOB run count, too */
nuclear@1 246 entropy->saved.EOBRUN = 0;
nuclear@1 247
nuclear@1 248 /* Reset restart counter */
nuclear@1 249 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@1 250
nuclear@1 251 /* Reset out-of-data flag, unless read_restart_marker left us smack up
nuclear@1 252 * against a marker. In that case we will end up treating the next data
nuclear@1 253 * segment as empty, and we can avoid producing bogus output pixels by
nuclear@1 254 * leaving the flag set.
nuclear@1 255 */
nuclear@1 256 if (cinfo->unread_marker == 0)
nuclear@1 257 entropy->pub.insufficient_data = FALSE;
nuclear@1 258
nuclear@1 259 return TRUE;
nuclear@1 260 }
nuclear@1 261
nuclear@1 262
nuclear@1 263 /*
nuclear@1 264 * Huffman MCU decoding.
nuclear@1 265 * Each of these routines decodes and returns one MCU's worth of
nuclear@1 266 * Huffman-compressed coefficients.
nuclear@1 267 * The coefficients are reordered from zigzag order into natural array order,
nuclear@1 268 * but are not dequantized.
nuclear@1 269 *
nuclear@1 270 * The i'th block of the MCU is stored into the block pointed to by
nuclear@1 271 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
nuclear@1 272 *
nuclear@1 273 * We return FALSE if data source requested suspension. In that case no
nuclear@1 274 * changes have been made to permanent state. (Exception: some output
nuclear@1 275 * coefficients may already have been assigned. This is harmless for
nuclear@1 276 * spectral selection, since we'll just re-assign them on the next call.
nuclear@1 277 * Successive approximation AC refinement has to be more careful, however.)
nuclear@1 278 */
nuclear@1 279
nuclear@1 280 /*
nuclear@1 281 * MCU decoding for DC initial scan (either spectral selection,
nuclear@1 282 * or first pass of successive approximation).
nuclear@1 283 */
nuclear@1 284
nuclear@1 285 METHODDEF(boolean)
nuclear@1 286 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 287 {
nuclear@1 288 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 289 int Al = cinfo->Al;
nuclear@1 290 register int s, r;
nuclear@1 291 int blkn, ci;
nuclear@1 292 JBLOCKROW block;
nuclear@1 293 BITREAD_STATE_VARS;
nuclear@1 294 savable_state state;
nuclear@1 295 d_derived_tbl * tbl;
nuclear@1 296 jpeg_component_info * compptr;
nuclear@1 297
nuclear@1 298 /* Process restart marker if needed; may have to suspend */
nuclear@1 299 if (cinfo->restart_interval) {
nuclear@1 300 if (entropy->restarts_to_go == 0)
nuclear@1 301 if (! process_restart(cinfo))
nuclear@1 302 return FALSE;
nuclear@1 303 }
nuclear@1 304
nuclear@1 305 /* If we've run out of data, just leave the MCU set to zeroes.
nuclear@1 306 * This way, we return uniform gray for the remainder of the segment.
nuclear@1 307 */
nuclear@1 308 if (! entropy->pub.insufficient_data) {
nuclear@1 309
nuclear@1 310 /* Load up working state */
nuclear@1 311 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
nuclear@1 312 ASSIGN_STATE(state, entropy->saved);
nuclear@1 313
nuclear@1 314 /* Outer loop handles each block in the MCU */
nuclear@1 315
nuclear@1 316 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
nuclear@1 317 block = MCU_data[blkn];
nuclear@1 318 ci = cinfo->MCU_membership[blkn];
nuclear@1 319 compptr = cinfo->cur_comp_info[ci];
nuclear@1 320 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
nuclear@1 321
nuclear@1 322 /* Decode a single block's worth of coefficients */
nuclear@1 323
nuclear@1 324 /* Section F.2.2.1: decode the DC coefficient difference */
nuclear@1 325 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
nuclear@1 326 if (s) {
nuclear@1 327 CHECK_BIT_BUFFER(br_state, s, return FALSE);
nuclear@1 328 r = GET_BITS(s);
nuclear@1 329 s = HUFF_EXTEND(r, s);
nuclear@1 330 }
nuclear@1 331
nuclear@1 332 /* Convert DC difference to actual value, update last_dc_val */
nuclear@1 333 s += state.last_dc_val[ci];
nuclear@1 334 state.last_dc_val[ci] = s;
nuclear@1 335 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
nuclear@1 336 (*block)[0] = (JCOEF) (s << Al);
nuclear@1 337 }
nuclear@1 338
nuclear@1 339 /* Completed MCU, so update state */
nuclear@1 340 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
nuclear@1 341 ASSIGN_STATE(entropy->saved, state);
nuclear@1 342 }
nuclear@1 343
nuclear@1 344 /* Account for restart interval (no-op if not using restarts) */
nuclear@1 345 entropy->restarts_to_go--;
nuclear@1 346
nuclear@1 347 return TRUE;
nuclear@1 348 }
nuclear@1 349
nuclear@1 350
nuclear@1 351 /*
nuclear@1 352 * MCU decoding for AC initial scan (either spectral selection,
nuclear@1 353 * or first pass of successive approximation).
nuclear@1 354 */
nuclear@1 355
nuclear@1 356 METHODDEF(boolean)
nuclear@1 357 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 358 {
nuclear@1 359 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 360 int Se = cinfo->Se;
nuclear@1 361 int Al = cinfo->Al;
nuclear@1 362 register int s, k, r;
nuclear@1 363 unsigned int EOBRUN;
nuclear@1 364 JBLOCKROW block;
nuclear@1 365 BITREAD_STATE_VARS;
nuclear@1 366 d_derived_tbl * tbl;
nuclear@1 367
nuclear@1 368 /* Process restart marker if needed; may have to suspend */
nuclear@1 369 if (cinfo->restart_interval) {
nuclear@1 370 if (entropy->restarts_to_go == 0)
nuclear@1 371 if (! process_restart(cinfo))
nuclear@1 372 return FALSE;
nuclear@1 373 }
nuclear@1 374
nuclear@1 375 /* If we've run out of data, just leave the MCU set to zeroes.
nuclear@1 376 * This way, we return uniform gray for the remainder of the segment.
nuclear@1 377 */
nuclear@1 378 if (! entropy->pub.insufficient_data) {
nuclear@1 379
nuclear@1 380 /* Load up working state.
nuclear@1 381 * We can avoid loading/saving bitread state if in an EOB run.
nuclear@1 382 */
nuclear@1 383 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
nuclear@1 384
nuclear@1 385 /* There is always only one block per MCU */
nuclear@1 386
nuclear@1 387 if (EOBRUN > 0) /* if it's a band of zeroes... */
nuclear@1 388 EOBRUN--; /* ...process it now (we do nothing) */
nuclear@1 389 else {
nuclear@1 390 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
nuclear@1 391 block = MCU_data[0];
nuclear@1 392 tbl = entropy->ac_derived_tbl;
nuclear@1 393
nuclear@1 394 for (k = cinfo->Ss; k <= Se; k++) {
nuclear@1 395 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
nuclear@1 396 r = s >> 4;
nuclear@1 397 s &= 15;
nuclear@1 398 if (s) {
nuclear@1 399 k += r;
nuclear@1 400 CHECK_BIT_BUFFER(br_state, s, return FALSE);
nuclear@1 401 r = GET_BITS(s);
nuclear@1 402 s = HUFF_EXTEND(r, s);
nuclear@1 403 /* Scale and output coefficient in natural (dezigzagged) order */
nuclear@1 404 (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
nuclear@1 405 } else {
nuclear@1 406 if (r == 15) { /* ZRL */
nuclear@1 407 k += 15; /* skip 15 zeroes in band */
nuclear@1 408 } else { /* EOBr, run length is 2^r + appended bits */
nuclear@1 409 EOBRUN = 1 << r;
nuclear@1 410 if (r) { /* EOBr, r > 0 */
nuclear@1 411 CHECK_BIT_BUFFER(br_state, r, return FALSE);
nuclear@1 412 r = GET_BITS(r);
nuclear@1 413 EOBRUN += r;
nuclear@1 414 }
nuclear@1 415 EOBRUN--; /* this band is processed at this moment */
nuclear@1 416 break; /* force end-of-band */
nuclear@1 417 }
nuclear@1 418 }
nuclear@1 419 }
nuclear@1 420
nuclear@1 421 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
nuclear@1 422 }
nuclear@1 423
nuclear@1 424 /* Completed MCU, so update state */
nuclear@1 425 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
nuclear@1 426 }
nuclear@1 427
nuclear@1 428 /* Account for restart interval (no-op if not using restarts) */
nuclear@1 429 entropy->restarts_to_go--;
nuclear@1 430
nuclear@1 431 return TRUE;
nuclear@1 432 }
nuclear@1 433
nuclear@1 434
nuclear@1 435 /*
nuclear@1 436 * MCU decoding for DC successive approximation refinement scan.
nuclear@1 437 * Note: we assume such scans can be multi-component, although the spec
nuclear@1 438 * is not very clear on the point.
nuclear@1 439 */
nuclear@1 440
nuclear@1 441 METHODDEF(boolean)
nuclear@1 442 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 443 {
nuclear@1 444 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 445 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
nuclear@1 446 int blkn;
nuclear@1 447 JBLOCKROW block;
nuclear@1 448 BITREAD_STATE_VARS;
nuclear@1 449
nuclear@1 450 /* Process restart marker if needed; may have to suspend */
nuclear@1 451 if (cinfo->restart_interval) {
nuclear@1 452 if (entropy->restarts_to_go == 0)
nuclear@1 453 if (! process_restart(cinfo))
nuclear@1 454 return FALSE;
nuclear@1 455 }
nuclear@1 456
nuclear@1 457 /* Not worth the cycles to check insufficient_data here,
nuclear@1 458 * since we will not change the data anyway if we read zeroes.
nuclear@1 459 */
nuclear@1 460
nuclear@1 461 /* Load up working state */
nuclear@1 462 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
nuclear@1 463
nuclear@1 464 /* Outer loop handles each block in the MCU */
nuclear@1 465
nuclear@1 466 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
nuclear@1 467 block = MCU_data[blkn];
nuclear@1 468
nuclear@1 469 /* Encoded data is simply the next bit of the two's-complement DC value */
nuclear@1 470 CHECK_BIT_BUFFER(br_state, 1, return FALSE);
nuclear@1 471 if (GET_BITS(1))
nuclear@1 472 (*block)[0] |= p1;
nuclear@1 473 /* Note: since we use |=, repeating the assignment later is safe */
nuclear@1 474 }
nuclear@1 475
nuclear@1 476 /* Completed MCU, so update state */
nuclear@1 477 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
nuclear@1 478
nuclear@1 479 /* Account for restart interval (no-op if not using restarts) */
nuclear@1 480 entropy->restarts_to_go--;
nuclear@1 481
nuclear@1 482 return TRUE;
nuclear@1 483 }
nuclear@1 484
nuclear@1 485
nuclear@1 486 /*
nuclear@1 487 * MCU decoding for AC successive approximation refinement scan.
nuclear@1 488 */
nuclear@1 489
nuclear@1 490 METHODDEF(boolean)
nuclear@1 491 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 492 {
nuclear@1 493 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 494 int Se = cinfo->Se;
nuclear@1 495 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
nuclear@1 496 int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
nuclear@1 497 register int s, k, r;
nuclear@1 498 unsigned int EOBRUN;
nuclear@1 499 JBLOCKROW block;
nuclear@1 500 JCOEFPTR thiscoef;
nuclear@1 501 BITREAD_STATE_VARS;
nuclear@1 502 d_derived_tbl * tbl;
nuclear@1 503 int num_newnz;
nuclear@1 504 int newnz_pos[DCTSIZE2];
nuclear@1 505
nuclear@1 506 /* Process restart marker if needed; may have to suspend */
nuclear@1 507 if (cinfo->restart_interval) {
nuclear@1 508 if (entropy->restarts_to_go == 0)
nuclear@1 509 if (! process_restart(cinfo))
nuclear@1 510 return FALSE;
nuclear@1 511 }
nuclear@1 512
nuclear@1 513 /* If we've run out of data, don't modify the MCU.
nuclear@1 514 */
nuclear@1 515 if (! entropy->pub.insufficient_data) {
nuclear@1 516
nuclear@1 517 /* Load up working state */
nuclear@1 518 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
nuclear@1 519 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
nuclear@1 520
nuclear@1 521 /* There is always only one block per MCU */
nuclear@1 522 block = MCU_data[0];
nuclear@1 523 tbl = entropy->ac_derived_tbl;
nuclear@1 524
nuclear@1 525 /* If we are forced to suspend, we must undo the assignments to any newly
nuclear@1 526 * nonzero coefficients in the block, because otherwise we'd get confused
nuclear@1 527 * next time about which coefficients were already nonzero.
nuclear@1 528 * But we need not undo addition of bits to already-nonzero coefficients;
nuclear@1 529 * instead, we can test the current bit to see if we already did it.
nuclear@1 530 */
nuclear@1 531 num_newnz = 0;
nuclear@1 532
nuclear@1 533 /* initialize coefficient loop counter to start of band */
nuclear@1 534 k = cinfo->Ss;
nuclear@1 535
nuclear@1 536 if (EOBRUN == 0) {
nuclear@1 537 for (; k <= Se; k++) {
nuclear@1 538 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
nuclear@1 539 r = s >> 4;
nuclear@1 540 s &= 15;
nuclear@1 541 if (s) {
nuclear@1 542 if (s != 1) /* size of new coef should always be 1 */
nuclear@1 543 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
nuclear@1 544 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
nuclear@1 545 if (GET_BITS(1))
nuclear@1 546 s = p1; /* newly nonzero coef is positive */
nuclear@1 547 else
nuclear@1 548 s = m1; /* newly nonzero coef is negative */
nuclear@1 549 } else {
nuclear@1 550 if (r != 15) {
nuclear@1 551 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
nuclear@1 552 if (r) {
nuclear@1 553 CHECK_BIT_BUFFER(br_state, r, goto undoit);
nuclear@1 554 r = GET_BITS(r);
nuclear@1 555 EOBRUN += r;
nuclear@1 556 }
nuclear@1 557 break; /* rest of block is handled by EOB logic */
nuclear@1 558 }
nuclear@1 559 /* note s = 0 for processing ZRL */
nuclear@1 560 }
nuclear@1 561 /* Advance over already-nonzero coefs and r still-zero coefs,
nuclear@1 562 * appending correction bits to the nonzeroes. A correction bit is 1
nuclear@1 563 * if the absolute value of the coefficient must be increased.
nuclear@1 564 */
nuclear@1 565 do {
nuclear@1 566 thiscoef = *block + jpeg_natural_order[k];
nuclear@1 567 if (*thiscoef != 0) {
nuclear@1 568 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
nuclear@1 569 if (GET_BITS(1)) {
nuclear@1 570 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
nuclear@1 571 if (*thiscoef >= 0)
nuclear@1 572 *thiscoef += p1;
nuclear@1 573 else
nuclear@1 574 *thiscoef += m1;
nuclear@1 575 }
nuclear@1 576 }
nuclear@1 577 } else {
nuclear@1 578 if (--r < 0)
nuclear@1 579 break; /* reached target zero coefficient */
nuclear@1 580 }
nuclear@1 581 k++;
nuclear@1 582 } while (k <= Se);
nuclear@1 583 if (s) {
nuclear@1 584 int pos = jpeg_natural_order[k];
nuclear@1 585 /* Output newly nonzero coefficient */
nuclear@1 586 (*block)[pos] = (JCOEF) s;
nuclear@1 587 /* Remember its position in case we have to suspend */
nuclear@1 588 newnz_pos[num_newnz++] = pos;
nuclear@1 589 }
nuclear@1 590 }
nuclear@1 591 }
nuclear@1 592
nuclear@1 593 if (EOBRUN > 0) {
nuclear@1 594 /* Scan any remaining coefficient positions after the end-of-band
nuclear@1 595 * (the last newly nonzero coefficient, if any). Append a correction
nuclear@1 596 * bit to each already-nonzero coefficient. A correction bit is 1
nuclear@1 597 * if the absolute value of the coefficient must be increased.
nuclear@1 598 */
nuclear@1 599 for (; k <= Se; k++) {
nuclear@1 600 thiscoef = *block + jpeg_natural_order[k];
nuclear@1 601 if (*thiscoef != 0) {
nuclear@1 602 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
nuclear@1 603 if (GET_BITS(1)) {
nuclear@1 604 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
nuclear@1 605 if (*thiscoef >= 0)
nuclear@1 606 *thiscoef += p1;
nuclear@1 607 else
nuclear@1 608 *thiscoef += m1;
nuclear@1 609 }
nuclear@1 610 }
nuclear@1 611 }
nuclear@1 612 }
nuclear@1 613 /* Count one block completed in EOB run */
nuclear@1 614 EOBRUN--;
nuclear@1 615 }
nuclear@1 616
nuclear@1 617 /* Completed MCU, so update state */
nuclear@1 618 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
nuclear@1 619 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
nuclear@1 620 }
nuclear@1 621
nuclear@1 622 /* Account for restart interval (no-op if not using restarts) */
nuclear@1 623 entropy->restarts_to_go--;
nuclear@1 624
nuclear@1 625 return TRUE;
nuclear@1 626
nuclear@1 627 undoit:
nuclear@1 628 /* Re-zero any output coefficients that we made newly nonzero */
nuclear@1 629 while (num_newnz > 0)
nuclear@1 630 (*block)[newnz_pos[--num_newnz]] = 0;
nuclear@1 631
nuclear@1 632 return FALSE;
nuclear@1 633 }
nuclear@1 634
nuclear@1 635
nuclear@1 636 /*
nuclear@1 637 * Module initialization routine for progressive Huffman entropy decoding.
nuclear@1 638 */
nuclear@1 639
nuclear@1 640 GLOBAL(void)
nuclear@1 641 jinit_phuff_decoder (j_decompress_ptr cinfo)
nuclear@1 642 {
nuclear@1 643 phuff_entropy_ptr entropy;
nuclear@1 644 int *coef_bit_ptr;
nuclear@1 645 int ci, i;
nuclear@1 646
nuclear@1 647 entropy = (phuff_entropy_ptr)
nuclear@1 648 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 649 SIZEOF(phuff_entropy_decoder));
nuclear@1 650 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
nuclear@1 651 entropy->pub.start_pass = start_pass_phuff_decoder;
nuclear@1 652
nuclear@1 653 /* Mark derived tables unallocated */
nuclear@1 654 for (i = 0; i < NUM_HUFF_TBLS; i++) {
nuclear@1 655 entropy->derived_tbls[i] = NULL;
nuclear@1 656 }
nuclear@1 657
nuclear@1 658 /* Create progression status table */
nuclear@1 659 cinfo->coef_bits = (int (*)[DCTSIZE2])
nuclear@1 660 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 661 cinfo->num_components*DCTSIZE2*SIZEOF(int));
nuclear@1 662 coef_bit_ptr = & cinfo->coef_bits[0][0];
nuclear@1 663 for (ci = 0; ci < cinfo->num_components; ci++)
nuclear@1 664 for (i = 0; i < DCTSIZE2; i++)
nuclear@1 665 *coef_bit_ptr++ = -1;
nuclear@1 666 }
nuclear@1 667
nuclear@1 668 #endif /* D_PROGRESSIVE_SUPPORTED */