Add alpha blending for wxImage::Paste

Add test cases for wxImage::Paste.

Closes #12458.

Co-Authored-By: Rachel Mark <kramdar@gmail.com>
This commit is contained in:
Eric Raijmakers
2020-09-23 11:56:58 +02:00
committed by Vadim Zeitlin
parent 8ae9987a29
commit 6e8da8641c
13 changed files with 490 additions and 26 deletions

View File

@@ -30,8 +30,9 @@ namespace Catch
class ImageRGBMatcher : public Catch::MatcherBase<wxImage>
{
public:
ImageRGBMatcher(const wxImage& image)
ImageRGBMatcher(const wxImage& image, int tolerance)
: m_image(image)
, m_tolerance(tolerance)
{
}
@@ -53,7 +54,8 @@ public:
{
for ( int y = 0; y < m_image.GetHeight(); ++y )
{
if ( *d1 != *d2 )
const unsigned char diff = *d1 > * d2 ? *d1 - *d2 : *d2 - *d1;
if (diff > m_tolerance)
{
m_diffDesc.Printf
(
@@ -72,11 +74,11 @@ public:
}
}
// We should never get here as we know that the images are different
// and so should have returned from inside the loop above.
wxFAIL_MSG("unreachable");
// We can only get here when the images are different AND we've not exited the
// method from the loop. That implies the tolerance must have caused this.
wxASSERT_MSG(m_tolerance > 0, "Unreachable without tolerance");
return false;
return true;
}
std::string describe() const wxOVERRIDE
@@ -92,12 +94,67 @@ public:
private:
const wxImage m_image;
const int m_tolerance;
mutable wxString m_diffDesc;
};
inline ImageRGBMatcher RGBSameAs(const wxImage& image)
{
return ImageRGBMatcher(image);
return ImageRGBMatcher(image, 0);
}
// Allows small differences (within given tolerance) for r, g, and b values.
inline ImageRGBMatcher RGBSimilarTo(const wxImage& image, int tolerance)
{
return ImageRGBMatcher(image, tolerance);
}
class ImageAlphaMatcher : public Catch::MatcherBase<wxImage>
{
public:
ImageAlphaMatcher(unsigned char alpha)
: m_alpha(alpha)
{
}
bool match(const wxImage& other) const wxOVERRIDE
{
if (!other.HasAlpha())
{
m_diffDesc = "no alpha data";
return false;
}
unsigned char center_alpha =
*(other.GetAlpha() + (other.GetWidth() / 2) + (other.GetHeight() / 2 * other.GetWidth()));
if (m_alpha != center_alpha)
{
m_diffDesc.Printf("got alpha %u", center_alpha);
return false;
}
return true;
}
std::string describe() const wxOVERRIDE
{
std::string desc;
if (!m_diffDesc.empty())
desc = m_diffDesc.ToStdString(wxConvUTF8);
return desc;
}
private:
const unsigned char m_alpha;
mutable wxString m_diffDesc;
};
inline ImageAlphaMatcher CenterAlphaPixelEquals(unsigned char alpha)
{
return ImageAlphaMatcher(alpha);
}
#endif // _WX_TESTS_TESTIMAGE_H_