image.cpp 40 KB

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