1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/affinetransform.cpp
3 // Purpose: Unit test for transformations implemented for wxAffineMatrix2D
4 // Author: Catalin Raceanu
6 // Copyright: (c) 2011 wxWidgets development team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
19 #include "wx/dcmemory.h"
20 #include "wx/affinematrix2d.h"
23 #include "testimage.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 class AffineTransformTestCase
: public CppUnit::TestCase
32 AffineTransformTestCase()
34 wxImage::AddHandler(new wxJPEGHandler
);
40 CPPUNIT_TEST_SUITE( AffineTransformTestCase
);
41 CPPUNIT_TEST( InvertMatrix
);
42 CPPUNIT_TEST( VMirrorAndTranslate
);
43 CPPUNIT_TEST( Rotate90Clockwise
);
44 CPPUNIT_TEST_SUITE_END();
47 void VMirrorAndTranslate();
48 void Rotate90Clockwise();
53 DECLARE_NO_COPY_CLASS(AffineTransformTestCase
)
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( AffineTransformTestCase
);
59 // also include in it's own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AffineTransformTestCase
, "AffineTransformTestCase" );
62 void AffineTransformTestCase::setUp()
64 m_imgOrig
.LoadFile("horse.jpg");
66 CPPUNIT_ASSERT( m_imgOrig
.IsOk() );
68 m_bmpOrig
= wxBitmap(m_imgOrig
);
71 void AffineTransformTestCase::InvertMatrix()
73 wxAffineMatrix2D matrix1
;
74 matrix1
.Set(wxMatrix2D(2, 1, 1, 1), wxPoint2DDouble(1, 1));
76 wxAffineMatrix2D
matrix2(matrix1
);
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
);
90 matrix2
.Concat(matrix1
);
91 CPPUNIT_ASSERT( matrix2
.IsIdentity() );
94 void AffineTransformTestCase::VMirrorAndTranslate()
96 wxBitmap
bmpUsingMatrix(m_bmpOrig
.GetWidth(), m_bmpOrig
.GetHeight());
98 // build the mirrored image using the transformation matrix
100 wxMemoryDC
dc(bmpUsingMatrix
);
102 if ( !dc
.CanUseTransformMatrix() )
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);
112 wxImage imgUsingMatrix
= bmpUsingMatrix
.ConvertToImage();
113 wxImage imgOrigVMirrored
= m_imgOrig
.Mirror(false);
115 CPPUNIT_ASSERT_EQUAL( imgUsingMatrix
, imgOrigVMirrored
);
118 void AffineTransformTestCase::Rotate90Clockwise()
120 wxBitmap
bmpUsingMatrix(m_bmpOrig
.GetHeight(), m_bmpOrig
.GetWidth());
122 // build the rotated image using the transformation matrix
124 wxMemoryDC
dc(bmpUsingMatrix
);
126 if ( !dc
.CanUseTransformMatrix() )
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);
136 wxImage imgUsingMatrix
= bmpUsingMatrix
.ConvertToImage();
137 wxImage imgOrigRotate90Clockwise
= m_imgOrig
.Rotate90(true);
139 CPPUNIT_ASSERT_EQUAL( imgUsingMatrix
, imgOrigRotate90Clockwise
);