14 #include <ext/libpng/png.h>
30 void PNGTextureWriter::writePNGDataToMemory(png_structp png_ptr, png_bytep inBytes, png_size_t inBytesToWrite) {
31 png_voidp io_ptr = png_get_io_ptr(png_ptr);
32 if (io_ptr ==
nullptr)
return;
35 pngOutputStream->
writeBytes((int8_t*)inBytes, inBytesToWrite);
44 if (
write(texture, data, removeAlphaChannel, flipY) ==
false)
return false;
46 FileSystem::getInstance()->setContent(pathName, fileName, data);
64 bool PNGTextureWriter::write(
int width,
int height,
int bytesPerPixel,
const ByteBuffer& textureByteBuffer, vector<uint8_t>& pngData,
bool removeAlphaChannel,
bool flipY) {
66 auto png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
71 auto info = png_create_info_struct(png);
72 if (info ==
nullptr) {
76 if (setjmp(png_jmpbuf(png))) {
93 removeAlphaChannel ==
false && bytesPerPixel == 4?PNG_COLOR_TYPE_RGBA:PNG_COLOR_TYPE_RGB,
95 PNG_COMPRESSION_TYPE_DEFAULT,
96 PNG_FILTER_TYPE_DEFAULT
98 png_write_info(png, info);
101 if (removeAlphaChannel ==
true && bytesPerPixel == 4) {
102 png_set_filler(png, 0, PNG_FILLER_AFTER);
106 auto success =
false;
107 auto rowPtrs =
new(nothrow)png_bytep[height];
108 if (rowPtrs !=
nullptr) {
112 auto pixelBuffer = (uint8_t*)textureByteBuffer.
getBuffer();
115 for (
auto y = 0; y < height; y++) rowPtrs[y] = pixelBuffer + width * bytesPerPixel * (height - 1 - y);
117 for (
auto y = 0; y < height; y++) rowPtrs[y] = pixelBuffer + width * bytesPerPixel * y;
120 png_write_image(png, rowPtrs);
121 png_write_end(png, NULL);
128 png_destroy_write_struct(&png, &info);
ByteBuffer getRGBTextureData()
uint8_t getRGBDepthBitsPerPixel() const
uint16_t getTextureHeight() const
uint16_t getTextureWidth() const
void writeBytes(int8_t *inBytes, int32_t inBytesToRead)
Read byte.
PNG texture writer class.
static bool write(Texture *texture, const string &pathName, const string &fileName, bool removeAlphaChannel=true, bool flipY=true)
Writes a texture to PNG file.
static void writePNGDataToMemory(png_structp png_ptr, png_bytep inBytes, png_size_t inBytesToWrite)
Write PNG data to memory.
static void flushPNGDataToMemory(png_structp png_ptr)
Flush PNG data.
File system singleton class.
const uint8_t * getBuffer() const