diff --git a/src/lib/openjp2/opj_intmath.h b/src/lib/openjp2/opj_intmath.h
index d7d85e52..ad135976 100644
--- a/src/lib/openjp2/opj_intmath.h
+++ b/src/lib/openjp2/opj_intmath.h
@@ -124,6 +124,28 @@ static INLINE OPJ_INT32 opj_int_clamp(OPJ_INT32 a, OPJ_INT32 min,
}
return a;
}
+
+/**
+Clamp an integer inside an interval
+@return
+
+- Returns a if (min < a < max)
+
- Returns max if (a > max)
+
- Returns min if (a < min)
+
+*/
+static INLINE OPJ_INT64 opj_int64_clamp(OPJ_INT64 a, OPJ_INT64 min,
+ OPJ_INT64 max)
+{
+ if (a < min) {
+ return min;
+ }
+ if (a > max) {
+ return max;
+ }
+ return a;
+}
+
/**
@return Get absolute value of integer
*/
diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c
index 3bcab981..26c9f2a4 100644
--- a/src/lib/openjp2/tcd.c
+++ b/src/lib/openjp2/tcd.c
@@ -2112,6 +2112,7 @@ static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
if (l_tccp->qmfbid == 1) {
for (j = 0; j < l_height; ++j) {
for (i = 0; i < l_width; ++i) {
+ /* TODO: do addition on int64 ? */
*l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
l_max);
++l_current_ptr;
@@ -2122,13 +2123,14 @@ static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
for (j = 0; j < l_height; ++j) {
for (i = 0; i < l_width; ++i) {
OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
- OPJ_INT32 l_value_int = (OPJ_INT32)opj_lrintf(l_value);
- if (l_value > INT_MAX ||
- (l_value_int > 0 && l_tccp->m_dc_level_shift > 0 &&
- l_value_int > INT_MAX - l_tccp->m_dc_level_shift)) {
+ if (l_value > INT_MAX) {
*l_current_ptr = l_max;
+ } else if (l_value < INT_MIN) {
+ *l_current_ptr = l_min;
} else {
- *l_current_ptr = opj_int_clamp(
+ /* Do addition on int64 to avoid overflows */
+ OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
+ *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
}
++l_current_ptr;