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 // ----------------------------------------------------------------------------
26 #include "wx/anidecod.h" // wxImageArray
27 #include "wx/palette.h"
30 #include "wx/mstream.h"
31 #include "wx/zstream.h"
32 #include "wx/wfstream.h"
34 #include "testimage.h"
42 { "horse.ico", wxBITMAP_TYPE_ICO
, 4 },
43 { "horse.xpm", wxBITMAP_TYPE_XPM
, 8 },
44 { "horse.png", wxBITMAP_TYPE_PNG
, 24 },
45 { "horse.ani", wxBITMAP_TYPE_ANI
, 24 },
46 { "horse.bmp", wxBITMAP_TYPE_BMP
, 8 },
47 { "horse.cur", wxBITMAP_TYPE_CUR
, 1 },
48 { "horse.gif", wxBITMAP_TYPE_GIF
, 8 },
49 { "horse.jpg", wxBITMAP_TYPE_JPEG
, 24 },
50 { "horse.pcx", wxBITMAP_TYPE_PCX
, 8 },
51 { "horse.pnm", wxBITMAP_TYPE_PNM
, 24 },
52 { "horse.tga", wxBITMAP_TYPE_TGA
, 8 },
53 { "horse.tif", wxBITMAP_TYPE_TIF
, 8 }
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 class ImageTestCase
: public CppUnit::TestCase
68 CPPUNIT_TEST_SUITE( ImageTestCase
);
69 CPPUNIT_TEST( LoadFromSocketStream
);
70 CPPUNIT_TEST( LoadFromZipStream
);
71 CPPUNIT_TEST( LoadFromFile
);
72 CPPUNIT_TEST( SizeImage
);
73 CPPUNIT_TEST( CompareLoadedImage
);
74 CPPUNIT_TEST( CompareSavedImage
);
75 CPPUNIT_TEST( SavePNG
);
76 CPPUNIT_TEST( SaveAnimatedGIF
);
77 CPPUNIT_TEST( ReadCorruptedTGA
);
78 CPPUNIT_TEST( GIFComment
);
79 CPPUNIT_TEST( DibPadding
);
80 CPPUNIT_TEST( BMPFlippingAndRLECompression
);
81 CPPUNIT_TEST_SUITE_END();
83 void LoadFromSocketStream();
84 void LoadFromZipStream();
87 void CompareLoadedImage();
88 void CompareSavedImage();
90 void SaveAnimatedGIF();
91 void ReadCorruptedTGA();
94 void BMPFlippingAndRLECompression();
96 DECLARE_NO_COPY_CLASS(ImageTestCase
)
99 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
100 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
102 ImageTestCase::ImageTestCase()
104 wxSocketBase::Initialize();
106 // the formats we're going to test:
107 wxImage::AddHandler(new wxICOHandler
);
108 wxImage::AddHandler(new wxXPMHandler
);
109 wxImage::AddHandler(new wxPNGHandler
);
110 wxImage::AddHandler(new wxANIHandler
);
111 wxImage::AddHandler(new wxBMPHandler
);
112 wxImage::AddHandler(new wxCURHandler
);
113 wxImage::AddHandler(new wxGIFHandler
);
114 wxImage::AddHandler(new wxJPEGHandler
);
115 wxImage::AddHandler(new wxPCXHandler
);
116 wxImage::AddHandler(new wxPNMHandler
);
117 wxImage::AddHandler(new wxTGAHandler
);
118 wxImage::AddHandler(new wxTIFFHandler
);
121 ImageTestCase::~ImageTestCase()
123 wxSocketBase::Shutdown();
126 void ImageTestCase::LoadFromFile()
129 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
130 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
133 void ImageTestCase::LoadFromSocketStream()
135 if (!IsNetworkAvailable()) // implemented in test.cpp
137 wxLogWarning("No network connectivity; skipping the "
138 "ImageTestCase::LoadFromSocketStream test unit.");
147 { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
148 { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
151 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
153 wxURL
url(testData
[i
].url
);
154 WX_ASSERT_EQUAL_MESSAGE
156 ("Constructing URL \"%s\" failed.", testData
[i
].url
),
161 wxInputStream
*in_stream
= url
.GetInputStream();
164 ("Opening URL \"%s\" failed.", testData
[i
].url
),
165 in_stream
&& in_stream
->IsOk()
170 // NOTE: it's important to inform wxImage about the type of the image being
171 // loaded otherwise it will try to autodetect the format, but that
172 // requires a seekable stream!
175 ("Loading image from \"%s\" failed.", testData
[i
].url
),
176 img
.LoadFile(*in_stream
, testData
[i
].type
)
183 void ImageTestCase::LoadFromZipStream()
185 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
187 switch (g_testfiles
[i
].type
)
189 case wxBITMAP_TYPE_XPM
:
190 case wxBITMAP_TYPE_GIF
:
191 case wxBITMAP_TYPE_PCX
:
192 case wxBITMAP_TYPE_TGA
:
193 case wxBITMAP_TYPE_TIF
:
194 continue; // skip testing those wxImageHandlers which cannot
195 // load data from non-seekable streams
201 // compress the test file on the fly:
202 wxMemoryOutputStream memOut
;
204 wxFileInputStream
file(g_testfiles
[i
].file
);
205 CPPUNIT_ASSERT(file
.IsOk());
207 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
208 CPPUNIT_ASSERT(compressFilter
.IsOk());
210 file
.Read(compressFilter
);
211 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
214 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
215 // allows us to test loading images from non-seekable streams other than socket streams
216 wxMemoryInputStream
memIn(memOut
);
217 CPPUNIT_ASSERT(memIn
.IsOk());
218 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
219 CPPUNIT_ASSERT(decompressFilter
.IsOk());
223 // NOTE: it's important to inform wxImage about the type of the image being
224 // loaded otherwise it will try to autodetect the format, but that
225 // requires a seekable stream!
226 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
227 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
231 void ImageTestCase::SizeImage()
233 // Test the wxImage::Size() function which takes a rectangle from source and
234 // places it in a new image at a given position. This test checks, if the
235 // correct areas are chosen, and clipping is done correctly.
238 static const char * xpm_orig
[] = {
239 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
251 // the expected results for all tests:
252 static const char * xpm_l_t
[] = {
253 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
265 static const char * xpm_t
[] = {
266 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
278 static const char * xpm_r_t
[] = {
279 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
291 static const char * xpm_l
[] = {
292 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
304 static const char * xpm_r
[] = {
305 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
317 static const char * xpm_l_b
[] = {
318 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
330 static const char * xpm_b
[] = {
331 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
343 static const char * xpm_r_b
[] = {
344 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
356 static const char * xpm_sm
[] = {
357 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
367 static const char * xpm_gt
[] = {
368 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
382 static const char * xpm_gt_l_t
[] = {
383 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
397 static const char * xpm_gt_l
[] = {
398 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
412 static const char * xpm_gt_l_b
[] = {
413 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
427 static const char * xpm_gt_l_bb
[] = {
428 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
442 static const char * xpm_gt_t
[] = {
443 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
457 static const char * xpm_gt_b
[] = {
458 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
472 static const char * xpm_gt_bb
[] = {
473 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
487 static const char * xpm_gt_r_t
[] = {
488 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
502 static const char * xpm_gt_r
[] = {
503 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
517 static const char * xpm_gt_r_b
[] = {
518 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
532 static const char * xpm_gt_r_bb
[] = {
533 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
547 static const char * xpm_gt_rr_t
[] = {
548 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
562 static const char * xpm_gt_rr
[] = {
563 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
577 static const char * xpm_gt_rr_b
[] = {
578 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
592 static const char * xpm_gt_rr_bb
[] = {
593 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
607 static const char * xpm_sm_ll_tt
[] = {
608 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
618 static const char * xpm_sm_ll_t
[] = {
619 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
629 static const char * xpm_sm_ll
[] = {
630 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
640 static const char * xpm_sm_ll_b
[] = {
641 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
651 static const char * xpm_sm_l_tt
[] = {
652 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
662 static const char * xpm_sm_l_t
[] = {
663 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
673 static const char * xpm_sm_l
[] = {
674 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
684 static const char * xpm_sm_l_b
[] = {
685 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
695 static const char * xpm_sm_tt
[] = {
696 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
706 static const char * xpm_sm_t
[] = {
707 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
717 static const char * xpm_sm_b
[] = {
718 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
728 static const char * xpm_sm_r_tt
[] = {
729 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
739 static const char * xpm_sm_r_t
[] = {
740 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
750 static const char * xpm_sm_r
[] = {
751 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
761 static const char * xpm_sm_r_b
[] = {
762 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
773 // this table defines all tests
776 int w
, h
, dx
, dy
; // first parameters for Size()
777 const char **ref_xpm
; // expected result
780 { 10, 10, 0, 0, xpm_orig
}, // same size, same position
781 { 12, 12, 0, 0, xpm_gt
}, // target larger, same position
782 { 8, 8, 0, 0, xpm_sm
}, // target smaller, same position
783 { 10, 10, -2, -2, xpm_l_t
}, // same size, move left up
784 { 10, 10, -2, 0, xpm_l
}, // same size, move left
785 { 10, 10, -2, 2, xpm_l_b
}, // same size, move left down
786 { 10, 10, 0, -2, xpm_t
}, // same size, move up
787 { 10, 10, 0, 2, xpm_b
}, // same size, move down
788 { 10, 10, 2, -2, xpm_r_t
}, // same size, move right up
789 { 10, 10, 2, 0, xpm_r
}, // same size, move right
790 { 10, 10, 2, 2, xpm_r_b
}, // same size, move right down
791 { 12, 12, -2, -2, xpm_gt_l_t
}, // target larger, move left up
792 { 12, 12, -2, 0, xpm_gt_l
}, // target larger, move left
793 { 12, 12, -2, 2, xpm_gt_l_b
}, // target larger, move left down
794 { 12, 12, -2, 4, xpm_gt_l_bb
}, // target larger, move left down
795 { 12, 12, 0, -2, xpm_gt_t
}, // target larger, move up
796 { 12, 12, 0, 2, xpm_gt_b
}, // target larger, move down
797 { 12, 12, 0, 4, xpm_gt_bb
}, // target larger, move down
798 { 12, 12, 2, -2, xpm_gt_r_t
}, // target larger, move right up
799 { 12, 12, 2, 0, xpm_gt_r
}, // target larger, move right
800 { 12, 12, 2, 2, xpm_gt_r_b
}, // target larger, move right down
801 { 12, 12, 2, 4, xpm_gt_r_bb
}, // target larger, move right down
802 { 12, 12, 4, -2, xpm_gt_rr_t
}, // target larger, move right up
803 { 12, 12, 4, 0, xpm_gt_rr
}, // target larger, move right
804 { 12, 12, 4, 2, xpm_gt_rr_b
}, // target larger, move right down
805 { 12, 12, 4, 4, xpm_gt_rr_bb
}, // target larger, move right down
806 { 8, 8, -4, -4, xpm_sm_ll_tt
}, // target smaller, move left up
807 { 8, 8, -4, -2, xpm_sm_ll_t
}, // target smaller, move left up
808 { 8, 8, -4, 0, xpm_sm_ll
}, // target smaller, move left
809 { 8, 8, -4, 2, xpm_sm_ll_b
}, // target smaller, move left down
810 { 8, 8, -2, -4, xpm_sm_l_tt
}, // target smaller, move left up
811 { 8, 8, -2, -2, xpm_sm_l_t
}, // target smaller, move left up
812 { 8, 8, -2, 0, xpm_sm_l
}, // target smaller, move left
813 { 8, 8, -2, 2, xpm_sm_l_b
}, // target smaller, move left down
814 { 8, 8, 0, -4, xpm_sm_tt
}, // target smaller, move up
815 { 8, 8, 0, -2, xpm_sm_t
}, // target smaller, move up
816 { 8, 8, 0, 2, xpm_sm_b
}, // target smaller, move down
817 { 8, 8, 2, -4, xpm_sm_r_tt
}, // target smaller, move right up
818 { 8, 8, 2, -2, xpm_sm_r_t
}, // target smaller, move right up
819 { 8, 8, 2, 0, xpm_sm_r
}, // target smaller, move right
820 { 8, 8, 2, 2, xpm_sm_r_b
}, // target smaller, move right down
823 const wxImage
src_img(xpm_orig
);
824 for ( unsigned i
= 0; i
< WXSIZEOF(sizeTestData
); i
++ )
826 SizeTestData
& st
= sizeTestData
[i
];
828 actual(src_img
.Size(wxSize(st
.w
, st
.h
), wxPoint(st
.dx
, st
.dy
), 0, 0, 0)),
829 expected(st
.ref_xpm
);
831 // to check results with an image viewer uncomment this:
832 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
833 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
835 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().x
, expected
.GetSize().x
);
836 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().y
, expected
.GetSize().y
);
838 WX_ASSERT_EQUAL_MESSAGE
840 ("Resize test #%u: (%d, %d), (%d, %d)", i
, st
.w
, st
.h
, st
.dx
, st
.dy
),
846 void ImageTestCase::CompareLoadedImage()
848 wxImage
expected8("horse.xpm");
849 CPPUNIT_ASSERT( expected8
.IsOk() );
851 wxImage
expected24("horse.png");
852 CPPUNIT_ASSERT( expected24
.IsOk() );
854 for (size_t i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
856 if ( !(g_testfiles
[i
].bitDepth
== 8 || g_testfiles
[i
].bitDepth
== 24)
857 || g_testfiles
[i
].type
== wxBITMAP_TYPE_JPEG
/*skip lossy JPEG*/)
862 wxImage
actual(g_testfiles
[i
].file
);
864 if ( actual
.GetSize() != expected8
.GetSize() )
870 WX_ASSERT_EQUAL_MESSAGE
872 ("Compare test '%s' for loading failed", g_testfiles
[i
].file
),
873 g_testfiles
[i
].bitDepth
== 8 ? expected8
: expected24
,
882 wxIMAGE_HAVE_ALPHA
= (1 << 0),
883 wxIMAGE_HAVE_PALETTE
= (1 << 1)
887 void CompareImage(const wxImageHandler
& handler
, const wxImage
& image
,
888 int properties
= 0, const wxImage
*compareTo
= NULL
)
890 wxBitmapType type
= handler
.GetType();
892 const bool testPalette
= (properties
& wxIMAGE_HAVE_PALETTE
) != 0;
894 This is getting messy and should probably be transformed into a table
895 with image format features before it gets hairier.
898 && ( !(type
== wxBITMAP_TYPE_BMP
899 || type
== wxBITMAP_TYPE_GIF
900 || type
== wxBITMAP_TYPE_ICO
901 || type
== wxBITMAP_TYPE_PNG
)
902 || type
== wxBITMAP_TYPE_XPM
) )
907 const bool testAlpha
= (properties
& wxIMAGE_HAVE_ALPHA
) != 0;
909 && !(type
== wxBITMAP_TYPE_PNG
|| type
== wxBITMAP_TYPE_TGA
) )
911 // don't test images with alpha if this handler doesn't support alpha
915 if (type
== wxBITMAP_TYPE_JPEG
/* skip lossy JPEG */)
920 wxMemoryOutputStream memOut
;
921 if ( !image
.SaveFile(memOut
, type
) )
923 // Unfortunately we can't know if the handler just doesn't support
924 // saving images, or if it failed to save.
928 wxMemoryInputStream
memIn(memOut
);
929 CPPUNIT_ASSERT(memIn
.IsOk());
931 wxImage
actual(memIn
);
932 CPPUNIT_ASSERT(actual
.IsOk());
934 const wxImage
*expected
= compareTo
? compareTo
: &image
;
935 CPPUNIT_ASSERT( actual
.GetSize() == expected
->GetSize() );
937 unsigned bitsPerPixel
= testPalette
? 8 : (testAlpha
? 32 : 24);
938 WX_ASSERT_EQUAL_MESSAGE
940 ("Compare test '%s (%d-bit)' for saving failed",
941 handler
.GetExtension(), bitsPerPixel
),
947 CPPUNIT_ASSERT(actual
.HasPalette()
948 == (testPalette
|| type
== wxBITMAP_TYPE_XPM
));
951 CPPUNIT_ASSERT( actual
.HasAlpha() == testAlpha
);
958 WX_ASSERT_EQUAL_MESSAGE
960 ("Compare alpha test '%s' for saving failed", handler
.GetExtension()),
966 void ImageTestCase::CompareSavedImage()
968 // FIXME-VC6: Pre-declare the loop variables for compatibility with
969 // pre-standard compilers such as MSVC6 that don't implement proper scope
970 // for the variables declared in the for loops.
973 wxImage
expected24("horse.png");
974 CPPUNIT_ASSERT( expected24
.IsOk() );
975 CPPUNIT_ASSERT( !expected24
.HasAlpha() );
977 wxImage expected8
= expected24
.ConvertToGreyscale();
980 unsigned char greys
[256];
981 for (i
= 0; i
< 256; ++i
)
985 wxPalette
palette(256, greys
, greys
, greys
);
986 expected8
.SetPalette(palette
);
987 #endif // #if wxUSE_PALETTE
989 expected8
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP_PALETTE
);
991 // Create an image with alpha based on the loaded image
992 wxImage
expected32(expected24
);
993 expected32
.SetAlpha();
995 int width
= expected32
.GetWidth();
996 int height
= expected32
.GetHeight();
997 for (y
= 0; y
< height
; ++y
)
999 for (x
= 0; x
< width
; ++x
)
1001 expected32
.SetAlpha(x
, y
, (x
*y
) & wxIMAGE_ALPHA_OPAQUE
);
1005 const wxList
& list
= wxImage::GetHandlers();
1006 for ( wxList::compatibility_iterator node
= list
.GetFirst();
1007 node
; node
= node
->GetNext() )
1009 wxImageHandler
*handler
= (wxImageHandler
*) node
->GetData();
1012 CompareImage(*handler
, expected8
, wxIMAGE_HAVE_PALETTE
);
1014 CompareImage(*handler
, expected24
);
1015 CompareImage(*handler
, expected32
, wxIMAGE_HAVE_ALPHA
);
1019 void ImageTestCase::SavePNG()
1021 wxImage
expected24("horse.png");
1022 CPPUNIT_ASSERT( expected24
.IsOk() );
1024 CPPUNIT_ASSERT( !expected24
.HasPalette() );
1025 #endif // #if wxUSE_PALETTE
1027 wxImage expected8
= expected24
.ConvertToGreyscale();
1030 horse.png converted to greyscale should be saved without a palette.
1032 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
), expected8
);
1035 But if we explicitly ask for trying to save with a palette, it should work.
1037 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1039 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1040 expected8
, wxIMAGE_HAVE_PALETTE
);
1043 CPPUNIT_ASSERT( expected8
.LoadFile("horse.gif") );
1045 CPPUNIT_ASSERT( expected8
.HasPalette() );
1046 #endif // #if wxUSE_PALETTE
1048 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1049 expected8
, wxIMAGE_HAVE_PALETTE
);
1052 Add alpha to the image in such a way that there will still be a maximum
1053 of 256 unique RGBA combinations. This should result in a saved
1054 PNG image still being palettised and having alpha.
1056 expected8
.SetAlpha();
1059 const int width
= expected8
.GetWidth();
1060 const int height
= expected8
.GetHeight();
1061 for (y
= 0; y
< height
; ++y
)
1063 for (x
= 0; x
< width
; ++x
)
1065 expected8
.SetAlpha(x
, y
, expected8
.GetRed(x
, y
));
1069 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1070 expected8
, wxIMAGE_HAVE_ALPHA
|wxIMAGE_HAVE_PALETTE
);
1073 Now change the alpha of the first pixel so that we can't save palettised
1074 anymore because there will be 256+1 entries which is beyond PNGs limit
1077 expected8
.SetAlpha(0, 0, 1);
1079 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1080 expected8
, wxIMAGE_HAVE_ALPHA
);
1083 Even if we explicitly ask for saving palettised it should not be done.
1085 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1086 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1087 expected8
, wxIMAGE_HAVE_ALPHA
);
1091 void ImageTestCase::SaveAnimatedGIF()
1094 wxImage
image("horse.gif");
1095 CPPUNIT_ASSERT( image
.IsOk() );
1097 wxImageArray images
;
1100 for (i
= 0; i
< 4-1; ++i
)
1102 images
.Add( images
[i
].Rotate90() );
1104 images
[i
+1].SetPalette(images
[0].GetPalette());
1107 wxMemoryOutputStream memOut
;
1108 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1110 wxGIFHandler handler
;
1111 wxMemoryInputStream
memIn(memOut
);
1112 CPPUNIT_ASSERT(memIn
.IsOk());
1113 const int imageCount
= handler
.GetImageCount(memIn
);
1114 CPPUNIT_ASSERT_EQUAL(4, imageCount
);
1116 for (i
= 0; i
< imageCount
; ++i
)
1118 wxFileOffset pos
= memIn
.TellI();
1119 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true, i
) );
1122 WX_ASSERT_EQUAL_MESSAGE
1124 ("Compare test for GIF frame number %d failed", i
),
1129 #endif // #if wxUSE_PALETTE
1132 void ImageTestCase::ReadCorruptedTGA()
1134 static unsigned char corruptTGA
[18+1+3] =
1138 10, // RLE compressed image.
1144 1, 0, // Width is 1.
1145 1, 0, // Height is 1.
1146 24, // Bits per pixel.
1149 0xff, // Run length (repeat next pixel 127+1 times).
1150 0xff, 0xff, 0xff // One 24-bit pixel.
1153 wxMemoryInputStream
memIn(corruptTGA
, WXSIZEOF(corruptTGA
));
1154 CPPUNIT_ASSERT(memIn
.IsOk());
1157 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1161 Instead of repeating a pixel 127+1 times, now tell it there will
1162 follow 127+1 uncompressed pixels (while we only should have 1 in total).
1164 corruptTGA
[18] = 0x7f;
1165 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1168 static void TestGIFComment(const wxString
& comment
)
1170 wxImage
image("horse.gif");
1172 image
.SetOption(wxIMAGE_OPTION_GIF_COMMENT
, comment
);
1173 wxMemoryOutputStream memOut
;
1174 CPPUNIT_ASSERT(image
.SaveFile(memOut
, wxBITMAP_TYPE_GIF
));
1176 wxMemoryInputStream
memIn(memOut
);
1177 CPPUNIT_ASSERT( image
.LoadFile(memIn
) );
1179 CPPUNIT_ASSERT_EQUAL(comment
,
1180 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1183 void ImageTestCase::GIFComment()
1185 // Test reading a comment.
1186 wxImage
image("horse.gif");
1187 CPPUNIT_ASSERT_EQUAL(" Imported from GRADATION image: gray",
1188 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1191 // Test writing a comment and reading it back.
1192 TestGIFComment("Giving the GIF a gifted giraffe as a gift");
1195 // Test writing and reading a comment again but with a long comment.
1196 TestGIFComment(wxString(wxT('a'), 256)
1197 + wxString(wxT('b'), 256)
1198 + wxString(wxT('c'), 256));
1201 // Test writing comments in an animated GIF and reading them back.
1202 CPPUNIT_ASSERT( image
.LoadFile("horse.gif") );
1204 wxImageArray images
;
1206 for (i
= 0; i
< 4; ++i
)
1210 images
.Add( images
[i
-1].Rotate90() );
1211 images
[i
].SetPalette(images
[0].GetPalette());
1218 images
[i
].SetOption(wxIMAGE_OPTION_GIF_COMMENT
,
1219 wxString::Format("GIF comment for frame #%d", i
+1));
1224 wxMemoryOutputStream memOut
;
1225 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1227 wxGIFHandler handler
;
1228 wxMemoryInputStream
memIn(memOut
);
1229 CPPUNIT_ASSERT(memIn
.IsOk());
1230 const int imageCount
= handler
.GetImageCount(memIn
);
1231 for (i
= 0; i
< imageCount
; ++i
)
1233 wxFileOffset pos
= memIn
.TellI();
1234 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true /*verbose?*/, i
) );
1236 CPPUNIT_ASSERT_EQUAL(
1237 wxString::Format("GIF comment for frame #%d", i
+1),
1238 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1243 void ImageTestCase::DibPadding()
1246 There used to be an error with calculating the DWORD aligned scan line
1247 pitch for a BMP/ICO resulting in buffer overwrites (with at least MSVC9
1248 Debug this gave a heap corruption assertion when saving the mask of
1249 an ICO). Test for it here.
1251 wxImage
image("horse.gif");
1252 CPPUNIT_ASSERT( image
.IsOk() );
1254 image
= image
.Scale(99, 99);
1256 wxMemoryOutputStream memOut
;
1257 CPPUNIT_ASSERT( image
.SaveFile(memOut
, wxBITMAP_TYPE_ICO
) );
1260 static void CompareBMPImage(const wxString
& file1
, const wxString
& file2
)
1262 wxImage
image1(file1
);
1263 CPPUNIT_ASSERT( image1
.IsOk() );
1265 wxImage
image2(file2
);
1266 CPPUNIT_ASSERT( image2
.IsOk() );
1268 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_BMP
), image1
, 0, &image2
);
1271 void ImageTestCase::BMPFlippingAndRLECompression()
1273 CompareBMPImage("image/horse_grey.bmp", "image/horse_grey_flipped.bmp");
1275 CompareBMPImage("image/horse_rle8.bmp", "image/horse_grey.bmp");
1276 CompareBMPImage("image/horse_rle8.bmp", "image/horse_rle8_flipped.bmp");
1278 CompareBMPImage("image/horse_rle4.bmp", "image/horse_rle4_flipped.bmp");
1280 #endif //wxUSE_IMAGE
1284 TODO: add lots of more tests to wxImage functions