KiCad PCB EDA Suite
test_bitmap_base.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 (C) 2019 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, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
30 
31 // Code under test
32 #include <bitmap_base.h>
33 
34 #include "wximage_test_utils.h"
35 
36 #include <wx/mstream.h>
37 
38 
39 // Dummy PNG image 8x8, 4 tiles:
40 //
41 // green, black,
42 // red, blue
43 // ( the black tile is a a circle, otherwise older wx's seem to crash)
44 static const std::vector<unsigned char> png_data_4tile = { //
45  0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
46  0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x08, 0x06, 0x00, 0x00, 0x00, 0xC4, 0x0F, 0xBE,
47  0x8B, 0x00, 0x00, 0x00, 0x04, 0x73, 0x42, 0x49, 0x54, 0x08, 0x08, 0x08, 0x08, 0x7C, 0x08, 0x64,
48  0x88, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0E, 0xC4, 0x00, 0x00, 0x0E,
49  0xC4, 0x01, 0x95, 0x2B, 0x0E, 0x1B, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6F,
50  0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x00, 0x77, 0x77, 0x77, 0x2E, 0x69, 0x6E, 0x6B, 0x73, 0x63,
51  0x61, 0x70, 0x65, 0x2E, 0x6F, 0x72, 0x67, 0x9B, 0xEE, 0x3C, 0x1A, 0x00, 0x00, 0x00, 0x40, 0x49,
52  0x44, 0x41, 0x54, 0x18, 0x95, 0xA5, 0xCD, 0x4B, 0x0A, 0xC0, 0x20, 0x14, 0x43, 0xD1, 0x63, 0x77,
53  0xA7, 0xDD, 0xFF, 0x06, 0xD4, 0x7D, 0x3C, 0x07, 0x95, 0x42, 0x3F, 0xE0, 0xC0, 0x84, 0x0C, 0x02,
54  0xE1, 0x86, 0x78, 0x99, 0x13, 0x1D, 0x0D, 0xC5, 0xCF, 0xA0, 0x21, 0x66, 0xEA, 0x61, 0xA9, 0x2F,
55  0xA1, 0x4C, 0x4A, 0x45, 0x4E, 0x71, 0xA1, 0x6E, 0xA5, 0x67, 0xB5, 0xBC, 0xD8, 0x1F, 0x0C, 0xA7,
56  0xFD, 0x23, 0x67, 0x70, 0xDA, 0x89, 0xA2, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
57  0x42, 0x60, 0x82
58 };
59 
61 static const wxSize size_4tile{ 8, 8 };
62 
63 static const KIGFX::COLOR4D col_red{ 1.0, 0.0, 0.0, 1.0 };
64 static const KIGFX::COLOR4D col_green{ 0.0, 1.0, 0.0, 1.0 };
65 static const KIGFX::COLOR4D col_blue{ 0.0, 0.0, 1.0, 1.0 };
66 static const KIGFX::COLOR4D col_black{ 0.0, 0.0, 0.0, 1.0 };
67 
68 
70 {
71 public:
73  {
74  wxMemoryInputStream mis( png_data_4tile.data(), png_data_4tile.size() );
75 
76  bool ok = m_4tile.ReadImageFile( mis );
77 
78  // this is needed for most tests and can fail if the image handlers
79  // are not initialised (e.g. with wxInitAllImageHandlers)
80  BOOST_REQUIRE( ok );
81 
82  // use an easier scale for test purposes
83  m_4tile.SetPixelSizeIu( 2.0 );
84  m_4tile.SetScale( 5.0 );
85  }
86 
87  /*
88  * Simple image of 4 coloured tiles
89  */
91 };
92 
93 
97 BOOST_FIXTURE_TEST_SUITE( BitmapBase, TEST_BITMAP_BASE_FIXTURE )
98 
99 
100 
104 {
106  // Const to test we can get this data from a const object
107  const BITMAP_BASE& cempty = empty;
108 
109  BOOST_CHECK_EQUAL( cempty.GetImageData(), nullptr );
110  BOOST_CHECK_EQUAL( cempty.GetPPI(), 300 );
111  BOOST_CHECK_EQUAL( cempty.GetScale(), 1.0 );
112  BOOST_CHECK_EQUAL( cempty.GetPixelSizeIu(), 254000.0 / 300.0 );
113 
114  // can do this on an empty image
115  empty.Rotate( true );
116  empty.Mirror( true );
117 }
118 
119 
124 {
126  BITMAP_BASE copied = empty;
127 
128  // should still have nothing in it
129  BOOST_CHECK_EQUAL( copied.GetImageData(), nullptr );
130 }
131 
132 
134 {
135  int m_x;
136  int m_y;
138 };
139 
140 
144 BOOST_AUTO_TEST_CASE( BasicProps )
145 {
146  // make sure we can do all this to a const img
147  const BITMAP_BASE& img = m_4tile;
148 
149  // have "some" image data
150  BOOST_REQUIRE_NE( img.GetImageData(), nullptr );
151 
152  // still default props here
153  BOOST_CHECK_EQUAL( img.GetPPI(), 300 );
154  BOOST_CHECK_EQUAL( img.GetScale(), 5.0 );
155 
156  // we changed this, make sure it's right
157  BOOST_CHECK_EQUAL( img.GetPixelSizeIu(), 2.0 );
158 
160  BOOST_CHECK( img.GetSize() == size_4tile * 10 );
161 
162  const EDA_RECT bb = img.GetBoundingBox();
163  BOOST_CHECK( bb.GetPosition() == wxPoint( -40, -40 ) );
164  BOOST_CHECK( bb.GetEnd() == wxPoint( 40, 40 ) );
165 }
166 
167 
171 BOOST_AUTO_TEST_CASE( BasicImage )
172 {
173  const wxImage* img_data = m_4tile.GetImageData();
174  BOOST_REQUIRE_NE( img_data, nullptr );
175 
176  // green, black,
177  // red, blue
178  const std::vector<TEST_PIXEL_CASE> exp_pixels = {
179  { 1, 1, col_green },
180  { 6, 1, col_black },
181  { 1, 6, col_red },
182  { 6, 6, col_blue },
183  };
184 
185  for( const auto& c : exp_pixels )
186  {
187  BOOST_CHECK_PREDICATE(
188  KI_TEST::IsImagePixelOfColor, ( *img_data )( c.m_x )( c.m_y )( c.m_color ) );
189  }
190 }
191 
195 BOOST_AUTO_TEST_CASE( RotateImage )
196 {
197  // Note the parameter name is wrong here, true is clockwise
198  m_4tile.Rotate( false );
199 
200  const wxImage* img_data = m_4tile.GetImageData();
201  BOOST_REQUIRE_NE( img_data, nullptr );
202 
203  // black, blue,
204  // green, red
205  const std::vector<TEST_PIXEL_CASE> exp_pixels = {
206  { 1, 1, col_black },
207  { 6, 1, col_blue },
208  { 1, 6, col_green },
209  { 6, 6, col_red },
210  };
211 
212  for( const auto& c : exp_pixels )
213  {
214  BOOST_CHECK_PREDICATE(
215  KI_TEST::IsImagePixelOfColor, ( *img_data )( c.m_x )( c.m_y )( c.m_color ) );
216  }
217 }
218 
222 BOOST_AUTO_TEST_CASE( MirrorImage )
223 {
224  m_4tile.Mirror( true );
225 
226  const wxImage* img_data = m_4tile.GetImageData();
227  BOOST_REQUIRE_NE( img_data, nullptr );
228 
229  // red, blue
230  // green, black
231  const std::vector<TEST_PIXEL_CASE> exp_pixels = {
232  { 1, 1, col_red },
233  { 6, 1, col_blue },
234  { 1, 6, col_green },
235  { 6, 6, col_black },
236  };
237 
238  for( const auto& c : exp_pixels )
239  {
240  BOOST_CHECK_PREDICATE(
241  KI_TEST::IsImagePixelOfColor, ( *img_data )( c.m_x )( c.m_y )( c.m_color ) );
242  }
243 }
244 
245 BOOST_AUTO_TEST_SUITE_END()
void SetScale(double aScale)
Definition: bitmap_base.h:80
double GetScale() const
Definition: bitmap_base.h:79
wxImage * GetImageData()
Definition: bitmap_base.h:70
static const wxSize size_4tile
static const KIGFX::COLOR4D col_red
const EDA_RECT GetBoundingBox() const
Return the orthogonal, bounding box of this object for display purposes.
BOOST_CHECK(v2.Cross(v1)==1)
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
wxSize GetSize() const
static const std::vector< unsigned char > png_data_4tile
4tile is an 8x8 image
BOOST_AUTO_TEST_CASE(Empty)
Declare the test suite.
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:68
static const KIGFX::COLOR4D col_green
double GetPixelSizeIu() const
Definition: bitmap_base.h:67
bool IsImagePixelOfColor(const wxImage &aImage, int aX, int aY, const KIGFX::COLOR4D &aColor)
Predicate to check an image pixel matches color and alpha.
static const KIGFX::COLOR4D col_blue
Handle the component boundary box.
Definition: eda_rect.h:42
wxSize GetSizePixels() const
Definition: bitmap_base.h:124
static bool empty(const wxTextEntryBase *aCtrl)
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
Definition: bitmap_base.cpp:95
static const KIGFX::COLOR4D col_black
KIGFX::COLOR4D m_color
int GetPPI() const
Definition: bitmap_base.h:135
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103