From 59b844347c19c143e965ac767dc928a98c97fb17 Mon Sep 17 00:00:00 2001 From: Matthieu Darbois Date: Mon, 22 Dec 2014 15:50:32 +0000 Subject: [trunk] fixed component precision upscaling in opj_decompress (fixes issue 458) --- src/bin/jp2/convert.c | 52 +++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index d56f3904..7f4593b0 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -102,43 +102,51 @@ void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision) } /* Component precision scaling */ +static void scale_component_up(opj_image_comp_t* component, OPJ_UINT32 precision) +{ + OPJ_SIZE_T i, len; + + len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h; + if (component->sgnd) { + OPJ_INT64 newMax = (1U << (precision - 1)); + OPJ_INT64 oldMax = (1U << (component->prec - 1)); + OPJ_INT32* l_data = component->data; + for (i = 0; i < len; ++i) { + l_data[i] = (OPJ_INT32)(((OPJ_INT64)l_data[i] * newMax) / oldMax); + } + } else { + OPJ_UINT64 newMax = (1U << precision) - 1U; + OPJ_UINT64 oldMax = (1U << component->prec) - 1U; + OPJ_UINT32* l_data = (OPJ_UINT32*)component->data; + for (i = 0; i < len; ++i) { + l_data[i] = (OPJ_UINT32)(((OPJ_UINT64)l_data[i] * newMax) / oldMax); + } + } + component->prec = precision; +} void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision) { int shift; - OPJ_SIZE_T i; - OPJ_SIZE_T len; + OPJ_SIZE_T i, len; if (component->prec == precision) { return; } if (component->prec < precision) { - shift = (int)(precision - component->prec); - } else { - shift = (int)(component->prec - precision); + scale_component_up(component, precision); + return; } + shift = (int)(component->prec - precision); len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h; - if (component->sgnd) { OPJ_INT32* l_data = component->data; - if (component->prec < precision) { - for (i = 0; i < len; ++i) { - l_data[i] <<= shift; - } - } else { - for (i = 0; i < len; ++i) { - l_data[i] >>= shift; - } + for (i = 0; i < len; ++i) { + l_data[i] >>= shift; } } else { OPJ_UINT32* l_data = (OPJ_UINT32*)component->data; - if (component->prec < precision) { - for (i = 0; i < len; ++i) { - l_data[i] <<= shift; - } - } else { - for (i = 0; i < len; ++i) { - l_data[i] >>= shift; - } + for (i = 0; i < len; ++i) { + l_data[i] >>= shift; } } component->prec = precision; -- cgit v1.2.3