]> git.saurik.com Git - wxWidgets.git/blame - tests/image/image.cpp
No code changes.
[wxWidgets.git] / tests / image / image.cpp
CommitLineData
21fe35af
FM
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
232fdc63
VZ
17#if wxUSE_IMAGE
18
21fe35af
FM
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#endif // WX_PRECOMP
25
77b83d0a 26#include "wx/anidecod.h" // wxImageArray
8ee313d2 27#include "wx/palette.h"
21fe35af 28#include "wx/url.h"
9e91447c 29#include "wx/log.h"
21fe35af
FM
30#include "wx/mstream.h"
31#include "wx/zstream.h"
32#include "wx/wfstream.h"
33
a25b5bbf
VZ
34#include "testimage.h"
35
21fe35af
FM
36struct testData {
37 const char* file;
38 wxBitmapType type;
2b39567f 39 unsigned bitDepth;
51fe4100 40} g_testfiles[] =
21fe35af 41{
2b39567f
DS
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 }
21fe35af
FM
54};
55
56
57// ----------------------------------------------------------------------------
58// test class
59// ----------------------------------------------------------------------------
60
61class ImageTestCase : public CppUnit::TestCase
62{
63public:
64 ImageTestCase();
65 ~ImageTestCase();
66
67private:
68 CPPUNIT_TEST_SUITE( ImageTestCase );
69 CPPUNIT_TEST( LoadFromSocketStream );
70 CPPUNIT_TEST( LoadFromZipStream );
71 CPPUNIT_TEST( LoadFromFile );
2e51fb30 72 CPPUNIT_TEST( SizeImage );
2b39567f
DS
73 CPPUNIT_TEST( CompareLoadedImage );
74 CPPUNIT_TEST( CompareSavedImage );
8529b0b9 75 CPPUNIT_TEST( SavePNG );
77b83d0a 76 CPPUNIT_TEST( SaveAnimatedGIF );
fdfedfc0 77 CPPUNIT_TEST( ReadCorruptedTGA );
15f345aa 78 CPPUNIT_TEST( GIFComment );
2a6545a5 79 CPPUNIT_TEST( DibPadding );
d06c3098 80 CPPUNIT_TEST( BMPFlippingAndRLECompression );
21fe35af
FM
81 CPPUNIT_TEST_SUITE_END();
82
83 void LoadFromSocketStream();
84 void LoadFromZipStream();
85 void LoadFromFile();
2e51fb30 86 void SizeImage();
2b39567f
DS
87 void CompareLoadedImage();
88 void CompareSavedImage();
8529b0b9 89 void SavePNG();
77b83d0a 90 void SaveAnimatedGIF();
fdfedfc0 91 void ReadCorruptedTGA();
15f345aa 92 void GIFComment();
2a6545a5 93 void DibPadding();
d06c3098 94 void BMPFlippingAndRLECompression();
21fe35af
FM
95
96 DECLARE_NO_COPY_CLASS(ImageTestCase)
97};
98
99CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase );
100CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase, "ImageTestCase" );
101
102ImageTestCase::ImageTestCase()
103{
104 wxSocketBase::Initialize();
105
106 // the formats we're going to test:
107 wxImage::AddHandler(new wxICOHandler);
108 wxImage::AddHandler(new wxXPMHandler);
109 wxImage::AddHandler(new wxPNGHandler);
110 wxImage::AddHandler(new wxANIHandler);
111 wxImage::AddHandler(new wxBMPHandler);
112 wxImage::AddHandler(new wxCURHandler);
113 wxImage::AddHandler(new wxGIFHandler);
114 wxImage::AddHandler(new wxJPEGHandler);
115 wxImage::AddHandler(new wxPCXHandler);
116 wxImage::AddHandler(new wxPNMHandler);
117 wxImage::AddHandler(new wxTGAHandler);
118 wxImage::AddHandler(new wxTIFFHandler);
119}
120
121ImageTestCase::~ImageTestCase()
122{
123 wxSocketBase::Shutdown();
124}
125
126void ImageTestCase::LoadFromFile()
127{
128 wxImage img;
129 for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++)
130 CPPUNIT_ASSERT(img.LoadFile(g_testfiles[i].file));
131}
132
133void ImageTestCase::LoadFromSocketStream()
134{
c99214eb
FM
135 if (!IsNetworkAvailable()) // implemented in test.cpp
136 {
51fe4100
VZ
137 wxLogWarning("No network connectivity; skipping the "
138 "ImageTestCase::LoadFromSocketStream test unit.");
c99214eb
FM
139 return;
140 }
141
21fe35af
FM
142 struct {
143 const char* url;
144 wxBitmapType type;
51fe4100 145 } testData[] =
21fe35af 146 {
686d0cc0
VZ
147 { "http://www.wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG },
148 { "http://www.wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO }
21fe35af 149 };
51fe4100 150
21fe35af
FM
151 for (unsigned int i=0; i<WXSIZEOF(testData); i++)
152 {
153 wxURL url(testData[i].url);
686d0cc0
VZ
154 WX_ASSERT_EQUAL_MESSAGE
155 (
156 ("Constructing URL \"%s\" failed.", testData[i].url),
157 wxURL_NOERR,
158 url.GetError()
159 );
21fe35af
FM
160
161 wxInputStream *in_stream = url.GetInputStream();
686d0cc0
VZ
162 WX_ASSERT_MESSAGE
163 (
164 ("Opening URL \"%s\" failed.", testData[i].url),
165 in_stream && in_stream->IsOk()
166 );
21fe35af
FM
167
168 wxImage img;
51fe4100 169
21fe35af
FM
170 // NOTE: it's important to inform wxImage about the type of the image being
171 // loaded otherwise it will try to autodetect the format, but that
172 // requires a seekable stream!
686d0cc0
VZ
173 WX_ASSERT_MESSAGE
174 (
175 ("Loading image from \"%s\" failed.", testData[i].url),
176 img.LoadFile(*in_stream, testData[i].type)
177 );
51fe4100 178
21fe35af
FM
179 delete in_stream;
180 }
181}
182
183void ImageTestCase::LoadFromZipStream()
184{
185 for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++)
186 {
187 switch (g_testfiles[i].type)
188 {
189 case wxBITMAP_TYPE_XPM:
190 case wxBITMAP_TYPE_GIF:
191 case wxBITMAP_TYPE_PCX:
192 case wxBITMAP_TYPE_TGA:
193 case wxBITMAP_TYPE_TIF:
194 continue; // skip testing those wxImageHandlers which cannot
195 // load data from non-seekable streams
51fe4100 196
21fe35af
FM
197 default:
198 ; // proceed
199 }
51fe4100 200
21fe35af
FM
201 // compress the test file on the fly:
202 wxMemoryOutputStream memOut;
203 {
204 wxFileInputStream file(g_testfiles[i].file);
205 CPPUNIT_ASSERT(file.IsOk());
51fe4100 206
21fe35af
FM
207 wxZlibOutputStream compressFilter(memOut, 5, wxZLIB_GZIP);
208 CPPUNIT_ASSERT(compressFilter.IsOk());
51fe4100 209
21fe35af
FM
210 file.Read(compressFilter);
211 CPPUNIT_ASSERT(file.GetLastError() == wxSTREAM_EOF);
212 }
51fe4100 213
21fe35af
FM
214 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
215 // allows us to test loading images from non-seekable streams other than socket streams
216 wxMemoryInputStream memIn(memOut);
217 CPPUNIT_ASSERT(memIn.IsOk());
218 wxZlibInputStream decompressFilter(memIn, wxZLIB_GZIP);
219 CPPUNIT_ASSERT(decompressFilter.IsOk());
51fe4100 220
21fe35af 221 wxImage img;
51fe4100 222
21fe35af
FM
223 // NOTE: it's important to inform wxImage about the type of the image being
224 // loaded otherwise it will try to autodetect the format, but that
225 // requires a seekable stream!
226 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles[i].type),
227 img.LoadFile(decompressFilter, g_testfiles[i].type));
228 }
229}
230
2e51fb30
VZ
231void ImageTestCase::SizeImage()
232{
233 // Test the wxImage::Size() function which takes a rectangle from source and
234 // places it in a new image at a given position. This test checks, if the
235 // correct areas are chosen, and clipping is done correctly.
236
237 // our test image:
238 static const char * xpm_orig[] = {
239 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
240 " .....",
241 " ++++@@@@.",
242 " +... @.",
243 " +.@@++ @.",
244 " +.@ .+ @.",
245 ".@ +. @.+ ",
246 ".@ ++@@.+ ",
247 ".@ ...+ ",
248 ".@@@@++++ ",
249 "..... "
250 };
251 // the expected results for all tests:
252 static const char * xpm_l_t[] = {
253 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
254 "... @.BB",
255 ".@@++ @.BB",
256 ".@ .+ @.BB",
257 " +. @.+ BB",
258 " ++@@.+ BB",
259 " ...+ BB",
260 "@@@++++ BB",
261 "... BB",
262 "BBBBBBBBBB",
263 "BBBBBBBBBB"
264 };
265 static const char * xpm_t[] = {
266 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
267 " +... @.",
268 " +.@@++ @.",
269 " +.@ .+ @.",
270 ".@ +. @.+ ",
271 ".@ ++@@.+ ",
272 ".@ ...+ ",
273 ".@@@@++++ ",
274 "..... ",
275 "BBBBBBBBBB",
276 "BBBBBBBBBB"
277 };
278 static const char * xpm_r_t[] = {
279 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
280 "BB +... ",
281 "BB +.@@++ ",
282 "BB +.@ .+ ",
283 "BB.@ +. @.",
284 "BB.@ ++@@.",
285 "BB.@ ...",
286 "BB.@@@@+++",
287 "BB..... ",
288 "BBBBBBBBBB",
289 "BBBBBBBBBB"
290 };
291 static const char * xpm_l[] = {
292 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
293 " .....BB",
294 "+++@@@@.BB",
295 "... @.BB",
296 ".@@++ @.BB",
297 ".@ .+ @.BB",
298 " +. @.+ BB",
299 " ++@@.+ BB",
300 " ...+ BB",
301 "@@@++++ BB",
302 "... BB"
303 };
304 static const char * xpm_r[] = {
305 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
306 "BB ...",
307 "BB ++++@@@",
308 "BB +... ",
309 "BB +.@@++ ",
310 "BB +.@ .+ ",
311 "BB.@ +. @.",
312 "BB.@ ++@@.",
313 "BB.@ ...",
314 "BB.@@@@+++",
315 "BB..... "
316 };
317 static const char * xpm_l_b[] = {
318 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
319 "BBBBBBBBBB",
320 "BBBBBBBBBB",
321 " .....BB",
322 "+++@@@@.BB",
323 "... @.BB",
324 ".@@++ @.BB",
325 ".@ .+ @.BB",
326 " +. @.+ BB",
327 " ++@@.+ BB",
328 " ...+ BB"
329 };
330 static const char * xpm_b[] = {
331 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
332 "BBBBBBBBBB",
333 "BBBBBBBBBB",
334 " .....",
335 " ++++@@@@.",
336 " +... @.",
337 " +.@@++ @.",
338 " +.@ .+ @.",
339 ".@ +. @.+ ",
340 ".@ ++@@.+ ",
341 ".@ ...+ "
342 };
343 static const char * xpm_r_b[] = {
344 "10 10 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
345 "BBBBBBBBBB",
346 "BBBBBBBBBB",
347 "BB ...",
348 "BB ++++@@@",
349 "BB +... ",
350 "BB +.@@++ ",
351 "BB +.@ .+ ",
352 "BB.@ +. @.",
353 "BB.@ ++@@.",
354 "BB.@ ..."
355 };
356 static const char * xpm_sm[] = {
357 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
358 " .....",
359 " ++++@@@",
360 " +... ",
361 " +.@@++ ",
362 " +.@ .+ ",
363 ".@ +. @.",
364 ".@ ++@@.",
365 ".@ ..."
366 };
367 static const char * xpm_gt[] = {
368 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
369 " .....BB",
370 " ++++@@@@.BB",
371 " +... @.BB",
372 " +.@@++ @.BB",
373 " +.@ .+ @.BB",
374 ".@ +. @.+ BB",
375 ".@ ++@@.+ BB",
376 ".@ ...+ BB",
377 ".@@@@++++ BB",
378 "..... BB",
379 "BBBBBBBBBBBB",
380 "BBBBBBBBBBBB"
381 };
382 static const char * xpm_gt_l_t[] = {
383 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
384 "... @.BBBB",
385 ".@@++ @.BBBB",
386 ".@ .+ @.BBBB",
387 " +. @.+ BBBB",
388 " ++@@.+ BBBB",
389 " ...+ BBBB",
390 "@@@++++ BBBB",
391 "... BBBB",
392 "BBBBBBBBBBBB",
393 "BBBBBBBBBBBB",
394 "BBBBBBBBBBBB",
395 "BBBBBBBBBBBB"
396 };
397 static const char * xpm_gt_l[] = {
398 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
399 " .....BBBB",
400 "+++@@@@.BBBB",
401 "... @.BBBB",
402 ".@@++ @.BBBB",
403 ".@ .+ @.BBBB",
404 " +. @.+ BBBB",
405 " ++@@.+ BBBB",
406 " ...+ BBBB",
407 "@@@++++ BBBB",
408 "... BBBB",
409 "BBBBBBBBBBBB",
410 "BBBBBBBBBBBB"
411 };
412 static const char * xpm_gt_l_b[] = {
413 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
414 "BBBBBBBBBBBB",
415 "BBBBBBBBBBBB",
416 " .....BBBB",
417 "+++@@@@.BBBB",
418 "... @.BBBB",
419 ".@@++ @.BBBB",
420 ".@ .+ @.BBBB",
421 " +. @.+ BBBB",
422 " ++@@.+ BBBB",
423 " ...+ BBBB",
424 "@@@++++ BBBB",
425 "... BBBB"
426 };
427 static const char * xpm_gt_l_bb[] = {
428 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
429 "BBBBBBBBBBBB",
430 "BBBBBBBBBBBB",
431 "BBBBBBBBBBBB",
432 "BBBBBBBBBBBB",
433 " .....BBBB",
434 "+++@@@@.BBBB",
435 "... @.BBBB",
436 ".@@++ @.BBBB",
437 ".@ .+ @.BBBB",
438 " +. @.+ BBBB",
439 " ++@@.+ BBBB",
440 " ...+ BBBB"
441 };
442 static const char * xpm_gt_t[] = {
443 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
444 " +... @.BB",
445 " +.@@++ @.BB",
446 " +.@ .+ @.BB",
447 ".@ +. @.+ BB",
448 ".@ ++@@.+ BB",
449 ".@ ...+ BB",
450 ".@@@@++++ BB",
451 "..... BB",
452 "BBBBBBBBBBBB",
453 "BBBBBBBBBBBB",
454 "BBBBBBBBBBBB",
455 "BBBBBBBBBBBB"
456 };
457 static const char * xpm_gt_b[] = {
458 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
459 "BBBBBBBBBBBB",
460 "BBBBBBBBBBBB",
461 " .....BB",
462 " ++++@@@@.BB",
463 " +... @.BB",
464 " +.@@++ @.BB",
465 " +.@ .+ @.BB",
466 ".@ +. @.+ BB",
467 ".@ ++@@.+ BB",
468 ".@ ...+ BB",
469 ".@@@@++++ BB",
470 "..... BB"
471 };
472 static const char * xpm_gt_bb[] = {
473 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
474 "BBBBBBBBBBBB",
475 "BBBBBBBBBBBB",
476 "BBBBBBBBBBBB",
477 "BBBBBBBBBBBB",
478 " .....BB",
479 " ++++@@@@.BB",
480 " +... @.BB",
481 " +.@@++ @.BB",
482 " +.@ .+ @.BB",
483 ".@ +. @.+ BB",
484 ".@ ++@@.+ BB",
485 ".@ ...+ BB"
486 };
487 static const char * xpm_gt_r_t[] = {
488 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
489 "BB +... @.",
490 "BB +.@@++ @.",
491 "BB +.@ .+ @.",
492 "BB.@ +. @.+ ",
493 "BB.@ ++@@.+ ",
494 "BB.@ ...+ ",
495 "BB.@@@@++++ ",
496 "BB..... ",
497 "BBBBBBBBBBBB",
498 "BBBBBBBBBBBB",
499 "BBBBBBBBBBBB",
500 "BBBBBBBBBBBB"
501 };
502 static const char * xpm_gt_r[] = {
503 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
504 "BB .....",
505 "BB ++++@@@@.",
506 "BB +... @.",
507 "BB +.@@++ @.",
508 "BB +.@ .+ @.",
509 "BB.@ +. @.+ ",
510 "BB.@ ++@@.+ ",
511 "BB.@ ...+ ",
512 "BB.@@@@++++ ",
513 "BB..... ",
514 "BBBBBBBBBBBB",
515 "BBBBBBBBBBBB"
516 };
517 static const char * xpm_gt_r_b[] = {
518 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
519 "BBBBBBBBBBBB",
520 "BBBBBBBBBBBB",
521 "BB .....",
522 "BB ++++@@@@.",
523 "BB +... @.",
524 "BB +.@@++ @.",
525 "BB +.@ .+ @.",
526 "BB.@ +. @.+ ",
527 "BB.@ ++@@.+ ",
528 "BB.@ ...+ ",
529 "BB.@@@@++++ ",
530 "BB..... "
531 };
532 static const char * xpm_gt_r_bb[] = {
533 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
534 "BBBBBBBBBBBB",
535 "BBBBBBBBBBBB",
536 "BBBBBBBBBBBB",
537 "BBBBBBBBBBBB",
538 "BB .....",
539 "BB ++++@@@@.",
540 "BB +... @.",
541 "BB +.@@++ @.",
542 "BB +.@ .+ @.",
543 "BB.@ +. @.+ ",
544 "BB.@ ++@@.+ ",
545 "BB.@ ...+ "
546 };
547 static const char * xpm_gt_rr_t[] = {
548 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
549 "BBBB +... ",
550 "BBBB +.@@++ ",
551 "BBBB +.@ .+ ",
552 "BBBB.@ +. @.",
553 "BBBB.@ ++@@.",
554 "BBBB.@ ...",
555 "BBBB.@@@@+++",
556 "BBBB..... ",
557 "BBBBBBBBBBBB",
558 "BBBBBBBBBBBB",
559 "BBBBBBBBBBBB",
560 "BBBBBBBBBBBB"
561 };
562 static const char * xpm_gt_rr[] = {
563 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
564 "BBBB ...",
565 "BBBB ++++@@@",
566 "BBBB +... ",
567 "BBBB +.@@++ ",
568 "BBBB +.@ .+ ",
569 "BBBB.@ +. @.",
570 "BBBB.@ ++@@.",
571 "BBBB.@ ...",
572 "BBBB.@@@@+++",
573 "BBBB..... ",
574 "BBBBBBBBBBBB",
575 "BBBBBBBBBBBB"
576 };
577 static const char * xpm_gt_rr_b[] = {
578 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
579 "BBBBBBBBBBBB",
580 "BBBBBBBBBBBB",
581 "BBBB ...",
582 "BBBB ++++@@@",
583 "BBBB +... ",
584 "BBBB +.@@++ ",
585 "BBBB +.@ .+ ",
586 "BBBB.@ +. @.",
587 "BBBB.@ ++@@.",
588 "BBBB.@ ...",
589 "BBBB.@@@@+++",
590 "BBBB..... "
591 };
592 static const char * xpm_gt_rr_bb[] = {
593 "12 12 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
594 "BBBBBBBBBBBB",
595 "BBBBBBBBBBBB",
596 "BBBBBBBBBBBB",
597 "BBBBBBBBBBBB",
598 "BBBB ...",
599 "BBBB ++++@@@",
600 "BBBB +... ",
601 "BBBB +.@@++ ",
602 "BBBB +.@ .+ ",
603 "BBBB.@ +. @.",
604 "BBBB.@ ++@@.",
605 "BBBB.@ ..."
606 };
607 static const char * xpm_sm_ll_tt[] = {
608 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
609 " .+ @.BB",
610 ". @.+ BB",
611 "+@@.+ BB",
612 " ...+ BB",
613 "@++++ BB",
614 ". BB",
615 "BBBBBBBB",
616 "BBBBBBBB"
617 };
618 static const char * xpm_sm_ll_t[] = {
619 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
620 ". @.BB",
621 "@++ @.BB",
622 " .+ @.BB",
623 ". @.+ BB",
624 "+@@.+ BB",
625 " ...+ BB",
626 "@++++ BB",
627 ". BB"
628 };
629 static const char * xpm_sm_ll[] = {
630 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
631 " .....BB",
632 "+@@@@.BB",
633 ". @.BB",
634 "@++ @.BB",
635 " .+ @.BB",
636 ". @.+ BB",
637 "+@@.+ BB",
638 " ...+ BB"
639 };
640 static const char * xpm_sm_ll_b[] = {
641 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
642 "BBBBBBBB",
643 "BBBBBBBB",
644 " .....BB",
645 "+@@@@.BB",
646 ". @.BB",
647 "@++ @.BB",
648 " .+ @.BB",
649 ". @.+ BB"
650 };
651 static const char * xpm_sm_l_tt[] = {
652 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
653 ".@ .+ @.",
654 " +. @.+ ",
655 " ++@@.+ ",
656 " ...+ ",
657 "@@@++++ ",
658 "... ",
659 "BBBBBBBB",
660 "BBBBBBBB"
661 };
662 static const char * xpm_sm_l_t[] = {
663 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
664 "... @.",
665 ".@@++ @.",
666 ".@ .+ @.",
667 " +. @.+ ",
668 " ++@@.+ ",
669 " ...+ ",
670 "@@@++++ ",
671 "... "
672 };
673 static const char * xpm_sm_l[] = {
674 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
675 " .....",
676 "+++@@@@.",
677 "... @.",
678 ".@@++ @.",
679 ".@ .+ @.",
680 " +. @.+ ",
681 " ++@@.+ ",
682 " ...+ "
683 };
684 static const char * xpm_sm_l_b[] = {
685 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
686 "BBBBBBBB",
687 "BBBBBBBB",
688 " .....",
689 "+++@@@@.",
690 "... @.",
691 ".@@++ @.",
692 ".@ .+ @.",
693 " +. @.+ "
694 };
695 static const char * xpm_sm_tt[] = {
696 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
697 " +.@ .+ ",
698 ".@ +. @.",
699 ".@ ++@@.",
700 ".@ ...",
701 ".@@@@+++",
702 "..... ",
703 "BBBBBBBB",
704 "BBBBBBBB"
705 };
706 static const char * xpm_sm_t[] = {
707 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
708 " +... ",
709 " +.@@++ ",
710 " +.@ .+ ",
711 ".@ +. @.",
712 ".@ ++@@.",
713 ".@ ...",
714 ".@@@@+++",
715 "..... "
716 };
717 static const char * xpm_sm_b[] = {
718 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
719 "BBBBBBBB",
720 "BBBBBBBB",
721 " ...",
722 " ++++@@@",
723 " +... ",
724 " +.@@++ ",
725 " +.@ .+ ",
726 ".@ +. @."
727 };
728 static const char * xpm_sm_r_tt[] = {
729 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
730 "BB +.@ .",
731 "BB.@ +. ",
732 "BB.@ ++@",
733 "BB.@ .",
734 "BB.@@@@+",
735 "BB..... ",
736 "BBBBBBBB",
737 "BBBBBBBB"
738 };
739 static const char * xpm_sm_r_t[] = {
740 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
741 "BB +... ",
742 "BB +.@@+",
743 "BB +.@ .",
744 "BB.@ +. ",
745 "BB.@ ++@",
746 "BB.@ .",
747 "BB.@@@@+",
748 "BB..... "
749 };
750 static const char * xpm_sm_r[] = {
751 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
752 "BB .",
753 "BB ++++@",
754 "BB +... ",
755 "BB +.@@+",
756 "BB +.@ .",
757 "BB.@ +. ",
758 "BB.@ ++@",
759 "BB.@ ."
760 };
761 static const char * xpm_sm_r_b[] = {
762 "8 8 5 1", "B c Black", " c #00ff00", ". c #0000ff", "+ c #7f7f7f", "@ c #FF0000",
763 "BBBBBBBB",
764 "BBBBBBBB",
765 "BB .",
766 "BB ++++@",
767 "BB +... ",
768 "BB +.@@+",
769 "BB +.@ .",
770 "BB.@ +. "
771 };
772
773 // this table defines all tests
774 struct SizeTestData
775 {
776 int w, h, dx, dy; // first parameters for Size()
777 const char **ref_xpm; // expected result
778 } sizeTestData[] =
779 {
780 { 10, 10, 0, 0, xpm_orig}, // same size, same position
781 { 12, 12, 0, 0, xpm_gt}, // target larger, same position
782 { 8, 8, 0, 0, xpm_sm}, // target smaller, same position
783 { 10, 10, -2, -2, xpm_l_t}, // same size, move left up
784 { 10, 10, -2, 0, xpm_l}, // same size, move left
785 { 10, 10, -2, 2, xpm_l_b}, // same size, move left down
786 { 10, 10, 0, -2, xpm_t}, // same size, move up
787 { 10, 10, 0, 2, xpm_b}, // same size, move down
788 { 10, 10, 2, -2, xpm_r_t}, // same size, move right up
789 { 10, 10, 2, 0, xpm_r}, // same size, move right
790 { 10, 10, 2, 2, xpm_r_b}, // same size, move right down
791 { 12, 12, -2, -2, xpm_gt_l_t}, // target larger, move left up
792 { 12, 12, -2, 0, xpm_gt_l}, // target larger, move left
793 { 12, 12, -2, 2, xpm_gt_l_b}, // target larger, move left down
794 { 12, 12, -2, 4, xpm_gt_l_bb}, // target larger, move left down
795 { 12, 12, 0, -2, xpm_gt_t}, // target larger, move up
796 { 12, 12, 0, 2, xpm_gt_b}, // target larger, move down
797 { 12, 12, 0, 4, xpm_gt_bb}, // target larger, move down
798 { 12, 12, 2, -2, xpm_gt_r_t}, // target larger, move right up
799 { 12, 12, 2, 0, xpm_gt_r}, // target larger, move right
800 { 12, 12, 2, 2, xpm_gt_r_b}, // target larger, move right down
801 { 12, 12, 2, 4, xpm_gt_r_bb}, // target larger, move right down
802 { 12, 12, 4, -2, xpm_gt_rr_t}, // target larger, move right up
803 { 12, 12, 4, 0, xpm_gt_rr}, // target larger, move right
804 { 12, 12, 4, 2, xpm_gt_rr_b}, // target larger, move right down
805 { 12, 12, 4, 4, xpm_gt_rr_bb}, // target larger, move right down
806 { 8, 8, -4, -4, xpm_sm_ll_tt}, // target smaller, move left up
807 { 8, 8, -4, -2, xpm_sm_ll_t}, // target smaller, move left up
808 { 8, 8, -4, 0, xpm_sm_ll}, // target smaller, move left
809 { 8, 8, -4, 2, xpm_sm_ll_b}, // target smaller, move left down
810 { 8, 8, -2, -4, xpm_sm_l_tt}, // target smaller, move left up
811 { 8, 8, -2, -2, xpm_sm_l_t}, // target smaller, move left up
812 { 8, 8, -2, 0, xpm_sm_l}, // target smaller, move left
813 { 8, 8, -2, 2, xpm_sm_l_b}, // target smaller, move left down
814 { 8, 8, 0, -4, xpm_sm_tt}, // target smaller, move up
815 { 8, 8, 0, -2, xpm_sm_t}, // target smaller, move up
816 { 8, 8, 0, 2, xpm_sm_b}, // target smaller, move down
817 { 8, 8, 2, -4, xpm_sm_r_tt}, // target smaller, move right up
818 { 8, 8, 2, -2, xpm_sm_r_t}, // target smaller, move right up
819 { 8, 8, 2, 0, xpm_sm_r}, // target smaller, move right
820 { 8, 8, 2, 2, xpm_sm_r_b}, // target smaller, move right down
821 };
822
823 const wxImage src_img(xpm_orig);
824 for ( unsigned i = 0; i < WXSIZEOF(sizeTestData); i++ )
825 {
826 SizeTestData& st = sizeTestData[i];
827 wxImage
828 actual(src_img.Size(wxSize(st.w, st.h), wxPoint(st.dx, st.dy), 0, 0, 0)),
829 expected(st.ref_xpm);
830
831 // to check results with an image viewer uncomment this:
832 //actual.SaveFile(wxString::Format("imagetest-%02d-actual.png", i), wxBITMAP_TYPE_PNG);
833 //expected.SaveFile(wxString::Format("imagetest-%02d-exp.png", i), wxBITMAP_TYPE_PNG);
834
835 CPPUNIT_ASSERT_EQUAL( actual.GetSize().x, expected.GetSize().x );
836 CPPUNIT_ASSERT_EQUAL( actual.GetSize().y, expected.GetSize().y );
837
a25b5bbf 838 WX_ASSERT_EQUAL_MESSAGE
2e51fb30
VZ
839 (
840 ("Resize test #%u: (%d, %d), (%d, %d)", i, st.w, st.h, st.dx, st.dy),
a25b5bbf 841 expected, actual
2e51fb30
VZ
842 );
843 }
844}
845
2b39567f
DS
846void ImageTestCase::CompareLoadedImage()
847{
848 wxImage expected8("horse.xpm");
849 CPPUNIT_ASSERT( expected8.IsOk() );
850
851 wxImage expected24("horse.png");
852 CPPUNIT_ASSERT( expected24.IsOk() );
853
2b39567f
DS
854 for (size_t i=0; i<WXSIZEOF(g_testfiles); i++)
855 {
856 if ( !(g_testfiles[i].bitDepth == 8 || g_testfiles[i].bitDepth == 24)
857 || g_testfiles[i].type == wxBITMAP_TYPE_JPEG /*skip lossy JPEG*/)
858 {
859 continue;
860 }
861
862 wxImage actual(g_testfiles[i].file);
863
864 if ( actual.GetSize() != expected8.GetSize() )
865 {
866 continue;
867 }
868
869
a25b5bbf 870 WX_ASSERT_EQUAL_MESSAGE
2b39567f
DS
871 (
872 ("Compare test '%s' for loading failed", g_testfiles[i].file),
a25b5bbf
VZ
873 g_testfiles[i].bitDepth == 8 ? expected8 : expected24,
874 actual
2b39567f
DS
875 );
876 }
877
878}
879
8ee313d2
DS
880enum
881{
882 wxIMAGE_HAVE_ALPHA = (1 << 0),
883 wxIMAGE_HAVE_PALETTE = (1 << 1)
884};
885
5828d76a 886static
8ee313d2
DS
887void CompareImage(const wxImageHandler& handler, const wxImage& image,
888 int properties = 0, const wxImage *compareTo = NULL)
2b39567f 889{
3d926ff8 890 wxBitmapType type = handler.GetType();
8ee313d2
DS
891
892 const bool testPalette = (properties & wxIMAGE_HAVE_PALETTE) != 0;
893 /*
894 This is getting messy and should probably be transformed into a table
895 with image format features before it gets hairier.
896 */
897 if ( testPalette
898 && ( !(type == wxBITMAP_TYPE_BMP
899 || type == wxBITMAP_TYPE_GIF
2a6545a5 900 || type == wxBITMAP_TYPE_ICO
8ee313d2
DS
901 || type == wxBITMAP_TYPE_PNG)
902 || type == wxBITMAP_TYPE_XPM) )
903 {
904 return;
905 }
906
907 const bool testAlpha = (properties & wxIMAGE_HAVE_ALPHA) != 0;
3d926ff8
DS
908 if (testAlpha
909 && !(type == wxBITMAP_TYPE_PNG || type == wxBITMAP_TYPE_TGA) )
5828d76a
DS
910 {
911 // don't test images with alpha if this handler doesn't support alpha
912 return;
913 }
2b39567f 914
7ffc9aff 915 if (type == wxBITMAP_TYPE_JPEG /* skip lossy JPEG */)
5828d76a 916 {
5828d76a
DS
917 return;
918 }
2b39567f 919
5828d76a 920 wxMemoryOutputStream memOut;
8ee313d2 921 if ( !image.SaveFile(memOut, type) )
2b39567f 922 {
5828d76a
DS
923 // Unfortunately we can't know if the handler just doesn't support
924 // saving images, or if it failed to save.
925 return;
926 }
2b39567f 927
5828d76a
DS
928 wxMemoryInputStream memIn(memOut);
929 CPPUNIT_ASSERT(memIn.IsOk());
2b39567f 930
5828d76a
DS
931 wxImage actual(memIn);
932 CPPUNIT_ASSERT(actual.IsOk());
2b39567f 933
8ee313d2
DS
934 const wxImage *expected = compareTo ? compareTo : &image;
935 CPPUNIT_ASSERT( actual.GetSize() == expected->GetSize() );
2b39567f 936
8ee313d2 937 unsigned bitsPerPixel = testPalette ? 8 : (testAlpha ? 32 : 24);
a25b5bbf 938 WX_ASSERT_EQUAL_MESSAGE
5828d76a 939 (
8ee313d2
DS
940 ("Compare test '%s (%d-bit)' for saving failed",
941 handler.GetExtension(), bitsPerPixel),
a25b5bbf
VZ
942 *expected,
943 actual
5828d76a 944 );
2b39567f 945
8ee313d2
DS
946#if wxUSE_PALETTE
947 CPPUNIT_ASSERT(actual.HasPalette()
948 == (testPalette || type == wxBITMAP_TYPE_XPM));
949#endif
950
951 CPPUNIT_ASSERT( actual.HasAlpha() == testAlpha);
952
5828d76a
DS
953 if (!testAlpha)
954 {
955 return;
956 }
957
a25b5bbf 958 WX_ASSERT_EQUAL_MESSAGE
5828d76a
DS
959 (
960 ("Compare alpha test '%s' for saving failed", handler.GetExtension()),
a25b5bbf
VZ
961 *expected,
962 actual
5828d76a
DS
963 );
964}
965
966void ImageTestCase::CompareSavedImage()
967{
2960bae8
VZ
968 // FIXME-VC6: Pre-declare the loop variables for compatibility with
969 // pre-standard compilers such as MSVC6 that don't implement proper scope
970 // for the variables declared in the for loops.
971 int i, x, y;
972
5828d76a
DS
973 wxImage expected24("horse.png");
974 CPPUNIT_ASSERT( expected24.IsOk() );
975 CPPUNIT_ASSERT( !expected24.HasAlpha() );
976
8ee313d2 977 wxImage expected8 = expected24.ConvertToGreyscale();
8ee313d2 978
5d875c11 979#if wxUSE_PALETTE
8ee313d2 980 unsigned char greys[256];
2960bae8 981 for (i = 0; i < 256; ++i)
8ee313d2
DS
982 {
983 greys[i] = i;
984 }
985 wxPalette palette(256, greys, greys, greys);
986 expected8.SetPalette(palette);
5d875c11
DS
987#endif // #if wxUSE_PALETTE
988
8ee313d2
DS
989 expected8.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP_PALETTE);
990
5828d76a
DS
991 // Create an image with alpha based on the loaded image
992 wxImage expected32(expected24);
993 expected32.SetAlpha();
994
995 int width = expected32.GetWidth();
996 int height = expected32.GetHeight();
2960bae8 997 for (y = 0; y < height; ++y)
5828d76a 998 {
2960bae8 999 for (x = 0; x < width; ++x)
5828d76a
DS
1000 {
1001 expected32.SetAlpha(x, y, (x*y) & wxIMAGE_ALPHA_OPAQUE);
1002 }
1003 }
1004
1005 const wxList& list = wxImage::GetHandlers();
1006 for ( wxList::compatibility_iterator node = list.GetFirst();
1007 node; node = node->GetNext() )
1008 {
1009 wxImageHandler *handler = (wxImageHandler *) node->GetData();
1010
8ee313d2
DS
1011#if wxUSE_PALETTE
1012 CompareImage(*handler, expected8, wxIMAGE_HAVE_PALETTE);
1013#endif
5828d76a 1014 CompareImage(*handler, expected24);
8ee313d2
DS
1015 CompareImage(*handler, expected32, wxIMAGE_HAVE_ALPHA);
1016 }
8529b0b9 1017}
8ee313d2 1018
8529b0b9
DS
1019void ImageTestCase::SavePNG()
1020{
1021 wxImage expected24("horse.png");
1022 CPPUNIT_ASSERT( expected24.IsOk() );
1023#if wxUSE_PALETTE
1024 CPPUNIT_ASSERT( !expected24.HasPalette() );
1025#endif // #if wxUSE_PALETTE
8ee313d2 1026
8529b0b9
DS
1027 wxImage expected8 = expected24.ConvertToGreyscale();
1028
1029 /*
1030 horse.png converted to greyscale should be saved without a palette.
1031 */
1032 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG), expected8);
1033
1034 /*
1035 But if we explicitly ask for trying to save with a palette, it should work.
1036 */
1037 expected8.SetOption(wxIMAGE_OPTION_PNG_FORMAT, wxPNG_TYPE_PALETTE);
1038
1039 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1040 expected8, wxIMAGE_HAVE_PALETTE);
1041
1042
1043 CPPUNIT_ASSERT( expected8.LoadFile("horse.gif") );
5d875c11 1044#if wxUSE_PALETTE
8ee313d2 1045 CPPUNIT_ASSERT( expected8.HasPalette() );
5d875c11 1046#endif // #if wxUSE_PALETTE
8ee313d2 1047
8529b0b9
DS
1048 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1049 expected8, wxIMAGE_HAVE_PALETTE);
1050
1051 /*
1052 Add alpha to the image in such a way that there will still be a maximum
1053 of 256 unique RGBA combinations. This should result in a saved
1054 PNG image still being palettised and having alpha.
1055 */
8ee313d2
DS
1056 expected8.SetAlpha();
1057
8529b0b9
DS
1058 int x, y;
1059 const int width = expected8.GetWidth();
1060 const int height = expected8.GetHeight();
2960bae8 1061 for (y = 0; y < height; ++y)
8ee313d2 1062 {
2960bae8 1063 for (x = 0; x < width; ++x)
8ee313d2 1064 {
8529b0b9 1065 expected8.SetAlpha(x, y, expected8.GetRed(x, y));
8ee313d2 1066 }
2b39567f 1067 }
8ee313d2 1068
8ee313d2 1069 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
8529b0b9 1070 expected8, wxIMAGE_HAVE_ALPHA|wxIMAGE_HAVE_PALETTE);
8ee313d2 1071
8ee313d2 1072 /*
8529b0b9
DS
1073 Now change the alpha of the first pixel so that we can't save palettised
1074 anymore because there will be 256+1 entries which is beyond PNGs limit
1075 of 256 entries.
8ee313d2 1076 */
8529b0b9 1077 expected8.SetAlpha(0, 0, 1);
8ee313d2 1078
8529b0b9
DS
1079 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
1080 expected8, wxIMAGE_HAVE_ALPHA);
8ee313d2
DS
1081
1082 /*
8529b0b9 1083 Even if we explicitly ask for saving palettised it should not be done.
8ee313d2 1084 */
8529b0b9 1085 expected8.SetOption(wxIMAGE_OPTION_PNG_FORMAT, wxPNG_TYPE_PALETTE);
8ee313d2 1086 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_PNG),
8529b0b9
DS
1087 expected8, wxIMAGE_HAVE_ALPHA);
1088
2b39567f
DS
1089}
1090
77b83d0a
DS
1091void ImageTestCase::SaveAnimatedGIF()
1092{
1093#if wxUSE_PALETTE
1094 wxImage image("horse.gif");
1095 CPPUNIT_ASSERT( image.IsOk() );
1096
1097 wxImageArray images;
1098 images.Add(image);
1099 int i;
1100 for (i = 0; i < 4-1; ++i)
1101 {
1102 images.Add( images[i].Rotate90() );
1103
1104 images[i+1].SetPalette(images[0].GetPalette());
1105 }
1106
1107 wxMemoryOutputStream memOut;
1108 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images, &memOut) );
1109
1110 wxGIFHandler handler;
1111 wxMemoryInputStream memIn(memOut);
1112 CPPUNIT_ASSERT(memIn.IsOk());
1113 const int imageCount = handler.GetImageCount(memIn);
1114 CPPUNIT_ASSERT_EQUAL(4, imageCount);
1115
1116 for (i = 0; i < imageCount; ++i)
1117 {
1118 wxFileOffset pos = memIn.TellI();
1119 CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true, i) );
1120 memIn.SeekI(pos);
1121
a25b5bbf 1122 WX_ASSERT_EQUAL_MESSAGE
77b83d0a
DS
1123 (
1124 ("Compare test for GIF frame number %d failed", i),
a25b5bbf
VZ
1125 images[i],
1126 image
77b83d0a
DS
1127 );
1128 }
1129#endif // #if wxUSE_PALETTE
1130}
1131
fdfedfc0
DS
1132void ImageTestCase::ReadCorruptedTGA()
1133{
1134 static unsigned char corruptTGA[18+1+3] =
1135 {
1136 0,
1137 0,
1138 10, // RLE compressed image.
1139 0, 0,
1140 0, 0,
1141 0,
1142 0, 0,
1143 0, 0,
1144 1, 0, // Width is 1.
1145 1, 0, // Height is 1.
1146 24, // Bits per pixel.
1147 0,
1148
1149 0xff, // Run length (repeat next pixel 127+1 times).
1150 0xff, 0xff, 0xff // One 24-bit pixel.
1151 };
1152
1153 wxMemoryInputStream memIn(corruptTGA, WXSIZEOF(corruptTGA));
1154 CPPUNIT_ASSERT(memIn.IsOk());
1155
1156 wxImage tgaImage;
1157 CPPUNIT_ASSERT( !tgaImage.LoadFile(memIn) );
1158
1159
1160 /*
1161 Instead of repeating a pixel 127+1 times, now tell it there will
1162 follow 127+1 uncompressed pixels (while we only should have 1 in total).
1163 */
1164 corruptTGA[18] = 0x7f;
1165 CPPUNIT_ASSERT( !tgaImage.LoadFile(memIn) );
1166}
1167
15f345aa
DS
1168static void TestGIFComment(const wxString& comment)
1169{
1170 wxImage image("horse.gif");
1171
1172 image.SetOption(wxIMAGE_OPTION_GIF_COMMENT, comment);
1173 wxMemoryOutputStream memOut;
1174 CPPUNIT_ASSERT(image.SaveFile(memOut, wxBITMAP_TYPE_GIF));
1175
1176 wxMemoryInputStream memIn(memOut);
1177 CPPUNIT_ASSERT( image.LoadFile(memIn) );
1178
1179 CPPUNIT_ASSERT_EQUAL(comment,
1180 image.GetOption(wxIMAGE_OPTION_GIF_COMMENT));
1181}
1182
1183void ImageTestCase::GIFComment()
1184{
1185 // Test reading a comment.
1186 wxImage image("horse.gif");
1187 CPPUNIT_ASSERT_EQUAL(" Imported from GRADATION image: gray",
1188 image.GetOption(wxIMAGE_OPTION_GIF_COMMENT));
1189
1190
1191 // Test writing a comment and reading it back.
1192 TestGIFComment("Giving the GIF a gifted giraffe as a gift");
1193
1194
1195 // Test writing and reading a comment again but with a long comment.
1196 TestGIFComment(wxString(wxT('a'), 256)
1197 + wxString(wxT('b'), 256)
1198 + wxString(wxT('c'), 256));
1199
1200
1201 // Test writing comments in an animated GIF and reading them back.
1202 CPPUNIT_ASSERT( image.LoadFile("horse.gif") );
1203
1204 wxImageArray images;
1205 int i;
1206 for (i = 0; i < 4; ++i)
1207 {
1208 if (i)
1209 {
1210 images.Add( images[i-1].Rotate90() );
1211 images[i].SetPalette(images[0].GetPalette());
1212 }
1213 else
1214 {
1215 images.Add(image);
1216 }
1217
1218 images[i].SetOption(wxIMAGE_OPTION_GIF_COMMENT,
1219 wxString::Format("GIF comment for frame #%d", i+1));
1220
1221 }
1222
1223
1224 wxMemoryOutputStream memOut;
1225 CPPUNIT_ASSERT( wxGIFHandler().SaveAnimation(images, &memOut) );
1226
1227 wxGIFHandler handler;
1228 wxMemoryInputStream memIn(memOut);
1229 CPPUNIT_ASSERT(memIn.IsOk());
1230 const int imageCount = handler.GetImageCount(memIn);
1231 for (i = 0; i < imageCount; ++i)
1232 {
1233 wxFileOffset pos = memIn.TellI();
1234 CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true /*verbose?*/, i) );
1235
1236 CPPUNIT_ASSERT_EQUAL(
1237 wxString::Format("GIF comment for frame #%d", i+1),
1238 image.GetOption(wxIMAGE_OPTION_GIF_COMMENT));
1239 memIn.SeekI(pos);
1240 }
1241}
1242
2a6545a5
DS
1243void ImageTestCase::DibPadding()
1244{
1245 /*
1246 There used to be an error with calculating the DWORD aligned scan line
1247 pitch for a BMP/ICO resulting in buffer overwrites (with at least MSVC9
1248 Debug this gave a heap corruption assertion when saving the mask of
1249 an ICO). Test for it here.
1250 */
1251 wxImage image("horse.gif");
1252 CPPUNIT_ASSERT( image.IsOk() );
1253
1254 image = image.Scale(99, 99);
1255
a5a0ba3d
DS
1256 wxMemoryOutputStream memOut;
1257 CPPUNIT_ASSERT( image.SaveFile(memOut, wxBITMAP_TYPE_ICO) );
2a6545a5
DS
1258}
1259
d06c3098
DS
1260static void CompareBMPImage(const wxString& file1, const wxString& file2)
1261{
1262 wxImage image1(file1);
1263 CPPUNIT_ASSERT( image1.IsOk() );
1264
1265 wxImage image2(file2);
1266 CPPUNIT_ASSERT( image2.IsOk() );
1267
1268 CompareImage(*wxImage::FindHandler(wxBITMAP_TYPE_BMP), image1, 0, &image2);
1269}
1270
1271void ImageTestCase::BMPFlippingAndRLECompression()
1272{
1273 CompareBMPImage("image/horse_grey.bmp", "image/horse_grey_flipped.bmp");
1274
1275 CompareBMPImage("image/horse_rle8.bmp", "image/horse_grey.bmp");
1276 CompareBMPImage("image/horse_rle8.bmp", "image/horse_rle8_flipped.bmp");
1277
1278 CompareBMPImage("image/horse_rle4.bmp", "image/horse_rle4_flipped.bmp");
1279}
232fdc63
VZ
1280#endif //wxUSE_IMAGE
1281
2e51fb30 1282
21fe35af
FM
1283/*
1284 TODO: add lots of more tests to wxImage functions
1285*/