Add to ImageRGBMatcher ability to check alpha values in wxImage tests
This commit is contained in:
@@ -30,9 +30,10 @@ namespace Catch
|
||||
class ImageRGBMatcher : public Catch::MatcherBase<wxImage>
|
||||
{
|
||||
public:
|
||||
ImageRGBMatcher(const wxImage& image, int tolerance)
|
||||
ImageRGBMatcher(const wxImage& image, int tolerance, bool checkAlpha = false)
|
||||
: m_image(image)
|
||||
, m_tolerance(tolerance)
|
||||
, m_checkAlpha(checkAlpha)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -46,14 +47,34 @@ public:
|
||||
|
||||
if ( memcmp(other.GetData(), m_image.GetData(),
|
||||
other.GetWidth()*other.GetHeight()*3) == 0 )
|
||||
return true;
|
||||
{
|
||||
if ( m_checkAlpha )
|
||||
{
|
||||
if ( !other.HasAlpha() && !m_image.HasAlpha() )
|
||||
return true;
|
||||
|
||||
if ( other.HasAlpha() && m_image.HasAlpha() &&
|
||||
memcmp(other.GetAlpha(), m_image.GetAlpha(), other.GetWidth() * other.GetHeight()) == 0 )
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const unsigned char* d1 = m_image.GetData();
|
||||
const unsigned char* d2 = other.GetData();
|
||||
const unsigned char* a1 = m_image.GetAlpha();
|
||||
const unsigned char* a2 = other.GetAlpha();
|
||||
bool dispAlphaVal = a1 || a2;
|
||||
for ( int y = 0; y < m_image.GetHeight(); ++y )
|
||||
{
|
||||
for ( int x = 0; x < m_image.GetWidth(); ++x )
|
||||
{
|
||||
wxString a1txt = dispAlphaVal ? (a1 != NULL ? wxString::Format("%02x", *a1) : "--") : "";
|
||||
wxString a2txt = dispAlphaVal ? (a2 != NULL ? wxString::Format("%02x", *a2) : "--") : "";
|
||||
|
||||
for ( int i = 0; i < 3; i++ )
|
||||
{
|
||||
const unsigned char diff = d1[i] > d2[i] ? d1[i] - d2[i] : d2[i] - d1[i];
|
||||
@@ -62,9 +83,43 @@ public:
|
||||
m_diffDesc.Printf
|
||||
(
|
||||
"first mismatch is at (%d, %d) which "
|
||||
"has value 0x%02x%02x%02x instead of the "
|
||||
"expected 0x%02x%02x%02x",
|
||||
x, y, d2[0], d2[1], d2[2], d1[0], d1[1], d1[2]
|
||||
"has value 0x%02x%02x%02x%s instead of the "
|
||||
"expected 0x%02x%02x%02x%s",
|
||||
x, y, d2[0], d2[1], d2[2], a2txt, d1[0], d1[1], d1[2], a1txt
|
||||
);
|
||||
|
||||
// Don't show all mismatches, there may be too many of them.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( m_checkAlpha )
|
||||
{
|
||||
if ( a1 && a2 )
|
||||
{
|
||||
const unsigned char diff = *a1 > *a2 ? *a1 - *a2 : *a2 - *a1;
|
||||
if ( diff > m_tolerance )
|
||||
{
|
||||
m_diffDesc.Printf
|
||||
(
|
||||
"first mismatch is at (%d, %d) which "
|
||||
"has value 0x%02x%02x%02x%02x instead of the "
|
||||
"expected 0x%02x%02x%02x%02x",
|
||||
x, y, d2[0], d2[1], d2[2], *a2, d1[0], d1[1], d1[2], *a1
|
||||
);
|
||||
|
||||
// Don't show all mismatches, there may be too many of them.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ( a1txt != a2txt )
|
||||
{
|
||||
m_diffDesc.Printf
|
||||
(
|
||||
"first mismatch is at (%d, %d) which "
|
||||
"has value 0x%02x%02x%02x%s instead of the "
|
||||
"expected 0x%02x%02x%02x%s",
|
||||
x, y, d2[0], d2[1], d2[2], a2txt, d1[0], d1[1], d1[2], a1txt
|
||||
);
|
||||
|
||||
// Don't show all mismatches, there may be too many of them.
|
||||
@@ -74,6 +129,8 @@ public:
|
||||
|
||||
d1 += 3;
|
||||
d2 += 3;
|
||||
if ( a1 ) a1++;
|
||||
if ( a2 ) a2++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,18 +155,24 @@ public:
|
||||
private:
|
||||
const wxImage m_image;
|
||||
const int m_tolerance;
|
||||
const bool m_checkAlpha;
|
||||
mutable wxString m_diffDesc;
|
||||
};
|
||||
|
||||
inline ImageRGBMatcher RGBSameAs(const wxImage& image)
|
||||
{
|
||||
return ImageRGBMatcher(image, 0);
|
||||
return ImageRGBMatcher(image, 0, false);
|
||||
}
|
||||
|
||||
inline ImageRGBMatcher RGBASameAs(const wxImage& image)
|
||||
{
|
||||
return ImageRGBMatcher(image, 0, true);
|
||||
}
|
||||
|
||||
// Allows small differences (within given tolerance) for r, g, and b values.
|
||||
inline ImageRGBMatcher RGBSimilarTo(const wxImage& image, int tolerance)
|
||||
{
|
||||
return ImageRGBMatcher(image, tolerance);
|
||||
return ImageRGBMatcher(image, tolerance, false);
|
||||
}
|
||||
|
||||
class ImageAlphaMatcher : public Catch::MatcherBase<wxImage>
|
||||
|
Reference in New Issue
Block a user