diff options
Diffstat (limited to 'src/bin/jp2')
| -rw-r--r-- | src/bin/jp2/convert.c | 1807 | ||||
| -rw-r--r-- | src/bin/jp2/convert.h | 18 | ||||
| -rw-r--r-- | src/bin/jp2/convertbmp.c | 1370 | ||||
| -rw-r--r-- | src/bin/jp2/convertpng.c | 823 | ||||
| -rw-r--r-- | src/bin/jp2/converttif.c | 1077 | ||||
| -rw-r--r-- | src/bin/jp2/index.c | 699 | ||||
| -rw-r--r-- | src/bin/jp2/index.h | 6 | ||||
| -rw-r--r-- | src/bin/jp2/opj_compress.c | 431 | ||||
| -rw-r--r-- | src/bin/jp2/opj_decompress.c | 2585 | ||||
| -rw-r--r-- | src/bin/jp2/opj_dump.c | 851 | ||||
| -rw-r--r-- | src/bin/jp2/windirent.h | 389 |
11 files changed, 4940 insertions, 5116 deletions
diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index 13713204..d2e799f5 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -8,7 +8,7 @@ * Copyright (c) 2002-2014, Professor Benoit Macq * Copyright (c) 2001-2003, David Janssens * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2007, Francois-Olivier Devaux * Copyright (c) 2003-2014, Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan @@ -50,7 +50,8 @@ * * log2(a) */ -static int int_floorlog2(int a) { +static int int_floorlog2(int a) +{ int l; for (l = 0; a > 1; l++) { a >>= 1; @@ -61,88 +62,88 @@ static int int_floorlog2(int a) { /* Component precision scaling */ void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision) { - OPJ_SIZE_T i; - OPJ_SIZE_T len; - OPJ_UINT32 umax = (OPJ_UINT32)((OPJ_INT32)-1); - - len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h; - if (precision < 32) { - umax = (1U << precision) - 1U; - } - - if (component->sgnd) { - OPJ_INT32* l_data = component->data; - OPJ_INT32 max = (OPJ_INT32)(umax / 2U); - OPJ_INT32 min = -max - 1; - for (i = 0; i < len; ++i) { - if (l_data[i] > max) { - l_data[i] = max; - } else if (l_data[i] < min) { - l_data[i] = min; - } - } - } else { - OPJ_UINT32* l_data = (OPJ_UINT32*)component->data; - for (i = 0; i < len; ++i) { - if (l_data[i] > umax) { - l_data[i] = umax; - } - } - } - component->prec = precision; + OPJ_SIZE_T i; + OPJ_SIZE_T len; + OPJ_UINT32 umax = (OPJ_UINT32)((OPJ_INT32)-1); + + len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h; + if (precision < 32) { + umax = (1U << precision) - 1U; + } + + if (component->sgnd) { + OPJ_INT32* l_data = component->data; + OPJ_INT32 max = (OPJ_INT32)(umax / 2U); + OPJ_INT32 min = -max - 1; + for (i = 0; i < len; ++i) { + if (l_data[i] > max) { + l_data[i] = max; + } else if (l_data[i] < min) { + l_data[i] = min; + } + } + } else { + OPJ_UINT32* l_data = (OPJ_UINT32*)component->data; + for (i = 0; i < len; ++i) { + if (l_data[i] > umax) { + l_data[i] = umax; + } + } + } + component->prec = 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 = (OPJ_INT64)(1U << (precision - 1)); - OPJ_INT64 oldMax = (OPJ_INT64)(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 = (OPJ_UINT64)((1U << precision) - 1U); - OPJ_UINT64 oldMax = (OPJ_UINT64)((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; - component->bpp = precision; + OPJ_SIZE_T i, len; + + len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h; + if (component->sgnd) { + OPJ_INT64 newMax = (OPJ_INT64)(1U << (precision - 1)); + OPJ_INT64 oldMax = (OPJ_INT64)(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 = (OPJ_UINT64)((1U << precision) - 1U); + OPJ_UINT64 oldMax = (OPJ_UINT64)((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; + component->bpp = precision; } void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision) { - int shift; - OPJ_SIZE_T i, len; - - if (component->prec == precision) { - return; - } - if (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; - for (i = 0; i < len; ++i) { - l_data[i] >>= shift; - } - } else { - OPJ_UINT32* l_data = (OPJ_UINT32*)component->data; - for (i = 0; i < len; ++i) { - l_data[i] >>= shift; - } - } - component->bpp = precision; - component->prec = precision; + int shift; + OPJ_SIZE_T i, len; + + if (component->prec == precision) { + return; + } + if (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; + for (i = 0; i < len; ++i) { + l_data[i] >>= shift; + } + } else { + OPJ_UINT32* l_data = (OPJ_UINT32*)component->data; + for (i = 0; i < len; ++i) { + l_data[i] >>= shift; + } + } + component->bpp = precision; + component->prec = precision; } @@ -150,381 +151,381 @@ void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision) /* used by PNG/TIFF */ static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length) { - memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32)); + memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32)); } static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - OPJ_INT32* pDst0 = pDst[0]; - OPJ_INT32* pDst1 = pDst[1]; - - for (i = 0; i < length; i++) { - pDst0[i] = pSrc[2*i+0]; - pDst1[i] = pSrc[2*i+1]; - } + OPJ_SIZE_T i; + OPJ_INT32* pDst0 = pDst[0]; + OPJ_INT32* pDst1 = pDst[1]; + + for (i = 0; i < length; i++) { + pDst0[i] = pSrc[2*i+0]; + pDst1[i] = pSrc[2*i+1]; + } } static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - OPJ_INT32* pDst0 = pDst[0]; - OPJ_INT32* pDst1 = pDst[1]; - OPJ_INT32* pDst2 = pDst[2]; - - for (i = 0; i < length; i++) { - pDst0[i] = pSrc[3*i+0]; - pDst1[i] = pSrc[3*i+1]; - pDst2[i] = pSrc[3*i+2]; - } + OPJ_SIZE_T i; + OPJ_INT32* pDst0 = pDst[0]; + OPJ_INT32* pDst1 = pDst[1]; + OPJ_INT32* pDst2 = pDst[2]; + + for (i = 0; i < length; i++) { + pDst0[i] = pSrc[3*i+0]; + pDst1[i] = pSrc[3*i+1]; + pDst2[i] = pSrc[3*i+2]; + } } static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - OPJ_INT32* pDst0 = pDst[0]; - OPJ_INT32* pDst1 = pDst[1]; - OPJ_INT32* pDst2 = pDst[2]; - OPJ_INT32* pDst3 = pDst[3]; - - for (i = 0; i < length; i++) { - pDst0[i] = pSrc[4*i+0]; - pDst1[i] = pSrc[4*i+1]; - pDst2[i] = pSrc[4*i+2]; - pDst3[i] = pSrc[4*i+3]; - } + OPJ_SIZE_T i; + OPJ_INT32* pDst0 = pDst[0]; + OPJ_INT32* pDst1 = pDst[1]; + OPJ_INT32* pDst2 = pDst[2]; + OPJ_INT32* pDst3 = pDst[3]; + + for (i = 0; i < length; i++) { + pDst0[i] = pSrc[4*i+0]; + pDst1[i] = pSrc[4*i+1]; + pDst2[i] = pSrc[4*i+2]; + pDst3[i] = pSrc[4*i+3]; + } } const convert_32s_CXPX convert_32s_CXPX_LUT[5] = { - NULL, - convert_32s_C1P1, - convert_32s_C2P2, - convert_32s_C3P3, - convert_32s_C4P4 + NULL, + convert_32s_C1P1, + convert_32s_C2P2, + convert_32s_C3P3, + convert_32s_C4P4 }; static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust) { - OPJ_SIZE_T i; - const OPJ_INT32* pSrc0 = pSrc[0]; - - for (i = 0; i < length; i++) { - pDst[i] = pSrc0[i] + adjust; - } + OPJ_SIZE_T i; + const OPJ_INT32* pSrc0 = pSrc[0]; + + for (i = 0; i < length; i++) { + pDst[i] = pSrc0[i] + adjust; + } } static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust) { - OPJ_SIZE_T i; - const OPJ_INT32* pSrc0 = pSrc[0]; - const OPJ_INT32* pSrc1 = pSrc[1]; - - for (i = 0; i < length; i++) { - pDst[2*i+0] = pSrc0[i] + adjust; - pDst[2*i+1] = pSrc1[i] + adjust; - } + OPJ_SIZE_T i; + const OPJ_INT32* pSrc0 = pSrc[0]; + const OPJ_INT32* pSrc1 = pSrc[1]; + + for (i = 0; i < length; i++) { + pDst[2*i+0] = pSrc0[i] + adjust; + pDst[2*i+1] = pSrc1[i] + adjust; + } } static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust) { - OPJ_SIZE_T i; - const OPJ_INT32* pSrc0 = pSrc[0]; - const OPJ_INT32* pSrc1 = pSrc[1]; - const OPJ_INT32* pSrc2 = pSrc[2]; - - for (i = 0; i < length; i++) { - pDst[3*i+0] = pSrc0[i] + adjust; - pDst[3*i+1] = pSrc1[i] + adjust; - pDst[3*i+2] = pSrc2[i] + adjust; - } + OPJ_SIZE_T i; + const OPJ_INT32* pSrc0 = pSrc[0]; + const OPJ_INT32* pSrc1 = pSrc[1]; + const OPJ_INT32* pSrc2 = pSrc[2]; + + for (i = 0; i < length; i++) { + pDst[3*i+0] = pSrc0[i] + adjust; + pDst[3*i+1] = pSrc1[i] + adjust; + pDst[3*i+2] = pSrc2[i] + adjust; + } } static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust) { - OPJ_SIZE_T i; - const OPJ_INT32* pSrc0 = pSrc[0]; - const OPJ_INT32* pSrc1 = pSrc[1]; - const OPJ_INT32* pSrc2 = pSrc[2]; - const OPJ_INT32* pSrc3 = pSrc[3]; - - for (i = 0; i < length; i++) { - pDst[4*i+0] = pSrc0[i] + adjust; - pDst[4*i+1] = pSrc1[i] + adjust; - pDst[4*i+2] = pSrc2[i] + adjust; - pDst[4*i+3] = pSrc3[i] + adjust; - } + OPJ_SIZE_T i; + const OPJ_INT32* pSrc0 = pSrc[0]; + const OPJ_INT32* pSrc1 = pSrc[1]; + const OPJ_INT32* pSrc2 = pSrc[2]; + const OPJ_INT32* pSrc3 = pSrc[3]; + + for (i = 0; i < length; i++) { + pDst[4*i+0] = pSrc0[i] + adjust; + pDst[4*i+1] = pSrc1[i] + adjust; + pDst[4*i+2] = pSrc2[i] + adjust; + pDst[4*i+3] = pSrc3[i] + adjust; + } } const convert_32s_PXCX convert_32s_PXCX_LUT[5] = { - NULL, - convert_32s_P1C1, - convert_32s_P2C2, - convert_32s_P3C3, - convert_32s_P4C4 + NULL, + convert_32s_P1C1, + convert_32s_P2C2, + convert_32s_P3C3, + convert_32s_P4C4 }; /* bit depth conversions */ /* used by PNG/TIFF up to 8bpp */ static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) { - OPJ_UINT32 val = *pSrc++; - pDst[i+0] = (OPJ_INT32)( val >> 7); - pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U); - pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U); - pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U); - pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U); - pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U); - pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U); - pDst[i+7] = (OPJ_INT32)(val & 0x1U); - } - if (length & 7U) { - OPJ_UINT32 val = *pSrc++; - length = length & 7U; - pDst[i+0] = (OPJ_INT32)(val >> 7); - - if (length > 1U) { - pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U); - if (length > 2U) { - pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U); - if (length > 3U) { - pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U); - if (length > 4U) { - pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U); - if (length > 5U) { - pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U); - if (length > 6U) { - pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U); - } - } - } - } - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) { + OPJ_UINT32 val = *pSrc++; + pDst[i+0] = (OPJ_INT32)( val >> 7); + pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U); + pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U); + pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U); + pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U); + pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U); + pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U); + pDst[i+7] = (OPJ_INT32)(val & 0x1U); + } + if (length & 7U) { + OPJ_UINT32 val = *pSrc++; + length = length & 7U; + pDst[i+0] = (OPJ_INT32)(val >> 7); + + if (length > 1U) { + pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U); + if (length > 2U) { + pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U); + if (length > 3U) { + pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U); + if (length > 4U) { + pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U); + if (length > 5U) { + pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U); + if (length > 6U) { + pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U); + } + } + } + } + } + } + } } static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 val = *pSrc++; - pDst[i+0] = (OPJ_INT32)( val >> 6); - pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U); - pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U); - pDst[i+3] = (OPJ_INT32)(val & 0x3U); - } - if (length & 3U) { - OPJ_UINT32 val = *pSrc++; - length = length & 3U; - pDst[i+0] = (OPJ_INT32)(val >> 6); - - if (length > 1U) { - pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U); - if (length > 2U) { - pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U); - - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 val = *pSrc++; + pDst[i+0] = (OPJ_INT32)( val >> 6); + pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U); + pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U); + pDst[i+3] = (OPJ_INT32)(val & 0x3U); + } + if (length & 3U) { + OPJ_UINT32 val = *pSrc++; + length = length & 3U; + pDst[i+0] = (OPJ_INT32)(val >> 6); + + if (length > 1U) { + pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U); + if (length > 2U) { + pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U); + + } + } + } } static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { - OPJ_UINT32 val = *pSrc++; - pDst[i+0] = (OPJ_INT32)(val >> 4); - pDst[i+1] = (OPJ_INT32)(val & 0xFU); - } - if (length & 1U) { - OPJ_UINT8 val = *pSrc++; - pDst[i+0] = (OPJ_INT32)(val >> 4); - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { + OPJ_UINT32 val = *pSrc++; + pDst[i+0] = (OPJ_INT32)(val >> 4); + pDst[i+1] = (OPJ_INT32)(val & 0xFU); + } + if (length & 1U) { + OPJ_UINT8 val = *pSrc++; + pDst[i+0] = (OPJ_INT32)(val >> 4); + } } static void convert_6u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 val0 = *pSrc++; - OPJ_UINT32 val1 = *pSrc++; - OPJ_UINT32 val2 = *pSrc++; - pDst[i+0] = (OPJ_INT32)(val0 >> 2); - pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4)); - pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6)); - pDst[i+3] = (OPJ_INT32)(val2 & 0x3FU); - - } - if (length & 3U) { - OPJ_UINT32 val0 = *pSrc++; - length = length & 3U; - pDst[i+0] = (OPJ_INT32)(val0 >> 2); - - if (length > 1U) { - OPJ_UINT32 val1 = *pSrc++; - pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4)); - if (length > 2U) { - OPJ_UINT32 val2 = *pSrc++; - pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6)); - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 val0 = *pSrc++; + OPJ_UINT32 val1 = *pSrc++; + OPJ_UINT32 val2 = *pSrc++; + pDst[i+0] = (OPJ_INT32)(val0 >> 2); + pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4)); + pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6)); + pDst[i+3] = (OPJ_INT32)(val2 & 0x3FU); + + } + if (length & 3U) { + OPJ_UINT32 val0 = *pSrc++; + length = length & 3U; + pDst[i+0] = (OPJ_INT32)(val0 >> 2); + + if (length > 1U) { + OPJ_UINT32 val1 = *pSrc++; + pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4)); + if (length > 2U) { + OPJ_UINT32 val2 = *pSrc++; + pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6)); + } + } + } } static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < length; i++) { - pDst[i] = pSrc[i]; - } + OPJ_SIZE_T i; + for (i = 0; i < length; i++) { + pDst[i] = pSrc[i]; + } } const convert_XXx32s_C1R convert_XXu32s_C1R_LUT[9] = { - NULL, - convert_1u32s_C1R, - convert_2u32s_C1R, - NULL, - convert_4u32s_C1R, - NULL, - convert_6u32s_C1R, - NULL, - convert_8u32s_C1R + NULL, + convert_1u32s_C1R, + convert_2u32s_C1R, + NULL, + convert_4u32s_C1R, + NULL, + convert_6u32s_C1R, + NULL, + convert_8u32s_C1R }; static void convert_32s1u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; - OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; - OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; - OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4]; - OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5]; - OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6]; - OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7]; - - *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7); - } - - if (length & 7U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = 0U; - OPJ_UINT32 src2 = 0U; - OPJ_UINT32 src3 = 0U; - OPJ_UINT32 src4 = 0U; - OPJ_UINT32 src5 = 0U; - OPJ_UINT32 src6 = 0U; - length = length & 7U; - - if (length > 1U) { - src1 = (OPJ_UINT32)pSrc[i+1]; - if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i+2]; - if (length > 3U) { - src3 = (OPJ_UINT32)pSrc[i+3]; - if (length > 4U) { - src4 = (OPJ_UINT32)pSrc[i+4]; - if (length > 5U) { - src5 = (OPJ_UINT32)pSrc[i+5]; - if (length > 6U) { - src6 = (OPJ_UINT32)pSrc[i+6]; - } - } - } - } - } - } - *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1)); - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; + OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; + OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; + OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4]; + OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5]; + OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6]; + OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7]; + + *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7); + } + + if (length & 7U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = 0U; + OPJ_UINT32 src2 = 0U; + OPJ_UINT32 src3 = 0U; + OPJ_UINT32 src4 = 0U; + OPJ_UINT32 src5 = 0U; + OPJ_UINT32 src6 = 0U; + length = length & 7U; + + if (length > 1U) { + src1 = (OPJ_UINT32)pSrc[i+1]; + if (length > 2U) { + src2 = (OPJ_UINT32)pSrc[i+2]; + if (length > 3U) { + src3 = (OPJ_UINT32)pSrc[i+3]; + if (length > 4U) { + src4 = (OPJ_UINT32)pSrc[i+4]; + if (length > 5U) { + src5 = (OPJ_UINT32)pSrc[i+5]; + if (length > 6U) { + src6 = (OPJ_UINT32)pSrc[i+6]; + } + } + } + } + } + } + *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1)); + } } static void convert_32s2u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; - OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; - OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; - - *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3); - } - - if (length & 3U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = 0U; - OPJ_UINT32 src2 = 0U; - length = length & 3U; - - if (length > 1U) { - src1 = (OPJ_UINT32)pSrc[i+1]; - if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i+2]; - } - } - *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2)); - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; + OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; + OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; + + *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3); + } + + if (length & 3U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = 0U; + OPJ_UINT32 src2 = 0U; + length = length & 3U; + + if (length > 1U) { + src1 = (OPJ_UINT32)pSrc[i+1]; + if (length > 2U) { + src2 = (OPJ_UINT32)pSrc[i+2]; + } + } + *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2)); + } } static void convert_32s4u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; - - *pDst++ = (OPJ_BYTE)((src0 << 4) | src1); - } - - if (length & 1U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - *pDst++ = (OPJ_BYTE)((src0 << 4)); - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; + + *pDst++ = (OPJ_BYTE)((src0 << 4) | src1); + } + + if (length & 1U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + *pDst++ = (OPJ_BYTE)((src0 << 4)); + } } static void convert_32s6u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; - OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; - OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; - - *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4)); - *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2)); - *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3); - } - - if (length & 3U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = 0U; - OPJ_UINT32 src2 = 0U; - length = length & 3U; - - if (length > 1U) { - src1 = (OPJ_UINT32)pSrc[i+1]; - if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i+2]; - } - } - *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4)); - if (length > 1U) { - *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2)); - if (length > 2U) { - *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6)); - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; + OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; + OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; + + *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4)); + *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2)); + *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3); + } + + if (length & 3U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = 0U; + OPJ_UINT32 src2 = 0U; + length = length & 3U; + + if (length > 1U) { + src1 = (OPJ_UINT32)pSrc[i+1]; + if (length > 2U) { + src2 = (OPJ_UINT32)pSrc[i+2]; + } + } + *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4)); + if (length > 1U) { + *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2)); + if (length > 2U) { + *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6)); + } + } + } } static void convert_32s8u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < length; ++i) { - pDst[i] = (OPJ_BYTE)pSrc[i]; - } + OPJ_SIZE_T i; + for (i = 0; i < length; ++i) { + pDst[i] = (OPJ_BYTE)pSrc[i]; + } } const convert_32sXXx_C1R convert_32sXXu_C1R_LUT[9] = { - NULL, - convert_32s1u_C1R, - convert_32s2u_C1R, - NULL, - convert_32s4u_C1R, - NULL, - convert_32s6u_C1R, - NULL, - convert_32s8u_C1R + NULL, + convert_32s1u_C1R, + convert_32s2u_C1R, + NULL, + convert_32s4u_C1R, + NULL, + convert_32s6u_C1R, + NULL, + convert_32s8u_C1R }; /* -->> -->> -->> -->> @@ -535,8 +536,7 @@ const convert_32sXXx_C1R convert_32sXXu_C1R_LUT[9] = { #ifdef INFORMATION_ONLY /* TGA header definition. */ -struct tga_header -{ +struct tga_header { unsigned char id_length; /* Image id field length */ unsigned char colour_map_type; /* Colour map type */ unsigned char image_type; /* Image type */ @@ -558,7 +558,8 @@ struct tga_header }; #endif /* INFORMATION_ONLY */ -static unsigned short get_ushort(const unsigned char *data) { +static unsigned short get_ushort(const unsigned char *data) +{ unsigned short val = *(const unsigned short *)data; #ifdef OPJ_BIG_ENDIAN val = ((val & 0xffU) << 8) | (val >> 8); @@ -568,7 +569,7 @@ static unsigned short get_ushort(const unsigned char *data) { #define TGA_HEADER_SIZE 18 -static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, +static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, unsigned int *width, unsigned int *height, int *flip_image) { int palette_size; @@ -581,8 +582,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, if (!bits_per_pixel || !width || !height || !flip_image) return 0; - if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 ) - { + if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0 ; } @@ -608,11 +608,9 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, *height = (unsigned int)image_h; /* Ignore tga identifier, if present ... */ - if (id_len) - { + if (id_len) { unsigned char *id = (unsigned char *) malloc(id_len); - if ( !fread(id, id_len, 1, fp) ) - { + if ( !fread(id, id_len, 1, fp) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); free(id); return 0 ; @@ -623,8 +621,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, /* Test for compressed formats ... not yet supported ... // Note :- 9 - RLE encoded palettized. // 10 - RLE encoded RGB. */ - if (image_type > 8) - { + if (image_type > 8) { fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n"); return 0 ; } @@ -634,8 +631,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, /* Palettized formats are not yet supported, skip over the palette, if present ... */ palette_size = cmap_len * (cmap_entry_size/8); - if (palette_size>0) - { + if (palette_size>0) { fprintf(stderr, "File contains a palette - not yet supported."); fseek(fp, palette_size, SEEK_CUR); } @@ -651,7 +647,7 @@ static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x) #endif -static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, +static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, OPJ_BOOL flip_image) { OPJ_UINT16 image_w, image_h, us0; @@ -665,7 +661,7 @@ static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, if ( bits_per_pixel < 256 ) pixel_depth = (unsigned char)bits_per_pixel; - else{ + else { fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header"); return 0; } @@ -713,7 +709,8 @@ fails: return 0; } -opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { +opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) +{ FILE *f; opj_image_t *image; unsigned int image_width, image_height, pixel_bit_depth; @@ -753,8 +750,7 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { if (mono) { color_space = OPJ_CLRSPC_GRAY; numcomps = save_alpha ? 2 : 1; - } - else { + } else { numcomps = save_alpha ? 4 : 3; color_space = OPJ_CLRSPC_SRGB; } @@ -788,8 +784,7 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { image->y1 = !image->y0 ? (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1; /* set image data */ - for (y=0; y < image_height; y++) - { + for (y=0; y < image_height; y++) { int index; if (flip_image) @@ -797,28 +792,23 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { else index = (int)(y*image_width); - if (numcomps==3) - { - for (x=0;x<image_width;x++) - { + if (numcomps==3) { + for (x=0; x<image_width; x++) { unsigned char r,g,b; - if( !fread(&b, 1, 1, f) ) - { + if( !fread(&b, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); fclose(f); return NULL; } - if ( !fread(&g, 1, 1, f) ) - { + if ( !fread(&g, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); fclose(f); return NULL; } - if ( !fread(&r, 1, 1, f) ) - { + if ( !fread(&r, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); fclose(f); @@ -830,35 +820,28 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { image->comps[2].data[index]=b; index++; } - } - else if (numcomps==4) - { - for (x=0;x<image_width;x++) - { + } else if (numcomps==4) { + for (x=0; x<image_width; x++) { unsigned char r,g,b,a; - if ( !fread(&b, 1, 1, f) ) - { + if ( !fread(&b, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); fclose(f); return NULL; } - if ( !fread(&g, 1, 1, f) ) - { + if ( !fread(&g, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); fclose(f); return NULL; } - if ( !fread(&r, 1, 1, f) ) - { + if ( !fread(&r, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); fclose(f); return NULL; } - if ( !fread(&a, 1, 1, f) ) - { + if ( !fread(&a, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image); fclose(f); @@ -871,8 +854,7 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { image->comps[3].data[index]=a; index++; } - } - else { + } else { fprintf(stderr, "Currently unsupported bit depth : %s\n", filename); } } @@ -880,7 +862,8 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { return image; } -int imagetotga(opj_image_t * image, const char *outfile) { +int imagetotga(opj_image_t * image, const char *outfile) +{ int width, height, bpp, x, y; OPJ_BOOL write_alpha; unsigned int i; @@ -919,7 +902,7 @@ int imagetotga(opj_image_t * image, const char *outfile) { bpp = write_alpha ? 32 : 24; if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE)) - goto fin; + goto fin; alpha_channel = image->numcomps-1; @@ -929,74 +912,69 @@ int imagetotga(opj_image_t * image, const char *outfile) { adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); - for (y=0; y < height; y++) - { - unsigned int index= (unsigned int)(y*width); - - for (x=0; x < width; x++, index++) - { - r = (float)(image->comps[0].data[index] + adjustR); - - if (image->numcomps > 2) - { - g = (float)(image->comps[1].data[index] + adjustG); - b = (float)(image->comps[2].data[index] + adjustB); - } - else - {/* Greyscale ... */ - g = r; - b = r; - } - -/* TGA format writes BGR ... */ - if(b > 255.) b = 255.; else if(b < 0.) b = 0.; - value = (unsigned char)(b*scale); - res = fwrite(&value,1,1,fdest); - - if( res < 1 ) - { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - goto fin; - } - if(g > 255.) g = 255.; else if(g < 0.) g = 0.; - value = (unsigned char)(g*scale); - res = fwrite(&value,1,1,fdest); - - if( res < 1 ) - { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - goto fin; - } - if(r > 255.) r = 255.; else if(r < 0.) r = 0.; - value = (unsigned char)(r*scale); - res = fwrite(&value,1,1,fdest); - - if( res < 1 ) - { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - goto fin; - } - - if (write_alpha) - { - a = (float)(image->comps[alpha_channel].data[index]); - if(a > 255.) a = 255.; else if(a < 0.) a = 0.; - value = (unsigned char)(a*scale); - res = fwrite(&value,1,1,fdest); - - if( res < 1 ) - { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - goto fin; - } - } - } - } - fails = 0; + for (y=0; y < height; y++) { + unsigned int index= (unsigned int)(y*width); + + for (x=0; x < width; x++, index++) { + r = (float)(image->comps[0].data[index] + adjustR); + + if (image->numcomps > 2) { + g = (float)(image->comps[1].data[index] + adjustG); + b = (float)(image->comps[2].data[index] + adjustB); + } else { + /* Greyscale ... */ + g = r; + b = r; + } + + /* TGA format writes BGR ... */ + if(b > 255.) b = 255.; + else if(b < 0.) b = 0.; + value = (unsigned char)(b*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + if(g > 255.) g = 255.; + else if(g < 0.) g = 0.; + value = (unsigned char)(g*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + if(r > 255.) r = 255.; + else if(r < 0.) r = 0.; + value = (unsigned char)(r*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + + if (write_alpha) { + a = (float)(image->comps[alpha_channel].data[index]); + if(a > 255.) a = 255.; + else if(a < 0.) a = 0.; + value = (unsigned char)(a*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + } + } + } + fails = 0; fin: - fclose(fdest); + fclose(fdest); - return fails; + return fails; } /* -->> -->> -->> -->> @@ -1009,8 +987,7 @@ PGX IMAGE FORMAT static unsigned char readuchar(FILE * f) { unsigned char c1; - if ( !fread(&c1, 1, 1, f) ) - { + if ( !fread(&c1, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0; } @@ -1020,13 +997,11 @@ static unsigned char readuchar(FILE * f) static unsigned short readushort(FILE * f, int bigendian) { unsigned char c1, c2; - if ( !fread(&c1, 1, 1, f) ) - { + if ( !fread(&c1, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0; } - if ( !fread(&c2, 1, 1, f) ) - { + if ( !fread(&c2, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0; } @@ -1039,23 +1014,19 @@ static unsigned short readushort(FILE * f, int bigendian) static unsigned int readuint(FILE * f, int bigendian) { unsigned char c1, c2, c3, c4; - if ( !fread(&c1, 1, 1, f) ) - { + if ( !fread(&c1, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0; } - if ( !fread(&c2, 1, 1, f) ) - { + if ( !fread(&c2, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0; } - if ( !fread(&c3, 1, 1, f) ) - { + if ( !fread(&c3, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0; } - if ( !fread(&c4, 1, 1, f) ) - { + if ( !fread(&c4, 1, 1, f) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); return 0; } @@ -1065,7 +1036,8 @@ static unsigned int readuint(FILE * f, int bigendian) return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(c2 << 8) + c1; } -opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { +opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) +{ FILE *f = NULL; int w, h, prec; int i, numcomps, max; @@ -1095,7 +1067,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { } fseek(f, 0, SEEK_SET); - if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){ + if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9) { fclose(f); fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n"); return NULL; @@ -1131,15 +1103,15 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { } else { cmptparm.sgnd = 0; } - if(prec < 8) - { + if(prec < 8) { force8 = 1; - ushift = 8 - prec; dshift = prec - ushift; - if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0; + ushift = 8 - prec; + dshift = prec - ushift; + if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); + else adjustS = 0; cmptparm.sgnd = 0; prec = 8; - } - else ushift = dshift = force8 = adjustS = 0; + } else ushift = dshift = force8 = adjustS = 0; cmptparm.prec = (OPJ_UINT32)prec; cmptparm.bpp = (OPJ_UINT32)prec; @@ -1164,8 +1136,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { for (i = 0; i < w * h; i++) { int v; - if(force8) - { + if(force8) { v = readuchar(f) + adjustS; v = (v<<ushift) + (v>>dshift); comp->data[i] = (unsigned char)v; @@ -1207,105 +1178,96 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { static INLINE int clamp( const int value, const int prec, const int sgnd ) { - if( sgnd ) - { - if (prec <= 8) return CLAMP(value,-128,127); - else if (prec <= 16) return CLAMP(value,-32768,32767); - else return CLAMP(value,-2147483647-1,2147483647); - } - else - { - if (prec <= 8) return CLAMP(value,0,255); - else if (prec <= 16) return CLAMP(value,0,65535); - else return value; /*CLAMP(value,0,4294967295);*/ + if( sgnd ) { + if (prec <= 8) return CLAMP(value,-128,127); + else if (prec <= 16) return CLAMP(value,-32768,32767); + else return CLAMP(value,-2147483647-1,2147483647); + } else { + if (prec <= 8) return CLAMP(value,0,255); + else if (prec <= 16) return CLAMP(value,0,65535); + else return value; /*CLAMP(value,0,4294967295);*/ } } -int imagetopgx(opj_image_t * image, const char *outfile) +int imagetopgx(opj_image_t * image, const char *outfile) { - int w, h; - int i, j, fails = 1; - unsigned int compno; - FILE *fdest = NULL; - - for (compno = 0; compno < image->numcomps; compno++) - { - opj_image_comp_t *comp = &image->comps[compno]; - char bname[256]; /* buffer for name */ - char *name = bname; /* pointer */ - int nbytes = 0; - size_t res; - const size_t olen = strlen(outfile); - const size_t dotpos = olen - 4; - const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */ - - if( outfile[dotpos] != '.' ) - { - /* `pgx` was recognized but there is no dot at expected position */ - fprintf(stderr, "ERROR -> Impossible happen." ); - goto fin; - } - if( total > 256 ) - { - name = (char*)malloc(total+1); - if (name == NULL) { - goto fin; - } - } - strncpy(name, outfile, dotpos); - sprintf(name+dotpos, "_%u.pgx", compno); - fdest = fopen(name, "wb"); - /* don't need name anymore */ - - if (!fdest) - { - - fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); - if( total > 256 ) free(name); - goto fin; - } - - w = (int)image->comps[compno].w; - h = (int)image->comps[compno].h; - - fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, - w, h); - - if (comp->prec <= 8) - nbytes = 1; - else if (comp->prec <= 16) - nbytes = 2; - else - nbytes = 4; - - for (i = 0; i < w * h; i++) - { - /* FIXME: clamp func is being called within a loop */ - const int val = clamp(image->comps[compno].data[i], - (int)comp->prec, (int)comp->sgnd); - - for (j = nbytes - 1; j >= 0; j--) - { - int v = (int)(val >> (j * 8)); - unsigned char byte = (unsigned char)v; - res = fwrite(&byte, 1, 1, fdest); - - if( res < 1 ) - { - fprintf(stderr, "failed to write 1 byte for %s\n", name); - if( total > 256 ) free(name); - goto fin; - } - } - } - if( total > 256 ) free(name); - fclose(fdest); fdest = NULL; - } - fails = 0; + int w, h; + int i, j, fails = 1; + unsigned int compno; + FILE *fdest = NULL; + + for (compno = 0; compno < image->numcomps; compno++) { + opj_image_comp_t *comp = &image->comps[compno]; + char bname[256]; /* buffer for name */ + char *name = bname; /* pointer */ + int nbytes = 0; + size_t res; + const size_t olen = strlen(outfile); + const size_t dotpos = olen - 4; + const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */ + + if( outfile[dotpos] != '.' ) { + /* `pgx` was recognized but there is no dot at expected position */ + fprintf(stderr, "ERROR -> Impossible happen." ); + goto fin; + } + if( total > 256 ) { + name = (char*)malloc(total+1); + if (name == NULL) { + goto fin; + } + } + strncpy(name, outfile, dotpos); + sprintf(name+dotpos, "_%u.pgx", compno); + fdest = fopen(name, "wb"); + /* don't need name anymore */ + + if (!fdest) { + + fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); + if( total > 256 ) free(name); + goto fin; + } + + w = (int)image->comps[compno].w; + h = (int)image->comps[compno].h; + + fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, + w, h); + + if (comp->prec <= 8) + nbytes = 1; + else if (comp->prec <= 16) + nbytes = 2; + else + nbytes = 4; + + for (i = 0; i < w * h; i++) { + /* FIXME: clamp func is being called within a loop */ + const int val = clamp(image->comps[compno].data[i], + (int)comp->prec, (int)comp->sgnd); + + for (j = nbytes - 1; j >= 0; j--) { + int v = (int)(val >> (j * 8)); + unsigned char byte = (unsigned char)v; + res = fwrite(&byte, 1, 1, fdest); + + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", name); + if( total > 256 ) free(name); + goto fin; + } + } + } + if( total > 256 ) free(name); + fclose(fdest); + fdest = NULL; + } + fails = 0; fin: - if(fdest) fclose(fdest); + if(fdest) fclose(fdest); - return fails; + return fails; } /* -->> -->> -->> -->> @@ -1314,8 +1276,7 @@ PNM IMAGE FORMAT <<-- <<-- <<-- <<-- */ -struct pnm_header -{ +struct pnm_header { int width, height, maxval, depth, format; char rgb, rgba, gray, graya, bw; char ok; @@ -1323,10 +1284,12 @@ struct pnm_header static char *skip_white(char *s) { - while(*s) - { + while(*s) { if(*s == '\n' || *s == '\r') return NULL; - if(isspace(*s)) { ++s; continue; } + if(isspace(*s)) { + ++s; + continue; + } return s; } return NULL; @@ -1343,12 +1306,14 @@ static char *skip_int(char *start, int *out_n) if(s == NULL) return NULL; start = s; - while(*s) - { + while(*s) { if( !isdigit(*s)) break; ++s; } - c = *s; *s = 0; *out_n = atoi(start); *s = c; + c = *s; + *s = 0; + *out_n = atoi(start); + *s = c; return s; } @@ -1361,12 +1326,17 @@ static char *skip_idf(char *start, char out_idf[256]) if(s == NULL) return NULL; start = s; - while(*s) - { - if(isalpha(*s) || *s == '_') { ++s; continue; } + while(*s) { + if(isalpha(*s) || *s == '_') { + ++s; + continue; + } break; } - c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c; + c = *s; + *s = 0; + strncpy(out_idf, start, 255); + *s = c; return s; } @@ -1376,95 +1346,91 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph) char idf[256], type[256]; char line[256]; - if (fgets(line, 250, reader) == NULL) - { + if (fgets(line, 250, reader) == NULL) { fprintf(stderr,"\nWARNING: fgets return a NULL value"); return; } - if(line[0] != 'P') - { - fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return; + if(line[0] != 'P') { + fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); + return; } format = atoi(line + 1); - if(format < 1 || format > 7) - { + if(format < 1 || format > 7) { fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format); return; } ph->format = format; ttype = end = have_wh = 0; - while(fgets(line, 250, reader)) - { + while(fgets(line, 250, reader)) { char *s; if(*line == '#') continue; s = line; - if(format == 7) - { + if(format == 7) { s = skip_idf(s, idf); if(s == NULL || *s == 0) return; - if(strcmp(idf, "ENDHDR") == 0) - { - end = 1; break; + if(strcmp(idf, "ENDHDR") == 0) { + end = 1; + break; } - if(strcmp(idf, "WIDTH") == 0) - { + if(strcmp(idf, "WIDTH") == 0) { s = skip_int(s, &ph->width); if(s == NULL || *s == 0) return; continue; } - if(strcmp(idf, "HEIGHT") == 0) - { + if(strcmp(idf, "HEIGHT") == 0) { s = skip_int(s, &ph->height); if(s == NULL || *s == 0) return; continue; } - if(strcmp(idf, "DEPTH") == 0) - { + if(strcmp(idf, "DEPTH") == 0) { s = skip_int(s, &ph->depth); if(s == NULL || *s == 0) return; continue; } - if(strcmp(idf, "MAXVAL") == 0) - { + if(strcmp(idf, "MAXVAL") == 0) { s = skip_int(s, &ph->maxval); if(s == NULL || *s == 0) return; continue; } - if(strcmp(idf, "TUPLTYPE") == 0) - { + if(strcmp(idf, "TUPLTYPE") == 0) { s = skip_idf(s, type); if(s == NULL || *s == 0) return; - if(strcmp(type, "BLACKANDWHITE") == 0) - { - ph->bw = 1; ttype = 1; continue; + if(strcmp(type, "BLACKANDWHITE") == 0) { + ph->bw = 1; + ttype = 1; + continue; } - if(strcmp(type, "GRAYSCALE") == 0) - { - ph->gray = 1; ttype = 1; continue; + if(strcmp(type, "GRAYSCALE") == 0) { + ph->gray = 1; + ttype = 1; + continue; } - if(strcmp(type, "GRAYSCALE_ALPHA") == 0) - { - ph->graya = 1; ttype = 1; continue; + if(strcmp(type, "GRAYSCALE_ALPHA") == 0) { + ph->graya = 1; + ttype = 1; + continue; } - if(strcmp(type, "RGB") == 0) - { - ph->rgb = 1; ttype = 1; continue; + if(strcmp(type, "RGB") == 0) { + ph->rgb = 1; + ttype = 1; + continue; } - if(strcmp(type, "RGB_ALPHA") == 0) - { - ph->rgba = 1; ttype = 1; continue; + if(strcmp(type, "RGB_ALPHA") == 0) { + ph->rgba = 1; + ttype = 1; + continue; } fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type); return; @@ -1473,8 +1439,7 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph) return; } /* if(format == 7) */ - if( !have_wh) - { + if( !have_wh) { s = skip_int(s, &ph->width); s = skip_int(s, &ph->height); @@ -1482,22 +1447,19 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph) have_wh = 1; if(format == 1 || format == 4) break; - - if(format == 2 || format == 3 || format == 5 || format == 6) - { + + if(format == 2 || format == 3 || format == 5 || format == 6) { if (skip_int(s, &ph->maxval) != NULL) { if(ph->maxval > 65535) { return; - } - else { + } else { break; } } } continue; } - if(format == 2 || format == 3 || format == 5 || format == 6) - { + if(format == 2 || format == 3 || format == 5 || format == 6) { /* P2, P3, P5, P6: */ s = skip_int(s, &ph->maxval); @@ -1505,31 +1467,24 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph) } break; }/* while(fgets( ) */ - if(format == 2 || format == 3 || format > 4) - { + if(format == 2 || format == 3 || format > 4) { if(ph->maxval < 1 || ph->maxval > 65535) return; } if(ph->width < 1 || ph->height < 1) return; - if(format == 7) - { - if(!end) - { - fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return; + if(format == 7) { + if(!end) { + fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); + return; } if(ph->depth < 1 || ph->depth > 4) return; if(ph->width && ph->height && ph->depth && ph->maxval && ttype) ph->ok = 1; - } - else - { - if(format != 1 && format != 4) - { + } else { + if(format != 1 && format != 4) { if(ph->width && ph->height && ph->maxval) ph->ok = 1; - } - else - { + } else { if(ph->width && ph->height) ph->ok = 1; ph->maxval = 255; } @@ -1556,7 +1511,8 @@ static int has_prec(int val) return 16; } -opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { +opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) +{ int subsampling_dx = parameters->subsampling_dx; int subsampling_dy = parameters->subsampling_dy; @@ -1567,8 +1523,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { opj_image_t * image = NULL; struct pnm_header header_info; - if((fp = fopen(filename, "rb")) == NULL) - { + if((fp = fopen(filename, "rb")) == NULL) { fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename); return NULL; } @@ -1576,12 +1531,14 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { read_pnm_header(fp, &header_info); - if(!header_info.ok) { fclose(fp); return NULL; } + if(!header_info.ok) { + fclose(fp); + return NULL; + } format = header_info.format; - switch(format) - { + switch(format) { case 1: /* ascii bitmap */ case 4: /* raw bitmap */ numcomps = 1; @@ -1601,7 +1558,9 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { numcomps = header_info.depth; break; - default: fclose(fp); return NULL; + default: + fclose(fp); + return NULL; } if(numcomps < 3) color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */ @@ -1619,8 +1578,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t)); - for(i = 0; i < numcomps; i++) - { + for(i = 0; i < numcomps; i++) { cmptparm[i].prec = (OPJ_UINT32)prec; cmptparm[i].bpp = (OPJ_UINT32)prec; cmptparm[i].sgnd = 0; @@ -1631,7 +1589,10 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { } image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); - if(!image) { fclose(fp); return NULL; } + if(!image) { + fclose(fp); + return NULL; + } /* set image offset and reference grid */ image->x0 = (OPJ_UINT32)parameters->image_offset_x0; @@ -1639,14 +1600,11 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1); image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1); - if((format == 2) || (format == 3)) /* ascii pixmap */ - { + if((format == 2) || (format == 3)) { /* ascii pixmap */ unsigned int index; - for (i = 0; i < w * h; i++) - { - for(compno = 0; compno < numcomps; compno++) - { + for (i = 0; i < w * h; i++) { + for(compno = 0; compno < numcomps; compno++) { index = 0; if (fscanf(fp, "%u", &index) != 1) fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); @@ -1654,91 +1612,70 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { image->comps[compno].data[i] = (OPJ_INT32)(index * 255)/header_info.maxval; } } - } - else - if((format == 5) - || (format == 6) - ||((format == 7) - && ( header_info.gray || header_info.graya - || header_info.rgb || header_info.rgba)))/* binary pixmap */ - { - unsigned char c0, c1, one; - - one = (prec < 9); - - for (i = 0; i < w * h; i++) - { - for(compno = 0; compno < numcomps; compno++) - { - if ( !fread(&c0, 1, 1, fp) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - fclose(fp); - return NULL; - } - if(one) - { - image->comps[compno].data[i] = c0; - } - else - { - if ( !fread(&c1, 1, 1, fp) ) - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - /* netpbm: */ - image->comps[compno].data[i] = ((c0<<8) | c1); - } + } else if((format == 5) + || (format == 6) + ||((format == 7) + && ( header_info.gray || header_info.graya + || header_info.rgb || header_info.rgba))) { /* binary pixmap */ + unsigned char c0, c1, one; + + one = (prec < 9); + + for (i = 0; i < w * h; i++) { + for(compno = 0; compno < numcomps; compno++) { + if ( !fread(&c0, 1, 1, fp) ) { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + fclose(fp); + return NULL; + } + if(one) { + image->comps[compno].data[i] = c0; + } else { + if ( !fread(&c1, 1, 1, fp) ) + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + /* netpbm: */ + image->comps[compno].data[i] = ((c0<<8) | c1); } } } - else - if(format == 1) /* ascii bitmap */ - { - for (i = 0; i < w * h; i++) - { - unsigned int index; + } else if(format == 1) { /* ascii bitmap */ + for (i = 0; i < w * h; i++) { + unsigned int index; - if ( fscanf(fp, "%u", &index) != 1) - fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); + if ( fscanf(fp, "%u", &index) != 1) + fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); - image->comps[0].data[i] = (index?0:255); + image->comps[0].data[i] = (index?0:255); + } + } else if(format == 4) { + int x, y, bit; + unsigned char uc; + + i = 0; + for(y = 0; y < h; ++y) { + bit = -1; + uc = 0; + + for(x = 0; x < w; ++x) { + if(bit == -1) { + bit = 7; + uc = (unsigned char)getc(fp); } + image->comps[0].data[i] = (((uc>>bit) & 1)?0:255); + --bit; + ++i; } - else - if(format == 4) - { - int x, y, bit; - unsigned char uc; - - i = 0; - for(y = 0; y < h; ++y) - { - bit = -1; uc = 0; - - for(x = 0; x < w; ++x) - { - if(bit == -1) - { - bit = 7; - uc = (unsigned char)getc(fp); - } - image->comps[0].data[i] = (((uc>>bit) & 1)?0:255); - --bit; ++i; - } - } - } - else - if((format == 7 && header_info.bw)) /*MONO*/ - { - unsigned char uc; - - for(i = 0; i < w * h; ++i) - { - if ( !fread(&uc, 1, 1, fp) ) - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - image->comps[0].data[i] = (uc & 1)?0:255; - } - } + } + } else if((format == 7 && header_info.bw)) { /*MONO*/ + unsigned char uc; + + for(i = 0; i < w * h; ++i) { + if ( !fread(&uc, 1, 1, fp) ) + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + image->comps[0].data[i] = (uc & 1)?0:255; + } + } fclose(fp); return image; @@ -1757,57 +1694,55 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) const char *tmp = outfile; char *destname; - alpha = NULL; + alpha = NULL; - if((prec = (int)image->comps[0].prec) > 16) - { + if((prec = (int)image->comps[0].prec) > 16) { fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16" "\n\t: refused.\n",__FILE__,__LINE__,prec); return 1; } - two = has_alpha = 0; fails = 1; + two = has_alpha = 0; + fails = 1; ncomp = image->numcomps; - while (*tmp) ++tmp; tmp -= 2; + while (*tmp) ++tmp; + tmp -= 2; want_gray = (*tmp == 'g' || *tmp == 'G'); ncomp = image->numcomps; if(want_gray) ncomp = 1; if ((force_split == 0) && - (ncomp == 2 /* GRAYA */ - || (ncomp > 2 /* RGB, RGBA */ - && image->comps[0].dx == image->comps[1].dx - && image->comps[1].dx == image->comps[2].dx - && image->comps[0].dy == image->comps[1].dy - && image->comps[1].dy == image->comps[2].dy - && image->comps[0].prec == image->comps[1].prec - && image->comps[1].prec == image->comps[2].prec - ))) - { + (ncomp == 2 /* GRAYA */ + || (ncomp > 2 /* RGB, RGBA */ + && image->comps[0].dx == image->comps[1].dx + && image->comps[1].dx == image->comps[2].dx + && image->comps[0].dy == image->comps[1].dy + && image->comps[1].dy == image->comps[2].dy + && image->comps[0].prec == image->comps[1].prec + && image->comps[1].prec == image->comps[2].prec + ))) { fdest = fopen(outfile, "wb"); - if (!fdest) - { + if (!fdest) { fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); return fails; } two = (prec > 8); triple = (ncomp > 2); - wr = (int)image->comps[0].w; hr = (int)image->comps[0].h; - max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2); + wr = (int)image->comps[0].w; + hr = (int)image->comps[0].h; + max = (1<<prec) - 1; + has_alpha = (ncomp == 4 || ncomp == 2); red = image->comps[0].data; - if(triple) - { + if(triple) { green = image->comps[1].data; blue = image->comps[2].data; - } - else green = blue = NULL; + } else green = blue = NULL; - if(has_alpha) - { + if(has_alpha) { const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA"); fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %u\n" @@ -1815,53 +1750,53 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) wr, hr, ncomp, max, tt); alpha = image->comps[ncomp - 1].data; adjustA = (image->comps[ncomp - 1].sgnd ? - 1 << (image->comps[ncomp - 1].prec - 1) : 0); - } - else - { + 1 << (image->comps[ncomp - 1].prec - 1) : 0); + } else { fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n", opj_version(), wr, hr, max); adjustA = 0; } adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - if(triple) - { + if(triple) { adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); - } - else adjustG = adjustB = 0; + } else adjustG = adjustB = 0; - for(i = 0; i < wr * hr; ++i) - { - if(two) - { - v = *red + adjustR; ++red; -if(v > 65535) v = 65535; else if(v < 0) v = 0; + for(i = 0; i < wr * hr; ++i) { + if(two) { + v = *red + adjustR; + ++red; + if(v > 65535) v = 65535; + else if(v < 0) v = 0; /* netpbm: */ fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); - if(triple) - { - v = *green + adjustG; ++green; -if(v > 65535) v = 65535; else if(v < 0) v = 0; + if(triple) { + v = *green + adjustG; + ++green; + if(v > 65535) v = 65535; + else if(v < 0) v = 0; /* netpbm: */ fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); - v = *blue + adjustB; ++blue; -if(v > 65535) v = 65535; else if(v < 0) v = 0; + v = *blue + adjustB; + ++blue; + if(v > 65535) v = 65535; + else if(v < 0) v = 0; /* netpbm: */ fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); }/* if(triple) */ - if(has_alpha) - { - v = *alpha + adjustA; ++alpha; - if(v > 65535) v = 65535; else if(v < 0) v = 0; + if(has_alpha) { + v = *alpha + adjustA; + ++alpha; + if(v > 65535) v = 65535; + else if(v < 0) v = 0; /* netpbm: */ fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); @@ -1871,64 +1806,63 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0; } /* if(two) */ /* prec <= 8: */ - v = *red++; - if(v > 255) v = 255; else if(v < 0) v = 0; - - fprintf(fdest, "%c", (unsigned char)v); - if(triple) - { - v = *green++; - if(v > 255) v = 255; else if(v < 0) v = 0; - - fprintf(fdest, "%c", (unsigned char)v); - v = *blue++; - if(v > 255) v = 255; else if(v < 0) v = 0; - - fprintf(fdest, "%c", (unsigned char)v); - } - if(has_alpha) - { - v = *alpha++; - if(v > 255) v = 255; else if(v < 0) v = 0; - - fprintf(fdest, "%c", (unsigned char)v); - } + v = *red++; + if(v > 255) v = 255; + else if(v < 0) v = 0; + + fprintf(fdest, "%c", (unsigned char)v); + if(triple) { + v = *green++; + if(v > 255) v = 255; + else if(v < 0) v = 0; + + fprintf(fdest, "%c", (unsigned char)v); + v = *blue++; + if(v > 255) v = 255; + else if(v < 0) v = 0; + + fprintf(fdest, "%c", (unsigned char)v); + } + if(has_alpha) { + v = *alpha++; + if(v > 255) v = 255; + else if(v < 0) v = 0; + + fprintf(fdest, "%c", (unsigned char)v); + } } /* for(i */ - fclose(fdest); return 0; + fclose(fdest); + return 0; } /* YUV or MONO: */ - if (image->numcomps > ncomp) - { + if (image->numcomps > ncomp) { fprintf(stderr,"WARNING -> [PGM file] Only the first component\n"); fprintf(stderr," is written to the file\n"); } destname = (char*)malloc(strlen(outfile) + 8); - for (compno = 0; compno < ncomp; compno++) - { - if (ncomp > 1) - { - /*sprintf(destname, "%d.%s", compno, outfile);*/ - const size_t olen = strlen(outfile); - const size_t dotpos = olen - 4; - - strncpy(destname, outfile, dotpos); - sprintf(destname+dotpos, "_%u.pgm", compno); - } - else + for (compno = 0; compno < ncomp; compno++) { + if (ncomp > 1) { + /*sprintf(destname, "%d.%s", compno, outfile);*/ + const size_t olen = strlen(outfile); + const size_t dotpos = olen - 4; + + strncpy(destname, outfile, dotpos); + sprintf(destname+dotpos, "_%u.pgm", compno); + } else sprintf(destname, "%s", outfile); fdest = fopen(destname, "wb"); - if (!fdest) - { + if (!fdest) { fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname); free(destname); return 1; } - wr = (int)image->comps[compno].w; hr = (int)image->comps[compno].h; + wr = (int)image->comps[compno].w; + hr = (int)image->comps[compno].h; prec = (int)image->comps[compno].prec; max = (1<<prec) - 1; @@ -1937,36 +1871,35 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0; red = image->comps[compno].data; adjustR = - (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0); + (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0); - if(prec > 8) - { - for (i = 0; i < wr * hr; i++) - { - v = *red + adjustR; ++red; -if(v > 65535) v = 65535; else if(v < 0) v = 0; + if(prec > 8) { + for (i = 0; i < wr * hr; i++) { + v = *red + adjustR; + ++red; + if(v > 65535) v = 65535; + else if(v < 0) v = 0; /* netpbm: */ fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); - if(has_alpha) - { + if(has_alpha) { v = *alpha++; -if(v > 65535) v = 65535; else if(v < 0) v = 0; + if(v > 65535) v = 65535; + else if(v < 0) v = 0; /* netpbm: */ fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); } }/* for(i */ - } - else /* prec <= 8 */ - { - for(i = 0; i < wr * hr; ++i) - { - v = *red + adjustR; ++red; - if(v > 255) v = 255; else if(v < 0) v = 0; - - fprintf(fdest, "%c", (unsigned char)v); + } else { /* prec <= 8 */ + for(i = 0; i < wr * hr; ++i) { + v = *red + adjustR; + ++red; + if(v > 255) v = 255; + else if(v < 0) v = 0; + + fprintf(fdest, "%c", (unsigned char)v); } } fclose(fdest); @@ -1981,7 +1914,8 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0; RAW IMAGE FORMAT <<-- <<-- <<-- <<-- */ -static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) { +static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) +{ int subsampling_dx = parameters->subsampling_dx; int subsampling_dy = parameters->subsampling_dy; @@ -1992,8 +1926,7 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p opj_image_t * image = NULL; unsigned short ch; - if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0) - { + if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0) { fprintf(stderr,"\nError: invalid raw image parameters\n"); fprintf(stderr,"Please use the Format option -F:\n"); fprintf(stderr,"-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n"); @@ -2054,8 +1987,7 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; - if(raw_cp->rawBitDepth <= 8) - { + if(raw_cp->rawBitDepth <= 8) { unsigned char value = 0; for(compno = 0; compno < numcomps; compno++) { int nloop = (w*h)/(raw_cp->rawComps[compno].dx*raw_cp->rawComps[compno].dy); @@ -2069,9 +2001,7 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value; } } - } - else if(raw_cp->rawBitDepth <= 16) - { + } else if(raw_cp->rawBitDepth <= 16) { unsigned short value; for(compno = 0; compno < numcomps; compno++) { int nloop = (w*h)/(raw_cp->rawComps[compno].dx*raw_cp->rawComps[compno].dy); @@ -2090,19 +2020,15 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p fclose(f); return NULL; } - if( big_endian ) - { + if( big_endian ) { value = (unsigned short)((temp1 << 8) + temp2); - } - else - { + } else { value = (unsigned short)((temp2 << 8) + temp1); } image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value; } } - } - else { + } else { fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n"); opj_image_destroy(image); fclose(f); @@ -2117,11 +2043,13 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p return image; } -opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) { +opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) +{ return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE); } -opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) { +opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) +{ return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE); } @@ -2136,8 +2064,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL unsigned char uc; (void)big_endian; - if((image->numcomps * image->x1 * image->y1) == 0) - { + if((image->numcomps * image->x1 * image->y1) == 0) { fprintf(stderr,"\nError: invalid raw image parameters\n"); return 1; } @@ -2151,24 +2078,22 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL fails = 1; fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps); - for(compno = 0; compno < image->numcomps; compno++) - { + for(compno = 0; compno < image->numcomps; compno++) { fprintf(stdout,"Component %u characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w, image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned"); w = (int)image->comps[compno].w; h = (int)image->comps[compno].h; - if(image->comps[compno].prec <= 8) - { - if(image->comps[compno].sgnd == 1) - { + if(image->comps[compno].prec <= 8) { + if(image->comps[compno].sgnd == 1) { mask = (1 << image->comps[compno].prec) - 1; ptr = image->comps[compno].data; for (line = 0; line < h; line++) { for(row = 0; row < w; row++) { curr = *ptr; - if(curr > 127) curr = 127; else if(curr < -128) curr = -128; + if(curr > 127) curr = 127; + else if(curr < -128) curr = -128; uc = (unsigned char) (curr & mask); res = fwrite(&uc, 1, 1, rawFile); if( res < 1 ) { @@ -2178,15 +2103,14 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL ptr++; } } - } - else if(image->comps[compno].sgnd == 0) - { + } else if(image->comps[compno].sgnd == 0) { mask = (1 << image->comps[compno].prec) - 1; ptr = image->comps[compno].data; for (line = 0; line < h; line++) { for(row = 0; row < w; row++) { curr = *ptr; - if(curr > 255) curr = 255; else if(curr < 0) curr = 0; + if(curr > 255) curr = 255; + else if(curr < 0) curr = 0; uc = (unsigned char) (curr & mask); res = fwrite(&uc, 1, 1, rawFile); if( res < 1 ) { @@ -2197,18 +2121,19 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL } } } - } - else if(image->comps[compno].prec <= 16) - { - if(image->comps[compno].sgnd == 1) - { - union { signed short val; signed char vals[2]; } uc16; + } else if(image->comps[compno].prec <= 16) { + if(image->comps[compno].sgnd == 1) { + union { + signed short val; + signed char vals[2]; + } uc16; mask = (1 << image->comps[compno].prec) - 1; ptr = image->comps[compno].data; for (line = 0; line < h; line++) { for(row = 0; row < w; row++) { curr = *ptr; - if(curr > 32767 ) curr = 32767; else if( curr < -32768) curr = -32768; + if(curr > 32767 ) curr = 32767; + else if( curr < -32768) curr = -32768; uc16.val = (signed short)(curr & mask); res = fwrite(uc16.vals, 1, 2, rawFile); if( res < 2 ) { @@ -2218,16 +2143,18 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL ptr++; } } - } - else if(image->comps[compno].sgnd == 0) - { - union { unsigned short val; unsigned char vals[2]; } uc16; + } else if(image->comps[compno].sgnd == 0) { + union { + unsigned short val; + unsigned char vals[2]; + } uc16; mask = (1 << image->comps[compno].prec) - 1; ptr = image->comps[compno].data; for (line = 0; line < h; line++) { for(row = 0; row < w; row++) { curr = *ptr; - if(curr > 65535 ) curr = 65535; else if( curr < 0) curr = 0; + if(curr > 65535 ) curr = 65535; + else if( curr < 0) curr = 0; uc16.val = (unsigned short)(curr & mask); res = fwrite(uc16.vals, 1, 2, rawFile); if( res < 2 ) { @@ -2238,19 +2165,15 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL } } } - } - else if (image->comps[compno].prec <= 32) - { + } else if (image->comps[compno].prec <= 32) { fprintf(stderr,"More than 16 bits per component no handled yet\n"); goto fin; - } - else - { + } else { fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec); goto fin; } } - fails = 0; + fails = 0; fin: fclose(rawFile); return fails; diff --git a/src/bin/jp2/convert.h b/src/bin/jp2/convert.h index ab1d61a8..8c54b0af 100644 --- a/src/bin/jp2/convert.h +++ b/src/bin/jp2/convert.h @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -8,7 +8,7 @@ * Copyright (c) 2002-2014, Professor Benoit Macq * Copyright (c) 2001-2003, David Janssens * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2007, Francois-Olivier Devaux * Copyright (c) 2003-2014, Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * All rights reserved. @@ -50,19 +50,19 @@ typedef struct raw_comp_cparameters { /**@name RAW image encoding parameters */ /*@{*/ typedef struct raw_cparameters { - /** width of the raw image */ - int rawWidth; - /** height of the raw image */ - int rawHeight; + /** width of the raw image */ + int rawWidth; + /** height of the raw image */ + int rawHeight; /** number of components of the raw image */ - int rawComp; + int rawComp; /** bit depth of the raw image */ int rawBitDepth; /** signed/unsigned raw image */ OPJ_BOOL rawSigned; /** raw components parameters */ raw_comp_cparameters_t *rawComps; - /*@}*/ + /*@}*/ } raw_cparameters_t; /* Component precision clipping */ diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index 910574b8..ae5f232e 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -8,7 +8,7 @@ * Copyright (c) 2002-2014, Professor Benoit Macq * Copyright (c) 2001-2003, David Janssens * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2007, Francois-Olivier Devaux * Copyright (c) 2003-2014, Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan @@ -46,743 +46,722 @@ #include "convert.h" typedef struct { - OPJ_UINT16 bfType; /* 'BM' for Bitmap (19776) */ - OPJ_UINT32 bfSize; /* Size of the file */ - OPJ_UINT16 bfReserved1; /* Reserved : 0 */ - OPJ_UINT16 bfReserved2; /* Reserved : 0 */ - OPJ_UINT32 bfOffBits; /* Offset */ + OPJ_UINT16 bfType; /* 'BM' for Bitmap (19776) */ + OPJ_UINT32 bfSize; /* Size of the file */ + OPJ_UINT16 bfReserved1; /* Reserved : 0 */ + OPJ_UINT16 bfReserved2; /* Reserved : 0 */ + OPJ_UINT32 bfOffBits; /* Offset */ } OPJ_BITMAPFILEHEADER; typedef struct { - OPJ_UINT32 biSize; /* Size of the structure in bytes */ - OPJ_UINT32 biWidth; /* Width of the image in pixels */ - OPJ_UINT32 biHeight; /* Heigth of the image in pixels */ - OPJ_UINT16 biPlanes; /* 1 */ - OPJ_UINT16 biBitCount; /* Number of color bits by pixels */ - OPJ_UINT32 biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ - OPJ_UINT32 biSizeImage; /* Size of the image in bytes */ - OPJ_UINT32 biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ - OPJ_UINT32 biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ - OPJ_UINT32 biClrUsed; /* Number of color used in the image (0: ALL) */ - OPJ_UINT32 biClrImportant; /* Number of important color (0: ALL) */ - OPJ_UINT32 biRedMask; /* Red channel bit mask */ - OPJ_UINT32 biGreenMask; /* Green channel bit mask */ - OPJ_UINT32 biBlueMask; /* Blue channel bit mask */ - OPJ_UINT32 biAlphaMask; /* Alpha channel bit mask */ - OPJ_UINT32 biColorSpaceType; /* Color space type */ - OPJ_UINT8 biColorSpaceEP[36]; /* Color space end points */ - OPJ_UINT32 biRedGamma; /* Red channel gamma */ - OPJ_UINT32 biGreenGamma; /* Green channel gamma */ - OPJ_UINT32 biBlueGamma; /* Blue channel gamma */ - OPJ_UINT32 biIntent; /* Intent */ - OPJ_UINT32 biIccProfileData; /* ICC profile data */ - OPJ_UINT32 biIccProfileSize; /* ICC profile size */ - OPJ_UINT32 biReserved; /* Reserved */ + OPJ_UINT32 biSize; /* Size of the structure in bytes */ + OPJ_UINT32 biWidth; /* Width of the image in pixels */ + OPJ_UINT32 biHeight; /* Heigth of the image in pixels */ + OPJ_UINT16 biPlanes; /* 1 */ + OPJ_UINT16 biBitCount; /* Number of color bits by pixels */ + OPJ_UINT32 biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ + OPJ_UINT32 biSizeImage; /* Size of the image in bytes */ + OPJ_UINT32 biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ + OPJ_UINT32 biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ + OPJ_UINT32 biClrUsed; /* Number of color used in the image (0: ALL) */ + OPJ_UINT32 biClrImportant; /* Number of important color (0: ALL) */ + OPJ_UINT32 biRedMask; /* Red channel bit mask */ + OPJ_UINT32 biGreenMask; /* Green channel bit mask */ + OPJ_UINT32 biBlueMask; /* Blue channel bit mask */ + OPJ_UINT32 biAlphaMask; /* Alpha channel bit mask */ + OPJ_UINT32 biColorSpaceType; /* Color space type */ + OPJ_UINT8 biColorSpaceEP[36]; /* Color space end points */ + OPJ_UINT32 biRedGamma; /* Red channel gamma */ + OPJ_UINT32 biGreenGamma; /* Green channel gamma */ + OPJ_UINT32 biBlueGamma; /* Blue channel gamma */ + OPJ_UINT32 biIntent; /* Intent */ + OPJ_UINT32 biIccProfileData; /* ICC profile data */ + OPJ_UINT32 biIccProfileSize; /* ICC profile size */ + OPJ_UINT32 biReserved; /* Reserved */ } OPJ_BITMAPINFOHEADER; static void opj_applyLUT8u_8u32s_C1R( - OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride, - OPJ_INT32* pDst, OPJ_INT32 dstStride, - OPJ_UINT8 const* pLUT, - OPJ_UINT32 width, OPJ_UINT32 height) + OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride, + OPJ_INT32* pDst, OPJ_INT32 dstStride, + OPJ_UINT8 const* pLUT, + OPJ_UINT32 width, OPJ_UINT32 height) { - OPJ_UINT32 y; - - for (y = height; y != 0U; --y) { - OPJ_UINT32 x; - - for(x = 0; x < width; x++) - { - pDst[x] = (OPJ_INT32)pLUT[pSrc[x]]; - } - pSrc += srcStride; - pDst += dstStride; - } + OPJ_UINT32 y; + + for (y = height; y != 0U; --y) { + OPJ_UINT32 x; + + for(x = 0; x < width; x++) { + pDst[x] = (OPJ_INT32)pLUT[pSrc[x]]; + } + pSrc += srcStride; + pDst += dstStride; + } } static void opj_applyLUT8u_8u32s_C1P3R( - OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride, - OPJ_INT32* const* pDst, OPJ_INT32 const* pDstStride, - OPJ_UINT8 const* const* pLUT, - OPJ_UINT32 width, OPJ_UINT32 height) + OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride, + OPJ_INT32* const* pDst, OPJ_INT32 const* pDstStride, + OPJ_UINT8 const* const* pLUT, + OPJ_UINT32 width, OPJ_UINT32 height) { - OPJ_UINT32 y; - OPJ_INT32* pR = pDst[0]; - OPJ_INT32* pG = pDst[1]; - OPJ_INT32* pB = pDst[2]; - OPJ_UINT8 const* pLUT_R = pLUT[0]; - OPJ_UINT8 const* pLUT_G = pLUT[1]; - OPJ_UINT8 const* pLUT_B = pLUT[2]; - - for (y = height; y != 0U; --y) { - OPJ_UINT32 x; - - for(x = 0; x < width; x++) - { - OPJ_UINT8 idx = pSrc[x]; - pR[x] = (OPJ_INT32)pLUT_R[idx]; - pG[x] = (OPJ_INT32)pLUT_G[idx]; - pB[x] = (OPJ_INT32)pLUT_B[idx]; - } - pSrc += srcStride; - pR += pDstStride[0]; - pG += pDstStride[1]; - pB += pDstStride[2]; - } + OPJ_UINT32 y; + OPJ_INT32* pR = pDst[0]; + OPJ_INT32* pG = pDst[1]; + OPJ_INT32* pB = pDst[2]; + OPJ_UINT8 const* pLUT_R = pLUT[0]; + OPJ_UINT8 const* pLUT_G = pLUT[1]; + OPJ_UINT8 const* pLUT_B = pLUT[2]; + + for (y = height; y != 0U; --y) { + OPJ_UINT32 x; + + for(x = 0; x < width; x++) { + OPJ_UINT8 idx = pSrc[x]; + pR[x] = (OPJ_INT32)pLUT_R[idx]; + pG[x] = (OPJ_INT32)pLUT_G[idx]; + pB[x] = (OPJ_INT32)pLUT_B[idx]; + } + pSrc += srcStride; + pR += pDstStride[0]; + pG += pDstStride[1]; + pB += pDstStride[2]; + } } static void bmp24toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image) { - int index; - OPJ_UINT32 width, height; - OPJ_UINT32 x, y; - const OPJ_UINT8 *pSrc = NULL; - - width = image->comps[0].w; - height = image->comps[0].h; - - index = 0; - pSrc = pData + (height - 1U) * stride; - for(y = 0; y < height; y++) - { - for(x = 0; x < width; x++) - { - image->comps[0].data[index] = (OPJ_INT32)pSrc[3*x+2]; /* R */ - image->comps[1].data[index] = (OPJ_INT32)pSrc[3*x+1]; /* G */ - image->comps[2].data[index] = (OPJ_INT32)pSrc[3*x+0]; /* B */ - index++; - } - pSrc -= stride; - } + int index; + OPJ_UINT32 width, height; + OPJ_UINT32 x, y; + const OPJ_UINT8 *pSrc = NULL; + + width = image->comps[0].w; + height = image->comps[0].h; + + index = 0; + pSrc = pData + (height - 1U) * stride; + for(y = 0; y < height; y++) { + for(x = 0; x < width; x++) { + image->comps[0].data[index] = (OPJ_INT32)pSrc[3*x+2]; /* R */ + image->comps[1].data[index] = (OPJ_INT32)pSrc[3*x+1]; /* G */ + image->comps[2].data[index] = (OPJ_INT32)pSrc[3*x+0]; /* B */ + index++; + } + pSrc -= stride; + } } static void bmp_mask_get_shift_and_prec(OPJ_UINT32 mask, OPJ_UINT32* shift, OPJ_UINT32* prec) { - OPJ_UINT32 l_shift, l_prec; - - l_shift = l_prec = 0U; - - if (mask != 0U) { - while ((mask & 1U) == 0U) { - mask >>= 1; - l_shift++; - } - while (mask & 1U) { - mask >>= 1; - l_prec++; - } - } - *shift = l_shift; *prec = l_prec; + OPJ_UINT32 l_shift, l_prec; + + l_shift = l_prec = 0U; + + if (mask != 0U) { + while ((mask & 1U) == 0U) { + mask >>= 1; + l_shift++; + } + while (mask & 1U) { + mask >>= 1; + l_prec++; + } + } + *shift = l_shift; + *prec = l_prec; } static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask, OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask) { - int index; - OPJ_UINT32 width, height; - OPJ_UINT32 x, y; - const OPJ_UINT8 *pSrc = NULL; - OPJ_BOOL hasAlpha = OPJ_FALSE; - OPJ_UINT32 redShift, redPrec; - OPJ_UINT32 greenShift, greenPrec; - OPJ_UINT32 blueShift, bluePrec; - OPJ_UINT32 alphaShift, alphaPrec; - - width = image->comps[0].w; - height = image->comps[0].h; - - hasAlpha = image->numcomps > 3U; - - bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec); - bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec); - bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec); - bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec); - - image->comps[0].bpp = redPrec; - image->comps[0].prec = redPrec; - image->comps[1].bpp = greenPrec; - image->comps[1].prec = greenPrec; - image->comps[2].bpp = bluePrec; - image->comps[2].prec = bluePrec; - if (hasAlpha) { - image->comps[3].bpp = alphaPrec; - image->comps[3].prec = alphaPrec; - } - - index = 0; - pSrc = pData + (height - 1U) * stride; - for(y = 0; y < height; y++) - { - for(x = 0; x < width; x++) - { - OPJ_UINT32 value = 0U; - - value |= ((OPJ_UINT32)pSrc[4*x+0]) << 0; - value |= ((OPJ_UINT32)pSrc[4*x+1]) << 8; - value |= ((OPJ_UINT32)pSrc[4*x+2]) << 16; - value |= ((OPJ_UINT32)pSrc[4*x+3]) << 24; - - image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >> redShift); /* R */ - image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >> greenShift); /* G */ - image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >> blueShift); /* B */ - if (hasAlpha) { - image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >> alphaShift); /* A */ - } - index++; - } - pSrc -= stride; - } + int index; + OPJ_UINT32 width, height; + OPJ_UINT32 x, y; + const OPJ_UINT8 *pSrc = NULL; + OPJ_BOOL hasAlpha = OPJ_FALSE; + OPJ_UINT32 redShift, redPrec; + OPJ_UINT32 greenShift, greenPrec; + OPJ_UINT32 blueShift, bluePrec; + OPJ_UINT32 alphaShift, alphaPrec; + + width = image->comps[0].w; + height = image->comps[0].h; + + hasAlpha = image->numcomps > 3U; + + bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec); + bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec); + bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec); + bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec); + + image->comps[0].bpp = redPrec; + image->comps[0].prec = redPrec; + image->comps[1].bpp = greenPrec; + image->comps[1].prec = greenPrec; + image->comps[2].bpp = bluePrec; + image->comps[2].prec = bluePrec; + if (hasAlpha) { + image->comps[3].bpp = alphaPrec; + image->comps[3].prec = alphaPrec; + } + + index = 0; + pSrc = pData + (height - 1U) * stride; + for(y = 0; y < height; y++) { + for(x = 0; x < width; x++) { + OPJ_UINT32 value = 0U; + + value |= ((OPJ_UINT32)pSrc[4*x+0]) << 0; + value |= ((OPJ_UINT32)pSrc[4*x+1]) << 8; + value |= ((OPJ_UINT32)pSrc[4*x+2]) << 16; + value |= ((OPJ_UINT32)pSrc[4*x+3]) << 24; + + image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >> redShift); /* R */ + image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >> greenShift); /* G */ + image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >> blueShift); /* B */ + if (hasAlpha) { + image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >> alphaShift); /* A */ + } + index++; + } + pSrc -= stride; + } } static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask, OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask) { - int index; - OPJ_UINT32 width, height; - OPJ_UINT32 x, y; - const OPJ_UINT8 *pSrc = NULL; - OPJ_BOOL hasAlpha = OPJ_FALSE; - OPJ_UINT32 redShift, redPrec; - OPJ_UINT32 greenShift, greenPrec; - OPJ_UINT32 blueShift, bluePrec; - OPJ_UINT32 alphaShift, alphaPrec; - - width = image->comps[0].w; - height = image->comps[0].h; - - hasAlpha = image->numcomps > 3U; - - bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec); - bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec); - bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec); - bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec); - - image->comps[0].bpp = redPrec; - image->comps[0].prec = redPrec; - image->comps[1].bpp = greenPrec; - image->comps[1].prec = greenPrec; - image->comps[2].bpp = bluePrec; - image->comps[2].prec = bluePrec; - if (hasAlpha) { - image->comps[3].bpp = alphaPrec; - image->comps[3].prec = alphaPrec; - } - - index = 0; - pSrc = pData + (height - 1U) * stride; - for(y = 0; y < height; y++) - { - for(x = 0; x < width; x++) - { - OPJ_UINT32 value = 0U; - - value |= ((OPJ_UINT32)pSrc[2*x+0]) << 0; - value |= ((OPJ_UINT32)pSrc[2*x+1]) << 8; - - image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >> redShift); /* R */ - image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >> greenShift); /* G */ - image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >> blueShift); /* B */ - if (hasAlpha) { - image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >> alphaShift); /* A */ - } - index++; - } - pSrc -= stride; - } + int index; + OPJ_UINT32 width, height; + OPJ_UINT32 x, y; + const OPJ_UINT8 *pSrc = NULL; + OPJ_BOOL hasAlpha = OPJ_FALSE; + OPJ_UINT32 redShift, redPrec; + OPJ_UINT32 greenShift, greenPrec; + OPJ_UINT32 blueShift, bluePrec; + OPJ_UINT32 alphaShift, alphaPrec; + + width = image->comps[0].w; + height = image->comps[0].h; + + hasAlpha = image->numcomps > 3U; + + bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec); + bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec); + bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec); + bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec); + + image->comps[0].bpp = redPrec; + image->comps[0].prec = redPrec; + image->comps[1].bpp = greenPrec; + image->comps[1].prec = greenPrec; + image->comps[2].bpp = bluePrec; + image->comps[2].prec = bluePrec; + if (hasAlpha) { + image->comps[3].bpp = alphaPrec; + image->comps[3].prec = alphaPrec; + } + + index = 0; + pSrc = pData + (height - 1U) * stride; + for(y = 0; y < height; y++) { + for(x = 0; x < width; x++) { + OPJ_UINT32 value = 0U; + + value |= ((OPJ_UINT32)pSrc[2*x+0]) << 0; + value |= ((OPJ_UINT32)pSrc[2*x+1]) << 8; + + image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >> redShift); /* R */ + image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >> greenShift); /* G */ + image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >> blueShift); /* B */ + if (hasAlpha) { + image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >> alphaShift); /* A */ + } + index++; + } + pSrc -= stride; + } } static opj_image_t* bmp8toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image, OPJ_UINT8 const* const* pLUT) { - OPJ_UINT32 width, height; - const OPJ_UINT8 *pSrc = NULL; - - width = image->comps[0].w; - height = image->comps[0].h; - - pSrc = pData + (height - 1U) * stride; - if (image->numcomps == 1U) { - opj_applyLUT8u_8u32s_C1R(pSrc, -(OPJ_INT32)stride, image->comps[0].data, (OPJ_INT32)width, pLUT[0], width, height); - } - else { - OPJ_INT32* pDst[3]; - OPJ_INT32 pDstStride[3]; - - pDst[0] = image->comps[0].data; pDst[1] = image->comps[1].data; pDst[2] = image->comps[2].data; - pDstStride[0] = (OPJ_INT32)width; pDstStride[1] = (OPJ_INT32)width; pDstStride[2] = (OPJ_INT32)width; - opj_applyLUT8u_8u32s_C1P3R(pSrc, -(OPJ_INT32)stride, pDst, pDstStride, pLUT, width, height); - } - return image; + OPJ_UINT32 width, height; + const OPJ_UINT8 *pSrc = NULL; + + width = image->comps[0].w; + height = image->comps[0].h; + + pSrc = pData + (height - 1U) * stride; + if (image->numcomps == 1U) { + opj_applyLUT8u_8u32s_C1R(pSrc, -(OPJ_INT32)stride, image->comps[0].data, (OPJ_INT32)width, pLUT[0], width, height); + } else { + OPJ_INT32* pDst[3]; + OPJ_INT32 pDstStride[3]; + + pDst[0] = image->comps[0].data; + pDst[1] = image->comps[1].data; + pDst[2] = image->comps[2].data; + pDstStride[0] = (OPJ_INT32)width; + pDstStride[1] = (OPJ_INT32)width; + pDstStride[2] = (OPJ_INT32)width; + opj_applyLUT8u_8u32s_C1P3R(pSrc, -(OPJ_INT32)stride, pDst, pDstStride, pLUT, width, height); + } + return image; } static OPJ_BOOL bmp_read_file_header(FILE* IN, OPJ_BITMAPFILEHEADER* header) { - header->bfType = (OPJ_UINT16)getc(IN); - header->bfType |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); - - if (header->bfType != 19778) { - fprintf(stderr,"Error, not a BMP file!\n"); - return OPJ_FALSE; - } - - /* FILE HEADER */ - /* ------------- */ - header->bfSize = (OPJ_UINT32)getc(IN); - header->bfSize |= (OPJ_UINT32)getc(IN) << 8; - header->bfSize |= (OPJ_UINT32)getc(IN) << 16; - header->bfSize |= (OPJ_UINT32)getc(IN) << 24; - - header->bfReserved1 = (OPJ_UINT16)getc(IN); - header->bfReserved1 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); - - header->bfReserved2 = (OPJ_UINT16)getc(IN); - header->bfReserved2 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); - - header->bfOffBits = (OPJ_UINT32)getc(IN); - header->bfOffBits |= (OPJ_UINT32)getc(IN) << 8; - header->bfOffBits |= (OPJ_UINT32)getc(IN) << 16; - header->bfOffBits |= (OPJ_UINT32)getc(IN) << 24; - return OPJ_TRUE; + header->bfType = (OPJ_UINT16)getc(IN); + header->bfType |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); + + if (header->bfType != 19778) { + fprintf(stderr,"Error, not a BMP file!\n"); + return OPJ_FALSE; + } + + /* FILE HEADER */ + /* ------------- */ + header->bfSize = (OPJ_UINT32)getc(IN); + header->bfSize |= (OPJ_UINT32)getc(IN) << 8; + header->bfSize |= (OPJ_UINT32)getc(IN) << 16; + header->bfSize |= (OPJ_UINT32)getc(IN) << 24; + + header->bfReserved1 = (OPJ_UINT16)getc(IN); + header->bfReserved1 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); + + header->bfReserved2 = (OPJ_UINT16)getc(IN); + header->bfReserved2 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); + + header->bfOffBits = (OPJ_UINT32)getc(IN); + header->bfOffBits |= (OPJ_UINT32)getc(IN) << 8; + header->bfOffBits |= (OPJ_UINT32)getc(IN) << 16; + header->bfOffBits |= (OPJ_UINT32)getc(IN) << 24; + return OPJ_TRUE; } static OPJ_BOOL bmp_read_info_header(FILE* IN, OPJ_BITMAPINFOHEADER* header) { - memset(header, 0, sizeof(*header)); - /* INFO HEADER */ - /* ------------- */ - header->biSize = (OPJ_UINT32)getc(IN); - header->biSize |= (OPJ_UINT32)getc(IN) << 8; - header->biSize |= (OPJ_UINT32)getc(IN) << 16; - header->biSize |= (OPJ_UINT32)getc(IN) << 24; - - switch (header->biSize) { - case 12U: /* BITMAPCOREHEADER */ - case 40U: /* BITMAPINFOHEADER */ - case 52U: /* BITMAPV2INFOHEADER */ - case 56U: /* BITMAPV3INFOHEADER */ - case 108U: /* BITMAPV4HEADER */ - case 124U: /* BITMAPV5HEADER */ - break; - default: - fprintf(stderr,"Error, unknown BMP header size %d\n", header->biSize); - return OPJ_FALSE; - } - - header->biWidth = (OPJ_UINT32)getc(IN); - header->biWidth |= (OPJ_UINT32)getc(IN) << 8; - header->biWidth |= (OPJ_UINT32)getc(IN) << 16; - header->biWidth |= (OPJ_UINT32)getc(IN) << 24; - - header->biHeight = (OPJ_UINT32)getc(IN); - header->biHeight |= (OPJ_UINT32)getc(IN) << 8; - header->biHeight |= (OPJ_UINT32)getc(IN) << 16; - header->biHeight |= (OPJ_UINT32)getc(IN) << 24; - - header->biPlanes = (OPJ_UINT16)getc(IN); - header->biPlanes |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); - - header->biBitCount = (OPJ_UINT16)getc(IN); - header->biBitCount |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); - - if(header->biSize >= 40U) { - header->biCompression = (OPJ_UINT32)getc(IN); - header->biCompression |= (OPJ_UINT32)getc(IN) << 8; - header->biCompression |= (OPJ_UINT32)getc(IN) << 16; - header->biCompression |= (OPJ_UINT32)getc(IN) << 24; - - header->biSizeImage = (OPJ_UINT32)getc(IN); - header->biSizeImage |= (OPJ_UINT32)getc(IN) << 8; - header->biSizeImage |= (OPJ_UINT32)getc(IN) << 16; - header->biSizeImage |= (OPJ_UINT32)getc(IN) << 24; - - header->biXpelsPerMeter = (OPJ_UINT32)getc(IN); - header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8; - header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16; - header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24; - - header->biYpelsPerMeter = (OPJ_UINT32)getc(IN); - header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8; - header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16; - header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24; - - header->biClrUsed = (OPJ_UINT32)getc(IN); - header->biClrUsed |= (OPJ_UINT32)getc(IN) << 8; - header->biClrUsed |= (OPJ_UINT32)getc(IN) << 16; - header->biClrUsed |= (OPJ_UINT32)getc(IN) << 24; - - header->biClrImportant = (OPJ_UINT32)getc(IN); - header->biClrImportant |= (OPJ_UINT32)getc(IN) << 8; - header->biClrImportant |= (OPJ_UINT32)getc(IN) << 16; - header->biClrImportant |= (OPJ_UINT32)getc(IN) << 24; - } - - if(header->biSize >= 56U) { - header->biRedMask = (OPJ_UINT32)getc(IN); - header->biRedMask |= (OPJ_UINT32)getc(IN) << 8; - header->biRedMask |= (OPJ_UINT32)getc(IN) << 16; - header->biRedMask |= (OPJ_UINT32)getc(IN) << 24; - - header->biGreenMask = (OPJ_UINT32)getc(IN); - header->biGreenMask |= (OPJ_UINT32)getc(IN) << 8; - header->biGreenMask |= (OPJ_UINT32)getc(IN) << 16; - header->biGreenMask |= (OPJ_UINT32)getc(IN) << 24; - - header->biBlueMask = (OPJ_UINT32)getc(IN); - header->biBlueMask |= (OPJ_UINT32)getc(IN) << 8; - header->biBlueMask |= (OPJ_UINT32)getc(IN) << 16; - header->biBlueMask |= (OPJ_UINT32)getc(IN) << 24; - - header->biAlphaMask = (OPJ_UINT32)getc(IN); - header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 8; - header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 16; - header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 24; - } - - if(header->biSize >= 108U) { - header->biColorSpaceType = (OPJ_UINT32)getc(IN); - header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 8; - header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 16; - header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 24; - - if (fread(&(header->biColorSpaceEP), 1U, sizeof(header->biColorSpaceEP), IN) != sizeof(header->biColorSpaceEP)) { - fprintf(stderr,"Error, can't read BMP header\n"); - return OPJ_FALSE; - } - - header->biRedGamma = (OPJ_UINT32)getc(IN); - header->biRedGamma |= (OPJ_UINT32)getc(IN) << 8; - header->biRedGamma |= (OPJ_UINT32)getc(IN) << 16; - header->biRedGamma |= (OPJ_UINT32)getc(IN) << 24; - - header->biGreenGamma = (OPJ_UINT32)getc(IN); - header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 8; - header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 16; - header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 24; - - header->biBlueGamma = (OPJ_UINT32)getc(IN); - header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 8; - header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 16; - header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 24; - } - - if(header->biSize >= 124U) { - header->biIntent = (OPJ_UINT32)getc(IN); - header->biIntent |= (OPJ_UINT32)getc(IN) << 8; - header->biIntent |= (OPJ_UINT32)getc(IN) << 16; - header->biIntent |= (OPJ_UINT32)getc(IN) << 24; - - header->biIccProfileData = (OPJ_UINT32)getc(IN); - header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 8; - header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 16; - header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 24; - - header->biIccProfileSize = (OPJ_UINT32)getc(IN); - header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 8; - header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 16; - header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 24; - - header->biReserved = (OPJ_UINT32)getc(IN); - header->biReserved |= (OPJ_UINT32)getc(IN) << 8; - header->biReserved |= (OPJ_UINT32)getc(IN) << 16; - header->biReserved |= (OPJ_UINT32)getc(IN) << 24; - } - return OPJ_TRUE; + memset(header, 0, sizeof(*header)); + /* INFO HEADER */ + /* ------------- */ + header->biSize = (OPJ_UINT32)getc(IN); + header->biSize |= (OPJ_UINT32)getc(IN) << 8; + header->biSize |= (OPJ_UINT32)getc(IN) << 16; + header->biSize |= (OPJ_UINT32)getc(IN) << 24; + + switch (header->biSize) { + case 12U: /* BITMAPCOREHEADER */ + case 40U: /* BITMAPINFOHEADER */ + case 52U: /* BITMAPV2INFOHEADER */ + case 56U: /* BITMAPV3INFOHEADER */ + case 108U: /* BITMAPV4HEADER */ + case 124U: /* BITMAPV5HEADER */ + break; + default: + fprintf(stderr,"Error, unknown BMP header size %d\n", header->biSize); + return OPJ_FALSE; + } + + header->biWidth = (OPJ_UINT32)getc(IN); + header->biWidth |= (OPJ_UINT32)getc(IN) << 8; + header->biWidth |= (OPJ_UINT32)getc(IN) << 16; + header->biWidth |= (OPJ_UINT32)getc(IN) << 24; + + header->biHeight = (OPJ_UINT32)getc(IN); + header->biHeight |= (OPJ_UINT32)getc(IN) << 8; + header->biHeight |= (OPJ_UINT32)getc(IN) << 16; + header->biHeight |= (OPJ_UINT32)getc(IN) << 24; + + header->biPlanes = (OPJ_UINT16)getc(IN); + header->biPlanes |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); + + header->biBitCount = (OPJ_UINT16)getc(IN); + header->biBitCount |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8); + + if(header->biSize >= 40U) { + header->biCompression = (OPJ_UINT32)getc(IN); + header->biCompression |= (OPJ_UINT32)getc(IN) << 8; + header->biCompression |= (OPJ_UINT32)getc(IN) << 16; + header->biCompression |= (OPJ_UINT32)getc(IN) << 24; + + header->biSizeImage = (OPJ_UINT32)getc(IN); + header->biSizeImage |= (OPJ_UINT32)getc(IN) << 8; + header->biSizeImage |= (OPJ_UINT32)getc(IN) << 16; + header->biSizeImage |= (OPJ_UINT32)getc(IN) << 24; + + header->biXpelsPerMeter = (OPJ_UINT32)getc(IN); + header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8; + header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16; + header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24; + + header->biYpelsPerMeter = (OPJ_UINT32)getc(IN); + header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8; + header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16; + header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24; + + header->biClrUsed = (OPJ_UINT32)getc(IN); + header->biClrUsed |= (OPJ_UINT32)getc(IN) << 8; + header->biClrUsed |= (OPJ_UINT32)getc(IN) << 16; + header->biClrUsed |= (OPJ_UINT32)getc(IN) << 24; + + header->biClrImportant = (OPJ_UINT32)getc(IN); + header->biClrImportant |= (OPJ_UINT32)getc(IN) << 8; + header->biClrImportant |= (OPJ_UINT32)getc(IN) << 16; + header->biClrImportant |= (OPJ_UINT32)getc(IN) << 24; + } + + if(header->biSize >= 56U) { + header->biRedMask = (OPJ_UINT32)getc(IN); + header->biRedMask |= (OPJ_UINT32)getc(IN) << 8; + header->biRedMask |= (OPJ_UINT32)getc(IN) << 16; + header->biRedMask |= (OPJ_UINT32)getc(IN) << 24; + + header->biGreenMask = (OPJ_UINT32)getc(IN); + header->biGreenMask |= (OPJ_UINT32)getc(IN) << 8; + header->biGreenMask |= (OPJ_UINT32)getc(IN) << 16; + header->biGreenMask |= (OPJ_UINT32)getc(IN) << 24; + + header->biBlueMask = (OPJ_UINT32)getc(IN); + header->biBlueMask |= (OPJ_UINT32)getc(IN) << 8; + header->biBlueMask |= (OPJ_UINT32)getc(IN) << 16; + header->biBlueMask |= (OPJ_UINT32)getc(IN) << 24; + + header->biAlphaMask = (OPJ_UINT32)getc(IN); + header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 8; + header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 16; + header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 24; + } + + if(header->biSize >= 108U) { + header->biColorSpaceType = (OPJ_UINT32)getc(IN); + header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 8; + header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 16; + header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 24; + + if (fread(&(header->biColorSpaceEP), 1U, sizeof(header->biColorSpaceEP), IN) != sizeof(header->biColorSpaceEP)) { + fprintf(stderr,"Error, can't read BMP header\n"); + return OPJ_FALSE; + } + + header->biRedGamma = (OPJ_UINT32)getc(IN); + header->biRedGamma |= (OPJ_UINT32)getc(IN) << 8; + header->biRedGamma |= (OPJ_UINT32)getc(IN) << 16; + header->biRedGamma |= (OPJ_UINT32)getc(IN) << 24; + + header->biGreenGamma = (OPJ_UINT32)getc(IN); + header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 8; + header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 16; + header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 24; + + header->biBlueGamma = (OPJ_UINT32)getc(IN); + header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 8; + header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 16; + header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 24; + } + + if(header->biSize >= 124U) { + header->biIntent = (OPJ_UINT32)getc(IN); + header->biIntent |= (OPJ_UINT32)getc(IN) << 8; + header->biIntent |= (OPJ_UINT32)getc(IN) << 16; + header->biIntent |= (OPJ_UINT32)getc(IN) << 24; + + header->biIccProfileData = (OPJ_UINT32)getc(IN); + header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 8; + header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 16; + header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 24; + + header->biIccProfileSize = (OPJ_UINT32)getc(IN); + header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 8; + header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 16; + header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 24; + + header->biReserved = (OPJ_UINT32)getc(IN); + header->biReserved |= (OPJ_UINT32)getc(IN) << 8; + header->biReserved |= (OPJ_UINT32)getc(IN) << 16; + header->biReserved |= (OPJ_UINT32)getc(IN) << 24; + } + return OPJ_TRUE; } static OPJ_BOOL bmp_read_raw_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height) { - OPJ_ARG_NOT_USED(width); - - if ( fread(pData, sizeof(OPJ_UINT8), stride * height, IN) != (stride * height) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return OPJ_FALSE; - } - return OPJ_TRUE; + OPJ_ARG_NOT_USED(width); + + if ( fread(pData, sizeof(OPJ_UINT8), stride * height, IN) != (stride * height) ) { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return OPJ_FALSE; + } + return OPJ_TRUE; } static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height) { - OPJ_UINT32 x, y; - OPJ_UINT8 *pix; - const OPJ_UINT8 *beyond; - - beyond = pData + stride * height; - pix = pData; - - x = y = 0U; - while (y < height) - { - int c = getc(IN); - - if (c) { - int j; - OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN); - - for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { - *pix = c1; - } - } - else { - c = getc(IN); - if (c == 0x00) { /* EOL */ - x = 0; - ++y; - pix = pData + y * stride + x; - } - else if (c == 0x01) { /* EOP */ - break; - } - else if (c == 0x02) { /* MOVE by dxdy */ - c = getc(IN); - x += (OPJ_UINT32)c; - c = getc(IN); - y += (OPJ_UINT32)c; - pix = pData + y * stride + x; - } - else /* 03 .. 255 */ - { - int j; - for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) - { - OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN); - *pix = c1; - } - if ((OPJ_UINT32)c & 1U) { /* skip padding byte */ - getc(IN); - } - } - } - }/* while() */ - return OPJ_TRUE; + OPJ_UINT32 x, y; + OPJ_UINT8 *pix; + const OPJ_UINT8 *beyond; + + beyond = pData + stride * height; + pix = pData; + + x = y = 0U; + while (y < height) { + int c = getc(IN); + + if (c) { + int j; + OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN); + + for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { + *pix = c1; + } + } else { + c = getc(IN); + if (c == 0x00) { /* EOL */ + x = 0; + ++y; + pix = pData + y * stride + x; + } else if (c == 0x01) { /* EOP */ + break; + } else if (c == 0x02) { /* MOVE by dxdy */ + c = getc(IN); + x += (OPJ_UINT32)c; + c = getc(IN); + y += (OPJ_UINT32)c; + pix = pData + y * stride + x; + } else { /* 03 .. 255 */ + int j; + for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { + OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN); + *pix = c1; + } + if ((OPJ_UINT32)c & 1U) { /* skip padding byte */ + getc(IN); + } + } + } + }/* while() */ + return OPJ_TRUE; } static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height) { - OPJ_UINT32 x, y; - OPJ_UINT8 *pix; - const OPJ_UINT8 *beyond; - - beyond = pData + stride * height; - pix = pData; - x = y = 0U; - while(y < height) - { - int c = getc(IN); - if(c == EOF) break; - - if(c) {/* encoded mode */ - int j; - OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN); - - for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { - *pix = (OPJ_UINT8)((j&1) ? (c1 & 0x0fU) : ((c1>>4)&0x0fU)); - } - } - else { /* absolute mode */ - c = getc(IN); - if(c == EOF) break; - - if(c == 0x00) { /* EOL */ - x = 0; y++; pix = pData + y * stride; - } - else if(c == 0x01) { /* EOP */ - break; - } - else if(c == 0x02) { /* MOVE by dxdy */ - c = getc(IN); x += (OPJ_UINT32)c; - c = getc(IN); y += (OPJ_UINT32)c; - pix = pData + y * stride + x; - } - else { /* 03 .. 255 : absolute mode */ - int j; - OPJ_UINT8 c1 = 0U; - - for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { - if((j&1) == 0) { - c1 = (OPJ_UINT8)getc(IN); - } - *pix = (OPJ_UINT8)((j&1) ? (c1 & 0x0fU) : ((c1>>4)&0x0fU)); - } - if(((c&3) == 1) || ((c&3) == 2)) { /* skip padding byte */ - getc(IN); - } - } - } - } /* while(y < height) */ - return OPJ_TRUE; + OPJ_UINT32 x, y; + OPJ_UINT8 *pix; + const OPJ_UINT8 *beyond; + + beyond = pData + stride * height; + pix = pData; + x = y = 0U; + while(y < height) { + int c = getc(IN); + if(c == EOF) break; + + if(c) {/* encoded mode */ + int j; + OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN); + + for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { + *pix = (OPJ_UINT8)((j&1) ? (c1 & 0x0fU) : ((c1>>4)&0x0fU)); + } + } else { /* absolute mode */ + c = getc(IN); + if(c == EOF) break; + + if(c == 0x00) { /* EOL */ + x = 0; + y++; + pix = pData + y * stride; + } else if(c == 0x01) { /* EOP */ + break; + } else if(c == 0x02) { /* MOVE by dxdy */ + c = getc(IN); + x += (OPJ_UINT32)c; + c = getc(IN); + y += (OPJ_UINT32)c; + pix = pData + y * stride + x; + } else { /* 03 .. 255 : absolute mode */ + int j; + OPJ_UINT8 c1 = 0U; + + for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { + if((j&1) == 0) { + c1 = (OPJ_UINT8)getc(IN); + } + *pix = (OPJ_UINT8)((j&1) ? (c1 & 0x0fU) : ((c1>>4)&0x0fU)); + } + if(((c&3) == 1) || ((c&3) == 2)) { /* skip padding byte */ + getc(IN); + } + } + } + } /* while(y < height) */ + return OPJ_TRUE; } opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) { - opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */ - OPJ_UINT8 lut_R[256], lut_G[256], lut_B[256]; - OPJ_UINT8 const* pLUT[3]; - opj_image_t * image = NULL; - FILE *IN; - OPJ_BITMAPFILEHEADER File_h; - OPJ_BITMAPINFOHEADER Info_h; - OPJ_UINT32 i, palette_len, numcmpts = 1U; - OPJ_BOOL l_result = OPJ_FALSE; - OPJ_UINT8* pData = NULL; - OPJ_UINT32 stride; - - pLUT[0] = lut_R; pLUT[1] = lut_G; pLUT[2] = lut_B; - - IN = fopen(filename, "rb"); - if (!IN) - { - fprintf(stderr, "Failed to open %s for reading !!\n", filename); - return NULL; - } - - if (!bmp_read_file_header(IN, &File_h)) { - fclose(IN); - return NULL; - } - if (!bmp_read_info_header(IN, &Info_h)) { - fclose(IN); - return NULL; - } - - /* Load palette */ - if (Info_h.biBitCount <= 8U) - { - memset(&lut_R[0], 0, sizeof(lut_R)); - memset(&lut_G[0], 0, sizeof(lut_G)); - memset(&lut_B[0], 0, sizeof(lut_B)); - - palette_len = Info_h.biClrUsed; - if((palette_len == 0U) && (Info_h.biBitCount <= 8U)) { - palette_len = (1U << Info_h.biBitCount); - } - if (palette_len > 256U) { - palette_len = 256U; - } - if (palette_len > 0U) { - OPJ_UINT8 has_color = 0U; - for (i = 0U; i < palette_len; i++) { - lut_B[i] = (OPJ_UINT8)getc(IN); - lut_G[i] = (OPJ_UINT8)getc(IN); - lut_R[i] = (OPJ_UINT8)getc(IN); - (void)getc(IN); /* padding */ - has_color |= (lut_B[i] ^ lut_G[i]) | (lut_G[i] ^ lut_R[i]); - } - if(has_color) { - numcmpts = 3U; - } - } - } else { - numcmpts = 3U; - if ((Info_h.biCompression == 3) && (Info_h.biAlphaMask != 0U)) { - numcmpts++; - } - } - - stride = ((Info_h.biWidth * Info_h.biBitCount + 31U) / 32U) * 4U; /* rows are aligned on 32bits */ - if (Info_h.biBitCount == 4 && Info_h.biCompression == 2) { /* RLE 4 gets decoded as 8 bits data for now... */ - stride = ((Info_h.biWidth * 8U + 31U) / 32U) * 4U; - } - pData = (OPJ_UINT8 *) calloc(1, stride * Info_h.biHeight * sizeof(OPJ_UINT8)); - if (pData == NULL) { - fclose(IN); - return NULL; - } - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, (long)File_h.bfOffBits, SEEK_SET); - - switch (Info_h.biCompression) { - case 0: - case 3: - /* read raw data */ - l_result = bmp_read_raw_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight); - break; - case 1: - /* read rle8 data */ - l_result = bmp_read_rle8_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight); - break; - case 2: - /* read rle4 data */ - l_result = bmp_read_rle4_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight); - break; - default: - fprintf(stderr, "Unsupported BMP compression\n"); - l_result = OPJ_FALSE; - break; - } - if (!l_result) { - free(pData); - fclose(IN); - return NULL; - } - - /* create the image */ - memset(&cmptparm[0], 0, sizeof(cmptparm)); - for(i = 0; i < 4U; i++) - { - cmptparm[i].prec = 8; - cmptparm[i].bpp = 8; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = (OPJ_UINT32)parameters->subsampling_dx; - cmptparm[i].dy = (OPJ_UINT32)parameters->subsampling_dy; - cmptparm[i].w = Info_h.biWidth; - cmptparm[i].h = Info_h.biHeight; - } - - image = opj_image_create(numcmpts, &cmptparm[0], (numcmpts == 1U) ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB); - if(!image) { - fclose(IN); - free(pData); - return NULL; - } - if (numcmpts == 4U) { - image->comps[3].alpha = 1; - } - - /* set image offset and reference grid */ - image->x0 = (OPJ_UINT32)parameters->image_offset_x0; - image->y0 = (OPJ_UINT32)parameters->image_offset_y0; - image->x1 = image->x0 + (Info_h.biWidth - 1U) * (OPJ_UINT32)parameters->subsampling_dx + 1U; - image->y1 = image->y0 + (Info_h.biHeight - 1U) * (OPJ_UINT32)parameters->subsampling_dy + 1U; - - /* Read the data */ - if (Info_h.biBitCount == 24 && Info_h.biCompression == 0) { /*RGB */ - bmp24toimage(pData, stride, image); - } - else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /* RGB 8bpp Indexed */ - bmp8toimage(pData, stride, image, pLUT); - } - else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/ - bmp8toimage(pData, stride, image, pLUT); - } - else if (Info_h.biBitCount == 4 && Info_h.biCompression == 2) { /*RLE4*/ - bmp8toimage(pData, stride, image, pLUT); /* RLE 4 gets decoded as 8 bits data for now */ - } - else if (Info_h.biBitCount == 32 && Info_h.biCompression == 0) { /* RGBX */ - bmpmask32toimage(pData, stride, image, 0x00FF0000U, 0x0000FF00U, 0x000000FFU, 0x00000000U); - } - else if (Info_h.biBitCount == 32 && Info_h.biCompression == 3) { /* bitmask */ - bmpmask32toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask); - } - else if (Info_h.biBitCount == 16 && Info_h.biCompression == 0) { /* RGBX */ - bmpmask16toimage(pData, stride, image, 0x7C00U, 0x03E0U, 0x001FU, 0x0000U); - } - else if (Info_h.biBitCount == 16 && Info_h.biCompression == 3) { /* bitmask */ - if ((Info_h.biRedMask == 0U) && (Info_h.biGreenMask == 0U) && (Info_h.biBlueMask == 0U)) { - Info_h.biRedMask = 0xF800U; - Info_h.biGreenMask = 0x07E0U; - Info_h.biBlueMask = 0x001FU; - } - bmpmask16toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask); - } - else { - opj_image_destroy(image); - image = NULL; - fprintf(stderr, "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount); - } - free(pData); - fclose(IN); - return image; + opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */ + OPJ_UINT8 lut_R[256], lut_G[256], lut_B[256]; + OPJ_UINT8 const* pLUT[3]; + opj_image_t * image = NULL; + FILE *IN; + OPJ_BITMAPFILEHEADER File_h; + OPJ_BITMAPINFOHEADER Info_h; + OPJ_UINT32 i, palette_len, numcmpts = 1U; + OPJ_BOOL l_result = OPJ_FALSE; + OPJ_UINT8* pData = NULL; + OPJ_UINT32 stride; + + pLUT[0] = lut_R; + pLUT[1] = lut_G; + pLUT[2] = lut_B; + + IN = fopen(filename, "rb"); + if (!IN) { + fprintf(stderr, "Failed to open %s for reading !!\n", filename); + return NULL; + } + + if (!bmp_read_file_header(IN, &File_h)) { + fclose(IN); + return NULL; + } + if (!bmp_read_info_header(IN, &Info_h)) { + fclose(IN); + return NULL; + } + + /* Load palette */ + if (Info_h.biBitCount <= 8U) { + memset(&lut_R[0], 0, sizeof(lut_R)); + memset(&lut_G[0], 0, sizeof(lut_G)); + memset(&lut_B[0], 0, sizeof(lut_B)); + + palette_len = Info_h.biClrUsed; + if((palette_len == 0U) && (Info_h.biBitCount <= 8U)) { + palette_len = (1U << Info_h.biBitCount); + } + if (palette_len > 256U) { + palette_len = 256U; + } + if (palette_len > 0U) { + OPJ_UINT8 has_color = 0U; + for (i = 0U; i < palette_len; i++) { + lut_B[i] = (OPJ_UINT8)getc(IN); + lut_G[i] = (OPJ_UINT8)getc(IN); + lut_R[i] = (OPJ_UINT8)getc(IN); + (void)getc(IN); /* padding */ + has_color |= (lut_B[i] ^ lut_G[i]) | (lut_G[i] ^ lut_R[i]); + } + if(has_color) { + numcmpts = 3U; + } + } + } else { + numcmpts = 3U; + if ((Info_h.biCompression == 3) && (Info_h.biAlphaMask != 0U)) { + numcmpts++; + } + } + + stride = ((Info_h.biWidth * Info_h.biBitCount + 31U) / 32U) * 4U; /* rows are aligned on 32bits */ + if (Info_h.biBitCount == 4 && Info_h.biCompression == 2) { /* RLE 4 gets decoded as 8 bits data for now... */ + stride = ((Info_h.biWidth * 8U + 31U) / 32U) * 4U; + } + pData = (OPJ_UINT8 *) calloc(1, stride * Info_h.biHeight * sizeof(OPJ_UINT8)); + if (pData == NULL) { + fclose(IN); + return NULL; + } + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, (long)File_h.bfOffBits, SEEK_SET); + + switch (Info_h.biCompression) { + case 0: + case 3: + /* read raw data */ + l_result = bmp_read_raw_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight); + break; + case 1: + /* read rle8 data */ + l_result = bmp_read_rle8_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight); + break; + case 2: + /* read rle4 data */ + l_result = bmp_read_rle4_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight); + break; + default: + fprintf(stderr, "Unsupported BMP compression\n"); + l_result = OPJ_FALSE; + break; + } + if (!l_result) { + free(pData); + fclose(IN); + return NULL; + } + + /* create the image */ + memset(&cmptparm[0], 0, sizeof(cmptparm)); + for(i = 0; i < 4U; i++) { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)parameters->subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)parameters->subsampling_dy; + cmptparm[i].w = Info_h.biWidth; + cmptparm[i].h = Info_h.biHeight; + } + + image = opj_image_create(numcmpts, &cmptparm[0], (numcmpts == 1U) ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB); + if(!image) { + fclose(IN); + free(pData); + return NULL; + } + if (numcmpts == 4U) { + image->comps[3].alpha = 1; + } + + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = image->x0 + (Info_h.biWidth - 1U) * (OPJ_UINT32)parameters->subsampling_dx + 1U; + image->y1 = image->y0 + (Info_h.biHeight - 1U) * (OPJ_UINT32)parameters->subsampling_dy + 1U; + + /* Read the data */ + if (Info_h.biBitCount == 24 && Info_h.biCompression == 0) { /*RGB */ + bmp24toimage(pData, stride, image); + } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /* RGB 8bpp Indexed */ + bmp8toimage(pData, stride, image, pLUT); + } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/ + bmp8toimage(pData, stride, image, pLUT); + } else if (Info_h.biBitCount == 4 && Info_h.biCompression == 2) { /*RLE4*/ + bmp8toimage(pData, stride, image, pLUT); /* RLE 4 gets decoded as 8 bits data for now */ + } else if (Info_h.biBitCount == 32 && Info_h.biCompression == 0) { /* RGBX */ + bmpmask32toimage(pData, stride, image, 0x00FF0000U, 0x0000FF00U, 0x000000FFU, 0x00000000U); + } else if (Info_h.biBitCount == 32 && Info_h.biCompression == 3) { /* bitmask */ + bmpmask32toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask); + } else if (Info_h.biBitCount == 16 && Info_h.biCompression == 0) { /* RGBX */ + bmpmask16toimage(pData, stride, image, 0x7C00U, 0x03E0U, 0x001FU, 0x0000U); + } else if (Info_h.biBitCount == 16 && Info_h.biCompression == 3) { /* bitmask */ + if ((Info_h.biRedMask == 0U) && (Info_h.biGreenMask == 0U) && (Info_h.biBlueMask == 0U)) { + Info_h.biRedMask = 0xF800U; + Info_h.biGreenMask = 0x07E0U; + Info_h.biBlueMask = 0x001FU; + } + bmpmask16toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask); + } else { + opj_image_destroy(image); + image = NULL; + fprintf(stderr, "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount); + } + free(pData); + fclose(IN); + return image; } -int imagetobmp(opj_image_t * image, const char *outfile) { +int imagetobmp(opj_image_t * image, const char *outfile) +{ int w, h; int i, pad; FILE *fdest = NULL; @@ -850,20 +829,17 @@ int imagetobmp(opj_image_t * image, const char *outfile) { if (image->comps[0].prec > 8) { adjustR = (int)image->comps[0].prec - 8; printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); - } - else + } else adjustR = 0; if (image->comps[1].prec > 8) { adjustG = (int)image->comps[1].prec - 8; printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec); - } - else + } else adjustG = 0; if (image->comps[2].prec > 8) { adjustB = (int)image->comps[2].prec - 8; printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec); - } - else + } else adjustB = 0; for (i = 0; i < w * h; i++) { @@ -873,19 +849,22 @@ int imagetobmp(opj_image_t * image, const char *outfile) { r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); r = ((r >> adjustR)+((r >> (adjustR-1))%2)); - if(r > 255) r = 255; else if(r < 0) r = 0; + if(r > 255) r = 255; + else if(r < 0) r = 0; rc = (OPJ_UINT8)r; g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); g = ((g >> adjustG)+((g >> (adjustG-1))%2)); - if(g > 255) g = 255; else if(g < 0) g = 0; + if(g > 255) g = 255; + else if(g < 0) g = 0; gc = (OPJ_UINT8)g; b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); b = ((b >> adjustB)+((b >> (adjustB-1))%2)); - if(b > 255) b = 255; else if(b < 0) b = 0; + if(b > 255) b = 255; + else if(b < 0) b = 0; bc = (OPJ_UINT8)b; fprintf(fdest, "%c%c%c", bc, gc, rc); @@ -949,7 +928,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) { if (image->comps[0].prec > 8) { adjustR = (int)image->comps[0].prec - 8; printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); - }else + } else adjustR = 0; for (i = 0; i < 256; i++) { @@ -962,7 +941,8 @@ int imagetobmp(opj_image_t * image, const char *outfile) { r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); r = ((r >> adjustR)+((r >> (adjustR-1))%2)); - if(r > 255) r = 255; else if(r < 0) r = 0; + if(r > 255) r = 255; + else if(r < 0) r = 0; fprintf(fdest, "%c", (OPJ_UINT8)r); diff --git a/src/bin/jp2/convertpng.c b/src/bin/jp2/convertpng.c index 8d117412..10531028 100644 --- a/src/bin/jp2/convertpng.c +++ b/src/bin/jp2/convertpng.c @@ -56,437 +56,422 @@ static void convert_16u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < length; i++) { - OPJ_INT32 val0 = *pSrc++; - OPJ_INT32 val1 = *pSrc++; - pDst[i] = val0 << 8 | val1; - } + OPJ_SIZE_T i; + for (i = 0; i < length; i++) { + OPJ_INT32 val0 = *pSrc++; + OPJ_INT32 val1 = *pSrc++; + pDst[i] = val0 << 8 | val1; + } } opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params) { - png_structp png = NULL; - png_infop info = NULL; - double gamma; - int bit_depth, interlace_type,compression_type, filter_type; - OPJ_UINT32 i; - png_uint_32 width, height = 0U; - int color_type; - FILE *reader = NULL; - OPJ_BYTE** rows = NULL; - OPJ_INT32* row32s = NULL; - /* j2k: */ - opj_image_t *image = NULL; - opj_image_cmptparm_t cmptparm[4]; - OPJ_UINT32 nr_comp; - OPJ_BYTE sigbuf[8]; - convert_XXx32s_C1R cvtXXTo32s = NULL; - convert_32s_CXPX cvtCxToPx = NULL; - OPJ_INT32* planes[4]; - - if((reader = fopen(read_idf, "rb")) == NULL) - { - fprintf(stderr,"pngtoimage: can not open %s\n",read_idf); - return NULL; - } - - if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE - || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) - { - fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf); - goto fin; - } - - if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, - NULL, NULL, NULL)) == NULL) - goto fin; - if((info = png_create_info_struct(png)) == NULL) - goto fin; - - if(setjmp(png_jmpbuf(png))) - goto fin; - - png_init_io(png, reader); - png_set_sig_bytes(png, MAGIC_SIZE); - - png_read_info(png, info); - - if(png_get_IHDR(png, info, &width, &height, - &bit_depth, &color_type, &interlace_type, - &compression_type, &filter_type) == 0) - goto fin; - - /* png_set_expand(): - * expand paletted images to RGB, expand grayscale images of - * less than 8-bit depth to 8-bit depth, and expand tRNS chunks - * to alpha channels. - */ - if(color_type == PNG_COLOR_TYPE_PALETTE) { - png_set_expand(png); - } - - if(png_get_valid(png, info, PNG_INFO_tRNS)) { - png_set_expand(png); - } - /* We might wan't to expand background */ - /* - if(png_get_valid(png, info, PNG_INFO_bKGD)) { - png_color_16p bgnd; - png_get_bKGD(png, info, &bgnd); - png_set_background(png, bgnd, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); - } - */ - - if( !png_get_gAMA(png, info, &gamma)) - gamma = 1.0; - - /* we're not displaying but converting, screen gamma == 1.0 */ - png_set_gamma(png, 1.0, gamma); - - png_read_update_info(png, info); - - color_type = png_get_color_type(png, info); - - switch (color_type) { - case PNG_COLOR_TYPE_GRAY: - nr_comp = 1; - break; - case PNG_COLOR_TYPE_GRAY_ALPHA: - nr_comp = 2; - break; - case PNG_COLOR_TYPE_RGB: - nr_comp = 3; - break; - case PNG_COLOR_TYPE_RGB_ALPHA: - nr_comp = 4; - break; - default: - fprintf(stderr,"pngtoimage: colortype %d is not supported\n", color_type); - goto fin; - } - cvtCxToPx = convert_32s_CXPX_LUT[nr_comp]; - bit_depth = png_get_bit_depth(png, info); - - switch (bit_depth) { - case 1: - case 2: - case 4: - case 8: - cvtXXTo32s = convert_XXu32s_C1R_LUT[bit_depth]; - break; - case 16: /* 16 bpp is specific to PNG */ - cvtXXTo32s = convert_16u32s_C1R; - break; - default: - fprintf(stderr,"pngtoimage: bit depth %d is not supported\n", bit_depth); - goto fin; - } - - - rows = (OPJ_BYTE**)calloc(height+1, sizeof(OPJ_BYTE*)); - for(i = 0; i < height; ++i) - rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png,info)); - - png_read_image(png, rows); - - /* Create image */ - memset(cmptparm, 0, sizeof(cmptparm)); - for(i = 0; i < nr_comp; ++i) - { - cmptparm[i].prec = (OPJ_UINT32)bit_depth; - /* bits_per_pixel: 8 or 16 */ - cmptparm[i].bpp = (OPJ_UINT32)bit_depth; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = (OPJ_UINT32)params->subsampling_dx; - cmptparm[i].dy = (OPJ_UINT32)params->subsampling_dy; - cmptparm[i].w = (OPJ_UINT32)width; - cmptparm[i].h = (OPJ_UINT32)height; - } - - image = opj_image_create(nr_comp, &cmptparm[0], (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY); - if(image == NULL) goto fin; - image->x0 = (OPJ_UINT32)params->image_offset_x0; - image->y0 = (OPJ_UINT32)params->image_offset_y0; - image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32)params->subsampling_dx + 1 + image->x0); - image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)params->subsampling_dy + 1 + image->y0); - - row32s = (OPJ_INT32 *)malloc((size_t)width * nr_comp * sizeof(OPJ_INT32)); - if(row32s == NULL) goto fin; - - /* Set alpha channel */ - image->comps[nr_comp-1U].alpha = 1U - (nr_comp & 1U); - - for(i = 0; i < nr_comp; i++) - { - planes[i] = image->comps[i].data; - } - - for(i = 0; i < height; ++i) - { - cvtXXTo32s(rows[i], row32s, (OPJ_SIZE_T)width * nr_comp); - cvtCxToPx(row32s, planes, width); - planes[0] += width; - planes[1] += width; - planes[2] += width; - planes[3] += width; - } + png_structp png = NULL; + png_infop info = NULL; + double gamma; + int bit_depth, interlace_type,compression_type, filter_type; + OPJ_UINT32 i; + png_uint_32 width, height = 0U; + int color_type; + FILE *reader = NULL; + OPJ_BYTE** rows = NULL; + OPJ_INT32* row32s = NULL; + /* j2k: */ + opj_image_t *image = NULL; + opj_image_cmptparm_t cmptparm[4]; + OPJ_UINT32 nr_comp; + OPJ_BYTE sigbuf[8]; + convert_XXx32s_C1R cvtXXTo32s = NULL; + convert_32s_CXPX cvtCxToPx = NULL; + OPJ_INT32* planes[4]; + + if((reader = fopen(read_idf, "rb")) == NULL) { + fprintf(stderr,"pngtoimage: can not open %s\n",read_idf); + return NULL; + } + + if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE + || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) { + fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf); + goto fin; + } + + if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, + NULL, NULL, NULL)) == NULL) + goto fin; + if((info = png_create_info_struct(png)) == NULL) + goto fin; + + if(setjmp(png_jmpbuf(png))) + goto fin; + + png_init_io(png, reader); + png_set_sig_bytes(png, MAGIC_SIZE); + + png_read_info(png, info); + + if(png_get_IHDR(png, info, &width, &height, + &bit_depth, &color_type, &interlace_type, + &compression_type, &filter_type) == 0) + goto fin; + + /* png_set_expand(): + * expand paletted images to RGB, expand grayscale images of + * less than 8-bit depth to 8-bit depth, and expand tRNS chunks + * to alpha channels. + */ + if(color_type == PNG_COLOR_TYPE_PALETTE) { + png_set_expand(png); + } + + if(png_get_valid(png, info, PNG_INFO_tRNS)) { + png_set_expand(png); + } + /* We might wan't to expand background */ + /* + if(png_get_valid(png, info, PNG_INFO_bKGD)) { + png_color_16p bgnd; + png_get_bKGD(png, info, &bgnd); + png_set_background(png, bgnd, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); + } + */ + + if( !png_get_gAMA(png, info, &gamma)) + gamma = 1.0; + + /* we're not displaying but converting, screen gamma == 1.0 */ + png_set_gamma(png, 1.0, gamma); + + png_read_update_info(png, info); + + color_type = png_get_color_type(png, info); + + switch (color_type) { + case PNG_COLOR_TYPE_GRAY: + nr_comp = 1; + break; + case PNG_COLOR_TYPE_GRAY_ALPHA: + nr_comp = 2; + break; + case PNG_COLOR_TYPE_RGB: + nr_comp = 3; + break; + case PNG_COLOR_TYPE_RGB_ALPHA: + nr_comp = 4; + break; + default: + fprintf(stderr,"pngtoimage: colortype %d is not supported\n", color_type); + goto fin; + } + cvtCxToPx = convert_32s_CXPX_LUT[nr_comp]; + bit_depth = png_get_bit_depth(png, info); + + switch (bit_depth) { + case 1: + case 2: + case 4: + case 8: + cvtXXTo32s = convert_XXu32s_C1R_LUT[bit_depth]; + break; + case 16: /* 16 bpp is specific to PNG */ + cvtXXTo32s = convert_16u32s_C1R; + break; + default: + fprintf(stderr,"pngtoimage: bit depth %d is not supported\n", bit_depth); + goto fin; + } + + + rows = (OPJ_BYTE**)calloc(height+1, sizeof(OPJ_BYTE*)); + for(i = 0; i < height; ++i) + rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png,info)); + + png_read_image(png, rows); + + /* Create image */ + memset(cmptparm, 0, sizeof(cmptparm)); + for(i = 0; i < nr_comp; ++i) { + cmptparm[i].prec = (OPJ_UINT32)bit_depth; + /* bits_per_pixel: 8 or 16 */ + cmptparm[i].bpp = (OPJ_UINT32)bit_depth; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)params->subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)params->subsampling_dy; + cmptparm[i].w = (OPJ_UINT32)width; + cmptparm[i].h = (OPJ_UINT32)height; + } + + image = opj_image_create(nr_comp, &cmptparm[0], (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY); + if(image == NULL) goto fin; + image->x0 = (OPJ_UINT32)params->image_offset_x0; + image->y0 = (OPJ_UINT32)params->image_offset_y0; + image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32)params->subsampling_dx + 1 + image->x0); + image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)params->subsampling_dy + 1 + image->y0); + + row32s = (OPJ_INT32 *)malloc((size_t)width * nr_comp * sizeof(OPJ_INT32)); + if(row32s == NULL) goto fin; + + /* Set alpha channel */ + image->comps[nr_comp-1U].alpha = 1U - (nr_comp & 1U); + + for(i = 0; i < nr_comp; i++) { + planes[i] = image->comps[i].data; + } + + for(i = 0; i < height; ++i) { + cvtXXTo32s(rows[i], row32s, (OPJ_SIZE_T)width * nr_comp); + cvtCxToPx(row32s, planes, width); + planes[0] += width; + planes[1] += width; + planes[2] += width; + planes[3] += width; + } fin: - if(rows) - { - for(i = 0; i < height; ++i) - free(rows[i]); - free(rows); - } - if (row32s) { - free(row32s); - } - if(png) - png_destroy_read_struct(&png, &info, NULL); - - fclose(reader); - - return image; - + if(rows) { + for(i = 0; i < height; ++i) + free(rows[i]); + free(rows); + } + if (row32s) { + free(row32s); + } + if(png) + png_destroy_read_struct(&png, &info, NULL); + + fclose(reader); + + return image; + }/* pngtoimage() */ static void convert_32s16u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < length; i++) { - OPJ_UINT32 val = (OPJ_UINT32)pSrc[i]; - *pDst++ = (OPJ_BYTE)(val >> 8); - *pDst++ = (OPJ_BYTE)val; - } + OPJ_SIZE_T i; + for (i = 0; i < length; i++) { + OPJ_UINT32 val = (OPJ_UINT32)pSrc[i]; + *pDst++ = (OPJ_BYTE)(val >> 8); + *pDst++ = (OPJ_BYTE)val; + } } int imagetopng(opj_image_t * image, const char *write_idf) { - FILE * volatile writer = NULL; - png_structp png = NULL; - png_infop info = NULL; - png_bytep volatile row_buf = NULL; - int nr_comp, color_type; - volatile int prec; - png_color_8 sig_bit; - OPJ_INT32 const* planes[4]; - int i; - OPJ_INT32* volatile buffer32s = NULL; - - volatile int fails = 1; - - memset(&sig_bit, 0, sizeof(sig_bit)); - prec = (int)image->comps[0].prec; - planes[0] = image->comps[0].data; - nr_comp = (int)image->numcomps; - - if (nr_comp > 4) { - nr_comp = 4; - } - for (i = 1; i < nr_comp; ++i) { - if (image->comps[0].dx != image->comps[i].dx) { - break; - } - if (image->comps[0].dy != image->comps[i].dy) { - break; - } - if (image->comps[0].prec != image->comps[i].prec) { - break; - } - if (image->comps[0].sgnd != image->comps[i].sgnd) { - break; - } - planes[i] = image->comps[i].data; - } - if (i != nr_comp) { - fprintf(stderr,"imagetopng: All components shall have the same subsampling, same bit depth, same sign.\n"); - fprintf(stderr,"\tAborting\n"); - return 1; - } - for (i = 0; i < nr_comp; ++i) { - clip_component(&(image->comps[i]), image->comps[0].prec); - } - if(prec > 8 && prec < 16) - { - for (i = 0; i < nr_comp; ++i) { - scale_component(&(image->comps[i]), 16); - } - prec = 16; - } - else if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */ - { - for (i = 0; i < nr_comp; ++i) { - scale_component(&(image->comps[i]), 8); - } - prec = 8; - } else if((prec > 1) && (prec < 8) && ((prec == 6) || ((prec & 1)==1))) { /* GRAY with non native precision */ - if ((prec == 5) || (prec == 6)) { - prec = 8; - } else { - prec++; - } - for (i = 0; i < nr_comp; ++i) { - scale_component(&(image->comps[i]), (OPJ_UINT32)prec); - } - } - - if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) - { - fprintf(stderr,"imagetopng: can not create %s\n\twrong bit_depth %d\n", write_idf, prec); - return fails; - } - - writer = fopen(write_idf, "wb"); - - if(writer == NULL) return fails; - - /* Create and initialize the png_struct with the desired error handler - * functions. If you want to use the default stderr and longjump method, - * you can supply NULL for the last three parameters. We also check that - * the library version is compatible with the one used at compile time, - * in case we are using dynamically linked libraries. REQUIRED. - */ - png = png_create_write_struct(PNG_LIBPNG_VER_STRING, - NULL, NULL, NULL); - /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */ - - if(png == NULL) goto fin; - - /* Allocate/initialize the image information data. REQUIRED - */ - info = png_create_info_struct(png); - - if(info == NULL) goto fin; - - /* Set error handling. REQUIRED if you are not supplying your own - * error handling functions in the png_create_write_struct() call. - */ - if(setjmp(png_jmpbuf(png))) goto fin; - - /* I/O initialization functions is REQUIRED - */ - png_init_io(png, writer); - - /* Set the image information here. Width and height are up to 2^31, - * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on - * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, - * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, - * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or - * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST - * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. - * REQUIRED - * - * ERRORS: - * - * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8 - * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8 - * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8 - * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8 - * - */ - png_set_compression_level(png, Z_BEST_COMPRESSION); - - if(nr_comp >= 3) /* RGB(A) */ - { - color_type = PNG_COLOR_TYPE_RGB; - sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec; - } - else /* GRAY(A) */ - { - color_type = PNG_COLOR_TYPE_GRAY; - sig_bit.gray = (png_byte)prec; - } - if((nr_comp & 1) == 0) /* ALPHA */ - { - color_type |= PNG_COLOR_MASK_ALPHA; - sig_bit.alpha = (png_byte)prec; - } - - png_set_IHDR(png, info, image->comps[0].w, image->comps[0].h, prec, color_type, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - - png_set_sBIT(png, info, &sig_bit); - /* png_set_gamma(png, 2.2, 1./2.2); */ - /* png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); */ - png_write_info(png, info); - - /* setup conversion */ - { - OPJ_SIZE_T rowStride; - png_size_t png_row_size; - - png_row_size = png_get_rowbytes(png, info); - rowStride = ((OPJ_SIZE_T)image->comps[0].w * (OPJ_SIZE_T)nr_comp * (OPJ_SIZE_T)prec + 7U) / 8U; - if (rowStride != (OPJ_SIZE_T)png_row_size) { - fprintf(stderr, "Invalid PNG row size\n"); - goto fin; - } - row_buf = (png_bytep)malloc(png_row_size); - if (row_buf == NULL) { - fprintf(stderr, "Can't allocate memory for PNG row\n"); - goto fin; - } - buffer32s = (OPJ_INT32*)malloc((OPJ_SIZE_T)image->comps[0].w * (OPJ_SIZE_T)nr_comp * sizeof(OPJ_INT32)); - if (buffer32s == NULL) { - fprintf(stderr, "Can't allocate memory for interleaved 32s row\n"); - goto fin; - } - } - - /* convert */ - { - OPJ_SIZE_T width= image->comps[0].w; - OPJ_UINT32 y; - convert_32s_PXCX cvtPxToCx = convert_32s_PXCX_LUT[nr_comp]; - convert_32sXXx_C1R cvt32sToPack = NULL; - OPJ_INT32 adjust = image->comps[0].sgnd ? 1 << (prec - 1) : 0; - png_bytep row_buf_cpy = row_buf; - OPJ_INT32* buffer32s_cpy = buffer32s; - - switch (prec) { - case 1: - case 2: - case 4: - case 8: - cvt32sToPack = convert_32sXXu_C1R_LUT[prec]; - break; - case 16: - cvt32sToPack = convert_32s16u_C1R; - break; - default: - /* never here */ - break; - } - - for(y = 0; y < image->comps[0].h; ++y) - { - cvtPxToCx(planes, buffer32s_cpy, width, adjust); - cvt32sToPack(buffer32s_cpy, row_buf_cpy, width * (OPJ_SIZE_T)nr_comp); - png_write_row(png, row_buf_cpy); - planes[0] += width; - planes[1] += width; - planes[2] += width; - planes[3] += width; - } - } - - png_write_end(png, info); - - fails = 0; - + FILE * volatile writer = NULL; + png_structp png = NULL; + png_infop info = NULL; + png_bytep volatile row_buf = NULL; + int nr_comp, color_type; + volatile int prec; + png_color_8 sig_bit; + OPJ_INT32 const* planes[4]; + int i; + OPJ_INT32* volatile buffer32s = NULL; + + volatile int fails = 1; + + memset(&sig_bit, 0, sizeof(sig_bit)); + prec = (int)image->comps[0].prec; + planes[0] = image->comps[0].data; + nr_comp = (int)image->numcomps; + + if (nr_comp > 4) { + nr_comp = 4; + } + for (i = 1; i < nr_comp; ++i) { + if (image->comps[0].dx != image->comps[i].dx) { + break; + } + if (image->comps[0].dy != image->comps[i].dy) { + break; + } + if (image->comps[0].prec != image->comps[i].prec) { + break; + } + if (image->comps[0].sgnd != image->comps[i].sgnd) { + break; + } + planes[i] = image->comps[i].data; + } + if (i != nr_comp) { + fprintf(stderr,"imagetopng: All components shall have the same subsampling, same bit depth, same sign.\n"); + fprintf(stderr,"\tAborting\n"); + return 1; + } + for (i = 0; i < nr_comp; ++i) { + clip_component(&(image->comps[i]), image->comps[0].prec); + } + if(prec > 8 && prec < 16) { + for (i = 0; i < nr_comp; ++i) { + scale_component(&(image->comps[i]), 16); + } + prec = 16; + } else if(prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */ + for (i = 0; i < nr_comp; ++i) { + scale_component(&(image->comps[i]), 8); + } + prec = 8; + } else if((prec > 1) && (prec < 8) && ((prec == 6) || ((prec & 1)==1))) { /* GRAY with non native precision */ + if ((prec == 5) || (prec == 6)) { + prec = 8; + } else { + prec++; + } + for (i = 0; i < nr_comp; ++i) { + scale_component(&(image->comps[i]), (OPJ_UINT32)prec); + } + } + + if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) { + fprintf(stderr,"imagetopng: can not create %s\n\twrong bit_depth %d\n", write_idf, prec); + return fails; + } + + writer = fopen(write_idf, "wb"); + + if(writer == NULL) return fails; + + /* Create and initialize the png_struct with the desired error handler + * functions. If you want to use the default stderr and longjump method, + * you can supply NULL for the last three parameters. We also check that + * the library version is compatible with the one used at compile time, + * in case we are using dynamically linked libraries. REQUIRED. + */ + png = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, NULL, NULL); + /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */ + + if(png == NULL) goto fin; + + /* Allocate/initialize the image information data. REQUIRED + */ + info = png_create_info_struct(png); + + if(info == NULL) goto fin; + + /* Set error handling. REQUIRED if you are not supplying your own + * error handling functions in the png_create_write_struct() call. + */ + if(setjmp(png_jmpbuf(png))) goto fin; + + /* I/O initialization functions is REQUIRED + */ + png_init_io(png, writer); + + /* Set the image information here. Width and height are up to 2^31, + * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on + * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, + * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, + * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or + * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST + * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. + * REQUIRED + * + * ERRORS: + * + * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8 + * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8 + * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8 + * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8 + * + */ + png_set_compression_level(png, Z_BEST_COMPRESSION); + + if(nr_comp >= 3) { /* RGB(A) */ + color_type = PNG_COLOR_TYPE_RGB; + sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec; + } else { /* GRAY(A) */ + color_type = PNG_COLOR_TYPE_GRAY; + sig_bit.gray = (png_byte)prec; + } + if((nr_comp & 1) == 0) { /* ALPHA */ + color_type |= PNG_COLOR_MASK_ALPHA; + sig_bit.alpha = (png_byte)prec; + } + + png_set_IHDR(png, info, image->comps[0].w, image->comps[0].h, prec, color_type, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + png_set_sBIT(png, info, &sig_bit); + /* png_set_gamma(png, 2.2, 1./2.2); */ + /* png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); */ + png_write_info(png, info); + + /* setup conversion */ + { + OPJ_SIZE_T rowStride; + png_size_t png_row_size; + + png_row_size = png_get_rowbytes(png, info); + rowStride = ((OPJ_SIZE_T)image->comps[0].w * (OPJ_SIZE_T)nr_comp * (OPJ_SIZE_T)prec + 7U) / 8U; + if (rowStride != (OPJ_SIZE_T)png_row_size) { + fprintf(stderr, "Invalid PNG row size\n"); + goto fin; + } + row_buf = (png_bytep)malloc(png_row_size); + if (row_buf == NULL) { + fprintf(stderr, "Can't allocate memory for PNG row\n"); + goto fin; + } + buffer32s = (OPJ_INT32*)malloc((OPJ_SIZE_T)image->comps[0].w * (OPJ_SIZE_T)nr_comp * sizeof(OPJ_INT32)); + if (buffer32s == NULL) { + fprintf(stderr, "Can't allocate memory for interleaved 32s row\n"); + goto fin; + } + } + + /* convert */ + { + OPJ_SIZE_T width= image->comps[0].w; + OPJ_UINT32 y; + convert_32s_PXCX cvtPxToCx = convert_32s_PXCX_LUT[nr_comp]; + convert_32sXXx_C1R cvt32sToPack = NULL; + OPJ_INT32 adjust = image->comps[0].sgnd ? 1 << (prec - 1) : 0; + png_bytep row_buf_cpy = row_buf; + OPJ_INT32* buffer32s_cpy = buffer32s; + + switch (prec) { + case 1: + case 2: + case 4: + case 8: + cvt32sToPack = convert_32sXXu_C1R_LUT[prec]; + break; + case 16: + cvt32sToPack = convert_32s16u_C1R; + break; + default: + /* never here */ + break; + } + + for(y = 0; y < image->comps[0].h; ++y) { + cvtPxToCx(planes, buffer32s_cpy, width, adjust); + cvt32sToPack(buffer32s_cpy, row_buf_cpy, width * (OPJ_SIZE_T)nr_comp); + png_write_row(png, row_buf_cpy); + planes[0] += width; + planes[1] += width; + planes[2] += width; + planes[3] += width; + } + } + + png_write_end(png, info); + + fails = 0; + fin: - if(png) { - png_destroy_write_struct(&png, &info); - } - if(row_buf) { - free(row_buf); - } - if(buffer32s) { - free(buffer32s); - } - fclose(writer); - - if(fails) (void)remove(write_idf); /* ignore return value */ - - return fails; + if(png) { + png_destroy_write_struct(&png, &info); + } + if(row_buf) { + free(row_buf); + } + if(buffer32s) { + free(buffer32s); + } + fclose(writer); + + if(fails) (void)remove(write_idf); /* ignore return value */ + + return fails; }/* imagetopng() */ diff --git a/src/bin/jp2/converttif.c b/src/bin/jp2/converttif.c index dbda0741..907b93a5 100644 --- a/src/bin/jp2/converttif.c +++ b/src/bin/jp2/converttif.c @@ -52,360 +52,357 @@ #include "convert.h" /* -->> -->> -->> -->> - + TIFF IMAGE FORMAT - + <<-- <<-- <<-- <<-- */ static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; - OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; - OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; - - *pDst++ = (OPJ_BYTE)(src0 >> 2); - *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4)); - *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6)); - *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2) | (src3 >> 8)); - *pDst++ = (OPJ_BYTE)(src3); - } - - if (length & 3U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = 0U; - OPJ_UINT32 src2 = 0U; - length = length & 3U; - - if (length > 1U) { - src1 = (OPJ_UINT32)pSrc[i+1]; - if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i+2]; - } - } - *pDst++ = (OPJ_BYTE)(src0 >> 2); - *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4)); - if (length > 1U) { - *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6)); - if (length > 2U) { - *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2)); - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; + OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; + OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; + + *pDst++ = (OPJ_BYTE)(src0 >> 2); + *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4)); + *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6)); + *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2) | (src3 >> 8)); + *pDst++ = (OPJ_BYTE)(src3); + } + + if (length & 3U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = 0U; + OPJ_UINT32 src2 = 0U; + length = length & 3U; + + if (length > 1U) { + src1 = (OPJ_UINT32)pSrc[i+1]; + if (length > 2U) { + src2 = (OPJ_UINT32)pSrc[i+2]; + } + } + *pDst++ = (OPJ_BYTE)(src0 >> 2); + *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4)); + if (length > 1U) { + *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6)); + if (length > 2U) { + *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2)); + } + } + } } static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; - - *pDst++ = (OPJ_BYTE)(src0 >> 4); - *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8)); - *pDst++ = (OPJ_BYTE)(src1); - } - - if (length & 1U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - *pDst++ = (OPJ_BYTE)(src0 >> 4); - *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4)); - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; + + *pDst++ = (OPJ_BYTE)(src0 >> 4); + *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8)); + *pDst++ = (OPJ_BYTE)(src1); + } + + if (length & 1U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + *pDst++ = (OPJ_BYTE)(src0 >> 4); + *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4)); + } } static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; - OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; - OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; - - *pDst++ = (OPJ_BYTE)(src0 >> 6); - *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12)); - *pDst++ = (OPJ_BYTE)(src1 >> 4); - *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10)); - *pDst++ = (OPJ_BYTE)(src2 >> 2); - *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | (src3 >> 8)); - *pDst++ = (OPJ_BYTE)(src3); - } - - if (length & 3U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; - OPJ_UINT32 src1 = 0U; - OPJ_UINT32 src2 = 0U; - length = length & 3U; - - if (length > 1U) { - src1 = (OPJ_UINT32)pSrc[i+1]; - if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i+2]; - } - } - *pDst++ = (OPJ_BYTE)(src0 >> 6); - *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12)); - if (length > 1U) { - *pDst++ = (OPJ_BYTE)(src1 >> 4); - *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10)); - if (length > 2U) { - *pDst++ = (OPJ_BYTE)(src2 >> 2); - *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6)); - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1]; + OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2]; + OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3]; + + *pDst++ = (OPJ_BYTE)(src0 >> 6); + *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12)); + *pDst++ = (OPJ_BYTE)(src1 >> 4); + *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10)); + *pDst++ = (OPJ_BYTE)(src2 >> 2); + *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | (src3 >> 8)); + *pDst++ = (OPJ_BYTE)(src3); + } + + if (length & 3U) { + OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0]; + OPJ_UINT32 src1 = 0U; + OPJ_UINT32 src2 = 0U; + length = length & 3U; + + if (length > 1U) { + src1 = (OPJ_UINT32)pSrc[i+1]; + if (length > 2U) { + src2 = (OPJ_UINT32)pSrc[i+2]; + } + } + *pDst++ = (OPJ_BYTE)(src0 >> 6); + *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12)); + if (length > 1U) { + *pDst++ = (OPJ_BYTE)(src1 >> 4); + *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10)); + if (length > 2U) { + *pDst++ = (OPJ_BYTE)(src2 >> 2); + *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6)); + } + } + } } static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < length; ++i) { - pDst[i] = (OPJ_UINT16)pSrc[i]; - } + OPJ_SIZE_T i; + for (i = 0; i < length; ++i) { + pDst[i] = (OPJ_UINT16)pSrc[i]; + } } int imagetotif(opj_image_t * image, const char *outfile) { - int width, height; - int bps,adjust, sgnd; - int tiPhoto; - TIFF *tif; - tdata_t buf; - tsize_t strip_size; - OPJ_UINT32 i, numcomps; - OPJ_SIZE_T rowStride; - OPJ_INT32* buffer32s = NULL; - OPJ_INT32 const* planes[4]; - convert_32s_PXCX cvtPxToCx = NULL; - convert_32sXXx_C1R cvt32sToTif = NULL; - - bps = (int)image->comps[0].prec; - planes[0] = image->comps[0].data; - - numcomps = image->numcomps; - - if (image->color_space == OPJ_CLRSPC_CMYK) { - if (numcomps < 4U) { - fprintf(stderr,"imagetotif: CMYK images shall be composed of at least 4 planes.\n"); - fprintf(stderr,"\tAborting\n"); - return 1; - } - tiPhoto = PHOTOMETRIC_SEPARATED; - if (numcomps > 4U) { - numcomps = 4U; /* Alpha not supported */ - } - } - else if (numcomps > 2U) { - tiPhoto = PHOTOMETRIC_RGB; - if (numcomps > 4U) { - numcomps = 4U; - } - } else { - tiPhoto = PHOTOMETRIC_MINISBLACK; - } - for (i = 1U; i < numcomps; ++i) { - if (image->comps[0].dx != image->comps[i].dx) { - break; - } - if (image->comps[0].dy != image->comps[i].dy) { - break; - } - if (image->comps[0].prec != image->comps[i].prec) { - break; - } - if (image->comps[0].sgnd != image->comps[i].sgnd) { - break; - } - planes[i] = image->comps[i].data; - } - if (i != numcomps) { - fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n"); - fprintf(stderr,"\tAborting\n"); - return 1; - } - - if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0; - if(bps == 0) - { - fprintf(stderr,"imagetotif: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",bps); - fprintf(stderr,"\tAborting\n"); - return 1; - } - tif = TIFFOpen(outfile, "wb"); - if (!tif) - { - fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile); - return 1; - } - for (i = 0U; i < numcomps; ++i) { - clip_component(&(image->comps[i]), image->comps[0].prec); - } - cvtPxToCx = convert_32s_PXCX_LUT[numcomps]; - switch (bps) { - case 1: - case 2: - case 4: - case 6: - case 8: - cvt32sToTif = convert_32sXXu_C1R_LUT[bps]; - break; - case 10: - cvt32sToTif = tif_32sto10u; - break; - case 12: - cvt32sToTif = tif_32sto12u; - break; - case 14: - cvt32sToTif = tif_32sto14u; - break; - case 16: - cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u; - break; - default: - /* never here */ - break; - } - sgnd = (int)image->comps[0].sgnd; - adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0; - width = (int)image->comps[0].w; - height = (int)image->comps[0].h; - - TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); - TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); - TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps); - TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); - TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); - TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); - TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto); - TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); - - strip_size = TIFFStripSize(tif); - rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U; - if (rowStride != (OPJ_SIZE_T)strip_size) { - fprintf(stderr, "Invalid TIFF strip size\n"); - TIFFClose(tif); - return 1; - } - buf = _TIFFmalloc(strip_size); - if (buf == NULL) { - TIFFClose(tif); - return 1; - } - buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32)); - if (buffer32s == NULL) { - _TIFFfree(buf); - TIFFClose(tif); - return 1; - } - - for (i = 0; i < image->comps[0].h; ++i) { - cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust); - cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps); - (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size); - planes[0] += width; - planes[1] += width; - planes[2] += width; - planes[3] += width; - } - _TIFFfree((void*)buf); - TIFFClose(tif); - free(buffer32s); - - return 0; + int width, height; + int bps,adjust, sgnd; + int tiPhoto; + TIFF *tif; + tdata_t buf; + tsize_t strip_size; + OPJ_UINT32 i, numcomps; + OPJ_SIZE_T rowStride; + OPJ_INT32* buffer32s = NULL; + OPJ_INT32 const* planes[4]; + convert_32s_PXCX cvtPxToCx = NULL; + convert_32sXXx_C1R cvt32sToTif = NULL; + + bps = (int)image->comps[0].prec; + planes[0] = image->comps[0].data; + + numcomps = image->numcomps; + + if (image->color_space == OPJ_CLRSPC_CMYK) { + if (numcomps < 4U) { + fprintf(stderr,"imagetotif: CMYK images shall be composed of at least 4 planes.\n"); + fprintf(stderr,"\tAborting\n"); + return 1; + } + tiPhoto = PHOTOMETRIC_SEPARATED; + if (numcomps > 4U) { + numcomps = 4U; /* Alpha not supported */ + } + } else if (numcomps > 2U) { + tiPhoto = PHOTOMETRIC_RGB; + if (numcomps > 4U) { + numcomps = 4U; + } + } else { + tiPhoto = PHOTOMETRIC_MINISBLACK; + } + for (i = 1U; i < numcomps; ++i) { + if (image->comps[0].dx != image->comps[i].dx) { + break; + } + if (image->comps[0].dy != image->comps[i].dy) { + break; + } + if (image->comps[0].prec != image->comps[i].prec) { + break; + } + if (image->comps[0].sgnd != image->comps[i].sgnd) { + break; + } + planes[i] = image->comps[i].data; + } + if (i != numcomps) { + fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n"); + fprintf(stderr,"\tAborting\n"); + return 1; + } + + if((bps > 16) || ((bps != 1) && (bps & 1))) bps = 0; + if(bps == 0) { + fprintf(stderr,"imagetotif: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",bps); + fprintf(stderr,"\tAborting\n"); + return 1; + } + tif = TIFFOpen(outfile, "wb"); + if (!tif) { + fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile); + return 1; + } + for (i = 0U; i < numcomps; ++i) { + clip_component(&(image->comps[i]), image->comps[0].prec); + } + cvtPxToCx = convert_32s_PXCX_LUT[numcomps]; + switch (bps) { + case 1: + case 2: + case 4: + case 6: + case 8: + cvt32sToTif = convert_32sXXu_C1R_LUT[bps]; + break; + case 10: + cvt32sToTif = tif_32sto10u; + break; + case 12: + cvt32sToTif = tif_32sto12u; + break; + case 14: + cvt32sToTif = tif_32sto14u; + break; + case 16: + cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u; + break; + default: + /* never here */ + break; + } + sgnd = (int)image->comps[0].sgnd; + adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0; + width = (int)image->comps[0].w; + height = (int)image->comps[0].h; + + TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); + TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); + TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps); + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); + TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); + TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); + TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto); + TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); + + strip_size = TIFFStripSize(tif); + rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U; + if (rowStride != (OPJ_SIZE_T)strip_size) { + fprintf(stderr, "Invalid TIFF strip size\n"); + TIFFClose(tif); + return 1; + } + buf = _TIFFmalloc(strip_size); + if (buf == NULL) { + TIFFClose(tif); + return 1; + } + buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32)); + if (buffer32s == NULL) { + _TIFFfree(buf); + TIFFClose(tif); + return 1; + } + + for (i = 0; i < image->comps[0].h; ++i) { + cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust); + cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps); + (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size); + planes[0] += width; + planes[1] += width; + planes[2] += width; + planes[3] += width; + } + _TIFFfree((void*)buf); + TIFFClose(tif); + free(buffer32s); + + return 0; }/* imagetotif() */ static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 val0 = *pSrc++; - OPJ_UINT32 val1 = *pSrc++; - OPJ_UINT32 val2 = *pSrc++; - OPJ_UINT32 val3 = *pSrc++; - OPJ_UINT32 val4 = *pSrc++; - - pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6)); - pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4)); - pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2)); - pDst[i+3] = (OPJ_INT32)(((val3 & 0x3U) << 8) | val4); - - } - if (length & 3U) { - OPJ_UINT32 val0 = *pSrc++; - OPJ_UINT32 val1 = *pSrc++; - length = length & 3U; - pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6)); - - if (length > 1U) { - OPJ_UINT32 val2 = *pSrc++; - pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4)); - if (length > 2U) { - OPJ_UINT32 val3 = *pSrc++; - pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2)); - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 val0 = *pSrc++; + OPJ_UINT32 val1 = *pSrc++; + OPJ_UINT32 val2 = *pSrc++; + OPJ_UINT32 val3 = *pSrc++; + OPJ_UINT32 val4 = *pSrc++; + + pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6)); + pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4)); + pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2)); + pDst[i+3] = (OPJ_INT32)(((val3 & 0x3U) << 8) | val4); + + } + if (length & 3U) { + OPJ_UINT32 val0 = *pSrc++; + OPJ_UINT32 val1 = *pSrc++; + length = length & 3U; + pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6)); + + if (length > 1U) { + OPJ_UINT32 val2 = *pSrc++; + pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4)); + if (length > 2U) { + OPJ_UINT32 val3 = *pSrc++; + pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2)); + } + } + } } static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { - OPJ_UINT32 val0 = *pSrc++; - OPJ_UINT32 val1 = *pSrc++; - OPJ_UINT32 val2 = *pSrc++; - - pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4)); - pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2); - } - if (length & 1U) { - OPJ_UINT32 val0 = *pSrc++; - OPJ_UINT32 val1 = *pSrc++; - pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4)); - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) { + OPJ_UINT32 val0 = *pSrc++; + OPJ_UINT32 val1 = *pSrc++; + OPJ_UINT32 val2 = *pSrc++; + + pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4)); + pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2); + } + if (length & 1U) { + OPJ_UINT32 val0 = *pSrc++; + OPJ_UINT32 val1 = *pSrc++; + pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4)); + } } static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { - OPJ_UINT32 val0 = *pSrc++; - OPJ_UINT32 val1 = *pSrc++; - OPJ_UINT32 val2 = *pSrc++; - OPJ_UINT32 val3 = *pSrc++; - OPJ_UINT32 val4 = *pSrc++; - OPJ_UINT32 val5 = *pSrc++; - OPJ_UINT32 val6 = *pSrc++; - - pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2)); - pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4)); - pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6)); - pDst[i+3] = (OPJ_INT32)(((val5 & 0x3FU) << 8) | val6); - - } - if (length & 3U) { - OPJ_UINT32 val0 = *pSrc++; - OPJ_UINT32 val1 = *pSrc++; - length = length & 3U; - pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2)); - - if (length > 1U) { - OPJ_UINT32 val2 = *pSrc++; - OPJ_UINT32 val3 = *pSrc++; - pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4)); - if (length > 2U) { - OPJ_UINT32 val4 = *pSrc++; - OPJ_UINT32 val5 = *pSrc++; - pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6)); - } - } - } + OPJ_SIZE_T i; + for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) { + OPJ_UINT32 val0 = *pSrc++; + OPJ_UINT32 val1 = *pSrc++; + OPJ_UINT32 val2 = *pSrc++; + OPJ_UINT32 val3 = *pSrc++; + OPJ_UINT32 val4 = *pSrc++; + OPJ_UINT32 val5 = *pSrc++; + OPJ_UINT32 val6 = *pSrc++; + + pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2)); + pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4)); + pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6)); + pDst[i+3] = (OPJ_INT32)(((val5 & 0x3FU) << 8) | val6); + + } + if (length & 3U) { + OPJ_UINT32 val0 = *pSrc++; + OPJ_UINT32 val1 = *pSrc++; + length = length & 3U; + pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2)); + + if (length > 1U) { + OPJ_UINT32 val2 = *pSrc++; + OPJ_UINT32 val3 = *pSrc++; + pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4)); + if (length > 2U) { + OPJ_UINT32 val4 = *pSrc++; + OPJ_UINT32 val5 = *pSrc++; + pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6)); + } + } + } } /* seems that libtiff decodes this to machine endianness */ static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length) { - OPJ_SIZE_T i; - for (i = 0; i < length; i++) { - pDst[i] = pSrc[i]; - } + OPJ_SIZE_T i; + for (i = 0; i < length; i++) { + pDst[i] = pSrc[i]; + } } /* @@ -414,231 +411,219 @@ static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T len */ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) { - int subsampling_dx = parameters->subsampling_dx; - int subsampling_dy = parameters->subsampling_dy; - TIFF *tif; - tdata_t buf; - tstrip_t strip; - tsize_t strip_size; - int j, currentPlane, numcomps = 0, w, h; - OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN; - opj_image_cmptparm_t cmptparm[4]; /* RGBA */ - opj_image_t *image = NULL; - int has_alpha = 0; - unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC; - unsigned int tiWidth, tiHeight; - OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz); - convert_XXx32s_C1R cvtTifTo32s = NULL; - convert_32s_CXPX cvtCxToPx = NULL; - OPJ_INT32* buffer32s = NULL; - OPJ_INT32* planes[4]; - OPJ_SIZE_T rowStride; - - tif = TIFFOpen(filename, "r"); - - if(!tif) - { - fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename); - return 0; - } - tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0; - tiWidth = tiHeight = 0; - - TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth); - TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight); - TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps); - TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf); - TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp); - TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto); - TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC); - w= (int)tiWidth; - h= (int)tiHeight; - - if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) { - fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps); - fprintf(stderr,"\tAborting\n"); - TIFFClose(tif); - return NULL; - } - if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) { - fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto); - fprintf(stderr,"\tAborting\n"); - TIFFClose(tif); - return NULL; - } - - switch (tiBps) { - case 1: - case 2: - case 4: - case 6: - case 8: - cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps]; - break; - /* others are specific to TIFF */ - case 10: - cvtTifTo32s = tif_10uto32s; - break; - case 12: - cvtTifTo32s = tif_12uto32s; - break; - case 14: - cvtTifTo32s = tif_14uto32s; - break; - case 16: - cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s; - break; - default: - /* never here */ - break; - } - - {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */ - uint16* sampleinfo; - uint16 extrasamples; - - TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, - &extrasamples, &sampleinfo); - - if(extrasamples >= 1) - { - switch(sampleinfo[0]) - { - case EXTRASAMPLE_UNSPECIFIED: - /* Workaround for some images without correct info about alpha channel - */ - if(tiSpp > 3) - has_alpha = 1; - break; - - case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */ - case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */ - has_alpha = 1; - break; - } - } - else /* extrasamples == 0 */ - if(tiSpp == 4 || tiSpp == 2) has_alpha = 1; - } - - /* initialize image components */ - memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); - - if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) { - fprintf(stdout,"WARNING:\n" - "Input image bitdepth is %d bits\n" - "TIF conversion has automatically rescaled to 12-bits\n" - "to comply with cinema profiles.\n", - tiBps); - } else { - is_cinema = 0U; - } - - if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */ - { - numcomps = 3 + has_alpha; - color_space = OPJ_CLRSPC_SRGB; - } - else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */ - { - numcomps = 1 + has_alpha; - color_space = OPJ_CLRSPC_GRAY; - } - - cvtCxToPx = convert_32s_CXPX_LUT[numcomps]; - if (tiPC == PLANARCONFIG_SEPARATE) { - cvtCxToPx = convert_32s_CXPX_LUT[1]; /* override */ - tiSpp = 1U; /* consider only one sample per plane */ - } - - for(j = 0; j < numcomps; j++) - { - cmptparm[j].prec = tiBps; - cmptparm[j].bpp = tiBps; - cmptparm[j].dx = (OPJ_UINT32)subsampling_dx; - cmptparm[j].dy = (OPJ_UINT32)subsampling_dy; - cmptparm[j].w = (OPJ_UINT32)w; - cmptparm[j].h = (OPJ_UINT32)h; - } - - image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); - if(!image) - { - TIFFClose(tif); - return NULL; - } - /* set image offset and reference grid */ - image->x0 = (OPJ_UINT32)parameters->image_offset_x0; - image->y0 = (OPJ_UINT32)parameters->image_offset_y0; - image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : - image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; - image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : - image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; - - for(j = 0; j < numcomps; j++) - { - planes[j] = image->comps[j].data; - } - image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1)); - - strip_size = TIFFStripSize(tif); - - buf = _TIFFmalloc(strip_size); - if (buf == NULL) { - TIFFClose(tif); - opj_image_destroy(image); - return NULL; - } - rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U; - buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32)); - if (buffer32s == NULL) { - _TIFFfree(buf); - TIFFClose(tif); - opj_image_destroy(image); - return NULL; - } - - strip = 0; - currentPlane = 0; - do - { - planes[0] = image->comps[currentPlane].data; /* to manage planar data */ - h= (int)tiHeight; - /* Read the Image components */ - for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) - { - const OPJ_UINT8 *dat8; - OPJ_SIZE_T ssize; - - ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size); - dat8 = (const OPJ_UINT8*)buf; - - while (ssize >= rowStride) { - cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp); - cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w); - planes[0] += w; - planes[1] += w; - planes[2] += w; - planes[3] += w; - dat8 += rowStride; - ssize -= rowStride; - h--; - } - } - currentPlane++; - } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps)); - - free(buffer32s); - _TIFFfree(buf); - TIFFClose(tif); - - if (is_cinema) { - for (j=0; j < numcomps; ++j) { - scale_component(&(image->comps[j]), 12); - } - - } - return image; + int subsampling_dx = parameters->subsampling_dx; + int subsampling_dy = parameters->subsampling_dy; + TIFF *tif; + tdata_t buf; + tstrip_t strip; + tsize_t strip_size; + int j, currentPlane, numcomps = 0, w, h; + OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN; + opj_image_cmptparm_t cmptparm[4]; /* RGBA */ + opj_image_t *image = NULL; + int has_alpha = 0; + unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC; + unsigned int tiWidth, tiHeight; + OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz); + convert_XXx32s_C1R cvtTifTo32s = NULL; + convert_32s_CXPX cvtCxToPx = NULL; + OPJ_INT32* buffer32s = NULL; + OPJ_INT32* planes[4]; + OPJ_SIZE_T rowStride; + + tif = TIFFOpen(filename, "r"); + + if(!tif) { + fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename); + return 0; + } + tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0; + tiWidth = tiHeight = 0; + + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight); + TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps); + TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf); + TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp); + TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto); + TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC); + w= (int)tiWidth; + h= (int)tiHeight; + + if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) { + fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps); + fprintf(stderr,"\tAborting\n"); + TIFFClose(tif); + return NULL; + } + if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) { + fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto); + fprintf(stderr,"\tAborting\n"); + TIFFClose(tif); + return NULL; + } + + switch (tiBps) { + case 1: + case 2: + case 4: + case 6: + case 8: + cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps]; + break; + /* others are specific to TIFF */ + case 10: + cvtTifTo32s = tif_10uto32s; + break; + case 12: + cvtTifTo32s = tif_12uto32s; + break; + case 14: + cvtTifTo32s = tif_14uto32s; + break; + case 16: + cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s; + break; + default: + /* never here */ + break; + } + + {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */ + uint16* sampleinfo; + uint16 extrasamples; + + TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, + &extrasamples, &sampleinfo); + + if(extrasamples >= 1) { + switch(sampleinfo[0]) { + case EXTRASAMPLE_UNSPECIFIED: + /* Workaround for some images without correct info about alpha channel + */ + if(tiSpp > 3) + has_alpha = 1; + break; + + case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */ + case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */ + has_alpha = 1; + break; + } + } else /* extrasamples == 0 */ + if(tiSpp == 4 || tiSpp == 2) has_alpha = 1; + } + + /* initialize image components */ + memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); + + if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) { + fprintf(stdout,"WARNING:\n" + "Input image bitdepth is %d bits\n" + "TIF conversion has automatically rescaled to 12-bits\n" + "to comply with cinema profiles.\n", + tiBps); + } else { + is_cinema = 0U; + } + + if(tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */ + numcomps = 3 + has_alpha; + color_space = OPJ_CLRSPC_SRGB; + } else if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */ + numcomps = 1 + has_alpha; + color_space = OPJ_CLRSPC_GRAY; + } + + cvtCxToPx = convert_32s_CXPX_LUT[numcomps]; + if (tiPC == PLANARCONFIG_SEPARATE) { + cvtCxToPx = convert_32s_CXPX_LUT[1]; /* override */ + tiSpp = 1U; /* consider only one sample per plane */ + } + + for(j = 0; j < numcomps; j++) { + cmptparm[j].prec = tiBps; + cmptparm[j].bpp = tiBps; + cmptparm[j].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[j].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[j].w = (OPJ_UINT32)w; + cmptparm[j].h = (OPJ_UINT32)h; + } + + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + if(!image) { + TIFFClose(tif); + return NULL; + } + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : + image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : + image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; + + for(j = 0; j < numcomps; j++) { + planes[j] = image->comps[j].data; + } + image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1)); + + strip_size = TIFFStripSize(tif); + + buf = _TIFFmalloc(strip_size); + if (buf == NULL) { + TIFFClose(tif); + opj_image_destroy(image); + return NULL; + } + rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U; + buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32)); + if (buffer32s == NULL) { + _TIFFfree(buf); + TIFFClose(tif); + opj_image_destroy(image); + return NULL; + } + + strip = 0; + currentPlane = 0; + do { + planes[0] = image->comps[currentPlane].data; /* to manage planar data */ + h= (int)tiHeight; + /* Read the Image components */ + for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) { + const OPJ_UINT8 *dat8; + OPJ_SIZE_T ssize; + + ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size); + dat8 = (const OPJ_UINT8*)buf; + + while (ssize >= rowStride) { + cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp); + cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w); + planes[0] += w; + planes[1] += w; + planes[2] += w; + planes[3] += w; + dat8 += rowStride; + ssize -= rowStride; + h--; + } + } + currentPlane++; + } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps)); + + free(buffer32s); + _TIFFfree(buf); + TIFFClose(tif); + + if (is_cinema) { + for (j=0; j < numcomps; ++j) { + scale_component(&(image->comps[j]), 12); + } + + } + return image; }/* tiftoimage() */ diff --git a/src/bin/jp2/index.c b/src/bin/jp2/index.c index af6e1381..65acbf5b 100644 --- a/src/bin/jp2/index.c +++ b/src/bin/jp2/index.c @@ -1,12 +1,12 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium * Copyright (c) 2002-2014, Professor Benoit Macq - * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2007, Francois-Olivier Devaux * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -42,356 +42,357 @@ /** Write a structured index to a file -@param cstr_info Codestream information +@param cstr_info Codestream information @param index Index filename @return Returns 0 if successful, returns 1 otherwise */ -int write_index_file(opj_codestream_info_t *cstr_info, char *index) { - int tileno, compno, layno, resno, precno, pack_nb, x, y; - FILE *stream = NULL; - double total_disto = 0; -/* UniPG>> */ - int tilepartno; - char disto_on, numpix_on; +int write_index_file(opj_codestream_info_t *cstr_info, char *index) +{ + int tileno, compno, layno, resno, precno, pack_nb, x, y; + FILE *stream = NULL; + double total_disto = 0; + /* UniPG>> */ + int tilepartno; + char disto_on, numpix_on; #ifdef USE_JPWL - if (!strcmp(index, JPWL_PRIVATEINDEX_NAME)) - return 0; + if (!strcmp(index, JPWL_PRIVATEINDEX_NAME)) + return 0; #endif /* USE_JPWL */ -/* <<UniPG */ - - if (!cstr_info) - return 1; - - stream = fopen(index, "w"); - if (!stream) { - fprintf(stderr, "failed to open index file [%s] for writing\n", index); - return 1; - } - - if (cstr_info->tile[0].distotile) - disto_on = 1; - else - disto_on = 0; - - if (cstr_info->tile[0].numpix) - numpix_on = 1; - else - numpix_on = 0; - - fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h); - fprintf(stream, "%d\n", cstr_info->prog); - fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y); - fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th); - fprintf(stream, "%d\n", cstr_info->numcomps); - fprintf(stream, "%d\n", cstr_info->numlayers); - fprintf(stream, "%d\n", cstr_info->numdecompos[0]); /* based on component 0 */ - - for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) { - fprintf(stream, "[%d,%d] ", - (1 << cstr_info->tile[0].pdx[resno]), (1 << cstr_info->tile[0].pdx[resno])); /* based on tile 0 and component 0 */ - } - - fprintf(stream, "\n"); -/* UniPG>> */ - fprintf(stream, "%d\n", cstr_info->main_head_start); -/* <<UniPG */ - fprintf(stream, "%d\n", cstr_info->main_head_end); - fprintf(stream, "%d\n", cstr_info->codestream_size); - - fprintf(stream, "\nINFO ON TILES\n"); - fprintf(stream, "tileno start_pos end_hd end_tile nbparts"); - if (disto_on) - fprintf(stream," disto"); - if (numpix_on) - fprintf(stream," nbpix"); - if (disto_on && numpix_on) - fprintf(stream," disto/nbpix"); - fprintf(stream, "\n"); - - for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) { - fprintf(stream, "%4d %9d %9d %9d %9d", - cstr_info->tile[tileno].tileno, - cstr_info->tile[tileno].start_pos, - cstr_info->tile[tileno].end_header, - cstr_info->tile[tileno].end_pos, - cstr_info->tile[tileno].num_tps); - if (disto_on) - fprintf(stream," %9e", cstr_info->tile[tileno].distotile); - if (numpix_on) - fprintf(stream," %9d", cstr_info->tile[tileno].numpix); - if (disto_on && numpix_on) - fprintf(stream," %9e", cstr_info->tile[tileno].distotile / cstr_info->tile[tileno].numpix); - fprintf(stream, "\n"); - } - - for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) { - OPJ_OFF_T start_pos, end_ph_pos, end_pos; - double disto = 0; - int max_numdecompos = 0; - pack_nb = 0; - - for (compno = 0; compno < cstr_info->numcomps; compno++) { - if (max_numdecompos < cstr_info->numdecompos[compno]) - max_numdecompos = cstr_info->numdecompos[compno]; - } - - fprintf(stream, "\nTILE %d DETAILS\n", tileno); - fprintf(stream, "part_nb tileno start_pack num_packs start_pos end_tph_pos end_pos\n"); - for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++) - fprintf(stream, "%4d %9d %9d %9d %9d %11d %9d\n", - tilepartno, tileno, - cstr_info->tile[tileno].tp[tilepartno].tp_start_pack, - cstr_info->tile[tileno].tp[tilepartno].tp_numpacks, - cstr_info->tile[tileno].tp[tilepartno].tp_start_pos, - cstr_info->tile[tileno].tp[tilepartno].tp_end_header, - cstr_info->tile[tileno].tp[tilepartno].tp_end_pos - ); - - if (cstr_info->prog == OPJ_LRCP) { /* LRCP */ - fprintf(stream, "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos"); - if (disto_on) - fprintf(stream, " disto"); - fprintf(stream,"\n"); - - for (layno = 0; layno < cstr_info->numlayers; layno++) { - for (resno = 0; resno < max_numdecompos + 1; resno++) { - for (compno = 0; compno < cstr_info->numcomps; compno++) { - int prec_max; - if (resno > cstr_info->numdecompos[compno]) - break; - prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; - for (precno = 0; precno < prec_max; precno++) { - start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; - end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; - end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; - disto = cstr_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %7d %5d %6d %6d %6" PRId64 " %6" PRId64 " %7" PRId64, - pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos); - if (disto_on) - fprintf(stream, " %8e", disto); - fprintf(stream, "\n"); - total_disto += disto; - pack_nb++; - } - } - } - } - } /* LRCP */ - - else if (cstr_info->prog == OPJ_RLCP) { /* RLCP */ - fprintf(stream, "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos\n"); - if (disto_on) - fprintf(stream, " disto"); - fprintf(stream,"\n"); - - for (resno = 0; resno < max_numdecompos + 1; resno++) { - for (layno = 0; layno < cstr_info->numlayers; layno++) { - for (compno = 0; compno < cstr_info->numcomps; compno++) { - int prec_max; - if (resno > cstr_info->numdecompos[compno]) - break; - prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; - for (precno = 0; precno < prec_max; precno++) { - start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; - end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; - end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; - disto = cstr_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %5d %7d %6d %6d %9" PRId64 " %9" PRId64 " %7" PRId64, - pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos); - if (disto_on) - fprintf(stream, " %8e", disto); - fprintf(stream, "\n"); - total_disto += disto; - pack_nb++; - } - } - } - } - } /* RLCP */ - - else if (cstr_info->prog == OPJ_RPCL) { /* RPCL */ - - fprintf(stream, "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos"); - if (disto_on) - fprintf(stream, " disto"); - fprintf(stream,"\n"); - - for (resno = 0; resno < max_numdecompos + 1; resno++) { - int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; - for (precno = 0; precno < numprec; precno++) { - /* I suppose components have same XRsiz, YRsiz */ - int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x; - int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y; - int x1 = x0 + cstr_info->tile_x; - int y1 = y0 + cstr_info->tile_y; - for (compno = 0; compno < cstr_info->numcomps; compno++) { - int pcnx = cstr_info->tile[tileno].pw[resno]; - int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno ); - int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno ); - int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; - int precno_y = (int) floor( (float)precno/(float)pcnx ); - if (resno > cstr_info->numdecompos[compno]) - break; - for(y = y0; y < y1; y++) { - if (precno_y*pcy == y ) { - for (x = x0; x < x1; x++) { - if (precno_x*pcx == x ) { - for (layno = 0; layno < cstr_info->numlayers; layno++) { - start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; - end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; - end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; - disto = cstr_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %5d %6d %6d %7d %9" PRId64 " %9" PRId64 " %7" PRId64, - pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos); - if (disto_on) - fprintf(stream, " %8e", disto); - fprintf(stream, "\n"); - total_disto += disto; - pack_nb++; - } - } - }/* x = x0..x1 */ - } - } /* y = y0..y1 */ - } /* precno */ - } /* compno */ - } /* resno */ - } /* RPCL */ - - else if (cstr_info->prog == OPJ_PCRL) { /* PCRL */ - /* I suppose components have same XRsiz, YRsiz */ - int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x; - int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y; - int x1 = x0 + cstr_info->tile_x; - int y1 = y0 + cstr_info->tile_y; - - /* Count the maximum number of precincts */ - int max_numprec = 0; - for (resno = 0; resno < max_numdecompos + 1; resno++) { - int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; - if (numprec > max_numprec) - max_numprec = numprec; - } - - fprintf(stream, "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos"); - if (disto_on) - fprintf(stream, " disto"); - fprintf(stream,"\n"); - - for (precno = 0; precno < max_numprec; precno++) { - for (compno = 0; compno < cstr_info->numcomps; compno++) { - for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) { - int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; - int pcnx = cstr_info->tile[tileno].pw[resno]; - int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno ); - int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno ); - int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; - int precno_y = (int) floor( (float)precno/(float)pcnx ); - if (precno >= numprec) - continue; - for(y = y0; y < y1; y++) { - if (precno_y*pcy == y ) { - for (x = x0; x < x1; x++) { - if (precno_x*pcx == x ) { - for (layno = 0; layno < cstr_info->numlayers; layno++) { - start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; - end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; - end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; - disto = cstr_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64, - pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos); - if (disto_on) - fprintf(stream, " %8e", disto); - fprintf(stream, "\n"); - total_disto += disto; - pack_nb++; - } - } - }/* x = x0..x1 */ - } - } /* y = y0..y1 */ - } /* resno */ - } /* compno */ - } /* precno */ - } /* PCRL */ - - else { /* CPRL */ - /* Count the maximum number of precincts */ - int max_numprec = 0; - for (resno = 0; resno < max_numdecompos + 1; resno++) { - int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; - if (numprec > max_numprec) - max_numprec = numprec; - } - - fprintf(stream, "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos"); - if (disto_on) - fprintf(stream, " disto"); - fprintf(stream,"\n"); - - for (compno = 0; compno < cstr_info->numcomps; compno++) { - /* I suppose components have same XRsiz, YRsiz */ - int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x; - int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y; - int x1 = x0 + cstr_info->tile_x; - int y1 = y0 + cstr_info->tile_y; - - for (precno = 0; precno < max_numprec; precno++) { - for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) { - int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; - int pcnx = cstr_info->tile[tileno].pw[resno]; - int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno ); - int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno ); - int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; - int precno_y = (int) floor( (float)precno/(float)pcnx ); - if (precno >= numprec) - continue; - - for(y = y0; y < y1; y++) { - if (precno_y*pcy == y ) { - for (x = x0; x < x1; x++) { - if (precno_x*pcx == x ) { - for (layno = 0; layno < cstr_info->numlayers; layno++) { - start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; - end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; - end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; - disto = cstr_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64, - pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos); - if (disto_on) - fprintf(stream, " %8e", disto); - fprintf(stream, "\n"); - total_disto += disto; - pack_nb++; - } - } - }/* x = x0..x1 */ - } - } /* y = y0..y1 */ - } /* resno */ - } /* precno */ - } /* compno */ - } /* CPRL */ - } /* tileno */ - - if (disto_on) { - fprintf(stream, "%8e\n", cstr_info->D_max); /* SE max */ - fprintf(stream, "%.8e\n", total_disto); /* SE totale */ - } -/* UniPG>> */ - /* print the markers' list */ - if (cstr_info->marknum) { - fprintf(stream, "\nMARKER LIST\n"); - fprintf(stream, "%d\n", cstr_info->marknum); - fprintf(stream, "type\tstart_pos length\n"); - for (x = 0; x < cstr_info->marknum; x++) - fprintf(stream, "%X\t%9" PRId64 " %9d\n", cstr_info->marker[x].type, cstr_info->marker[x].pos, cstr_info->marker[x].len); - } -/* <<UniPG */ - fclose(stream); - - fprintf(stderr,"Generated index file %s\n", index); - - return 0; + /* <<UniPG */ + + if (!cstr_info) + return 1; + + stream = fopen(index, "w"); + if (!stream) { + fprintf(stderr, "failed to open index file [%s] for writing\n", index); + return 1; + } + + if (cstr_info->tile[0].distotile) + disto_on = 1; + else + disto_on = 0; + + if (cstr_info->tile[0].numpix) + numpix_on = 1; + else + numpix_on = 0; + + fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h); + fprintf(stream, "%d\n", cstr_info->prog); + fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y); + fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th); + fprintf(stream, "%d\n", cstr_info->numcomps); + fprintf(stream, "%d\n", cstr_info->numlayers); + fprintf(stream, "%d\n", cstr_info->numdecompos[0]); /* based on component 0 */ + + for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) { + fprintf(stream, "[%d,%d] ", + (1 << cstr_info->tile[0].pdx[resno]), (1 << cstr_info->tile[0].pdx[resno])); /* based on tile 0 and component 0 */ + } + + fprintf(stream, "\n"); + /* UniPG>> */ + fprintf(stream, "%d\n", cstr_info->main_head_start); + /* <<UniPG */ + fprintf(stream, "%d\n", cstr_info->main_head_end); + fprintf(stream, "%d\n", cstr_info->codestream_size); + + fprintf(stream, "\nINFO ON TILES\n"); + fprintf(stream, "tileno start_pos end_hd end_tile nbparts"); + if (disto_on) + fprintf(stream," disto"); + if (numpix_on) + fprintf(stream," nbpix"); + if (disto_on && numpix_on) + fprintf(stream," disto/nbpix"); + fprintf(stream, "\n"); + + for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) { + fprintf(stream, "%4d %9d %9d %9d %9d", + cstr_info->tile[tileno].tileno, + cstr_info->tile[tileno].start_pos, + cstr_info->tile[tileno].end_header, + cstr_info->tile[tileno].end_pos, + cstr_info->tile[tileno].num_tps); + if (disto_on) + fprintf(stream," %9e", cstr_info->tile[tileno].distotile); + if (numpix_on) + fprintf(stream," %9d", cstr_info->tile[tileno].numpix); + if (disto_on && numpix_on) + fprintf(stream," %9e", cstr_info->tile[tileno].distotile / cstr_info->tile[tileno].numpix); + fprintf(stream, "\n"); + } + + for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) { + OPJ_OFF_T start_pos, end_ph_pos, end_pos; + double disto = 0; + int max_numdecompos = 0; + pack_nb = 0; + + for (compno = 0; compno < cstr_info->numcomps; compno++) { + if (max_numdecompos < cstr_info->numdecompos[compno]) + max_numdecompos = cstr_info->numdecompos[compno]; + } + + fprintf(stream, "\nTILE %d DETAILS\n", tileno); + fprintf(stream, "part_nb tileno start_pack num_packs start_pos end_tph_pos end_pos\n"); + for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++) + fprintf(stream, "%4d %9d %9d %9d %9d %11d %9d\n", + tilepartno, tileno, + cstr_info->tile[tileno].tp[tilepartno].tp_start_pack, + cstr_info->tile[tileno].tp[tilepartno].tp_numpacks, + cstr_info->tile[tileno].tp[tilepartno].tp_start_pos, + cstr_info->tile[tileno].tp[tilepartno].tp_end_header, + cstr_info->tile[tileno].tp[tilepartno].tp_end_pos + ); + + if (cstr_info->prog == OPJ_LRCP) { /* LRCP */ + fprintf(stream, "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos"); + if (disto_on) + fprintf(stream, " disto"); + fprintf(stream,"\n"); + + for (layno = 0; layno < cstr_info->numlayers; layno++) { + for (resno = 0; resno < max_numdecompos + 1; resno++) { + for (compno = 0; compno < cstr_info->numcomps; compno++) { + int prec_max; + if (resno > cstr_info->numdecompos[compno]) + break; + prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; + for (precno = 0; precno < prec_max; precno++) { + start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; + end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; + end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; + disto = cstr_info->tile[tileno].packet[pack_nb].disto; + fprintf(stream, "%4d %6d %7d %5d %6d %6d %6" PRId64 " %6" PRId64 " %7" PRId64, + pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos); + if (disto_on) + fprintf(stream, " %8e", disto); + fprintf(stream, "\n"); + total_disto += disto; + pack_nb++; + } + } + } + } + } /* LRCP */ + + else if (cstr_info->prog == OPJ_RLCP) { /* RLCP */ + fprintf(stream, "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos\n"); + if (disto_on) + fprintf(stream, " disto"); + fprintf(stream,"\n"); + + for (resno = 0; resno < max_numdecompos + 1; resno++) { + for (layno = 0; layno < cstr_info->numlayers; layno++) { + for (compno = 0; compno < cstr_info->numcomps; compno++) { + int prec_max; + if (resno > cstr_info->numdecompos[compno]) + break; + prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; + for (precno = 0; precno < prec_max; precno++) { + start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; + end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; + end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; + disto = cstr_info->tile[tileno].packet[pack_nb].disto; + fprintf(stream, "%4d %6d %5d %7d %6d %6d %9" PRId64 " %9" PRId64 " %7" PRId64, + pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos); + if (disto_on) + fprintf(stream, " %8e", disto); + fprintf(stream, "\n"); + total_disto += disto; + pack_nb++; + } + } + } + } + } /* RLCP */ + + else if (cstr_info->prog == OPJ_RPCL) { /* RPCL */ + + fprintf(stream, "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos"); + if (disto_on) + fprintf(stream, " disto"); + fprintf(stream,"\n"); + + for (resno = 0; resno < max_numdecompos + 1; resno++) { + int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; + for (precno = 0; precno < numprec; precno++) { + /* I suppose components have same XRsiz, YRsiz */ + int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x; + int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y; + int x1 = x0 + cstr_info->tile_x; + int y1 = y0 + cstr_info->tile_y; + for (compno = 0; compno < cstr_info->numcomps; compno++) { + int pcnx = cstr_info->tile[tileno].pw[resno]; + int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno ); + int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno ); + int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; + int precno_y = (int) floor( (float)precno/(float)pcnx ); + if (resno > cstr_info->numdecompos[compno]) + break; + for(y = y0; y < y1; y++) { + if (precno_y*pcy == y ) { + for (x = x0; x < x1; x++) { + if (precno_x*pcx == x ) { + for (layno = 0; layno < cstr_info->numlayers; layno++) { + start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; + end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; + end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; + disto = cstr_info->tile[tileno].packet[pack_nb].disto; + fprintf(stream, "%4d %6d %5d %6d %6d %7d %9" PRId64 " %9" PRId64 " %7" PRId64, + pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos); + if (disto_on) + fprintf(stream, " %8e", disto); + fprintf(stream, "\n"); + total_disto += disto; + pack_nb++; + } + } + }/* x = x0..x1 */ + } + } /* y = y0..y1 */ + } /* precno */ + } /* compno */ + } /* resno */ + } /* RPCL */ + + else if (cstr_info->prog == OPJ_PCRL) { /* PCRL */ + /* I suppose components have same XRsiz, YRsiz */ + int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x; + int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y; + int x1 = x0 + cstr_info->tile_x; + int y1 = y0 + cstr_info->tile_y; + + /* Count the maximum number of precincts */ + int max_numprec = 0; + for (resno = 0; resno < max_numdecompos + 1; resno++) { + int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; + if (numprec > max_numprec) + max_numprec = numprec; + } + + fprintf(stream, "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos"); + if (disto_on) + fprintf(stream, " disto"); + fprintf(stream,"\n"); + + for (precno = 0; precno < max_numprec; precno++) { + for (compno = 0; compno < cstr_info->numcomps; compno++) { + for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) { + int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; + int pcnx = cstr_info->tile[tileno].pw[resno]; + int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno ); + int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno ); + int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; + int precno_y = (int) floor( (float)precno/(float)pcnx ); + if (precno >= numprec) + continue; + for(y = y0; y < y1; y++) { + if (precno_y*pcy == y ) { + for (x = x0; x < x1; x++) { + if (precno_x*pcx == x ) { + for (layno = 0; layno < cstr_info->numlayers; layno++) { + start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; + end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; + end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; + disto = cstr_info->tile[tileno].packet[pack_nb].disto; + fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64, + pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos); + if (disto_on) + fprintf(stream, " %8e", disto); + fprintf(stream, "\n"); + total_disto += disto; + pack_nb++; + } + } + }/* x = x0..x1 */ + } + } /* y = y0..y1 */ + } /* resno */ + } /* compno */ + } /* precno */ + } /* PCRL */ + + else { /* CPRL */ + /* Count the maximum number of precincts */ + int max_numprec = 0; + for (resno = 0; resno < max_numdecompos + 1; resno++) { + int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; + if (numprec > max_numprec) + max_numprec = numprec; + } + + fprintf(stream, "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos"); + if (disto_on) + fprintf(stream, " disto"); + fprintf(stream,"\n"); + + for (compno = 0; compno < cstr_info->numcomps; compno++) { + /* I suppose components have same XRsiz, YRsiz */ + int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x; + int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y; + int x1 = x0 + cstr_info->tile_x; + int y1 = y0 + cstr_info->tile_y; + + for (precno = 0; precno < max_numprec; precno++) { + for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) { + int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; + int pcnx = cstr_info->tile[tileno].pw[resno]; + int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno ); + int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno ); + int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; + int precno_y = (int) floor( (float)precno/(float)pcnx ); + if (precno >= numprec) + continue; + + for(y = y0; y < y1; y++) { + if (precno_y*pcy == y ) { + for (x = x0; x < x1; x++) { + if (precno_x*pcx == x ) { + for (layno = 0; layno < cstr_info->numlayers; layno++) { + start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos; + end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos; + end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos; + disto = cstr_info->tile[tileno].packet[pack_nb].disto; + fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64, + pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos); + if (disto_on) + fprintf(stream, " %8e", disto); + fprintf(stream, "\n"); + total_disto += disto; + pack_nb++; + } + } + }/* x = x0..x1 */ + } + } /* y = y0..y1 */ + } /* resno */ + } /* precno */ + } /* compno */ + } /* CPRL */ + } /* tileno */ + + if (disto_on) { + fprintf(stream, "%8e\n", cstr_info->D_max); /* SE max */ + fprintf(stream, "%.8e\n", total_disto); /* SE totale */ + } + /* UniPG>> */ + /* print the markers' list */ + if (cstr_info->marknum) { + fprintf(stream, "\nMARKER LIST\n"); + fprintf(stream, "%d\n", cstr_info->marknum); + fprintf(stream, "type\tstart_pos length\n"); + for (x = 0; x < cstr_info->marknum; x++) + fprintf(stream, "%X\t%9" PRId64 " %9d\n", cstr_info->marker[x].type, cstr_info->marker[x].pos, cstr_info->marker[x].len); + } + /* <<UniPG */ + fclose(stream); + + fprintf(stderr,"Generated index file %s\n", index); + + return 0; } diff --git a/src/bin/jp2/index.h b/src/bin/jp2/index.h index d8b448d0..b461e91b 100644 --- a/src/bin/jp2/index.h +++ b/src/bin/jp2/index.h @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -40,7 +40,7 @@ extern "C" { /** Write a structured index to a file -@param cstr_info Codestream information +@param cstr_info Codestream information @param index Index filename @return Returns 0 if successful, returns 1 otherwise */ diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index 9d690a56..1332d3a8 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -8,7 +8,7 @@ * Copyright (c) 2002-2014, Professor Benoit Macq * Copyright (c) 2001-2003, David Janssens * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2007, Francois-Olivier Devaux * Copyright (c) 2003-2014, Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan @@ -71,14 +71,14 @@ #include "format_defs.h" #include "opj_string.h" -typedef struct dircnt{ +typedef struct dircnt { /** Buffer for holding images read from Directory*/ char *filename_buf; /** Pointer to the buffer*/ char **filename; -}dircnt_t; +} dircnt_t; -typedef struct img_folder{ +typedef struct img_folder { /** The directory path of the folder containing input images*/ char *imgdirpath; /** Output format*/ @@ -87,9 +87,10 @@ typedef struct img_folder{ char set_imgdir; /** Enable Cod Format for output*/ char set_out_format; -}img_fol_t; +} img_fol_t; -static void encode_help_display(void) { +static void encode_help_display(void) +{ fprintf(stdout,"\nThis is the opj_compress utility from the OpenJPEG project.\n" "It compresses various image formats with the JPEG 2000 algorithm.\n" "It has been compiled against openjp2 library v%s.\n\n",opj_version()); @@ -108,9 +109,9 @@ static void encode_help_display(void) { fprintf(stdout," * No sub-sampling in x or y direction\n"); fprintf(stdout," * No mode switch activated\n"); fprintf(stdout," * Progression order: LRCP\n"); - #ifdef FIXME_INDEX +#ifdef FIXME_INDEX fprintf(stdout," * No index file\n"); - #endif /* FIXME_INDEX */ +#endif /* FIXME_INDEX */ fprintf(stdout," * No ROI upshifted\n"); fprintf(stdout," * No offset of the origin of the image\n"); fprintf(stdout," * No offset of the origin of the tiles\n"); @@ -218,10 +219,10 @@ static void encode_help_display(void) { fprintf(stdout," Divide packets of every tile into tile-parts.\n"); fprintf(stdout," Division is made by grouping Resolutions (R), Layers (L)\n"); fprintf(stdout," or Components (C).\n"); - #ifdef FIXME_INDEX +#ifdef FIXME_INDEX fprintf(stdout,"-x <index file>\n"); fprintf(stdout," Create an index file.\n"); - #endif /*FIXME_INDEX*/ +#endif /*FIXME_INDEX*/ fprintf(stdout,"-ROI c=<component index>,U=<upshifting value>\n"); fprintf(stdout," Quantization indices upshifted for a component. \n"); fprintf(stdout," Warning: This option does not implement the usual ROI (Region of Interest).\n"); @@ -353,7 +354,8 @@ static void encode_help_display(void) { #endif /*FIXME_INDEX*/ } -static OPJ_PROG_ORDER give_progression(const char progression[4]) { +static OPJ_PROG_ORDER give_progression(const char progression[4]) +{ if(strncmp(progression, "LRCP", 4) == 0) { return OPJ_LRCP; } @@ -373,7 +375,8 @@ static OPJ_PROG_ORDER give_progression(const char progression[4]) { return OPJ_PROG_UNKNOWN; } -static unsigned int get_num_images(char *imgdirpath){ +static unsigned int get_num_images(char *imgdirpath) +{ DIR *dir; struct dirent* content; unsigned int num_images = 0; @@ -381,13 +384,13 @@ static unsigned int get_num_images(char *imgdirpath){ /*Reading the input images from given input directory*/ dir= opendir(imgdirpath); - if(!dir){ + if(!dir) { fprintf(stderr,"Could not open Folder %s\n",imgdirpath); return 0; } num_images=0; - while((content=readdir(dir))!=NULL){ + while((content=readdir(dir))!=NULL) { if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) continue; num_images++; @@ -396,7 +399,8 @@ static unsigned int get_num_images(char *imgdirpath){ return num_images; } -static int load_images(dircnt_t *dirptr, char *imgdirpath){ +static int load_images(dircnt_t *dirptr, char *imgdirpath) +{ DIR *dir; struct dirent* content; int i = 0; @@ -404,25 +408,26 @@ static int load_images(dircnt_t *dirptr, char *imgdirpath){ /*Reading the input images from given input directory*/ dir= opendir(imgdirpath); - if(!dir){ + if(!dir) { fprintf(stderr,"Could not open Folder %s\n",imgdirpath); return 1; - }else { + } else { fprintf(stderr,"Folder opened successfully\n"); } - while((content=readdir(dir))!=NULL){ + while((content=readdir(dir))!=NULL) { if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) continue; strcpy(dirptr->filename[i],content->d_name); i++; } - closedir(dir); + closedir(dir); return 0; } -static int get_file_format(char *filename) { +static int get_file_format(char *filename) +{ unsigned int i; static const char *extension[] = { "pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc" @@ -442,12 +447,14 @@ static int get_file_format(char *filename) { return -1; } -static char * get_file_name(char *name){ +static char * get_file_name(char *name) +{ char *fname = strtok(name,"."); return fname; } -static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t *parameters){ +static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t *parameters) +{ char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; char *temp_p, temp1[OPJ_PATH_LEN]=""; @@ -460,14 +467,14 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_c if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) { return 1; } - + /*Set output file*/ strcpy(temp_ofname,get_file_name(image_filename)); - while((temp_p = strtok(NULL,".")) != NULL){ + while((temp_p = strtok(NULL,".")) != NULL) { strcat(temp_ofname,temp1); sprintf(temp1,".%s",temp_p); } - if(img_fol->set_out_format==1){ + if(img_fol->set_out_format==1) { sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) { return 1; @@ -479,10 +486,11 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_c /* ------------------------------------------------------------------------------------ */ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters, - img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename, size_t indexfilename_size) { + img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename, size_t indexfilename_size) +{ OPJ_UINT32 i, j; int totlen, c; - opj_option_t long_option[]={ + opj_option_t long_option[]= { {"cinema2K",REQ_ARG, NULL ,'w'}, {"cinema4K",NO_ARG, NULL ,'y'}, {"ImgDir",REQ_ARG, NULL ,'z'}, @@ -498,22 +506,21 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param /* parse the command line */ const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:" - #ifdef USE_JPWL - "W:" - #endif /* USE_JPWL */ - "h"; +#ifdef USE_JPWL + "W:" +#endif /* USE_JPWL */ + "h"; totlen=sizeof(long_option); img_fol->set_out_format=0; raw_cp->rawWidth = 0; - do{ + do { c = opj_getopt_long(argc, argv, optlist,long_option,totlen); if (c == -1) break; switch (c) { - case 'i': /* input file */ - { + case 'i': { /* input file */ char *infile = opj_optarg; parameters->decod_format = get_file_format(infile); switch(parameters->decod_format) { @@ -537,12 +544,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param return 1; } } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'o': /* output file */ - { + case 'o': { /* output file */ char *outfile = opj_optarg; parameters->cod_format = get_file_format(outfile); switch(parameters->cod_format) { @@ -557,11 +563,10 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param return 1; } } - break; + break; - /* ----------------------------------------------------- */ - case 'O': /* output format */ - { + /* ----------------------------------------------------- */ + case 'O': { /* output format */ char outformat[50]; char *of = opj_optarg; sprintf(outformat,".%s",of); @@ -577,14 +582,13 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param return 1; } } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'r': /* rates rates/distorsion */ - { + case 'r': { /* rates rates/distorsion */ char *s = opj_optarg; parameters->tcp_numlayers = 0; while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) == 1) { @@ -598,13 +602,12 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } parameters->cp_disto_alloc = 1; } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'F': /* Raw image format parameters */ - { + case 'F': { /* Raw image format parameters */ OPJ_BOOL wrong = OPJ_FALSE; char *substr1; char *substr2; @@ -687,12 +690,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param return 1; } } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'q': /* add fixed_quality */ - { + case 'q': { /* add fixed_quality */ char *s = opj_optarg; while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers]) == 1) { parameters->tcp_numlayers++; @@ -705,13 +707,12 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } parameters->cp_fixed_quality = 1; } - break; + break; - /* dda */ - /* ----------------------------------------------------- */ + /* dda */ + /* ----------------------------------------------------- */ - case 'f': /* mod fixed_quality (before : -q) */ - { + case 'f': { /* mod fixed_quality (before : -q) */ int *row = NULL, *col = NULL; OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0; @@ -753,28 +754,25 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } parameters->cp_fixed_alloc = 1; } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 't': /* tiles */ - { + case 't': { /* tiles */ sscanf(opj_optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy); parameters->tile_size_on = OPJ_TRUE; } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'n': /* resolution */ - { + case 'n': { /* resolution */ sscanf(opj_optarg, "%d", ¶meters->numresolution); } - break; + break; - /* ----------------------------------------------------- */ - case 'c': /* precinct dimension */ - { + /* ----------------------------------------------------- */ + case 'c': { /* precinct dimension */ char sep; int res_spec = 0; @@ -783,26 +781,23 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param do { sep = 0; ret = sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec], - ¶meters->prch_init[res_spec], &sep); - if( !(ret == 2 && sep == 0) && !(ret == 3 && sep == ',') ) - { - fprintf(stderr,"\nError: could not parse precinct dimension: '%s' %x\n", s, sep); - fprintf(stderr,"Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n"); - return 1; - } + ¶meters->prch_init[res_spec], &sep); + if( !(ret == 2 && sep == 0) && !(ret == 3 && sep == ',') ) { + fprintf(stderr,"\nError: could not parse precinct dimension: '%s' %x\n", s, sep); + fprintf(stderr,"Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n"); + return 1; + } parameters->csty |= 0x01; res_spec++; s = strpbrk(s, "]") + 2; - } - while (sep == ','); + } while (sep == ','); parameters->res_spec = res_spec; } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'b': /* code-block dimension */ - { + case 'b': { /* code-block dimension */ int cblockw_init = 0, cblockh_init = 0; sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init); if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024 @@ -815,12 +810,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param parameters->cblockw_init = cblockw_init; parameters->cblockh_init = cblockh_init; } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'x': /* creation of index file */ - { + case 'x': { /* creation of index file */ if (opj_strcpy_s(indexfilename, indexfilename_size, opj_optarg) != 0) { return 1; } @@ -830,12 +824,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param " '-x' option ignored.\n"); /* << FIXME ADE INDEX */ } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'p': /* progression order */ - { + case 'p': { /* progression order */ char progression[4]; strncpy(progression, opj_optarg, 4); @@ -846,24 +839,22 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param return 1; } } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 's': /* subsampling factor */ - { + case 's': { /* subsampling factor */ if (sscanf(opj_optarg, "%d,%d", ¶meters->subsampling_dx, ¶meters->subsampling_dy) != 2) { fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n"); return 1; } } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'd': /* coordonnate of the reference grid */ - { + case 'd': { /* coordonnate of the reference grid */ if (sscanf(opj_optarg, "%d,%d", ¶meters->image_offset_x0, ¶meters->image_offset_y0) != 2) { fprintf(stderr, "-d 'coordonnate of the reference grid' argument " @@ -871,18 +862,17 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param return 1; } } - break; + break; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ case 'h': /* display an help description */ encode_help_display(); return 1; - /* ----------------------------------------------------- */ + /* ----------------------------------------------------- */ - case 'P': /* POC */ - { + case 'P': { /* POC */ int numpocs = 0; /* number of progression order change (POC) default 0 */ opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */ @@ -905,28 +895,25 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } parameters->numpocs = (OPJ_UINT32)numpocs; } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'S': /* SOP marker */ - { + case 'S': { /* SOP marker */ parameters->csty |= 0x02; } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'E': /* EPH marker */ - { + case 'E': { /* EPH marker */ parameters->csty |= 0x04; } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'M': /* Mode switch pas tous au point !! */ - { + case 'M': { /* Mode switch pas tous au point !! */ int value = 0; if (sscanf(opj_optarg, "%d", &value) == 1) { for (i = 0; i <= 5; i++) { @@ -936,85 +923,78 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } } } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'R': /* ROI */ - { + case 'R': { /* ROI */ if (sscanf(opj_optarg, "c=%d,U=%d", ¶meters->roi_compno, ¶meters->roi_shift) != 2) { fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n"); return 1; } } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'T': /* Tile offset */ - { + case 'T': { /* Tile offset */ if (sscanf(opj_optarg, "%d,%d", ¶meters->cp_tx0, ¶meters->cp_ty0) != 2) { fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]"); return 1; } } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'C': /* add a comment */ - { + case 'C': { /* add a comment */ parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1); if(parameters->cp_comment) { strcpy(parameters->cp_comment, opj_optarg); } } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'I': /* reversible or not */ - { + case 'I': { /* reversible or not */ parameters->irreversible = 1; } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'u': /* Tile part generation*/ - { + case 'u': { /* Tile part generation*/ parameters->tp_flag = opj_optarg[0]; parameters->tp_on = 1; } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'z': /* Image Directory path */ - { + case 'z': { /* Image Directory path */ img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); strcpy(img_fol->imgdirpath,opj_optarg); img_fol->set_imgdir=1; } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'w': /* Digital Cinema 2K profile compliance*/ - { + case 'w': { /* Digital Cinema 2K profile compliance*/ int fps=0; sscanf(opj_optarg,"%d",&fps); - if(fps == 24){ + if(fps == 24) { parameters->rsiz = OPJ_PROFILE_CINEMA_2K; parameters->max_comp_size = OPJ_CINEMA_24_COMP; parameters->max_cs_size = OPJ_CINEMA_24_CS; - }else if(fps == 48 ){ + } else if(fps == 48 ) { parameters->rsiz = OPJ_PROFILE_CINEMA_2K; parameters->max_comp_size = OPJ_CINEMA_48_COMP; parameters->max_cs_size = OPJ_CINEMA_48_CS; - }else { + } else { fprintf(stderr,"Incorrect value!! must be 24 or 48\n"); return 1; } @@ -1022,37 +1002,34 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param "Other options specified could be overriden\n"); } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'y': /* Digital Cinema 4K profile compliance*/ - { + case 'y': { /* Digital Cinema 4K profile compliance*/ parameters->rsiz = OPJ_PROFILE_CINEMA_4K; fprintf(stdout,"CINEMA 4K profile activated\n" "Other options specified could be overriden\n"); } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'Y': /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/ - { + case 'Y': { /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/ int mct_mode=0; sscanf(opj_optarg,"%d",&mct_mode); - if(mct_mode < 0 || mct_mode > 2){ + if(mct_mode < 0 || mct_mode > 2) { fprintf(stderr,"MCT incorrect value!! Current accepted values are 0, 1 or 2.\n"); return 1; } parameters->tcp_mct = (char) mct_mode; } - break; + break; - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'm': /* mct input file */ - { + case 'm': { /* mct input file */ char *lFilename = opj_optarg; char *lMatrix; char *lCurrentPtr ; @@ -1107,14 +1084,14 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param return 1; } lCurrentDoublePtr = lSpace; - for (i2=0;i2<lMctComp;++i2) { + for (i2=0; i2<lMctComp; ++i2) { lStrLen = strlen(lCurrentPtr) + 1; *lCurrentDoublePtr++ = (float) atof(lCurrentPtr); lCurrentPtr += lStrLen; } l_int_ptr = (int*) lCurrentDoublePtr; - for (i2=0;i2<lNbComp;++i2) { + for (i2=0; i2<lNbComp; ++i2) { lStrLen = strlen(lCurrentPtr) + 1; *l_int_ptr++ = atoi(lCurrentPtr); lCurrentPtr += lStrLen; @@ -1127,17 +1104,16 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param free(lSpace); free(lMatrix); } - break; + break; /* ------------------------------------------------------ */ /* UniPG>> */ #ifdef USE_JPWL - /* ------------------------------------------------------ */ + /* ------------------------------------------------------ */ - case 'W': /* JPWL capabilities switched on */ - { + case 'W': { /* JPWL capabilities switched on */ char *token = NULL; int hprot, pprot, sens, addr, size, range; @@ -1164,7 +1140,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param if(sscanf(token, "h=%d", &hprot) == 1) { /* Main header, specified */ if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) || - ((hprot >= 37) && (hprot <= 128)))) { + ((hprot >= 37) && (hprot <= 128)))) { fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n", hprot); return 1; } @@ -1173,7 +1149,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } else if(sscanf(token, "h%d=%d", &tile, &hprot) == 2) { /* Tile part header, specified */ if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) || - ((hprot >= 37) && (hprot <= 128)))) { + ((hprot >= 37) && (hprot <= 128)))) { fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n", hprot); return 1; } @@ -1219,7 +1195,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param if (sscanf(token, "p=%d", &pprot) == 1) { /* Method for all tiles and all packets */ if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { + ((pprot >= 37) && (pprot <= 128)))) { fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n", pprot); return 1; } @@ -1230,7 +1206,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) { /* method specified from that tile on */ if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { + ((pprot >= 37) && (pprot <= 128)))) { fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); return 1; } @@ -1247,7 +1223,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) { /* method fully specified from that tile and that packet on */ if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { + ((pprot >= 37) && (pprot <= 128)))) { fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); return 1; } @@ -1268,7 +1244,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) { /* default method from that tile and that packet on */ if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { + ((pprot >= 37) && (pprot <= 128)))) { fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); return 1; } @@ -1450,51 +1426,50 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param parameters->jpwl_epc_on = OPJ_TRUE; } - break; + break; #endif /* USE_JPWL */ - /* <<UniPG */ - /* ------------------------------------------------------ */ + /* <<UniPG */ + /* ------------------------------------------------------ */ - case 'J': /* jpip on */ - { + case 'J': { /* jpip on */ parameters->jpip_on = OPJ_TRUE; } - break; - /* ------------------------------------------------------ */ + break; + /* ------------------------------------------------------ */ default: fprintf(stderr, "[WARNING] An invalid option has been ignored\n"); break; } - }while(c != -1); + } while(c != -1); - if(img_fol->set_imgdir == 1){ - if(!(parameters->infile[0] == 0)){ + if(img_fol->set_imgdir == 1) { + if(!(parameters->infile[0] == 0)) { fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together !!\n"); return 1; } - if(img_fol->set_out_format == 0){ + if(img_fol->set_out_format == 0) { fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used !!\n"); fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n"); return 1; } - if(!((parameters->outfile[0] == 0))){ + if(!((parameters->outfile[0] == 0))) { fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together !!\n"); fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n"); return 1; } - }else{ + } else { if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { fprintf(stderr, "[ERROR] Required parameters are missing\n" - "Example: %s -i image.pgm -o image.j2k\n",argv[0]); + "Example: %s -i image.pgm -o image.j2k\n",argv[0]); fprintf(stderr, " Help: %s -h\n",argv[0]); return 1; } } if ( (parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0) - || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) { + || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) { fprintf(stderr,"[ERROR] invalid raw image parameters\n"); fprintf(stderr,"Please use the Format option -F:\n"); fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); @@ -1533,9 +1508,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param /* If subsampled image is provided, automatically disable MCT */ if ( ((parameters->decod_format == RAW_DFMT) || (parameters->decod_format == RAWL_DFMT)) - && ( ((raw_cp->rawComp > 1 ) && ((raw_cp->rawComps[1].dx > 1) || (raw_cp->rawComps[1].dy > 1))) - || ((raw_cp->rawComp > 2 ) && ((raw_cp->rawComps[2].dx > 1) || (raw_cp->rawComps[2].dy > 1))) - )) { + && ( ((raw_cp->rawComp > 1 ) && ((raw_cp->rawComps[1].dx > 1) || (raw_cp->rawComps[1].dy > 1))) + || ((raw_cp->rawComp > 2 ) && ((raw_cp->rawComps[2].dx > 1) || (raw_cp->rawComps[2].dy > 1))) + )) { parameters->tcp_mct = 0; } @@ -1547,43 +1522,47 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param /** sample error debug callback expecting no client object */ -static void error_callback(const char *msg, void *client_data) { +static void error_callback(const char *msg, void *client_data) +{ (void)client_data; fprintf(stdout, "[ERROR] %s", msg); } /** sample warning debug callback expecting no client object */ -static void warning_callback(const char *msg, void *client_data) { +static void warning_callback(const char *msg, void *client_data) +{ (void)client_data; fprintf(stdout, "[WARNING] %s", msg); } /** sample debug callback expecting no client object */ -static void info_callback(const char *msg, void *client_data) { +static void info_callback(const char *msg, void *client_data) +{ (void)client_data; fprintf(stdout, "[INFO] %s", msg); } -OPJ_FLOAT64 opj_clock(void) { +OPJ_FLOAT64 opj_clock(void) +{ #ifdef _WIN32 - /* _WIN32: use QueryPerformance (very accurate) */ + /* _WIN32: use QueryPerformance (very accurate) */ LARGE_INTEGER freq , t ; /* freq is the clock speed of the CPU */ QueryPerformanceFrequency(&freq) ; - /* cout << "freq = " << ((double) freq.QuadPart) << endl; */ + /* cout << "freq = " << ((double) freq.QuadPart) << endl; */ /* t is the high resolution performance counter (see MSDN) */ QueryPerformanceCounter ( & t ) ; return freq.QuadPart ? ( t.QuadPart /(OPJ_FLOAT64) freq.QuadPart ) : 0 ; #else - /* Unix or Linux: use resource usage */ + /* Unix or Linux: use resource usage */ struct rusage t; OPJ_FLOAT64 procTime; /* (1) Get the rusage data structure at this moment (man getrusage) */ getrusage(0,&t); /* (2) What is the elapsed time ? - CPU time = User time + System time */ - /* (2a) Get the seconds */ + /* (2a) Get the seconds */ procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec); /* (2b) More precisely! Get the microseconds part ! */ return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ; @@ -1596,7 +1575,8 @@ OPJ_FLOAT64 opj_clock(void) { * OPJ_COMPRESS MAIN */ /* -------------------------------------------------------------------------- */ -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ opj_cparameters_t parameters; /* compression parameters */ @@ -1639,35 +1619,35 @@ int main(int argc, char **argv) { } /* Read directory if necessary */ - if(img_fol.set_imgdir==1){ + if(img_fol.set_imgdir==1) { num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); - if(dirptr){ + if(dirptr) { dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ dirptr->filename = (char**) malloc(num_images*sizeof(char*)); - if(!dirptr->filename_buf){ + if(!dirptr->filename_buf) { return 0; } - for(i=0;i<num_images;i++){ + for(i=0; i<num_images; i++) { dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN; } } - if(load_images(dirptr,img_fol.imgdirpath)==1){ + if(load_images(dirptr,img_fol.imgdirpath)==1) { return 0; } - if (num_images==0){ + if (num_images==0) { fprintf(stdout,"Folder is empty\n"); return 0; } - }else{ + } else { num_images=1; } /*Encoding image one by one*/ - for(imageno=0;imageno<num_images;imageno++) { + for(imageno=0; imageno<num_images; imageno++) { image = NULL; fprintf(stderr,"\n"); - if(img_fol.set_imgdir==1){ + if(img_fol.set_imgdir==1) { if (get_next_file((int)imageno, dirptr,&img_fol, ¶meters)) { fprintf(stderr,"skipping file...\n"); continue; @@ -1769,8 +1749,8 @@ int main(int argc, char **argv) { } /* Can happen if input file is TIFF or PNG - * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined -*/ + * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined + */ if( !image) { fprintf(stderr, "Unable to load file: got no image\n"); return 1; @@ -1780,12 +1760,12 @@ int main(int argc, char **argv) { if (parameters.tcp_mct == (char) 255) { /* mct mode has not been set in commandline */ parameters.tcp_mct = (image->numcomps >= 3) ? 1 : 0; } else { /* mct mode has been set in commandline */ - if ((parameters.tcp_mct == 1) && (image->numcomps < 3)){ + if ((parameters.tcp_mct == 1) && (image->numcomps < 3)) { fprintf(stderr, "RGB->YCC conversion cannot be used:\n"); fprintf(stderr, "Input image has less than 3 components\n"); return 1; } - if ((parameters.tcp_mct == 2) && (!parameters.mct_data)){ + if ((parameters.tcp_mct == 2) && (!parameters.mct_data)) { fprintf(stderr, "Custom MCT has been set but no array-based MCT\n"); fprintf(stderr, "has been provided. Aborting.\n"); return 1; @@ -1796,14 +1776,12 @@ int main(int argc, char **argv) { /* ---------------------------- */ switch(parameters.cod_format) { - case J2K_CFMT: /* JPEG-2000 codestream */ - { + case J2K_CFMT: { /* JPEG-2000 codestream */ /* Get a decoder handle */ l_codec = opj_create_compress(OPJ_CODEC_J2K); break; } - case JP2_CFMT: /* JPEG 2000 compressed image data */ - { + case JP2_CFMT: { /* JPEG 2000 compressed image data */ /* Get a decoder handle */ l_codec = opj_create_compress(OPJ_CODEC_JP2); break; @@ -1835,7 +1813,7 @@ int main(int argc, char **argv) { /* open a byte stream for writing and allocate memory for all tiles */ l_stream = opj_stream_create_default_file_stream(parameters.outfile,OPJ_FALSE); - if (! l_stream){ + if (! l_stream) { return 1; } @@ -1849,7 +1827,7 @@ int main(int argc, char **argv) { OPJ_UINT32 l_data_size = 512*512*3; l_data = (OPJ_BYTE*) calloc( 1,l_data_size); assert( l_data ); - for (i=0;i<l_nb_tiles;++i) { + for (i=0; i<l_nb_tiles; ++i) { if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) { fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i); opj_stream_destroy(l_stream); @@ -1859,8 +1837,7 @@ int main(int argc, char **argv) { } } free(l_data); - } - else { + } else { bSuccess = bSuccess && opj_encode(l_codec, l_stream); if (!bSuccess) { fprintf(stderr, "failed to encode image: opj_encode\n"); @@ -1876,11 +1853,11 @@ int main(int argc, char **argv) { opj_destroy_codec(l_codec); opj_image_destroy(image); fprintf(stderr, "failed to encode image\n"); - remove(parameters.outfile); + remove(parameters.outfile); return 1; } - num_compressed_files++; + num_compressed_files++; fprintf(stdout,"[INFO] Generated outfile %s\n",parameters.outfile); /* close and free the byte stream */ opj_stream_destroy(l_stream); @@ -1897,10 +1874,10 @@ int main(int argc, char **argv) { if(parameters.cp_comment) free(parameters.cp_comment); if(parameters.cp_matrice) free(parameters.cp_matrice); if(raw_cp.rawComps) free(raw_cp.rawComps); - + t = opj_clock() - t; if (num_compressed_files) { - fprintf(stdout, "encode time: %d ms \n", (int)((t * 1000.0)/(OPJ_FLOAT64)num_compressed_files)); + fprintf(stdout, "encode time: %d ms \n", (int)((t * 1000.0)/(OPJ_FLOAT64)num_compressed_files)); } return 0; diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c index f3b1cd5c..21b4cea4 100644 --- a/src/bin/jp2/opj_decompress.c +++ b/src/bin/jp2/opj_decompress.c @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -8,11 +8,11 @@ * Copyright (c) 2002-2014, Professor Benoit Macq * Copyright (c) 2001-2003, David Janssens * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2007, Francois-Olivier Devaux * Copyright (c) 2003-2014, Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan - * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR * Copyright (c) 2012, CS Systemes d'Information, France * All rights reserved. * @@ -77,80 +77,77 @@ #include "format_defs.h" #include "opj_string.h" -typedef struct dircnt{ - /** Buffer for holding images read from Directory*/ - char *filename_buf; - /** Pointer to the buffer*/ - char **filename; -}dircnt_t; - - -typedef struct img_folder{ - /** The directory path of the folder containing input images*/ - char *imgdirpath; - /** Output format*/ - const char *out_format; - /** Enable option*/ - char set_imgdir; - /** Enable Cod Format for output*/ - char set_out_format; - -}img_fol_t; - -typedef enum opj_prec_mode -{ - OPJ_PREC_MODE_CLIP, - OPJ_PREC_MODE_SCALE +typedef struct dircnt { + /** Buffer for holding images read from Directory*/ + char *filename_buf; + /** Pointer to the buffer*/ + char **filename; +} dircnt_t; + + +typedef struct img_folder { + /** The directory path of the folder containing input images*/ + char *imgdirpath; + /** Output format*/ + const char *out_format; + /** Enable option*/ + char set_imgdir; + /** Enable Cod Format for output*/ + char set_out_format; + +} img_fol_t; + +typedef enum opj_prec_mode { + OPJ_PREC_MODE_CLIP, + OPJ_PREC_MODE_SCALE } opj_precision_mode; -typedef struct opj_prec -{ - OPJ_UINT32 prec; - opj_precision_mode mode; -}opj_precision; - -typedef struct opj_decompress_params -{ - /** core library parameters */ - opj_dparameters_t core; - - /** input file name */ - char infile[OPJ_PATH_LEN]; - /** output file name */ - char outfile[OPJ_PATH_LEN]; - /** input file format 0: J2K, 1: JP2, 2: JPT */ - int decod_format; - /** output file format 0: PGX, 1: PxM, 2: BMP */ - int cod_format; - /** index file name */ - char indexfilename[OPJ_PATH_LEN]; - - /** Decoding area left boundary */ - OPJ_UINT32 DA_x0; - /** Decoding area right boundary */ - OPJ_UINT32 DA_x1; - /** Decoding area up boundary */ - OPJ_UINT32 DA_y0; - /** Decoding area bottom boundary */ - OPJ_UINT32 DA_y1; - /** Verbose mode */ - OPJ_BOOL m_verbose; - - /** tile number ot the decoded tile*/ - OPJ_UINT32 tile_index; - /** Nb of tile to decode */ - OPJ_UINT32 nb_tile_to_decode; - - opj_precision* precision; - OPJ_UINT32 nb_precision; - - /* force output colorspace to RGB */ - int force_rgb; - /* upsample components according to their dx/dy values */ - int upsample; - /* split output components to different files */ - int split_pnm; -}opj_decompress_parameters; +typedef struct opj_prec { + OPJ_UINT32 prec; + opj_precision_mode mode; +} opj_precision; + +typedef struct opj_decompress_params { + /** core library parameters */ + opj_dparameters_t core; + + /** input file name */ + char infile[OPJ_PATH_LEN]; + /** output file name */ + char outfile[OPJ_PATH_LEN]; + /** input file format 0: J2K, 1: JP2, 2: JPT */ + int decod_format; + /** output file format 0: PGX, 1: PxM, 2: BMP */ + int cod_format; + /** index file name */ + char indexfilename[OPJ_PATH_LEN]; + + /** Decoding area left boundary */ + OPJ_UINT32 DA_x0; + /** Decoding area right boundary */ + OPJ_UINT32 DA_x1; + /** Decoding area up boundary */ + OPJ_UINT32 DA_y0; + /** Decoding area bottom boundary */ + OPJ_UINT32 DA_y1; + /** Verbose mode */ + OPJ_BOOL m_verbose; + + /** tile number ot the decoded tile*/ + OPJ_UINT32 tile_index; + /** Nb of tile to decode */ + OPJ_UINT32 nb_tile_to_decode; + + opj_precision* precision; + OPJ_UINT32 nb_precision; + + /* force output colorspace to RGB */ + int force_rgb; + /* upsample components according to their dx/dy values */ + int upsample; + /* split output components to different files */ + int split_pnm; +} opj_decompress_parameters; /* -------------------------------------------------------------------------- */ /* Declarations */ @@ -166,253 +163,256 @@ int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsi static opj_image_t* convert_gray_to_rgb(opj_image_t* original); /* -------------------------------------------------------------------------- */ -static void decode_help_display(void) { - fprintf(stdout,"\nThis is the opj_decompress utility from the OpenJPEG project.\n" - "It decompresses JPEG 2000 codestreams to various image formats.\n" - "It has been compiled against openjp2 library v%s.\n\n",opj_version()); - - fprintf(stdout,"Parameters:\n" - "-----------\n" - "\n" - " -ImgDir <directory> \n" - " Image file Directory path \n" - " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n" - " REQUIRED only if -ImgDir is used\n" - " Output format for decompressed images.\n"); - fprintf(stdout," -i <compressed file>\n" - " REQUIRED only if an Input image directory is not specified\n" - " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n" - " is identified based on its suffix.\n"); - fprintf(stdout," -o <decompressed file>\n" - " REQUIRED\n" - " Currently accepts formats specified above (see OutFor option)\n" - " Binary data is written to the file (not ascii). If a PGX\n" - " filename is given, there will be as many output files as there are\n" - " components: an indice starting from 0 will then be appended to the\n" - " output filename, just before the \"pgx\" extension. If a PGM filename\n" - " is given and there are more than one component, only the first component\n" - " will be written to the file.\n"); - fprintf(stdout," -r <reduce factor>\n" - " Set the number of highest resolution levels to be discarded. The\n" - " image resolution is effectively divided by 2 to the power of the\n" - " number of discarded levels. The reduce factor is limited by the\n" - " smallest total number of decomposition levels among tiles.\n" - " -l <number of quality layers to decode>\n" - " Set the maximum number of quality layers to decode. If there are\n" - " less quality layers than the specified number, all the quality layers\n" - " are decoded.\n"); - fprintf(stdout," -x \n" - " Create an index file *.Idx (-x index_name.Idx) \n" - " -d <x0,y0,x1,y1>\n" - " OPTIONAL\n" - " Decoding area\n" - " By default all the image is decoded.\n" - " -t <tile_number>\n" - " OPTIONAL\n" - " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n" - " By default all tiles are decoded.\n"); - fprintf(stdout," -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n" - " OPTIONAL\n" - " Force the precision (bit depth) of components.\n"); - fprintf(stdout," There shall be at least 1 value. Theres no limit on the number of values (comma separated, last values ignored if too much values).\n" - " If there are less values than components, the last value is used for remaining components.\n" - " If 'C' is specified (default), values are clipped.\n" - " If 'S' is specified, values are scaled.\n" - " A 0 value can be specified (meaning original bit depth).\n"); - fprintf(stdout," -force-rgb\n" - " Force output image colorspace to RGB\n" - " -upsample\n" - " Downsampled components will be upsampled to image size\n" - " -split-pnm\n" - " Split output components to different files when writing to PNM\n" - "\n"); -/* UniPG>> */ +static void decode_help_display(void) +{ + fprintf(stdout,"\nThis is the opj_decompress utility from the OpenJPEG project.\n" + "It decompresses JPEG 2000 codestreams to various image formats.\n" + "It has been compiled against openjp2 library v%s.\n\n",opj_version()); + + fprintf(stdout,"Parameters:\n" + "-----------\n" + "\n" + " -ImgDir <directory> \n" + " Image file Directory path \n" + " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n" + " REQUIRED only if -ImgDir is used\n" + " Output format for decompressed images.\n"); + fprintf(stdout," -i <compressed file>\n" + " REQUIRED only if an Input image directory is not specified\n" + " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n" + " is identified based on its suffix.\n"); + fprintf(stdout," -o <decompressed file>\n" + " REQUIRED\n" + " Currently accepts formats specified above (see OutFor option)\n" + " Binary data is written to the file (not ascii). If a PGX\n" + " filename is given, there will be as many output files as there are\n" + " components: an indice starting from 0 will then be appended to the\n" + " output filename, just before the \"pgx\" extension. If a PGM filename\n" + " is given and there are more than one component, only the first component\n" + " will be written to the file.\n"); + fprintf(stdout," -r <reduce factor>\n" + " Set the number of highest resolution levels to be discarded. The\n" + " image resolution is effectively divided by 2 to the power of the\n" + " number of discarded levels. The reduce factor is limited by the\n" + " smallest total number of decomposition levels among tiles.\n" + " -l <number of quality layers to decode>\n" + " Set the maximum number of quality layers to decode. If there are\n" + " less quality layers than the specified number, all the quality layers\n" + " are decoded.\n"); + fprintf(stdout," -x \n" + " Create an index file *.Idx (-x index_name.Idx) \n" + " -d <x0,y0,x1,y1>\n" + " OPTIONAL\n" + " Decoding area\n" + " By default all the image is decoded.\n" + " -t <tile_number>\n" + " OPTIONAL\n" + " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n" + " By default all tiles are decoded.\n"); + fprintf(stdout," -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n" + " OPTIONAL\n" + " Force the precision (bit depth) of components.\n"); + fprintf(stdout," There shall be at least 1 value. Theres no limit on the number of values (comma separated, last values ignored if too much values).\n" + " If there are less values than components, the last value is used for remaining components.\n" + " If 'C' is specified (default), values are clipped.\n" + " If 'S' is specified, values are scaled.\n" + " A 0 value can be specified (meaning original bit depth).\n"); + fprintf(stdout," -force-rgb\n" + " Force output image colorspace to RGB\n" + " -upsample\n" + " Downsampled components will be upsampled to image size\n" + " -split-pnm\n" + " Split output components to different files when writing to PNM\n" + "\n"); + /* UniPG>> */ #ifdef USE_JPWL - fprintf(stdout," -W <options>\n" - " Activates the JPWL correction capability, if the codestream complies.\n" - " Options can be a comma separated list of <param=val> tokens:\n" - " c, c=numcomps\n" - " numcomps is the number of expected components in the codestream\n" - " (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS); + fprintf(stdout," -W <options>\n" + " Activates the JPWL correction capability, if the codestream complies.\n" + " Options can be a comma separated list of <param=val> tokens:\n" + " c, c=numcomps\n" + " numcomps is the number of expected components in the codestream\n" + " (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS); #endif /* USE_JPWL */ -/* <<UniPG */ - fprintf(stdout,"\n"); + /* <<UniPG */ + fprintf(stdout,"\n"); } /* -------------------------------------------------------------------------- */ static OPJ_BOOL parse_precision(const char* option, opj_decompress_parameters* parameters) { - const char* l_remaining = option; - OPJ_BOOL l_result = OPJ_TRUE; - - /* reset */ - if (parameters->precision) { - free(parameters->precision); - parameters->precision = NULL; - } - parameters->nb_precision = 0U; - - for(;;) - { - int prec; - char mode; - char comma; - int count; - - count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma); - if (count == 1) { - mode = 'C'; - count++; - } - if ((count == 2) || (mode==',')) { - if (mode==',') { - mode = 'C'; - } - comma=','; - count = 3; - } - if (count == 3) { - if ((prec < 1) || (prec > 32)) { - fprintf(stderr,"Invalid precision %d in precision option %s\n", prec, option); - l_result = OPJ_FALSE; - break; - } - if ((mode != 'C') && (mode != 'S')) { - fprintf(stderr,"Invalid precision mode %c in precision option %s\n", mode, option); - l_result = OPJ_FALSE; - break; - } - if (comma != ',') { - fprintf(stderr,"Invalid character %c in precision option %s\n", comma, option); - l_result = OPJ_FALSE; - break; - } - - if (parameters->precision == NULL) { - /* first one */ - parameters->precision = (opj_precision *)malloc(sizeof(opj_precision)); - if (parameters->precision == NULL) { - fprintf(stderr,"Could not allocate memory for precision option\n"); - l_result = OPJ_FALSE; - break; - } - } else { - OPJ_UINT32 l_new_size = parameters->nb_precision + 1U; - opj_precision* l_new; - - if (l_new_size == 0U) { - fprintf(stderr,"Could not allocate memory for precision option\n"); - l_result = OPJ_FALSE; - break; - } - - l_new = (opj_precision *)realloc(parameters->precision, l_new_size * sizeof(opj_precision)); - if (l_new == NULL) { - fprintf(stderr,"Could not allocate memory for precision option\n"); - l_result = OPJ_FALSE; - break; - } - parameters->precision = l_new; - } - - parameters->precision[parameters->nb_precision].prec = (OPJ_UINT32)prec; - switch (mode) { - case 'C': - parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP; - break; - case 'S': - parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE; - break; - default: - break; - } - parameters->nb_precision++; - - l_remaining = strchr(l_remaining, ','); - if (l_remaining == NULL) { - break; - } - l_remaining += 1; - } else { - fprintf(stderr,"Could not parse precision option %s\n", option); - l_result = OPJ_FALSE; - break; - } - } - - return l_result; + const char* l_remaining = option; + OPJ_BOOL l_result = OPJ_TRUE; + + /* reset */ + if (parameters->precision) { + free(parameters->precision); + parameters->precision = NULL; + } + parameters->nb_precision = 0U; + + for(;;) { + int prec; + char mode; + char comma; + int count; + + count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma); + if (count == 1) { + mode = 'C'; + count++; + } + if ((count == 2) || (mode==',')) { + if (mode==',') { + mode = 'C'; + } + comma=','; + count = 3; + } + if (count == 3) { + if ((prec < 1) || (prec > 32)) { + fprintf(stderr,"Invalid precision %d in precision option %s\n", prec, option); + l_result = OPJ_FALSE; + break; + } + if ((mode != 'C') && (mode != 'S')) { + fprintf(stderr,"Invalid precision mode %c in precision option %s\n", mode, option); + l_result = OPJ_FALSE; + break; + } + if (comma != ',') { + fprintf(stderr,"Invalid character %c in precision option %s\n", comma, option); + l_result = OPJ_FALSE; + break; + } + + if (parameters->precision == NULL) { + /* first one */ + parameters->precision = (opj_precision *)malloc(sizeof(opj_precision)); + if (parameters->precision == NULL) { + fprintf(stderr,"Could not allocate memory for precision option\n"); + l_result = OPJ_FALSE; + break; + } + } else { + OPJ_UINT32 l_new_size = parameters->nb_precision + 1U; + opj_precision* l_new; + + if (l_new_size == 0U) { + fprintf(stderr,"Could not allocate memory for precision option\n"); + l_result = OPJ_FALSE; + break; + } + + l_new = (opj_precision *)realloc(parameters->precision, l_new_size * sizeof(opj_precision)); + if (l_new == NULL) { + fprintf(stderr,"Could not allocate memory for precision option\n"); + l_result = OPJ_FALSE; + break; + } + parameters->precision = l_new; + } + + parameters->precision[parameters->nb_precision].prec = (OPJ_UINT32)prec; + switch (mode) { + case 'C': + parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP; + break; + case 'S': + parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE; + break; + default: + break; + } + parameters->nb_precision++; + + l_remaining = strchr(l_remaining, ','); + if (l_remaining == NULL) { + break; + } + l_remaining += 1; + } else { + fprintf(stderr,"Could not parse precision option %s\n", option); + l_result = OPJ_FALSE; + break; + } + } + + return l_result; } /* -------------------------------------------------------------------------- */ -int get_num_images(char *imgdirpath){ - DIR *dir; - struct dirent* content; - int num_images = 0; - - /*Reading the input images from given input directory*/ - - dir= opendir(imgdirpath); - if(!dir){ - fprintf(stderr,"Could not open Folder %s\n",imgdirpath); - return 0; - } - - while((content=readdir(dir))!=NULL){ - if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) - continue; - num_images++; - } - closedir(dir); - return num_images; +int get_num_images(char *imgdirpath) +{ + DIR *dir; + struct dirent* content; + int num_images = 0; + + /*Reading the input images from given input directory*/ + + dir= opendir(imgdirpath); + if(!dir) { + fprintf(stderr,"Could not open Folder %s\n",imgdirpath); + return 0; + } + + while((content=readdir(dir))!=NULL) { + if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) + continue; + num_images++; + } + closedir(dir); + return num_images; } /* -------------------------------------------------------------------------- */ -int load_images(dircnt_t *dirptr, char *imgdirpath){ - DIR *dir; - struct dirent* content; - int i = 0; - - /*Reading the input images from given input directory*/ - - dir= opendir(imgdirpath); - if(!dir){ - fprintf(stderr,"Could not open Folder %s\n",imgdirpath); - return 1; - }else { - fprintf(stderr,"Folder opened successfully\n"); - } - - while((content=readdir(dir))!=NULL){ - if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) - continue; - - strcpy(dirptr->filename[i],content->d_name); - i++; - } - closedir(dir); - return 0; +int load_images(dircnt_t *dirptr, char *imgdirpath) +{ + DIR *dir; + struct dirent* content; + int i = 0; + + /*Reading the input images from given input directory*/ + + dir= opendir(imgdirpath); + if(!dir) { + fprintf(stderr,"Could not open Folder %s\n",imgdirpath); + return 1; + } else { + fprintf(stderr,"Folder opened successfully\n"); + } + + while((content=readdir(dir))!=NULL) { + if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) + continue; + + strcpy(dirptr->filename[i],content->d_name); + i++; + } + closedir(dir); + return 0; } /* -------------------------------------------------------------------------- */ -int get_file_format(const char *filename) { - unsigned int i; - static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" }; - static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT }; - const char * ext = strrchr(filename, '.'); - if (ext == NULL) - return -1; - ext++; - if(*ext) { - for(i = 0; i < sizeof(format)/sizeof(*format); i++) { - if(strcasecmp(ext, extension[i]) == 0) { - return format[i]; - } - } - } - - return -1; +int get_file_format(const char *filename) +{ + unsigned int i; + static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" }; + static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT }; + const char * ext = strrchr(filename, '.'); + if (ext == NULL) + return -1; + ext++; + if(*ext) { + for(i = 0; i < sizeof(format)/sizeof(*format); i++) { + if(strcasecmp(ext, extension[i]) == 0) { + return format[i]; + } + } + } + + return -1; } #ifdef _WIN32 @@ -422,33 +422,34 @@ const char* path_separator = "/"; #endif /* -------------------------------------------------------------------------- */ -char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters){ - char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; - char *temp_p, temp1[OPJ_PATH_LEN]=""; - - strcpy(image_filename,dirptr->filename[imageno]); - fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename); - sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator, image_filename); - parameters->decod_format = infile_format(infilename); - if (parameters->decod_format == -1) - return 1; - if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) { - return 1; - } - - /*Set output file*/ - strcpy(temp_ofname,strtok(image_filename,".")); - while((temp_p = strtok(NULL,".")) != NULL){ - strcat(temp_ofname,temp1); - sprintf(temp1,".%s",temp_p); - } - if(img_fol->set_out_format==1){ - sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); - if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) { - return 1; - } - } - return 0; +char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters) +{ + char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; + char *temp_p, temp1[OPJ_PATH_LEN]=""; + + strcpy(image_filename,dirptr->filename[imageno]); + fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename); + sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator, image_filename); + parameters->decod_format = infile_format(infilename); + if (parameters->decod_format == -1) + return 1; + if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) { + return 1; + } + + /*Set output file*/ + strcpy(temp_ofname,strtok(image_filename,".")); + while((temp_p = strtok(NULL,".")) != NULL) { + strcat(temp_ofname,temp1); + sprintf(temp1,".%s",temp_p); + } + if(img_fol->set_out_format==1) { + sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); + if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) { + return 1; + } + } + return 0; } /* -------------------------------------------------------------------------- */ @@ -459,52 +460,50 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompre static int infile_format(const char *fname) { - FILE *reader; - const char *s, *magic_s; - int ext_format, magic_format; - unsigned char buf[12]; - OPJ_SIZE_T l_nb_read; + FILE *reader; + const char *s, *magic_s; + int ext_format, magic_format; + unsigned char buf[12]; + OPJ_SIZE_T l_nb_read; - reader = fopen(fname, "rb"); + reader = fopen(fname, "rb"); - if (reader == NULL) - return -2; + if (reader == NULL) + return -2; - memset(buf, 0, 12); - l_nb_read = fread(buf, 1, 12, reader); - fclose(reader); - if (l_nb_read != 12) - return -1; + memset(buf, 0, 12); + l_nb_read = fread(buf, 1, 12, reader); + fclose(reader); + if (l_nb_read != 12) + return -1; - ext_format = get_file_format(fname); + ext_format = get_file_format(fname); - if (ext_format == JPT_CFMT) - return JPT_CFMT; + if (ext_format == JPT_CFMT) + return JPT_CFMT; - if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) { - magic_format = JP2_CFMT; - magic_s = ".jp2"; - } - else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) { - magic_format = J2K_CFMT; - magic_s = ".j2k or .jpc or .j2c"; - } - else - return -1; + if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) { + magic_format = JP2_CFMT; + magic_s = ".jp2"; + } else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) { + magic_format = J2K_CFMT; + magic_s = ".j2k or .jpc or .j2c"; + } else + return -1; - if (magic_format == ext_format) - return ext_format; + if (magic_format == ext_format) + return ext_format; - s = fname + strlen(fname) - 4; + s = fname + strlen(fname) - 4; - fputs("\n===========================================\n", stderr); - fprintf(stderr, "The extension of this file is incorrect.\n" - "FOUND %s. SHOULD BE %s\n", s, magic_s); - fputs("===========================================\n", stderr); + fputs("\n===========================================\n", stderr); + fprintf(stderr, "The extension of this file is incorrect.\n" + "FOUND %s. SHOULD BE %s\n", s, magic_s); + fputs("===========================================\n", stderr); - return magic_format; + return magic_format; } /* -------------------------------------------------------------------------- */ @@ -512,334 +511,323 @@ static int infile_format(const char *fname) * Parse the command line */ /* -------------------------------------------------------------------------- */ -int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol) { - /* parse the command line */ - int totlen, c; - opj_option_t long_option[]={ - {"ImgDir", REQ_ARG, NULL,'y'}, - {"OutFor", REQ_ARG, NULL,'O'}, - {"force-rgb", NO_ARG, NULL, 1}, - {"upsample", NO_ARG, NULL, 1}, - {"split-pnm", NO_ARG, NULL, 1} - }; - - const char optlist[] = "i:o:r:l:x:d:t:p:" - -/* UniPG>> */ +int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol) +{ + /* parse the command line */ + int totlen, c; + opj_option_t long_option[]= { + {"ImgDir", REQ_ARG, NULL,'y'}, + {"OutFor", REQ_ARG, NULL,'O'}, + {"force-rgb", NO_ARG, NULL, 1}, + {"upsample", NO_ARG, NULL, 1}, + {"split-pnm", NO_ARG, NULL, 1} + }; + + const char optlist[] = "i:o:r:l:x:d:t:p:" + + /* UniPG>> */ #ifdef USE_JPWL - "W:" + "W:" #endif /* USE_JPWL */ -/* <<UniPG */ - "h" ; - - long_option[2].flag = &(parameters->force_rgb); - long_option[3].flag = &(parameters->upsample); - long_option[4].flag = &(parameters->split_pnm); - totlen=sizeof(long_option); - opj_reset_options_reading(); - img_fol->set_out_format = 0; - do { - c = opj_getopt_long(argc, argv,optlist,long_option,totlen); - if (c == -1) - break; - switch (c) { - case 0: /* long opt with flag */ - break; - case 'i': /* input file */ - { - char *infile = opj_optarg; - parameters->decod_format = infile_format(infile); - switch(parameters->decod_format) { - case J2K_CFMT: - break; - case JP2_CFMT: - break; - case JPT_CFMT: - break; - case -2: - fprintf(stderr, - "!! infile cannot be read: %s !!\n\n", - infile); - return 1; - default: - fprintf(stderr, - "[ERROR] Unknown input file format: %s \n" - " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n", - infile); - return 1; - } - if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) { - fprintf(stderr, "[ERROR] Path is too long\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'o': /* output file */ - { - char *outfile = opj_optarg; - parameters->cod_format = get_file_format(outfile); - switch(parameters->cod_format) { - case PGX_DFMT: - break; - case PXM_DFMT: - break; - case BMP_DFMT: - break; - case TIF_DFMT: - break; - case RAW_DFMT: - break; - case RAWL_DFMT: - break; - case TGA_DFMT: - break; - case PNG_DFMT: - break; - default: - fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outfile); - return 1; - } - if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) { - fprintf(stderr, "[ERROR] Path is too long\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'O': /* output format */ - { - char outformat[50]; - char *of = opj_optarg; - sprintf(outformat,".%s",of); - img_fol->set_out_format = 1; - parameters->cod_format = get_file_format(outformat); - switch(parameters->cod_format) { - case PGX_DFMT: - img_fol->out_format = "pgx"; - break; - case PXM_DFMT: - img_fol->out_format = "ppm"; - break; - case BMP_DFMT: - img_fol->out_format = "bmp"; - break; - case TIF_DFMT: - img_fol->out_format = "tif"; - break; - case RAW_DFMT: - img_fol->out_format = "raw"; - break; - case RAWL_DFMT: - img_fol->out_format = "rawl"; - break; - case TGA_DFMT: - img_fol->out_format = "raw"; - break; - case PNG_DFMT: - img_fol->out_format = "png"; - break; - default: - fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outformat); - return 1; - break; - } - } - break; - - /* ----------------------------------------------------- */ - - - case 'r': /* reduce option */ - { - sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce)); - } - break; - - /* ----------------------------------------------------- */ - - - case 'l': /* layering option */ - { - sscanf(opj_optarg, "%u", &(parameters->core.cp_layer)); - } - break; - - /* ----------------------------------------------------- */ - - case 'h': /* display an help description */ - decode_help_display(); - return 1; - + /* <<UniPG */ + "h" ; + + long_option[2].flag = &(parameters->force_rgb); + long_option[3].flag = &(parameters->upsample); + long_option[4].flag = &(parameters->split_pnm); + totlen=sizeof(long_option); + opj_reset_options_reading(); + img_fol->set_out_format = 0; + do { + c = opj_getopt_long(argc, argv,optlist,long_option,totlen); + if (c == -1) + break; + switch (c) { + case 0: /* long opt with flag */ + break; + case 'i': { /* input file */ + char *infile = opj_optarg; + parameters->decod_format = infile_format(infile); + switch(parameters->decod_format) { + case J2K_CFMT: + break; + case JP2_CFMT: + break; + case JPT_CFMT: + break; + case -2: + fprintf(stderr, + "!! infile cannot be read: %s !!\n\n", + infile); + return 1; + default: + fprintf(stderr, + "[ERROR] Unknown input file format: %s \n" + " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n", + infile); + return 1; + } + if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) { + fprintf(stderr, "[ERROR] Path is too long\n"); + return 1; + } + } + break; + + /* ----------------------------------------------------- */ + + case 'o': { /* output file */ + char *outfile = opj_optarg; + parameters->cod_format = get_file_format(outfile); + switch(parameters->cod_format) { + case PGX_DFMT: + break; + case PXM_DFMT: + break; + case BMP_DFMT: + break; + case TIF_DFMT: + break; + case RAW_DFMT: + break; + case RAWL_DFMT: + break; + case TGA_DFMT: + break; + case PNG_DFMT: + break; + default: + fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outfile); + return 1; + } + if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) { + fprintf(stderr, "[ERROR] Path is too long\n"); + return 1; + } + } + break; + + /* ----------------------------------------------------- */ + + case 'O': { /* output format */ + char outformat[50]; + char *of = opj_optarg; + sprintf(outformat,".%s",of); + img_fol->set_out_format = 1; + parameters->cod_format = get_file_format(outformat); + switch(parameters->cod_format) { + case PGX_DFMT: + img_fol->out_format = "pgx"; + break; + case PXM_DFMT: + img_fol->out_format = "ppm"; + break; + case BMP_DFMT: + img_fol->out_format = "bmp"; + break; + case TIF_DFMT: + img_fol->out_format = "tif"; + break; + case RAW_DFMT: + img_fol->out_format = "raw"; + break; + case RAWL_DFMT: + img_fol->out_format = "rawl"; + break; + case TGA_DFMT: + img_fol->out_format = "raw"; + break; + case PNG_DFMT: + img_fol->out_format = "png"; + break; + default: + fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outformat); + return 1; + break; + } + } + break; + + /* ----------------------------------------------------- */ + + + case 'r': { /* reduce option */ + sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce)); + } + break; + + /* ----------------------------------------------------- */ + + + case 'l': { /* layering option */ + sscanf(opj_optarg, "%u", &(parameters->core.cp_layer)); + } + break; + + /* ----------------------------------------------------- */ + + case 'h': /* display an help description */ + decode_help_display(); + return 1; + + /* ----------------------------------------------------- */ + + case 'y': { /* Image Directory path */ + img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); + strcpy(img_fol->imgdirpath,opj_optarg); + img_fol->set_imgdir=1; + } + break; + + /* ----------------------------------------------------- */ + + case 'd': { /* Input decode ROI */ + size_t size_optarg = (size_t)strlen(opj_optarg) + 1U; + char *ROI_values = (char*) malloc(size_optarg); + if (ROI_values == NULL) { + fprintf(stderr, "[ERROR] Couldn't allocate memory\n"); + return 1; + } + ROI_values[0] = '\0'; + memcpy(ROI_values, opj_optarg, size_optarg); + /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */ + parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1); + + free(ROI_values); + } + break; + + /* ----------------------------------------------------- */ + + case 't': { /* Input tile index */ + sscanf(opj_optarg, "%u", ¶meters->tile_index); + parameters->nb_tile_to_decode = 1; + } + break; + + /* ----------------------------------------------------- */ + + case 'x': { /* Creation of index file */ + if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename), opj_optarg) != 0) { + fprintf(stderr, "[ERROR] Path is too long\n"); + return 1; + } + } + break; + + /* ----------------------------------------------------- */ + case 'p': { /* Force precision */ + if (!parse_precision(opj_optarg, parameters)) { + return 1; + } + } + break; /* ----------------------------------------------------- */ - case 'y': /* Image Directory path */ - { - img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); - strcpy(img_fol->imgdirpath,opj_optarg); - img_fol->set_imgdir=1; - } - break; - - /* ----------------------------------------------------- */ - - case 'd': /* Input decode ROI */ - { - size_t size_optarg = (size_t)strlen(opj_optarg) + 1U; - char *ROI_values = (char*) malloc(size_optarg); - if (ROI_values == NULL) { - fprintf(stderr, "[ERROR] Couldn't allocate memory\n"); - return 1; - } - ROI_values[0] = '\0'; - memcpy(ROI_values, opj_optarg, size_optarg); - /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */ - parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1); - - free(ROI_values); - } - break; - - /* ----------------------------------------------------- */ - - case 't': /* Input tile index */ - { - sscanf(opj_optarg, "%u", ¶meters->tile_index); - parameters->nb_tile_to_decode = 1; - } - break; - - /* ----------------------------------------------------- */ - - case 'x': /* Creation of index file */ - { - if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename), opj_optarg) != 0) { - fprintf(stderr, "[ERROR] Path is too long\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - case 'p': /* Force precision */ - { - if (!parse_precision(opj_optarg, parameters)) - { - return 1; - } - } - break; - /* ----------------------------------------------------- */ - - /* UniPG>> */ + /* UniPG>> */ #ifdef USE_JPWL - - case 'W': /* activate JPWL correction */ - { - char *token = NULL; - - token = strtok(opj_optarg, ","); - while(token != NULL) { - - /* search expected number of components */ - if (*token == 'c') { - - static int compno; - - compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */ - - if(sscanf(token, "c=%d", &compno) == 1) { - /* Specified */ - if ((compno < 1) || (compno > 256)) { - fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno); - return 1; - } - parameters->jpwl_exp_comps = compno; - - } else if (!strcmp(token, "c")) { - /* default */ - parameters->jpwl_exp_comps = compno; /* auto for default size */ - - } else { - fprintf(stderr, "ERROR -> invalid components specified = %s\n", token); - return 1; - }; - } - - /* search maximum number of tiles */ - if (*token == 't') { - - static int tileno; - - tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */ - - if(sscanf(token, "t=%d", &tileno) == 1) { - /* Specified */ - if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) { - fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno); - return 1; - } - parameters->jpwl_max_tiles = tileno; - - } else if (!strcmp(token, "t")) { - /* default */ - parameters->jpwl_max_tiles = tileno; /* auto for default size */ - - } else { - fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token); - return 1; - }; - } - - /* next token or bust */ - token = strtok(NULL, ","); - }; - parameters->jpwl_correct = OPJ_TRUE; - fprintf(stdout, "JPWL correction capability activated\n"); - fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps); - } - break; + + case 'W': { /* activate JPWL correction */ + char *token = NULL; + + token = strtok(opj_optarg, ","); + while(token != NULL) { + + /* search expected number of components */ + if (*token == 'c') { + + static int compno; + + compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */ + + if(sscanf(token, "c=%d", &compno) == 1) { + /* Specified */ + if ((compno < 1) || (compno > 256)) { + fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno); + return 1; + } + parameters->jpwl_exp_comps = compno; + + } else if (!strcmp(token, "c")) { + /* default */ + parameters->jpwl_exp_comps = compno; /* auto for default size */ + + } else { + fprintf(stderr, "ERROR -> invalid components specified = %s\n", token); + return 1; + }; + } + + /* search maximum number of tiles */ + if (*token == 't') { + + static int tileno; + + tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */ + + if(sscanf(token, "t=%d", &tileno) == 1) { + /* Specified */ + if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) { + fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno); + return 1; + } + parameters->jpwl_max_tiles = tileno; + + } else if (!strcmp(token, "t")) { + /* default */ + parameters->jpwl_max_tiles = tileno; /* auto for default size */ + + } else { + fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token); + return 1; + }; + } + + /* next token or bust */ + token = strtok(NULL, ","); + }; + parameters->jpwl_correct = OPJ_TRUE; + fprintf(stdout, "JPWL correction capability activated\n"); + fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps); + } + break; #endif /* USE_JPWL */ -/* <<UniPG */ + /* <<UniPG */ + + /* ----------------------------------------------------- */ - /* ----------------------------------------------------- */ - default: fprintf(stderr, "[WARNING] An invalid option has been ignored.\n"); break; - } - }while(c != -1); + } + } while(c != -1); - /* check for possible errors */ - if(img_fol->set_imgdir==1){ - if(!(parameters->infile[0]==0)){ + /* check for possible errors */ + if(img_fol->set_imgdir==1) { + if(!(parameters->infile[0]==0)) { fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n"); - return 1; - } - if(img_fol->set_out_format == 0){ + return 1; + } + if(img_fol->set_out_format == 0) { fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n"); fprintf(stderr, "Only one format allowed.\n" - "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n"); - return 1; - } - if(!((parameters->outfile[0] == 0))){ + "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n"); + return 1; + } + if(!((parameters->outfile[0] == 0))) { fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n"); - return 1; - } - }else{ - if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { + return 1; + } + } else { + if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { fprintf(stderr, "[ERROR] Required parameters are missing\n" - "Example: %s -i image.j2k -o image.pgm\n",argv[0]); + "Example: %s -i image.j2k -o image.pgm\n",argv[0]); fprintf(stderr, " Help: %s -h\n",argv[0]); - return 1; - } - } + return 1; + } + } - return 0; + return 0; } /* -------------------------------------------------------------------------- */ @@ -850,46 +838,48 @@ int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *para /* -------------------------------------------------------------------------- */ int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1) { - int it = 0; - int values[4]; - char delims[] = ","; - char *result = NULL; - result = strtok( inArg, delims ); - - while( (result != NULL) && (it < 4 ) ) { - values[it] = atoi(result); - result = strtok( NULL, delims ); - it++; - } - - if (it != 4) { - return EXIT_FAILURE; - } - else{ - *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1]; - *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3]; - return EXIT_SUCCESS; - } + int it = 0; + int values[4]; + char delims[] = ","; + char *result = NULL; + result = strtok( inArg, delims ); + + while( (result != NULL) && (it < 4 ) ) { + values[it] = atoi(result); + result = strtok( NULL, delims ); + it++; + } + + if (it != 4) { + return EXIT_FAILURE; + } else { + *DA_x0 = (OPJ_UINT32)values[0]; + *DA_y0 = (OPJ_UINT32)values[1]; + *DA_x1 = (OPJ_UINT32)values[2]; + *DA_y1 = (OPJ_UINT32)values[3]; + return EXIT_SUCCESS; + } } -OPJ_FLOAT64 opj_clock(void) { +OPJ_FLOAT64 opj_clock(void) +{ #ifdef _WIN32 - /* _WIN32: use QueryPerformance (very accurate) */ + /* _WIN32: use QueryPerformance (very accurate) */ LARGE_INTEGER freq , t ; /* freq is the clock speed of the CPU */ QueryPerformanceFrequency(&freq) ; - /* cout << "freq = " << ((double) freq.QuadPart) << endl; */ + /* cout << "freq = " << ((double) freq.QuadPart) << endl; */ /* t is the high resolution performance counter (see MSDN) */ QueryPerformanceCounter ( & t ) ; - return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) : 0; + return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) : 0; #else - /* Unix or Linux: use resource usage */ + /* Unix or Linux: use resource usage */ struct rusage t; OPJ_FLOAT64 procTime; /* (1) Get the rusage data structure at this moment (man getrusage) */ getrusage(0,&t); /* (2) What is the elapsed time ? - CPU time = User time + System time */ - /* (2a) Get the seconds */ + /* (2a) Get the seconds */ procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec); /* (2b) More precisely! Get the microseconds part ! */ return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ; @@ -901,275 +891,277 @@ OPJ_FLOAT64 opj_clock(void) { /** sample error callback expecting a FILE* client object */ -static void error_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[ERROR] %s", msg); +static void error_callback(const char *msg, void *client_data) +{ + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); } /** sample warning callback expecting a FILE* client object */ -static void warning_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[WARNING] %s", msg); +static void warning_callback(const char *msg, void *client_data) +{ + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); } /** sample debug callback expecting no client object */ -static void info_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[INFO] %s", msg); +static void info_callback(const char *msg, void *client_data) +{ + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); } static void set_default_parameters(opj_decompress_parameters* parameters) { - if (parameters) { - memset(parameters, 0, sizeof(opj_decompress_parameters)); - - /* default decoding parameters (command line specific) */ - parameters->decod_format = -1; - parameters->cod_format = -1; - - /* default decoding parameters (core) */ - opj_set_default_decoder_parameters(&(parameters->core)); - } + if (parameters) { + memset(parameters, 0, sizeof(opj_decompress_parameters)); + + /* default decoding parameters (command line specific) */ + parameters->decod_format = -1; + parameters->cod_format = -1; + + /* default decoding parameters (core) */ + opj_set_default_decoder_parameters(&(parameters->core)); + } } static void destroy_parameters(opj_decompress_parameters* parameters) { - if (parameters) { - if (parameters->precision) { - free(parameters->precision); - parameters->precision = NULL; - } - } + if (parameters) { + if (parameters->precision) { + free(parameters->precision); + parameters->precision = NULL; + } + } } /* -------------------------------------------------------------------------- */ static opj_image_t* convert_gray_to_rgb(opj_image_t* original) { - OPJ_UINT32 compno; - opj_image_t* l_new_image = NULL; - opj_image_cmptparm_t* l_new_components = NULL; - - l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) * sizeof(opj_image_cmptparm_t)); - if (l_new_components == NULL) { - fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n"); - opj_image_destroy(original); - return NULL; - } - - l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp = original->comps[0].bpp; - l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx = original->comps[0].dx; - l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy = original->comps[0].dy; - l_new_components[0].h = l_new_components[1].h = l_new_components[2].h = original->comps[0].h; - l_new_components[0].w = l_new_components[1].w = l_new_components[2].w = original->comps[0].w; - l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec = original->comps[0].prec; - l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd = original->comps[0].sgnd; - l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 = original->comps[0].x0; - l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 = original->comps[0].y0; - - for(compno = 1U; compno < original->numcomps; ++compno) { - l_new_components[compno+2U].bpp = original->comps[compno].bpp; - l_new_components[compno+2U].dx = original->comps[compno].dx; - l_new_components[compno+2U].dy = original->comps[compno].dy; - l_new_components[compno+2U].h = original->comps[compno].h; - l_new_components[compno+2U].w = original->comps[compno].w; - l_new_components[compno+2U].prec = original->comps[compno].prec; - l_new_components[compno+2U].sgnd = original->comps[compno].sgnd; - l_new_components[compno+2U].x0 = original->comps[compno].x0; - l_new_components[compno+2U].y0 = original->comps[compno].y0; - } - - l_new_image = opj_image_create(original->numcomps + 2U, l_new_components, OPJ_CLRSPC_SRGB); - free(l_new_components); - if (l_new_image == NULL) { - fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n"); - opj_image_destroy(original); - return NULL; - } - - l_new_image->x0 = original->x0; - l_new_image->x1 = original->x1; - l_new_image->y0 = original->y0; - l_new_image->y1 = original->y1; - - l_new_image->comps[0].factor = l_new_image->comps[1].factor = l_new_image->comps[2].factor = original->comps[0].factor; - l_new_image->comps[0].alpha = l_new_image->comps[1].alpha = l_new_image->comps[2].alpha = original->comps[0].alpha; - l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded = l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded; - - memcpy(l_new_image->comps[0].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); - memcpy(l_new_image->comps[1].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); - memcpy(l_new_image->comps[2].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); - - for(compno = 1U; compno < original->numcomps; ++compno) { - l_new_image->comps[compno+2U].factor = original->comps[compno].factor; - l_new_image->comps[compno+2U].alpha = original->comps[compno].alpha; - l_new_image->comps[compno+2U].resno_decoded = original->comps[compno].resno_decoded; - memcpy(l_new_image->comps[compno+2U].data, original->comps[compno].data, original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32)); - } - opj_image_destroy(original); - return l_new_image; + OPJ_UINT32 compno; + opj_image_t* l_new_image = NULL; + opj_image_cmptparm_t* l_new_components = NULL; + + l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) * sizeof(opj_image_cmptparm_t)); + if (l_new_components == NULL) { + fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n"); + opj_image_destroy(original); + return NULL; + } + + l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp = original->comps[0].bpp; + l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx = original->comps[0].dx; + l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy = original->comps[0].dy; + l_new_components[0].h = l_new_components[1].h = l_new_components[2].h = original->comps[0].h; + l_new_components[0].w = l_new_components[1].w = l_new_components[2].w = original->comps[0].w; + l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec = original->comps[0].prec; + l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd = original->comps[0].sgnd; + l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 = original->comps[0].x0; + l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 = original->comps[0].y0; + + for(compno = 1U; compno < original->numcomps; ++compno) { + l_new_components[compno+2U].bpp = original->comps[compno].bpp; + l_new_components[compno+2U].dx = original->comps[compno].dx; + l_new_components[compno+2U].dy = original->comps[compno].dy; + l_new_components[compno+2U].h = original->comps[compno].h; + l_new_components[compno+2U].w = original->comps[compno].w; + l_new_components[compno+2U].prec = original->comps[compno].prec; + l_new_components[compno+2U].sgnd = original->comps[compno].sgnd; + l_new_components[compno+2U].x0 = original->comps[compno].x0; + l_new_components[compno+2U].y0 = original->comps[compno].y0; + } + + l_new_image = opj_image_create(original->numcomps + 2U, l_new_components, OPJ_CLRSPC_SRGB); + free(l_new_components); + if (l_new_image == NULL) { + fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n"); + opj_image_destroy(original); + return NULL; + } + + l_new_image->x0 = original->x0; + l_new_image->x1 = original->x1; + l_new_image->y0 = original->y0; + l_new_image->y1 = original->y1; + + l_new_image->comps[0].factor = l_new_image->comps[1].factor = l_new_image->comps[2].factor = original->comps[0].factor; + l_new_image->comps[0].alpha = l_new_image->comps[1].alpha = l_new_image->comps[2].alpha = original->comps[0].alpha; + l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded = l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded; + + memcpy(l_new_image->comps[0].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); + memcpy(l_new_image->comps[1].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); + memcpy(l_new_image->comps[2].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); + + for(compno = 1U; compno < original->numcomps; ++compno) { + l_new_image->comps[compno+2U].factor = original->comps[compno].factor; + l_new_image->comps[compno+2U].alpha = original->comps[compno].alpha; + l_new_image->comps[compno+2U].resno_decoded = original->comps[compno].resno_decoded; + memcpy(l_new_image->comps[compno+2U].data, original->comps[compno].data, original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32)); + } + opj_image_destroy(original); + return l_new_image; } /* -------------------------------------------------------------------------- */ static opj_image_t* upsample_image_components(opj_image_t* original) { - opj_image_t* l_new_image = NULL; - opj_image_cmptparm_t* l_new_components = NULL; - OPJ_BOOL l_upsample_need = OPJ_FALSE; - OPJ_UINT32 compno; - - for (compno = 0U; compno < original->numcomps; ++compno) { - if (original->comps[compno].factor > 0U) { - fprintf(stderr, "ERROR -> opj_decompress: -upsample not supported with reduction\n"); - opj_image_destroy(original); - return NULL; - } - if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) { - l_upsample_need = OPJ_TRUE; - break; - } - } - if (!l_upsample_need) { - return original; - } - /* Upsample is needed */ - l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(opj_image_cmptparm_t)); - if (l_new_components == NULL) { - fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n"); - opj_image_destroy(original); - return NULL; - } - - for (compno = 0U; compno < original->numcomps; ++compno) { - opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]); - opj_image_comp_t* l_org_cmp = &(original->comps[compno]); - - l_new_cmp->bpp = l_org_cmp->bpp; - l_new_cmp->prec = l_org_cmp->prec; - l_new_cmp->sgnd = l_org_cmp->sgnd; - l_new_cmp->x0 = original->x0; - l_new_cmp->y0 = original->y0; - l_new_cmp->dx = 1; - l_new_cmp->dy = 1; - l_new_cmp->w = l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */ - l_new_cmp->h = l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */ - - if (l_org_cmp->dx > 1U) { - l_new_cmp->w = original->x1 - original->x0; - } - - if (l_org_cmp->dy > 1U) { - l_new_cmp->h = original->y1 - original->y0; - } - } - - l_new_image = opj_image_create(original->numcomps, l_new_components, original->color_space); - free(l_new_components); - if (l_new_image == NULL) { - fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n"); - opj_image_destroy(original); - return NULL; - } - - l_new_image->x0 = original->x0; - l_new_image->x1 = original->x1; - l_new_image->y0 = original->y0; - l_new_image->y1 = original->y1; - - for (compno = 0U; compno < original->numcomps; ++compno) { - opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]); - opj_image_comp_t* l_org_cmp = &(original->comps[compno]); - - l_new_cmp->factor = l_org_cmp->factor; - l_new_cmp->alpha = l_org_cmp->alpha; - l_new_cmp->resno_decoded = l_org_cmp->resno_decoded; - - if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) { - const OPJ_INT32* l_src = l_org_cmp->data; - OPJ_INT32* l_dst = l_new_cmp->data; - OPJ_UINT32 y; - OPJ_UINT32 xoff, yoff; - - /* need to take into account dx & dy */ - xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0; - yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0; - if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) { - fprintf(stderr, "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n"); - opj_image_destroy(original); - opj_image_destroy(l_new_image); - return NULL; - } - - for (y = 0U; y < yoff; ++y) { - memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32)); - l_dst += l_new_cmp->w; - } - - if(l_new_cmp->h > (l_org_cmp->dy - 1U)) { /* check subtraction overflow for really small images */ - for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) { - OPJ_UINT32 x, dy; - OPJ_UINT32 xorg; - - xorg = 0U; - for (x = 0U; x < xoff; ++x) { - l_dst[x] = 0; - } - if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */ - for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) { - OPJ_UINT32 dx; - for (dx = 0U; dx < l_org_cmp->dx; ++dx) { - l_dst[x + dx] = l_src[xorg]; - } - } - } - for (; x < l_new_cmp->w; ++x) { - l_dst[x] = l_src[xorg]; - } - l_dst += l_new_cmp->w; - - for (dy = 1U; dy < l_org_cmp->dy; ++dy) { - memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32)); - l_dst += l_new_cmp->w; - } - l_src += l_org_cmp->w; - } - } - if (y < l_new_cmp->h) { - OPJ_UINT32 x; - OPJ_UINT32 xorg; - - xorg = 0U; - for (x = 0U; x < xoff; ++x) { - l_dst[x] = 0; - } - if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */ - for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) { - OPJ_UINT32 dx; - for (dx = 0U; dx < l_org_cmp->dx; ++dx) { - l_dst[x + dx] = l_src[xorg]; - } - } - } - for (; x < l_new_cmp->w; ++x) { - l_dst[x] = l_src[xorg]; - } - l_dst += l_new_cmp->w; - ++y; - for (; y < l_new_cmp->h; ++y) { - memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32)); - l_dst += l_new_cmp->w; - } - } - } - else { - memcpy(l_new_cmp->data, l_org_cmp->data, l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32)); - } - } - opj_image_destroy(original); - return l_new_image; + opj_image_t* l_new_image = NULL; + opj_image_cmptparm_t* l_new_components = NULL; + OPJ_BOOL l_upsample_need = OPJ_FALSE; + OPJ_UINT32 compno; + + for (compno = 0U; compno < original->numcomps; ++compno) { + if (original->comps[compno].factor > 0U) { + fprintf(stderr, "ERROR -> opj_decompress: -upsample not supported with reduction\n"); + opj_image_destroy(original); + return NULL; + } + if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) { + l_upsample_need = OPJ_TRUE; + break; + } + } + if (!l_upsample_need) { + return original; + } + /* Upsample is needed */ + l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(opj_image_cmptparm_t)); + if (l_new_components == NULL) { + fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n"); + opj_image_destroy(original); + return NULL; + } + + for (compno = 0U; compno < original->numcomps; ++compno) { + opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]); + opj_image_comp_t* l_org_cmp = &(original->comps[compno]); + + l_new_cmp->bpp = l_org_cmp->bpp; + l_new_cmp->prec = l_org_cmp->prec; + l_new_cmp->sgnd = l_org_cmp->sgnd; + l_new_cmp->x0 = original->x0; + l_new_cmp->y0 = original->y0; + l_new_cmp->dx = 1; + l_new_cmp->dy = 1; + l_new_cmp->w = l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */ + l_new_cmp->h = l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */ + + if (l_org_cmp->dx > 1U) { + l_new_cmp->w = original->x1 - original->x0; + } + + if (l_org_cmp->dy > 1U) { + l_new_cmp->h = original->y1 - original->y0; + } + } + + l_new_image = opj_image_create(original->numcomps, l_new_components, original->color_space); + free(l_new_components); + if (l_new_image == NULL) { + fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n"); + opj_image_destroy(original); + return NULL; + } + + l_new_image->x0 = original->x0; + l_new_image->x1 = original->x1; + l_new_image->y0 = original->y0; + l_new_image->y1 = original->y1; + + for (compno = 0U; compno < original->numcomps; ++compno) { + opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]); + opj_image_comp_t* l_org_cmp = &(original->comps[compno]); + + l_new_cmp->factor = l_org_cmp->factor; + l_new_cmp->alpha = l_org_cmp->alpha; + l_new_cmp->resno_decoded = l_org_cmp->resno_decoded; + + if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) { + const OPJ_INT32* l_src = l_org_cmp->data; + OPJ_INT32* l_dst = l_new_cmp->data; + OPJ_UINT32 y; + OPJ_UINT32 xoff, yoff; + + /* need to take into account dx & dy */ + xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0; + yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0; + if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) { + fprintf(stderr, "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n"); + opj_image_destroy(original); + opj_image_destroy(l_new_image); + return NULL; + } + + for (y = 0U; y < yoff; ++y) { + memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32)); + l_dst += l_new_cmp->w; + } + + if(l_new_cmp->h > (l_org_cmp->dy - 1U)) { /* check subtraction overflow for really small images */ + for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) { + OPJ_UINT32 x, dy; + OPJ_UINT32 xorg; + + xorg = 0U; + for (x = 0U; x < xoff; ++x) { + l_dst[x] = 0; + } + if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */ + for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) { + OPJ_UINT32 dx; + for (dx = 0U; dx < l_org_cmp->dx; ++dx) { + l_dst[x + dx] = l_src[xorg]; + } + } + } + for (; x < l_new_cmp->w; ++x) { + l_dst[x] = l_src[xorg]; + } + l_dst += l_new_cmp->w; + + for (dy = 1U; dy < l_org_cmp->dy; ++dy) { + memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32)); + l_dst += l_new_cmp->w; + } + l_src += l_org_cmp->w; + } + } + if (y < l_new_cmp->h) { + OPJ_UINT32 x; + OPJ_UINT32 xorg; + + xorg = 0U; + for (x = 0U; x < xoff; ++x) { + l_dst[x] = 0; + } + if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */ + for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) { + OPJ_UINT32 dx; + for (dx = 0U; dx < l_org_cmp->dx; ++dx) { + l_dst[x + dx] = l_src[xorg]; + } + } + } + for (; x < l_new_cmp->w; ++x) { + l_dst[x] = l_src[xorg]; + } + l_dst += l_new_cmp->w; + ++y; + for (; y < l_new_cmp->h; ++y) { + memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32)); + l_dst += l_new_cmp->w; + } + } + } else { + memcpy(l_new_cmp->data, l_org_cmp->data, l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32)); + } + } + opj_image_destroy(original); + return l_new_image; } /* -------------------------------------------------------------------------- */ @@ -1179,398 +1171,381 @@ static opj_image_t* upsample_image_components(opj_image_t* original) /* -------------------------------------------------------------------------- */ int main(int argc, char **argv) { - opj_decompress_parameters parameters; /* decompression parameters */ - opj_image_t* image = NULL; - opj_stream_t *l_stream = NULL; /* Stream */ - opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ - opj_codestream_index_t* cstr_index = NULL; - - OPJ_INT32 num_images, imageno; - img_fol_t img_fol; - dircnt_t *dirptr = NULL; - int failed = 0; - OPJ_FLOAT64 t, tCumulative = 0; - OPJ_UINT32 numDecompressedImages = 0; - - /* set decoding parameters to default values */ - set_default_parameters(¶meters); - - /* Initialize img_fol */ - memset(&img_fol,0,sizeof(img_fol_t)); - - /* parse input and get user encoding parameters */ - if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) { - destroy_parameters(¶meters); - return EXIT_FAILURE; - } - - /* Initialize reading of directory */ - if(img_fol.set_imgdir==1){ - int it_image; - num_images=get_num_images(img_fol.imgdirpath); - - dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); - if(dirptr){ - dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ - dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); - - if(!dirptr->filename_buf){ - destroy_parameters(¶meters); - return EXIT_FAILURE; - } - for(it_image=0;it_image<num_images;it_image++){ - dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; - } - } - if(load_images(dirptr,img_fol.imgdirpath)==1){ - destroy_parameters(¶meters); - return EXIT_FAILURE; - } - if (num_images==0){ - fprintf(stdout,"Folder is empty\n"); - destroy_parameters(¶meters); - return EXIT_FAILURE; - } - }else{ - num_images=1; - } - - /*Decoding image one by one*/ - for(imageno = 0; imageno < num_images ; imageno++) { - - fprintf(stderr,"\n"); - - if(img_fol.set_imgdir==1){ - if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) { - fprintf(stderr,"skipping file...\n"); - destroy_parameters(¶meters); - continue; - } - } - - /* read the input file and put it in memory */ - /* ---------------------------------------- */ - - l_stream = opj_stream_create_default_file_stream(parameters.infile,1); - if (!l_stream){ - fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile); - destroy_parameters(¶meters); - return EXIT_FAILURE; - } - - /* decode the JPEG2000 stream */ - /* ---------------------- */ - - switch(parameters.decod_format) { - case J2K_CFMT: /* JPEG-2000 codestream */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(OPJ_CODEC_J2K); - break; - } - case JP2_CFMT: /* JPEG 2000 compressed image data */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(OPJ_CODEC_JP2); - break; - } - case JPT_CFMT: /* JPEG 2000, JPIP */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(OPJ_CODEC_JPT); - break; - } - default: - fprintf(stderr, "skipping file..\n"); - destroy_parameters(¶meters); - opj_stream_destroy(l_stream); - continue; - } - - /* catch events using our callbacks and give a local context */ - opj_set_info_handler(l_codec, info_callback,00); - opj_set_warning_handler(l_codec, warning_callback,00); - opj_set_error_handler(l_codec, error_callback,00); - - t = opj_clock(); - - /* Setup the decoder decoding parameters using user parameters */ - if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){ - fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n"); - destroy_parameters(¶meters); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - return EXIT_FAILURE; - } - - - /* Read the main header of the codestream and if necessary the JP2 boxes*/ - if(! opj_read_header(l_stream, l_codec, &image)){ - fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n"); - destroy_parameters(¶meters); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(image); - return EXIT_FAILURE; - } - - if (!parameters.nb_tile_to_decode) { - /* Optional if you want decode the entire image */ - if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0, - (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){ - fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n"); - destroy_parameters(¶meters); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(image); - return EXIT_FAILURE; - } - - /* Get the decoded image */ - if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) { - fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n"); - destroy_parameters(¶meters); - opj_destroy_codec(l_codec); - opj_stream_destroy(l_stream); - opj_image_destroy(image); - return EXIT_FAILURE; - } - } - else { - - /* It is just here to illustrate how to use the resolution after set parameters */ - /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) { - fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n"); - opj_destroy_codec(l_codec); - opj_stream_destroy(l_stream); - opj_image_destroy(image); - return EXIT_FAILURE; - }*/ - - if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) { - fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n"); - destroy_parameters(¶meters); - opj_destroy_codec(l_codec); - opj_stream_destroy(l_stream); - opj_image_destroy(image); - return EXIT_FAILURE; - } - fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index); - } - - tCumulative += opj_clock() - t; - numDecompressedImages++; - - /* Close the byte stream */ - opj_stream_destroy(l_stream); - - if( image->color_space != OPJ_CLRSPC_SYCC - && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy - && image->comps[1].dx != 1 ) - image->color_space = OPJ_CLRSPC_SYCC; - else if (image->numcomps <= 2) - image->color_space = OPJ_CLRSPC_GRAY; - - if(image->color_space == OPJ_CLRSPC_SYCC){ - color_sycc_to_rgb(image); - } - else if((image->color_space == OPJ_CLRSPC_CMYK) && (parameters.cod_format != TIF_DFMT)){ - color_cmyk_to_rgb(image); - } - else if(image->color_space == OPJ_CLRSPC_EYCC){ - color_esycc_to_rgb(image); - } - - if(image->icc_profile_buf) { + opj_decompress_parameters parameters; /* decompression parameters */ + opj_image_t* image = NULL; + opj_stream_t *l_stream = NULL; /* Stream */ + opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ + opj_codestream_index_t* cstr_index = NULL; + + OPJ_INT32 num_images, imageno; + img_fol_t img_fol; + dircnt_t *dirptr = NULL; + int failed = 0; + OPJ_FLOAT64 t, tCumulative = 0; + OPJ_UINT32 numDecompressedImages = 0; + + /* set decoding parameters to default values */ + set_default_parameters(¶meters); + + /* Initialize img_fol */ + memset(&img_fol,0,sizeof(img_fol_t)); + + /* parse input and get user encoding parameters */ + if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) { + destroy_parameters(¶meters); + return EXIT_FAILURE; + } + + /* Initialize reading of directory */ + if(img_fol.set_imgdir==1) { + int it_image; + num_images=get_num_images(img_fol.imgdirpath); + + dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); + if(dirptr) { + dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ + dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); + + if(!dirptr->filename_buf) { + destroy_parameters(¶meters); + return EXIT_FAILURE; + } + for(it_image=0; it_image<num_images; it_image++) { + dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; + } + } + if(load_images(dirptr,img_fol.imgdirpath)==1) { + destroy_parameters(¶meters); + return EXIT_FAILURE; + } + if (num_images==0) { + fprintf(stdout,"Folder is empty\n"); + destroy_parameters(¶meters); + return EXIT_FAILURE; + } + } else { + num_images=1; + } + + /*Decoding image one by one*/ + for(imageno = 0; imageno < num_images ; imageno++) { + + fprintf(stderr,"\n"); + + if(img_fol.set_imgdir==1) { + if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) { + fprintf(stderr,"skipping file...\n"); + destroy_parameters(¶meters); + continue; + } + } + + /* read the input file and put it in memory */ + /* ---------------------------------------- */ + + l_stream = opj_stream_create_default_file_stream(parameters.infile,1); + if (!l_stream) { + fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile); + destroy_parameters(¶meters); + return EXIT_FAILURE; + } + + /* decode the JPEG2000 stream */ + /* ---------------------- */ + + switch(parameters.decod_format) { + case J2K_CFMT: { /* JPEG-2000 codestream */ + /* Get a decoder handle */ + l_codec = opj_create_decompress(OPJ_CODEC_J2K); + break; + } + case JP2_CFMT: { /* JPEG 2000 compressed image data */ + /* Get a decoder handle */ + l_codec = opj_create_decompress(OPJ_CODEC_JP2); + break; + } + case JPT_CFMT: { /* JPEG 2000, JPIP */ + /* Get a decoder handle */ + l_codec = opj_create_decompress(OPJ_CODEC_JPT); + break; + } + default: + fprintf(stderr, "skipping file..\n"); + destroy_parameters(¶meters); + opj_stream_destroy(l_stream); + continue; + } + + /* catch events using our callbacks and give a local context */ + opj_set_info_handler(l_codec, info_callback,00); + opj_set_warning_handler(l_codec, warning_callback,00); + opj_set_error_handler(l_codec, error_callback,00); + + t = opj_clock(); + + /* Setup the decoder decoding parameters using user parameters */ + if ( !opj_setup_decoder(l_codec, &(parameters.core)) ) { + fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n"); + destroy_parameters(¶meters); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + return EXIT_FAILURE; + } + + + /* Read the main header of the codestream and if necessary the JP2 boxes*/ + if(! opj_read_header(l_stream, l_codec, &image)) { + fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n"); + destroy_parameters(¶meters); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(image); + return EXIT_FAILURE; + } + + if (!parameters.nb_tile_to_decode) { + /* Optional if you want decode the entire image */ + if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0, + (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)) { + fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n"); + destroy_parameters(¶meters); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(image); + return EXIT_FAILURE; + } + + /* Get the decoded image */ + if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) { + fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n"); + destroy_parameters(¶meters); + opj_destroy_codec(l_codec); + opj_stream_destroy(l_stream); + opj_image_destroy(image); + return EXIT_FAILURE; + } + } else { + + /* It is just here to illustrate how to use the resolution after set parameters */ + /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) { + fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n"); + opj_destroy_codec(l_codec); + opj_stream_destroy(l_stream); + opj_image_destroy(image); + return EXIT_FAILURE; + }*/ + + if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) { + fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n"); + destroy_parameters(¶meters); + opj_destroy_codec(l_codec); + opj_stream_destroy(l_stream); + opj_image_destroy(image); + return EXIT_FAILURE; + } + fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index); + } + + tCumulative += opj_clock() - t; + numDecompressedImages++; + + /* Close the byte stream */ + opj_stream_destroy(l_stream); + + if( image->color_space != OPJ_CLRSPC_SYCC + && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy + && image->comps[1].dx != 1 ) + image->color_space = OPJ_CLRSPC_SYCC; + else if (image->numcomps <= 2) + image->color_space = OPJ_CLRSPC_GRAY; + + if(image->color_space == OPJ_CLRSPC_SYCC) { + color_sycc_to_rgb(image); + } else if((image->color_space == OPJ_CLRSPC_CMYK) && (parameters.cod_format != TIF_DFMT)) { + color_cmyk_to_rgb(image); + } else if(image->color_space == OPJ_CLRSPC_EYCC) { + color_esycc_to_rgb(image); + } + + if(image->icc_profile_buf) { #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) - if(image->icc_profile_len) - color_apply_icc_profile(image); - else - color_cielab_to_rgb(image); + if(image->icc_profile_len) + color_apply_icc_profile(image); + else + color_cielab_to_rgb(image); #endif - free(image->icc_profile_buf); - image->icc_profile_buf = NULL; image->icc_profile_len = 0; - } - - /* Force output precision */ - /* ---------------------- */ - if (parameters.precision != NULL) - { - OPJ_UINT32 compno; - for (compno = 0; compno < image->numcomps; ++compno) - { - OPJ_UINT32 precno = compno; - OPJ_UINT32 prec; - - if (precno >= parameters.nb_precision) { - precno = parameters.nb_precision - 1U; - } - - prec = parameters.precision[precno].prec; - if (prec == 0) { - prec = image->comps[compno].prec; - } - - switch (parameters.precision[precno].mode) { - case OPJ_PREC_MODE_CLIP: - clip_component(&(image->comps[compno]), prec); - break; - case OPJ_PREC_MODE_SCALE: - scale_component(&(image->comps[compno]), prec); - break; - default: - break; - } - - } - } - - /* Upsample components */ - /* ------------------- */ - if (parameters.upsample) - { - image = upsample_image_components(image); - if (image == NULL) { - fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n"); - destroy_parameters(¶meters); - opj_destroy_codec(l_codec); - return EXIT_FAILURE; - } - } - - /* Force RGB output */ - /* ---------------- */ - if (parameters.force_rgb) - { - switch (image->color_space) { - case OPJ_CLRSPC_SRGB: - break; - case OPJ_CLRSPC_GRAY: - image = convert_gray_to_rgb(image); - break; - default: - fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n"); - opj_image_destroy(image); - image = NULL; - break; - } - if (image == NULL) { - fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n"); - destroy_parameters(¶meters); - opj_destroy_codec(l_codec); - return EXIT_FAILURE; - } - } - - /* create output image */ - /* ------------------- */ - switch (parameters.cod_format) { - case PXM_DFMT: /* PNM PGM PPM */ - if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) { + free(image->icc_profile_buf); + image->icc_profile_buf = NULL; + image->icc_profile_len = 0; + } + + /* Force output precision */ + /* ---------------------- */ + if (parameters.precision != NULL) { + OPJ_UINT32 compno; + for (compno = 0; compno < image->numcomps; ++compno) { + OPJ_UINT32 precno = compno; + OPJ_UINT32 prec; + + if (precno >= parameters.nb_precision) { + precno = parameters.nb_precision - 1U; + } + + prec = parameters.precision[precno].prec; + if (prec == 0) { + prec = image->comps[compno].prec; + } + + switch (parameters.precision[precno].mode) { + case OPJ_PREC_MODE_CLIP: + clip_component(&(image->comps[compno]), prec); + break; + case OPJ_PREC_MODE_SCALE: + scale_component(&(image->comps[compno]), prec); + break; + default: + break; + } + + } + } + + /* Upsample components */ + /* ------------------- */ + if (parameters.upsample) { + image = upsample_image_components(image); + if (image == NULL) { + fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n"); + destroy_parameters(¶meters); + opj_destroy_codec(l_codec); + return EXIT_FAILURE; + } + } + + /* Force RGB output */ + /* ---------------- */ + if (parameters.force_rgb) { + switch (image->color_space) { + case OPJ_CLRSPC_SRGB: + break; + case OPJ_CLRSPC_GRAY: + image = convert_gray_to_rgb(image); + break; + default: + fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n"); + opj_image_destroy(image); + image = NULL; + break; + } + if (image == NULL) { + fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n"); + destroy_parameters(¶meters); + opj_destroy_codec(l_codec); + return EXIT_FAILURE; + } + } + + /* create output image */ + /* ------------------- */ + switch (parameters.cod_format) { + case PXM_DFMT: /* PNM PGM PPM */ + if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) { fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; - case PGX_DFMT: /* PGX */ - if(imagetopgx(image, parameters.outfile)){ + case PGX_DFMT: /* PGX */ + if(imagetopgx(image, parameters.outfile)) { fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; - case BMP_DFMT: /* BMP */ - if(imagetobmp(image, parameters.outfile)){ + case BMP_DFMT: /* BMP */ + if(imagetobmp(image, parameters.outfile)) { fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; #ifdef OPJ_HAVE_LIBTIFF - case TIF_DFMT: /* TIFF */ - if(imagetotif(image, parameters.outfile)){ + case TIF_DFMT: /* TIFF */ + if(imagetotif(image, parameters.outfile)) { fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; #endif /* OPJ_HAVE_LIBTIFF */ - case RAW_DFMT: /* RAW */ - if(imagetoraw(image, parameters.outfile)){ + case RAW_DFMT: /* RAW */ + if(imagetoraw(image, parameters.outfile)) { fprintf(stderr,"[ERROR] Error generating raw file. Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; - case RAWL_DFMT: /* RAWL */ - if(imagetorawl(image, parameters.outfile)){ + case RAWL_DFMT: /* RAWL */ + if(imagetorawl(image, parameters.outfile)) { fprintf(stderr,"[ERROR] Error generating rawl file. Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; - case TGA_DFMT: /* TGA */ - if(imagetotga(image, parameters.outfile)){ + case TGA_DFMT: /* TGA */ + if(imagetotga(image, parameters.outfile)) { fprintf(stderr,"[ERROR] Error generating tga file. Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; #ifdef OPJ_HAVE_LIBPNG - case PNG_DFMT: /* PNG */ - if(imagetopng(image, parameters.outfile)){ + case PNG_DFMT: /* PNG */ + if(imagetopng(image, parameters.outfile)) { fprintf(stderr,"[ERROR] Error generating png file. Outfile %s not generated\n",parameters.outfile); - failed = 1; - } - else { + failed = 1; + } else { fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); - } - break; + } + break; #endif /* OPJ_HAVE_LIBPNG */ -/* Can happen if output file is TIFF or PNG - * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined -*/ - default: - fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); - failed = 1; - } + /* Can happen if output file is TIFF or PNG + * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined + */ + default: + fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile); + failed = 1; + } - /* free remaining structures */ - if (l_codec) { - opj_destroy_codec(l_codec); - } + /* free remaining structures */ + if (l_codec) { + opj_destroy_codec(l_codec); + } - /* free image data structure */ - opj_image_destroy(image); + /* free image data structure */ + opj_image_destroy(image); - /* destroy the codestream index */ - opj_destroy_cstr_index(&cstr_index); + /* destroy the codestream index */ + opj_destroy_cstr_index(&cstr_index); - if(failed) (void)remove(parameters.outfile); /* ignore return value */ - } - destroy_parameters(¶meters); - if (numDecompressedImages) { - fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages)); - } - return failed ? EXIT_FAILURE : EXIT_SUCCESS; + if(failed) (void)remove(parameters.outfile); /* ignore return value */ + } + destroy_parameters(¶meters); + if (numDecompressedImages) { + fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages)); + } + return failed ? EXIT_FAILURE : EXIT_SUCCESS; } /*end main*/ diff --git a/src/bin/jp2/opj_dump.c b/src/bin/jp2/opj_dump.c index 1e51f43c..5380f965 100644 --- a/src/bin/jp2/opj_dump.c +++ b/src/bin/jp2/opj_dump.c @@ -1,11 +1,11 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * * Copyright (c) 2010, Mathieu Malaterre, GDCM - * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France + * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France * Copyright (c) 2012, CS Systemes d'Information, France * All rights reserved. * @@ -59,26 +59,26 @@ #include "format_defs.h" #include "opj_string.h" -typedef struct dircnt{ - /** Buffer for holding images read from Directory*/ - char *filename_buf; - /** Pointer to the buffer*/ - char **filename; -}dircnt_t; +typedef struct dircnt { + /** Buffer for holding images read from Directory*/ + char *filename_buf; + /** Pointer to the buffer*/ + char **filename; +} dircnt_t; -typedef struct img_folder{ - /** The directory path of the folder containing input images*/ - char *imgdirpath; - /** Output format*/ - const char *out_format; - /** Enable option*/ - char set_imgdir; - /** Enable Cod Format for output*/ - char set_out_format; +typedef struct img_folder { + /** The directory path of the folder containing input images*/ + char *imgdirpath; + /** Output format*/ + const char *out_format; + /** Enable option*/ + char set_imgdir; + /** Enable Cod Format for output*/ + char set_out_format; - int flag; -}img_fol_t; + int flag; +} img_fol_t; /* -------------------------------------------------------------------------- */ /* Declarations */ @@ -91,7 +91,8 @@ static int infile_format(const char *fname); static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol); /* -------------------------------------------------------------------------- */ -static void decode_help_display(void) { +static void decode_help_display(void) +{ fprintf(stdout,"\nThis is the opj_dump utility from the OpenJPEG project.\n" "It dumps JPEG 2000 codestream info to stdout or a given file.\n" "It has been compiled against openjp2 library v%s.\n\n",opj_version()); @@ -100,120 +101,124 @@ static void decode_help_display(void) { fprintf(stdout,"-----------\n"); fprintf(stdout,"\n"); fprintf(stdout," -ImgDir <directory>\n"); - fprintf(stdout," Image file Directory path \n"); - fprintf(stdout," -i <compressed file>\n"); - fprintf(stdout," REQUIRED only if an Input image directory not specified\n"); - fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"); - fprintf(stdout," is identified based on its suffix.\n"); - fprintf(stdout," -o <output file>\n"); - fprintf(stdout," OPTIONAL\n"); - fprintf(stdout," Output file where file info will be dump.\n"); - fprintf(stdout," By default it will be in the stdout.\n"); + fprintf(stdout," Image file Directory path \n"); + fprintf(stdout," -i <compressed file>\n"); + fprintf(stdout," REQUIRED only if an Input image directory not specified\n"); + fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"); + fprintf(stdout," is identified based on its suffix.\n"); + fprintf(stdout," -o <output file>\n"); + fprintf(stdout," OPTIONAL\n"); + fprintf(stdout," Output file where file info will be dump.\n"); + fprintf(stdout," By default it will be in the stdout.\n"); fprintf(stdout," -v "); /* FIXME WIP_MSD */ - fprintf(stdout," OPTIONAL\n"); + fprintf(stdout," OPTIONAL\n"); fprintf(stdout," Enable informative messages\n"); fprintf(stdout," By default verbose mode is off.\n"); - fprintf(stdout,"\n"); + fprintf(stdout,"\n"); } /* -------------------------------------------------------------------------- */ -static int get_num_images(char *imgdirpath){ - DIR *dir; - struct dirent* content; - int num_images = 0; - - /*Reading the input images from given input directory*/ - - dir= opendir(imgdirpath); - if(!dir){ - fprintf(stderr,"Could not open Folder %s\n",imgdirpath); - return 0; - } - - while((content=readdir(dir))!=NULL){ - if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) - continue; - num_images++; - } - closedir(dir); - return num_images; +static int get_num_images(char *imgdirpath) +{ + DIR *dir; + struct dirent* content; + int num_images = 0; + + /*Reading the input images from given input directory*/ + + dir= opendir(imgdirpath); + if(!dir) { + fprintf(stderr,"Could not open Folder %s\n",imgdirpath); + return 0; + } + + while((content=readdir(dir))!=NULL) { + if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) + continue; + num_images++; + } + closedir(dir); + return num_images; } /* -------------------------------------------------------------------------- */ -static int load_images(dircnt_t *dirptr, char *imgdirpath){ - DIR *dir; - struct dirent* content; - int i = 0; - - /*Reading the input images from given input directory*/ - - dir= opendir(imgdirpath); - if(!dir){ - fprintf(stderr,"Could not open Folder %s\n",imgdirpath); - return 1; - }else { - fprintf(stderr,"Folder opened successfully\n"); - } - - while((content=readdir(dir))!=NULL){ - if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) - continue; - - strcpy(dirptr->filename[i],content->d_name); - i++; - } - closedir(dir); - return 0; +static int load_images(dircnt_t *dirptr, char *imgdirpath) +{ + DIR *dir; + struct dirent* content; + int i = 0; + + /*Reading the input images from given input directory*/ + + dir= opendir(imgdirpath); + if(!dir) { + fprintf(stderr,"Could not open Folder %s\n",imgdirpath); + return 1; + } else { + fprintf(stderr,"Folder opened successfully\n"); + } + + while((content=readdir(dir))!=NULL) { + if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) + continue; + + strcpy(dirptr->filename[i],content->d_name); + i++; + } + closedir(dir); + return 0; } /* -------------------------------------------------------------------------- */ -static int get_file_format(const char *filename) { - unsigned int i; - static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" }; - static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT }; - const char *ext = strrchr(filename, '.'); - if (ext == NULL) - return -1; - ext++; - if(ext) { - for(i = 0; i < sizeof(format)/sizeof(*format); i++) { - if(_strnicmp(ext, extension[i], 3) == 0) { - return format[i]; - } - } - } - - return -1; +static int get_file_format(const char *filename) +{ + unsigned int i; + static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" }; + static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT }; + const char *ext = strrchr(filename, '.'); + if (ext == NULL) + return -1; + ext++; + if(ext) { + for(i = 0; i < sizeof(format)/sizeof(*format); i++) { + if(_strnicmp(ext, extension[i], 3) == 0) { + return format[i]; + } + } + } + + return -1; } /* -------------------------------------------------------------------------- */ -static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){ - char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; - char *temp_p, temp1[OPJ_PATH_LEN]=""; - - strcpy(image_filename,dirptr->filename[imageno]); - fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename); - parameters->decod_format = get_file_format(image_filename); - if (parameters->decod_format == -1) - return 1; - sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename); - if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) { - return 1; - } - - /*Set output file*/ - strcpy(temp_ofname,strtok(image_filename,".")); - while((temp_p = strtok(NULL,".")) != NULL){ - strcat(temp_ofname,temp1); - sprintf(temp1,".%s",temp_p); - } - if(img_fol->set_out_format==1){ - sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); - if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) { - return 1; - } - } - return 0; +static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters) +{ + char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; + char *temp_p, temp1[OPJ_PATH_LEN]=""; + + strcpy(image_filename,dirptr->filename[imageno]); + fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename); + parameters->decod_format = get_file_format(image_filename); + if (parameters->decod_format == -1) + return 1; + sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename); + if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) { + return 1; + } + + /*Set output file*/ + strcpy(temp_ofname,strtok(image_filename,".")); + while((temp_p = strtok(NULL,".")) != NULL) { + strcat(temp_ofname,temp1); + sprintf(temp1,".%s",temp_p); + } + if(img_fol->set_out_format==1) { + sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); + if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) { + return 1; + } + } + return 0; } /* -------------------------------------------------------------------------- */ @@ -224,169 +229,164 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_d static int infile_format(const char *fname) { - FILE *reader; - const char *s, *magic_s; - int ext_format, magic_format; - unsigned char buf[12]; - size_t l_nb_read; + FILE *reader; + const char *s, *magic_s; + int ext_format, magic_format; + unsigned char buf[12]; + size_t l_nb_read; - reader = fopen(fname, "rb"); + reader = fopen(fname, "rb"); - if (reader == NULL) - return -1; + if (reader == NULL) + return -1; - memset(buf, 0, 12); - l_nb_read = fread(buf, 1, 12, reader); - fclose(reader); - if (l_nb_read != 12) - return -1; + memset(buf, 0, 12); + l_nb_read = fread(buf, 1, 12, reader); + fclose(reader); + if (l_nb_read != 12) + return -1; - ext_format = get_file_format(fname); + ext_format = get_file_format(fname); - if (ext_format == JPT_CFMT) - return JPT_CFMT; + if (ext_format == JPT_CFMT) + return JPT_CFMT; - if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) { - magic_format = JP2_CFMT; - magic_s = ".jp2"; - } - else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) { - magic_format = J2K_CFMT; - magic_s = ".j2k or .jpc or .j2c"; - } - else - return -1; + if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) { + magic_format = JP2_CFMT; + magic_s = ".jp2"; + } else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) { + magic_format = J2K_CFMT; + magic_s = ".j2k or .jpc or .j2c"; + } else + return -1; - if (magic_format == ext_format) - return ext_format; + if (magic_format == ext_format) + return ext_format; - s = fname + strlen(fname) - 4; + s = fname + strlen(fname) - 4; - fputs("\n===========================================\n", stderr); - fprintf(stderr, "The extension of this file is incorrect.\n" - "FOUND %s. SHOULD BE %s\n", s, magic_s); - fputs("===========================================\n", stderr); + fputs("\n===========================================\n", stderr); + fprintf(stderr, "The extension of this file is incorrect.\n" + "FOUND %s. SHOULD BE %s\n", s, magic_s); + fputs("===========================================\n", stderr); - return magic_format; + return magic_format; } /* -------------------------------------------------------------------------- */ /** * Parse the command line */ /* -------------------------------------------------------------------------- */ -static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) { - int totlen, c; - opj_option_t long_option[]={ +static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) +{ + int totlen, c; + opj_option_t long_option[]= { {"ImgDir",REQ_ARG, NULL ,'y'} - }; + }; const char optlist[] = "i:o:f:hv"; - totlen=sizeof(long_option); - img_fol->set_out_format = 0; - do { - c = opj_getopt_long(argc, argv,optlist,long_option,totlen); - if (c == -1) - break; - switch (c) { - case 'i': /* input file */ - { - char *infile = opj_optarg; - parameters->decod_format = infile_format(infile); - switch(parameters->decod_format) { - case J2K_CFMT: - break; - case JP2_CFMT: - break; - case JPT_CFMT: - break; - default: - fprintf(stderr, - "[ERROR] Unknown input file format: %s \n" - " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n", - infile); - return 1; - } - if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) { - fprintf(stderr, "[ERROR] Path is too long\n"); - return 1; - } - } - break; - - /* ------------------------------------------------------ */ - - case 'o': /* output file */ - { - if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), opj_optarg) != 0) { - fprintf(stderr, "[ERROR] Path is too long\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - case 'f': /* flag */ - img_fol->flag = atoi(opj_optarg); + totlen=sizeof(long_option); + img_fol->set_out_format = 0; + do { + c = opj_getopt_long(argc, argv,optlist,long_option,totlen); + if (c == -1) + break; + switch (c) { + case 'i': { /* input file */ + char *infile = opj_optarg; + parameters->decod_format = infile_format(infile); + switch(parameters->decod_format) { + case J2K_CFMT: + break; + case JP2_CFMT: + break; + case JPT_CFMT: + break; + default: + fprintf(stderr, + "[ERROR] Unknown input file format: %s \n" + " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n", + infile); + return 1; + } + if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) { + fprintf(stderr, "[ERROR] Path is too long\n"); + return 1; + } + } + break; + + /* ------------------------------------------------------ */ + + case 'o': { /* output file */ + if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), opj_optarg) != 0) { + fprintf(stderr, "[ERROR] Path is too long\n"); + return 1; + } + } + break; + + /* ----------------------------------------------------- */ + case 'f': /* flag */ + img_fol->flag = atoi(opj_optarg); + break; + /* ----------------------------------------------------- */ + + case 'h': /* display an help description */ + decode_help_display(); + return 1; + + /* ------------------------------------------------------ */ + + case 'y': { /* Image Directory path */ + img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); + strcpy(img_fol->imgdirpath,opj_optarg); + img_fol->set_imgdir=1; + } + break; + + /* ----------------------------------------------------- */ + + case 'v': { /* Verbose mode */ + parameters->m_verbose = 1; + } break; - /* ----------------------------------------------------- */ - - case 'h': /* display an help description */ - decode_help_display(); - return 1; - - /* ------------------------------------------------------ */ - - case 'y': /* Image Directory path */ - { - img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); - strcpy(img_fol->imgdirpath,opj_optarg); - img_fol->set_imgdir=1; - } - break; - - /* ----------------------------------------------------- */ - - case 'v': /* Verbose mode */ - { - parameters->m_verbose = 1; - } - break; - - /* ----------------------------------------------------- */ + + /* ----------------------------------------------------- */ default: fprintf(stderr, "[WARNING] An invalid option has been ignored.\n"); break; } - }while(c != -1); + } while(c != -1); - /* check for possible errors */ - if(img_fol->set_imgdir==1){ - if(!(parameters->infile[0]==0)){ + /* check for possible errors */ + if(img_fol->set_imgdir==1) { + if(!(parameters->infile[0]==0)) { fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n"); - return 1; - } - if(img_fol->set_out_format == 0){ + return 1; + } + if(img_fol->set_out_format == 0) { fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n"); fprintf(stderr, "Only one format allowed.\n" - "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n"); - return 1; - } - if(!(parameters->outfile[0] == 0)){ + "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n"); + return 1; + } + if(!(parameters->outfile[0] == 0)) { fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together\n"); - return 1; - } - }else{ - if(parameters->infile[0] == 0) { + return 1; + } + } else { + if(parameters->infile[0] == 0) { fprintf(stderr, "[ERROR] Required parameter is missing\n"); - fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]); + fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]); fprintf(stderr, " Help: %s -h\n",argv[0]); - return 1; - } - } + return 1; + } + } - return 0; + return 0; } /* -------------------------------------------------------------------------- */ @@ -394,23 +394,26 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param /** sample error debug callback expecting no client object */ -static void error_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[ERROR] %s", msg); +static void error_callback(const char *msg, void *client_data) +{ + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); } /** sample warning debug callback expecting no client object */ -static void warning_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[WARNING] %s", msg); +static void warning_callback(const char *msg, void *client_data) +{ + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); } /** sample debug callback expecting no client object */ -static void info_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[INFO] %s", msg); +static void info_callback(const char *msg, void *client_data) +{ + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); } /* -------------------------------------------------------------------------- */ @@ -420,178 +423,174 @@ static void info_callback(const char *msg, void *client_data) { /* -------------------------------------------------------------------------- */ int main(int argc, char *argv[]) { - FILE *fout = NULL; + FILE *fout = NULL; - opj_dparameters_t parameters; /* Decompression parameters */ - opj_image_t* image = NULL; /* Image structure */ - opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ - opj_stream_t *l_stream = NULL; /* Stream */ - opj_codestream_info_v2_t* cstr_info = NULL; - opj_codestream_index_t* cstr_index = NULL; + opj_dparameters_t parameters; /* Decompression parameters */ + opj_image_t* image = NULL; /* Image structure */ + opj_codec_t* l_codec = NULL; /* Handle to a decompressor */ + opj_stream_t *l_stream = NULL; /* Stream */ + opj_codestream_info_v2_t* cstr_info = NULL; + opj_codestream_index_t* cstr_index = NULL; - OPJ_INT32 num_images, imageno; - img_fol_t img_fol; - dircnt_t *dirptr = NULL; + OPJ_INT32 num_images, imageno; + img_fol_t img_fol; + dircnt_t *dirptr = NULL; #ifdef MSD - OPJ_BOOL l_go_on = OPJ_TRUE; - OPJ_UINT32 l_max_data_size = 1000; - OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000); + OPJ_BOOL l_go_on = OPJ_TRUE; + OPJ_UINT32 l_max_data_size = 1000; + OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000); #endif - /* Set decoding parameters to default values */ - opj_set_default_decoder_parameters(¶meters); - - /* Initialize img_fol */ - memset(&img_fol,0,sizeof(img_fol_t)); - img_fol.flag = OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND; - - /* Parse input and get user encoding parameters */ - if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) { - return EXIT_FAILURE; - } - - /* Initialize reading of directory */ - if(img_fol.set_imgdir==1){ - int it_image; - num_images=get_num_images(img_fol.imgdirpath); - - dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); - if(dirptr){ - dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ - dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); - - if(!dirptr->filename_buf){ - return EXIT_FAILURE; - } - - for(it_image=0;it_image<num_images;it_image++){ - dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; - } - } - if(load_images(dirptr,img_fol.imgdirpath)==1){ - return EXIT_FAILURE; - } - - if (num_images==0){ - fprintf(stdout,"Folder is empty\n"); - return EXIT_FAILURE; - } - }else{ - num_images=1; - } - - /* Try to open for writing the output file if necessary */ - if (parameters.outfile[0] != 0){ - fout = fopen(parameters.outfile,"w"); - if (!fout){ - fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile); - return EXIT_FAILURE; - } - } - else - fout = stdout; - - /* Read the header of each image one by one */ - for(imageno = 0; imageno < num_images ; imageno++){ - - fprintf(stderr,"\n"); - - if(img_fol.set_imgdir==1){ - if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) { - fprintf(stderr,"skipping file...\n"); - continue; - } - } - - /* Read the input file and put it in memory */ - /* ---------------------------------------- */ - - l_stream = opj_stream_create_default_file_stream(parameters.infile,1); - if (!l_stream){ - fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",parameters.infile); - return EXIT_FAILURE; - } - - /* Read the JPEG2000 stream */ - /* ------------------------ */ - - switch(parameters.decod_format) { - case J2K_CFMT: /* JPEG-2000 codestream */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(OPJ_CODEC_J2K); - break; - } - case JP2_CFMT: /* JPEG 2000 compressed image data */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(OPJ_CODEC_JP2); - break; - } - case JPT_CFMT: /* JPEG 2000, JPIP */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(OPJ_CODEC_JPT); - break; - } - default: - fprintf(stderr, "skipping file..\n"); - opj_stream_destroy(l_stream); - continue; - } - - /* catch events using our callbacks and give a local context */ - opj_set_info_handler(l_codec, info_callback,00); - opj_set_warning_handler(l_codec, warning_callback,00); - opj_set_error_handler(l_codec, error_callback,00); - - /* Setup the decoder decoding parameters using user parameters */ - if ( !opj_setup_decoder(l_codec, ¶meters) ){ - fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n"); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - fclose(fout); - return EXIT_FAILURE; - } - - /* Read the main header of the codestream and if necessary the JP2 boxes*/ - if(! opj_read_header(l_stream, l_codec, &image)){ - fprintf(stderr, "ERROR -> opj_dump: failed to read the header\n"); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(image); - fclose(fout); - return EXIT_FAILURE; - } - - opj_dump_codec(l_codec, img_fol.flag, fout ); - - cstr_info = opj_get_cstr_info(l_codec); - - cstr_index = opj_get_cstr_index(l_codec); - - /* close the byte stream */ - opj_stream_destroy(l_stream); - - /* free remaining structures */ - if (l_codec) { - opj_destroy_codec(l_codec); - } - - /* destroy the image header */ - opj_image_destroy(image); - - /* destroy the codestream index */ - opj_destroy_cstr_index(&cstr_index); - - /* destroy the codestream info */ - opj_destroy_cstr_info(&cstr_info); - - } - - /* Close the output file */ - fclose(fout); - - return EXIT_SUCCESS; + /* Set decoding parameters to default values */ + opj_set_default_decoder_parameters(¶meters); + + /* Initialize img_fol */ + memset(&img_fol,0,sizeof(img_fol_t)); + img_fol.flag = OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND; + + /* Parse input and get user encoding parameters */ + if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) { + return EXIT_FAILURE; + } + + /* Initialize reading of directory */ + if(img_fol.set_imgdir==1) { + int it_image; + num_images=get_num_images(img_fol.imgdirpath); + + dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); + if(dirptr) { + dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ + dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); + + if(!dirptr->filename_buf) { + return EXIT_FAILURE; + } + + for(it_image=0; it_image<num_images; it_image++) { + dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; + } + } + if(load_images(dirptr,img_fol.imgdirpath)==1) { + return EXIT_FAILURE; + } + + if (num_images==0) { + fprintf(stdout,"Folder is empty\n"); + return EXIT_FAILURE; + } + } else { + num_images=1; + } + + /* Try to open for writing the output file if necessary */ + if (parameters.outfile[0] != 0) { + fout = fopen(parameters.outfile,"w"); + if (!fout) { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile); + return EXIT_FAILURE; + } + } else + fout = stdout; + + /* Read the header of each image one by one */ + for(imageno = 0; imageno < num_images ; imageno++) { + + fprintf(stderr,"\n"); + + if(img_fol.set_imgdir==1) { + if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) { + fprintf(stderr,"skipping file...\n"); + continue; + } + } + + /* Read the input file and put it in memory */ + /* ---------------------------------------- */ + + l_stream = opj_stream_create_default_file_stream(parameters.infile,1); + if (!l_stream) { + fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",parameters.infile); + return EXIT_FAILURE; + } + + /* Read the JPEG2000 stream */ + /* ------------------------ */ + + switch(parameters.decod_format) { + case J2K_CFMT: { /* JPEG-2000 codestream */ + /* Get a decoder handle */ + l_codec = opj_create_decompress(OPJ_CODEC_J2K); + break; + } + case JP2_CFMT: { /* JPEG 2000 compressed image data */ + /* Get a decoder handle */ + l_codec = opj_create_decompress(OPJ_CODEC_JP2); + break; + } + case JPT_CFMT: { /* JPEG 2000, JPIP */ + /* Get a decoder handle */ + l_codec = opj_create_decompress(OPJ_CODEC_JPT); + break; + } + default: + fprintf(stderr, "skipping file..\n"); + opj_stream_destroy(l_stream); + continue; + } + + /* catch events using our callbacks and give a local context */ + opj_set_info_handler(l_codec, info_callback,00); + opj_set_warning_handler(l_codec, warning_callback,00); + opj_set_error_handler(l_codec, error_callback,00); + + /* Setup the decoder decoding parameters using user parameters */ + if ( !opj_setup_decoder(l_codec, ¶meters) ) { + fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n"); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + fclose(fout); + return EXIT_FAILURE; + } + + /* Read the main header of the codestream and if necessary the JP2 boxes*/ + if(! opj_read_header(l_stream, l_codec, &image)) { + fprintf(stderr, "ERROR -> opj_dump: failed to read the header\n"); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(image); + fclose(fout); + return EXIT_FAILURE; + } + + opj_dump_codec(l_codec, img_fol.flag, fout ); + + cstr_info = opj_get_cstr_info(l_codec); + + cstr_index = opj_get_cstr_index(l_codec); + + /* close the byte stream */ + opj_stream_destroy(l_stream); + + /* free remaining structures */ + if (l_codec) { + opj_destroy_codec(l_codec); + } + + /* destroy the image header */ + opj_image_destroy(image); + + /* destroy the codestream index */ + opj_destroy_cstr_index(&cstr_index); + + /* destroy the codestream info */ + opj_destroy_cstr_info(&cstr_info); + + } + + /* Close the output file */ + fclose(fout); + + return EXIT_SUCCESS; } diff --git a/src/bin/jp2/windirent.h b/src/bin/jp2/windirent.h index bef28194..b6e9de49 100644 --- a/src/bin/jp2/windirent.h +++ b/src/bin/jp2/windirent.h @@ -1,9 +1,9 @@ /* * uce-dirent.h - operating system independent dirent implementation - * + * * Copyright (C) 1998-2002 Toni Ronkko - * + * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * ``Software''), to deal in the Software without restriction, including @@ -11,10 +11,10 @@ * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. @@ -22,8 +22,8 @@ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. - * - * + * + * * May 28 1998, Toni Ronkko <tronkko@messi.uku.fi> * * $Id: uce-dirent.h,v 1.7 2002/05/13 10:48:35 tr Exp $ @@ -59,7 +59,7 @@ * Revision 1.1 1998/07/04 16:27:51 tr * Initial revision * - * + * * MSVC 1.0 scans automatic dependencies incorrectly when your project * contains this very header. The problem is that MSVC cannot handle * include directives inside #if..#endif block those are never entered. @@ -106,14 +106,14 @@ */ #if !defined(HAVE_DIRENT_H) && !defined(HAVE_DIRECT_H) && !defined(HAVE_SYS_DIR_H) && !defined(HAVE_NDIR_H) && !defined(HAVE_SYS_NDIR_H) && !defined(HAVE_DIR_H) # if defined(_MSC_VER) /* Microsoft C/C++ */ - /* no dirent.h */ +/* no dirent.h */ # elif defined(__MINGW32__) /* MinGW */ - /* no dirent.h */ +/* no dirent.h */ # elif defined(__BORLANDC__) /* Borland C/C++ */ # define HAVE_DIRENT_H # define VOID_CLOSEDIR # elif defined(__TURBOC__) /* Borland Turbo C */ - /* no dirent.h */ +/* no dirent.h */ # elif defined(__WATCOMC__) /* Watcom C/C++ */ # define HAVE_DIRECT_H # elif defined(__apollo) /* Apollo */ @@ -172,7 +172,7 @@ #elif defined(MSDOS) || defined(WIN32) - /* figure out type of underlaying directory interface to be used */ +/* figure out type of underlaying directory interface to be used */ # if defined(WIN32) # define DIRENT_WIN32_INTERFACE # elif defined(MSDOS) @@ -181,7 +181,7 @@ # error "missing native dirent interface" # endif - /*** WIN32 specifics ***/ +/*** WIN32 specifics ***/ # if defined(DIRENT_WIN32_INTERFACE) # include <windows.h> # if !defined(DIRENT_MAXNAMLEN) @@ -189,11 +189,11 @@ # endif - /*** MS-DOS specifics ***/ +/*** MS-DOS specifics ***/ # elif defined(DIRENT_MSDOS_INTERFACE) # include <dos.h> - /* Borland defines file length macros in dir.h */ +/* Borland defines file length macros in dir.h */ # if defined(__BORLANDC__) # include <dir.h> # if !defined(DIRENT_MAXNAMLEN) @@ -203,7 +203,7 @@ # define _find_t find_t # endif - /* Turbo C defines ffblk structure in dir.h */ +/* Turbo C defines ffblk structure in dir.h */ # elif defined(__TURBOC__) # include <dir.h> # if !defined(DIRENT_MAXNAMLEN) @@ -211,13 +211,13 @@ # endif # define DIRENT_USE_FFBLK - /* MSVC */ +/* MSVC */ # elif defined(_MSC_VER) # if !defined(DIRENT_MAXNAMLEN) # define DIRENT_MAXNAMLEN (12) # endif - /* Watcom */ +/* Watcom */ # elif defined(__WATCOMC__) # if !defined(DIRENT_MAXNAMLEN) # if defined(__OS2__) || defined(__NT__) @@ -230,7 +230,7 @@ # endif # endif - /*** generic MS-DOS and MS-Windows stuff ***/ +/*** generic MS-DOS and MS-Windows stuff ***/ # if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN) # define NAME_MAX DIRENT_MAXNAMLEN # endif @@ -239,16 +239,16 @@ # endif - /* - * Substitute for real dirent structure. Note that `d_name' field is a - * true character array although we have it copied in the implementation - * dependent data. We could save some memory if we had declared `d_name' - * as a pointer referring the name within implementation dependent data. - * We have not done that since some code may rely on sizeof(d_name) to be - * something other than four. Besides, directory entries are typically so - * small that it takes virtually no time to copy them from place to place. - */ - typedef struct dirent { +/* + * Substitute for real dirent structure. Note that `d_name' field is a + * true character array although we have it copied in the implementation + * dependent data. We could save some memory if we had declared `d_name' + * as a pointer referring the name within implementation dependent data. + * We have not done that since some code may rely on sizeof(d_name) to be + * something other than four. Besides, directory entries are typically so + * small that it takes virtually no time to copy them from place to place. + */ +typedef struct dirent { char d_name[NAME_MAX + 1]; /*** Operating system specific part ***/ @@ -261,21 +261,21 @@ struct _find_t data; # endif # endif - } dirent; +} dirent; - /* DIR substitute structure containing directory name. The name is - * essential for the operation of ``rewinndir'' function. */ - typedef struct DIR { +/* DIR substitute structure containing directory name. The name is + * essential for the operation of ``rewinndir'' function. */ +typedef struct DIR { char *dirname; /* directory being scanned */ dirent current; /* current entry */ int dirent_filled; /* is current un-processed? */ - /*** Operating system specific part ***/ + /*** Operating system specific part ***/ # if defined(DIRENT_WIN32_INTERFACE) HANDLE search_handle; # elif defined(DIRENT_MSDOS_INTERFACE) # endif - } DIR; +} DIR; # ifdef __cplusplus extern "C" { @@ -325,7 +325,7 @@ static void _setdirname (struct DIR *dirp); * internal working area that is used for retrieving individual directory * entries. The internal working area has no fields of your interest. * - * <ret>Returns a pointer to the internal working area or NULL in case the + * <ret>Returns a pointer to the internal working area or NULL in case the * directory stream could not be opened. Global `errno' variable will set * in case of error as follows: * @@ -344,46 +344,45 @@ static void _setdirname (struct DIR *dirp); */ static DIR *opendir(const char *dirname) { - DIR *dirp; - assert (dirname != NULL); - - dirp = (DIR*)malloc (sizeof (struct DIR)); - if (dirp != NULL) { - char *p; - - /* allocate room for directory name */ - dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*")); - if (dirp->dirname == NULL) { - /* failed to duplicate directory name. errno set by malloc() */ - free (dirp); - return NULL; - } - /* Copy directory name while appending directory separator and "*.*". - * Directory separator is not appended if the name already ends with - * drive or directory separator. Directory separator is assumed to be - * '/' or '\' and drive separator is assumed to be ':'. */ - strcpy (dirp->dirname, dirname); - p = strchr (dirp->dirname, '\0'); - if (dirp->dirname < p && - *(p - 1) != '\\' && *(p - 1) != '/' && *(p - 1) != ':') - { - strcpy (p++, "\\"); - } + DIR *dirp; + assert (dirname != NULL); + + dirp = (DIR*)malloc (sizeof (struct DIR)); + if (dirp != NULL) { + char *p; + + /* allocate room for directory name */ + dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*")); + if (dirp->dirname == NULL) { + /* failed to duplicate directory name. errno set by malloc() */ + free (dirp); + return NULL; + } + /* Copy directory name while appending directory separator and "*.*". + * Directory separator is not appended if the name already ends with + * drive or directory separator. Directory separator is assumed to be + * '/' or '\' and drive separator is assumed to be ':'. */ + strcpy (dirp->dirname, dirname); + p = strchr (dirp->dirname, '\0'); + if (dirp->dirname < p && + *(p - 1) != '\\' && *(p - 1) != '/' && *(p - 1) != ':') { + strcpy (p++, "\\"); + } # ifdef DIRENT_WIN32_INTERFACE - strcpy (p, "*"); /*scan files with and without extension in win32*/ + strcpy (p, "*"); /*scan files with and without extension in win32*/ # else - strcpy (p, "*.*"); /*scan files with and without extension in DOS*/ + strcpy (p, "*.*"); /*scan files with and without extension in DOS*/ # endif - /* open stream */ - if (_initdir (dirp) == 0) { - /* initialization failed */ - free (dirp->dirname); - free (dirp); - return NULL; + /* open stream */ + if (_initdir (dirp) == 0) { + /* initialization failed */ + free (dirp->dirname); + free (dirp); + return NULL; + } } - } - return dirp; + return dirp; } @@ -436,55 +435,55 @@ static DIR *opendir(const char *dirname) static struct dirent * readdir (DIR *dirp) { - assert(dirp != NULL); - if (dirp == NULL) { - errno = EBADF; - return NULL; - } + assert(dirp != NULL); + if (dirp == NULL) { + errno = EBADF; + return NULL; + } #if defined(DIRENT_WIN32_INTERFACE) - if (dirp->search_handle == INVALID_HANDLE_VALUE) { - /* directory stream was opened/rewound incorrectly or it ended normally */ - errno = EBADF; - return NULL; - } + if (dirp->search_handle == INVALID_HANDLE_VALUE) { + /* directory stream was opened/rewound incorrectly or it ended normally */ + errno = EBADF; + return NULL; + } #endif - if (dirp->dirent_filled != 0) { - /* - * Directory entry has already been retrieved and there is no need to - * retrieve a new one. Directory entry will be retrieved in advance - * when the user calls readdir function for the first time. This is so - * because real dirent has separate functions for opening and reading - * the stream whereas Win32 and DOS dirents open the stream - * automatically when we retrieve the first file. Therefore, we have to - * save the first file when opening the stream and later we have to - * return the saved entry when the user tries to read the first entry. - */ - dirp->dirent_filled = 0; - } else { - /* fill in entry and return that */ + if (dirp->dirent_filled != 0) { + /* + * Directory entry has already been retrieved and there is no need to + * retrieve a new one. Directory entry will be retrieved in advance + * when the user calls readdir function for the first time. This is so + * because real dirent has separate functions for opening and reading + * the stream whereas Win32 and DOS dirents open the stream + * automatically when we retrieve the first file. Therefore, we have to + * save the first file when opening the stream and later we have to + * return the saved entry when the user tries to read the first entry. + */ + dirp->dirent_filled = 0; + } else { + /* fill in entry and return that */ #if defined(DIRENT_WIN32_INTERFACE) - if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) { - /* Last file has been processed or an error occurred */ - FindClose (dirp->search_handle); - dirp->search_handle = INVALID_HANDLE_VALUE; - errno = ENOENT; - return NULL; - } + if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) { + /* Last file has been processed or an error occurred */ + FindClose (dirp->search_handle); + dirp->search_handle = INVALID_HANDLE_VALUE; + errno = ENOENT; + return NULL; + } # elif defined(DIRENT_MSDOS_INTERFACE) - if (_dos_findnext (&dirp->current.data) != 0) { - /* _dos_findnext and findnext will set errno to ENOENT when no - * more entries could be retrieved. */ - return NULL; - } + if (_dos_findnext (&dirp->current.data) != 0) { + /* _dos_findnext and findnext will set errno to ENOENT when no + * more entries could be retrieved. */ + return NULL; + } # endif - _setdirname (dirp); - assert (dirp->dirent_filled == 0); - } - return &dirp->current; + _setdirname (dirp); + assert (dirp->dirent_filled == 0); + } + return &dirp->current; } @@ -509,37 +508,37 @@ readdir (DIR *dirp) */ static int closedir (DIR *dirp) -{ - int retcode = 0; - - /* make sure that dirp points to legal structure */ - assert (dirp != NULL); - if (dirp == NULL) { - errno = EBADF; - return -1; - } - - /* free directory name and search handles */ - if (dirp->dirname != NULL) free (dirp->dirname); +{ + int retcode = 0; + + /* make sure that dirp points to legal structure */ + assert (dirp != NULL); + if (dirp == NULL) { + errno = EBADF; + return -1; + } + + /* free directory name and search handles */ + if (dirp->dirname != NULL) free (dirp->dirname); #if defined(DIRENT_WIN32_INTERFACE) - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - if (FindClose (dirp->search_handle) == FALSE) { - /* Unknown error */ - retcode = -1; - errno = EBADF; + if (dirp->search_handle != INVALID_HANDLE_VALUE) { + if (FindClose (dirp->search_handle) == FALSE) { + /* Unknown error */ + retcode = -1; + errno = EBADF; + } } - } -#endif +#endif - /* clear dirp structure to make sure that it cannot be used anymore*/ - memset (dirp, 0, sizeof (*dirp)); + /* clear dirp structure to make sure that it cannot be used anymore*/ + memset (dirp, 0, sizeof (*dirp)); # if defined(DIRENT_WIN32_INTERFACE) - dirp->search_handle = INVALID_HANDLE_VALUE; + dirp->search_handle = INVALID_HANDLE_VALUE; # endif - free (dirp); - return retcode; + free (dirp); + return retcode; } @@ -566,31 +565,31 @@ closedir (DIR *dirp) */ static void rewinddir (DIR *dirp) -{ - /* make sure that dirp is legal */ - assert (dirp != NULL); - if (dirp == NULL) { - errno = EBADF; - return; - } - assert (dirp->dirname != NULL); - - /* close previous stream */ +{ + /* make sure that dirp is legal */ + assert (dirp != NULL); + if (dirp == NULL) { + errno = EBADF; + return; + } + assert (dirp->dirname != NULL); + + /* close previous stream */ #if defined(DIRENT_WIN32_INTERFACE) - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - if (FindClose (dirp->search_handle) == FALSE) { - /* Unknown error */ - errno = EBADF; + if (dirp->search_handle != INVALID_HANDLE_VALUE) { + if (FindClose (dirp->search_handle) == FALSE) { + /* Unknown error */ + errno = EBADF; + } } - } #endif - /* re-open previous stream */ - if (_initdir (dirp) == 0) { - /* initialization failed but we cannot deal with error. User will notice - * error later when she tries to retrieve first directory enty. */ - /*EMPTY*/; - } + /* re-open previous stream */ + if (_initdir (dirp) == 0) { + /* initialization failed but we cannot deal with error. User will notice + * error later when she tries to retrieve first directory enty. */ + /*EMPTY*/; + } } @@ -600,37 +599,36 @@ rewinddir (DIR *dirp) */ static int _initdir (DIR *dirp) -{ - assert (dirp != NULL); - assert (dirp->dirname != NULL); - dirp->dirent_filled = 0; +{ + assert (dirp != NULL); + assert (dirp->dirname != NULL); + dirp->dirent_filled = 0; # if defined(DIRENT_WIN32_INTERFACE) - /* Open stream and retrieve first file */ - dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data); - if (dirp->search_handle == INVALID_HANDLE_VALUE) { - /* something went wrong but we don't know what. GetLastError() could - * give us more information about the error, but then we should map - * the error code into errno. */ - errno = ENOENT; - return 0; - } + /* Open stream and retrieve first file */ + dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data); + if (dirp->search_handle == INVALID_HANDLE_VALUE) { + /* something went wrong but we don't know what. GetLastError() could + * give us more information about the error, but then we should map + * the error code into errno. */ + errno = ENOENT; + return 0; + } # elif defined(DIRENT_MSDOS_INTERFACE) - if (_dos_findfirst (dirp->dirname, - _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN, - &dirp->current.data) != 0) - { - /* _dos_findfirst and findfirst will set errno to ENOENT when no - * more entries could be retrieved. */ - return 0; - } + if (_dos_findfirst (dirp->dirname, + _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN, + &dirp->current.data) != 0) { + /* _dos_findfirst and findfirst will set errno to ENOENT when no + * more entries could be retrieved. */ + return 0; + } # endif - /* initialize DIR and it's first entry */ - _setdirname (dirp); - dirp->dirent_filled = 1; - return 1; + /* initialize DIR and it's first entry */ + _setdirname (dirp); + dirp->dirent_filled = 1; + return 1; } @@ -641,14 +639,14 @@ static const char * _getdirname (const struct dirent *dp) { #if defined(DIRENT_WIN32_INTERFACE) - return dp->data.cFileName; - + return dp->data.cFileName; + #elif defined(DIRENT_USE_FFBLK) - return dp->data.ff_name; - + return dp->data.ff_name; + #else - return dp->data.name; -#endif + return dp->data.name; +#endif } @@ -656,16 +654,17 @@ _getdirname (const struct dirent *dp) * Copy name of implementation dependent directory entry to the d_name field. */ static void -_setdirname (struct DIR *dirp) { - /* make sure that d_name is long enough */ - assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX); - - strncpy (dirp->current.d_name, - _getdirname (&dirp->current), - NAME_MAX); - dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/ +_setdirname (struct DIR *dirp) +{ + /* make sure that d_name is long enough */ + assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX); + + strncpy (dirp->current.d_name, + _getdirname (&dirp->current), + NAME_MAX); + dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/ } - + # ifdef __cplusplus } # endif |
