From 3d59540f8778262e3d9dd7d3bcdaf8da37f5fac0 Mon Sep 17 00:00:00 2001 From: Dimitri Schoolwerth Date: Wed, 19 Jan 2011 13:47:18 +0000 Subject: [PATCH] Improved detection of alpha channels in TIFF images. Some TIFF images are not properly formed, for example having an extra channel marked as being unspecified data while they should be treated as being an alpha channel. Detect some of those cases so that these TIFF images now will have alpha. Applied patch by gmeeker. Closes #12874. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66717 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/imagtiff.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/common/imagtiff.cpp b/src/common/imagtiff.cpp index 5d8476daec..4994a0ae24 100644 --- a/src/common/imagtiff.cpp +++ b/src/common/imagtiff.cpp @@ -296,13 +296,23 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w ); TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h ); + uint16 photometric; + uint16 samplesPerPixel; uint16 extraSamples; uint16* samplesInfo; + TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel); TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extraSamples, &samplesInfo); - const bool hasAlpha = (extraSamples == 1 && - (samplesInfo[0] == EXTRASAMPLE_ASSOCALPHA || - samplesInfo[0] == EXTRASAMPLE_UNASSALPHA)); + if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) + { + photometric = PHOTOMETRIC_MINISWHITE; + } + const bool hasAlpha = (extraSamples >= 1 + && ((samplesInfo[0] == EXTRASAMPLE_UNSPECIFIED && samplesPerPixel > 3) + || samplesInfo[0] == EXTRASAMPLE_ASSOCALPHA + || samplesInfo[0] == EXTRASAMPLE_UNASSALPHA)) + || (extraSamples == 0 && samplesPerPixel == 4 + && photometric == PHOTOMETRIC_RGB); // guard against integer overflow during multiplication which could result // in allocating a too small buffer and then overflowing it -- 2.45.2