Merge pull request #1244 from rouault/fix_pi_warnings
[openjpeg.git] / src / bin / jpwl / convert.c
index 7343528b689ea4d6d010417ab3d81923552c02d5..b7fb5b5c6bce4fe38b7084be3d37b72ea0068f07 100644 (file)
@@ -41,6 +41,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <limits.h>
 
 #ifdef OPJ_HAVE_LIBTIFF
 #include <tiffio.h>
@@ -444,7 +445,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
 {
     int width, height, bpp, x, y;
     opj_bool write_alpha;
-    int i, adjustR, adjustG, adjustB;
+    int i, adjustR, adjustG = 0, adjustB = 0;
     unsigned int alpha_channel;
     float r, g, b, a;
     unsigned char value;
@@ -464,6 +465,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
                 || (image->comps[0].prec != image->comps[i + 1].prec)) {
             fprintf(stderr,
                     "Unable to create a tga file with such J2K image charateristics.");
+            fclose(fdest);
             return 1;
         }
     }
@@ -477,6 +479,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
     /* Write TGA header  */
     bpp = write_alpha ? 32 : 24;
     if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
+        fclose(fdest);
         return 1;
     }
 
@@ -485,8 +488,10 @@ int imagetotga(opj_image_t * image, const char *outfile)
     scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
 
     adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
-    adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
-    adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+    if (image->numcomps >= 3) {
+        adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
+        adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+    }
 
     for (y = 0; y < height; y++) {
         unsigned int index = y * width;
@@ -507,6 +512,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
             res = fwrite(&value, 1, 1, fdest);
             if (res < 1) {
                 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+                fclose(fdest);
                 return 1;
             }
 
@@ -514,6 +520,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
             res = fwrite(&value, 1, 1, fdest);
             if (res < 1) {
                 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+                fclose(fdest);
                 return 1;
             }
 
@@ -521,6 +528,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
             res = fwrite(&value, 1, 1, fdest);
             if (res < 1) {
                 fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+                fclose(fdest);
                 return 1;
             }
 
@@ -530,12 +538,15 @@ int imagetotga(opj_image_t * image, const char *outfile)
                 res = fwrite(&value, 1, 1, fdest);
                 if (res < 1) {
                     fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+                    fclose(fdest);
                     return 1;
                 }
             }
         }
     }
 
+    fclose(fdest);
+
     return 0;
 }
 
@@ -737,6 +748,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
 
         if (fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H,
                   IN) != (3 * W + PAD) * H) {
+            fclose(IN);
             free(RGB);
             opj_image_destroy(image);
             fprintf(stderr,
@@ -828,6 +840,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
         RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
 
         if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
+            fclose(IN);
             free(table_R);
             free(table_G);
             free(table_B);
@@ -1348,10 +1361,11 @@ 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,
+    if (fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1,
                &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
         fprintf(stderr,
                 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
+        fclose(f);
         return NULL;
     }
 
@@ -1371,6 +1385,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
         bigendian = 0;
     } else {
         fprintf(stderr, "Bad pgx header, please check input file\n");
+        fclose(f);
         return NULL;
     }
 
@@ -1482,7 +1497,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
         const size_t olen = strlen(outfile);
         const size_t dotpos = olen - 4;
         const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
-        if (outfile[dotpos] != '.') {
+        if (olen < 4 || outfile[dotpos] != '.') {
             /* `pgx` was recognized but there is no dot at expected position */
             fprintf(stderr, "ERROR -> Impossible happen.");
             return 1;
@@ -1499,6 +1514,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
         fdest = fopen(name, "wb");
         if (!fdest) {
             fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
+            free(name);
             return 1;
         }
         /* don't need name anymore */
@@ -1859,6 +1875,15 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
         return NULL;
     }
 
+    /* This limitation could be removed by making sure to use size_t below */
+    if (header_info.height != 0 &&
+            header_info.width > INT_MAX / header_info.height) {
+        fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
+                header_info.width, header_info.height);
+        fclose(fp);
+        return NULL;
+    }
+
     format = header_info.format;
 
     switch (format) {