cppcheck fix for openjp2 (#740)
[openjpeg.git] / src / bin / jp2 / convert.c
index 6f3e0a22c6d650436df7d49069d46420548ff886..940ac2fa80d7cd0faa7ace64d0ab7581223460b4 100644 (file)
@@ -114,6 +114,7 @@ static void scale_component_up(opj_image_comp_t* component, OPJ_UINT32 precision
                }
        }
        component->prec = precision;
+       component->bpp = precision;
 }
 void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision)
 {
@@ -145,6 +146,387 @@ void scale_component(opj_image_comp_t* component, OPJ_UINT32 precision)
 }
 
 
+/* planar / interleaved conversions */
+/* used by PNG/TIFF */
+static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
+{
+       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
@@ -176,14 +558,12 @@ struct tga_header
 };
 #endif /* INFORMATION_ONLY */
 
-static unsigned short get_ushort(unsigned short val) {
-
+static unsigned short get_ushort(const unsigned char *data) {
+    unsigned short val = *(const unsigned short *)data;
 #ifdef OPJ_BIG_ENDIAN
-    return( ((val & 0xff) << 8) + (val >> 8) );
-#else
-    return( val );
+    val = ((val & 0xffU) << 8) | (val >> 8);
 #endif
-
+    return val;
 }
 
 #define TGA_HEADER_SIZE 18
@@ -192,7 +572,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
                           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;
@@ -200,31 +580,28 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
 
     if (!bits_per_pixel || !width || !height || !flip_image)
         return 0;
-    tga = (unsigned char*)malloc(18);
 
     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 = (unsigned char)tga[0];
-    /*cmap_type = (unsigned char)tga[1];*/
-    image_type = (unsigned char)tga[2];
-    /*cmap_index = get_ushort(*(unsigned short*)(&tga[3]));*/
-    cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
-    cmap_entry_size = (unsigned char)tga[7];
+    id_len = tga[0];
+    /*cmap_type = tga[1];*/
+    image_type = tga[2];
+    /*cmap_index = get_ushort(&tga[3]);*/
+    cmap_len = get_ushort(&tga[5]);
+    cmap_entry_size = tga[7];
 
 
 #if 0
-    x_origin = get_ushort(*(unsigned short*)(&tga[8]));
-    y_origin = get_ushort(*(unsigned short*)(&tga[10]));
+    x_origin = get_ushort(&tga[8]);
+    y_origin = get_ushort(&tga[10]);
 #endif
-    image_w = get_ushort(*(unsigned short*)(&tga[12]));
-    image_h = get_ushort(*(unsigned short*)(&tga[14]));
-    pixel_depth = (unsigned char)tga[16];
-    image_desc  = (unsigned char)tga[17];
-
-    free(tga);
+    image_w = get_ushort(&tga[12]);
+    image_h = get_ushort(&tga[14]);
+    pixel_depth = tga[16];
+    image_desc  = tga[17];
 
     *bits_per_pixel = (unsigned int)pixel_depth;
     *width  = (unsigned int)image_w;
@@ -234,6 +611,10 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
     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");
@@ -267,10 +648,9 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
 
 #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
@@ -278,7 +658,7 @@ static INLINE int16_t swap16(int16_t x)
 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;
 
@@ -357,12 +737,16 @@ 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;
+    }
 
     /* 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));
@@ -395,8 +779,11 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
     /* 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;
@@ -424,18 +811,21 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
                 {
                     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;
                 }
 
@@ -454,24 +844,28 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
                 {
                     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;
                 }
 
@@ -486,6 +880,7 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
             fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
         }
     }
+    fclose(f);
     return image;
 }
 
@@ -512,6 +907,7 @@ int imagetotga(opj_image_t * image, const char *outfile) {
         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;
         }
@@ -704,6 +1100,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
 
     fseek(f, 0, SEEK_SET);
     if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
+        fclose(f);
         fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
         return NULL;
     }
@@ -721,6 +1118,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
     } else if (endian2=='M' && endian1=='L') {
         bigendian = 0;
     } else {
+        fclose(f);
         fprintf(stderr, "Bad pgx header, please check input file\n");
         return NULL;
     }
@@ -835,7 +1233,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
   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 */
@@ -846,25 +1244,31 @@ int imagetopgx(opj_image_t * image, const char *outfile)
     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, "_%d.pgx", compno);
+    sprintf(name+dotpos, "_%u.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;
@@ -880,26 +1284,28 @@ int imagetopgx(opj_image_t * image, const char *outfile)
       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);
@@ -936,7 +1342,7 @@ static char *skip_int(char *start, int *out_n)
     char *s;
     char c;
 
-    *out_n = 0; s = start;
+    *out_n = 0;
 
     s = skip_white(start);
     if(s == NULL) return NULL;
@@ -1273,6 +1679,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
                   {
                   fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                   opj_image_destroy(image);
+                  fclose(fp);
                   return NULL;
                   }
                     if(one)
@@ -1408,7 +1815,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
         {
             const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA");
 
-            fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
+            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;
@@ -1504,7 +1911,11 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
         fprintf(stderr,"           is written to the file\n");
     }
     destname = (char*)malloc(strlen(outfile) + 8);
-
+       if(destname == NULL){
+               fprintf(stderr, "imagetopnm: memory out\n");
+               fclose(fdest);
+               return 1;
+       }
     for (compno = 0; compno < ncomp; compno++)
     {
     if (ncomp > 1)
@@ -1514,7 +1925,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
       const size_t dotpos = olen - 4;
 
       strncpy(destname, outfile, dotpos);
-      sprintf(destname+dotpos, "_%d.pgm", compno);
+      sprintf(destname+dotpos, "_%u.pgm", compno);
       }
         else
             sprintf(destname, "%s", outfile);
@@ -1626,6 +2037,7 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
     if (!cmptparm) {
         fprintf(stderr, "Failed to allocate image components parameters !!\n");
         fprintf(stderr,"Aborting\n");
+        fclose(f);
         return NULL;
     }
     /* initialize image components */
@@ -1659,6 +2071,8 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
             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;
@@ -1669,16 +2083,20 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
     {
         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 )
@@ -1695,6 +2113,8 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
     }
     else {
         fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
+        opj_image_destroy(image);
+        fclose(f);
         return NULL;
     }
 
@@ -1742,7 +2162,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
 
     for(compno = 0; compno < image->numcomps; compno++)
     {
-        fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
+        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;