summaryrefslogtreecommitdiff
path: root/jpeg/jdhuff.c
diff options
context:
space:
mode:
Diffstat (limited to 'jpeg/jdhuff.c')
-rw-r--r--jpeg/jdhuff.c106
1 files changed, 56 insertions, 50 deletions
diff --git a/jpeg/jdhuff.c b/jpeg/jdhuff.c
index 835d06ecb..aea06f603 100644
--- a/jpeg/jdhuff.c
+++ b/jpeg/jdhuff.c
@@ -2,7 +2,7 @@
* jdhuff.c
*
* Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2006-2016 by Guido Vollbeding.
+ * Modified 2006-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
@@ -341,13 +341,12 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
htbl =
isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
if (htbl == NULL)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+ htbl = jpeg_std_huff_table((j_common_ptr) cinfo, isDC, tblno);
/* Allocate a workspace if we haven't already done so. */
if (*pdtbl == NULL)
- *pdtbl = (d_derived_tbl *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(d_derived_tbl));
+ *pdtbl = (d_derived_tbl *) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(d_derived_tbl));
dtbl = *pdtbl;
dtbl->pub = htbl; /* fill in back link */
@@ -706,7 +705,7 @@ process_restart (j_decompress_ptr cinfo)
METHODDEF(boolean)
decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
int Al = cinfo->Al;
register int s, r;
@@ -730,7 +729,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if (! entropy->insufficient_data) {
/* Load up working state */
- BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(state, entropy->saved);
/* Outer loop handles each block in the MCU */
@@ -759,12 +758,13 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
}
/* Completed MCU, so update state */
- BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(entropy->saved, state);
}
- /* Account for restart interval (no-op if not using restarts) */
- entropy->restarts_to_go--;
+ /* Account for restart interval if using restarts */
+ if (cinfo->restart_interval)
+ entropy->restarts_to_go--;
return TRUE;
}
@@ -777,7 +777,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
METHODDEF(boolean)
decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int s, k, r;
unsigned int EOBRUN;
@@ -809,7 +809,7 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if (EOBRUN) /* if it's a band of zeroes... */
EOBRUN--; /* ...process it now (we do nothing) */
else {
- BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
Se = cinfo->Se;
Al = cinfo->Al;
natural_order = cinfo->natural_order;
@@ -842,15 +842,16 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
}
}
- BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
}
/* Completed MCU, so update state */
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
}
- /* Account for restart interval (no-op if not using restarts) */
- entropy->restarts_to_go--;
+ /* Account for restart interval if using restarts */
+ if (cinfo->restart_interval)
+ entropy->restarts_to_go--;
return TRUE;
}
@@ -864,9 +865,10 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
METHODDEF(boolean)
decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- int p1, blkn;
+ JCOEF p1;
+ int blkn;
BITREAD_STATE_VARS;
/* Process restart marker if needed; may have to suspend */
@@ -881,7 +883,7 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
*/
/* Load up working state */
- BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
@@ -896,10 +898,11 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
}
/* Completed MCU, so update state */
- BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
- /* Account for restart interval (no-op if not using restarts) */
- entropy->restarts_to_go--;
+ /* Account for restart interval if using restarts */
+ if (cinfo->restart_interval)
+ entropy->restarts_to_go--;
return TRUE;
}
@@ -911,11 +914,12 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
METHODDEF(boolean)
decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int s, k, r;
unsigned int EOBRUN;
- int Se, p1, m1;
+ int Se;
+ JCOEF p1, m1;
const int * natural_order;
JBLOCKROW block;
JCOEFPTR thiscoef;
@@ -937,11 +941,11 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
Se = cinfo->Se;
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
- m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
+ m1 = -p1; /* -1 in the bit position being coded */
natural_order = cinfo->natural_order;
/* Load up working state */
- BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
/* There is always only one block per MCU */
@@ -1043,12 +1047,13 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
}
/* Completed MCU, so update state */
- BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
}
- /* Account for restart interval (no-op if not using restarts) */
- entropy->restarts_to_go--;
+ /* Account for restart interval if using restarts */
+ if (cinfo->restart_interval)
+ entropy->restarts_to_go--;
return TRUE;
@@ -1091,7 +1096,7 @@ decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
Se = cinfo->lim_Se;
/* Load up working state */
- BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(state, entropy->saved);
/* Outer loop handles each block in the MCU */
@@ -1178,12 +1183,13 @@ decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
}
/* Completed MCU, so update state */
- BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(entropy->saved, state);
}
- /* Account for restart interval (no-op if not using restarts) */
- entropy->restarts_to_go--;
+ /* Account for restart interval if using restarts */
+ if (cinfo->restart_interval)
+ entropy->restarts_to_go--;
return TRUE;
}
@@ -1215,7 +1221,7 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if (! entropy->insufficient_data) {
/* Load up working state */
- BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
+ BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(state, entropy->saved);
/* Outer loop handles each block in the MCU */
@@ -1302,12 +1308,13 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
}
/* Completed MCU, so update state */
- BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
+ BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(entropy->saved, state);
}
- /* Account for restart interval (no-op if not using restarts) */
- entropy->restarts_to_go--;
+ /* Account for restart interval if using restarts */
+ if (cinfo->restart_interval)
+ entropy->restarts_to_go--;
return TRUE;
}
@@ -1343,11 +1350,11 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
goto bad;
}
if (cinfo->Al > 13) { /* need not check for < 0 */
- /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
- * but the spec doesn't say so, and we try to be liberal about what we
- * accept. Note: large Al values could result in out-of-range DC
- * coefficients during early scans, leading to bizarre displays due to
- * overflows in the IDCT math. But we won't crash.
+ /* Arguably the maximum Al value should be less than 13 for 8-bit
+ * precision, but the spec doesn't say so, and we try to be liberal
+ * about what we accept. Note: large Al values could result in
+ * out-of-range DC coefficients during early scans, leading to bizarre
+ * displays due to overflows in the IDCT math. But we won't crash.
*/
bad:
ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
@@ -1451,7 +1458,8 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
compptr = cinfo->cur_comp_info[ci];
/* Precalculate which table to use for each block */
entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
- entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
+ entropy->ac_cur_tbls[blkn] = /* AC needs no table when not present */
+ cinfo->lim_Se ? entropy->ac_derived_tbls[compptr->ac_tbl_no] : NULL;
/* Decide whether we really care about the coefficient values */
if (compptr->component_needed) {
ci = compptr->DCT_v_scaled_size;
@@ -1494,7 +1502,6 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
if (ci <= 0 || ci > 8) ci = 8;
if (i <= 0 || i > 8) i = 8;
entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1];
- break;
}
} else {
entropy->coef_limit[blkn] = 0;
@@ -1522,9 +1529,8 @@ jinit_huff_decoder (j_decompress_ptr cinfo)
huff_entropy_ptr entropy;
int i;
- entropy = (huff_entropy_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(huff_entropy_decoder));
+ entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_decoder));
cinfo->entropy = &entropy->pub;
entropy->pub.start_pass = start_pass_huff_decoder;
entropy->pub.finish_pass = finish_pass_huff;
@@ -1532,9 +1538,9 @@ jinit_huff_decoder (j_decompress_ptr cinfo)
if (cinfo->progressive_mode) {
/* Create progression status table */
int *coef_bit_ptr, ci;
- cinfo->coef_bits = (int (*)[DCTSIZE2])
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- cinfo->num_components*DCTSIZE2*SIZEOF(int));
+ cinfo->coef_bits = (int (*)[DCTSIZE2]) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components * DCTSIZE2 * SIZEOF(int));
coef_bit_ptr = & cinfo->coef_bits[0][0];
for (ci = 0; ci < cinfo->num_components; ci++)
for (i = 0; i < DCTSIZE2; i++)
@@ -1545,7 +1551,7 @@ jinit_huff_decoder (j_decompress_ptr cinfo)
entropy->derived_tbls[i] = NULL;
}
} else {
- /* Mark tables unallocated */
+ /* Mark derived tables unallocated */
for (i = 0; i < NUM_HUFF_TBLS; i++) {
entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
}