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_TIFF
, 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( SaveTIFF
);
77 CPPUNIT_TEST( SaveAnimatedGIF
);
78 CPPUNIT_TEST( ReadCorruptedTGA
);
79 CPPUNIT_TEST( GIFComment
);
80 CPPUNIT_TEST( DibPadding
);
81 CPPUNIT_TEST( BMPFlippingAndRLECompression
);
82 CPPUNIT_TEST_SUITE_END();
84 void LoadFromSocketStream();
85 void LoadFromZipStream();
88 void CompareLoadedImage();
89 void CompareSavedImage();
92 void SaveAnimatedGIF();
93 void ReadCorruptedTGA();
96 void BMPFlippingAndRLECompression();
98 DECLARE_NO_COPY_CLASS(ImageTestCase
)
101 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
102 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
104 ImageTestCase::ImageTestCase()
106 wxSocketBase::Initialize();
108 // the formats we're going to test:
109 wxImage::AddHandler(new wxICOHandler
);
110 wxImage::AddHandler(new wxXPMHandler
);
111 wxImage::AddHandler(new wxPNGHandler
);
112 wxImage::AddHandler(new wxANIHandler
);
113 wxImage::AddHandler(new wxBMPHandler
);
114 wxImage::AddHandler(new wxCURHandler
);
115 wxImage::AddHandler(new wxGIFHandler
);
116 wxImage::AddHandler(new wxJPEGHandler
);
117 wxImage::AddHandler(new wxPCXHandler
);
118 wxImage::AddHandler(new wxPNMHandler
);
119 wxImage::AddHandler(new wxTGAHandler
);
120 wxImage::AddHandler(new wxTIFFHandler
);
123 ImageTestCase::~ImageTestCase()
125 wxSocketBase::Shutdown();
128 void ImageTestCase::LoadFromFile()
131 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
132 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
135 void ImageTestCase::LoadFromSocketStream()
137 if (!IsNetworkAvailable()) // implemented in test.cpp
139 wxLogWarning("No network connectivity; skipping the "
140 "ImageTestCase::LoadFromSocketStream test unit.");
149 { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
150 { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
153 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
155 wxURL
url(testData
[i
].url
);
156 WX_ASSERT_EQUAL_MESSAGE
158 ("Constructing URL \"%s\" failed.", testData
[i
].url
),
163 wxInputStream
*in_stream
= url
.GetInputStream();
166 ("Opening URL \"%s\" failed.", testData
[i
].url
),
167 in_stream
&& in_stream
->IsOk()
172 // NOTE: it's important to inform wxImage about the type of the image being
173 // loaded otherwise it will try to autodetect the format, but that
174 // requires a seekable stream!
177 ("Loading image from \"%s\" failed.", testData
[i
].url
),
178 img
.LoadFile(*in_stream
, testData
[i
].type
)
185 void ImageTestCase::LoadFromZipStream()
187 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
189 switch (g_testfiles
[i
].type
)
191 case wxBITMAP_TYPE_XPM
:
192 case wxBITMAP_TYPE_GIF
:
193 case wxBITMAP_TYPE_PCX
:
194 case wxBITMAP_TYPE_TGA
:
195 case wxBITMAP_TYPE_TIFF
:
196 continue; // skip testing those wxImageHandlers which cannot
197 // load data from non-seekable streams
203 // compress the test file on the fly:
204 wxMemoryOutputStream memOut
;
206 wxFileInputStream
file(g_testfiles
[i
].file
);
207 CPPUNIT_ASSERT(file
.IsOk());
209 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
210 CPPUNIT_ASSERT(compressFilter
.IsOk());
212 file
.Read(compressFilter
);
213 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
216 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
217 // allows us to test loading images from non-seekable streams other than socket streams
218 wxMemoryInputStream
memIn(memOut
);
219 CPPUNIT_ASSERT(memIn
.IsOk());
220 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
221 CPPUNIT_ASSERT(decompressFilter
.IsOk());
225 // NOTE: it's important to inform wxImage about the type of the image being
226 // loaded otherwise it will try to autodetect the format, but that
227 // requires a seekable stream!
228 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
229 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
233 void ImageTestCase::SizeImage()
235 // Test the wxImage::Size() function which takes a rectangle from source and
236 // places it in a new image at a given position. This test checks, if the
237 // correct areas are chosen, and clipping is done correctly.
240 static const char * xpm_orig
[] = {
241 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
253 // the expected results for all tests:
254 static const char * xpm_l_t
[] = {
255 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
267 static const char * xpm_t
[] = {
268 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
280 static const char * xpm_r_t
[] = {
281 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
293 static const char * xpm_l
[] = {
294 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
306 static const char * xpm_r
[] = {
307 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
319 static const char * xpm_l_b
[] = {
320 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
332 static const char * xpm_b
[] = {
333 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
345 static const char * xpm_r_b
[] = {
346 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
358 static const char * xpm_sm
[] = {
359 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
369 static const char * xpm_gt
[] = {
370 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
384 static const char * xpm_gt_l_t
[] = {
385 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
399 static const char * xpm_gt_l
[] = {
400 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
414 static const char * xpm_gt_l_b
[] = {
415 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
429 static const char * xpm_gt_l_bb
[] = {
430 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
444 static const char * xpm_gt_t
[] = {
445 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
459 static const char * xpm_gt_b
[] = {
460 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
474 static const char * xpm_gt_bb
[] = {
475 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
489 static const char * xpm_gt_r_t
[] = {
490 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
504 static const char * xpm_gt_r
[] = {
505 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
519 static const char * xpm_gt_r_b
[] = {
520 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
534 static const char * xpm_gt_r_bb
[] = {
535 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
549 static const char * xpm_gt_rr_t
[] = {
550 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
564 static const char * xpm_gt_rr
[] = {
565 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
579 static const char * xpm_gt_rr_b
[] = {
580 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
594 static const char * xpm_gt_rr_bb
[] = {
595 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
609 static const char * xpm_sm_ll_tt
[] = {
610 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
620 static const char * xpm_sm_ll_t
[] = {
621 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
631 static const char * xpm_sm_ll
[] = {
632 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
642 static const char * xpm_sm_ll_b
[] = {
643 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
653 static const char * xpm_sm_l_tt
[] = {
654 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
664 static const char * xpm_sm_l_t
[] = {
665 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
675 static const char * xpm_sm_l
[] = {
676 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
686 static const char * xpm_sm_l_b
[] = {
687 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
697 static const char * xpm_sm_tt
[] = {
698 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
708 static const char * xpm_sm_t
[] = {
709 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
719 static const char * xpm_sm_b
[] = {
720 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
730 static const char * xpm_sm_r_tt
[] = {
731 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
741 static const char * xpm_sm_r_t
[] = {
742 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
752 static const char * xpm_sm_r
[] = {
753 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
763 static const char * xpm_sm_r_b
[] = {
764 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
775 // this table defines all tests
778 int w
, h
, dx
, dy
; // first parameters for Size()
779 const char **ref_xpm
; // expected result
782 { 10, 10, 0, 0, xpm_orig
}, // same size, same position
783 { 12, 12, 0, 0, xpm_gt
}, // target larger, same position
784 { 8, 8, 0, 0, xpm_sm
}, // target smaller, same position
785 { 10, 10, -2, -2, xpm_l_t
}, // same size, move left up
786 { 10, 10, -2, 0, xpm_l
}, // same size, move left
787 { 10, 10, -2, 2, xpm_l_b
}, // same size, move left down
788 { 10, 10, 0, -2, xpm_t
}, // same size, move up
789 { 10, 10, 0, 2, xpm_b
}, // same size, move down
790 { 10, 10, 2, -2, xpm_r_t
}, // same size, move right up
791 { 10, 10, 2, 0, xpm_r
}, // same size, move right
792 { 10, 10, 2, 2, xpm_r_b
}, // same size, move right down
793 { 12, 12, -2, -2, xpm_gt_l_t
}, // target larger, move left up
794 { 12, 12, -2, 0, xpm_gt_l
}, // target larger, move left
795 { 12, 12, -2, 2, xpm_gt_l_b
}, // target larger, move left down
796 { 12, 12, -2, 4, xpm_gt_l_bb
}, // target larger, move left down
797 { 12, 12, 0, -2, xpm_gt_t
}, // target larger, move up
798 { 12, 12, 0, 2, xpm_gt_b
}, // target larger, move down
799 { 12, 12, 0, 4, xpm_gt_bb
}, // target larger, move down
800 { 12, 12, 2, -2, xpm_gt_r_t
}, // target larger, move right up
801 { 12, 12, 2, 0, xpm_gt_r
}, // target larger, move right
802 { 12, 12, 2, 2, xpm_gt_r_b
}, // target larger, move right down
803 { 12, 12, 2, 4, xpm_gt_r_bb
}, // target larger, move right down
804 { 12, 12, 4, -2, xpm_gt_rr_t
}, // target larger, move right up
805 { 12, 12, 4, 0, xpm_gt_rr
}, // target larger, move right
806 { 12, 12, 4, 2, xpm_gt_rr_b
}, // target larger, move right down
807 { 12, 12, 4, 4, xpm_gt_rr_bb
}, // target larger, move right down
808 { 8, 8, -4, -4, xpm_sm_ll_tt
}, // target smaller, move left up
809 { 8, 8, -4, -2, xpm_sm_ll_t
}, // target smaller, move left up
810 { 8, 8, -4, 0, xpm_sm_ll
}, // target smaller, move left
811 { 8, 8, -4, 2, xpm_sm_ll_b
}, // target smaller, move left down
812 { 8, 8, -2, -4, xpm_sm_l_tt
}, // target smaller, move left up
813 { 8, 8, -2, -2, xpm_sm_l_t
}, // target smaller, move left up
814 { 8, 8, -2, 0, xpm_sm_l
}, // target smaller, move left
815 { 8, 8, -2, 2, xpm_sm_l_b
}, // target smaller, move left down
816 { 8, 8, 0, -4, xpm_sm_tt
}, // target smaller, move up
817 { 8, 8, 0, -2, xpm_sm_t
}, // target smaller, move up
818 { 8, 8, 0, 2, xpm_sm_b
}, // target smaller, move down
819 { 8, 8, 2, -4, xpm_sm_r_tt
}, // target smaller, move right up
820 { 8, 8, 2, -2, xpm_sm_r_t
}, // target smaller, move right up
821 { 8, 8, 2, 0, xpm_sm_r
}, // target smaller, move right
822 { 8, 8, 2, 2, xpm_sm_r_b
}, // target smaller, move right down
825 const wxImage
src_img(xpm_orig
);
826 for ( unsigned i
= 0; i
< WXSIZEOF(sizeTestData
); i
++ )
828 SizeTestData
& st
= sizeTestData
[i
];
830 actual(src_img
.Size(wxSize(st
.w
, st
.h
), wxPoint(st
.dx
, st
.dy
), 0, 0, 0)),
831 expected(st
.ref_xpm
);
833 // to check results with an image viewer uncomment this:
834 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
835 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
837 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().x
, expected
.GetSize().x
);
838 CPPUNIT_ASSERT_EQUAL( actual
.GetSize().y
, expected
.GetSize().y
);
840 WX_ASSERT_EQUAL_MESSAGE
842 ("Resize test #%u: (%d, %d), (%d, %d)", i
, st
.w
, st
.h
, st
.dx
, st
.dy
),
848 void ImageTestCase::CompareLoadedImage()
850 wxImage
expected8("horse.xpm");
851 CPPUNIT_ASSERT( expected8
.IsOk() );
853 wxImage
expected24("horse.png");
854 CPPUNIT_ASSERT( expected24
.IsOk() );
856 for (size_t i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
858 if ( !(g_testfiles
[i
].bitDepth
== 8 || g_testfiles
[i
].bitDepth
== 24)
859 || g_testfiles
[i
].type
== wxBITMAP_TYPE_JPEG
/*skip lossy JPEG*/)
864 wxImage
actual(g_testfiles
[i
].file
);
866 if ( actual
.GetSize() != expected8
.GetSize() )
872 WX_ASSERT_EQUAL_MESSAGE
874 ("Compare test '%s' for loading failed", g_testfiles
[i
].file
),
875 g_testfiles
[i
].bitDepth
== 8 ? expected8
: expected24
,
884 wxIMAGE_HAVE_ALPHA
= (1 << 0),
885 wxIMAGE_HAVE_PALETTE
= (1 << 1)
889 void CompareImage(const wxImageHandler
& handler
, const wxImage
& image
,
890 int properties
= 0, const wxImage
*compareTo
= NULL
)
892 wxBitmapType type
= handler
.GetType();
894 const bool testPalette
= (properties
& wxIMAGE_HAVE_PALETTE
) != 0;
896 This is getting messy and should probably be transformed into a table
897 with image format features before it gets hairier.
900 && ( !(type
== wxBITMAP_TYPE_BMP
901 || type
== wxBITMAP_TYPE_GIF
902 || type
== wxBITMAP_TYPE_ICO
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
912 || type
== wxBITMAP_TYPE_TIFF
) )
914 // don't test images with alpha if this handler doesn't support alpha
918 if (type
== wxBITMAP_TYPE_JPEG
/* skip lossy JPEG */)
923 wxMemoryOutputStream memOut
;
924 if ( !image
.SaveFile(memOut
, type
) )
926 // Unfortunately we can't know if the handler just doesn't support
927 // saving images, or if it failed to save.
931 wxMemoryInputStream
memIn(memOut
);
932 CPPUNIT_ASSERT(memIn
.IsOk());
934 wxImage
actual(memIn
);
935 CPPUNIT_ASSERT(actual
.IsOk());
937 const wxImage
*expected
= compareTo
? compareTo
: &image
;
938 CPPUNIT_ASSERT( actual
.GetSize() == expected
->GetSize() );
940 unsigned bitsPerPixel
= testPalette
? 8 : (testAlpha
? 32 : 24);
941 WX_ASSERT_EQUAL_MESSAGE
943 ("Compare test '%s (%d-bit)' for saving failed",
944 handler
.GetExtension(), bitsPerPixel
),
950 CPPUNIT_ASSERT(actual
.HasPalette()
951 == (testPalette
|| type
== wxBITMAP_TYPE_XPM
));
954 CPPUNIT_ASSERT( actual
.HasAlpha() == testAlpha
);
961 WX_ASSERT_EQUAL_MESSAGE
963 ("Compare alpha test '%s' for saving failed", handler
.GetExtension()),
969 static void SetAlpha(wxImage
*image
)
973 unsigned char *ptr
= image
->GetAlpha();
974 const int width
= image
->GetWidth();
975 const int height
= image
->GetHeight();
976 for (int y
= 0; y
< height
; ++y
)
978 for (int x
= 0; x
< width
; ++x
)
980 ptr
[y
*width
+ x
] = (x
*y
) & wxIMAGE_ALPHA_OPAQUE
;
985 void ImageTestCase::CompareSavedImage()
987 // FIXME-VC6: Pre-declare the loop variables for compatibility with
988 // pre-standard compilers such as MSVC6 that don't implement proper scope
989 // for the variables declared in the for loops.
992 wxImage
expected24("horse.png");
993 CPPUNIT_ASSERT( expected24
.IsOk() );
994 CPPUNIT_ASSERT( !expected24
.HasAlpha() );
996 wxImage expected8
= expected24
.ConvertToGreyscale();
999 unsigned char greys
[256];
1000 for (i
= 0; i
< 256; ++i
)
1004 wxPalette
palette(256, greys
, greys
, greys
);
1005 expected8
.SetPalette(palette
);
1006 #endif // #if wxUSE_PALETTE
1008 expected8
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP_PALETTE
);
1010 // Create an image with alpha based on the loaded image
1011 wxImage
expected32(expected24
);
1013 SetAlpha(&expected32
);
1015 const wxList
& list
= wxImage::GetHandlers();
1016 for ( wxList::compatibility_iterator node
= list
.GetFirst();
1017 node
; node
= node
->GetNext() )
1019 wxImageHandler
*handler
= (wxImageHandler
*) node
->GetData();
1022 CompareImage(*handler
, expected8
, wxIMAGE_HAVE_PALETTE
);
1024 CompareImage(*handler
, expected24
);
1025 CompareImage(*handler
, expected32
, wxIMAGE_HAVE_ALPHA
);
1029 void ImageTestCase::SavePNG()
1031 wxImage
expected24("horse.png");
1032 CPPUNIT_ASSERT( expected24
.IsOk() );
1034 CPPUNIT_ASSERT( !expected24
.HasPalette() );
1035 #endif // #if wxUSE_PALETTE
1037 wxImage expected8
= expected24
.ConvertToGreyscale();
1040 horse.png converted to greyscale should be saved without a palette.
1042 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
), expected8
);
1045 But if we explicitly ask for trying to save with a palette, it should work.
1047 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1049 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1050 expected8
, wxIMAGE_HAVE_PALETTE
);
1053 CPPUNIT_ASSERT( expected8
.LoadFile("horse.gif") );
1055 CPPUNIT_ASSERT( expected8
.HasPalette() );
1056 #endif // #if wxUSE_PALETTE
1058 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1059 expected8
, wxIMAGE_HAVE_PALETTE
);
1062 Add alpha to the image in such a way that there will still be a maximum
1063 of 256 unique RGBA combinations. This should result in a saved
1064 PNG image still being palettised and having alpha.
1066 expected8
.SetAlpha();
1069 const int width
= expected8
.GetWidth();
1070 const int height
= expected8
.GetHeight();
1071 for (y
= 0; y
< height
; ++y
)
1073 for (x
= 0; x
< width
; ++x
)
1075 expected8
.SetAlpha(x
, y
, expected8
.GetRed(x
, y
));
1079 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1080 expected8
, wxIMAGE_HAVE_ALPHA
|wxIMAGE_HAVE_PALETTE
);
1083 Now change the alpha of the first pixel so that we can't save palettised
1084 anymore because there will be 256+1 entries which is beyond PNGs limit
1087 expected8
.SetAlpha(0, 0, 1);
1089 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1090 expected8
, wxIMAGE_HAVE_ALPHA
);
1093 Even if we explicitly ask for saving palettised it should not be done.
1095 expected8
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, wxPNG_TYPE_PALETTE
);
1096 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG
),
1097 expected8
, wxIMAGE_HAVE_ALPHA
);
1101 static void TestTIFFImage(const wxString
& option
, int value
,
1102 const wxImage
*compareImage
= NULL
)
1107 image
= *compareImage
;
1111 (void) image
.LoadFile("horse.png");
1113 CPPUNIT_ASSERT( image
.IsOk() );
1115 wxMemoryOutputStream memOut
;
1116 image
.SetOption(option
, value
);
1118 CPPUNIT_ASSERT(image
.SaveFile(memOut
, wxBITMAP_TYPE_TIFF
));
1120 wxMemoryInputStream
memIn(memOut
);
1121 CPPUNIT_ASSERT(memIn
.IsOk());
1123 wxImage
savedImage(memIn
);
1124 CPPUNIT_ASSERT(savedImage
.IsOk());
1126 WX_ASSERT_EQUAL_MESSAGE(("While checking for option %s", option
),
1127 true, savedImage
.HasOption(option
));
1129 WX_ASSERT_EQUAL_MESSAGE(("While testing for %s", option
),
1130 value
, savedImage
.GetOptionInt(option
));
1132 WX_ASSERT_EQUAL_MESSAGE(("HasAlpha() not equal"), image
.HasAlpha(), savedImage
.HasAlpha());
1135 void ImageTestCase::SaveTIFF()
1137 TestTIFFImage(wxIMAGE_OPTION_TIFF_BITSPERSAMPLE
, 1);
1138 TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL
, 1);
1139 TestTIFFImage(wxIMAGE_OPTION_TIFF_PHOTOMETRIC
, 0/*PHOTOMETRIC_MINISWHITE*/);
1140 TestTIFFImage(wxIMAGE_OPTION_TIFF_PHOTOMETRIC
, 1/*PHOTOMETRIC_MINISBLACK*/);
1142 wxImage
alphaImage("horse.png");
1143 CPPUNIT_ASSERT( alphaImage
.IsOk() );
1144 SetAlpha(&alphaImage
);
1147 TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL
, 4, &alphaImage
);
1150 TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL
, 2, &alphaImage
);
1153 alphaImage
.SetOption(wxIMAGE_OPTION_TIFF_BITSPERSAMPLE
, 1);
1154 TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL
, 2, &alphaImage
);
1157 void ImageTestCase::SaveAnimatedGIF()
1160 wxImage
image("horse.gif");
1161 CPPUNIT_ASSERT( image
.IsOk() );
1163 wxImageArray images
;
1166 for (i
= 0; i
< 4-1; ++i
)
1168 images
.Add( images
[i
].Rotate90() );
1170 images
[i
+1].SetPalette(images
[0].GetPalette());
1173 wxMemoryOutputStream memOut
;
1174 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1176 wxGIFHandler handler
;
1177 wxMemoryInputStream
memIn(memOut
);
1178 CPPUNIT_ASSERT(memIn
.IsOk());
1179 const int imageCount
= handler
.GetImageCount(memIn
);
1180 CPPUNIT_ASSERT_EQUAL(4, imageCount
);
1182 for (i
= 0; i
< imageCount
; ++i
)
1184 wxFileOffset pos
= memIn
.TellI();
1185 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true, i
) );
1188 WX_ASSERT_EQUAL_MESSAGE
1190 ("Compare test for GIF frame number %d failed", i
),
1195 #endif // #if wxUSE_PALETTE
1198 void ImageTestCase::ReadCorruptedTGA()
1200 static unsigned char corruptTGA
[18+1+3] =
1204 10, // RLE compressed image.
1210 1, 0, // Width is 1.
1211 1, 0, // Height is 1.
1212 24, // Bits per pixel.
1215 0xff, // Run length (repeat next pixel 127+1 times).
1216 0xff, 0xff, 0xff // One 24-bit pixel.
1219 wxMemoryInputStream
memIn(corruptTGA
, WXSIZEOF(corruptTGA
));
1220 CPPUNIT_ASSERT(memIn
.IsOk());
1223 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1227 Instead of repeating a pixel 127+1 times, now tell it there will
1228 follow 127+1 uncompressed pixels (while we only should have 1 in total).
1230 corruptTGA
[18] = 0x7f;
1231 CPPUNIT_ASSERT( !tgaImage
.LoadFile(memIn
) );
1234 static void TestGIFComment(const wxString
& comment
)
1236 wxImage
image("horse.gif");
1238 image
.SetOption(wxIMAGE_OPTION_GIF_COMMENT
, comment
);
1239 wxMemoryOutputStream memOut
;
1240 CPPUNIT_ASSERT(image
.SaveFile(memOut
, wxBITMAP_TYPE_GIF
));
1242 wxMemoryInputStream
memIn(memOut
);
1243 CPPUNIT_ASSERT( image
.LoadFile(memIn
) );
1245 CPPUNIT_ASSERT_EQUAL(comment
,
1246 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1249 void ImageTestCase::GIFComment()
1251 // Test reading a comment.
1252 wxImage
image("horse.gif");
1253 CPPUNIT_ASSERT_EQUAL(" Imported from GRADATION image: gray",
1254 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1257 // Test writing a comment and reading it back.
1258 TestGIFComment("Giving the GIF a gifted giraffe as a gift");
1261 // Test writing and reading a comment again but with a long comment.
1262 TestGIFComment(wxString(wxT('a'), 256)
1263 + wxString(wxT('b'), 256)
1264 + wxString(wxT('c'), 256));
1267 // Test writing comments in an animated GIF and reading them back.
1268 CPPUNIT_ASSERT( image
.LoadFile("horse.gif") );
1270 wxImageArray images
;
1272 for (i
= 0; i
< 4; ++i
)
1276 images
.Add( images
[i
-1].Rotate90() );
1277 images
[i
].SetPalette(images
[0].GetPalette());
1284 images
[i
].SetOption(wxIMAGE_OPTION_GIF_COMMENT
,
1285 wxString::Format("GIF comment for frame #%d", i
+1));
1290 wxMemoryOutputStream memOut
;
1291 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images
, &memOut
) );
1293 wxGIFHandler handler
;
1294 wxMemoryInputStream
memIn(memOut
);
1295 CPPUNIT_ASSERT(memIn
.IsOk());
1296 const int imageCount
= handler
.GetImageCount(memIn
);
1297 for (i
= 0; i
< imageCount
; ++i
)
1299 wxFileOffset pos
= memIn
.TellI();
1300 CPPUNIT_ASSERT( handler
.LoadFile(&image
, memIn
, true /*verbose?*/, i
) );
1302 CPPUNIT_ASSERT_EQUAL(
1303 wxString::Format("GIF comment for frame #%d", i
+1),
1304 image
.GetOption(wxIMAGE_OPTION_GIF_COMMENT
));
1309 void ImageTestCase::DibPadding()
1312 There used to be an error with calculating the DWORD aligned scan line
1313 pitch for a BMP/ICO resulting in buffer overwrites (with at least MSVC9
1314 Debug this gave a heap corruption assertion when saving the mask of
1315 an ICO). Test for it here.
1317 wxImage
image("horse.gif");
1318 CPPUNIT_ASSERT( image
.IsOk() );
1320 image
= image
.Scale(99, 99);
1322 wxMemoryOutputStream memOut
;
1323 CPPUNIT_ASSERT( image
.SaveFile(memOut
, wxBITMAP_TYPE_ICO
) );
1326 static void CompareBMPImage(const wxString
& file1
, const wxString
& file2
)
1328 wxImage
image1(file1
);
1329 CPPUNIT_ASSERT( image1
.IsOk() );
1331 wxImage
image2(file2
);
1332 CPPUNIT_ASSERT( image2
.IsOk() );
1334 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_BMP
), image1
, 0, &image2
);
1337 void ImageTestCase::BMPFlippingAndRLECompression()
1339 CompareBMPImage("image/horse_grey.bmp", "image/horse_grey_flipped.bmp");
1341 CompareBMPImage("image/horse_rle8.bmp", "image/horse_grey.bmp");
1342 CompareBMPImage("image/horse_rle8.bmp", "image/horse_rle8_flipped.bmp");
1344 CompareBMPImage("image/horse_rle4.bmp", "image/horse_rle4_flipped.bmp");
1346 #endif //wxUSE_IMAGE
1350 TODO: add lots of more tests to wxImage functions