Encoding/PNG.h

Sat, 08 Mar 2014 13:55:46 +0200

author
larukedi
date
Sat, 08 Mar 2014 13:55:46 +0200
changeset 401
85c7261e590a
parent 348
f97c151d5566
child 818
f8e100b28170
permissions
-rw-r--r--

added LICENSE and README.md files

215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
1 #pragma once
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
2
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
3 #include <vector>
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
4 #include "../Utils/UtilsBase.h"
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
5 #include <fstream>
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
6 #include <functional>
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
7
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
8 extern "C" {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
9 struct png_struct_def;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
10 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
11
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
12 namespace gge { namespace encoding {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
13
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
14
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
15 namespace png {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
16
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
17 //Streamer bases
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
18 class Reader {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
19 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
20 void (*Read)(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
21 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
22
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
23 class Writer {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
24 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
25 void (*Write)(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
26 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
27
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
28 class Buffer {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
29 public:
348
f97c151d5566 * Fixed GCC warnings, including several possible leaks
cemkalyoncu
parents: 318
diff changeset
30 virtual ~Buffer() {}
f97c151d5566 * Fixed GCC warnings, including several possible leaks
cemkalyoncu
parents: 318
diff changeset
31
215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
32 virtual unsigned char *Offset(int offset)=0;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
33 virtual void Resize(int size)=0;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
34 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
35
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
36
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
37 //Vector streamers
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
38 class VectorReader;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
39 void ReadVector(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
40
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
41 class VectorReader : public Reader {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
42 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
43 VectorReader(const std::vector<Byte> &Buf) : Buf(Buf), BufPos(0) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
44 Read=&ReadVector;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
45 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
46
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
47 const std::vector<Byte> &Buf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
48 unsigned BufPos;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
49 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
50 inline Reader *ReadyReadStruct(const std::vector<Byte> &vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
51 return new VectorReader(vec);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
52 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
53 inline unsigned long long GetReadSize(const std::vector<Byte> &vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
54 return vec.size();
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
55 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
56 inline void VectorSeek(Reader *r, long long addr) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
57 VectorReader *reader=(VectorReader *)r;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
58 reader->BufPos+=(unsigned)addr;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
59 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
60 inline std::function<void(Reader*, long long)> SeekFn(const std::vector<Byte> &vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
61 return &VectorSeek;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
62 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
63
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
64 class VectorWriter;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
65 void WriteVector(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
66 class VectorWriter : public Writer {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
67 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
68 VectorWriter(std::vector<Byte> &Buf) : Buf(Buf) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
69 Write=&WriteVector;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
70 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
71
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
72 std::vector<Byte> &Buf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
73 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
74 inline Writer *ReadyWriteStruct(std::vector<Byte> &vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
75 return new VectorWriter(vec);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
76 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
77
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
78 class VectorBuffer : public Buffer {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
79 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
80 VectorBuffer(std::vector<Byte> &vector) : vector(vector) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
81
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
82 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
83
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
84 virtual unsigned char *Offset(int offset) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
85 return &vector[offset];
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
86 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
87 virtual void Resize(int size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
88 vector.resize(size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
89 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
90
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
91 protected:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
92 std::vector<Byte> &vector;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
93 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
94
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
95 inline Buffer *CreateBuffer(std::vector<Byte> &vector) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
96 return new VectorBuffer(vector);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
97 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
98
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
99
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
100 //Array streamers
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
101 class ArrayReader;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
102 void ReadArray(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
103
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
104 class ArrayReader : public Reader {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
105 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
106 ArrayReader(const Byte *Buf) : Buf(Buf), BufPos(0) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
107 Read=&ReadArray;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
108 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
109
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
110 const Byte *Buf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
111 unsigned BufPos;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
112 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
113 inline Reader *ReadyReadStruct(const Byte *vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
114 return new ArrayReader(vec);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
115 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
116 inline unsigned long long GetReadSize(const Byte *vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
117 return (unsigned long long)(long long)-1;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
118 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
119 inline void ArraySeek(Reader *r, long long addr) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
120 ArrayReader *reader=(ArrayReader *)r;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
121 reader->BufPos+=(unsigned)addr;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
122 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
123 inline std::function<void(Reader*, long long)> SeekFn(const Byte *vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
124 return &ArraySeek;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
125 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
126
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
127 class ArrayWriter;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
128 void WriteArray(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
129 class ArrayWriter : public Writer {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
130 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
131 ArrayWriter(Byte *Buf) : Buf(Buf), BufPos(0) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
132 Write=&WriteArray;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
133 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
134
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
135 Byte *Buf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
136 unsigned BufPos;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
137 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
138 inline Writer *ReadyWriteStruct(Byte *vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
139 return new ArrayWriter(vec);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
140 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
141
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
142 class ArrayBuffer : public Buffer {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
143 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
144 ArrayBuffer(Byte *&array) : array(array) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
145
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
146 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
147
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
148 virtual unsigned char *Offset(int offset) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
149 return &array[offset];
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
150 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
151 virtual void Resize(int size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
152 if(array)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
153 array=(Byte*)std::realloc(array, size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
154 else
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
155 array=(Byte*)std::malloc(size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
156 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
157
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
158 protected:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
159 Byte *&array;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
160 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
161
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
162 inline Buffer *CreateBuffer(Byte *&array) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
163 return new ArrayBuffer(array);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
164 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
165
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
166
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
167 //File streamers
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
168 class FileReader;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
169 void ReadFile(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
170 unsigned long long GetReadSize(std::istream &f);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
171 class FileReader : public Reader {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
172 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
173 FileReader(std::istream &Buf) : Buf(Buf) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
174 Read=&ReadFile;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
175 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
176
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
177 std::istream &Buf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
178 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
179 inline Reader *ReadyReadStruct(std::istream &f) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
180 return new FileReader(f);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
181 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
182 inline unsigned long long GetReadSize(std::istream &f) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
183 auto c=f.tellg();
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
184 f.seekg(0, std::ios::end);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
185 auto e=f.tellg();
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
186 f.seekg(c, std::ios::beg);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
187 return e-c;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
188 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
189 inline void FileSeek(Reader *r, long long addr) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
190 FileReader *reader=(FileReader *)r;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
191 reader->Buf.seekg(addr, std::ios::cur);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
192 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
193 inline std::function<void(Reader*, long long)> SeekFn(std::istream &vec) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
194 return &FileSeek;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
195 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
196
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
197 class FileWriter;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
198 void WriteFile(png_struct_def *p, unsigned char *buf, size_t size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
199 class FileWriter : public Writer {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
200 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
201 FileWriter(std::ostream &Buf) : Buf(Buf) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
202 Write=&WriteFile;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
203 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
204
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
205 std::ostream &Buf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
206 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
207 inline Writer *ReadyWriteStruct(std::ostream &f) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
208 return new FileWriter(f);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
209 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
210 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
211
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
212 //currently only works for RGBA
259
9514a8774b67 * Wave system is moved to multimedia module
cemkalyoncu
parents: 215
diff changeset
213 //see LZMA for template class decisions
215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
214 class PNG {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
215 public:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
216
318
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 259
diff changeset
217 class Info {
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 259
diff changeset
218 public:
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 259
diff changeset
219 int Width, Height;
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 259
diff changeset
220 bool Alpha;
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 259
diff changeset
221 int RowBytes;
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 259
diff changeset
222 };
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 259
diff changeset
223
215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
224 PNG() { }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
225
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
226 //throws runtime error
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
227 //Using this system with arrays is extremely dangerous make sure your arrays are big enough
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
228 template <class I_, class O_>
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
229 void Encode(I_ &input, O_ &output, int width, int height) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
230 encode(png::CreateBuffer(input), png::ReadyWriteStruct(output), width, height);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
231 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
232
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
233 //throws runtime error
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
234 //Using this system with arrays is extremely dangerous make sure your arrays are big enough
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
235 template <class I_, class O_>
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
236 Info Decode(I_ &input, O_ &output) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
237 return decode(png::ReadyReadStruct(input), png::CreateBuffer(output));
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
238 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
239
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
240 int PropertySize();
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
241
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
242 protected:
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
243 void encode(png::Buffer *buffer,png::Writer *write, int width, int height);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
244 Info decode(png::Reader *reader,png::Buffer *buffer);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
245
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
246 };
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
247
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
248 extern PNG Png;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
249
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
250 } }

mercurial