summaryrefslogtreecommitdiff
path: root/src/bin/jp2
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2017-05-09 15:44:46 +0200
committerEven Rouault <even.rouault@spatialys.com>2017-05-09 20:46:20 +0200
commit563bd8499e63db976ca8358216138647593354bc (patch)
tree003599ed2b0cffd932d2122c3f72c142070efafd /src/bin/jp2
parentd4e54e9f35d532062533f1d369c159810b01d224 (diff)
Reformat whole codebase with astyle.options (#128)
Diffstat (limited to 'src/bin/jp2')
-rw-r--r--src/bin/jp2/convert.c2472
-rw-r--r--src/bin/jp2/convertbmp.c1621
-rw-r--r--src/bin/jp2/convertpng.c875
-rw-r--r--src/bin/jp2/converttif.c2671
-rw-r--r--src/bin/jp2/index.c757
-rw-r--r--src/bin/jp2/opj_compress.c1495
-rw-r--r--src/bin/jp2/opj_decompress.c2833
-rw-r--r--src/bin/jp2/opj_dump.c946
8 files changed, 7137 insertions, 6533 deletions
diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c
index deee4f6e..a540128f 100644
--- a/src/bin/jp2/convert.c
+++ b/src/bin/jp2/convert.c
@@ -1,6 +1,6 @@
/*
- * The copyright in this software is being made available under the 2-clauses
- * BSD License, included below. This software may be subject to other third
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
@@ -8,7 +8,7 @@
* Copyright (c) 2002-2014, Professor Benoit Macq
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
* Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* Copyright (c) 2006-2007, Parvatha Elangovan
@@ -50,7 +50,8 @@
*
* log2(a)
*/
-static int int_floorlog2(int a) {
+static int int_floorlog2(int a)
+{
int l;
for (l = 0; a > 1; l++) {
a >>= 1;
@@ -61,470 +62,491 @@ static int int_floorlog2(int a) {
/* Component precision scaling */
void clip_component(opj_image_comp_t* component, OPJ_UINT32 precision)
{
- OPJ_SIZE_T i;
- OPJ_SIZE_T len;
- OPJ_UINT32 umax = (OPJ_UINT32)((OPJ_INT32)-1);
-
- len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
- if (precision < 32) {
- umax = (1U << precision) - 1U;
- }
-
- if (component->sgnd) {
- OPJ_INT32* l_data = component->data;
- OPJ_INT32 max = (OPJ_INT32)(umax / 2U);
- OPJ_INT32 min = -max - 1;
- for (i = 0; i < len; ++i) {
- if (l_data[i] > max) {
- l_data[i] = max;
- } else if (l_data[i] < min) {
- l_data[i] = min;
- }
- }
- } else {
- OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
- for (i = 0; i < len; ++i) {
- if (l_data[i] > umax) {
- l_data[i] = umax;
- }
- }
- }
- component->prec = precision;
+ OPJ_SIZE_T i;
+ OPJ_SIZE_T len;
+ OPJ_UINT32 umax = (OPJ_UINT32)((OPJ_INT32) - 1);
+
+ len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
+ if (precision < 32) {
+ umax = (1U << precision) - 1U;
+ }
+
+ if (component->sgnd) {
+ OPJ_INT32* l_data = component->data;
+ OPJ_INT32 max = (OPJ_INT32)(umax / 2U);
+ OPJ_INT32 min = -max - 1;
+ for (i = 0; i < len; ++i) {
+ if (l_data[i] > max) {
+ l_data[i] = max;
+ } else if (l_data[i] < min) {
+ l_data[i] = min;
+ }
+ }
+ } else {
+ OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
+ for (i = 0; i < len; ++i) {
+ if (l_data[i] > umax) {
+ l_data[i] = umax;
+ }
+ }
+ }
+ component->prec = precision;
}
/* Component precision scaling */
-static void scale_component_up(opj_image_comp_t* component, OPJ_UINT32 precision)
+static void scale_component_up(opj_image_comp_t* component,
+ OPJ_UINT32 precision)
{
- OPJ_SIZE_T i, len;
-
- len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
- if (component->sgnd) {
- OPJ_INT64 newMax = (OPJ_INT64)(1U << (precision - 1));
- OPJ_INT64 oldMax = (OPJ_INT64)(1U << (component->prec - 1));
- OPJ_INT32* l_data = component->data;
- for (i = 0; i < len; ++i) {
- l_data[i] = (OPJ_INT32)(((OPJ_INT64)l_data[i] * newMax) / oldMax);
- }
- } else {
- OPJ_UINT64 newMax = (OPJ_UINT64)((1U << precision) - 1U);
- OPJ_UINT64 oldMax = (OPJ_UINT64)((1U << component->prec) - 1U);
- OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
- for (i = 0; i < len; ++i) {
- l_data[i] = (OPJ_UINT32)(((OPJ_UINT64)l_data[i] * newMax) / oldMax);
- }
- }
- component->prec = precision;
- component->bpp = precision;
+ OPJ_SIZE_T i, len;
+
+ len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
+ if (component->sgnd) {
+ OPJ_INT64 newMax = (OPJ_INT64)(1U << (precision - 1));
+ OPJ_INT64 oldMax = (OPJ_INT64)(1U << (component->prec - 1));
+ OPJ_INT32* l_data = component->data;
+ for (i = 0; i < len; ++i) {
+ l_data[i] = (OPJ_INT32)(((OPJ_INT64)l_data[i] * newMax) / oldMax);
+ }
+ } else {
+ OPJ_UINT64 newMax = (OPJ_UINT64)((1U << precision) - 1U);
+ OPJ_UINT64 oldMax = (OPJ_UINT64)((1U << component->prec) - 1U);
+ OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
+ for (i = 0; i < len; ++i) {
+ l_data[i] = (OPJ_UINT32)(((OPJ_UINT64)l_data[i] * newMax) / oldMax);
+ }
+ }
+ component->prec = precision;
+ component->bpp = precision;
}
void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision)
{
- int shift;
- OPJ_SIZE_T i, len;
-
- if (component->prec == precision) {
- return;
- }
- if (component->prec < precision) {
- scale_component_up(component, precision);
- return;
- }
- shift = (int)(component->prec - precision);
- len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
- if (component->sgnd) {
- OPJ_INT32* l_data = component->data;
- for (i = 0; i < len; ++i) {
- l_data[i] >>= shift;
- }
- } else {
- OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
- for (i = 0; i < len; ++i) {
- l_data[i] >>= shift;
- }
- }
- component->bpp = precision;
- component->prec = precision;
+ int shift;
+ OPJ_SIZE_T i, len;
+
+ if (component->prec == precision) {
+ return;
+ }
+ if (component->prec < precision) {
+ scale_component_up(component, precision);
+ return;
+ }
+ shift = (int)(component->prec - precision);
+ len = (OPJ_SIZE_T)component->w * (OPJ_SIZE_T)component->h;
+ if (component->sgnd) {
+ OPJ_INT32* l_data = component->data;
+ for (i = 0; i < len; ++i) {
+ l_data[i] >>= shift;
+ }
+ } else {
+ OPJ_UINT32* l_data = (OPJ_UINT32*)component->data;
+ for (i = 0; i < len; ++i) {
+ l_data[i] >>= shift;
+ }
+ }
+ component->bpp = precision;
+ component->prec = precision;
}
/* 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));
+ 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];
-
- for (i = 0; i < length; i++) {
- pDst0[i] = pSrc[2*i+0];
- pDst1[i] = pSrc[2*i+1];
- }
+ OPJ_SIZE_T i;
+ OPJ_INT32* pDst0 = pDst[0];
+ OPJ_INT32* pDst1 = pDst[1];
+
+ for (i = 0; i < length; i++) {
+ pDst0[i] = pSrc[2 * i + 0];
+ pDst1[i] = pSrc[2 * i + 1];
+ }
}
-static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
+static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- OPJ_INT32* pDst0 = pDst[0];
- OPJ_INT32* pDst1 = pDst[1];
- OPJ_INT32* pDst2 = pDst[2];
-
- for (i = 0; i < length; i++) {
- pDst0[i] = pSrc[3*i+0];
- pDst1[i] = pSrc[3*i+1];
- pDst2[i] = pSrc[3*i+2];
- }
+ OPJ_SIZE_T i;
+ OPJ_INT32* pDst0 = pDst[0];
+ OPJ_INT32* pDst1 = pDst[1];
+ OPJ_INT32* pDst2 = pDst[2];
+
+ for (i = 0; i < length; i++) {
+ pDst0[i] = pSrc[3 * i + 0];
+ pDst1[i] = pSrc[3 * i + 1];
+ pDst2[i] = pSrc[3 * i + 2];
+ }
}
-static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
+static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- OPJ_INT32* pDst0 = pDst[0];
- OPJ_INT32* pDst1 = pDst[1];
- OPJ_INT32* pDst2 = pDst[2];
- OPJ_INT32* pDst3 = pDst[3];
-
- for (i = 0; i < length; i++) {
- pDst0[i] = pSrc[4*i+0];
- pDst1[i] = pSrc[4*i+1];
- pDst2[i] = pSrc[4*i+2];
- pDst3[i] = pSrc[4*i+3];
- }
+ OPJ_SIZE_T i;
+ OPJ_INT32* pDst0 = pDst[0];
+ OPJ_INT32* pDst1 = pDst[1];
+ OPJ_INT32* pDst2 = pDst[2];
+ OPJ_INT32* pDst3 = pDst[3];
+
+ for (i = 0; i < length; i++) {
+ pDst0[i] = pSrc[4 * i + 0];
+ pDst1[i] = pSrc[4 * i + 1];
+ pDst2[i] = pSrc[4 * i + 2];
+ pDst3[i] = pSrc[4 * i + 3];
+ }
}
const convert_32s_CXPX convert_32s_CXPX_LUT[5] = {
- NULL,
- convert_32s_C1P1,
- convert_32s_C2P2,
- convert_32s_C3P3,
- convert_32s_C4P4
+ NULL,
+ convert_32s_C1P1,
+ convert_32s_C2P2,
+ convert_32s_C3P3,
+ convert_32s_C4P4
};
-static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
+static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length, OPJ_INT32 adjust)
{
- OPJ_SIZE_T i;
- const OPJ_INT32* pSrc0 = pSrc[0];
-
- for (i = 0; i < length; i++) {
- pDst[i] = pSrc0[i] + adjust;
- }
+ OPJ_SIZE_T i;
+ const OPJ_INT32* pSrc0 = pSrc[0];
+
+ for (i = 0; i < length; i++) {
+ pDst[i] = pSrc0[i] + adjust;
+ }
}
-static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
+static void convert_32s_P2C2(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length, OPJ_INT32 adjust)
{
- OPJ_SIZE_T i;
- const OPJ_INT32* pSrc0 = pSrc[0];
- const OPJ_INT32* pSrc1 = pSrc[1];
-
- for (i = 0; i < length; i++) {
- pDst[2*i+0] = pSrc0[i] + adjust;
- pDst[2*i+1] = pSrc1[i] + adjust;
- }
+ OPJ_SIZE_T i;
+ const OPJ_INT32* pSrc0 = pSrc[0];
+ const OPJ_INT32* pSrc1 = pSrc[1];
+
+ for (i = 0; i < length; i++) {
+ pDst[2 * i + 0] = pSrc0[i] + adjust;
+ pDst[2 * i + 1] = pSrc1[i] + adjust;
+ }
}
-static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
+static void convert_32s_P3C3(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length, OPJ_INT32 adjust)
{
- OPJ_SIZE_T i;
- const OPJ_INT32* pSrc0 = pSrc[0];
- const OPJ_INT32* pSrc1 = pSrc[1];
- const OPJ_INT32* pSrc2 = pSrc[2];
-
- for (i = 0; i < length; i++) {
- pDst[3*i+0] = pSrc0[i] + adjust;
- pDst[3*i+1] = pSrc1[i] + adjust;
- pDst[3*i+2] = pSrc2[i] + adjust;
- }
+ OPJ_SIZE_T i;
+ const OPJ_INT32* pSrc0 = pSrc[0];
+ const OPJ_INT32* pSrc1 = pSrc[1];
+ const OPJ_INT32* pSrc2 = pSrc[2];
+
+ for (i = 0; i < length; i++) {
+ pDst[3 * i + 0] = pSrc0[i] + adjust;
+ pDst[3 * i + 1] = pSrc1[i] + adjust;
+ pDst[3 * i + 2] = pSrc2[i] + adjust;
+ }
}
-static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
+static void convert_32s_P4C4(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length, OPJ_INT32 adjust)
{
- OPJ_SIZE_T i;
- const OPJ_INT32* pSrc0 = pSrc[0];
- const OPJ_INT32* pSrc1 = pSrc[1];
- const OPJ_INT32* pSrc2 = pSrc[2];
- const OPJ_INT32* pSrc3 = pSrc[3];
-
- for (i = 0; i < length; i++) {
- pDst[4*i+0] = pSrc0[i] + adjust;
- pDst[4*i+1] = pSrc1[i] + adjust;
- pDst[4*i+2] = pSrc2[i] + adjust;
- pDst[4*i+3] = pSrc3[i] + adjust;
- }
+ OPJ_SIZE_T i;
+ const OPJ_INT32* pSrc0 = pSrc[0];
+ const OPJ_INT32* pSrc1 = pSrc[1];
+ const OPJ_INT32* pSrc2 = pSrc[2];
+ const OPJ_INT32* pSrc3 = pSrc[3];
+
+ for (i = 0; i < length; i++) {
+ pDst[4 * i + 0] = pSrc0[i] + adjust;
+ pDst[4 * i + 1] = pSrc1[i] + adjust;
+ pDst[4 * i + 2] = pSrc2[i] + adjust;
+ pDst[4 * i + 3] = pSrc3[i] + adjust;
+ }
}
const convert_32s_PXCX convert_32s_PXCX_LUT[5] = {
- NULL,
- convert_32s_P1C1,
- convert_32s_P2C2,
- convert_32s_P3C3,
- convert_32s_P4C4
+ NULL,
+ convert_32s_P1C1,
+ convert_32s_P2C2,
+ convert_32s_P3C3,
+ convert_32s_P4C4
};
/* bit depth conversions */
/* used by PNG/TIFF up to 8bpp */
-static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val = *pSrc++;
- pDst[i+0] = (OPJ_INT32)( val >> 7);
- pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
- pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
- pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
- pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
- pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
- pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
- pDst[i+7] = (OPJ_INT32)(val & 0x1U);
- }
- if (length & 7U) {
- OPJ_UINT32 val = *pSrc++;
- length = length & 7U;
- pDst[i+0] = (OPJ_INT32)(val >> 7);
-
- if (length > 1U) {
- pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
- if (length > 2U) {
- pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
- if (length > 3U) {
- pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
- if (length > 4U) {
- pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
- if (length > 5U) {
- pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
- if (length > 6U) {
- pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val = *pSrc++;
+ pDst[i + 0] = (OPJ_INT32)(val >> 7);
+ pDst[i + 1] = (OPJ_INT32)((val >> 6) & 0x1U);
+ pDst[i + 2] = (OPJ_INT32)((val >> 5) & 0x1U);
+ pDst[i + 3] = (OPJ_INT32)((val >> 4) & 0x1U);
+ pDst[i + 4] = (OPJ_INT32)((val >> 3) & 0x1U);
+ pDst[i + 5] = (OPJ_INT32)((val >> 2) & 0x1U);
+ pDst[i + 6] = (OPJ_INT32)((val >> 1) & 0x1U);
+ pDst[i + 7] = (OPJ_INT32)(val & 0x1U);
+ }
+ if (length & 7U) {
+ OPJ_UINT32 val = *pSrc++;
+ length = length & 7U;
+ pDst[i + 0] = (OPJ_INT32)(val >> 7);
+
+ if (length > 1U) {
+ pDst[i + 1] = (OPJ_INT32)((val >> 6) & 0x1U);
+ if (length > 2U) {
+ pDst[i + 2] = (OPJ_INT32)((val >> 5) & 0x1U);
+ if (length > 3U) {
+ pDst[i + 3] = (OPJ_INT32)((val >> 4) & 0x1U);
+ if (length > 4U) {
+ pDst[i + 4] = (OPJ_INT32)((val >> 3) & 0x1U);
+ if (length > 5U) {
+ pDst[i + 5] = (OPJ_INT32)((val >> 2) & 0x1U);
+ if (length > 6U) {
+ pDst[i + 6] = (OPJ_INT32)((val >> 1) & 0x1U);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
-static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 val = *pSrc++;
- pDst[i+0] = (OPJ_INT32)( val >> 6);
- pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
- pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
- pDst[i+3] = (OPJ_INT32)(val & 0x3U);
- }
- if (length & 3U) {
- OPJ_UINT32 val = *pSrc++;
- length = length & 3U;
- pDst[i+0] = (OPJ_INT32)(val >> 6);
-
- if (length > 1U) {
- pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
- if (length > 2U) {
- pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
-
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 val = *pSrc++;
+ pDst[i + 0] = (OPJ_INT32)(val >> 6);
+ pDst[i + 1] = (OPJ_INT32)((val >> 4) & 0x3U);
+ pDst[i + 2] = (OPJ_INT32)((val >> 2) & 0x3U);
+ pDst[i + 3] = (OPJ_INT32)(val & 0x3U);
+ }
+ if (length & 3U) {
+ OPJ_UINT32 val = *pSrc++;
+ length = length & 3U;
+ pDst[i + 0] = (OPJ_INT32)(val >> 6);
+
+ if (length > 1U) {
+ pDst[i + 1] = (OPJ_INT32)((val >> 4) & 0x3U);
+ if (length > 2U) {
+ pDst[i + 2] = (OPJ_INT32)((val >> 2) & 0x3U);
+
+ }
+ }
+ }
}
-static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
- OPJ_UINT32 val = *pSrc++;
- pDst[i+0] = (OPJ_INT32)(val >> 4);
- pDst[i+1] = (OPJ_INT32)(val & 0xFU);
- }
- if (length & 1U) {
- OPJ_UINT8 val = *pSrc++;
- pDst[i+0] = (OPJ_INT32)(val >> 4);
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
+ OPJ_UINT32 val = *pSrc++;
+ pDst[i + 0] = (OPJ_INT32)(val >> 4);
+ pDst[i + 1] = (OPJ_INT32)(val & 0xFU);
+ }
+ if (length & 1U) {
+ OPJ_UINT8 val = *pSrc++;
+ pDst[i + 0] = (OPJ_INT32)(val >> 4);
+ }
}
-static void convert_6u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void convert_6u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- pDst[i+0] = (OPJ_INT32)(val0 >> 2);
- pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
- pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
- pDst[i+3] = (OPJ_INT32)(val2 & 0x3FU);
-
- }
- if (length & 3U) {
- OPJ_UINT32 val0 = *pSrc++;
- length = length & 3U;
- pDst[i+0] = (OPJ_INT32)(val0 >> 2);
-
- if (length > 1U) {
- OPJ_UINT32 val1 = *pSrc++;
- pDst[i+1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
- if (length > 2U) {
- OPJ_UINT32 val2 = *pSrc++;
- pDst[i+2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ pDst[i + 0] = (OPJ_INT32)(val0 >> 2);
+ pDst[i + 1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
+ pDst[i + 2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
+ pDst[i + 3] = (OPJ_INT32)(val2 & 0x3FU);
+
+ }
+ if (length & 3U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ length = length & 3U;
+ pDst[i + 0] = (OPJ_INT32)(val0 >> 2);
+
+ if (length > 1U) {
+ OPJ_UINT32 val1 = *pSrc++;
+ pDst[i + 1] = (OPJ_INT32)(((val0 & 0x3U) << 4) | (val1 >> 4));
+ if (length > 2U) {
+ OPJ_UINT32 val2 = *pSrc++;
+ pDst[i + 2] = (OPJ_INT32)(((val1 & 0xFU) << 2) | (val2 >> 6));
+ }
+ }
+ }
}
-static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < length; i++) {
- pDst[i] = pSrc[i];
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < length; i++) {
+ pDst[i] = pSrc[i];
+ }
}
const convert_XXx32s_C1R convert_XXu32s_C1R_LUT[9] = {
- NULL,
- convert_1u32s_C1R,
- convert_2u32s_C1R,
- NULL,
- convert_4u32s_C1R,
- NULL,
- convert_6u32s_C1R,
- NULL,
- convert_8u32s_C1R
+ NULL,
+ convert_1u32s_C1R,
+ convert_2u32s_C1R,
+ NULL,
+ convert_4u32s_C1R,
+ NULL,
+ convert_6u32s_C1R,
+ NULL,
+ convert_8u32s_C1R
};
-static void convert_32s1u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void convert_32s1u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1) | src7);
- }
-
- if (length & 7U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = 0U;
- OPJ_UINT32 src2 = 0U;
- OPJ_UINT32 src3 = 0U;
- OPJ_UINT32 src4 = 0U;
- OPJ_UINT32 src5 = 0U;
- OPJ_UINT32 src6 = 0U;
- length = length & 7U;
-
- if (length > 1U) {
- src1 = (OPJ_UINT32)pSrc[i+1];
- if (length > 2U) {
- src2 = (OPJ_UINT32)pSrc[i+2];
- if (length > 3U) {
- src3 = (OPJ_UINT32)pSrc[i+3];
- if (length > 4U) {
- src4 = (OPJ_UINT32)pSrc[i+4];
- if (length > 5U) {
- src5 = (OPJ_UINT32)pSrc[i+5];
- if (length > 6U) {
- src6 = (OPJ_UINT32)pSrc[i+6];
- }
- }
- }
- }
- }
- }
- *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) | (src4 << 3) | (src5 << 2) | (src6 << 1));
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) |
+ (src4 << 3) | (src5 << 2) | (src6 << 1) | src7);
+ }
+
+ if (length & 7U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = 0U;
+ OPJ_UINT32 src2 = 0U;
+ OPJ_UINT32 src3 = 0U;
+ OPJ_UINT32 src4 = 0U;
+ OPJ_UINT32 src5 = 0U;
+ OPJ_UINT32 src6 = 0U;
+ length = length & 7U;
+
+ if (length > 1U) {
+ src1 = (OPJ_UINT32)pSrc[i + 1];
+ if (length > 2U) {
+ src2 = (OPJ_UINT32)pSrc[i + 2];
+ if (length > 3U) {
+ src3 = (OPJ_UINT32)pSrc[i + 3];
+ if (length > 4U) {
+ src4 = (OPJ_UINT32)pSrc[i + 4];
+ if (length > 5U) {
+ src5 = (OPJ_UINT32)pSrc[i + 5];
+ if (length > 6U) {
+ src6 = (OPJ_UINT32)pSrc[i + 6];
+ }
+ }
+ }
+ }
+ }
+ }
+ *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 << 6) | (src2 << 5) | (src3 << 4) |
+ (src4 << 3) | (src5 << 2) | (src6 << 1));
+ }
}
-static void convert_32s2u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void convert_32s2u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
-
- *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3);
- }
-
- if (length & 3U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = 0U;
- OPJ_UINT32 src2 = 0U;
- length = length & 3U;
-
- if (length > 1U) {
- src1 = (OPJ_UINT32)pSrc[i+1];
- if (length > 2U) {
- src2 = (OPJ_UINT32)pSrc[i+2];
- }
- }
- *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2));
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+
+ *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2) | src3);
+ }
+
+ if (length & 3U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = 0U;
+ OPJ_UINT32 src2 = 0U;
+ length = length & 3U;
+
+ if (length > 1U) {
+ src1 = (OPJ_UINT32)pSrc[i + 1];
+ if (length > 2U) {
+ src2 = (OPJ_UINT32)pSrc[i + 2];
+ }
+ }
+ *pDst++ = (OPJ_BYTE)((src0 << 6) | (src1 << 4) | (src2 << 2));
+ }
}
-static void convert_32s4u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void convert_32s4u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
-
- *pDst++ = (OPJ_BYTE)((src0 << 4) | src1);
- }
-
- if (length & 1U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- *pDst++ = (OPJ_BYTE)((src0 << 4));
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+
+ *pDst++ = (OPJ_BYTE)((src0 << 4) | src1);
+ }
+
+ if (length & 1U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ *pDst++ = (OPJ_BYTE)((src0 << 4));
+ }
}
-static void convert_32s6u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void convert_32s6u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
-
- *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
- *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
- *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3);
- }
-
- if (length & 3U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = 0U;
- OPJ_UINT32 src2 = 0U;
- length = length & 3U;
-
- if (length > 1U) {
- src1 = (OPJ_UINT32)pSrc[i+1];
- if (length > 2U) {
- src2 = (OPJ_UINT32)pSrc[i+2];
- }
- }
- *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
- if (length > 1U) {
- *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
- if (length > 2U) {
- *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+
+ *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
+ *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
+ *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | src3);
+ }
+
+ if (length & 3U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = 0U;
+ OPJ_UINT32 src2 = 0U;
+ length = length & 3U;
+
+ if (length > 1U) {
+ src1 = (OPJ_UINT32)pSrc[i + 1];
+ if (length > 2U) {
+ src2 = (OPJ_UINT32)pSrc[i + 2];
+ }
+ }
+ *pDst++ = (OPJ_BYTE)((src0 << 2) | (src1 >> 4));
+ if (length > 1U) {
+ *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 2));
+ if (length > 2U) {
+ *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
+ }
+ }
+ }
}
-static void convert_32s8u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void convert_32s8u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < length; ++i) {
- pDst[i] = (OPJ_BYTE)pSrc[i];
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < length; ++i) {
+ pDst[i] = (OPJ_BYTE)pSrc[i];
+ }
}
const convert_32sXXx_C1R convert_32sXXu_C1R_LUT[9] = {
- NULL,
- convert_32s1u_C1R,
- convert_32s2u_C1R,
- NULL,
- convert_32s4u_C1R,
- NULL,
- convert_32s6u_C1R,
- NULL,
- convert_32s8u_C1R
+ NULL,
+ convert_32s1u_C1R,
+ convert_32s2u_C1R,
+ NULL,
+ convert_32s4u_C1R,
+ NULL,
+ convert_32s6u_C1R,
+ NULL,
+ convert_32s8u_C1R
};
/* -->> -->> -->> -->>
@@ -535,8 +557,7 @@ const convert_32sXXx_C1R convert_32sXXu_C1R_LUT[9] = {
#ifdef INFORMATION_ONLY
/* TGA header definition. */
-struct tga_header
-{
+struct tga_header {
unsigned char id_length; /* Image id field length */
unsigned char colour_map_type; /* Colour map type */
unsigned char image_type; /* Image type */
@@ -558,7 +579,8 @@ struct tga_header
};
#endif /* INFORMATION_ONLY */
-static unsigned short get_ushort(const unsigned char *data) {
+static unsigned short get_ushort(const unsigned char *data)
+{
unsigned short val = *(const unsigned short *)data;
#ifdef OPJ_BIG_ENDIAN
val = ((val & 0xffU) << 8) | (val >> 8);
@@ -568,7 +590,7 @@ static unsigned short get_ushort(const unsigned char *data) {
#define TGA_HEADER_SIZE 18
-static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
+static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
unsigned int *width, unsigned int *height, int *flip_image)
{
int palette_size;
@@ -578,12 +600,13 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size;
unsigned short /*x_origin, y_origin,*/ image_w, image_h;
- if (!bits_per_pixel || !width || !height || !flip_image)
+ if (!bits_per_pixel || !width || !height || !flip_image) {
return 0;
+ }
- if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ 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 ;
}
id_len = tga[0];
@@ -608,16 +631,15 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
*height = (unsigned int)image_h;
/* Ignore tga identifier, if present ... */
- if (id_len)
- {
+ if (id_len) {
unsigned char *id = (unsigned char *) malloc(id_len);
- if(id == 0){
- fprintf(stderr, "tga_readheader: memory out\n");
- return 0;
- }
- if ( !fread(id, id_len, 1, fp) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (id == 0) {
+ fprintf(stderr, "tga_readheader: memory out\n");
+ return 0;
+ }
+ if (!fread(id, id_len, 1, fp)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
free(id);
return 0 ;
}
@@ -626,9 +648,8 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
/* Test for compressed formats ... not yet supported ...
// Note :- 9 - RLE encoded palettized.
- // 10 - RLE encoded RGB. */
- if (image_type > 8)
- {
+ // 10 - RLE encoded RGB. */
+ if (image_type > 8) {
fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
return 0 ;
}
@@ -636,10 +657,9 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
*flip_image = !(image_desc & 32);
/* Palettized formats are not yet supported, skip over the palette, if present ... */
- palette_size = cmap_len * (cmap_entry_size/8);
+ palette_size = cmap_len * (cmap_entry_size / 8);
- if (palette_size>0)
- {
+ if (palette_size > 0) {
fprintf(stderr, "File contains a palette - not yet supported.");
fseek(fp, palette_size, SEEK_CUR);
}
@@ -655,60 +675,90 @@ static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x)
#endif
-static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
+static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
OPJ_BOOL flip_image)
{
OPJ_UINT16 image_w, image_h, us0;
unsigned char uc0, image_type;
unsigned char pixel_depth, image_desc;
- if (!bits_per_pixel || !width || !height)
+ if (!bits_per_pixel || !width || !height) {
return 0;
+ }
pixel_depth = 0;
- if ( bits_per_pixel < 256 )
+ if (bits_per_pixel < 256) {
pixel_depth = (unsigned char)bits_per_pixel;
- else{
- fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
+ } else {
+ fprintf(stderr, "ERROR: Wrong bits per pixel inside tga_header");
return 0;
}
uc0 = 0;
- if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */
- if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */
+ if (fwrite(&uc0, 1, 1, fp) != 1) {
+ goto fails; /* id_length */
+ }
+ if (fwrite(&uc0, 1, 1, fp) != 1) {
+ goto fails; /* colour_map_type */
+ }
image_type = 2; /* Uncompressed. */
- if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
+ if (fwrite(&image_type, 1, 1, fp) != 1) {
+ goto fails;
+ }
us0 = 0;
- if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */
- if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */
- if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */
+ if (fwrite(&us0, 2, 1, fp) != 1) {
+ goto fails; /* colour_map_index */
+ }
+ if (fwrite(&us0, 2, 1, fp) != 1) {
+ goto fails; /* colour_map_length */
+ }
+ if (fwrite(&uc0, 1, 1, fp) != 1) {
+ goto fails; /* colour_map_entry_size */
+ }
- if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */
- if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
+ if (fwrite(&us0, 2, 1, fp) != 1) {
+ goto fails; /* x_origin */
+ }
+ if (fwrite(&us0, 2, 1, fp) != 1) {
+ goto fails; /* y_origin */
+ }
image_w = (unsigned short)width;
image_h = (unsigned short) height;
#ifndef OPJ_BIG_ENDIAN
- if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
- if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
+ if (fwrite(&image_w, 2, 1, fp) != 1) {
+ goto fails;
+ }
+ if (fwrite(&image_h, 2, 1, fp) != 1) {
+ goto fails;
+ }
#else
image_w = swap16(image_w);
image_h = swap16(image_h);
- if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
- if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
+ if (fwrite(&image_w, 2, 1, fp) != 1) {
+ goto fails;
+ }
+ if (fwrite(&image_h, 2, 1, fp) != 1) {
+ goto fails;
+ }
#endif
- if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
+ if (fwrite(&pixel_depth, 1, 1, fp) != 1) {
+ goto fails;
+ }
image_desc = 8; /* 8 bits per component. */
- if (flip_image)
+ if (flip_image) {
image_desc |= 32;
- if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
+ }
+ if (fwrite(&image_desc, 1, 1, fp) != 1) {
+ goto fails;
+ }
return 1;
@@ -717,13 +767,14 @@ fails:
return 0;
}
-opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
+opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters)
+{
FILE *f;
opj_image_t *image;
unsigned int image_width, image_height, pixel_bit_depth;
unsigned int x, y;
- int flip_image=0;
- opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
+ int flip_image = 0;
+ opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */
int numcomps;
OPJ_COLOR_SPACE color_space;
OPJ_BOOL mono ;
@@ -737,7 +788,8 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
return 0;
}
- if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) {
+ if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height,
+ &flip_image)) {
fclose(f);
return NULL;
}
@@ -751,14 +803,15 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
/* initialize image components */
memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
- mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */
- save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
+ mono = (pixel_bit_depth == 8) ||
+ (pixel_bit_depth == 16); /* Mono with & without alpha. */
+ save_alpha = (pixel_bit_depth == 16) ||
+ (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
if (mono) {
color_space = OPJ_CLRSPC_GRAY;
numcomps = save_alpha ? 2 : 1;
- }
- else {
+ } else {
numcomps = save_alpha ? 4 : 3;
color_space = OPJ_CLRSPC_SRGB;
}
@@ -788,95 +841,93 @@ 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->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++)
- {
+ for (y = 0; y < image_height; y++) {
int index;
- if (flip_image)
- index = (int)((image_height-y-1)*image_width);
- else
- index = (int)(y*image_width);
+ if (flip_image) {
+ index = (int)((image_height - y - 1) * image_width);
+ } else {
+ index = (int)(y * image_width);
+ }
- if (numcomps==3)
- {
- for (x=0;x<image_width;x++)
- {
- unsigned char r,g,b;
+ if (numcomps == 3) {
+ for (x = 0; x < image_width; x++) {
+ unsigned char r, g, b;
- if( !fread(&b, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&b, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- if ( !fread(&g, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&g, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- if ( !fread(&r, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&r, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- image->comps[0].data[index]=r;
- image->comps[1].data[index]=g;
- image->comps[2].data[index]=b;
+ image->comps[0].data[index] = r;
+ image->comps[1].data[index] = g;
+ image->comps[2].data[index] = b;
index++;
}
- }
- else if (numcomps==4)
- {
- for (x=0;x<image_width;x++)
- {
- unsigned char r,g,b,a;
- if ( !fread(&b, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ } else if (numcomps == 4) {
+ for (x = 0; x < image_width; x++) {
+ unsigned char r, g, b, a;
+ if (!fread(&b, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- if ( !fread(&g, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&g, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- if ( !fread(&r, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&r, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- if ( !fread(&a, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&a, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- image->comps[0].data[index]=r;
- image->comps[1].data[index]=g;
- image->comps[2].data[index]=b;
- image->comps[3].data[index]=a;
+ image->comps[0].data[index] = r;
+ image->comps[1].data[index] = g;
+ image->comps[2].data[index] = b;
+ image->comps[3].data[index] = a;
index++;
}
- }
- else {
+ } else {
fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
}
}
@@ -884,13 +935,14 @@ 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;
int adjustR, adjustG, adjustB, fails;
unsigned int alpha_channel;
- float r,g,b,a;
+ float r, g, b, a;
unsigned char value;
float scale;
FILE *fdest;
@@ -903,12 +955,13 @@ int imagetotga(opj_image_t * image, const char *outfile) {
return 1;
}
- 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)) {
+ 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)) {
fclose(fdest);
- fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
+ fprintf(stderr,
+ "Unable to create a tga file with such J2K image charateristics.");
return 1;
}
}
@@ -917,90 +970,98 @@ int imagetotga(opj_image_t * image, const char *outfile) {
height = (int)image->comps[0].h;
/* Mono with alpha, or RGB with alpha. */
- write_alpha = (image->numcomps==2) || (image->numcomps==4);
+ write_alpha = (image->numcomps == 2) || (image->numcomps == 4);
/* Write TGA header */
bpp = write_alpha ? 32 : 24;
- if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
- goto fin;
+ if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
+ goto fin;
+ }
- alpha_channel = image->numcomps-1;
+ 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);
adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
- for (y=0; y < height; y++)
- {
- unsigned int index= (unsigned int)(y*width);
-
- for (x=0; x < width; x++, index++)
- {
- r = (float)(image->comps[0].data[index] + adjustR);
-
- if (image->numcomps > 2)
- {
- g = (float)(image->comps[1].data[index] + adjustG);
- b = (float)(image->comps[2].data[index] + adjustB);
- }
- else
- {/* Greyscale ... */
- g = r;
- b = r;
- }
-
-/* TGA format writes BGR ... */
- if(b > 255.) b = 255.; else if(b < 0.) b = 0.;
- value = (unsigned char)(b*scale);
- res = fwrite(&value,1,1,fdest);
-
- if( res < 1 )
- {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- goto fin;
- }
- if(g > 255.) g = 255.; else if(g < 0.) g = 0.;
- value = (unsigned char)(g*scale);
- res = fwrite(&value,1,1,fdest);
-
- if( res < 1 )
- {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- goto fin;
- }
- if(r > 255.) r = 255.; else if(r < 0.) r = 0.;
- value = (unsigned char)(r*scale);
- res = fwrite(&value,1,1,fdest);
-
- if( res < 1 )
- {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- goto fin;
- }
-
- if (write_alpha)
- {
- a = (float)(image->comps[alpha_channel].data[index]);
- if(a > 255.) a = 255.; else if(a < 0.) a = 0.;
- value = (unsigned char)(a*scale);
- res = fwrite(&value,1,1,fdest);
-
- if( res < 1 )
- {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- goto fin;
- }
- }
- }
- }
- fails = 0;
+ for (y = 0; y < height; y++) {
+ unsigned int index = (unsigned int)(y * width);
+
+ for (x = 0; x < width; x++, index++) {
+ r = (float)(image->comps[0].data[index] + adjustR);
+
+ if (image->numcomps > 2) {
+ g = (float)(image->comps[1].data[index] + adjustG);
+ b = (float)(image->comps[2].data[index] + adjustB);
+ } else {
+ /* Greyscale ... */
+ g = r;
+ b = r;
+ }
+
+ /* TGA format writes BGR ... */
+ if (b > 255.) {
+ b = 255.;
+ } else if (b < 0.) {
+ b = 0.;
+ }
+ value = (unsigned char)(b * scale);
+ res = fwrite(&value, 1, 1, fdest);
+
+ if (res < 1) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ goto fin;
+ }
+ if (g > 255.) {
+ g = 255.;
+ } else if (g < 0.) {
+ g = 0.;
+ }
+ value = (unsigned char)(g * scale);
+ res = fwrite(&value, 1, 1, fdest);
+
+ if (res < 1) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ goto fin;
+ }
+ if (r > 255.) {
+ r = 255.;
+ } else if (r < 0.) {
+ r = 0.;
+ }
+ value = (unsigned char)(r * scale);
+ res = fwrite(&value, 1, 1, fdest);
+
+ if (res < 1) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ goto fin;
+ }
+
+ if (write_alpha) {
+ a = (float)(image->comps[alpha_channel].data[index]);
+ if (a > 255.) {
+ a = 255.;
+ } else if (a < 0.) {
+ a = 0.;
+ }
+ value = (unsigned char)(a * scale);
+ res = fwrite(&value, 1, 1, fdest);
+
+ if (res < 1) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+ goto fin;
+ }
+ }
+ }
+ }
+ fails = 0;
fin:
- fclose(fdest);
+ fclose(fdest);
- return fails;
+ return fails;
}
/* -->> -->> -->> -->>
@@ -1013,9 +1074,9 @@ PGX IMAGE FORMAT
static unsigned char readuchar(FILE * f)
{
unsigned char c1;
- if ( !fread(&c1, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&c1, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
return 0;
}
return c1;
@@ -1024,61 +1085,66 @@ static unsigned char readuchar(FILE * f)
static unsigned short readushort(FILE * f, int bigendian)
{
unsigned char c1, c2;
- if ( !fread(&c1, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&c1, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
return 0;
}
- if ( !fread(&c2, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&c2, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
return 0;
}
- if (bigendian)
+ if (bigendian) {
return (unsigned short)((c1 << 8) + c2);
- else
+ } else {
return (unsigned short)((c2 << 8) + c1);
+ }
}
static unsigned int readuint(FILE * f, int bigendian)
{
unsigned char c1, c2, c3, c4;
- if ( !fread(&c1, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&c1, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
return 0;
}
- if ( !fread(&c2, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&c2, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
return 0;
}
- if ( !fread(&c3, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&c3, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
return 0;
}
- if ( !fread(&c4, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+ if (!fread(&c4, 1, 1, f)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
return 0;
}
- if (bigendian)
- 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;
+ if (bigendian) {
+ 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;
+ }
}
-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_cmptparm_t cmptparm; /* maximum of 1 component */
opj_image_t * image = NULL;
int adjustS, ushift, dshift, force8;
- char endian1,endian2,sign;
+ char endian1, endian2, sign;
char signtmp[32];
char temp[32];
@@ -1099,23 +1165,27 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
}
fseek(f, 0, SEEK_SET);
- if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
+ if (fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1,
+ &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
fclose(f);
- fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
+ fprintf(stderr,
+ "ERROR: Failed to read the right number of element from the fscanf() function!\n");
return NULL;
}
- i=0;
- sign='+';
- while (signtmp[i]!='\0') {
- if (signtmp[i]=='-') sign='-';
+ i = 0;
+ sign = '+';
+ while (signtmp[i] != '\0') {
+ if (signtmp[i] == '-') {
+ sign = '-';
+ }
i++;
}
fgetc(f);
- if (endian1=='M' && endian2=='L') {
+ if (endian1 == 'M' && endian2 == 'L') {
bigendian = 1;
- } else if (endian2=='M' && endian1=='L') {
+ } else if (endian2 == 'M' && endian1 == 'L') {
bigendian = 0;
} else {
fclose(f);
@@ -1127,23 +1197,32 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
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.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;
} else {
cmptparm.sgnd = 0;
}
- if(prec < 8)
- {
+ if (prec < 8) {
force8 = 1;
- ushift = 8 - prec; dshift = prec - ushift;
- if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0;
+ ushift = 8 - prec;
+ dshift = prec - ushift;
+ if (cmptparm.sgnd) {
+ adjustS = (1 << (prec - 1));
+ } else {
+ adjustS = 0;
+ }
cmptparm.sgnd = 0;
prec = 8;
+ } else {
+ ushift = dshift = force8 = adjustS = 0;
}
- else ushift = dshift = force8 = adjustS = 0;
cmptparm.prec = (OPJ_UINT32)prec;
cmptparm.bpp = (OPJ_UINT32)prec;
@@ -1152,7 +1231,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
/* create the image */
image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space);
- if(!image) {
+ if (!image) {
fclose(f);
return NULL;
}
@@ -1168,13 +1247,14 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
for (i = 0; i < w * h; i++) {
int v;
- if(force8)
- {
+ if (force8) {
v = readuchar(f) + adjustS;
- v = (v<<ushift) + (v>>dshift);
+ v = (v << ushift) + (v >> dshift);
comp->data[i] = (unsigned char)v;
- if(v > max) max = v;
+ if (v > max) {
+ max = v;
+ }
continue;
}
@@ -1197,8 +1277,9 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
v = (int) readuint(f, bigendian);
}
}
- if (v > max)
+ if (v > max) {
max = v;
+ }
comp->data[i] = v;
}
fclose(f);
@@ -1209,108 +1290,116 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
#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);
- else if (prec <= 16) return CLAMP(value,-32768,32767);
- else return CLAMP(value,-2147483647-1,2147483647);
- }
- else
- {
- if (prec <= 8) return CLAMP(value,0,255);
- else if (prec <= 16) return CLAMP(value,0,65535);
- else return value; /*CLAMP(value,0,4294967295);*/
+ if (sgnd) {
+ if (prec <= 8) {
+ return CLAMP(value, -128, 127);
+ } else if (prec <= 16) {
+ return CLAMP(value, -32768, 32767);
+ } else {
+ return CLAMP(value, -2147483647 - 1, 2147483647);
+ }
+ } else {
+ if (prec <= 8) {
+ return CLAMP(value, 0, 255);
+ } else if (prec <= 16) {
+ return CLAMP(value, 0, 65535);
+ } else {
+ return value; /*CLAMP(value,0,4294967295);*/
+ }
}
}
-int imagetopgx(opj_image_t * image, const char *outfile)
+int imagetopgx(opj_image_t * image, const char *outfile)
{
- int w, h;
- int i, j, fails = 1;
- unsigned int compno;
- FILE *fdest = NULL;
-
- for (compno = 0; compno < image->numcomps; compno++)
- {
- opj_image_comp_t *comp = &image->comps[compno];
- char bname[256]; /* buffer for name */
- char *name = bname; /* pointer */
- int nbytes = 0;
- size_t res;
- const size_t olen = strlen(outfile);
- const size_t dotpos = olen - 4;
- const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
-
- if( outfile[dotpos] != '.' )
- {
- /* `pgx` was recognized but there is no dot at expected position */
- fprintf(stderr, "ERROR -> Impossible happen." );
- goto fin;
- }
- if( total > 256 )
- {
- name = (char*)malloc(total+1);
- if (name == NULL) {
- fprintf(stderr, "imagetopgx: memory out\n");
- goto fin;
- }
- }
- strncpy(name, outfile, dotpos);
- sprintf(name+dotpos, "_%u.pgx", compno);
- fdest = fopen(name, "wb");
- /* don't need name anymore */
-
- if (!fdest)
- {
-
- fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
- if( total > 256 ) free(name);
- goto fin;
- }
-
- w = (int)image->comps[compno].w;
- h = (int)image->comps[compno].h;
-
- fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
- w, h);
-
- if (comp->prec <= 8)
- nbytes = 1;
- else if (comp->prec <= 16)
- nbytes = 2;
- else
- nbytes = 4;
-
- for (i = 0; i < w * h; i++)
- {
- /* FIXME: clamp func is being called within a loop */
- const int val = clamp(image->comps[compno].data[i],
- (int)comp->prec, (int)comp->sgnd);
-
- for (j = nbytes - 1; j >= 0; j--)
- {
- int v = (int)(val >> (j * 8));
- unsigned char byte = (unsigned char)v;
- res = fwrite(&byte, 1, 1, fdest);
-
- if( res < 1 )
- {
- fprintf(stderr, "failed to write 1 byte for %s\n", name);
- if( total > 256 ) free(name);
- goto fin;
- }
- }
- }
- if( total > 256 ) free(name);
- fclose(fdest); fdest = NULL;
- }
- fails = 0;
+ int w, h;
+ int i, j, fails = 1;
+ unsigned int compno;
+ FILE *fdest = NULL;
+
+ for (compno = 0; compno < image->numcomps; compno++) {
+ opj_image_comp_t *comp = &image->comps[compno];
+ char bname[256]; /* buffer for name */
+ char *name = bname; /* pointer */
+ int nbytes = 0;
+ size_t res;
+ const size_t olen = strlen(outfile);
+ const size_t dotpos = olen - 4;
+ const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
+
+ if (outfile[dotpos] != '.') {
+ /* `pgx` was recognized but there is no dot at expected position */
+ fprintf(stderr, "ERROR -> Impossible happen.");
+ goto fin;
+ }
+ if (total > 256) {
+ name = (char*)malloc(total + 1);
+ if (name == NULL) {
+ fprintf(stderr, "imagetopgx: memory out\n");
+ goto fin;
+ }
+ }
+ strncpy(name, outfile, dotpos);
+ sprintf(name + dotpos, "_%u.pgx", compno);
+ fdest = fopen(name, "wb");
+ /* don't need name anymore */
+
+ if (!fdest) {
+
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
+ if (total > 256) {
+ free(name);
+ }
+ goto fin;
+ }
+
+ w = (int)image->comps[compno].w;
+ h = (int)image->comps[compno].h;
+
+ fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
+ w, h);
+
+ if (comp->prec <= 8) {
+ nbytes = 1;
+ } else if (comp->prec <= 16) {
+ nbytes = 2;
+ } else {
+ nbytes = 4;
+ }
+
+ for (i = 0; i < w * h; i++) {
+ /* FIXME: clamp func is being called within a loop */
+ const int val = clamp(image->comps[compno].data[i],
+ (int)comp->prec, (int)comp->sgnd);
+
+ for (j = nbytes - 1; j >= 0; j--) {
+ int v = (int)(val >> (j * 8));
+ unsigned char byte = (unsigned char)v;
+ res = fwrite(&byte, 1, 1, fdest);
+
+ if (res < 1) {
+ fprintf(stderr, "failed to write 1 byte for %s\n", name);
+ if (total > 256) {
+ free(name);
+ }
+ goto fin;
+ }
+ }
+ }
+ if (total > 256) {
+ free(name);
+ }
+ fclose(fdest);
+ fdest = NULL;
+ }
+ fails = 0;
fin:
- if(fdest) fclose(fdest);
+ if (fdest) {
+ fclose(fdest);
+ }
- return fails;
+ return fails;
}
/* -->> -->> -->> -->>
@@ -1319,8 +1408,7 @@ PNM IMAGE FORMAT
<<-- <<-- <<-- <<-- */
-struct pnm_header
-{
+struct pnm_header {
int width, height, maxval, depth, format;
char rgb, rgba, gray, graya, bw;
char ok;
@@ -1328,12 +1416,15 @@ struct pnm_header
static char *skip_white(char *s)
{
- if (s != NULL)
- {
- while(*s)
- {
- if(*s == '\n' || *s == '\r') return NULL;
- if(isspace(*s)) { ++s; continue; }
+ if (s != NULL) {
+ while (*s) {
+ if (*s == '\n' || *s == '\r') {
+ return NULL;
+ }
+ if (isspace(*s)) {
+ ++s;
+ continue;
+ }
return s;
}
}
@@ -1348,15 +1439,21 @@ static char *skip_int(char *start, int *out_n)
*out_n = 0;
s = skip_white(start);
- if(s == NULL) return NULL;
+ if (s == NULL) {
+ return NULL;
+ }
start = s;
- while(*s)
- {
- if( !isdigit(*s)) break;
+ while (*s) {
+ if (!isdigit(*s)) {
+ break;
+ }
++s;
}
- c = *s; *s = 0; *out_n = atoi(start); *s = c;
+ c = *s;
+ *s = 0;
+ *out_n = atoi(start);
+ *s = c;
return s;
}
@@ -1366,15 +1463,22 @@ static char *skip_idf(char *start, char out_idf[256])
char c;
s = skip_white(start);
- if(s == NULL) return NULL;
+ if (s == NULL) {
+ return NULL;
+ }
start = s;
- while(*s)
- {
- if(isalpha(*s) || *s == '_') { ++s; continue; }
+ while (*s) {
+ if (isalpha(*s) || *s == '_') {
+ ++s;
+ continue;
+ }
break;
}
- c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c;
+ c = *s;
+ *s = 0;
+ strncpy(out_idf, start, 255);
+ *s = c;
return s;
}
@@ -1384,147 +1488,169 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph)
char idf[256], type[256];
char line[256];
- if (fgets(line, 250, reader) == NULL)
- {
- fprintf(stderr,"\nWARNING: fgets return a NULL value");
+ if (fgets(line, 250, reader) == NULL) {
+ fprintf(stderr, "\nWARNING: fgets return a NULL value");
return;
}
- if(line[0] != 'P')
- {
- fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return;
+ if (line[0] != 'P') {
+ fprintf(stderr, "read_pnm_header:PNM:magic P missing\n");
+ return;
}
format = atoi(line + 1);
- if(format < 1 || format > 7)
- {
- fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format);
+ if (format < 1 || format > 7) {
+ fprintf(stderr, "read_pnm_header:magic format %d invalid\n", format);
return;
}
ph->format = format;
ttype = end = 0;
- while(fgets(line, 250, reader))
- {
+ while (fgets(line, 250, reader)) {
char *s;
int allow_null = 0;
- if(*line == '#') continue;
+ if (*line == '#') {
+ continue;
+ }
s = line;
- if(format == 7)
- {
+ if (format == 7) {
s = skip_idf(s, idf);
- if(s == NULL || *s == 0) return;
+ if (s == NULL || *s == 0) {
+ return;
+ }
- if(strcmp(idf, "ENDHDR") == 0)
- {
- end = 1; break;
+ if (strcmp(idf, "ENDHDR") == 0) {
+ end = 1;
+ break;
}
- if(strcmp(idf, "WIDTH") == 0)
- {
+ if (strcmp(idf, "WIDTH") == 0) {
s = skip_int(s, &ph->width);
- if(s == NULL || *s == 0) return;
+ if (s == NULL || *s == 0) {
+ return;
+ }
continue;
}
- if(strcmp(idf, "HEIGHT") == 0)
- {
+ if (strcmp(idf, "HEIGHT") == 0) {
s = skip_int(s, &ph->height);
- if(s == NULL || *s == 0) return;
+ if (s == NULL || *s == 0) {
+ return;
+ }
continue;
}
- if(strcmp(idf, "DEPTH") == 0)
- {
+ if (strcmp(idf, "DEPTH") == 0) {
s = skip_int(s, &ph->depth);
- if(s == NULL || *s == 0) return;
+ if (s == NULL || *s == 0) {
+ return;
+ }
continue;
}
- if(strcmp(idf, "MAXVAL") == 0)
- {
+ if (strcmp(idf, "MAXVAL") == 0) {
s = skip_int(s, &ph->maxval);
- if(s == NULL || *s == 0) return;
+ if (s == NULL || *s == 0) {
+ return;
+ }
continue;
}
- if(strcmp(idf, "TUPLTYPE") == 0)
- {
+ if (strcmp(idf, "TUPLTYPE") == 0) {
s = skip_idf(s, type);
- if(s == NULL || *s == 0) return;
+ if (s == NULL || *s == 0) {
+ return;
+ }
- if(strcmp(type, "BLACKANDWHITE") == 0)
- {
- ph->bw = 1; ttype = 1; continue;
+ if (strcmp(type, "BLACKANDWHITE") == 0) {
+ ph->bw = 1;
+ ttype = 1;
+ continue;
}
- if(strcmp(type, "GRAYSCALE") == 0)
- {
- ph->gray = 1; ttype = 1; continue;
+ if (strcmp(type, "GRAYSCALE") == 0) {
+ ph->gray = 1;
+ ttype = 1;
+ continue;
}
- if(strcmp(type, "GRAYSCALE_ALPHA") == 0)
- {
- ph->graya = 1; ttype = 1; continue;
+ if (strcmp(type, "GRAYSCALE_ALPHA") == 0) {
+ ph->graya = 1;
+ ttype = 1;
+ continue;
}
- if(strcmp(type, "RGB") == 0)
- {
- ph->rgb = 1; ttype = 1; continue;
+ if (strcmp(type, "RGB") == 0) {
+ ph->rgb = 1;
+ ttype = 1;
+ continue;
}
- if(strcmp(type, "RGB_ALPHA") == 0)
- {
- ph->rgba = 1; ttype = 1; continue;
+ if (strcmp(type, "RGB_ALPHA") == 0) {
+ ph->rgba = 1;
+ ttype = 1;
+ continue;
}
- fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type);
+ fprintf(stderr, "read_pnm_header:unknown P7 TUPLTYPE %s\n", type);
return;
}
- fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf);
+ fprintf(stderr, "read_pnm_header:unknown P7 idf %s\n", idf);
return;
} /* if(format == 7) */
/* Here format is in range [1,6] */
if (ph->width == 0) {
s = skip_int(s, &ph->width);
- if ((s == NULL) || (*s == 0) || (ph->width < 1)) return;
+ if ((s == NULL) || (*s == 0) || (ph->width < 1)) {
+ return;
+ }
allow_null = 1;
}
if (ph->height == 0) {
s = skip_int(s, &ph->height);
- if ((s == NULL) && allow_null) continue;
- if ((s == NULL) || (*s == 0) || (ph->height < 1)) return;
- if(format == 1 || format == 4) {
+ if ((s == NULL) && allow_null) {
+ continue;
+ }
+ if ((s == NULL) || (*s == 0) || (ph->height < 1)) {
+ return;
+ }
+ if (format == 1 || format == 4) {
break;
}
allow_null = 1;
}
/* here, format is in P2, P3, P5, P6 */
s = skip_int(s, &ph->maxval);
- if ((s == NULL) && allow_null) continue;
- if ((s == NULL) || (*s == 0)) return;
+ if ((s == NULL) && allow_null) {
+ continue;
+ }
+ if ((s == NULL) || (*s == 0)) {
+ return;
+ }
break;
}/* while(fgets( ) */
- if(format == 2 || format == 3 || format > 4)
- {
- if(ph->maxval < 1 || ph->maxval > 65535) return;
+ if (format == 2 || format == 3 || format > 4) {
+ if (ph->maxval < 1 || ph->maxval > 65535) {
+ return;
+ }
+ }
+ if (ph->width < 1 || ph->height < 1) {
+ return;
}
- if(ph->width < 1 || ph->height < 1) return;
- if(format == 7)
- {
- if(!end)
- {
- fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return;
+ if (format == 7) {
+ if (!end) {
+ fprintf(stderr, "read_pnm_header:P7 without ENDHDR\n");
+ return;
+ }
+ if (ph->depth < 1 || ph->depth > 4) {
+ return;
}
- if(ph->depth < 1 || ph->depth > 4) return;
- if (ttype)
+ if (ttype) {
ph->ok = 1;
- }
- else
- {
+ }
+ } else {
ph->ok = 1;
- if(format == 1 || format == 4)
- {
+ if (format == 1 || format == 4) {
ph->maxval = 255;
}
}
@@ -1532,25 +1658,56 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph)
static int has_prec(int val)
{
- if(val < 2) return 1;
- if(val < 4) return 2;
- if(val < 8) return 3;
- if(val < 16) return 4;
- if(val < 32) return 5;
- if(val < 64) return 6;
- if(val < 128) return 7;
- if(val < 256) return 8;
- if(val < 512) return 9;
- if(val < 1024) return 10;
- if(val < 2048) return 11;
- if(val < 4096) return 12;
- if(val < 8192) return 13;
- if(val < 16384) return 14;
- if(val < 32768) return 15;
+ if (val < 2) {
+ return 1;
+ }
+ if (val < 4) {
+ return 2;
+ }
+ if (val < 8) {
+ return 3;
+ }
+ if (val < 16) {
+ return 4;
+ }
+ if (val < 32) {
+ return 5;
+ }
+ if (val < 64) {
+ return 6;
+ }
+ if (val < 128) {
+ return 7;
+ }
+ if (val < 256) {
+ return 8;
+ }
+ if (val < 512) {
+ return 9;
+ }
+ if (val < 1024) {
+ return 10;
+ }
+ if (val < 2048) {
+ return 11;
+ }
+ if (val < 4096) {
+ return 12;
+ }
+ if (val < 8192) {
+ return 13;
+ }
+ if (val < 16384) {
+ return 14;
+ }
+ if (val < 32768) {
+ return 15;
+ }
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;
@@ -1561,21 +1718,22 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
opj_image_t * image = NULL;
struct pnm_header header_info;
- if((fp = fopen(filename, "rb")) == NULL)
- {
- fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename);
+ if ((fp = fopen(filename, "rb")) == NULL) {
+ fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n", filename);
return NULL;
}
memset(&header_info, 0, sizeof(struct pnm_header));
read_pnm_header(fp, &header_info);
- if(!header_info.ok) { fclose(fp); return NULL; }
+ if (!header_info.ok) {
+ fclose(fp);
+ return NULL;
+ }
format = header_info.format;
- switch(format)
- {
+ switch (format) {
case 1: /* ascii bitmap */
case 4: /* raw bitmap */
numcomps = 1;
@@ -1595,16 +1753,21 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
numcomps = header_info.depth;
break;
- default: fclose(fp); return NULL;
+ default:
+ fclose(fp);
+ return NULL;
+ }
+ if (numcomps < 3) {
+ color_space = OPJ_CLRSPC_GRAY; /* GRAY, GRAYA */
+ } else {
+ color_space = OPJ_CLRSPC_SRGB; /* RGB, RGBA */
}
- if(numcomps < 3)
- color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */
- else
- color_space = OPJ_CLRSPC_SRGB;/* RGB, RGBA */
prec = has_prec(header_info.maxval);
- if(prec < 8) prec = 8;
+ if (prec < 8) {
+ prec = 8;
+ }
w = header_info.width;
h = header_info.height;
@@ -1613,8 +1776,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
- for(i = 0; i < numcomps; i++)
- {
+ for (i = 0; i < numcomps; i++) {
cmptparm[i].prec = (OPJ_UINT32)prec;
cmptparm[i].bpp = (OPJ_UINT32)prec;
cmptparm[i].sgnd = 0;
@@ -1625,114 +1787,104 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
}
image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
- if(!image) { fclose(fp); return NULL; }
+ if (!image) {
+ fclose(fp);
+ return NULL;
+ }
/* set image offset and reference grid */
image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
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->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx
+ + 1);
+ image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy
+ + 1);
- if((format == 2) || (format == 3)) /* ascii pixmap */
- {
+ if ((format == 2) || (format == 3)) { /* ascii pixmap */
unsigned int index;
- for (i = 0; i < w * h; i++)
- {
- for(compno = 0; compno < numcomps; compno++)
- {
+ for (i = 0; i < w * h; i++) {
+ for (compno = 0; compno < numcomps; compno++) {
index = 0;
- if (fscanf(fp, "%u", &index) != 1)
- fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
+ if (fscanf(fp, "%u", &index) != 1) {
+ fprintf(stderr,
+ "\nWARNING: fscanf return a number of element different from the expected.\n");
+ }
- 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)
- || (format == 6)
- ||((format == 7)
- && ( header_info.gray || header_info.graya
- || header_info.rgb || header_info.rgba)))/* binary pixmap */
- {
- unsigned char c0, c1, one;
-
- one = (prec < 9);
-
- for (i = 0; i < w * h; i++)
- {
- for(compno = 0; compno < numcomps; compno++)
- {
- if ( !fread(&c0, 1, 1, fp) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- opj_image_destroy(image);
- fclose(fp);
- return NULL;
- }
- if(one)
- {
- image->comps[compno].data[i] = c0;
- }
- else
- {
- if ( !fread(&c1, 1, 1, fp) )
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- /* netpbm: */
- image->comps[compno].data[i] = ((c0<<8) | c1);
+ } else if ((format == 5)
+ || (format == 6)
+ || ((format == 7)
+ && (header_info.gray || header_info.graya
+ || header_info.rgb || header_info.rgba))) { /* binary pixmap */
+ unsigned char c0, c1, one;
+
+ one = (prec < 9);
+
+ for (i = 0; i < w * h; i++) {
+ for (compno = 0; compno < numcomps; compno++) {
+ if (!fread(&c0, 1, 1, fp)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
+ opj_image_destroy(image);
+ fclose(fp);
+ return NULL;
+ }
+ if (one) {
+ image->comps[compno].data[i] = c0;
+ } else {
+ if (!fread(&c1, 1, 1, fp)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
}
+ /* netpbm: */
+ image->comps[compno].data[i] = ((c0 << 8) | c1);
}
}
}
- else
- if(format == 1) /* ascii bitmap */
- {
- for (i = 0; i < w * h; i++)
- {
- unsigned int index;
+ } else if (format == 1) { /* ascii bitmap */
+ for (i = 0; i < w * h; i++) {
+ unsigned int index;
- if ( fscanf(fp, "%u", &index) != 1)
- fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
+ if (fscanf(fp, "%u", &index) != 1) {
+ fprintf(stderr,
+ "\nWARNING: fscanf return a number of element different from the expected.\n");
+ }
- image->comps[0].data[i] = (index?0:255);
+ image->comps[0].data[i] = (index ? 0 : 255);
+ }
+ } else if (format == 4) {
+ int x, y, bit;
+ unsigned char uc;
+
+ i = 0;
+ for (y = 0; y < h; ++y) {
+ bit = -1;
+ uc = 0;
+
+ for (x = 0; x < w; ++x) {
+ if (bit == -1) {
+ bit = 7;
+ uc = (unsigned char)getc(fp);
}
+ image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255);
+ --bit;
+ ++i;
}
- else
- if(format == 4)
- {
- int x, y, bit;
- unsigned char uc;
-
- i = 0;
- for(y = 0; y < h; ++y)
- {
- bit = -1; uc = 0;
-
- for(x = 0; x < w; ++x)
- {
- if(bit == -1)
- {
- bit = 7;
- uc = (unsigned char)getc(fp);
- }
- image->comps[0].data[i] = (((uc>>bit) & 1)?0:255);
- --bit; ++i;
- }
- }
- }
- else
- if((format == 7 && header_info.bw)) /*MONO*/
- {
- unsigned char uc;
-
- for(i = 0; i < w * h; ++i)
- {
- if ( !fread(&uc, 1, 1, fp) )
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- image->comps[0].data[i] = (uc & 1)?0:255;
- }
- }
+ }
+ } else if ((format == 7 && header_info.bw)) { /*MONO*/
+ unsigned char uc;
+
+ for (i = 0; i < w * h; ++i) {
+ if (!fread(&uc, 1, 1, fp)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
+ }
+ image->comps[0].data[i] = (uc & 1) ? 0 : 255;
+ }
+ }
fclose(fp);
return image;
@@ -1753,215 +1905,253 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
alpha = NULL;
- 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);
+ if ((prec = (int)image->comps[0].prec) > 16) {
+ fprintf(stderr, "%s:%d:imagetopnm\n\tprecision %d is larger than 16"
+ "\n\t: refused.\n", __FILE__, __LINE__, prec);
return 1;
}
- two = has_alpha = 0; fails = 1;
+ two = has_alpha = 0;
+ fails = 1;
ncomp = image->numcomps;
- while (*tmp) ++tmp; tmp -= 2;
+ while (*tmp) {
+ ++tmp;
+ }
+ tmp -= 2;
want_gray = (*tmp == 'g' || *tmp == 'G');
ncomp = image->numcomps;
- if(want_gray) ncomp = 1;
+ if (want_gray) {
+ ncomp = 1;
+ }
if ((force_split == 0) &&
- (ncomp == 2 /* GRAYA */
- || (ncomp > 2 /* RGB, RGBA */
- && image->comps[0].dx == image->comps[1].dx
- && image->comps[1].dx == image->comps[2].dx
- && image->comps[0].dy == image->comps[1].dy
- && image->comps[1].dy == image->comps[2].dy
- && image->comps[0].prec == image->comps[1].prec
- && image->comps[1].prec == image->comps[2].prec
- )))
- {
+ (ncomp == 2 /* GRAYA */
+ || (ncomp > 2 /* RGB, RGBA */
+ && image->comps[0].dx == image->comps[1].dx
+ && image->comps[1].dx == image->comps[2].dx
+ && image->comps[0].dy == image->comps[1].dy
+ && image->comps[1].dy == image->comps[2].dy
+ && image->comps[0].prec == image->comps[1].prec
+ && image->comps[1].prec == image->comps[2].prec
+ ))) {
fdest = fopen(outfile, "wb");
- if (!fdest)
- {
+ if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
return fails;
}
two = (prec > 8);
triple = (ncomp > 2);
- wr = (int)image->comps[0].w; hr = (int)image->comps[0].h;
- max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
+ wr = (int)image->comps[0].w;
+ hr = (int)image->comps[0].h;
+ max = (1 << prec) - 1;
+ has_alpha = (ncomp == 4 || ncomp == 2);
red = image->comps[0].data;
- if(triple)
- {
+ if (triple) {
green = image->comps[1].data;
blue = image->comps[2].data;
+ } else {
+ green = blue = NULL;
}
- else green = blue = NULL;
- if(has_alpha)
- {
- const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
+ if (has_alpha) {
+ const char *tt = (triple ? "RGB_ALPHA" : "GRAYSCALE_ALPHA");
fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %u\n"
"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 ?
- 1 << (image->comps[ncomp - 1].prec - 1) : 0);
- }
- else
- {
+ 1 << (image->comps[ncomp - 1].prec - 1) : 0);
+ } else {
fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
opj_version(), wr, hr, max);
adjustA = 0;
}
adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- if(triple)
- {
+ if (triple) {
adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+ } else {
+ adjustG = adjustB = 0;
}
- else adjustG = adjustB = 0;
- for(i = 0; i < wr * hr; ++i)
- {
- if(two)
- {
- v = *red + adjustR; ++red;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ for (i = 0; i < wr * hr; ++i) {
+ if (two) {
+ v = *red + adjustR;
+ ++red;
+ if (v > 65535) {
+ v = 65535;
+ } else if (v < 0) {
+ v = 0;
+ }
/* netpbm: */
- fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
-
- if(triple)
- {
- v = *green + adjustG; ++green;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
+
+ if (triple) {
+ v = *green + adjustG;
+ ++green;
+ if (v > 65535) {
+ v = 65535;
+ } else if (v < 0) {
+ v = 0;
+ }
/* netpbm: */
- fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
-
- v = *blue + adjustB; ++blue;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
+
+ v = *blue + adjustB;
+ ++blue;
+ if (v > 65535) {
+ v = 65535;
+ } else if (v < 0) {
+ v = 0;
+ }
/* 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) */
- if(has_alpha)
- {
- v = *alpha + adjustA; ++alpha;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if (has_alpha) {
+ v = *alpha + adjustA;
+ ++alpha;
+ if (v > 65535) {
+ v = 65535;
+ } else if (v < 0) {
+ v = 0;
+ }
/* netpbm: */
- fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
+ fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
}
continue;
- } /* if(two) */
+ } /* if(two) */
/* prec <= 8: */
v = *red++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ if (v > 255) {
+ v = 255;
+ } else if (v < 0) {
+ v = 0;
+ }
fprintf(fdest, "%c", (unsigned char)v);
- if(triple)
- {
+ if (triple) {
v = *green++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ if (v > 255) {
+ v = 255;
+ } else if (v < 0) {
+ v = 0;
+ }
fprintf(fdest, "%c", (unsigned char)v);
v = *blue++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ if (v > 255) {
+ v = 255;
+ } else if (v < 0) {
+ v = 0;
+ }
fprintf(fdest, "%c", (unsigned char)v);
- }
- if(has_alpha)
- {
+ }
+ if (has_alpha) {
v = *alpha++;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ if (v > 255) {
+ v = 255;
+ } else if (v < 0) {
+ v = 0;
+ }
fprintf(fdest, "%c", (unsigned char)v);
}
- } /* for(i */
+ } /* for(i */
- fclose(fdest); return 0;
+ fclose(fdest);
+ return 0;
}
/* YUV or MONO: */
- if (image->numcomps > ncomp)
- {
- fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
- fprintf(stderr," is written to the file\n");
+ if (image->numcomps > ncomp) {
+ fprintf(stderr, "WARNING -> [PGM file] Only the first component\n");
+ fprintf(stderr, " is written to the file\n");
}
destname = (char*)malloc(strlen(outfile) + 8);
- if(destname == NULL){
+ if (destname == NULL) {
fprintf(stderr, "imagetopnm: memory out\n");
return 1;
}
- for (compno = 0; compno < ncomp; compno++)
- {
- if (ncomp > 1)
- {
+ for (compno = 0; compno < ncomp; compno++) {
+ if (ncomp > 1) {
/*sprintf(destname, "%d.%s", compno, outfile);*/
const size_t olen = strlen(outfile);
const size_t dotpos = olen - 4;
strncpy(destname, outfile, dotpos);
- sprintf(destname+dotpos, "_%u.pgm", compno);
- }
- else
+ sprintf(destname + dotpos, "_%u.pgm", compno);
+ } else {
sprintf(destname, "%s", outfile);
+ }
fdest = fopen(destname, "wb");
- if (!fdest)
- {
+ if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
free(destname);
return 1;
}
- wr = (int)image->comps[compno].w; hr = (int)image->comps[compno].h;
+ wr = (int)image->comps[compno].w;
+ hr = (int)image->comps[compno].h;
prec = (int)image->comps[compno].prec;
- max = (1<<prec) - 1;
+ max = (1 << prec) - 1;
fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
opj_version(), wr, hr, max);
red = image->comps[compno].data;
adjustR =
- (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
-
- if(prec > 8)
- {
- for (i = 0; i < wr * hr; i++)
- {
- v = *red + adjustR; ++red;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
+
+ if (prec > 8) {
+ for (i = 0; i < wr * hr; i++) {
+ v = *red + adjustR;
+ ++red;
+ if (v > 65535) {
+ v = 65535;
+ } else if (v < 0) {
+ v = 0;
+ }
/* 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)
- {
+ if (has_alpha) {
v = *alpha++;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
+ if (v > 65535) {
+ v = 65535;
+ } else if (v < 0) {
+ v = 0;
+ }
/* netpbm: */
- fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
+ fprintf(fdest, "%c%c", (unsigned char)(v >> 8), (unsigned char)v);
}
}/* for(i */
- }
- else /* prec <= 8 */
- {
- for(i = 0; i < wr * hr; ++i)
- {
- v = *red + adjustR; ++red;
- if(v > 255) v = 255; else if(v < 0) v = 0;
+ } else { /* prec <= 8 */
+ for (i = 0; i < wr * hr; ++i) {
+ v = *red + adjustR;
+ ++red;
+ if (v > 255) {
+ v = 255;
+ } else if (v < 0) {
+ v = 0;
+ }
fprintf(fdest, "%c", (unsigned char)v);
}
@@ -1978,7 +2168,9 @@ 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;
@@ -1989,22 +2181,25 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
opj_image_t * image = NULL;
unsigned short ch;
- if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
- {
- fprintf(stderr,"\nError: invalid raw image parameters\n");
- fprintf(stderr,"Please use the Format option -F:\n");
- fprintf(stderr,"-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
- fprintf(stderr,"If subsampling is omitted, 1x1 is assumed for all components\n");
- fprintf(stderr,"Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
- fprintf(stderr," for raw 512x512 image with 4:2:0 subsampling\n");
- fprintf(stderr,"Aborting.\n");
+ if ((!(raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp &
+ raw_cp->rawBitDepth)) == 0) {
+ fprintf(stderr, "\nError: invalid raw image parameters\n");
+ fprintf(stderr, "Please use the Format option -F:\n");
+ fprintf(stderr,
+ "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
+ fprintf(stderr,
+ "If subsampling is omitted, 1x1 is assumed for all components\n");
+ fprintf(stderr,
+ "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
+ fprintf(stderr, " for raw 512x512 image with 4:2:0 subsampling\n");
+ fprintf(stderr, "Aborting.\n");
return NULL;
}
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Failed to open %s for reading !!\n", filename);
- fprintf(stderr,"Aborting\n");
+ fprintf(stderr, "Aborting\n");
return NULL;
}
numcomps = raw_cp->rawComp;
@@ -2021,15 +2216,16 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
}
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");
+ fprintf(stderr, "Aborting\n");
fclose(f);
return NULL;
}
/* initialize image components */
- for(i = 0; i < numcomps; i++) {
+ 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;
@@ -2041,88 +2237,91 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
/* create the image */
image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
free(cmptparm);
- if(!image) {
+ 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->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) *
+ (OPJ_UINT32)subsampling_dx + 1;
+ image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) *
+ (OPJ_UINT32)subsampling_dy + 1;
- if(raw_cp->rawBitDepth <= 8)
- {
+ if (raw_cp->rawBitDepth <= 8) {
unsigned char value = 0;
- for(compno = 0; compno < numcomps; compno++) {
- int nloop = (w*h)/(raw_cp->rawComps[compno].dx*raw_cp->rawComps[compno].dy);
+ for (compno = 0; compno < numcomps; compno++) {
+ int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
+ raw_cp->rawComps[compno].dy);
for (i = 0; i < nloop; i++) {
if (!fread(&value, 1, 1, f)) {
- fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
opj_image_destroy(image);
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)
- {
+ } else if (raw_cp->rawBitDepth <= 16) {
unsigned short value;
- for(compno = 0; compno < numcomps; compno++) {
- int nloop = (w*h)/(raw_cp->rawComps[compno].dx*raw_cp->rawComps[compno].dy);
+ for (compno = 0; compno < numcomps; compno++) {
+ int nloop = (w * h) / (raw_cp->rawComps[compno].dx *
+ raw_cp->rawComps[compno].dy);
for (i = 0; i < nloop; i++) {
unsigned char temp1;
unsigned char temp2;
if (!fread(&temp1, 1, 1, f)) {
- fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
if (!fread(&temp2, 1, 1, f)) {
- fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ fprintf(stderr, "Error reading raw file. End of file probably reached.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
- if( big_endian )
- {
+ if (big_endian) {
value = (unsigned short)((temp1 << 8) + temp2);
- }
- else
- {
+ } else {
value = (unsigned short)((temp2 << 8) + temp1);
}
- image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
+ image->comps[compno].data[i] = raw_cp->rawSigned ? (short)value : value;
}
}
- }
- else {
- fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
+ } else {
+ fprintf(stderr,
+ "OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
opj_image_destroy(image);
fclose(f);
return NULL;
}
if (fread(&ch, 1, 1, f)) {
- fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
+ fprintf(stderr, "Warning. End of raw file not reached... processing anyway\n");
}
fclose(f);
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;
@@ -2133,9 +2332,8 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
unsigned char uc;
(void)big_endian;
- if((image->numcomps * image->x1 * image->y1) == 0)
- {
- fprintf(stderr,"\nError: invalid raw image parameters\n");
+ if ((image->numcomps * image->x1 * image->y1) == 0) {
+ fprintf(stderr, "\nError: invalid raw image parameters\n");
return 1;
}
@@ -2146,47 +2344,52 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
}
fails = 1;
- fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
+ 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");
+ for (compno = 0; compno < image->numcomps; compno++) {
+ fprintf(stdout, "Component %u characteristics: %dx%dx%d %s\n", compno,
+ image->comps[compno].w,
+ image->comps[compno].h, image->comps[compno].prec,
+ image->comps[compno].sgnd == 1 ? "signed" : "unsigned");
w = (int)image->comps[compno].w;
h = (int)image->comps[compno].h;
- if(image->comps[compno].prec <= 8)
- {
- if(image->comps[compno].sgnd == 1)
- {
+ if (image->comps[compno].prec <= 8) {
+ if (image->comps[compno].sgnd == 1) {
mask = (1 << image->comps[compno].prec) - 1;
ptr = image->comps[compno].data;
for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
+ for (row = 0; row < w; row++) {
curr = *ptr;
- if(curr > 127) curr = 127; else if(curr < -128) curr = -128;
- uc = (unsigned char) (curr & mask);
+ if (curr > 127) {
+ curr = 127;
+ } else if (curr < -128) {
+ curr = -128;
+ }
+ uc = (unsigned char)(curr & mask);
res = fwrite(&uc, 1, 1, rawFile);
- if( res < 1 ) {
+ if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
goto fin;
}
ptr++;
}
}
- }
- else if(image->comps[compno].sgnd == 0)
- {
+ } else if (image->comps[compno].sgnd == 0) {
mask = (1 << image->comps[compno].prec) - 1;
ptr = image->comps[compno].data;
for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
+ for (row = 0; row < w; row++) {
curr = *ptr;
- if(curr > 255) curr = 255; else if(curr < 0) curr = 0;
- uc = (unsigned char) (curr & mask);
+ if (curr > 255) {
+ curr = 255;
+ } else if (curr < 0) {
+ curr = 0;
+ }
+ uc = (unsigned char)(curr & mask);
res = fwrite(&uc, 1, 1, rawFile);
- if( res < 1 ) {
+ if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
goto fin;
}
@@ -2194,40 +2397,49 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
}
}
}
- }
- else if(image->comps[compno].prec <= 16)
- {
- if(image->comps[compno].sgnd == 1)
- {
- union { signed short val; signed char vals[2]; } uc16;
+ } else if (image->comps[compno].prec <= 16) {
+ if (image->comps[compno].sgnd == 1) {
+ union {
+ signed short val;
+ signed char vals[2];
+ } uc16;
mask = (1 << image->comps[compno].prec) - 1;
ptr = image->comps[compno].data;
for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
+ for (row = 0; row < w; row++) {
curr = *ptr;
- if(curr > 32767 ) curr = 32767; else if( curr < -32768) curr = -32768;
+ if (curr > 32767) {
+ curr = 32767;
+ } else if (curr < -32768) {
+ curr = -32768;
+ }
uc16.val = (signed short)(curr & mask);
res = fwrite(uc16.vals, 1, 2, rawFile);
- if( res < 2 ) {
+ if (res < 2) {
fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
goto fin;
}
ptr++;
}
}
- }
- else if(image->comps[compno].sgnd == 0)
- {
- union { unsigned short val; unsigned char vals[2]; } uc16;
+ } else if (image->comps[compno].sgnd == 0) {
+ union {
+ unsigned short val;
+ unsigned char vals[2];
+ } uc16;
mask = (1 << image->comps[compno].prec) - 1;
ptr = image->comps[compno].data;
for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
+ for (row = 0; row < w; row++) {
curr = *ptr;
- if(curr > 65535 ) curr = 65535; else if( curr < 0) curr = 0;
+ if (curr > 65535) {
+ curr = 65535;
+ } else if (curr < 0) {
+ curr = 0;
+ }
uc16.val = (unsigned short)(curr & mask);
res = fwrite(uc16.vals, 1, 2, rawFile);
- if( res < 2 ) {
+ if (res < 2) {
fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
goto fin;
}
@@ -2235,19 +2447,15 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
}
}
}
- }
- else if (image->comps[compno].prec <= 32)
- {
- fprintf(stderr,"More than 16 bits per component no handled yet\n");
+ } else if (image->comps[compno].prec <= 32) {
+ fprintf(stderr, "More than 16 bits per component no handled yet\n");
goto fin;
- }
- else
- {
- fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
+ } else {
+ fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
goto fin;
}
}
- fails = 0;
+ fails = 0;
fin:
fclose(rawFile);
return fails;
diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c
index ae83077c..0bc9c9f3 100644
--- a/src/bin/jp2/convertbmp.c
+++ b/src/bin/jp2/convertbmp.c
@@ -1,6 +1,6 @@
/*
- * The copyright in this software is being made available under the 2-clauses
- * BSD License, included below. This software may be subject to other third
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
@@ -8,7 +8,7 @@
* Copyright (c) 2002-2014, Professor Benoit Macq
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
* Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* Copyright (c) 2006-2007, Parvatha Elangovan
@@ -46,761 +46,786 @@
#include "convert.h"
typedef struct {
- OPJ_UINT16 bfType; /* 'BM' for Bitmap (19776) */
- OPJ_UINT32 bfSize; /* Size of the file */
- OPJ_UINT16 bfReserved1; /* Reserved : 0 */
- OPJ_UINT16 bfReserved2; /* Reserved : 0 */
- OPJ_UINT32 bfOffBits; /* Offset */
+ OPJ_UINT16 bfType; /* 'BM' for Bitmap (19776) */
+ OPJ_UINT32 bfSize; /* Size of the file */
+ OPJ_UINT16 bfReserved1; /* Reserved : 0 */
+ OPJ_UINT16 bfReserved2; /* Reserved : 0 */
+ OPJ_UINT32 bfOffBits; /* Offset */
} OPJ_BITMAPFILEHEADER;
typedef struct {
- OPJ_UINT32 biSize; /* Size of the structure in bytes */
- OPJ_UINT32 biWidth; /* Width of the image in pixels */
- OPJ_UINT32 biHeight; /* Heigth of the image in pixels */
- OPJ_UINT16 biPlanes; /* 1 */
- OPJ_UINT16 biBitCount; /* Number of color bits by pixels */
- OPJ_UINT32 biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
- OPJ_UINT32 biSizeImage; /* Size of the image in bytes */
- OPJ_UINT32 biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
- OPJ_UINT32 biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
- OPJ_UINT32 biClrUsed; /* Number of color used in the image (0: ALL) */
- OPJ_UINT32 biClrImportant; /* Number of important color (0: ALL) */
- OPJ_UINT32 biRedMask; /* Red channel bit mask */
- OPJ_UINT32 biGreenMask; /* Green channel bit mask */
- OPJ_UINT32 biBlueMask; /* Blue channel bit mask */
- OPJ_UINT32 biAlphaMask; /* Alpha channel bit mask */
- OPJ_UINT32 biColorSpaceType; /* Color space type */
- OPJ_UINT8 biColorSpaceEP[36]; /* Color space end points */
- OPJ_UINT32 biRedGamma; /* Red channel gamma */
- OPJ_UINT32 biGreenGamma; /* Green channel gamma */
- OPJ_UINT32 biBlueGamma; /* Blue channel gamma */
- OPJ_UINT32 biIntent; /* Intent */
- OPJ_UINT32 biIccProfileData; /* ICC profile data */
- OPJ_UINT32 biIccProfileSize; /* ICC profile size */
- OPJ_UINT32 biReserved; /* Reserved */
+ OPJ_UINT32 biSize; /* Size of the structure in bytes */
+ OPJ_UINT32 biWidth; /* Width of the image in pixels */
+ OPJ_UINT32 biHeight; /* Heigth of the image in pixels */
+ OPJ_UINT16 biPlanes; /* 1 */
+ OPJ_UINT16 biBitCount; /* Number of color bits by pixels */
+ OPJ_UINT32 biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */
+ OPJ_UINT32 biSizeImage; /* Size of the image in bytes */
+ OPJ_UINT32 biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */
+ OPJ_UINT32 biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */
+ OPJ_UINT32 biClrUsed; /* Number of color used in the image (0: ALL) */
+ OPJ_UINT32 biClrImportant; /* Number of important color (0: ALL) */
+ OPJ_UINT32 biRedMask; /* Red channel bit mask */
+ OPJ_UINT32 biGreenMask; /* Green channel bit mask */
+ OPJ_UINT32 biBlueMask; /* Blue channel bit mask */
+ OPJ_UINT32 biAlphaMask; /* Alpha channel bit mask */
+ OPJ_UINT32 biColorSpaceType; /* Color space type */
+ OPJ_UINT8 biColorSpaceEP[36]; /* Color space end points */
+ OPJ_UINT32 biRedGamma; /* Red channel gamma */
+ OPJ_UINT32 biGreenGamma; /* Green channel gamma */
+ OPJ_UINT32 biBlueGamma; /* Blue channel gamma */
+ OPJ_UINT32 biIntent; /* Intent */
+ OPJ_UINT32 biIccProfileData; /* ICC profile data */
+ OPJ_UINT32 biIccProfileSize; /* ICC profile size */
+ OPJ_UINT32 biReserved; /* Reserved */
} OPJ_BITMAPINFOHEADER;
static void opj_applyLUT8u_8u32s_C1R(
- OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride,
- OPJ_INT32* pDst, OPJ_INT32 dstStride,
- OPJ_UINT8 const* pLUT,
- OPJ_UINT32 width, OPJ_UINT32 height)
+ OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride,
+ OPJ_INT32* pDst, OPJ_INT32 dstStride,
+ OPJ_UINT8 const* pLUT,
+ OPJ_UINT32 width, OPJ_UINT32 height)
{
- OPJ_UINT32 y;
-
- for (y = height; y != 0U; --y) {
- OPJ_UINT32 x;
-
- for(x = 0; x < width; x++)
- {
- pDst[x] = (OPJ_INT32)pLUT[pSrc[x]];
- }
- pSrc += srcStride;
- pDst += dstStride;
- }
+ OPJ_UINT32 y;
+
+ for (y = height; y != 0U; --y) {
+ OPJ_UINT32 x;
+
+ for (x = 0; x < width; x++) {
+ pDst[x] = (OPJ_INT32)pLUT[pSrc[x]];
+ }
+ pSrc += srcStride;
+ pDst += dstStride;
+ }
}
static void opj_applyLUT8u_8u32s_C1P3R(
- OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride,
- OPJ_INT32* const* pDst, OPJ_INT32 const* pDstStride,
- OPJ_UINT8 const* const* pLUT,
- OPJ_UINT32 width, OPJ_UINT32 height)
+ OPJ_UINT8 const* pSrc, OPJ_INT32 srcStride,
+ OPJ_INT32* const* pDst, OPJ_INT32 const* pDstStride,
+ OPJ_UINT8 const* const* pLUT,
+ OPJ_UINT32 width, OPJ_UINT32 height)
{
- OPJ_UINT32 y;
- OPJ_INT32* pR = pDst[0];
- OPJ_INT32* pG = pDst[1];
- OPJ_INT32* pB = pDst[2];
- OPJ_UINT8 const* pLUT_R = pLUT[0];
- OPJ_UINT8 const* pLUT_G = pLUT[1];
- OPJ_UINT8 const* pLUT_B = pLUT[2];
-
- for (y = height; y != 0U; --y) {
- OPJ_UINT32 x;
-
- for(x = 0; x < width; x++)
- {
- OPJ_UINT8 idx = pSrc[x];
- pR[x] = (OPJ_INT32)pLUT_R[idx];
- pG[x] = (OPJ_INT32)pLUT_G[idx];
- pB[x] = (OPJ_INT32)pLUT_B[idx];
- }
- pSrc += srcStride;
- pR += pDstStride[0];
- pG += pDstStride[1];
- pB += pDstStride[2];
- }
+ OPJ_UINT32 y;
+ OPJ_INT32* pR = pDst[0];
+ OPJ_INT32* pG = pDst[1];
+ OPJ_INT32* pB = pDst[2];
+ OPJ_UINT8 const* pLUT_R = pLUT[0];
+ OPJ_UINT8 const* pLUT_G = pLUT[1];
+ OPJ_UINT8 const* pLUT_B = pLUT[2];
+
+ for (y = height; y != 0U; --y) {
+ OPJ_UINT32 x;
+
+ for (x = 0; x < width; x++) {
+ OPJ_UINT8 idx = pSrc[x];
+ pR[x] = (OPJ_INT32)pLUT_R[idx];
+ pG[x] = (OPJ_INT32)pLUT_G[idx];
+ pB[x] = (OPJ_INT32)pLUT_B[idx];
+ }
+ pSrc += srcStride;
+ pR += pDstStride[0];
+ pG += pDstStride[1];
+ pB += pDstStride[2];
+ }
}
-static void bmp24toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image)
+static void bmp24toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
+ opj_image_t* image)
{
- int index;
- OPJ_UINT32 width, height;
- OPJ_UINT32 x, y;
- const OPJ_UINT8 *pSrc = NULL;
-
- width = image->comps[0].w;
- height = image->comps[0].h;
-
- index = 0;
- pSrc = pData + (height - 1U) * stride;
- for(y = 0; y < height; y++)
- {
- for(x = 0; x < width; x++)
- {
- image->comps[0].data[index] = (OPJ_INT32)pSrc[3*x+2]; /* R */
- image->comps[1].data[index] = (OPJ_INT32)pSrc[3*x+1]; /* G */
- image->comps[2].data[index] = (OPJ_INT32)pSrc[3*x+0]; /* B */
- index++;
- }
- pSrc -= stride;
- }
+ int index;
+ OPJ_UINT32 width, height;
+ OPJ_UINT32 x, y;
+ const OPJ_UINT8 *pSrc = NULL;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ index = 0;
+ pSrc = pData + (height - 1U) * stride;
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ image->comps[0].data[index] = (OPJ_INT32)pSrc[3 * x + 2]; /* R */
+ image->comps[1].data[index] = (OPJ_INT32)pSrc[3 * x + 1]; /* G */
+ image->comps[2].data[index] = (OPJ_INT32)pSrc[3 * x + 0]; /* B */
+ index++;
+ }
+ pSrc -= stride;
+ }
}
-static void bmp_mask_get_shift_and_prec(OPJ_UINT32 mask, OPJ_UINT32* shift, OPJ_UINT32* prec)
+static void bmp_mask_get_shift_and_prec(OPJ_UINT32 mask, OPJ_UINT32* shift,
+ OPJ_UINT32* prec)
{
- OPJ_UINT32 l_shift, l_prec;
-
- l_shift = l_prec = 0U;
-
- if (mask != 0U) {
- while ((mask & 1U) == 0U) {
- mask >>= 1;
- l_shift++;
- }
- while (mask & 1U) {
- mask >>= 1;
- l_prec++;
- }
- }
- *shift = l_shift; *prec = l_prec;
+ OPJ_UINT32 l_shift, l_prec;
+
+ l_shift = l_prec = 0U;
+
+ if (mask != 0U) {
+ while ((mask & 1U) == 0U) {
+ mask >>= 1;
+ l_shift++;
+ }
+ while (mask & 1U) {
+ mask >>= 1;
+ l_prec++;
+ }
+ }
+ *shift = l_shift;
+ *prec = l_prec;
}
-static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask, OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask)
+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 greenShift, greenPrec;
- OPJ_UINT32 blueShift, bluePrec;
- OPJ_UINT32 alphaShift, alphaPrec;
-
- width = image->comps[0].w;
- height = image->comps[0].h;
-
- hasAlpha = image->numcomps > 3U;
-
- bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec);
- bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
- bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec);
- bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
-
- image->comps[0].bpp = redPrec;
- image->comps[0].prec = redPrec;
- image->comps[1].bpp = greenPrec;
- image->comps[1].prec = greenPrec;
- image->comps[2].bpp = bluePrec;
- image->comps[2].prec = bluePrec;
- if (hasAlpha) {
- image->comps[3].bpp = alphaPrec;
- image->comps[3].prec = alphaPrec;
- }
-
- index = 0;
- pSrc = pData + (height - 1U) * stride;
- for(y = 0; y < height; y++)
- {
- for(x = 0; x < width; x++)
- {
- OPJ_UINT32 value = 0U;
-
- value |= ((OPJ_UINT32)pSrc[4*x+0]) << 0;
- value |= ((OPJ_UINT32)pSrc[4*x+1]) << 8;
- value |= ((OPJ_UINT32)pSrc[4*x+2]) << 16;
- value |= ((OPJ_UINT32)pSrc[4*x+3]) << 24;
-
- image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >> redShift); /* R */
- image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >> greenShift); /* G */
- image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >> blueShift); /* B */
- if (hasAlpha) {
- image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >> alphaShift); /* A */
- }
- index++;
- }
- pSrc -= stride;
- }
+ int index;
+ OPJ_UINT32 width, height;
+ OPJ_UINT32 x, y;
+ const OPJ_UINT8 *pSrc = NULL;
+ OPJ_BOOL hasAlpha;
+ OPJ_UINT32 redShift, redPrec;
+ OPJ_UINT32 greenShift, greenPrec;
+ OPJ_UINT32 blueShift, bluePrec;
+ OPJ_UINT32 alphaShift, alphaPrec;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ hasAlpha = image->numcomps > 3U;
+
+ bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec);
+ bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
+ bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec);
+ bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
+
+ image->comps[0].bpp = redPrec;
+ image->comps[0].prec = redPrec;
+ image->comps[1].bpp = greenPrec;
+ image->comps[1].prec = greenPrec;
+ image->comps[2].bpp = bluePrec;
+ image->comps[2].prec = bluePrec;
+ if (hasAlpha) {
+ image->comps[3].bpp = alphaPrec;
+ image->comps[3].prec = alphaPrec;
+ }
+
+ index = 0;
+ pSrc = pData + (height - 1U) * stride;
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ OPJ_UINT32 value = 0U;
+
+ value |= ((OPJ_UINT32)pSrc[4 * x + 0]) << 0;
+ value |= ((OPJ_UINT32)pSrc[4 * x + 1]) << 8;
+ value |= ((OPJ_UINT32)pSrc[4 * x + 2]) << 16;
+ value |= ((OPJ_UINT32)pSrc[4 * x + 3]) << 24;
+
+ image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >>
+ redShift); /* R */
+ image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >>
+ greenShift); /* G */
+ image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >>
+ blueShift); /* B */
+ if (hasAlpha) {
+ image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >>
+ alphaShift); /* A */
+ }
+ index++;
+ }
+ pSrc -= stride;
+ }
}
-static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask, OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask)
+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 greenShift, greenPrec;
- OPJ_UINT32 blueShift, bluePrec;
- OPJ_UINT32 alphaShift, alphaPrec;
-
- width = image->comps[0].w;
- height = image->comps[0].h;
-
- hasAlpha = image->numcomps > 3U;
-
- bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec);
- bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
- bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec);
- bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
-
- image->comps[0].bpp = redPrec;
- image->comps[0].prec = redPrec;
- image->comps[1].bpp = greenPrec;
- image->comps[1].prec = greenPrec;
- image->comps[2].bpp = bluePrec;
- image->comps[2].prec = bluePrec;
- if (hasAlpha) {
- image->comps[3].bpp = alphaPrec;
- image->comps[3].prec = alphaPrec;
- }
-
- index = 0;
- pSrc = pData + (height - 1U) * stride;
- for(y = 0; y < height; y++)
- {
- for(x = 0; x < width; x++)
- {
- OPJ_UINT32 value = 0U;
-
- value |= ((OPJ_UINT32)pSrc[2*x+0]) << 0;
- value |= ((OPJ_UINT32)pSrc[2*x+1]) << 8;
-
- image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >> redShift); /* R */
- image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >> greenShift); /* G */
- image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >> blueShift); /* B */
- if (hasAlpha) {
- image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >> alphaShift); /* A */
- }
- index++;
- }
- pSrc -= stride;
- }
+ int index;
+ OPJ_UINT32 width, height;
+ OPJ_UINT32 x, y;
+ const OPJ_UINT8 *pSrc = NULL;
+ OPJ_BOOL hasAlpha;
+ OPJ_UINT32 redShift, redPrec;
+ OPJ_UINT32 greenShift, greenPrec;
+ OPJ_UINT32 blueShift, bluePrec;
+ OPJ_UINT32 alphaShift, alphaPrec;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ hasAlpha = image->numcomps > 3U;
+
+ bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec);
+ bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
+ bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec);
+ bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
+
+ image->comps[0].bpp = redPrec;
+ image->comps[0].prec = redPrec;
+ image->comps[1].bpp = greenPrec;
+ image->comps[1].prec = greenPrec;
+ image->comps[2].bpp = bluePrec;
+ image->comps[2].prec = bluePrec;
+ if (hasAlpha) {
+ image->comps[3].bpp = alphaPrec;
+ image->comps[3].prec = alphaPrec;
+ }
+
+ index = 0;
+ pSrc = pData + (height - 1U) * stride;
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ OPJ_UINT32 value = 0U;
+
+ value |= ((OPJ_UINT32)pSrc[2 * x + 0]) << 0;
+ value |= ((OPJ_UINT32)pSrc[2 * x + 1]) << 8;
+
+ image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >>
+ redShift); /* R */
+ image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >>
+ greenShift); /* G */
+ image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >>
+ blueShift); /* B */
+ if (hasAlpha) {
+ image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >>
+ alphaShift); /* A */
+ }
+ index++;
+ }
+ pSrc -= stride;
+ }
}
-static opj_image_t* bmp8toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image, OPJ_UINT8 const* const* pLUT)
+static opj_image_t* bmp8toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
+ opj_image_t* image, OPJ_UINT8 const* const* pLUT)
{
- OPJ_UINT32 width, height;
- const OPJ_UINT8 *pSrc = NULL;
-
- width = image->comps[0].w;
- height = image->comps[0].h;
-
- pSrc = pData + (height - 1U) * stride;
- if (image->numcomps == 1U) {
- opj_applyLUT8u_8u32s_C1R(pSrc, -(OPJ_INT32)stride, image->comps[0].data, (OPJ_INT32)width, pLUT[0], width, height);
- }
- else {
- OPJ_INT32* pDst[3];
- OPJ_INT32 pDstStride[3];
-
- pDst[0] = image->comps[0].data; pDst[1] = image->comps[1].data; pDst[2] = image->comps[2].data;
- pDstStride[0] = (OPJ_INT32)width; pDstStride[1] = (OPJ_INT32)width; pDstStride[2] = (OPJ_INT32)width;
- opj_applyLUT8u_8u32s_C1P3R(pSrc, -(OPJ_INT32)stride, pDst, pDstStride, pLUT, width, height);
- }
- return image;
+ OPJ_UINT32 width, height;
+ const OPJ_UINT8 *pSrc = NULL;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ pSrc = pData + (height - 1U) * stride;
+ if (image->numcomps == 1U) {
+ opj_applyLUT8u_8u32s_C1R(pSrc, -(OPJ_INT32)stride, image->comps[0].data,
+ (OPJ_INT32)width, pLUT[0], width, height);
+ } else {
+ OPJ_INT32* pDst[3];
+ OPJ_INT32 pDstStride[3];
+
+ pDst[0] = image->comps[0].data;
+ pDst[1] = image->comps[1].data;
+ pDst[2] = image->comps[2].data;
+ pDstStride[0] = (OPJ_INT32)width;
+ pDstStride[1] = (OPJ_INT32)width;
+ pDstStride[2] = (OPJ_INT32)width;
+ opj_applyLUT8u_8u32s_C1P3R(pSrc, -(OPJ_INT32)stride, pDst, pDstStride, pLUT,
+ width, height);
+ }
+ return image;
}
static OPJ_BOOL bmp_read_file_header(FILE* IN, OPJ_BITMAPFILEHEADER* header)
{
- header->bfType = (OPJ_UINT16)getc(IN);
- header->bfType |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
-
- if (header->bfType != 19778) {
- fprintf(stderr,"Error, not a BMP file!\n");
- return OPJ_FALSE;
- }
-
- /* FILE HEADER */
- /* ------------- */
- header->bfSize = (OPJ_UINT32)getc(IN);
- header->bfSize |= (OPJ_UINT32)getc(IN) << 8;
- header->bfSize |= (OPJ_UINT32)getc(IN) << 16;
- header->bfSize |= (OPJ_UINT32)getc(IN) << 24;
-
- header->bfReserved1 = (OPJ_UINT16)getc(IN);
- header->bfReserved1 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
-
- header->bfReserved2 = (OPJ_UINT16)getc(IN);
- header->bfReserved2 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
-
- header->bfOffBits = (OPJ_UINT32)getc(IN);
- header->bfOffBits |= (OPJ_UINT32)getc(IN) << 8;
- header->bfOffBits |= (OPJ_UINT32)getc(IN) << 16;
- header->bfOffBits |= (OPJ_UINT32)getc(IN) << 24;
- return OPJ_TRUE;
+ header->bfType = (OPJ_UINT16)getc(IN);
+ header->bfType |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ if (header->bfType != 19778) {
+ fprintf(stderr, "Error, not a BMP file!\n");
+ return OPJ_FALSE;
+ }
+
+ /* FILE HEADER */
+ /* ------------- */
+ header->bfSize = (OPJ_UINT32)getc(IN);
+ header->bfSize |= (OPJ_UINT32)getc(IN) << 8;
+ header->bfSize |= (OPJ_UINT32)getc(IN) << 16;
+ header->bfSize |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->bfReserved1 = (OPJ_UINT16)getc(IN);
+ header->bfReserved1 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ header->bfReserved2 = (OPJ_UINT16)getc(IN);
+ header->bfReserved2 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ header->bfOffBits = (OPJ_UINT32)getc(IN);
+ header->bfOffBits |= (OPJ_UINT32)getc(IN) << 8;
+ header->bfOffBits |= (OPJ_UINT32)getc(IN) << 16;
+ header->bfOffBits |= (OPJ_UINT32)getc(IN) << 24;
+ return OPJ_TRUE;
}
static OPJ_BOOL bmp_read_info_header(FILE* IN, OPJ_BITMAPINFOHEADER* header)
{
- memset(header, 0, sizeof(*header));
- /* INFO HEADER */
- /* ------------- */
- header->biSize = (OPJ_UINT32)getc(IN);
- header->biSize |= (OPJ_UINT32)getc(IN) << 8;
- header->biSize |= (OPJ_UINT32)getc(IN) << 16;
- header->biSize |= (OPJ_UINT32)getc(IN) << 24;
-
- switch (header->biSize) {
- case 12U: /* BITMAPCOREHEADER */
- case 40U: /* BITMAPINFOHEADER */
- case 52U: /* BITMAPV2INFOHEADER */
- case 56U: /* BITMAPV3INFOHEADER */
- case 108U: /* BITMAPV4HEADER */
- case 124U: /* BITMAPV5HEADER */
- break;
- default:
- fprintf(stderr,"Error, unknown BMP header size %d\n", header->biSize);
- return OPJ_FALSE;
- }
-
- header->biWidth = (OPJ_UINT32)getc(IN);
- header->biWidth |= (OPJ_UINT32)getc(IN) << 8;
- header->biWidth |= (OPJ_UINT32)getc(IN) << 16;
- header->biWidth |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biHeight = (OPJ_UINT32)getc(IN);
- header->biHeight |= (OPJ_UINT32)getc(IN) << 8;
- header->biHeight |= (OPJ_UINT32)getc(IN) << 16;
- header->biHeight |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biPlanes = (OPJ_UINT16)getc(IN);
- header->biPlanes |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
-
- header->biBitCount = (OPJ_UINT16)getc(IN);
- header->biBitCount |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
-
- if(header->biSize >= 40U) {
- header->biCompression = (OPJ_UINT32)getc(IN);
- header->biCompression |= (OPJ_UINT32)getc(IN) << 8;
- header->biCompression |= (OPJ_UINT32)getc(IN) << 16;
- header->biCompression |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biSizeImage = (OPJ_UINT32)getc(IN);
- header->biSizeImage |= (OPJ_UINT32)getc(IN) << 8;
- header->biSizeImage |= (OPJ_UINT32)getc(IN) << 16;
- header->biSizeImage |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biXpelsPerMeter = (OPJ_UINT32)getc(IN);
- header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
- header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
- header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biYpelsPerMeter = (OPJ_UINT32)getc(IN);
- header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
- header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
- header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biClrUsed = (OPJ_UINT32)getc(IN);
- header->biClrUsed |= (OPJ_UINT32)getc(IN) << 8;
- header->biClrUsed |= (OPJ_UINT32)getc(IN) << 16;
- header->biClrUsed |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biClrImportant = (OPJ_UINT32)getc(IN);
- header->biClrImportant |= (OPJ_UINT32)getc(IN) << 8;
- header->biClrImportant |= (OPJ_UINT32)getc(IN) << 16;
- header->biClrImportant |= (OPJ_UINT32)getc(IN) << 24;
- }
-
- if(header->biSize >= 56U) {
- header->biRedMask = (OPJ_UINT32)getc(IN);
- header->biRedMask |= (OPJ_UINT32)getc(IN) << 8;
- header->biRedMask |= (OPJ_UINT32)getc(IN) << 16;
- header->biRedMask |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biGreenMask = (OPJ_UINT32)getc(IN);
- header->biGreenMask |= (OPJ_UINT32)getc(IN) << 8;
- header->biGreenMask |= (OPJ_UINT32)getc(IN) << 16;
- header->biGreenMask |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biBlueMask = (OPJ_UINT32)getc(IN);
- header->biBlueMask |= (OPJ_UINT32)getc(IN) << 8;
- header->biBlueMask |= (OPJ_UINT32)getc(IN) << 16;
- header->biBlueMask |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biAlphaMask = (OPJ_UINT32)getc(IN);
- header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 8;
- header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 16;
- header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 24;
- }
-
- if(header->biSize >= 108U) {
- header->biColorSpaceType = (OPJ_UINT32)getc(IN);
- header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 8;
- header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 16;
- header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 24;
-
- if (fread(&(header->biColorSpaceEP), 1U, sizeof(header->biColorSpaceEP), IN) != sizeof(header->biColorSpaceEP)) {
- fprintf(stderr,"Error, can't read BMP header\n");
- return OPJ_FALSE;
- }
-
- header->biRedGamma = (OPJ_UINT32)getc(IN);
- header->biRedGamma |= (OPJ_UINT32)getc(IN) << 8;
- header->biRedGamma |= (OPJ_UINT32)getc(IN) << 16;
- header->biRedGamma |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biGreenGamma = (OPJ_UINT32)getc(IN);
- header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 8;
- header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 16;
- header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biBlueGamma = (OPJ_UINT32)getc(IN);
- header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 8;
- header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 16;
- header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 24;
- }
-
- if(header->biSize >= 124U) {
- header->biIntent = (OPJ_UINT32)getc(IN);
- header->biIntent |= (OPJ_UINT32)getc(IN) << 8;
- header->biIntent |= (OPJ_UINT32)getc(IN) << 16;
- header->biIntent |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biIccProfileData = (OPJ_UINT32)getc(IN);
- header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 8;
- header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 16;
- header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biIccProfileSize = (OPJ_UINT32)getc(IN);
- header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 8;
- header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 16;
- header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 24;
-
- header->biReserved = (OPJ_UINT32)getc(IN);
- header->biReserved |= (OPJ_UINT32)getc(IN) << 8;
- header->biReserved |= (OPJ_UINT32)getc(IN) << 16;
- header->biReserved |= (OPJ_UINT32)getc(IN) << 24;
- }
- return OPJ_TRUE;
+ memset(header, 0, sizeof(*header));
+ /* INFO HEADER */
+ /* ------------- */
+ header->biSize = (OPJ_UINT32)getc(IN);
+ header->biSize |= (OPJ_UINT32)getc(IN) << 8;
+ header->biSize |= (OPJ_UINT32)getc(IN) << 16;
+ header->biSize |= (OPJ_UINT32)getc(IN) << 24;
+
+ switch (header->biSize) {
+ case 12U: /* BITMAPCOREHEADER */
+ case 40U: /* BITMAPINFOHEADER */
+ case 52U: /* BITMAPV2INFOHEADER */
+ case 56U: /* BITMAPV3INFOHEADER */
+ case 108U: /* BITMAPV4HEADER */
+ case 124U: /* BITMAPV5HEADER */
+ break;
+ default:
+ fprintf(stderr, "Error, unknown BMP header size %d\n", header->biSize);
+ return OPJ_FALSE;
+ }
+
+ header->biWidth = (OPJ_UINT32)getc(IN);
+ header->biWidth |= (OPJ_UINT32)getc(IN) << 8;
+ header->biWidth |= (OPJ_UINT32)getc(IN) << 16;
+ header->biWidth |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biHeight = (OPJ_UINT32)getc(IN);
+ header->biHeight |= (OPJ_UINT32)getc(IN) << 8;
+ header->biHeight |= (OPJ_UINT32)getc(IN) << 16;
+ header->biHeight |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biPlanes = (OPJ_UINT16)getc(IN);
+ header->biPlanes |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ header->biBitCount = (OPJ_UINT16)getc(IN);
+ header->biBitCount |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ if (header->biSize >= 40U) {
+ header->biCompression = (OPJ_UINT32)getc(IN);
+ header->biCompression |= (OPJ_UINT32)getc(IN) << 8;
+ header->biCompression |= (OPJ_UINT32)getc(IN) << 16;
+ header->biCompression |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biSizeImage = (OPJ_UINT32)getc(IN);
+ header->biSizeImage |= (OPJ_UINT32)getc(IN) << 8;
+ header->biSizeImage |= (OPJ_UINT32)getc(IN) << 16;
+ header->biSizeImage |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biXpelsPerMeter = (OPJ_UINT32)getc(IN);
+ header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
+ header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
+ header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biYpelsPerMeter = (OPJ_UINT32)getc(IN);
+ header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
+ header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
+ header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biClrUsed = (OPJ_UINT32)getc(IN);
+ header->biClrUsed |= (OPJ_UINT32)getc(IN) << 8;
+ header->biClrUsed |= (OPJ_UINT32)getc(IN) << 16;
+ header->biClrUsed |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biClrImportant = (OPJ_UINT32)getc(IN);
+ header->biClrImportant |= (OPJ_UINT32)getc(IN) << 8;
+ header->biClrImportant |= (OPJ_UINT32)getc(IN) << 16;
+ header->biClrImportant |= (OPJ_UINT32)getc(IN) << 24;
+ }
+
+ if (header->biSize >= 56U) {
+ header->biRedMask = (OPJ_UINT32)getc(IN);
+ header->biRedMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biRedMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biRedMask |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biGreenMask = (OPJ_UINT32)getc(IN);
+ header->biGreenMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biGreenMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biGreenMask |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biBlueMask = (OPJ_UINT32)getc(IN);
+ header->biBlueMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biBlueMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biBlueMask |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biAlphaMask = (OPJ_UINT32)getc(IN);
+ header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 24;
+ }
+
+ if (header->biSize >= 108U) {
+ header->biColorSpaceType = (OPJ_UINT32)getc(IN);
+ header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 8;
+ header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 16;
+ header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 24;
+
+ if (fread(&(header->biColorSpaceEP), 1U, sizeof(header->biColorSpaceEP),
+ IN) != sizeof(header->biColorSpaceEP)) {
+ fprintf(stderr, "Error, can't read BMP header\n");
+ return OPJ_FALSE;
+ }
+
+ header->biRedGamma = (OPJ_UINT32)getc(IN);
+ header->biRedGamma |= (OPJ_UINT32)getc(IN) << 8;
+ header->biRedGamma |= (OPJ_UINT32)getc(IN) << 16;
+ header->biRedGamma |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biGreenGamma = (OPJ_UINT32)getc(IN);
+ header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 8;
+ header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 16;
+ header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biBlueGamma = (OPJ_UINT32)getc(IN);
+ header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 8;
+ header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 16;
+ header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 24;
+ }
+
+ if (header->biSize >= 124U) {
+ header->biIntent = (OPJ_UINT32)getc(IN);
+ header->biIntent |= (OPJ_UINT32)getc(IN) << 8;
+ header->biIntent |= (OPJ_UINT32)getc(IN) << 16;
+ header->biIntent |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biIccProfileData = (OPJ_UINT32)getc(IN);
+ header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 8;
+ header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 16;
+ header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biIccProfileSize = (OPJ_UINT32)getc(IN);
+ header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 8;
+ header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 16;
+ header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biReserved = (OPJ_UINT32)getc(IN);
+ header->biReserved |= (OPJ_UINT32)getc(IN) << 8;
+ header->biReserved |= (OPJ_UINT32)getc(IN) << 16;
+ header->biReserved |= (OPJ_UINT32)getc(IN) << 24;
+ }
+ return OPJ_TRUE;
}
-static OPJ_BOOL bmp_read_raw_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
+static OPJ_BOOL bmp_read_raw_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride,
+ OPJ_UINT32 width, OPJ_UINT32 height)
{
- OPJ_ARG_NOT_USED(width);
-
- if ( fread(pData, sizeof(OPJ_UINT8), stride * height, IN) != (stride * height) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return OPJ_FALSE;
- }
- return OPJ_TRUE;
+ OPJ_ARG_NOT_USED(width);
+
+ if (fread(pData, sizeof(OPJ_UINT8), stride * height, IN) != (stride * height)) {
+ fprintf(stderr,
+ "\nError: fread return a number of element different from the expected.\n");
+ return OPJ_FALSE;
+ }
+ return OPJ_TRUE;
}
-static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
+static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData,
+ OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
{
- OPJ_UINT32 x, y;
- OPJ_UINT8 *pix;
- const OPJ_UINT8 *beyond;
-
- beyond = pData + stride * height;
- pix = pData;
-
- x = y = 0U;
- while (y < height)
- {
- int c = getc(IN);
-
- if (c) {
- int j;
- OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
-
- for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
- *pix = c1;
- }
- }
- else {
- c = getc(IN);
- if (c == 0x00) { /* EOL */
- x = 0;
- ++y;
- pix = pData + y * stride + x;
- }
- else if (c == 0x01) { /* EOP */
- break;
- }
- else if (c == 0x02) { /* MOVE by dxdy */
- c = getc(IN);
- x += (OPJ_UINT32)c;
- c = getc(IN);
- y += (OPJ_UINT32)c;
- pix = pData + y * stride + x;
- }
- else /* 03 .. 255 */
- {
- int j;
- for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++)
- {
- OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
- *pix = c1;
- }
- if ((OPJ_UINT32)c & 1U) { /* skip padding byte */
- getc(IN);
- }
- }
- }
- }/* while() */
- return OPJ_TRUE;
+ OPJ_UINT32 x, y;
+ OPJ_UINT8 *pix;
+ const OPJ_UINT8 *beyond;
+
+ beyond = pData + stride * height;
+ pix = pData;
+
+ x = y = 0U;
+ while (y < height) {
+ int c = getc(IN);
+
+ if (c) {
+ int j;
+ OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
+
+ for (j = 0; (j < c) && (x < width) &&
+ ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
+ *pix = c1;
+ }
+ } else {
+ c = getc(IN);
+ if (c == 0x00) { /* EOL */
+ x = 0;
+ ++y;
+ pix = pData + y * stride + x;
+ } else if (c == 0x01) { /* EOP */
+ break;
+ } else if (c == 0x02) { /* MOVE by dxdy */
+ c = getc(IN);
+ x += (OPJ_UINT32)c;
+ c = getc(IN);
+ y += (OPJ_UINT32)c;
+ pix = pData + y * stride + x;
+ } else { /* 03 .. 255 */
+ int j;
+ for (j = 0; (j < c) && (x < width) &&
+ ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
+ OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
+ *pix = c1;
+ }
+ if ((OPJ_UINT32)c & 1U) { /* skip padding byte */
+ getc(IN);
+ }
+ }
+ }
+ }/* while() */
+ return OPJ_TRUE;
}
-static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
+static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData,
+ OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
{
- OPJ_UINT32 x, y;
- OPJ_UINT8 *pix;
- const OPJ_UINT8 *beyond;
-
- beyond = pData + stride * height;
- pix = pData;
- x = y = 0U;
- while(y < height)
- {
- int c = getc(IN);
- if(c == EOF) break;
-
- if(c) {/* encoded mode */
- int j;
- OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
-
- for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
- *pix = (OPJ_UINT8)((j&1) ? (c1 & 0x0fU) : ((c1>>4)&0x0fU));
- }
- }
- else { /* absolute mode */
- c = getc(IN);
- if(c == EOF) break;
-
- if(c == 0x00) { /* EOL */
- x = 0; y++; pix = pData + y * stride;
- }
- else if(c == 0x01) { /* EOP */
- break;
- }
- else if(c == 0x02) { /* MOVE by dxdy */
- c = getc(IN); x += (OPJ_UINT32)c;
- c = getc(IN); y += (OPJ_UINT32)c;
- pix = pData + y * stride + x;
- }
- else { /* 03 .. 255 : absolute mode */
- int j;
- OPJ_UINT8 c1 = 0U;
-
- for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
- if((j&1) == 0) {
- c1 = (OPJ_UINT8)getc(IN);
- }
- *pix = (OPJ_UINT8)((j&1) ? (c1 & 0x0fU) : ((c1>>4)&0x0fU));
- }
- if(((c&3) == 1) || ((c&3) == 2)) { /* skip padding byte */
- getc(IN);
- }
- }
- }
- } /* while(y < height) */
- return OPJ_TRUE;
+ OPJ_UINT32 x, y;
+ OPJ_UINT8 *pix;
+ const OPJ_UINT8 *beyond;
+
+ beyond = pData + stride * height;
+ pix = pData;
+ x = y = 0U;
+ while (y < height) {
+ int c = getc(IN);
+ if (c == EOF) {
+ break;
+ }
+
+ if (c) { /* encoded mode */
+ int j;
+ OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
+
+ for (j = 0; (j < c) && (x < width) &&
+ ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
+ *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU));
+ }
+ } else { /* absolute mode */
+ c = getc(IN);
+ if (c == EOF) {
+ break;
+ }
+
+ if (c == 0x00) { /* EOL */
+ x = 0;
+ y++;
+ pix = pData + y * stride;
+ } else if (c == 0x01) { /* EOP */
+ break;
+ } else if (c == 0x02) { /* MOVE by dxdy */
+ c = getc(IN);
+ x += (OPJ_UINT32)c;
+ c = getc(IN);
+ y += (OPJ_UINT32)c;
+ pix = pData + y * stride + x;
+ } else { /* 03 .. 255 : absolute mode */
+ int j;
+ OPJ_UINT8 c1 = 0U;
+
+ for (j = 0; (j < c) && (x < width) &&
+ ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
+ if ((j & 1) == 0) {
+ c1 = (OPJ_UINT8)getc(IN);
+ }
+ *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU));
+ }
+ if (((c & 3) == 1) || ((c & 3) == 2)) { /* skip padding byte */
+ getc(IN);
+ }
+ }
+ }
+ } /* while(y < height) */
+ return OPJ_TRUE;
}
opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
{
- opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */
- OPJ_UINT8 lut_R[256], lut_G[256], lut_B[256];
- OPJ_UINT8 const* pLUT[3];
- opj_image_t * image = NULL;
- FILE *IN;
- OPJ_BITMAPFILEHEADER File_h;
- OPJ_BITMAPINFOHEADER Info_h;
- OPJ_UINT32 i, palette_len, numcmpts = 1U;
- OPJ_BOOL l_result = OPJ_FALSE;
- OPJ_UINT8* pData = NULL;
- OPJ_UINT32 stride;
-
- pLUT[0] = lut_R; pLUT[1] = lut_G; pLUT[2] = lut_B;
-
- IN = fopen(filename, "rb");
- if (!IN)
- {
- fprintf(stderr, "Failed to open %s for reading !!\n", filename);
- return NULL;
- }
-
- if (!bmp_read_file_header(IN, &File_h)) {
- fclose(IN);
- return NULL;
- }
- if (!bmp_read_info_header(IN, &Info_h)) {
- fclose(IN);
- return NULL;
- }
-
- /* Load palette */
- if (Info_h.biBitCount <= 8U)
- {
- memset(&lut_R[0], 0, sizeof(lut_R));
- memset(&lut_G[0], 0, sizeof(lut_G));
- memset(&lut_B[0], 0, sizeof(lut_B));
-
- palette_len = Info_h.biClrUsed;
- if((palette_len == 0U) && (Info_h.biBitCount <= 8U)) {
- palette_len = (1U << Info_h.biBitCount);
- }
- if (palette_len > 256U) {
- palette_len = 256U;
- }
- if (palette_len > 0U) {
- OPJ_UINT8 has_color = 0U;
- for (i = 0U; i < palette_len; i++) {
- lut_B[i] = (OPJ_UINT8)getc(IN);
- lut_G[i] = (OPJ_UINT8)getc(IN);
- lut_R[i] = (OPJ_UINT8)getc(IN);
- (void)getc(IN); /* padding */
- has_color |= (lut_B[i] ^ lut_G[i]) | (lut_G[i] ^ lut_R[i]);
- }
- if(has_color) {
- numcmpts = 3U;
- }
- }
- } else {
- numcmpts = 3U;
- if ((Info_h.biCompression == 3) && (Info_h.biAlphaMask != 0U)) {
- numcmpts++;
- }
- }
-
- if (Info_h.biWidth == 0 || Info_h.biHeight == 0) {
- fclose(IN);
- return NULL;
- }
-
- 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) {
- fclose(IN);
- return NULL;
- }
- stride = ((Info_h.biWidth * 8U + 31U) / 32U) * 4U;
- }
-
- if (stride > ((OPJ_UINT32)-1) / sizeof(OPJ_UINT8) / Info_h.biHeight) {
- fclose(IN);
- return NULL;
- }
- pData = (OPJ_UINT8 *) calloc(1, stride * Info_h.biHeight * sizeof(OPJ_UINT8));
- if (pData == NULL) {
- fclose(IN);
- return NULL;
- }
- /* Place the cursor at the beginning of the image information */
- fseek(IN, 0, SEEK_SET);
- fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
-
- switch (Info_h.biCompression) {
- case 0:
- case 3:
- /* read raw data */
- l_result = bmp_read_raw_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight);
- break;
- case 1:
- /* read rle8 data */
- l_result = bmp_read_rle8_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight);
- break;
- case 2:
- /* read rle4 data */
- l_result = bmp_read_rle4_data(IN, pData, stride, Info_h.biWidth, Info_h.biHeight);
- break;
- default:
- fprintf(stderr, "Unsupported BMP compression\n");
- l_result = OPJ_FALSE;
- break;
- }
- if (!l_result) {
- free(pData);
- fclose(IN);
- return NULL;
- }
-
- /* create the image */
- memset(&cmptparm[0], 0, sizeof(cmptparm));
- for(i = 0; i < 4U; i++)
- {
- cmptparm[i].prec = 8;
- cmptparm[i].bpp = 8;
- cmptparm[i].sgnd = 0;
- cmptparm[i].dx = (OPJ_UINT32)parameters->subsampling_dx;
- cmptparm[i].dy = (OPJ_UINT32)parameters->subsampling_dy;
- cmptparm[i].w = Info_h.biWidth;
- cmptparm[i].h = Info_h.biHeight;
- }
-
- image = opj_image_create(numcmpts, &cmptparm[0], (numcmpts == 1U) ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB);
- if(!image) {
- fclose(IN);
- free(pData);
- return NULL;
- }
- if (numcmpts == 4U) {
- image->comps[3].alpha = 1;
- }
-
- /* set image offset and reference grid */
- image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
- image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
- image->x1 = image->x0 + (Info_h.biWidth - 1U) * (OPJ_UINT32)parameters->subsampling_dx + 1U;
- image->y1 = image->y0 + (Info_h.biHeight - 1U) * (OPJ_UINT32)parameters->subsampling_dy + 1U;
-
- /* Read the data */
- if (Info_h.biBitCount == 24 && Info_h.biCompression == 0) { /*RGB */
- bmp24toimage(pData, stride, image);
- }
- else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { /* RGB 8bpp Indexed */
- bmp8toimage(pData, stride, image, pLUT);
- }
- else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
- bmp8toimage(pData, stride, image, pLUT);
- }
- else if (Info_h.biBitCount == 4 && Info_h.biCompression == 2) { /*RLE4*/
- bmp8toimage(pData, stride, image, pLUT); /* RLE 4 gets decoded as 8 bits data for now */
- }
- else if (Info_h.biBitCount == 32 && Info_h.biCompression == 0) { /* RGBX */
- bmpmask32toimage(pData, stride, image, 0x00FF0000U, 0x0000FF00U, 0x000000FFU, 0x00000000U);
- }
- else if (Info_h.biBitCount == 32 && Info_h.biCompression == 3) { /* bitmask */
- bmpmask32toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask);
- }
- else if (Info_h.biBitCount == 16 && Info_h.biCompression == 0) { /* RGBX */
- bmpmask16toimage(pData, stride, image, 0x7C00U, 0x03E0U, 0x001FU, 0x0000U);
- }
- else if (Info_h.biBitCount == 16 && Info_h.biCompression == 3) { /* bitmask */
- if ((Info_h.biRedMask == 0U) && (Info_h.biGreenMask == 0U) && (Info_h.biBlueMask == 0U)) {
- Info_h.biRedMask = 0xF800U;
- Info_h.biGreenMask = 0x07E0U;
- Info_h.biBlueMask = 0x001FU;
- }
- bmpmask16toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask, Info_h.biBlueMask, Info_h.biAlphaMask);
- }
- else {
- opj_image_destroy(image);
- image = NULL;
- fprintf(stderr, "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
- }
- free(pData);
- fclose(IN);
- return image;
+ opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */
+ OPJ_UINT8 lut_R[256], lut_G[256], lut_B[256];
+ OPJ_UINT8 const* pLUT[3];
+ opj_image_t * image = NULL;
+ FILE *IN;
+ OPJ_BITMAPFILEHEADER File_h;
+ OPJ_BITMAPINFOHEADER Info_h;
+ OPJ_UINT32 i, palette_len, numcmpts = 1U;
+ OPJ_BOOL l_result = OPJ_FALSE;
+ OPJ_UINT8* pData = NULL;
+ OPJ_UINT32 stride;
+
+ pLUT[0] = lut_R;
+ pLUT[1] = lut_G;
+ pLUT[2] = lut_B;
+
+ IN = fopen(filename, "rb");
+ if (!IN) {
+ fprintf(stderr, "Failed to open %s for reading !!\n", filename);
+ return NULL;
+ }
+
+ if (!bmp_read_file_header(IN, &File_h)) {
+ fclose(IN);
+ return NULL;
+ }
+ if (!bmp_read_info_header(IN, &Info_h)) {
+ fclose(IN);
+ return NULL;
+ }
+
+ /* Load palette */
+ if (Info_h.biBitCount <= 8U) {
+ memset(&lut_R[0], 0, sizeof(lut_R));
+ memset(&lut_G[0], 0, sizeof(lut_G));
+ memset(&lut_B[0], 0, sizeof(lut_B));
+
+ palette_len = Info_h.biClrUsed;
+ if ((palette_len == 0U) && (Info_h.biBitCount <= 8U)) {
+ palette_len = (1U << Info_h.biBitCount);
+ }
+ if (palette_len > 256U) {
+ palette_len = 256U;
+ }
+ if (palette_len > 0U) {
+ OPJ_UINT8 has_color = 0U;
+ for (i = 0U; i < palette_len; i++) {
+ lut_B[i] = (OPJ_UINT8)getc(IN);
+ lut_G[i] = (OPJ_UINT8)getc(IN);
+ lut_R[i] = (OPJ_UINT8)getc(IN);
+ (void)getc(IN); /* padding */
+ has_color |= (lut_B[i] ^ lut_G[i]) | (lut_G[i] ^ lut_R[i]);
+ }
+ if (has_color) {
+ numcmpts = 3U;
+ }
+ }
+ } else {
+ numcmpts = 3U;
+ if ((Info_h.biCompression == 3) && (Info_h.biAlphaMask != 0U)) {
+ numcmpts++;
+ }
+ }
+
+ if (Info_h.biWidth == 0 || Info_h.biHeight == 0) {
+ fclose(IN);
+ return NULL;
+ }
+
+ 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) {
+ fclose(IN);
+ return NULL;
+ }
+ stride = ((Info_h.biWidth * 8U + 31U) / 32U) * 4U;
+ }
+
+ if (stride > ((OPJ_UINT32) - 1) / sizeof(OPJ_UINT8) / Info_h.biHeight) {
+ fclose(IN);
+ return NULL;
+ }
+ pData = (OPJ_UINT8 *) calloc(1, stride * Info_h.biHeight * sizeof(OPJ_UINT8));
+ if (pData == NULL) {
+ fclose(IN);
+ return NULL;
+ }
+ /* Place the cursor at the beginning of the image information */
+ fseek(IN, 0, SEEK_SET);
+ fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
+
+ switch (Info_h.biCompression) {
+ case 0:
+ case 3:
+ /* read raw data */
+ l_result = bmp_read_raw_data(IN, pData, stride, Info_h.biWidth,
+ Info_h.biHeight);
+ break;
+ case 1:
+ /* read rle8 data */
+ l_result = bmp_read_rle8_data(IN, pData, stride, Info_h.biWidth,
+ Info_h.biHeight);
+ break;
+ case 2:
+ /* read rle4 data */
+ l_result = bmp_read_rle4_data(IN, pData, stride, Info_h.biWidth,
+ Info_h.biHeight);
+ break;
+ default:
+ fprintf(stderr, "Unsupported BMP compression\n");
+ l_result = OPJ_FALSE;
+ break;
+ }
+ if (!l_result) {
+ free(pData);
+ fclose(IN);
+ return NULL;
+ }
+
+ /* create the image */
+ memset(&cmptparm[0], 0, sizeof(cmptparm));
+ for (i = 0; i < 4U; i++) {
+ cmptparm[i].prec = 8;
+ cmptparm[i].bpp = 8;
+ cmptparm[i].sgnd = 0;
+ cmptparm[i].dx = (OPJ_UINT32)parameters->subsampling_dx;
+ cmptparm[i].dy = (OPJ_UINT32)parameters->subsampling_dy;
+ cmptparm[i].w = Info_h.biWidth;
+ cmptparm[i].h = Info_h.biHeight;
+ }
+
+ image = opj_image_create(numcmpts, &cmptparm[0],
+ (numcmpts == 1U) ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB);
+ if (!image) {
+ fclose(IN);
+ free(pData);
+ return NULL;
+ }
+ if (numcmpts == 4U) {
+ image->comps[3].alpha = 1;
+ }
+
+ /* set image offset and reference grid */
+ image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+ image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+ image->x1 = image->x0 + (Info_h.biWidth - 1U) * (OPJ_UINT32)
+ parameters->subsampling_dx + 1U;
+ image->y1 = image->y0 + (Info_h.biHeight - 1U) * (OPJ_UINT32)
+ parameters->subsampling_dy + 1U;
+
+ /* Read the data */
+ if (Info_h.biBitCount == 24 && Info_h.biCompression == 0) { /*RGB */
+ bmp24toimage(pData, stride, image);
+ } else if (Info_h.biBitCount == 8 &&
+ Info_h.biCompression == 0) { /* RGB 8bpp Indexed */
+ bmp8toimage(pData, stride, image, pLUT);
+ } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { /*RLE8*/
+ bmp8toimage(pData, stride, image, pLUT);
+ } else if (Info_h.biBitCount == 4 && Info_h.biCompression == 2) { /*RLE4*/
+ bmp8toimage(pData, stride, image,
+ pLUT); /* RLE 4 gets decoded as 8 bits data for now */
+ } else if (Info_h.biBitCount == 32 && Info_h.biCompression == 0) { /* RGBX */
+ bmpmask32toimage(pData, stride, image, 0x00FF0000U, 0x0000FF00U, 0x000000FFU,
+ 0x00000000U);
+ } else if (Info_h.biBitCount == 32 && Info_h.biCompression == 3) { /* bitmask */
+ bmpmask32toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask,
+ Info_h.biBlueMask, Info_h.biAlphaMask);
+ } else if (Info_h.biBitCount == 16 && Info_h.biCompression == 0) { /* RGBX */
+ bmpmask16toimage(pData, stride, image, 0x7C00U, 0x03E0U, 0x001FU, 0x0000U);
+ } else if (Info_h.biBitCount == 16 && Info_h.biCompression == 3) { /* bitmask */
+ if ((Info_h.biRedMask == 0U) && (Info_h.biGreenMask == 0U) &&
+ (Info_h.biBlueMask == 0U)) {
+ Info_h.biRedMask = 0xF800U;
+ Info_h.biGreenMask = 0x07E0U;
+ Info_h.biBlueMask = 0x001FU;
+ }
+ bmpmask16toimage(pData, stride, image, Info_h.biRedMask, Info_h.biGreenMask,
+ Info_h.biBlueMask, Info_h.biAlphaMask);
+ } else {
+ opj_image_destroy(image);
+ image = NULL;
+ fprintf(stderr,
+ "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
+ Info_h.biBitCount);
+ }
+ free(pData);
+ fclose(IN);
+ return image;
}
-int imagetobmp(opj_image_t * image, const char *outfile) {
+int imagetobmp(opj_image_t * image, const char *outfile)
+{
int w, h;
int i, pad;
FILE *fdest = NULL;
@@ -835,54 +860,65 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
/* 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);
- 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, ((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,
+ ((54) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
- 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", (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", (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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 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", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 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", (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", (7834) & 0xff, ((7834) >> 8) & 0xff,
+ ((7834) >> 16) & 0xff, ((7834) >> 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", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
+ ((0) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff,
+ ((0) >> 24) & 0xff);
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
+ 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
+ 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
+ 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;
@@ -890,31 +926,44 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- r = ((r >> adjustR)+((r >> (adjustR-1))%2));
- if(r > 255) r = 255; else if(r < 0) r = 0;
+ 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);
- g = ((g >> adjustG)+((g >> (adjustG-1))%2));
- if(g > 255) g = 255; else if(g < 0) g = 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);
- b = ((b >> adjustB)+((b >> (adjustB-1))%2));
- if(b > 255) b = 255; else if(b < 0) b = 0;
+ b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
+ if (b > 255) {
+ b = 255;
+ } else if (b < 0) {
+ b = 0;
+ }
bc = (OPJ_UINT8)b;
fprintf(fdest, "%c%c%c", bc, gc, rc);
if ((i + 1) % w == 0) {
- for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) /* ADD */
+ for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) { /* ADD */
fprintf(fdest, "%c", 0);
+ }
}
}
fclose(fdest);
- } else { /* Gray-scale */
+ } else { /* Gray-scale */
/* -->> -->> -->> -->>
8 bits non code (Gray scale)
@@ -932,43 +981,52 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
/* 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", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 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,
((54 + 1024) >> 16) & 0xff,
((54 + 1024) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
- 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", (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", (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", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 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", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 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", (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,
+ ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
+ ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
+ ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
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
+ 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++) {
fprintf(fdest, "%c%c%c%c", i, i, i, 0);
@@ -979,14 +1037,19 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- r = ((r >> adjustR)+((r >> (adjustR-1))%2));
- if(r > 255) r = 255; else if(r < 0) r = 0;
+ r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
+ if (r > 255) {
+ r = 255;
+ } else if (r < 0) {
+ r = 0;
+ }
fprintf(fdest, "%c", (OPJ_UINT8)r);
if ((i + 1) % w == 0) {
- for (pad = (w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) /* ADD */
+ for (pad = (w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) { /* ADD */
fprintf(fdest, "%c", 0);
+ }
}
}
fclose(fdest);
diff --git a/src/bin/jp2/convertpng.c b/src/bin/jp2/convertpng.c
index 5635c7d0..44d985f2 100644
--- a/src/bin/jp2/convertpng.c
+++ b/src/bin/jp2/convertpng.c
@@ -54,447 +54,464 @@
/* PNG allows bits per sample: 1, 2, 4, 8, 16 */
-static void convert_16u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void convert_16u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < length; i++) {
- OPJ_INT32 val0 = *pSrc++;
- OPJ_INT32 val1 = *pSrc++;
- pDst[i] = val0 << 8 | val1;
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < length; i++) {
+ OPJ_INT32 val0 = *pSrc++;
+ OPJ_INT32 val1 = *pSrc++;
+ pDst[i] = val0 << 8 | val1;
+ }
}
opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
{
- png_structp png = NULL;
- png_infop info = NULL;
- double gamma;
- int bit_depth, interlace_type,compression_type, filter_type;
- OPJ_UINT32 i;
- png_uint_32 width, height = 0U;
- int color_type;
- FILE *reader = NULL;
- OPJ_BYTE** rows = NULL;
- OPJ_INT32* row32s = NULL;
- /* j2k: */
- opj_image_t *image = NULL;
- opj_image_cmptparm_t cmptparm[4];
- OPJ_UINT32 nr_comp;
- OPJ_BYTE sigbuf[8];
- convert_XXx32s_C1R cvtXXTo32s = NULL;
- convert_32s_CXPX cvtCxToPx = NULL;
- OPJ_INT32* planes[4];
-
- if((reader = fopen(read_idf, "rb")) == NULL)
- {
- fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
- return NULL;
- }
-
- if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
- || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
- {
- fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
- goto fin;
- }
-
- if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
- NULL, NULL, NULL)) == NULL)
- goto fin;
- if((info = png_create_info_struct(png)) == NULL)
- goto fin;
-
- if(setjmp(png_jmpbuf(png)))
- goto fin;
-
- png_init_io(png, reader);
- png_set_sig_bytes(png, MAGIC_SIZE);
-
- png_read_info(png, info);
-
- if(png_get_IHDR(png, info, &width, &height,
- &bit_depth, &color_type, &interlace_type,
- &compression_type, &filter_type) == 0)
- goto fin;
-
- /* png_set_expand():
- * expand paletted images to RGB, expand grayscale images of
- * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
- * to alpha channels.
- */
- if(color_type == PNG_COLOR_TYPE_PALETTE) {
- png_set_expand(png);
- }
-
- if(png_get_valid(png, info, PNG_INFO_tRNS)) {
- png_set_expand(png);
- }
- /* We might wan't to expand background */
- /*
- if(png_get_valid(png, info, PNG_INFO_bKGD)) {
- png_color_16p bgnd;
- png_get_bKGD(png, info, &bgnd);
- png_set_background(png, bgnd, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
- }
- */
-
- if( !png_get_gAMA(png, info, &gamma))
- gamma = 1.0;
-
- /* we're not displaying but converting, screen gamma == 1.0 */
- png_set_gamma(png, 1.0, gamma);
-
- png_read_update_info(png, info);
-
- color_type = png_get_color_type(png, info);
-
- switch (color_type) {
- case PNG_COLOR_TYPE_GRAY:
- nr_comp = 1;
- break;
- case PNG_COLOR_TYPE_GRAY_ALPHA:
- nr_comp = 2;
- break;
- case PNG_COLOR_TYPE_RGB:
- nr_comp = 3;
- break;
- case PNG_COLOR_TYPE_RGB_ALPHA:
- nr_comp = 4;
- break;
- default:
- fprintf(stderr,"pngtoimage: colortype %d is not supported\n", color_type);
- goto fin;
- }
- cvtCxToPx = convert_32s_CXPX_LUT[nr_comp];
- bit_depth = png_get_bit_depth(png, info);
-
- switch (bit_depth) {
- case 1:
- case 2:
- case 4:
- case 8:
- cvtXXTo32s = convert_XXu32s_C1R_LUT[bit_depth];
- break;
- case 16: /* 16 bpp is specific to PNG */
- cvtXXTo32s = convert_16u32s_C1R;
- break;
- default:
- fprintf(stderr,"pngtoimage: bit depth %d is not supported\n", bit_depth);
- goto fin;
- }
-
-
- rows = (OPJ_BYTE**)calloc(height+1, sizeof(OPJ_BYTE*));
- if(rows == NULL){
- fprintf(stderr, "pngtoimage: memory out\n");
- goto fin;
- }
- for(i = 0; i < height; ++i){
- rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png,info));
- if(rows[i] == NULL){
- fprintf(stderr,"pngtoimage: memory out\n");
- goto fin;
- }
- }
- png_read_image(png, rows);
-
- /* Create image */
- memset(cmptparm, 0, sizeof(cmptparm));
- for(i = 0; i < nr_comp; ++i)
- {
- cmptparm[i].prec = (OPJ_UINT32)bit_depth;
- /* bits_per_pixel: 8 or 16 */
- cmptparm[i].bpp = (OPJ_UINT32)bit_depth;
- cmptparm[i].sgnd = 0;
- cmptparm[i].dx = (OPJ_UINT32)params->subsampling_dx;
- cmptparm[i].dy = (OPJ_UINT32)params->subsampling_dy;
- cmptparm[i].w = (OPJ_UINT32)width;
- cmptparm[i].h = (OPJ_UINT32)height;
- }
-
- image = opj_image_create(nr_comp, &cmptparm[0], (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY);
- if(image == NULL) goto fin;
- image->x0 = (OPJ_UINT32)params->image_offset_x0;
- image->y0 = (OPJ_UINT32)params->image_offset_y0;
- image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32)params->subsampling_dx + 1 + image->x0);
- image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)params->subsampling_dy + 1 + image->y0);
-
- row32s = (OPJ_INT32 *)malloc((size_t)width * nr_comp * sizeof(OPJ_INT32));
- if(row32s == NULL) goto fin;
-
- /* Set alpha channel */
- image->comps[nr_comp-1U].alpha = 1U - (nr_comp & 1U);
-
- for(i = 0; i < nr_comp; i++)
- {
- planes[i] = image->comps[i].data;
- }
-
- for(i = 0; i < height; ++i)
- {
- cvtXXTo32s(rows[i], row32s, (OPJ_SIZE_T)width * nr_comp);
- cvtCxToPx(row32s, planes, width);
- planes[0] += width;
- planes[1] += width;
- planes[2] += width;
- planes[3] += width;
- }
+ png_structp png = NULL;
+ png_infop info = NULL;
+ double gamma;
+ int bit_depth, interlace_type, compression_type, filter_type;
+ OPJ_UINT32 i;
+ png_uint_32 width, height = 0U;
+ int color_type;
+ FILE *reader = NULL;
+ OPJ_BYTE** rows = NULL;
+ OPJ_INT32* row32s = NULL;
+ /* j2k: */
+ opj_image_t *image = NULL;
+ opj_image_cmptparm_t cmptparm[4];
+ OPJ_UINT32 nr_comp;
+ OPJ_BYTE sigbuf[8];
+ convert_XXx32s_C1R cvtXXTo32s = NULL;
+ convert_32s_CXPX cvtCxToPx = NULL;
+ OPJ_INT32* planes[4];
+
+ if ((reader = fopen(read_idf, "rb")) == NULL) {
+ fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
+ return NULL;
+ }
+
+ if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
+ || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
+ fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
+ goto fin;
+ }
+
+ if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL)) == NULL) {
+ goto fin;
+ }
+ if ((info = png_create_info_struct(png)) == NULL) {
+ goto fin;
+ }
+
+ if (setjmp(png_jmpbuf(png))) {
+ goto fin;
+ }
+
+ png_init_io(png, reader);
+ png_set_sig_bytes(png, MAGIC_SIZE);
+
+ png_read_info(png, info);
+
+ if (png_get_IHDR(png, info, &width, &height,
+ &bit_depth, &color_type, &interlace_type,
+ &compression_type, &filter_type) == 0) {
+ goto fin;
+ }
+
+ /* png_set_expand():
+ * expand paletted images to RGB, expand grayscale images of
+ * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
+ * to alpha channels.
+ */
+ if (color_type == PNG_COLOR_TYPE_PALETTE) {
+ png_set_expand(png);
+ }
+
+ if (png_get_valid(png, info, PNG_INFO_tRNS)) {
+ png_set_expand(png);
+ }
+ /* We might wan't to expand background */
+ /*
+ if(png_get_valid(png, info, PNG_INFO_bKGD)) {
+ png_color_16p bgnd;
+ png_get_bKGD(png, info, &bgnd);
+ png_set_background(png, bgnd, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
+ }
+ */
+
+ if (!png_get_gAMA(png, info, &gamma)) {
+ gamma = 1.0;
+ }
+
+ /* we're not displaying but converting, screen gamma == 1.0 */
+ png_set_gamma(png, 1.0, gamma);
+
+ png_read_update_info(png, info);
+
+ color_type = png_get_color_type(png, info);
+
+ switch (color_type) {
+ case PNG_COLOR_TYPE_GRAY:
+ nr_comp = 1;
+ break;
+ case PNG_COLOR_TYPE_GRAY_ALPHA:
+ nr_comp = 2;
+ break;
+ case PNG_COLOR_TYPE_RGB:
+ nr_comp = 3;
+ break;
+ case PNG_COLOR_TYPE_RGB_ALPHA:
+ nr_comp = 4;
+ break;
+ default:
+ fprintf(stderr, "pngtoimage: colortype %d is not supported\n", color_type);
+ goto fin;
+ }
+ cvtCxToPx = convert_32s_CXPX_LUT[nr_comp];
+ bit_depth = png_get_bit_depth(png, info);
+
+ switch (bit_depth) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ cvtXXTo32s = convert_XXu32s_C1R_LUT[bit_depth];
+ break;
+ case 16: /* 16 bpp is specific to PNG */
+ cvtXXTo32s = convert_16u32s_C1R;
+ break;
+ default:
+ fprintf(stderr, "pngtoimage: bit depth %d is not supported\n", bit_depth);
+ goto fin;
+ }
+
+
+ rows = (OPJ_BYTE**)calloc(height + 1, sizeof(OPJ_BYTE*));
+ if (rows == NULL) {
+ fprintf(stderr, "pngtoimage: memory out\n");
+ goto fin;
+ }
+ for (i = 0; i < height; ++i) {
+ rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png, info));
+ if (rows[i] == NULL) {
+ fprintf(stderr, "pngtoimage: memory out\n");
+ goto fin;
+ }
+ }
+ png_read_image(png, rows);
+
+ /* Create image */
+ memset(cmptparm, 0, sizeof(cmptparm));
+ for (i = 0; i < nr_comp; ++i) {
+ cmptparm[i].prec = (OPJ_UINT32)bit_depth;
+ /* bits_per_pixel: 8 or 16 */
+ cmptparm[i].bpp = (OPJ_UINT32)bit_depth;
+ cmptparm[i].sgnd = 0;
+ cmptparm[i].dx = (OPJ_UINT32)params->subsampling_dx;
+ cmptparm[i].dy = (OPJ_UINT32)params->subsampling_dy;
+ cmptparm[i].w = (OPJ_UINT32)width;
+ cmptparm[i].h = (OPJ_UINT32)height;
+ }
+
+ image = opj_image_create(nr_comp, &cmptparm[0],
+ (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY);
+ if (image == NULL) {
+ goto fin;
+ }
+ image->x0 = (OPJ_UINT32)params->image_offset_x0;
+ image->y0 = (OPJ_UINT32)params->image_offset_y0;
+ image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32)
+ params->subsampling_dx + 1 + image->x0);
+ image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)
+ params->subsampling_dy + 1 + image->y0);
+
+ row32s = (OPJ_INT32 *)malloc((size_t)width * nr_comp * sizeof(OPJ_INT32));
+ if (row32s == NULL) {
+ goto fin;
+ }
+
+ /* Set alpha channel */
+ image->comps[nr_comp - 1U].alpha = 1U - (nr_comp & 1U);
+
+ for (i = 0; i < nr_comp; i++) {
+ planes[i] = image->comps[i].data;
+ }
+
+ for (i = 0; i < height; ++i) {
+ cvtXXTo32s(rows[i], row32s, (OPJ_SIZE_T)width * nr_comp);
+ cvtCxToPx(row32s, planes, width);
+ planes[0] += width;
+ planes[1] += width;
+ planes[2] += width;
+ planes[3] += width;
+ }
fin:
- if(rows)
- {
- for(i = 0; i < height; ++i)
- if(rows[i]) free(rows[i]);
- free(rows);
- }
- if (row32s) {
- free(row32s);
- }
- if(png)
- png_destroy_read_struct(&png, &info, NULL);
-
- fclose(reader);
-
- return image;
-
+ if (rows) {
+ for (i = 0; i < height; ++i)
+ if (rows[i]) {
+ free(rows[i]);
+ }
+ free(rows);
+ }
+ if (row32s) {
+ free(row32s);
+ }
+ if (png) {
+ png_destroy_read_struct(&png, &info, NULL);
+ }
+
+ fclose(reader);
+
+ return image;
+
}/* pngtoimage() */
-static void convert_32s16u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void convert_32s16u_C1R(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < length; i++) {
- OPJ_UINT32 val = (OPJ_UINT32)pSrc[i];
- *pDst++ = (OPJ_BYTE)(val >> 8);
- *pDst++ = (OPJ_BYTE)val;
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < length; i++) {
+ OPJ_UINT32 val = (OPJ_UINT32)pSrc[i];
+ *pDst++ = (OPJ_BYTE)(val >> 8);
+ *pDst++ = (OPJ_BYTE)val;
+ }
}
int imagetopng(opj_image_t * image, const char *write_idf)
{
- FILE * volatile writer = NULL;
- png_structp png = NULL;
- png_infop info = NULL;
- png_bytep volatile row_buf = NULL;
- int nr_comp, color_type;
- volatile int prec;
- png_color_8 sig_bit;
- OPJ_INT32 const* planes[4];
- int i;
- OPJ_INT32* volatile buffer32s = NULL;
-
- volatile int fails = 1;
-
- memset(&sig_bit, 0, sizeof(sig_bit));
- prec = (int)image->comps[0].prec;
- planes[0] = image->comps[0].data;
- nr_comp = (int)image->numcomps;
-
- if (nr_comp > 4) {
- nr_comp = 4;
- }
- for (i = 1; i < nr_comp; ++i) {
- if (image->comps[0].dx != image->comps[i].dx) {
- break;
- }
- if (image->comps[0].dy != image->comps[i].dy) {
- break;
- }
- if (image->comps[0].prec != image->comps[i].prec) {
- break;
- }
- if (image->comps[0].sgnd != image->comps[i].sgnd) {
- break;
- }
- planes[i] = image->comps[i].data;
- }
- if (i != nr_comp) {
- fprintf(stderr,"imagetopng: All components shall have the same subsampling, same bit depth, same sign.\n");
- fprintf(stderr,"\tAborting\n");
- return 1;
- }
- for (i = 0; i < nr_comp; ++i) {
- clip_component(&(image->comps[i]), image->comps[0].prec);
- }
- if(prec > 8 && prec < 16)
- {
- for (i = 0; i < nr_comp; ++i) {
- scale_component(&(image->comps[i]), 16);
- }
- prec = 16;
- }
- else if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
- {
- for (i = 0; i < nr_comp; ++i) {
- scale_component(&(image->comps[i]), 8);
- }
- prec = 8;
- } else if((prec > 1) && (prec < 8) && ((prec == 6) || ((prec & 1)==1))) { /* GRAY with non native precision */
- if ((prec == 5) || (prec == 6)) {
- prec = 8;
- } else {
- prec++;
- }
- for (i = 0; i < nr_comp; ++i) {
- scale_component(&(image->comps[i]), (OPJ_UINT32)prec);
- }
- }
-
- if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
- {
- fprintf(stderr,"imagetopng: can not create %s\n\twrong bit_depth %d\n", write_idf, prec);
- return fails;
- }
-
- writer = fopen(write_idf, "wb");
-
- if(writer == NULL) return fails;
-
- /* Create and initialize the png_struct with the desired error handler
- * functions. If you want to use the default stderr and longjump method,
- * you can supply NULL for the last three parameters. We also check that
- * the library version is compatible with the one used at compile time,
- * in case we are using dynamically linked libraries. REQUIRED.
- */
- png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
- NULL, NULL, NULL);
- /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
-
- if(png == NULL) goto fin;
-
- /* Allocate/initialize the image information data. REQUIRED
- */
- info = png_create_info_struct(png);
-
- if(info == NULL) goto fin;
-
- /* Set error handling. REQUIRED if you are not supplying your own
- * error handling functions in the png_create_write_struct() call.
- */
- if(setjmp(png_jmpbuf(png))) goto fin;
-
- /* I/O initialization functions is REQUIRED
- */
- png_init_io(png, writer);
-
- /* Set the image information here. Width and height are up to 2^31,
- * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
- * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
- * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
- * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
- * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
- * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
- * REQUIRED
- *
- * ERRORS:
- *
- * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
- * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
- * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
- * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
- *
- */
- png_set_compression_level(png, Z_BEST_COMPRESSION);
-
- if(nr_comp >= 3) /* RGB(A) */
- {
- color_type = PNG_COLOR_TYPE_RGB;
- sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec;
- }
- else /* GRAY(A) */
- {
- color_type = PNG_COLOR_TYPE_GRAY;
- sig_bit.gray = (png_byte)prec;
- }
- if((nr_comp & 1) == 0) /* ALPHA */
- {
- color_type |= PNG_COLOR_MASK_ALPHA;
- sig_bit.alpha = (png_byte)prec;
- }
-
- png_set_IHDR(png, info, image->comps[0].w, image->comps[0].h, prec, color_type,
- PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-
- png_set_sBIT(png, info, &sig_bit);
- /* png_set_gamma(png, 2.2, 1./2.2); */
- /* png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); */
- png_write_info(png, info);
-
- /* setup conversion */
- {
- OPJ_SIZE_T rowStride;
- png_size_t png_row_size;
-
- png_row_size = png_get_rowbytes(png, info);
- rowStride = ((OPJ_SIZE_T)image->comps[0].w * (OPJ_SIZE_T)nr_comp * (OPJ_SIZE_T)prec + 7U) / 8U;
- if (rowStride != (OPJ_SIZE_T)png_row_size) {
- fprintf(stderr, "Invalid PNG row size\n");
- goto fin;
- }
- row_buf = (png_bytep)malloc(png_row_size);
- if (row_buf == NULL) {
- fprintf(stderr, "Can't allocate memory for PNG row\n");
- goto fin;
- }
- buffer32s = (OPJ_INT32*)malloc((OPJ_SIZE_T)image->comps[0].w * (OPJ_SIZE_T)nr_comp * sizeof(OPJ_INT32));
- if (buffer32s == NULL) {
- fprintf(stderr, "Can't allocate memory for interleaved 32s row\n");
- goto fin;
- }
- }
-
- /* convert */
- {
- OPJ_SIZE_T width= image->comps[0].w;
- OPJ_UINT32 y;
- convert_32s_PXCX cvtPxToCx = convert_32s_PXCX_LUT[nr_comp];
- convert_32sXXx_C1R cvt32sToPack = NULL;
- OPJ_INT32 adjust = image->comps[0].sgnd ? 1 << (prec - 1) : 0;
- png_bytep row_buf_cpy = row_buf;
- OPJ_INT32* buffer32s_cpy = buffer32s;
-
- switch (prec) {
- case 1:
- case 2:
- case 4:
- case 8:
- cvt32sToPack = convert_32sXXu_C1R_LUT[prec];
- break;
- case 16:
- cvt32sToPack = convert_32s16u_C1R;
- break;
- default:
- /* never here */
- break;
- }
-
- for(y = 0; y < image->comps[0].h; ++y)
- {
- cvtPxToCx(planes, buffer32s_cpy, width, adjust);
- cvt32sToPack(buffer32s_cpy, row_buf_cpy, width * (OPJ_SIZE_T)nr_comp);
- png_write_row(png, row_buf_cpy);
- planes[0] += width;
- planes[1] += width;
- planes[2] += width;
- planes[3] += width;
- }
- }
-
- png_write_end(png, info);
-
- fails = 0;
-
+ FILE * volatile writer = NULL;
+ png_structp png = NULL;
+ png_infop info = NULL;
+ png_bytep volatile row_buf = NULL;
+ int nr_comp, color_type;
+ volatile int prec;
+ png_color_8 sig_bit;
+ OPJ_INT32 const* planes[4];
+ int i;
+ OPJ_INT32* volatile buffer32s = NULL;
+
+ volatile int fails = 1;
+
+ memset(&sig_bit, 0, sizeof(sig_bit));
+ prec = (int)image->comps[0].prec;
+ planes[0] = image->comps[0].data;
+ nr_comp = (int)image->numcomps;
+
+ if (nr_comp > 4) {
+ nr_comp = 4;
+ }
+ for (i = 1; i < nr_comp; ++i) {
+ if (image->comps[0].dx != image->comps[i].dx) {
+ break;
+ }
+ if (image->comps[0].dy != image->comps[i].dy) {
+ break;
+ }
+ if (image->comps[0].prec != image->comps[i].prec) {
+ break;
+ }
+ if (image->comps[0].sgnd != image->comps[i].sgnd) {
+ break;
+ }
+ planes[i] = image->comps[i].data;
+ }
+ if (i != nr_comp) {
+ fprintf(stderr,
+ "imagetopng: All components shall have the same subsampling, same bit depth, same sign.\n");
+ fprintf(stderr, "\tAborting\n");
+ return 1;
+ }
+ for (i = 0; i < nr_comp; ++i) {
+ clip_component(&(image->comps[i]), image->comps[0].prec);
+ }
+ if (prec > 8 && prec < 16) {
+ for (i = 0; i < nr_comp; ++i) {
+ scale_component(&(image->comps[i]), 16);
+ }
+ prec = 16;
+ } else if (prec < 8 && nr_comp > 1) { /* GRAY_ALPHA, RGB, RGB_ALPHA */
+ for (i = 0; i < nr_comp; ++i) {
+ scale_component(&(image->comps[i]), 8);
+ }
+ prec = 8;
+ } else if ((prec > 1) && (prec < 8) && ((prec == 6) ||
+ ((prec & 1) == 1))) { /* GRAY with non native precision */
+ if ((prec == 5) || (prec == 6)) {
+ prec = 8;
+ } else {
+ prec++;
+ }
+ for (i = 0; i < nr_comp; ++i) {
+ scale_component(&(image->comps[i]), (OPJ_UINT32)prec);
+ }
+ }
+
+ if (prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) {
+ fprintf(stderr, "imagetopng: can not create %s\n\twrong bit_depth %d\n",
+ write_idf, prec);
+ return fails;
+ }
+
+ writer = fopen(write_idf, "wb");
+
+ if (writer == NULL) {
+ return fails;
+ }
+
+ /* Create and initialize the png_struct with the desired error handler
+ * functions. If you want to use the default stderr and longjump method,
+ * you can supply NULL for the last three parameters. We also check that
+ * the library version is compatible with the one used at compile time,
+ * in case we are using dynamically linked libraries. REQUIRED.
+ */
+ png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL);
+ /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
+
+ if (png == NULL) {
+ goto fin;
+ }
+
+ /* Allocate/initialize the image information data. REQUIRED
+ */
+ info = png_create_info_struct(png);
+
+ if (info == NULL) {
+ goto fin;
+ }
+
+ /* Set error handling. REQUIRED if you are not supplying your own
+ * error handling functions in the png_create_write_struct() call.
+ */
+ if (setjmp(png_jmpbuf(png))) {
+ goto fin;
+ }
+
+ /* I/O initialization functions is REQUIRED
+ */
+ png_init_io(png, writer);
+
+ /* Set the image information here. Width and height are up to 2^31,
+ * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
+ * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
+ * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
+ * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
+ * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
+ * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
+ * REQUIRED
+ *
+ * ERRORS:
+ *
+ * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
+ * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
+ * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
+ * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
+ *
+ */
+ png_set_compression_level(png, Z_BEST_COMPRESSION);
+
+ if (nr_comp >= 3) { /* RGB(A) */
+ color_type = PNG_COLOR_TYPE_RGB;
+ sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec;
+ } else { /* GRAY(A) */
+ color_type = PNG_COLOR_TYPE_GRAY;
+ sig_bit.gray = (png_byte)prec;
+ }
+ if ((nr_comp & 1) == 0) { /* ALPHA */
+ color_type |= PNG_COLOR_MASK_ALPHA;
+ sig_bit.alpha = (png_byte)prec;
+ }
+
+ png_set_IHDR(png, info, image->comps[0].w, image->comps[0].h, prec, color_type,
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+
+ png_set_sBIT(png, info, &sig_bit);
+ /* png_set_gamma(png, 2.2, 1./2.2); */
+ /* png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); */
+ png_write_info(png, info);
+
+ /* setup conversion */
+ {
+ OPJ_SIZE_T rowStride;
+ png_size_t png_row_size;
+
+ png_row_size = png_get_rowbytes(png, info);
+ rowStride = ((OPJ_SIZE_T)image->comps[0].w * (OPJ_SIZE_T)nr_comp *
+ (OPJ_SIZE_T)prec + 7U) / 8U;
+ if (rowStride != (OPJ_SIZE_T)png_row_size) {
+ fprintf(stderr, "Invalid PNG row size\n");
+ goto fin;
+ }
+ row_buf = (png_bytep)malloc(png_row_size);
+ if (row_buf == NULL) {
+ fprintf(stderr, "Can't allocate memory for PNG row\n");
+ goto fin;
+ }
+ buffer32s = (OPJ_INT32*)malloc((OPJ_SIZE_T)image->comps[0].w *
+ (OPJ_SIZE_T)nr_comp * sizeof(OPJ_INT32));
+ if (buffer32s == NULL) {
+ fprintf(stderr, "Can't allocate memory for interleaved 32s row\n");
+ goto fin;
+ }
+ }
+
+ /* convert */
+ {
+ OPJ_SIZE_T width = image->comps[0].w;
+ OPJ_UINT32 y;
+ convert_32s_PXCX cvtPxToCx = convert_32s_PXCX_LUT[nr_comp];
+ convert_32sXXx_C1R cvt32sToPack = NULL;
+ OPJ_INT32 adjust = image->comps[0].sgnd ? 1 << (prec - 1) : 0;
+ png_bytep row_buf_cpy = row_buf;
+ OPJ_INT32* buffer32s_cpy = buffer32s;
+
+ switch (prec) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ cvt32sToPack = convert_32sXXu_C1R_LUT[prec];
+ break;
+ case 16:
+ cvt32sToPack = convert_32s16u_C1R;
+ break;
+ default:
+ /* never here */
+ break;
+ }
+
+ for (y = 0; y < image->comps[0].h; ++y) {
+ cvtPxToCx(planes, buffer32s_cpy, width, adjust);
+ cvt32sToPack(buffer32s_cpy, row_buf_cpy, width * (OPJ_SIZE_T)nr_comp);
+ png_write_row(png, row_buf_cpy);
+ planes[0] += width;
+ planes[1] += width;
+ planes[2] += width;
+ planes[3] += width;
+ }
+ }
+
+ png_write_end(png, info);
+
+ fails = 0;
+
fin:
- if(png) {
- png_destroy_write_struct(&png, &info);
- }
- if(row_buf) {
- free(row_buf);
- }
- if(buffer32s) {
- free(buffer32s);
- }
- fclose(writer);
-
- if(fails) (void)remove(write_idf); /* ignore return value */
-
- return fails;
+ if (png) {
+ png_destroy_write_struct(&png, &info);
+ }
+ if (row_buf) {
+ free(row_buf);
+ }
+ if (buffer32s) {
+ free(buffer32s);
+ }
+ fclose(writer);
+
+ if (fails) {
+ (void)remove(write_idf); /* ignore return value */
+ }
+
+ return fails;
}/* imagetopng() */
diff --git a/src/bin/jp2/converttif.c b/src/bin/jp2/converttif.c
index 143d3be6..c4eba4b7 100644
--- a/src/bin/jp2/converttif.c
+++ b/src/bin/jp2/converttif.c
@@ -52,1152 +52,1176 @@
#include "convert.h"
/* -->> -->> -->> -->>
-
+
TIFF IMAGE FORMAT
-
+
<<-- <<-- <<-- <<-- */
#define PUTBITS2(s, nb) \
- trailing <<= remaining; \
- trailing |= (unsigned int)((s) >> (nb - remaining)); \
- *pDst++ = (OPJ_BYTE)trailing; \
- trailing = (unsigned int)((s) & ((1U << (nb - remaining)) - 1U)); \
- if (nb >= (remaining + 8)) { \
- *pDst++ = (OPJ_BYTE)(trailing >> (nb - (remaining + 8))); \
- trailing &= (unsigned int)((1U << (nb - (remaining + 8))) - 1U); \
- remaining += 16 - nb; \
- } else { \
- remaining += 8 - nb; \
- }
+ trailing <<= remaining; \
+ trailing |= (unsigned int)((s) >> (nb - remaining)); \
+ *pDst++ = (OPJ_BYTE)trailing; \
+ trailing = (unsigned int)((s) & ((1U << (nb - remaining)) - 1U)); \
+ if (nb >= (remaining + 8)) { \
+ *pDst++ = (OPJ_BYTE)(trailing >> (nb - (remaining + 8))); \
+ trailing &= (unsigned int)((1U << (nb - (remaining + 8))) - 1U); \
+ remaining += 16 - nb; \
+ } else { \
+ remaining += 8 - nb; \
+ }
#define PUTBITS(s, nb) \
if (nb >= remaining) { \
- PUTBITS2(s, nb) \
- } else { \
- trailing <<= nb; \
- trailing |= (unsigned int)(s); \
- remaining -= nb; \
- }
+ PUTBITS2(s, nb) \
+ } else { \
+ trailing <<= nb; \
+ trailing |= (unsigned int)(s); \
+ remaining -= nb; \
+ }
#define FLUSHBITS() \
- if (remaining != 8) { \
- trailing <<= remaining; \
- *pDst++ = (OPJ_BYTE)trailing; \
- }
+ if (remaining != 8) { \
+ trailing <<= remaining; \
+ *pDst++ = (OPJ_BYTE)trailing; \
+ }
-static void tif_32sto3u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto3u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
-
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 << 5) | (src1 << 2) | (src2 >> 1));
- *pDst++ = (OPJ_BYTE)((src2 << 7) | (src3 << 4) | (src4 << 1) | (src5 >> 2));
- *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 << 3) | (src7));
- }
-
- if (length & 7U) {
- unsigned int trailing = 0U;
- int remaining = 8U;
- length &= 7U;
- PUTBITS((OPJ_UINT32)pSrc[i+0], 3)
- if (length > 1U) {
- PUTBITS((OPJ_UINT32)pSrc[i+1], 3)
- if (length > 2U) {
- PUTBITS((OPJ_UINT32)pSrc[i+2], 3)
- if (length > 3U) {
- PUTBITS((OPJ_UINT32)pSrc[i+3], 3)
- if (length > 4U) {
- PUTBITS((OPJ_UINT32)pSrc[i+4], 3)
- if (length > 5U) {
- PUTBITS((OPJ_UINT32)pSrc[i+5], 3)
- if (length > 6U) {
- PUTBITS((OPJ_UINT32)pSrc[i+6], 3)
- }
- }
- }
- }
- }
- }
- FLUSHBITS()
- }
+ OPJ_SIZE_T i;
+
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 << 5) | (src1 << 2) | (src2 >> 1));
+ *pDst++ = (OPJ_BYTE)((src2 << 7) | (src3 << 4) | (src4 << 1) | (src5 >> 2));
+ *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 << 3) | (src7));
+ }
+
+ if (length & 7U) {
+ unsigned int trailing = 0U;
+ int remaining = 8U;
+ length &= 7U;
+ PUTBITS((OPJ_UINT32)pSrc[i + 0], 3)
+ if (length > 1U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 1], 3)
+ if (length > 2U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 2], 3)
+ if (length > 3U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 3], 3)
+ if (length > 4U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 4], 3)
+ if (length > 5U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 5], 3)
+ if (length > 6U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 6], 3)
+ }
+ }
+ }
+ }
+ }
+ }
+ FLUSHBITS()
+ }
}
-static void tif_32sto5u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto5u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
-
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 << 3) | (src1 >> 2));
- *pDst++ = (OPJ_BYTE)((src1 << 6) | (src2 << 1) | (src3 >> 4));
- *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 1));
- *pDst++ = (OPJ_BYTE)((src4 << 7) | (src5 << 2) | (src6 >> 3));
- *pDst++ = (OPJ_BYTE)((src6 << 5) | (src7));
-
- }
-
- if (length & 7U) {
- unsigned int trailing = 0U;
- int remaining = 8U;
- length &= 7U;
- PUTBITS((OPJ_UINT32)pSrc[i+0], 5)
- if (length > 1U) {
- PUTBITS((OPJ_UINT32)pSrc[i+1], 5)
- if (length > 2U) {
- PUTBITS((OPJ_UINT32)pSrc[i+2], 5)
- if (length > 3U) {
- PUTBITS((OPJ_UINT32)pSrc[i+3], 5)
- if (length > 4U) {
- PUTBITS((OPJ_UINT32)pSrc[i+4], 5)
- if (length > 5U) {
- PUTBITS((OPJ_UINT32)pSrc[i+5], 5)
- if (length > 6U) {
- PUTBITS((OPJ_UINT32)pSrc[i+6], 5)
- }
- }
- }
- }
- }
- }
- FLUSHBITS()
- }
+ OPJ_SIZE_T i;
+
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 << 3) | (src1 >> 2));
+ *pDst++ = (OPJ_BYTE)((src1 << 6) | (src2 << 1) | (src3 >> 4));
+ *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 1));
+ *pDst++ = (OPJ_BYTE)((src4 << 7) | (src5 << 2) | (src6 >> 3));
+ *pDst++ = (OPJ_BYTE)((src6 << 5) | (src7));
+
+ }
+
+ if (length & 7U) {
+ unsigned int trailing = 0U;
+ int remaining = 8U;
+ length &= 7U;
+ PUTBITS((OPJ_UINT32)pSrc[i + 0], 5)
+ if (length > 1U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 1], 5)
+ if (length > 2U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 2], 5)
+ if (length > 3U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 3], 5)
+ if (length > 4U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 4], 5)
+ if (length > 5U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 5], 5)
+ if (length > 6U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 6], 5)
+ }
+ }
+ }
+ }
+ }
+ }
+ FLUSHBITS()
+ }
}
-static void tif_32sto7u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto7u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
-
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 << 1) | (src1 >> 6));
- *pDst++ = (OPJ_BYTE)((src1 << 2) | (src2 >> 5));
- *pDst++ = (OPJ_BYTE)((src2 << 3) | (src3 >> 4));
- *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 3));
- *pDst++ = (OPJ_BYTE)((src4 << 5) | (src5 >> 2));
- *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 >> 1));
- *pDst++ = (OPJ_BYTE)((src6 << 7) | (src7));
- }
-
- if (length & 7U) {
- unsigned int trailing = 0U;
- int remaining = 8U;
- length &= 7U;
- PUTBITS((OPJ_UINT32)pSrc[i+0], 7)
- if (length > 1U) {
- PUTBITS((OPJ_UINT32)pSrc[i+1], 7)
- if (length > 2U) {
- PUTBITS((OPJ_UINT32)pSrc[i+2], 7)
- if (length > 3U) {
- PUTBITS((OPJ_UINT32)pSrc[i+3], 7)
- if (length > 4U) {
- PUTBITS((OPJ_UINT32)pSrc[i+4], 7)
- if (length > 5U) {
- PUTBITS((OPJ_UINT32)pSrc[i+5], 7)
- if (length > 6U) {
- PUTBITS((OPJ_UINT32)pSrc[i+6], 7)
- }
- }
- }
- }
- }
- }
- FLUSHBITS()
- }
+ OPJ_SIZE_T i;
+
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 << 1) | (src1 >> 6));
+ *pDst++ = (OPJ_BYTE)((src1 << 2) | (src2 >> 5));
+ *pDst++ = (OPJ_BYTE)((src2 << 3) | (src3 >> 4));
+ *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 3));
+ *pDst++ = (OPJ_BYTE)((src4 << 5) | (src5 >> 2));
+ *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 >> 1));
+ *pDst++ = (OPJ_BYTE)((src6 << 7) | (src7));
+ }
+
+ if (length & 7U) {
+ unsigned int trailing = 0U;
+ int remaining = 8U;
+ length &= 7U;
+ PUTBITS((OPJ_UINT32)pSrc[i + 0], 7)
+ if (length > 1U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 1], 7)
+ if (length > 2U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 2], 7)
+ if (length > 3U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 3], 7)
+ if (length > 4U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 4], 7)
+ if (length > 5U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 5], 7)
+ if (length > 6U) {
+ PUTBITS((OPJ_UINT32)pSrc[i + 6], 7)
+ }
+ }
+ }
+ }
+ }
+ }
+ FLUSHBITS()
+ }
}
-static void tif_32sto9u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto9u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
-
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 >> 1));
- *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 >> 2));
- *pDst++ = (OPJ_BYTE)((src1 << 6) | (src2 >> 3));
- *pDst++ = (OPJ_BYTE)((src2 << 5) | (src3 >> 4));
- *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 5));
- *pDst++ = (OPJ_BYTE)((src4 << 3) | (src5 >> 6));
- *pDst++ = (OPJ_BYTE)((src5 << 2) | (src6 >> 7));
- *pDst++ = (OPJ_BYTE)((src6 << 1) | (src7 >> 8));
- *pDst++ = (OPJ_BYTE)(src7);
- }
-
- if (length & 7U) {
- unsigned int trailing = 0U;
- int remaining = 8U;
- length &= 7U;
- PUTBITS2((OPJ_UINT32)pSrc[i+0], 9)
- if (length > 1U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+1], 9)
- if (length > 2U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+2], 9)
- if (length > 3U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+3], 9)
- if (length > 4U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+4], 9)
- if (length > 5U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+5], 9)
- if (length > 6U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+6], 9)
- }
- }
- }
- }
- }
- }
- FLUSHBITS()
- }
+ OPJ_SIZE_T i;
+
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 >> 1));
+ *pDst++ = (OPJ_BYTE)((src0 << 7) | (src1 >> 2));
+ *pDst++ = (OPJ_BYTE)((src1 << 6) | (src2 >> 3));
+ *pDst++ = (OPJ_BYTE)((src2 << 5) | (src3 >> 4));
+ *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 5));
+ *pDst++ = (OPJ_BYTE)((src4 << 3) | (src5 >> 6));
+ *pDst++ = (OPJ_BYTE)((src5 << 2) | (src6 >> 7));
+ *pDst++ = (OPJ_BYTE)((src6 << 1) | (src7 >> 8));
+ *pDst++ = (OPJ_BYTE)(src7);
+ }
+
+ if (length & 7U) {
+ unsigned int trailing = 0U;
+ int remaining = 8U;
+ length &= 7U;
+ PUTBITS2((OPJ_UINT32)pSrc[i + 0], 9)
+ if (length > 1U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 1], 9)
+ if (length > 2U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 2], 9)
+ if (length > 3U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 3], 9)
+ if (length > 4U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 4], 9)
+ if (length > 5U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 5], 9)
+ if (length > 6U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 6], 9)
+ }
+ }
+ }
+ }
+ }
+ }
+ FLUSHBITS()
+ }
}
-static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto10u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
-
- *pDst++ = (OPJ_BYTE)(src0 >> 2);
- *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
- *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
- *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2) | (src3 >> 8));
- *pDst++ = (OPJ_BYTE)(src3);
- }
-
- if (length & 3U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = 0U;
- OPJ_UINT32 src2 = 0U;
- length = length & 3U;
-
- if (length > 1U) {
- src1 = (OPJ_UINT32)pSrc[i+1];
- if (length > 2U) {
- src2 = (OPJ_UINT32)pSrc[i+2];
- }
- }
- *pDst++ = (OPJ_BYTE)(src0 >> 2);
- *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
- if (length > 1U) {
- *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
- if (length > 2U) {
- *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2));
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+
+ *pDst++ = (OPJ_BYTE)(src0 >> 2);
+ *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
+ *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
+ *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2) | (src3 >> 8));
+ *pDst++ = (OPJ_BYTE)(src3);
+ }
+
+ if (length & 3U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = 0U;
+ OPJ_UINT32 src2 = 0U;
+ length = length & 3U;
+
+ if (length > 1U) {
+ src1 = (OPJ_UINT32)pSrc[i + 1];
+ if (length > 2U) {
+ src2 = (OPJ_UINT32)pSrc[i + 2];
+ }
+ }
+ *pDst++ = (OPJ_BYTE)(src0 >> 2);
+ *pDst++ = (OPJ_BYTE)(((src0 & 0x3U) << 6) | (src1 >> 4));
+ if (length > 1U) {
+ *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 6));
+ if (length > 2U) {
+ *pDst++ = (OPJ_BYTE)(((src2 & 0x3FU) << 2));
+ }
+ }
+ }
}
-static void tif_32sto11u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto11u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
-
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 >> 3));
- *pDst++ = (OPJ_BYTE)((src0 << 5) | (src1 >> 6));
- *pDst++ = (OPJ_BYTE)((src1 << 2) | (src2 >> 9));
- *pDst++ = (OPJ_BYTE)((src2 >> 1));
- *pDst++ = (OPJ_BYTE)((src2 << 7) | (src3 >> 4));
- *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 7));
- *pDst++ = (OPJ_BYTE)((src4 << 1) | (src5 >> 10));
- *pDst++ = (OPJ_BYTE)((src5 >> 2));
- *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 >> 5));
- *pDst++ = (OPJ_BYTE)((src6 << 3) | (src7 >> 8));
- *pDst++ = (OPJ_BYTE)(src7);
- }
-
- if (length & 7U) {
- unsigned int trailing = 0U;
- int remaining = 8U;
- length &= 7U;
- PUTBITS2((OPJ_UINT32)pSrc[i+0], 11)
- if (length > 1U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+1], 11)
- if (length > 2U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+2], 11)
- if (length > 3U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+3], 11)
- if (length > 4U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+4], 11)
- if (length > 5U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+5], 11)
- if (length > 6U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+6], 11)
- }
- }
- }
- }
- }
- }
- FLUSHBITS()
- }
+ OPJ_SIZE_T i;
+
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 >> 3));
+ *pDst++ = (OPJ_BYTE)((src0 << 5) | (src1 >> 6));
+ *pDst++ = (OPJ_BYTE)((src1 << 2) | (src2 >> 9));
+ *pDst++ = (OPJ_BYTE)((src2 >> 1));
+ *pDst++ = (OPJ_BYTE)((src2 << 7) | (src3 >> 4));
+ *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 7));
+ *pDst++ = (OPJ_BYTE)((src4 << 1) | (src5 >> 10));
+ *pDst++ = (OPJ_BYTE)((src5 >> 2));
+ *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 >> 5));
+ *pDst++ = (OPJ_BYTE)((src6 << 3) | (src7 >> 8));
+ *pDst++ = (OPJ_BYTE)(src7);
+ }
+
+ if (length & 7U) {
+ unsigned int trailing = 0U;
+ int remaining = 8U;
+ length &= 7U;
+ PUTBITS2((OPJ_UINT32)pSrc[i + 0], 11)
+ if (length > 1U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 1], 11)
+ if (length > 2U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 2], 11)
+ if (length > 3U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 3], 11)
+ if (length > 4U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 4], 11)
+ if (length > 5U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 5], 11)
+ if (length > 6U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 6], 11)
+ }
+ }
+ }
+ }
+ }
+ }
+ FLUSHBITS()
+ }
}
-static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto12u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
-
- *pDst++ = (OPJ_BYTE)(src0 >> 4);
- *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8));
- *pDst++ = (OPJ_BYTE)(src1);
- }
-
- if (length & 1U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- *pDst++ = (OPJ_BYTE)(src0 >> 4);
- *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4));
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+
+ *pDst++ = (OPJ_BYTE)(src0 >> 4);
+ *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4) | (src1 >> 8));
+ *pDst++ = (OPJ_BYTE)(src1);
+ }
+
+ if (length & 1U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ *pDst++ = (OPJ_BYTE)(src0 >> 4);
+ *pDst++ = (OPJ_BYTE)(((src0 & 0xFU) << 4));
+ }
}
-static void tif_32sto13u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto13u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
-
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 >> 5));
- *pDst++ = (OPJ_BYTE)((src0 << 3) | (src1 >> 10));
- *pDst++ = (OPJ_BYTE)((src1 >> 2));
- *pDst++ = (OPJ_BYTE)((src1 << 6) | (src2 >> 7));
- *pDst++ = (OPJ_BYTE)((src2 << 1) | (src3 >> 12));
- *pDst++ = (OPJ_BYTE)((src3 >> 4));
- *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 9));
- *pDst++ = (OPJ_BYTE)((src4 >> 1));
- *pDst++ = (OPJ_BYTE)((src4 << 7) | (src5 >> 6));
- *pDst++ = (OPJ_BYTE)((src5 << 2) | (src6 >> 11));
- *pDst++ = (OPJ_BYTE)((src6 >> 3));
- *pDst++ = (OPJ_BYTE)((src6 << 5) | (src7 >> 8));
- *pDst++ = (OPJ_BYTE)(src7);
- }
-
- if (length & 7U) {
- unsigned int trailing = 0U;
- int remaining = 8U;
- length &= 7U;
- PUTBITS2((OPJ_UINT32)pSrc[i+0], 13)
- if (length > 1U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+1], 13)
- if (length > 2U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+2], 13)
- if (length > 3U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+3], 13)
- if (length > 4U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+4], 13)
- if (length > 5U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+5], 13)
- if (length > 6U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+6], 13)
- }
- }
- }
- }
- }
- }
- FLUSHBITS()
- }
+ OPJ_SIZE_T i;
+
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 >> 5));
+ *pDst++ = (OPJ_BYTE)((src0 << 3) | (src1 >> 10));
+ *pDst++ = (OPJ_BYTE)((src1 >> 2));
+ *pDst++ = (OPJ_BYTE)((src1 << 6) | (src2 >> 7));
+ *pDst++ = (OPJ_BYTE)((src2 << 1) | (src3 >> 12));
+ *pDst++ = (OPJ_BYTE)((src3 >> 4));
+ *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 9));
+ *pDst++ = (OPJ_BYTE)((src4 >> 1));
+ *pDst++ = (OPJ_BYTE)((src4 << 7) | (src5 >> 6));
+ *pDst++ = (OPJ_BYTE)((src5 << 2) | (src6 >> 11));
+ *pDst++ = (OPJ_BYTE)((src6 >> 3));
+ *pDst++ = (OPJ_BYTE)((src6 << 5) | (src7 >> 8));
+ *pDst++ = (OPJ_BYTE)(src7);
+ }
+
+ if (length & 7U) {
+ unsigned int trailing = 0U;
+ int remaining = 8U;
+ length &= 7U;
+ PUTBITS2((OPJ_UINT32)pSrc[i + 0], 13)
+ if (length > 1U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 1], 13)
+ if (length > 2U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 2], 13)
+ if (length > 3U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 3], 13)
+ if (length > 4U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 4], 13)
+ if (length > 5U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 5], 13)
+ if (length > 6U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 6], 13)
+ }
+ }
+ }
+ }
+ }
+ }
+ FLUSHBITS()
+ }
}
-static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto14u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
-
- *pDst++ = (OPJ_BYTE)(src0 >> 6);
- *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
- *pDst++ = (OPJ_BYTE)(src1 >> 4);
- *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
- *pDst++ = (OPJ_BYTE)(src2 >> 2);
- *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | (src3 >> 8));
- *pDst++ = (OPJ_BYTE)(src3);
- }
-
- if (length & 3U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = 0U;
- OPJ_UINT32 src2 = 0U;
- length = length & 3U;
-
- if (length > 1U) {
- src1 = (OPJ_UINT32)pSrc[i+1];
- if (length > 2U) {
- src2 = (OPJ_UINT32)pSrc[i+2];
- }
- }
- *pDst++ = (OPJ_BYTE)(src0 >> 6);
- *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
- if (length > 1U) {
- *pDst++ = (OPJ_BYTE)(src1 >> 4);
- *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
- if (length > 2U) {
- *pDst++ = (OPJ_BYTE)(src2 >> 2);
- *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+
+ *pDst++ = (OPJ_BYTE)(src0 >> 6);
+ *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
+ *pDst++ = (OPJ_BYTE)(src1 >> 4);
+ *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
+ *pDst++ = (OPJ_BYTE)(src2 >> 2);
+ *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6) | (src3 >> 8));
+ *pDst++ = (OPJ_BYTE)(src3);
+ }
+
+ if (length & 3U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = 0U;
+ OPJ_UINT32 src2 = 0U;
+ length = length & 3U;
+
+ if (length > 1U) {
+ src1 = (OPJ_UINT32)pSrc[i + 1];
+ if (length > 2U) {
+ src2 = (OPJ_UINT32)pSrc[i + 2];
+ }
+ }
+ *pDst++ = (OPJ_BYTE)(src0 >> 6);
+ *pDst++ = (OPJ_BYTE)(((src0 & 0x3FU) << 2) | (src1 >> 12));
+ if (length > 1U) {
+ *pDst++ = (OPJ_BYTE)(src1 >> 4);
+ *pDst++ = (OPJ_BYTE)(((src1 & 0xFU) << 4) | (src2 >> 10));
+ if (length > 2U) {
+ *pDst++ = (OPJ_BYTE)(src2 >> 2);
+ *pDst++ = (OPJ_BYTE)(((src2 & 0x3U) << 6));
+ }
+ }
+ }
}
-static void tif_32sto15u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst, OPJ_SIZE_T length)
+static void tif_32sto15u(const OPJ_INT32* pSrc, OPJ_BYTE* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
-
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i+0];
- OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i+1];
- OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i+2];
- OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i+3];
- OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i+4];
- OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i+5];
- OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i+6];
- OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i+7];
-
- *pDst++ = (OPJ_BYTE)((src0 >> 7));
- *pDst++ = (OPJ_BYTE)((src0 << 1) | (src1 >> 14));
- *pDst++ = (OPJ_BYTE)((src1 >> 6));
- *pDst++ = (OPJ_BYTE)((src1 << 2) | (src2 >> 13));
- *pDst++ = (OPJ_BYTE)((src2 >> 5));
- *pDst++ = (OPJ_BYTE)((src2 << 3) | (src3 >> 12));
- *pDst++ = (OPJ_BYTE)((src3 >> 4));
- *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 11));
- *pDst++ = (OPJ_BYTE)((src4 >> 3));
- *pDst++ = (OPJ_BYTE)((src4 << 5) | (src5 >> 10));
- *pDst++ = (OPJ_BYTE)((src5 >> 2));
- *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 >> 9));
- *pDst++ = (OPJ_BYTE)((src6 >> 1));
- *pDst++ = (OPJ_BYTE)((src6 << 7) | (src7 >> 8));
- *pDst++ = (OPJ_BYTE)(src7);
- }
-
- if (length & 7U) {
- unsigned int trailing = 0U;
- int remaining = 8U;
- length &= 7U;
- PUTBITS2((OPJ_UINT32)pSrc[i+0], 15)
- if (length > 1U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+1], 15)
- if (length > 2U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+2], 15)
- if (length > 3U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+3], 15)
- if (length > 4U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+4], 15)
- if (length > 5U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+5], 15)
- if (length > 6U) {
- PUTBITS2((OPJ_UINT32)pSrc[i+6], 15)
- }
- }
- }
- }
- }
- }
- FLUSHBITS()
- }
+ OPJ_SIZE_T i;
+
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 src0 = (OPJ_UINT32)pSrc[i + 0];
+ OPJ_UINT32 src1 = (OPJ_UINT32)pSrc[i + 1];
+ OPJ_UINT32 src2 = (OPJ_UINT32)pSrc[i + 2];
+ OPJ_UINT32 src3 = (OPJ_UINT32)pSrc[i + 3];
+ OPJ_UINT32 src4 = (OPJ_UINT32)pSrc[i + 4];
+ OPJ_UINT32 src5 = (OPJ_UINT32)pSrc[i + 5];
+ OPJ_UINT32 src6 = (OPJ_UINT32)pSrc[i + 6];
+ OPJ_UINT32 src7 = (OPJ_UINT32)pSrc[i + 7];
+
+ *pDst++ = (OPJ_BYTE)((src0 >> 7));
+ *pDst++ = (OPJ_BYTE)((src0 << 1) | (src1 >> 14));
+ *pDst++ = (OPJ_BYTE)((src1 >> 6));
+ *pDst++ = (OPJ_BYTE)((src1 << 2) | (src2 >> 13));
+ *pDst++ = (OPJ_BYTE)((src2 >> 5));
+ *pDst++ = (OPJ_BYTE)((src2 << 3) | (src3 >> 12));
+ *pDst++ = (OPJ_BYTE)((src3 >> 4));
+ *pDst++ = (OPJ_BYTE)((src3 << 4) | (src4 >> 11));
+ *pDst++ = (OPJ_BYTE)((src4 >> 3));
+ *pDst++ = (OPJ_BYTE)((src4 << 5) | (src5 >> 10));
+ *pDst++ = (OPJ_BYTE)((src5 >> 2));
+ *pDst++ = (OPJ_BYTE)((src5 << 6) | (src6 >> 9));
+ *pDst++ = (OPJ_BYTE)((src6 >> 1));
+ *pDst++ = (OPJ_BYTE)((src6 << 7) | (src7 >> 8));
+ *pDst++ = (OPJ_BYTE)(src7);
+ }
+
+ if (length & 7U) {
+ unsigned int trailing = 0U;
+ int remaining = 8U;
+ length &= 7U;
+ PUTBITS2((OPJ_UINT32)pSrc[i + 0], 15)
+ if (length > 1U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 1], 15)
+ if (length > 2U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 2], 15)
+ if (length > 3U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 3], 15)
+ if (length > 4U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 4], 15)
+ if (length > 5U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 5], 15)
+ if (length > 6U) {
+ PUTBITS2((OPJ_UINT32)pSrc[i + 6], 15)
+ }
+ }
+ }
+ }
+ }
+ }
+ FLUSHBITS()
+ }
}
-static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T length)
+static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < length; ++i) {
- pDst[i] = (OPJ_UINT16)pSrc[i];
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < length; ++i) {
+ pDst[i] = (OPJ_UINT16)pSrc[i];
+ }
}
int imagetotif(opj_image_t * image, const char *outfile)
{
- int width, height;
- int bps,adjust, sgnd;
- int tiPhoto;
- TIFF *tif;
- tdata_t buf;
- tsize_t strip_size;
- OPJ_UINT32 i, numcomps;
- OPJ_SIZE_T rowStride;
- OPJ_INT32* buffer32s = NULL;
- OPJ_INT32 const* planes[4];
- convert_32s_PXCX cvtPxToCx = NULL;
- convert_32sXXx_C1R cvt32sToTif = NULL;
-
- bps = (int)image->comps[0].prec;
- planes[0] = image->comps[0].data;
-
- numcomps = image->numcomps;
-
- if (image->color_space == OPJ_CLRSPC_CMYK) {
- if (numcomps < 4U) {
- fprintf(stderr,"imagetotif: CMYK images shall be composed of at least 4 planes.\n");
- fprintf(stderr,"\tAborting\n");
- return 1;
- }
- tiPhoto = PHOTOMETRIC_SEPARATED;
- if (numcomps > 4U) {
- numcomps = 4U; /* Alpha not supported */
- }
- }
- else if (numcomps > 2U) {
- tiPhoto = PHOTOMETRIC_RGB;
- if (numcomps > 4U) {
- numcomps = 4U;
- }
- } else {
- tiPhoto = PHOTOMETRIC_MINISBLACK;
- }
- for (i = 1U; i < numcomps; ++i) {
- if (image->comps[0].dx != image->comps[i].dx) {
- break;
- }
- if (image->comps[0].dy != image->comps[i].dy) {
- break;
- }
- if (image->comps[0].prec != image->comps[i].prec) {
- break;
- }
- if (image->comps[0].sgnd != image->comps[i].sgnd) {
- break;
- }
- planes[i] = image->comps[i].data;
- }
- if (i != numcomps) {
- fprintf(stderr,"imagetotif: All components shall have the same subsampling, same bit depth.\n");
- fprintf(stderr,"\tAborting\n");
- return 1;
- }
-
- if(bps > 16) bps = 0;
- if(bps == 0)
- {
- fprintf(stderr,"imagetotif: Bits=%d, Only 1 to 16 bits implemented\n",bps);
- fprintf(stderr,"\tAborting\n");
- return 1;
- }
- tif = TIFFOpen(outfile, "wb");
- if (!tif)
- {
- fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
- return 1;
- }
- for (i = 0U; i < numcomps; ++i) {
- clip_component(&(image->comps[i]), image->comps[0].prec);
- }
- cvtPxToCx = convert_32s_PXCX_LUT[numcomps];
- switch (bps) {
- case 1:
- case 2:
- case 4:
- case 6:
- case 8:
- cvt32sToTif = convert_32sXXu_C1R_LUT[bps];
- break;
- case 3:
- cvt32sToTif = tif_32sto3u;
- break;
- case 5:
- cvt32sToTif = tif_32sto5u;
- break;
- case 7:
- cvt32sToTif = tif_32sto7u;
- break;
- case 9:
- cvt32sToTif = tif_32sto9u;
- break;
- case 10:
- cvt32sToTif = tif_32sto10u;
- break;
- case 11:
- cvt32sToTif = tif_32sto11u;
- break;
- case 12:
- cvt32sToTif = tif_32sto12u;
- break;
- case 13:
- cvt32sToTif = tif_32sto13u;
- break;
- case 14:
- cvt32sToTif = tif_32sto14u;
- break;
- case 15:
- cvt32sToTif = tif_32sto15u;
- break;
- case 16:
- cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u;
- break;
- default:
- /* never here */
- break;
- }
- sgnd = (int)image->comps[0].sgnd;
- adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
- width = (int)image->comps[0].w;
- height = (int)image->comps[0].h;
-
- TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
- TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
- TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
- TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
- TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
- TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
- TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
- TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
-
- strip_size = TIFFStripSize(tif);
- rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U;
- if (rowStride != (OPJ_SIZE_T)strip_size) {
- fprintf(stderr, "Invalid TIFF strip size\n");
- TIFFClose(tif);
- return 1;
- }
- buf = _TIFFmalloc(strip_size);
- if (buf == NULL) {
- TIFFClose(tif);
- return 1;
- }
- buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
- if (buffer32s == NULL) {
- _TIFFfree(buf);
- TIFFClose(tif);
- return 1;
- }
-
- for (i = 0; i < image->comps[0].h; ++i) {
- cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust);
- cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps);
- (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
- planes[0] += width;
- planes[1] += width;
- planes[2] += width;
- planes[3] += width;
- }
- _TIFFfree((void*)buf);
- TIFFClose(tif);
- free(buffer32s);
-
- return 0;
+ int width, height;
+ int bps, adjust, sgnd;
+ int tiPhoto;
+ TIFF *tif;
+ tdata_t buf;
+ tsize_t strip_size;
+ OPJ_UINT32 i, numcomps;
+ OPJ_SIZE_T rowStride;
+ OPJ_INT32* buffer32s = NULL;
+ OPJ_INT32 const* planes[4];
+ convert_32s_PXCX cvtPxToCx = NULL;
+ convert_32sXXx_C1R cvt32sToTif = NULL;
+
+ bps = (int)image->comps[0].prec;
+ planes[0] = image->comps[0].data;
+
+ numcomps = image->numcomps;
+
+ if (image->color_space == OPJ_CLRSPC_CMYK) {
+ if (numcomps < 4U) {
+ fprintf(stderr,
+ "imagetotif: CMYK images shall be composed of at least 4 planes.\n");
+ fprintf(stderr, "\tAborting\n");
+ return 1;
+ }
+ tiPhoto = PHOTOMETRIC_SEPARATED;
+ if (numcomps > 4U) {
+ numcomps = 4U; /* Alpha not supported */
+ }
+ } else if (numcomps > 2U) {
+ tiPhoto = PHOTOMETRIC_RGB;
+ if (numcomps > 4U) {
+ numcomps = 4U;
+ }
+ } else {
+ tiPhoto = PHOTOMETRIC_MINISBLACK;
+ }
+ for (i = 1U; i < numcomps; ++i) {
+ if (image->comps[0].dx != image->comps[i].dx) {
+ break;
+ }
+ if (image->comps[0].dy != image->comps[i].dy) {
+ break;
+ }
+ if (image->comps[0].prec != image->comps[i].prec) {
+ break;
+ }
+ if (image->comps[0].sgnd != image->comps[i].sgnd) {
+ break;
+ }
+ planes[i] = image->comps[i].data;
+ }
+ if (i != numcomps) {
+ fprintf(stderr,
+ "imagetotif: All components shall have the same subsampling, same bit depth.\n");
+ fprintf(stderr, "\tAborting\n");
+ return 1;
+ }
+
+ if (bps > 16) {
+ bps = 0;
+ }
+ if (bps == 0) {
+ fprintf(stderr, "imagetotif: Bits=%d, Only 1 to 16 bits implemented\n", bps);
+ fprintf(stderr, "\tAborting\n");
+ return 1;
+ }
+ tif = TIFFOpen(outfile, "wb");
+ if (!tif) {
+ fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
+ return 1;
+ }
+ for (i = 0U; i < numcomps; ++i) {
+ clip_component(&(image->comps[i]), image->comps[0].prec);
+ }
+ cvtPxToCx = convert_32s_PXCX_LUT[numcomps];
+ switch (bps) {
+ case 1:
+ case 2:
+ case 4:
+ case 6:
+ case 8:
+ cvt32sToTif = convert_32sXXu_C1R_LUT[bps];
+ break;
+ case 3:
+ cvt32sToTif = tif_32sto3u;
+ break;
+ case 5:
+ cvt32sToTif = tif_32sto5u;
+ break;
+ case 7:
+ cvt32sToTif = tif_32sto7u;
+ break;
+ case 9:
+ cvt32sToTif = tif_32sto9u;
+ break;
+ case 10:
+ cvt32sToTif = tif_32sto10u;
+ break;
+ case 11:
+ cvt32sToTif = tif_32sto11u;
+ break;
+ case 12:
+ cvt32sToTif = tif_32sto12u;
+ break;
+ case 13:
+ cvt32sToTif = tif_32sto13u;
+ break;
+ case 14:
+ cvt32sToTif = tif_32sto14u;
+ break;
+ case 15:
+ cvt32sToTif = tif_32sto15u;
+ break;
+ case 16:
+ cvt32sToTif = (convert_32sXXx_C1R)tif_32sto16u;
+ break;
+ default:
+ /* never here */
+ break;
+ }
+ sgnd = (int)image->comps[0].sgnd;
+ adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
+ width = (int)image->comps[0].w;
+ height = (int)image->comps[0].h;
+
+ TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
+ TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
+ TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
+ TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+ TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+ TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
+ TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
+
+ strip_size = TIFFStripSize(tif);
+ rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U;
+ if (rowStride != (OPJ_SIZE_T)strip_size) {
+ fprintf(stderr, "Invalid TIFF strip size\n");
+ TIFFClose(tif);
+ return 1;
+ }
+ buf = _TIFFmalloc(strip_size);
+ if (buf == NULL) {
+ TIFFClose(tif);
+ return 1;
+ }
+ buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(
+ OPJ_INT32));
+ if (buffer32s == NULL) {
+ _TIFFfree(buf);
+ TIFFClose(tif);
+ return 1;
+ }
+
+ for (i = 0; i < image->comps[0].h; ++i) {
+ cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust);
+ cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps);
+ (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
+ planes[0] += width;
+ planes[1] += width;
+ planes[2] += width;
+ planes[3] += width;
+ }
+ _TIFFfree((void*)buf);
+ TIFFClose(tif);
+ free(buffer32s);
+
+ return 0;
}/* imagetotif() */
#define GETBITS(dest, nb) { \
- int needed = (nb); \
- unsigned int dst = 0U; \
- if (available == 0) { \
- val = *pSrc++; \
- available = 8; \
- } \
- while (needed > available) { \
- dst |= val & ((1U << available) - 1U); \
- needed -= available; \
- dst <<= needed; \
- val = *pSrc++; \
- available = 8; \
- } \
- dst |= (val >> (available - needed)) & ((1U << needed) - 1U); \
- available -= needed; \
- dest = (OPJ_INT32)dst; \
+ int needed = (nb); \
+ unsigned int dst = 0U; \
+ if (available == 0) { \
+ val = *pSrc++; \
+ available = 8; \
+ } \
+ while (needed > available) { \
+ dst |= val & ((1U << available) - 1U); \
+ needed -= available; \
+ dst <<= needed; \
+ val = *pSrc++; \
+ available = 8; \
+ } \
+ dst |= (val >> (available - needed)) & ((1U << needed) - 1U); \
+ available -= needed; \
+ dest = (OPJ_INT32)dst; \
}
-static void tif_3uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_3uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 >> 5));
- pDst[i+1] = (OPJ_INT32)(((val0 & 0x1FU) >> 2));
- pDst[i+2] = (OPJ_INT32)(((val0 & 0x3U) << 1) | (val1 >> 7));
- pDst[i+3] = (OPJ_INT32)(((val1 & 0x7FU) >> 4));
- pDst[i+4] = (OPJ_INT32)(((val1 & 0xFU) >> 1));
- pDst[i+5] = (OPJ_INT32)(((val1 & 0x1U) << 2) | (val2 >> 6));
- pDst[i+6] = (OPJ_INT32)(((val2 & 0x3FU) >> 3));
- pDst[i+7] = (OPJ_INT32)(((val2 & 0x7U)));
-
- }
- if (length & 7U) {
- unsigned int val;
- int available = 0;
-
- length = length & 7U;
-
- GETBITS(pDst[i+0], 3)
-
- if (length > 1U) {
- GETBITS(pDst[i+1], 3)
- if (length > 2U) {
- GETBITS(pDst[i+2], 3)
- if (length > 3U) {
- GETBITS(pDst[i+3], 3)
- if (length > 4U) {
- GETBITS(pDst[i+4], 3)
- if (length > 5U) {
- GETBITS(pDst[i+5], 3)
- if (length > 6U) {
- GETBITS(pDst[i+6], 3)
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 >> 5));
+ pDst[i + 1] = (OPJ_INT32)(((val0 & 0x1FU) >> 2));
+ pDst[i + 2] = (OPJ_INT32)(((val0 & 0x3U) << 1) | (val1 >> 7));
+ pDst[i + 3] = (OPJ_INT32)(((val1 & 0x7FU) >> 4));
+ pDst[i + 4] = (OPJ_INT32)(((val1 & 0xFU) >> 1));
+ pDst[i + 5] = (OPJ_INT32)(((val1 & 0x1U) << 2) | (val2 >> 6));
+ pDst[i + 6] = (OPJ_INT32)(((val2 & 0x3FU) >> 3));
+ pDst[i + 7] = (OPJ_INT32)(((val2 & 0x7U)));
+
+ }
+ if (length & 7U) {
+ unsigned int val;
+ int available = 0;
+
+ length = length & 7U;
+
+ GETBITS(pDst[i + 0], 3)
+
+ if (length > 1U) {
+ GETBITS(pDst[i + 1], 3)
+ if (length > 2U) {
+ GETBITS(pDst[i + 2], 3)
+ if (length > 3U) {
+ GETBITS(pDst[i + 3], 3)
+ if (length > 4U) {
+ GETBITS(pDst[i + 4], 3)
+ if (length > 5U) {
+ GETBITS(pDst[i + 5], 3)
+ if (length > 6U) {
+ GETBITS(pDst[i + 6], 3)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
-static void tif_5uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_5uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 >> 3));
- pDst[i+1] = (OPJ_INT32)(((val0 & 0x7U) << 2) | (val1 >> 6));
- pDst[i+2] = (OPJ_INT32)(((val1 & 0x3FU) >> 1));
- pDst[i+3] = (OPJ_INT32)(((val1 & 0x1U) << 4) | (val2 >> 4));
- pDst[i+4] = (OPJ_INT32)(((val2 & 0xFU) << 1) | (val3 >> 7));
- pDst[i+5] = (OPJ_INT32)(((val3 & 0x7FU) >> 2));
- pDst[i+6] = (OPJ_INT32)(((val3 & 0x3U) << 3) | (val4 >> 5));
- pDst[i+7] = (OPJ_INT32)(((val4 & 0x1FU)));
-
- }
- if (length & 7U) {
- unsigned int val;
- int available = 0;
-
- length = length & 7U;
-
- GETBITS(pDst[i+0], 5)
-
- if (length > 1U) {
- GETBITS(pDst[i+1], 5)
- if (length > 2U) {
- GETBITS(pDst[i+2], 5)
- if (length > 3U) {
- GETBITS(pDst[i+3], 5)
- if (length > 4U) {
- GETBITS(pDst[i+4], 5)
- if (length > 5U) {
- GETBITS(pDst[i+5], 5)
- if (length > 6U) {
- GETBITS(pDst[i+6], 5)
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 >> 3));
+ pDst[i + 1] = (OPJ_INT32)(((val0 & 0x7U) << 2) | (val1 >> 6));
+ pDst[i + 2] = (OPJ_INT32)(((val1 & 0x3FU) >> 1));
+ pDst[i + 3] = (OPJ_INT32)(((val1 & 0x1U) << 4) | (val2 >> 4));
+ pDst[i + 4] = (OPJ_INT32)(((val2 & 0xFU) << 1) | (val3 >> 7));
+ pDst[i + 5] = (OPJ_INT32)(((val3 & 0x7FU) >> 2));
+ pDst[i + 6] = (OPJ_INT32)(((val3 & 0x3U) << 3) | (val4 >> 5));
+ pDst[i + 7] = (OPJ_INT32)(((val4 & 0x1FU)));
+
+ }
+ if (length & 7U) {
+ unsigned int val;
+ int available = 0;
+
+ length = length & 7U;
+
+ GETBITS(pDst[i + 0], 5)
+
+ if (length > 1U) {
+ GETBITS(pDst[i + 1], 5)
+ if (length > 2U) {
+ GETBITS(pDst[i + 2], 5)
+ if (length > 3U) {
+ GETBITS(pDst[i + 3], 5)
+ if (length > 4U) {
+ GETBITS(pDst[i + 4], 5)
+ if (length > 5U) {
+ GETBITS(pDst[i + 5], 5)
+ if (length > 6U) {
+ GETBITS(pDst[i + 6], 5)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
-static void tif_7uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_7uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
- OPJ_UINT32 val5 = *pSrc++;
- OPJ_UINT32 val6 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 >> 1));
- pDst[i+1] = (OPJ_INT32)(((val0 & 0x1U) << 6) | (val1 >> 2));
- pDst[i+2] = (OPJ_INT32)(((val1 & 0x3U) << 5) | (val2 >> 3));
- pDst[i+3] = (OPJ_INT32)(((val2 & 0x7U) << 4) | (val3 >> 4));
- pDst[i+4] = (OPJ_INT32)(((val3 & 0xFU) << 3) | (val4 >> 5));
- pDst[i+5] = (OPJ_INT32)(((val4 & 0x1FU) << 2) | (val5 >> 6));
- pDst[i+6] = (OPJ_INT32)(((val5 & 0x3FU) << 1) | (val6 >> 7));
- pDst[i+7] = (OPJ_INT32)(((val6 & 0x7FU)));
-
- }
- if (length & 7U) {
- unsigned int val;
- int available = 0;
-
- length = length & 7U;
-
- GETBITS(pDst[i+0], 7)
-
- if (length > 1U) {
- GETBITS(pDst[i+1], 7)
- if (length > 2U) {
- GETBITS(pDst[i+2], 7)
- if (length > 3U) {
- GETBITS(pDst[i+3], 7)
- if (length > 4U) {
- GETBITS(pDst[i+4], 7)
- if (length > 5U) {
- GETBITS(pDst[i+5], 7)
- if (length > 6U) {
- GETBITS(pDst[i+6], 7)
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+ OPJ_UINT32 val5 = *pSrc++;
+ OPJ_UINT32 val6 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 >> 1));
+ pDst[i + 1] = (OPJ_INT32)(((val0 & 0x1U) << 6) | (val1 >> 2));
+ pDst[i + 2] = (OPJ_INT32)(((val1 & 0x3U) << 5) | (val2 >> 3));
+ pDst[i + 3] = (OPJ_INT32)(((val2 & 0x7U) << 4) | (val3 >> 4));
+ pDst[i + 4] = (OPJ_INT32)(((val3 & 0xFU) << 3) | (val4 >> 5));
+ pDst[i + 5] = (OPJ_INT32)(((val4 & 0x1FU) << 2) | (val5 >> 6));
+ pDst[i + 6] = (OPJ_INT32)(((val5 & 0x3FU) << 1) | (val6 >> 7));
+ pDst[i + 7] = (OPJ_INT32)(((val6 & 0x7FU)));
+
+ }
+ if (length & 7U) {
+ unsigned int val;
+ int available = 0;
+
+ length = length & 7U;
+
+ GETBITS(pDst[i + 0], 7)
+
+ if (length > 1U) {
+ GETBITS(pDst[i + 1], 7)
+ if (length > 2U) {
+ GETBITS(pDst[i + 2], 7)
+ if (length > 3U) {
+ GETBITS(pDst[i + 3], 7)
+ if (length > 4U) {
+ GETBITS(pDst[i + 4], 7)
+ if (length > 5U) {
+ GETBITS(pDst[i + 5], 7)
+ if (length > 6U) {
+ GETBITS(pDst[i + 6], 7)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
-static void tif_9uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_9uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
- OPJ_UINT32 val5 = *pSrc++;
- OPJ_UINT32 val6 = *pSrc++;
- OPJ_UINT32 val7 = *pSrc++;
- OPJ_UINT32 val8 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 << 1) | (val1 >> 7));
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x7FU) << 2) | (val2 >> 6));
- pDst[i+2] = (OPJ_INT32)(((val2 & 0x3FU) << 3) | (val3 >> 5));
- pDst[i+3] = (OPJ_INT32)(((val3 & 0x1FU) << 4) | (val4 >> 4));
- pDst[i+4] = (OPJ_INT32)(((val4 & 0xFU) << 5) | (val5 >> 3));
- pDst[i+5] = (OPJ_INT32)(((val5 & 0x7U) << 6) | (val6 >> 2));
- pDst[i+6] = (OPJ_INT32)(((val6 & 0x3U) << 7) | (val7 >> 1));
- pDst[i+7] = (OPJ_INT32)(((val7 & 0x1U) << 8) | (val8));
-
- }
- if (length & 7U) {
- unsigned int val;
- int available = 0;
-
- length = length & 7U;
-
- GETBITS(pDst[i+0], 9)
-
- if (length > 1U) {
- GETBITS(pDst[i+1], 9)
- if (length > 2U) {
- GETBITS(pDst[i+2], 9)
- if (length > 3U) {
- GETBITS(pDst[i+3], 9)
- if (length > 4U) {
- GETBITS(pDst[i+4], 9)
- if (length > 5U) {
- GETBITS(pDst[i+5], 9)
- if (length > 6U) {
- GETBITS(pDst[i+6], 9)
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+ OPJ_UINT32 val5 = *pSrc++;
+ OPJ_UINT32 val6 = *pSrc++;
+ OPJ_UINT32 val7 = *pSrc++;
+ OPJ_UINT32 val8 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 << 1) | (val1 >> 7));
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x7FU) << 2) | (val2 >> 6));
+ pDst[i + 2] = (OPJ_INT32)(((val2 & 0x3FU) << 3) | (val3 >> 5));
+ pDst[i + 3] = (OPJ_INT32)(((val3 & 0x1FU) << 4) | (val4 >> 4));
+ pDst[i + 4] = (OPJ_INT32)(((val4 & 0xFU) << 5) | (val5 >> 3));
+ pDst[i + 5] = (OPJ_INT32)(((val5 & 0x7U) << 6) | (val6 >> 2));
+ pDst[i + 6] = (OPJ_INT32)(((val6 & 0x3U) << 7) | (val7 >> 1));
+ pDst[i + 7] = (OPJ_INT32)(((val7 & 0x1U) << 8) | (val8));
+
+ }
+ if (length & 7U) {
+ unsigned int val;
+ int available = 0;
+
+ length = length & 7U;
+
+ GETBITS(pDst[i + 0], 9)
+
+ if (length > 1U) {
+ GETBITS(pDst[i + 1], 9)
+ if (length > 2U) {
+ GETBITS(pDst[i + 2], 9)
+ if (length > 3U) {
+ GETBITS(pDst[i + 3], 9)
+ if (length > 4U) {
+ GETBITS(pDst[i + 4], 9)
+ if (length > 5U) {
+ GETBITS(pDst[i + 5], 9)
+ if (length > 6U) {
+ GETBITS(pDst[i + 6], 9)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
-static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
- pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
- pDst[i+3] = (OPJ_INT32)(((val3 & 0x3U) << 8) | val4);
-
- }
- if (length & 3U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- length = length & 3U;
- pDst[i+0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
-
- if (length > 1U) {
- OPJ_UINT32 val2 = *pSrc++;
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
- if (length > 2U) {
- OPJ_UINT32 val3 = *pSrc++;
- pDst[i+2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
+ pDst[i + 2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
+ pDst[i + 3] = (OPJ_INT32)(((val3 & 0x3U) << 8) | val4);
+
+ }
+ if (length & 3U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ length = length & 3U;
+ pDst[i + 0] = (OPJ_INT32)((val0 << 2) | (val1 >> 6));
+
+ if (length > 1U) {
+ OPJ_UINT32 val2 = *pSrc++;
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x3FU) << 4) | (val2 >> 4));
+ if (length > 2U) {
+ OPJ_UINT32 val3 = *pSrc++;
+ pDst[i + 2] = (OPJ_INT32)(((val2 & 0xFU) << 6) | (val3 >> 2));
+ }
+ }
+ }
}
-static void tif_11uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_11uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
- OPJ_UINT32 val5 = *pSrc++;
- OPJ_UINT32 val6 = *pSrc++;
- OPJ_UINT32 val7 = *pSrc++;
- OPJ_UINT32 val8 = *pSrc++;
- OPJ_UINT32 val9 = *pSrc++;
- OPJ_UINT32 val10 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 << 3) | (val1 >> 5));
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x1FU) << 6) | (val2 >> 2));
- pDst[i+2] = (OPJ_INT32)(((val2 & 0x3U) << 9) | (val3 << 1) | (val4 >> 7));
- pDst[i+3] = (OPJ_INT32)(((val4 & 0x7FU) << 4) | (val5 >> 4));
- pDst[i+4] = (OPJ_INT32)(((val5 & 0xFU) << 7) | (val6 >> 1));
- pDst[i+5] = (OPJ_INT32)(((val6 & 0x1U) << 10) | (val7 << 2) | (val8 >> 6));
- pDst[i+6] = (OPJ_INT32)(((val8 & 0x3FU) << 5) | (val9 >> 3));
- pDst[i+7] = (OPJ_INT32)(((val9 & 0x7U) << 8) | (val10));
-
- }
- if (length & 7U) {
- unsigned int val;
- int available = 0;
-
- length = length & 7U;
-
- GETBITS(pDst[i+0], 11)
-
- if (length > 1U) {
- GETBITS(pDst[i+1], 11)
- if (length > 2U) {
- GETBITS(pDst[i+2], 11)
- if (length > 3U) {
- GETBITS(pDst[i+3], 11)
- if (length > 4U) {
- GETBITS(pDst[i+4], 11)
- if (length > 5U) {
- GETBITS(pDst[i+5], 11)
- if (length > 6U) {
- GETBITS(pDst[i+6], 11)
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+ OPJ_UINT32 val5 = *pSrc++;
+ OPJ_UINT32 val6 = *pSrc++;
+ OPJ_UINT32 val7 = *pSrc++;
+ OPJ_UINT32 val8 = *pSrc++;
+ OPJ_UINT32 val9 = *pSrc++;
+ OPJ_UINT32 val10 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 << 3) | (val1 >> 5));
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x1FU) << 6) | (val2 >> 2));
+ pDst[i + 2] = (OPJ_INT32)(((val2 & 0x3U) << 9) | (val3 << 1) | (val4 >> 7));
+ pDst[i + 3] = (OPJ_INT32)(((val4 & 0x7FU) << 4) | (val5 >> 4));
+ pDst[i + 4] = (OPJ_INT32)(((val5 & 0xFU) << 7) | (val6 >> 1));
+ pDst[i + 5] = (OPJ_INT32)(((val6 & 0x1U) << 10) | (val7 << 2) | (val8 >> 6));
+ pDst[i + 6] = (OPJ_INT32)(((val8 & 0x3FU) << 5) | (val9 >> 3));
+ pDst[i + 7] = (OPJ_INT32)(((val9 & 0x7U) << 8) | (val10));
+
+ }
+ if (length & 7U) {
+ unsigned int val;
+ int available = 0;
+
+ length = length & 7U;
+
+ GETBITS(pDst[i + 0], 11)
+
+ if (length > 1U) {
+ GETBITS(pDst[i + 1], 11)
+ if (length > 2U) {
+ GETBITS(pDst[i + 2], 11)
+ if (length > 3U) {
+ GETBITS(pDst[i + 3], 11)
+ if (length > 4U) {
+ GETBITS(pDst[i + 4], 11)
+ if (length > 5U) {
+ GETBITS(pDst[i + 5], 11)
+ if (length > 6U) {
+ GETBITS(pDst[i + 6], 11)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
-static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
- pDst[i+1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
- }
- if (length & 1U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i += 2U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0xFU) << 8) | val2);
+ }
+ if (length & 1U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ pDst[i + 0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
+ }
}
-static void tif_13uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_13uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
- OPJ_UINT32 val5 = *pSrc++;
- OPJ_UINT32 val6 = *pSrc++;
- OPJ_UINT32 val7 = *pSrc++;
- OPJ_UINT32 val8 = *pSrc++;
- OPJ_UINT32 val9 = *pSrc++;
- OPJ_UINT32 val10 = *pSrc++;
- OPJ_UINT32 val11 = *pSrc++;
- OPJ_UINT32 val12 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 << 5) | (val1 >> 3));
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x7U) << 10) | (val2 << 2) | (val3 >> 6));
- pDst[i+2] = (OPJ_INT32)(((val3 & 0x3FU) << 7) | (val4 >> 1));
- pDst[i+3] = (OPJ_INT32)(((val4 & 0x1U) << 12) | (val5 << 4) | (val6 >> 4));
- pDst[i+4] = (OPJ_INT32)(((val6 & 0xFU) << 9) | (val7 << 1) | (val8 >> 7));
- pDst[i+5] = (OPJ_INT32)(((val8 & 0x7FU) << 6) | (val9 >> 2));
- pDst[i+6] = (OPJ_INT32)(((val9 & 0x3U) << 11) | (val10 << 3) | (val11 >> 5));
- pDst[i+7] = (OPJ_INT32)(((val11 & 0x1FU) << 8) | (val12));
-
- }
- if (length & 7U) {
- unsigned int val;
- int available = 0;
-
- length = length & 7U;
-
- GETBITS(pDst[i+0], 13)
-
- if (length > 1U) {
- GETBITS(pDst[i+1], 13)
- if (length > 2U) {
- GETBITS(pDst[i+2], 13)
- if (length > 3U) {
- GETBITS(pDst[i+3], 13)
- if (length > 4U) {
- GETBITS(pDst[i+4], 13)
- if (length > 5U) {
- GETBITS(pDst[i+5], 13)
- if (length > 6U) {
- GETBITS(pDst[i+6], 13)
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+ OPJ_UINT32 val5 = *pSrc++;
+ OPJ_UINT32 val6 = *pSrc++;
+ OPJ_UINT32 val7 = *pSrc++;
+ OPJ_UINT32 val8 = *pSrc++;
+ OPJ_UINT32 val9 = *pSrc++;
+ OPJ_UINT32 val10 = *pSrc++;
+ OPJ_UINT32 val11 = *pSrc++;
+ OPJ_UINT32 val12 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 << 5) | (val1 >> 3));
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x7U) << 10) | (val2 << 2) | (val3 >> 6));
+ pDst[i + 2] = (OPJ_INT32)(((val3 & 0x3FU) << 7) | (val4 >> 1));
+ pDst[i + 3] = (OPJ_INT32)(((val4 & 0x1U) << 12) | (val5 << 4) | (val6 >> 4));
+ pDst[i + 4] = (OPJ_INT32)(((val6 & 0xFU) << 9) | (val7 << 1) | (val8 >> 7));
+ pDst[i + 5] = (OPJ_INT32)(((val8 & 0x7FU) << 6) | (val9 >> 2));
+ pDst[i + 6] = (OPJ_INT32)(((val9 & 0x3U) << 11) | (val10 << 3) | (val11 >> 5));
+ pDst[i + 7] = (OPJ_INT32)(((val11 & 0x1FU) << 8) | (val12));
+
+ }
+ if (length & 7U) {
+ unsigned int val;
+ int available = 0;
+
+ length = length & 7U;
+
+ GETBITS(pDst[i + 0], 13)
+
+ if (length > 1U) {
+ GETBITS(pDst[i + 1], 13)
+ if (length > 2U) {
+ GETBITS(pDst[i + 2], 13)
+ if (length > 3U) {
+ GETBITS(pDst[i + 3], 13)
+ if (length > 4U) {
+ GETBITS(pDst[i + 4], 13)
+ if (length > 5U) {
+ GETBITS(pDst[i + 5], 13)
+ if (length > 6U) {
+ GETBITS(pDst[i + 6], 13)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
-static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
- OPJ_UINT32 val5 = *pSrc++;
- OPJ_UINT32 val6 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
- pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
- pDst[i+3] = (OPJ_INT32)(((val5 & 0x3FU) << 8) | val6);
-
- }
- if (length & 3U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- length = length & 3U;
- pDst[i+0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
-
- if (length > 1U) {
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
- if (length > 2U) {
- OPJ_UINT32 val4 = *pSrc++;
- OPJ_UINT32 val5 = *pSrc++;
- pDst[i+2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i += 4U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+ OPJ_UINT32 val5 = *pSrc++;
+ OPJ_UINT32 val6 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
+ pDst[i + 2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
+ pDst[i + 3] = (OPJ_INT32)(((val5 & 0x3FU) << 8) | val6);
+
+ }
+ if (length & 3U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ length = length & 3U;
+ pDst[i + 0] = (OPJ_INT32)((val0 << 6) | (val1 >> 2));
+
+ if (length > 1U) {
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x3U) << 12) | (val2 << 4) | (val3 >> 4));
+ if (length > 2U) {
+ OPJ_UINT32 val4 = *pSrc++;
+ OPJ_UINT32 val5 = *pSrc++;
+ pDst[i + 2] = (OPJ_INT32)(((val3 & 0xFU) << 10) | (val4 << 2) | (val5 >> 6));
+ }
+ }
+ }
}
-static void tif_15uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_15uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
- OPJ_UINT32 val0 = *pSrc++;
- OPJ_UINT32 val1 = *pSrc++;
- OPJ_UINT32 val2 = *pSrc++;
- OPJ_UINT32 val3 = *pSrc++;
- OPJ_UINT32 val4 = *pSrc++;
- OPJ_UINT32 val5 = *pSrc++;
- OPJ_UINT32 val6 = *pSrc++;
- OPJ_UINT32 val7 = *pSrc++;
- OPJ_UINT32 val8 = *pSrc++;
- OPJ_UINT32 val9 = *pSrc++;
- OPJ_UINT32 val10 = *pSrc++;
- OPJ_UINT32 val11 = *pSrc++;
- OPJ_UINT32 val12 = *pSrc++;
- OPJ_UINT32 val13 = *pSrc++;
- OPJ_UINT32 val14 = *pSrc++;
-
- pDst[i+0] = (OPJ_INT32)((val0 << 7) | (val1 >> 1));
- pDst[i+1] = (OPJ_INT32)(((val1 & 0x1U) << 14) | (val2 << 6) | (val3 >> 2));
- pDst[i+2] = (OPJ_INT32)(((val3 & 0x3U) << 13) | (val4 << 5) | (val5 >> 3));
- pDst[i+3] = (OPJ_INT32)(((val5 & 0x7U) << 12) | (val6 << 4) | (val7 >> 4));
- pDst[i+4] = (OPJ_INT32)(((val7 & 0xFU) << 11) | (val8 << 3) | (val9 >> 5));
- pDst[i+5] = (OPJ_INT32)(((val9 & 0x1FU) << 10) | (val10 << 2) | (val11 >> 6));
- pDst[i+6] = (OPJ_INT32)(((val11 & 0x3FU) << 9) | (val12 << 1) | (val13 >> 7));
- pDst[i+7] = (OPJ_INT32)(((val13 & 0x7FU) << 8) | (val14));
-
- }
- if (length & 7U) {
- unsigned int val;
- int available = 0;
-
- length = length & 7U;
-
- GETBITS(pDst[i+0], 15)
-
- if (length > 1U) {
- GETBITS(pDst[i+1], 15)
- if (length > 2U) {
- GETBITS(pDst[i+2], 15)
- if (length > 3U) {
- GETBITS(pDst[i+3], 15)
- if (length > 4U) {
- GETBITS(pDst[i+4], 15)
- if (length > 5U) {
- GETBITS(pDst[i+5], 15)
- if (length > 6U) {
- GETBITS(pDst[i+6], 15)
- }
- }
- }
- }
- }
- }
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i += 8U) {
+ OPJ_UINT32 val0 = *pSrc++;
+ OPJ_UINT32 val1 = *pSrc++;
+ OPJ_UINT32 val2 = *pSrc++;
+ OPJ_UINT32 val3 = *pSrc++;
+ OPJ_UINT32 val4 = *pSrc++;
+ OPJ_UINT32 val5 = *pSrc++;
+ OPJ_UINT32 val6 = *pSrc++;
+ OPJ_UINT32 val7 = *pSrc++;
+ OPJ_UINT32 val8 = *pSrc++;
+ OPJ_UINT32 val9 = *pSrc++;
+ OPJ_UINT32 val10 = *pSrc++;
+ OPJ_UINT32 val11 = *pSrc++;
+ OPJ_UINT32 val12 = *pSrc++;
+ OPJ_UINT32 val13 = *pSrc++;
+ OPJ_UINT32 val14 = *pSrc++;
+
+ pDst[i + 0] = (OPJ_INT32)((val0 << 7) | (val1 >> 1));
+ pDst[i + 1] = (OPJ_INT32)(((val1 & 0x1U) << 14) | (val2 << 6) | (val3 >> 2));
+ pDst[i + 2] = (OPJ_INT32)(((val3 & 0x3U) << 13) | (val4 << 5) | (val5 >> 3));
+ pDst[i + 3] = (OPJ_INT32)(((val5 & 0x7U) << 12) | (val6 << 4) | (val7 >> 4));
+ pDst[i + 4] = (OPJ_INT32)(((val7 & 0xFU) << 11) | (val8 << 3) | (val9 >> 5));
+ pDst[i + 5] = (OPJ_INT32)(((val9 & 0x1FU) << 10) | (val10 << 2) | (val11 >> 6));
+ pDst[i + 6] = (OPJ_INT32)(((val11 & 0x3FU) << 9) | (val12 << 1) | (val13 >> 7));
+ pDst[i + 7] = (OPJ_INT32)(((val13 & 0x7FU) << 8) | (val14));
+
+ }
+ if (length & 7U) {
+ unsigned int val;
+ int available = 0;
+
+ length = length & 7U;
+
+ GETBITS(pDst[i + 0], 15)
+
+ if (length > 1U) {
+ GETBITS(pDst[i + 1], 15)
+ if (length > 2U) {
+ GETBITS(pDst[i + 2], 15)
+ if (length > 3U) {
+ GETBITS(pDst[i + 3], 15)
+ if (length > 4U) {
+ GETBITS(pDst[i + 4], 15)
+ if (length > 5U) {
+ GETBITS(pDst[i + 5], 15)
+ if (length > 6U) {
+ GETBITS(pDst[i + 6], 15)
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
/* seems that libtiff decodes this to machine endianness */
-static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst,
+ OPJ_SIZE_T length)
{
- OPJ_SIZE_T i;
- for (i = 0; i < length; i++) {
- pDst[i] = pSrc[i];
- }
+ OPJ_SIZE_T i;
+ for (i = 0; i < length; i++) {
+ pDst[i] = pSrc[i];
+ }
}
/*
@@ -1206,252 +1230,245 @@ static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T len
*/
opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
{
- int subsampling_dx = parameters->subsampling_dx;
- int subsampling_dy = parameters->subsampling_dy;
- TIFF *tif;
- tdata_t buf;
- tstrip_t strip;
- tsize_t strip_size;
- int j, currentPlane, numcomps = 0, w, h;
- OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
- opj_image_cmptparm_t cmptparm[4]; /* RGBA */
- opj_image_t *image = NULL;
- int has_alpha = 0;
- unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
- unsigned int tiWidth, tiHeight;
- OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
- convert_XXx32s_C1R cvtTifTo32s = NULL;
- convert_32s_CXPX cvtCxToPx = NULL;
- OPJ_INT32* buffer32s = NULL;
- OPJ_INT32* planes[4];
- OPJ_SIZE_T rowStride;
-
- tif = TIFFOpen(filename, "r");
-
- if(!tif)
- {
- fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
- return 0;
- }
- tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
- tiWidth = tiHeight = 0;
-
- TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
- TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
- TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
- TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
- TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
- TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
- TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
- w= (int)tiWidth;
- h= (int)tiHeight;
-
- if(tiBps > 16U) {
- fprintf(stderr,"tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n",tiBps);
- fprintf(stderr,"\tAborting\n");
- TIFFClose(tif);
- return NULL;
- }
- if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
- fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
- fprintf(stderr,"\tAborting\n");
- TIFFClose(tif);
- return NULL;
- }
-
- switch (tiBps) {
- case 1:
- case 2:
- case 4:
- case 6:
- case 8:
- cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps];
- break;
- /* others are specific to TIFF */
- case 3:
- cvtTifTo32s = tif_3uto32s;
- break;
- case 5:
- cvtTifTo32s = tif_5uto32s;
- break;
- case 7:
- cvtTifTo32s = tif_7uto32s;
- break;
- case 9:
- cvtTifTo32s = tif_9uto32s;
- break;
- case 10:
- cvtTifTo32s = tif_10uto32s;
- break;
- case 11:
- cvtTifTo32s = tif_11uto32s;
- break;
- case 12:
- cvtTifTo32s = tif_12uto32s;
- break;
- case 13:
- cvtTifTo32s = tif_13uto32s;
- break;
- case 14:
- cvtTifTo32s = tif_14uto32s;
- break;
- case 15:
- cvtTifTo32s = tif_15uto32s;
- break;
- case 16:
- cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s;
- break;
- default:
- /* never here */
- break;
- }
-
- {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
- uint16* sampleinfo;
- uint16 extrasamples;
-
- TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
- &extrasamples, &sampleinfo);
-
- if(extrasamples >= 1)
- {
- switch(sampleinfo[0])
- {
- case EXTRASAMPLE_UNSPECIFIED:
- /* Workaround for some images without correct info about alpha channel
- */
- if(tiSpp > 3)
- has_alpha = 1;
- break;
-
- case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
- case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
- has_alpha = 1;
- break;
- }
- }
- else /* extrasamples == 0 */
- if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
- }
-
- /* initialize image components */
- memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
-
- if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
- fprintf(stdout,"WARNING:\n"
- "Input image bitdepth is %d bits\n"
- "TIF conversion has automatically rescaled to 12-bits\n"
- "to comply with cinema profiles.\n",
- tiBps);
- } else {
- is_cinema = 0U;
- }
-
- if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
- {
- numcomps = 3 + has_alpha;
- color_space = OPJ_CLRSPC_SRGB;
- }
- else if (tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
- {
- numcomps = 1 + has_alpha;
- color_space = OPJ_CLRSPC_GRAY;
- }
-
- cvtCxToPx = convert_32s_CXPX_LUT[numcomps];
- if (tiPC == PLANARCONFIG_SEPARATE) {
- cvtCxToPx = convert_32s_CXPX_LUT[1]; /* override */
- tiSpp = 1U; /* consider only one sample per plane */
- }
-
- for(j = 0; j < numcomps; j++)
- {
- cmptparm[j].prec = tiBps;
- cmptparm[j].bpp = tiBps;
- cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
- cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
- cmptparm[j].w = (OPJ_UINT32)w;
- cmptparm[j].h = (OPJ_UINT32)h;
- }
-
- image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
- if(!image)
- {
- TIFFClose(tif);
- return NULL;
- }
- /* set image offset and reference grid */
- image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
- image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
- image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
- image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
- image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
- image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
-
- for(j = 0; j < numcomps; j++)
- {
- planes[j] = image->comps[j].data;
- }
- image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
-
- strip_size = TIFFStripSize(tif);
-
- buf = _TIFFmalloc(strip_size);
- if (buf == NULL) {
- TIFFClose(tif);
- opj_image_destroy(image);
- return NULL;
- }
- rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
- buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
- if (buffer32s == NULL) {
- _TIFFfree(buf);
- TIFFClose(tif);
- opj_image_destroy(image);
- return NULL;
- }
-
- strip = 0;
- currentPlane = 0;
- do
- {
- planes[0] = image->comps[currentPlane].data; /* to manage planar data */
- h= (int)tiHeight;
- /* Read the Image components */
- for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
- {
- const OPJ_UINT8 *dat8;
- OPJ_SIZE_T ssize;
-
- ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
- dat8 = (const OPJ_UINT8*)buf;
-
- while (ssize >= rowStride) {
- cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
- cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
- planes[0] += w;
- planes[1] += w;
- planes[2] += w;
- planes[3] += w;
- dat8 += rowStride;
- ssize -= rowStride;
- h--;
- }
- }
- currentPlane++;
- } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
-
- free(buffer32s);
- _TIFFfree(buf);
- TIFFClose(tif);
-
- if (is_cinema) {
- for (j=0; j < numcomps; ++j) {
- scale_component(&(image->comps[j]), 12);
- }
-
- }
- return image;
+ int subsampling_dx = parameters->subsampling_dx;
+ int subsampling_dy = parameters->subsampling_dy;
+ TIFF *tif;
+ tdata_t buf;
+ tstrip_t strip;
+ tsize_t strip_size;
+ int j, currentPlane, numcomps = 0, w, h;
+ OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
+ opj_image_cmptparm_t cmptparm[4]; /* RGBA */
+ opj_image_t *image = NULL;
+ int has_alpha = 0;
+ unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
+ unsigned int tiWidth, tiHeight;
+ OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
+ convert_XXx32s_C1R cvtTifTo32s = NULL;
+ convert_32s_CXPX cvtCxToPx = NULL;
+ OPJ_INT32* buffer32s = NULL;
+ OPJ_INT32* planes[4];
+ OPJ_SIZE_T rowStride;
+
+ tif = TIFFOpen(filename, "r");
+
+ if (!tif) {
+ fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
+ return 0;
+ }
+ tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
+ tiWidth = tiHeight = 0;
+
+ TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
+ TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
+ TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
+ TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
+ TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
+ TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
+ TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
+ w = (int)tiWidth;
+ h = (int)tiHeight;
+
+ if (tiBps > 16U) {
+ fprintf(stderr, "tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n", tiBps);
+ fprintf(stderr, "\tAborting\n");
+ TIFFClose(tif);
+ return NULL;
+ }
+ if (tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
+ fprintf(stderr,
+ "tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",
+ (int) tiPhoto);
+ fprintf(stderr, "\tAborting\n");
+ TIFFClose(tif);
+ return NULL;
+ }
+
+ switch (tiBps) {
+ case 1:
+ case 2:
+ case 4:
+ case 6:
+ case 8:
+ cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps];
+ break;
+ /* others are specific to TIFF */
+ case 3:
+ cvtTifTo32s = tif_3uto32s;
+ break;
+ case 5:
+ cvtTifTo32s = tif_5uto32s;
+ break;
+ case 7:
+ cvtTifTo32s = tif_7uto32s;
+ break;
+ case 9:
+ cvtTifTo32s = tif_9uto32s;
+ break;
+ case 10:
+ cvtTifTo32s = tif_10uto32s;
+ break;
+ case 11:
+ cvtTifTo32s = tif_11uto32s;
+ break;
+ case 12:
+ cvtTifTo32s = tif_12uto32s;
+ break;
+ case 13:
+ cvtTifTo32s = tif_13uto32s;
+ break;
+ case 14:
+ cvtTifTo32s = tif_14uto32s;
+ break;
+ case 15:
+ cvtTifTo32s = tif_15uto32s;
+ break;
+ case 16:
+ cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s;
+ break;
+ default:
+ /* never here */
+ break;
+ }
+
+ {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
+ uint16* sampleinfo;
+ uint16 extrasamples;
+
+ TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
+ &extrasamples, &sampleinfo);
+
+ if (extrasamples >= 1) {
+ switch (sampleinfo[0]) {
+ case EXTRASAMPLE_UNSPECIFIED:
+ /* Workaround for some images without correct info about alpha channel
+ */
+ if (tiSpp > 3) {
+ has_alpha = 1;
+ }
+ break;
+
+ case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
+ case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
+ has_alpha = 1;
+ break;
+ }
+ } else /* extrasamples == 0 */
+ if (tiSpp == 4 || tiSpp == 2) {
+ has_alpha = 1;
+ }
+ }
+
+ /* initialize image components */
+ memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
+
+ if ((tiPhoto == PHOTOMETRIC_RGB) && (is_cinema) && (tiBps != 12U)) {
+ fprintf(stdout, "WARNING:\n"
+ "Input image bitdepth is %d bits\n"
+ "TIF conversion has automatically rescaled to 12-bits\n"
+ "to comply with cinema profiles.\n",
+ tiBps);
+ } else {
+ is_cinema = 0U;
+ }
+
+ if (tiPhoto == PHOTOMETRIC_RGB) { /* RGB(A) */
+ numcomps = 3 + has_alpha;
+ color_space = OPJ_CLRSPC_SRGB;
+ } else if (tiPhoto == PHOTOMETRIC_MINISBLACK) { /* GRAY(A) */
+ numcomps = 1 + has_alpha;
+ color_space = OPJ_CLRSPC_GRAY;
+ }
+
+ cvtCxToPx = convert_32s_CXPX_LUT[numcomps];
+ if (tiPC == PLANARCONFIG_SEPARATE) {
+ cvtCxToPx = convert_32s_CXPX_LUT[1]; /* override */
+ tiSpp = 1U; /* consider only one sample per plane */
+ }
+
+ for (j = 0; j < numcomps; j++) {
+ cmptparm[j].prec = tiBps;
+ cmptparm[j].bpp = tiBps;
+ cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
+ cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
+ cmptparm[j].w = (OPJ_UINT32)w;
+ cmptparm[j].h = (OPJ_UINT32)h;
+ }
+
+ image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
+ if (!image) {
+ TIFFClose(tif);
+ return NULL;
+ }
+ /* set image offset and reference grid */
+ image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+ image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+ image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
+ image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
+ image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
+ image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
+
+ for (j = 0; j < numcomps; j++) {
+ planes[j] = image->comps[j].data;
+ }
+ image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
+
+ strip_size = TIFFStripSize(tif);
+
+ buf = _TIFFmalloc(strip_size);
+ if (buf == NULL) {
+ TIFFClose(tif);
+ opj_image_destroy(image);
+ return NULL;
+ }
+ rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
+ buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
+ if (buffer32s == NULL) {
+ _TIFFfree(buf);
+ TIFFClose(tif);
+ opj_image_destroy(image);
+ return NULL;
+ }
+
+ strip = 0;
+ currentPlane = 0;
+ do {
+ planes[0] = image->comps[currentPlane].data; /* to manage planar data */
+ h = (int)tiHeight;
+ /* Read the Image components */
+ for (; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) {
+ const OPJ_UINT8 *dat8;
+ OPJ_SIZE_T ssize;
+
+ ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
+ dat8 = (const OPJ_UINT8*)buf;
+
+ while (ssize >= rowStride) {
+ cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
+ cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);
+ planes[0] += w;
+ planes[1] += w;
+ planes[2] += w;
+ planes[3] += w;
+ dat8 += rowStride;
+ ssize -= rowStride;
+ h--;
+ }
+ }
+ currentPlane++;
+ } while ((tiPC == PLANARCONFIG_SEPARATE) && (currentPlane < numcomps));
+
+ free(buffer32s);
+ _TIFFfree(buf);
+ TIFFClose(tif);
+
+ if (is_cinema) {
+ for (j = 0; j < numcomps; ++j) {
+ scale_component(&(image->comps[j]), 12);
+ }
+
+ }
+ return image;
}/* tiftoimage() */
diff --git a/src/bin/jp2/index.c b/src/bin/jp2/index.c
index 478f556a..3eae2f97 100644
--- a/src/bin/jp2/index.c
+++ b/src/bin/jp2/index.c
@@ -1,12 +1,12 @@
/*
- * The copyright in this software is being made available under the 2-clauses
- * BSD License, included below. This software may be subject to other third
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
* Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2014, Professor Benoit Macq
- * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -42,356 +42,415 @@
/**
Write a structured index to a file
-@param cstr_info Codestream information
+@param cstr_info Codestream information
@param index Index filename
@return Returns 0 if successful, returns 1 otherwise
*/
-int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
- int tileno, compno, layno, resno, precno, pack_nb, x, y;
- FILE *stream = NULL;
- double total_disto = 0;
-/* UniPG>> */
- int tilepartno;
- char disto_on, numpix_on;
+int write_index_file(opj_codestream_info_t *cstr_info, char *index)
+{
+ int tileno, compno, layno, resno, precno, pack_nb, x, y;
+ FILE *stream = NULL;
+ double total_disto = 0;
+ /* UniPG>> */
+ int tilepartno;
+ char disto_on, numpix_on;
#ifdef USE_JPWL
- if (!strcmp(index, JPWL_PRIVATEINDEX_NAME))
- return 0;
+ if (!strcmp(index, JPWL_PRIVATEINDEX_NAME)) {
+ return 0;
+ }
#endif /* USE_JPWL */
-/* <<UniPG */
-
- if (!cstr_info)
- return 1;
-
- stream = fopen(index, "w");
- if (!stream) {
- fprintf(stderr, "failed to open index file [%s] for writing\n", index);
- return 1;
- }
-
- if (cstr_info->tile[0].distotile > 0.0)
- disto_on = 1;
- else
- disto_on = 0;
-
- if (cstr_info->tile[0].numpix)
- numpix_on = 1;
- else
- numpix_on = 0;
-
- fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h);
- fprintf(stream, "%d\n", cstr_info->prog);
- fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y);
- fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th);
- fprintf(stream, "%d\n", cstr_info->numcomps);
- fprintf(stream, "%d\n", cstr_info->numlayers);
- fprintf(stream, "%d\n", cstr_info->numdecompos[0]); /* based on component 0 */
-
- for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) {
- fprintf(stream, "[%d,%d] ",
- (1 << cstr_info->tile[0].pdx[resno]), (1 << cstr_info->tile[0].pdx[resno])); /* based on tile 0 and component 0 */
- }
-
- fprintf(stream, "\n");
-/* UniPG>> */
- fprintf(stream, "%d\n", cstr_info->main_head_start);
-/* <<UniPG */
- fprintf(stream, "%d\n", cstr_info->main_head_end);
- fprintf(stream, "%d\n", cstr_info->codestream_size);
-
- fprintf(stream, "\nINFO ON TILES\n");
- fprintf(stream, "tileno start_pos end_hd end_tile nbparts");
- if (disto_on)
- fprintf(stream," disto");
- if (numpix_on)
- fprintf(stream," nbpix");
- if (disto_on && numpix_on)
- fprintf(stream," disto/nbpix");
- fprintf(stream, "\n");
-
- for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
- fprintf(stream, "%4d %9d %9d %9d %9d",
- cstr_info->tile[tileno].tileno,
- cstr_info->tile[tileno].start_pos,
- cstr_info->tile[tileno].end_header,
- cstr_info->tile[tileno].end_pos,
- cstr_info->tile[tileno].num_tps);
- if (disto_on)
- fprintf(stream," %9e", cstr_info->tile[tileno].distotile);
- if (numpix_on)
- fprintf(stream," %9d", cstr_info->tile[tileno].numpix);
- if (disto_on && numpix_on)
- fprintf(stream," %9e", cstr_info->tile[tileno].distotile / cstr_info->tile[tileno].numpix);
- fprintf(stream, "\n");
- }
-
- for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
- OPJ_OFF_T start_pos, end_ph_pos, end_pos;
- double disto = 0;
- int max_numdecompos = 0;
- pack_nb = 0;
-
- for (compno = 0; compno < cstr_info->numcomps; compno++) {
- if (max_numdecompos < cstr_info->numdecompos[compno])
- max_numdecompos = cstr_info->numdecompos[compno];
- }
-
- fprintf(stream, "\nTILE %d DETAILS\n", tileno);
- fprintf(stream, "part_nb tileno start_pack num_packs start_pos end_tph_pos end_pos\n");
- for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++)
- fprintf(stream, "%4d %9d %9d %9d %9d %11d %9d\n",
- tilepartno, tileno,
- cstr_info->tile[tileno].tp[tilepartno].tp_start_pack,
- cstr_info->tile[tileno].tp[tilepartno].tp_numpacks,
- cstr_info->tile[tileno].tp[tilepartno].tp_start_pos,
- cstr_info->tile[tileno].tp[tilepartno].tp_end_header,
- cstr_info->tile[tileno].tp[tilepartno].tp_end_pos
- );
-
- if (cstr_info->prog == OPJ_LRCP) { /* LRCP */
- fprintf(stream, "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos");
- if (disto_on)
- fprintf(stream, " disto");
- fprintf(stream,"\n");
-
- for (layno = 0; layno < cstr_info->numlayers; layno++) {
- for (resno = 0; resno < max_numdecompos + 1; resno++) {
- for (compno = 0; compno < cstr_info->numcomps; compno++) {
- int prec_max;
- if (resno > cstr_info->numdecompos[compno])
- break;
- prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
- for (precno = 0; precno < prec_max; precno++) {
- start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
- end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
- end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
- disto = cstr_info->tile[tileno].packet[pack_nb].disto;
- fprintf(stream, "%4d %6d %7d %5d %6d %6d %6" PRId64 " %6" PRId64 " %7" PRId64,
- pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos);
- if (disto_on)
- fprintf(stream, " %8e", disto);
- fprintf(stream, "\n");
- total_disto += disto;
- pack_nb++;
- }
- }
- }
- }
- } /* LRCP */
-
- else if (cstr_info->prog == OPJ_RLCP) { /* RLCP */
- fprintf(stream, "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos\n");
- if (disto_on)
- fprintf(stream, " disto");
- fprintf(stream,"\n");
-
- for (resno = 0; resno < max_numdecompos + 1; resno++) {
- for (layno = 0; layno < cstr_info->numlayers; layno++) {
- for (compno = 0; compno < cstr_info->numcomps; compno++) {
- int prec_max;
- if (resno > cstr_info->numdecompos[compno])
- break;
- prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
- for (precno = 0; precno < prec_max; precno++) {
- start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
- end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
- end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
- disto = cstr_info->tile[tileno].packet[pack_nb].disto;
- fprintf(stream, "%4d %6d %5d %7d %6d %6d %9" PRId64 " %9" PRId64 " %7" PRId64,
- pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos);
- if (disto_on)
- fprintf(stream, " %8e", disto);
- fprintf(stream, "\n");
- total_disto += disto;
- pack_nb++;
- }
- }
- }
- }
- } /* RLCP */
-
- else if (cstr_info->prog == OPJ_RPCL) { /* RPCL */
-
- fprintf(stream, "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos");
- if (disto_on)
- fprintf(stream, " disto");
- fprintf(stream,"\n");
-
- for (resno = 0; resno < max_numdecompos + 1; resno++) {
- int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
- for (precno = 0; precno < numprec; precno++) {
- /* I suppose components have same XRsiz, YRsiz */
- int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
- int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
- int x1 = x0 + cstr_info->tile_x;
- int y1 = y0 + cstr_info->tile_y;
- for (compno = 0; compno < cstr_info->numcomps; compno++) {
- int pcnx = cstr_info->tile[tileno].pw[resno];
- int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
- int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
- int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
- int precno_y = (int) floor( (float)precno/(float)pcnx );
- if (resno > cstr_info->numdecompos[compno])
- break;
- for(y = y0; y < y1; y++) {
- if (precno_y*pcy == y ) {
- for (x = x0; x < x1; x++) {
- if (precno_x*pcx == x ) {
- for (layno = 0; layno < cstr_info->numlayers; layno++) {
- start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
- end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
- end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
- disto = cstr_info->tile[tileno].packet[pack_nb].disto;
- fprintf(stream, "%4d %6d %5d %6d %6d %7d %9" PRId64 " %9" PRId64 " %7" PRId64,
- pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos);
- if (disto_on)
- fprintf(stream, " %8e", disto);
- fprintf(stream, "\n");
- total_disto += disto;
- pack_nb++;
- }
- }
- }/* x = x0..x1 */
- }
- } /* y = y0..y1 */
- } /* precno */
- } /* compno */
- } /* resno */
- } /* RPCL */
-
- else if (cstr_info->prog == OPJ_PCRL) { /* PCRL */
- /* I suppose components have same XRsiz, YRsiz */
- int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
- int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
- int x1 = x0 + cstr_info->tile_x;
- int y1 = y0 + cstr_info->tile_y;
-
- /* Count the maximum number of precincts */
- int max_numprec = 0;
- for (resno = 0; resno < max_numdecompos + 1; resno++) {
- int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
- if (numprec > max_numprec)
- max_numprec = numprec;
- }
-
- fprintf(stream, "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos");
- if (disto_on)
- fprintf(stream, " disto");
- fprintf(stream,"\n");
-
- for (precno = 0; precno < max_numprec; precno++) {
- for (compno = 0; compno < cstr_info->numcomps; compno++) {
- for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {
- int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
- int pcnx = cstr_info->tile[tileno].pw[resno];
- int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
- int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
- int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
- int precno_y = (int) floor( (float)precno/(float)pcnx );
- if (precno >= numprec)
- continue;
- for(y = y0; y < y1; y++) {
- if (precno_y*pcy == y ) {
- for (x = x0; x < x1; x++) {
- if (precno_x*pcx == x ) {
- for (layno = 0; layno < cstr_info->numlayers; layno++) {
- start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
- end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
- end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
- disto = cstr_info->tile[tileno].packet[pack_nb].disto;
- fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64,
- pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos);
- if (disto_on)
- fprintf(stream, " %8e", disto);
- fprintf(stream, "\n");
- total_disto += disto;
- pack_nb++;
- }
- }
- }/* x = x0..x1 */
- }
- } /* y = y0..y1 */
- } /* resno */
- } /* compno */
- } /* precno */
- } /* PCRL */
-
- else { /* CPRL */
- /* Count the maximum number of precincts */
- int max_numprec = 0;
- for (resno = 0; resno < max_numdecompos + 1; resno++) {
- int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
- if (numprec > max_numprec)
- max_numprec = numprec;
- }
-
- fprintf(stream, "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos");
- if (disto_on)
- fprintf(stream, " disto");
- fprintf(stream,"\n");
-
- for (compno = 0; compno < cstr_info->numcomps; compno++) {
- /* I suppose components have same XRsiz, YRsiz */
- int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
- int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
- int x1 = x0 + cstr_info->tile_x;
- int y1 = y0 + cstr_info->tile_y;
-
- for (precno = 0; precno < max_numprec; precno++) {
- for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {
- int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
- int pcnx = cstr_info->tile[tileno].pw[resno];
- int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
- int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
- int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
- int precno_y = (int) floor( (float)precno/(float)pcnx );
- if (precno >= numprec)
- continue;
-
- for(y = y0; y < y1; y++) {
- if (precno_y*pcy == y ) {
- for (x = x0; x < x1; x++) {
- if (precno_x*pcx == x ) {
- for (layno = 0; layno < cstr_info->numlayers; layno++) {
- start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
- end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
- end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
- disto = cstr_info->tile[tileno].packet[pack_nb].disto;
- fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64,
- pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos);
- if (disto_on)
- fprintf(stream, " %8e", disto);
- fprintf(stream, "\n");
- total_disto += disto;
- pack_nb++;
- }
- }
- }/* x = x0..x1 */
- }
- } /* y = y0..y1 */
- } /* resno */
- } /* precno */
- } /* compno */
- } /* CPRL */
- } /* tileno */
-
- if (disto_on) {
- fprintf(stream, "%8e\n", cstr_info->D_max); /* SE max */
- fprintf(stream, "%.8e\n", total_disto); /* SE totale */
- }
-/* UniPG>> */
- /* print the markers' list */
- if (cstr_info->marknum) {
- fprintf(stream, "\nMARKER LIST\n");
- fprintf(stream, "%d\n", cstr_info->marknum);
- fprintf(stream, "type\tstart_pos length\n");
- for (x = 0; x < cstr_info->marknum; x++)
- fprintf(stream, "%X\t%9" PRId64 " %9d\n", cstr_info->marker[x].type, cstr_info->marker[x].pos, cstr_info->marker[x].len);
- }
-/* <<UniPG */
- fclose(stream);
-
- fprintf(stderr,"Generated index file %s\n", index);
-
- return 0;
+ /* <<UniPG */
+
+ if (!cstr_info) {
+ return 1;
+ }
+
+ stream = fopen(index, "w");
+ if (!stream) {
+ fprintf(stderr, "failed to open index file [%s] for writing\n", index);
+ return 1;
+ }
+
+ if (cstr_info->tile[0].distotile > 0.0) {
+ disto_on = 1;
+ } else {
+ disto_on = 0;
+ }
+
+ if (cstr_info->tile[0].numpix) {
+ numpix_on = 1;
+ } else {
+ numpix_on = 0;
+ }
+
+ fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h);
+ fprintf(stream, "%d\n", cstr_info->prog);
+ fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y);
+ fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th);
+ fprintf(stream, "%d\n", cstr_info->numcomps);
+ fprintf(stream, "%d\n", cstr_info->numlayers);
+ fprintf(stream, "%d\n", cstr_info->numdecompos[0]); /* based on component 0 */
+
+ for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) {
+ fprintf(stream, "[%d,%d] ",
+ (1 << cstr_info->tile[0].pdx[resno]),
+ (1 << cstr_info->tile[0].pdx[resno])); /* based on tile 0 and component 0 */
+ }
+
+ fprintf(stream, "\n");
+ /* UniPG>> */
+ fprintf(stream, "%d\n", cstr_info->main_head_start);
+ /* <<UniPG */
+ fprintf(stream, "%d\n", cstr_info->main_head_end);
+ fprintf(stream, "%d\n", cstr_info->codestream_size);
+
+ fprintf(stream, "\nINFO ON TILES\n");
+ fprintf(stream, "tileno start_pos end_hd end_tile nbparts");
+ if (disto_on) {
+ fprintf(stream, " disto");
+ }
+ if (numpix_on) {
+ fprintf(stream, " nbpix");
+ }
+ if (disto_on && numpix_on) {
+ fprintf(stream, " disto/nbpix");
+ }
+ fprintf(stream, "\n");
+
+ for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
+ fprintf(stream, "%4d %9d %9d %9d %9d",
+ cstr_info->tile[tileno].tileno,
+ cstr_info->tile[tileno].start_pos,
+ cstr_info->tile[tileno].end_header,
+ cstr_info->tile[tileno].end_pos,
+ cstr_info->tile[tileno].num_tps);
+ if (disto_on) {
+ fprintf(stream, " %9e", cstr_info->tile[tileno].distotile);
+ }
+ if (numpix_on) {
+ fprintf(stream, " %9d", cstr_info->tile[tileno].numpix);
+ }
+ if (disto_on && numpix_on) {
+ fprintf(stream, " %9e", cstr_info->tile[tileno].distotile /
+ cstr_info->tile[tileno].numpix);
+ }
+ fprintf(stream, "\n");
+ }
+
+ for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
+ OPJ_OFF_T start_pos, end_ph_pos, end_pos;
+ double disto = 0;
+ int max_numdecompos = 0;
+ pack_nb = 0;
+
+ for (compno = 0; compno < cstr_info->numcomps; compno++) {
+ if (max_numdecompos < cstr_info->numdecompos[compno]) {
+ max_numdecompos = cstr_info->numdecompos[compno];
+ }
+ }
+
+ fprintf(stream, "\nTILE %d DETAILS\n", tileno);
+ fprintf(stream,
+ "part_nb tileno start_pack num_packs start_pos end_tph_pos end_pos\n");
+ for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++)
+ fprintf(stream, "%4d %9d %9d %9d %9d %11d %9d\n",
+ tilepartno, tileno,
+ cstr_info->tile[tileno].tp[tilepartno].tp_start_pack,
+ cstr_info->tile[tileno].tp[tilepartno].tp_numpacks,
+ cstr_info->tile[tileno].tp[tilepartno].tp_start_pos,
+ cstr_info->tile[tileno].tp[tilepartno].tp_end_header,
+ cstr_info->tile[tileno].tp[tilepartno].tp_end_pos
+ );
+
+ if (cstr_info->prog == OPJ_LRCP) { /* LRCP */
+ fprintf(stream,
+ "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos");
+ if (disto_on) {
+ fprintf(stream, " disto");
+ }
+ fprintf(stream, "\n");
+
+ for (layno = 0; layno < cstr_info->numlayers; layno++) {
+ for (resno = 0; resno < max_numdecompos + 1; resno++) {
+ for (compno = 0; compno < cstr_info->numcomps; compno++) {
+ int prec_max;
+ if (resno > cstr_info->numdecompos[compno]) {
+ break;
+ }
+ prec_max = cstr_info->tile[tileno].pw[resno] *
+ cstr_info->tile[tileno].ph[resno];
+ for (precno = 0; precno < prec_max; precno++) {
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+ fprintf(stream, "%4d %6d %7d %5d %6d %6d %6" PRId64 " %6" PRId64 " %7"
+ PRId64,
+ pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos);
+ if (disto_on) {
+ fprintf(stream, " %8e", disto);
+ }
+ fprintf(stream, "\n");
+ total_disto += disto;
+ pack_nb++;
+ }
+ }
+ }
+ }
+ } /* LRCP */
+
+ else if (cstr_info->prog == OPJ_RLCP) { /* RLCP */
+ fprintf(stream,
+ "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos\n");
+ if (disto_on) {
+ fprintf(stream, " disto");
+ }
+ fprintf(stream, "\n");
+
+ for (resno = 0; resno < max_numdecompos + 1; resno++) {
+ for (layno = 0; layno < cstr_info->numlayers; layno++) {
+ for (compno = 0; compno < cstr_info->numcomps; compno++) {
+ int prec_max;
+ if (resno > cstr_info->numdecompos[compno]) {
+ break;
+ }
+ prec_max = cstr_info->tile[tileno].pw[resno] *
+ cstr_info->tile[tileno].ph[resno];
+ for (precno = 0; precno < prec_max; precno++) {
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+ fprintf(stream, "%4d %6d %5d %7d %6d %6d %9" PRId64 " %9" PRId64 " %7" PRId64,
+ pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos);
+ if (disto_on) {
+ fprintf(stream, " %8e", disto);
+ }
+ fprintf(stream, "\n");
+ total_disto += disto;
+ pack_nb++;
+ }
+ }
+ }
+ }
+ } /* RLCP */
+
+ else if (cstr_info->prog == OPJ_RPCL) { /* RPCL */
+
+ fprintf(stream,
+ "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos");
+ if (disto_on) {
+ fprintf(stream, " disto");
+ }
+ fprintf(stream, "\n");
+
+ for (resno = 0; resno < max_numdecompos + 1; resno++) {
+ int numprec = cstr_info->tile[tileno].pw[resno] *
+ cstr_info->tile[tileno].ph[resno];
+ for (precno = 0; precno < numprec; precno++) {
+ /* I suppose components have same XRsiz, YRsiz */
+ int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
+ (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
+ int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
+ (float)cstr_info->tw) * cstr_info->tile_y;
+ int x1 = x0 + cstr_info->tile_x;
+ int y1 = y0 + cstr_info->tile_y;
+ for (compno = 0; compno < cstr_info->numcomps; compno++) {
+ int pcnx = cstr_info->tile[tileno].pw[resno];
+ int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
+ cstr_info->numdecompos[compno] - resno);
+ int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
+ cstr_info->numdecompos[compno] - resno);
+ int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
+ int precno_y = (int) floor((float)precno / (float)pcnx);
+ if (resno > cstr_info->numdecompos[compno]) {
+ break;
+ }
+ for (y = y0; y < y1; y++) {
+ if (precno_y * pcy == y) {
+ for (x = x0; x < x1; x++) {
+ if (precno_x * pcx == x) {
+ for (layno = 0; layno < cstr_info->numlayers; layno++) {
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+ fprintf(stream, "%4d %6d %5d %6d %6d %7d %9" PRId64 " %9" PRId64 " %7" PRId64,
+ pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos);
+ if (disto_on) {
+ fprintf(stream, " %8e", disto);
+ }
+ fprintf(stream, "\n");
+ total_disto += disto;
+ pack_nb++;
+ }
+ }
+ }/* x = x0..x1 */
+ }
+ } /* y = y0..y1 */
+ } /* precno */
+ } /* compno */
+ } /* resno */
+ } /* RPCL */
+
+ else if (cstr_info->prog == OPJ_PCRL) { /* PCRL */
+ /* I suppose components have same XRsiz, YRsiz */
+ int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
+ (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
+ int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
+ (float)cstr_info->tw) * cstr_info->tile_y;
+ int x1 = x0 + cstr_info->tile_x;
+ int y1 = y0 + cstr_info->tile_y;
+
+ /* Count the maximum number of precincts */
+ int max_numprec = 0;
+ for (resno = 0; resno < max_numdecompos + 1; resno++) {
+ int numprec = cstr_info->tile[tileno].pw[resno] *
+ cstr_info->tile[tileno].ph[resno];
+ if (numprec > max_numprec) {
+ max_numprec = numprec;
+ }
+ }
+
+ fprintf(stream,
+ "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos");
+ if (disto_on) {
+ fprintf(stream, " disto");
+ }
+ fprintf(stream, "\n");
+
+ for (precno = 0; precno < max_numprec; precno++) {
+ for (compno = 0; compno < cstr_info->numcomps; compno++) {
+ for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {
+ int numprec = cstr_info->tile[tileno].pw[resno] *
+ cstr_info->tile[tileno].ph[resno];
+ int pcnx = cstr_info->tile[tileno].pw[resno];
+ int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
+ cstr_info->numdecompos[compno] - resno);
+ int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
+ cstr_info->numdecompos[compno] - resno);
+ int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
+ int precno_y = (int) floor((float)precno / (float)pcnx);
+ if (precno >= numprec) {
+ continue;
+ }
+ for (y = y0; y < y1; y++) {
+ if (precno_y * pcy == y) {
+ for (x = x0; x < x1; x++) {
+ if (precno_x * pcx == x) {
+ for (layno = 0; layno < cstr_info->numlayers; layno++) {
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+ fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64,
+ pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos);
+ if (disto_on) {
+ fprintf(stream, " %8e", disto);
+ }
+ fprintf(stream, "\n");
+ total_disto += disto;
+ pack_nb++;
+ }
+ }
+ }/* x = x0..x1 */
+ }
+ } /* y = y0..y1 */
+ } /* resno */
+ } /* compno */
+ } /* precno */
+ } /* PCRL */
+
+ else { /* CPRL */
+ /* Count the maximum number of precincts */
+ int max_numprec = 0;
+ for (resno = 0; resno < max_numdecompos + 1; resno++) {
+ int numprec = cstr_info->tile[tileno].pw[resno] *
+ cstr_info->tile[tileno].ph[resno];
+ if (numprec > max_numprec) {
+ max_numprec = numprec;
+ }
+ }
+
+ fprintf(stream,
+ "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos");
+ if (disto_on) {
+ fprintf(stream, " disto");
+ }
+ fprintf(stream, "\n");
+
+ for (compno = 0; compno < cstr_info->numcomps; compno++) {
+ /* I suppose components have same XRsiz, YRsiz */
+ int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
+ (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
+ int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
+ (float)cstr_info->tw) * cstr_info->tile_y;
+ int x1 = x0 + cstr_info->tile_x;
+ int y1 = y0 + cstr_info->tile_y;
+
+ for (precno = 0; precno < max_numprec; precno++) {
+ for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {
+ int numprec = cstr_info->tile[tileno].pw[resno] *
+ cstr_info->tile[tileno].ph[resno];
+ int pcnx = cstr_info->tile[tileno].pw[resno];
+ int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
+ cstr_info->numdecompos[compno] - resno);
+ int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
+ cstr_info->numdecompos[compno] - resno);
+ int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
+ int precno_y = (int) floor((float)precno / (float)pcnx);
+ if (precno >= numprec) {
+ continue;
+ }
+
+ for (y = y0; y < y1; y++) {
+ if (precno_y * pcy == y) {
+ for (x = x0; x < x1; x++) {
+ if (precno_x * pcx == x) {
+ for (layno = 0; layno < cstr_info->numlayers; layno++) {
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+ fprintf(stream, "%4d %6d %6d %6d %5d %7d %9" PRId64 " %9" PRId64 " %7" PRId64,
+ pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos);
+ if (disto_on) {
+ fprintf(stream, " %8e", disto);
+ }
+ fprintf(stream, "\n");
+ total_disto += disto;
+ pack_nb++;
+ }
+ }
+ }/* x = x0..x1 */
+ }
+ } /* y = y0..y1 */
+ } /* resno */
+ } /* precno */
+ } /* compno */
+ } /* CPRL */
+ } /* tileno */
+
+ if (disto_on) {
+ fprintf(stream, "%8e\n", cstr_info->D_max); /* SE max */
+ fprintf(stream, "%.8e\n", total_disto); /* SE totale */
+ }
+ /* UniPG>> */
+ /* print the markers' list */
+ if (cstr_info->marknum) {
+ fprintf(stream, "\nMARKER LIST\n");
+ fprintf(stream, "%d\n", cstr_info->marknum);
+ fprintf(stream, "type\tstart_pos length\n");
+ for (x = 0; x < cstr_info->marknum; x++) {
+ fprintf(stream, "%X\t%9" PRId64 " %9d\n", cstr_info->marker[x].type,
+ cstr_info->marker[x].pos, cstr_info->marker[x].len);
+ }
+ }
+ /* <<UniPG */
+ fclose(stream);
+
+ fprintf(stderr, "Generated index file %s\n", index);
+
+ return 0;
}
diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c
index aa8091ab..2ef1c6be 100644
--- a/src/bin/jp2/opj_compress.c
+++ b/src/bin/jp2/opj_compress.c
@@ -1,6 +1,6 @@
/*
- * The copyright in this software is being made available under the 2-clauses
- * BSD License, included below. This software may be subject to other third
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
@@ -8,7 +8,7 @@
* Copyright (c) 2002-2014, Professor Benoit Macq
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
* Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* Copyright (c) 2006-2007, Parvatha Elangovan
@@ -71,14 +71,14 @@
#include "format_defs.h"
#include "opj_string.h"
-typedef struct dircnt{
+typedef struct dircnt {
/** Buffer for holding images read from Directory*/
char *filename_buf;
/** Pointer to the buffer*/
char **filename;
-}dircnt_t;
+} dircnt_t;
-typedef struct img_folder{
+typedef struct img_folder {
/** The directory path of the folder containing input images*/
char *imgdirpath;
/** Output format*/
@@ -87,342 +87,419 @@ typedef struct img_folder{
char set_imgdir;
/** Enable Cod Format for output*/
char set_out_format;
-}img_fol_t;
+} img_fol_t;
-static void encode_help_display(void) {
- fprintf(stdout,"\nThis is the opj_compress utility from the OpenJPEG project.\n"
+static void encode_help_display(void)
+{
+ fprintf(stdout,
+ "\nThis is the opj_compress utility from the OpenJPEG project.\n"
"It compresses various image formats with the JPEG 2000 algorithm.\n"
- "It has been compiled against openjp2 library v%s.\n\n",opj_version());
-
- fprintf(stdout,"Default encoding options:\n");
- fprintf(stdout,"-------------------------\n");
- fprintf(stdout,"\n");
- fprintf(stdout," * Lossless\n");
- fprintf(stdout," * 1 tile\n");
- fprintf(stdout," * RGB->YCC conversion if at least 3 components\n");
- fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
- fprintf(stdout," * Size of code-block : 64 x 64\n");
- fprintf(stdout," * Number of resolutions: 6\n");
- fprintf(stdout," * No SOP marker in the codestream\n");
- fprintf(stdout," * No EPH marker in the codestream\n");
- fprintf(stdout," * No sub-sampling in x or y direction\n");
- fprintf(stdout," * No mode switch activated\n");
- fprintf(stdout," * Progression order: LRCP\n");
- #ifdef FIXME_INDEX
- fprintf(stdout," * No index file\n");
- #endif /* FIXME_INDEX */
- fprintf(stdout," * No ROI upshifted\n");
- fprintf(stdout," * No offset of the origin of the image\n");
- fprintf(stdout," * No offset of the origin of the tiles\n");
- fprintf(stdout," * Reversible DWT 5-3\n");
+ "It has been compiled against openjp2 library v%s.\n\n", opj_version());
+
+ fprintf(stdout, "Default encoding options:\n");
+ fprintf(stdout, "-------------------------\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, " * Lossless\n");
+ fprintf(stdout, " * 1 tile\n");
+ fprintf(stdout, " * RGB->YCC conversion if at least 3 components\n");
+ fprintf(stdout, " * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
+ fprintf(stdout, " * Size of code-block : 64 x 64\n");
+ fprintf(stdout, " * Number of resolutions: 6\n");
+ fprintf(stdout, " * No SOP marker in the codestream\n");
+ fprintf(stdout, " * No EPH marker in the codestream\n");
+ fprintf(stdout, " * No sub-sampling in x or y direction\n");
+ fprintf(stdout, " * No mode switch activated\n");
+ fprintf(stdout, " * Progression order: LRCP\n");
+#ifdef FIXME_INDEX
+ fprintf(stdout, " * No index file\n");
+#endif /* FIXME_INDEX */
+ fprintf(stdout, " * No ROI upshifted\n");
+ fprintf(stdout, " * No offset of the origin of the image\n");
+ fprintf(stdout, " * No offset of the origin of the tiles\n");
+ fprintf(stdout, " * Reversible DWT 5-3\n");
/* UniPG>> */
#ifdef USE_JPWL
- fprintf(stdout," * No JPWL protection\n");
+ fprintf(stdout, " * No JPWL protection\n");
#endif /* USE_JPWL */
/* <<UniPG */
- fprintf(stdout,"\n");
-
- fprintf(stdout,"Note:\n");
- fprintf(stdout,"-----\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
- fprintf(stdout,"COD and QCD never appear in the tile_header.\n");
- fprintf(stdout,"\n");
-
- fprintf(stdout,"Parameters:\n");
- fprintf(stdout,"-----------\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"Required Parameters (except with -h):\n");
- fprintf(stdout,"One of the two options -ImgDir or -i must be used\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"-i <file>\n");
- fprintf(stdout," Input file\n");
- fprintf(stdout," Known extensions are <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n");
- fprintf(stdout," If used, '-o <file>' must be provided\n");
- fprintf(stdout,"-o <compressed file>\n");
- fprintf(stdout," Output file (accepted extensions are j2k or jp2).\n");
- fprintf(stdout,"-ImgDir <dir>\n");
- fprintf(stdout," Image file Directory path (example ../Images) \n");
- fprintf(stdout," When using this option -OutFor must be used\n");
- fprintf(stdout,"-OutFor <J2K|J2C|JP2>\n");
- fprintf(stdout," Output format for compressed files.\n");
- fprintf(stdout," Required only if -ImgDir is used\n");
- fprintf(stdout,"-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
- fprintf(stdout," Characteristics of the raw input image\n");
- fprintf(stdout," If subsampling is omitted, 1x1 is assumed for all components\n");
- fprintf(stdout," Example: -F 512,512,3,8,u@1x1:2x2:2x2\n");
- fprintf(stdout," for raw 512x512 image with 4:2:0 subsampling\n");
- fprintf(stdout," Required only if RAW or RAWL input file is provided.\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"Optional Parameters:\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"-h\n");
- fprintf(stdout," Display the help information.\n");
- fprintf(stdout,"-r <compression ratio>,<compression ratio>,...\n");
- fprintf(stdout," Different compression ratios for successive layers.\n");
- fprintf(stdout," The rate specified for each quality level is the desired\n");
- fprintf(stdout," compression factor.\n");
- fprintf(stdout," Decreasing ratios required.\n");
- fprintf(stdout," Example: -r 20,10,1 means \n");
- fprintf(stdout," quality layer 1: compress 20x, \n");
- fprintf(stdout," quality layer 2: compress 10x \n");
- fprintf(stdout," quality layer 3: compress lossless\n");
- fprintf(stdout," Options -r and -q cannot be used together.\n");
- fprintf(stdout,"-q <psnr value>,<psnr value>,<psnr value>,...\n");
- fprintf(stdout," Different psnr for successive layers (-q 30,40,50).\n");
- fprintf(stdout," Increasing PSNR values required.\n");
- fprintf(stdout," Options -r and -q cannot be used together.\n");
- fprintf(stdout,"-n <number of resolutions>\n");
- fprintf(stdout," Number of resolutions.\n");
- fprintf(stdout," It corresponds to the number of DWT decompositions +1. \n");
- fprintf(stdout," Default: 6.\n");
- fprintf(stdout,"-b <cblk width>,<cblk height>\n");
- fprintf(stdout," Code-block size. The dimension must respect the constraint \n");
- fprintf(stdout," defined in the JPEG-2000 standard (no dimension smaller than 4 \n");
- fprintf(stdout," or greater than 1024, no code-block with more than 4096 coefficients).\n");
- fprintf(stdout," The maximum value authorized is 64x64. \n");
- fprintf(stdout," Default: 64x64.\n");
- fprintf(stdout,"-c [<prec width>,<prec height>],[<prec width>,<prec height>],...\n");
- fprintf(stdout," Precinct size. Values specified must be power of 2. \n");
- fprintf(stdout," Multiple records may be supplied, in which case the first record refers\n");
- fprintf(stdout," to the highest resolution level and subsequent records to lower \n");
- fprintf(stdout," resolution levels. The last specified record is right-shifted for each \n");
- fprintf(stdout," remaining lower resolution levels.\n");
- fprintf(stdout," Default: 215x215 at each resolution.\n");
- fprintf(stdout,"-t <tile width>,<tile height>\n");
- fprintf(stdout," Tile size.\n");
- fprintf(stdout," Default: the dimension of the whole image, thus only one tile.\n");
- fprintf(stdout,"-p <LRCP|RLCP|RPCL|PCRL|CPRL>\n");
- fprintf(stdout," Progression order.\n");
- fprintf(stdout," Default: LRCP.\n");
- fprintf(stdout,"-s <subX,subY>\n");
- fprintf(stdout," Subsampling factor.\n");
- fprintf(stdout," Subsampling bigger than 2 can produce error\n");
- fprintf(stdout," Default: no subsampling.\n");
- fprintf(stdout,"-POC <progression order change>/<progression order change>/...\n");
- fprintf(stdout," Progression order change.\n");
- fprintf(stdout," The syntax of a progression order change is the following:\n");
- fprintf(stdout," T<tile>=<resStart>,<compStart>,<layerEnd>,<resEnd>,<compEnd>,<progOrder>\n");
- fprintf(stdout," Example: -POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL\n");
- fprintf(stdout,"-SOP\n");
- fprintf(stdout," Write SOP marker before each packet.\n");
- fprintf(stdout,"-EPH\n");
- fprintf(stdout," Write EPH marker after each header packet.\n");
- fprintf(stdout,"-M <key value>\n");
- fprintf(stdout," Mode switch.\n");
- fprintf(stdout," [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
- fprintf(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)]\n");
- fprintf(stdout," Indicate multiple modes by adding their values.\n");
- fprintf(stdout," Example: RESTART(4) + RESET(2) + SEGMARK(32) => -M 38\n");
- fprintf(stdout,"-TP <R|L|C>\n");
- fprintf(stdout," Divide packets of every tile into tile-parts.\n");
- fprintf(stdout," Division is made by grouping Resolutions (R), Layers (L)\n");
- fprintf(stdout," or Components (C).\n");
- #ifdef FIXME_INDEX
- fprintf(stdout,"-x <index file>\n");
- fprintf(stdout," Create an index file.\n");
- #endif /*FIXME_INDEX*/
- fprintf(stdout,"-ROI c=<component index>,U=<upshifting value>\n");
- fprintf(stdout," Quantization indices upshifted for a component. \n");
- fprintf(stdout," Warning: This option does not implement the usual ROI (Region of Interest).\n");
- fprintf(stdout," It should be understood as a 'Component of Interest'. It offers the \n");
- fprintf(stdout," possibility to upshift the value of a component during quantization step.\n");
- fprintf(stdout," The value after c= is the component number [0, 1, 2, ...] and the value \n");
- fprintf(stdout," after U= is the value of upshifting. U must be in the range [0, 37].\n");
- fprintf(stdout,"-d <image offset X,image offset Y>\n");
- fprintf(stdout," Offset of the origin of the image.\n");
- fprintf(stdout,"-T <tile offset X,tile offset Y>\n");
- fprintf(stdout," Offset of the origin of the tiles.\n");
- fprintf(stdout,"-I\n");
- fprintf(stdout," Use the irreversible DWT 9-7.\n");
- fprintf(stdout,"-mct <0|1|2>\n");
- fprintf(stdout," Explicitly specifies if a Multiple Component Transform has to be used.\n");
- fprintf(stdout," 0: no MCT ; 1: RGB->YCC conversion ; 2: custom MCT.\n");
- fprintf(stdout," If custom MCT, \"-m\" option has to be used (see hereunder).\n");
- fprintf(stdout," By default, RGB->YCC conversion is used if there are 3 components or more,\n");
- fprintf(stdout," no conversion otherwise.\n");
- fprintf(stdout,"-m <file>\n");
- fprintf(stdout," Use array-based MCT, values are coma separated, line by line\n");
- fprintf(stdout," No specific separators between lines, no space allowed between values.\n");
- fprintf(stdout," If this option is used, it automatically sets \"-mct\" option to 2.\n");
- fprintf(stdout,"-cinema2K <24|48>\n");
- fprintf(stdout," Digital Cinema 2K profile compliant codestream.\n");
- fprintf(stdout," Need to specify the frames per second for a 2K resolution.\n");
- fprintf(stdout," Only 24 or 48 fps are currently allowed.\n");
- fprintf(stdout,"-cinema4K\n");
- fprintf(stdout," Digital Cinema 4K profile compliant codestream.\n");
- fprintf(stdout," Frames per second not required. Default value is 24fps.\n");
- fprintf(stdout,"-jpip\n");
- fprintf(stdout," Write jpip codestream index box in JP2 output file.\n");
- fprintf(stdout," Currently supports only RPCL order.\n");
- fprintf(stdout,"-C <comment>\n");
- fprintf(stdout," Add <comment> in the comment marker segment.\n");
+ fprintf(stdout, "\n");
+
+ fprintf(stdout, "Note:\n");
+ fprintf(stdout, "-----\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout,
+ "The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
+ fprintf(stdout, "COD and QCD never appear in the tile_header.\n");
+ fprintf(stdout, "\n");
+
+ fprintf(stdout, "Parameters:\n");
+ fprintf(stdout, "-----------\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, "Required Parameters (except with -h):\n");
+ fprintf(stdout, "One of the two options -ImgDir or -i must be used\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, "-i <file>\n");
+ fprintf(stdout, " Input file\n");
+ fprintf(stdout,
+ " Known extensions are <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n");
+ fprintf(stdout, " If used, '-o <file>' must be provided\n");
+ fprintf(stdout, "-o <compressed file>\n");
+ fprintf(stdout, " Output file (accepted extensions are j2k or jp2).\n");
+ fprintf(stdout, "-ImgDir <dir>\n");
+ fprintf(stdout, " Image file Directory path (example ../Images) \n");
+ fprintf(stdout, " When using this option -OutFor must be used\n");
+ fprintf(stdout, "-OutFor <J2K|J2C|JP2>\n");
+ fprintf(stdout, " Output format for compressed files.\n");
+ fprintf(stdout, " Required only if -ImgDir is used\n");
+ fprintf(stdout,
+ "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
+ fprintf(stdout, " Characteristics of the raw input image\n");
+ fprintf(stdout,
+ " If subsampling is omitted, 1x1 is assumed for all components\n");
+ fprintf(stdout, " Example: -F 512,512,3,8,u@1x1:2x2:2x2\n");
+ fprintf(stdout,
+ " for raw 512x512 image with 4:2:0 subsampling\n");
+ fprintf(stdout, " Required only if RAW or RAWL input file is provided.\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, "Optional Parameters:\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, "-h\n");
+ fprintf(stdout, " Display the help information.\n");
+ fprintf(stdout, "-r <compression ratio>,<compression ratio>,...\n");
+ fprintf(stdout, " Different compression ratios for successive layers.\n");
+ fprintf(stdout,
+ " The rate specified for each quality level is the desired\n");
+ fprintf(stdout, " compression factor.\n");
+ fprintf(stdout, " Decreasing ratios required.\n");
+ fprintf(stdout, " Example: -r 20,10,1 means \n");
+ fprintf(stdout, " quality layer 1: compress 20x, \n");
+ fprintf(stdout, " quality layer 2: compress 10x \n");
+ fprintf(stdout, " quality layer 3: compress lossless\n");
+ fprintf(stdout, " Options -r and -q cannot be used together.\n");
+ fprintf(stdout, "-q <psnr value>,<psnr value>,<psnr value>,...\n");
+ fprintf(stdout, " Different psnr for successive layers (-q 30,40,50).\n");
+ fprintf(stdout, " Increasing PSNR values required.\n");
+ fprintf(stdout, " Options -r and -q cannot be used together.\n");
+ fprintf(stdout, "-n <number of resolutions>\n");
+ fprintf(stdout, " Number of resolutions.\n");
+ fprintf(stdout,
+ " It corresponds to the number of DWT decompositions +1. \n");
+ fprintf(stdout, " Default: 6.\n");
+ fprintf(stdout, "-b <cblk width>,<cblk height>\n");
+ fprintf(stdout,
+ " Code-block size. The dimension must respect the constraint \n");
+ fprintf(stdout,
+ " defined in the JPEG-2000 standard (no dimension smaller than 4 \n");
+ fprintf(stdout,
+ " or greater than 1024, no code-block with more than 4096 coefficients).\n");
+ fprintf(stdout, " The maximum value authorized is 64x64. \n");
+ fprintf(stdout, " Default: 64x64.\n");
+ fprintf(stdout,
+ "-c [<prec width>,<prec height>],[<prec width>,<prec height>],...\n");
+ fprintf(stdout, " Precinct size. Values specified must be power of 2. \n");
+ fprintf(stdout,
+ " Multiple records may be supplied, in which case the first record refers\n");
+ fprintf(stdout,
+ " to the highest resolution level and subsequent records to lower \n");
+ fprintf(stdout,
+ " resolution levels. The last specified record is right-shifted for each \n");
+ fprintf(stdout, " remaining lower resolution levels.\n");
+ fprintf(stdout, " Default: 215x215 at each resolution.\n");
+ fprintf(stdout, "-t <tile width>,<tile height>\n");
+ fprintf(stdout, " Tile size.\n");
+ fprintf(stdout,
+ " Default: the dimension of the whole image, thus only one tile.\n");
+ fprintf(stdout, "-p <LRCP|RLCP|RPCL|PCRL|CPRL>\n");
+ fprintf(stdout, " Progression order.\n");
+ fprintf(stdout, " Default: LRCP.\n");
+ fprintf(stdout, "-s <subX,subY>\n");
+ fprintf(stdout, " Subsampling factor.\n");
+ fprintf(stdout, " Subsampling bigger than 2 can produce error\n");
+ fprintf(stdout, " Default: no subsampling.\n");
+ fprintf(stdout,
+ "-POC <progression order change>/<progression order change>/...\n");
+ fprintf(stdout, " Progression order change.\n");
+ fprintf(stdout,
+ " The syntax of a progression order change is the following:\n");
+ fprintf(stdout,
+ " T<tile>=<resStart>,<compStart>,<layerEnd>,<resEnd>,<compEnd>,<progOrder>\n");
+ fprintf(stdout, " Example: -POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL\n");
+ fprintf(stdout, "-SOP\n");
+ fprintf(stdout, " Write SOP marker before each packet.\n");
+ fprintf(stdout, "-EPH\n");
+ fprintf(stdout, " Write EPH marker after each header packet.\n");
+ fprintf(stdout, "-M <key value>\n");
+ fprintf(stdout, " Mode switch.\n");
+ fprintf(stdout, " [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
+ fprintf(stdout, " 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)]\n");
+ fprintf(stdout, " Indicate multiple modes by adding their values.\n");
+ fprintf(stdout,
+ " Example: RESTART(4) + RESET(2) + SEGMARK(32) => -M 38\n");
+ fprintf(stdout, "-TP <R|L|C>\n");
+ fprintf(stdout, " Divide packets of every tile into tile-parts.\n");
+ fprintf(stdout,
+ " Division is made by grouping Resolutions (R), Layers (L)\n");
+ fprintf(stdout, " or Components (C).\n");
+#ifdef FIXME_INDEX
+ fprintf(stdout, "-x <index file>\n");
+ fprintf(stdout, " Create an index file.\n");
+#endif /*FIXME_INDEX*/
+ fprintf(stdout, "-ROI c=<component index>,U=<upshifting value>\n");
+ fprintf(stdout, " Quantization indices upshifted for a component. \n");
+ fprintf(stdout,
+ " Warning: This option does not implement the usual ROI (Region of Interest).\n");
+ fprintf(stdout,
+ " It should be understood as a 'Component of Interest'. It offers the \n");
+ fprintf(stdout,
+ " possibility to upshift the value of a component during quantization step.\n");
+ fprintf(stdout,
+ " The value after c= is the component number [0, 1, 2, ...] and the value \n");
+ fprintf(stdout,
+ " after U= is the value of upshifting. U must be in the range [0, 37].\n");
+ fprintf(stdout, "-d <image offset X,image offset Y>\n");
+ fprintf(stdout, " Offset of the origin of the image.\n");
+ fprintf(stdout, "-T <tile offset X,tile offset Y>\n");
+ fprintf(stdout, " Offset of the origin of the tiles.\n");
+ fprintf(stdout, "-I\n");
+ fprintf(stdout, " Use the irreversible DWT 9-7.\n");
+ fprintf(stdout, "-mct <0|1|2>\n");
+ fprintf(stdout,
+ " Explicitly specifies if a Multiple Component Transform has to be used.\n");
+ fprintf(stdout, " 0: no MCT ; 1: RGB->YCC conversion ; 2: custom MCT.\n");
+ fprintf(stdout,
+ " If custom MCT, \"-m\" option has to be used (see hereunder).\n");
+ fprintf(stdout,
+ " By default, RGB->YCC conversion is used if there are 3 components or more,\n");
+ fprintf(stdout, " no conversion otherwise.\n");
+ fprintf(stdout, "-m <file>\n");
+ fprintf(stdout,
+ " Use array-based MCT, values are coma separated, line by line\n");
+ fprintf(stdout,
+ " No specific separators between lines, no space allowed between values.\n");
+ fprintf(stdout,
+ " If this option is used, it automatically sets \"-mct\" option to 2.\n");
+ fprintf(stdout, "-cinema2K <24|48>\n");
+ fprintf(stdout, " Digital Cinema 2K profile compliant codestream.\n");
+ fprintf(stdout,
+ " Need to specify the frames per second for a 2K resolution.\n");
+ fprintf(stdout, " Only 24 or 48 fps are currently allowed.\n");
+ fprintf(stdout, "-cinema4K\n");
+ fprintf(stdout, " Digital Cinema 4K profile compliant codestream.\n");
+ fprintf(stdout, " Frames per second not required. Default value is 24fps.\n");
+ fprintf(stdout, "-jpip\n");
+ fprintf(stdout, " Write jpip codestream index box in JP2 output file.\n");
+ fprintf(stdout, " Currently supports only RPCL order.\n");
+ fprintf(stdout, "-C <comment>\n");
+ fprintf(stdout, " Add <comment> in the comment marker segment.\n");
/* UniPG>> */
#ifdef USE_JPWL
- fprintf(stdout,"-W <params>\n");
- fprintf(stdout," Adoption of JPWL (Part 11) capabilities (-W params)\n");
- fprintf(stdout," The <params> field can be written and repeated in any order:\n");
- fprintf(stdout," [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
- fprintf(stdout," ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
- fprintf(stdout," h selects the header error protection (EPB): 'type' can be\n");
- fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
- fprintf(stdout," if 'tilepart' is absent, it is for main and tile headers\n");
- fprintf(stdout," if 'tilepart' is present, it applies from that tile\n");
- fprintf(stdout," onwards, up to the next h<> spec, or to the last tilepart\n");
- fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
- fprintf(stdout," p selects the packet error protection (EEP/UEP with EPBs)\n");
- fprintf(stdout," to be applied to raw data: 'type' can be\n");
- fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
- fprintf(stdout," if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
- fprintf(stdout," if 'tilepart:pack' is present, it applies from that tile\n");
- fprintf(stdout," and that packet onwards, up to the next packet spec\n");
- fprintf(stdout," or to the last packet in the last tilepart in the stream\n");
- fprintf(stdout," (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
- fprintf(stdout," s enables sensitivity data insertion (ESD): 'method' can be\n");
- fprintf(stdout," [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
- fprintf(stdout," 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
- fprintf(stdout," if 'tilepart' is absent, it is for main header only\n");
- fprintf(stdout," if 'tilepart' is present, it applies from that tile\n");
- fprintf(stdout," onwards, up to the next s<> spec, or to the last tilepart\n");
- fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
- fprintf(stdout," g determines the addressing mode: <range> can be\n");
- fprintf(stdout," [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
- fprintf(stdout," a determines the size of data addressing: <addr> can be\n");
- fprintf(stdout," 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
- fprintf(stdout," z determines the size of sensitivity values: <size> can be\n");
- fprintf(stdout," 1/2 bytes, for the transformed pseudo-floating point value\n");
- fprintf(stdout," ex.:\n");
- fprintf(stdout," h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
- fprintf(stdout," s0=6,s3=-1,a=0,g=1,z=1\n");
- fprintf(stdout," means\n");
- fprintf(stdout," predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
- fprintf(stdout," CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
- fprintf(stdout," UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
- fprintf(stdout," UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
- fprintf(stdout," UEP rs default for packets of tilepart 1,\n");
- fprintf(stdout," no UEP for packets 0 to 19 of tilepart 3,\n");
- fprintf(stdout," UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
- fprintf(stdout," relative sensitivity ESD for MH,\n");
- fprintf(stdout," TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
- fprintf(stdout," size of addresses and 1 byte for each sensitivity value\n");
- fprintf(stdout," ex.:\n");
- fprintf(stdout," h,s,p\n");
- fprintf(stdout," means\n");
- fprintf(stdout," default protection to headers (MH and TPHs) as well as\n");
- fprintf(stdout," data packets, one ESD in MH\n");
- fprintf(stdout," N.B.: use the following recommendations when specifying\n");
- fprintf(stdout," the JPWL parameters list\n");
- fprintf(stdout," - when you use UEP, always pair the 'p' option with 'h'\n");
+ fprintf(stdout, "-W <params>\n");
+ fprintf(stdout, " Adoption of JPWL (Part 11) capabilities (-W params)\n");
+ fprintf(stdout,
+ " The <params> field can be written and repeated in any order:\n");
+ fprintf(stdout, " [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
+ fprintf(stdout, " ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
+ fprintf(stdout,
+ " h selects the header error protection (EPB): 'type' can be\n");
+ fprintf(stdout,
+ " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
+ fprintf(stdout,
+ " if 'tilepart' is absent, it is for main and tile headers\n");
+ fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
+ fprintf(stdout,
+ " onwards, up to the next h<> spec, or to the last tilepart\n");
+ fprintf(stdout, " in the codestream (max. %d specs)\n",
+ JPWL_MAX_NO_TILESPECS);
+ fprintf(stdout,
+ " p selects the packet error protection (EEP/UEP with EPBs)\n");
+ fprintf(stdout, " to be applied to raw data: 'type' can be\n");
+ fprintf(stdout,
+ " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
+ fprintf(stdout,
+ " if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
+ fprintf(stdout,
+ " if 'tilepart:pack' is present, it applies from that tile\n");
+ fprintf(stdout,
+ " and that packet onwards, up to the next packet spec\n");
+ fprintf(stdout,
+ " or to the last packet in the last tilepart in the stream\n");
+ fprintf(stdout, " (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
+ fprintf(stdout,
+ " s enables sensitivity data insertion (ESD): 'method' can be\n");
+ fprintf(stdout,
+ " [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
+ fprintf(stdout, " 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
+ fprintf(stdout, " if 'tilepart' is absent, it is for main header only\n");
+ fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
+ fprintf(stdout,
+ " onwards, up to the next s<> spec, or to the last tilepart\n");
+ fprintf(stdout, " in the codestream (max. %d specs)\n",
+ JPWL_MAX_NO_TILESPECS);
+ fprintf(stdout, " g determines the addressing mode: <range> can be\n");
+ fprintf(stdout, " [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
+ fprintf(stdout,
+ " a determines the size of data addressing: <addr> can be\n");
+ fprintf(stdout,
+ " 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
+ fprintf(stdout,
+ " z determines the size of sensitivity values: <size> can be\n");
+ fprintf(stdout,
+ " 1/2 bytes, for the transformed pseudo-floating point value\n");
+ fprintf(stdout, " ex.:\n");
+ fprintf(stdout,
+ " h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
+ fprintf(stdout, " s0=6,s3=-1,a=0,g=1,z=1\n");
+ fprintf(stdout, " means\n");
+ fprintf(stdout,
+ " predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
+ fprintf(stdout,
+ " CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
+ fprintf(stdout, " UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
+ fprintf(stdout,
+ " UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
+ fprintf(stdout, " UEP rs default for packets of tilepart 1,\n");
+ fprintf(stdout, " no UEP for packets 0 to 19 of tilepart 3,\n");
+ fprintf(stdout,
+ " UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
+ fprintf(stdout, " relative sensitivity ESD for MH,\n");
+ fprintf(stdout,
+ " TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
+ fprintf(stdout,
+ " size of addresses and 1 byte for each sensitivity value\n");
+ fprintf(stdout, " ex.:\n");
+ fprintf(stdout, " h,s,p\n");
+ fprintf(stdout, " means\n");
+ fprintf(stdout,
+ " default protection to headers (MH and TPHs) as well as\n");
+ fprintf(stdout, " data packets, one ESD in MH\n");
+ fprintf(stdout,
+ " N.B.: use the following recommendations when specifying\n");
+ fprintf(stdout, " the JPWL parameters list\n");
+ fprintf(stdout,
+ " - when you use UEP, always pair the 'p' option with 'h'\n");
#endif /* USE_JPWL */
/* <<UniPG */
- fprintf(stdout,"\n");
+ fprintf(stdout, "\n");
#ifdef FIXME_INDEX
- fprintf(stdout,"Index structure:\n");
- fprintf(stdout,"----------------\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"Image_height Image_width\n");
- fprintf(stdout,"progression order\n");
- fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
- fprintf(stdout,"Tiles_nb_X Tiles_nb_Y\n");
- fprintf(stdout,"Components_nb\n");
- fprintf(stdout,"Layers_nb\n");
- fprintf(stdout,"decomposition_levels\n");
- fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
- fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
- fprintf(stdout,"Main_header_start_position\n");
- fprintf(stdout,"Main_header_end_position\n");
- fprintf(stdout,"Codestream_size\n");
- fprintf(stdout,"\n");
- fprintf(stdout,"INFO ON TILES\n");
- fprintf(stdout,"tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
- fprintf(stdout,"Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
- fprintf(stdout,"Tile_1 '' '' '' '' '' '' ''\n");
- fprintf(stdout,"...\n");
- fprintf(stdout,"Tile_Nt '' '' '' '' '' '' ''\n");
- fprintf(stdout,"...\n");
- fprintf(stdout,"TILE 0 DETAILS\n");
- fprintf(stdout,"part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
- fprintf(stdout,"...\n");
- fprintf(stdout,"Progression_string\n");
- fprintf(stdout,"pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
- fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
- fprintf(stdout,"...\n");
- fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
- fprintf(stdout,"MaxDisto\n");
- fprintf(stdout,"TotalDisto\n\n");
+ fprintf(stdout, "Index structure:\n");
+ fprintf(stdout, "----------------\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, "Image_height Image_width\n");
+ fprintf(stdout, "progression order\n");
+ fprintf(stdout, "Tiles_size_X Tiles_size_Y\n");
+ fprintf(stdout, "Tiles_nb_X Tiles_nb_Y\n");
+ fprintf(stdout, "Components_nb\n");
+ fprintf(stdout, "Layers_nb\n");
+ fprintf(stdout, "decomposition_levels\n");
+ fprintf(stdout, "[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
+ fprintf(stdout, " [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
+ fprintf(stdout, "Main_header_start_position\n");
+ fprintf(stdout, "Main_header_end_position\n");
+ fprintf(stdout, "Codestream_size\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, "INFO ON TILES\n");
+ fprintf(stdout,
+ "tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
+ fprintf(stdout,
+ "Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
+ fprintf(stdout,
+ "Tile_1 '' '' '' '' '' '' ''\n");
+ fprintf(stdout, "...\n");
+ fprintf(stdout,
+ "Tile_Nt '' '' '' '' '' '' ''\n");
+ fprintf(stdout, "...\n");
+ fprintf(stdout, "TILE 0 DETAILS\n");
+ fprintf(stdout, "part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
+ fprintf(stdout, "...\n");
+ fprintf(stdout, "Progression_string\n");
+ fprintf(stdout,
+ "pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
+ fprintf(stdout,
+ "Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
+ fprintf(stdout, "...\n");
+ fprintf(stdout,
+ "Tpacket_Np '' '' '' '' '' '' '' ''\n");
+ fprintf(stdout, "MaxDisto\n");
+ fprintf(stdout, "TotalDisto\n\n");
#endif /*FIXME_INDEX*/
}
-static OPJ_PROG_ORDER give_progression(const char progression[4]) {
- if(strncmp(progression, "LRCP", 4) == 0) {
+static OPJ_PROG_ORDER give_progression(const char progression[4])
+{
+ if (strncmp(progression, "LRCP", 4) == 0) {
return OPJ_LRCP;
}
- if(strncmp(progression, "RLCP", 4) == 0) {
+ if (strncmp(progression, "RLCP", 4) == 0) {
return OPJ_RLCP;
}
- if(strncmp(progression, "RPCL", 4) == 0) {
+ if (strncmp(progression, "RPCL", 4) == 0) {
return OPJ_RPCL;
}
- if(strncmp(progression, "PCRL", 4) == 0) {
+ if (strncmp(progression, "PCRL", 4) == 0) {
return OPJ_PCRL;
}
- if(strncmp(progression, "CPRL", 4) == 0) {
+ if (strncmp(progression, "CPRL", 4) == 0) {
return OPJ_CPRL;
}
return OPJ_PROG_UNKNOWN;
}
-static unsigned int get_num_images(char *imgdirpath){
+static unsigned int get_num_images(char *imgdirpath)
+{
DIR *dir;
struct dirent* content;
unsigned int num_images = 0;
/*Reading the input images from given input directory*/
- dir= opendir(imgdirpath);
- if(!dir){
- fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
+ dir = opendir(imgdirpath);
+ if (!dir) {
+ fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
return 0;
}
- num_images=0;
- while((content=readdir(dir))!=NULL){
- if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
+ num_images = 0;
+ while ((content = readdir(dir)) != NULL) {
+ if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
continue;
+ }
num_images++;
}
closedir(dir);
return num_images;
}
-static int load_images(dircnt_t *dirptr, char *imgdirpath){
+static int load_images(dircnt_t *dirptr, char *imgdirpath)
+{
DIR *dir;
struct dirent* content;
int i = 0;
/*Reading the input images from given input directory*/
- dir= opendir(imgdirpath);
- if(!dir){
- fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
+ dir = opendir(imgdirpath);
+ if (!dir) {
+ fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
return 1;
- }else {
- fprintf(stderr,"Folder opened successfully\n");
+ } else {
+ fprintf(stderr, "Folder opened successfully\n");
}
- while((content=readdir(dir))!=NULL){
- if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
+ while ((content = readdir(dir)) != NULL) {
+ if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
continue;
+ }
- strcpy(dirptr->filename[i],content->d_name);
+ strcpy(dirptr->filename[i], content->d_name);
i++;
}
- closedir(dir);
+ closedir(dir);
return 0;
}
-static int get_file_format(char *filename) {
+static int get_file_format(char *filename)
+{
unsigned int i;
static const char *extension[] = {
"pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc"
@@ -431,45 +508,54 @@ static int get_file_format(char *filename) {
PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT
};
char * ext = strrchr(filename, '.');
- if (ext == NULL)
+ if (ext == NULL) {
return -1;
+ }
ext++;
- for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
- if(strcasecmp(ext, extension[i]) == 0) {
+ for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
+ if (strcasecmp(ext, extension[i]) == 0) {
return format[i];
}
}
return -1;
}
-static char * get_file_name(char *name){
- char *fname = strtok(name,".");
+static char * get_file_name(char *name)
+{
+ char *fname = strtok(name, ".");
return fname;
}
-static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t *parameters){
- 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]="";
+static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
+ opj_cparameters_t *parameters)
+{
+ char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
+ outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
+ char *temp_p, temp1[OPJ_PATH_LEN] = "";
- strcpy(image_filename,dirptr->filename[imageno]);
- fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
+ strcpy(image_filename, dirptr->filename[imageno]);
+ fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
parameters->decod_format = get_file_format(image_filename);
- if (parameters->decod_format == -1)
+ if (parameters->decod_format == -1) {
return 1;
- sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
- if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) {
+ }
+ sprintf(infilename, "%s/%s", img_fol->imgdirpath, image_filename);
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
+ infilename) != 0) {
return 1;
}
-
+
/*Set output file*/
- strcpy(temp_ofname,get_file_name(image_filename));
- while((temp_p = strtok(NULL,".")) != NULL){
- strcat(temp_ofname,temp1);
- sprintf(temp1,".%s",temp_p);
+ strcpy(temp_ofname, get_file_name(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) {
+ 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;
}
}
@@ -478,45 +564,48 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_c
/* ------------------------------------------------------------------------------------ */
-static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
- img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename, size_t indexfilename_size) {
+static int parse_cmdline_encoder(int argc, char **argv,
+ opj_cparameters_t *parameters,
+ img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename,
+ size_t indexfilename_size)
+{
OPJ_UINT32 i, j;
int totlen, c;
- opj_option_t long_option[]={
- {"cinema2K",REQ_ARG, NULL ,'w'},
- {"cinema4K",NO_ARG, NULL ,'y'},
- {"ImgDir",REQ_ARG, NULL ,'z'},
- {"TP",REQ_ARG, NULL ,'u'},
- {"SOP",NO_ARG, NULL ,'S'},
- {"EPH",NO_ARG, NULL ,'E'},
- {"OutFor",REQ_ARG, NULL ,'O'},
- {"POC",REQ_ARG, NULL ,'P'},
- {"ROI",REQ_ARG, NULL ,'R'},
- {"jpip",NO_ARG, NULL, 'J'},
- {"mct",REQ_ARG, NULL, 'Y'}
+ opj_option_t long_option[] = {
+ {"cinema2K", REQ_ARG, NULL, 'w'},
+ {"cinema4K", NO_ARG, NULL, 'y'},
+ {"ImgDir", REQ_ARG, NULL, 'z'},
+ {"TP", REQ_ARG, NULL, 'u'},
+ {"SOP", NO_ARG, NULL, 'S'},
+ {"EPH", NO_ARG, NULL, 'E'},
+ {"OutFor", REQ_ARG, NULL, 'O'},
+ {"POC", REQ_ARG, NULL, 'P'},
+ {"ROI", REQ_ARG, NULL, 'R'},
+ {"jpip", NO_ARG, NULL, 'J'},
+ {"mct", REQ_ARG, NULL, 'Y'}
};
/* parse the command line */
const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:"
- #ifdef USE_JPWL
- "W:"
- #endif /* USE_JPWL */
- "h";
+#ifdef USE_JPWL
+ "W:"
+#endif /* USE_JPWL */
+ "h";
- totlen=sizeof(long_option);
- img_fol->set_out_format=0;
+ totlen = sizeof(long_option);
+ img_fol->set_out_format = 0;
raw_cp->rawWidth = 0;
- do{
- c = opj_getopt_long(argc, argv, optlist,long_option,totlen);
- if (c == -1)
+ do {
+ c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
+ if (c == -1) {
break;
+ }
switch (c) {
- case 'i': /* input file */
- {
+ case 'i': { /* input file */
char *infile = opj_optarg;
parameters->decod_format = get_file_format(infile);
- switch(parameters->decod_format) {
+ switch (parameters->decod_format) {
case PGX_DFMT:
case PXM_DFMT:
case BMP_DFMT:
@@ -537,37 +626,37 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
return 1;
}
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'o': /* output file */
- {
+ case 'o': { /* output file */
char *outfile = opj_optarg;
parameters->cod_format = get_file_format(outfile);
- switch(parameters->cod_format) {
+ switch (parameters->cod_format) {
case J2K_CFMT:
case JP2_CFMT:
break;
default:
- fprintf(stderr, "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
+ fprintf(stderr,
+ "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
return 1;
}
- if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) {
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
+ outfile) != 0) {
return 1;
}
}
- break;
+ break;
- /* ----------------------------------------------------- */
- case 'O': /* output format */
- {
+ /* ----------------------------------------------------- */
+ case 'O': { /* output format */
char outformat[50];
char *of = opj_optarg;
- sprintf(outformat,".%s",of);
+ sprintf(outformat, ".%s", of);
img_fol->set_out_format = 1;
parameters->cod_format = get_file_format(outformat);
- switch(parameters->cod_format) {
+ switch (parameters->cod_format) {
case J2K_CFMT:
case JP2_CFMT:
img_fol->out_format = opj_optarg;
@@ -577,56 +666,57 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
return 1;
}
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'r': /* rates rates/distorsion */
- {
+ case 'r': { /* rates rates/distorsion */
char *s = opj_optarg;
parameters->tcp_numlayers = 0;
- while (sscanf(s, "%f", &parameters->tcp_rates[parameters->tcp_numlayers]) == 1) {
+ while (sscanf(s, "%f", &parameters->tcp_rates[parameters->tcp_numlayers]) ==
+ 1) {
parameters->tcp_numlayers++;
while (*s && *s != ',') {
s++;
}
- if (!*s)
+ if (!*s) {
break;
+ }
s++;
}
parameters->cp_disto_alloc = 1;
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'F': /* Raw image format parameters */
- {
+ case 'F': { /* Raw image format parameters */
OPJ_BOOL wrong = OPJ_FALSE;
char *substr1;
char *substr2;
char *sep;
char signo;
- int width,height,bitdepth,ncomp;
+ int width, height, bitdepth, ncomp;
OPJ_UINT32 len;
OPJ_BOOL raw_signed = OPJ_FALSE;
- substr2 = strchr(opj_optarg,'@');
+ substr2 = strchr(opj_optarg, '@');
if (substr2 == NULL) {
len = (OPJ_UINT32) strlen(opj_optarg);
} else {
- len = (OPJ_UINT32) (substr2 - opj_optarg);
+ len = (OPJ_UINT32)(substr2 - opj_optarg);
substr2++; /* skip '@' character */
}
- substr1 = (char*) malloc((len+1)*sizeof(char));
+ substr1 = (char*) malloc((len + 1) * sizeof(char));
if (substr1 == NULL) {
return 1;
}
- memcpy(substr1,opj_optarg,len);
+ memcpy(substr1, opj_optarg, len);
substr1[len] = '\0';
- if (sscanf(substr1, "%d,%d,%d,%d,%c", &width, &height, &ncomp, &bitdepth, &signo) == 5) {
+ if (sscanf(substr1, "%d,%d,%d,%d,%c", &width, &height, &ncomp, &bitdepth,
+ &signo) == 5) {
if (signo == 's') {
raw_signed = OPJ_TRUE;
} else if (signo == 'u') {
@@ -646,18 +736,19 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
raw_cp->rawComp = ncomp;
raw_cp->rawBitDepth = bitdepth;
raw_cp->rawSigned = raw_signed;
- raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(ncomp))*sizeof(raw_comp_cparameters_t));
- if(raw_cp->rawComps == NULL){
- free(substr1);
- return 1;
- }
+ raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(
+ ncomp)) * sizeof(raw_comp_cparameters_t));
+ if (raw_cp->rawComps == NULL) {
+ free(substr1);
+ return 1;
+ }
for (compno = 0; compno < ncomp && !wrong; compno++) {
if (substr2 == NULL) {
raw_cp->rawComps[compno].dx = lastdx;
raw_cp->rawComps[compno].dy = lastdy;
} else {
- int dx,dy;
- sep = strchr(substr2,':');
+ int dx, dy;
+ sep = strchr(substr2, ':');
if (sep == NULL) {
if (sscanf(substr2, "%dx%d", &dx, &dy) == 2) {
lastdx = dx;
@@ -681,57 +772,61 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
}
free(substr1);
if (wrong) {
- fprintf(stderr,"\nError: invalid raw image parameters\n");
- fprintf(stderr,"Please use the Format option -F:\n");
- fprintf(stderr,"-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
- fprintf(stderr,"If subsampling is omitted, 1x1 is assumed for all components\n");
- fprintf(stderr,"Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
- fprintf(stderr," for raw 512x512 image with 4:2:0 subsampling\n");
- fprintf(stderr,"Aborting.\n");
+ fprintf(stderr, "\nError: invalid raw image parameters\n");
+ fprintf(stderr, "Please use the Format option -F:\n");
+ fprintf(stderr,
+ "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
+ fprintf(stderr,
+ "If subsampling is omitted, 1x1 is assumed for all components\n");
+ fprintf(stderr,
+ "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
+ fprintf(stderr, " for raw 512x512 image with 4:2:0 subsampling\n");
+ fprintf(stderr, "Aborting.\n");
return 1;
}
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'q': /* add fixed_quality */
- {
+ case 'q': { /* add fixed_quality */
char *s = opj_optarg;
- while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers]) == 1) {
+ while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers])
+ == 1) {
parameters->tcp_numlayers++;
while (*s && *s != ',') {
s++;
}
- if (!*s)
+ if (!*s) {
break;
+ }
s++;
}
parameters->cp_fixed_quality = 1;
}
- break;
+ break;
- /* dda */
- /* ----------------------------------------------------- */
+ /* dda */
+ /* ----------------------------------------------------- */
- case 'f': /* mod fixed_quality (before : -q) */
- {
+ case 'f': { /* mod fixed_quality (before : -q) */
int *row = NULL, *col = NULL;
OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0;
char *s = opj_optarg;
sscanf(s, "%u", &numlayers);
s++;
- if (numlayers > 9)
+ if (numlayers > 9) {
s++;
+ }
parameters->tcp_numlayers = (int)numlayers;
numresolution = (OPJ_UINT32)parameters->numresolution;
matrix_width = numresolution * 3;
parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
- if(parameters->cp_matrice == NULL){
- return 1;
- }
+ if (parameters->cp_matrice == NULL) {
+ return 1;
+ }
s = s + 2;
for (i = 0; i < numlayers; i++) {
@@ -740,48 +835,50 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
parameters->tcp_rates[i] = 1;
sscanf(s, "%d,", &col[0]);
s += 2;
- if (col[0] > 9)
+ if (col[0] > 9) {
s++;
+ }
col[1] = 0;
col[2] = 0;
for (j = 1; j < numresolution; j++) {
col += 3;
sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
s += 6;
- if (col[0] > 9)
+ if (col[0] > 9) {
s++;
- if (col[1] > 9)
+ }
+ if (col[1] > 9) {
s++;
- if (col[2] > 9)
+ }
+ if (col[2] > 9) {
s++;
+ }
}
- if (i < numlayers - 1)
+ if (i < numlayers - 1) {
s++;
+ }
}
parameters->cp_fixed_alloc = 1;
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 't': /* tiles */
- {
+ case 't': { /* tiles */
sscanf(opj_optarg, "%d,%d", &parameters->cp_tdx, &parameters->cp_tdy);
parameters->tile_size_on = OPJ_TRUE;
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'n': /* resolution */
- {
+ case 'n': { /* resolution */
sscanf(opj_optarg, "%d", &parameters->numresolution);
}
- break;
+ break;
- /* ----------------------------------------------------- */
- case 'c': /* precinct dimension */
- {
+ /* ----------------------------------------------------- */
+ case 'c': { /* precinct dimension */
char sep;
int res_spec = 0;
@@ -790,26 +887,24 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
do {
sep = 0;
ret = sscanf(s, "[%d,%d]%c", &parameters->prcw_init[res_spec],
- &parameters->prch_init[res_spec], &sep);
- if( !(ret == 2 && sep == 0) && !(ret == 3 && sep == ',') )
- {
- fprintf(stderr,"\nError: could not parse precinct dimension: '%s' %x\n", s, sep);
- fprintf(stderr,"Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n");
- return 1;
- }
+ &parameters->prch_init[res_spec], &sep);
+ if (!(ret == 2 && sep == 0) && !(ret == 3 && sep == ',')) {
+ fprintf(stderr, "\nError: could not parse precinct dimension: '%s' %x\n", s,
+ sep);
+ fprintf(stderr, "Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n");
+ return 1;
+ }
parameters->csty |= 0x01;
res_spec++;
s = strpbrk(s, "]") + 2;
- }
- while (sep == ',');
+ } while (sep == ',');
parameters->res_spec = res_spec;
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'b': /* code-block dimension */
- {
+ case 'b': { /* code-block dimension */
int cblockw_init = 0, cblockh_init = 0;
sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init);
if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
@@ -822,12 +917,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
parameters->cblockw_init = cblockw_init;
parameters->cblockh_init = cblockh_init;
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'x': /* creation of index file */
- {
+ case 'x': { /* creation of index file */
if (opj_strcpy_s(indexfilename, indexfilename_size, opj_optarg) != 0) {
return 1;
}
@@ -837,12 +931,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
" '-x' option ignored.\n");
/* << FIXME ADE INDEX */
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'p': /* progression order */
- {
+ case 'p': { /* progression order */
char progression[4];
strncpy(progression, opj_optarg, 4);
@@ -853,45 +946,42 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
return 1;
}
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 's': /* subsampling factor */
- {
+ case 's': { /* subsampling factor */
if (sscanf(opj_optarg, "%d,%d", &parameters->subsampling_dx,
&parameters->subsampling_dy) != 2) {
- fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
+ fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
return 1;
}
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'd': /* coordonnate of the reference grid */
- {
+ case 'd': { /* coordonnate of the reference grid */
if (sscanf(opj_optarg, "%d,%d", &parameters->image_offset_x0,
&parameters->image_offset_y0) != 2) {
- fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
+ fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
"error !! [-d x0,y0]\n");
return 1;
}
}
- break;
+ break;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'h': /* display an help description */
+ case 'h': /* display an help description */
encode_help_display();
return 1;
- /* ----------------------------------------------------- */
+ /* ----------------------------------------------------- */
- case 'P': /* POC */
- {
- int numpocs = 0; /* number of progression order change (POC) default 0 */
- opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
+ case 'P': { /* POC */
+ int numpocs = 0; /* number of progression order change (POC) default 0 */
+ opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
char *s = opj_optarg;
POC = parameters->POC;
@@ -912,157 +1002,147 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
}
parameters->numpocs = (OPJ_UINT32)numpocs;
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'S': /* SOP marker */
- {
+ case 'S': { /* SOP marker */
parameters->csty |= 0x02;
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'E': /* EPH marker */
- {
+ case 'E': { /* EPH marker */
parameters->csty |= 0x04;
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'M': /* Mode switch pas tous au point !! */
- {
+ case 'M': { /* Mode switch pas tous au point !! */
int value = 0;
if (sscanf(opj_optarg, "%d", &value) == 1) {
for (i = 0; i <= 5; i++) {
int cache = value & (1 << i);
- if (cache)
+ if (cache) {
parameters->mode |= (1 << i);
+ }
}
}
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'R': /* ROI */
- {
+ case 'R': { /* ROI */
if (sscanf(opj_optarg, "c=%d,U=%d", &parameters->roi_compno,
&parameters->roi_shift) != 2) {
fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n");
return 1;
}
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'T': /* Tile offset */
- {
- if (sscanf(opj_optarg, "%d,%d", &parameters->cp_tx0, &parameters->cp_ty0) != 2) {
+ case 'T': { /* Tile offset */
+ if (sscanf(opj_optarg, "%d,%d", &parameters->cp_tx0,
+ &parameters->cp_ty0) != 2) {
fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
return 1;
}
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'C': /* add a comment */
- {
+ case 'C': { /* add a comment */
parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1);
- if(parameters->cp_comment) {
+ if (parameters->cp_comment) {
strcpy(parameters->cp_comment, opj_optarg);
}
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'I': /* reversible or not */
- {
+ case 'I': { /* reversible or not */
parameters->irreversible = 1;
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'u': /* Tile part generation*/
- {
+ case 'u': { /* Tile part generation*/
parameters->tp_flag = opj_optarg[0];
parameters->tp_on = 1;
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'z': /* Image Directory path */
- {
+ case 'z': { /* Image Directory path */
img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
- if(img_fol->imgdirpath == NULL){
- return 1;
- }
- strcpy(img_fol->imgdirpath,opj_optarg);
- img_fol->set_imgdir=1;
+ if (img_fol->imgdirpath == NULL) {
+ return 1;
+ }
+ strcpy(img_fol->imgdirpath, opj_optarg);
+ img_fol->set_imgdir = 1;
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'w': /* Digital Cinema 2K profile compliance*/
- {
- int fps=0;
- sscanf(opj_optarg,"%d",&fps);
- if(fps == 24){
+ case 'w': { /* Digital Cinema 2K profile compliance*/
+ int fps = 0;
+ sscanf(opj_optarg, "%d", &fps);
+ if (fps == 24) {
parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
parameters->max_comp_size = OPJ_CINEMA_24_COMP;
parameters->max_cs_size = OPJ_CINEMA_24_CS;
- }else if(fps == 48 ){
+ } else if (fps == 48) {
parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
parameters->max_comp_size = OPJ_CINEMA_48_COMP;
parameters->max_cs_size = OPJ_CINEMA_48_CS;
- }else {
- fprintf(stderr,"Incorrect value!! must be 24 or 48\n");
+ } else {
+ fprintf(stderr, "Incorrect value!! must be 24 or 48\n");
return 1;
}
- fprintf(stdout,"CINEMA 2K profile activated\n"
+ fprintf(stdout, "CINEMA 2K profile activated\n"
"Other options specified could be overridden\n");
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'y': /* Digital Cinema 4K profile compliance*/
- {
+ case 'y': { /* Digital Cinema 4K profile compliance*/
parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
- fprintf(stdout,"CINEMA 4K profile activated\n"
+ fprintf(stdout, "CINEMA 4K profile activated\n"
"Other options specified could be overridden\n");
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'Y': /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/
- {
- int mct_mode=0;
- sscanf(opj_optarg,"%d",&mct_mode);
- if(mct_mode < 0 || mct_mode > 2){
- fprintf(stderr,"MCT incorrect value!! Current accepted values are 0, 1 or 2.\n");
+ case 'Y': { /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/
+ int mct_mode = 0;
+ sscanf(opj_optarg, "%d", &mct_mode);
+ if (mct_mode < 0 || mct_mode > 2) {
+ fprintf(stderr,
+ "MCT incorrect value!! Current accepted values are 0, 1 or 2.\n");
return 1;
}
parameters->tcp_mct = (char) mct_mode;
}
- break;
+ break;
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'm': /* mct input file */
- {
+ case 'm': { /* mct input file */
char *lFilename = opj_optarg;
char *lMatrix;
char *lCurrentPtr ;
@@ -1073,15 +1153,15 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
size_t lStrLen, lStrFread;
/* Open file */
- FILE * lFile = fopen(lFilename,"r");
+ FILE * lFile = fopen(lFilename, "r");
if (lFile == NULL) {
return 1;
}
/* Set size of file and read its content*/
- fseek(lFile,0,SEEK_END);
+ fseek(lFile, 0, SEEK_END);
lStrLen = (size_t)ftell(lFile);
- fseek(lFile,0,SEEK_SET);
+ fseek(lFile, 0, SEEK_SET);
lMatrix = (char *) malloc(lStrLen + 1);
if (lMatrix == NULL) {
fclose(lFile);
@@ -1089,7 +1169,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
}
lStrFread = fread(lMatrix, 1, lStrLen, lFile);
fclose(lFile);
- if( lStrLen != lStrFread ) {
+ if (lStrLen != lStrFread) {
free(lMatrix);
return 1;
}
@@ -1098,7 +1178,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
lCurrentPtr = lMatrix;
/* replace ',' by 0 */
- while (*lCurrentPtr != 0 ) {
+ while (*lCurrentPtr != 0) {
if (*lCurrentPtr == ' ') {
*lCurrentPtr = 0;
++lNbComp;
@@ -1108,52 +1188,53 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
++lNbComp;
lCurrentPtr = lMatrix;
- lNbComp = (int) (sqrt(4*lNbComp + 1)/2. - 0.5);
+ lNbComp = (int)(sqrt(4 * lNbComp + 1) / 2. - 0.5);
lMctComp = lNbComp * lNbComp;
lTotalComp = lMctComp + lNbComp;
lSpace = (float *) malloc((size_t)lTotalComp * sizeof(float));
- if(lSpace == NULL) {
+ if (lSpace == NULL) {
free(lMatrix);
return 1;
}
lCurrentDoublePtr = lSpace;
- for (i2=0;i2<lMctComp;++i2) {
+ for (i2 = 0; i2 < lMctComp; ++i2) {
lStrLen = strlen(lCurrentPtr) + 1;
*lCurrentDoublePtr++ = (float) atof(lCurrentPtr);
lCurrentPtr += lStrLen;
}
l_int_ptr = (int*) lCurrentDoublePtr;
- for (i2=0;i2<lNbComp;++i2) {
+ for (i2 = 0; i2 < lNbComp; ++i2) {
lStrLen = strlen(lCurrentPtr) + 1;
*l_int_ptr++ = atoi(lCurrentPtr);
lCurrentPtr += lStrLen;
}
/* TODO should not be here ! */
- opj_set_MCT(parameters, lSpace, (int *)(lSpace + lMctComp), (OPJ_UINT32)lNbComp);
+ opj_set_MCT(parameters, lSpace, (int *)(lSpace + lMctComp),
+ (OPJ_UINT32)lNbComp);
/* Free memory*/
free(lSpace);
free(lMatrix);
}
- break;
+ break;
/* ------------------------------------------------------ */
/* UniPG>> */
#ifdef USE_JPWL
- /* ------------------------------------------------------ */
+ /* ------------------------------------------------------ */
- case 'W': /* JPWL capabilities switched on */
- {
+ case 'W': { /* JPWL capabilities switched on */
char *token = NULL;
int hprot, pprot, sens, addr, size, range;
/* we need to enable indexing */
if (!indexfilename || !*indexfilename) {
- if (opj_strcpy_s(indexfilename, indexfilename_size, JPWL_PRIVATEINDEX_NAME) != 0) {
+ if (opj_strcpy_s(indexfilename, indexfilename_size,
+ JPWL_PRIVATEINDEX_NAME) != 0) {
return 1;
}
}
@@ -1162,7 +1243,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
/* break the option in comma points and parse the result */
token = strtok(opj_optarg, ",");
- while(token != NULL) {
+ while (token != NULL) {
/* search header error protection method */
if (*token == 'h') {
@@ -1171,24 +1252,27 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
hprot = 1; /* predefined method */
- if(sscanf(token, "h=%d", &hprot) == 1) {
+ if (sscanf(token, "h=%d", &hprot) == 1) {
/* Main header, specified */
if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
- ((hprot >= 37) && (hprot <= 128)))) {
- fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n", hprot);
+ ((hprot >= 37) && (hprot <= 128)))) {
+ fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n",
+ hprot);
return 1;
}
parameters->jpwl_hprot_MH = hprot;
- } else if(sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
+ } else if (sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
/* Tile part header, specified */
if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
- ((hprot >= 37) && (hprot <= 128)))) {
- fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n", hprot);
+ ((hprot >= 37) && (hprot <= 128)))) {
+ fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n",
+ hprot);
return 1;
}
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on protection method t = %d\n", tile);
return 1;
}
if (tilespec < JPWL_MAX_NO_TILESPECS) {
@@ -1196,10 +1280,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
parameters->jpwl_hprot_TPH[tilespec++] = hprot;
}
- } else if(sscanf(token, "h%d", &tile) == 1) {
+ } else if (sscanf(token, "h%d", &tile) == 1) {
/* Tile part header, unspecified */
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on protection method t = %d\n", tile);
return 1;
}
if (tilespec < JPWL_MAX_NO_TILESPECS) {
@@ -1229,8 +1314,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
if (sscanf(token, "p=%d", &pprot) == 1) {
/* Method for all tiles and all packets */
if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
- ((pprot >= 37) && (pprot <= 128)))) {
- fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n", pprot);
+ ((pprot >= 37) && (pprot <= 128)))) {
+ fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n",
+ pprot);
return 1;
}
parameters->jpwl_pprot_tileno[0] = 0;
@@ -1240,12 +1326,13 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
} else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) {
/* method specified from that tile on */
if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
- ((pprot >= 37) && (pprot <= 128)))) {
+ ((pprot >= 37) && (pprot <= 128)))) {
fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
return 1;
}
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on protection method p = %d\n", tile);
return 1;
}
if (packspec < JPWL_MAX_NO_PACKSPECS) {
@@ -1257,16 +1344,18 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
} else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) {
/* method fully specified from that tile and that packet on */
if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
- ((pprot >= 37) && (pprot <= 128)))) {
+ ((pprot >= 37) && (pprot <= 128)))) {
fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
return 1;
}
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on protection method p = %d\n", tile);
return 1;
}
if (pack < 0) {
- fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack);
+ fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
+ pack);
return 1;
}
if (packspec < JPWL_MAX_NO_PACKSPECS) {
@@ -1278,16 +1367,18 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
} else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) {
/* default method from that tile and that packet on */
if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
- ((pprot >= 37) && (pprot <= 128)))) {
+ ((pprot >= 37) && (pprot <= 128)))) {
fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
return 1;
}
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on protection method p = %d\n", tile);
return 1;
}
if (pack < 0) {
- fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack);
+ fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
+ pack);
return 1;
}
if (packspec < JPWL_MAX_NO_PACKSPECS) {
@@ -1299,7 +1390,8 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
} else if (sscanf(token, "p%d", &tile) == 1) {
/* default from a tile on */
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on protection method p = %d\n", tile);
return 1;
}
if (packspec < JPWL_MAX_NO_PACKSPECS) {
@@ -1329,22 +1421,25 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
sens = 0; /* predefined: relative error */
- if(sscanf(token, "s=%d", &sens) == 1) {
+ if (sscanf(token, "s=%d", &sens) == 1) {
/* Main header, specified */
if ((sens < -1) || (sens > 7)) {
- fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n", sens);
+ fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n",
+ sens);
return 1;
}
parameters->jpwl_sens_MH = sens;
- } else if(sscanf(token, "s%d=%d", &tile, &sens) == 2) {
+ } else if (sscanf(token, "s%d=%d", &tile, &sens) == 2) {
/* Tile part header, specified */
if ((sens < -1) || (sens > 7)) {
- fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n", sens);
+ fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n",
+ sens);
return 1;
}
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
return 1;
}
if (tilespec < JPWL_MAX_NO_TILESPECS) {
@@ -1352,10 +1447,11 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
parameters->jpwl_sens_TPH[tilespec++] = sens;
}
- } else if(sscanf(token, "s%d", &tile) == 1) {
+ } else if (sscanf(token, "s%d", &tile) == 1) {
/* Tile part header, unspecified */
if (tile < 0) {
- fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
+ fprintf(stderr,
+ "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
return 1;
}
if (tilespec < JPWL_MAX_NO_TILESPECS) {
@@ -1381,7 +1477,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
addr = 0; /* predefined: auto */
- if(sscanf(token, "a=%d", &addr) == 1) {
+ if (sscanf(token, "a=%d", &addr) == 1) {
/* Specified */
if ((addr != 0) && (addr != 2) && (addr != 4)) {
fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr);
@@ -1406,7 +1502,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
size = 1; /* predefined: 1 byte */
- if(sscanf(token, "z=%d", &size) == 1) {
+ if (sscanf(token, "z=%d", &size) == 1) {
/* Specified */
if ((size != 0) && (size != 1) && (size != 2)) {
fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size);
@@ -1431,7 +1527,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
range = 0; /* predefined: 0 (packet) */
- if(sscanf(token, "g=%d", &range) == 1) {
+ if (sscanf(token, "g=%d", &range) == 1) {
/* Specified */
if ((range < 0) || (range > 3)) {
fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range);
@@ -1460,76 +1556,81 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
parameters->jpwl_epc_on = OPJ_TRUE;
}
- break;
+ break;
#endif /* USE_JPWL */
- /* <<UniPG */
- /* ------------------------------------------------------ */
+ /* <<UniPG */
+ /* ------------------------------------------------------ */
- case 'J': /* jpip on */
- {
+ case 'J': { /* jpip on */
parameters->jpip_on = OPJ_TRUE;
}
- break;
- /* ------------------------------------------------------ */
+ break;
+ /* ------------------------------------------------------ */
default:
fprintf(stderr, "[WARNING] An invalid option has been ignored\n");
break;
}
- }while(c != -1);
+ } while (c != -1);
- if(img_fol->set_imgdir == 1){
- if(!(parameters->infile[0] == 0)){
+ if (img_fol->set_imgdir == 1) {
+ if (!(parameters->infile[0] == 0)) {
fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together !!\n");
return 1;
}
- if(img_fol->set_out_format == 0){
- fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
+ if (img_fol->set_out_format == 0) {
+ fprintf(stderr,
+ "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n");
return 1;
}
- if(!((parameters->outfile[0] == 0))){
+ if (!((parameters->outfile[0] == 0))) {
fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together !!\n");
fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n");
return 1;
}
- }else{
- if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
+ } else {
+ if ((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
fprintf(stderr, "[ERROR] Required parameters are missing\n"
- "Example: %s -i image.pgm -o image.j2k\n",argv[0]);
- fprintf(stderr, " Help: %s -h\n",argv[0]);
+ "Example: %s -i image.pgm -o image.j2k\n", argv[0]);
+ fprintf(stderr, " Help: %s -h\n", argv[0]);
return 1;
}
}
- if ( (parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0)
- || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) {
- fprintf(stderr,"[ERROR] invalid raw image parameters\n");
- fprintf(stderr,"Please use the Format option -F:\n");
- fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
- fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
- fprintf(stderr,"Aborting\n");
+ if ((parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0)
+ || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) {
+ fprintf(stderr, "[ERROR] invalid raw image parameters\n");
+ fprintf(stderr, "Please use the Format option -F:\n");
+ fprintf(stderr,
+ "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
+ fprintf(stderr, "Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
+ fprintf(stderr, "Aborting\n");
return 1;
}
- if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality)
- && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^ parameters->cp_fixed_quality))) {
+ if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc ||
+ parameters->cp_fixed_quality)
+ && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^
+ parameters->cp_fixed_quality))) {
fprintf(stderr, "[ERROR] options -r -q and -f cannot be used together !!\n");
return 1;
- } /* mod fixed_quality */
+ } /* mod fixed_quality */
/* if no rate entered, lossless by default */
if (parameters->tcp_numlayers == 0) {
- parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
+ parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
parameters->tcp_numlayers++;
parameters->cp_disto_alloc = 1;
}
- if((parameters->cp_tx0 > parameters->image_offset_x0) || (parameters->cp_ty0 > parameters->image_offset_y0)) {
+ if ((parameters->cp_tx0 > parameters->image_offset_x0) ||
+ (parameters->cp_ty0 > parameters->image_offset_y0)) {
fprintf(stderr,
"[ERROR] Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
- parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0, parameters->image_offset_y0);
+ parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0,
+ parameters->image_offset_y0);
return 1;
}
@@ -1542,10 +1643,13 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
}
/* If subsampled image is provided, automatically disable MCT */
- if ( ((parameters->decod_format == RAW_DFMT) || (parameters->decod_format == RAWL_DFMT))
- && ( ((raw_cp->rawComp > 1 ) && ((raw_cp->rawComps[1].dx > 1) || (raw_cp->rawComps[1].dy > 1)))
- || ((raw_cp->rawComp > 2 ) && ((raw_cp->rawComps[2].dx > 1) || (raw_cp->rawComps[2].dy > 1)))
- )) {
+ if (((parameters->decod_format == RAW_DFMT) ||
+ (parameters->decod_format == RAWL_DFMT))
+ && (((raw_cp->rawComp > 1) && ((raw_cp->rawComps[1].dx > 1) ||
+ (raw_cp->rawComps[1].dy > 1)))
+ || ((raw_cp->rawComp > 2) && ((raw_cp->rawComps[2].dx > 1) ||
+ (raw_cp->rawComps[2].dy > 1)))
+ )) {
parameters->tcp_mct = 0;
}
@@ -1558,46 +1662,51 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
/**
sample error debug callback expecting no client object
*/
-static void error_callback(const char *msg, void *client_data) {
+static void error_callback(const char *msg, void *client_data)
+{
(void)client_data;
fprintf(stdout, "[ERROR] %s", msg);
}
/**
sample warning debug callback expecting no client object
*/
-static void warning_callback(const char *msg, void *client_data) {
+static void warning_callback(const char *msg, void *client_data)
+{
(void)client_data;
fprintf(stdout, "[WARNING] %s", msg);
}
/**
sample debug callback expecting no client object
*/
-static void info_callback(const char *msg, void *client_data) {
+static void info_callback(const char *msg, void *client_data)
+{
(void)client_data;
fprintf(stdout, "[INFO] %s", msg);
}
-OPJ_FLOAT64 opj_clock(void) {
+OPJ_FLOAT64 opj_clock(void)
+{
#ifdef _WIN32
- /* _WIN32: use QueryPerformance (very accurate) */
- LARGE_INTEGER freq , t ;
+ /* _WIN32: use QueryPerformance (very accurate) */
+ LARGE_INTEGER freq, t ;
/* freq is the clock speed of the CPU */
QueryPerformanceFrequency(&freq) ;
- /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
+ /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
/* t is the high resolution performance counter (see MSDN) */
- QueryPerformanceCounter ( & t ) ;
- return freq.QuadPart ? ( t.QuadPart /(OPJ_FLOAT64) freq.QuadPart ) : 0 ;
+ QueryPerformanceCounter(& t) ;
+ return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64) freq.QuadPart) : 0 ;
#else
- /* Unix or Linux: use resource usage */
+ /* Unix or Linux: use resource usage */
struct rusage t;
OPJ_FLOAT64 procTime;
/* (1) Get the rusage data structure at this moment (man getrusage) */
- getrusage(0,&t);
+ getrusage(0, &t);
/* (2) What is the elapsed time ? - CPU time = User time + System time */
- /* (2a) Get the seconds */
+ /* (2a) Get the seconds */
procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
/* (2b) More precisely! Get the microseconds part ! */
- return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;
+ return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) *
+ 1e-6) ;
#endif
}
@@ -1607,9 +1716,10 @@ OPJ_FLOAT64 opj_clock(void) {
* OPJ_COMPRESS MAIN
*/
/* -------------------------------------------------------------------------- */
-int main(int argc, char **argv) {
+int main(int argc, char **argv)
+{
- opj_cparameters_t parameters; /* compression parameters */
+ opj_cparameters_t parameters; /* compression parameters */
opj_stream_t *l_stream = 00;
opj_codec_t* l_codec = 00;
@@ -1617,7 +1727,7 @@ int main(int argc, char **argv) {
raw_cparameters_t raw_cp;
OPJ_SIZE_T num_compressed_files = 0;
- char indexfilename[OPJ_PATH_LEN]; /* index file name */
+ char indexfilename[OPJ_PATH_LEN]; /* index file name */
unsigned int i, num_images, imageno;
img_fol_t img_fol;
@@ -1633,7 +1743,7 @@ int main(int argc, char **argv) {
/* Initialize indexfilename and img_fol */
*indexfilename = 0;
- memset(&img_fol,0,sizeof(img_fol_t));
+ memset(&img_fol, 0, sizeof(img_fol_t));
/* raw_cp initialization */
raw_cp.rawBitDepth = 0;
@@ -1644,48 +1754,51 @@ int main(int argc, char **argv) {
raw_cp.rawWidth = 0;
/* parse input and get user encoding parameters */
- parameters.tcp_mct = (char) 255; /* This will be set later according to the input image or the provided option */
- if(parse_cmdline_encoder(argc, argv, &parameters,&img_fol, &raw_cp, indexfilename, sizeof(indexfilename)) == 1) {
+ parameters.tcp_mct = (char)
+ 255; /* This will be set later according to the input image or the provided option */
+ if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol, &raw_cp,
+ indexfilename, sizeof(indexfilename)) == 1) {
goto fails;
}
/* Read directory if necessary */
- if(img_fol.set_imgdir==1){
- num_images=get_num_images(img_fol.imgdirpath);
- dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
- if(dirptr){
- dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
- dirptr->filename = (char**) malloc(num_images*sizeof(char*));
- if(!dirptr->filename_buf){
+ if (img_fol.set_imgdir == 1) {
+ num_images = get_num_images(img_fol.imgdirpath);
+ dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
+ if (dirptr) {
+ dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
+ char)); /* Stores at max 10 image file names*/
+ dirptr->filename = (char**) malloc(num_images * sizeof(char*));
+ if (!dirptr->filename_buf) {
return 0;
}
- for(i=0;i<num_images;i++){
- dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
+ for (i = 0; i < num_images; i++) {
+ dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN;
}
}
- if(load_images(dirptr,img_fol.imgdirpath)==1){
+ if (load_images(dirptr, img_fol.imgdirpath) == 1) {
return 0;
}
- if (num_images==0){
- fprintf(stdout,"Folder is empty\n");
+ if (num_images == 0) {
+ fprintf(stdout, "Folder is empty\n");
return 0;
}
- }else{
- num_images=1;
+ } else {
+ num_images = 1;
}
/*Encoding image one by one*/
- for(imageno=0;imageno<num_images;imageno++) {
+ for (imageno = 0; imageno < num_images; imageno++) {
image = NULL;
- fprintf(stderr,"\n");
+ fprintf(stderr, "\n");
- if(img_fol.set_imgdir==1){
- if (get_next_file((int)imageno, dirptr,&img_fol, &parameters)) {
- fprintf(stderr,"skipping file...\n");
+ if (img_fol.set_imgdir == 1) {
+ if (get_next_file((int)imageno, dirptr, &img_fol, &parameters)) {
+ fprintf(stderr, "skipping file...\n");
continue;
}
}
- switch(parameters.decod_format) {
+ switch (parameters.decod_format) {
case PGX_DFMT:
break;
case PXM_DFMT:
@@ -1702,7 +1815,7 @@ int main(int argc, char **argv) {
case PNG_DFMT:
break;
default:
- fprintf(stderr,"skipping file...\n");
+ fprintf(stderr, "skipping file...\n");
continue;
}
@@ -1780,23 +1893,24 @@ int main(int argc, char **argv) {
}
/* Can happen if input file is TIFF or PNG
- * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
-*/
- if( !image) {
+ * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
+ */
+ if (!image) {
fprintf(stderr, "Unable to load file: got no image\n");
return 1;
}
/* Decide if MCT should be used */
- if (parameters.tcp_mct == (char) 255) { /* mct mode has not been set in commandline */
+ if (parameters.tcp_mct == (char)
+ 255) { /* mct mode has not been set in commandline */
parameters.tcp_mct = (image->numcomps >= 3) ? 1 : 0;
} else { /* mct mode has been set in commandline */
- if ((parameters.tcp_mct == 1) && (image->numcomps < 3)){
+ if ((parameters.tcp_mct == 1) && (image->numcomps < 3)) {
fprintf(stderr, "RGB->YCC conversion cannot be used:\n");
fprintf(stderr, "Input image has less than 3 components\n");
return 1;
}
- if ((parameters.tcp_mct == 2) && (!parameters.mct_data)){
+ if ((parameters.tcp_mct == 2) && (!parameters.mct_data)) {
fprintf(stderr, "Custom MCT has been set but no array-based MCT\n");
fprintf(stderr, "has been provided. Aborting.\n");
return 1;
@@ -1806,15 +1920,13 @@ int main(int argc, char **argv) {
/* encode the destination image */
/* ---------------------------- */
- switch(parameters.cod_format) {
- case J2K_CFMT: /* JPEG-2000 codestream */
- {
+ switch (parameters.cod_format) {
+ case J2K_CFMT: { /* JPEG-2000 codestream */
/* Get a decoder handle */
l_codec = opj_create_compress(OPJ_CODEC_J2K);
break;
}
- case JP2_CFMT: /* JPEG 2000 compressed image data */
- {
+ case JP2_CFMT: { /* JPEG 2000 compressed image data */
/* Get a decoder handle */
l_codec = opj_create_compress(OPJ_CODEC_JP2);
break;
@@ -1826,11 +1938,11 @@ int main(int argc, char **argv) {
}
/* 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);
+ 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);
- if( bUseTiles ) {
+ if (bUseTiles) {
parameters.cp_tx0 = 0;
parameters.cp_ty0 = 0;
parameters.tile_size_on = OPJ_TRUE;
@@ -1845,26 +1957,27 @@ int main(int argc, char **argv) {
}
/* open a byte stream for writing and allocate memory for all tiles */
- l_stream = opj_stream_create_default_file_stream(parameters.outfile,OPJ_FALSE);
- if (! l_stream){
+ l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE);
+ if (! l_stream) {
return 1;
}
/* encode the image */
- bSuccess = opj_start_compress(l_codec,image,l_stream);
+ bSuccess = opj_start_compress(l_codec, image, l_stream);
if (!bSuccess) {
fprintf(stderr, "failed to encode image: opj_start_compress\n");
}
- if( bSuccess && bUseTiles ) {
+ if (bSuccess && bUseTiles) {
OPJ_BYTE *l_data;
- OPJ_UINT32 l_data_size = 512*512*3;
- l_data = (OPJ_BYTE*) calloc( 1,l_data_size);
- if(l_data == NULL){
- goto fails;
- }
- for (i=0;i<l_nb_tiles;++i) {
- if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
- fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
+ OPJ_UINT32 l_data_size = 512 * 512 * 3;
+ l_data = (OPJ_BYTE*) calloc(1, l_data_size);
+ if (l_data == NULL) {
+ goto fails;
+ }
+ for (i = 0; i < l_nb_tiles; ++i) {
+ if (! opj_write_tile(l_codec, i, l_data, l_data_size, l_stream)) {
+ fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",
+ i);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(image);
@@ -1872,8 +1985,7 @@ int main(int argc, char **argv) {
}
}
free(l_data);
- }
- else {
+ } else {
bSuccess = bSuccess && opj_encode(l_codec, l_stream);
if (!bSuccess) {
fprintf(stderr, "failed to encode image: opj_encode\n");
@@ -1889,12 +2001,12 @@ int main(int argc, char **argv) {
opj_destroy_codec(l_codec);
opj_image_destroy(image);
fprintf(stderr, "failed to encode image\n");
- remove(parameters.outfile);
+ remove(parameters.outfile);
return 1;
}
- num_compressed_files++;
- fprintf(stdout,"[INFO] Generated outfile %s\n",parameters.outfile);
+ num_compressed_files++;
+ fprintf(stdout, "[INFO] Generated outfile %s\n", parameters.outfile);
/* close and free the byte stream */
opj_stream_destroy(l_stream);
@@ -1907,26 +2019,45 @@ int main(int argc, char **argv) {
}
/* free user parameters structure */
- if(parameters.cp_comment) free(parameters.cp_comment);
- if(parameters.cp_matrice) free(parameters.cp_matrice);
- if(raw_cp.rawComps) free(raw_cp.rawComps);
-
+ if (parameters.cp_comment) {
+ free(parameters.cp_comment);
+ }
+ if (parameters.cp_matrice) {
+ free(parameters.cp_matrice);
+ }
+ if (raw_cp.rawComps) {
+ free(raw_cp.rawComps);
+ }
+
t = opj_clock() - t;
if (num_compressed_files) {
- fprintf(stdout, "encode time: %d ms \n", (int)((t * 1000.0)/(OPJ_FLOAT64)num_compressed_files));
+ fprintf(stdout, "encode time: %d ms \n",
+ (int)((t * 1000.0) / (OPJ_FLOAT64)num_compressed_files));
}
return 0;
fails:
- if(parameters.cp_comment) free(parameters.cp_comment);
- if(parameters.cp_matrice) free(parameters.cp_matrice);
- if(raw_cp.rawComps) free(raw_cp.rawComps);
- if(img_fol.imgdirpath) free(img_fol.imgdirpath);
- if(dirptr){
- if(dirptr->filename_buf) free(dirptr->filename_buf);
- if(dirptr->filename) free(dirptr->filename);
- free(dirptr);
- }
- return 1;
+ if (parameters.cp_comment) {
+ free(parameters.cp_comment);
+ }
+ if (parameters.cp_matrice) {
+ free(parameters.cp_matrice);
+ }
+ if (raw_cp.rawComps) {
+ free(raw_cp.rawComps);
+ }
+ if (img_fol.imgdirpath) {
+ free(img_fol.imgdirpath);
+ }
+ if (dirptr) {
+ if (dirptr->filename_buf) {
+ free(dirptr->filename_buf);
+ }
+ if (dirptr->filename) {
+ free(dirptr->filename);
+ }
+ free(dirptr);
+ }
+ return 1;
}
diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c
index 83160c3d..397e6370 100644
--- a/src/bin/jp2/opj_decompress.c
+++ b/src/bin/jp2/opj_decompress.c
@@ -1,6 +1,6 @@
/*
- * The copyright in this software is being made available under the 2-clauses
- * BSD License, included below. This software may be subject to other third
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
@@ -8,11 +8,11 @@
* Copyright (c) 2002-2014, Professor Benoit Macq
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
* Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* Copyright (c) 2006-2007, Parvatha Elangovan
- * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
* Copyright (c) 2012, CS Systemes d'Information, France
* All rights reserved.
*
@@ -78,347 +78,359 @@
#include "format_defs.h"
#include "opj_string.h"
-typedef struct dircnt{
- /** Buffer for holding images read from Directory*/
- char *filename_buf;
- /** Pointer to the buffer*/
- char **filename;
-}dircnt_t;
-
-
-typedef struct img_folder{
- /** The directory path of the folder containing input images*/
- char *imgdirpath;
- /** Output format*/
- const char *out_format;
- /** Enable option*/
- char set_imgdir;
- /** Enable Cod Format for output*/
- char set_out_format;
-
-}img_fol_t;
-
-typedef enum opj_prec_mode
-{
- OPJ_PREC_MODE_CLIP,
- OPJ_PREC_MODE_SCALE
+typedef struct dircnt {
+ /** Buffer for holding images read from Directory*/
+ char *filename_buf;
+ /** Pointer to the buffer*/
+ char **filename;
+} dircnt_t;
+
+
+typedef struct img_folder {
+ /** The directory path of the folder containing input images*/
+ char *imgdirpath;
+ /** Output format*/
+ const char *out_format;
+ /** Enable option*/
+ char set_imgdir;
+ /** Enable Cod Format for output*/
+ char set_out_format;
+
+} img_fol_t;
+
+typedef enum opj_prec_mode {
+ OPJ_PREC_MODE_CLIP,
+ OPJ_PREC_MODE_SCALE
} opj_precision_mode;
-typedef struct opj_prec
-{
- OPJ_UINT32 prec;
- opj_precision_mode mode;
-}opj_precision;
-
-typedef struct opj_decompress_params
-{
- /** core library parameters */
- opj_dparameters_t core;
-
- /** input file name */
- char infile[OPJ_PATH_LEN];
- /** output file name */
- char outfile[OPJ_PATH_LEN];
- /** input file format 0: J2K, 1: JP2, 2: JPT */
- int decod_format;
- /** output file format 0: PGX, 1: PxM, 2: BMP */
- int cod_format;
- /** index file name */
- char indexfilename[OPJ_PATH_LEN];
-
- /** Decoding area left boundary */
- OPJ_UINT32 DA_x0;
- /** Decoding area right boundary */
- OPJ_UINT32 DA_x1;
- /** Decoding area up boundary */
- OPJ_UINT32 DA_y0;
- /** Decoding area bottom boundary */
- OPJ_UINT32 DA_y1;
- /** Verbose mode */
- OPJ_BOOL m_verbose;
-
- /** tile number ot the decoded tile*/
- OPJ_UINT32 tile_index;
- /** Nb of tile to decode */
- OPJ_UINT32 nb_tile_to_decode;
-
- opj_precision* precision;
- OPJ_UINT32 nb_precision;
-
- /* force output colorspace to RGB */
- int force_rgb;
- /* upsample components according to their dx/dy values */
- int upsample;
- /* split output components to different files */
- int split_pnm;
+typedef struct opj_prec {
+ OPJ_UINT32 prec;
+ opj_precision_mode mode;
+} opj_precision;
+
+typedef struct opj_decompress_params {
+ /** core library parameters */
+ opj_dparameters_t core;
+
+ /** input file name */
+ char infile[OPJ_PATH_LEN];
+ /** output file name */
+ char outfile[OPJ_PATH_LEN];
+ /** input file format 0: J2K, 1: JP2, 2: JPT */
+ int decod_format;
+ /** output file format 0: PGX, 1: PxM, 2: BMP */
+ int cod_format;
+ /** index file name */
+ char indexfilename[OPJ_PATH_LEN];
+
+ /** Decoding area left boundary */
+ OPJ_UINT32 DA_x0;
+ /** Decoding area right boundary */
+ OPJ_UINT32 DA_x1;
+ /** Decoding area up boundary */
+ OPJ_UINT32 DA_y0;
+ /** Decoding area bottom boundary */
+ OPJ_UINT32 DA_y1;
+ /** Verbose mode */
+ OPJ_BOOL m_verbose;
+
+ /** tile number ot the decoded tile*/
+ OPJ_UINT32 tile_index;
+ /** Nb of tile to decode */
+ OPJ_UINT32 nb_tile_to_decode;
+
+ opj_precision* precision;
+ OPJ_UINT32 nb_precision;
+
+ /* force output colorspace to RGB */
+ int force_rgb;
+ /* upsample components according to their dx/dy values */
+ int upsample;
+ /* split output components to different files */
+ int split_pnm;
/** number of threads */
int num_threads;
-}opj_decompress_parameters;
+} 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);
+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);
+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 opj_image_t* convert_gray_to_rgb(opj_image_t* original);
/* -------------------------------------------------------------------------- */
-static void decode_help_display(void) {
- fprintf(stdout,"\nThis is the opj_decompress utility from the OpenJPEG project.\n"
- "It decompresses JPEG 2000 codestreams to various image formats.\n"
- "It has been compiled against openjp2 library v%s.\n\n",opj_version());
-
- fprintf(stdout,"Parameters:\n"
- "-----------\n"
- "\n"
- " -ImgDir <directory> \n"
- " Image file Directory path \n"
- " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n"
- " REQUIRED only if -ImgDir is used\n"
- " Output format for decompressed images.\n");
- fprintf(stdout," -i <compressed file>\n"
- " REQUIRED only if an Input image directory is not specified\n"
- " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"
- " is identified based on its suffix.\n");
- fprintf(stdout," -o <decompressed file>\n"
- " REQUIRED\n"
- " Currently accepts formats specified above (see OutFor option)\n"
- " Binary data is written to the file (not ascii). If a PGX\n"
- " filename is given, there will be as many output files as there are\n"
- " components: an indice starting from 0 will then be appended to the\n"
- " output filename, just before the \"pgx\" extension. If a PGM filename\n"
- " is given and there are more than one component, only the first component\n"
- " will be written to the file.\n");
- fprintf(stdout," -r <reduce factor>\n"
- " Set the number of highest resolution levels to be discarded. The\n"
- " image resolution is effectively divided by 2 to the power of the\n"
- " number of discarded levels. The reduce factor is limited by the\n"
- " smallest total number of decomposition levels among tiles.\n"
- " -l <number of quality layers to decode>\n"
- " Set the maximum number of quality layers to decode. If there are\n"
- " less quality layers than the specified number, all the quality layers\n"
- " are decoded.\n");
- fprintf(stdout," -x \n"
- " Create an index file *.Idx (-x index_name.Idx) \n"
- " -d <x0,y0,x1,y1>\n"
- " OPTIONAL\n"
- " Decoding area\n"
- " By default all the image is decoded.\n"
- " -t <tile_number>\n"
- " OPTIONAL\n"
- " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n"
- " By default all tiles are decoded.\n");
- fprintf(stdout," -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n"
- " OPTIONAL\n"
- " Force the precision (bit depth) of components.\n");
- fprintf(stdout," There shall be at least 1 value. Theres no limit on the number of values (comma separated, last values ignored if too much values).\n"
- " If there are less values than components, the last value is used for remaining components.\n"
- " If 'C' is specified (default), values are clipped.\n"
- " If 'S' is specified, values are scaled.\n"
- " A 0 value can be specified (meaning original bit depth).\n");
- fprintf(stdout," -force-rgb\n"
- " Force output image colorspace to RGB\n"
- " -upsample\n"
- " Downsampled components will be upsampled to image size\n"
- " -split-pnm\n"
- " Split output components to different files when writing to PNM\n");
- if( opj_has_thread_support() ) {
- fprintf(stdout," -threads <num_threads>\n"
- " Number of threads to use for decoding.\n");
- }
-/* UniPG>> */
+static void decode_help_display(void)
+{
+ fprintf(stdout,
+ "\nThis is the opj_decompress utility from the OpenJPEG project.\n"
+ "It decompresses JPEG 2000 codestreams to various image formats.\n"
+ "It has been compiled against openjp2 library v%s.\n\n", opj_version());
+
+ fprintf(stdout, "Parameters:\n"
+ "-----------\n"
+ "\n"
+ " -ImgDir <directory> \n"
+ " Image file Directory path \n"
+ " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n"
+ " REQUIRED only if -ImgDir is used\n"
+ " Output format for decompressed images.\n");
+ fprintf(stdout, " -i <compressed file>\n"
+ " REQUIRED only if an Input image directory is not specified\n"
+ " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"
+ " is identified based on its suffix.\n");
+ fprintf(stdout, " -o <decompressed file>\n"
+ " REQUIRED\n"
+ " Currently accepts formats specified above (see OutFor option)\n"
+ " Binary data is written to the file (not ascii). If a PGX\n"
+ " filename is given, there will be as many output files as there are\n"
+ " components: an indice starting from 0 will then be appended to the\n"
+ " output filename, just before the \"pgx\" extension. If a PGM filename\n"
+ " is given and there are more than one component, only the first component\n"
+ " will be written to the file.\n");
+ fprintf(stdout, " -r <reduce factor>\n"
+ " Set the number of highest resolution levels to be discarded. The\n"
+ " image resolution is effectively divided by 2 to the power of the\n"
+ " number of discarded levels. The reduce factor is limited by the\n"
+ " smallest total number of decomposition levels among tiles.\n"
+ " -l <number of quality layers to decode>\n"
+ " Set the maximum number of quality layers to decode. If there are\n"
+ " less quality layers than the specified number, all the quality layers\n"
+ " are decoded.\n");
+ fprintf(stdout, " -x \n"
+ " Create an index file *.Idx (-x index_name.Idx) \n"
+ " -d <x0,y0,x1,y1>\n"
+ " OPTIONAL\n"
+ " Decoding area\n"
+ " By default all the image is decoded.\n"
+ " -t <tile_number>\n"
+ " OPTIONAL\n"
+ " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n"
+ " By default all tiles are decoded.\n");
+ fprintf(stdout, " -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n"
+ " OPTIONAL\n"
+ " Force the precision (bit depth) of components.\n");
+ fprintf(stdout,
+ " There shall be at least 1 value. Theres no limit on the number of values (comma separated, last values ignored if too much values).\n"
+ " If there are less values than components, the last value is used for remaining components.\n"
+ " If 'C' is specified (default), values are clipped.\n"
+ " If 'S' is specified, values are scaled.\n"
+ " A 0 value can be specified (meaning original bit depth).\n");
+ fprintf(stdout, " -force-rgb\n"
+ " Force output image colorspace to RGB\n"
+ " -upsample\n"
+ " Downsampled components will be upsampled to image size\n"
+ " -split-pnm\n"
+ " Split output components to different files when writing to PNM\n");
+ if (opj_has_thread_support()) {
+ fprintf(stdout, " -threads <num_threads>\n"
+ " Number of threads to use for decoding.\n");
+ }
+ /* UniPG>> */
#ifdef USE_JPWL
- fprintf(stdout," -W <options>\n"
- " Activates the JPWL correction capability, if the codestream complies.\n"
- " Options can be a comma separated list of <param=val> tokens:\n"
- " c, c=numcomps\n"
- " numcomps is the number of expected components in the codestream\n"
- " (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
+ fprintf(stdout, " -W <options>\n"
+ " Activates the JPWL correction capability, if the codestream complies.\n"
+ " Options can be a comma separated list of <param=val> tokens:\n"
+ " c, c=numcomps\n"
+ " numcomps is the number of expected components in the codestream\n"
+ " (search of first EPB rely upon this, default is %d)\n",
+ JPWL_EXPECTED_COMPONENTS);
#endif /* USE_JPWL */
-/* <<UniPG */
- fprintf(stdout,"\n");
+ /* <<UniPG */
+ fprintf(stdout, "\n");
}
/* -------------------------------------------------------------------------- */
-static OPJ_BOOL parse_precision(const char* option, opj_decompress_parameters* parameters)
+static OPJ_BOOL parse_precision(const char* option,
+ opj_decompress_parameters* parameters)
{
- const char* l_remaining = option;
- OPJ_BOOL l_result = OPJ_TRUE;
-
- /* reset */
- if (parameters->precision) {
- free(parameters->precision);
- parameters->precision = NULL;
- }
- parameters->nb_precision = 0U;
-
- for(;;)
- {
- int prec;
- char mode;
- char comma;
- int count;
-
- count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma);
- if (count == 1) {
- mode = 'C';
- count++;
- }
- if ((count == 2) || (mode==',')) {
- if (mode==',') {
- mode = 'C';
- }
- comma=',';
- count = 3;
- }
- if (count == 3) {
- if ((prec < 1) || (prec > 32)) {
- fprintf(stderr,"Invalid precision %d in precision option %s\n", prec, option);
- l_result = OPJ_FALSE;
- break;
- }
- if ((mode != 'C') && (mode != 'S')) {
- fprintf(stderr,"Invalid precision mode %c in precision option %s\n", mode, option);
- l_result = OPJ_FALSE;
- break;
- }
- if (comma != ',') {
- fprintf(stderr,"Invalid character %c in precision option %s\n", comma, option);
- l_result = OPJ_FALSE;
- break;
- }
-
- if (parameters->precision == NULL) {
- /* first one */
- parameters->precision = (opj_precision *)malloc(sizeof(opj_precision));
- if (parameters->precision == NULL) {
- fprintf(stderr,"Could not allocate memory for precision option\n");
- l_result = OPJ_FALSE;
- break;
- }
- } else {
- OPJ_UINT32 l_new_size = parameters->nb_precision + 1U;
- opj_precision* l_new;
-
- if (l_new_size == 0U) {
- fprintf(stderr,"Could not allocate memory for precision option\n");
- l_result = OPJ_FALSE;
- break;
- }
-
- l_new = (opj_precision *)realloc(parameters->precision, l_new_size * sizeof(opj_precision));
- if (l_new == NULL) {
- fprintf(stderr,"Could not allocate memory for precision option\n");
- l_result = OPJ_FALSE;
- break;
- }
- parameters->precision = l_new;
- }
-
- parameters->precision[parameters->nb_precision].prec = (OPJ_UINT32)prec;
- switch (mode) {
- case 'C':
- parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP;
- break;
- case 'S':
- parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE;
- break;
- default:
- break;
- }
- parameters->nb_precision++;
-
- l_remaining = strchr(l_remaining, ',');
- if (l_remaining == NULL) {
- break;
- }
- l_remaining += 1;
- } else {
- fprintf(stderr,"Could not parse precision option %s\n", option);
- l_result = OPJ_FALSE;
- break;
- }
- }
-
- return l_result;
+ const char* l_remaining = option;
+ OPJ_BOOL l_result = OPJ_TRUE;
+
+ /* reset */
+ if (parameters->precision) {
+ free(parameters->precision);
+ parameters->precision = NULL;
+ }
+ parameters->nb_precision = 0U;
+
+ for (;;) {
+ int prec;
+ char mode;
+ char comma;
+ int count;
+
+ count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma);
+ if (count == 1) {
+ mode = 'C';
+ count++;
+ }
+ if ((count == 2) || (mode == ',')) {
+ if (mode == ',') {
+ mode = 'C';
+ }
+ comma = ',';
+ count = 3;
+ }
+ if (count == 3) {
+ if ((prec < 1) || (prec > 32)) {
+ fprintf(stderr, "Invalid precision %d in precision option %s\n", prec, option);
+ l_result = OPJ_FALSE;
+ break;
+ }
+ if ((mode != 'C') && (mode != 'S')) {
+ fprintf(stderr, "Invalid precision mode %c in precision option %s\n", mode,
+ option);
+ l_result = OPJ_FALSE;
+ break;
+ }
+ if (comma != ',') {
+ fprintf(stderr, "Invalid character %c in precision option %s\n", comma, option);
+ l_result = OPJ_FALSE;
+ break;
+ }
+
+ if (parameters->precision == NULL) {
+ /* first one */
+ parameters->precision = (opj_precision *)malloc(sizeof(opj_precision));
+ if (parameters->precision == NULL) {
+ fprintf(stderr, "Could not allocate memory for precision option\n");
+ l_result = OPJ_FALSE;
+ break;
+ }
+ } else {
+ OPJ_UINT32 l_new_size = parameters->nb_precision + 1U;
+ opj_precision* l_new;
+
+ if (l_new_size == 0U) {
+ fprintf(stderr, "Could not allocate memory for precision option\n");
+ l_result = OPJ_FALSE;
+ break;
+ }
+
+ l_new = (opj_precision *)realloc(parameters->precision,
+ l_new_size * sizeof(opj_precision));
+ if (l_new == NULL) {
+ fprintf(stderr, "Could not allocate memory for precision option\n");
+ l_result = OPJ_FALSE;
+ break;
+ }
+ parameters->precision = l_new;
+ }
+
+ parameters->precision[parameters->nb_precision].prec = (OPJ_UINT32)prec;
+ switch (mode) {
+ case 'C':
+ parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP;
+ break;
+ case 'S':
+ parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE;
+ break;
+ default:
+ break;
+ }
+ parameters->nb_precision++;
+
+ l_remaining = strchr(l_remaining, ',');
+ if (l_remaining == NULL) {
+ break;
+ }
+ l_remaining += 1;
+ } else {
+ fprintf(stderr, "Could not parse precision option %s\n", option);
+ l_result = OPJ_FALSE;
+ break;
+ }
+ }
+
+ return l_result;
}
/* -------------------------------------------------------------------------- */
-int get_num_images(char *imgdirpath){
- DIR *dir;
- struct dirent* content;
- int num_images = 0;
-
- /*Reading the input images from given input directory*/
-
- dir= opendir(imgdirpath);
- if(!dir){
- fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
- return 0;
- }
-
- while((content=readdir(dir))!=NULL){
- if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
- continue;
- num_images++;
- }
- closedir(dir);
- return num_images;
+int get_num_images(char *imgdirpath)
+{
+ DIR *dir;
+ struct dirent* content;
+ int num_images = 0;
+
+ /*Reading the input images from given input directory*/
+
+ dir = opendir(imgdirpath);
+ if (!dir) {
+ fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
+ return 0;
+ }
+
+ while ((content = readdir(dir)) != NULL) {
+ if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
+ continue;
+ }
+ num_images++;
+ }
+ closedir(dir);
+ return num_images;
}
/* -------------------------------------------------------------------------- */
-int load_images(dircnt_t *dirptr, char *imgdirpath){
- DIR *dir;
- struct dirent* content;
- int i = 0;
-
- /*Reading the input images from given input directory*/
-
- dir= opendir(imgdirpath);
- if(!dir){
- fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
- return 1;
- }else {
- fprintf(stderr,"Folder opened successfully\n");
- }
-
- while((content=readdir(dir))!=NULL){
- if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
- continue;
-
- strcpy(dirptr->filename[i],content->d_name);
- i++;
- }
- closedir(dir);
- return 0;
+int load_images(dircnt_t *dirptr, char *imgdirpath)
+{
+ DIR *dir;
+ struct dirent* content;
+ int i = 0;
+
+ /*Reading the input images from given input directory*/
+
+ dir = opendir(imgdirpath);
+ if (!dir) {
+ fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
+ return 1;
+ } else {
+ fprintf(stderr, "Folder opened successfully\n");
+ }
+
+ while ((content = readdir(dir)) != NULL) {
+ if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
+ continue;
+ }
+
+ strcpy(dirptr->filename[i], content->d_name);
+ i++;
+ }
+ closedir(dir);
+ return 0;
}
/* -------------------------------------------------------------------------- */
-int get_file_format(const char *filename) {
- unsigned int i;
- static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
- static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
- const char * ext = strrchr(filename, '.');
- if (ext == NULL)
- return -1;
- ext++;
- if(*ext) {
- for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
- if(strcasecmp(ext, extension[i]) == 0) {
- return format[i];
- }
- }
- }
-
- return -1;
+int get_file_format(const char *filename)
+{
+ unsigned int i;
+ static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
+ static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
+ const char * ext = strrchr(filename, '.');
+ if (ext == NULL) {
+ return -1;
+ }
+ ext++;
+ if (*ext) {
+ for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
+ if (strcasecmp(ext, extension[i]) == 0) {
+ return format[i];
+ }
+ }
+ }
+
+ return -1;
}
#ifdef _WIN32
@@ -428,33 +440,41 @@ const char* path_separator = "/";
#endif
/* -------------------------------------------------------------------------- */
-char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters){
- char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
- char *temp_p, temp1[OPJ_PATH_LEN]="";
-
- strcpy(image_filename,dirptr->filename[imageno]);
- fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
- sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator, image_filename);
- parameters->decod_format = infile_format(infilename);
- if (parameters->decod_format == -1)
- return 1;
- if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) {
- return 1;
- }
-
- /*Set output file*/
- strcpy(temp_ofname,strtok(image_filename,"."));
- while((temp_p = strtok(NULL,".")) != NULL){
- strcat(temp_ofname,temp1);
- sprintf(temp1,".%s",temp_p);
- }
- if(img_fol->set_out_format==1){
- sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
- if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) {
- return 1;
- }
- }
- return 0;
+char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
+ opj_decompress_parameters *parameters)
+{
+ char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
+ outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
+ char *temp_p, temp1[OPJ_PATH_LEN] = "";
+
+ strcpy(image_filename, dirptr->filename[imageno]);
+ fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
+ sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator,
+ image_filename);
+ parameters->decod_format = infile_format(infilename);
+ if (parameters->decod_format == -1) {
+ return 1;
+ }
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
+ infilename) != 0) {
+ return 1;
+ }
+
+ /*Set output file*/
+ strcpy(temp_ofname, strtok(image_filename, "."));
+ while ((temp_p = strtok(NULL, ".")) != NULL) {
+ strcat(temp_ofname, temp1);
+ sprintf(temp1, ".%s", temp_p);
+ }
+ if (img_fol->set_out_format == 1) {
+ sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
+ img_fol->out_format);
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
+ outfilename) != 0) {
+ return 1;
+ }
+ }
+ return 0;
}
/* -------------------------------------------------------------------------- */
@@ -465,52 +485,55 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompre
static int infile_format(const char *fname)
{
- FILE *reader;
- const char *s, *magic_s;
- int ext_format, magic_format;
- unsigned char buf[12];
- OPJ_SIZE_T l_nb_read;
+ FILE *reader;
+ const char *s, *magic_s;
+ int ext_format, magic_format;
+ unsigned char buf[12];
+ OPJ_SIZE_T l_nb_read;
- reader = fopen(fname, "rb");
+ reader = fopen(fname, "rb");
- if (reader == NULL)
- return -2;
+ if (reader == NULL) {
+ return -2;
+ }
- memset(buf, 0, 12);
- l_nb_read = fread(buf, 1, 12, reader);
- fclose(reader);
- if (l_nb_read != 12)
- return -1;
+ memset(buf, 0, 12);
+ l_nb_read = fread(buf, 1, 12, reader);
+ fclose(reader);
+ if (l_nb_read != 12) {
+ return -1;
+ }
- ext_format = get_file_format(fname);
+ ext_format = get_file_format(fname);
- if (ext_format == JPT_CFMT)
- return JPT_CFMT;
+ if (ext_format == JPT_CFMT) {
+ return JPT_CFMT;
+ }
- if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
- magic_format = JP2_CFMT;
- magic_s = ".jp2";
- }
- else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
- magic_format = J2K_CFMT;
- magic_s = ".j2k or .jpc or .j2c";
- }
- else
- return -1;
+ if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
+ magic_format = JP2_CFMT;
+ magic_s = ".jp2";
+ } else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
+ magic_format = J2K_CFMT;
+ magic_s = ".j2k or .jpc or .j2c";
+ } else {
+ return -1;
+ }
- if (magic_format == ext_format)
- return ext_format;
+ if (magic_format == ext_format) {
+ return ext_format;
+ }
- s = fname + strlen(fname) - 4;
+ s = fname + strlen(fname) - 4;
- fputs("\n===========================================\n", stderr);
- fprintf(stderr, "The extension of this file is incorrect.\n"
- "FOUND %s. SHOULD BE %s\n", s, magic_s);
- fputs("===========================================\n", stderr);
+ fputs("\n===========================================\n", stderr);
+ fprintf(stderr, "The extension of this file is incorrect.\n"
+ "FOUND %s. SHOULD BE %s\n", s, magic_s);
+ fputs("===========================================\n", stderr);
- return magic_format;
+ return magic_format;
}
/* -------------------------------------------------------------------------- */
@@ -518,354 +541,350 @@ static int infile_format(const char *fname)
* Parse the command line
*/
/* -------------------------------------------------------------------------- */
-int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol) {
- /* parse the command line */
- int totlen, c;
- opj_option_t long_option[]={
- {"ImgDir", REQ_ARG, NULL,'y'},
- {"OutFor", REQ_ARG, NULL,'O'},
- {"force-rgb", NO_ARG, NULL, 1},
- {"upsample", NO_ARG, NULL, 1},
- {"split-pnm", NO_ARG, NULL, 1},
- {"threads", REQ_ARG, NULL, 'T'}
- };
-
- const char optlist[] = "i:o:r:l:x:d:t:p:"
-
-/* UniPG>> */
+int parse_cmdline_decoder(int argc, char **argv,
+ opj_decompress_parameters *parameters, img_fol_t *img_fol)
+{
+ /* parse the command line */
+ int totlen, c;
+ opj_option_t long_option[] = {
+ {"ImgDir", REQ_ARG, NULL, 'y'},
+ {"OutFor", REQ_ARG, NULL, 'O'},
+ {"force-rgb", NO_ARG, NULL, 1},
+ {"upsample", NO_ARG, NULL, 1},
+ {"split-pnm", NO_ARG, NULL, 1},
+ {"threads", REQ_ARG, NULL, 'T'}
+ };
+
+ const char optlist[] = "i:o:r:l:x:d:t:p:"
+
+ /* UniPG>> */
#ifdef USE_JPWL
- "W:"
+ "W:"
#endif /* USE_JPWL */
-/* <<UniPG */
- "h" ;
-
- long_option[2].flag = &(parameters->force_rgb);
- long_option[3].flag = &(parameters->upsample);
- long_option[4].flag = &(parameters->split_pnm);
- totlen=sizeof(long_option);
- opj_reset_options_reading();
- img_fol->set_out_format = 0;
- do {
- c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
- if (c == -1)
- break;
- switch (c) {
- case 0: /* long opt with flag */
- break;
- case 'i': /* input file */
- {
- char *infile = opj_optarg;
- parameters->decod_format = infile_format(infile);
- switch(parameters->decod_format) {
- case J2K_CFMT:
- break;
- case JP2_CFMT:
- break;
- case JPT_CFMT:
- break;
- case -2:
- fprintf(stderr,
- "!! infile cannot be read: %s !!\n\n",
- infile);
- return 1;
- default:
- fprintf(stderr,
- "[ERROR] Unknown input file format: %s \n"
- " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
- infile);
- return 1;
- }
- if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
- fprintf(stderr, "[ERROR] Path is too long\n");
- return 1;
- }
- }
- break;
-
- /* ----------------------------------------------------- */
-
- case 'o': /* output file */
- {
- char *outfile = opj_optarg;
- parameters->cod_format = get_file_format(outfile);
- switch(parameters->cod_format) {
- case PGX_DFMT:
- break;
- case PXM_DFMT:
- break;
- case BMP_DFMT:
- break;
- case TIF_DFMT:
- break;
- case RAW_DFMT:
- break;
- case RAWL_DFMT:
- break;
- case TGA_DFMT:
- break;
- case PNG_DFMT:
- break;
- default:
- fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outfile);
- return 1;
- }
- if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) {
- fprintf(stderr, "[ERROR] Path is too long\n");
- return 1;
- }
- }
- break;
-
- /* ----------------------------------------------------- */
-
- case 'O': /* output format */
- {
- char outformat[50];
- char *of = opj_optarg;
- sprintf(outformat,".%s",of);
- img_fol->set_out_format = 1;
- parameters->cod_format = get_file_format(outformat);
- switch(parameters->cod_format) {
- case PGX_DFMT:
- img_fol->out_format = "pgx";
- break;
- case PXM_DFMT:
- img_fol->out_format = "ppm";
- break;
- case BMP_DFMT:
- img_fol->out_format = "bmp";
- break;
- case TIF_DFMT:
- img_fol->out_format = "tif";
- break;
- case RAW_DFMT:
- img_fol->out_format = "raw";
- break;
- case RAWL_DFMT:
- img_fol->out_format = "rawl";
- break;
- case TGA_DFMT:
- img_fol->out_format = "raw";
- break;
- case PNG_DFMT:
- img_fol->out_format = "png";
- break;
- default:
- fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outformat);
- return 1;
- break;
- }
- }
- break;
-
- /* ----------------------------------------------------- */
-
-
- case 'r': /* reduce option */
- {
- sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce));
- }
- break;
-
- /* ----------------------------------------------------- */
-
-
- case 'l': /* layering option */
- {
- sscanf(opj_optarg, "%u", &(parameters->core.cp_layer));
- }
- break;
-
- /* ----------------------------------------------------- */
-
- case 'h': /* display an help description */
- decode_help_display();
- return 1;
-
+ /* <<UniPG */
+ "h" ;
+
+ long_option[2].flag = &(parameters->force_rgb);
+ long_option[3].flag = &(parameters->upsample);
+ long_option[4].flag = &(parameters->split_pnm);
+ totlen = sizeof(long_option);
+ opj_reset_options_reading();
+ img_fol->set_out_format = 0;
+ do {
+ c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
+ if (c == -1) {
+ break;
+ }
+ switch (c) {
+ case 0: /* long opt with flag */
+ break;
+ case 'i': { /* input file */
+ char *infile = opj_optarg;
+ parameters->decod_format = infile_format(infile);
+ switch (parameters->decod_format) {
+ case J2K_CFMT:
+ break;
+ case JP2_CFMT:
+ break;
+ case JPT_CFMT:
+ break;
+ case -2:
+ fprintf(stderr,
+ "!! infile cannot be read: %s !!\n\n",
+ infile);
+ return 1;
+ default:
+ fprintf(stderr,
+ "[ERROR] Unknown input file format: %s \n"
+ " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
+ infile);
+ return 1;
+ }
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+ case 'o': { /* output file */
+ char *outfile = opj_optarg;
+ parameters->cod_format = get_file_format(outfile);
+ switch (parameters->cod_format) {
+ case PGX_DFMT:
+ break;
+ case PXM_DFMT:
+ break;
+ case BMP_DFMT:
+ break;
+ case TIF_DFMT:
+ break;
+ case RAW_DFMT:
+ break;
+ case RAWL_DFMT:
+ break;
+ case TGA_DFMT:
+ break;
+ case PNG_DFMT:
+ break;
+ default:
+ fprintf(stderr,
+ "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n",
+ outfile);
+ return 1;
+ }
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
+ outfile) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+ case 'O': { /* output format */
+ char outformat[50];
+ char *of = opj_optarg;
+ sprintf(outformat, ".%s", of);
+ img_fol->set_out_format = 1;
+ parameters->cod_format = get_file_format(outformat);
+ switch (parameters->cod_format) {
+ case PGX_DFMT:
+ img_fol->out_format = "pgx";
+ break;
+ case PXM_DFMT:
+ img_fol->out_format = "ppm";
+ break;
+ case BMP_DFMT:
+ img_fol->out_format = "bmp";
+ break;
+ case TIF_DFMT:
+ img_fol->out_format = "tif";
+ break;
+ case RAW_DFMT:
+ img_fol->out_format = "raw";
+ break;
+ case RAWL_DFMT:
+ img_fol->out_format = "rawl";
+ break;
+ case TGA_DFMT:
+ img_fol->out_format = "raw";
+ break;
+ case PNG_DFMT:
+ img_fol->out_format = "png";
+ break;
+ default:
+ fprintf(stderr,
+ "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n",
+ outformat);
+ return 1;
+ break;
+ }
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+
+ case 'r': { /* reduce option */
+ sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce));
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+
+ case 'l': { /* layering option */
+ sscanf(opj_optarg, "%u", &(parameters->core.cp_layer));
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+ case 'h': /* display an help description */
+ decode_help_display();
+ return 1;
+
+ /* ----------------------------------------------------- */
+
+ case 'y': { /* Image Directory path */
+ img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+ 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, &parameters->DA_x0, &parameters->DA_y0,
+ &parameters->DA_x1, &parameters->DA_y1);
+
+ free(ROI_values);
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+ case 't': { /* Input tile index */
+ sscanf(opj_optarg, "%u", &parameters->tile_index);
+ parameters->nb_tile_to_decode = 1;
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+ case 'x': { /* Creation of index file */
+ if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename),
+ opj_optarg) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+ case 'p': { /* Force precision */
+ if (!parse_precision(opj_optarg, parameters)) {
+ return 1;
+ }
+ }
+ break;
/* ----------------------------------------------------- */
- case 'y': /* Image Directory path */
- {
- img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
- 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, &parameters->DA_x0, &parameters->DA_y0, &parameters->DA_x1, &parameters->DA_y1);
-
- free(ROI_values);
- }
- break;
-
- /* ----------------------------------------------------- */
-
- case 't': /* Input tile index */
- {
- sscanf(opj_optarg, "%u", &parameters->tile_index);
- parameters->nb_tile_to_decode = 1;
- }
- break;
-
- /* ----------------------------------------------------- */
-
- case 'x': /* Creation of index file */
- {
- if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename), opj_optarg) != 0) {
- fprintf(stderr, "[ERROR] Path is too long\n");
- return 1;
- }
- }
- break;
-
- /* ----------------------------------------------------- */
- case 'p': /* Force precision */
- {
- if (!parse_precision(opj_optarg, parameters))
- {
- return 1;
- }
- }
- break;
- /* ----------------------------------------------------- */
-
- /* UniPG>> */
+ /* UniPG>> */
#ifdef USE_JPWL
-
- case 'W': /* activate JPWL correction */
- {
- char *token = NULL;
-
- token = strtok(opj_optarg, ",");
- while(token != NULL) {
-
- /* search expected number of components */
- if (*token == 'c') {
-
- static int compno;
-
- compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
-
- if(sscanf(token, "c=%d", &compno) == 1) {
- /* Specified */
- if ((compno < 1) || (compno > 256)) {
- fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
- return 1;
- }
- parameters->jpwl_exp_comps = compno;
-
- } else if (!strcmp(token, "c")) {
- /* default */
- parameters->jpwl_exp_comps = compno; /* auto for default size */
-
- } else {
- fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
- return 1;
- };
- }
-
- /* search maximum number of tiles */
- if (*token == 't') {
-
- static int tileno;
-
- tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
-
- if(sscanf(token, "t=%d", &tileno) == 1) {
- /* Specified */
- if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
- fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
- return 1;
- }
- parameters->jpwl_max_tiles = tileno;
-
- } else if (!strcmp(token, "t")) {
- /* default */
- parameters->jpwl_max_tiles = tileno; /* auto for default size */
-
- } else {
- fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
- return 1;
- };
- }
-
- /* next token or bust */
- token = strtok(NULL, ",");
- };
- parameters->jpwl_correct = OPJ_TRUE;
- fprintf(stdout, "JPWL correction capability activated\n");
- fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
- }
- break;
+
+ case 'W': { /* activate JPWL correction */
+ char *token = NULL;
+
+ token = strtok(opj_optarg, ",");
+ while (token != NULL) {
+
+ /* search expected number of components */
+ if (*token == 'c') {
+
+ static int compno;
+
+ compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
+
+ if (sscanf(token, "c=%d", &compno) == 1) {
+ /* Specified */
+ if ((compno < 1) || (compno > 256)) {
+ fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
+ return 1;
+ }
+ parameters->jpwl_exp_comps = compno;
+
+ } else if (!strcmp(token, "c")) {
+ /* default */
+ parameters->jpwl_exp_comps = compno; /* auto for default size */
+
+ } else {
+ fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
+ return 1;
+ };
+ }
+
+ /* search maximum number of tiles */
+ if (*token == 't') {
+
+ static int tileno;
+
+ tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
+
+ if (sscanf(token, "t=%d", &tileno) == 1) {
+ /* Specified */
+ if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
+ fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
+ return 1;
+ }
+ parameters->jpwl_max_tiles = tileno;
+
+ } else if (!strcmp(token, "t")) {
+ /* default */
+ parameters->jpwl_max_tiles = tileno; /* auto for default size */
+
+ } else {
+ fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
+ return 1;
+ };
+ }
+
+ /* next token or bust */
+ token = strtok(NULL, ",");
+ };
+ parameters->jpwl_correct = OPJ_TRUE;
+ fprintf(stdout, "JPWL correction capability activated\n");
+ fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
+ }
+ break;
#endif /* USE_JPWL */
-/* <<UniPG */
-
- /* ----------------------------------------------------- */
- case 'T': /* Number of threads */
- {
- if( strcmp(opj_optarg, "ALL_CPUS") == 0 )
- {
- parameters->num_threads = opj_get_num_cpus();
- if( parameters->num_threads == 1 )
- parameters->num_threads = 0;
- }
- else
- {
- sscanf(opj_optarg, "%d", &parameters->num_threads);
- }
- }
- break;
-
- /* ----------------------------------------------------- */
-
+ /* <<UniPG */
+
+ /* ----------------------------------------------------- */
+ case 'T': { /* Number of threads */
+ if (strcmp(opj_optarg, "ALL_CPUS") == 0) {
+ parameters->num_threads = opj_get_num_cpus();
+ if (parameters->num_threads == 1) {
+ parameters->num_threads = 0;
+ }
+ } else {
+ sscanf(opj_optarg, "%d", &parameters->num_threads);
+ }
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
default:
fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
break;
- }
- }while(c != -1);
+ }
+ } while (c != -1);
- /* check for possible errors */
- if(img_fol->set_imgdir==1){
- if(!(parameters->infile[0]==0)){
+ /* check for possible errors */
+ if (img_fol->set_imgdir == 1) {
+ if (!(parameters->infile[0] == 0)) {
fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
- return 1;
- }
- if(img_fol->set_out_format == 0){
- fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
+ return 1;
+ }
+ if (img_fol->set_out_format == 0) {
+ fprintf(stderr,
+ "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
fprintf(stderr, "Only one format allowed.\n"
- "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
- return 1;
- }
- if(!((parameters->outfile[0] == 0))){
+ "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
+ return 1;
+ }
+ if (!((parameters->outfile[0] == 0))) {
fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n");
- return 1;
- }
- }else{
- if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
+ return 1;
+ }
+ } else {
+ if ((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
fprintf(stderr, "[ERROR] Required parameters are missing\n"
- "Example: %s -i image.j2k -o image.pgm\n",argv[0]);
- fprintf(stderr, " Help: %s -h\n",argv[0]);
- return 1;
- }
- }
+ "Example: %s -i image.j2k -o image.pgm\n", argv[0]);
+ fprintf(stderr, " Help: %s -h\n", argv[0]);
+ return 1;
+ }
+ }
- return 0;
+ return 0;
}
/* -------------------------------------------------------------------------- */
@@ -874,56 +893,60 @@ int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *para
* separator = ","
*/
/* -------------------------------------------------------------------------- */
-int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
+int parse_DA_values(char* inArg, unsigned int *DA_x0, unsigned int *DA_y0,
+ unsigned int *DA_x1, unsigned int *DA_y1)
{
- int it = 0;
- int values[4];
- char delims[] = ",";
- char *result = NULL;
- result = strtok( inArg, delims );
-
- while( (result != NULL) && (it < 4 ) ) {
- values[it] = atoi(result);
- result = strtok( NULL, delims );
- it++;
- }
-
- if (it != 4) {
- return EXIT_FAILURE;
- }
- else{
- *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1];
- *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3];
- return EXIT_SUCCESS;
- }
+ int it = 0;
+ int values[4];
+ char delims[] = ",";
+ char *result = NULL;
+ result = strtok(inArg, delims);
+
+ while ((result != NULL) && (it < 4)) {
+ values[it] = atoi(result);
+ result = strtok(NULL, delims);
+ it++;
+ }
+
+ if (it != 4) {
+ return EXIT_FAILURE;
+ } else {
+ *DA_x0 = (OPJ_UINT32)values[0];
+ *DA_y0 = (OPJ_UINT32)values[1];
+ *DA_x1 = (OPJ_UINT32)values[2];
+ *DA_y1 = (OPJ_UINT32)values[3];
+ return EXIT_SUCCESS;
+ }
}
-OPJ_FLOAT64 opj_clock(void) {
+OPJ_FLOAT64 opj_clock(void)
+{
#ifdef _WIN32
- /* _WIN32: use QueryPerformance (very accurate) */
- LARGE_INTEGER freq , t ;
+ /* _WIN32: use QueryPerformance (very accurate) */
+ LARGE_INTEGER freq, t ;
/* freq is the clock speed of the CPU */
QueryPerformanceFrequency(&freq) ;
- /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
+ /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
/* t is the high resolution performance counter (see MSDN) */
- QueryPerformanceCounter ( & t ) ;
- return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) : 0;
+ QueryPerformanceCounter(& t) ;
+ return freq.QuadPart ? (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 );
+ 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 ) ;
+ /* 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
}
@@ -932,275 +955,310 @@ OPJ_FLOAT64 opj_clock(void) {
/**
sample error callback expecting a FILE* client object
*/
-static void error_callback(const char *msg, void *client_data) {
- (void)client_data;
- fprintf(stdout, "[ERROR] %s", msg);
+static void error_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+ fprintf(stdout, "[ERROR] %s", msg);
}
/**
sample warning callback expecting a FILE* client object
*/
-static void warning_callback(const char *msg, void *client_data) {
- (void)client_data;
- fprintf(stdout, "[WARNING] %s", msg);
+static void warning_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+ fprintf(stdout, "[WARNING] %s", msg);
}
/**
sample debug callback expecting no client object
*/
-static void info_callback(const char *msg, void *client_data) {
- (void)client_data;
- fprintf(stdout, "[INFO] %s", msg);
+static void info_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+ fprintf(stdout, "[INFO] %s", msg);
}
static void set_default_parameters(opj_decompress_parameters* parameters)
{
- if (parameters) {
- memset(parameters, 0, sizeof(opj_decompress_parameters));
-
- /* default decoding parameters (command line specific) */
- parameters->decod_format = -1;
- parameters->cod_format = -1;
-
- /* default decoding parameters (core) */
- opj_set_default_decoder_parameters(&(parameters->core));
- }
+ if (parameters) {
+ memset(parameters, 0, sizeof(opj_decompress_parameters));
+
+ /* default decoding parameters (command line specific) */
+ parameters->decod_format = -1;
+ parameters->cod_format = -1;
+
+ /* default decoding parameters (core) */
+ opj_set_default_decoder_parameters(&(parameters->core));
+ }
}
static void destroy_parameters(opj_decompress_parameters* parameters)
{
- if (parameters) {
- if (parameters->precision) {
- free(parameters->precision);
- parameters->precision = NULL;
- }
- }
+ if (parameters) {
+ if (parameters->precision) {
+ free(parameters->precision);
+ parameters->precision = NULL;
+ }
+ }
}
/* -------------------------------------------------------------------------- */
static opj_image_t* convert_gray_to_rgb(opj_image_t* original)
{
- OPJ_UINT32 compno;
- opj_image_t* l_new_image = NULL;
- opj_image_cmptparm_t* l_new_components = NULL;
-
- l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) * sizeof(opj_image_cmptparm_t));
- if (l_new_components == NULL) {
- fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
- opj_image_destroy(original);
- return NULL;
- }
-
- l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp = original->comps[0].bpp;
- l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx = original->comps[0].dx;
- l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy = original->comps[0].dy;
- l_new_components[0].h = l_new_components[1].h = l_new_components[2].h = original->comps[0].h;
- l_new_components[0].w = l_new_components[1].w = l_new_components[2].w = original->comps[0].w;
- l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec = original->comps[0].prec;
- l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd = original->comps[0].sgnd;
- l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 = original->comps[0].x0;
- l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 = original->comps[0].y0;
-
- for(compno = 1U; compno < original->numcomps; ++compno) {
- l_new_components[compno+2U].bpp = original->comps[compno].bpp;
- l_new_components[compno+2U].dx = original->comps[compno].dx;
- l_new_components[compno+2U].dy = original->comps[compno].dy;
- l_new_components[compno+2U].h = original->comps[compno].h;
- l_new_components[compno+2U].w = original->comps[compno].w;
- l_new_components[compno+2U].prec = original->comps[compno].prec;
- l_new_components[compno+2U].sgnd = original->comps[compno].sgnd;
- l_new_components[compno+2U].x0 = original->comps[compno].x0;
- l_new_components[compno+2U].y0 = original->comps[compno].y0;
- }
-
- l_new_image = opj_image_create(original->numcomps + 2U, l_new_components, OPJ_CLRSPC_SRGB);
- free(l_new_components);
- if (l_new_image == NULL) {
- fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
- opj_image_destroy(original);
- return NULL;
- }
-
- l_new_image->x0 = original->x0;
- l_new_image->x1 = original->x1;
- l_new_image->y0 = original->y0;
- l_new_image->y1 = original->y1;
-
- l_new_image->comps[0].factor = l_new_image->comps[1].factor = l_new_image->comps[2].factor = original->comps[0].factor;
- l_new_image->comps[0].alpha = l_new_image->comps[1].alpha = l_new_image->comps[2].alpha = original->comps[0].alpha;
- l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded = l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded;
-
- memcpy(l_new_image->comps[0].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
- memcpy(l_new_image->comps[1].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
- memcpy(l_new_image->comps[2].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
-
- for(compno = 1U; compno < original->numcomps; ++compno) {
- l_new_image->comps[compno+2U].factor = original->comps[compno].factor;
- l_new_image->comps[compno+2U].alpha = original->comps[compno].alpha;
- l_new_image->comps[compno+2U].resno_decoded = original->comps[compno].resno_decoded;
- memcpy(l_new_image->comps[compno+2U].data, original->comps[compno].data, original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32));
- }
- opj_image_destroy(original);
- return l_new_image;
+ OPJ_UINT32 compno;
+ opj_image_t* l_new_image = NULL;
+ opj_image_cmptparm_t* l_new_components = NULL;
+
+ l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) *
+ sizeof(opj_image_cmptparm_t));
+ if (l_new_components == NULL) {
+ fprintf(stderr,
+ "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
+ opj_image_destroy(original);
+ return NULL;
+ }
+
+ l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp =
+ original->comps[0].bpp;
+ l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx =
+ original->comps[0].dx;
+ l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy =
+ original->comps[0].dy;
+ l_new_components[0].h = l_new_components[1].h = l_new_components[2].h =
+ original->comps[0].h;
+ l_new_components[0].w = l_new_components[1].w = l_new_components[2].w =
+ original->comps[0].w;
+ l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec =
+ original->comps[0].prec;
+ l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd =
+ original->comps[0].sgnd;
+ l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 =
+ original->comps[0].x0;
+ l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 =
+ original->comps[0].y0;
+
+ for (compno = 1U; compno < original->numcomps; ++compno) {
+ l_new_components[compno + 2U].bpp = original->comps[compno].bpp;
+ l_new_components[compno + 2U].dx = original->comps[compno].dx;
+ l_new_components[compno + 2U].dy = original->comps[compno].dy;
+ l_new_components[compno + 2U].h = original->comps[compno].h;
+ l_new_components[compno + 2U].w = original->comps[compno].w;
+ l_new_components[compno + 2U].prec = original->comps[compno].prec;
+ l_new_components[compno + 2U].sgnd = original->comps[compno].sgnd;
+ l_new_components[compno + 2U].x0 = original->comps[compno].x0;
+ l_new_components[compno + 2U].y0 = original->comps[compno].y0;
+ }
+
+ l_new_image = opj_image_create(original->numcomps + 2U, l_new_components,
+ OPJ_CLRSPC_SRGB);
+ free(l_new_components);
+ if (l_new_image == NULL) {
+ fprintf(stderr,
+ "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
+ opj_image_destroy(original);
+ return NULL;
+ }
+
+ l_new_image->x0 = original->x0;
+ l_new_image->x1 = original->x1;
+ l_new_image->y0 = original->y0;
+ l_new_image->y1 = original->y1;
+
+ l_new_image->comps[0].factor = l_new_image->comps[1].factor =
+ l_new_image->comps[2].factor = original->comps[0].factor;
+ l_new_image->comps[0].alpha = l_new_image->comps[1].alpha =
+ l_new_image->comps[2].alpha = original->comps[0].alpha;
+ l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded =
+ l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded;
+
+ memcpy(l_new_image->comps[0].data, original->comps[0].data,
+ original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
+ memcpy(l_new_image->comps[1].data, original->comps[0].data,
+ original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
+ memcpy(l_new_image->comps[2].data, original->comps[0].data,
+ original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
+
+ for (compno = 1U; compno < original->numcomps; ++compno) {
+ l_new_image->comps[compno + 2U].factor = original->comps[compno].factor;
+ l_new_image->comps[compno + 2U].alpha = original->comps[compno].alpha;
+ l_new_image->comps[compno + 2U].resno_decoded =
+ original->comps[compno].resno_decoded;
+ memcpy(l_new_image->comps[compno + 2U].data, original->comps[compno].data,
+ original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32));
+ }
+ opj_image_destroy(original);
+ return l_new_image;
}
/* -------------------------------------------------------------------------- */
static opj_image_t* upsample_image_components(opj_image_t* original)
{
- opj_image_t* l_new_image = NULL;
- opj_image_cmptparm_t* l_new_components = NULL;
- OPJ_BOOL l_upsample_need = OPJ_FALSE;
- OPJ_UINT32 compno;
-
- for (compno = 0U; compno < original->numcomps; ++compno) {
- if (original->comps[compno].factor > 0U) {
- fprintf(stderr, "ERROR -> opj_decompress: -upsample not supported with reduction\n");
- opj_image_destroy(original);
- return NULL;
- }
- if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) {
- l_upsample_need = OPJ_TRUE;
- break;
- }
- }
- if (!l_upsample_need) {
- return original;
- }
- /* Upsample is needed */
- l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(opj_image_cmptparm_t));
- if (l_new_components == NULL) {
- fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
- opj_image_destroy(original);
- return NULL;
- }
-
- for (compno = 0U; compno < original->numcomps; ++compno) {
- opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]);
- opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
-
- l_new_cmp->bpp = l_org_cmp->bpp;
- l_new_cmp->prec = l_org_cmp->prec;
- l_new_cmp->sgnd = l_org_cmp->sgnd;
- l_new_cmp->x0 = original->x0;
- l_new_cmp->y0 = original->y0;
- l_new_cmp->dx = 1;
- l_new_cmp->dy = 1;
- l_new_cmp->w = l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */
- l_new_cmp->h = l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */
-
- if (l_org_cmp->dx > 1U) {
- l_new_cmp->w = original->x1 - original->x0;
- }
-
- if (l_org_cmp->dy > 1U) {
- l_new_cmp->h = original->y1 - original->y0;
- }
- }
-
- l_new_image = opj_image_create(original->numcomps, l_new_components, original->color_space);
- free(l_new_components);
- if (l_new_image == NULL) {
- fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
- opj_image_destroy(original);
- return NULL;
- }
-
- l_new_image->x0 = original->x0;
- l_new_image->x1 = original->x1;
- l_new_image->y0 = original->y0;
- l_new_image->y1 = original->y1;
-
- for (compno = 0U; compno < original->numcomps; ++compno) {
- opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]);
- opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
-
- l_new_cmp->factor = l_org_cmp->factor;
- l_new_cmp->alpha = l_org_cmp->alpha;
- l_new_cmp->resno_decoded = l_org_cmp->resno_decoded;
-
- if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) {
- const OPJ_INT32* l_src = l_org_cmp->data;
- OPJ_INT32* l_dst = l_new_cmp->data;
- OPJ_UINT32 y;
- OPJ_UINT32 xoff, yoff;
-
- /* need to take into account dx & dy */
- xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0;
- yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0;
- if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) {
- fprintf(stderr, "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n");
- opj_image_destroy(original);
- opj_image_destroy(l_new_image);
- return NULL;
- }
-
- for (y = 0U; y < yoff; ++y) {
- memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32));
- l_dst += l_new_cmp->w;
- }
-
- if(l_new_cmp->h > (l_org_cmp->dy - 1U)) { /* check subtraction overflow for really small images */
- for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) {
- OPJ_UINT32 x, dy;
- OPJ_UINT32 xorg;
-
- xorg = 0U;
- for (x = 0U; x < xoff; ++x) {
- l_dst[x] = 0;
- }
- if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */
- for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
- OPJ_UINT32 dx;
- for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
- l_dst[x + dx] = l_src[xorg];
- }
- }
- }
- for (; x < l_new_cmp->w; ++x) {
- l_dst[x] = l_src[xorg];
- }
- l_dst += l_new_cmp->w;
-
- for (dy = 1U; dy < l_org_cmp->dy; ++dy) {
- memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
- l_dst += l_new_cmp->w;
- }
- l_src += l_org_cmp->w;
- }
- }
- if (y < l_new_cmp->h) {
- OPJ_UINT32 x;
- OPJ_UINT32 xorg;
-
- xorg = 0U;
- for (x = 0U; x < xoff; ++x) {
- l_dst[x] = 0;
- }
- if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */
- for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
- OPJ_UINT32 dx;
- for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
- l_dst[x + dx] = l_src[xorg];
- }
- }
- }
- for (; x < l_new_cmp->w; ++x) {
- l_dst[x] = l_src[xorg];
- }
- l_dst += l_new_cmp->w;
- ++y;
- for (; y < l_new_cmp->h; ++y) {
- memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
- l_dst += l_new_cmp->w;
- }
- }
- }
- else {
- memcpy(l_new_cmp->data, l_org_cmp->data, l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32));
- }
- }
- opj_image_destroy(original);
- return l_new_image;
+ opj_image_t* l_new_image = NULL;
+ opj_image_cmptparm_t* l_new_components = NULL;
+ OPJ_BOOL l_upsample_need = OPJ_FALSE;
+ OPJ_UINT32 compno;
+
+ for (compno = 0U; compno < original->numcomps; ++compno) {
+ if (original->comps[compno].factor > 0U) {
+ fprintf(stderr,
+ "ERROR -> opj_decompress: -upsample not supported with reduction\n");
+ opj_image_destroy(original);
+ return NULL;
+ }
+ if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) {
+ l_upsample_need = OPJ_TRUE;
+ break;
+ }
+ }
+ if (!l_upsample_need) {
+ return original;
+ }
+ /* Upsample is needed */
+ l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(
+ opj_image_cmptparm_t));
+ if (l_new_components == NULL) {
+ fprintf(stderr,
+ "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
+ opj_image_destroy(original);
+ return NULL;
+ }
+
+ for (compno = 0U; compno < original->numcomps; ++compno) {
+ opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]);
+ opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
+
+ l_new_cmp->bpp = l_org_cmp->bpp;
+ l_new_cmp->prec = l_org_cmp->prec;
+ l_new_cmp->sgnd = l_org_cmp->sgnd;
+ l_new_cmp->x0 = original->x0;
+ l_new_cmp->y0 = original->y0;
+ l_new_cmp->dx = 1;
+ l_new_cmp->dy = 1;
+ l_new_cmp->w =
+ l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */
+ l_new_cmp->h =
+ l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */
+
+ if (l_org_cmp->dx > 1U) {
+ l_new_cmp->w = original->x1 - original->x0;
+ }
+
+ if (l_org_cmp->dy > 1U) {
+ l_new_cmp->h = original->y1 - original->y0;
+ }
+ }
+
+ l_new_image = opj_image_create(original->numcomps, l_new_components,
+ original->color_space);
+ free(l_new_components);
+ if (l_new_image == NULL) {
+ fprintf(stderr,
+ "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
+ opj_image_destroy(original);
+ return NULL;
+ }
+
+ l_new_image->x0 = original->x0;
+ l_new_image->x1 = original->x1;
+ l_new_image->y0 = original->y0;
+ l_new_image->y1 = original->y1;
+
+ for (compno = 0U; compno < original->numcomps; ++compno) {
+ opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]);
+ opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
+
+ l_new_cmp->factor = l_org_cmp->factor;
+ l_new_cmp->alpha = l_org_cmp->alpha;
+ l_new_cmp->resno_decoded = l_org_cmp->resno_decoded;
+
+ if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) {
+ const OPJ_INT32* l_src = l_org_cmp->data;
+ OPJ_INT32* l_dst = l_new_cmp->data;
+ OPJ_UINT32 y;
+ OPJ_UINT32 xoff, yoff;
+
+ /* need to take into account dx & dy */
+ xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0;
+ yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0;
+ if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) {
+ fprintf(stderr,
+ "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n");
+ opj_image_destroy(original);
+ opj_image_destroy(l_new_image);
+ return NULL;
+ }
+
+ for (y = 0U; y < yoff; ++y) {
+ memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32));
+ l_dst += l_new_cmp->w;
+ }
+
+ if (l_new_cmp->h > (l_org_cmp->dy -
+ 1U)) { /* check subtraction overflow for really small images */
+ for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) {
+ OPJ_UINT32 x, dy;
+ OPJ_UINT32 xorg;
+
+ xorg = 0U;
+ for (x = 0U; x < xoff; ++x) {
+ l_dst[x] = 0;
+ }
+ if (l_new_cmp->w > (l_org_cmp->dx -
+ 1U)) { /* check subtraction overflow for really small images */
+ for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
+ OPJ_UINT32 dx;
+ for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
+ l_dst[x + dx] = l_src[xorg];
+ }
+ }
+ }
+ for (; x < l_new_cmp->w; ++x) {
+ l_dst[x] = l_src[xorg];
+ }
+ l_dst += l_new_cmp->w;
+
+ for (dy = 1U; dy < l_org_cmp->dy; ++dy) {
+ memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
+ l_dst += l_new_cmp->w;
+ }
+ l_src += l_org_cmp->w;
+ }
+ }
+ if (y < l_new_cmp->h) {
+ OPJ_UINT32 x;
+ OPJ_UINT32 xorg;
+
+ xorg = 0U;
+ for (x = 0U; x < xoff; ++x) {
+ l_dst[x] = 0;
+ }
+ if (l_new_cmp->w > (l_org_cmp->dx -
+ 1U)) { /* check subtraction overflow for really small images */
+ for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
+ OPJ_UINT32 dx;
+ for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
+ l_dst[x + dx] = l_src[xorg];
+ }
+ }
+ }
+ for (; x < l_new_cmp->w; ++x) {
+ l_dst[x] = l_src[xorg];
+ }
+ l_dst += l_new_cmp->w;
+ ++y;
+ for (; y < l_new_cmp->h; ++y) {
+ memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
+ l_dst += l_new_cmp->w;
+ }
+ }
+ } else {
+ memcpy(l_new_cmp->data, l_org_cmp->data,
+ l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32));
+ }
+ }
+ opj_image_destroy(original);
+ return l_new_image;
}
/* -------------------------------------------------------------------------- */
@@ -1210,406 +1268,427 @@ static opj_image_t* upsample_image_components(opj_image_t* original)
/* -------------------------------------------------------------------------- */
int main(int argc, char **argv)
{
- opj_decompress_parameters parameters; /* decompression parameters */
- opj_image_t* image = NULL;
- opj_stream_t *l_stream = NULL; /* Stream */
- opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
- opj_codestream_index_t* cstr_index = NULL;
-
- OPJ_INT32 num_images, imageno;
- img_fol_t img_fol;
- dircnt_t *dirptr = NULL;
- int failed = 0;
- OPJ_FLOAT64 t, tCumulative = 0;
- OPJ_UINT32 numDecompressedImages = 0;
-
- /* set decoding parameters to default values */
- set_default_parameters(&parameters);
-
- /* Initialize img_fol */
- memset(&img_fol,0,sizeof(img_fol_t));
-
- /* parse input and get user encoding parameters */
- if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
- failed = 1; goto fin;
- }
-
- /* 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(&parameters);
- return EXIT_FAILURE;
- }
- dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
- 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(stdout,"Folder is empty\n");
- failed = 1; goto fin;
- }
- }else{
- num_images=1;
- }
-
- /*Decoding image one by one*/
- for(imageno = 0; imageno < num_images ; imageno++) {
-
- fprintf(stderr,"\n");
-
- if(img_fol.set_imgdir==1){
- if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
- fprintf(stderr,"skipping file...\n");
- destroy_parameters(&parameters);
- continue;
- }
- }
-
- /* read the input file and put it in memory */
- /* ---------------------------------------- */
-
- l_stream = opj_stream_create_default_file_stream(parameters.infile,1);
- if (!l_stream){
- fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
- 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(&parameters);
- opj_stream_destroy(l_stream);
- continue;
- }
-
- /* catch events using our callbacks and give a local context */
- opj_set_info_handler(l_codec, info_callback,00);
- opj_set_warning_handler(l_codec, warning_callback,00);
- opj_set_error_handler(l_codec, error_callback,00);
-
- t = opj_clock();
-
- /* Setup the decoder decoding parameters using user parameters */
- if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){
- fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
- opj_stream_destroy(l_stream);
- opj_destroy_codec(l_codec);
- 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);
- 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);
- failed = 1; goto fin;
- }
-
- if (!parameters.nb_tile_to_decode) {
- /* Optional if you want decode the entire image */
- if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
- (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){
- fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
- opj_stream_destroy(l_stream);
- opj_destroy_codec(l_codec);
- opj_image_destroy(image);
- 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);
- failed = 1; goto fin;
- }
- }
- else {
-
- /* It is just here to illustrate how to use the resolution after set parameters */
- /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
- fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
- opj_destroy_codec(l_codec);
- opj_stream_destroy(l_stream);
- opj_image_destroy(image);
- failed = 1; goto fin;
- }*/
-
- 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);
- failed = 1; goto fin;
- }
- fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
- }
-
- tCumulative += opj_clock() - t;
- numDecompressedImages++;
-
- /* Close the byte stream */
- opj_stream_destroy(l_stream);
-
- if( image->color_space != OPJ_CLRSPC_SYCC
- && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
- && image->comps[1].dx != 1 )
- image->color_space = OPJ_CLRSPC_SYCC;
- else if (image->numcomps <= 2)
- image->color_space = OPJ_CLRSPC_GRAY;
-
- if(image->color_space == OPJ_CLRSPC_SYCC){
- color_sycc_to_rgb(image);
- }
- else if((image->color_space == OPJ_CLRSPC_CMYK) && (parameters.cod_format != TIF_DFMT)){
- color_cmyk_to_rgb(image);
- }
- else if(image->color_space == OPJ_CLRSPC_EYCC){
- color_esycc_to_rgb(image);
- }
-
- if(image->icc_profile_buf) {
+ opj_decompress_parameters parameters; /* decompression parameters */
+ opj_image_t* image = NULL;
+ opj_stream_t *l_stream = NULL; /* Stream */
+ opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
+ opj_codestream_index_t* cstr_index = NULL;
+
+ OPJ_INT32 num_images, imageno;
+ img_fol_t img_fol;
+ dircnt_t *dirptr = NULL;
+ int failed = 0;
+ OPJ_FLOAT64 t, tCumulative = 0;
+ OPJ_UINT32 numDecompressedImages = 0;
+
+ /* set decoding parameters to default values */
+ set_default_parameters(&parameters);
+
+ /* Initialize img_fol */
+ memset(&img_fol, 0, sizeof(img_fol_t));
+
+ /* parse input and get user encoding parameters */
+ if (parse_cmdline_decoder(argc, argv, &parameters, &img_fol) == 1) {
+ failed = 1;
+ goto fin;
+ }
+
+ /* 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(&parameters);
+ return EXIT_FAILURE;
+ }
+ dirptr->filename_buf = (char*)malloc((size_t)num_images * OPJ_PATH_LEN * sizeof(
+ char)); /* Stores at max 10 image file names*/
+ 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(stdout, "Folder is empty\n");
+ failed = 1;
+ goto fin;
+ }
+ } else {
+ num_images = 1;
+ }
+
+ /*Decoding image one by one*/
+ for (imageno = 0; imageno < num_images ; imageno++) {
+
+ fprintf(stderr, "\n");
+
+ if (img_fol.set_imgdir == 1) {
+ if (get_next_file(imageno, dirptr, &img_fol, &parameters)) {
+ fprintf(stderr, "skipping file...\n");
+ destroy_parameters(&parameters);
+ continue;
+ }
+ }
+
+ /* read the input file and put it in memory */
+ /* ---------------------------------------- */
+
+ l_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
+ if (!l_stream) {
+ fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",
+ parameters.infile);
+ 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(&parameters);
+ opj_stream_destroy(l_stream);
+ continue;
+ }
+
+ /* catch events using our callbacks and give a local context */
+ opj_set_info_handler(l_codec, info_callback, 00);
+ opj_set_warning_handler(l_codec, warning_callback, 00);
+ opj_set_error_handler(l_codec, error_callback, 00);
+
+ t = opj_clock();
+
+ /* Setup the decoder decoding parameters using user parameters */
+ if (!opj_setup_decoder(l_codec, &(parameters.core))) {
+ fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ 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);
+ 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);
+ failed = 1;
+ goto fin;
+ }
+
+ if (!parameters.nb_tile_to_decode) {
+ /* Optional if you want decode the entire image */
+ if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
+ (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1,
+ (OPJ_INT32)parameters.DA_y1)) {
+ fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(image);
+ 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);
+ failed = 1;
+ goto fin;
+ }
+ } else {
+
+ /* It is just here to illustrate how to use the resolution after set parameters */
+ /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
+ fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
+ opj_destroy_codec(l_codec);
+ opj_stream_destroy(l_stream);
+ opj_image_destroy(image);
+ failed = 1; goto fin;
+ }*/
+
+ 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);
+ failed = 1;
+ goto fin;
+ }
+ fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
+ }
+
+ tCumulative += opj_clock() - t;
+ numDecompressedImages++;
+
+ /* Close the byte stream */
+ opj_stream_destroy(l_stream);
+
+ if (image->color_space != OPJ_CLRSPC_SYCC
+ && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
+ && image->comps[1].dx != 1) {
+ image->color_space = OPJ_CLRSPC_SYCC;
+ } else if (image->numcomps <= 2) {
+ image->color_space = OPJ_CLRSPC_GRAY;
+ }
+
+ if (image->color_space == OPJ_CLRSPC_SYCC) {
+ color_sycc_to_rgb(image);
+ } else if ((image->color_space == OPJ_CLRSPC_CMYK) &&
+ (parameters.cod_format != TIF_DFMT)) {
+ color_cmyk_to_rgb(image);
+ } else if (image->color_space == OPJ_CLRSPC_EYCC) {
+ color_esycc_to_rgb(image);
+ }
+
+ if (image->icc_profile_buf) {
#if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
- if(image->icc_profile_len)
- color_apply_icc_profile(image);
- else
- color_cielab_to_rgb(image);
+ if (image->icc_profile_len) {
+ color_apply_icc_profile(image);
+ } else {
+ color_cielab_to_rgb(image);
+ }
#endif
- free(image->icc_profile_buf);
- image->icc_profile_buf = NULL; image->icc_profile_len = 0;
- }
-
- /* Force output precision */
- /* ---------------------- */
- if (parameters.precision != NULL)
- {
- OPJ_UINT32 compno;
- for (compno = 0; compno < image->numcomps; ++compno)
- {
- OPJ_UINT32 precno = compno;
- OPJ_UINT32 prec;
-
- if (precno >= parameters.nb_precision) {
- precno = parameters.nb_precision - 1U;
- }
-
- prec = parameters.precision[precno].prec;
- if (prec == 0) {
- prec = image->comps[compno].prec;
- }
-
- switch (parameters.precision[precno].mode) {
- case OPJ_PREC_MODE_CLIP:
- clip_component(&(image->comps[compno]), prec);
- break;
- case OPJ_PREC_MODE_SCALE:
- scale_component(&(image->comps[compno]), prec);
- break;
- default:
- break;
- }
-
- }
- }
-
- /* Upsample components */
- /* ------------------- */
- if (parameters.upsample)
- {
- image = upsample_image_components(image);
- if (image == NULL) {
- fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n");
- opj_destroy_codec(l_codec);
- failed = 1; goto fin;
- }
- }
-
- /* Force RGB output */
- /* ---------------- */
- if (parameters.force_rgb)
- {
- switch (image->color_space) {
- case OPJ_CLRSPC_SRGB:
- break;
- case OPJ_CLRSPC_GRAY:
- image = convert_gray_to_rgb(image);
- break;
- default:
- fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
- opj_image_destroy(image);
- image = NULL;
- break;
- }
- if (image == NULL) {
- fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
- opj_destroy_codec(l_codec);
- failed = 1; goto fin;
- }
- }
-
- /* create output image */
- /* ------------------- */
- switch (parameters.cod_format) {
- case PXM_DFMT: /* PNM PGM PPM */
- if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) {
- fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
-
- case PGX_DFMT: /* PGX */
- if(imagetopgx(image, parameters.outfile)){
- fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
-
- case BMP_DFMT: /* BMP */
- if(imagetobmp(image, parameters.outfile)){
- fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
+ 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;
+ goto fin;
+ }
+ }
+
+ /* Force RGB output */
+ /* ---------------- */
+ if (parameters.force_rgb) {
+ switch (image->color_space) {
+ case OPJ_CLRSPC_SRGB:
+ break;
+ case OPJ_CLRSPC_GRAY:
+ image = convert_gray_to_rgb(image);
+ break;
+ default:
+ fprintf(stderr,
+ "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
+ opj_image_destroy(image);
+ image = NULL;
+ break;
+ }
+ if (image == NULL) {
+ fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
+ opj_destroy_codec(l_codec);
+ failed = 1;
+ goto fin;
+ }
+ }
+
+ /* create output image */
+ /* ------------------- */
+ switch (parameters.cod_format) {
+ case PXM_DFMT: /* PNM PGM PPM */
+ if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) {
+ fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
+
+ case PGX_DFMT: /* PGX */
+ if (imagetopgx(image, parameters.outfile)) {
+ fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
+
+ case BMP_DFMT: /* BMP */
+ if (imagetobmp(image, parameters.outfile)) {
+ fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
#ifdef OPJ_HAVE_LIBTIFF
- case TIF_DFMT: /* TIFF */
- if(imagetotif(image, parameters.outfile)){
- fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
+ case TIF_DFMT: /* TIFF */
+ if (imagetotif(image, parameters.outfile)) {
+ fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
#endif /* OPJ_HAVE_LIBTIFF */
- case RAW_DFMT: /* RAW */
- if(imagetoraw(image, parameters.outfile)){
- fprintf(stderr,"[ERROR] Error generating raw file. Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
-
- case RAWL_DFMT: /* RAWL */
- if(imagetorawl(image, parameters.outfile)){
- fprintf(stderr,"[ERROR] Error generating rawl file. Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
-
- case TGA_DFMT: /* TGA */
- if(imagetotga(image, parameters.outfile)){
- fprintf(stderr,"[ERROR] Error generating tga file. Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
+ case RAW_DFMT: /* RAW */
+ if (imagetoraw(image, parameters.outfile)) {
+ fprintf(stderr, "[ERROR] Error generating raw file. Outfile %s not generated\n",
+ parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
+
+ case RAWL_DFMT: /* RAWL */
+ if (imagetorawl(image, parameters.outfile)) {
+ fprintf(stderr,
+ "[ERROR] Error generating rawl file. Outfile %s not generated\n",
+ parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
+
+ case TGA_DFMT: /* TGA */
+ if (imagetotga(image, parameters.outfile)) {
+ fprintf(stderr, "[ERROR] Error generating tga file. Outfile %s not generated\n",
+ parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
#ifdef OPJ_HAVE_LIBPNG
- case PNG_DFMT: /* PNG */
- if(imagetopng(image, parameters.outfile)){
- fprintf(stderr,"[ERROR] Error generating png file. Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
- else {
- fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
- }
- break;
+ case PNG_DFMT: /* PNG */
+ if (imagetopng(image, parameters.outfile)) {
+ fprintf(stderr, "[ERROR] Error generating png file. Outfile %s not generated\n",
+ parameters.outfile);
+ failed = 1;
+ } else {
+ fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
+ }
+ break;
#endif /* OPJ_HAVE_LIBPNG */
-/* Can happen if output file is TIFF or PNG
- * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
-*/
- default:
- fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
- failed = 1;
- }
+ /* Can happen if output file is TIFF or PNG
+ * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
+ */
+ default:
+ fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
+ failed = 1;
+ }
- /* free remaining structures */
- if (l_codec) {
- opj_destroy_codec(l_codec);
- }
+ /* free remaining structures */
+ if (l_codec) {
+ opj_destroy_codec(l_codec);
+ }
- /* free image data structure */
- opj_image_destroy(image);
+ /* free image data structure */
+ opj_image_destroy(image);
- /* destroy the codestream index */
- opj_destroy_cstr_index(&cstr_index);
+ /* destroy the codestream index */
+ opj_destroy_cstr_index(&cstr_index);
- if(failed) (void)remove(parameters.outfile); /* ignore return value */
- }
+ if (failed) {
+ (void)remove(parameters.outfile); /* ignore return value */
+ }
+ }
fin:
- destroy_parameters(&parameters);
- 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) {
- fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
- }
- return failed ? EXIT_FAILURE : EXIT_SUCCESS;
+ destroy_parameters(&parameters);
+ 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) {
+ fprintf(stdout, "decode time: %d ms\n",
+ (int)((tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
+ }
+ return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
/*end main()*/
diff --git a/src/bin/jp2/opj_dump.c b/src/bin/jp2/opj_dump.c
index d62eea14..c286b02a 100644
--- a/src/bin/jp2/opj_dump.c
+++ b/src/bin/jp2/opj_dump.c
@@ -1,11 +1,11 @@
/*
- * The copyright in this software is being made available under the 2-clauses
- * BSD License, included below. This software may be subject to other third
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
* Copyright (c) 2010, Mathieu Malaterre, GDCM
- * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
* Copyright (c) 2012, CS Systemes d'Information, France
* All rights reserved.
*
@@ -59,161 +59,179 @@
#include "format_defs.h"
#include "opj_string.h"
-typedef struct dircnt{
- /** Buffer for holding images read from Directory*/
- char *filename_buf;
- /** Pointer to the buffer*/
- char **filename;
-}dircnt_t;
+typedef struct dircnt {
+ /** Buffer for holding images read from Directory*/
+ char *filename_buf;
+ /** Pointer to the buffer*/
+ char **filename;
+} dircnt_t;
-typedef struct img_folder{
- /** The directory path of the folder containing input images*/
- char *imgdirpath;
- /** Output format*/
- const char *out_format;
- /** Enable option*/
- char set_imgdir;
- /** Enable Cod Format for output*/
- char set_out_format;
+typedef struct img_folder {
+ /** The directory path of the folder containing input images*/
+ char *imgdirpath;
+ /** Output format*/
+ const char *out_format;
+ /** Enable option*/
+ char set_imgdir;
+ /** Enable Cod Format for output*/
+ char set_out_format;
- int flag;
-}img_fol_t;
+ int flag;
+} img_fol_t;
/* -------------------------------------------------------------------------- */
/* Declarations */
static int get_num_images(char *imgdirpath);
static int load_images(dircnt_t *dirptr, char *imgdirpath);
static int get_file_format(const char *filename);
-static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
+static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
+ opj_dparameters_t *parameters);
static int infile_format(const char *fname);
-static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol);
+static int parse_cmdline_decoder(int argc, char **argv,
+ opj_dparameters_t *parameters, img_fol_t *img_fol);
/* -------------------------------------------------------------------------- */
-static void decode_help_display(void) {
- fprintf(stdout,"\nThis is the opj_dump utility from the OpenJPEG project.\n"
+static void decode_help_display(void)
+{
+ fprintf(stdout, "\nThis is the opj_dump utility from the OpenJPEG project.\n"
"It dumps JPEG 2000 codestream info to stdout or a given file.\n"
- "It has been compiled against openjp2 library v%s.\n\n",opj_version());
-
- fprintf(stdout,"Parameters:\n");
- fprintf(stdout,"-----------\n");
- fprintf(stdout,"\n");
- fprintf(stdout," -ImgDir <directory>\n");
- fprintf(stdout," Image file Directory path \n");
- fprintf(stdout," -i <compressed file>\n");
- fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
- fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
- fprintf(stdout," is identified based on its suffix.\n");
- fprintf(stdout," -o <output file>\n");
- fprintf(stdout," OPTIONAL\n");
- fprintf(stdout," Output file where file info will be dump.\n");
- fprintf(stdout," By default it will be in the stdout.\n");
- fprintf(stdout," -v "); /* FIXME WIP_MSD */
- fprintf(stdout," OPTIONAL\n");
- fprintf(stdout," Enable informative messages\n");
- fprintf(stdout," By default verbose mode is off.\n");
- fprintf(stdout,"\n");
+ "It has been compiled against openjp2 library v%s.\n\n", opj_version());
+
+ fprintf(stdout, "Parameters:\n");
+ fprintf(stdout, "-----------\n");
+ fprintf(stdout, "\n");
+ fprintf(stdout, " -ImgDir <directory>\n");
+ fprintf(stdout, " Image file Directory path \n");
+ fprintf(stdout, " -i <compressed file>\n");
+ fprintf(stdout,
+ " REQUIRED only if an Input image directory not specified\n");
+ fprintf(stdout,
+ " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
+ fprintf(stdout, " is identified based on its suffix.\n");
+ fprintf(stdout, " -o <output file>\n");
+ fprintf(stdout, " OPTIONAL\n");
+ fprintf(stdout, " Output file where file info will be dump.\n");
+ fprintf(stdout, " By default it will be in the stdout.\n");
+ fprintf(stdout, " -v "); /* FIXME WIP_MSD */
+ fprintf(stdout, " OPTIONAL\n");
+ fprintf(stdout, " Enable informative messages\n");
+ fprintf(stdout, " By default verbose mode is off.\n");
+ fprintf(stdout, "\n");
}
/* -------------------------------------------------------------------------- */
-static int get_num_images(char *imgdirpath){
- DIR *dir;
- struct dirent* content;
- int num_images = 0;
-
- /*Reading the input images from given input directory*/
-
- dir= opendir(imgdirpath);
- if(!dir){
- fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
- return 0;
- }
-
- while((content=readdir(dir))!=NULL){
- if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
- continue;
- num_images++;
- }
- closedir(dir);
- return num_images;
+static int get_num_images(char *imgdirpath)
+{
+ DIR *dir;
+ struct dirent* content;
+ int num_images = 0;
+
+ /*Reading the input images from given input directory*/
+
+ dir = opendir(imgdirpath);
+ if (!dir) {
+ fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
+ return 0;
+ }
+
+ while ((content = readdir(dir)) != NULL) {
+ if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
+ continue;
+ }
+ num_images++;
+ }
+ closedir(dir);
+ return num_images;
}
/* -------------------------------------------------------------------------- */
-static int load_images(dircnt_t *dirptr, char *imgdirpath){
- DIR *dir;
- struct dirent* content;
- int i = 0;
-
- /*Reading the input images from given input directory*/
-
- dir= opendir(imgdirpath);
- if(!dir){
- fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
- return 1;
- }else {
- fprintf(stderr,"Folder opened successfully\n");
- }
-
- while((content=readdir(dir))!=NULL){
- if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
- continue;
-
- strcpy(dirptr->filename[i],content->d_name);
- i++;
- }
- closedir(dir);
- return 0;
+static int load_images(dircnt_t *dirptr, char *imgdirpath)
+{
+ DIR *dir;
+ struct dirent* content;
+ int i = 0;
+
+ /*Reading the input images from given input directory*/
+
+ dir = opendir(imgdirpath);
+ if (!dir) {
+ fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
+ return 1;
+ } else {
+ fprintf(stderr, "Folder opened successfully\n");
+ }
+
+ while ((content = readdir(dir)) != NULL) {
+ if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
+ continue;
+ }
+
+ strcpy(dirptr->filename[i], content->d_name);
+ i++;
+ }
+ closedir(dir);
+ return 0;
}
/* -------------------------------------------------------------------------- */
-static int get_file_format(const char *filename) {
- unsigned int i;
- static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
- static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
- const char *ext = strrchr(filename, '.');
- if (ext == NULL)
- return -1;
- ext++;
- if(ext) {
- for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
- if(_strnicmp(ext, extension[i], 3) == 0) {
- return format[i];
- }
- }
- }
-
- return -1;
+static int get_file_format(const char *filename)
+{
+ unsigned int i;
+ static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
+ static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
+ const char *ext = strrchr(filename, '.');
+ if (ext == NULL) {
+ return -1;
+ }
+ ext++;
+ if (ext) {
+ for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
+ if (_strnicmp(ext, extension[i], 3) == 0) {
+ return format[i];
+ }
+ }
+ }
+
+ return -1;
}
/* -------------------------------------------------------------------------- */
-static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
- char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
- char *temp_p, temp1[OPJ_PATH_LEN]="";
-
- strcpy(image_filename,dirptr->filename[imageno]);
- fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
- parameters->decod_format = get_file_format(image_filename);
- if (parameters->decod_format == -1)
- return 1;
- sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
- if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) {
- return 1;
- }
-
- /*Set output file*/
- strcpy(temp_ofname,strtok(image_filename,"."));
- while((temp_p = strtok(NULL,".")) != NULL){
- strcat(temp_ofname,temp1);
- sprintf(temp1,".%s",temp_p);
- }
- if(img_fol->set_out_format==1){
- sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
- if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) {
- return 1;
- }
- }
- return 0;
+static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
+ opj_dparameters_t *parameters)
+{
+ char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
+ outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
+ char *temp_p, temp1[OPJ_PATH_LEN] = "";
+
+ strcpy(image_filename, dirptr->filename[imageno]);
+ fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
+ parameters->decod_format = get_file_format(image_filename);
+ if (parameters->decod_format == -1) {
+ return 1;
+ }
+ sprintf(infilename, "%s/%s", img_fol->imgdirpath, image_filename);
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
+ infilename) != 0) {
+ return 1;
+ }
+
+ /*Set output file*/
+ strcpy(temp_ofname, strtok(image_filename, "."));
+ while ((temp_p = strtok(NULL, ".")) != NULL) {
+ strcat(temp_ofname, temp1);
+ sprintf(temp1, ".%s", temp_p);
+ }
+ if (img_fol->set_out_format == 1) {
+ sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
+ img_fol->out_format);
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
+ outfilename) != 0) {
+ return 1;
+ }
+ }
+ return 0;
}
/* -------------------------------------------------------------------------- */
@@ -224,172 +242,176 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_d
static int infile_format(const char *fname)
{
- FILE *reader;
- const char *s, *magic_s;
- int ext_format, magic_format;
- unsigned char buf[12];
- size_t l_nb_read;
+ FILE *reader;
+ const char *s, *magic_s;
+ int ext_format, magic_format;
+ unsigned char buf[12];
+ size_t l_nb_read;
- reader = fopen(fname, "rb");
+ reader = fopen(fname, "rb");
- if (reader == NULL)
- return -1;
+ if (reader == NULL) {
+ return -1;
+ }
- memset(buf, 0, 12);
- l_nb_read = fread(buf, 1, 12, reader);
- fclose(reader);
- if (l_nb_read != 12)
- return -1;
+ memset(buf, 0, 12);
+ l_nb_read = fread(buf, 1, 12, reader);
+ fclose(reader);
+ if (l_nb_read != 12) {
+ return -1;
+ }
- ext_format = get_file_format(fname);
+ ext_format = get_file_format(fname);
- if (ext_format == JPT_CFMT)
- return JPT_CFMT;
+ if (ext_format == JPT_CFMT) {
+ return JPT_CFMT;
+ }
- if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
- magic_format = JP2_CFMT;
- magic_s = ".jp2";
- }
- else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
- magic_format = J2K_CFMT;
- magic_s = ".j2k or .jpc or .j2c";
- }
- else
- return -1;
+ if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
+ magic_format = JP2_CFMT;
+ magic_s = ".jp2";
+ } else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
+ magic_format = J2K_CFMT;
+ magic_s = ".j2k or .jpc or .j2c";
+ } else {
+ return -1;
+ }
- if (magic_format == ext_format)
- return ext_format;
+ if (magic_format == ext_format) {
+ return ext_format;
+ }
- s = fname + strlen(fname) - 4;
+ s = fname + strlen(fname) - 4;
- fputs("\n===========================================\n", stderr);
- fprintf(stderr, "The extension of this file is incorrect.\n"
- "FOUND %s. SHOULD BE %s\n", s, magic_s);
- fputs("===========================================\n", stderr);
+ fputs("\n===========================================\n", stderr);
+ fprintf(stderr, "The extension of this file is incorrect.\n"
+ "FOUND %s. SHOULD BE %s\n", s, magic_s);
+ fputs("===========================================\n", stderr);
- return magic_format;
+ return magic_format;
}
/* -------------------------------------------------------------------------- */
/**
* Parse the command line
*/
/* -------------------------------------------------------------------------- */
-static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
- int totlen, c;
- opj_option_t long_option[]={
- {"ImgDir",REQ_ARG, NULL ,'y'}
- };
+static int parse_cmdline_decoder(int argc, char **argv,
+ opj_dparameters_t *parameters, img_fol_t *img_fol)
+{
+ int totlen, c;
+ opj_option_t long_option[] = {
+ {"ImgDir", REQ_ARG, NULL, 'y'}
+ };
const char optlist[] = "i:o:f:hv";
- totlen=sizeof(long_option);
- img_fol->set_out_format = 0;
- do {
- c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
- if (c == -1)
- break;
- switch (c) {
- case 'i': /* input file */
- {
- char *infile = opj_optarg;
- parameters->decod_format = infile_format(infile);
- switch(parameters->decod_format) {
- case J2K_CFMT:
- break;
- case JP2_CFMT:
- break;
- case JPT_CFMT:
- break;
- default:
- fprintf(stderr,
- "[ERROR] Unknown input file format: %s \n"
- " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
- infile);
- return 1;
- }
- if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
- fprintf(stderr, "[ERROR] Path is too long\n");
- return 1;
- }
- }
- break;
-
- /* ------------------------------------------------------ */
-
- case 'o': /* output file */
- {
- if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), opj_optarg) != 0) {
- fprintf(stderr, "[ERROR] Path is too long\n");
- return 1;
- }
- }
- break;
-
- /* ----------------------------------------------------- */
- case 'f': /* flag */
- img_fol->flag = atoi(opj_optarg);
+ totlen = sizeof(long_option);
+ img_fol->set_out_format = 0;
+ do {
+ c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
+ if (c == -1) {
+ break;
+ }
+ switch (c) {
+ case 'i': { /* input file */
+ char *infile = opj_optarg;
+ parameters->decod_format = infile_format(infile);
+ switch (parameters->decod_format) {
+ case J2K_CFMT:
+ break;
+ case JP2_CFMT:
+ break;
+ case JPT_CFMT:
+ break;
+ default:
+ fprintf(stderr,
+ "[ERROR] Unknown input file format: %s \n"
+ " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
+ infile);
+ return 1;
+ }
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
+ }
+ break;
+
+ /* ------------------------------------------------------ */
+
+ case 'o': { /* output file */
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
+ opj_optarg) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+ case 'f': /* flag */
+ img_fol->flag = atoi(opj_optarg);
+ break;
+ /* ----------------------------------------------------- */
+
+ case 'h': /* display an help description */
+ decode_help_display();
+ return 1;
+
+ /* ------------------------------------------------------ */
+
+ case 'y': { /* Image Directory path */
+ img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+ if (img_fol->imgdirpath == NULL) {
+ return 1;
+ }
+ strcpy(img_fol->imgdirpath, opj_optarg);
+ img_fol->set_imgdir = 1;
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+
+ case 'v': { /* Verbose mode */
+ parameters->m_verbose = 1;
+ }
break;
- /* ----------------------------------------------------- */
-
- case 'h': /* display an help description */
- decode_help_display();
- return 1;
-
- /* ------------------------------------------------------ */
-
- case 'y': /* Image Directory path */
- {
- img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
- if(img_fol->imgdirpath == NULL){
- return 1;
- }
- strcpy(img_fol->imgdirpath,opj_optarg);
- img_fol->set_imgdir=1;
- }
- break;
-
- /* ----------------------------------------------------- */
-
- case 'v': /* Verbose mode */
- {
- parameters->m_verbose = 1;
- }
- break;
-
- /* ----------------------------------------------------- */
+
+ /* ----------------------------------------------------- */
default:
fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
break;
}
- }while(c != -1);
+ } while (c != -1);
- /* check for possible errors */
- if(img_fol->set_imgdir==1){
- if(!(parameters->infile[0]==0)){
+ /* check for possible errors */
+ if (img_fol->set_imgdir == 1) {
+ if (!(parameters->infile[0] == 0)) {
fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
- return 1;
- }
- if(img_fol->set_out_format == 0){
- fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
+ return 1;
+ }
+ if (img_fol->set_out_format == 0) {
+ fprintf(stderr,
+ "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
fprintf(stderr, "Only one format allowed.\n"
- "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
- return 1;
- }
- if(!(parameters->outfile[0] == 0)){
+ "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
+ return 1;
+ }
+ if (!(parameters->outfile[0] == 0)) {
fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together\n");
- return 1;
- }
- }else{
- if(parameters->infile[0] == 0) {
+ return 1;
+ }
+ } else {
+ if (parameters->infile[0] == 0) {
fprintf(stderr, "[ERROR] Required parameter is missing\n");
- fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
- fprintf(stderr, " Help: %s -h\n",argv[0]);
- return 1;
- }
- }
+ fprintf(stderr, "Example: %s -i image.j2k\n", argv[0]);
+ fprintf(stderr, " Help: %s -h\n", argv[0]);
+ return 1;
+ }
+ }
- return 0;
+ return 0;
}
/* -------------------------------------------------------------------------- */
@@ -397,23 +419,26 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param
/**
sample error debug callback expecting no client object
*/
-static void error_callback(const char *msg, void *client_data) {
- (void)client_data;
- fprintf(stdout, "[ERROR] %s", msg);
+static void error_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+ fprintf(stdout, "[ERROR] %s", msg);
}
/**
sample warning debug callback expecting no client object
*/
-static void warning_callback(const char *msg, void *client_data) {
- (void)client_data;
- fprintf(stdout, "[WARNING] %s", msg);
+static void warning_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+ fprintf(stdout, "[WARNING] %s", msg);
}
/**
sample debug callback expecting no client object
*/
-static void info_callback(const char *msg, void *client_data) {
- (void)client_data;
- fprintf(stdout, "[INFO] %s", msg);
+static void info_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+ fprintf(stdout, "[INFO] %s", msg);
}
/* -------------------------------------------------------------------------- */
@@ -423,188 +448,193 @@ static void info_callback(const char *msg, void *client_data) {
/* -------------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
- FILE *fout = NULL;
-
- opj_dparameters_t parameters; /* Decompression parameters */
- opj_image_t* image = NULL; /* Image structure */
- opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
- opj_stream_t *l_stream = NULL; /* Stream */
- opj_codestream_info_v2_t* cstr_info = NULL;
- opj_codestream_index_t* cstr_index = NULL;
-
- OPJ_INT32 num_images, imageno;
- img_fol_t img_fol;
- dircnt_t *dirptr = NULL;
-
- /* Set decoding parameters to default values */
- opj_set_default_decoder_parameters(&parameters);
-
- /* Initialize img_fol */
- memset(&img_fol,0,sizeof(img_fol_t));
- img_fol.flag = OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND;
-
- /* Parse input and get user encoding parameters */
- if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
- if(img_fol.imgdirpath) free(img_fol.imgdirpath);
-
- return EXIT_FAILURE;
- }
-
- /* Initialize reading of directory */
- if(img_fol.set_imgdir==1){
- int it_image;
- num_images=get_num_images(img_fol.imgdirpath);
-
- dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
- if(!dirptr){
- return EXIT_FAILURE;
- }
- dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
- if(!dirptr->filename_buf){
- free(dirptr);
- return EXIT_FAILURE;
- }
- dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
-
- if(!dirptr->filename){
- goto fails;
- }
-
- 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){
- goto fails;
- }
-
- if (num_images==0){
- fprintf(stdout,"Folder is empty\n");
- goto fails;
- }
- }else{
- num_images=1;
- }
-
- /* Try to open for writing the output file if necessary */
- if (parameters.outfile[0] != 0){
- fout = fopen(parameters.outfile,"w");
- if (!fout){
- fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
- goto fails;
- }
- }
- else
- fout = stdout;
-
- /* Read the header of each image one by one */
- for(imageno = 0; imageno < num_images ; imageno++){
-
- fprintf(stderr,"\n");
-
- if(img_fol.set_imgdir==1){
- if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
- fprintf(stderr,"skipping file...\n");
- continue;
- }
- }
-
- /* Read the input file and put it in memory */
- /* ---------------------------------------- */
-
- l_stream = opj_stream_create_default_file_stream(parameters.infile,1);
- if (!l_stream){
- fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",parameters.infile);
- goto fails;
- }
-
- /* Read the JPEG2000 stream */
- /* ------------------------ */
-
- switch(parameters.decod_format) {
- case J2K_CFMT: /* JPEG-2000 codestream */
- {
- /* Get a decoder handle */
- l_codec = opj_create_decompress(OPJ_CODEC_J2K);
- break;
- }
- case JP2_CFMT: /* JPEG 2000 compressed image data */
- {
- /* Get a decoder handle */
- l_codec = opj_create_decompress(OPJ_CODEC_JP2);
- break;
- }
- case JPT_CFMT: /* JPEG 2000, JPIP */
- {
- /* Get a decoder handle */
- l_codec = opj_create_decompress(OPJ_CODEC_JPT);
- break;
- }
- default:
- fprintf(stderr, "skipping file..\n");
- opj_stream_destroy(l_stream);
- continue;
- }
-
- /* catch events using our callbacks and give a local context */
- opj_set_info_handler(l_codec, info_callback,00);
- opj_set_warning_handler(l_codec, warning_callback,00);
- opj_set_error_handler(l_codec, error_callback,00);
-
- /* Setup the decoder decoding parameters using user parameters */
- if ( !opj_setup_decoder(l_codec, &parameters) ){
- fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");
- opj_stream_destroy(l_stream);
- opj_destroy_codec(l_codec);
- fclose(fout);
- goto fails;
- }
-
- /* Read the main header of the codestream and if necessary the JP2 boxes*/
- if(! opj_read_header(l_stream, l_codec, &image)){
- fprintf(stderr, "ERROR -> opj_dump: failed to read the header\n");
- opj_stream_destroy(l_stream);
- opj_destroy_codec(l_codec);
- opj_image_destroy(image);
- fclose(fout);
- goto fails;
- }
-
- opj_dump_codec(l_codec, img_fol.flag, fout );
-
- cstr_info = opj_get_cstr_info(l_codec);
-
- cstr_index = opj_get_cstr_index(l_codec);
-
- /* close the byte stream */
- opj_stream_destroy(l_stream);
-
- /* free remaining structures */
- if (l_codec) {
- opj_destroy_codec(l_codec);
- }
-
- /* destroy the image header */
- opj_image_destroy(image);
-
- /* destroy the codestream index */
- opj_destroy_cstr_index(&cstr_index);
-
- /* destroy the codestream info */
- opj_destroy_cstr_info(&cstr_info);
-
- }
-
- /* Close the output file */
- fclose(fout);
-
- return EXIT_SUCCESS;
+ FILE *fout = NULL;
+
+ opj_dparameters_t parameters; /* Decompression parameters */
+ opj_image_t* image = NULL; /* Image structure */
+ opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
+ opj_stream_t *l_stream = NULL; /* Stream */
+ opj_codestream_info_v2_t* cstr_info = NULL;
+ opj_codestream_index_t* cstr_index = NULL;
+
+ OPJ_INT32 num_images, imageno;
+ img_fol_t img_fol;
+ dircnt_t *dirptr = NULL;
+
+ /* Set decoding parameters to default values */
+ opj_set_default_decoder_parameters(&parameters);
+
+ /* Initialize img_fol */
+ memset(&img_fol, 0, sizeof(img_fol_t));
+ img_fol.flag = OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND;
+
+ /* Parse input and get user encoding parameters */
+ if (parse_cmdline_decoder(argc, argv, &parameters, &img_fol) == 1) {
+ if (img_fol.imgdirpath) {
+ free(img_fol.imgdirpath);
+ }
+
+ return EXIT_FAILURE;
+ }
+
+ /* Initialize reading of directory */
+ if (img_fol.set_imgdir == 1) {
+ int it_image;
+ num_images = get_num_images(img_fol.imgdirpath);
+
+ dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
+ if (!dirptr) {
+ return EXIT_FAILURE;
+ }
+ dirptr->filename_buf = (char*)malloc((size_t)num_images * OPJ_PATH_LEN * sizeof(
+ char)); /* Stores at max 10 image file names*/
+ if (!dirptr->filename_buf) {
+ free(dirptr);
+ return EXIT_FAILURE;
+ }
+ dirptr->filename = (char**) malloc((size_t)num_images * sizeof(char*));
+
+ if (!dirptr->filename) {
+ goto fails;
+ }
+
+ 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) {
+ goto fails;
+ }
+
+ if (num_images == 0) {
+ fprintf(stdout, "Folder is empty\n");
+ goto fails;
+ }
+ } else {
+ num_images = 1;
+ }
+
+ /* Try to open for writing the output file if necessary */
+ if (parameters.outfile[0] != 0) {
+ fout = fopen(parameters.outfile, "w");
+ if (!fout) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
+ goto fails;
+ }
+ } else {
+ fout = stdout;
+ }
+
+ /* Read the header of each image one by one */
+ for (imageno = 0; imageno < num_images ; imageno++) {
+
+ fprintf(stderr, "\n");
+
+ if (img_fol.set_imgdir == 1) {
+ if (get_next_file(imageno, dirptr, &img_fol, &parameters)) {
+ fprintf(stderr, "skipping file...\n");
+ continue;
+ }
+ }
+
+ /* Read the input file and put it in memory */
+ /* ---------------------------------------- */
+
+ l_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
+ if (!l_stream) {
+ fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",
+ parameters.infile);
+ goto fails;
+ }
+
+ /* Read the JPEG2000 stream */
+ /* ------------------------ */
+
+ switch (parameters.decod_format) {
+ case J2K_CFMT: { /* JPEG-2000 codestream */
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_J2K);
+ break;
+ }
+ case JP2_CFMT: { /* JPEG 2000 compressed image data */
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_JP2);
+ break;
+ }
+ case JPT_CFMT: { /* JPEG 2000, JPIP */
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_JPT);
+ break;
+ }
+ default:
+ fprintf(stderr, "skipping file..\n");
+ opj_stream_destroy(l_stream);
+ continue;
+ }
+
+ /* catch events using our callbacks and give a local context */
+ opj_set_info_handler(l_codec, info_callback, 00);
+ opj_set_warning_handler(l_codec, warning_callback, 00);
+ opj_set_error_handler(l_codec, error_callback, 00);
+
+ /* Setup the decoder decoding parameters using user parameters */
+ if (!opj_setup_decoder(l_codec, &parameters)) {
+ fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ fclose(fout);
+ goto fails;
+ }
+
+ /* Read the main header of the codestream and if necessary the JP2 boxes*/
+ if (! opj_read_header(l_stream, l_codec, &image)) {
+ fprintf(stderr, "ERROR -> opj_dump: failed to read the header\n");
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(image);
+ fclose(fout);
+ goto fails;
+ }
+
+ opj_dump_codec(l_codec, img_fol.flag, fout);
+
+ cstr_info = opj_get_cstr_info(l_codec);
+
+ cstr_index = opj_get_cstr_index(l_codec);
+
+ /* close the byte stream */
+ opj_stream_destroy(l_stream);
+
+ /* free remaining structures */
+ if (l_codec) {
+ opj_destroy_codec(l_codec);
+ }
+
+ /* destroy the image header */
+ opj_image_destroy(image);
+
+ /* destroy the codestream index */
+ opj_destroy_cstr_index(&cstr_index);
+
+ /* destroy the codestream info */
+ opj_destroy_cstr_info(&cstr_info);
+
+ }
+
+ /* Close the output file */
+ fclose(fout);
+
+ return EXIT_SUCCESS;
fails:
- if(dirptr){
- if(dirptr->filename) free(dirptr->filename);
- if(dirptr->filename_buf) free(dirptr->filename_buf);
- free(dirptr);
- }
- return EXIT_FAILURE;
+ if (dirptr) {
+ if (dirptr->filename) {
+ free(dirptr->filename);
+ }
+ if (dirptr->filename_buf) {
+ free(dirptr->filename_buf);
+ }
+ free(dirptr);
+ }
+ return EXIT_FAILURE;
}