TDME2  1.9.200
PNGTextureWriter.cpp
Go to the documentation of this file.
2 
3 #include <new>
4 #include <string>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
8 #include <tdme/engine/Texture.h>
12 #include <tdme/utilities/Console.h>
13 
14 #include <ext/libpng/png.h>
15 
16 using std::nothrow;
17 using std::string;
18 using std::to_string;
19 using std::vector;
20 
22 
28 
29 
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;
33 
34  PNGOutputStream* pngOutputStream = static_cast<PNGOutputStream*>(io_ptr);
35  pngOutputStream->writeBytes((int8_t*)inBytes, inBytesToWrite);
36 }
37 
38 void PNGTextureWriter::flushPNGDataToMemory(png_structp png_ptr) {
39  // no op
40 }
41 
42 bool PNGTextureWriter::write(Texture* texture, const string& pathName, const string& fileName, bool removeAlphaChannel, bool flipY) {
43  vector<uint8_t> data;
44  if (write(texture, data, removeAlphaChannel, flipY) == false) return false;
45 
46  FileSystem::getInstance()->setContent(pathName, fileName, data);
47 
48  return true;
49 }
50 
51 bool PNGTextureWriter::write(Texture* texture, vector<uint8_t>& pngData, bool removeAlphaChannel, bool flipY) {
52  return
53  write(
54  texture->getTextureWidth(),
55  texture->getTextureHeight(),
56  texture->getRGBDepthBitsPerPixel() / 8,
57  texture->getRGBTextureData(),
58  pngData,
59  removeAlphaChannel,
60  flipY
61  );
62 }
63 
64 bool PNGTextureWriter::write(int width, int height, int bytesPerPixel, const ByteBuffer& textureByteBuffer, vector<uint8_t>& pngData, bool removeAlphaChannel, bool flipY) {
65  // see: https://gist.github.com/niw/5963798
66  auto png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
67  if (png == nullptr) {
68  return false;
69  }
70 
71  auto info = png_create_info_struct(png);
72  if (info == nullptr) {
73  return false;
74  }
75 
76  if (setjmp(png_jmpbuf(png))) {
77  return false;
78  }
79 
80  // create PNG input stream
81  PNGOutputStream pngOutputStream(&pngData);
82 
83  // set up custom read function
85 
86  // output is 8bit depth, RGBA format.
87  png_set_IHDR(
88  png,
89  info,
90  width,
91  height,
92  8,
93  removeAlphaChannel == false && bytesPerPixel == 4?PNG_COLOR_TYPE_RGBA:PNG_COLOR_TYPE_RGB,
94  PNG_INTERLACE_NONE,
95  PNG_COMPRESSION_TYPE_DEFAULT,
96  PNG_FILTER_TYPE_DEFAULT
97  );
98  png_write_info(png, info);
99 
100  // remove the alpha channel for PNG_COLOR_TYPE_RGB format
101  if (removeAlphaChannel == true && bytesPerPixel == 4) {
102  png_set_filler(png, 0, PNG_FILLER_AFTER);
103  }
104 
105  // write png
106  auto success = false;
107  auto rowPtrs = new(nothrow)png_bytep[height];
108  if (rowPtrs != nullptr) {
109  //
110  success = true;
111  // create pointers to each line beginning in texture byte buffer
112  auto pixelBuffer = (uint8_t*)textureByteBuffer.getBuffer();
113  //
114  if (flipY == true) {
115  for (auto y = 0; y < height; y++) rowPtrs[y] = pixelBuffer + width * bytesPerPixel * (height - 1 - y);
116  } else {
117  for (auto y = 0; y < height; y++) rowPtrs[y] = pixelBuffer + width * bytesPerPixel * y;
118  }
119  //
120  png_write_image(png, rowPtrs);
121  png_write_end(png, NULL);
122 
123  //
124  delete [] rowPtrs;
125  }
126 
127  //
128  png_destroy_write_struct(&png, &info);
129 
130  //
131  return success;
132 }
Texture entity.
Definition: Texture.h:24
ByteBuffer getRGBTextureData()
Definition: Texture.h:253
uint8_t getRGBDepthBitsPerPixel() const
Definition: Texture.h:186
uint16_t getTextureHeight() const
Definition: Texture.h:211
uint16_t getTextureWidth() const
Definition: Texture.h:218
void writeBytes(int8_t *inBytes, int32_t inBytesToRead)
Read byte.
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.
Definition: FileSystem.h:17
const uint8_t * getBuffer() const
Definition: Buffer.h:150
Byte buffer class.
Definition: ByteBuffer.h:27
Console class.
Definition: Console.h:29