TDME2 1.9.121
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PNGTextureWriter.cpp
Go to the documentation of this file.
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#include <string>
7#include <vector>
8
9#include <tdme/tdme.h>
15
16#include <ext/libpng/png.h>
17
18using std::string;
19using std::to_string;
20using std::vector;
21
23
29
30
31void PNGTextureWriter::writePNGDataToMemory(png_structp png_ptr, png_bytep inBytes, png_size_t inBytesToWrite) {
32 png_voidp io_ptr = png_get_io_ptr(png_ptr);
33 if (io_ptr == nullptr) return;
34
35 PNGOutputStream* pngOutputStream = static_cast<PNGOutputStream*>(io_ptr);
36 pngOutputStream->writeBytes((int8_t*)inBytes, inBytesToWrite);
37}
38
39void PNGTextureWriter::flushPNGDataToMemory(png_structp png_ptr) {
40 // no op
41}
42
43bool PNGTextureWriter::write(Texture* texture, const string& pathName, const string& fileName, bool removeAlphaChannel, bool flipY) {
44 vector<uint8_t> data;
45 if (write(texture, data, removeAlphaChannel, flipY) == false) return false;
46
47 FileSystem::getInstance()->setContent(pathName, fileName, data);
48
49 return true;
50}
51
52bool PNGTextureWriter::write(Texture* texture, vector<uint8_t>& pngData, bool removeAlphaChannel, bool flipY) {
53 // see: https://gist.github.com/niw/5963798
54 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
55 if (!png) {
56 return false;
57 }
58
59 png_infop info = png_create_info_struct(png);
60 if (!info) {
61 return false;
62 }
63
64 if (setjmp(png_jmpbuf(png))) {
65 return false;
66 }
67
68 // create PNG input stream
69 PNGOutputStream pngOutputStream(&pngData);
70
71 // set up custom read function
73
74 //
75 auto bytesPerPixel = texture->getDepth() / 8;
76 auto width = texture->getTextureWidth();
77 auto height = texture->getTextureHeight();
78 auto pixels = texture->getTextureData();
79
80 // output is 8bit depth, RGBA format.
81 png_set_IHDR(
82 png,
83 info,
84 width,
85 height,
86 8,
87 removeAlphaChannel == false && bytesPerPixel == 4?PNG_COLOR_TYPE_RGBA:PNG_COLOR_TYPE_RGB,
88 PNG_INTERLACE_NONE,
89 PNG_COMPRESSION_TYPE_DEFAULT,
90 PNG_FILTER_TYPE_DEFAULT
91 );
92 png_write_info(png, info);
93
94 // remove the alpha channel for PNG_COLOR_TYPE_RGB format
95 if (removeAlphaChannel == true) {
96 png_set_filler(png, 0, PNG_FILLER_AFTER);
97 }
98
99 //
100 png_bytep* row_pointers = new png_bytep[height];
101 if (flipY == true) {
102 for (auto y = 0; y < height; y++) row_pointers[y] = pixels->getBuffer() + width * bytesPerPixel * (height - 1 - y);
103 } else {
104 for (auto y = 0; y < height; y++) row_pointers[y] = pixels->getBuffer() + width * bytesPerPixel * y;
105 }
106
107 png_write_image(png, row_pointers);
108 png_write_end(png, NULL);
109
110 free (row_pointers);
111
112 png_destroy_write_struct(&png, &info);
113
114 return true;
115}
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:14
Byte buffer class.
Definition: ByteBuffer.h:24
Console class.
Definition: Console.h:26