]> git.saurik.com Git - wxWidgets.git/commitdiff
Improved detection of alpha channels in TIFF images.
authorDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Wed, 19 Jan 2011 13:47:18 +0000 (13:47 +0000)
committerDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Wed, 19 Jan 2011 13:47:18 +0000 (13:47 +0000)
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

index 5d8476daec91218bb557fb0a0082462a5af75270..4994a0ae2450acd16b95d2f03b6d0d76f81b4993 100644 (file)
@@ -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