]> git.saurik.com Git - wxWidgets.git/commitdiff
Expanded existing image saving test to also verify alpha content of saved images.
authorDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Tue, 28 Dec 2010 22:05:01 +0000 (22:05 +0000)
committerDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Tue, 28 Dec 2010 22:05:01 +0000 (22:05 +0000)
Compare the alpha data of saved images (where applicable, currently for PNG only) to a generated alpha channel. Refactored most of ImageTestCase.CompareSavedImage into (static) function CompareImage to easily compare with a 24-bit image and then a 32-bit one.

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

tests/image/image.cpp

index c71139795154b2e749300c06498bf5958c0ec2f1..2e72d8118cbe7a6a8f11c303e62042e9d6a5bb84 100644 (file)
@@ -870,62 +870,106 @@ void ImageTestCase::CompareLoadedImage()
 
 }
 
-void ImageTestCase::CompareSavedImage()
+static
+void CompareImage(const wxImageHandler& handler, const wxImage& expected)
 {
-    wxImage expected24("horse.png");
-    CPPUNIT_ASSERT( expected24.IsOk() );
+    bool testAlpha = expected.HasAlpha();
+    if (testAlpha && type != wxBITMAP_TYPE_PNG)
+    {
+        // don't test images with alpha if this handler doesn't support alpha
+        return;
+    }
 
-    const size_t dataLen = expected24.GetWidth() * expected24.GetHeight() * 3;
+    wxBitmapType type = handler.GetType();
+    if (type == wxBITMAP_TYPE_JPEG /* skip lossy JPEG */
+        || type == wxBITMAP_TYPE_TIF)
+    {
+        /*
+        TIFF is skipped because the memory stream can't be loaded. Libtiff
+        looks for a TIFF directory at offset 120008 while the memory
+        stream size is only 120008 bytes (when saving as a file
+        the file size is 120280 bytes).
+        */
+        return;
+    }
 
-    const wxList& list = wxImage::GetHandlers();
-    for ( wxList::compatibility_iterator node = list.GetFirst();
-        node; node = node->GetNext() )
+    wxMemoryOutputStream memOut;
+    if ( !expected.SaveFile(memOut, type) )
     {
-        wxImageHandler *handler = (wxImageHandler *) node->GetData();
+        // Unfortunately we can't know if the handler just doesn't support
+        // saving images, or if it failed to save.
+        return;
+    }
 
-        wxBitmapType type = handler->GetType();
-        if (type == wxBITMAP_TYPE_JPEG /* skip lossy JPEG */
-            || type == wxBITMAP_TYPE_TIF)
-        {
-            /*
-            TIFF is skipped because the memory stream can't be loaded. Libtiff
-            looks for a TIFF directory at offset 120008 while the memory
-            stream size is only 120008 bytes (when saving as a file
-            the file size is 120280 bytes).
-            */
-            continue;
-        }
+    if ( !memOut.GetSize() )
+    {
+        // A handler that does not support saving can return true during
+        // SaveFile, in that case the stream is empty.
+        return;
+    }
 
-        wxMemoryOutputStream memOut;
-        if ( !expected24.SaveFile(memOut, type) )
-        {
-            // Unfortunately we can't know if the handler just doesn't support
-            // saving images, or if it failed to save.
-            continue;
-        }
 
-        if ( !memOut.GetSize() )
-        {
-            // A handler that does not support saving can return true during
-            // SaveFile, in that case the stream is empty.
-            continue;
-        }
+    wxMemoryInputStream memIn(memOut);
+    CPPUNIT_ASSERT(memIn.IsOk());
 
-        wxMemoryInputStream memIn(memOut);
-        CPPUNIT_ASSERT(memIn.IsOk());
+    wxImage actual(memIn);
+    CPPUNIT_ASSERT(actual.IsOk());
 
-        wxImage actual24(memIn);
-        CPPUNIT_ASSERT(actual24.IsOk());
+    CPPUNIT_ASSERT( actual.GetSize() == expected.GetSize() );
 
-        CPPUNIT_ASSERT( actual24.GetSize() == expected24.GetSize() );
+    WX_ASSERT_MESSAGE
+    (
+        ("Compare test '%s' for saving failed", handler.GetExtension()),
 
-        WX_ASSERT_MESSAGE
-        (
-            ("Compare test '%s' for saving failed", handler->GetExtension()),
+        memcmp(actual.GetData(), expected.GetData(),
+            expected.GetWidth() * expected.GetHeight() * 3) == 0
+    );
 
-            memcmp(actual24.GetData(), expected24.GetData(),
-                dataLen) == 0
-        );
+    if (!testAlpha)
+    {
+        return;
+    }
+
+
+    CPPUNIT_ASSERT( actual.HasAlpha() );
+
+    WX_ASSERT_MESSAGE
+    (
+        ("Compare alpha test '%s' for saving failed", handler.GetExtension()),
+
+        memcmp(actual.GetAlpha(), expected.GetAlpha(),
+            expected.GetWidth() * expected.GetHeight()) == 0
+    );
+}
+
+void ImageTestCase::CompareSavedImage()
+{
+    wxImage expected24("horse.png");
+    CPPUNIT_ASSERT( expected24.IsOk() );
+    CPPUNIT_ASSERT( !expected24.HasAlpha() );
+
+    // Create an image with alpha based on the loaded image
+    wxImage expected32(expected24);
+    expected32.SetAlpha();
+
+    int width = expected32.GetWidth();
+    int height = expected32.GetHeight();
+    for (int y = 0; y < height; ++y)
+    {
+        for (int x = 0; x < width; ++x)
+        {
+            expected32.SetAlpha(x, y, (x*y) & wxIMAGE_ALPHA_OPAQUE);
+        }
+    }
+
+    const wxList& list = wxImage::GetHandlers();
+    for ( wxList::compatibility_iterator node = list.GetFirst();
+        node; node = node->GetNext() )
+    {
+        wxImageHandler *handler = (wxImageHandler *) node->GetData();
+
+        CompareImage(*handler, expected24);
+        CompareImage(*handler, expected32);
     }
 }