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 // ----------------------------------------------------------------------------
27 #include "wx/mstream.h"
28 #include "wx/zstream.h"
29 #include "wx/wfstream.h"
36 { "horse.ico", wxBITMAP_TYPE_ICO
},
37 { "horse.xpm", wxBITMAP_TYPE_XPM
},
38 { "horse.png", wxBITMAP_TYPE_PNG
},
39 { "horse.ani", wxBITMAP_TYPE_ANI
},
40 { "horse.bmp", wxBITMAP_TYPE_BMP
},
41 { "horse.cur", wxBITMAP_TYPE_CUR
},
42 { "horse.gif", wxBITMAP_TYPE_GIF
},
43 { "horse.jpg", wxBITMAP_TYPE_JPEG
},
44 { "horse.pcx", wxBITMAP_TYPE_PCX
},
45 { "horse.pnm", wxBITMAP_TYPE_PNM
},
46 { "horse.tga", wxBITMAP_TYPE_TGA
},
47 { "horse.tif", wxBITMAP_TYPE_TIF
}
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class ImageTestCase
: public CppUnit::TestCase
62 CPPUNIT_TEST_SUITE( ImageTestCase
);
63 CPPUNIT_TEST( LoadFromSocketStream
);
64 CPPUNIT_TEST( LoadFromZipStream
);
65 CPPUNIT_TEST( LoadFromFile
);
66 CPPUNIT_TEST( SizeImage
);
67 CPPUNIT_TEST_SUITE_END();
69 void LoadFromSocketStream();
70 void LoadFromZipStream();
74 DECLARE_NO_COPY_CLASS(ImageTestCase
)
77 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
78 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
80 ImageTestCase::ImageTestCase()
82 wxSocketBase::Initialize();
84 // the formats we're going to test:
85 wxImage::AddHandler(new wxICOHandler
);
86 wxImage::AddHandler(new wxXPMHandler
);
87 wxImage::AddHandler(new wxPNGHandler
);
88 wxImage::AddHandler(new wxANIHandler
);
89 wxImage::AddHandler(new wxBMPHandler
);
90 wxImage::AddHandler(new wxCURHandler
);
91 wxImage::AddHandler(new wxGIFHandler
);
92 wxImage::AddHandler(new wxJPEGHandler
);
93 wxImage::AddHandler(new wxPCXHandler
);
94 wxImage::AddHandler(new wxPNMHandler
);
95 wxImage::AddHandler(new wxTGAHandler
);
96 wxImage::AddHandler(new wxTIFFHandler
);
99 ImageTestCase::~ImageTestCase()
101 wxSocketBase::Shutdown();
104 void ImageTestCase::LoadFromFile()
107 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
108 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
111 void ImageTestCase::LoadFromSocketStream()
113 if (!IsNetworkAvailable()) // implemented in test.cpp
115 wxLogWarning("No network connectivity; skipping the ImageTestCase::LoadFromSocketStream test unit.");
124 { "http://wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
125 { "http://wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
128 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
130 wxURL
url(testData
[i
].url
);
131 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
133 wxInputStream
*in_stream
= url
.GetInputStream();
134 CPPUNIT_ASSERT(in_stream
&& in_stream
->IsOk());
138 // NOTE: it's important to inform wxImage about the type of the image being
139 // loaded otherwise it will try to autodetect the format, but that
140 // requires a seekable stream!
141 CPPUNIT_ASSERT(img
.LoadFile(*in_stream
, testData
[i
].type
));
147 void ImageTestCase::LoadFromZipStream()
149 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
151 switch (g_testfiles
[i
].type
)
153 case wxBITMAP_TYPE_XPM
:
154 case wxBITMAP_TYPE_GIF
:
155 case wxBITMAP_TYPE_PCX
:
156 case wxBITMAP_TYPE_TGA
:
157 case wxBITMAP_TYPE_TIF
:
158 continue; // skip testing those wxImageHandlers which cannot
159 // load data from non-seekable streams
165 // compress the test file on the fly:
166 wxMemoryOutputStream memOut
;
168 wxFileInputStream
file(g_testfiles
[i
].file
);
169 CPPUNIT_ASSERT(file
.IsOk());
171 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
172 CPPUNIT_ASSERT(compressFilter
.IsOk());
174 file
.Read(compressFilter
);
175 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
178 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
179 // allows us to test loading images from non-seekable streams other than socket streams
180 wxMemoryInputStream
memIn(memOut
);
181 CPPUNIT_ASSERT(memIn
.IsOk());
182 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
183 CPPUNIT_ASSERT(decompressFilter
.IsOk());
187 // NOTE: it's important to inform wxImage about the type of the image being
188 // loaded otherwise it will try to autodetect the format, but that
189 // requires a seekable stream!
190 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
191 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
195 void ImageTestCase::SizeImage()
197 // Test the wxImage::Size() function which takes a rectangle from source and
198 // places it in a new image at a given position. This test checks, if the
199 // correct areas are chosen, and clipping is done correctly.
202 static const char * xpm_orig
[] = {
203 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
215 // the expected results for all tests:
216 static const char * xpm_l_t
[] = {
217 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
229 static const char * xpm_t
[] = {
230 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
242 static const char * xpm_r_t
[] = {
243 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
255 static const char * xpm_l
[] = {
256 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
268 static const char * xpm_r
[] = {
269 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
281 static const char * xpm_l_b
[] = {
282 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
294 static const char * xpm_b
[] = {
295 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
307 static const char * xpm_r_b
[] = {
308 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
320 static const char * xpm_sm
[] = {
321 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
331 static const char * xpm_gt
[] = {
332 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
346 static const char * xpm_gt_l_t
[] = {
347 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
361 static const char * xpm_gt_l
[] = {
362 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
376 static const char * xpm_gt_l_b
[] = {
377 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
391 static const char * xpm_gt_l_bb
[] = {
392 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
406 static const char * xpm_gt_t
[] = {
407 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
421 static const char * xpm_gt_b
[] = {
422 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
436 static const char * xpm_gt_bb
[] = {
437 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
451 static const char * xpm_gt_r_t
[] = {
452 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
466 static const char * xpm_gt_r
[] = {
467 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
481 static const char * xpm_gt_r_b
[] = {
482 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
496 static const char * xpm_gt_r_bb
[] = {
497 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
511 static const char * xpm_gt_rr_t
[] = {
512 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
526 static const char * xpm_gt_rr
[] = {
527 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
541 static const char * xpm_gt_rr_b
[] = {
542 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
556 static const char * xpm_gt_rr_bb
[] = {
557 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
571 static const char * xpm_sm_ll_tt
[] = {
572 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
582 static const char * xpm_sm_ll_t
[] = {
583 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
593 static const char * xpm_sm_ll
[] = {
594 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
604 static const char * xpm_sm_ll_b
[] = {
605 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
615 static const char * xpm_sm_l_tt
[] = {
616 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
626 static const char * xpm_sm_l_t
[] = {
627 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
637 static const char * xpm_sm_l
[] = {
638 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
648 static const char * xpm_sm_l_b
[] = {
649 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
659 static const char * xpm_sm_tt
[] = {
660 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
670 static const char * xpm_sm_t
[] = {
671 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
681 static const char * xpm_sm_b
[] = {
682 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
692 static const char * xpm_sm_r_tt
[] = {
693 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
703 static const char * xpm_sm_r_t
[] = {
704 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
714 static const char * xpm_sm_r
[] = {
715 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
725 static const char * xpm_sm_r_b
[] = {
726 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
737 // this table defines all tests
740 int w
, h
, dx
, dy
; // first parameters for Size()
741 const char **ref_xpm
; // expected result
744 { 10, 10, 0, 0, xpm_orig
}, // same size, same position
745 { 12, 12, 0, 0, xpm_gt
}, // target larger, same position
746 { 8, 8, 0, 0, xpm_sm
}, // target smaller, same position
747 { 10, 10, -2, -2, xpm_l_t
}, // same size, move left up
748 { 10, 10, -2, 0, xpm_l
}, // same size, move left
749 { 10, 10, -2, 2, xpm_l_b
}, // same size, move left down
750 { 10, 10, 0, -2, xpm_t
}, // same size, move up
751 { 10, 10, 0, 2, xpm_b
}, // same size, move down
752 { 10, 10, 2, -2, xpm_r_t
}, // same size, move right up
753 { 10, 10, 2, 0, xpm_r
}, // same size, move right
754 { 10, 10, 2, 2, xpm_r_b
}, // same size, move right down
755 { 12, 12, -2, -2, xpm_gt_l_t
}, // target larger, move left up
756 { 12, 12, -2, 0, xpm_gt_l
}, // target larger, move left
757 { 12, 12, -2, 2, xpm_gt_l_b
}, // target larger, move left down
758 { 12, 12, -2, 4, xpm_gt_l_bb
}, // target larger, move left down
759 { 12, 12, 0, -2, xpm_gt_t
}, // target larger, move up
760 { 12, 12, 0, 2, xpm_gt_b
}, // target larger, move down
761 { 12, 12, 0, 4, xpm_gt_bb
}, // target larger, move down
762 { 12, 12, 2, -2, xpm_gt_r_t
}, // target larger, move right up
763 { 12, 12, 2, 0, xpm_gt_r
}, // target larger, move right
764 { 12, 12, 2, 2, xpm_gt_r_b
}, // target larger, move right down
765 { 12, 12, 2, 4, xpm_gt_r_bb
}, // target larger, move right down
766 { 12, 12, 4, -2, xpm_gt_rr_t
}, // target larger, move right up
767 { 12, 12, 4, 0, xpm_gt_rr
}, // target larger, move right
768 { 12, 12, 4, 2, xpm_gt_rr_b
}, // target larger, move right down
769 { 12, 12, 4, 4, xpm_gt_rr_bb
}, // target larger, move right down
770 { 8, 8, -4, -4, xpm_sm_ll_tt
}, // target smaller, move left up
771 { 8, 8, -4, -2, xpm_sm_ll_t
}, // target smaller, move left up
772 { 8, 8, -4, 0, xpm_sm_ll
}, // target smaller, move left
773 { 8, 8, -4, 2, xpm_sm_ll_b
}, // target smaller, move left down
774 { 8, 8, -2, -4, xpm_sm_l_tt
}, // target smaller, move left up
775 { 8, 8, -2, -2, xpm_sm_l_t
}, // target smaller, move left up
776 { 8, 8, -2, 0, xpm_sm_l
}, // target smaller, move left
777 { 8, 8, -2, 2, xpm_sm_l_b
}, // target smaller, move left down
778 { 8, 8, 0, -4, xpm_sm_tt
}, // target smaller, move up
779 { 8, 8, 0, -2, xpm_sm_t
}, // target smaller, move up
780 { 8, 8, 0, 2, xpm_sm_b
}, // target smaller, move down
781 { 8, 8, 2, -4, xpm_sm_r_tt
}, // target smaller, move right up
782 { 8, 8, 2, -2, xpm_sm_r_t
}, // target smaller, move right up
783 { 8, 8, 2, 0, xpm_sm_r
}, // target smaller, move right
784 { 8, 8, 2, 2, xpm_sm_r_b
}, // target smaller, move right down
787 const wxImage
src_img(xpm_orig
);
788 for ( unsigned i
= 0; i
< WXSIZEOF(sizeTestData
); i
++ )
790 SizeTestData
& st
= sizeTestData
[i
];
792 actual(src_img
.Size(wxSize(st
.w
, st
.h
), wxPoint(st
.dx
, st
.dy
), 0, 0, 0)),
793 expected(st
.ref_xpm
);
795 // to check results with an image viewer uncomment this:
796 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
797 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
799 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().x
, expected
.GetSize().x
);
800 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().y
, expected
.GetSize().y
);
802 const unsigned data_len
= 3 * expected
.GetHeight() * expected
.GetWidth();
806 ("Resize test #%u: (%d, %d), (%d, %d)", i
, st
.w
, st
.h
, st
.dx
, st
.dy
),
807 memcmp(actual
.GetData(), expected
.GetData(), data_len
) == 0
814 TODO: add lots of more tests to wxImage functions