Fix typos in comments and string
[openjpeg.git] / src / bin / jp2 / convert.c
index 6f3e0a22c6d650436df7d49069d46420548ff886..1a18711caf0096fde67b054438dc9b85091ccde9 100644 (file)
@@ -145,6 +145,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
@@ -179,9 +560,9 @@ struct tga_header
 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
 
 }
@@ -192,7 +573,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,7 +581,6 @@ 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 )
     {
@@ -224,8 +604,6 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
     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;
@@ -267,10 +645,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 +655,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 +734,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 +776,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 +808,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 +841,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 +877,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 +904,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 +1097,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 +1115,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 +1230,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 +1241,30 @@ 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) {
+                               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;
@@ -880,26 +1280,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);
@@ -1273,6 +1675,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)
@@ -1626,6 +2029,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 +2063,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 +2075,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 +2105,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;
     }