TDME2  1.9.200
PNGTextureReader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
9 #include <tdme/engine/fwd-tdme.h>
11 
12 #include <ext/libpng/png.h>
13 
14 using std::map;
15 using std::string;
16 using std::vector;
17 
20 
21 /**
22  * PNG texture reader class
23  * @author Andreas Drewke
24  */
26 {
27  friend class tdme::engine::Texture;
28 
29 public:
30  /**
31  * Read PNG header from memory
32  * @param pngData png vector data to read PNG from
33  * @param width width
34  * @param height height
35  * @param bytes per pixel
36  * @return success
37  */
38  static bool readHeader(const vector<uint8_t>& pngData, int& width, int& height, uint8_t& bytesPerPixel);
39 
40  /**
41  * Read PNG from memory into texture byte buffer
42  * @param pngData png vector data to read PNG from
43  * @param textureByteBuffer texture byte buffer
44  * @return success
45  */
46  static bool read(const vector<uint8_t>& pngData, ByteBuffer& textureByteBuffer);
47 
48  /**
49  * Read PNG from memory
50  * @param textureId texture id
51  * @param pngData png vector data to read PNG from
52  * @param powerOfTwo scale image to fit power of two dimensions
53  * @param idPrefix id prefix
54  */
55  static Texture* read(const string& textureId, const vector<uint8_t>& pngData, bool powerOfTwo = true, const string& idPrefix = string());
56 
57 private:
58  /**
59  * PNG input stream
60  */
62  public:
63 
64  /**
65  * Public constructor
66  * @param data data
67  */
68  PNGInputStream(const vector<uint8_t>* data): offset(0), data(data) {
69  }
70 
71  /**
72  * Destructor
73  */
75  }
76 
77  /**
78  * Read bytes
79  * @param outBytes out bytes
80  * @param outBytesToRead out bytes to read
81  */
82  void readBytes(int8_t* outBytes, int32_t outBytesToRead) {
83  for (int32_t i = 0; i < outBytesToRead && offset < data->size(); i++) {
84  outBytes[i] = (*data)[offset++];
85  }
86  }
87 
88  private:
89  int offset;
90  const vector<uint8_t>* data;
91 
92  };
93 
94  /**
95  * Read PNG data from memory
96  * @param png_ptr png structure
97  * @param outBytes out bytes
98  * @param outBytesToRead out bytes to read
99  */
100  static void readDataFromMemory(png_structp png_ptr, png_bytep outBytes, png_size_t outBytesToRead);
101 
102 };
Texture entity.
Definition: Texture.h:24
PNGInputStream(const vector< uint8_t > *data)
Public constructor.
void readBytes(int8_t *outBytes, int32_t outBytesToRead)
Read bytes.
static bool read(const vector< uint8_t > &pngData, ByteBuffer &textureByteBuffer)
Read PNG from memory into texture byte buffer.
static void readDataFromMemory(png_structp png_ptr, png_bytep outBytes, png_size_t outBytesToRead)
Read PNG data from memory.
static bool readHeader(const vector< uint8_t > &pngData, int &width, int &height, uint8_t &bytesPerPixel)
Read PNG header from memory.
Byte buffer class.
Definition: ByteBuffer.h:27