#include <string.h>
#include <ctype.h>
-#ifdef OPJ_HAVE_LIBTIFF
-#include <tiffio.h>
-#endif /* OPJ_HAVE_LIBTIFF */
-
-#ifdef OPJ_HAVE_LIBPNG
-#include <zlib.h>
-#include <png.h>
-#endif /* OPJ_HAVE_LIBPNG */
-
#include "openjpeg.h"
#include "convert.h"
}
+/* planar / interleaved conversions */
+/* used by PNG/TIFF */
+static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
+{
+ memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
+}
+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];
+ }
+}
+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];
+ }
+}
+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];
+ }
+}
+const convert_32s_CXPX convert_32s_CXPX_LUT[5] = {
+ NULL,
+ convert_32s_C1P1,
+ convert_32s_C2P2,
+ convert_32s_C3P3,
+ convert_32s_C4P4
+};
+
+static void convert_32s_P1C1(OPJ_INT32 const* const* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length, OPJ_INT32 adjust)
+{
+ OPJ_SIZE_T i;
+ const OPJ_INT32* pSrc0 = pSrc[0];
+
+ for (i = 0; i < length; i++) {
+ pDst[i] = pSrc0[i] + adjust;
+ }
+}
+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;
+ }
+}
+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;
+ }
+}
+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;
+ }
+}
+const convert_32s_PXCX convert_32s_PXCX_LUT[5] = {
+ NULL,
+ convert_32s_P1C1,
+ convert_32s_P2C2,
+ convert_32s_P3C3,
+ convert_32s_P4C4
+};
+
+/* bit depth conversions */
+/* used by PNG/TIFF up to 8bpp */
+static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+{
+ OPJ_SIZE_T i;
+ for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
+ OPJ_UINT32 val = *pSrc++;
+ pDst[i+0] = (OPJ_INT32)( val >> 7);
+ pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
+ pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
+ pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
+ pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
+ pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
+ pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
+ pDst[i+7] = (OPJ_INT32)(val & 0x1U);
+ }
+ if (length & 7U) {
+ OPJ_UINT32 val = *pSrc++;
+ length = length & 7U;
+ pDst[i+0] = (OPJ_INT32)(val >> 7);
+
+ if (length > 1U) {
+ pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
+ if (length > 2U) {
+ pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
+ if (length > 3U) {
+ pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
+ if (length > 4U) {
+ pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
+ if (length > 5U) {
+ pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
+ if (length > 6U) {
+ pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+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);
+
+ }
+ }
+ }
+}
+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);
+ }
+}
+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));
+ }
+ }
+ }
+}
+static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
+{
+ OPJ_SIZE_T i;
+ for (i = 0; i < length; i++) {
+ pDst[i] = pSrc[i];
+ }
+}
+const convert_XXx32s_C1R convert_XXu32s_C1R_LUT[9] = {
+ NULL,
+ convert_1u32s_C1R,
+ convert_2u32s_C1R,
+ NULL,
+ convert_4u32s_C1R,
+ NULL,
+ convert_6u32s_C1R,
+ NULL,
+ convert_8u32s_C1R
+};
+
+
+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));
+ }
+}
+
+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));
+ }
+}
+
+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));
+ }
+}
+
+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));
+ }
+ }
+ }
+}
+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];
+ }
+}
+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
+};
+
/* -->> -->> -->> -->>
TGA IMAGE FORMAT
static unsigned short get_ushort(unsigned short val) {
#ifdef OPJ_BIG_ENDIAN
- return( ((val & 0xff) << 8) + (val >> 8) );
+ return (unsigned short)(((val & 0xffU) << 8) | (val >> 8));
#else
- return( val );
+ return val;
#endif
}
unsigned int *width, unsigned int *height, int *flip_image)
{
int palette_size;
- unsigned char *tga ;
+ unsigned char tga[TGA_HEADER_SIZE];
unsigned char id_len, /*cmap_type,*/ image_type;
unsigned char pixel_depth, image_desc;
unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size;
if (!bits_per_pixel || !width || !height || !flip_image)
return 0;
- tga = (unsigned char*)malloc(18);
if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
{
pixel_depth = (unsigned char)tga[16];
image_desc = (unsigned char)tga[17];
- free(tga);
-
*bits_per_pixel = (unsigned int)pixel_depth;
*width = (unsigned int)image_w;
*height = (unsigned int)image_h;
#ifdef OPJ_BIG_ENDIAN
-static INLINE int16_t swap16(int16_t x)
+static INLINE OPJ_UINT16 swap16(OPJ_UINT16 x)
{
- return((((u_int16_t)x & 0x00ffU) << 8) |
- (((u_int16_t)x & 0xff00U) >> 8));
+ return (OPJ_UINT16)(((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
}
#endif
static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
OPJ_BOOL flip_image)
{
- unsigned short image_w, image_h, us0;
+ OPJ_UINT16 image_w, image_h, us0;
unsigned char uc0, image_type;
unsigned char pixel_depth, image_desc;
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;
+ }
/* We currently only support 24 & 32 bit tga's ... */
- if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
+ if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
+ fclose(f);
return NULL;
+ }
/* initialize image components */
memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
/* create the image */
image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
- if (!image)
+ if (!image) {
+ fclose(f);
return NULL;
+ }
+
/* set image offset and reference grid */
image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
{
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");
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");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
{
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");
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");
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");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
}
}
+ fclose(f);
return image;
}
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.");
return 1;
}
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){
+ fclose(f);
fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
return NULL;
}
} else if (endian2=='M' && endian1=='L') {
bigendian = 0;
} else {
+ fclose(f);
fprintf(stderr, "Bad pgx header, please check input file\n");
return NULL;
}
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 */
const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
if( outfile[dotpos] != '.' )
- {
+ {
/* `pgx` was recognized but there is no dot at expected position */
fprintf(stderr, "ERROR -> Impossible happen." );
goto fin;
- }
+ }
if( total > 256 )
- {
+ {
name = (char*)malloc(total+1);
- }
+ if (name == NULL) {
+ goto fin;
+ }
+ }
strncpy(name, outfile, dotpos);
sprintf(name+dotpos, "_%d.pgx", compno);
fdest = fopen(name, "wb");
- /* dont need name anymore */
- if( total > 256 ) free(name);
+ /* 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;
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);
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(fp);
return NULL;
}
if(one)
if (!cmptparm) {
fprintf(stderr, "Failed to allocate image components parameters !!\n");
fprintf(stderr,"Aborting\n");
+ fclose(f);
return NULL;
}
/* initialize image components */
for (i = 0; i < nloop; i++) {
if (!fread(&value, 1, 1, f)) {
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;
{
unsigned short value;
for(compno = 0; compno < numcomps; compno++) {
- int nloop = (w*h)/(raw_cp->rawComps[compno].dx*raw_cp->rawComps[compno].dx);
+ 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");
+ 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");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if( big_endian )
}
else {
fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
return imagetoraw_common(image, outfile, OPJ_FALSE);
}
-#ifdef OPJ_HAVE_LIBPNG
-
-#define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
-#define MAGIC_SIZE 8
-/* PNG allows bits per sample: 1, 2, 4, 8, 16 */
-
-opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
-{
- png_structp png;
- png_infop info;
- double gamma, display_exponent;
- int bit_depth, interlace_type,compression_type, filter_type;
- int unit;
- png_uint_32 resx, resy;
- unsigned int i, j;
- png_uint_32 width, height;
- int color_type, has_alpha, is16;
- unsigned char *s;
- FILE *reader;
- unsigned char **rows;
- /* j2k: */
- opj_image_t *image;
- opj_image_cmptparm_t cmptparm[4];
- int sub_dx, sub_dy;
- unsigned int nr_comp;
- int *r, *g, *b, *a = NULL;
- unsigned char sigbuf[8];
-
- if((reader = fopen(read_idf, "rb")) == NULL)
- {
- fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
- return NULL;
- }
- image = NULL; png = NULL; rows = 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;
- }
- /* libpng-VERSION/example.c:
- * PC : screen_gamma = 2.2;
- * Mac: screen_gamma = 1.7 or 1.0;
-*/
- display_exponent = 2.2;
-
- 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);
- else
- if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
- png_set_expand(png);
-
- if(png_get_valid(png, info, PNG_INFO_tRNS))
- png_set_expand(png);
-
- is16 = (bit_depth == 16);
-
- /* GRAY => RGB; GRAY_ALPHA => RGBA
-*/
- if(color_type == PNG_COLOR_TYPE_GRAY
- || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
- {
- png_set_gray_to_rgb(png);
- color_type =
- (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB:
- PNG_COLOR_TYPE_RGB_ALPHA);
- }
- if( !png_get_gAMA(png, info, &gamma))
- gamma = 0.45455;
-
- png_set_gamma(png, display_exponent, gamma);
-
- png_read_update_info(png, info);
-
- png_get_pHYs(png, info, &resx, &resy, &unit);
-
- color_type = png_get_color_type(png, info);
-
- has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
-
- nr_comp = 3 + (unsigned int)has_alpha;
-
- bit_depth = png_get_bit_depth(png, info);
-
- rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
- for(i = 0; i < height; ++i)
- rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
-
- png_read_image(png, rows);
-
- memset(cmptparm, 0, sizeof(cmptparm));
-
- sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
-
- 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)sub_dx;
- cmptparm[i].dy = (OPJ_UINT32)sub_dy;
- cmptparm[i].w = (OPJ_UINT32)width;
- cmptparm[i].h = (OPJ_UINT32)height;
- }
-
- image = opj_image_create(nr_comp, &cmptparm[0], OPJ_CLRSPC_SRGB);
-
- 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)sub_dx + 1 + image->x0);
- image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)sub_dy + 1 + image->y0);
-
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
- if(has_alpha) {
- a = image->comps[3].data;
- image->comps[3].alpha = 1;
- }
-
- for(i = 0; i < height; ++i)
- {
- s = rows[i];
-
- for(j = 0; j < width; ++j)
- {
- if(is16)
- {
- *r++ = s[0]<<8|s[1]; s += 2;
-
- *g++ = s[0]<<8|s[1]; s += 2;
-
- *b++ = s[0]<<8|s[1]; s += 2;
-
- if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
-
- continue;
- }
- *r++ = *s++; *g++ = *s++; *b++ = *s++;
-
- if(has_alpha) *a++ = *s++;
- }
- }
-fin:
- if(rows)
- {
- for(i = 0; i < height; ++i)
- free(rows[i]);
- free(rows);
- }
- if(png)
- png_destroy_read_struct(&png, &info, NULL);
-
- fclose(reader);
-
- return image;
-
-}/* pngtoimage() */
-
-int imagetopng(opj_image_t * image, const char *write_idf)
-{
- FILE *writer;
- png_structp png;
- png_infop info;
- int *red, *green, *blue, *alpha;
- unsigned char *row_buf, *d;
- int has_alpha, width, height, nr_comp, color_type;
- int adjustR, adjustG, adjustB, adjustA, x, y, fails;
- int prec, ushift, dshift, is16, force16, force8;
- unsigned short mask = 0xffff;
- png_color_8 sig_bit;
-
- is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
- prec = (int)image->comps[0].prec;
- nr_comp = (int)image->numcomps;
-
- if(prec > 8 && prec < 16)
- {
- ushift = 16 - prec; dshift = prec - ushift;
- prec = 16; force16 = 1;
- }
- else
- if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
- {
- ushift = 8 - prec; dshift = 8 - ushift;
- prec = 8; force8 = 1;
- }
-
- 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;
-
- info = NULL; has_alpha = 0;
-
- /* 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(prec == 16) mask = 0xffff;
- else
- if(prec == 8) mask = 0x00ff;
- else
- if(prec == 4) mask = 0x000f;
- else
- if(prec == 2) mask = 0x0003;
- else
- if(prec == 1) mask = 0x0001;
-
- if(nr_comp >= 3
- && image->comps[0].dx == image->comps[1].dx
- && image->comps[1].dx == image->comps[2].dx
- && image->comps[0].dy == image->comps[1].dy
- && image->comps[1].dy == image->comps[2].dy
- && image->comps[0].prec == image->comps[1].prec
- && image->comps[1].prec == image->comps[2].prec)
- {
- int v;
-
- has_alpha = (nr_comp > 3);
-
- is16 = (prec == 16);
-
- width = (int)image->comps[0].w;
- height = (int)image->comps[0].h;
-
- red = image->comps[0].data;
- green = image->comps[1].data;
- blue = image->comps[2].data;
-
- sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec;
-
- if(has_alpha)
- {
- sig_bit.alpha = (png_byte)prec;
- alpha = image->comps[3].data;
- color_type = PNG_COLOR_TYPE_RGB_ALPHA;
- adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
- }
- else
- {
- sig_bit.alpha = 0; alpha = NULL;
- color_type = PNG_COLOR_TYPE_RGB;
- adjustA = 0;
- }
- png_set_sBIT(png, info, &sig_bit);
-
- png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, prec,
- color_type,
- PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-
- png_set_gamma(png, 2.2, 1./2.2);
- png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL);
- /*=============================*/
- png_write_info(png, info);
- /*=============================*/
- if(prec < 8)
- {
- png_set_packing(png);
- }
-// printf("%s:%d:sgnd(%d,%d,%d) w(%d) h(%d) alpha(%d)\n",__FILE__,__LINE__,
-//image->comps[0].sgnd,
-//image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
-
- 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);
-
- row_buf = (unsigned char*)malloc((size_t)width * (size_t)nr_comp * 2);
-
- for(y = 0; y < height; ++y)
- {
- d = row_buf;
-
- for(x = 0; x < width; ++x)
- {
- if(is16)
- {
- v = *red + adjustR; ++red;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- v = *green + adjustG; ++green;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- v = *blue + adjustB; ++blue;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- if(has_alpha)
- {
- v = *alpha + adjustA; ++alpha;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
- }
- continue;
- }/* if(is16) */
-
- v = *red + adjustR; ++red;
- if(v > 255) v = 255; else if(v < 0) v = 0;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- v = *green + adjustG; ++green;
- if(v > 255) v = 255; else if(v < 0) v = 0;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- v = *blue + adjustB; ++blue;
- if(v > 255) v = 255; else if(v < 0) v = 0;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- if(has_alpha)
- {
- v = *alpha + adjustA; ++alpha;
- if(v > 255) v = 255; else if(v < 0) v = 0;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
- }
- } /* for(x) */
-
- png_write_row(png, row_buf);
-
- } /* for(y) */
- free(row_buf);
-
- }/* nr_comp >= 3 */
- else
- if(nr_comp == 1 /* GRAY */
- || ( nr_comp == 2 /* GRAY_ALPHA */
- && image->comps[0].dx == image->comps[1].dx
- && image->comps[0].dy == image->comps[1].dy
- && image->comps[0].prec == image->comps[1].prec))
- {
- int v;
-
- red = image->comps[0].data;
-
- sig_bit.gray = (png_byte)prec;
- sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
- alpha = NULL; adjustA = 0;
- color_type = PNG_COLOR_TYPE_GRAY;
-
- if(nr_comp == 2)
- {
- has_alpha = 1; sig_bit.alpha = (png_byte)prec;
- alpha = image->comps[1].data;
- color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
- adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
- }
- width = (int)image->comps[0].w;
- height = (int)image->comps[0].h;
-
- png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, sig_bit.gray,
- 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);
- /*=============================*/
- adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
-
- if(prec < 8)
- {
- png_set_packing(png);
- }
-
- if(prec > 8)
- {
- row_buf = (unsigned char*)
- malloc((size_t)width * (size_t)nr_comp * sizeof(unsigned short));
-
- for(y = 0; y < height; ++y)
- {
- d = row_buf;
-
- for(x = 0; x < width; ++x)
- {
- v = *red + adjustR; ++red;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- if(has_alpha)
- {
- v = *alpha++;
- if(v > 65535) v = 65535; else if(v < 0) v = 0;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
- }
- }/* for(x) */
- png_write_row(png, row_buf);
-
- } /* for(y) */
- free(row_buf);
- }
- else /* prec <= 8 */
- {
- row_buf = (unsigned char*)calloc((size_t)width, (size_t)nr_comp * 2);
-
- for(y = 0; y < height; ++y)
- {
- d = row_buf;
-
- for(x = 0; x < width; ++x)
- {
- v = *red + adjustR; ++red;
- if(v > 255) v = 255; else if(v < 0) v = 0;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- if(has_alpha)
- {
- v = *alpha + adjustA; ++alpha;
- if(v > 255) v = 255; else if(v < 0) v = 0;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
- }
- }/* for(x) */
-
- png_write_row(png, row_buf);
-
- } /* for(y) */
- free(row_buf);
- }
- }
- else
- {
- fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
- goto fin;
- }
- png_write_end(png, info);
-
- fails = 0;
-
-fin:
-
- if(png)
- {
- png_destroy_write_struct(&png, &info);
- }
- fclose(writer);
-
- if(fails) remove(write_idf);
-
- return fails;
-}/* imagetopng() */
-#endif /* OPJ_HAVE_LIBPNG */