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 "
116 "ImageTestCase::LoadFromSocketStream test unit.");
125 { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
126 { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
129 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
131 wxURL
url(testData
[i
].url
);
132 WX_ASSERT_EQUAL_MESSAGE
134 ("Constructing URL \"%s\" failed.", testData
[i
].url
),
139 wxInputStream
*in_stream
= url
.GetInputStream();
142 ("Opening URL \"%s\" failed.", testData
[i
].url
),
143 in_stream
&& in_stream
->IsOk()
148 // NOTE: it's important to inform wxImage about the type of the image being
149 // loaded otherwise it will try to autodetect the format, but that
150 // requires a seekable stream!
153 ("Loading image from \"%s\" failed.", testData
[i
].url
),
154 img
.LoadFile(*in_stream
, testData
[i
].type
)
161 void ImageTestCase
::LoadFromZipStream()
163 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
165 switch (g_testfiles
[i
].type
)
167 case wxBITMAP_TYPE_XPM
:
168 case wxBITMAP_TYPE_GIF
:
169 case wxBITMAP_TYPE_PCX
:
170 case wxBITMAP_TYPE_TGA
:
171 case wxBITMAP_TYPE_TIF
:
172 continue; // skip testing those wxImageHandlers which cannot
173 // load data from non-seekable streams
179 // compress the test file on the fly:
180 wxMemoryOutputStream memOut
;
182 wxFileInputStream
file(g_testfiles
[i
].file
);
183 CPPUNIT_ASSERT(file
.IsOk());
185 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
186 CPPUNIT_ASSERT(compressFilter
.IsOk());
188 file
.Read(compressFilter
);
189 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
192 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
193 // allows us to test loading images from non-seekable streams other than socket streams
194 wxMemoryInputStream
memIn(memOut
);
195 CPPUNIT_ASSERT(memIn
.IsOk());
196 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
197 CPPUNIT_ASSERT(decompressFilter
.IsOk());
201 // NOTE: it's important to inform wxImage about the type of the image being
202 // loaded otherwise it will try to autodetect the format, but that
203 // requires a seekable stream!
204 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
205 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
209 void ImageTestCase
::SizeImage()
211 // Test the wxImage::Size() function which takes a rectangle from source and
212 // places it in a new image at a given position. This test checks, if the
213 // correct areas are chosen, and clipping is done correctly.
216 static const char * xpm_orig
[] = {
217 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
229 // the expected results for all tests:
230 static const char * xpm_l_t
[] = {
231 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
243 static const char * xpm_t
[] = {
244 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
256 static const char * xpm_r_t
[] = {
257 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
269 static const char * xpm_l
[] = {
270 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
282 static const char * xpm_r
[] = {
283 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
295 static const char * xpm_l_b
[] = {
296 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
308 static const char * xpm_b
[] = {
309 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
321 static const char * xpm_r_b
[] = {
322 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
334 static const char * xpm_sm
[] = {
335 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
345 static const char * xpm_gt
[] = {
346 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
360 static const char * xpm_gt_l_t
[] = {
361 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
375 static const char * xpm_gt_l
[] = {
376 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
390 static const char * xpm_gt_l_b
[] = {
391 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
405 static const char * xpm_gt_l_bb
[] = {
406 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
420 static const char * xpm_gt_t
[] = {
421 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
435 static const char * xpm_gt_b
[] = {
436 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
450 static const char * xpm_gt_bb
[] = {
451 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
465 static const char * xpm_gt_r_t
[] = {
466 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
480 static const char * xpm_gt_r
[] = {
481 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
495 static const char * xpm_gt_r_b
[] = {
496 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
510 static const char * xpm_gt_r_bb
[] = {
511 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
525 static const char * xpm_gt_rr_t
[] = {
526 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
540 static const char * xpm_gt_rr
[] = {
541 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
555 static const char * xpm_gt_rr_b
[] = {
556 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
570 static const char * xpm_gt_rr_bb
[] = {
571 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
585 static const char * xpm_sm_ll_tt
[] = {
586 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
596 static const char * xpm_sm_ll_t
[] = {
597 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
607 static const char * xpm_sm_ll
[] = {
608 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
618 static const char * xpm_sm_ll_b
[] = {
619 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
629 static const char * xpm_sm_l_tt
[] = {
630 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
640 static const char * xpm_sm_l_t
[] = {
641 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
651 static const char * xpm_sm_l
[] = {
652 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
662 static const char * xpm_sm_l_b
[] = {
663 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
673 static const char * xpm_sm_tt
[] = {
674 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
684 static const char * xpm_sm_t
[] = {
685 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
695 static const char * xpm_sm_b
[] = {
696 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
706 static const char * xpm_sm_r_tt
[] = {
707 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
717 static const char * xpm_sm_r_t
[] = {
718 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
728 static const char * xpm_sm_r
[] = {
729 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
739 static const char * xpm_sm_r_b
[] = {
740 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
751 // this table defines all tests
754 int w
, h
, dx
, dy
; // first parameters for Size()
755 const char **ref_xpm
; // expected result
758 { 10, 10, 0, 0, xpm_orig
}, // same size, same position
759 { 12, 12, 0, 0, xpm_gt
}, // target larger, same position
760 { 8, 8, 0, 0, xpm_sm
}, // target smaller, same position
761 { 10, 10, -2, -2, xpm_l_t
}, // same size, move left up
762 { 10, 10, -2, 0, xpm_l
}, // same size, move left
763 { 10, 10, -2, 2, xpm_l_b
}, // same size, move left down
764 { 10, 10, 0, -2, xpm_t
}, // same size, move up
765 { 10, 10, 0, 2, xpm_b
}, // same size, move down
766 { 10, 10, 2, -2, xpm_r_t
}, // same size, move right up
767 { 10, 10, 2, 0, xpm_r
}, // same size, move right
768 { 10, 10, 2, 2, xpm_r_b
}, // same size, move right down
769 { 12, 12, -2, -2, xpm_gt_l_t
}, // target larger, move left up
770 { 12, 12, -2, 0, xpm_gt_l
}, // target larger, move left
771 { 12, 12, -2, 2, xpm_gt_l_b
}, // target larger, move left down
772 { 12, 12, -2, 4, xpm_gt_l_bb
}, // target larger, move left down
773 { 12, 12, 0, -2, xpm_gt_t
}, // target larger, move up
774 { 12, 12, 0, 2, xpm_gt_b
}, // target larger, move down
775 { 12, 12, 0, 4, xpm_gt_bb
}, // target larger, move down
776 { 12, 12, 2, -2, xpm_gt_r_t
}, // target larger, move right up
777 { 12, 12, 2, 0, xpm_gt_r
}, // target larger, move right
778 { 12, 12, 2, 2, xpm_gt_r_b
}, // target larger, move right down
779 { 12, 12, 2, 4, xpm_gt_r_bb
}, // target larger, move right down
780 { 12, 12, 4, -2, xpm_gt_rr_t
}, // target larger, move right up
781 { 12, 12, 4, 0, xpm_gt_rr
}, // target larger, move right
782 { 12, 12, 4, 2, xpm_gt_rr_b
}, // target larger, move right down
783 { 12, 12, 4, 4, xpm_gt_rr_bb
}, // target larger, move right down
784 { 8, 8, -4, -4, xpm_sm_ll_tt
}, // target smaller, move left up
785 { 8, 8, -4, -2, xpm_sm_ll_t
}, // target smaller, move left up
786 { 8, 8, -4, 0, xpm_sm_ll
}, // target smaller, move left
787 { 8, 8, -4, 2, xpm_sm_ll_b
}, // target smaller, move left down
788 { 8, 8, -2, -4, xpm_sm_l_tt
}, // target smaller, move left up
789 { 8, 8, -2, -2, xpm_sm_l_t
}, // target smaller, move left up
790 { 8, 8, -2, 0, xpm_sm_l
}, // target smaller, move left
791 { 8, 8, -2, 2, xpm_sm_l_b
}, // target smaller, move left down
792 { 8, 8, 0, -4, xpm_sm_tt
}, // target smaller, move up
793 { 8, 8, 0, -2, xpm_sm_t
}, // target smaller, move up
794 { 8, 8, 0, 2, xpm_sm_b
}, // target smaller, move down
795 { 8, 8, 2, -4, xpm_sm_r_tt
}, // target smaller, move right up
796 { 8, 8, 2, -2, xpm_sm_r_t
}, // target smaller, move right up
797 { 8, 8, 2, 0, xpm_sm_r
}, // target smaller, move right
798 { 8, 8, 2, 2, xpm_sm_r_b
}, // target smaller, move right down
801 const wxImage
src_img(xpm_orig
);
802 for ( unsigned i
= 0; i
< WXSIZEOF(sizeTestData
); i
++ )
804 SizeTestData
& st
= sizeTestData
[i
];
806 actual(src_img
.Size(wxSize(st
.w
, st
.h
), wxPoint(st
.dx
, st
.dy
), 0, 0, 0)),
807 expected(st
.ref_xpm
);
809 // to check results with an image viewer uncomment this:
810 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
811 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
813 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().x
, expected
.GetSize().x
);
814 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().y
, expected
.GetSize().y
);
816 const unsigned data_len
= 3 * expected
.GetHeight() * expected
.GetWidth();
820 ("Resize test #%u: (%d, %d), (%d, %d)", i
, st
.w
, st
.h
, st
.dx
, st
.dy
),
821 memcmp(actual
.GetData(), expected
.GetData(), data_len
) == 0
828 TODO: add lots of more tests to wxImage functions