# HG changeset patch # User Matti Hamalainen # Date 1481225895 -7200 # Node ID d04d9e3a77d0cfb316d5d52ec03a01ba55024454 # Parent bfd933ef38b45d8ccb9355d35868324f8c9a8924 Add checking TIFF photometric interpretation tag data. diff -r bfd933ef38b4 -r d04d9e3a77d0 bpgenc.c --- 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;