From 46440fe368d26bc4d1d93705e2c4d670fb954a9a Mon Sep 17 00:00:00 2001 From: mayeut Date: Sun, 18 Oct 2015 18:10:38 +0200 Subject: [PATCH] Fix some calculations in tcd_malloc_encode and tcd_malloc_decode_tile Fix #388 --- libopenjpeg/int.h | 11 ++++++++++- libopenjpeg/tcd.c | 16 ++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libopenjpeg/int.h b/libopenjpeg/int.h index 147dccfe..0ed5ba40 100644 --- a/libopenjpeg/int.h +++ b/libopenjpeg/int.h @@ -113,8 +113,17 @@ Divide an integer by a power of 2 and round upwards @return Returns a divided by 2^b */ static INLINE int int_ceildivpow2(int a, int b) { - return (int)((a + (int64_t)(1 << b) - 1) >> b); + return (int)((a + ((int64_t)1 << b) - 1) >> b); } + +/** + + Divide a 64bits integer by a power of 2 and round upwards + + @return Returns a divided by 2^b + + */ +static INLINE int int64_ceildivpow2(int64_t a, int b) { + return (int)((a + ((int64_t)1 << b) - 1) >> b); +} + /** Divide an integer by a power of 2 and round downwards @return Returns a divided by 2^b diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index 190d7f99..d185d7d1 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -276,10 +276,10 @@ void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int c band->y1 = int_ceildivpow2(tilec->y1, levelno); } else { /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); + band->x0 = int64_ceildivpow2(tilec->x0 - ((int64_t)x0b << levelno), levelno + 1); + band->y0 = int64_ceildivpow2(tilec->y0 - ((int64_t)y0b << levelno), levelno + 1); + band->x1 = int64_ceildivpow2(tilec->x1 - ((int64_t)x0b << levelno), levelno + 1); + band->y1 = int64_ceildivpow2(tilec->y1 - ((int64_t)y0b << levelno), levelno + 1); } ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; @@ -789,10 +789,10 @@ void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, band->y1 = int_ceildivpow2(tilec->y1, levelno); } else { /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); + band->x0 = int64_ceildivpow2(tilec->x0 - ((int64_t)x0b << levelno), levelno + 1); + band->y0 = int64_ceildivpow2(tilec->y0 - ((int64_t)y0b << levelno), levelno + 1); + band->x1 = int64_ceildivpow2(tilec->x1 - ((int64_t)x0b << levelno), levelno + 1); + band->y1 = int64_ceildivpow2(tilec->y1 - ((int64_t)y0b << levelno), levelno + 1); } ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];