diff --git a/Utilities/PNGHelper.cpp b/Utilities/PNGHelper.cpp index bb03985f..97fce84f 100644 --- a/Utilities/PNGHelper.cpp +++ b/Utilities/PNGHelper.cpp @@ -3,20 +3,32 @@ #include "PNGHelper.h" #include "miniz.h" -bool PNGHelper::WritePNG(std::stringstream &stream, uint32_t* buffer, uint32_t xSize, uint32_t ySize, uint32_t bitsPerPixel) +bool PNGHelper::WritePNG(std::stringstream& stream, uint32_t* buffer, uint32_t xSize, uint32_t ySize, uint32_t bitsPerPixel) { size_t pngSize = 0; - //ARGB -> BGR - uint32_t size = xSize * ySize * bitsPerPixel / 8 / 4; - vector convertedData(size*3, 0); - for(uint32_t i = 0; i < size; i++) { - convertedData[i * 3] = (buffer[i] & 0xFF0000) >> 16; - convertedData[i * 3 + 1] = (buffer[i] & 0xFF00) >> 8; - convertedData[i * 3 + 2] = (buffer[i] & 0xFF); + uint32_t size = xSize * ySize * bitsPerPixel / 8; + vector convertedData(size, 0); + if(bitsPerPixel == 32) { + //ARGB -> ABGR + for(uint32_t i = 0; i < size / 4; i++) { + convertedData[i * 4] = (buffer[i] & 0xFF0000) >> 16; + convertedData[i * 4 + 1] = (buffer[i] & 0xFF00) >> 8; + convertedData[i * 4 + 2] = (buffer[i] & 0xFF); + convertedData[i * 4 + 3] = (buffer[i] & 0xFF000000) >> 24; + } + } else if(bitsPerPixel == 24) { + //ARGB -> BGR + for(uint32_t i = 0; i < size / 3; i++) { + convertedData[i * 3] = (buffer[i] & 0xFF0000) >> 16; + convertedData[i * 3 + 1] = (buffer[i] & 0xFF00) >> 8; + convertedData[i * 3 + 2] = (buffer[i] & 0xFF); + } + } else { + return false; } - void *pngData = tdefl_write_image_to_png_file_in_memory_ex(convertedData.data(), xSize, ySize, 3, &pngSize, MZ_DEFAULT_LEVEL, MZ_FALSE); + void* pngData = tdefl_write_image_to_png_file_in_memory_ex(convertedData.data(), xSize, ySize, bitsPerPixel / 8, &pngSize, MZ_DEFAULT_LEVEL, MZ_FALSE); if(!pngData) { std::cout << "tdefl_write_image_to_png_file_in_memory_ex() failed!" << std::endl; return false; diff --git a/Utilities/PNGHelper.h b/Utilities/PNGHelper.h index 531a7094..96e3455f 100644 --- a/Utilities/PNGHelper.h +++ b/Utilities/PNGHelper.h @@ -7,8 +7,8 @@ private: static int DecodePNG(vector& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32 = true); public: - static bool WritePNG(std::stringstream &stream, uint32_t* buffer, uint32_t xSize, uint32_t ySize, uint32_t bitsPerPixel = 32); - static bool WritePNG(string filename, uint32_t* buffer, uint32_t xSize, uint32_t ySize, uint32_t bitsPerPixel = 32); + static bool WritePNG(std::stringstream &stream, uint32_t* buffer, uint32_t xSize, uint32_t ySize, uint32_t bitsPerPixel = 24); + static bool WritePNG(string filename, uint32_t* buffer, uint32_t xSize, uint32_t ySize, uint32_t bitsPerPixel = 24); static bool ReadPNG(string filename, vector &pngData, uint32_t &pngWidth, uint32_t &pngHeight); static bool ReadPNG(vector input, vector &output, uint32_t &pngWidth, uint32_t &pngHeight); }; \ No newline at end of file