File: testimage.h

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (232 lines) | stat: -rw-r--r-- 7,363 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/testimage.h
// Purpose:     Unit test helpers for dealing with wxImage.
// Author:      Vadim Zeitlin
// Copyright:   (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_TESTS_TESTIMAGE_H_
#define _WX_TESTS_TESTIMAGE_H_

#include "wx/image.h"

namespace Catch
{
    template <>
    struct StringMaker<wxImage>
    {
        static std::string convert(const wxImage& image)
        {
            return wxString::Format("image of size %d*%d with%s alpha",
                                    image.GetWidth(),
                                    image.GetHeight(),
                                    image.HasAlpha() ? "" : "out")
                    .ToStdString();
        }
    };
}

class ImageRGBMatcher : public Catch::MatcherBase<wxImage>
{
public:
    ImageRGBMatcher(const wxImage& image, int tolerance, bool checkAlpha = false)
        : m_image(image)
        , m_tolerance(tolerance)
        , m_checkAlpha(checkAlpha)
    {
    }

    bool match(const wxImage& other) const wxOVERRIDE
    {
        if ( other.GetWidth() != m_image.GetWidth() )
            return false;

        if ( other.GetHeight() != m_image.GetHeight() )
            return false;

        if ( memcmp(other.GetData(), m_image.GetData(),
                    other.GetWidth()*other.GetHeight()*3) == 0 )
        {
            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 wxString dispAlphaValNull("--");
        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) : dispAlphaValNull) : wxString();
                wxString a2txt = dispAlphaVal ? (a2 != NULL ? wxString::Format("%02x", *a2) : dispAlphaValNull) : wxString();

                for ( int i = 0; i < 3; i++ )
                {
                    const unsigned char diff = d1[i] > d2[i] ? d1[i] - d2[i] : d2[i] - d1[i];
                    if ( diff > m_tolerance )
                    {
                        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.
                        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.
                        return false;
                    }
                }

                d1 += 3;
                d2 += 3;
                if ( a1 ) a1++;
                if ( a2 ) a2++;
            }
        }

        // 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 true;
    }

    std::string describe() const wxOVERRIDE
    {
        std::string desc = "doesn't have the same RGB data as " +
                                Catch::toString(m_image);

        if ( !m_diffDesc.empty() )
            desc += + ": " + m_diffDesc.ToStdString(wxConvUTF8);

        return desc;
    }

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, 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, false);
}

inline ImageRGBMatcher RGBASimilarTo(const wxImage& image, int tolerance)
{
    return ImageRGBMatcher(image, tolerance, true);
}

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_