1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/image/image.cpp
3 // Purpose: Test wxImage
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2009 Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
29 #include "wx/mstream.h"
30 #include "wx/zstream.h"
31 #include "wx/wfstream.h"
38 { "horse.ico", wxBITMAP_TYPE_ICO
},
39 { "horse.xpm", wxBITMAP_TYPE_XPM
},
40 { "horse.png", wxBITMAP_TYPE_PNG
},
41 { "horse.ani", wxBITMAP_TYPE_ANI
},
42 { "horse.bmp", wxBITMAP_TYPE_BMP
},
43 { "horse.cur", wxBITMAP_TYPE_CUR
},
44 { "horse.gif", wxBITMAP_TYPE_GIF
},
45 { "horse.jpg", wxBITMAP_TYPE_JPEG
},
46 { "horse.pcx", wxBITMAP_TYPE_PCX
},
47 { "horse.pnm", wxBITMAP_TYPE_PNM
},
48 { "horse.tga", wxBITMAP_TYPE_TGA
},
49 { "horse.tif", wxBITMAP_TYPE_TIF
}
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class ImageTestCase
: public CppUnit::TestCase
64 CPPUNIT_TEST_SUITE( ImageTestCase
);
65 CPPUNIT_TEST( LoadFromSocketStream
);
66 CPPUNIT_TEST( LoadFromZipStream
);
67 CPPUNIT_TEST( LoadFromFile
);
68 CPPUNIT_TEST( SizeImage
);
69 CPPUNIT_TEST_SUITE_END();
71 void LoadFromSocketStream();
72 void LoadFromZipStream();
76 DECLARE_NO_COPY_CLASS(ImageTestCase
)
79 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
80 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
82 ImageTestCase::ImageTestCase()
84 wxSocketBase::Initialize();
86 // the formats we're going to test:
87 wxImage::AddHandler(new wxICOHandler
);
88 wxImage::AddHandler(new wxXPMHandler
);
89 wxImage::AddHandler(new wxPNGHandler
);
90 wxImage::AddHandler(new wxANIHandler
);
91 wxImage::AddHandler(new wxBMPHandler
);
92 wxImage::AddHandler(new wxCURHandler
);
93 wxImage::AddHandler(new wxGIFHandler
);
94 wxImage::AddHandler(new wxJPEGHandler
);
95 wxImage::AddHandler(new wxPCXHandler
);
96 wxImage::AddHandler(new wxPNMHandler
);
97 wxImage::AddHandler(new wxTGAHandler
);
98 wxImage::AddHandler(new wxTIFFHandler
);
101 ImageTestCase::~ImageTestCase()
103 wxSocketBase::Shutdown();
106 void ImageTestCase::LoadFromFile()
109 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
110 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
113 void ImageTestCase::LoadFromSocketStream()
115 if (!IsNetworkAvailable()) // implemented in test.cpp
117 wxLogWarning("No network connectivity; skipping the "
118 "ImageTestCase::LoadFromSocketStream test unit.");
127 { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
128 { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
131 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
133 wxURL
url(testData
[i
].url
);
134 WX_ASSERT_EQUAL_MESSAGE
136 ("Constructing URL \"%s\" failed.", testData
[i
].url
),
141 wxInputStream
*in_stream
= url
.GetInputStream();
144 ("Opening URL \"%s\" failed.", testData
[i
].url
),
145 in_stream
&& in_stream
->IsOk()
150 // NOTE: it's important to inform wxImage about the type of the image being
151 // loaded otherwise it will try to autodetect the format, but that
152 // requires a seekable stream!
155 ("Loading image from \"%s\" failed.", testData
[i
].url
),
156 img
.LoadFile(*in_stream
, testData
[i
].type
)
163 void ImageTestCase::LoadFromZipStream()
165 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
167 switch (g_testfiles
[i
].type
)
169 case wxBITMAP_TYPE_XPM
:
170 case wxBITMAP_TYPE_GIF
:
171 case wxBITMAP_TYPE_PCX
:
172 case wxBITMAP_TYPE_TGA
:
173 case wxBITMAP_TYPE_TIF
:
174 continue; // skip testing those wxImageHandlers which cannot
175 // load data from non-seekable streams
181 // compress the test file on the fly:
182 wxMemoryOutputStream memOut
;
184 wxFileInputStream
file(g_testfiles
[i
].file
);
185 CPPUNIT_ASSERT(file
.IsOk());
187 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
188 CPPUNIT_ASSERT(compressFilter
.IsOk());
190 file
.Read(compressFilter
);
191 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
194 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
195 // allows us to test loading images from non-seekable streams other than socket streams
196 wxMemoryInputStream
memIn(memOut
);
197 CPPUNIT_ASSERT(memIn
.IsOk());
198 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
199 CPPUNIT_ASSERT(decompressFilter
.IsOk());
203 // NOTE: it's important to inform wxImage about the type of the image being
204 // loaded otherwise it will try to autodetect the format, but that
205 // requires a seekable stream!
206 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
207 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
211 void ImageTestCase::SizeImage()
213 // Test the wxImage::Size() function which takes a rectangle from source and
214 // places it in a new image at a given position. This test checks, if the
215 // correct areas are chosen, and clipping is done correctly.
218 static const char * xpm_orig
[] = {
219 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
231 // the expected results for all tests:
232 static const char * xpm_l_t
[] = {
233 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
245 static const char * xpm_t
[] = {
246 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
258 static const char * xpm_r_t
[] = {
259 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
271 static const char * xpm_l
[] = {
272 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
284 static const char * xpm_r
[] = {
285 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
297 static const char * xpm_l_b
[] = {
298 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
310 static const char * xpm_b
[] = {
311 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
323 static const char * xpm_r_b
[] = {
324 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
336 static const char * xpm_sm
[] = {
337 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
347 static const char * xpm_gt
[] = {
348 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
362 static const char * xpm_gt_l_t
[] = {
363 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
377 static const char * xpm_gt_l
[] = {
378 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
392 static const char * xpm_gt_l_b
[] = {
393 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
407 static const char * xpm_gt_l_bb
[] = {
408 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
422 static const char * xpm_gt_t
[] = {
423 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
437 static const char * xpm_gt_b
[] = {
438 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
452 static const char * xpm_gt_bb
[] = {
453 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
467 static const char * xpm_gt_r_t
[] = {
468 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
482 static const char * xpm_gt_r
[] = {
483 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
497 static const char * xpm_gt_r_b
[] = {
498 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
512 static const char * xpm_gt_r_bb
[] = {
513 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
527 static const char * xpm_gt_rr_t
[] = {
528 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
542 static const char * xpm_gt_rr
[] = {
543 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
557 static const char * xpm_gt_rr_b
[] = {
558 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
572 static const char * xpm_gt_rr_bb
[] = {
573 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
587 static const char * xpm_sm_ll_tt
[] = {
588 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
598 static const char * xpm_sm_ll_t
[] = {
599 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
609 static const char * xpm_sm_ll
[] = {
610 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
620 static const char * xpm_sm_ll_b
[] = {
621 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
631 static const char * xpm_sm_l_tt
[] = {
632 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
642 static const char * xpm_sm_l_t
[] = {
643 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
653 static const char * xpm_sm_l
[] = {
654 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
664 static const char * xpm_sm_l_b
[] = {
665 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
675 static const char * xpm_sm_tt
[] = {
676 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
686 static const char * xpm_sm_t
[] = {
687 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
697 static const char * xpm_sm_b
[] = {
698 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
708 static const char * xpm_sm_r_tt
[] = {
709 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
719 static const char * xpm_sm_r_t
[] = {
720 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
730 static const char * xpm_sm_r
[] = {
731 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
741 static const char * xpm_sm_r_b
[] = {
742 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
753 // this table defines all tests
756 int w
, h
, dx
, dy
; // first parameters for Size()
757 const char **ref_xpm
; // expected result
760 { 10, 10, 0, 0, xpm_orig
}, // same size, same position
761 { 12, 12, 0, 0, xpm_gt
}, // target larger, same position
762 { 8, 8, 0, 0, xpm_sm
}, // target smaller, same position
763 { 10, 10, -2, -2, xpm_l_t
}, // same size, move left up
764 { 10, 10, -2, 0, xpm_l
}, // same size, move left
765 { 10, 10, -2, 2, xpm_l_b
}, // same size, move left down
766 { 10, 10, 0, -2, xpm_t
}, // same size, move up
767 { 10, 10, 0, 2, xpm_b
}, // same size, move down
768 { 10, 10, 2, -2, xpm_r_t
}, // same size, move right up
769 { 10, 10, 2, 0, xpm_r
}, // same size, move right
770 { 10, 10, 2, 2, xpm_r_b
}, // same size, move right down
771 { 12, 12, -2, -2, xpm_gt_l_t
}, // target larger, move left up
772 { 12, 12, -2, 0, xpm_gt_l
}, // target larger, move left
773 { 12, 12, -2, 2, xpm_gt_l_b
}, // target larger, move left down
774 { 12, 12, -2, 4, xpm_gt_l_bb
}, // target larger, move left down
775 { 12, 12, 0, -2, xpm_gt_t
}, // target larger, move up
776 { 12, 12, 0, 2, xpm_gt_b
}, // target larger, move down
777 { 12, 12, 0, 4, xpm_gt_bb
}, // target larger, move down
778 { 12, 12, 2, -2, xpm_gt_r_t
}, // target larger, move right up
779 { 12, 12, 2, 0, xpm_gt_r
}, // target larger, move right
780 { 12, 12, 2, 2, xpm_gt_r_b
}, // target larger, move right down
781 { 12, 12, 2, 4, xpm_gt_r_bb
}, // target larger, move right down
782 { 12, 12, 4, -2, xpm_gt_rr_t
}, // target larger, move right up
783 { 12, 12, 4, 0, xpm_gt_rr
}, // target larger, move right
784 { 12, 12, 4, 2, xpm_gt_rr_b
}, // target larger, move right down
785 { 12, 12, 4, 4, xpm_gt_rr_bb
}, // target larger, move right down
786 { 8, 8, -4, -4, xpm_sm_ll_tt
}, // target smaller, move left up
787 { 8, 8, -4, -2, xpm_sm_ll_t
}, // target smaller, move left up
788 { 8, 8, -4, 0, xpm_sm_ll
}, // target smaller, move left
789 { 8, 8, -4, 2, xpm_sm_ll_b
}, // target smaller, move left down
790 { 8, 8, -2, -4, xpm_sm_l_tt
}, // target smaller, move left up
791 { 8, 8, -2, -2, xpm_sm_l_t
}, // target smaller, move left up
792 { 8, 8, -2, 0, xpm_sm_l
}, // target smaller, move left
793 { 8, 8, -2, 2, xpm_sm_l_b
}, // target smaller, move left down
794 { 8, 8, 0, -4, xpm_sm_tt
}, // target smaller, move up
795 { 8, 8, 0, -2, xpm_sm_t
}, // target smaller, move up
796 { 8, 8, 0, 2, xpm_sm_b
}, // target smaller, move down
797 { 8, 8, 2, -4, xpm_sm_r_tt
}, // target smaller, move right up
798 { 8, 8, 2, -2, xpm_sm_r_t
}, // target smaller, move right up
799 { 8, 8, 2, 0, xpm_sm_r
}, // target smaller, move right
800 { 8, 8, 2, 2, xpm_sm_r_b
}, // target smaller, move right down
803 const wxImage
src_img(xpm_orig
);
804 for ( unsigned i
= 0; i
< WXSIZEOF(sizeTestData
); i
++ )
806 SizeTestData
& st
= sizeTestData
[i
];
808 actual(src_img
.Size(wxSize(st
.w
, st
.h
), wxPoint(st
.dx
, st
.dy
), 0, 0, 0)),
809 expected(st
.ref_xpm
);
811 // to check results with an image viewer uncomment this:
812 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
813 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
815 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().x
, expected
.GetSize().x
);
816 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().y
, expected
.GetSize().y
);
818 const unsigned data_len
= 3 * expected
.GetHeight() * expected
.GetWidth();
822 ("Resize test #%u: (%d, %d), (%d, %d)", i
, st
.w
, st
.h
, st
.dx
, st
.dy
),
823 memcmp(actual
.GetData(), expected
.GetData(), data_len
) == 0
832 TODO: add lots of more tests to wxImage functions