From d02826005371bfdcbbd39aaebba228749a3450ca Mon Sep 17 00:00:00 2001 From: caesar Date: Sun, 19 Apr 2020 14:34:00 +0800 Subject: [PATCH 1/4] add c_vector can read and write Memory stream --- src/bin/jp2/CMakeLists.txt | 2 +- src/bin/jp2/convert.c | 1047 ++++++++------- src/bin/jp2/convert.h | 3 + src/bin/jp2/convertbmp.c | 931 ++++++++----- src/bin/jp2/opj_decompress_c_vector.c | 1721 +++++++++++++++++++++++++ src/lib/openjp2/CMakeLists.txt | 2 + src/lib/openjp2/c_vector.c | 228 ++++ src/lib/openjp2/c_vector.h | 110 ++ src/lib/openjp2/openjpeg.c | 958 +++++++------- src/lib/openjp2/openjpeg.h | 19 + 10 files changed, 3748 insertions(+), 1273 deletions(-) create mode 100644 src/bin/jp2/opj_decompress_c_vector.c create mode 100644 src/lib/openjp2/c_vector.c create mode 100644 src/lib/openjp2/c_vector.h diff --git a/src/bin/jp2/CMakeLists.txt b/src/bin/jp2/CMakeLists.txt index 4d4bd952f..def7528b2 100644 --- a/src/bin/jp2/CMakeLists.txt +++ b/src/bin/jp2/CMakeLists.txt @@ -42,7 +42,7 @@ if(WIN32) endif() # Loop over all executables: -foreach(exe opj_decompress opj_compress opj_dump) +foreach(exe opj_decompress opj_decompress_c_vector opj_compress opj_dump) add_executable(${exe} ${exe}.c ${common_SRCS}) if(NOT ${CMAKE_VERSION} VERSION_LESS "2.8.12") target_compile_options(${exe} PRIVATE ${OPENJP2_COMPILE_OPTIONS}) diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index 23f820c0d..cb0d6d83c 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -51,8 +51,7 @@ * * 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,20 +60,19 @@ static int int_floorlog2(int a) } /* Component precision scaling */ -void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision) -{ +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); + OPJ_UINT32 umax = (OPJ_UINT32) ((OPJ_INT32) -1); - len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h; + 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 *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) { @@ -84,7 +82,7 @@ void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision) } } } else { - OPJ_UINT32* l_data = (OPJ_UINT32*)component->data; + OPJ_UINT32 *l_data = (OPJ_UINT32 *) component->data; for (i = 0; i < len; ++i) { if (l_data[i] > umax) { l_data[i] = umax; @@ -95,32 +93,31 @@ void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision) } /* Component precision scaling */ -static void scale_component_up(opj_image_comp_t* component, - OPJ_UINT32 precision) -{ +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; + 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; + 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); + 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; + 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); + 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) -{ + +void scale_component(opj_image_comp_t *component, OPJ_UINT32 precision) { int shift; OPJ_SIZE_T i, len; @@ -131,15 +128,15 @@ void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision) scale_component_up(component, precision); return; } - shift = (int)(component->prec - precision); - len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h; + 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; + 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; + OPJ_UINT32 *l_data = (OPJ_UINT32 *) component->data; for (i = 0; i < len; ++i) { l_data[i] >>= shift; } @@ -151,30 +148,29 @@ void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision) /* planar / interleaved conversions */ /* used by PNG/TIFF */ -static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, - OPJ_SIZE_T length) -{ +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)); } -static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, - OPJ_SIZE_T length) -{ + +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]; + 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) -{ + +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]; + 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]; @@ -182,14 +178,14 @@ static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, pDst2[i] = pSrc[3 * i + 2]; } } -static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, - OPJ_SIZE_T length) -{ + +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]; + 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]; @@ -198,43 +194,43 @@ static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, 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) -{ +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]; + 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) -{ + +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]; + 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) -{ + +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]; + 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; @@ -242,14 +238,14 @@ static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, 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) -{ + +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]; + 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; @@ -258,48 +254,48 @@ static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, 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) -{ +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) { + 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); + 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); + pDst[i + 0] = (OPJ_INT32) (val >> 7); if (length > 1U) { - pDst[i + 1] = (OPJ_INT32)((val >> 6) & 0x1U); + pDst[i + 1] = (OPJ_INT32) ((val >> 6) & 0x1U); if (length > 2U) { - pDst[i + 2] = (OPJ_INT32)((val >> 5) & 0x1U); + pDst[i + 2] = (OPJ_INT32) ((val >> 5) & 0x1U); if (length > 3U) { - pDst[i + 3] = (OPJ_INT32)((val >> 4) & 0x1U); + pDst[i + 3] = (OPJ_INT32) ((val >> 4) & 0x1U); if (length > 4U) { - pDst[i + 4] = (OPJ_INT32)((val >> 3) & 0x1U); + pDst[i + 4] = (OPJ_INT32) ((val >> 3) & 0x1U); if (length > 5U) { - pDst[i + 5] = (OPJ_INT32)((val >> 2) & 0x1U); + pDst[i + 5] = (OPJ_INT32) ((val >> 2) & 0x1U); if (length > 6U) { - pDst[i + 6] = (OPJ_INT32)((val >> 1) & 0x1U); + pDst[i + 6] = (OPJ_INT32) ((val >> 1) & 0x1U); } } } @@ -308,115 +304,115 @@ static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, } } } -static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, - OPJ_SIZE_T length) -{ + +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) { + 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); + 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); + pDst[i + 0] = (OPJ_INT32) (val >> 6); if (length > 1U) { - pDst[i + 1] = (OPJ_INT32)((val >> 4) & 0x3U); + pDst[i + 1] = (OPJ_INT32) ((val >> 4) & 0x3U); if (length > 2U) { - pDst[i + 2] = (OPJ_INT32)((val >> 2) & 0x3U); + pDst[i + 2] = (OPJ_INT32) ((val >> 2) & 0x3U); } } } } -static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, - OPJ_SIZE_T length) -{ + +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) { + 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); + 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); + pDst[i + 0] = (OPJ_INT32) (val >> 4); } } -static void convert_6u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, - OPJ_SIZE_T length) -{ + +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) { + 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); + 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); + pDst[i + 0] = (OPJ_INT32) (val0 >> 2); if (length > 1U) { OPJ_UINT32 val1 = *pSrc++; - pDst[i + 1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4)); + 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)); + 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) -{ + +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]; } } + 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) -{ +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]; + 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); + *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 src0 = (OPJ_UINT32) pSrc[i + 0]; OPJ_UINT32 src1 = 0U; OPJ_UINT32 src2 = 0U; OPJ_UINT32 src3 = 0U; @@ -426,128 +422,126 @@ static void convert_32s1u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, length = length & 7U; if (length > 1U) { - src1 = (OPJ_UINT32)pSrc[i + 1]; + src1 = (OPJ_UINT32) pSrc[i + 1]; if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i + 2]; + src2 = (OPJ_UINT32) pSrc[i + 2]; if (length > 3U) { - src3 = (OPJ_UINT32)pSrc[i + 3]; + src3 = (OPJ_UINT32) pSrc[i + 3]; if (length > 4U) { - src4 = (OPJ_UINT32)pSrc[i + 4]; + src4 = (OPJ_UINT32) pSrc[i + 4]; if (length > 5U) { - src5 = (OPJ_UINT32)pSrc[i + 5]; + src5 = (OPJ_UINT32) pSrc[i + 5]; if (length > 6U) { - src6 = (OPJ_UINT32)pSrc[i + 6]; + src6 = (OPJ_UINT32) pSrc[i + 6]; } } } } } } - *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | - (src4 << 3) | (src5 << 2) | (src6 << 1)); + *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) -{ +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]; + 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); + *pDst++ = (OPJ_BYTE) ((src0 << 6) | (src1 << 4) | (src2 << 2) | src3); } if (length & 3U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0]; + 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]; + src1 = (OPJ_UINT32) pSrc[i + 1]; if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i + 2]; + src2 = (OPJ_UINT32) pSrc[i + 2]; } } - *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 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) -{ +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]; + 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); + *pDst++ = (OPJ_BYTE) ((src0 << 4) | src1); } if (length & 1U) { - OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0]; - *pDst++ = (OPJ_BYTE)((src0 << 4)); + 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) -{ +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]; + 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); + *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 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]; + src1 = (OPJ_UINT32) pSrc[i + 1]; if (length > 2U) { - src2 = (OPJ_UINT32)pSrc[i + 2]; + src2 = (OPJ_UINT32) pSrc[i + 2]; } } - *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4)); + *pDst++ = (OPJ_BYTE) ((src0 << 2) | (src1 >> 4)); if (length > 1U) { - *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2)); + *pDst++ = (OPJ_BYTE) (((src1 & 0xFU) << 4) | (src2 >> 2)); if (length > 2U) { - *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6)); + *pDst++ = (OPJ_BYTE) (((src2 & 0x3U) << 6)); } } } } -static void convert_32s8u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, - OPJ_SIZE_T length) -{ + +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]; + 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 }; /* -->> -->> -->> -->> @@ -581,16 +575,14 @@ struct tga_header { #endif /* INFORMATION_ONLY */ /* Returns a ushort from a little-endian serialized value */ -static unsigned short get_tga_ushort(const unsigned char *data) -{ - return (unsigned short)(data[0] | (data[1] << 8)); +static unsigned short get_tga_ushort(const unsigned char *data) { + return (unsigned short) (data[0] | (data[1] << 8)); } #define TGA_HEADER_SIZE 18 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, - unsigned int *width, unsigned int *height, int *flip_image) -{ + unsigned int *width, unsigned int *height, int *flip_image) { int palette_size; unsigned char tga[TGA_HEADER_SIZE]; unsigned char id_len, /*cmap_type,*/ image_type; @@ -605,7 +597,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, 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 ; + return 0; } id_len = tga[0]; /*cmap_type = tga[1];*/ @@ -622,11 +614,11 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, image_w = get_tga_ushort(&tga[12]); image_h = get_tga_ushort(&tga[14]); pixel_depth = tga[16]; - image_desc = tga[17]; + image_desc = tga[17]; - *bits_per_pixel = (unsigned int)pixel_depth; - *width = (unsigned int)image_w; - *height = (unsigned int)image_h; + *bits_per_pixel = (unsigned int) pixel_depth; + *width = (unsigned int) image_w; + *height = (unsigned int) image_h; /* Ignore tga identifier, if present ... */ if (id_len) { @@ -639,7 +631,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); free(id); - return 0 ; + return 0; } free(id); } @@ -649,7 +641,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, // 10 - RLE encoded RGB. */ if (image_type > 8) { fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n"); - return 0 ; + return 0; } *flip_image = !(image_desc & 32); @@ -674,8 +666,7 @@ static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x) #endif static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, - OPJ_BOOL flip_image) -{ + OPJ_BOOL flip_image) { OPJ_UINT16 image_w, image_h, us0; unsigned char uc0, image_type; unsigned char pixel_depth, image_desc; @@ -687,7 +678,7 @@ static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, pixel_depth = 0; if (bits_per_pixel < 256) { - pixel_depth = (unsigned char)bits_per_pixel; + pixel_depth = (unsigned char) bits_per_pixel; } else { fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header"); return 0; @@ -724,7 +715,7 @@ static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, goto fails; /* y_origin */ } - image_w = (unsigned short)width; + image_w = (unsigned short) width; image_h = (unsigned short) height; #ifndef OPJ_BIG_ENDIAN @@ -760,13 +751,12 @@ static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, return 1; -fails: + fails: fputs("\nwrite_tgaheader: write ERROR\n", stderr); 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; @@ -775,7 +765,7 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */ int numcomps; OPJ_COLOR_SPACE color_space; - OPJ_BOOL mono ; + OPJ_BOOL mono; OPJ_BOOL save_alpha; int subsampling_dx, subsampling_dy; int i; @@ -817,15 +807,15 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) /* If the declared file size is > 10 MB, check that the file is big */ /* enough to avoid excessive memory allocations */ if (image_height != 0 && - image_width > 10000000U / image_height / (OPJ_UINT32)numcomps) { + image_width > 10000000U / image_height / (OPJ_UINT32) numcomps) { char ch; OPJ_UINT64 expected_file_size = - (OPJ_UINT64)image_width * image_height * (OPJ_UINT32)numcomps; + (OPJ_UINT64) image_width * image_height * (OPJ_UINT32) numcomps; long curpos = ftell(f); - if (expected_file_size > (OPJ_UINT64)INT_MAX) { - expected_file_size = (OPJ_UINT64)INT_MAX; + if (expected_file_size > (OPJ_UINT64) INT_MAX) { + expected_file_size = (OPJ_UINT64) INT_MAX; } - fseek(f, (long)expected_file_size - 1, SEEK_SET); + fseek(f, (long) expected_file_size - 1, SEEK_SET); if (fread(&ch, 1, 1, f) != 1) { fclose(f); return NULL; @@ -840,14 +830,14 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) cmptparm[i].prec = 8; cmptparm[i].bpp = 8; cmptparm[i].sgnd = 0; - cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; - cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[i].dx = (OPJ_UINT32) subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32) subsampling_dy; cmptparm[i].w = image_width; cmptparm[i].h = image_height; } /* create the image */ - image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + image = opj_image_create((OPJ_UINT32) numcomps, &cmptparm[0], color_space); if (!image) { fclose(f); @@ -856,23 +846,23 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) /* 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)(image_width - 1) * - (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1) * - (OPJ_UINT32)subsampling_dx + 1; - 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; + image->x0 = (OPJ_UINT32) parameters->image_offset_x0; + image->y0 = (OPJ_UINT32) parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32) (image_width - 1) * + (OPJ_UINT32) subsampling_dx + 1 : image->x0 + (OPJ_UINT32) (image_width - 1) * + (OPJ_UINT32) subsampling_dx + 1; + 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++) { int index; if (flip_image) { - index = (int)((image_height - y - 1) * image_width); + index = (int) ((image_height - y - 1) * image_width); } else { - index = (int)(y * image_width); + index = (int) (y * image_width); } if (numcomps == 3) { @@ -952,8 +942,7 @@ 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; @@ -974,9 +963,9 @@ int imagetotga(opj_image_t * image, const char *outfile) for (i = 0; i < image->numcomps - 1; i++) { if ((image->comps[0].dx != image->comps[i + 1].dx) - || (image->comps[0].dy != image->comps[i + 1].dy) - || (image->comps[0].prec != image->comps[i + 1].prec) - || (image->comps[0].sgnd != image->comps[i + 1].sgnd)) { + || (image->comps[0].dy != image->comps[i + 1].dy) + || (image->comps[0].prec != image->comps[i + 1].prec) + || (image->comps[0].sgnd != image->comps[i + 1].sgnd)) { fclose(fdest); fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.\n"); @@ -984,8 +973,8 @@ int imagetotga(opj_image_t * image, const char *outfile) } } - width = (int)image->comps[0].w; - height = (int)image->comps[0].h; + width = (int) image->comps[0].w; + height = (int) image->comps[0].h; /* Mono with alpha, or RGB with alpha. */ write_alpha = (image->numcomps == 2) || (image->numcomps == 4); @@ -999,7 +988,7 @@ int imagetotga(opj_image_t * image, const char *outfile) alpha_channel = image->numcomps - 1; - scale = 255.0f / (float)((1 << image->comps[0].prec) - 1); + scale = 255.0f / (float) ((1 << image->comps[0].prec) - 1); adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); if (image->numcomps >= 3) { @@ -1008,14 +997,14 @@ int imagetotga(opj_image_t * image, const char *outfile) } for (y = 0; y < height; y++) { - unsigned int index = (unsigned int)(y * width); + unsigned int index = (unsigned int) (y * width); for (x = 0; x < width; x++, index++) { - r = (float)(image->comps[0].data[index] + adjustR); + 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); + g = (float) (image->comps[1].data[index] + adjustG); + b = (float) (image->comps[2].data[index] + adjustB); } else { /* Greyscale ... */ g = r; @@ -1028,7 +1017,7 @@ int imagetotga(opj_image_t * image, const char *outfile) } else if (b < 0.) { b = 0.; } - value = (unsigned char)(b * scale); + value = (unsigned char) (b * scale); res = fwrite(&value, 1, 1, fdest); if (res < 1) { @@ -1040,7 +1029,7 @@ int imagetotga(opj_image_t * image, const char *outfile) } else if (g < 0.) { g = 0.; } - value = (unsigned char)(g * scale); + value = (unsigned char) (g * scale); res = fwrite(&value, 1, 1, fdest); if (res < 1) { @@ -1052,7 +1041,7 @@ int imagetotga(opj_image_t * image, const char *outfile) } else if (r < 0.) { r = 0.; } - value = (unsigned char)(r * scale); + value = (unsigned char) (r * scale); res = fwrite(&value, 1, 1, fdest); if (res < 1) { @@ -1061,13 +1050,13 @@ int imagetotga(opj_image_t * image, const char *outfile) } if (write_alpha) { - a = (float)(image->comps[alpha_channel].data[index]); + a = (float) (image->comps[alpha_channel].data[index]); if (a > 255.) { a = 255.; } else if (a < 0.) { a = 0.; } - value = (unsigned char)(a * scale); + value = (unsigned char) (a * scale); res = fwrite(&value, 1, 1, fdest); if (res < 1) { @@ -1078,7 +1067,7 @@ int imagetotga(opj_image_t * image, const char *outfile) } } fails = 0; -fin: + fin: fclose(fdest); return fails; @@ -1091,8 +1080,7 @@ PGX IMAGE FORMAT <<-- <<-- <<-- <<-- */ -static unsigned char readuchar(FILE * f) -{ +static unsigned char readuchar(FILE *f) { unsigned char c1; if (!fread(&c1, 1, 1, f)) { fprintf(stderr, @@ -1102,8 +1090,7 @@ static unsigned char readuchar(FILE * f) return c1; } -static unsigned short readushort(FILE * f, int bigendian) -{ +static unsigned short readushort(FILE *f, int bigendian) { unsigned char c1, c2; if (!fread(&c1, 1, 1, f)) { fprintf(stderr, @@ -1116,14 +1103,13 @@ static unsigned short readushort(FILE * f, int bigendian) return 0; } if (bigendian) { - return (unsigned short)((c1 << 8) + c2); + return (unsigned short) ((c1 << 8) + c2); } else { - return (unsigned short)((c2 << 8) + c1); + return (unsigned short) ((c2 << 8) + c1); } } -static unsigned int readuint(FILE * f, int bigendian) -{ +static unsigned int readuint(FILE *f, int bigendian) { unsigned char c1, c2, c3, c4; if (!fread(&c1, 1, 1, f)) { fprintf(stderr, @@ -1146,22 +1132,21 @@ static unsigned int readuint(FILE * f, int bigendian) return 0; } if (bigendian) { - return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)( - c3 << 8) + c4; + return (unsigned int) (c1 << 24) + (unsigned int) (c2 << 16) + (unsigned int) ( + c3 << 8) + c4; } else { - return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)( - c2 << 8) + c1; + 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; OPJ_COLOR_SPACE color_space; opj_image_cmptparm_t cmptparm; /* maximum of 1 component */ - opj_image_t * image = NULL; + opj_image_t *image = NULL; int adjustS, ushift, dshift, force8; OPJ_UINT64 expected_file_size; @@ -1221,14 +1206,14 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) } expected_file_size = - (OPJ_UINT64)w * (OPJ_UINT64)h * (prec > 16 ? 4 : prec > 8 ? 2 : 1); + (OPJ_UINT64) w * (OPJ_UINT64) h * (prec > 16 ? 4 : prec > 8 ? 2 : 1); if (expected_file_size > 10000000U) { char ch; long curpos = ftell(f); - if (expected_file_size > (OPJ_UINT64)INT_MAX) { - expected_file_size = (OPJ_UINT64)INT_MAX; + if (expected_file_size > (OPJ_UINT64) INT_MAX) { + expected_file_size = (OPJ_UINT64) INT_MAX; } - fseek(f, (long)expected_file_size - 1, SEEK_SET); + fseek(f, (long) expected_file_size - 1, SEEK_SET); if (fread(&ch, 1, 1, f) != 1) { fprintf(stderr, "File too short\n"); fclose(f); @@ -1239,14 +1224,16 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) /* initialize image component */ - cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0; - cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0; - cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx + - 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx - + 1; - cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy + - 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy - + 1; + cmptparm.x0 = (OPJ_UINT32) parameters->image_offset_x0; + cmptparm.y0 = (OPJ_UINT32) parameters->image_offset_y0; + cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32) ((w - 1) * parameters->subsampling_dx + + 1) : cmptparm.x0 + + (OPJ_UINT32) (w - 1) * (OPJ_UINT32) parameters->subsampling_dx + + 1; + cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32) ((h - 1) * parameters->subsampling_dy + + 1) : cmptparm.y0 + + (OPJ_UINT32) (h - 1) * (OPJ_UINT32) parameters->subsampling_dy + + 1; if (sign == '-') { cmptparm.sgnd = 1; @@ -1268,13 +1255,13 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) ushift = dshift = force8 = adjustS = 0; } - cmptparm.prec = (OPJ_UINT32)prec; - cmptparm.bpp = (OPJ_UINT32)prec; - cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx; - cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy; + cmptparm.prec = (OPJ_UINT32) prec; + cmptparm.bpp = (OPJ_UINT32) prec; + cmptparm.dx = (OPJ_UINT32) parameters->subsampling_dx; + cmptparm.dy = (OPJ_UINT32) parameters->subsampling_dy; /* create the image */ - image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space); + image = opj_image_create((OPJ_UINT32) numcomps, &cmptparm, color_space); if (!image) { fclose(f); return NULL; @@ -1294,7 +1281,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) if (force8) { v = readuchar(f) + adjustS; v = (v << ushift) + (v >> dshift); - comp->data[i] = (unsigned char)v; + comp->data[i] = (unsigned char) v; if (v > max) { max = v; @@ -1316,7 +1303,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) } } else { if (!comp->sgnd) { - v = (int)readuint(f, bigendian); + v = (int) readuint(f, bigendian); } else { v = (int) readuint(f, bigendian); } @@ -1327,15 +1314,14 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) comp->data[i] = v; } fclose(f); - comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1; + comp->bpp = (OPJ_UINT32) int_floorlog2(max) + 1; return image; } -#define CLAMP(x,a,b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x))) +#define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x))) -static INLINE int clamp(const int value, const int prec, const int sgnd) -{ +static INLINE int clamp(const int value, const int prec, const int sgnd) { if (sgnd) { if (prec <= 8) { return CLAMP(value, -128, 127); @@ -1355,8 +1341,7 @@ static INLINE int clamp(const int value, const int prec, const int sgnd) } } -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; @@ -1378,7 +1363,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) goto fin; } if (total > 256) { - name = (char*)malloc(total + 1); + name = (char *) malloc(total + 1); if (name == NULL) { fprintf(stderr, "imagetopgx: memory out\n"); goto fin; @@ -1398,8 +1383,8 @@ int imagetopgx(opj_image_t * image, const char *outfile) goto fin; } - w = (int)image->comps[compno].w; - h = (int)image->comps[compno].h; + 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); @@ -1413,7 +1398,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) } if (nbytes == 1) { - unsigned char* line_buffer = malloc((size_t)w); + unsigned char *line_buffer = malloc((size_t) w); if (line_buffer == NULL) { fprintf(stderr, "Out of memory"); if (total > 256) { @@ -1424,18 +1409,18 @@ int imagetopgx(opj_image_t * image, const char *outfile) for (j = 0; j < h; j++) { if (comp->prec == 8 && comp->sgnd == 0) { for (i = 0; i < w; i++) { - line_buffer[i] = (unsigned char)CLAMP(image->comps[compno].data[j * w + i], 0, - 255); + line_buffer[i] = (unsigned char) CLAMP(image->comps[compno].data[j * w + i], 0, + 255); } } else { for (i = 0; i < w; i++) { line_buffer[i] = (unsigned char) - clamp(image->comps[compno].data[j * w + i], - (int)comp->prec, (int)comp->sgnd); + clamp(image->comps[compno].data[j * w + i], + (int) comp->prec, (int) comp->sgnd); } } - res = fwrite(line_buffer, 1, (size_t)w, fdest); - if (res != (size_t)w) { + res = fwrite(line_buffer, 1, (size_t) w, fdest); + if (res != (size_t) w) { fprintf(stderr, "failed to write %d bytes for %s\n", w, name); if (total > 256) { free(name); @@ -1450,11 +1435,11 @@ int imagetopgx(opj_image_t * image, const char *outfile) 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); + (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; + int v = (int) (val >> (j * 8)); + unsigned char byte = (unsigned char) v; res = fwrite(&byte, 1, 1, fdest); if (res < 1) { @@ -1475,7 +1460,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) fdest = NULL; } fails = 0; -fin: + fin: if (fdest) { fclose(fdest); } @@ -1495,8 +1480,7 @@ struct pnm_header { char ok; }; -static char *skip_white(char *s) -{ +static char *skip_white(char *s) { if (s != NULL) { while (*s) { if (*s == '\n' || *s == '\r') { @@ -1512,8 +1496,7 @@ static char *skip_white(char *s) return NULL; } -static char *skip_int(char *start, int *out_n) -{ +static char *skip_int(char *start, int *out_n) { char *s; char c; @@ -1538,8 +1521,7 @@ static char *skip_int(char *start, int *out_n) return s; } -static char *skip_idf(char *start, char out_idf[256]) -{ +static char *skip_idf(char *start, char out_idf[256]) { char *s; char c; @@ -1563,8 +1545,7 @@ static char *skip_idf(char *start, char out_idf[256]) return s; } -static void read_pnm_header(FILE *reader, struct pnm_header *ph) -{ +static void read_pnm_header(FILE *reader, struct pnm_header *ph) { int format, end, ttype; char idf[256], type[256]; char line[256]; @@ -1737,8 +1718,7 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph) } } -static int has_prec(int val) -{ +static int has_prec(int val) { if (val < 2) { return 1; } @@ -1787,8 +1767,7 @@ 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; @@ -1796,7 +1775,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) int i, compno, numcomps, w, h, prec, format; OPJ_COLOR_SPACE color_space; opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */ - opj_image_t * image = NULL; + opj_image_t *image = NULL; struct pnm_header header_info; if ((fp = fopen(filename, "rb")) == NULL) { @@ -1813,15 +1792,15 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) } if (header_info.width == 0 - || header_info.height == 0 - || (header_info.format == 7 && header_info.depth == 0)) { + || header_info.height == 0 + || (header_info.format == 7 && header_info.depth == 0)) { fclose(fp); return NULL; } /* This limitation could be removed by making sure to use size_t below */ if (header_info.height != 0 && - header_info.width > INT_MAX / header_info.height) { + header_info.width > INT_MAX / header_info.height) { fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n", header_info.width, header_info.height); fclose(fp); @@ -1831,28 +1810,28 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) format = header_info.format; switch (format) { - case 1: /* ascii bitmap */ - case 4: /* raw bitmap */ - numcomps = 1; - break; + case 1: /* ascii bitmap */ + case 4: /* raw bitmap */ + numcomps = 1; + break; - case 2: /* ascii greymap */ - case 5: /* raw greymap */ - numcomps = 1; - break; + case 2: /* ascii greymap */ + case 5: /* raw greymap */ + numcomps = 1; + break; - case 3: /* ascii pixmap */ - case 6: /* raw pixmap */ - numcomps = 3; - break; + case 3: /* ascii pixmap */ + case 6: /* raw pixmap */ + numcomps = 3; + break; - case 7: /* arbitrary map */ - numcomps = header_info.depth; - break; + case 7: /* arbitrary map */ + 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 */ @@ -1871,18 +1850,18 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) subsampling_dx = parameters->subsampling_dx; subsampling_dy = parameters->subsampling_dy; - memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t)); + memset(&cmptparm[0], 0, (size_t) numcomps * sizeof(opj_image_cmptparm_t)); for (i = 0; i < numcomps; i++) { - cmptparm[i].prec = (OPJ_UINT32)prec; - cmptparm[i].bpp = (OPJ_UINT32)prec; + cmptparm[i].prec = (OPJ_UINT32) prec; + cmptparm[i].bpp = (OPJ_UINT32) prec; cmptparm[i].sgnd = 0; - cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; - cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; - cmptparm[i].w = (OPJ_UINT32)w; - cmptparm[i].h = (OPJ_UINT32)h; + cmptparm[i].dx = (OPJ_UINT32) subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32) subsampling_dy; + cmptparm[i].w = (OPJ_UINT32) w; + cmptparm[i].h = (OPJ_UINT32) h; } - image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + image = opj_image_create((OPJ_UINT32) numcomps, &cmptparm[0], color_space); if (!image) { fclose(fp); @@ -1890,12 +1869,12 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) } /* set image offset and reference grid */ - image->x0 = (OPJ_UINT32)parameters->image_offset_x0; - image->y0 = (OPJ_UINT32)parameters->image_offset_y0; - 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); + image->x0 = (OPJ_UINT32) parameters->image_offset_x0; + image->y0 = (OPJ_UINT32) parameters->image_offset_y0; + 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 */ unsigned int index; @@ -1910,7 +1889,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) return NULL; } - image->comps[compno].data[i] = (OPJ_INT32)(index * 255) / header_info.maxval; + image->comps[compno].data[i] = (OPJ_INT32) (index * 255) / header_info.maxval; } } } else if ((format == 5) @@ -1977,7 +1956,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) return NULL; } } - image->comps[0].data[i] = ((((unsigned char)uc >> bit) & 1) ? 0 : 255); + image->comps[0].data[i] = ((((unsigned char) uc >> bit) & 1) ? 0 : 255); --bit; ++i; } @@ -2000,15 +1979,14 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) return image; }/* pnmtoimage() */ -static int are_comps_similar(opj_image_t * image) -{ +static int are_comps_similar(opj_image_t *image) { unsigned int i; for (i = 1; i < image->numcomps; i++) { if (image->comps[0].dx != image->comps[i].dx || - image->comps[0].dy != image->comps[i].dy || - (i <= 2 && - (image->comps[0].prec != image->comps[i].prec || - image->comps[0].sgnd != image->comps[i].sgnd))) { + image->comps[0].dy != image->comps[i].dy || + (i <= 2 && + (image->comps[0].prec != image->comps[i].prec || + image->comps[0].sgnd != image->comps[i].sgnd))) { return OPJ_FALSE; } } @@ -2016,8 +1994,7 @@ static int are_comps_similar(opj_image_t * image) } -int imagetopnm(opj_image_t * image, const char *outfile, int force_split) -{ +int imagetopnm(opj_image_t *image, const char *outfile, int force_split) { int *red, *green, *blue, *alpha; int wr, hr, max; int i; @@ -2031,9 +2008,9 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) 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); + "\n\t: refused.\n", __FILE__, __LINE__, prec); return 1; } two = has_alpha = 0; @@ -2052,7 +2029,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } if ((force_split == 0) && ncomp >= 2 && - are_comps_similar(image)) { + are_comps_similar(image)) { fdest = fopen(outfile, "wb"); if (!fdest) { @@ -2061,8 +2038,8 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } two = (prec > 8); triple = (ncomp > 2); - wr = (int)image->comps[0].w; - hr = (int)image->comps[0].h; + wr = (int) image->comps[0].w; + hr = (int) image->comps[0].h; max = (1 << prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2); @@ -2095,7 +2072,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA"); fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %u\n" - "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(), + "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(), wr, hr, ncomp, max, tt); alpha = image->comps[ncomp - 1].data; adjustA = (image->comps[ncomp - 1].sgnd ? @@ -2125,7 +2102,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } /* netpbm: */ - fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v); + fprintf(fdest, "%c%c", (unsigned char) (v >> 8), (unsigned char) v); if (triple) { v = *green + adjustG; @@ -2137,9 +2114,9 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } /* netpbm: */ - fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v); + fprintf(fdest, "%c%c", (unsigned char) (v >> 8), (unsigned char) v); - v = *blue + adjustB; + v = *blue + adjustB; ++blue; if (v > 65535) { v = 65535; @@ -2148,7 +2125,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } /* netpbm: */ - fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v); + fprintf(fdest, "%c%c", (unsigned char) (v >> 8), (unsigned char) v); }/* if(triple) */ @@ -2162,7 +2139,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } /* netpbm: */ - fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v); + fprintf(fdest, "%c%c", (unsigned char) (v >> 8), (unsigned char) v); } continue; @@ -2176,7 +2153,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) v = 0; } - fprintf(fdest, "%c", (unsigned char)v); + fprintf(fdest, "%c", (unsigned char) v); if (triple) { v = *green++; if (v > 255) { @@ -2185,7 +2162,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) v = 0; } - fprintf(fdest, "%c", (unsigned char)v); + fprintf(fdest, "%c", (unsigned char) v); v = *blue++; if (v > 255) { v = 255; @@ -2193,7 +2170,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) v = 0; } - fprintf(fdest, "%c", (unsigned char)v); + fprintf(fdest, "%c", (unsigned char) v); } if (has_alpha) { v = *alpha++; @@ -2203,7 +2180,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) v = 0; } - fprintf(fdest, "%c", (unsigned char)v); + fprintf(fdest, "%c", (unsigned char) v); } } /* for(i */ @@ -2217,7 +2194,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) fprintf(stderr, "WARNING -> [PGM file] Only the first component\n"); fprintf(stderr, " is written to the file\n"); } - destname = (char*)malloc(strlen(outfile) + 8); + destname = (char *) malloc(strlen(outfile) + 8); if (destname == NULL) { fprintf(stderr, "imagetopnm: memory out\n"); return 1; @@ -2240,9 +2217,9 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) free(destname); return 1; } - wr = (int)image->comps[compno].w; - hr = (int)image->comps[compno].h; - prec = (int)image->comps[compno].prec; + wr = (int) image->comps[compno].w; + hr = (int) image->comps[compno].h; + prec = (int) image->comps[compno].prec; max = (1 << prec) - 1; fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n", @@ -2255,7 +2232,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } 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++) { @@ -2268,7 +2245,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } /* netpbm: */ - fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v); + fprintf(fdest, "%c%c", (unsigned char) (v >> 8), (unsigned char) v); if (has_alpha) { v = *alpha++; @@ -2279,7 +2256,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) } /* netpbm: */ - fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v); + fprintf(fdest, "%c%c", (unsigned char) (v >> 8), (unsigned char) v); } }/* for(i */ } else { /* prec <= 8 */ @@ -2292,7 +2269,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) v = 0; } - fprintf(fdest, "%c", (unsigned char)v); + fprintf(fdest, "%c", (unsigned char) v); } } fclose(fdest); @@ -2307,9 +2284,8 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) 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; @@ -2317,11 +2293,11 @@ static opj_image_t* rawtoimage_common(const char *filename, int i, compno, numcomps, w, h; OPJ_COLOR_SPACE color_space; opj_image_cmptparm_t *cmptparm; - opj_image_t * image = NULL; + opj_image_t *image = NULL; unsigned short ch; if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & - raw_cp->rawBitDepth)) == 0) { + raw_cp->rawBitDepth)) == 0) { fprintf(stderr, "\nError: invalid raw image parameters\n"); fprintf(stderr, "Please use the Format option -F:\n"); fprintf(stderr, @@ -2355,8 +2331,8 @@ static opj_image_t* rawtoimage_common(const char *filename, } w = raw_cp->rawWidth; h = raw_cp->rawHeight; - cmptparm = (opj_image_cmptparm_t*) calloc((OPJ_UINT32)numcomps, - sizeof(opj_image_cmptparm_t)); + cmptparm = (opj_image_cmptparm_t *) calloc((OPJ_UINT32) numcomps, + sizeof(opj_image_cmptparm_t)); if (!cmptparm) { fprintf(stderr, "Failed to allocate image components parameters !!\n"); fprintf(stderr, "Aborting\n"); @@ -2365,28 +2341,28 @@ static opj_image_t* rawtoimage_common(const char *filename, } /* initialize image components */ for (i = 0; i < numcomps; i++) { - cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth; - cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth; - cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned; - cmptparm[i].dx = (OPJ_UINT32)(subsampling_dx * raw_cp->rawComps[i].dx); - cmptparm[i].dy = (OPJ_UINT32)(subsampling_dy * raw_cp->rawComps[i].dy); - cmptparm[i].w = (OPJ_UINT32)w; - cmptparm[i].h = (OPJ_UINT32)h; + cmptparm[i].prec = (OPJ_UINT32) raw_cp->rawBitDepth; + cmptparm[i].bpp = (OPJ_UINT32) raw_cp->rawBitDepth; + cmptparm[i].sgnd = (OPJ_UINT32) raw_cp->rawSigned; + cmptparm[i].dx = (OPJ_UINT32) (subsampling_dx * raw_cp->rawComps[i].dx); + cmptparm[i].dy = (OPJ_UINT32) (subsampling_dy * raw_cp->rawComps[i].dy); + cmptparm[i].w = (OPJ_UINT32) w; + cmptparm[i].h = (OPJ_UINT32) h; } /* create the image */ - image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + image = opj_image_create((OPJ_UINT32) numcomps, &cmptparm[0], color_space); free(cmptparm); if (!image) { fclose(f); 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 = (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; + image->x0 = (OPJ_UINT32) parameters->image_offset_x0; + image->y0 = (OPJ_UINT32) parameters->image_offset_y0; + 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) { unsigned char value = 0; @@ -2400,7 +2376,7 @@ static opj_image_t* rawtoimage_common(const char *filename, fclose(f); return NULL; } - image->comps[compno].data[i] = raw_cp->rawSigned ? (char)value : value; + image->comps[compno].data[i] = raw_cp->rawSigned ? (char) value : value; } } } else if (raw_cp->rawBitDepth <= 16) { @@ -2424,11 +2400,11 @@ static opj_image_t* rawtoimage_common(const char *filename, return NULL; } if (big_endian) { - value = (unsigned short)((temp1 << 8) + temp2); + value = (unsigned short) ((temp1 << 8) + temp2); } else { - value = (unsigned short)((temp2 << 8) + temp1); + value = (unsigned short) ((temp2 << 8) + temp1); } - image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value; + image->comps[compno].data[i] = raw_cp->rawSigned ? (short) value : value; } } } else { @@ -2447,21 +2423,18 @@ static opj_image_t* rawtoimage_common(const char *filename, 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); } -static int imagetoraw_common(opj_image_t * image, const char *outfile, - OPJ_BOOL big_endian) -{ +static int imagetoraw_common(opj_image_t *image, const char *outfile, + OPJ_BOOL big_endian) { FILE *rawFile = NULL; size_t res; unsigned int compno, numcomps; @@ -2469,7 +2442,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, int line, row, curr, mask; int *ptr; unsigned char uc; - (void)big_endian; + (void) big_endian; if ((image->numcomps * image->x1 * image->y1) == 0) { fprintf(stderr, "\nError: invalid raw image parameters\n"); @@ -2518,22 +2491,22 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, 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; + w = (int) image->comps[compno].w; + h = (int) image->comps[compno].h; 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++) { + for (row = 0; row < w; row++) { curr = *ptr; if (curr > 127) { curr = 127; } else if (curr < -128) { curr = -128; } - uc = (unsigned char)(curr & mask); + uc = (unsigned char) (curr & mask); res = fwrite(&uc, 1, 1, rawFile); if (res < 1) { fprintf(stderr, "failed to write 1 byte for %s\n", outfile); @@ -2546,14 +2519,14 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, mask = (1 << image->comps[compno].prec) - 1; ptr = image->comps[compno].data; for (line = 0; line < h; line++) { - for (row = 0; row < w; row++) { + for (row = 0; row < w; row++) { curr = *ptr; if (curr > 255) { curr = 255; } else if (curr < 0) { curr = 0; } - uc = (unsigned char)(curr & mask); + uc = (unsigned char) (curr & mask); res = fwrite(&uc, 1, 1, rawFile); if (res < 1) { fprintf(stderr, "failed to write 1 byte for %s\n", outfile); @@ -2572,14 +2545,14 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, mask = (1 << image->comps[compno].prec) - 1; ptr = image->comps[compno].data; for (line = 0; line < h; line++) { - for (row = 0; row < w; row++) { + for (row = 0; row < w; row++) { curr = *ptr; if (curr > 32767) { curr = 32767; } else if (curr < -32768) { curr = -32768; } - uc16.val = (signed short)(curr & mask); + uc16.val = (signed short) (curr & mask); res = fwrite(uc16.vals, 1, 2, rawFile); if (res < 2) { fprintf(stderr, "failed to write 2 byte for %s\n", outfile); @@ -2596,14 +2569,14 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, mask = (1 << image->comps[compno].prec) - 1; ptr = image->comps[compno].data; for (line = 0; line < h; line++) { - for (row = 0; row < w; row++) { + for (row = 0; row < w; row++) { curr = *ptr; if (curr > 65535) { curr = 65535; } else if (curr < 0) { curr = 0; } - uc16.val = (unsigned short)(curr & mask); + uc16.val = (unsigned short) (curr & mask); res = fwrite(uc16.vals, 1, 2, rawFile); if (res < 2) { fprintf(stderr, "failed to write 2 byte for %s\n", outfile); @@ -2622,18 +2595,180 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, } } fails = 0; -fin: + fin: fclose(rawFile); return fails; } -int imagetoraw(opj_image_t * image, const char *outfile) -{ +int imagetoraw_c_vector(opj_image_t *image, c_vector *outfile, + OPJ_BOOL big_endian) { + size_t res; + unsigned int compno, numcomps; + int w, h, fails; + int line, row, curr, mask; + int *ptr; + unsigned char uc; + (void) big_endian; + + if ((image->numcomps * image->x1 * image->y1) == 0) { + fprintf(stderr, "\nError: invalid raw image parameters\n"); + return 1; + } + + numcomps = image->numcomps; + + if (numcomps > 4) { + numcomps = 4; + } + + for (compno = 1; compno < numcomps; ++compno) { + if (image->comps[0].dx != image->comps[compno].dx) { + break; + } + if (image->comps[0].dy != image->comps[compno].dy) { + break; + } + if (image->comps[0].prec != image->comps[compno].prec) { + break; + } + if (image->comps[0].sgnd != image->comps[compno].sgnd) { + break; + } + } + if (compno != numcomps) { + fprintf(stderr, + "imagetoraw_common: All components shall have the same subsampling, same bit depth, same sign.\n"); + fprintf(stderr, "\tAborting\n"); + return 1; + } + + if (!outfile) { + fprintf(stderr, "Failed to writing !!\n"); + return 1; + } + + fails = 1; + fprintf(stdout, "Raw image characteristics: %d components\n", image->numcomps); + + 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) { + 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; + } + uc = (unsigned char) (curr & mask); + res = c_vector_push_back(outfile, &uc, 0, 1); + if (res != 0) { + fprintf(stderr, "failed to write 1 byte\n"); + goto fin; + } + ptr++; + } + } + } 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; + } + uc = (unsigned char) (curr & mask); + res = c_vector_push_back(outfile, &uc, 0, 1); + if (res != 0) { + fprintf(stderr, "failed to write 1 byte\n"); + goto fin; + } + ptr++; + } + } + } + } 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; + } + uc16.val = (signed short) (curr & mask); + res = c_vector_push_back(outfile, uc16.vals, 0, 2); + if (res != 0) { + fprintf(stderr, "failed to write 2 byte\n"); + goto fin; + } + ptr++; + } + } + } 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; + } + uc16.val = (unsigned short) (curr & mask); + res = c_vector_push_back(outfile, uc16.vals, 0, 2); + if (res != 0) { + fprintf(stderr, "failed to write 2 byte\n"); + goto fin; + } + ptr++; + } + } + } + } else if (image->comps[compno].prec <= 32) { + fprintf(stderr, "More than 16 bits per component not handled yet\n"); + goto fin; + } else { + fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec); + goto fin; + } + } + fails = 0; + fin: + return fails; +} + +int imagetoraw(opj_image_t *image, const char *outfile) { return imagetoraw_common(image, outfile, OPJ_TRUE); } -int imagetorawl(opj_image_t * image, const char *outfile) -{ +int imagetorawl(opj_image_t *image, const char *outfile) { return imagetoraw_common(image, outfile, OPJ_FALSE); } diff --git a/src/bin/jp2/convert.h b/src/bin/jp2/convert.h index 61439d49a..5ef50e7ad 100644 --- a/src/bin/jp2/convert.h +++ b/src/bin/jp2/convert.h @@ -93,6 +93,7 @@ int imagetotga(opj_image_t * image, const char *outfile); /* BMP conversion */ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters); int imagetobmp(opj_image_t *image, const char *outfile); +int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile); /* TIFF conversion*/ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters); @@ -117,6 +118,8 @@ opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp); +extern int imagetoraw_c_vector(opj_image_t * image, c_vector *outfile, + OPJ_BOOL big_endian); /* PNG conversion*/ extern int imagetopng(opj_image_t *image, const char *write_idf); extern opj_image_t* pngtoimage(const char *filename, diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index 2fc4e9bc4..1fee51843 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -70,7 +70,7 @@ typedef struct { 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_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 */ @@ -81,18 +81,17 @@ typedef struct { } 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]]; + pDst[x] = (OPJ_INT32) pLUT[pSrc[x]]; } pSrc += srcStride; pDst += dstStride; @@ -100,27 +99,26 @@ static void opj_applyLUT8u_8u32s_C1R( } 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]; + 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]; + 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]; @@ -129,33 +127,31 @@ static void opj_applyLUT8u_8u32s_C1P3R( } } -static void bmp24toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, - opj_image_t* image) -{ +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; + 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 */ + 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) -{ +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; @@ -174,28 +170,27 @@ static void bmp_mask_get_shift_and_prec(OPJ_UINT32 mask, OPJ_UINT32* 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) -{ +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_UINT32 redShift, redPrec; + OPJ_UINT32 redShift, redPrec; OPJ_UINT32 greenShift, greenPrec; - OPJ_UINT32 blueShift, bluePrec; + OPJ_UINT32 blueShift, bluePrec; OPJ_UINT32 alphaShift, alphaPrec; - width = image->comps[0].w; + 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(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(blueMask, &blueShift, &bluePrec); bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec); image->comps[0].bpp = redPrec; @@ -215,20 +210,20 @@ static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, 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 */ + 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 */ + image->comps[3].data[index] = (OPJ_INT32) ((value & alphaMask) >> + alphaShift); /* A */ } index++; } @@ -236,28 +231,27 @@ static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 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) -{ +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_UINT32 redShift, redPrec; + OPJ_UINT32 redShift, redPrec; OPJ_UINT32 greenShift, greenPrec; - OPJ_UINT32 blueShift, bluePrec; + OPJ_UINT32 blueShift, bluePrec; OPJ_UINT32 alphaShift, alphaPrec; - width = image->comps[0].w; + 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(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(blueMask, &blueShift, &bluePrec); bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec); image->comps[0].bpp = redPrec; @@ -277,18 +271,18 @@ static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, 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; + 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 */ + 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 */ + image->comps[3].data[index] = (OPJ_INT32) ((value & alphaMask) >> + alphaShift); /* A */ } index++; } @@ -296,39 +290,37 @@ static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, } } -static opj_image_t* bmp8toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, - opj_image_t* image, OPJ_UINT8 const* const* pLUT) -{ +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; + 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); + 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]; + 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, + 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); +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"); @@ -337,140 +329,140 @@ static OPJ_BOOL bmp_read_file_header(FILE* IN, OPJ_BITMAPFILEHEADER* header) /* 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->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->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->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; + 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) -{ + +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; + 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; + 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->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->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->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); + header->biBitCount = (OPJ_UINT16) getc(IN); + header->biBitCount |= (OPJ_UINT16) ((OPJ_UINT32) getc(IN) << 8); if (header->biBitCount == 0) { fprintf(stderr, "Error, invalid biBitCount %d\n", 0); return OPJ_FALSE; } 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; + 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->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; if (!header->biRedMask) { fprintf(stderr, "Error, invalid red mask value %d\n", header->biRedMask); return OPJ_FALSE; } - 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->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; if (!header->biGreenMask) { fprintf(stderr, "Error, invalid green mask value %d\n", header->biGreenMask); return OPJ_FALSE; } - 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->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; if (!header->biBlueMask) { fprintf(stderr, "Error, invalid blue mask value %d\n", header->biBlueMask); return OPJ_FALSE; } - 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; + 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; + 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)) { @@ -478,49 +470,48 @@ static OPJ_BOOL bmp_read_info_header(FILE* IN, OPJ_BITMAPINFOHEADER* header) 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->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->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; + 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; + 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) -{ +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)) { @@ -531,9 +522,8 @@ static OPJ_BOOL bmp_read_raw_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, return OPJ_TRUE; } -static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData, - OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height) -{ +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, written; OPJ_UINT8 *pix; const OPJ_UINT8 *beyond; @@ -556,10 +546,10 @@ static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData, if (c1_int == EOF) { return OPJ_FALSE; } - c1 = (OPJ_UINT8)c1_int; + c1 = (OPJ_UINT8) c1_int; for (j = 0; (j < c) && (x < width) && - ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { + ((OPJ_SIZE_T) pix < (OPJ_SIZE_T) beyond); j++, x++, pix++) { *pix = c1; written++; } @@ -580,28 +570,28 @@ static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData, if (c == EOF) { return OPJ_FALSE; } - x += (OPJ_UINT32)c; + x += (OPJ_UINT32) c; c = getc(IN); if (c == EOF) { return OPJ_FALSE; } - y += (OPJ_UINT32)c; + 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_SIZE_T) pix < (OPJ_SIZE_T) beyond); j++, x++, pix++) { int c1_int; OPJ_UINT8 c1; c1_int = getc(IN); if (c1_int == EOF) { return OPJ_FALSE; } - c1 = (OPJ_UINT8)c1_int; + c1 = (OPJ_UINT8) c1_int; *pix = c1; written++; } - if ((OPJ_UINT32)c & 1U) { /* skip padding byte */ + if ((OPJ_UINT32) c & 1U) { /* skip padding byte */ c = getc(IN); if (c == EOF) { return OPJ_FALSE; @@ -619,9 +609,8 @@ static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData, return OPJ_TRUE; } -static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, - OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height) -{ +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, written; OPJ_UINT8 *pix; const OPJ_UINT8 *beyond; @@ -643,11 +632,11 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, if (c1_int == EOF) { return OPJ_FALSE; } - c1 = (OPJ_UINT8)c1_int; + c1 = (OPJ_UINT8) c1_int; 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)); + ((OPJ_SIZE_T) pix < (OPJ_SIZE_T) beyond); j++, x++, pix++) { + *pix = (OPJ_UINT8) ((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU)); written++; } } else { /* absolute mode */ @@ -667,28 +656,28 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, if (c == EOF) { return OPJ_FALSE; } - x += (OPJ_UINT32)c; + x += (OPJ_UINT32) c; c = getc(IN); if (c == EOF) { return OPJ_FALSE; } - y += (OPJ_UINT32)c; + 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++) { + ((OPJ_SIZE_T) pix < (OPJ_SIZE_T) beyond); j++, x++, pix++) { if ((j & 1) == 0) { int c1_int; c1_int = getc(IN); if (c1_int == EOF) { return OPJ_FALSE; } - c1 = (OPJ_UINT8)c1_int; + c1 = (OPJ_UINT8) c1_int; } - *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU)); + *pix = (OPJ_UINT8) ((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU)); written++; } if (((c & 3) == 1) || ((c & 3) == 2)) { /* skip padding byte */ @@ -707,18 +696,17 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, return OPJ_TRUE; } -opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) -{ +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; + 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_UINT8 *pData = NULL; OPJ_UINT32 stride; pLUT[0] = lut_R; @@ -756,10 +744,10 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) 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 */ + 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) { @@ -778,22 +766,22 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) return NULL; } - if (Info_h.biBitCount > (((OPJ_UINT32) - 1) - 31) / Info_h.biWidth) { + if (Info_h.biBitCount > (((OPJ_UINT32) -1) - 31) / Info_h.biWidth) { fclose(IN); return NULL; } 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... */ - if (8 > (((OPJ_UINT32) - 1) - 31) / Info_h.biWidth) { + Info_h.biCompression == 2) { /* RLE 4 gets decoded as 8 bits data for now... */ + if (8 > (((OPJ_UINT32) -1) - 31) / Info_h.biWidth) { fclose(IN); return NULL; } stride = ((Info_h.biWidth * 8U + 31U) / 32U) * 4U; } - if (stride > ((OPJ_UINT32) - 1) / sizeof(OPJ_UINT8) / Info_h.biHeight) { + if (stride > ((OPJ_UINT32) -1) / sizeof(OPJ_UINT8) / Info_h.biHeight) { fclose(IN); return NULL; } @@ -804,29 +792,29 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) } /* Place the cursor at the beginning of the image information */ fseek(IN, 0, SEEK_SET); - fseek(IN, (long)File_h.bfOffBits, 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; + 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); @@ -838,12 +826,12 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) memset(&cmptparm[0], 0, sizeof(cmptparm)); for (i = 0; i < 4U; i++) { cmptparm[i].prec = 8; - cmptparm[i].bpp = 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; + 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], @@ -858,12 +846,12 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) } /* 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->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; + parameters->subsampling_dy + 1U; /* Read the data */ if (Info_h.biBitCount == 24 && Info_h.biCompression == 0) { /*RGB */ @@ -881,10 +869,10 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) 0x00000000U); } else if (Info_h.biBitCount == 32 && Info_h.biCompression == 3) { /* bitmask */ if ((Info_h.biRedMask == 0U) && (Info_h.biGreenMask == 0U) && - (Info_h.biBlueMask == 0U)) { - Info_h.biRedMask = 0x00FF0000U; + (Info_h.biBlueMask == 0U)) { + Info_h.biRedMask = 0x00FF0000U; Info_h.biGreenMask = 0x0000FF00U; - Info_h.biBlueMask = 0x000000FFU; + Info_h.biBlueMask = 0x000000FFU; } bmpmask32toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask); @@ -892,10 +880,10 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) 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.biBlueMask == 0U)) { + Info_h.biRedMask = 0xF800U; Info_h.biGreenMask = 0x07E0U; - Info_h.biBlueMask = 0x001FU; + Info_h.biBlueMask = 0x001FU; } bmpmask16toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask); @@ -911,8 +899,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) 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; @@ -924,13 +911,13 @@ int imagetobmp(opj_image_t * image, const char *outfile) return 1; } if (image->numcomps >= 3 && 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 - && image->comps[0].sgnd == image->comps[1].sgnd - && image->comps[1].sgnd == image->comps[2].sgnd) { + && 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 + && image->comps[0].sgnd == image->comps[1].sgnd + && image->comps[1].sgnd == image->comps[2].sgnd) { /* -->> -->> -->> -->> 24 bits color @@ -942,18 +929,18 @@ int imagetobmp(opj_image_t * image, const char *outfile) return 1; } - w = (int)image->comps[0].w; - h = (int)image->comps[0].h; + w = (int) image->comps[0].w; + h = (int) image->comps[0].h; fprintf(fdest, "BM"); /* FILE HEADER */ /* ------------- */ fprintf(fdest, "%c%c%c%c", - (OPJ_UINT8)(h * w * 3 + 3 * h * (w % 2) + 54) & 0xff, - (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff, - (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff, - (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff); + (OPJ_UINT8) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff, @@ -963,22 +950,22 @@ int imagetobmp(opj_image_t * image, const char *outfile) /* ------------- */ fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((w) & 0xff), - (OPJ_UINT8)((w) >> 8) & 0xff, - (OPJ_UINT8)((w) >> 16) & 0xff, - (OPJ_UINT8)((w) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((h) & 0xff), - (OPJ_UINT8)((h) >> 8) & 0xff, - (OPJ_UINT8)((h) >> 16) & 0xff, - (OPJ_UINT8)((h) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (OPJ_UINT8) ((w) & 0xff), + (OPJ_UINT8) ((w) >> 8) & 0xff, + (OPJ_UINT8) ((w) >> 16) & 0xff, + (OPJ_UINT8) ((w) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (OPJ_UINT8) ((h) & 0xff), + (OPJ_UINT8) ((h) >> 8) & 0xff, + (OPJ_UINT8) ((h) >> 16) & 0xff, + (OPJ_UINT8) ((h) >> 24) & 0xff); fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)(3 * h * w + 3 * h * (w % 2)) & 0xff, - (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff, - (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff, - (OPJ_UINT8)((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (OPJ_UINT8) (3 * h * w + 3 * h * (w % 2)) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, @@ -989,21 +976,21 @@ int imagetobmp(opj_image_t * image, const char *outfile) ((0) >> 24) & 0xff); if (image->comps[0].prec > 8) { - adjustR = (int)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 { adjustR = 0; } if (image->comps[1].prec > 8) { - adjustG = (int)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 { adjustG = 0; } if (image->comps[2].prec > 8) { - adjustB = (int)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 { @@ -1024,7 +1011,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) } else if (r < 0) { r = 0; } - rc = (OPJ_UINT8)r; + 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); @@ -1036,7 +1023,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) } else if (g < 0) { g = 0; } - gc = (OPJ_UINT8)g; + 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); @@ -1048,7 +1035,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) } else if (b < 0) { b = 0; } - bc = (OPJ_UINT8)b; + bc = (OPJ_UINT8) b; fprintf(fdest, "%c%c%c", bc, gc, rc); @@ -1074,17 +1061,17 @@ int imagetobmp(opj_image_t * image, const char *outfile) fprintf(stderr, "imagetobmp: only first component of %d is used.\n", image->numcomps); } - w = (int)image->comps[0].w; - h = (int)image->comps[0].h; + w = (int) image->comps[0].w; + h = (int) image->comps[0].h; fprintf(fdest, "BM"); /* FILE HEADER */ /* ------------- */ - fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)(h * w + 54 + 1024 + h * (w % 2)) & 0xff, - (OPJ_UINT8)((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff, - (OPJ_UINT8)((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff, - (OPJ_UINT8)((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (OPJ_UINT8) (h * w + 54 + 1024 + h * (w % 2)) & 0xff, + (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff, + (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff, + (OPJ_UINT8) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, @@ -1095,22 +1082,22 @@ int imagetobmp(opj_image_t * image, const char *outfile) /* ------------- */ fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((w) & 0xff), - (OPJ_UINT8)((w) >> 8) & 0xff, - (OPJ_UINT8)((w) >> 16) & 0xff, - (OPJ_UINT8)((w) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)((h) & 0xff), - (OPJ_UINT8)((h) >> 8) & 0xff, - (OPJ_UINT8)((h) >> 16) & 0xff, - (OPJ_UINT8)((h) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (OPJ_UINT8) ((w) & 0xff), + (OPJ_UINT8) ((w) >> 8) & 0xff, + (OPJ_UINT8) ((w) >> 16) & 0xff, + (OPJ_UINT8) ((w) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (OPJ_UINT8) ((h) & 0xff), + (OPJ_UINT8) ((h) >> 8) & 0xff, + (OPJ_UINT8) ((h) >> 16) & 0xff, + (OPJ_UINT8) ((h) >> 24) & 0xff); fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (OPJ_UINT8)(h * w + h * (w % 2)) & 0xff, - (OPJ_UINT8)((h * w + h * (w % 2)) >> 8) & 0xff, - (OPJ_UINT8)((h * w + h * (w % 2)) >> 16) & 0xff, - (OPJ_UINT8)((h * w + h * (w % 2)) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (OPJ_UINT8) (h * w + h * (w % 2)) & 0xff, + (OPJ_UINT8) ((h * w + h * (w % 2)) >> 8) & 0xff, + (OPJ_UINT8) ((h * w + h * (w % 2)) >> 16) & 0xff, + (OPJ_UINT8) ((h * w + h * (w % 2)) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, @@ -1121,7 +1108,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) ((256) >> 16) & 0xff, ((256) >> 24) & 0xff); if (image->comps[0].prec > 8) { - adjustR = (int)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 { @@ -1146,7 +1133,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) r = 0; } - fprintf(fdest, "%c", (OPJ_UINT8)r); + fprintf(fdest, "%c", (OPJ_UINT8) r); if ((i + 1) % w == 0) { for (pad = (w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) { /* ADD */ @@ -1159,3 +1146,277 @@ int imagetobmp(opj_image_t * image, const char *outfile) return 0; } + +int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile) { + int w, h; + int i, pad; + int adjustR, adjustG, adjustB; + + if (image->comps[0].prec < 8) { + fprintf(stderr, "imagetobmp: Unsupported precision: %d\n", + image->comps[0].prec); + return 1; + } + if (image->numcomps >= 3 && 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 + && image->comps[0].sgnd == image->comps[1].sgnd + && image->comps[1].sgnd == image->comps[2].sgnd) { + + /* -->> -->> -->> -->> + 24 bits color + <<-- <<-- <<-- <<-- */ + + if (!outfile) { + fprintf(stderr, "ERROR -> failed for writing\n"); + return 1; + } + + w = (int) image->comps[0].w; + h = (int) image->comps[0].h; + + c_vector_push_back(outfile, "BM", 0, 2); + + /* FILE HEADER */ + /* ------------- */ + OPJ_UINT8 _1185[4] = { + (OPJ_UINT8) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff}; + c_vector_push_back(outfile, _1185, 0, 4); + OPJ_UINT8 _1191[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, + ((0) >> 24) & 0xff}; + c_vector_push_back(outfile, _1191, 0, 4); + + OPJ_UINT8 _1195[4] = {(54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff, + ((54) >> 24) & 0xff}; + c_vector_push_back(outfile, _1195, 0, 4); + + /* INFO HEADER */ + /* ------------- */ + OPJ_UINT8 _1201[4] = {(40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, + ((40) >> 24) & 0xff}; + c_vector_push_back(outfile, _1201, 0, 4); + + OPJ_UINT8 _1205[4] = {(OPJ_UINT8) ((w) & 0xff), + (OPJ_UINT8) ((w) >> 8) & 0xff, + (OPJ_UINT8) ((w) >> 16) & 0xff, + (OPJ_UINT8) ((w) >> 24) & 0xff}; + c_vector_push_back(outfile, _1205, 0, 4); + OPJ_UINT8 _1210[4] = {(OPJ_UINT8) ((h) & 0xff), + (OPJ_UINT8) ((h) >> 8) & 0xff, + (OPJ_UINT8) ((h) >> 16) & 0xff, + (OPJ_UINT8) ((h) >> 24) & 0xff}; + c_vector_push_back(outfile, _1210, 0, 4); + OPJ_UINT8 _1215[4] = {(1) & 0xff, ((1) >> 8) & 0xff, (24) & 0xff, ((24) >> 8) & 0xff}; + c_vector_push_back(outfile, _1215, 0, 4); + OPJ_UINT8 _1218[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, + ((0) >> 24) & 0xff}; + c_vector_push_back(outfile, _1218, 0, 4); + OPJ_UINT8 _1220[4] = {(OPJ_UINT8) (3 * h * w + 3 * h * (w % 2)) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff, + (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff}; + c_vector_push_back(outfile, _1220, 0, 4); + OPJ_UINT8 _1225[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, + ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; + c_vector_push_back(outfile, _1225, 0, 4); + OPJ_UINT8 _1228[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, + ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; + c_vector_push_back(outfile, _1228, 0, 4); + OPJ_UINT8 _1231[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, + ((0) >> 24) & 0xff}; + c_vector_push_back(outfile, _1231, 0, 4); + OPJ_UINT8 _1234[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, + ((0) >> 24) & 0xff}; + c_vector_push_back(outfile, _1234, 0, 4); + + 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 { + 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 { + 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 { + adjustB = 0; + } + + for (i = 0; i < w * h; i++) { + OPJ_UINT8 rc, gc, bc; + int r, g, b; + + 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); + if (adjustR > 0) { + r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2)); + } + 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); + if (adjustG > 0) { + g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2)); + } + 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); + if (adjustB > 0) { + b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2)); + } + if (b > 255) { + b = 255; + } else if (b < 0) { + b = 0; + } + bc = (OPJ_UINT8) b; + + OPJ_UINT8 _1300[3] = {bc, gc, rc}; + c_vector_push_back(outfile, _1300, 0, 3); + + if ((i + 1) % w == 0) { + for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) { /* ADD */ + c_vector_push_back(outfile, "0", 0, 1); + } + } + } + } else { /* Gray-scale */ + + /* -->> -->> -->> -->> + 8 bits non code (Gray scale) + <<-- <<-- <<-- <<-- */ + + if (!outfile) { + fprintf(stderr, "ERROR -> failed to writing\n"); + return 1; + } + if (image->numcomps > 1) { + fprintf(stderr, "imagetobmp: only first component of %d is used.\n", + image->numcomps); + } + w = (int) image->comps[0].w; + h = (int) image->comps[0].h; + + + c_vector_push_back(outfile, "BM", 0, 2); + + /* FILE HEADER */ + /* ------------- */ + OPJ_UINT8 _1331[4] = {(OPJ_UINT8) (h * w + 54 + 1024 + h * (w % 2)) & 0xff, + (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff, + (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff, + (OPJ_UINT8) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff}; + c_vector_push_back(outfile, _1331, 0, 4); + OPJ_UINT8 _1336[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, + ((0) >> 24) & 0xff}; + c_vector_push_back(outfile, _1336, 0, 4); + + OPJ_UINT8 _1340[4] = {(54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, + ((54 + 1024) >> 16) & 0xff, + ((54 + 1024) >> 24) & 0xff}; + c_vector_push_back(outfile, _1340, 0, 4); + + /* INFO HEADER */ + /* ------------- */ + OPJ_UINT8 _1347[4] = {(40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, + ((40) >> 24) & 0xff}; + c_vector_push_back(outfile, _1347, 0, 4); + OPJ_UINT8 _1350[4] = {(OPJ_UINT8) ((w) & 0xff), + (OPJ_UINT8) ((w) >> 8) & 0xff, + (OPJ_UINT8) ((w) >> 16) & 0xff, + (OPJ_UINT8) ((w) >> 24) & 0xff}; + c_vector_push_back(outfile, _1350, 0, 4); + OPJ_UINT8 _1355[4] = {(OPJ_UINT8) ((h) & 0xff), + (OPJ_UINT8) ((h) >> 8) & 0xff, + (OPJ_UINT8) ((h) >> 16) & 0xff, + (OPJ_UINT8) ((h) >> 24) & 0xff}; + c_vector_push_back(outfile, _1355, 0, 4); + OPJ_UINT8 _1360[4] = {(1) & 0xff, ((1) >> 8) & 0xff, (8) & 0xff, ((8) >> 8) & 0xff}; + c_vector_push_back(outfile, _1360, 0, 4); + OPJ_UINT8 _1363[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, + ((0) >> 24) & 0xff}; + c_vector_push_back(outfile, _1363, 0, 4); + OPJ_UINT8 _1366[4] = {(OPJ_UINT8) (h * w + h * (w % 2)) & 0xff, + (OPJ_UINT8) ((h * w + h * (w % 2)) >> 8) & 0xff, + (OPJ_UINT8) ((h * w + h * (w % 2)) >> 16) & 0xff, + (OPJ_UINT8) ((h * w + h * (w % 2)) >> 24) & 0xff}; + c_vector_push_back(outfile, _1366, 0, 4); + OPJ_UINT8 _1371[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, + ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; + c_vector_push_back(outfile, _1371, 0, 4); + OPJ_UINT8 _1374[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, + ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; + c_vector_push_back(outfile, _1374, 0, 4); + OPJ_UINT8 _1377[4] = {(256) & 0xff, ((256) >> 8) & 0xff, + ((256) >> 16) & 0xff, ((256) >> 24) & 0xff}; + c_vector_push_back(outfile, _1377, 0, 4); + OPJ_UINT8 _1380[4] = {(256) & 0xff, ((256) >> 8) & 0xff, + ((256) >> 16) & 0xff, ((256) >> 24) & 0xff}; + c_vector_push_back(outfile, _1380, 0, 4); + + 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 { + adjustR = 0; + } + + for (i = 0; i < 256; i++) { + OPJ_UINT8 _1392[4] = {i, i, i, 0}; + c_vector_push_back(outfile, _1392, 0, 4); + } + + for (i = 0; i < w * h; i++) { + int r; + + 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); + if (adjustR > 0) { + r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2)); + } + if (r > 255) { + r = 255; + } else if (r < 0) { + r = 0; + } + + OPJ_UINT8 _1410[1] = {(OPJ_UINT8) r}; + c_vector_push_back(outfile, _1410, 0, 1); + + if ((i + 1) % w == 0) { + for (pad = (w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) { /* ADD */ + c_vector_push_back(outfile, "0", 0, 1); + } + } + } + } + + return 0; +} diff --git a/src/bin/jp2/opj_decompress_c_vector.c b/src/bin/jp2/opj_decompress_c_vector.c new file mode 100644 index 000000000..ed5fd2c71 --- /dev/null +++ b/src/bin/jp2/opj_decompress_c_vector.c @@ -0,0 +1,1721 @@ +/* + * 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) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * 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) 2012, CS Systemes d'Information, France + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include "opj_apps_config.h" + +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include "windirent.h" +#else + +#include + +#endif /* _WIN32 */ + +#ifdef _WIN32 +#include +#define strcasecmp _stricmp +#define strncasecmp _strnicmp +#else + +#include +#include +#include +#include + +#endif /* _WIN32 */ + +#include "openjpeg.h" +#include "opj_getopt.h" +#include "convert.h" +#include "index.h" + +#ifdef OPJ_HAVE_LIBLCMS2 + +#include + +#endif +#ifdef OPJ_HAVE_LIBLCMS1 +#include +#endif + +#include "color.h" + +#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 +} 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 of 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; + /** number of threads */ + int num_threads; + /* Quiet */ + int quiet; + /** number of components to decode */ + OPJ_UINT32 numcomps; + /** indices of components to decode */ + OPJ_UINT32 *comps_indices; +} opj_decompress_parameters; + +/* -------------------------------------------------------------------------- */ +/* Declarations */ +int get_num_images(char *imgdirpath); + +int load_images(dircnt_t *dirptr, char *imgdirpath); + +int get_file_format(const char *filename); + +char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol, + opj_decompress_parameters *parameters); + +static int infile_format(const char *fname); + +int parse_cmdline_decoder(int argc, char **argv, + opj_decompress_parameters *parameters, img_fol_t *img_fol); + +int parse_DA_values(char *inArg, unsigned int *DA_x0, unsigned int *DA_y0, + unsigned int *DA_x1, unsigned int *DA_y1); + +/* -------------------------------------------------------------------------- */ +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 \n" + " Image file Directory path \n" + " -OutFor \n" + " REQUIRED only if -ImgDir is used\n" + " Output format for decompressed images.\n"); + fprintf(stdout, " -i \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 \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 \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 \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 \n" + " OPTIONAL\n" + " Decoding area\n" + " By default all the image is decoded.\n" + " -t \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 [C|S][,[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, " -c first_comp_index[,second_comp_index][,...]\n" + " OPTIONAL\n" + " To limit the number of components to decoded.\n" + " Component indices are numbered starting at 0.\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"); + if (opj_has_thread_support()) { + fprintf(stdout, " -threads \n" + " Number of threads to use for decoding or ALL_CPUS for all available cores.\n"); + } + fprintf(stdout, " -quiet\n" + " Disable output from the library and other output.\n"); + /* UniPG>> */ +#ifdef USE_JPWL + fprintf(stdout, " -W \n" + " Activates the JPWL correction capability, if the codestream complies.\n" + " Options can be a comma separated list of 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 */ + /* <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 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; +} + +#ifdef _WIN32 +const char* path_separator = "\\"; +#else +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; +} + +/* -------------------------------------------------------------------------- */ +#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a" +#define JP2_MAGIC "\x0d\x0a\x87\x0a" +/* position 45: "\xff\x52" */ +#define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51" + +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; + + reader = fopen(fname, "rb"); + + 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; + } + + + ext_format = get_file_format(fname); + + 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 (magic_format == ext_format) { + return ext_format; + } + + 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); + + return magic_format; +} + +/* -------------------------------------------------------------------------- */ +/** + * 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}, + {"threads", REQ_ARG, NULL, 'T'}, + {"quiet", NO_ARG, NULL, 1}, + }; + + const char optlist[] = "i:o:r:l:x:d:t:p:c:" + + /* UniPG>> */ + #ifdef USE_JPWL + "W:" + #endif /* USE_JPWL */ + /* <force_rgb); + long_option[3].flag = &(parameters->upsample); + long_option[4].flag = &(parameters->split_pnm); + long_option[6].flag = &(parameters->quiet); + 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); + if (img_fol->imgdirpath == NULL) { + return 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 'c': { /* Componenets */ + const char *iter = opj_optarg; + while (1) { + parameters->numcomps++; + parameters->comps_indices = (OPJ_UINT32 *) realloc( + parameters->comps_indices, + parameters->numcomps * sizeof(OPJ_UINT32)); + parameters->comps_indices[parameters->numcomps - 1] = + (OPJ_UINT32) atoi(iter); + iter = strchr(iter, ','); + if (iter == NULL) { + break; + } + iter++; + } + } + break; + /* ----------------------------------------------------- */ + + /* 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; + if (!(parameter->quiet)) { + fprintf(stdout, "JPWL correction capability activated\n"); + fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps); + } + } + break; +#endif /* USE_JPWL */ + /* <num_threads = opj_get_num_cpus(); + if (parameters->num_threads == 1) { + parameters->num_threads = 0; + } + } else { + sscanf(opj_optarg, "%d", ¶meters->num_threads); + } + } + break; + + /* ----------------------------------------------------- */ + + default: + fprintf(stderr, "[WARNING] An invalid option has been ignored.\n"); + break; + } + } while (c != -1); + + /* 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) { + fprintf(stderr, + "[ERROR] When -ImgDir is used, -OutFor 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))) { + fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n"); + 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]); + fprintf(stderr, " Help: %s -h\n", argv[0]); + return 1; + } + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/** + * Parse decoding area input values + * separator = "," + */ +/* -------------------------------------------------------------------------- */ +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; + } +} + +OPJ_FLOAT64 opj_clock(void) { +#ifdef _WIN32 + /* _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; */ + /* t is the high resolution performance counter (see MSDN) */ + QueryPerformanceCounter(& t) ; + return freq.QuadPart ? ((OPJ_FLOAT64)t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) : + 0; +#elif defined(__linux) + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return ((OPJ_FLOAT64)ts.tv_sec + (OPJ_FLOAT64)ts.tv_nsec * 1e-9); +#else + /* Unix : use resource usage */ + /* FIXME: this counts the total CPU time, instead of the user perceived time */ + 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 */ + 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); +#endif +} + +/* -------------------------------------------------------------------------- */ + +/** +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); +} + +/** +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); +} + +/** +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); +} + +/** +sample quiet callback expecting no client object +*/ +static void quiet_callback(const char *msg, void *client_data) { + (void) msg; + (void) client_data; +} + +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)); + } +} + +static void destroy_parameters(opj_decompress_parameters *parameters) { + if (parameters) { + if (parameters->precision) { + free(parameters->precision); + parameters->precision = NULL; + } + + free(parameters->comps_indices); + parameters->comps_indices = NULL; + } +} + +/* -------------------------------------------------------------------------- */ + +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, + sizeof(OPJ_INT32) * l_org_cmp->w * l_org_cmp->h); + } + } + opj_image_destroy(original); + return l_new_image; +} + +/* -------------------------------------------------------------------------- */ +/** + * OPJ_DECOMPRESS MAIN + */ +/* -------------------------------------------------------------------------- */ +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; + OPJ_UINT32 cp_reduce; + + /* 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) { + failed = 1; + goto fin; + } + + cp_reduce = parameters.core.cp_reduce; + if (getenv("USE_OPJ_SET_DECODED_RESOLUTION_FACTOR") != NULL) { + /* For debugging/testing purposes, do not set the cp_reduce member */ + /* if USE_OPJ_SET_DECODED_RESOLUTION_FACTOR is defined, but used */ + /* the opj_set_decoded_resolution_factor() API instead */ + parameters.core.cp_reduce = 0; + } + + + /* 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) { + destroy_parameters(¶meters); + return EXIT_FAILURE; + } + /* Stores at max 10 image file names */ + dirptr->filename_buf = (char *) malloc(sizeof(char) * + (size_t) num_images * OPJ_PATH_LEN); + if (!dirptr->filename_buf) { + failed = 1; + goto fin; + } + + dirptr->filename = (char **) malloc((size_t) num_images * sizeof(char *)); + + if (!dirptr->filename) { + failed = 1; + goto fin; + } + 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) { + failed = 1; + goto fin; + } + if (num_images == 0) { + fprintf(stderr, "Folder is empty\n"); + failed = 1; + goto fin; + } + } else { + num_images = 1; + } + + /*Decoding image one by one*/ + for (imageno = 0; imageno < num_images; imageno++) { + + if (!parameters.quiet) { + 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 */ + /* ---------------------------------------- */ + + FILE *file = fopen(parameters.infile, "rb"); + if (!file) { + fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", + parameters.infile); + failed = 1; + goto fin; + } + fseek(file, 0, SEEK_SET); + fseek(file, 0, SEEK_END); + size_t size = ftell(file); + fseek(file, 0, SEEK_SET); + unsigned char *buf = (unsigned char *) malloc(size * sizeof(unsigned char)); + if (!buf) { + fclose(file); + failed = 1; + goto fin; + } + size_t _size = fread(buf, 1, size, file); + fclose(file); + if (_size != size) { + free(buf); + failed = 1; + goto fin; + } + c_vector *v = NULL; + c_vector_init(&v); + if (v == NULL) { + free(buf); + failed = 1; + goto fin; + } + if (0 != c_vector_push_back(v, buf, 0, size)) { + free(buf); + failed = 1; + goto fin; + } + free(buf); + buf = NULL; + l_stream = opj_stream_create_default_c_vector(v, 1); + if (!l_stream) { + c_vector_free(&v); + fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", + parameters.infile); + failed = 1; + goto fin; + } + + /* 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; + } + + if (parameters.quiet) { + /* Set all callbacks to quiet */ + opj_set_info_handler(l_codec, quiet_callback, 00); + opj_set_warning_handler(l_codec, quiet_callback, 00); + opj_set_error_handler(l_codec, quiet_callback, 00); + } else { + /* 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"); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + c_vector_free(&v); + failed = 1; + goto fin; + } + + if (parameters.num_threads >= 1 && + !opj_codec_set_threads(l_codec, parameters.num_threads)) { + fprintf(stderr, "ERROR -> opj_decompress: failed to set number of threads\n"); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + c_vector_free(&v); + failed = 1; + goto fin; + } + + /* 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"); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(image); + c_vector_free(&v); + failed = 1; + goto fin; + } + + if (parameters.numcomps) { + if (!opj_set_decoded_components(l_codec, + parameters.numcomps, + parameters.comps_indices, + OPJ_FALSE)) { + fprintf(stderr, + "ERROR -> opj_decompress: failed to set the component indices!\n"); + opj_destroy_codec(l_codec); + opj_stream_destroy(l_stream); + opj_image_destroy(image); + c_vector_free(&v); + failed = 1; + goto fin; + } + } + + if (getenv("USE_OPJ_SET_DECODED_RESOLUTION_FACTOR") != NULL) { + /* For debugging/testing purposes, and also an illustration on how to */ + /* use the alternative API opj_set_decoded_resolution_factor() instead */ + /* of setting parameters.cp_reduce */ + if (!opj_set_decoded_resolution_factor(l_codec, cp_reduce)) { + 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); + c_vector_free(&v); + failed = 1; + goto fin; + } + } + + if (!parameters.nb_tile_to_decode) { + if (getenv("SKIP_OPJ_SET_DECODE_AREA") != NULL && + parameters.DA_x0 == 0 && + parameters.DA_y0 == 0 && + parameters.DA_x1 == 0 && + parameters.DA_y1 == 0) { + /* For debugging/testing purposes, */ + /* do nothing if SKIP_OPJ_SET_DECODE_AREA env variable */ + /* is defined and no decoded area has been set */ + } + /* Optional if you want decode the entire image */ + else 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"); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(image); + c_vector_free(&v); + failed = 1; + goto fin; + } + + /* 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"); + opj_destroy_codec(l_codec); + opj_stream_destroy(l_stream); + opj_image_destroy(image); + c_vector_free(&v); + failed = 1; + goto fin; + } + } else { + if (!(parameters.DA_x0 == 0 && + parameters.DA_y0 == 0 && + parameters.DA_x1 == 0 && + parameters.DA_y1 == 0)) { + if (!(parameters.quiet)) { + fprintf(stderr, "WARNING: -d option ignored when used together with -t\n"); + } + } + + if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) { + fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n"); + opj_destroy_codec(l_codec); + opj_stream_destroy(l_stream); + opj_image_destroy(image); + c_vector_free(&v); + failed = 1; + goto fin; + } + if (!(parameters.quiet)) { + 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); + } +#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"); + opj_destroy_codec(l_codec); + failed = 1; + c_vector_free(&v); + goto fin; + } + } + + /* Force RGB output */ + /* ---------------- */ + if (parameters.force_rgb) { + switch (image->color_space) { + case OPJ_CLRSPC_SRGB: + 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"); + opj_destroy_codec(l_codec); + failed = 1; + c_vector_free(&v); + goto fin; + } + } + c_vector *outfile = NULL; + c_vector_init(&outfile); + if (outfile == NULL) { + fprintf(stderr, "[ERROR] Outfile c_vector\n"); + opj_destroy_codec(l_codec); + failed = 1; + c_vector_free(&v); + goto fin; + } + + /* create output image */ + /* ------------------- */ + switch (parameters.cod_format) { + case BMP_DFMT: /* BMP */ + if (imagetobmp_c_vector(image, outfile)) { + fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile); + failed = 1; + } else if (!(parameters.quiet)) { + fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile); + } + break; + case RAW_DFMT: /* RAW */ + if (imagetoraw_c_vector(image, outfile,OPJ_TRUE)) { + fprintf(stderr, "[ERROR] Error generating raw file. Outfile %s not generated\n", + parameters.outfile); + failed = 1; + } else if (!(parameters.quiet)) { + fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile); + } + break; + + case RAWL_DFMT: /* RAWL */ + if (imagetoraw_c_vector(image, outfile,OPJ_FALSE)) { + fprintf(stderr, + "[ERROR] Error generating rawl file. Outfile %s not generated\n", + parameters.outfile); + failed = 1; + } else if (!(parameters.quiet)) { + fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile); + } + break; + /* 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; + } + if (!failed && c_vector_size(outfile) > 0) { + FILE *out = fopen(parameters.outfile, "wb"); + fwrite(c_vector_data(outfile), c_vector_size(outfile), 1, out); + fclose(out); + } + c_vector_free(&outfile); + /* free remaining structures */ + if (l_codec) { + opj_destroy_codec(l_codec); + } + + + /* free image data structure */ + opj_image_destroy(image); + + /* destroy the codestream index */ + opj_destroy_cstr_index(&cstr_index); + + if (failed) { + (void) remove(parameters.outfile); /* ignore return value */ + } + c_vector_free(&v); + } + fin: + destroy_parameters(¶meters); + if (failed && img_fol.imgdirpath) { + free(img_fol.imgdirpath); + } + if (dirptr) { + if (dirptr->filename) { + free(dirptr->filename); + } + if (dirptr->filename_buf) { + free(dirptr->filename_buf); + } + free(dirptr); + } + if (numDecompressedImages && !failed && !(parameters.quiet)) { + 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/lib/openjp2/CMakeLists.txt b/src/lib/openjp2/CMakeLists.txt index b27148582..d421fdeb9 100644 --- a/src/lib/openjp2/CMakeLists.txt +++ b/src/lib/openjp2/CMakeLists.txt @@ -32,6 +32,8 @@ set(OPENJPEG_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/mqc.c ${CMAKE_CURRENT_SOURCE_DIR}/mqc.h ${CMAKE_CURRENT_SOURCE_DIR}/mqc_inl.h + ${CMAKE_CURRENT_SOURCE_DIR}/c_vector.h + ${CMAKE_CURRENT_SOURCE_DIR}/c_vector.c ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg.c ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg.h ${CMAKE_CURRENT_SOURCE_DIR}/opj_clock.c diff --git a/src/lib/openjp2/c_vector.c b/src/lib/openjp2/c_vector.c new file mode 100644 index 000000000..f16f53bbd --- /dev/null +++ b/src/lib/openjp2/c_vector.c @@ -0,0 +1,228 @@ +// +// Created by caesar kekxv on 2020/4/17. +// + +#include "c_vector.h" + +typedef struct c_vector { + void *items; + size_t total; + size_t index; +} c_vector; + + +/** + * init + * @param cVector + */ +int c_vector_init(c_vector **cVector) { + if (!cVector)return -1; + if (NULL != (*cVector))return -2; + *cVector = (c_vector *) malloc(sizeof(c_vector)); + (*cVector)->index = 0; + (*cVector)->total = 0; + (*cVector)->items = NULL; + return 0; +} + +/** + * get c_vector size + * @param cVector + * @return + */ +size_t c_vector_size(c_vector *cVector) { + if (!cVector)return -1; + return cVector->total; +} + +/** + * resize c_vector + * @param cVector + */ +static int c_vector_resize(c_vector *cVector, size_t count) { + if (!cVector)return -1; + if (cVector->items) { + void *items = (void *) realloc(cVector->items, sizeof(void) * count); + if (items) { + cVector->items = items; + cVector->total = count; + return 0; + } + return -1; + } else { + cVector->items = (void *) malloc(sizeof(void) * count); + if (cVector->items == NULL) { + return -1; + } else { + cVector->total = count; + return 0; + } + } +} + +/** + * push back c_vector + * @param cVector + * @param data + * @param offset + * @param count + */ +int c_vector_push_back(c_vector *cVector, void *data, size_t offset, size_t count) { + size_t index = cVector->total; + size_t new_size = count + cVector->total; + if (c_vector_resize(cVector, new_size) < 0) { + // error realloc + return -1; + } + memcpy(&(cVector->items[index]), &data[offset], count); + return 0; +} + +/** + * push zero to c_vector + * @param cVector + * @return + */ +int c_vector_push_back_zero(c_vector *cVector) { + unsigned char data[1] = {0}; + return c_vector_push_back(cVector, data, 0, 1); +} + +/** + * set c_vector data + * @param cVector + * @param offset + * @param data + * @param count + * @return + */ +size_t c_vector_set(c_vector *cVector, size_t index, void *data, size_t offset, size_t count) { + if (cVector->total <= index || index + count >= cVector->total) { + return -1; + } + memcpy(&(cVector->items[index]), &data[offset], count); + return 0; +} + +/** + * insert data + * @param cVector + * @param index + * @param data + * @param offset + * @param count + * @return + */ +size_t c_vector_insert(c_vector *cVector, size_t index, void *data, size_t offset, size_t count) { + if (cVector->total <= index) { + return -1; + } + size_t last_total = cVector->total; + size_t new_size = count + cVector->total; + if (c_vector_resize(cVector, new_size) < 0) { + // error realloc + return -1; + } + size_t len = last_total - index; + for (size_t i = 1; i <= len; i++) { + memcpy(&cVector->items[new_size - i], &cVector->items[last_total - i], 1); + } + memcpy(&(cVector->items[index]), &data[offset], count); + return 0; +} + +/** + * get c_vector data + * @param cVector + * @param offset + * @return + */ +void *c_vector_get(c_vector *cVector, size_t offset) { + if (cVector->total <= offset) { + return NULL; + } + return &cVector->items[offset]; +} + +/** + * get c_vector data + * @param cVector + * @return + */ +void *c_vector_data(c_vector *cVector) { + return c_vector_get(cVector, 0); +} + +/** + * c_vector delete + * @param cVector + * @param offset + * @param count + */ +size_t c_vector_delete(c_vector *cVector, size_t offset, size_t count) { + if (cVector->total <= offset) { + return -1; + } + if (count + offset >= cVector->total) { + cVector->total = cVector->total - offset; + return 0; + } + memcpy(&(cVector->items[offset]), &(cVector->items[offset + count]), cVector->total - count - offset); + cVector->total = cVector->total - count; + return 0; +} + +/** + * free c_vector + * @param cVector + */ +void c_vector_free(c_vector **cVector) { + if (!cVector)return; + if (!*cVector)return; + if ((*cVector)->items)free((*cVector)->items); + (*cVector)->items = NULL; + free(*cVector); + *cVector = NULL; +} + +int c_vector_seekg(size_t offset, c_vector *cVector) { + return c_vector_seek(cVector, offset, SEEK_SET); +} + +int c_vector_skip(size_t offset, c_vector *cVector) { + return c_vector_seek(cVector, offset, SEEK_CUR); +} + +int c_vector_seek(c_vector *cVector, size_t offset, int whence) { + if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) { + return -1; + } + if (SEEK_SET == whence) { + cVector->index = offset; + } else if (SEEK_END == whence) { + cVector->index = cVector->total - 1 - offset; + } else if (SEEK_CUR == whence) { + cVector->index = cVector->total + offset; + } + return -1; +} + +size_t c_vector_read(void *p_buffer, size_t p_nb_bytes, c_vector *v) { + if (v->total - v->index < p_nb_bytes) { + p_nb_bytes = v->total - v->index; + } + memcpy(p_buffer, &(v->items[v->index]), p_nb_bytes); + v->index += p_nb_bytes; + return p_nb_bytes; +} + +size_t c_vector_write(void *p_buffer, size_t p_nb_bytes, + c_vector *v) { + if (v->index + p_nb_bytes > v->total) { + c_vector_resize(v, v->index + p_nb_bytes); + } + c_vector_set(v, v->index, p_buffer, 0, p_nb_bytes); + v->index += p_nb_bytes; + return p_nb_bytes; +} + diff --git a/src/lib/openjp2/c_vector.h b/src/lib/openjp2/c_vector.h new file mode 100644 index 000000000..2aab522c6 --- /dev/null +++ b/src/lib/openjp2/c_vector.h @@ -0,0 +1,110 @@ +// +// Created by caesar kekxv on 2020/4/17. +// + +#ifndef CLANGTOOLS_VECTOR_H +#define CLANGTOOLS_VECTOR_H + +#include +#include +#include + +struct c_vector; +typedef struct c_vector c_vector; + +/** + * init + * @param cVector + */ +int c_vector_init(c_vector **cVector); + +/** + * get c_vector size + * @param cVector + * @return + */ +size_t c_vector_size(c_vector *cVector); + +/** + * resize c_vector + * @param cVector + */ +static int c_vector_resize(c_vector *cVector, size_t count); + +/** + * push back c_vector + * @param cVector + * @param data + * @param offset + * @param count + */ +int c_vector_push_back(c_vector *cVector, void *data, size_t offset, size_t count); + +/** + * push zero to c_vector + * @param cVector + * @return + */ +int c_vector_push_back_zero(c_vector *cVector); + +/** + * set data + * @param cVector + * @param index + * @param data + * @param offset + * @param count + * @return + */ +size_t c_vector_set(c_vector *cVector, size_t index, void *data, size_t offset, size_t count); + +/** + * insert data + * @param cVector + * @param index + * @param data + * @param offset + * @param count + * @return + */ +size_t c_vector_insert(c_vector *cVector, size_t index, void *data, size_t offset, size_t count); + +/** + * get c_vector data + * @param cVector + * @param offset + * @return + */ +void *c_vector_get(c_vector *cVector, size_t offset); + +/** + * get c_vector data + * @param cVector + * @return + */ +void *c_vector_data(c_vector *cVector); + +/** + * c_vector delete + * @param cVector + * @param offset + * @param count + */ +size_t c_vector_delete(c_vector *cVector, size_t offset, size_t count); + +/** + * free c_vector + * @param cVector + */ +void c_vector_free(c_vector **cVector); + +int c_vector_seek(c_vector *cVector, size_t offset, int whence); +int c_vector_seekg(size_t offset,c_vector *cVector); +int c_vector_skip(size_t offset,c_vector *cVector); + +size_t c_vector_read(void *p_buffer, size_t p_nb_bytes, c_vector *v); + +size_t c_vector_write(void *p_buffer, size_t p_nb_bytes, c_vector *v); + + +#endif //CLANGTOOLS_VECTOR_H diff --git a/src/lib/openjp2/openjpeg.c b/src/lib/openjp2/openjpeg.c index e406cb499..8ca831d11 100644 --- a/src/lib/openjp2/openjpeg.c +++ b/src/lib/openjp2/openjpeg.c @@ -41,12 +41,11 @@ /* ---------------------------------------------------------------------- */ /* Functions to set the message handlers */ -OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec, - opj_msg_callback p_callback, - void * p_user_data) -{ - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - if (! l_codec) { +OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t *p_codec, + opj_msg_callback p_callback, + void *p_user_data) { + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + if (!l_codec) { return OPJ_FALSE; } @@ -56,12 +55,11 @@ OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec, return OPJ_TRUE; } -OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec, - opj_msg_callback p_callback, - void * p_user_data) -{ - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - if (! l_codec) { +OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t *p_codec, + opj_msg_callback p_callback, + void *p_user_data) { + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + if (!l_codec) { return OPJ_FALSE; } @@ -71,12 +69,11 @@ OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec, return OPJ_TRUE; } -OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, - opj_msg_callback p_callback, - void * p_user_data) -{ - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - if (! l_codec) { +OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t *p_codec, + opj_msg_callback p_callback, + void *p_user_data) { + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + if (!l_codec) { return OPJ_FALSE; } @@ -88,32 +85,28 @@ OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, /* ---------------------------------------------------------------------- */ -static OPJ_SIZE_T opj_read_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes, - FILE * p_file) -{ +static OPJ_SIZE_T opj_read_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, + FILE *p_file) { OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file); - return l_nb_read ? l_nb_read : (OPJ_SIZE_T) - 1; + return l_nb_read ? l_nb_read : (OPJ_SIZE_T) -1; } -static OPJ_UINT64 opj_get_data_length_from_file(FILE * p_file) -{ +static OPJ_UINT64 opj_get_data_length_from_file(FILE *p_file) { OPJ_OFF_T file_length = 0; OPJ_FSEEK(p_file, 0, SEEK_END); - file_length = (OPJ_OFF_T)OPJ_FTELL(p_file); + file_length = (OPJ_OFF_T) OPJ_FTELL(p_file); OPJ_FSEEK(p_file, 0, SEEK_SET); - return (OPJ_UINT64)file_length; + return (OPJ_UINT64) file_length; } -static OPJ_SIZE_T opj_write_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes, - FILE * p_file) -{ +static OPJ_SIZE_T opj_write_from_file(void *p_buffer, OPJ_SIZE_T p_nb_bytes, + FILE *p_file) { return fwrite(p_buffer, 1, p_nb_bytes, p_file); } -static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data) -{ +static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, FILE *p_user_data) { if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_CUR)) { return -1; } @@ -121,8 +114,7 @@ static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data) return p_nb_bytes; } -static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data) -{ +static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, FILE *p_user_data) { if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_SET)) { return OPJ_FALSE; } @@ -157,19 +149,17 @@ DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) /* ---------------------------------------------------------------------- */ -const char* OPJ_CALLCONV opj_version(void) -{ +const char *OPJ_CALLCONV opj_version(void) { return OPJ_PACKAGE_VERSION; } /* ---------------------------------------------------------------------- */ /* DECOMPRESSION FUNCTIONS*/ -opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) -{ +opj_codec_t *OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) { opj_codec_private_t *l_codec = 00; - l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t)); + l_codec = (opj_codec_private_t *) opj_calloc(1, sizeof(opj_codec_private_t)); if (!l_codec) { return 00; } @@ -177,193 +167,192 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) l_codec->is_decompressor = 1; switch (p_format) { - case OPJ_CODEC_J2K: - l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) j2k_dump; - - l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)( - void*)) j2k_get_cstr_info; - - l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)( - void*)) j2k_get_cstr_index; - - l_codec->m_codec_data.m_decompression.opj_decode = - (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - opj_image_t*, struct opj_event_mgr *)) opj_j2k_decode; - - l_codec->m_codec_data.m_decompression.opj_end_decompress = - (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_end_decompress; - - l_codec->m_codec_data.m_decompression.opj_read_header = - (OPJ_BOOL(*)(struct opj_stream_private *, - void *, - opj_image_t **, - struct opj_event_mgr *)) opj_j2k_read_header; - - l_codec->m_codec_data.m_decompression.opj_destroy = - (void (*)(void *))opj_j2k_destroy; - - l_codec->m_codec_data.m_decompression.opj_setup_decoder = - (void (*)(void *, opj_dparameters_t *)) opj_j2k_setup_decoder; - - l_codec->m_codec_data.m_decompression.opj_read_tile_header = - (OPJ_BOOL(*)(void *, - OPJ_UINT32*, - OPJ_UINT32*, - OPJ_INT32*, OPJ_INT32*, - OPJ_INT32*, OPJ_INT32*, - OPJ_UINT32*, - OPJ_BOOL*, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_read_tile_header; - - l_codec->m_codec_data.m_decompression.opj_decode_tile_data = - (OPJ_BOOL(*)(void *, - OPJ_UINT32, - OPJ_BYTE*, - OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_decode_tile; - - l_codec->m_codec_data.m_decompression.opj_set_decode_area = - (OPJ_BOOL(*)(void *, - opj_image_t*, - OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, - struct opj_event_mgr *)) opj_j2k_set_decode_area; - - l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = - (OPJ_BOOL(*)(void *p_codec, - opj_stream_private_t *p_cio, - opj_image_t *p_image, - struct opj_event_mgr * p_manager, - OPJ_UINT32 tile_index)) opj_j2k_get_tile; - - l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = - (OPJ_BOOL(*)(void * p_codec, - OPJ_UINT32 res_factor, - struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor; - - l_codec->m_codec_data.m_decompression.opj_set_decoded_components = - (OPJ_BOOL(*)(void * p_codec, - OPJ_UINT32 numcomps, - const OPJ_UINT32 * comps_indices, - struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_components; - - l_codec->opj_set_threads = - (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads; - - l_codec->m_codec = opj_j2k_create_decompress(); - - if (! l_codec->m_codec) { - opj_free(l_codec); - return NULL; - } + case OPJ_CODEC_J2K: + l_codec->opj_dump_codec = (void (*)(void *, OPJ_INT32, FILE *)) j2k_dump; + + l_codec->opj_get_codec_info = (opj_codestream_info_v2_t *(*)( + void *)) j2k_get_cstr_info; + + l_codec->opj_get_codec_index = (opj_codestream_index_t *(*)( + void *)) j2k_get_cstr_index; + + l_codec->m_codec_data.m_decompression.opj_decode = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + opj_image_t *, struct opj_event_mgr *)) opj_j2k_decode; + + l_codec->m_codec_data.m_decompression.opj_end_decompress = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_end_decompress; + + l_codec->m_codec_data.m_decompression.opj_read_header = + (OPJ_BOOL(*)(struct opj_stream_private *, + void *, + opj_image_t **, + struct opj_event_mgr *)) opj_j2k_read_header; + + l_codec->m_codec_data.m_decompression.opj_destroy = + (void (*)(void *)) opj_j2k_destroy; + + l_codec->m_codec_data.m_decompression.opj_setup_decoder = + (void (*)(void *, opj_dparameters_t *)) opj_j2k_setup_decoder; + + l_codec->m_codec_data.m_decompression.opj_read_tile_header = + (OPJ_BOOL(*)(void *, + OPJ_UINT32 *, + OPJ_UINT32 *, + OPJ_INT32 *, OPJ_INT32 *, + OPJ_INT32 *, OPJ_INT32 *, + OPJ_UINT32 *, + OPJ_BOOL *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_read_tile_header; + + l_codec->m_codec_data.m_decompression.opj_decode_tile_data = + (OPJ_BOOL(*)(void *, + OPJ_UINT32, + OPJ_BYTE *, + OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_decode_tile; + + l_codec->m_codec_data.m_decompression.opj_set_decode_area = + (OPJ_BOOL(*)(void *, + opj_image_t *, + OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, + struct opj_event_mgr *)) opj_j2k_set_decode_area; + + l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = + (OPJ_BOOL(*)(void *p_codec, + opj_stream_private_t *p_cio, + opj_image_t *p_image, + struct opj_event_mgr *p_manager, + OPJ_UINT32 tile_index)) opj_j2k_get_tile; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = + (OPJ_BOOL(*)(void *p_codec, + OPJ_UINT32 res_factor, + struct opj_event_mgr *p_manager)) opj_j2k_set_decoded_resolution_factor; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_components = + (OPJ_BOOL(*)(void *p_codec, + OPJ_UINT32 numcomps, + const OPJ_UINT32 *comps_indices, + struct opj_event_mgr *p_manager)) opj_j2k_set_decoded_components; + + l_codec->opj_set_threads = + (OPJ_BOOL(*)(void *p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads; + + l_codec->m_codec = opj_j2k_create_decompress(); + + if (!l_codec->m_codec) { + opj_free(l_codec); + return NULL; + } - break; + break; + + case OPJ_CODEC_JP2: + /* get a JP2 decoder handle */ + l_codec->opj_dump_codec = (void (*)(void *, OPJ_INT32, FILE *)) jp2_dump; + + l_codec->opj_get_codec_info = (opj_codestream_info_v2_t *(*)( + void *)) jp2_get_cstr_info; + + l_codec->opj_get_codec_index = (opj_codestream_index_t *(*)( + void *)) jp2_get_cstr_index; + + l_codec->m_codec_data.m_decompression.opj_decode = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + opj_image_t *, + struct opj_event_mgr *)) opj_jp2_decode; + + l_codec->m_codec_data.m_decompression.opj_end_decompress = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_end_decompress; + + l_codec->m_codec_data.m_decompression.opj_read_header = + (OPJ_BOOL(*)(struct opj_stream_private *, + void *, + opj_image_t **, + struct opj_event_mgr *)) opj_jp2_read_header; + + l_codec->m_codec_data.m_decompression.opj_read_tile_header = + (OPJ_BOOL(*)(void *, + OPJ_UINT32 *, + OPJ_UINT32 *, + OPJ_INT32 *, + OPJ_INT32 *, + OPJ_INT32 *, + OPJ_INT32 *, + OPJ_UINT32 *, + OPJ_BOOL *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_read_tile_header; + + l_codec->m_codec_data.m_decompression.opj_decode_tile_data = + (OPJ_BOOL(*)(void *, + OPJ_UINT32, OPJ_BYTE *, OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_decode_tile; + + l_codec->m_codec_data.m_decompression.opj_destroy = (void (*)( + void *)) opj_jp2_destroy; + + l_codec->m_codec_data.m_decompression.opj_setup_decoder = + (void (*)(void *, opj_dparameters_t *)) opj_jp2_setup_decoder; + + l_codec->m_codec_data.m_decompression.opj_set_decode_area = + (OPJ_BOOL(*)(void *, + opj_image_t *, + OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, + struct opj_event_mgr *)) opj_jp2_set_decode_area; + + l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = + (OPJ_BOOL(*)(void *p_codec, + opj_stream_private_t *p_cio, + opj_image_t *p_image, + struct opj_event_mgr *p_manager, + OPJ_UINT32 tile_index)) opj_jp2_get_tile; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = + (OPJ_BOOL(*)(void *p_codec, + OPJ_UINT32 res_factor, + opj_event_mgr_t *p_manager)) opj_jp2_set_decoded_resolution_factor; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_components = + (OPJ_BOOL(*)(void *p_codec, + OPJ_UINT32 numcomps, + const OPJ_UINT32 *comps_indices, + struct opj_event_mgr *p_manager)) opj_jp2_set_decoded_components; + + l_codec->opj_set_threads = + (OPJ_BOOL(*)(void *p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads; + + l_codec->m_codec = opj_jp2_create(OPJ_TRUE); + + if (!l_codec->m_codec) { + opj_free(l_codec); + return 00; + } - case OPJ_CODEC_JP2: - /* get a JP2 decoder handle */ - l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) jp2_dump; - - l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)( - void*)) jp2_get_cstr_info; - - l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)( - void*)) jp2_get_cstr_index; - - l_codec->m_codec_data.m_decompression.opj_decode = - (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - opj_image_t*, - struct opj_event_mgr *)) opj_jp2_decode; - - l_codec->m_codec_data.m_decompression.opj_end_decompress = - (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_end_decompress; - - l_codec->m_codec_data.m_decompression.opj_read_header = - (OPJ_BOOL(*)(struct opj_stream_private *, - void *, - opj_image_t **, - struct opj_event_mgr *)) opj_jp2_read_header; - - l_codec->m_codec_data.m_decompression.opj_read_tile_header = - (OPJ_BOOL(*)(void *, - OPJ_UINT32*, - OPJ_UINT32*, - OPJ_INT32*, - OPJ_INT32*, - OPJ_INT32 *, - OPJ_INT32 *, - OPJ_UINT32 *, - OPJ_BOOL *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_read_tile_header; - - l_codec->m_codec_data.m_decompression.opj_decode_tile_data = - (OPJ_BOOL(*)(void *, - OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_decode_tile; - - l_codec->m_codec_data.m_decompression.opj_destroy = (void (*)( - void *))opj_jp2_destroy; - - l_codec->m_codec_data.m_decompression.opj_setup_decoder = - (void (*)(void *, opj_dparameters_t *)) opj_jp2_setup_decoder; - - l_codec->m_codec_data.m_decompression.opj_set_decode_area = - (OPJ_BOOL(*)(void *, - opj_image_t*, - OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, - struct opj_event_mgr *)) opj_jp2_set_decode_area; - - l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = - (OPJ_BOOL(*)(void *p_codec, - opj_stream_private_t *p_cio, - opj_image_t *p_image, - struct opj_event_mgr * p_manager, - OPJ_UINT32 tile_index)) opj_jp2_get_tile; - - l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = - (OPJ_BOOL(*)(void * p_codec, - OPJ_UINT32 res_factor, - opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor; - - l_codec->m_codec_data.m_decompression.opj_set_decoded_components = - (OPJ_BOOL(*)(void * p_codec, - OPJ_UINT32 numcomps, - const OPJ_UINT32 * comps_indices, - struct opj_event_mgr * p_manager)) opj_jp2_set_decoded_components; - - l_codec->opj_set_threads = - (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads; - - l_codec->m_codec = opj_jp2_create(OPJ_TRUE); - - if (! l_codec->m_codec) { + break; + case OPJ_CODEC_UNKNOWN: + case OPJ_CODEC_JPT: + default: opj_free(l_codec); return 00; - } - - break; - case OPJ_CODEC_UNKNOWN: - case OPJ_CODEC_JPT: - default: - opj_free(l_codec); - return 00; } opj_set_default_event_handler(&(l_codec->m_event_mgr)); - return (opj_codec_t*) l_codec; + return (opj_codec_t *) l_codec; } void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t - *parameters) -{ + *parameters) { if (parameters) { memset(parameters, 0, sizeof(opj_dparameters_t)); /* default decoding parameters */ @@ -385,31 +374,29 @@ void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t OPJ_BOOL OPJ_CALLCONV opj_codec_set_threads(opj_codec_t *p_codec, - int num_threads) -{ + int num_threads) { if (p_codec && (num_threads >= 0)) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; - return l_codec->opj_set_threads(l_codec->m_codec, (OPJ_UINT32)num_threads); + return l_codec->opj_set_threads(l_codec->m_codec, (OPJ_UINT32) num_threads); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec, opj_dparameters_t *parameters - ) -{ +) { if (p_codec && parameters) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n"); return OPJ_FALSE; } l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec, - parameters); + parameters); return OPJ_TRUE; } return OPJ_FALSE; @@ -417,22 +404,21 @@ OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec, OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream, opj_codec_t *p_codec, - opj_image_t **p_image) -{ + opj_image_t **p_image) { if (p_codec && p_stream) { - opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; - opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_read_header function is not a decompressor handler.\n"); return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_read_header(l_stream, - l_codec->m_codec, - p_image, - &(l_codec->m_event_mgr)); + l_codec->m_codec, + p_image, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; @@ -440,14 +426,13 @@ OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream, OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec, - OPJ_UINT32 numcomps, - const OPJ_UINT32* comps_indices, - OPJ_BOOL apply_color_transforms) -{ + OPJ_UINT32 numcomps, + const OPJ_UINT32 *comps_indices, + OPJ_BOOL apply_color_transforms) { if (p_codec) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_set_decoded_components function is not a decompressor handler.\n"); return OPJ_FALSE; @@ -459,163 +444,156 @@ OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec, return OPJ_FALSE; } - return l_codec->m_codec_data.m_decompression.opj_set_decoded_components( - l_codec->m_codec, - numcomps, - comps_indices, - &(l_codec->m_event_mgr)); + return l_codec->m_codec_data.m_decompression.opj_set_decoded_components( + l_codec->m_codec, + numcomps, + comps_indices, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_decode(opj_codec_t *p_codec, opj_stream_t *p_stream, - opj_image_t* p_image) -{ + opj_image_t *p_image) { if (p_codec && p_stream) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec, - l_stream, - p_image, - &(l_codec->m_event_mgr)); + l_stream, + p_image, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(opj_codec_t *p_codec, - opj_image_t* p_image, - OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, - OPJ_INT32 p_end_x, OPJ_INT32 p_end_y - ) -{ + opj_image_t *p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y +) { if (p_codec) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return OPJ_FALSE; } - return l_codec->m_codec_data.m_decompression.opj_set_decode_area( - l_codec->m_codec, - p_image, - p_start_x, p_start_y, - p_end_x, p_end_y, - &(l_codec->m_event_mgr)); + return l_codec->m_codec_data.m_decompression.opj_set_decode_area( + l_codec->m_codec, + p_image, + p_start_x, p_start_y, + p_end_x, p_end_y, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(opj_codec_t *p_codec, - opj_stream_t * p_stream, - OPJ_UINT32 * p_tile_index, - OPJ_UINT32 * p_data_size, - OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, - OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, - OPJ_UINT32 * p_nb_comps, - OPJ_BOOL * p_should_go_on) -{ + opj_stream_t *p_stream, + OPJ_UINT32 *p_tile_index, + OPJ_UINT32 *p_data_size, + OPJ_INT32 *p_tile_x0, OPJ_INT32 *p_tile_y0, + OPJ_INT32 *p_tile_x1, OPJ_INT32 *p_tile_y1, + OPJ_UINT32 *p_nb_comps, + OPJ_BOOL *p_should_go_on) { if (p_codec && p_stream && p_data_size && p_tile_index) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_read_tile_header( - l_codec->m_codec, - p_tile_index, - p_data_size, - p_tile_x0, p_tile_y0, - p_tile_x1, p_tile_y1, - p_nb_comps, - p_should_go_on, - l_stream, - &(l_codec->m_event_mgr)); + l_codec->m_codec, + p_tile_index, + p_data_size, + p_tile_x0, p_tile_y0, + p_tile_x1, p_tile_y1, + p_nb_comps, + p_should_go_on, + l_stream, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(opj_codec_t *p_codec, - OPJ_UINT32 p_tile_index, - OPJ_BYTE * p_data, - OPJ_UINT32 p_data_size, - opj_stream_t *p_stream - ) -{ + OPJ_UINT32 p_tile_index, + OPJ_BYTE *p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream +) { if (p_codec && p_data && p_stream) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_decode_tile_data( - l_codec->m_codec, - p_tile_index, - p_data, - p_data_size, - l_stream, - &(l_codec->m_event_mgr)); + l_codec->m_codec, + p_tile_index, + p_data, + p_data_size, + l_stream, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(opj_codec_t *p_codec, - opj_stream_t *p_stream, - opj_image_t *p_image, - OPJ_UINT32 tile_index) -{ + opj_stream_t *p_stream, + opj_image_t *p_image, + OPJ_UINT32 tile_index) { if (p_codec && p_stream) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( - l_codec->m_codec, - l_stream, - p_image, - &(l_codec->m_event_mgr), - tile_index); + l_codec->m_codec, + l_stream, + p_image, + &(l_codec->m_event_mgr), + tile_index); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, - OPJ_UINT32 res_factor) -{ - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + OPJ_UINT32 res_factor) { + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; if (!l_codec) { return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor( - l_codec->m_codec, - res_factor, - &(l_codec->m_event_mgr)); + l_codec->m_codec, + res_factor, + &(l_codec->m_event_mgr)); } /* ---------------------------------------------------------------------- */ /* COMPRESSION FUNCTIONS*/ -opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) -{ +opj_codec_t *OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) { opj_codec_private_t *l_codec = 00; - l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t)); + l_codec = (opj_codec_private_t *) opj_calloc(1, sizeof(opj_codec_private_t)); if (!l_codec) { return 00; } @@ -623,95 +601,94 @@ opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) l_codec->is_decompressor = 0; switch (p_format) { - case OPJ_CODEC_J2K: - l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_encode; - - l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_end_compress; - - l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_image *, - struct opj_event_mgr *)) opj_j2k_start_compress; - - l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *, - OPJ_UINT32, - OPJ_BYTE*, - OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_write_tile; - - l_codec->m_codec_data.m_compression.opj_destroy = (void (*)( + case OPJ_CODEC_J2K: + l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_encode; + + l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_end_compress; + + l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_image *, + struct opj_event_mgr *)) opj_j2k_start_compress; + + l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *, + OPJ_UINT32, + OPJ_BYTE *, + OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_write_tile; + + l_codec->m_codec_data.m_compression.opj_destroy = (void (*)( void *)) opj_j2k_destroy; - l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *, - opj_cparameters_t *, - struct opj_image *, - struct opj_event_mgr *)) opj_j2k_setup_encoder; + l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *, + opj_cparameters_t *, + struct opj_image *, + struct opj_event_mgr *)) opj_j2k_setup_encoder; - l_codec->m_codec = opj_j2k_create_compress(); - if (! l_codec->m_codec) { - opj_free(l_codec); - return 00; - } + l_codec->m_codec = opj_j2k_create_compress(); + if (!l_codec->m_codec) { + opj_free(l_codec); + return 00; + } - break; + break; + + case OPJ_CODEC_JP2: + /* get a JP2 decoder handle */ + l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_encode; + + l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_end_compress; - case OPJ_CODEC_JP2: - /* get a JP2 decoder handle */ - l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_encode; - - l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_end_compress; - - l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *, - struct opj_stream_private *, - struct opj_image *, - struct opj_event_mgr *)) opj_jp2_start_compress; - - l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *, - OPJ_UINT32, - OPJ_BYTE*, - OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_write_tile; - - l_codec->m_codec_data.m_compression.opj_destroy = (void (*)( + l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_image *, + struct opj_event_mgr *)) opj_jp2_start_compress; + + l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *, + OPJ_UINT32, + OPJ_BYTE *, + OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_write_tile; + + l_codec->m_codec_data.m_compression.opj_destroy = (void (*)( void *)) opj_jp2_destroy; - l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *, - opj_cparameters_t *, - struct opj_image *, - struct opj_event_mgr *)) opj_jp2_setup_encoder; + l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *, + opj_cparameters_t *, + struct opj_image *, + struct opj_event_mgr *)) opj_jp2_setup_encoder; - l_codec->m_codec = opj_jp2_create(OPJ_FALSE); - if (! l_codec->m_codec) { - opj_free(l_codec); - return 00; - } + l_codec->m_codec = opj_jp2_create(OPJ_FALSE); + if (!l_codec->m_codec) { + opj_free(l_codec); + return 00; + } - break; + break; - case OPJ_CODEC_UNKNOWN: - case OPJ_CODEC_JPT: - default: - opj_free(l_codec); - return 00; + case OPJ_CODEC_UNKNOWN: + case OPJ_CODEC_JPT: + default: + opj_free(l_codec); + return 00; } opj_set_default_event_handler(&(l_codec->m_event_mgr)); - return (opj_codec_t*) l_codec; + return (opj_codec_t *) l_codec; } void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t - *parameters) -{ + *parameters) { if (parameters) { memset(parameters, 0, sizeof(opj_cparameters_t)); /* default coding parameters */ @@ -772,16 +749,15 @@ void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, opj_cparameters_t *parameters, - opj_image_t *p_image) -{ + opj_image_t *p_image) { if (p_codec && parameters && p_image) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec, - parameters, - p_image, - &(l_codec->m_event_mgr)); + parameters, + p_image, + &(l_codec->m_event_mgr)); } } @@ -789,34 +765,32 @@ OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, } OPJ_BOOL OPJ_CALLCONV opj_start_compress(opj_codec_t *p_codec, - opj_image_t * p_image, - opj_stream_t *p_stream) -{ + opj_image_t *p_image, + opj_stream_t *p_stream) { if (p_codec && p_stream) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec, - l_stream, - p_image, - &(l_codec->m_event_mgr)); + l_stream, + p_image, + &(l_codec->m_event_mgr)); } } return OPJ_FALSE; } -OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream) -{ +OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream) { if (p_info && p_stream) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_info; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec, - l_stream, - &(l_codec->m_event_mgr)); + l_stream, + &(l_codec->m_event_mgr)); } } @@ -825,16 +799,15 @@ OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream) } OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec, - opj_stream_t *p_stream) -{ + opj_stream_t *p_stream) { if (p_codec && p_stream) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec, - l_stream, - &(l_codec->m_event_mgr)); + l_stream, + &(l_codec->m_event_mgr)); } } return OPJ_FALSE; @@ -842,31 +815,29 @@ OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec, } OPJ_BOOL OPJ_CALLCONV opj_end_decompress(opj_codec_t *p_codec, - opj_stream_t *p_stream) -{ + opj_stream_t *p_stream) { if (p_codec && p_stream) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; - if (! l_codec->is_decompressor) { + if (!l_codec->is_decompressor) { return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_end_decompress( - l_codec->m_codec, - l_stream, - &(l_codec->m_event_mgr)); + l_codec->m_codec, + l_stream, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; } OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters, - OPJ_FLOAT32 * pEncodingMatrix, - OPJ_INT32 * p_dc_shift, OPJ_UINT32 pNbComp) -{ - OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32); - OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32); + OPJ_FLOAT32 *pEncodingMatrix, + OPJ_INT32 *p_dc_shift, OPJ_UINT32 pNbComp) { + OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32) sizeof(OPJ_FLOAT32); + OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32) sizeof(OPJ_INT32); OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size; /* add MCT capability */ @@ -880,12 +851,12 @@ OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters, /* use array based MCT */ parameters->tcp_mct = 2; parameters->mct_data = opj_malloc(l_mct_total_size); - if (! parameters->mct_data) { + if (!parameters->mct_data) { return OPJ_FALSE; } memcpy(parameters->mct_data, pEncodingMatrix, l_matrix_size); - memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size, p_dc_shift, + memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size, p_dc_shift, l_dc_shift_size); return OPJ_TRUE; @@ -893,24 +864,23 @@ OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters, OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec, OPJ_UINT32 p_tile_index, - OPJ_BYTE * p_data, + OPJ_BYTE *p_data, OPJ_UINT32 p_data_size, - opj_stream_t *p_stream) -{ + opj_stream_t *p_stream) { if (p_codec && p_stream && p_data) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; - opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t *l_stream = (opj_stream_private_t *) p_stream; if (l_codec->is_decompressor) { return OPJ_FALSE; } return l_codec->m_codec_data.m_compression.opj_write_tile(l_codec->m_codec, - p_tile_index, - p_data, - p_data_size, - l_stream, - &(l_codec->m_event_mgr)); + p_tile_index, + p_data, + p_data_size, + l_stream, + &(l_codec->m_event_mgr)); } return OPJ_FALSE; @@ -918,10 +888,9 @@ OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec, /* ---------------------------------------------------------------------- */ -void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec) -{ +void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec) { if (p_codec) { - opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; if (l_codec->is_decompressor) { l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec); @@ -938,10 +907,9 @@ void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec) void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec, OPJ_INT32 info_flag, - FILE* output_stream) -{ + FILE *output_stream) { if (p_codec) { - opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream); return; @@ -952,10 +920,9 @@ void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec, return; } -opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec) -{ +opj_codestream_info_v2_t *OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec) { if (p_codec) { - opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; return l_codec->opj_get_codec_info(l_codec->m_codec); } @@ -963,8 +930,7 @@ opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec) return NULL; } -void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) -{ +void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) { if (cstr_info) { if ((*cstr_info)->m_default_tile_info.tccp_info) { @@ -980,10 +946,9 @@ void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) } } -opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec) -{ +opj_codestream_index_t *OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec) { if (p_codec) { - opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; + opj_codec_private_t *l_codec = (opj_codec_private_t *) p_codec; return l_codec->opj_get_codec_index(l_codec->m_codec); } @@ -991,31 +956,28 @@ opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec) return NULL; } -void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index) -{ +void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index) { if (*p_cstr_index) { j2k_destroy_cstr_index(*p_cstr_index); (*p_cstr_index) = NULL; } } -opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream( - const char *fname, OPJ_BOOL p_is_read_stream) -{ +opj_stream_t *OPJ_CALLCONV opj_stream_create_default_file_stream( + const char *fname, OPJ_BOOL p_is_read_stream) { return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream); } -opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream( - const char *fname, - OPJ_SIZE_T p_size, - OPJ_BOOL p_is_read_stream) -{ - opj_stream_t* l_stream = 00; +opj_stream_t *OPJ_CALLCONV opj_stream_create_file_stream( + const char *fname, + OPJ_SIZE_T p_size, + OPJ_BOOL p_is_read_stream) { + opj_stream_t *l_stream = 00; FILE *p_file; const char *mode; - if (! fname) { + if (!fname) { return NULL; } @@ -1027,12 +989,12 @@ opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream( p_file = fopen(fname, mode); - if (! p_file) { + if (!p_file) { return NULL; } l_stream = opj_stream_create(p_size, p_is_read_stream); - if (! l_stream) { + if (!l_stream) { fclose(p_file); return NULL; } @@ -1051,15 +1013,49 @@ opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream( } -void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size) -{ - void* ret = opj_aligned_malloc(size); +opj_stream_t *OPJ_CALLCONV opj_stream_create_default_c_vector( + c_vector *fname, OPJ_BOOL p_is_read_stream) { + return opj_stream_create_c_vector(fname, OPJ_J2K_STREAM_CHUNK_SIZE, + p_is_read_stream); +} + +/** Create a stream from a file identified with c_vector + * @param v c_vector + * @param p_buffer_size size of the chunk used to stream + * @param p_is_read_stream whether the stream is a read stream (true) or not (false) +*/ +OPJ_API opj_stream_t *OPJ_CALLCONV opj_stream_create_c_vector( + c_vector *v, + OPJ_SIZE_T p_size, + OPJ_BOOL p_is_read_stream) { + opj_stream_t *l_stream = 00; + if (!v) { + return NULL; + } + + l_stream = opj_stream_create(p_size, p_is_read_stream); + if (!l_stream) { + return NULL; + } + + opj_stream_set_user_data(l_stream, v, NULL); + opj_stream_set_user_data_length(l_stream, c_vector_size(v)); + opj_stream_set_read_function(l_stream, (opj_stream_read_fn) c_vector_read); + opj_stream_set_write_function(l_stream, (opj_stream_write_fn) c_vector_write); + opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) c_vector_seekg); + opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) c_vector_skip); + + return l_stream; +} + + +void *OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size) { + void *ret = opj_aligned_malloc(size); /* printf("opj_image_data_alloc %p\n", ret); */ return ret; } -void OPJ_CALLCONV opj_image_data_free(void* ptr) -{ +void OPJ_CALLCONV opj_image_data_free(void *ptr) { /* printf("opj_image_data_free %p\n", ptr); */ opj_aligned_free(ptr); } diff --git a/src/lib/openjp2/openjpeg.h b/src/lib/openjp2/openjpeg.h index 50ed02d87..11b0e7316 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -130,6 +130,7 @@ typedef uint64_t OPJ_UINT64; typedef int64_t OPJ_OFF_T; /* 64-bit file offset type */ #include +#include "c_vector.h" typedef size_t OPJ_SIZE_T; /* Avoid compile-time warning because parameter is not used */ @@ -1252,6 +1253,24 @@ OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream( OPJ_SIZE_T p_buffer_size, OPJ_BOOL p_is_read_stream); +/** + * + * @param fname + * @param p_is_read_stream + * @return + */ +opj_stream_t *OPJ_CALLCONV opj_stream_create_default_c_vector( + c_vector *fname, OPJ_BOOL p_is_read_stream); +/** Create a stream from a file identified with c_vector + * @param v c_vector + * @param p_buffer_size size of the chunk used to stream + * @param p_is_read_stream whether the stream is a read stream (true) or not (false) +*/ +OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_c_vector( + c_vector *v, + OPJ_SIZE_T p_buffer_size, + OPJ_BOOL p_is_read_stream); + /* ========================================================== event manager functions definitions From a0ce21b06c7097b02dd2d6e117c05d85ed8aa02a Mon Sep 17 00:00:00 2001 From: caesar Date: Sun, 19 Apr 2020 15:41:41 +0800 Subject: [PATCH 2/4] fix gcc build --- src/bin/jp2/convertbmp.c | 287 +++++++++++++++----------- src/bin/jp2/opj_decompress_c_vector.c | 30 ++- src/lib/openjp2/c_vector.c | 54 ++--- src/lib/openjp2/c_vector.h | 32 +-- 4 files changed, 239 insertions(+), 164 deletions(-) diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index 1fee51843..cefb3dd09 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -1151,6 +1151,7 @@ int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile) { int w, h; int i, pad; int adjustR, adjustG, adjustB; + OPJ_UINT8 _tmp[100]; if (image->comps[0].prec < 8) { fprintf(stderr, "imagetobmp: Unsupported precision: %d\n", @@ -1182,58 +1183,83 @@ int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile) { /* FILE HEADER */ /* ------------- */ - OPJ_UINT8 _1185[4] = { - (OPJ_UINT8) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff, - (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff, - (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff, - (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff}; - c_vector_push_back(outfile, _1185, 0, 4); - OPJ_UINT8 _1191[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, - ((0) >> 24) & 0xff}; - c_vector_push_back(outfile, _1191, 0, 4); - - OPJ_UINT8 _1195[4] = {(54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff, - ((54) >> 24) & 0xff}; - c_vector_push_back(outfile, _1195, 0, 4); + _tmp[0] = (OPJ_UINT8) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff; + _tmp[1] = (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (0) & 0xff; + _tmp[1] = ((0) >> 8) & 0xff; + _tmp[2] = ((0) >> 16) & 0xff; + _tmp[3] = ((0) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (54) & 0xff; + _tmp[1] = ((54) >> 8) & 0xff; + _tmp[2] = ((54) >> 16) & 0xff; + _tmp[3] = ((54) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); /* INFO HEADER */ /* ------------- */ - OPJ_UINT8 _1201[4] = {(40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, - ((40) >> 24) & 0xff}; - c_vector_push_back(outfile, _1201, 0, 4); - - OPJ_UINT8 _1205[4] = {(OPJ_UINT8) ((w) & 0xff), - (OPJ_UINT8) ((w) >> 8) & 0xff, - (OPJ_UINT8) ((w) >> 16) & 0xff, - (OPJ_UINT8) ((w) >> 24) & 0xff}; - c_vector_push_back(outfile, _1205, 0, 4); - OPJ_UINT8 _1210[4] = {(OPJ_UINT8) ((h) & 0xff), - (OPJ_UINT8) ((h) >> 8) & 0xff, - (OPJ_UINT8) ((h) >> 16) & 0xff, - (OPJ_UINT8) ((h) >> 24) & 0xff}; - c_vector_push_back(outfile, _1210, 0, 4); - OPJ_UINT8 _1215[4] = {(1) & 0xff, ((1) >> 8) & 0xff, (24) & 0xff, ((24) >> 8) & 0xff}; - c_vector_push_back(outfile, _1215, 0, 4); - OPJ_UINT8 _1218[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, - ((0) >> 24) & 0xff}; - c_vector_push_back(outfile, _1218, 0, 4); - OPJ_UINT8 _1220[4] = {(OPJ_UINT8) (3 * h * w + 3 * h * (w % 2)) & 0xff, - (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff, - (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff, - (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff}; - c_vector_push_back(outfile, _1220, 0, 4); - OPJ_UINT8 _1225[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, - ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; - c_vector_push_back(outfile, _1225, 0, 4); - OPJ_UINT8 _1228[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, - ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; - c_vector_push_back(outfile, _1228, 0, 4); - OPJ_UINT8 _1231[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, - ((0) >> 24) & 0xff}; - c_vector_push_back(outfile, _1231, 0, 4); - OPJ_UINT8 _1234[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, - ((0) >> 24) & 0xff}; - c_vector_push_back(outfile, _1234, 0, 4); + _tmp[0] = (40) & 0xff; + _tmp[1] = ((40) >> 8) & 0xff; + _tmp[2] = ((40) >> 16) & 0xff; + _tmp[3] = ((40) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (OPJ_UINT8) ((w) & 0xff); + _tmp[1] = (OPJ_UINT8) ((w) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((w) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((w) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (OPJ_UINT8) ((h) & 0xff); + _tmp[1] = (OPJ_UINT8) ((h) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((h) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((h) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (1) & 0xff; + _tmp[1] = ((1) >> 8) & 0xff; + _tmp[2] = (24) & 0xff; + _tmp[3] = ((24) >> 8) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (0) & 0xff; + _tmp[1] = ((0) >> 8) & 0xff; + _tmp[2] = ((0) >> 16) & 0xff; + _tmp[3] = ((0) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (OPJ_UINT8) (3 * h * w + 3 * h * (w % 2)) & 0xff; + _tmp[1] = (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (7834) & 0xff; + _tmp[1] = ((7834) >> 8) & 0xff; + _tmp[2] = ((7834) >> 16) & 0xff; + _tmp[3] = ((7834) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (7834) & 0xff; + _tmp[1] = ((7834) >> 8) & 0xff; + _tmp[2] = ((7834) >> 16) & 0xff; + _tmp[3] = ((7834) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (0) & 0xff; + _tmp[1] = ((0) >> 8) & 0xff; + _tmp[2] = ((0) >> 16) & 0xff; + _tmp[3] = ((0) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (0) & 0xff; + _tmp[1] = ((0) >> 8) & 0xff; + _tmp[2] = ((0) >> 16) & 0xff; + _tmp[3] = ((0) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); if (image->comps[0].prec > 8) { adjustR = (int) image->comps[0].prec - 8; @@ -1297,8 +1323,10 @@ int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile) { } bc = (OPJ_UINT8) b; - OPJ_UINT8 _1300[3] = {bc, gc, rc}; - c_vector_push_back(outfile, _1300, 0, 3); + _tmp[0] = bc; + _tmp[1] = gc; + _tmp[2] = rc; + c_vector_push_back(outfile, _tmp, 0, 3); if ((i + 1) % w == 0) { for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) { /* ADD */ @@ -1308,77 +1336,103 @@ int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile) { } } else { /* Gray-scale */ - /* -->> -->> -->> -->> - 8 bits non code (Gray scale) - <<-- <<-- <<-- <<-- */ +/* -->> -->> -->> -->> +8 bits non code (Gray scale) +<<-- <<-- <<-- <<-- */ if (!outfile) { - fprintf(stderr, "ERROR -> failed to writing\n"); + fprintf(stderr, + "ERROR -> failed to writing\n"); return 1; } if (image->numcomps > 1) { - fprintf(stderr, "imagetobmp: only first component of %d is used.\n", + fprintf(stderr, + "imagetobmp: only first component of %d is used.\n", image->numcomps); } w = (int) image->comps[0].w; h = (int) image->comps[0].h; - c_vector_push_back(outfile, "BM", 0, 2); - - /* FILE HEADER */ - /* ------------- */ - OPJ_UINT8 _1331[4] = {(OPJ_UINT8) (h * w + 54 + 1024 + h * (w % 2)) & 0xff, - (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff, - (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff, - (OPJ_UINT8) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff}; - c_vector_push_back(outfile, _1331, 0, 4); - OPJ_UINT8 _1336[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, - ((0) >> 24) & 0xff}; - c_vector_push_back(outfile, _1336, 0, 4); - - OPJ_UINT8 _1340[4] = {(54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, - ((54 + 1024) >> 16) & 0xff, - ((54 + 1024) >> 24) & 0xff}; - c_vector_push_back(outfile, _1340, 0, 4); - - /* INFO HEADER */ - /* ------------- */ - OPJ_UINT8 _1347[4] = {(40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, - ((40) >> 24) & 0xff}; - c_vector_push_back(outfile, _1347, 0, 4); - OPJ_UINT8 _1350[4] = {(OPJ_UINT8) ((w) & 0xff), - (OPJ_UINT8) ((w) >> 8) & 0xff, - (OPJ_UINT8) ((w) >> 16) & 0xff, - (OPJ_UINT8) ((w) >> 24) & 0xff}; - c_vector_push_back(outfile, _1350, 0, 4); - OPJ_UINT8 _1355[4] = {(OPJ_UINT8) ((h) & 0xff), - (OPJ_UINT8) ((h) >> 8) & 0xff, - (OPJ_UINT8) ((h) >> 16) & 0xff, - (OPJ_UINT8) ((h) >> 24) & 0xff}; - c_vector_push_back(outfile, _1355, 0, 4); - OPJ_UINT8 _1360[4] = {(1) & 0xff, ((1) >> 8) & 0xff, (8) & 0xff, ((8) >> 8) & 0xff}; - c_vector_push_back(outfile, _1360, 0, 4); - OPJ_UINT8 _1363[4] = {(0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, - ((0) >> 24) & 0xff}; - c_vector_push_back(outfile, _1363, 0, 4); - OPJ_UINT8 _1366[4] = {(OPJ_UINT8) (h * w + h * (w % 2)) & 0xff, - (OPJ_UINT8) ((h * w + h * (w % 2)) >> 8) & 0xff, - (OPJ_UINT8) ((h * w + h * (w % 2)) >> 16) & 0xff, - (OPJ_UINT8) ((h * w + h * (w % 2)) >> 24) & 0xff}; - c_vector_push_back(outfile, _1366, 0, 4); - OPJ_UINT8 _1371[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, - ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; - c_vector_push_back(outfile, _1371, 0, 4); - OPJ_UINT8 _1374[4] = {(7834) & 0xff, ((7834) >> 8) & 0xff, - ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff}; - c_vector_push_back(outfile, _1374, 0, 4); - OPJ_UINT8 _1377[4] = {(256) & 0xff, ((256) >> 8) & 0xff, - ((256) >> 16) & 0xff, ((256) >> 24) & 0xff}; - c_vector_push_back(outfile, _1377, 0, 4); - OPJ_UINT8 _1380[4] = {(256) & 0xff, ((256) >> 8) & 0xff, - ((256) >> 16) & 0xff, ((256) >> 24) & 0xff}; - c_vector_push_back(outfile, _1380, 0, 4); + c_vector_push_back(outfile, + "BM", 0, 2); + +/* FILE HEADER */ +/* ------------- */ + _tmp[0] = (OPJ_UINT8) (h * w + 54 + 1024 + h * (w % 2)) & 0xff; + _tmp[1] = (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (0) & 0xff; + _tmp[1] = ((0) >> 8) & 0xff; + _tmp[2] = ((0) >> 16) & 0xff; + _tmp[3] = ((0) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (54 + 1024) & 0xff; + _tmp[1] = ((54 + 1024) >> 8) & 0xff; + _tmp[2] = ((54 + 1024) >> 16) & 0xff; + _tmp[3] = ((54 + 1024) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + +/* INFO HEADER */ +/* ------------- */ + _tmp[0] = (40) & 0xff; + _tmp[1] = ((40) >> 8) & 0xff; + _tmp[2] = ((40) >> 16) & 0xff; + _tmp[3] = ((40) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (OPJ_UINT8) ((w) & 0xff); + _tmp[1] = (OPJ_UINT8) ((w) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((w) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((w) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (OPJ_UINT8) ((h) & 0xff); + _tmp[1] = (OPJ_UINT8) ((h) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((h) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((h) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (1) & 0xff; + _tmp[1] = ((1) >> 8) & 0xff; + _tmp[2] = (8) & 0xff; + _tmp[3] = ((8) >> 8) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (0) & 0xff; + _tmp[1] = ((0) >> 8) & 0xff; + _tmp[2] = ((0) >> 16) & 0xff; + _tmp[3] = ((0) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (OPJ_UINT8) (h * w + h * (w % 2)) & 0xff; + _tmp[1] = (OPJ_UINT8) ((h * w + h * (w % 2)) >> 8) & 0xff; + _tmp[2] = (OPJ_UINT8) ((h * w + h * (w % 2)) >> 16) & 0xff; + _tmp[3] = (OPJ_UINT8) ((h * w + h * (w % 2)) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (7834) & 0xff; + _tmp[1] = ((7834) >> 8) & 0xff; + _tmp[2] = ((7834) >> 16) & 0xff; + _tmp[3] = ((7834) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (7834) & 0xff; + _tmp[1] = ((7834) >> 8) & 0xff; + _tmp[2] = ((7834) >> 16) & 0xff; + _tmp[3] = ((7834) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + + _tmp[0] = (256) & 0xff; + _tmp[1] = ((256) >> 8) & 0xff; + _tmp[2] = ((256) >> 16) & 0xff; + _tmp[3] = ((256) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, 0, 4); + _tmp[0] = (256) & 0xff; + _tmp[1] = ((256) >> 8) & 0xff; + _tmp[2] = ((256) >> 16) & 0xff; + _tmp[3] = ((256) >> 24) & 0xff; + c_vector_push_back(outfile, _tmp, + 0, 4); if (image->comps[0].prec > 8) { adjustR = (int) image->comps[0].prec - 8; @@ -1389,8 +1443,12 @@ int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile) { } for (i = 0; i < 256; i++) { - OPJ_UINT8 _1392[4] = {i, i, i, 0}; - c_vector_push_back(outfile, _1392, 0, 4); + _tmp[0] = i; + _tmp[1] = i; + _tmp[2] = i; + _tmp[3] = 0; + c_vector_push_back(outfile, _tmp, + 0, 4); } for (i = 0; i < w * h; i++) { @@ -1407,8 +1465,7 @@ int imagetobmp_c_vector(opj_image_t *image, c_vector *outfile) { r = 0; } - OPJ_UINT8 _1410[1] = {(OPJ_UINT8) r}; - c_vector_push_back(outfile, _1410, 0, 1); + c_vector_push_back(outfile, &r, 0, 1); if ((i + 1) % w == 0) { for (pad = (w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) { /* ADD */ diff --git a/src/bin/jp2/opj_decompress_c_vector.c b/src/bin/jp2/opj_decompress_c_vector.c index ed5fd2c71..e11f06b16 100644 --- a/src/bin/jp2/opj_decompress_c_vector.c +++ b/src/bin/jp2/opj_decompress_c_vector.c @@ -970,7 +970,7 @@ OPJ_FLOAT64 opj_clock(void) { #elif defined(__linux) struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); - return ((OPJ_FLOAT64)ts.tv_sec + (OPJ_FLOAT64)ts.tv_nsec * 1e-9); + return ((OPJ_FLOAT64) ts.tv_sec + (OPJ_FLOAT64) ts.tv_nsec * 1e-9); #else /* Unix : use resource usage */ /* FIXME: this counts the total CPU time, instead of the user perceived time */ @@ -1236,6 +1236,15 @@ int main(int argc, char **argv) { OPJ_UINT32 numDecompressedImages = 0; OPJ_UINT32 cp_reduce; + FILE *file; + size_t size; + size_t _size; + int64_t _size1 = 0; + c_vector *v = NULL; + c_vector *outfile = NULL; + unsigned char *buf = NULL; + + /* set decoding parameters to default values */ set_default_parameters(¶meters); @@ -1316,7 +1325,7 @@ int main(int argc, char **argv) { /* read the input file and put it in memory */ /* ---------------------------------------- */ - FILE *file = fopen(parameters.infile, "rb"); + file = fopen(parameters.infile, "rb"); if (!file) { fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile); @@ -1325,22 +1334,26 @@ int main(int argc, char **argv) { } fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_END); - size_t size = ftell(file); + _size1 = ftell(file); + if(_size1<0){ + size = (size_t) -_size1; + } else{ + size = (size_t) _size1; + } fseek(file, 0, SEEK_SET); - unsigned char *buf = (unsigned char *) malloc(size * sizeof(unsigned char)); + buf = (unsigned char *) malloc(size * sizeof(unsigned char)); if (!buf) { fclose(file); failed = 1; goto fin; } - size_t _size = fread(buf, 1, size, file); + _size = fread(buf, 1, size, file); fclose(file); if (_size != size) { free(buf); failed = 1; goto fin; } - c_vector *v = NULL; c_vector_init(&v); if (v == NULL) { free(buf); @@ -1627,7 +1640,6 @@ int main(int argc, char **argv) { goto fin; } } - c_vector *outfile = NULL; c_vector_init(&outfile); if (outfile == NULL) { fprintf(stderr, "[ERROR] Outfile c_vector\n"); @@ -1649,7 +1661,7 @@ int main(int argc, char **argv) { } break; case RAW_DFMT: /* RAW */ - if (imagetoraw_c_vector(image, outfile,OPJ_TRUE)) { + if (imagetoraw_c_vector(image, outfile, OPJ_TRUE)) { fprintf(stderr, "[ERROR] Error generating raw file. Outfile %s not generated\n", parameters.outfile); failed = 1; @@ -1659,7 +1671,7 @@ int main(int argc, char **argv) { break; case RAWL_DFMT: /* RAWL */ - if (imagetoraw_c_vector(image, outfile,OPJ_FALSE)) { + if (imagetoraw_c_vector(image, outfile, OPJ_FALSE)) { fprintf(stderr, "[ERROR] Error generating rawl file. Outfile %s not generated\n", parameters.outfile); diff --git a/src/lib/openjp2/c_vector.c b/src/lib/openjp2/c_vector.c index f16f53bbd..89fbc929a 100644 --- a/src/lib/openjp2/c_vector.c +++ b/src/lib/openjp2/c_vector.c @@ -31,7 +31,7 @@ int c_vector_init(c_vector **cVector) { * @return */ size_t c_vector_size(c_vector *cVector) { - if (!cVector)return -1; + if (!cVector)return 0; return cVector->total; } @@ -39,10 +39,11 @@ size_t c_vector_size(c_vector *cVector) { * resize c_vector * @param cVector */ -static int c_vector_resize(c_vector *cVector, size_t count) { +int c_vector_resize(c_vector *cVector, size_t count) { + void *items = NULL; if (!cVector)return -1; if (cVector->items) { - void *items = (void *) realloc(cVector->items, sizeof(void) * count); + items = (void *) realloc(cVector->items, sizeof(void) * count); if (items) { cVector->items = items; cVector->total = count; @@ -68,13 +69,14 @@ static int c_vector_resize(c_vector *cVector, size_t count) { * @param count */ int c_vector_push_back(c_vector *cVector, void *data, size_t offset, size_t count) { - size_t index = cVector->total; - size_t new_size = count + cVector->total; + size_t index, new_size; + index = cVector->total; + new_size = count + cVector->total; if (c_vector_resize(cVector, new_size) < 0) { // error realloc return -1; } - memcpy(&(cVector->items[index]), &data[offset], count); + memcpy(cVector->items + index, data + offset, count); return 0; } @@ -98,10 +100,10 @@ int c_vector_push_back_zero(c_vector *cVector) { */ size_t c_vector_set(c_vector *cVector, size_t index, void *data, size_t offset, size_t count) { if (cVector->total <= index || index + count >= cVector->total) { - return -1; + return 0; } - memcpy(&(cVector->items[index]), &data[offset], count); - return 0; + memcpy(cVector->items + index, data + offset, count); + return count; } /** @@ -114,21 +116,25 @@ size_t c_vector_set(c_vector *cVector, size_t index, void *data, size_t offset, * @return */ size_t c_vector_insert(c_vector *cVector, size_t index, void *data, size_t offset, size_t count) { + size_t last_total; + size_t len; + size_t i; + size_t new_size; if (cVector->total <= index) { - return -1; + return 0; } - size_t last_total = cVector->total; - size_t new_size = count + cVector->total; + last_total = cVector->total; + new_size = count + cVector->total; if (c_vector_resize(cVector, new_size) < 0) { // error realloc - return -1; + return 0; } - size_t len = last_total - index; - for (size_t i = 1; i <= len; i++) { - memcpy(&cVector->items[new_size - i], &cVector->items[last_total - i], 1); + len = last_total - index; + for (i = 1; i <= len; i++) { + memcpy(cVector->items + new_size - i, cVector->items + last_total - i, 1); } - memcpy(&(cVector->items[index]), &data[offset], count); - return 0; + memcpy(cVector->items + index, data + offset, count); + return count; } /** @@ -141,7 +147,7 @@ void *c_vector_get(c_vector *cVector, size_t offset) { if (cVector->total <= offset) { return NULL; } - return &cVector->items[offset]; + return cVector->items + offset; } /** @@ -161,15 +167,15 @@ void *c_vector_data(c_vector *cVector) { */ size_t c_vector_delete(c_vector *cVector, size_t offset, size_t count) { if (cVector->total <= offset) { - return -1; + return 0; } if (count + offset >= cVector->total) { cVector->total = cVector->total - offset; - return 0; + return count; } - memcpy(&(cVector->items[offset]), &(cVector->items[offset + count]), cVector->total - count - offset); + memcpy(cVector->items + offset, cVector->items + offset + count, cVector->total - count - offset); cVector->total = cVector->total - count; - return 0; + return count; } /** @@ -211,7 +217,7 @@ size_t c_vector_read(void *p_buffer, size_t p_nb_bytes, c_vector *v) { if (v->total - v->index < p_nb_bytes) { p_nb_bytes = v->total - v->index; } - memcpy(p_buffer, &(v->items[v->index]), p_nb_bytes); + memcpy(p_buffer, v->items + v->index, p_nb_bytes); v->index += p_nb_bytes; return p_nb_bytes; } diff --git a/src/lib/openjp2/c_vector.h b/src/lib/openjp2/c_vector.h index 2aab522c6..f9aaaa29f 100644 --- a/src/lib/openjp2/c_vector.h +++ b/src/lib/openjp2/c_vector.h @@ -16,20 +16,20 @@ typedef struct c_vector c_vector; * init * @param cVector */ -int c_vector_init(c_vector **cVector); +extern int c_vector_init(c_vector **cVector); /** * get c_vector size * @param cVector * @return */ -size_t c_vector_size(c_vector *cVector); +extern size_t c_vector_size(c_vector *cVector); /** * resize c_vector * @param cVector */ -static int c_vector_resize(c_vector *cVector, size_t count); +extern int c_vector_resize(c_vector *cVector, size_t count); /** * push back c_vector @@ -38,14 +38,14 @@ static int c_vector_resize(c_vector *cVector, size_t count); * @param offset * @param count */ -int c_vector_push_back(c_vector *cVector, void *data, size_t offset, size_t count); +extern int c_vector_push_back(c_vector *cVector, void *data, size_t offset, size_t count); /** * push zero to c_vector * @param cVector * @return */ -int c_vector_push_back_zero(c_vector *cVector); +extern int c_vector_push_back_zero(c_vector *cVector); /** * set data @@ -56,7 +56,7 @@ int c_vector_push_back_zero(c_vector *cVector); * @param count * @return */ -size_t c_vector_set(c_vector *cVector, size_t index, void *data, size_t offset, size_t count); +extern size_t c_vector_set(c_vector *cVector, size_t index, void *data, size_t offset, size_t count); /** * insert data @@ -67,7 +67,7 @@ size_t c_vector_set(c_vector *cVector, size_t index, void *data, size_t offset, * @param count * @return */ -size_t c_vector_insert(c_vector *cVector, size_t index, void *data, size_t offset, size_t count); +extern size_t c_vector_insert(c_vector *cVector, size_t index, void *data, size_t offset, size_t count); /** * get c_vector data @@ -75,14 +75,14 @@ size_t c_vector_insert(c_vector *cVector, size_t index, void *data, size_t offse * @param offset * @return */ -void *c_vector_get(c_vector *cVector, size_t offset); +extern void *c_vector_get(c_vector *cVector, size_t offset); /** * get c_vector data * @param cVector * @return */ -void *c_vector_data(c_vector *cVector); +extern void *c_vector_data(c_vector *cVector); /** * c_vector delete @@ -90,21 +90,21 @@ void *c_vector_data(c_vector *cVector); * @param offset * @param count */ -size_t c_vector_delete(c_vector *cVector, size_t offset, size_t count); +extern size_t c_vector_delete(c_vector *cVector, size_t offset, size_t count); /** * free c_vector * @param cVector */ -void c_vector_free(c_vector **cVector); +extern void c_vector_free(c_vector **cVector); -int c_vector_seek(c_vector *cVector, size_t offset, int whence); -int c_vector_seekg(size_t offset,c_vector *cVector); -int c_vector_skip(size_t offset,c_vector *cVector); +extern int c_vector_seek(c_vector *cVector, size_t offset, int whence); +extern int c_vector_seekg(size_t offset,c_vector *cVector); +extern int c_vector_skip(size_t offset,c_vector *cVector); -size_t c_vector_read(void *p_buffer, size_t p_nb_bytes, c_vector *v); +extern size_t c_vector_read(void *p_buffer, size_t p_nb_bytes, c_vector *v); -size_t c_vector_write(void *p_buffer, size_t p_nb_bytes, c_vector *v); +extern size_t c_vector_write(void *p_buffer, size_t p_nb_bytes, c_vector *v); #endif //CLANGTOOLS_VECTOR_H From 06fb629e7e98fab209ed1c6589dfb2f69e902c37 Mon Sep 17 00:00:00 2001 From: caesar Date: Sun, 19 Apr 2020 15:46:46 +0800 Subject: [PATCH 3/4] fix gcc build --- .gitignore | 3 +++ src/bin/jp2/convert.c | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index a6f4eff73..4c401df54 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ scripts/opjstyle* # Ignore directories made by `make`. /bin/ +/DG02File.j2k +/cmake-build-* +/.idea diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index cb0d6d83c..d77854044 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -2672,7 +2672,7 @@ int imagetoraw_c_vector(opj_image_t *image, c_vector *outfile, curr = -128; } uc = (unsigned char) (curr & mask); - res = c_vector_push_back(outfile, &uc, 0, 1); + res = c_vector_push_back(outfile, &uc, 0, 1)==0?0:1; if (res != 0) { fprintf(stderr, "failed to write 1 byte\n"); goto fin; @@ -2692,7 +2692,7 @@ int imagetoraw_c_vector(opj_image_t *image, c_vector *outfile, curr = 0; } uc = (unsigned char) (curr & mask); - res = c_vector_push_back(outfile, &uc, 0, 1); + res = c_vector_push_back(outfile, &uc, 0, 1)==0?0:1; if (res != 0) { fprintf(stderr, "failed to write 1 byte\n"); goto fin; @@ -2718,7 +2718,7 @@ int imagetoraw_c_vector(opj_image_t *image, c_vector *outfile, curr = -32768; } uc16.val = (signed short) (curr & mask); - res = c_vector_push_back(outfile, uc16.vals, 0, 2); + res = c_vector_push_back(outfile, uc16.vals, 0, 2)==0?0:1; if (res != 0) { fprintf(stderr, "failed to write 2 byte\n"); goto fin; @@ -2742,7 +2742,7 @@ int imagetoraw_c_vector(opj_image_t *image, c_vector *outfile, curr = 0; } uc16.val = (unsigned short) (curr & mask); - res = c_vector_push_back(outfile, uc16.vals, 0, 2); + res = c_vector_push_back(outfile, uc16.vals, 0, 2)==0?0:1; if (res != 0) { fprintf(stderr, "failed to write 2 byte\n"); goto fin; From 336835624ca4a040a9780beddbbb52a45916f1eb Mon Sep 17 00:00:00 2001 From: caesar Date: Sun, 19 Apr 2020 23:45:09 +0800 Subject: [PATCH 4/4] fix gcc build --- src/lib/openjp2/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/openjp2/CMakeLists.txt b/src/lib/openjp2/CMakeLists.txt index d421fdeb9..fe00246f8 100644 --- a/src/lib/openjp2/CMakeLists.txt +++ b/src/lib/openjp2/CMakeLists.txt @@ -122,7 +122,7 @@ install(TARGETS ${INSTALL_LIBS} ) # Install includes files -install(FILES openjpeg.h opj_stdint.h +install(FILES openjpeg.h opj_stdint.h c_vector.h DESTINATION ${OPENJPEG_INSTALL_INCLUDE_DIR} COMPONENT Headers )