]> git.saurik.com Git - wxWidgets.git/commitdiff
Added support for loading black and white TIFF images with alpha.
authorDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Sun, 28 Aug 2011 22:17:04 +0000 (22:17 +0000)
committerDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Sun, 28 Aug 2011 22:17:04 +0000 (22:17 +0000)
As TIFFReadRGBAImage[Oriented] can't deal with all images make use of TIFFReadScanline to decode per scanline. Currently only the case of a black and white image with alpha (for a total of 2 bits per pixel) is handled.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68945 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/imagtiff.cpp

index b643ff24e30d14e979ec725db06d2c7f9776133d..1083157434a6265d9cb6a41165567da2257c583c 100644 (file)
@@ -394,7 +394,54 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos
     if ( hasAlpha )
         image->SetAlpha();
 
-    if (!TIFFReadRGBAImageOriented( tif, w, h, raster, ORIENTATION_TOPLEFT, 0 ))
+    uint16 planarConfig = PLANARCONFIG_CONTIG;
+    (void) TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &planarConfig);
+
+    bool ok = true;
+    char msg[1024] = "";
+    if ( !TIFFRGBAImageOK(tif, msg)
+        && planarConfig == PLANARCONFIG_CONTIG
+        && samplesPerPixel == 2 && extraSamples == 1)
+    {
+        unsigned char *buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
+        uint32 pos = 0;
+        const int minValue = (photometric == PHOTOMETRIC_MINISWHITE) ? 255 : 0;
+        const int maxValue = 255 - minValue;
+
+        /*
+        Decode to ABGR format as that is what the code, that converts to
+        wxImage, later on expects (normally TIFFReadRGBAImageOriented is
+        used to decode which uses an ABGR layout).
+        */
+        for (uint32 y = 0; y < h; ++y)
+        {
+            if (TIFFReadScanline(tif, buf, y, 0) != 1)
+            {
+                ok = false;
+                break;
+            }
+
+            for (uint32 x = 0; x < w; ++x)
+            {
+                int mask = buf[x*2/8] << ((x*2)%8);
+
+                uint8 val = mask & 128 ? maxValue : minValue;
+                raster[pos] = val + (val << 8) + (val << 16)
+                    + ((mask & 64 ? maxValue : minValue) << 24);
+                pos++;
+            }
+        }
+
+        _TIFFfree(buf);
+    }
+    else
+    {
+        ok = TIFFReadRGBAImageOriented( tif, w, h, raster,
+            ORIENTATION_TOPLEFT, 0 ) != 0;
+    }
+
+
+    if (!ok)
     {
         if (verbose)
         {