]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/image/image.cpp | |
3 | // Purpose: Test wxImage | |
4 | // Author: Francesco Montorsi | |
5 | // Created: 2009-05-31 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2009 Francesco Montorsi | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ---------------------------------------------------------------------------- | |
12 | // headers | |
13 | // ---------------------------------------------------------------------------- | |
14 | ||
15 | #include "testprec.h" | |
16 | ||
17 | #if wxUSE_IMAGE | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #endif // WX_PRECOMP | |
25 | ||
26 | #include "wx/anidecod.h" // wxImageArray | |
27 | #include "wx/palette.h" | |
28 | #include "wx/url.h" | |
29 | #include "wx/log.h" | |
30 | #include "wx/mstream.h" | |
31 | #include "wx/zstream.h" | |
32 | #include "wx/wfstream.h" | |
33 | ||
34 | #include "testimage.h" | |
35 | ||
36 | struct testData { | |
37 | const char* file; | |
38 | wxBitmapType type; | |
39 | unsigned bitDepth; | |
40 | } g_testfiles[] = | |
41 | { | |
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 } | |
54 | }; | |
55 | ||
56 | ||
57 | // ---------------------------------------------------------------------------- | |
58 | // test class | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | class ImageTestCase : public CppUnit::TestCase | |
62 | { | |
63 | public: | |
64 | ImageTestCase(); | |
65 | ~ImageTestCase(); | |
66 | ||
67 | private: | |
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(); | |
83 | ||
84 | void LoadFromSocketStream(); | |
85 | void LoadFromZipStream(); | |
86 | void LoadFromFile(); | |
87 | void SizeImage(); | |
88 | void CompareLoadedImage(); | |
89 | void CompareSavedImage(); | |
90 | void SavePNG(); | |
91 | void SaveTIFF(); | |
92 | void SaveAnimatedGIF(); | |
93 | void ReadCorruptedTGA(); | |
94 | void GIFComment(); | |
95 | void DibPadding(); | |
96 | void BMPFlippingAndRLECompression(); | |
97 | ||
98 | DECLARE_NO_COPY_CLASS(ImageTestCase) | |
99 | }; | |
100 | ||
101 | CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase ); | |
102 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase, "ImageTestCase" ); | |
103 | ||
104 | ImageTestCase::ImageTestCase() | |
105 | { | |
106 | wxSocketBase::Initialize(); | |
107 | ||
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); | |
121 | } | |
122 | ||
123 | ImageTestCase::~ImageTestCase() | |
124 | { | |
125 | wxSocketBase::Shutdown(); | |
126 | } | |
127 | ||
128 | void ImageTestCase::LoadFromFile() | |
129 | { | |
130 | wxImage img; | |
131 | for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++) | |
132 | CPPUNIT_ASSERT(img.LoadFile(g_testfiles[i].file)); | |
133 | } | |
134 | ||
135 | void ImageTestCase::LoadFromSocketStream() | |
136 | { | |
137 | if (!IsNetworkAvailable()) // implemented in test.cpp | |
138 | { | |
139 | wxLogWarning("No network connectivity; skipping the " | |
140 | "ImageTestCase::LoadFromSocketStream test unit."); | |
141 | return; | |
142 | } | |
143 | ||
144 | struct { | |
145 | const char* url; | |
146 | wxBitmapType type; | |
147 | } testData[] = | |
148 | { | |
149 | { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG }, | |
150 | { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO } | |
151 | }; | |
152 | ||
153 | for (unsigned int i=0; i<WXSIZEOF(testData); i++) | |
154 | { | |
155 | wxURL url(testData[i].url); | |
156 | WX_ASSERT_EQUAL_MESSAGE | |
157 | ( | |
158 | ("Constructing URL \"%s\" failed.", testData[i].url), | |
159 | wxURL_NOERR, | |
160 | url.GetError() | |
161 | ); | |
162 | ||
163 | wxInputStream *in_stream = url.GetInputStream(); | |
164 | WX_ASSERT_MESSAGE | |
165 | ( | |
166 | ("Opening URL \"%s\" failed.", testData[i].url), | |
167 | in_stream && in_stream->IsOk() | |
168 | ); | |
169 | ||
170 | wxImage img; | |
171 | ||
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! | |
175 | WX_ASSERT_MESSAGE | |
176 | ( | |
177 | ("Loading image from \"%s\" failed.", testData[i].url), | |
178 | img.LoadFile(*in_stream, testData[i].type) | |
179 | ); | |
180 | ||
181 | delete in_stream; | |
182 | } | |
183 | } | |
184 | ||
185 | void ImageTestCase::LoadFromZipStream() | |
186 | { | |
187 | for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++) | |
188 | { | |
189 | switch (g_testfiles[i].type) | |
190 | { | |
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 | |
198 | ||
199 | default: | |
200 | ; // proceed | |
201 | } | |
202 | ||
203 | // compress the test file on the fly: | |
204 | wxMemoryOutputStream memOut; | |
205 | { | |
206 | wxFileInputStream file(g_testfiles[i].file); | |
207 | CPPUNIT_ASSERT(file.IsOk()); | |
208 | ||
209 | wxZlibOutputStream compressFilter(memOut, 5, wxZLIB_GZIP); | |
210 | CPPUNIT_ASSERT(compressFilter.IsOk()); | |
211 | ||
212 | file.Read(compressFilter); | |
213 | CPPUNIT_ASSERT(file.GetLastError() == wxSTREAM_EOF); | |
214 | } | |
215 | ||
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()); | |
222 | ||
223 | wxImage img; | |
224 | ||
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)); | |
230 | } | |
231 | } | |
232 | ||
233 | void ImageTestCase::SizeImage() | |
234 | { | |
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. | |
238 | ||
239 | // our test image: | |
240 | static const char * xpm_orig[] = { | |
241 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
242 | " .....", | |
243 | " ++++@@@@.", | |
244 | " +... @.", | |
245 | " +.@@++ @.", | |
246 | " +.@ .+ @.", | |
247 | ".@ +. @.+ ", | |
248 | ".@ ++@@.+ ", | |
249 | ".@ ...+ ", | |
250 | ".@@@@++++ ", | |
251 | "..... " | |
252 | }; | |
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", | |
256 | "... @.BB", | |
257 | ".@@++ @.BB", | |
258 | ".@ .+ @.BB", | |
259 | " +. @.+ BB", | |
260 | " ++@@.+ BB", | |
261 | " ...+ BB", | |
262 | "@@@++++ BB", | |
263 | "... BB", | |
264 | "BBBBBBBBBB", | |
265 | "BBBBBBBBBB" | |
266 | }; | |
267 | static const char * xpm_t[] = { | |
268 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
269 | " +... @.", | |
270 | " +.@@++ @.", | |
271 | " +.@ .+ @.", | |
272 | ".@ +. @.+ ", | |
273 | ".@ ++@@.+ ", | |
274 | ".@ ...+ ", | |
275 | ".@@@@++++ ", | |
276 | "..... ", | |
277 | "BBBBBBBBBB", | |
278 | "BBBBBBBBBB" | |
279 | }; | |
280 | static const char * xpm_r_t[] = { | |
281 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
282 | "BB +... ", | |
283 | "BB +.@@++ ", | |
284 | "BB +.@ .+ ", | |
285 | "BB.@ +. @.", | |
286 | "BB.@ ++@@.", | |
287 | "BB.@ ...", | |
288 | "BB.@@@@+++", | |
289 | "BB..... ", | |
290 | "BBBBBBBBBB", | |
291 | "BBBBBBBBBB" | |
292 | }; | |
293 | static const char * xpm_l[] = { | |
294 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
295 | " .....BB", | |
296 | "+++@@@@.BB", | |
297 | "... @.BB", | |
298 | ".@@++ @.BB", | |
299 | ".@ .+ @.BB", | |
300 | " +. @.+ BB", | |
301 | " ++@@.+ BB", | |
302 | " ...+ BB", | |
303 | "@@@++++ BB", | |
304 | "... BB" | |
305 | }; | |
306 | static const char * xpm_r[] = { | |
307 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
308 | "BB ...", | |
309 | "BB ++++@@@", | |
310 | "BB +... ", | |
311 | "BB +.@@++ ", | |
312 | "BB +.@ .+ ", | |
313 | "BB.@ +. @.", | |
314 | "BB.@ ++@@.", | |
315 | "BB.@ ...", | |
316 | "BB.@@@@+++", | |
317 | "BB..... " | |
318 | }; | |
319 | static const char * xpm_l_b[] = { | |
320 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
321 | "BBBBBBBBBB", | |
322 | "BBBBBBBBBB", | |
323 | " .....BB", | |
324 | "+++@@@@.BB", | |
325 | "... @.BB", | |
326 | ".@@++ @.BB", | |
327 | ".@ .+ @.BB", | |
328 | " +. @.+ BB", | |
329 | " ++@@.+ BB", | |
330 | " ...+ BB" | |
331 | }; | |
332 | static const char * xpm_b[] = { | |
333 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
334 | "BBBBBBBBBB", | |
335 | "BBBBBBBBBB", | |
336 | " .....", | |
337 | " ++++@@@@.", | |
338 | " +... @.", | |
339 | " +.@@++ @.", | |
340 | " +.@ .+ @.", | |
341 | ".@ +. @.+ ", | |
342 | ".@ ++@@.+ ", | |
343 | ".@ ...+ " | |
344 | }; | |
345 | static const char * xpm_r_b[] = { | |
346 | "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
347 | "BBBBBBBBBB", | |
348 | "BBBBBBBBBB", | |
349 | "BB ...", | |
350 | "BB ++++@@@", | |
351 | "BB +... ", | |
352 | "BB +.@@++ ", | |
353 | "BB +.@ .+ ", | |
354 | "BB.@ +. @.", | |
355 | "BB.@ ++@@.", | |
356 | "BB.@ ..." | |
357 | }; | |
358 | static const char * xpm_sm[] = { | |
359 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
360 | " .....", | |
361 | " ++++@@@", | |
362 | " +... ", | |
363 | " +.@@++ ", | |
364 | " +.@ .+ ", | |
365 | ".@ +. @.", | |
366 | ".@ ++@@.", | |
367 | ".@ ..." | |
368 | }; | |
369 | static const char * xpm_gt[] = { | |
370 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
371 | " .....BB", | |
372 | " ++++@@@@.BB", | |
373 | " +... @.BB", | |
374 | " +.@@++ @.BB", | |
375 | " +.@ .+ @.BB", | |
376 | ".@ +. @.+ BB", | |
377 | ".@ ++@@.+ BB", | |
378 | ".@ ...+ BB", | |
379 | ".@@@@++++ BB", | |
380 | "..... BB", | |
381 | "BBBBBBBBBBBB", | |
382 | "BBBBBBBBBBBB" | |
383 | }; | |
384 | static const char * xpm_gt_l_t[] = { | |
385 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
386 | "... @.BBBB", | |
387 | ".@@++ @.BBBB", | |
388 | ".@ .+ @.BBBB", | |
389 | " +. @.+ BBBB", | |
390 | " ++@@.+ BBBB", | |
391 | " ...+ BBBB", | |
392 | "@@@++++ BBBB", | |
393 | "... BBBB", | |
394 | "BBBBBBBBBBBB", | |
395 | "BBBBBBBBBBBB", | |
396 | "BBBBBBBBBBBB", | |
397 | "BBBBBBBBBBBB" | |
398 | }; | |
399 | static const char * xpm_gt_l[] = { | |
400 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
401 | " .....BBBB", | |
402 | "+++@@@@.BBBB", | |
403 | "... @.BBBB", | |
404 | ".@@++ @.BBBB", | |
405 | ".@ .+ @.BBBB", | |
406 | " +. @.+ BBBB", | |
407 | " ++@@.+ BBBB", | |
408 | " ...+ BBBB", | |
409 | "@@@++++ BBBB", | |
410 | "... BBBB", | |
411 | "BBBBBBBBBBBB", | |
412 | "BBBBBBBBBBBB" | |
413 | }; | |
414 | static const char * xpm_gt_l_b[] = { | |
415 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
416 | "BBBBBBBBBBBB", | |
417 | "BBBBBBBBBBBB", | |
418 | " .....BBBB", | |
419 | "+++@@@@.BBBB", | |
420 | "... @.BBBB", | |
421 | ".@@++ @.BBBB", | |
422 | ".@ .+ @.BBBB", | |
423 | " +. @.+ BBBB", | |
424 | " ++@@.+ BBBB", | |
425 | " ...+ BBBB", | |
426 | "@@@++++ BBBB", | |
427 | "... BBBB" | |
428 | }; | |
429 | static const char * xpm_gt_l_bb[] = { | |
430 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
431 | "BBBBBBBBBBBB", | |
432 | "BBBBBBBBBBBB", | |
433 | "BBBBBBBBBBBB", | |
434 | "BBBBBBBBBBBB", | |
435 | " .....BBBB", | |
436 | "+++@@@@.BBBB", | |
437 | "... @.BBBB", | |
438 | ".@@++ @.BBBB", | |
439 | ".@ .+ @.BBBB", | |
440 | " +. @.+ BBBB", | |
441 | " ++@@.+ BBBB", | |
442 | " ...+ BBBB" | |
443 | }; | |
444 | static const char * xpm_gt_t[] = { | |
445 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
446 | " +... @.BB", | |
447 | " +.@@++ @.BB", | |
448 | " +.@ .+ @.BB", | |
449 | ".@ +. @.+ BB", | |
450 | ".@ ++@@.+ BB", | |
451 | ".@ ...+ BB", | |
452 | ".@@@@++++ BB", | |
453 | "..... BB", | |
454 | "BBBBBBBBBBBB", | |
455 | "BBBBBBBBBBBB", | |
456 | "BBBBBBBBBBBB", | |
457 | "BBBBBBBBBBBB" | |
458 | }; | |
459 | static const char * xpm_gt_b[] = { | |
460 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
461 | "BBBBBBBBBBBB", | |
462 | "BBBBBBBBBBBB", | |
463 | " .....BB", | |
464 | " ++++@@@@.BB", | |
465 | " +... @.BB", | |
466 | " +.@@++ @.BB", | |
467 | " +.@ .+ @.BB", | |
468 | ".@ +. @.+ BB", | |
469 | ".@ ++@@.+ BB", | |
470 | ".@ ...+ BB", | |
471 | ".@@@@++++ BB", | |
472 | "..... BB" | |
473 | }; | |
474 | static const char * xpm_gt_bb[] = { | |
475 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
476 | "BBBBBBBBBBBB", | |
477 | "BBBBBBBBBBBB", | |
478 | "BBBBBBBBBBBB", | |
479 | "BBBBBBBBBBBB", | |
480 | " .....BB", | |
481 | " ++++@@@@.BB", | |
482 | " +... @.BB", | |
483 | " +.@@++ @.BB", | |
484 | " +.@ .+ @.BB", | |
485 | ".@ +. @.+ BB", | |
486 | ".@ ++@@.+ BB", | |
487 | ".@ ...+ BB" | |
488 | }; | |
489 | static const char * xpm_gt_r_t[] = { | |
490 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
491 | "BB +... @.", | |
492 | "BB +.@@++ @.", | |
493 | "BB +.@ .+ @.", | |
494 | "BB.@ +. @.+ ", | |
495 | "BB.@ ++@@.+ ", | |
496 | "BB.@ ...+ ", | |
497 | "BB.@@@@++++ ", | |
498 | "BB..... ", | |
499 | "BBBBBBBBBBBB", | |
500 | "BBBBBBBBBBBB", | |
501 | "BBBBBBBBBBBB", | |
502 | "BBBBBBBBBBBB" | |
503 | }; | |
504 | static const char * xpm_gt_r[] = { | |
505 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
506 | "BB .....", | |
507 | "BB ++++@@@@.", | |
508 | "BB +... @.", | |
509 | "BB +.@@++ @.", | |
510 | "BB +.@ .+ @.", | |
511 | "BB.@ +. @.+ ", | |
512 | "BB.@ ++@@.+ ", | |
513 | "BB.@ ...+ ", | |
514 | "BB.@@@@++++ ", | |
515 | "BB..... ", | |
516 | "BBBBBBBBBBBB", | |
517 | "BBBBBBBBBBBB" | |
518 | }; | |
519 | static const char * xpm_gt_r_b[] = { | |
520 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
521 | "BBBBBBBBBBBB", | |
522 | "BBBBBBBBBBBB", | |
523 | "BB .....", | |
524 | "BB ++++@@@@.", | |
525 | "BB +... @.", | |
526 | "BB +.@@++ @.", | |
527 | "BB +.@ .+ @.", | |
528 | "BB.@ +. @.+ ", | |
529 | "BB.@ ++@@.+ ", | |
530 | "BB.@ ...+ ", | |
531 | "BB.@@@@++++ ", | |
532 | "BB..... " | |
533 | }; | |
534 | static const char * xpm_gt_r_bb[] = { | |
535 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
536 | "BBBBBBBBBBBB", | |
537 | "BBBBBBBBBBBB", | |
538 | "BBBBBBBBBBBB", | |
539 | "BBBBBBBBBBBB", | |
540 | "BB .....", | |
541 | "BB ++++@@@@.", | |
542 | "BB +... @.", | |
543 | "BB +.@@++ @.", | |
544 | "BB +.@ .+ @.", | |
545 | "BB.@ +. @.+ ", | |
546 | "BB.@ ++@@.+ ", | |
547 | "BB.@ ...+ " | |
548 | }; | |
549 | static const char * xpm_gt_rr_t[] = { | |
550 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
551 | "BBBB +... ", | |
552 | "BBBB +.@@++ ", | |
553 | "BBBB +.@ .+ ", | |
554 | "BBBB.@ +. @.", | |
555 | "BBBB.@ ++@@.", | |
556 | "BBBB.@ ...", | |
557 | "BBBB.@@@@+++", | |
558 | "BBBB..... ", | |
559 | "BBBBBBBBBBBB", | |
560 | "BBBBBBBBBBBB", | |
561 | "BBBBBBBBBBBB", | |
562 | "BBBBBBBBBBBB" | |
563 | }; | |
564 | static const char * xpm_gt_rr[] = { | |
565 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
566 | "BBBB ...", | |
567 | "BBBB ++++@@@", | |
568 | "BBBB +... ", | |
569 | "BBBB +.@@++ ", | |
570 | "BBBB +.@ .+ ", | |
571 | "BBBB.@ +. @.", | |
572 | "BBBB.@ ++@@.", | |
573 | "BBBB.@ ...", | |
574 | "BBBB.@@@@+++", | |
575 | "BBBB..... ", | |
576 | "BBBBBBBBBBBB", | |
577 | "BBBBBBBBBBBB" | |
578 | }; | |
579 | static const char * xpm_gt_rr_b[] = { | |
580 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
581 | "BBBBBBBBBBBB", | |
582 | "BBBBBBBBBBBB", | |
583 | "BBBB ...", | |
584 | "BBBB ++++@@@", | |
585 | "BBBB +... ", | |
586 | "BBBB +.@@++ ", | |
587 | "BBBB +.@ .+ ", | |
588 | "BBBB.@ +. @.", | |
589 | "BBBB.@ ++@@.", | |
590 | "BBBB.@ ...", | |
591 | "BBBB.@@@@+++", | |
592 | "BBBB..... " | |
593 | }; | |
594 | static const char * xpm_gt_rr_bb[] = { | |
595 | "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
596 | "BBBBBBBBBBBB", | |
597 | "BBBBBBBBBBBB", | |
598 | "BBBBBBBBBBBB", | |
599 | "BBBBBBBBBBBB", | |
600 | "BBBB ...", | |
601 | "BBBB ++++@@@", | |
602 | "BBBB +... ", | |
603 | "BBBB +.@@++ ", | |
604 | "BBBB +.@ .+ ", | |
605 | "BBBB.@ +. @.", | |
606 | "BBBB.@ ++@@.", | |
607 | "BBBB.@ ..." | |
608 | }; | |
609 | static const char * xpm_sm_ll_tt[] = { | |
610 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
611 | " .+ @.BB", | |
612 | ". @.+ BB", | |
613 | "+@@.+ BB", | |
614 | " ...+ BB", | |
615 | "@++++ BB", | |
616 | ". BB", | |
617 | "BBBBBBBB", | |
618 | "BBBBBBBB" | |
619 | }; | |
620 | static const char * xpm_sm_ll_t[] = { | |
621 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
622 | ". @.BB", | |
623 | "@++ @.BB", | |
624 | " .+ @.BB", | |
625 | ". @.+ BB", | |
626 | "+@@.+ BB", | |
627 | " ...+ BB", | |
628 | "@++++ BB", | |
629 | ". BB" | |
630 | }; | |
631 | static const char * xpm_sm_ll[] = { | |
632 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
633 | " .....BB", | |
634 | "+@@@@.BB", | |
635 | ". @.BB", | |
636 | "@++ @.BB", | |
637 | " .+ @.BB", | |
638 | ". @.+ BB", | |
639 | "+@@.+ BB", | |
640 | " ...+ BB" | |
641 | }; | |
642 | static const char * xpm_sm_ll_b[] = { | |
643 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
644 | "BBBBBBBB", | |
645 | "BBBBBBBB", | |
646 | " .....BB", | |
647 | "+@@@@.BB", | |
648 | ". @.BB", | |
649 | "@++ @.BB", | |
650 | " .+ @.BB", | |
651 | ". @.+ BB" | |
652 | }; | |
653 | static const char * xpm_sm_l_tt[] = { | |
654 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
655 | ".@ .+ @.", | |
656 | " +. @.+ ", | |
657 | " ++@@.+ ", | |
658 | " ...+ ", | |
659 | "@@@++++ ", | |
660 | "... ", | |
661 | "BBBBBBBB", | |
662 | "BBBBBBBB" | |
663 | }; | |
664 | static const char * xpm_sm_l_t[] = { | |
665 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
666 | "... @.", | |
667 | ".@@++ @.", | |
668 | ".@ .+ @.", | |
669 | " +. @.+ ", | |
670 | " ++@@.+ ", | |
671 | " ...+ ", | |
672 | "@@@++++ ", | |
673 | "... " | |
674 | }; | |
675 | static const char * xpm_sm_l[] = { | |
676 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
677 | " .....", | |
678 | "+++@@@@.", | |
679 | "... @.", | |
680 | ".@@++ @.", | |
681 | ".@ .+ @.", | |
682 | " +. @.+ ", | |
683 | " ++@@.+ ", | |
684 | " ...+ " | |
685 | }; | |
686 | static const char * xpm_sm_l_b[] = { | |
687 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
688 | "BBBBBBBB", | |
689 | "BBBBBBBB", | |
690 | " .....", | |
691 | "+++@@@@.", | |
692 | "... @.", | |
693 | ".@@++ @.", | |
694 | ".@ .+ @.", | |
695 | " +. @.+ " | |
696 | }; | |
697 | static const char * xpm_sm_tt[] = { | |
698 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
699 | " +.@ .+ ", | |
700 | ".@ +. @.", | |
701 | ".@ ++@@.", | |
702 | ".@ ...", | |
703 | ".@@@@+++", | |
704 | "..... ", | |
705 | "BBBBBBBB", | |
706 | "BBBBBBBB" | |
707 | }; | |
708 | static const char * xpm_sm_t[] = { | |
709 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
710 | " +... ", | |
711 | " +.@@++ ", | |
712 | " +.@ .+ ", | |
713 | ".@ +. @.", | |
714 | ".@ ++@@.", | |
715 | ".@ ...", | |
716 | ".@@@@+++", | |
717 | "..... " | |
718 | }; | |
719 | static const char * xpm_sm_b[] = { | |
720 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
721 | "BBBBBBBB", | |
722 | "BBBBBBBB", | |
723 | " ...", | |
724 | " ++++@@@", | |
725 | " +... ", | |
726 | " +.@@++ ", | |
727 | " +.@ .+ ", | |
728 | ".@ +. @." | |
729 | }; | |
730 | static const char * xpm_sm_r_tt[] = { | |
731 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
732 | "BB +.@ .", | |
733 | "BB.@ +. ", | |
734 | "BB.@ ++@", | |
735 | "BB.@ .", | |
736 | "BB.@@@@+", | |
737 | "BB..... ", | |
738 | "BBBBBBBB", | |
739 | "BBBBBBBB" | |
740 | }; | |
741 | static const char * xpm_sm_r_t[] = { | |
742 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
743 | "BB +... ", | |
744 | "BB +.@@+", | |
745 | "BB +.@ .", | |
746 | "BB.@ +. ", | |
747 | "BB.@ ++@", | |
748 | "BB.@ .", | |
749 | "BB.@@@@+", | |
750 | "BB..... " | |
751 | }; | |
752 | static const char * xpm_sm_r[] = { | |
753 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
754 | "BB .", | |
755 | "BB ++++@", | |
756 | "BB +... ", | |
757 | "BB +.@@+", | |
758 | "BB +.@ .", | |
759 | "BB.@ +. ", | |
760 | "BB.@ ++@", | |
761 | "BB.@ ." | |
762 | }; | |
763 | static const char * xpm_sm_r_b[] = { | |
764 | "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000", | |
765 | "BBBBBBBB", | |
766 | "BBBBBBBB", | |
767 | "BB .", | |
768 | "BB ++++@", | |
769 | "BB +... ", | |
770 | "BB +.@@+", | |
771 | "BB +.@ .", | |
772 | "BB.@ +. " | |
773 | }; | |
774 | ||
775 | // this table defines all tests | |
776 | struct SizeTestData | |
777 | { | |
778 | int w, h, dx, dy; // first parameters for Size() | |
779 | const char **ref_xpm; // expected result | |
780 | } sizeTestData[] = | |
781 | { | |
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 | |
823 | }; | |
824 | ||
825 | const wxImage src_img(xpm_orig); | |
826 | for ( unsigned i = 0; i < WXSIZEOF(sizeTestData); i++ ) | |
827 | { | |
828 | SizeTestData& st = sizeTestData[i]; | |
829 | wxImage | |
830 | actual(src_img.Size(wxSize(st.w, st.h), wxPoint(st.dx, st.dy), 0, 0, 0)), | |
831 | expected(st.ref_xpm); | |
832 | ||
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); | |
836 | ||
837 | CPPUNIT_ASSERT_EQUAL( actual.GetSize().x, expected.GetSize().x ); | |
838 | CPPUNIT_ASSERT_EQUAL( actual.GetSize().y, expected.GetSize().y ); | |
839 | ||
840 | WX_ASSERT_EQUAL_MESSAGE | |
841 | ( | |
842 | ("Resize test #%u: (%d, %d), (%d, %d)", i, st.w, st.h, st.dx, st.dy), | |
843 | expected, actual | |
844 | ); | |
845 | } | |
846 | } | |
847 | ||
848 | void ImageTestCase::CompareLoadedImage() | |
849 | { | |
850 | wxImage expected8("horse.xpm"); | |
851 | CPPUNIT_ASSERT( expected8.IsOk() ); | |
852 | ||
853 | wxImage expected24("horse.png"); | |
854 | CPPUNIT_ASSERT( expected24.IsOk() ); | |
855 | ||
856 | for (size_t i=0; i<WXSIZEOF(g_testfiles); i++) | |
857 | { | |
858 | if ( !(g_testfiles[i].bitDepth == 8 || g_testfiles[i].bitDepth == 24) | |
859 | || g_testfiles[i].type == wxBITMAP_TYPE_JPEG /*skip lossy JPEG*/) | |
860 | { | |
861 | continue; | |
862 | } | |
863 | ||
864 | wxImage actual(g_testfiles[i].file); | |
865 | ||
866 | if ( actual.GetSize() != expected8.GetSize() ) | |
867 | { | |
868 | continue; | |
869 | } | |
870 | ||
871 | ||
872 | WX_ASSERT_EQUAL_MESSAGE | |
873 | ( | |
874 | ("Compare test '%s' for loading failed", g_testfiles[i].file), | |
875 | g_testfiles[i].bitDepth == 8 ? expected8 : expected24, | |
876 | actual | |
877 | ); | |
878 | } | |
879 | ||
880 | } | |
881 | ||
882 | enum | |
883 | { | |
884 | wxIMAGE_HAVE_ALPHA = (1 << 0), | |
885 | wxIMAGE_HAVE_PALETTE = (1 << 1) | |
886 | }; | |
887 | ||
888 | static | |
889 | void CompareImage(const wxImageHandler& handler, const wxImage& image, | |
890 | int properties = 0, const wxImage *compareTo = NULL) | |
891 | { | |
892 | wxBitmapType type = handler.GetType(); | |
893 | ||
894 | const bool testPalette = (properties & wxIMAGE_HAVE_PALETTE) != 0; | |
895 | /* | |
896 | This is getting messy and should probably be transformed into a table | |
897 | with image format features before it gets hairier. | |
898 | */ | |
899 | if ( testPalette | |
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) ) | |
905 | { | |
906 | return; | |
907 | } | |
908 | ||
909 | const bool testAlpha = (properties & wxIMAGE_HAVE_ALPHA) != 0; | |
910 | if (testAlpha | |
911 | && !(type == wxBITMAP_TYPE_PNG || type == wxBITMAP_TYPE_TGA | |
912 | || type == wxBITMAP_TYPE_TIFF) ) | |
913 | { | |
914 | // don't test images with alpha if this handler doesn't support alpha | |
915 | return; | |
916 | } | |
917 | ||
918 | if (type == wxBITMAP_TYPE_JPEG /* skip lossy JPEG */) | |
919 | { | |
920 | return; | |
921 | } | |
922 | ||
923 | wxMemoryOutputStream memOut; | |
924 | if ( !image.SaveFile(memOut, type) ) | |
925 | { | |
926 | // Unfortunately we can't know if the handler just doesn't support | |
927 | // saving images, or if it failed to save. | |
928 | return; | |
929 | } | |
930 | ||
931 | wxMemoryInputStream memIn(memOut); | |
932 | CPPUNIT_ASSERT(memIn.IsOk()); | |
933 | ||
934 | wxImage actual(memIn); | |
935 | CPPUNIT_ASSERT(actual.IsOk()); | |
936 | ||
937 | const wxImage *expected = compareTo ? compareTo : ℑ | |
938 | CPPUNIT_ASSERT( actual.GetSize() == expected->GetSize() ); | |
939 | ||
940 | unsigned bitsPerPixel = testPalette ? 8 : (testAlpha ? 32 : 24); | |
941 | WX_ASSERT_EQUAL_MESSAGE | |
942 | ( | |
943 | ("Compare test '%s (%d-bit)' for saving failed", | |
944 | handler.GetExtension(), bitsPerPixel), | |
945 | *expected, | |
946 | actual | |
947 | ); | |
948 | ||
949 | #if wxUSE_PALETTE | |
950 | CPPUNIT_ASSERT(actual.HasPalette() | |
951 | == (testPalette || type == wxBITMAP_TYPE_XPM)); | |
952 | #endif | |
953 | ||
954 | CPPUNIT_ASSERT( actual.HasAlpha() == testAlpha); | |
955 | ||
956 | if (!testAlpha) | |
957 | { | |
958 | return; | |
959 | } | |
960 | ||
961 | WX_ASSERT_EQUAL_MESSAGE | |
962 | ( | |
963 | ("Compare alpha test '%s' for saving failed", handler.GetExtension()), | |
964 | *expected, | |
965 | actual | |
966 | ); | |
967 | } | |
968 | ||
969 | static void SetAlpha(wxImage *image) | |
970 | { | |
971 | image->SetAlpha(); | |
972 | ||
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) | |
977 | { | |
978 | for (int x = 0; x < width; ++x) | |
979 | { | |
980 | ptr[y*width + x] = (x*y) & wxIMAGE_ALPHA_OPAQUE; | |
981 | } | |
982 | } | |
983 | } | |
984 | ||
985 | void ImageTestCase::CompareSavedImage() | |
986 | { | |
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. | |
990 | int i; | |
991 | ||
992 | wxImage expected24("horse.png"); | |
993 | CPPUNIT_ASSERT( expected24.IsOk() ); | |
994 | CPPUNIT_ASSERT( !expected24.HasAlpha() ); | |
995 | ||
996 | wxImage expected8 = expected24.ConvertToGreyscale(); | |
997 | ||
998 | #if wxUSE_PALETTE | |
999 | unsigned char greys[256]; | |
1000 | for (i = 0; i < 256; ++i) | |
1001 | { | |
1002 | greys[i] = i; | |
1003 | } | |
1004 | wxPalette palette(256, greys, greys, greys); | |
1005 | expected8.SetPalette(palette); | |
1006 | #endif // #if wxUSE_PALETTE | |
1007 | ||
1008 | expected8.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP_PALETTE); | |
1009 | ||
1010 | // Create an image with alpha based on the loaded image | |
1011 | wxImage expected32(expected24); | |
1012 | ||
1013 | SetAlpha(&expected32); | |
1014 | ||
1015 | const wxList& list = wxImage::GetHandlers(); | |
1016 | for ( wxList::compatibility_iterator node = list.GetFirst(); | |
1017 | node; node = node->GetNext() ) | |
1018 | { | |
1019 | wxImageHandler *handler = (wxImageHandler *) node->GetData(); | |
1020 | ||
1021 | #if wxUSE_PALETTE | |
1022 | CompareImage(*handler, expected8, wxIMAGE_HAVE_PALETTE); | |
1023 | #endif | |
1024 | CompareImage(*handler, expected24); | |
1025 | CompareImage(*handler, expected32, wxIMAGE_HAVE_ALPHA); | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | void ImageTestCase::SavePNG() | |
1030 | { | |
1031 | wxImage expected24("horse.png"); | |
1032 | CPPUNIT_ASSERT( expected24.IsOk() ); | |
1033 | #if wxUSE_PALETTE | |
1034 | CPPUNIT_ASSERT( !expected24.HasPalette() ); | |
1035 | #endif // #if wxUSE_PALETTE | |
1036 | ||
1037 | wxImage expected8 = expected24.ConvertToGreyscale(); | |
1038 | ||
1039 | /* | |
1040 | horse.png converted to greyscale should be saved without a palette. | |
1041 | */ | |
1042 | CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), expected8); | |
1043 | ||
1044 | /* | |
1045 | But if we explicitly ask for trying to save with a palette, it should work. | |
1046 | */ | |
1047 | expected8.SetOption(wxIMAGE_OPTION_PNG_FORMAT, wxPNG_TYPE_PALETTE); | |
1048 | ||
1049 | CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), | |
1050 | expected8, wxIMAGE_HAVE_PALETTE); | |
1051 | ||
1052 | ||
1053 | CPPUNIT_ASSERT( expected8.LoadFile("horse.gif") ); | |
1054 | #if wxUSE_PALETTE | |
1055 | CPPUNIT_ASSERT( expected8.HasPalette() ); | |
1056 | #endif // #if wxUSE_PALETTE | |
1057 | ||
1058 | CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), | |
1059 | expected8, wxIMAGE_HAVE_PALETTE); | |
1060 | ||
1061 | /* | |
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. | |
1065 | */ | |
1066 | expected8.SetAlpha(); | |
1067 | ||
1068 | int x, y; | |
1069 | const int width = expected8.GetWidth(); | |
1070 | const int height = expected8.GetHeight(); | |
1071 | for (y = 0; y < height; ++y) | |
1072 | { | |
1073 | for (x = 0; x < width; ++x) | |
1074 | { | |
1075 | expected8.SetAlpha(x, y, expected8.GetRed(x, y)); | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), | |
1080 | expected8, wxIMAGE_HAVE_ALPHA|wxIMAGE_HAVE_PALETTE); | |
1081 | ||
1082 | /* | |
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 | |
1085 | of 256 entries. | |
1086 | */ | |
1087 | expected8.SetAlpha(0, 0, 1); | |
1088 | ||
1089 | CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), | |
1090 | expected8, wxIMAGE_HAVE_ALPHA); | |
1091 | ||
1092 | /* | |
1093 | Even if we explicitly ask for saving palettised it should not be done. | |
1094 | */ | |
1095 | expected8.SetOption(wxIMAGE_OPTION_PNG_FORMAT, wxPNG_TYPE_PALETTE); | |
1096 | CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), | |
1097 | expected8, wxIMAGE_HAVE_ALPHA); | |
1098 | ||
1099 | } | |
1100 | ||
1101 | static void TestTIFFImage(const wxString& option, int value, | |
1102 | const wxImage *compareImage = NULL) | |
1103 | { | |
1104 | wxImage image; | |
1105 | if (compareImage) | |
1106 | { | |
1107 | image = *compareImage; | |
1108 | } | |
1109 | else | |
1110 | { | |
1111 | (void) image.LoadFile("horse.png"); | |
1112 | } | |
1113 | CPPUNIT_ASSERT( image.IsOk() ); | |
1114 | ||
1115 | wxMemoryOutputStream memOut; | |
1116 | image.SetOption(option, value); | |
1117 | ||
1118 | CPPUNIT_ASSERT(image.SaveFile(memOut, wxBITMAP_TYPE_TIFF)); | |
1119 | ||
1120 | wxMemoryInputStream memIn(memOut); | |
1121 | CPPUNIT_ASSERT(memIn.IsOk()); | |
1122 | ||
1123 | wxImage savedImage(memIn); | |
1124 | CPPUNIT_ASSERT(savedImage.IsOk()); | |
1125 | ||
1126 | WX_ASSERT_EQUAL_MESSAGE(("While checking for option %s", option), | |
1127 | true, savedImage.HasOption(option)); | |
1128 | ||
1129 | WX_ASSERT_EQUAL_MESSAGE(("While testing for %s", option), | |
1130 | value, savedImage.GetOptionInt(option)); | |
1131 | ||
1132 | WX_ASSERT_EQUAL_MESSAGE(("HasAlpha() not equal"), image.HasAlpha(), savedImage.HasAlpha()); | |
1133 | } | |
1134 | ||
1135 | void ImageTestCase::SaveTIFF() | |
1136 | { | |
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*/); | |
1141 | ||
1142 | wxImage alphaImage("horse.png"); | |
1143 | CPPUNIT_ASSERT( alphaImage.IsOk() ); | |
1144 | SetAlpha(&alphaImage); | |
1145 | ||
1146 | // RGB with alpha | |
1147 | TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL, 4, &alphaImage); | |
1148 | ||
1149 | // Grey with alpha | |
1150 | TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL, 2, &alphaImage); | |
1151 | ||
1152 | // B/W with alpha | |
1153 | alphaImage.SetOption(wxIMAGE_OPTION_TIFF_BITSPERSAMPLE, 1); | |
1154 | TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL, 2, &alphaImage); | |
1155 | } | |
1156 | ||
1157 | void ImageTestCase::SaveAnimatedGIF() | |
1158 | { | |
1159 | #if wxUSE_PALETTE | |
1160 | wxImage image("horse.gif"); | |
1161 | CPPUNIT_ASSERT( image.IsOk() ); | |
1162 | ||
1163 | wxImageArray images; | |
1164 | images.Add(image); | |
1165 | int i; | |
1166 | for (i = 0; i < 4-1; ++i) | |
1167 | { | |
1168 | images.Add( images[i].Rotate90() ); | |
1169 | ||
1170 | images[i+1].SetPalette(images[0].GetPalette()); | |
1171 | } | |
1172 | ||
1173 | wxMemoryOutputStream memOut; | |
1174 | CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images, &memOut) ); | |
1175 | ||
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); | |
1181 | ||
1182 | for (i = 0; i < imageCount; ++i) | |
1183 | { | |
1184 | wxFileOffset pos = memIn.TellI(); | |
1185 | CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true, i) ); | |
1186 | memIn.SeekI(pos); | |
1187 | ||
1188 | WX_ASSERT_EQUAL_MESSAGE | |
1189 | ( | |
1190 | ("Compare test for GIF frame number %d failed", i), | |
1191 | images[i], | |
1192 | image | |
1193 | ); | |
1194 | } | |
1195 | #endif // #if wxUSE_PALETTE | |
1196 | } | |
1197 | ||
1198 | void ImageTestCase::ReadCorruptedTGA() | |
1199 | { | |
1200 | static unsigned char corruptTGA[18+1+3] = | |
1201 | { | |
1202 | 0, | |
1203 | 0, | |
1204 | 10, // RLE compressed image. | |
1205 | 0, 0, | |
1206 | 0, 0, | |
1207 | 0, | |
1208 | 0, 0, | |
1209 | 0, 0, | |
1210 | 1, 0, // Width is 1. | |
1211 | 1, 0, // Height is 1. | |
1212 | 24, // Bits per pixel. | |
1213 | 0, | |
1214 | ||
1215 | 0xff, // Run length (repeat next pixel 127+1 times). | |
1216 | 0xff, 0xff, 0xff // One 24-bit pixel. | |
1217 | }; | |
1218 | ||
1219 | wxMemoryInputStream memIn(corruptTGA, WXSIZEOF(corruptTGA)); | |
1220 | CPPUNIT_ASSERT(memIn.IsOk()); | |
1221 | ||
1222 | wxImage tgaImage; | |
1223 | CPPUNIT_ASSERT( !tgaImage.LoadFile(memIn) ); | |
1224 | ||
1225 | ||
1226 | /* | |
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). | |
1229 | */ | |
1230 | corruptTGA[18] = 0x7f; | |
1231 | CPPUNIT_ASSERT( !tgaImage.LoadFile(memIn) ); | |
1232 | } | |
1233 | ||
1234 | static void TestGIFComment(const wxString& comment) | |
1235 | { | |
1236 | wxImage image("horse.gif"); | |
1237 | ||
1238 | image.SetOption(wxIMAGE_OPTION_GIF_COMMENT, comment); | |
1239 | wxMemoryOutputStream memOut; | |
1240 | CPPUNIT_ASSERT(image.SaveFile(memOut, wxBITMAP_TYPE_GIF)); | |
1241 | ||
1242 | wxMemoryInputStream memIn(memOut); | |
1243 | CPPUNIT_ASSERT( image.LoadFile(memIn) ); | |
1244 | ||
1245 | CPPUNIT_ASSERT_EQUAL(comment, | |
1246 | image.GetOption(wxIMAGE_OPTION_GIF_COMMENT)); | |
1247 | } | |
1248 | ||
1249 | void ImageTestCase::GIFComment() | |
1250 | { | |
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)); | |
1255 | ||
1256 | ||
1257 | // Test writing a comment and reading it back. | |
1258 | TestGIFComment("Giving the GIF a gifted giraffe as a gift"); | |
1259 | ||
1260 | ||
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)); | |
1265 | ||
1266 | ||
1267 | // Test writing comments in an animated GIF and reading them back. | |
1268 | CPPUNIT_ASSERT( image.LoadFile("horse.gif") ); | |
1269 | ||
1270 | wxImageArray images; | |
1271 | int i; | |
1272 | for (i = 0; i < 4; ++i) | |
1273 | { | |
1274 | if (i) | |
1275 | { | |
1276 | images.Add( images[i-1].Rotate90() ); | |
1277 | images[i].SetPalette(images[0].GetPalette()); | |
1278 | } | |
1279 | else | |
1280 | { | |
1281 | images.Add(image); | |
1282 | } | |
1283 | ||
1284 | images[i].SetOption(wxIMAGE_OPTION_GIF_COMMENT, | |
1285 | wxString::Format("GIF comment for frame #%d", i+1)); | |
1286 | ||
1287 | } | |
1288 | ||
1289 | ||
1290 | wxMemoryOutputStream memOut; | |
1291 | CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images, &memOut) ); | |
1292 | ||
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) | |
1298 | { | |
1299 | wxFileOffset pos = memIn.TellI(); | |
1300 | CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true /*verbose?*/, i) ); | |
1301 | ||
1302 | CPPUNIT_ASSERT_EQUAL( | |
1303 | wxString::Format("GIF comment for frame #%d", i+1), | |
1304 | image.GetOption(wxIMAGE_OPTION_GIF_COMMENT)); | |
1305 | memIn.SeekI(pos); | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | void ImageTestCase::DibPadding() | |
1310 | { | |
1311 | /* | |
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. | |
1316 | */ | |
1317 | wxImage image("horse.gif"); | |
1318 | CPPUNIT_ASSERT( image.IsOk() ); | |
1319 | ||
1320 | image = image.Scale(99, 99); | |
1321 | ||
1322 | wxMemoryOutputStream memOut; | |
1323 | CPPUNIT_ASSERT( image.SaveFile(memOut, wxBITMAP_TYPE_ICO) ); | |
1324 | } | |
1325 | ||
1326 | static void CompareBMPImage(const wxString& file1, const wxString& file2) | |
1327 | { | |
1328 | wxImage image1(file1); | |
1329 | CPPUNIT_ASSERT( image1.IsOk() ); | |
1330 | ||
1331 | wxImage image2(file2); | |
1332 | CPPUNIT_ASSERT( image2.IsOk() ); | |
1333 | ||
1334 | CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_BMP), image1, 0, &image2); | |
1335 | } | |
1336 | ||
1337 | void ImageTestCase::BMPFlippingAndRLECompression() | |
1338 | { | |
1339 | CompareBMPImage("image/horse_grey.bmp", "image/horse_grey_flipped.bmp"); | |
1340 | ||
1341 | CompareBMPImage("image/horse_rle8.bmp", "image/horse_grey.bmp"); | |
1342 | CompareBMPImage("image/horse_rle8.bmp", "image/horse_rle8_flipped.bmp"); | |
1343 | ||
1344 | CompareBMPImage("image/horse_rle4.bmp", "image/horse_rle4_flipped.bmp"); | |
1345 | } | |
1346 | #endif //wxUSE_IMAGE | |
1347 | ||
1348 | ||
1349 | /* | |
1350 | TODO: add lots of more tests to wxImage functions | |
1351 | */ |