Changes in converttif.c for PPC64
[openjpeg.git] / src / bin / jp2 / convert.c
index a540128fca56f88c7efdbcf1de1739cd4f43cfd6..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) {
@@ -1890,6 +1901,22 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
     return image;
 }/* pnmtoimage() */
 
+static int are_comps_similar(opj_image_t * image)
+{
+    unsigned int i;
+    for (i = 1; i < image->numcomps; i++) {
+        if (image->comps[0].dx != image->comps[i].dx ||
+                image->comps[0].dy != image->comps[i].dy ||
+                (i <= 2 &&
+                 (image->comps[0].prec != image->comps[i].prec ||
+                  image->comps[0].sgnd != image->comps[i].sgnd))) {
+            return OPJ_FALSE;
+        }
+    }
+    return OPJ_TRUE;
+}
+
+
 int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
 {
     int *red, *green, *blue, *alpha;
@@ -1925,16 +1952,8 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
         ncomp = 1;
     }
 
-    if ((force_split == 0) &&
-            (ncomp == 2 /* GRAYA */
-             || (ncomp > 2 /* RGB, RGBA */
-                 && image->comps[0].dx == image->comps[1].dx
-                 && image->comps[1].dx == image->comps[2].dx
-                 && image->comps[0].dy == image->comps[1].dy
-                 && image->comps[1].dy == image->comps[2].dy
-                 && image->comps[0].prec == image->comps[1].prec
-                 && image->comps[1].prec == image->comps[2].prec
-                ))) {
+    if ((force_split == 0) && ncomp >= 2 &&
+            are_comps_similar(image)) {
         fdest = fopen(outfile, "wb");
 
         if (!fdest) {
@@ -2325,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;
@@ -2337,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);
@@ -2448,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);