summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/libjpeg/jcarith.c
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/libjpeg/jcarith.c')
-rw-r--r--3rdparty/libjpeg/jcarith.c82
1 files changed, 46 insertions, 36 deletions
diff --git a/3rdparty/libjpeg/jcarith.c b/3rdparty/libjpeg/jcarith.c
index 0b7ea55d404..a64190e72e0 100644
--- a/3rdparty/libjpeg/jcarith.c
+++ b/3rdparty/libjpeg/jcarith.c
@@ -1,7 +1,7 @@
/*
* jcarith.c
*
- * Developed 1997-2009 by Guido Vollbeding.
+ * Developed 1997-2013 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.
*
@@ -223,7 +223,7 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
register INT32 qe, temp;
register int sv;
- /* Fetch values from our compact representation of Table D.2:
+ /* Fetch values from our compact representation of Table D.3(D.2):
* Qe values and probability estimation state machine
*/
sv = *st;
@@ -362,7 +362,6 @@ METHODDEF(boolean)
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
- JBLOCKROW block;
unsigned char *st;
int blkn, ci, tbl;
int v, v2, m;
@@ -381,14 +380,13 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
/* Encode the MCU data blocks */
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
- block = MCU_data[blkn];
ci = cinfo->MCU_membership[blkn];
tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
/* Compute the DC value after the required point transform by Al.
* This is simply an arithmetic right shift.
*/
- m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al);
+ m = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
/* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
@@ -453,11 +451,11 @@ METHODDEF(boolean)
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
+ const int * natural_order;
JBLOCKROW block;
unsigned char *st;
int tbl, k, ke;
int v, v2, m;
- const int * natural_order;
/* Emit restart marker if needed */
if (cinfo->restart_interval) {
@@ -479,7 +477,8 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
/* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
/* Establish EOB (end-of-block) index */
- for (ke = cinfo->Se; ke > 0; ke--)
+ ke = cinfo->Se;
+ do {
/* We must apply the point transform by Al. For AC coefficients this
* is an integer division with rounding towards 0. To do this portably
* in C, we shift after obtaining the absolute value.
@@ -490,13 +489,14 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
v = -v;
if (v >>= cinfo->Al) break;
}
+ } while (--ke);
/* Figure F.5: Encode_AC_Coefficients */
- for (k = cinfo->Ss; k <= ke; k++) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
+ for (k = cinfo->Ss - 1; k < ke;) {
+ st = entropy->ac_stats[tbl] + 3 * k;
arith_encode(cinfo, st, 0); /* EOB decision */
for (;;) {
- if ((v = (*block)[natural_order[k]]) >= 0) {
+ if ((v = (*block)[natural_order[++k]]) >= 0) {
if (v >>= cinfo->Al) {
arith_encode(cinfo, st + 1, 1);
arith_encode(cinfo, entropy->fixed_bin, 0);
@@ -510,7 +510,8 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
break;
}
}
- arith_encode(cinfo, st + 1, 0); st += 3; k++;
+ arith_encode(cinfo, st + 1, 0);
+ st += 3;
}
st += 2;
/* Figure F.8: Encoding the magnitude category of v */
@@ -537,9 +538,9 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
while (m >>= 1)
arith_encode(cinfo, st, (m & v) ? 1 : 0);
}
- /* Encode EOB decision only if k <= cinfo->Se */
- if (k <= cinfo->Se) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
+ /* Encode EOB decision only if k < cinfo->Se */
+ if (k < cinfo->Se) {
+ st = entropy->ac_stats[tbl] + 3 * k;
arith_encode(cinfo, st, 1);
}
@@ -549,6 +550,8 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
/*
* MCU encoding for DC successive approximation refinement scan.
+ * Note: we assume such scans can be multi-component,
+ * although the spec is not very clear on the point.
*/
METHODDEF(boolean)
@@ -590,11 +593,11 @@ METHODDEF(boolean)
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
+ const int * natural_order;
JBLOCKROW block;
unsigned char *st;
int tbl, k, ke, kex;
int v;
- const int * natural_order;
/* Emit restart marker if needed */
if (cinfo->restart_interval) {
@@ -616,7 +619,8 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
/* Section G.1.3.3: Encoding of AC coefficients */
/* Establish EOB (end-of-block) index */
- for (ke = cinfo->Se; ke > 0; ke--)
+ ke = cinfo->Se;
+ do {
/* We must apply the point transform by Al. For AC coefficients this
* is an integer division with rounding towards 0. To do this portably
* in C, we shift after obtaining the absolute value.
@@ -627,6 +631,7 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
v = -v;
if (v >>= cinfo->Al) break;
}
+ } while (--ke);
/* Establish EOBx (previous stage end-of-block) index */
for (kex = ke; kex > 0; kex--)
@@ -638,12 +643,12 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
}
/* Figure G.10: Encode_AC_Coefficients_SA */
- for (k = cinfo->Ss; k <= ke; k++) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
- if (k > kex)
+ for (k = cinfo->Ss - 1; k < ke;) {
+ st = entropy->ac_stats[tbl] + 3 * k;
+ if (k >= kex)
arith_encode(cinfo, st, 0); /* EOB decision */
for (;;) {
- if ((v = (*block)[natural_order[k]]) >= 0) {
+ if ((v = (*block)[natural_order[++k]]) >= 0) {
if (v >>= cinfo->Al) {
if (v >> 1) /* previously nonzero coef */
arith_encode(cinfo, st + 2, (v & 1));
@@ -665,12 +670,13 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
break;
}
}
- arith_encode(cinfo, st + 1, 0); st += 3; k++;
+ arith_encode(cinfo, st + 1, 0);
+ st += 3;
}
}
- /* Encode EOB decision only if k <= cinfo->Se */
- if (k <= cinfo->Se) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
+ /* Encode EOB decision only if k < cinfo->Se */
+ if (k < cinfo->Se) {
+ st = entropy->ac_stats[tbl] + 3 * k;
arith_encode(cinfo, st, 1);
}
@@ -686,12 +692,13 @@ METHODDEF(boolean)
encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
- jpeg_component_info * compptr;
+ const int * natural_order;
JBLOCKROW block;
unsigned char *st;
- int blkn, ci, tbl, k, ke;
+ int tbl, k, ke;
int v, v2, m;
- const int * natural_order;
+ int blkn, ci;
+ jpeg_component_info * compptr;
/* Emit restart marker if needed */
if (cinfo->restart_interval) {
@@ -765,18 +772,21 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
/* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
+ if ((ke = cinfo->lim_Se) == 0) continue;
tbl = compptr->ac_tbl_no;
/* Establish EOB (end-of-block) index */
- for (ke = cinfo->lim_Se; ke > 0; ke--)
+ do {
if ((*block)[natural_order[ke]]) break;
+ } while (--ke);
/* Figure F.5: Encode_AC_Coefficients */
- for (k = 1; k <= ke; k++) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
+ for (k = 0; k < ke;) {
+ st = entropy->ac_stats[tbl] + 3 * k;
arith_encode(cinfo, st, 0); /* EOB decision */
- while ((v = (*block)[natural_order[k]]) == 0) {
- arith_encode(cinfo, st + 1, 0); st += 3; k++;
+ while ((v = (*block)[natural_order[++k]]) == 0) {
+ arith_encode(cinfo, st + 1, 0);
+ st += 3;
}
arith_encode(cinfo, st + 1, 1);
/* Figure F.6: Encoding nonzero value v */
@@ -812,9 +822,9 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
while (m >>= 1)
arith_encode(cinfo, st, (m & v) ? 1 : 0);
}
- /* Encode EOB decision only if k <= cinfo->lim_Se */
- if (k <= cinfo->lim_Se) {
- st = entropy->ac_stats[tbl] + 3 * (k - 1);
+ /* Encode EOB decision only if k < cinfo->lim_Se */
+ if (k < cinfo->lim_Se) {
+ st = entropy->ac_stats[tbl] + 3 * k;
arith_encode(cinfo, st, 1);
}
}
@@ -919,7 +929,7 @@ jinit_arith_encoder (j_compress_ptr cinfo)
entropy = (arith_entropy_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(arith_entropy_encoder));
- cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
+ cinfo->entropy = &entropy->pub;
entropy->pub.start_pass = start_pass;
entropy->pub.finish_pass = finish_pass;