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_SUITE_END();
82 void LoadFromSocketStream();
83 void LoadFromZipStream();
86 void CompareLoadedImage();
87 void CompareSavedImage();
89 void SaveAnimatedGIF();
90 void ReadCorruptedTGA();
94 DECLARE_NO_COPY_CLASS(ImageTestCase
)
97 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
98 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
100 ImageTestCase::ImageTestCase()
102 wxSocketBase::Initialize();
104 // the formats we're going to test:
105 wxImage::AddHandler(new wxICOHandler
);
106 wxImage::AddHandler(new wxXPMHandler
);
107 wxImage::AddHandler(new wxPNGHandler
);
108 wxImage::AddHandler(new wxANIHandler
);
109 wxImage::AddHandler(new wxBMPHandler
);
110 wxImage::AddHandler(new wxCURHandler
);
111 wxImage::AddHandler(new wxGIFHandler
);
112 wxImage::AddHandler(new wxJPEGHandler
);
113 wxImage::AddHandler(new wxPCXHandler
);
114 wxImage::AddHandler(new wxPNMHandler
);
115 wxImage::AddHandler(new wxTGAHandler
);
116 wxImage::AddHandler(new wxTIFFHandler
);
119 ImageTestCase::~ImageTestCase()
121 wxSocketBase::Shutdown();
124 void ImageTestCase::LoadFromFile()
127 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
128 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
131 void ImageTestCase::LoadFromSocketStream()
133 if (!IsNetworkAvailable()) // implemented in test.cpp
135 wxLogWarning("No network connectivity; skipping the "
136 "ImageTestCase::LoadFromSocketStream test unit.");
145 { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
146 { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
149 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
151 wxURL
url(testData
[i
].url
);
152 WX_ASSERT_EQUAL_MESSAGE
154 ("Constructing URL \"%s\" failed.", testData
[i
].url
),
159 wxInputStream
*in_stream
= url
.GetInputStream();
162 ("Opening URL \"%s\" failed.", testData
[i
].url
),
163 in_stream
&& in_stream
->IsOk()
168 // NOTE: it's important to inform wxImage about the type of the image being
169 // loaded otherwise it will try to autodetect the format, but that
170 // requires a seekable stream!
173 ("Loading image from \"%s\" failed.", testData
[i
].url
),
174 img
.LoadFile(*in_stream
, testData
[i
].type
)
181 void ImageTestCase::LoadFromZipStream()
183 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
185 switch (g_testfiles
[i
].type
)
187 case wxBITMAP_TYPE_XPM
:
188 case wxBITMAP_TYPE_GIF
:
189 case wxBITMAP_TYPE_PCX
:
190 case wxBITMAP_TYPE_TGA
:
191 case wxBITMAP_TYPE_TIF
:
192 continue; // skip testing those wxImageHandlers which cannot
193 // load data from non-seekable streams
199 // compress the test file on the fly:
200 wxMemoryOutputStream memOut
;
202 wxFileInputStream
file(g_testfiles
[i
].file
);
203 CPPUNIT_ASSERT(file
.IsOk());
205 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
206 CPPUNIT_ASSERT(compressFilter
.IsOk());
208 file
.Read(compressFilter
);
209 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
212 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
213 // allows us to test loading images from non-seekable streams other than socket streams
214 wxMemoryInputStream
memIn(memOut
);
215 CPPUNIT_ASSERT(memIn
.IsOk());
216 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
217 CPPUNIT_ASSERT(decompressFilter
.IsOk());
221 // NOTE: it's important to inform wxImage about the type of the image being
222 // loaded otherwise it will try to autodetect the format, but that
223 // requires a seekable stream!
224 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
225 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
229 void ImageTestCase::SizeImage()
231 // Test the wxImage::Size() function which takes a rectangle from source and
232 // places it in a new image at a given position. This test checks, if the
233 // correct areas are chosen, and clipping is done correctly.
236 static const char * xpm_orig
[] = {
237 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
249 // the expected results for all tests:
250 static const char * xpm_l_t
[] = {
251 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
263 static const char * xpm_t
[] = {
264 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
276 static const char * xpm_r_t
[] = {
277 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
289 static const char * xpm_l
[] = {
290 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
302 static const char * xpm_r
[] = {
303 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
315 static const char * xpm_l_b
[] = {
316 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
328 static const char * xpm_b
[] = {
329 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
341 static const char * xpm_r_b
[] = {
342 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
354 static const char * xpm_sm
[] = {
355 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
365 static const char * xpm_gt
[] = {
366 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
380 static const char * xpm_gt_l_t
[] = {
381 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
395 static const char * xpm_gt_l
[] = {
396 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
410 static const char * xpm_gt_l_b
[] = {
411 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
425 static const char * xpm_gt_l_bb
[] = {
426 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
440 static const char * xpm_gt_t
[] = {
441 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
455 static const char * xpm_gt_b
[] = {
456 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
470 static const char * xpm_gt_bb
[] = {
471 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
485 static const char * xpm_gt_r_t
[] = {
486 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
500 static const char * xpm_gt_r
[] = {
501 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
515 static const char * xpm_gt_r_b
[] = {
516 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
530 static const char * xpm_gt_r_bb
[] = {
531 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
545 static const char * xpm_gt_rr_t
[] = {
546 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
560 static const char * xpm_gt_rr
[] = {
561 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
575 static const char * xpm_gt_rr_b
[] = {
576 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
590 static const char * xpm_gt_rr_bb
[] = {
591 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
605 static const char * xpm_sm_ll_tt
[] = {
606 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
616 static const char * xpm_sm_ll_t
[] = {
617 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
627 static const char * xpm_sm_ll
[] = {
628 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
638 static const char * xpm_sm_ll_b
[] = {
639 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
649 static const char * xpm_sm_l_tt
[] = {
650 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
660 static const char * xpm_sm_l_t
[] = {
661 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
671 static const char * xpm_sm_l
[] = {
672 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
682 static const char * xpm_sm_l_b
[] = {
683 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
693 static const char * xpm_sm_tt
[] = {
694 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
704 static const char * xpm_sm_t
[] = {
705 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
715 static const char * xpm_sm_b
[] = {
716 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
726 static const char * xpm_sm_r_tt
[] = {
727 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
737 static const char * xpm_sm_r_t
[] = {
738 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
748 static const char * xpm_sm_r
[] = {
749 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
759 static const char * xpm_sm_r_b
[] = {
760 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
771 // this table defines all tests
774 int w
, h
, dx
, dy
; // first parameters for Size()
775 const char **ref_xpm
; // expected result
778 { 10, 10, 0, 0, xpm_orig
}, // same size, same position
779 { 12, 12, 0, 0, xpm_gt
}, // target larger, same position
780 { 8, 8, 0, 0, xpm_sm
}, // target smaller, same position
781 { 10, 10, -2, -2, xpm_l_t
}, // same size, move left up
782 { 10, 10, -2, 0, xpm_l
}, // same size, move left
783 { 10, 10, -2, 2, xpm_l_b
}, // same size, move left down
784 { 10, 10, 0, -2, xpm_t
}, // same size, move up
785 { 10, 10, 0, 2, xpm_b
}, // same size, move down
786 { 10, 10, 2, -2, xpm_r_t
}, // same size, move right up
787 { 10, 10, 2, 0, xpm_r
}, // same size, move right
788 { 10, 10, 2, 2, xpm_r_b
}, // same size, move right down
789 { 12, 12, -2, -2, xpm_gt_l_t
}, // target larger, move left up
790 { 12, 12, -2, 0, xpm_gt_l
}, // target larger, move left
791 { 12, 12, -2, 2, xpm_gt_l_b
}, // target larger, move left down
792 { 12, 12, -2, 4, xpm_gt_l_bb
}, // target larger, move left down
793 { 12, 12, 0, -2, xpm_gt_t
}, // target larger, move up
794 { 12, 12, 0, 2, xpm_gt_b
}, // target larger, move down
795 { 12, 12, 0, 4, xpm_gt_bb
}, // target larger, move down
796 { 12, 12, 2, -2, xpm_gt_r_t
}, // target larger, move right up
797 { 12, 12, 2, 0, xpm_gt_r
}, // target larger, move right
798 { 12, 12, 2, 2, xpm_gt_r_b
}, // target larger, move right down
799 { 12, 12, 2, 4, xpm_gt_r_bb
}, // target larger, move right down
800 { 12, 12, 4, -2, xpm_gt_rr_t
}, // target larger, move right up
801 { 12, 12, 4, 0, xpm_gt_rr
}, // target larger, move right
802 { 12, 12, 4, 2, xpm_gt_rr_b
}, // target larger, move right down
803 { 12, 12, 4, 4, xpm_gt_rr_bb
}, // target larger, move right down
804 { 8, 8, -4, -4, xpm_sm_ll_tt
}, // target smaller, move left up
805 { 8, 8, -4, -2, xpm_sm_ll_t
}, // target smaller, move left up
806 { 8, 8, -4, 0, xpm_sm_ll
}, // target smaller, move left
807 { 8, 8, -4, 2, xpm_sm_ll_b
}, // target smaller, move left down
808 { 8, 8, -2, -4, xpm_sm_l_tt
}, // target smaller, move left up
809 { 8, 8, -2, -2, xpm_sm_l_t
}, // target smaller, move left up
810 { 8, 8, -2, 0, xpm_sm_l
}, // target smaller, move left
811 { 8, 8, -2, 2, xpm_sm_l_b
}, // target smaller, move left down
812 { 8, 8, 0, -4, xpm_sm_tt
}, // target smaller, move up
813 { 8, 8, 0, -2, xpm_sm_t
}, // target smaller, move up
814 { 8, 8, 0, 2, xpm_sm_b
}, // target smaller, move down
815 { 8, 8, 2, -4, xpm_sm_r_tt
}, // target smaller, move right up
816 { 8, 8, 2, -2, xpm_sm_r_t
}, // target smaller, move right up
817 { 8, 8, 2, 0, xpm_sm_r
}, // target smaller, move right
818 { 8, 8, 2, 2, xpm_sm_r_b
}, // target smaller, move right down
821 const wxImage
src_img(xpm_orig
);
822 for ( unsigned i
= 0; i
< WXSIZEOF(sizeTestData
); i
++ )
824 SizeTestData
& st
= sizeTestData
[i
];
826 actual(src_img
.Size(wxSize(st
.w
, st
.h
), wxPoint(st
.dx
, st
.dy
), 0, 0, 0)),
827 expected(st
.ref_xpm
);
829 // to check results with an image viewer uncomment this:
830 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
831 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
833 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().x
, expected
.GetSize().x
);
834 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().y
, expected
.GetSize().y
);
836 WX_ASSERT_EQUAL_MESSAGE
838 ("Resize test #%u: (%d, %d), (%d, %d)", i
, st
.w
, st
.h
, st
.dx
, st
.dy
),
844 void ImageTestCase::CompareLoadedImage()
846 wxImage
expected8("horse.xpm");
847 CPPUNIT_ASSERT( expected8
.IsOk() );
849 wxImage
expected24("horse.png");
850 CPPUNIT_ASSERT( expected24
.IsOk() );
852 for (size_t i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
854 if ( !(g_testfiles
[i
].bitDepth
== 8 || g_testfiles
[i
].bitDepth
== 24)
855 || g_testfiles
[i
].type
== wxBITMAP_TYPE_JPEG
/*skip lossy JPEG*/)
860 wxImage
actual(g_testfiles
[i
].file
);
862 if ( actual
.GetSize() != expected8
.GetSize() )
868 WX_ASSERT_EQUAL_MESSAGE
870 ("Compare test '%s' for loading failed", g_testfiles
[i
].file
),
871 g_testfiles
[i
].bitDepth
== 8 ? expected8
: expected24
,
880 wxIMAGE_HAVE_ALPHA
= (1 << 0),
881 wxIMAGE_HAVE_PALETTE
= (1 << 1)
885 void CompareImage(const wxImageHandler
& handler
, const wxImage
& image
,
886 int properties
= 0, const wxImage
*compareTo
= NULL
)
888 wxBitmapType type
= handler
.GetType();
890 const bool testPalette
= (properties
& wxIMAGE_HAVE_PALETTE
) != 0;
892 This is getting messy and should probably be transformed into a table
893 with image format features before it gets hairier.
896 && ( !(type
== wxBITMAP_TYPE_BMP
897 || type
== wxBITMAP_TYPE_GIF
898 || type
== wxBITMAP_TYPE_ICO
899 || type
== wxBITMAP_TYPE_PNG
)
900 || type
== wxBITMAP_TYPE_XPM
) )
905 const bool testAlpha
= (properties
& wxIMAGE_HAVE_ALPHA
) != 0;
907 && !(type
== wxBITMAP_TYPE_PNG
|| type
== wxBITMAP_TYPE_TGA
) )
909 // don't test images with alpha if this handler doesn't support alpha
913 if (type
== wxBITMAP_TYPE_JPEG
/* skip lossy JPEG */
914 || type
== wxBITMAP_TYPE_TIF
)
917 TIFF is skipped because the memory stream can't be loaded. Libtiff
918 looks for a TIFF directory at offset 120008 while the memory
919 stream size is only 120008 bytes (when saving as a file
920 the file size is 120280 bytes).
925 wxMemoryOutputStream memOut
;
926 if ( !image
.SaveFile(memOut
, type
) )
928 // Unfortunately we can't know if the handler just doesn't support
929 // saving images, or if it failed to save.
933 wxMemoryInputStream
memIn(memOut
);
934 CPPUNIT_ASSERT(memIn
.IsOk());
936 wxImage
actual(memIn
);
937 CPPUNIT_ASSERT(actual
.IsOk());
939 const wxImage
*expected
= compareTo
? compareTo
: &image
;
940 CPPUNIT_ASSERT( actual
.GetSize() == expected
->GetSize() );
942 unsigned bitsPerPixel
= testPalette
? 8 : (testAlpha
? 32 : 24);
943 WX_ASSERT_EQUAL_MESSAGE
945 ("Compare test '%s (%d-bit)' for saving failed",
946 handler
.GetExtension(), bitsPerPixel
),
952 CPPUNIT_ASSERT(actual
.HasPalette()
953 == (testPalette
|| type
== wxBITMAP_TYPE_XPM
));
956 CPPUNIT_ASSERT( actual
.HasAlpha() == testAlpha
);
963 WX_ASSERT_EQUAL_MESSAGE
965 ("Compare alpha test '%s' for saving failed", handler
.GetExtension()),
971 void ImageTestCase::CompareSavedImage()
973 // FIXME-VC6: Pre-declare the loop variables for compatibility with
974 // pre-standard compilers such as MSVC6 that don't implement proper scope
975 // for the variables declared in the for loops.
978 wxImage
expected24("horse.png");
979 CPPUNIT_ASSERT( expected24
.IsOk() );
980 CPPUNIT_ASSERT( !expected24
.HasAlpha() );
982 wxImage expected8
= expected24
.ConvertToGreyscale();
985 unsigned char greys
[256];
986 for (i
= 0; i
< 256; ++i
)
990 wxPalette
palette(256, greys
, greys
, greys
);
991 expected8
.SetPalette(palette
);
992 #endif // #if wxUSE_PALETTE
994 expected8
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP_PALETTE
);
996 // Create an image with alpha based on the loaded image
997 wxImage
expected32(expected24
);
998 expected32
.SetAlpha();
1000 int width
= expected32
.GetWidth();
1001 int height
= expected32
.GetHeight();
1002 for (y
= 0; y
< height
; ++y
)
1004 for (x
= 0; x
< width
; ++x
)
1006 expected32
.SetAlpha(x
, y
, (x
*y
) & wxIMAGE_ALPHA_OPAQUE
);
1010 const wxList
& list
= wxImage::GetHandlers();
1011 for ( wxList::compatibility_iterator node
= list
.GetFirst();
1012 node
; node
= node
->GetNext() )
1014 wxImageHandler
*handler
= (wxImageHandler
*) node
->GetData();
1017 CompareImage(*handler
, expected8
, wxIMAGE_HAVE_PALETTE
);
1019 CompareImage(*handler
, expected24
);
1020 CompareImage(*handler
, expected32
, wxIMAGE_HAVE_ALPHA
);
1024 void ImageTestCase::SavePNG()
1026 wxImage
expected24("horse.png");
1027 CPPUNIT_ASSERT( expected24
.IsOk() );
1029 CPPUNIT_ASSERT( !expected24
.HasPalette() );
1030 #endif // #if wxUSE_PALETTE
1032 wxImage expected8
= expected24
.ConvertToGreyscale();
1035 horse.png converted to greyscale should be saved without a palette.
1037 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
), expected8
);
1040 But if we explicitly ask for trying to save with a palette, it should work.
1042 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1044 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1045 expected8
, wxIMAGE_HAVE_PALETTE
);
1048 CPPUNIT_ASSERT( expected8
.LoadFile("horse.gif") );
1050 CPPUNIT_ASSERT( expected8
.HasPalette() );
1051 #endif // #if wxUSE_PALETTE
1053 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1054 expected8
, wxIMAGE_HAVE_PALETTE
);
1057 Add alpha to the image in such a way that there will still be a maximum
1058 of 256 unique RGBA combinations. This should result in a saved
1059 PNG image still being palettised and having alpha.
1061 expected8
.SetAlpha();
1064 const int width
= expected8
.GetWidth();
1065 const int height
= expected8
.GetHeight();
1066 for (y
= 0; y
< height
; ++y
)
1068 for (x
= 0; x
< width
; ++x
)
1070 expected8
.SetAlpha(x
, y
, expected8
.GetRed(x
, y
));
1074 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1075 expected8
, wxIMAGE_HAVE_ALPHA
|wxIMAGE_HAVE_PALETTE
);
1078 Now change the alpha of the first pixel so that we can't save palettised
1079 anymore because there will be 256+1 entries which is beyond PNGs limit
1082 expected8
.SetAlpha(0, 0, 1);
1084 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1085 expected8
, wxIMAGE_HAVE_ALPHA
);
1088 Even if we explicitly ask for saving palettised it should not be done.
1090 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1091 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1092 expected8
, wxIMAGE_HAVE_ALPHA
);
1096 void ImageTestCase::SaveAnimatedGIF()
1099 wxImage
image("horse.gif");
1100 CPPUNIT_ASSERT( image
.IsOk() );
1102 wxImageArray images
;
1105 for (i
= 0; i
< 4-1; ++i
)
1107 images
.Add( images
[i
].Rotate90() );
1109 images
[i
+1].SetPalette(images
[0].GetPalette());
1112 wxMemoryOutputStream memOut
;
1113 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1115 wxGIFHandler handler
;
1116 wxMemoryInputStream
memIn(memOut
);
1117 CPPUNIT_ASSERT(memIn
.IsOk());
1118 const int imageCount
= handler
.GetImageCount(memIn
);
1119 CPPUNIT_ASSERT_EQUAL(4, imageCount
);
1121 for (i
= 0; i
< imageCount
; ++i
)
1123 wxFileOffset pos
= memIn
.TellI();
1124 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true, i
) );
1127 WX_ASSERT_EQUAL_MESSAGE
1129 ("Compare test for GIF frame number %d failed", i
),
1134 #endif // #if wxUSE_PALETTE
1137 void ImageTestCase::ReadCorruptedTGA()
1139 static unsigned char corruptTGA
[18+1+3] =
1143 10, // RLE compressed image.
1149 1, 0, // Width is 1.
1150 1, 0, // Height is 1.
1151 24, // Bits per pixel.
1154 0xff, // Run length (repeat next pixel 127+1 times).
1155 0xff, 0xff, 0xff // One 24-bit pixel.
1158 wxMemoryInputStream
memIn(corruptTGA
, WXSIZEOF(corruptTGA
));
1159 CPPUNIT_ASSERT(memIn
.IsOk());
1162 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1166 Instead of repeating a pixel 127+1 times, now tell it there will
1167 follow 127+1 uncompressed pixels (while we only should have 1 in total).
1169 corruptTGA
[18] = 0x7f;
1170 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1173 static void TestGIFComment(const wxString
& comment
)
1175 wxImage
image("horse.gif");
1177 image
.SetOption(wxIMAGE_OPTION_GIF_COMMENT
, comment
);
1178 wxMemoryOutputStream memOut
;
1179 CPPUNIT_ASSERT(image
.SaveFile(memOut
, wxBITMAP_TYPE_GIF
));
1181 wxMemoryInputStream
memIn(memOut
);
1182 CPPUNIT_ASSERT( image
.LoadFile(memIn
) );
1184 CPPUNIT_ASSERT_EQUAL(comment
,
1185 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1188 void ImageTestCase::GIFComment()
1190 // Test reading a comment.
1191 wxImage
image("horse.gif");
1192 CPPUNIT_ASSERT_EQUAL(" Imported from GRADATION image: gray",
1193 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1196 // Test writing a comment and reading it back.
1197 TestGIFComment("Giving the GIF a gifted giraffe as a gift");
1200 // Test writing and reading a comment again but with a long comment.
1201 TestGIFComment(wxString(wxT('a'), 256)
1202 + wxString(wxT('b'), 256)
1203 + wxString(wxT('c'), 256));
1206 // Test writing comments in an animated GIF and reading them back.
1207 CPPUNIT_ASSERT( image
.LoadFile("horse.gif") );
1209 wxImageArray images
;
1211 for (i
= 0; i
< 4; ++i
)
1215 images
.Add( images
[i
-1].Rotate90() );
1216 images
[i
].SetPalette(images
[0].GetPalette());
1223 images
[i
].SetOption(wxIMAGE_OPTION_GIF_COMMENT
,
1224 wxString::Format("GIF comment for frame #%d", i
+1));
1229 wxMemoryOutputStream memOut
;
1230 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1232 wxGIFHandler handler
;
1233 wxMemoryInputStream
memIn(memOut
);
1234 CPPUNIT_ASSERT(memIn
.IsOk());
1235 const int imageCount
= handler
.GetImageCount(memIn
);
1236 for (i
= 0; i
< imageCount
; ++i
)
1238 wxFileOffset pos
= memIn
.TellI();
1239 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true /*verbose?*/, i
) );
1241 CPPUNIT_ASSERT_EQUAL(
1242 wxString::Format("GIF comment for frame #%d", i
+1),
1243 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1248 void ImageTestCase::DibPadding()
1251 There used to be an error with calculating the DWORD aligned scan line
1252 pitch for a BMP/ICO resulting in buffer overwrites (with at least MSVC9
1253 Debug this gave a heap corruption assertion when saving the mask of
1254 an ICO). Test for it here.
1256 wxImage
image("horse.gif");
1257 CPPUNIT_ASSERT( image
.IsOk() );
1259 image
= image
.Scale(99, 99);
1261 wxMemoryOutputStream memOut
;
1262 CPPUNIT_ASSERT( image
.SaveFile(memOut
, wxBITMAP_TYPE_ICO
) );
1265 #endif //wxUSE_IMAGE
1269 TODO: add lots of more tests to wxImage functions