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
28 #include "wx/palette.h"
31 #include "wx/mstream.h"
32 #include "wx/zstream.h"
33 #include "wx/wfstream.h"
41 { "horse.ico", wxBITMAP_TYPE_ICO
, 4 },
42 { "horse.xpm", wxBITMAP_TYPE_XPM
, 8 },
43 { "horse.png", wxBITMAP_TYPE_PNG
, 24 },
44 { "horse.ani", wxBITMAP_TYPE_ANI
, 24 },
45 { "horse.bmp", wxBITMAP_TYPE_BMP
, 8 },
46 { "horse.cur", wxBITMAP_TYPE_CUR
, 1 },
47 { "horse.gif", wxBITMAP_TYPE_GIF
, 8 },
48 { "horse.jpg", wxBITMAP_TYPE_JPEG
, 24 },
49 { "horse.pcx", wxBITMAP_TYPE_PCX
, 8 },
50 { "horse.pnm", wxBITMAP_TYPE_PNM
, 24 },
51 { "horse.tga", wxBITMAP_TYPE_TGA
, 8 },
52 { "horse.tif", wxBITMAP_TYPE_TIF
, 8 }
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 class ImageTestCase
: public CppUnit::TestCase
67 CPPUNIT_TEST_SUITE( ImageTestCase
);
68 CPPUNIT_TEST( LoadFromSocketStream
);
69 CPPUNIT_TEST( LoadFromZipStream
);
70 CPPUNIT_TEST( LoadFromFile
);
71 CPPUNIT_TEST( SizeImage
);
72 CPPUNIT_TEST( CompareLoadedImage
);
73 CPPUNIT_TEST( CompareSavedImage
);
74 CPPUNIT_TEST( SavePNG
);
75 CPPUNIT_TEST( SaveAnimatedGIF
);
76 CPPUNIT_TEST( ReadCorruptedTGA
);
77 CPPUNIT_TEST( GIFComment
);
78 CPPUNIT_TEST_SUITE_END();
80 void LoadFromSocketStream();
81 void LoadFromZipStream();
84 void CompareLoadedImage();
85 void CompareSavedImage();
87 void SaveAnimatedGIF();
88 void ReadCorruptedTGA();
91 DECLARE_NO_COPY_CLASS(ImageTestCase
)
94 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
95 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
97 ImageTestCase::ImageTestCase()
99 wxSocketBase::Initialize();
101 // the formats we're going to test:
102 wxImage::AddHandler(new wxICOHandler
);
103 wxImage::AddHandler(new wxXPMHandler
);
104 wxImage::AddHandler(new wxPNGHandler
);
105 wxImage::AddHandler(new wxANIHandler
);
106 wxImage::AddHandler(new wxBMPHandler
);
107 wxImage::AddHandler(new wxCURHandler
);
108 wxImage::AddHandler(new wxGIFHandler
);
109 wxImage::AddHandler(new wxJPEGHandler
);
110 wxImage::AddHandler(new wxPCXHandler
);
111 wxImage::AddHandler(new wxPNMHandler
);
112 wxImage::AddHandler(new wxTGAHandler
);
113 wxImage::AddHandler(new wxTIFFHandler
);
116 ImageTestCase::~ImageTestCase()
118 wxSocketBase::Shutdown();
121 void ImageTestCase::LoadFromFile()
124 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
125 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
128 void ImageTestCase::LoadFromSocketStream()
130 if (!IsNetworkAvailable()) // implemented in test.cpp
132 wxLogWarning("No network connectivity; skipping the "
133 "ImageTestCase::LoadFromSocketStream test unit.");
142 { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
143 { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
146 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
148 wxURL
url(testData
[i
].url
);
149 WX_ASSERT_EQUAL_MESSAGE
151 ("Constructing URL \"%s\" failed.", testData
[i
].url
),
156 wxInputStream
*in_stream
= url
.GetInputStream();
159 ("Opening URL \"%s\" failed.", testData
[i
].url
),
160 in_stream
&& in_stream
->IsOk()
165 // NOTE: it's important to inform wxImage about the type of the image being
166 // loaded otherwise it will try to autodetect the format, but that
167 // requires a seekable stream!
170 ("Loading image from \"%s\" failed.", testData
[i
].url
),
171 img
.LoadFile(*in_stream
, testData
[i
].type
)
178 void ImageTestCase::LoadFromZipStream()
180 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
182 switch (g_testfiles
[i
].type
)
184 case wxBITMAP_TYPE_XPM
:
185 case wxBITMAP_TYPE_GIF
:
186 case wxBITMAP_TYPE_PCX
:
187 case wxBITMAP_TYPE_TGA
:
188 case wxBITMAP_TYPE_TIF
:
189 continue; // skip testing those wxImageHandlers which cannot
190 // load data from non-seekable streams
196 // compress the test file on the fly:
197 wxMemoryOutputStream memOut
;
199 wxFileInputStream
file(g_testfiles
[i
].file
);
200 CPPUNIT_ASSERT(file
.IsOk());
202 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
203 CPPUNIT_ASSERT(compressFilter
.IsOk());
205 file
.Read(compressFilter
);
206 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
209 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
210 // allows us to test loading images from non-seekable streams other than socket streams
211 wxMemoryInputStream
memIn(memOut
);
212 CPPUNIT_ASSERT(memIn
.IsOk());
213 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
214 CPPUNIT_ASSERT(decompressFilter
.IsOk());
218 // NOTE: it's important to inform wxImage about the type of the image being
219 // loaded otherwise it will try to autodetect the format, but that
220 // requires a seekable stream!
221 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
222 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
226 void ImageTestCase::SizeImage()
228 // Test the wxImage::Size() function which takes a rectangle from source and
229 // places it in a new image at a given position. This test checks, if the
230 // correct areas are chosen, and clipping is done correctly.
233 static const char * xpm_orig
[] = {
234 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
246 // the expected results for all tests:
247 static const char * xpm_l_t
[] = {
248 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
260 static const char * xpm_t
[] = {
261 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
273 static const char * xpm_r_t
[] = {
274 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
286 static const char * xpm_l
[] = {
287 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
299 static const char * xpm_r
[] = {
300 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
312 static const char * xpm_l_b
[] = {
313 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
325 static const char * xpm_b
[] = {
326 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
338 static const char * xpm_r_b
[] = {
339 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
351 static const char * xpm_sm
[] = {
352 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
362 static const char * xpm_gt
[] = {
363 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
377 static const char * xpm_gt_l_t
[] = {
378 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
392 static const char * xpm_gt_l
[] = {
393 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
407 static const char * xpm_gt_l_b
[] = {
408 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
422 static const char * xpm_gt_l_bb
[] = {
423 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
437 static const char * xpm_gt_t
[] = {
438 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
452 static const char * xpm_gt_b
[] = {
453 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
467 static const char * xpm_gt_bb
[] = {
468 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
482 static const char * xpm_gt_r_t
[] = {
483 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
497 static const char * xpm_gt_r
[] = {
498 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
512 static const char * xpm_gt_r_b
[] = {
513 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
527 static const char * xpm_gt_r_bb
[] = {
528 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
542 static const char * xpm_gt_rr_t
[] = {
543 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
557 static const char * xpm_gt_rr
[] = {
558 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
572 static const char * xpm_gt_rr_b
[] = {
573 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
587 static const char * xpm_gt_rr_bb
[] = {
588 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
602 static const char * xpm_sm_ll_tt
[] = {
603 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
613 static const char * xpm_sm_ll_t
[] = {
614 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
624 static const char * xpm_sm_ll
[] = {
625 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
635 static const char * xpm_sm_ll_b
[] = {
636 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
646 static const char * xpm_sm_l_tt
[] = {
647 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
657 static const char * xpm_sm_l_t
[] = {
658 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
668 static const char * xpm_sm_l
[] = {
669 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
679 static const char * xpm_sm_l_b
[] = {
680 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
690 static const char * xpm_sm_tt
[] = {
691 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
701 static const char * xpm_sm_t
[] = {
702 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
712 static const char * xpm_sm_b
[] = {
713 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
723 static const char * xpm_sm_r_tt
[] = {
724 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
734 static const char * xpm_sm_r_t
[] = {
735 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
745 static const char * xpm_sm_r
[] = {
746 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
756 static const char * xpm_sm_r_b
[] = {
757 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
768 // this table defines all tests
771 int w
, h
, dx
, dy
; // first parameters for Size()
772 const char **ref_xpm
; // expected result
775 { 10, 10, 0, 0, xpm_orig
}, // same size, same position
776 { 12, 12, 0, 0, xpm_gt
}, // target larger, same position
777 { 8, 8, 0, 0, xpm_sm
}, // target smaller, same position
778 { 10, 10, -2, -2, xpm_l_t
}, // same size, move left up
779 { 10, 10, -2, 0, xpm_l
}, // same size, move left
780 { 10, 10, -2, 2, xpm_l_b
}, // same size, move left down
781 { 10, 10, 0, -2, xpm_t
}, // same size, move up
782 { 10, 10, 0, 2, xpm_b
}, // same size, move down
783 { 10, 10, 2, -2, xpm_r_t
}, // same size, move right up
784 { 10, 10, 2, 0, xpm_r
}, // same size, move right
785 { 10, 10, 2, 2, xpm_r_b
}, // same size, move right down
786 { 12, 12, -2, -2, xpm_gt_l_t
}, // target larger, move left up
787 { 12, 12, -2, 0, xpm_gt_l
}, // target larger, move left
788 { 12, 12, -2, 2, xpm_gt_l_b
}, // target larger, move left down
789 { 12, 12, -2, 4, xpm_gt_l_bb
}, // target larger, move left down
790 { 12, 12, 0, -2, xpm_gt_t
}, // target larger, move up
791 { 12, 12, 0, 2, xpm_gt_b
}, // target larger, move down
792 { 12, 12, 0, 4, xpm_gt_bb
}, // target larger, move down
793 { 12, 12, 2, -2, xpm_gt_r_t
}, // target larger, move right up
794 { 12, 12, 2, 0, xpm_gt_r
}, // target larger, move right
795 { 12, 12, 2, 2, xpm_gt_r_b
}, // target larger, move right down
796 { 12, 12, 2, 4, xpm_gt_r_bb
}, // target larger, move right down
797 { 12, 12, 4, -2, xpm_gt_rr_t
}, // target larger, move right up
798 { 12, 12, 4, 0, xpm_gt_rr
}, // target larger, move right
799 { 12, 12, 4, 2, xpm_gt_rr_b
}, // target larger, move right down
800 { 12, 12, 4, 4, xpm_gt_rr_bb
}, // target larger, move right down
801 { 8, 8, -4, -4, xpm_sm_ll_tt
}, // target smaller, move left up
802 { 8, 8, -4, -2, xpm_sm_ll_t
}, // target smaller, move left up
803 { 8, 8, -4, 0, xpm_sm_ll
}, // target smaller, move left
804 { 8, 8, -4, 2, xpm_sm_ll_b
}, // target smaller, move left down
805 { 8, 8, -2, -4, xpm_sm_l_tt
}, // target smaller, move left up
806 { 8, 8, -2, -2, xpm_sm_l_t
}, // target smaller, move left up
807 { 8, 8, -2, 0, xpm_sm_l
}, // target smaller, move left
808 { 8, 8, -2, 2, xpm_sm_l_b
}, // target smaller, move left down
809 { 8, 8, 0, -4, xpm_sm_tt
}, // target smaller, move up
810 { 8, 8, 0, -2, xpm_sm_t
}, // target smaller, move up
811 { 8, 8, 0, 2, xpm_sm_b
}, // target smaller, move down
812 { 8, 8, 2, -4, xpm_sm_r_tt
}, // target smaller, move right up
813 { 8, 8, 2, -2, xpm_sm_r_t
}, // target smaller, move right up
814 { 8, 8, 2, 0, xpm_sm_r
}, // target smaller, move right
815 { 8, 8, 2, 2, xpm_sm_r_b
}, // target smaller, move right down
818 const wxImage
src_img(xpm_orig
);
819 for ( unsigned i
= 0; i
< WXSIZEOF(sizeTestData
); i
++ )
821 SizeTestData
& st
= sizeTestData
[i
];
823 actual(src_img
.Size(wxSize(st
.w
, st
.h
), wxPoint(st
.dx
, st
.dy
), 0, 0, 0)),
824 expected(st
.ref_xpm
);
826 // to check results with an image viewer uncomment this:
827 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
828 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
830 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().x
, expected
.GetSize().x
);
831 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().y
, expected
.GetSize().y
);
833 const unsigned data_len
= 3 * expected
.GetHeight() * expected
.GetWidth();
837 ("Resize test #%u: (%d, %d), (%d, %d)", i
, st
.w
, st
.h
, st
.dx
, st
.dy
),
838 memcmp(actual
.GetData(), expected
.GetData(), data_len
) == 0
843 void ImageTestCase::CompareLoadedImage()
845 wxImage
expected8("horse.xpm");
846 CPPUNIT_ASSERT( expected8
.IsOk() );
848 wxImage
expected24("horse.png");
849 CPPUNIT_ASSERT( expected24
.IsOk() );
851 const size_t dataLen
= expected8
.GetWidth() * expected8
.GetHeight() * 3;
853 for (size_t i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
855 if ( !(g_testfiles
[i
].bitDepth
== 8 || g_testfiles
[i
].bitDepth
== 24)
856 || g_testfiles
[i
].type
== wxBITMAP_TYPE_JPEG
/*skip lossy JPEG*/)
861 wxImage
actual(g_testfiles
[i
].file
);
863 if ( actual
.GetSize() != expected8
.GetSize() )
871 ("Compare test '%s' for loading failed", g_testfiles
[i
].file
),
873 memcmp(actual
.GetData(),
874 (g_testfiles
[i
].bitDepth
== 8)
875 ? expected8
.GetData()
876 : expected24
.GetData(),
885 wxIMAGE_HAVE_ALPHA
= (1 << 0),
886 wxIMAGE_HAVE_PALETTE
= (1 << 1)
890 void CompareImage(const wxImageHandler
& handler
, const wxImage
& image
,
891 int properties
= 0, const wxImage
*compareTo
= NULL
)
893 wxBitmapType type
= handler
.GetType();
895 const bool testPalette
= (properties
& wxIMAGE_HAVE_PALETTE
) != 0;
897 This is getting messy and should probably be transformed into a table
898 with image format features before it gets hairier.
901 && ( !(type
== wxBITMAP_TYPE_BMP
902 || type
== wxBITMAP_TYPE_GIF
903 || type
== wxBITMAP_TYPE_PNG
)
904 || type
== wxBITMAP_TYPE_XPM
) )
909 const bool testAlpha
= (properties
& wxIMAGE_HAVE_ALPHA
) != 0;
911 && !(type
== wxBITMAP_TYPE_PNG
|| type
== wxBITMAP_TYPE_TGA
) )
913 // don't test images with alpha if this handler doesn't support alpha
917 if (type
== wxBITMAP_TYPE_JPEG
/* skip lossy JPEG */
918 || type
== wxBITMAP_TYPE_TIF
)
921 TIFF is skipped because the memory stream can't be loaded. Libtiff
922 looks for a TIFF directory at offset 120008 while the memory
923 stream size is only 120008 bytes (when saving as a file
924 the file size is 120280 bytes).
929 wxMemoryOutputStream memOut
;
930 if ( !image
.SaveFile(memOut
, type
) )
932 // Unfortunately we can't know if the handler just doesn't support
933 // saving images, or if it failed to save.
937 wxMemoryInputStream
memIn(memOut
);
938 CPPUNIT_ASSERT(memIn
.IsOk());
940 wxImage
actual(memIn
);
941 CPPUNIT_ASSERT(actual
.IsOk());
943 const wxImage
*expected
= compareTo
? compareTo
: &image
;
944 CPPUNIT_ASSERT( actual
.GetSize() == expected
->GetSize() );
946 unsigned bitsPerPixel
= testPalette
? 8 : (testAlpha
? 32 : 24);
949 ("Compare test '%s (%d-bit)' for saving failed",
950 handler
.GetExtension(), bitsPerPixel
),
952 memcmp(actual
.GetData(), expected
->GetData(),
953 expected
->GetWidth() * expected
->GetHeight() * 3) == 0
957 CPPUNIT_ASSERT(actual
.HasPalette()
958 == (testPalette
|| type
== wxBITMAP_TYPE_XPM
));
961 CPPUNIT_ASSERT( actual
.HasAlpha() == testAlpha
);
970 ("Compare alpha test '%s' for saving failed", handler
.GetExtension()),
972 memcmp(actual
.GetAlpha(), expected
->GetAlpha(),
973 expected
->GetWidth() * expected
->GetHeight()) == 0
977 void ImageTestCase::CompareSavedImage()
979 // FIXME-VC6: Pre-declare the loop variables for compatibility with
980 // pre-standard compilers such as MSVC6 that don't implement proper scope
981 // for the variables declared in the for loops.
984 wxImage
expected24("horse.png");
985 CPPUNIT_ASSERT( expected24
.IsOk() );
986 CPPUNIT_ASSERT( !expected24
.HasAlpha() );
988 wxImage expected8
= expected24
.ConvertToGreyscale();
991 unsigned char greys
[256];
992 for (i
= 0; i
< 256; ++i
)
996 wxPalette
palette(256, greys
, greys
, greys
);
997 expected8
.SetPalette(palette
);
998 #endif // #if wxUSE_PALETTE
1000 expected8
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP_PALETTE
);
1002 // Create an image with alpha based on the loaded image
1003 wxImage
expected32(expected24
);
1004 expected32
.SetAlpha();
1006 int width
= expected32
.GetWidth();
1007 int height
= expected32
.GetHeight();
1008 for (y
= 0; y
< height
; ++y
)
1010 for (x
= 0; x
< width
; ++x
)
1012 expected32
.SetAlpha(x
, y
, (x
*y
) & wxIMAGE_ALPHA_OPAQUE
);
1016 const wxList
& list
= wxImage::GetHandlers();
1017 for ( wxList::compatibility_iterator node
= list
.GetFirst();
1018 node
; node
= node
->GetNext() )
1020 wxImageHandler
*handler
= (wxImageHandler
*) node
->GetData();
1023 CompareImage(*handler
, expected8
, wxIMAGE_HAVE_PALETTE
);
1025 CompareImage(*handler
, expected24
);
1026 CompareImage(*handler
, expected32
, wxIMAGE_HAVE_ALPHA
);
1030 void ImageTestCase::SavePNG()
1032 wxImage
expected24("horse.png");
1033 CPPUNIT_ASSERT( expected24
.IsOk() );
1035 CPPUNIT_ASSERT( !expected24
.HasPalette() );
1036 #endif // #if wxUSE_PALETTE
1038 wxImage expected8
= expected24
.ConvertToGreyscale();
1041 horse.png converted to greyscale should be saved without a palette.
1043 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
), expected8
);
1046 But if we explicitly ask for trying to save with a palette, it should work.
1048 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1050 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1051 expected8
, wxIMAGE_HAVE_PALETTE
);
1054 CPPUNIT_ASSERT( expected8
.LoadFile("horse.gif") );
1056 CPPUNIT_ASSERT( expected8
.HasPalette() );
1057 #endif // #if wxUSE_PALETTE
1059 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1060 expected8
, wxIMAGE_HAVE_PALETTE
);
1063 Add alpha to the image in such a way that there will still be a maximum
1064 of 256 unique RGBA combinations. This should result in a saved
1065 PNG image still being palettised and having alpha.
1067 expected8
.SetAlpha();
1070 const int width
= expected8
.GetWidth();
1071 const int height
= expected8
.GetHeight();
1072 for (y
= 0; y
< height
; ++y
)
1074 for (x
= 0; x
< width
; ++x
)
1076 expected8
.SetAlpha(x
, y
, expected8
.GetRed(x
, y
));
1080 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1081 expected8
, wxIMAGE_HAVE_ALPHA
|wxIMAGE_HAVE_PALETTE
);
1084 Now change the alpha of the first pixel so that we can't save palettised
1085 anymore because there will be 256+1 entries which is beyond PNGs limit
1088 expected8
.SetAlpha(0, 0, 1);
1090 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1091 expected8
, wxIMAGE_HAVE_ALPHA
);
1094 Even if we explicitly ask for saving palettised it should not be done.
1096 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1097 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1098 expected8
, wxIMAGE_HAVE_ALPHA
);
1102 void ImageTestCase::SaveAnimatedGIF()
1105 wxImage
image("horse.gif");
1106 CPPUNIT_ASSERT( image
.IsOk() );
1108 wxImageArray images
;
1111 for (i
= 0; i
< 4-1; ++i
)
1113 images
.Add( images
[i
].Rotate90() );
1115 images
[i
+1].SetPalette(images
[0].GetPalette());
1118 wxMemoryOutputStream memOut
;
1119 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1121 wxGIFHandler handler
;
1122 wxMemoryInputStream
memIn(memOut
);
1123 CPPUNIT_ASSERT(memIn
.IsOk());
1124 const int imageCount
= handler
.GetImageCount(memIn
);
1125 CPPUNIT_ASSERT_EQUAL(4, imageCount
);
1127 for (i
= 0; i
< imageCount
; ++i
)
1129 wxFileOffset pos
= memIn
.TellI();
1130 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true, i
) );
1135 ("Compare test for GIF frame number %d failed", i
),
1136 memcmp(image
.GetData(), images
[i
].GetData(),
1137 images
[i
].GetWidth() * images
[i
].GetHeight() * 3) == 0
1140 #endif // #if wxUSE_PALETTE
1143 void ImageTestCase::ReadCorruptedTGA()
1145 static unsigned char corruptTGA
[18+1+3] =
1149 10, // RLE compressed image.
1155 1, 0, // Width is 1.
1156 1, 0, // Height is 1.
1157 24, // Bits per pixel.
1160 0xff, // Run length (repeat next pixel 127+1 times).
1161 0xff, 0xff, 0xff // One 24-bit pixel.
1164 wxMemoryInputStream
memIn(corruptTGA
, WXSIZEOF(corruptTGA
));
1165 CPPUNIT_ASSERT(memIn
.IsOk());
1168 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1172 Instead of repeating a pixel 127+1 times, now tell it there will
1173 follow 127+1 uncompressed pixels (while we only should have 1 in total).
1175 corruptTGA
[18] = 0x7f;
1176 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1179 static void TestGIFComment(const wxString
& comment
)
1181 wxImage
image("horse.gif");
1183 image
.SetOption(wxIMAGE_OPTION_GIF_COMMENT
, comment
);
1184 wxMemoryOutputStream memOut
;
1185 CPPUNIT_ASSERT(image
.SaveFile(memOut
, wxBITMAP_TYPE_GIF
));
1187 wxMemoryInputStream
memIn(memOut
);
1188 CPPUNIT_ASSERT( image
.LoadFile(memIn
) );
1190 CPPUNIT_ASSERT_EQUAL(comment
,
1191 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1194 void ImageTestCase::GIFComment()
1196 // Test reading a comment.
1197 wxImage
image("horse.gif");
1198 CPPUNIT_ASSERT_EQUAL(" Imported from GRADATION image: gray",
1199 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1202 // Test writing a comment and reading it back.
1203 TestGIFComment("Giving the GIF a gifted giraffe as a gift");
1206 // Test writing and reading a comment again but with a long comment.
1207 TestGIFComment(wxString(wxT('a'), 256)
1208 + wxString(wxT('b'), 256)
1209 + wxString(wxT('c'), 256));
1212 // Test writing comments in an animated GIF and reading them back.
1213 CPPUNIT_ASSERT( image
.LoadFile("horse.gif") );
1215 wxImageArray images
;
1217 for (i
= 0; i
< 4; ++i
)
1221 images
.Add( images
[i
-1].Rotate90() );
1222 images
[i
].SetPalette(images
[0].GetPalette());
1229 images
[i
].SetOption(wxIMAGE_OPTION_GIF_COMMENT
,
1230 wxString::Format("GIF comment for frame #%d", i
+1));
1235 wxMemoryOutputStream memOut
;
1236 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1238 wxGIFHandler handler
;
1239 wxMemoryInputStream
memIn(memOut
);
1240 CPPUNIT_ASSERT(memIn
.IsOk());
1241 const int imageCount
= handler
.GetImageCount(memIn
);
1242 for (i
= 0; i
< imageCount
; ++i
)
1244 wxFileOffset pos
= memIn
.TellI();
1245 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true /*verbose?*/, i
) );
1247 CPPUNIT_ASSERT_EQUAL(
1248 wxString::Format("GIF comment for frame #%d", i
+1),
1249 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1254 #endif //wxUSE_IMAGE
1258 TODO: add lots of more tests to wxImage functions