changeset 10:d04d9e3a77d0

Add checking TIFF photometric interpretation tag data.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 Dec 2016 21:38:15 +0200
parents bfd933ef38b4
children e70eb0e6acd5
files bpgenc.c
diffstat 1 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/bpgenc.c	Wed Dec 07 15:22:56 2016 +0200
+++ b/bpgenc.c	Thu Dec 08 21:38:15 2016 +0200
@@ -955,9 +955,9 @@
     BPGImageFormatEnum img_format;
     uint8_t *buf = NULL, *tmp_buf = NULL;
     uint32_t img_width, img_height, tmp_len;
-    int img_alpha, img_bpp;
+    int img_alpha, err_spp = 0;
     size_t img_linesize;
-    uint16_t img_depth, img_spp, img_pconfig;
+    uint16_t img_depth, img_spp, img_pconfig, img_pmetric;
     ColorConvertState cvt;
     RGBConvertFunc *convert_func;
 
@@ -978,10 +978,33 @@
     }
 
     TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &img_pconfig);
+    TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img_pmetric);
 
     // Check basics
-    if ((img_depth != 8 && img_depth != 16) || img_spp > 4)
+    switch (img_pmetric)
+    {
+        case PHOTOMETRIC_RGB:
+            img_format = BPG_FORMAT_444;
+            if (img_spp < 3)
+                err_spp = 1;
+            break;
+
+        default:
+            fprintf(stderr, "TIFF file has unsupported photometric interpretation %d!\n", img_pmetric);
+            goto err;
+    }
+
+    if (err_spp)
+    {
+            fprintf(stderr, "TIFF file has unsupported samples per pixel %d for format %d.\n", img_spp, img_pmetric);
+            goto err;
+    }
+
+    if (img_depth != 8 && img_depth != 16)
+    {
+        fprintf(stderr, "TIFF file has unsupported depth %d (not 8 or 16)!\n", img_depth);
         goto err;
+    }
 
     img_linesize = TIFFScanlineSize(tif);
     img_format = BPG_FORMAT_444;