summaryrefslogtreecommitdiff
path: root/jpeg/jchuff.c
diff options
context:
space:
mode:
authorManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2024-02-17 11:46:11 +0100
committerManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2024-02-17 12:20:28 +0100
commit3ecadc0a8e2acda3a13d7df5307137f10d66ef80 (patch)
tree23745953de060c5522c66f2f95e362b541203cd7 /jpeg/jchuff.c
parentbf938da079a2e3535ecd74ba3aa01a5a9b26f3f6 (diff)
Update bundled libjpeg to version 9f
Diffstat (limited to 'jpeg/jchuff.c')
-rw-r--r--jpeg/jchuff.c272
1 files changed, 144 insertions, 128 deletions
diff --git a/jpeg/jchuff.c b/jpeg/jchuff.c
index f3272c9fa..1f527b218 100644
--- a/jpeg/jchuff.c
+++ b/jpeg/jchuff.c
@@ -2,7 +2,7 @@
* jchuff.c
*
* Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2006-2020 by Guido Vollbeding.
+ * Modified 2006-2023 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.
*
@@ -26,17 +26,11 @@
/* The legal range of a DCT coefficient is
- * -1024 .. +1023 for 8-bit data;
- * -16384 .. +16383 for 12-bit data.
- * Hence the magnitude should always fit in 10 or 14 bits respectively.
+ * -1024 .. +1023 for 8-bit sample data precision;
+ * -16384 .. +16383 for 12-bit sample data precision.
+ * Hence the magnitude should always fit in sample data precision + 2 bits.
*/
-#if BITS_IN_JSAMPLE == 8
-#define MAX_COEF_BITS 10
-#else
-#define MAX_COEF_BITS 14
-#endif
-
/* Derived data constructed for each Huffman table */
typedef struct {
@@ -547,6 +541,7 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int temp, temp2;
register int nbits;
+ int max_coef_bits;
int blkn, ci, tbl;
ISHIFT_TEMPS
@@ -558,6 +553,9 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
if (entropy->restarts_to_go == 0)
emit_restart_e(entropy, entropy->next_restart_num);
+ /* Since we're encoding a difference, the range limit is twice as much. */
+ max_coef_bits = cinfo->data_precision + 3;
+
/* Encode the MCU data blocks */
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
ci = cinfo->MCU_membership[blkn];
@@ -569,12 +567,17 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
temp = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
/* DC differences are figured on the point-transformed values. */
- temp2 = temp - entropy->saved.last_dc_val[ci];
+ if ((temp2 = temp - entropy->saved.last_dc_val[ci]) == 0) {
+ /* Count/emit the Huffman-coded symbol for the number of bits */
+ emit_dc_symbol(entropy, tbl, 0);
+
+ continue;
+ }
+
entropy->saved.last_dc_val[ci] = temp;
/* Encode the DC coefficient difference per section G.1.2.1 */
- temp = temp2;
- if (temp < 0) {
+ if ((temp = temp2) < 0) {
temp = -temp; /* temp is abs value of input */
/* For a negative input, want temp2 = bitwise complement of abs(input) */
/* This code assumes we are on a two's complement machine */
@@ -583,14 +586,10 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
/* Find the number of bits needed for the magnitude of the coefficient */
nbits = 0;
- while (temp) {
- nbits++;
- temp >>= 1;
- }
- /* Check for out-of-range coefficient values.
- * Since we're encoding a difference, the range limit is twice as much.
- */
- if (nbits > MAX_COEF_BITS+1)
+ do nbits++; /* there must be at least one 1 bit */
+ while ((temp >>= 1));
+ /* Check for out-of-range coefficient values */
+ if (nbits > max_coef_bits)
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
/* Count/emit the Huffman-coded symbol for the number of bits */
@@ -598,8 +597,7 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
/* Emit that number of bits of the value, if positive, */
/* or the complement of its magnitude, if negative. */
- if (nbits) /* emit_bits rejects calls with size 0 */
- emit_bits_e(entropy, (unsigned int) temp2, nbits);
+ emit_bits_e(entropy, (unsigned int) temp2, nbits);
}
cinfo->dest->next_output_byte = entropy->next_output_byte;
@@ -633,7 +631,7 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
register int temp, temp2;
register int nbits;
register int r, k;
- int Se, Al;
+ int Se, Al, max_coef_bits;
entropy->next_output_byte = cinfo->dest->next_output_byte;
entropy->free_in_buffer = cinfo->dest->free_in_buffer;
@@ -646,6 +644,7 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
Se = cinfo->Se;
Al = cinfo->Al;
natural_order = cinfo->natural_order;
+ max_coef_bits = cinfo->data_precision + 2;
/* Encode the MCU data block */
block = MCU_data[0];
@@ -666,18 +665,23 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
*/
if (temp < 0) {
temp = -temp; /* temp is abs value of input */
- temp >>= Al; /* apply the point transform */
+ /* Apply the point transform, and watch out for case */
+ /* that nonzero coef is zero after point transform. */
+ if ((temp >>= Al) == 0) {
+ r++;
+ continue;
+ }
/* For a negative coef, want temp2 = bitwise complement of abs(coef) */
temp2 = ~temp;
} else {
- temp >>= Al; /* apply the point transform */
+ /* Apply the point transform, and watch out for case */
+ /* that nonzero coef is zero after point transform. */
+ if ((temp >>= Al) == 0) {
+ r++;
+ continue;
+ }
temp2 = temp;
}
- /* Watch out for case that nonzero coef is zero after point transform */
- if (temp == 0) {
- r++;
- continue;
- }
/* Emit any pending EOBRUN */
if (entropy->EOBRUN > 0)
@@ -689,11 +693,11 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
}
/* Find the number of bits needed for the magnitude of the coefficient */
- nbits = 1; /* there must be at least one 1 bit */
- while ((temp >>= 1))
- nbits++;
+ nbits = 0;
+ do nbits++; /* there must be at least one 1 bit */
+ while ((temp >>= 1));
/* Check for out-of-range coefficient values */
- if (nbits > MAX_COEF_BITS)
+ if (nbits > max_coef_bits)
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
/* Count/emit Huffman symbol for run length / number of bits */
@@ -916,83 +920,89 @@ encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
register int nbits;
register int r, k;
int Se = state->cinfo->lim_Se;
+ int max_coef_bits = state->cinfo->data_precision + 3;
const int * natural_order = state->cinfo->natural_order;
/* Encode the DC coefficient difference per section F.1.2.1 */
- temp = temp2 = block[0] - last_dc_val;
-
- if (temp < 0) {
- temp = -temp; /* temp is abs value of input */
- /* For a negative input, want temp2 = bitwise complement of abs(input) */
- /* This code assumes we are on a two's complement machine */
- temp2--;
- }
+ if ((temp = block[0] - last_dc_val) == 0) {
+ /* Emit the Huffman-coded symbol for the number of bits */
+ if (! emit_bits_s(state, dctbl->ehufco[0], dctbl->ehufsi[0]))
+ return FALSE;
+ } else {
+ if ((temp2 = temp) < 0) {
+ temp = -temp; /* temp is abs value of input */
+ /* For a negative input, want temp2 = bitwise complement of abs(input) */
+ /* This code assumes we are on a two's complement machine */
+ temp2--;
+ }
- /* Find the number of bits needed for the magnitude of the coefficient */
- nbits = 0;
- while (temp) {
- nbits++;
- temp >>= 1;
- }
- /* Check for out-of-range coefficient values.
- * Since we're encoding a difference, the range limit is twice as much.
- */
- if (nbits > MAX_COEF_BITS+1)
- ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 0;
+ do nbits++; /* there must be at least one 1 bit */
+ while ((temp >>= 1));
+ /* Check for out-of-range coefficient values.
+ * Since we're encoding a difference, the range limit is twice as much.
+ */
+ if (nbits > max_coef_bits)
+ ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
- /* Emit the Huffman-coded symbol for the number of bits */
- if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
- return FALSE;
+ /* Emit the Huffman-coded symbol for the number of bits */
+ if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
+ return FALSE;
- /* Emit that number of bits of the value, if positive, */
- /* or the complement of its magnitude, if negative. */
- if (nbits) /* emit_bits rejects calls with size 0 */
+ /* Emit that number of bits of the value, if positive, */
+ /* or the complement of its magnitude, if negative. */
if (! emit_bits_s(state, (unsigned int) temp2, nbits))
return FALSE;
+ }
/* Encode the AC coefficients per section F.1.2.2 */
r = 0; /* r = run length of zeros */
for (k = 1; k <= Se; k++) {
- if ((temp2 = block[natural_order[k]]) == 0) {
+ if ((temp = block[natural_order[k]]) == 0) {
r++;
- } else {
- /* if run length > 15, must emit special run-length-16 codes (0xF0) */
- while (r > 15) {
- if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
- return FALSE;
- r -= 16;
- }
-
- temp = temp2;
- if (temp < 0) {
- temp = -temp; /* temp is abs value of input */
- /* This code assumes we are on a two's complement machine */
- temp2--;
- }
-
- /* Find the number of bits needed for the magnitude of the coefficient */
- nbits = 1; /* there must be at least one 1 bit */
- while ((temp >>= 1))
- nbits++;
- /* Check for out-of-range coefficient values */
- if (nbits > MAX_COEF_BITS)
- ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
-
- /* Emit Huffman symbol for run length / number of bits */
- temp = (r << 4) + nbits;
- if (! emit_bits_s(state, actbl->ehufco[temp], actbl->ehufsi[temp]))
- return FALSE;
+ continue;
+ }
- /* Emit that number of bits of the value, if positive, */
- /* or the complement of its magnitude, if negative. */
- if (! emit_bits_s(state, (unsigned int) temp2, nbits))
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
return FALSE;
+ r -= 16;
+ }
- r = 0;
+ if ((temp2 = temp) < 0) {
+ temp = -temp; /* temp is abs value of input */
+ /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
+ /* This code assumes we are on a two's complement machine */
+ temp2--;
}
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 0;
+ do nbits++; /* there must be at least one 1 bit */
+ while ((temp >>= 1));
+ /* Check for out-of-range coefficient values.
+ * Use ">=" instead of ">" so can use the
+ * same one larger limit from DC check here.
+ */
+ if (nbits >= max_coef_bits)
+ ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
+
+ /* Emit Huffman symbol for run length / number of bits */
+ temp = (r << 4) + nbits;
+ if (! emit_bits_s(state, actbl->ehufco[temp], actbl->ehufsi[temp]))
+ return FALSE;
+
+ /* Emit that number of bits of the value, if positive, */
+ /* or the complement of its magnitude, if negative. */
+ if (! emit_bits_s(state, (unsigned int) temp2, nbits))
+ return FALSE;
+
+ r = 0; /* reset zero run length */
}
/* If the last coef(s) were zero, emit an end-of-block code */
@@ -1122,28 +1132,31 @@ htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
register int nbits;
register int r, k;
int Se = cinfo->lim_Se;
+ int max_coef_bits = cinfo->data_precision + 3;
const int * natural_order = cinfo->natural_order;
/* Encode the DC coefficient difference per section F.1.2.1 */
- temp = block[0] - last_dc_val;
- if (temp < 0)
- temp = -temp;
+ if ((temp = block[0] - last_dc_val) == 0) {
+ /* Count the Huffman symbol for the number of bits */
+ dc_counts[0]++;
+ } else {
+ if (temp < 0)
+ temp = -temp; /* temp is abs value of input */
- /* Find the number of bits needed for the magnitude of the coefficient */
- nbits = 0;
- while (temp) {
- nbits++;
- temp >>= 1;
- }
- /* Check for out-of-range coefficient values.
- * Since we're encoding a difference, the range limit is twice as much.
- */
- if (nbits > MAX_COEF_BITS+1)
- ERREXIT(cinfo, JERR_BAD_DCT_COEF);
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 0;
+ do nbits++; /* there must be at least one 1 bit */
+ while ((temp >>= 1));
+ /* Check for out-of-range coefficient values.
+ * Since we're encoding a difference, the range limit is twice as much.
+ */
+ if (nbits > max_coef_bits)
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
- /* Count the Huffman symbol for the number of bits */
- dc_counts[nbits]++;
+ /* Count the Huffman symbol for the number of bits */
+ dc_counts[nbits]++;
+ }
/* Encode the AC coefficients per section F.1.2.2 */
@@ -1152,30 +1165,33 @@ htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
for (k = 1; k <= Se; k++) {
if ((temp = block[natural_order[k]]) == 0) {
r++;
- } else {
- /* if run length > 15, must emit special run-length-16 codes (0xF0) */
- while (r > 15) {
- ac_counts[0xF0]++;
- r -= 16;
- }
+ continue;
+ }
+
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ ac_counts[0xF0]++;
+ r -= 16;
+ }
- /* Find the number of bits needed for the magnitude of the coefficient */
- if (temp < 0)
- temp = -temp;
+ if (temp < 0)
+ temp = -temp; /* temp is abs value of input */
- /* Find the number of bits needed for the magnitude of the coefficient */
- nbits = 1; /* there must be at least one 1 bit */
- while ((temp >>= 1))
- nbits++;
- /* Check for out-of-range coefficient values */
- if (nbits > MAX_COEF_BITS)
- ERREXIT(cinfo, JERR_BAD_DCT_COEF);
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ nbits = 0;
+ do nbits++; /* there must be at least one 1 bit */
+ while ((temp >>= 1));
+ /* Check for out-of-range coefficient values.
+ * Use ">=" instead of ">" so can use the
+ * same one larger limit from DC check here.
+ */
+ if (nbits >= max_coef_bits)
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
- /* Count Huffman symbol for run length / number of bits */
- ac_counts[(r << 4) + nbits]++;
+ /* Count Huffman symbol for run length / number of bits */
+ ac_counts[(r << 4) + nbits]++;
- r = 0;
- }
+ r = 0; /* reset zero run length */
}
/* If the last coef(s) were zero, emit an end-of-block code */