ImageWrite.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <list>
  5. #include <string>
  6. #include <vector>
  7. #include "png.h"
  8. #include "Common/FileUtil.h"
  9. #include "VideoCommon/ImageWrite.h"
  10. bool SaveData(const std::string& filename, const char* data)
  11. {
  12. std::ofstream f;
  13. OpenFStream(f, filename, std::ios::binary);
  14. f << data;
  15. return true;
  16. }
  17. /*
  18. TextureToPng
  19. Inputs:
  20. data : This is an array of RGBA with 8 bits per channel. 4 bytes for each pixel.
  21. row_stride: Determines the amount of bytes per row of pixels.
  22. */
  23. bool TextureToPng(u8* data, int row_stride, const std::string& filename, int width, int height, bool saveAlpha)
  24. {
  25. bool success = false;
  26. if (!data)
  27. return false;
  28. char title[] = "Dolphin Screenshot";
  29. char title_key[] = "Title";
  30. png_structp png_ptr = nullptr;
  31. png_infop info_ptr = nullptr;
  32. // Open file for writing (binary mode)
  33. File::IOFile fp(filename, "wb");
  34. if (!fp.IsOpen())
  35. {
  36. PanicAlertT("Screenshot failed: Could not open file \"%s\" (error %d)", filename.c_str(), errno);
  37. goto finalise;
  38. }
  39. // Initialize write structure
  40. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  41. if (png_ptr == nullptr)
  42. {
  43. PanicAlert("Screenshot failed: Could not allocate write struct");
  44. goto finalise;
  45. }
  46. // Initialize info structure
  47. info_ptr = png_create_info_struct(png_ptr);
  48. if (info_ptr == nullptr)
  49. {
  50. PanicAlert("Screenshot failed: Could not allocate info struct");
  51. goto finalise;
  52. }
  53. // Setup Exception handling
  54. if (setjmp(png_jmpbuf(png_ptr)))
  55. {
  56. PanicAlert("Screenshot failed: Error during PNG creation");
  57. goto finalise;
  58. }
  59. png_init_io(png_ptr, fp.GetHandle());
  60. // Write header (8 bit color depth)
  61. png_set_IHDR(png_ptr, info_ptr, width, height,
  62. 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
  63. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  64. png_text title_text;
  65. title_text.compression = PNG_TEXT_COMPRESSION_NONE;
  66. title_text.key = title_key;
  67. title_text.text = title;
  68. png_set_text(png_ptr, info_ptr, &title_text, 1);
  69. png_write_info(png_ptr, info_ptr);
  70. // Write image data
  71. for (auto y = 0; y < height; ++y)
  72. {
  73. u8* row_ptr = (u8*)data + y * row_stride;
  74. u8* ptr = row_ptr;
  75. for (auto x = 0; x < row_stride / 4; ++x)
  76. {
  77. if (!saveAlpha)
  78. ptr[3] = 0xff;
  79. ptr += 4;
  80. }
  81. png_write_row(png_ptr, row_ptr);
  82. }
  83. // End write
  84. png_write_end(png_ptr, nullptr);
  85. success = true;
  86. finalise:
  87. if (info_ptr != nullptr) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  88. if (png_ptr != nullptr) png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
  89. return success;
  90. }