Fix bad call to fclose with NULL pointer
[openjpeg.git] / src / bin / jp2 / convert.c
index 1fd64ee7ef93ffe935441258bd6611d92108647c..c130b3bfbc2ed2453388f16db05cd595954c6943 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)
 {
@@ -557,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 (unsigned short)(((val & 0xffU) << 8) | (val >> 8));
-#else
-    return val;
+    val = ((val & 0xffU) << 8) | (val >> 8);
 #endif
-
+    return val;
 }
 
 #define TGA_HEADER_SIZE 18
@@ -587,22 +586,22 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
         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];
+    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;
@@ -612,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");
@@ -1250,13 +1253,14 @@ int imagetopgx(opj_image_t * image, const char *outfile)
                {
       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 */
+    /* don't need name anymore */
                        
     if (!fdest) 
                {
@@ -1338,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;
@@ -1758,7 +1762,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
     const char *tmp = outfile;
     char *destname;
 
-       alpha = NULL;
+    alpha = NULL;
 
     if((prec = (int)image->comps[0].prec) > 16)
     {
@@ -1811,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;
@@ -1838,7 +1842,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
             if(two)
             {
                 v = *red + adjustR; ++red;
-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);
@@ -1846,13 +1850,13 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
                 if(triple)
                 {
                     v = *green + adjustG; ++green;
-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);
 
                     v =  *blue + adjustB; ++blue;
-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);
@@ -1862,7 +1866,7 @@ 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;
+                    if(v > 65535) v = 65535; else if(v < 0) v = 0;
 
                     /* netpbm: */
                     fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@@ -1872,28 +1876,28 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
             }  /* if(two) */
 
             /* prec <= 8: */
-       v = *red++;
-       if(v > 255) v = 255; else if(v < 0) v = 0;
+            v = *red++;
+            if(v > 255) v = 255; else if(v < 0) v = 0;
 
-       fprintf(fdest, "%c", (unsigned char)v);
+            fprintf(fdest, "%c", (unsigned char)v);
             if(triple)
- {
-       v = *green++;
-       if(v > 255) v = 255; else if(v < 0) v = 0;
           {
+                v = *green++;
+                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;
+                fprintf(fdest, "%c", (unsigned char)v);
+                v = *blue++;
+                if(v > 255) v = 255; else if(v < 0) v = 0;
 
-       fprintf(fdest, "%c", (unsigned char)v);
- }
+                fprintf(fdest, "%c", (unsigned char)v);
                                              }
             if(has_alpha)
- {
-       v = *alpha++;
-       if(v > 255) v = 255; else if(v < 0) v = 0;
           {
+                v = *alpha++;
+                if(v > 255) v = 255; else if(v < 0) v = 0;
 
-       fprintf(fdest, "%c", (unsigned char)v);
- }
+                fprintf(fdest, "%c", (unsigned char)v);
           }
         }      /* for(i */
 
         fclose(fdest); return 0;
@@ -1907,18 +1911,21 @@ 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");
+        return 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, "_%d.pgm", 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, "%s", outfile);
 
@@ -1945,7 +1952,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
             for (i = 0; i < wr * hr; i++)
             {
                 v = *red + adjustR; ++red;
-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);
@@ -1953,7 +1960,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
                 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);
@@ -1964,10 +1971,10 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
         {
             for(i = 0; i < wr * hr; ++i)
             {
-       v = *red + adjustR; ++red;
-       if(v > 255) v = 255; else if(v < 0) v = 0;
+                v = *red + adjustR; ++red;
+                if(v > 255) v = 255; else if(v < 0) v = 0;
 
-        fprintf(fdest, "%c", (unsigned char)v);
+                fprintf(fdest, "%c", (unsigned char)v);
             }
         }
         fclose(fdest);
@@ -2154,7 +2161,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;