KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wximage_test_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "wximage_test_utils.h"
21
22#include "color4d_test_utils.h"
23
24
25namespace KI_TEST
26{
27
28bool IsImagePixelOfColor( const wxImage& aImage, int aX, int aY, const KIGFX::COLOR4D& aColor )
29{
30 const wxSize imageSize = aImage.GetSize();
31
32 if( imageSize.x < aX || imageSize.y < aY )
33 {
34 BOOST_TEST_INFO( "Pixel (" << aX << ", " << aY << "is not in image of size (" << imageSize.x
35 << ", " << imageSize.y << ")" );
36 return false;
37 }
38
39 const int r = aImage.GetRed( aX, aY );
40 const int g = aImage.GetGreen( aX, aY );
41 const int b = aImage.GetBlue( aX, aY );
42
43 const int a = aImage.HasAlpha() ? aImage.GetAlpha( aX, aY ) : 255;
44
45 if( !KI_TEST::IsColorNearHex( aColor, r, g, b, a ) )
46 {
47 BOOST_TEST_INFO( "Colour doesn't match: got rgba(" << r << ", " << g << ", " << b << ", "
48 << a << "), expected " << aColor );
49 return false;
50 }
51
52 return true;
53}
54
55
56bool ImagesHaveSamePixels( const wxImage& aImgA, const wxImage& aImgB )
57{
58 if( aImgA.GetSize() != aImgB.GetSize() )
59 {
60 BOOST_TEST_INFO( "Image sizes differ: " << aImgA.GetSize().GetWidth() << "x"
61 << aImgA.GetSize().GetHeight() << " vs "
62 << aImgB.GetSize().GetWidth() << "x"
63 << aImgB.GetSize().GetHeight() );
64 return false;
65 }
66
67 for( int y = 0; y < aImgA.GetHeight(); ++y )
68 {
69 for( int x = 0; x < aImgA.GetWidth(); ++x )
70 {
71 const int rA = aImgA.GetRed( x, y );
72 const int gA = aImgA.GetGreen( x, y );
73 const int bA = aImgA.GetBlue( x, y );
74
75 const int rB = aImgB.GetRed( x, y );
76 const int gB = aImgB.GetGreen( x, y );
77 const int bB = aImgB.GetBlue( x, y );
78
79 if( rA != rB || gA != gB || bA != bB )
80 {
81 BOOST_TEST_INFO( "Pixel (" << x << ", " << y << ") differs: "
82 << "A(" << rA << ", " << gA << ", " << bA << ") "
83 << "B(" << rB << ", " << gB << ", " << bB << ")" );
84 return false;
85 }
86 }
87 }
88
89 return true;
90}
91
92} // namespace KI_TEST
93
94
95std::ostream& boost_test_print_type( std::ostream& os, wxImage const& aImage )
96{
97 const wxSize size = aImage.GetSize();
98 os << "wxImage[" << size.x << "x" << size.y << "]";
99 return os;
100}
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Test utilities for COLOUR4D objects.
bool IsColorNearHex(const KIGFX::COLOR4D &aCol, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Checks if a COLOR4D is close enough to a given RGB char value.
bool IsImagePixelOfColor(const wxImage &aImage, int aX, int aY, const KIGFX::COLOR4D &aColor)
Predicate to check an image pixel matches color and alpha.
bool ImagesHaveSamePixels(const wxImage &aImgA, const wxImage &aImgB)
Check if an image is identical to another image, pixel by pixel.
BOOST_TEST_INFO("Two-port Series .op current = "<< iDevice)
std::ostream & boost_test_print_type(std::ostream &os, wxImage const &aImage)