|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* jdphuff.c
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file was part of the Independent JPEG Group's software:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 1995-1997, Thomas G. Lane.
|
|
shun-iwasawa |
82a8f5 |
* libjpeg-turbo Modifications:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 2015-2016, 2018, D. R. Commander.
|
|
shun-iwasawa |
82a8f5 |
* For conditions of distribution and use, see the accompanying README.ijg
|
|
shun-iwasawa |
82a8f5 |
* file.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file contains Huffman entropy decoding routines for progressive JPEG.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Much of the complexity here has to do with supporting input suspension.
|
|
shun-iwasawa |
82a8f5 |
* If the data source module demands suspension, we want to be able to back
|
|
shun-iwasawa |
82a8f5 |
* up to the start of the current MCU. To do this, we copy state variables
|
|
shun-iwasawa |
82a8f5 |
* into local working storage, and update them back to the permanent
|
|
shun-iwasawa |
82a8f5 |
* storage only upon successful completion of an MCU.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* NOTE: All referenced figures are from
|
|
shun-iwasawa |
82a8f5 |
* Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define JPEG_INTERNALS
|
|
shun-iwasawa |
82a8f5 |
#include "jinclude.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jpeglib.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jdhuff.h" /* Declarations shared with jdhuff.c */
|
|
shun-iwasawa |
82a8f5 |
#include <limits.h></limits.h>
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifdef D_PROGRESSIVE_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Expanded entropy decoder object for progressive Huffman decoding.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The savable_state subrecord contains fields that change within an MCU,
|
|
shun-iwasawa |
82a8f5 |
* but must not be updated permanently until we complete the MCU.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef struct {
|
|
shun-iwasawa |
82a8f5 |
unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
|
|
shun-iwasawa |
82a8f5 |
int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
|
shun-iwasawa |
82a8f5 |
} savable_state;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* This macro is to work around compilers with missing or broken
|
|
shun-iwasawa |
82a8f5 |
* structure assignment. You'll need to fix this code if you have
|
|
shun-iwasawa |
82a8f5 |
* such a compiler and you change MAX_COMPS_IN_SCAN.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifndef NO_STRUCT_ASSIGN
|
|
shun-iwasawa |
82a8f5 |
#define ASSIGN_STATE(dest, src) ((dest) = (src))
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
#if MAX_COMPS_IN_SCAN == 4
|
|
shun-iwasawa |
82a8f5 |
#define ASSIGN_STATE(dest, src) \
|
|
shun-iwasawa |
82a8f5 |
((dest).EOBRUN = (src).EOBRUN, \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[0] = (src).last_dc_val[0], \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[1] = (src).last_dc_val[1], \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[2] = (src).last_dc_val[2], \
|
|
shun-iwasawa |
82a8f5 |
(dest).last_dc_val[3] = (src).last_dc_val[3])
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef struct {
|
|
shun-iwasawa |
82a8f5 |
struct jpeg_entropy_decoder pub; /* public fields */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* These fields are loaded into local variables at start of each MCU.
|
|
shun-iwasawa |
82a8f5 |
* In case of suspension, we exit WITHOUT updating them.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
bitread_perm_state bitstate; /* Bit buffer at start of MCU */
|
|
shun-iwasawa |
82a8f5 |
savable_state saved; /* Other state at start of MCU */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* These fields are NOT loaded into local working state. */
|
|
shun-iwasawa |
82a8f5 |
unsigned int restarts_to_go; /* MCUs left in this restart interval */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Pointers to derived tables (these workspaces have image lifespan) */
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *ac_derived_tbl; /* active table during an AC scan */
|
|
shun-iwasawa |
82a8f5 |
} phuff_entropy_decoder;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef phuff_entropy_decoder *phuff_entropy_ptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Forward declarations */
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean) decode_mcu_DC_first(j_decompress_ptr cinfo,
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW *MCU_data);
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean) decode_mcu_AC_first(j_decompress_ptr cinfo,
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW *MCU_data);
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean) decode_mcu_DC_refine(j_decompress_ptr cinfo,
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW *MCU_data);
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean) decode_mcu_AC_refine(j_decompress_ptr cinfo,
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW *MCU_data);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Initialize for a Huffman-compressed scan.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
start_pass_phuff_decoder(j_decompress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
boolean is_DC_band, bad;
|
|
shun-iwasawa |
82a8f5 |
int ci, coefi, tbl;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl **pdtbl;
|
|
shun-iwasawa |
82a8f5 |
int *coef_bit_ptr;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
is_DC_band = (cinfo->Ss == 0);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Validate scan parameters */
|
|
shun-iwasawa |
82a8f5 |
bad = FALSE;
|
|
shun-iwasawa |
82a8f5 |
if (is_DC_band) {
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Se != 0)
|
|
shun-iwasawa |
82a8f5 |
bad = TRUE;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
/* need not check Ss/Se < 0 since they came from unsigned bytes */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
|
|
shun-iwasawa |
82a8f5 |
bad = TRUE;
|
|
shun-iwasawa |
82a8f5 |
/* AC scans may have only one component */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->comps_in_scan != 1)
|
|
shun-iwasawa |
82a8f5 |
bad = TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Ah != 0) {
|
|
shun-iwasawa |
82a8f5 |
/* Successive approximation refinement scan: must have Al = Ah-1. */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Al != cinfo->Ah - 1)
|
|
shun-iwasawa |
82a8f5 |
bad = TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Al > 13) /* need not check for < 0 */
|
|
shun-iwasawa |
82a8f5 |
bad = TRUE;
|
|
shun-iwasawa |
82a8f5 |
/* Arguably the maximum Al value should be less than 13 for 8-bit precision,
|
|
shun-iwasawa |
82a8f5 |
* but the spec doesn't say so, and we try to be liberal about what we
|
|
shun-iwasawa |
82a8f5 |
* accept. Note: large Al values could result in out-of-range DC
|
|
shun-iwasawa |
82a8f5 |
* coefficients during early scans, leading to bizarre displays due to
|
|
shun-iwasawa |
82a8f5 |
* overflows in the IDCT math. But we won't crash.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (bad)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
|
|
shun-iwasawa |
82a8f5 |
cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
|
|
shun-iwasawa |
82a8f5 |
/* Update progression status, and verify that scan order is legal.
|
|
shun-iwasawa |
82a8f5 |
* Note that inter-scan inconsistencies are treated as warnings
|
|
shun-iwasawa |
82a8f5 |
* not fatal errors ... not clear if this is right way to behave.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
|
shun-iwasawa |
82a8f5 |
int cindex = cinfo->cur_comp_info[ci]->component_index;
|
|
shun-iwasawa |
82a8f5 |
coef_bit_ptr = &cinfo->coef_bits[cindex][0];
|
|
shun-iwasawa |
82a8f5 |
if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
|
|
shun-iwasawa |
82a8f5 |
WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
|
|
shun-iwasawa |
82a8f5 |
for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
|
|
shun-iwasawa |
82a8f5 |
int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Ah != expected)
|
|
shun-iwasawa |
82a8f5 |
WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
|
|
shun-iwasawa |
82a8f5 |
coef_bit_ptr[coefi] = cinfo->Al;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Select MCU decoding routine */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Ah == 0) {
|
|
shun-iwasawa |
82a8f5 |
if (is_DC_band)
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.decode_mcu = decode_mcu_DC_first;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.decode_mcu = decode_mcu_AC_first;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (is_DC_band)
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.decode_mcu = decode_mcu_DC_refine;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.decode_mcu = decode_mcu_AC_refine;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
|
shun-iwasawa |
82a8f5 |
compptr = cinfo->cur_comp_info[ci];
|
|
shun-iwasawa |
82a8f5 |
/* Make sure requested tables are present, and compute derived tables.
|
|
shun-iwasawa |
82a8f5 |
* We may build same derived table more than once, but it's not expensive.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (is_DC_band) {
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->Ah == 0) { /* DC refinement needs no table */
|
|
shun-iwasawa |
82a8f5 |
tbl = compptr->dc_tbl_no;
|
|
shun-iwasawa |
82a8f5 |
pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
tbl = compptr->ac_tbl_no;
|
|
shun-iwasawa |
82a8f5 |
pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
|
|
shun-iwasawa |
82a8f5 |
/* remember the single active table */
|
|
shun-iwasawa |
82a8f5 |
entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
/* Initialize DC predictions to 0 */
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.last_dc_val[ci] = 0;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Initialize bitread state variables */
|
|
shun-iwasawa |
82a8f5 |
entropy->bitstate.bits_left = 0;
|
|
shun-iwasawa |
82a8f5 |
entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.insufficient_data = FALSE;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Initialize private state variables */
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.EOBRUN = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Initialize restart counter */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go = cinfo->restart_interval;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Figure F.12: extend sign bit.
|
|
shun-iwasawa |
82a8f5 |
* On some machines, a shift and add will be faster than a table lookup.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define AVOID_TABLES
|
|
shun-iwasawa |
82a8f5 |
#ifdef AVOID_TABLES
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define NEG_1 ((unsigned)-1)
|
|
shun-iwasawa |
82a8f5 |
#define HUFF_EXTEND(x, s) \
|
|
shun-iwasawa |
82a8f5 |
((x) < (1 << ((s) - 1)) ? (x) + (((NEG_1) << (s)) + 1) : (x))
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define HUFF_EXTEND(x, s) \
|
|
shun-iwasawa |
82a8f5 |
((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
static const int extend_test[16] = { /* entry n is 2**(n-1) */
|
|
shun-iwasawa |
82a8f5 |
0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
|
|
shun-iwasawa |
82a8f5 |
0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */
|
|
shun-iwasawa |
82a8f5 |
0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1,
|
|
shun-iwasawa |
82a8f5 |
((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1,
|
|
shun-iwasawa |
82a8f5 |
((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
|
|
shun-iwasawa |
82a8f5 |
((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif /* AVOID_TABLES */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Check for a restart marker & resynchronize decoder.
|
|
shun-iwasawa |
82a8f5 |
* Returns FALSE if must suspend.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(boolean)
|
|
shun-iwasawa |
82a8f5 |
process_restart(j_decompress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int ci;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Throw away any unused bits remaining in bit buffer; */
|
|
shun-iwasawa |
82a8f5 |
/* include any full bytes in next_marker's count of discarded bytes */
|
|
shun-iwasawa |
82a8f5 |
cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
|
|
shun-iwasawa |
82a8f5 |
entropy->bitstate.bits_left = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Advance past the RSTn marker */
|
|
shun-iwasawa |
82a8f5 |
if (!(*cinfo->marker->read_restart_marker) (cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Re-initialize DC predictions to 0 */
|
|
shun-iwasawa |
82a8f5 |
for (ci = 0; ci < cinfo->comps_in_scan; ci++)
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.last_dc_val[ci] = 0;
|
|
shun-iwasawa |
82a8f5 |
/* Re-init EOB run count, too */
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.EOBRUN = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Reset restart counter */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go = cinfo->restart_interval;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Reset out-of-data flag, unless read_restart_marker left us smack up
|
|
shun-iwasawa |
82a8f5 |
* against a marker. In that case we will end up treating the next data
|
|
shun-iwasawa |
82a8f5 |
* segment as empty, and we can avoid producing bogus output pixels by
|
|
shun-iwasawa |
82a8f5 |
* leaving the flag set.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->unread_marker == 0)
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.insufficient_data = FALSE;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Huffman MCU decoding.
|
|
shun-iwasawa |
82a8f5 |
* Each of these routines decodes and returns one MCU's worth of
|
|
shun-iwasawa |
82a8f5 |
* Huffman-compressed coefficients.
|
|
shun-iwasawa |
82a8f5 |
* The coefficients are reordered from zigzag order into natural array order,
|
|
shun-iwasawa |
82a8f5 |
* but are not dequantized.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The i'th block of the MCU is stored into the block pointed to by
|
|
shun-iwasawa |
82a8f5 |
* MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* We return FALSE if data source requested suspension. In that case no
|
|
shun-iwasawa |
82a8f5 |
* changes have been made to permanent state. (Exception: some output
|
|
shun-iwasawa |
82a8f5 |
* coefficients may already have been assigned. This is harmless for
|
|
shun-iwasawa |
82a8f5 |
* spectral selection, since we'll just re-assign them on the next call.
|
|
shun-iwasawa |
82a8f5 |
* Successive approximation AC refinement has to be more careful, however.)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* MCU decoding for DC initial scan (either spectral selection,
|
|
shun-iwasawa |
82a8f5 |
* or first pass of successive approximation).
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int Al = cinfo->Al;
|
|
shun-iwasawa |
82a8f5 |
register int s, r;
|
|
shun-iwasawa |
82a8f5 |
int blkn, ci;
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW block;
|
|
shun-iwasawa |
82a8f5 |
BITREAD_STATE_VARS;
|
|
shun-iwasawa |
82a8f5 |
savable_state state;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *tbl;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Process restart marker if needed; may have to suspend */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->restart_interval) {
|
|
shun-iwasawa |
82a8f5 |
if (entropy->restarts_to_go == 0)
|
|
shun-iwasawa |
82a8f5 |
if (!process_restart(cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If we've run out of data, just leave the MCU set to zeroes.
|
|
shun-iwasawa |
82a8f5 |
* This way, we return uniform gray for the remainder of the segment.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (!entropy->pub.insufficient_data) {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(state, entropy->saved);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Outer loop handles each block in the MCU */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
|
shun-iwasawa |
82a8f5 |
block = MCU_data[blkn];
|
|
shun-iwasawa |
82a8f5 |
ci = cinfo->MCU_membership[blkn];
|
|
shun-iwasawa |
82a8f5 |
compptr = cinfo->cur_comp_info[ci];
|
|
shun-iwasawa |
82a8f5 |
tbl = entropy->derived_tbls[compptr->dc_tbl_no];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Decode a single block's worth of coefficients */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Section F.2.2.1: decode the DC coefficient difference */
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
s = HUFF_EXTEND(r, s);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Convert DC difference to actual value, update last_dc_val */
|
|
shun-iwasawa |
82a8f5 |
if ((state.last_dc_val[ci] >= 0 &&
|
|
shun-iwasawa |
82a8f5 |
s > INT_MAX - state.last_dc_val[ci]) ||
|
|
shun-iwasawa |
82a8f5 |
(state.last_dc_val[ci] < 0 && s < INT_MIN - state.last_dc_val[ci]))
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
|
|
shun-iwasawa |
82a8f5 |
s += state.last_dc_val[ci];
|
|
shun-iwasawa |
82a8f5 |
state.last_dc_val[ci] = s;
|
|
shun-iwasawa |
82a8f5 |
/* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
|
|
shun-iwasawa |
82a8f5 |
(*block)[0] = (JCOEF)LEFT_SHIFT(s, Al);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Completed MCU, so update state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
ASSIGN_STATE(entropy->saved, state);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Account for restart interval (no-op if not using restarts) */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go--;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* MCU decoding for AC initial scan (either spectral selection,
|
|
shun-iwasawa |
82a8f5 |
* or first pass of successive approximation).
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int Se = cinfo->Se;
|
|
shun-iwasawa |
82a8f5 |
int Al = cinfo->Al;
|
|
shun-iwasawa |
82a8f5 |
register int s, k, r;
|
|
shun-iwasawa |
82a8f5 |
unsigned int EOBRUN;
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW block;
|
|
shun-iwasawa |
82a8f5 |
BITREAD_STATE_VARS;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *tbl;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Process restart marker if needed; may have to suspend */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->restart_interval) {
|
|
shun-iwasawa |
82a8f5 |
if (entropy->restarts_to_go == 0)
|
|
shun-iwasawa |
82a8f5 |
if (!process_restart(cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If we've run out of data, just leave the MCU set to zeroes.
|
|
shun-iwasawa |
82a8f5 |
* This way, we return uniform gray for the remainder of the segment.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (!entropy->pub.insufficient_data) {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state.
|
|
shun-iwasawa |
82a8f5 |
* We can avoid loading/saving bitread state if in an EOB run.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* There is always only one block per MCU */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (EOBRUN > 0) /* if it's a band of zeroes... */
|
|
shun-iwasawa |
82a8f5 |
EOBRUN--; /* ...process it now (we do nothing) */
|
|
shun-iwasawa |
82a8f5 |
else {
|
|
shun-iwasawa |
82a8f5 |
BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
block = MCU_data[0];
|
|
shun-iwasawa |
82a8f5 |
tbl = entropy->ac_derived_tbl;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (k = cinfo->Ss; k <= Se; k++) {
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
|
|
shun-iwasawa |
82a8f5 |
r = s >> 4;
|
|
shun-iwasawa |
82a8f5 |
s &= 15;
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
k += r;
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(s);
|
|
shun-iwasawa |
82a8f5 |
s = HUFF_EXTEND(r, s);
|
|
shun-iwasawa |
82a8f5 |
/* Scale and output coefficient in natural (dezigzagged) order */
|
|
shun-iwasawa |
82a8f5 |
(*block)[jpeg_natural_order[k]] = (JCOEF)LEFT_SHIFT(s, Al);
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (r == 15) { /* ZRL */
|
|
shun-iwasawa |
82a8f5 |
k += 15; /* skip 15 zeroes in band */
|
|
shun-iwasawa |
82a8f5 |
} else { /* EOBr, run length is 2^r + appended bits */
|
|
shun-iwasawa |
82a8f5 |
EOBRUN = 1 << r;
|
|
shun-iwasawa |
82a8f5 |
if (r) { /* EOBr, r > 0 */
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, r, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(r);
|
|
shun-iwasawa |
82a8f5 |
EOBRUN += r;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
EOBRUN--; /* this band is processed at this moment */
|
|
shun-iwasawa |
82a8f5 |
break; /* force end-of-band */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Completed MCU, so update state */
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Account for restart interval (no-op if not using restarts) */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go--;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* MCU decoding for DC successive approximation refinement scan.
|
|
shun-iwasawa |
82a8f5 |
* Note: we assume such scans can be multi-component, although the spec
|
|
shun-iwasawa |
82a8f5 |
* is not very clear on the point.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
|
|
shun-iwasawa |
82a8f5 |
int blkn;
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW block;
|
|
shun-iwasawa |
82a8f5 |
BITREAD_STATE_VARS;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Process restart marker if needed; may have to suspend */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->restart_interval) {
|
|
shun-iwasawa |
82a8f5 |
if (entropy->restarts_to_go == 0)
|
|
shun-iwasawa |
82a8f5 |
if (!process_restart(cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Not worth the cycles to check insufficient_data here,
|
|
shun-iwasawa |
82a8f5 |
* since we will not change the data anyway if we read zeroes.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Outer loop handles each block in the MCU */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
|
shun-iwasawa |
82a8f5 |
block = MCU_data[blkn];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Encoded data is simply the next bit of the two's-complement DC value */
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, 1, return FALSE);
|
|
shun-iwasawa |
82a8f5 |
if (GET_BITS(1))
|
|
shun-iwasawa |
82a8f5 |
(*block)[0] |= p1;
|
|
shun-iwasawa |
82a8f5 |
/* Note: since we use |=, repeating the assignment later is safe */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Completed MCU, so update state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Account for restart interval (no-op if not using restarts) */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go--;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* MCU decoding for AC successive approximation refinement scan.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(boolean)
|
|
shun-iwasawa |
82a8f5 |
decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
|
|
shun-iwasawa |
82a8f5 |
int Se = cinfo->Se;
|
|
shun-iwasawa |
82a8f5 |
int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
|
|
shun-iwasawa |
82a8f5 |
int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
|
|
shun-iwasawa |
82a8f5 |
register int s, k, r;
|
|
shun-iwasawa |
82a8f5 |
unsigned int EOBRUN;
|
|
shun-iwasawa |
82a8f5 |
JBLOCKROW block;
|
|
shun-iwasawa |
82a8f5 |
JCOEFPTR thiscoef;
|
|
shun-iwasawa |
82a8f5 |
BITREAD_STATE_VARS;
|
|
shun-iwasawa |
82a8f5 |
d_derived_tbl *tbl;
|
|
shun-iwasawa |
82a8f5 |
int num_newnz;
|
|
shun-iwasawa |
82a8f5 |
int newnz_pos[DCTSIZE2];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Process restart marker if needed; may have to suspend */
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->restart_interval) {
|
|
shun-iwasawa |
82a8f5 |
if (entropy->restarts_to_go == 0)
|
|
shun-iwasawa |
82a8f5 |
if (!process_restart(cinfo))
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If we've run out of data, don't modify the MCU.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (!entropy->pub.insufficient_data) {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Load up working state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* There is always only one block per MCU */
|
|
shun-iwasawa |
82a8f5 |
block = MCU_data[0];
|
|
shun-iwasawa |
82a8f5 |
tbl = entropy->ac_derived_tbl;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* If we are forced to suspend, we must undo the assignments to any newly
|
|
shun-iwasawa |
82a8f5 |
* nonzero coefficients in the block, because otherwise we'd get confused
|
|
shun-iwasawa |
82a8f5 |
* next time about which coefficients were already nonzero.
|
|
shun-iwasawa |
82a8f5 |
* But we need not undo addition of bits to already-nonzero coefficients;
|
|
shun-iwasawa |
82a8f5 |
* instead, we can test the current bit to see if we already did it.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
num_newnz = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* initialize coefficient loop counter to start of band */
|
|
shun-iwasawa |
82a8f5 |
k = cinfo->Ss;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (EOBRUN == 0) {
|
|
shun-iwasawa |
82a8f5 |
for (; k <= Se; k++) {
|
|
shun-iwasawa |
82a8f5 |
HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
|
|
shun-iwasawa |
82a8f5 |
r = s >> 4;
|
|
shun-iwasawa |
82a8f5 |
s &= 15;
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
if (s != 1) /* size of new coef should always be 1 */
|
|
shun-iwasawa |
82a8f5 |
WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
|
shun-iwasawa |
82a8f5 |
if (GET_BITS(1))
|
|
shun-iwasawa |
82a8f5 |
s = p1; /* newly nonzero coef is positive */
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
s = m1; /* newly nonzero coef is negative */
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (r != 15) {
|
|
shun-iwasawa |
82a8f5 |
EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
|
|
shun-iwasawa |
82a8f5 |
if (r) {
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, r, goto undoit);
|
|
shun-iwasawa |
82a8f5 |
r = GET_BITS(r);
|
|
shun-iwasawa |
82a8f5 |
EOBRUN += r;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
break; /* rest of block is handled by EOB logic */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
/* note s = 0 for processing ZRL */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
/* Advance over already-nonzero coefs and r still-zero coefs,
|
|
shun-iwasawa |
82a8f5 |
* appending correction bits to the nonzeroes. A correction bit is 1
|
|
shun-iwasawa |
82a8f5 |
* if the absolute value of the coefficient must be increased.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
do {
|
|
shun-iwasawa |
82a8f5 |
thiscoef = *block + jpeg_natural_order[k];
|
|
shun-iwasawa |
82a8f5 |
if (*thiscoef != 0) {
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
|
shun-iwasawa |
82a8f5 |
if (GET_BITS(1)) {
|
|
shun-iwasawa |
82a8f5 |
if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
|
|
shun-iwasawa |
82a8f5 |
if (*thiscoef >= 0)
|
|
shun-iwasawa |
82a8f5 |
*thiscoef += p1;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
*thiscoef += m1;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
if (--r < 0)
|
|
shun-iwasawa |
82a8f5 |
break; /* reached target zero coefficient */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
k++;
|
|
shun-iwasawa |
82a8f5 |
} while (k <= Se);
|
|
shun-iwasawa |
82a8f5 |
if (s) {
|
|
shun-iwasawa |
82a8f5 |
int pos = jpeg_natural_order[k];
|
|
shun-iwasawa |
82a8f5 |
/* Output newly nonzero coefficient */
|
|
shun-iwasawa |
82a8f5 |
(*block)[pos] = (JCOEF)s;
|
|
shun-iwasawa |
82a8f5 |
/* Remember its position in case we have to suspend */
|
|
shun-iwasawa |
82a8f5 |
newnz_pos[num_newnz++] = pos;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (EOBRUN > 0) {
|
|
shun-iwasawa |
82a8f5 |
/* Scan any remaining coefficient positions after the end-of-band
|
|
shun-iwasawa |
82a8f5 |
* (the last newly nonzero coefficient, if any). Append a correction
|
|
shun-iwasawa |
82a8f5 |
* bit to each already-nonzero coefficient. A correction bit is 1
|
|
shun-iwasawa |
82a8f5 |
* if the absolute value of the coefficient must be increased.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
for (; k <= Se; k++) {
|
|
shun-iwasawa |
82a8f5 |
thiscoef = *block + jpeg_natural_order[k];
|
|
shun-iwasawa |
82a8f5 |
if (*thiscoef != 0) {
|
|
shun-iwasawa |
82a8f5 |
CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
|
shun-iwasawa |
82a8f5 |
if (GET_BITS(1)) {
|
|
shun-iwasawa |
82a8f5 |
if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
|
|
shun-iwasawa |
82a8f5 |
if (*thiscoef >= 0)
|
|
shun-iwasawa |
82a8f5 |
*thiscoef += p1;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
*thiscoef += m1;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
/* Count one block completed in EOB run */
|
|
shun-iwasawa |
82a8f5 |
EOBRUN--;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Completed MCU, so update state */
|
|
shun-iwasawa |
82a8f5 |
BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
|
|
shun-iwasawa |
82a8f5 |
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Account for restart interval (no-op if not using restarts) */
|
|
shun-iwasawa |
82a8f5 |
entropy->restarts_to_go--;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return TRUE;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
undoit:
|
|
shun-iwasawa |
82a8f5 |
/* Re-zero any output coefficients that we made newly nonzero */
|
|
shun-iwasawa |
82a8f5 |
while (num_newnz > 0)
|
|
shun-iwasawa |
82a8f5 |
(*block)[newnz_pos[--num_newnz]] = 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return FALSE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Module initialization routine for progressive Huffman entropy decoding.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jinit_phuff_decoder(j_decompress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
phuff_entropy_ptr entropy;
|
|
shun-iwasawa |
82a8f5 |
int *coef_bit_ptr;
|
|
shun-iwasawa |
82a8f5 |
int ci, i;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
entropy = (phuff_entropy_ptr)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(phuff_entropy_decoder));
|
|
shun-iwasawa |
82a8f5 |
cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
|
|
shun-iwasawa |
82a8f5 |
entropy->pub.start_pass = start_pass_phuff_decoder;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Mark derived tables unallocated */
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < NUM_HUFF_TBLS; i++) {
|
|
shun-iwasawa |
82a8f5 |
entropy->derived_tbls[i] = NULL;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Create progression status table */
|
|
shun-iwasawa |
82a8f5 |
cinfo->coef_bits = (int (*)[DCTSIZE2])
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
cinfo->num_components * DCTSIZE2 *
|
|
shun-iwasawa |
82a8f5 |
sizeof(int));
|
|
shun-iwasawa |
82a8f5 |
coef_bit_ptr = &cinfo->coef_bits[0][0];
|
|
shun-iwasawa |
82a8f5 |
for (ci = 0; ci < cinfo->num_components; ci++)
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < DCTSIZE2; i++)
|
|
shun-iwasawa |
82a8f5 |
*coef_bit_ptr++ = -1;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif /* D_PROGRESSIVE_SUPPORTED */
|