Changes in converttif.c for PPC64
[openjpeg.git] / src / bin / jp2 / convert.c
index b3eb85816a229dedca9f56c5e8017de101163ca3..e2e16027786e5c8f257398dd0060c700c93d4ba4 100644 (file)
@@ -41,6 +41,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <limits.h>
 
 #include "openjpeg.h"
 #include "convert.h"
@@ -958,10 +959,11 @@ int imagetotga(opj_image_t * image, const char *outfile)
     for (i = 0; i < image->numcomps - 1; i++) {
         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)) {
+                || (image->comps[0].prec != image->comps[i + 1].prec)
+                || (image->comps[0].sgnd != image->comps[i + 1].sgnd)) {
             fclose(fdest);
             fprintf(stderr,
-                    "Unable to create a tga file with such J2K image charateristics.");
+                    "Unable to create a tga file with such J2K image charateristics.\n");
             return 1;
         }
     }
@@ -1731,6 +1733,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) {
@@ -2333,7 +2344,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile,
 {
     FILE *rawFile = NULL;
     size_t res;
-    unsigned int compno;
+    unsigned int compno, numcomps;
     int w, h, fails;
     int line, row, curr, mask;
     int *ptr;
@@ -2345,6 +2356,33 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile,
         return 1;
     }
 
+    numcomps = image->numcomps;
+
+    if (numcomps > 4) {
+        numcomps = 4;
+    }
+
+    for (compno = 1; compno < numcomps; ++compno) {
+        if (image->comps[0].dx != image->comps[compno].dx) {
+            break;
+        }
+        if (image->comps[0].dy != image->comps[compno].dy) {
+            break;
+        }
+        if (image->comps[0].prec != image->comps[compno].prec) {
+            break;
+        }
+        if (image->comps[0].sgnd != image->comps[compno].sgnd) {
+            break;
+        }
+    }
+    if (compno != numcomps) {
+        fprintf(stderr,
+                "imagetoraw_common: All components shall have the same subsampling, same bit depth, same sign.\n");
+        fprintf(stderr, "\tAborting\n");
+        return 1;
+    }
+
     rawFile = fopen(outfile, "wb");
     if (!rawFile) {
         fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
@@ -2456,7 +2494,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile,
                 }
             }
         } else if (image->comps[compno].prec <= 32) {
-            fprintf(stderr, "More than 16 bits per component no handled yet\n");
+            fprintf(stderr, "More than 16 bits per component not handled yet\n");
             goto fin;
         } else {
             fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);