Merge pull request #1484 from mayeut/mandatory-inttypes-stdint
[openjpeg.git] / src / bin / jp2 / converttif.c
index 6714d69c72996c3814058027391dbfc341e8f514..e47a48d78a713cc12d6c4fde31be6b836bd1b376 100644 (file)
@@ -43,6 +43,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <limits.h>
+#include <inttypes.h>
 
 #ifndef OPJ_HAVE_LIBTIFF
 # error OPJ_HAVE_LIBTIFF_NOT_DEFINED
@@ -51,7 +52,6 @@
 #include <tiffio.h>
 #include "openjpeg.h"
 #include "convert.h"
-#include "opj_inttypes.h"
 
 /* -->> -->> -->> -->>
 
@@ -568,8 +568,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
 {
     TIFF *tif;
     tdata_t buf;
-    uint32 width, height;
-    uint16 bps, tiPhoto;
+    uint32_t width, height;
+    uint16_t bps, tiPhoto;
     int adjust, sgnd;
     int64_t strip_size, rowStride, TIFF_MAX;
     OPJ_UINT32 i, numcomps;
@@ -578,7 +578,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
     convert_32s_PXCX cvtPxToCx = NULL;
     convert_32sXXx_C1R cvt32sToTif = NULL;
 
-    bps = (uint16)image->comps[0].prec;
+    bps = (uint16_t)image->comps[0].prec;
     planes[0] = image->comps[0].data;
 
     numcomps = image->numcomps;
@@ -616,6 +616,12 @@ int imagetotif(opj_image_t * image, const char *outfile)
             break;
         }
         planes[i] = image->comps[i].data;
+        if (planes[i] == NULL) {
+            fprintf(stderr,
+                    "imagetotif: planes[%d] == NULL.\n", i);
+            fprintf(stderr, "\tAborting\n");
+            return 1;
+        }
     }
     if (i != numcomps) {
         fprintf(stderr,
@@ -688,12 +694,12 @@ int imagetotif(opj_image_t * image, const char *outfile)
     }
     sgnd = (int)image->comps[0].sgnd;
     adjust = sgnd ? (int)(1 << (image->comps[0].prec - 1)) : 0;
-    width   = (uint32)image->comps[0].w;
-    height  = (uint32)image->comps[0].h;
+    width   = (uint32_t)image->comps[0].w;
+    height  = (uint32_t)image->comps[0].h;
 
     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
-    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint16)numcomps);
+    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint16_t)numcomps);
     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
     TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
@@ -1241,7 +1247,8 @@ static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst,
  * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted
  * CINEMA                 : 12 bit precision
  */
-opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
+opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters,
+                        const unsigned int target_bitdepth)
 {
     int subsampling_dx = parameters->subsampling_dx;
     int subsampling_dy = parameters->subsampling_dy;
@@ -1253,8 +1260,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
     OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
     opj_image_cmptparm_t cmptparm[4]; /* RGBA */
     opj_image_t *image = NULL;
-    uint16 tiBps, tiPhoto, tiSf, tiSpp, tiPC;
-    uint32 tiWidth, tiHeight;
+    uint16_t tiBps, tiPhoto, tiSf, tiSpp, tiPC;
+    uint32_t tiWidth, tiHeight;
     OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
     convert_XXx32s_C1R cvtTifTo32s = NULL;
     convert_32s_CXPX cvtCxToPx = NULL;
@@ -1277,8 +1284,6 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
     TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
     TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
     TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
-    w = (int)tiWidth;
-    h = (int)tiHeight;
 
     if (tiSpp == 0 || tiSpp > 4) { /* should be 1 ... 4 */
         fprintf(stderr, "tiftoimage: Bad value for samples per pixel == %d.\n"
@@ -1383,7 +1388,6 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
 
     for (j = 0; j < numcomps; j++) {
         cmptparm[j].prec = tiBps;
-        cmptparm[j].bpp = tiBps;
         cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
         cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
         cmptparm[j].w = (OPJ_UINT32)w;
@@ -1501,6 +1505,10 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
             scale_component(&(image->comps[j]), 12);
         }
 
+    } else if ((target_bitdepth > 0) && (target_bitdepth != tiBps)) {
+        for (j = 0; j < numcomps; ++j) {
+            scale_component(&(image->comps[j]), target_bitdepth);
+        }
     }
     return image;