writeImageToFile.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #if defined(_MSC_VER)
  4. #define _CRT_SECURE_NO_WARNINGS 1
  5. #endif // _MSC_VER
  6. #include "testUtils.h"
  7. using namespace irr;
  8. using namespace core;
  9. using namespace scene;
  10. using namespace video;
  11. using namespace io;
  12. using namespace gui;
  13. //! Tests IVideoDriver::writeImageToFile() using IWriteFile
  14. bool writeImageToFile(void)
  15. {
  16. IrrlichtDevice *device = createDevice( EDT_BURNINGSVIDEO, dimension2d<u32>(160, 120), 32);
  17. if (!device)
  18. return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
  19. IVideoDriver* driver = device->getVideoDriver();
  20. ISceneManager * smgr = device->getSceneManager();
  21. // Draw a cube background so that we can check that the pixels' alpha is working.
  22. ISceneNode * cube = smgr->addCubeSceneNode(50.f, 0, -1, vector3df(0, 0, 60));
  23. cube->setMaterialTexture(0, driver->getTexture("../media/wall.bmp"));
  24. cube->setMaterialFlag(video::EMF_LIGHTING, false);
  25. (void)smgr->addCameraSceneNode();
  26. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, SColor(255,100,101,140));
  27. smgr->drawAll();
  28. // Test for benign handling of offscreen pixel values as well as onscreen ones.
  29. for(s32 x = -10; x < 170; ++x)
  30. {
  31. s32 y = 120 * x / 160;
  32. driver->drawPixel((u32)x, (u32)y, SColor(255, 255 * x / 640, 255 * (640 - x) / 640, 0));
  33. y = 120 - y;
  34. driver->drawPixel((u32)x, (u32)y, SColor(255 * x / 640, 0, 255, 255));
  35. }
  36. driver->endScene();
  37. bool result = false;
  38. IWriteFile * writtenFile = 0;
  39. IWriteFile * memoryFile = 0;
  40. const char * writtenFilename = 0;
  41. const u32 BUFFER_SIZE = 160 * 120 * 4;
  42. c8 * buffer = 0;
  43. const char * referenceFilename = 0;
  44. video::ECOLOR_FORMAT format;
  45. irr::video::IImage * screenshot = driver->createScreenShot(video::ECF_R8G8B8);
  46. if (!screenshot)
  47. {
  48. logTestString("Failed to take screenshot\n");
  49. assert_log(false);
  50. goto cleanup;
  51. }
  52. format = screenshot->getColorFormat();
  53. if (format != video::ECF_R8G8B8)
  54. {
  55. irr::video::IImage * fixedScreenshot = driver->createImage(video::ECF_R8G8B8, screenshot->getDimension());
  56. screenshot->copyTo(fixedScreenshot);
  57. screenshot->drop();
  58. screenshot = 0;
  59. if (!fixedScreenshot)
  60. {
  61. logTestString("Failed to convert screenshot to ECF_A8R8G8B8\n");
  62. assert_log(false);
  63. goto cleanup;
  64. }
  65. screenshot = fixedScreenshot;
  66. }
  67. buffer = new c8[BUFFER_SIZE];
  68. writtenFilename = "results/Burning's Video-writeImageToFile.png";
  69. memoryFile = device->getFileSystem()->createMemoryWriteFile(buffer, BUFFER_SIZE, writtenFilename, false);
  70. if (!driver->writeImageToFile(screenshot, memoryFile))
  71. {
  72. logTestString("Failed to write png to memory file\n");
  73. assert_log(false);
  74. goto cleanup;
  75. }
  76. writtenFile = device->getFileSystem()->createAndWriteFile(memoryFile->getFileName());
  77. if (!writtenFile)
  78. {
  79. logTestString("Can't open %s for writing.\n", writtenFilename);
  80. assert_log(false);
  81. goto cleanup;
  82. }
  83. if ((size_t)memoryFile->getPos() != writtenFile->write(buffer, (size_t)memoryFile->getPos()))
  84. {
  85. logTestString("Error while writing to %s.\n", writtenFilename);
  86. assert_log(false);
  87. goto cleanup;
  88. }
  89. writtenFile->drop();
  90. writtenFile = 0;
  91. referenceFilename = "media/Burning's Video-drawPixel.png";
  92. if ( fuzzyCompareImages(driver,writtenFilename, referenceFilename) < 99.9)
  93. {
  94. logTestString("File written from memory is not the same as the reference file. %s:%d\n" , __FILE__, __LINE__);
  95. // assert_log(false);
  96. goto cleanup;
  97. }
  98. result = true;
  99. cleanup:
  100. if ( screenshot )
  101. screenshot->drop();
  102. if(writtenFile)
  103. writtenFile->drop();
  104. if(memoryFile)
  105. memoryFile->drop();
  106. delete [] buffer;
  107. device->drop();
  108. return result;
  109. }