Add a unit test for wxAffineMatrix2D class and its support in wxDC.
[wxWidgets.git] / tests / graphics / affinematrix.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/affinetransform.cpp
3 // Purpose: Unit test for transformations implemented for wxAffineMatrix2D
4 // Author: Catalin Raceanu
5 // Created: 2011-04-14
6 // Copyright: (c) 2011 wxWidgets development team
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/dcmemory.h"
20 #include "wx/affinematrix2d.h"
21 #include "wx/math.h"
22
23 #include "testimage.h"
24
25 // ----------------------------------------------------------------------------
26 // test class
27 // ----------------------------------------------------------------------------
28
29 class AffineTransformTestCase : public CppUnit::TestCase
30 {
31 public:
32 AffineTransformTestCase()
33 {
34 wxImage::AddHandler(new wxJPEGHandler);
35 }
36
37 virtual void setUp();
38
39 private:
40 CPPUNIT_TEST_SUITE( AffineTransformTestCase );
41 CPPUNIT_TEST( InvertMatrix );
42 CPPUNIT_TEST( VMirrorAndTranslate );
43 CPPUNIT_TEST( Rotate90Clockwise );
44 CPPUNIT_TEST_SUITE_END();
45
46 void InvertMatrix();
47 void VMirrorAndTranslate();
48 void Rotate90Clockwise();
49
50 wxImage m_imgOrig;
51 wxBitmap m_bmpOrig;
52
53 DECLARE_NO_COPY_CLASS(AffineTransformTestCase)
54 };
55
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( AffineTransformTestCase );
58
59 // also include in it's own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AffineTransformTestCase, "AffineTransformTestCase" );
61
62 void AffineTransformTestCase::setUp()
63 {
64 m_imgOrig.LoadFile("horse.jpg");
65
66 CPPUNIT_ASSERT( m_imgOrig.IsOk() );
67
68 m_bmpOrig = wxBitmap(m_imgOrig);
69 }
70
71 void AffineTransformTestCase::InvertMatrix()
72 {
73 wxAffineMatrix2D matrix1;
74 matrix1.Set(wxMatrix2D(2, 1, 1, 1), wxPoint2DDouble(1, 1));
75
76 wxAffineMatrix2D matrix2(matrix1);
77
78 matrix2.Invert();
79
80 wxMatrix2D m;
81 wxPoint2DDouble p;
82 matrix2.Get(&m, &p);
83 CPPUNIT_ASSERT_EQUAL( 1, (int)m.m_11 );
84 CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_12 );
85 CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_21 );
86 CPPUNIT_ASSERT_EQUAL( 2, (int)m.m_22 );
87 CPPUNIT_ASSERT_EQUAL( 0, (int)p.m_x );
88 CPPUNIT_ASSERT_EQUAL( -1, (int)p.m_y );
89
90 matrix2.Concat(matrix1);
91 CPPUNIT_ASSERT( matrix2.IsIdentity() );
92 }
93
94 void AffineTransformTestCase::VMirrorAndTranslate()
95 {
96 wxBitmap bmpUsingMatrix(m_bmpOrig.GetWidth(), m_bmpOrig.GetHeight());
97
98 // build the mirrored image using the transformation matrix
99 {
100 wxMemoryDC dc(bmpUsingMatrix);
101
102 if ( !dc.CanUseTransformMatrix() )
103 return;
104
105 wxAffineMatrix2D matrix;
106 matrix.Mirror(wxVERTICAL);
107 matrix.Translate(0, m_bmpOrig.GetHeight() - 1);
108 dc.SetTransformMatrix(matrix);
109 dc.DrawBitmap(m_bmpOrig, 0, 0);
110 }
111
112 wxImage imgUsingMatrix = bmpUsingMatrix.ConvertToImage();
113 wxImage imgOrigVMirrored = m_imgOrig.Mirror(false);
114
115 CPPUNIT_ASSERT_EQUAL( imgUsingMatrix, imgOrigVMirrored );
116 }
117
118 void AffineTransformTestCase::Rotate90Clockwise()
119 {
120 wxBitmap bmpUsingMatrix(m_bmpOrig.GetHeight(), m_bmpOrig.GetWidth());
121
122 // build the rotated image using the transformation matrix
123 {
124 wxMemoryDC dc(bmpUsingMatrix);
125
126 if ( !dc.CanUseTransformMatrix() )
127 return;
128
129 wxAffineMatrix2D matrix;
130 matrix.Rotate(-0.5 * M_PI);
131 matrix.Translate(m_bmpOrig.GetHeight(), 0);
132 dc.SetTransformMatrix(matrix);
133 dc.DrawBitmap(m_bmpOrig, 0, 0);
134 }
135
136 wxImage imgUsingMatrix = bmpUsingMatrix.ConvertToImage();
137 wxImage imgOrigRotate90Clockwise = m_imgOrig.Rotate90(true);
138
139 CPPUNIT_ASSERT_EQUAL( imgUsingMatrix, imgOrigRotate90Clockwise );
140 }