HD Pack Builder: Fixed missing alpha channel in PNG files

This commit is contained in:
Sour 2020-04-26 11:55:54 -04:00
parent 069a98c50d
commit 55199beda7
2 changed files with 23 additions and 11 deletions

View file

@ -3,20 +3,32 @@
#include "PNGHelper.h" #include "PNGHelper.h"
#include "miniz.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; size_t pngSize = 0;
//ARGB -> BGR uint32_t size = xSize * ySize * bitsPerPixel / 8;
uint32_t size = xSize * ySize * bitsPerPixel / 8 / 4; vector<uint8_t> convertedData(size, 0);
vector<uint8_t> convertedData(size*3, 0); if(bitsPerPixel == 32) {
for(uint32_t i = 0; i < size; i++) { //ARGB -> ABGR
convertedData[i * 3] = (buffer[i] & 0xFF0000) >> 16; for(uint32_t i = 0; i < size / 4; i++) {
convertedData[i * 3 + 1] = (buffer[i] & 0xFF00) >> 8; convertedData[i * 4] = (buffer[i] & 0xFF0000) >> 16;
convertedData[i * 3 + 2] = (buffer[i] & 0xFF); 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) { if(!pngData) {
std::cout << "tdefl_write_image_to_png_file_in_memory_ex() failed!" << std::endl; std::cout << "tdefl_write_image_to_png_file_in_memory_ex() failed!" << std::endl;
return false; return false;

View file

@ -7,8 +7,8 @@ private:
static int DecodePNG(vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32 = true); static int DecodePNG(vector<unsigned char>& 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: public:
static bool WritePNG(std::stringstream &stream, 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 = 32); static bool WritePNG(string filename, uint32_t* buffer, uint32_t xSize, uint32_t ySize, uint32_t bitsPerPixel = 24);
static bool ReadPNG(string filename, vector<uint8_t> &pngData, uint32_t &pngWidth, uint32_t &pngHeight); static bool ReadPNG(string filename, vector<uint8_t> &pngData, uint32_t &pngWidth, uint32_t &pngHeight);
static bool ReadPNG(vector<uint8_t> input, vector<uint8_t> &output, uint32_t &pngWidth, uint32_t &pngHeight); static bool ReadPNG(vector<uint8_t> input, vector<uint8_t> &output, uint32_t &pngWidth, uint32_t &pngHeight);
}; };