Added option to TIFF handler for specifying the photometric interpretation.
[wxWidgets.git] / tests / image / image.cpp
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_TIF, 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_TIF:
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 {
913 // don't test images with alpha if this handler doesn't support alpha
914 return;
915 }
916
917 if (type == wxBITMAP_TYPE_JPEG /* skip lossy JPEG */)
918 {
919 return;
920 }
921
922 wxMemoryOutputStream memOut;
923 if ( !image.SaveFile(memOut, type) )
924 {
925 // Unfortunately we can't know if the handler just doesn't support
926 // saving images, or if it failed to save.
927 return;
928 }
929
930 wxMemoryInputStream memIn(memOut);
931 CPPUNIT_ASSERT(memIn.IsOk());
932
933 wxImage actual(memIn);
934 CPPUNIT_ASSERT(actual.IsOk());
935
936 const wxImage *expected = compareTo ? compareTo : &image;
937 CPPUNIT_ASSERT( actual.GetSize() == expected->GetSize() );
938
939 unsigned bitsPerPixel = testPalette ? 8 : (testAlpha ? 32 : 24);
940 WX_ASSERT_EQUAL_MESSAGE
941 (
942 ("Compare test '%s (%d-bit)' for saving failed",
943 handler.GetExtension(), bitsPerPixel),
944 *expected,
945 actual
946 );
947
948 #if wxUSE_PALETTE
949 CPPUNIT_ASSERT(actual.HasPalette()
950 == (testPalette || type == wxBITMAP_TYPE_XPM));
951 #endif
952
953 CPPUNIT_ASSERT( actual.HasAlpha() == testAlpha);
954
955 if (!testAlpha)
956 {
957 return;
958 }
959
960 WX_ASSERT_EQUAL_MESSAGE
961 (
962 ("Compare alpha test '%s' for saving failed", handler.GetExtension()),
963 *expected,
964 actual
965 );
966 }
967
968 void ImageTestCase::CompareSavedImage()
969 {
970 // FIXME-VC6: Pre-declare the loop variables for compatibility with
971 // pre-standard compilers such as MSVC6 that don't implement proper scope
972 // for the variables declared in the for loops.
973 int i, x, y;
974
975 wxImage expected24("horse.png");
976 CPPUNIT_ASSERT( expected24.IsOk() );
977 CPPUNIT_ASSERT( !expected24.HasAlpha() );
978
979 wxImage expected8 = expected24.ConvertToGreyscale();
980
981 #if wxUSE_PALETTE
982 unsigned char greys[256];
983 for (i = 0; i < 256; ++i)
984 {
985 greys[i] = i;
986 }
987 wxPalette palette(256, greys, greys, greys);
988 expected8.SetPalette(palette);
989 #endif // #if wxUSE_PALETTE
990
991 expected8.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP_PALETTE);
992
993 // Create an image with alpha based on the loaded image
994 wxImage expected32(expected24);
995 expected32.SetAlpha();
996
997 int width = expected32.GetWidth();
998 int height = expected32.GetHeight();
999 for (y = 0; y < height; ++y)
1000 {
1001 for (x = 0; x < width; ++x)
1002 {
1003 expected32.SetAlpha(x, y, (x*y) & wxIMAGE_ALPHA_OPAQUE);
1004 }
1005 }
1006
1007 const wxList& list = wxImage::GetHandlers();
1008 for ( wxList::compatibility_iterator node = list.GetFirst();
1009 node; node = node->GetNext() )
1010 {
1011 wxImageHandler *handler = (wxImageHandler *) node->GetData();
1012
1013 #if wxUSE_PALETTE
1014 CompareImage(*handler, expected8, wxIMAGE_HAVE_PALETTE);
1015 #endif
1016 CompareImage(*handler, expected24);
1017 CompareImage(*handler, expected32, wxIMAGE_HAVE_ALPHA);
1018 }
1019 }
1020
1021 void ImageTestCase::SavePNG()
1022 {
1023 wxImage expected24("horse.png");
1024 CPPUNIT_ASSERT( expected24.IsOk() );
1025 #if wxUSE_PALETTE
1026 CPPUNIT_ASSERT( !expected24.HasPalette() );
1027 #endif // #if wxUSE_PALETTE
1028
1029 wxImage expected8 = expected24.ConvertToGreyscale();
1030
1031 /*
1032 horse.png converted to greyscale should be saved without a palette.
1033 */
1034 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), expected8);
1035
1036 /*
1037 But if we explicitly ask for trying to save with a palette, it should work.
1038 */
1039 expected8.SetOption(wxIMAGE_OPTION_PNG_FORMAT, wxPNG_TYPE_PALETTE);
1040
1041 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1042 expected8, wxIMAGE_HAVE_PALETTE);
1043
1044
1045 CPPUNIT_ASSERT( expected8.LoadFile("horse.gif") );
1046 #if wxUSE_PALETTE
1047 CPPUNIT_ASSERT( expected8.HasPalette() );
1048 #endif // #if wxUSE_PALETTE
1049
1050 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1051 expected8, wxIMAGE_HAVE_PALETTE);
1052
1053 /*
1054 Add alpha to the image in such a way that there will still be a maximum
1055 of 256 unique RGBA combinations. This should result in a saved
1056 PNG image still being palettised and having alpha.
1057 */
1058 expected8.SetAlpha();
1059
1060 int x, y;
1061 const int width = expected8.GetWidth();
1062 const int height = expected8.GetHeight();
1063 for (y = 0; y < height; ++y)
1064 {
1065 for (x = 0; x < width; ++x)
1066 {
1067 expected8.SetAlpha(x, y, expected8.GetRed(x, y));
1068 }
1069 }
1070
1071 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1072 expected8, wxIMAGE_HAVE_ALPHA|wxIMAGE_HAVE_PALETTE);
1073
1074 /*
1075 Now change the alpha of the first pixel so that we can't save palettised
1076 anymore because there will be 256+1 entries which is beyond PNGs limit
1077 of 256 entries.
1078 */
1079 expected8.SetAlpha(0, 0, 1);
1080
1081 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1082 expected8, wxIMAGE_HAVE_ALPHA);
1083
1084 /*
1085 Even if we explicitly ask for saving palettised it should not be done.
1086 */
1087 expected8.SetOption(wxIMAGE_OPTION_PNG_FORMAT, wxPNG_TYPE_PALETTE);
1088 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1089 expected8, wxIMAGE_HAVE_ALPHA);
1090
1091 }
1092
1093 static void TestTIFFImage(const wxString& option, int value)
1094 {
1095 wxImage image("horse.png");
1096
1097 wxMemoryOutputStream memOut;
1098 image.SetOption(option, value);
1099
1100 CPPUNIT_ASSERT(image.SaveFile(memOut, wxBITMAP_TYPE_TIF));
1101
1102 wxMemoryInputStream memIn(memOut);
1103 CPPUNIT_ASSERT(memIn.IsOk());
1104
1105 wxImage savedImage(memIn);
1106 CPPUNIT_ASSERT(savedImage.IsOk());
1107
1108 WX_ASSERT_EQUAL_MESSAGE(("While checking for option %s", option),
1109 true, savedImage.HasOption(option));
1110
1111 WX_ASSERT_EQUAL_MESSAGE(("While testing for %s", option),
1112 value, savedImage.GetOptionInt(option));
1113 }
1114
1115 void ImageTestCase::SaveTIFF()
1116 {
1117 TestTIFFImage(wxIMAGE_OPTION_TIFF_BITSPERSAMPLE, 1);
1118 TestTIFFImage(wxIMAGE_OPTION_TIFF_SAMPLESPERPIXEL, 1);
1119 TestTIFFImage(wxIMAGE_OPTION_TIFF_PHOTOMETRIC, 0/*PHOTOMETRIC_MINISWHITE*/);
1120 TestTIFFImage(wxIMAGE_OPTION_TIFF_PHOTOMETRIC, 1/*PHOTOMETRIC_MINISBLACK*/);
1121 }
1122
1123 void ImageTestCase::SaveAnimatedGIF()
1124 {
1125 #if wxUSE_PALETTE
1126 wxImage image("horse.gif");
1127 CPPUNIT_ASSERT( image.IsOk() );
1128
1129 wxImageArray images;
1130 images.Add(image);
1131 int i;
1132 for (i = 0; i < 4-1; ++i)
1133 {
1134 images.Add( images[i].Rotate90() );
1135
1136 images[i+1].SetPalette(images[0].GetPalette());
1137 }
1138
1139 wxMemoryOutputStream memOut;
1140 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images, &memOut) );
1141
1142 wxGIFHandler handler;
1143 wxMemoryInputStream memIn(memOut);
1144 CPPUNIT_ASSERT(memIn.IsOk());
1145 const int imageCount = handler.GetImageCount(memIn);
1146 CPPUNIT_ASSERT_EQUAL(4, imageCount);
1147
1148 for (i = 0; i < imageCount; ++i)
1149 {
1150 wxFileOffset pos = memIn.TellI();
1151 CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true, i) );
1152 memIn.SeekI(pos);
1153
1154 WX_ASSERT_EQUAL_MESSAGE
1155 (
1156 ("Compare test for GIF frame number %d failed", i),
1157 images[i],
1158 image
1159 );
1160 }
1161 #endif // #if wxUSE_PALETTE
1162 }
1163
1164 void ImageTestCase::ReadCorruptedTGA()
1165 {
1166 static unsigned char corruptTGA[18+1+3] =
1167 {
1168 0,
1169 0,
1170 10, // RLE compressed image.
1171 0, 0,
1172 0, 0,
1173 0,
1174 0, 0,
1175 0, 0,
1176 1, 0, // Width is 1.
1177 1, 0, // Height is 1.
1178 24, // Bits per pixel.
1179 0,
1180
1181 0xff, // Run length (repeat next pixel 127+1 times).
1182 0xff, 0xff, 0xff // One 24-bit pixel.
1183 };
1184
1185 wxMemoryInputStream memIn(corruptTGA, WXSIZEOF(corruptTGA));
1186 CPPUNIT_ASSERT(memIn.IsOk());
1187
1188 wxImage tgaImage;
1189 CPPUNIT_ASSERT( !tgaImage.LoadFile(memIn) );
1190
1191
1192 /*
1193 Instead of repeating a pixel 127+1 times, now tell it there will
1194 follow 127+1 uncompressed pixels (while we only should have 1 in total).
1195 */
1196 corruptTGA[18] = 0x7f;
1197 CPPUNIT_ASSERT( !tgaImage.LoadFile(memIn) );
1198 }
1199
1200 static void TestGIFComment(const wxString& comment)
1201 {
1202 wxImage image("horse.gif");
1203
1204 image.SetOption(wxIMAGE_OPTION_GIF_COMMENT, comment);
1205 wxMemoryOutputStream memOut;
1206 CPPUNIT_ASSERT(image.SaveFile(memOut, wxBITMAP_TYPE_GIF));
1207
1208 wxMemoryInputStream memIn(memOut);
1209 CPPUNIT_ASSERT( image.LoadFile(memIn) );
1210
1211 CPPUNIT_ASSERT_EQUAL(comment,
1212 image.GetOption(wxIMAGE_OPTION_GIF_COMMENT));
1213 }
1214
1215 void ImageTestCase::GIFComment()
1216 {
1217 // Test reading a comment.
1218 wxImage image("horse.gif");
1219 CPPUNIT_ASSERT_EQUAL(" Imported from GRADATION image: gray",
1220 image.GetOption(wxIMAGE_OPTION_GIF_COMMENT));
1221
1222
1223 // Test writing a comment and reading it back.
1224 TestGIFComment("Giving the GIF a gifted giraffe as a gift");
1225
1226
1227 // Test writing and reading a comment again but with a long comment.
1228 TestGIFComment(wxString(wxT('a'), 256)
1229 + wxString(wxT('b'), 256)
1230 + wxString(wxT('c'), 256));
1231
1232
1233 // Test writing comments in an animated GIF and reading them back.
1234 CPPUNIT_ASSERT( image.LoadFile("horse.gif") );
1235
1236 wxImageArray images;
1237 int i;
1238 for (i = 0; i < 4; ++i)
1239 {
1240 if (i)
1241 {
1242 images.Add( images[i-1].Rotate90() );
1243 images[i].SetPalette(images[0].GetPalette());
1244 }
1245 else
1246 {
1247 images.Add(image);
1248 }
1249
1250 images[i].SetOption(wxIMAGE_OPTION_GIF_COMMENT,
1251 wxString::Format("GIF comment for frame #%d", i+1));
1252
1253 }
1254
1255
1256 wxMemoryOutputStream memOut;
1257 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images, &memOut) );
1258
1259 wxGIFHandler handler;
1260 wxMemoryInputStream memIn(memOut);
1261 CPPUNIT_ASSERT(memIn.IsOk());
1262 const int imageCount = handler.GetImageCount(memIn);
1263 for (i = 0; i < imageCount; ++i)
1264 {
1265 wxFileOffset pos = memIn.TellI();
1266 CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true /*verbose?*/, i) );
1267
1268 CPPUNIT_ASSERT_EQUAL(
1269 wxString::Format("GIF comment for frame #%d", i+1),
1270 image.GetOption(wxIMAGE_OPTION_GIF_COMMENT));
1271 memIn.SeekI(pos);
1272 }
1273 }
1274
1275 void ImageTestCase::DibPadding()
1276 {
1277 /*
1278 There used to be an error with calculating the DWORD aligned scan line
1279 pitch for a BMP/ICO resulting in buffer overwrites (with at least MSVC9
1280 Debug this gave a heap corruption assertion when saving the mask of
1281 an ICO). Test for it here.
1282 */
1283 wxImage image("horse.gif");
1284 CPPUNIT_ASSERT( image.IsOk() );
1285
1286 image = image.Scale(99, 99);
1287
1288 wxMemoryOutputStream memOut;
1289 CPPUNIT_ASSERT( image.SaveFile(memOut, wxBITMAP_TYPE_ICO) );
1290 }
1291
1292 static void CompareBMPImage(const wxString& file1, const wxString& file2)
1293 {
1294 wxImage image1(file1);
1295 CPPUNIT_ASSERT( image1.IsOk() );
1296
1297 wxImage image2(file2);
1298 CPPUNIT_ASSERT( image2.IsOk() );
1299
1300 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_BMP), image1, 0, &image2);
1301 }
1302
1303 void ImageTestCase::BMPFlippingAndRLECompression()
1304 {
1305 CompareBMPImage("image/horse_grey.bmp", "image/horse_grey_flipped.bmp");
1306
1307 CompareBMPImage("image/horse_rle8.bmp", "image/horse_grey.bmp");
1308 CompareBMPImage("image/horse_rle8.bmp", "image/horse_rle8_flipped.bmp");
1309
1310 CompareBMPImage("image/horse_rle4.bmp", "image/horse_rle4_flipped.bmp");
1311 }
1312 #endif //wxUSE_IMAGE
1313
1314
1315 /*
1316 TODO: add lots of more tests to wxImage functions
1317 */