1 | // picoPNG version 20080503 (cleaned up and ported to c by kaitek)␊ |
2 | // Copyright (c) 2005-2008 Lode Vandevenne␊ |
3 | //␊ |
4 | // This software is provided 'as-is', without any express or implied␊ |
5 | // warranty. In no event will the authors be held liable for any damages␊ |
6 | // arising from the use of this software.␊ |
7 | //␊ |
8 | // Permission is granted to anyone to use this software for any purpose,␊ |
9 | // including commercial applications, and to alter it and redistribute it␊ |
10 | // freely, subject to the following restrictions:␊ |
11 | //␊ |
12 | // 1. The origin of this software must not be misrepresented; you must not␊ |
13 | // claim that you wrote the original software. If you use this software␊ |
14 | // in a product, an acknowledgment in the product documentation would be␊ |
15 | // appreciated but is not required.␊ |
16 | // 2. Altered source versions must be plainly marked as such, and must not be␊ |
17 | // misrepresented as being the original software.␊ |
18 | // 3. This notice may not be removed or altered from any source distribution.␊ |
19 | ␊ |
20 | ␊ |
21 | #include "libsaio.h"␊ |
22 | #include "picopng.h"␊ |
23 | ␊ |
24 | /* sys.c */␊ |
25 | /*␊ |
26 | extern int open_bvdev(const char *bvd, const char *path);␊ |
27 | extern int close(int fdesc);␊ |
28 | extern int file_size(int fdesc);␊ |
29 | extern int read(int fdesc, char *buf, int count);␊ |
30 | */␊ |
31 | ␊ |
32 | /*************************************************************************************************/␊ |
33 | ␊ |
34 | typedef struct png_alloc_node {␊ |
35 | ␉struct png_alloc_node *prev, *next;␊ |
36 | ␉void *addr;␊ |
37 | ␉size_t size;␊ |
38 | } png_alloc_node_t;␊ |
39 | ␊ |
40 | png_alloc_node_t *png_alloc_head = NULL;␊ |
41 | png_alloc_node_t *png_alloc_tail = NULL;␊ |
42 | ␊ |
43 | png_alloc_node_t *png_alloc_find_node(void *addr)␊ |
44 | {␊ |
45 | ␉png_alloc_node_t *node;␊ |
46 | ␉for (node = png_alloc_head; node; node = node->next)␊ |
47 | ␉␉if (node->addr == addr)␊ |
48 | ␉␉␉break;␊ |
49 | ␉return node;␊ |
50 | }␊ |
51 | ␊ |
52 | void png_alloc_add_node(void *addr, size_t size)␊ |
53 | {␊ |
54 | ␉png_alloc_node_t *node;␊ |
55 | ␉if (png_alloc_find_node(addr))␊ |
56 | ␉␉return;␊ |
57 | ␉node = malloc(sizeof (png_alloc_node_t));␊ |
58 | ␉node->addr = addr;␊ |
59 | ␉node->size = size;␊ |
60 | ␉node->prev = png_alloc_tail;␊ |
61 | ␉node->next = NULL;␊ |
62 | ␉png_alloc_tail = node;␊ |
63 | ␉if (node->prev)␊ |
64 | ␉␉node->prev->next = node;␊ |
65 | ␉if (!png_alloc_head)␊ |
66 | ␉␉png_alloc_head = node;␊ |
67 | }␊ |
68 | ␊ |
69 | void png_alloc_remove_node(png_alloc_node_t *node)␊ |
70 | {␊ |
71 | ␉if (node->prev)␊ |
72 | ␉␉node->prev->next = node->next;␊ |
73 | ␉if (node->next)␊ |
74 | ␉␉node->next->prev = node->prev;␊ |
75 | ␉if (node == png_alloc_head)␊ |
76 | ␉␉png_alloc_head = node->next;␊ |
77 | ␉if (node == png_alloc_tail)␊ |
78 | ␉␉png_alloc_tail = node->prev;␊ |
79 | ␉node->prev = node->next = node->addr = NULL;␊ |
80 | ␉free(node);␊ |
81 | }␊ |
82 | ␊ |
83 | void *png_alloc_malloc(size_t size)␊ |
84 | {␊ |
85 | ␉void *addr = malloc(size);␊ |
86 | ␉png_alloc_add_node(addr, size);␊ |
87 | ␉return addr;␊ |
88 | }␊ |
89 | ␊ |
90 | void *png_alloc_realloc(void *addr, size_t size)␊ |
91 | {␊ |
92 | ␉void *new_addr;␊ |
93 | ␉if (!addr)␊ |
94 | ␉␉return png_alloc_malloc(size);␊ |
95 | ␉new_addr = realloc(addr, size);␊ |
96 | ␉if (new_addr != addr) {␊ |
97 | ␉␉png_alloc_node_t *old_node;␊ |
98 | ␉␉old_node = png_alloc_find_node(addr);␊ |
99 | ␉␉png_alloc_remove_node(old_node);␊ |
100 | ␉␉png_alloc_add_node(new_addr, size);␊ |
101 | ␉}␊ |
102 | ␉return new_addr;␊ |
103 | }␊ |
104 | ␊ |
105 | void png_alloc_free(void *addr)␊ |
106 | {␊ |
107 | ␉png_alloc_node_t *node = png_alloc_find_node(addr);␊ |
108 | ␉if (!node)␊ |
109 | ␉␉return;␊ |
110 | ␉png_alloc_remove_node(node);␊ |
111 | ␉free(addr);␊ |
112 | }␊ |
113 | ␊ |
114 | void png_alloc_free_all()␊ |
115 | {␊ |
116 | ␉while (png_alloc_tail) {␊ |
117 | ␉␉void *addr = png_alloc_tail->addr;␊ |
118 | ␉␉png_alloc_remove_node(png_alloc_tail);␊ |
119 | ␉␉free(addr);␊ |
120 | ␉}␊ |
121 | }␊ |
122 | ␊ |
123 | /*************************************************************************************************/␊ |
124 | ␊ |
125 | __unused void vector32_cleanup(vector32_t *p)␊ |
126 | {␊ |
127 | ␉p->size = p->allocsize = 0;␊ |
128 | ␉if (p->data)␊ |
129 | ␉␉png_alloc_free(p->data);␊ |
130 | ␉p->data = NULL;␊ |
131 | }␊ |
132 | ␊ |
133 | uint32_t vector32_resize(vector32_t *p, size_t size)␊ |
134 | {␉// returns 1 if success, 0 if failure ==> nothing done␊ |
135 | ␉if (size * sizeof (uint32_t) > p->allocsize) {␊ |
136 | ␉␉size_t newsize = size * sizeof (uint32_t) * 2;␊ |
137 | ␉␉void *data = png_alloc_realloc(p->data, newsize);␊ |
138 | ␉␉if (data) {␊ |
139 | ␉␉␉p->allocsize = newsize;␊ |
140 | ␉␉␉p->data = (uint32_t *) data;␊ |
141 | ␉␉␉p->size = size;␊ |
142 | ␉␉} else␊ |
143 | ␉␉␉return 0;␊ |
144 | ␉} else␊ |
145 | ␉␉p->size = size;␊ |
146 | ␉return 1;␊ |
147 | }␊ |
148 | ␊ |
149 | uint32_t vector32_resizev(vector32_t *p, size_t size, uint32_t value)␊ |
150 | {␉// resize and give all new elements the value␊ |
151 | ␉size_t oldsize = p->size, i;␊ |
152 | ␉if (!vector32_resize(p, size))␊ |
153 | ␉␉return 0;␊ |
154 | ␉for (i = oldsize; i < size; i++)␊ |
155 | ␉␉p->data[i] = value;␊ |
156 | ␉return 1;␊ |
157 | }␊ |
158 | ␊ |
159 | void vector32_init(vector32_t *p)␊ |
160 | {␊ |
161 | ␉p->data = NULL;␊ |
162 | ␉p->size = p->allocsize = 0;␊ |
163 | }␊ |
164 | ␊ |
165 | vector32_t *vector32_new(size_t size, uint32_t value)␊ |
166 | {␊ |
167 | ␉vector32_t *p = png_alloc_malloc(sizeof (vector32_t));␊ |
168 | ␉vector32_init(p);␊ |
169 | ␉if (size && !vector32_resizev(p, size, value))␊ |
170 | ␉␉return NULL;␊ |
171 | ␉return p;␊ |
172 | }␊ |
173 | ␊ |
174 | /*************************************************************************************************/␊ |
175 | ␊ |
176 | __unused void vector8_cleanup(vector8_t *p)␊ |
177 | {␊ |
178 | ␉p->size = p->allocsize = 0;␊ |
179 | ␉if (p->data)␊ |
180 | ␉␉png_alloc_free(p->data);␊ |
181 | ␉p->data = NULL;␊ |
182 | }␊ |
183 | ␊ |
184 | uint32_t vector8_resize(vector8_t *p, size_t size)␊ |
185 | {␉// returns 1 if success, 0 if failure ==> nothing done␊ |
186 | ␉// xxx: the use of sizeof uint32_t here seems like a bug (this descends from the lodepng vector␊ |
187 | ␉// compatibility functions which do the same). without this there is corruption in certain cases,␊ |
188 | ␉// so this was probably done to cover up allocation bug(s) in the original picopng code!␊ |
189 | ␉if (size * sizeof (uint32_t) > p->allocsize) {␊ |
190 | ␉␉size_t newsize = size * sizeof (uint32_t) * 2;␊ |
191 | ␉␉void *data = png_alloc_realloc(p->data, newsize);␊ |
192 | ␉␉if (data) {␊ |
193 | ␉␉␉p->allocsize = newsize;␊ |
194 | ␉␉␉p->data = (uint8_t *) data;␊ |
195 | ␉␉␉p->size = size;␊ |
196 | ␉␉} else␊ |
197 | ␉␉␉return 0; // error: not enough memory␊ |
198 | ␉} else␊ |
199 | ␉␉p->size = size;␊ |
200 | ␉return 1;␊ |
201 | }␊ |
202 | ␊ |
203 | uint32_t vector8_resizev(vector8_t *p, size_t size, uint8_t value)␊ |
204 | {␉// resize and give all new elements the value␊ |
205 | ␉size_t oldsize = p->size, i;␊ |
206 | ␉if (!vector8_resize(p, size))␊ |
207 | ␉␉return 0;␊ |
208 | ␉for (i = oldsize; i < size; i++)␊ |
209 | ␉␉p->data[i] = value;␊ |
210 | ␉return 1;␊ |
211 | }␊ |
212 | ␊ |
213 | void vector8_init(vector8_t *p)␊ |
214 | {␊ |
215 | ␉p->data = NULL;␊ |
216 | ␉p->size = p->allocsize = 0;␊ |
217 | }␊ |
218 | ␊ |
219 | vector8_t *vector8_new(size_t size, uint8_t value)␊ |
220 | {␊ |
221 | ␉vector8_t *p = png_alloc_malloc(sizeof (vector8_t));␊ |
222 | ␉vector8_init(p);␊ |
223 | ␉if (size && !vector8_resizev(p, size, value))␊ |
224 | ␉␉return NULL;␊ |
225 | ␉return p;␊ |
226 | }␊ |
227 | ␊ |
228 | vector8_t *vector8_copy(vector8_t *p)␊ |
229 | {␊ |
230 | ␉vector8_t *q = vector8_new(p->size, 0);␊ |
231 | ␉uint32_t n;␊ |
232 | ␉for (n = 0; n < q->size; n++)␊ |
233 | ␉␉q->data[n] = p->data[n];␊ |
234 | ␉return q;␊ |
235 | }␊ |
236 | ␊ |
237 | /*************************************************************************************************/␊ |
238 | ␊ |
239 | const uint32_t LENBASE[29] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51,␊ |
240 | ␉␉59, 67, 83, 99, 115, 131, 163, 195, 227, 258 };␊ |
241 | const uint32_t LENEXTRA[29] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,␊ |
242 | ␉␉4, 5, 5, 5, 5, 0 };␊ |
243 | const uint32_t DISTBASE[30] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,␊ |
244 | ␉␉513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 };␊ |
245 | const uint32_t DISTEXTRA[30] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,␊ |
246 | ␉␉10, 10, 11, 11, 12, 12, 13, 13 };␊ |
247 | // code length code lengths␊ |
248 | const uint32_t CLCL[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };␊ |
249 | ␊ |
250 | /*************************************************************************************************/␊ |
251 | ␊ |
252 | typedef struct {␊ |
253 | ␉// 2D representation of a huffman tree: The one dimension is "0" or "1", the other contains all␊ |
254 | ␉// nodes and leaves of the tree.␊ |
255 | ␉vector32_t *tree2d;␊ |
256 | } HuffmanTree;␊ |
257 | ␊ |
258 | HuffmanTree *HuffmanTree_new()␊ |
259 | {␊ |
260 | ␉HuffmanTree *tree = png_alloc_malloc(sizeof (HuffmanTree));␊ |
261 | ␉tree->tree2d = NULL;␊ |
262 | ␉return tree;␊ |
263 | }␊ |
264 | ␊ |
265 | int HuffmanTree_makeFromLengths(HuffmanTree *tree, const vector32_t *bitlen, uint32_t maxbitlen)␊ |
266 | {␉// make tree given the lengths␊ |
267 | ␉uint32_t bits, n, i;␊ |
268 | ␉uint32_t numcodes = (uint32_t) bitlen->size, treepos = 0, nodefilled = 0;␊ |
269 | ␉vector32_t *tree1d, *blcount, *nextcode;␊ |
270 | ␉tree1d = vector32_new(numcodes, 0);␊ |
271 | ␉blcount = vector32_new(maxbitlen + 1, 0);␊ |
272 | ␉nextcode = vector32_new(maxbitlen + 1, 0);␊ |
273 | ␉for (bits = 0; bits < numcodes; bits++)␊ |
274 | ␉␉blcount->data[bitlen->data[bits]]++; // count number of instances of each code length␊ |
275 | ␉for (bits = 1; bits <= maxbitlen; bits++)␊ |
276 | ␉␉nextcode->data[bits] = (nextcode->data[bits - 1] + blcount->data[bits - 1]) << 1;␊ |
277 | ␉for (n = 0; n < numcodes; n++)␊ |
278 | ␉␉if (bitlen->data[n] != 0)␊ |
279 | ␉␉␉tree1d->data[n] = nextcode->data[bitlen->data[n]]++; // generate all the codes␊ |
280 | ␉// 0x7fff here means the tree2d isn't filled there yet␊ |
281 | ␉vector32_t *tree2d = vector32_new(numcodes * 2, 0x7fff);␊ |
282 | ␉tree->tree2d = tree2d;␊ |
283 | ␉for (n = 0; n < numcodes; n++) // the codes␊ |
284 | ␉␉for (i = 0; i < bitlen->data[n]; i++) { // the bits for this code␊ |
285 | ␉␉␉uint32_t bit = (tree1d->data[n] >> (bitlen->data[n] - i - 1)) & 1;␊ |
286 | ␉␉␉if (treepos > numcodes - 2)␊ |
287 | ␉␉␉␉return 55;␊ |
288 | ␉␉␉if (tree2d->data[2 * treepos + bit] == 0x7fff) { // not yet filled in␊ |
289 | ␉␉␉␉if (i + 1 == bitlen->data[n]) { // last bit␊ |
290 | ␉␉␉␉␉tree2d->data[2 * treepos + bit] = n;␊ |
291 | ␉␉␉␉␉treepos = 0;␊ |
292 | ␉␉␉␉} else { // addresses are encoded as values > numcodes␊ |
293 | ␉␉␉␉␉tree2d->data[2 * treepos + bit] = ++nodefilled + numcodes;␊ |
294 | ␉␉␉␉␉treepos = nodefilled;␊ |
295 | ␉␉␉␉}␊ |
296 | ␉␉␉} else // subtract numcodes from address to get address value␊ |
297 | ␉␉␉␉treepos = tree2d->data[2 * treepos + bit] - numcodes;␊ |
298 | ␉␉}␊ |
299 | ␉return 0;␊ |
300 | }␊ |
301 | ␊ |
302 | int HuffmanTree_decode(const HuffmanTree *tree, bool *decoded, uint32_t *result, size_t *treepos,␊ |
303 | ␉␉uint32_t bit)␊ |
304 | {␉// Decodes a symbol from the tree␊ |
305 | ␉const vector32_t *tree2d = tree->tree2d;␊ |
306 | ␉uint32_t numcodes = (uint32_t) tree2d->size / 2;␊ |
307 | ␉if (*treepos >= numcodes)␊ |
308 | ␉␉return 11; // error: you appeared outside the codetree␊ |
309 | ␉*result = tree2d->data[2 * (*treepos) + bit];␊ |
310 | ␉*decoded = (*result < numcodes);␊ |
311 | ␉*treepos = *decoded ? 0 : *result - numcodes;␊ |
312 | ␉return 0;␊ |
313 | }␊ |
314 | ␊ |
315 | /*************************************************************************************************/␊ |
316 | ␊ |
317 | int Inflator_error;␊ |
318 | ␊ |
319 | uint32_t Zlib_readBitFromStream(size_t *bitp, const uint8_t *bits)␊ |
320 | {␊ |
321 | ␉uint32_t result = (bits[*bitp >> 3] >> (*bitp & 0x7)) & 1;␊ |
322 | ␉(*bitp)++;␊ |
323 | ␉return result;␊ |
324 | }␊ |
325 | ␊ |
326 | uint32_t Zlib_readBitsFromStream(size_t *bitp, const uint8_t *bits, size_t nbits)␊ |
327 | {␊ |
328 | ␉uint32_t i, result = 0;␊ |
329 | ␉for (i = 0; i < nbits; i++)␊ |
330 | ␉␉result += (Zlib_readBitFromStream(bitp, bits)) << i;␊ |
331 | ␉return result;␊ |
332 | }␊ |
333 | ␊ |
334 | void Inflator_generateFixedTrees(HuffmanTree *tree, HuffmanTree *treeD)␊ |
335 | {␉// get the tree of a deflated block with fixed tree␊ |
336 | ␉size_t i;␊ |
337 | ␉vector32_t *bitlen, *bitlenD;␊ |
338 | ␉bitlen = vector32_new(288, 8);␊ |
339 | ␉bitlenD = vector32_new(32, 5);␊ |
340 | ␉for (i = 144; i <= 255; i++)␊ |
341 | ␉␉bitlen->data[i] = 9;␊ |
342 | ␉for (i = 256; i <= 279; i++)␊ |
343 | ␉␉bitlen->data[i] = 7;␊ |
344 | ␉HuffmanTree_makeFromLengths(tree, bitlen, 15);␊ |
345 | ␉HuffmanTree_makeFromLengths(treeD, bitlenD, 15);␊ |
346 | }␊ |
347 | ␊ |
348 | uint32_t Inflator_huffmanDecodeSymbol(const uint8_t *in, size_t *bp, const HuffmanTree *codetree,␊ |
349 | ␉␉size_t inlength)␊ |
350 | {␉// decode a single symbol from given list of bits with given code tree. returns the symbol␊ |
351 | ␉bool decoded = false;␊ |
352 | ␉uint32_t ct = 0;␊ |
353 | ␉size_t treepos = 0;␊ |
354 | ␉for (;;) {␊ |
355 | ␉␉if ((*bp & 0x07) == 0 && (*bp >> 3) > inlength) {␊ |
356 | ␉␉␉Inflator_error = 10; // error: end reached without endcode␊ |
357 | ␉␉␉return 0;␊ |
358 | ␉␉}␊ |
359 | ␉␉Inflator_error = HuffmanTree_decode(codetree, &decoded, &ct, &treepos,␊ |
360 | ␉␉␉␉Zlib_readBitFromStream(bp, in));␊ |
361 | ␉␉if (Inflator_error)␊ |
362 | ␉␉␉return 0; // stop, an error happened␊ |
363 | ␉␉if (decoded)␊ |
364 | ␉␉␉return ct;␊ |
365 | ␉}␊ |
366 | }␊ |
367 | ␊ |
368 | void Inflator_getTreeInflateDynamic(HuffmanTree *tree, HuffmanTree *treeD, const uint8_t *in,␊ |
369 | ␉␉size_t *bp, size_t inlength)␊ |
370 | {␉// get the tree of a deflated block with dynamic tree, the tree itself is also Huffman␊ |
371 | ␉// compressed with a known tree␊ |
372 | ␉size_t i, n;␊ |
373 | ␉HuffmanTree *codelengthcodetree = HuffmanTree_new(); // the code tree for code length codes␊ |
374 | ␉vector32_t *bitlen, *bitlenD;␊ |
375 | ␉bitlen = vector32_new(288, 0);␊ |
376 | ␉bitlenD = vector32_new(32, 0);␊ |
377 | ␉if (*bp >> 3 >= inlength - 2) {␊ |
378 | ␉␉Inflator_error = 49; // the bit pointer is or will go past the memory␊ |
379 | ␉␉return;␊ |
380 | ␉}␊ |
381 | ␉size_t HLIT = Zlib_readBitsFromStream(bp, in, 5) + 257;␉// number of literal/length codes + 257␊ |
382 | ␉size_t HDIST = Zlib_readBitsFromStream(bp, in, 5) + 1;␉// number of dist codes + 1␊ |
383 | ␉size_t HCLEN = Zlib_readBitsFromStream(bp, in, 4) + 4;␉// number of code length codes + 4␊ |
384 | ␉vector32_t *codelengthcode; // lengths of tree to decode the lengths of the dynamic tree␊ |
385 | ␉codelengthcode = vector32_new(19, 0);␊ |
386 | ␉for (i = 0; i < 19; i++)␊ |
387 | ␉␉codelengthcode->data[CLCL[i]] = (i < HCLEN) ? Zlib_readBitsFromStream(bp, in, 3) : 0;␊ |
388 | ␉Inflator_error = HuffmanTree_makeFromLengths(codelengthcodetree, codelengthcode, 7);␊ |
389 | ␉if (Inflator_error)␊ |
390 | ␉␉return;␊ |
391 | ␉size_t replength;␊ |
392 | ␉for (i = 0; i < HLIT + HDIST; ) {␊ |
393 | ␉␉uint32_t code = Inflator_huffmanDecodeSymbol(in, bp, codelengthcodetree, inlength);␊ |
394 | ␉␉if (Inflator_error)␊ |
395 | ␉␉␉return;␊ |
396 | ␉␉if (code <= 15) { // a length code␊ |
397 | ␉␉␉if (i < HLIT)␊ |
398 | ␉␉␉␉bitlen->data[i++] = code;␊ |
399 | ␉␉␉else␊ |
400 | ␉␉␉␉bitlenD->data[i++ - HLIT] = code;␊ |
401 | ␉␉} else if (code == 16) { // repeat previous␊ |
402 | ␉␉␉if (*bp >> 3 >= inlength) {␊ |
403 | ␉␉␉␉Inflator_error = 50; // error, bit pointer jumps past memory␊ |
404 | ␉␉␉␉return;␊ |
405 | ␉␉␉}␊ |
406 | ␉␉␉replength = 3 + Zlib_readBitsFromStream(bp, in, 2);␊ |
407 | ␉␉␉uint32_t value; // set value to the previous code␊ |
408 | ␉␉␉if ((i - 1) < HLIT)␊ |
409 | ␉␉␉␉value = bitlen->data[i - 1];␊ |
410 | ␉␉␉else␊ |
411 | ␉␉␉␉value = bitlenD->data[i - HLIT - 1];␊ |
412 | ␉␉␉for (n = 0; n < replength; n++) { // repeat this value in the next lengths␊ |
413 | ␉␉␉␉if (i >= HLIT + HDIST) {␊ |
414 | ␉␉␉␉␉Inflator_error = 13; // error: i is larger than the amount of codes␊ |
415 | ␉␉␉␉␉return;␊ |
416 | ␉␉␉␉}␊ |
417 | ␉␉␉␉if (i < HLIT)␊ |
418 | ␉␉␉␉␉bitlen->data[i++] = value;␊ |
419 | ␉␉␉␉else␊ |
420 | ␉␉␉␉␉bitlenD->data[i++ - HLIT] = value;␊ |
421 | ␉␉␉}␊ |
422 | ␉␉} else if (code == 17) { // repeat "0" 3-10 times␊ |
423 | ␉␉␉if (*bp >> 3 >= inlength) {␊ |
424 | ␉␉␉␉Inflator_error = 50; // error, bit pointer jumps past memory␊ |
425 | ␉␉␉␉return;␊ |
426 | ␉␉␉}␊ |
427 | ␉␉␉replength = 3 + Zlib_readBitsFromStream(bp, in, 3);␊ |
428 | ␉␉␉for (n = 0; n < replength; n++) { // repeat this value in the next lengths␊ |
429 | ␉␉␉␉if (i >= HLIT + HDIST) {␊ |
430 | ␉␉␉␉␉Inflator_error = 14; // error: i is larger than the amount of codes␊ |
431 | ␉␉␉␉␉return;␊ |
432 | ␉␉␉␉}␊ |
433 | ␉␉␉␉if (i < HLIT)␊ |
434 | ␉␉␉␉␉bitlen->data[i++] = 0;␊ |
435 | ␉␉␉␉else␊ |
436 | ␉␉␉␉␉bitlenD->data[i++ - HLIT] = 0;␊ |
437 | ␉␉␉}␊ |
438 | ␉␉} else if (code == 18) { // repeat "0" 11-138 times␊ |
439 | ␉␉␉if (*bp >> 3 >= inlength) {␊ |
440 | ␉␉␉␉Inflator_error = 50; // error, bit pointer jumps past memory␊ |
441 | ␉␉␉␉return;␊ |
442 | ␉␉␉}␊ |
443 | ␉␉␉replength = 11 + Zlib_readBitsFromStream(bp, in, 7);␊ |
444 | ␉␉␉for (n = 0; n < replength; n++) { // repeat this value in the next lengths␊ |
445 | ␉␉␉␉if (i >= HLIT + HDIST) {␊ |
446 | ␉␉␉␉␉Inflator_error = 15; // error: i is larger than the amount of codes␊ |
447 | ␉␉␉␉␉return;␊ |
448 | ␉␉␉␉}␊ |
449 | ␉␉␉␉if (i < HLIT)␊ |
450 | ␉␉␉␉␉bitlen->data[i++] = 0;␊ |
451 | ␉␉␉␉else␊ |
452 | ␉␉␉␉␉bitlenD->data[i++ - HLIT] = 0;␊ |
453 | ␉␉␉}␊ |
454 | ␉␉} else {␊ |
455 | ␉␉␉Inflator_error = 16; // error: an nonexitent code appeared. This can never happen.␊ |
456 | ␉␉␉return;␊ |
457 | ␉␉}␊ |
458 | ␉}␊ |
459 | ␉if (bitlen->data[256] == 0) {␊ |
460 | ␉␉Inflator_error = 64; // the length of the end code 256 must be larger than 0␊ |
461 | ␉␉return;␊ |
462 | ␉}␊ |
463 | ␉// now we've finally got HLIT and HDIST, so generate the code trees, and the function is done␊ |
464 | ␉Inflator_error = HuffmanTree_makeFromLengths(tree, bitlen, 15);␊ |
465 | ␉if (Inflator_error)␊ |
466 | ␉␉return;␊ |
467 | ␉Inflator_error = HuffmanTree_makeFromLengths(treeD, bitlenD, 15);␊ |
468 | ␉if (Inflator_error)␊ |
469 | ␉␉return;␊ |
470 | }␊ |
471 | ␊ |
472 | void Inflator_inflateHuffmanBlock(vector8_t *out, const uint8_t *in, size_t *bp, size_t *pos,␊ |
473 | ␉␉size_t inlength, uint32_t btype)␊ |
474 | {␊ |
475 | ␉HuffmanTree *codetree, *codetreeD; // the code tree for Huffman codes, dist codes␊ |
476 | ␉codetree = HuffmanTree_new();␊ |
477 | ␉codetreeD = HuffmanTree_new();␊ |
478 | ␉if (btype == 1)␊ |
479 | ␉␉Inflator_generateFixedTrees(codetree, codetreeD);␊ |
480 | ␉else if (btype == 2) {␊ |
481 | ␉␉Inflator_getTreeInflateDynamic(codetree, codetreeD, in, bp, inlength);␊ |
482 | ␉␉if (Inflator_error)␊ |
483 | ␉␉␉return;␊ |
484 | ␉}␊ |
485 | ␉for (;;) {␊ |
486 | ␉␉uint32_t code = Inflator_huffmanDecodeSymbol(in, bp, codetree, inlength);␊ |
487 | ␉␉if (Inflator_error)␊ |
488 | ␉␉␉return;␊ |
489 | ␉␉if (code == 256) // end code␊ |
490 | ␉␉␉return;␊ |
491 | ␉␉else if (code <= 255) { // literal symbol␊ |
492 | ␉␉␉if (*pos >= out->size)␊ |
493 | ␉␉␉␉vector8_resize(out, (*pos + 1) * 2); // reserve more room␊ |
494 | ␉␉␉out->data[(*pos)++] = (uint8_t) code;␊ |
495 | ␉␉} else if (code >= 257 && code <= 285) { // length code␊ |
496 | ␉␉␉size_t length = LENBASE[code - 257], numextrabits = LENEXTRA[code - 257];␊ |
497 | ␉␉␉if ((*bp >> 3) >= inlength) {␊ |
498 | ␉␉␉␉Inflator_error = 51; // error, bit pointer will jump past memory␊ |
499 | ␉␉␉␉return;␊ |
500 | ␉␉␉}␊ |
501 | ␉␉␉length += Zlib_readBitsFromStream(bp, in, numextrabits);␊ |
502 | ␉␉␉uint32_t codeD = Inflator_huffmanDecodeSymbol(in, bp, codetreeD, inlength);␊ |
503 | ␉␉␉if (Inflator_error)␊ |
504 | ␉␉␉␉return;␊ |
505 | ␉␉␉if (codeD > 29) {␊ |
506 | ␉␉␉␉Inflator_error = 18; // error: invalid dist code (30-31 are never used)␊ |
507 | ␉␉␉␉return;␊ |
508 | ␉␉␉}␊ |
509 | ␉␉␉uint32_t dist = DISTBASE[codeD], numextrabitsD = DISTEXTRA[codeD];␊ |
510 | ␉␉␉if ((*bp >> 3) >= inlength) {␊ |
511 | ␉␉␉␉Inflator_error = 51; // error, bit pointer will jump past memory␊ |
512 | ␉␉␉␉return;␊ |
513 | ␉␉␉}␊ |
514 | ␉␉␉dist += Zlib_readBitsFromStream(bp, in, numextrabitsD);␊ |
515 | ␉␉␉size_t start = *pos, back = start - dist; // backwards␊ |
516 | ␉␉␉if (*pos + length >= out->size)␊ |
517 | ␉␉␉␉vector8_resize(out, (*pos + length) * 2); // reserve more room␊ |
518 | ␉␉␉size_t i;␊ |
519 | ␉␉␉for (i = 0; i < length; i++) {␊ |
520 | ␉␉␉␉out->data[(*pos)++] = out->data[back++];␊ |
521 | ␉␉␉␉if (back >= start)␊ |
522 | ␉␉␉␉␉back = start - dist;␊ |
523 | ␉␉␉}␊ |
524 | ␉␉}␊ |
525 | ␉}␊ |
526 | }␊ |
527 | ␊ |
528 | void Inflator_inflateNoCompression(vector8_t *out, const uint8_t *in, size_t *bp, size_t *pos,␊ |
529 | ␉␉size_t inlength)␊ |
530 | {␊ |
531 | ␉while ((*bp & 0x7) != 0)␊ |
532 | ␉␉(*bp)++; // go to first boundary of byte␊ |
533 | ␉size_t p = *bp / 8;␊ |
534 | ␉if (p >= inlength - 4) {␊ |
535 | ␉␉Inflator_error = 52; // error, bit pointer will jump past memory␊ |
536 | ␉␉return;␊ |
537 | ␉}␊ |
538 | ␉uint32_t LEN = in[p] + 256 * in[p + 1], NLEN = in[p + 2] + 256 * in[p + 3];␊ |
539 | ␉p += 4;␊ |
540 | ␉if (LEN + NLEN != 65535) {␊ |
541 | ␉␉Inflator_error = 21; // error: NLEN is not one's complement of LEN␊ |
542 | ␉␉return;␊ |
543 | ␉}␊ |
544 | ␉if (*pos + LEN >= out->size)␊ |
545 | ␉␉vector8_resize(out, *pos + LEN);␊ |
546 | ␉if (p + LEN > inlength) {␊ |
547 | ␉␉Inflator_error = 23; // error: reading outside of in buffer␊ |
548 | ␉␉return;␊ |
549 | ␉}␊ |
550 | ␉uint32_t n;␊ |
551 | ␉for (n = 0; n < LEN; n++)␊ |
552 | ␉␉out->data[(*pos)++] = in[p++]; // read LEN bytes of literal data␊ |
553 | ␉*bp = p * 8;␊ |
554 | }␊ |
555 | ␊ |
556 | void Inflator_inflate(vector8_t *out, const vector8_t *in, size_t inpos)␊ |
557 | {␊ |
558 | ␉size_t bp = 0, pos = 0; // bit pointer and byte pointer␊ |
559 | ␉Inflator_error = 0;␊ |
560 | ␉uint32_t BFINAL = 0;␊ |
561 | ␉while (!BFINAL && !Inflator_error) {␊ |
562 | ␉␉if (bp >> 3 >= in->size) {␊ |
563 | ␉␉␉Inflator_error = 52; // error, bit pointer will jump past memory␊ |
564 | ␉␉␉return;␊ |
565 | ␉␉}␊ |
566 | ␉␉BFINAL = Zlib_readBitFromStream(&bp, &in->data[inpos]);␊ |
567 | ␉␉uint32_t BTYPE = Zlib_readBitFromStream(&bp, &in->data[inpos]);␊ |
568 | ␉␉BTYPE += 2 * Zlib_readBitFromStream(&bp, &in->data[inpos]);␊ |
569 | ␉␉if (BTYPE == 3) {␊ |
570 | ␉␉␉Inflator_error = 20; // error: invalid BTYPE␊ |
571 | ␉␉␉return;␊ |
572 | ␉␉}␊ |
573 | ␉␉else if (BTYPE == 0)␊ |
574 | ␉␉␉Inflator_inflateNoCompression(out, &in->data[inpos], &bp, &pos, in->size);␊ |
575 | ␉␉else␊ |
576 | ␉␉␉Inflator_inflateHuffmanBlock(out, &in->data[inpos], &bp, &pos, in->size, BTYPE);␊ |
577 | ␉}␊ |
578 | ␉if (!Inflator_error)␊ |
579 | ␉␉vector8_resize(out, pos); // Only now we know the true size of out, resize it to that␊ |
580 | }␊ |
581 | ␊ |
582 | /*************************************************************************************************/␊ |
583 | ␊ |
584 | int Zlib_decompress(vector8_t *out, const vector8_t *in) // returns error value␊ |
585 | {␊ |
586 | ␉if (in->size < 2)␊ |
587 | ␉␉return 53; // error, size of zlib data too small␊ |
588 | ␉if ((in->data[0] * 256 + in->data[1]) % 31 != 0)␊ |
589 | ␉␉// error: 256 * in->data[0] + in->data[1] must be a multiple of 31, the FCHECK value is␊ |
590 | ␉␉// supposed to be made that way␊ |
591 | ␉␉return 24;␊ |
592 | ␉uint32_t CM = in->data[0] & 15, CINFO = (in->data[0] >> 4) & 15, FDICT = (in->data[1] >> 5) & 1;␊ |
593 | ␉if (CM != 8 || CINFO > 7)␊ |
594 | ␉␉// error: only compression method 8: inflate with sliding window of 32k is supported by␊ |
595 | ␉␉// the PNG spec␊ |
596 | ␉␉return 25;␊ |
597 | ␉if (FDICT != 0)␊ |
598 | ␉␉// error: the specification of PNG says about the zlib stream: "The additional flags shall␊ |
599 | ␉␉// not specify a preset dictionary."␊ |
600 | ␉␉return 26;␊ |
601 | ␉Inflator_inflate(out, in, 2);␊ |
602 | ␉return Inflator_error; // note: adler32 checksum was skipped and ignored␊ |
603 | }␊ |
604 | ␊ |
605 | /*************************************************************************************************/␊ |
606 | ␊ |
607 | #define PNG_SIGNATURE␉0x0a1a0a0d474e5089ull␊ |
608 | ␊ |
609 | #define CHUNK_IHDR␉␉0x52444849␊ |
610 | #define CHUNK_IDAT␉␉0x54414449␊ |
611 | #define CHUNK_IEND␉␉0x444e4549␊ |
612 | #define CHUNK_PLTE␉␉0x45544c50␊ |
613 | #define CHUNK_tRNS␉␉0x534e5274␊ |
614 | ␊ |
615 | int PNG_error;␊ |
616 | ␊ |
617 | uint32_t PNG_readBitFromReversedStream(size_t *bitp, const uint8_t *bits)␊ |
618 | {␊ |
619 | ␉uint32_t result = (bits[*bitp >> 3] >> (7 - (*bitp & 0x7))) & 1;␊ |
620 | ␉(*bitp)++;␊ |
621 | ␉return result;␊ |
622 | }␊ |
623 | ␊ |
624 | uint32_t PNG_readBitsFromReversedStream(size_t *bitp, const uint8_t *bits, uint32_t nbits)␊ |
625 | {␊ |
626 | ␉uint32_t i, result = 0;␊ |
627 | ␉for (i = nbits - 1; i < nbits; i--)␊ |
628 | ␉␉result += ((PNG_readBitFromReversedStream(bitp, bits)) << i);␊ |
629 | ␉return result;␊ |
630 | }␊ |
631 | ␊ |
632 | void PNG_setBitOfReversedStream(size_t *bitp, uint8_t *bits, uint32_t bit)␊ |
633 | {␊ |
634 | ␉bits[*bitp >> 3] |= (bit << (7 - (*bitp & 0x7)));␊ |
635 | ␉(*bitp)++;␊ |
636 | }␊ |
637 | ␊ |
638 | uint32_t PNG_read32bitInt(const uint8_t *buffer)␊ |
639 | {␊ |
640 | ␉return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];␊ |
641 | }␊ |
642 | ␊ |
643 | int PNG_checkColorValidity(uint32_t colorType, uint32_t bd) // return type is a LodePNG error code␊ |
644 | {␊ |
645 | ␉if ((colorType == 2 || colorType == 4 || colorType == 6)) {␊ |
646 | ␉␉if (!(bd == 8 || bd == 16))␊ |
647 | ␉␉␉return 37;␊ |
648 | ␉␉else␊ |
649 | ␉␉␉return 0;␊ |
650 | ␉} else if (colorType == 0) {␊ |
651 | ␉␉if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16))␊ |
652 | ␉␉␉return 37;␊ |
653 | ␉␉else␊ |
654 | ␉␉␉return 0;␊ |
655 | ␉} else if (colorType == 3) {␊ |
656 | ␉␉if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8))␊ |
657 | ␉␉␉return 37;␊ |
658 | ␉␉else␊ |
659 | ␉␉␉return 0;␊ |
660 | ␉} else␊ |
661 | ␉␉return 31; // nonexistent color type␊ |
662 | }␊ |
663 | ␊ |
664 | uint32_t PNG_getBpp(const PNG_info_t *info)␊ |
665 | {␊ |
666 | ␉uint32_t bitDepth, colorType;␊ |
667 | ␉bitDepth = info->bitDepth;␊ |
668 | ␉colorType = info->colorType;␊ |
669 | ␉if (colorType == 2)␊ |
670 | ␉␉return (3 * bitDepth);␊ |
671 | ␉else if (colorType >= 4)␊ |
672 | ␉␉return (colorType - 2) * bitDepth;␊ |
673 | ␉else␊ |
674 | ␉␉return bitDepth;␊ |
675 | }␊ |
676 | ␊ |
677 | void PNG_readPngHeader(PNG_info_t *info, const uint8_t *in, size_t inlength)␊ |
678 | {␉// read the information from the header and store it in the Info␊ |
679 | ␉if (inlength < 29) {␊ |
680 | ␉␉PNG_error = 27; // error: the data length is smaller than the length of the header␊ |
681 | ␉␉return;␊ |
682 | ␉}␊ |
683 | ␉if (*(uint64_t *) in != PNG_SIGNATURE) {␊ |
684 | ␉␉PNG_error = 28; // no PNG signature␊ |
685 | ␉␉return;␊ |
686 | ␉}␊ |
687 | ␉if (*(uint32_t *) &in[12] != CHUNK_IHDR) {␊ |
688 | ␉␉PNG_error = 29; // error: it doesn't start with a IHDR chunk!␊ |
689 | ␉␉return;␊ |
690 | ␉}␊ |
691 | ␉info->width = PNG_read32bitInt(&in[16]);␊ |
692 | ␉info->height = PNG_read32bitInt(&in[20]);␊ |
693 | ␉info->bitDepth = in[24];␊ |
694 | ␉info->colorType = in[25];␊ |
695 | ␉info->compressionMethod = in[26];␊ |
696 | ␉if (in[26] != 0) {␊ |
697 | ␉␉PNG_error = 32; // error: only compression method 0 is allowed in the specification␊ |
698 | ␉␉return;␊ |
699 | ␉}␊ |
700 | ␉info->filterMethod = in[27];␊ |
701 | ␉if (in[27] != 0) {␊ |
702 | ␉␉PNG_error = 33; // error: only filter method 0 is allowed in the specification␊ |
703 | ␉␉return;␊ |
704 | ␉}␊ |
705 | ␉info->interlaceMethod = in[28];␊ |
706 | ␉if (in[28] > 1) {␊ |
707 | ␉␉PNG_error = 34; // error: only interlace methods 0 and 1 exist in the specification␊ |
708 | ␉␉return;␊ |
709 | ␉}␊ |
710 | ␉PNG_error = PNG_checkColorValidity(info->colorType, info->bitDepth);␊ |
711 | }␊ |
712 | ␊ |
713 | int PNG_paethPredictor(int a, int b, int c) // Paeth predicter, used by PNG filter type 4␊ |
714 | {␊ |
715 | ␉int p, pa, pb, pc;␊ |
716 | ␉p = a + b - c;␊ |
717 | ␉pa = p > a ? (p - a) : (a - p);␊ |
718 | ␉pb = p > b ? (p - b) : (b - p);␊ |
719 | ␉pc = p > c ? (p - c) : (c - p);␊ |
720 | ␉return (pa <= pb && pa <= pc) ? a : (pb <= pc ? b : c);␊ |
721 | }␊ |
722 | ␊ |
723 | void PNG_unFilterScanline(uint8_t *recon, const uint8_t *scanline, const uint8_t *precon,␊ |
724 | ␉␉size_t bytewidth, uint32_t filterType, size_t length)␊ |
725 | {␊ |
726 | ␉size_t i;␊ |
727 | ␉switch (filterType) {␊ |
728 | ␉case 0:␊ |
729 | ␉␉for (i = 0; i < length; i++)␊ |
730 | ␉␉␉recon[i] = scanline[i];␊ |
731 | ␉␉break;␊ |
732 | ␉case 1:␊ |
733 | ␉␉for (i = 0; i < bytewidth; i++)␊ |
734 | ␉␉␉recon[i] = scanline[i];␊ |
735 | ␉␉for (i = bytewidth; i < length; i++)␊ |
736 | ␉␉␉recon[i] = scanline[i] + recon[i - bytewidth];␊ |
737 | ␉␉break;␊ |
738 | ␉case 2:␊ |
739 | ␉␉if (precon)␊ |
740 | ␉␉␉for (i = 0; i < length; i++)␊ |
741 | ␉␉␉␉recon[i] = scanline[i] + precon[i];␊ |
742 | ␉␉else␊ |
743 | ␉␉␉for (i = 0; i < length; i++)␊ |
744 | ␉␉␉␉recon[i] = scanline[i];␊ |
745 | ␉␉break;␊ |
746 | ␉case 3:␊ |
747 | ␉␉if (precon) {␊ |
748 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
749 | ␉␉␉␉recon[i] = scanline[i] + precon[i] / 2;␊ |
750 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
751 | ␉␉␉␉recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);␊ |
752 | ␉␉} else {␊ |
753 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
754 | ␉␉␉␉recon[i] = scanline[i];␊ |
755 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
756 | ␉␉␉␉recon[i] = scanline[i] + recon[i - bytewidth] / 2;␊ |
757 | ␉␉}␊ |
758 | ␉␉break;␊ |
759 | ␉case 4:␊ |
760 | ␉␉if (precon) {␊ |
761 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
762 | ␉␉␉␉recon[i] = (uint8_t) (scanline[i] + PNG_paethPredictor(0, precon[i], 0));␊ |
763 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
764 | ␉␉␉␉recon[i] = (uint8_t) (scanline[i] + PNG_paethPredictor(recon[i - bytewidth],␊ |
765 | ␉␉␉␉␉␉precon[i], precon[i - bytewidth]));␊ |
766 | ␉␉} else {␊ |
767 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
768 | ␉␉␉␉recon[i] = scanline[i];␊ |
769 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
770 | ␉␉␉␉recon[i] = (uint8_t) (scanline[i] + PNG_paethPredictor(recon[i - bytewidth], 0, 0));␊ |
771 | ␉␉}␊ |
772 | ␉␉break;␊ |
773 | ␉default:␊ |
774 | ␉␉PNG_error = 36; // error: nonexistent filter type given␊ |
775 | ␉␉return;␊ |
776 | ␉}␊ |
777 | }␊ |
778 | ␊ |
779 | void PNG_adam7Pass(uint8_t *out, uint8_t *linen, uint8_t *lineo, const uint8_t *in, uint32_t w,␊ |
780 | ␉␉size_t passleft, size_t passtop, size_t spacex, size_t spacey, size_t passw, size_t passh,␊ |
781 | ␉␉uint32_t bpp)␊ |
782 | {␉// filter and reposition the pixels into the output when the image is Adam7 interlaced. This␊ |
783 | ␉// function can only do it after the full image is already decoded. The out buffer must have␊ |
784 | ␉// the correct allocated memory size already.␊ |
785 | ␉if (passw == 0)␊ |
786 | ␉␉return;␊ |
787 | ␉size_t bytewidth = (bpp + 7) / 8, linelength = 1 + ((bpp * passw + 7) / 8);␊ |
788 | ␉uint32_t y;␊ |
789 | ␉for (y = 0; y < passh; y++) {␊ |
790 | ␉␉size_t i, b;␊ |
791 | ␉␉uint8_t filterType = in[y * linelength], *prevline = (y == 0) ? 0 : lineo;␊ |
792 | ␉␉PNG_unFilterScanline(linen, &in[y * linelength + 1], prevline, bytewidth, filterType,␊ |
793 | ␉␉␉␉(w * bpp + 7) / 8);␊ |
794 | ␉␉if (PNG_error)␊ |
795 | ␉␉␉return;␊ |
796 | ␉␉if (bpp >= 8)␊ |
797 | ␉␉␉for (i = 0; i < passw; i++)␊ |
798 | ␉␉␉␉for (b = 0; b < bytewidth; b++) // b = current byte of this pixel␊ |
799 | ␉␉␉␉␉out[bytewidth * w * (passtop + spacey * y) + bytewidth *␊ |
800 | ␉␉␉␉␉␉␉(passleft + spacex * i) + b] = linen[bytewidth * i + b];␊ |
801 | ␉␉else␊ |
802 | ␉␉␉for (i = 0; i < passw; i++) {␊ |
803 | ␉␉␉␉size_t obp, bp;␊ |
804 | ␉␉␉␉obp = bpp * w * (passtop + spacey * y) + bpp * (passleft + spacex * i);␊ |
805 | ␉␉␉␉bp = i * bpp;␊ |
806 | ␉␉␉␉for (b = 0; b < bpp; b++)␊ |
807 | ␉␉␉␉␉PNG_setBitOfReversedStream(&obp, out, PNG_readBitFromReversedStream(&bp, linen));␊ |
808 | ␉␉␉}␊ |
809 | ␉␉uint8_t *temp = linen;␊ |
810 | ␉␉linen = lineo;␊ |
811 | ␉␉lineo = temp; // swap the two buffer pointers "line old" and "line new"␊ |
812 | ␉}␊ |
813 | }␊ |
814 | ␊ |
815 | int PNG_convert(const PNG_info_t *info, vector8_t *out, const uint8_t *in)␊ |
816 | {␉// converts from any color type to 32-bit. return value = LodePNG error code␊ |
817 | ␉size_t i, c;␊ |
818 | ␉uint32_t bitDepth, colorType;␊ |
819 | ␉bitDepth = info->bitDepth;␊ |
820 | ␉colorType = info->colorType;␊ |
821 | ␉size_t numpixels = info->width * info->height, bp = 0;␊ |
822 | ␉vector8_resize(out, numpixels * 4);␊ |
823 | ␉uint8_t *out_data = out->size ? out->data : 0;␊ |
824 | ␉if (bitDepth == 8 && colorType == 0) // greyscale␊ |
825 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
826 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[i];␊ |
827 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && (in[i] == info->key_r)) ? 0 : 255;␊ |
828 | ␉␉}␊ |
829 | ␉else if (bitDepth == 8 && colorType == 2) // RGB color␊ |
830 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
831 | ␉␉␉for (c = 0; c < 3; c++)␊ |
832 | ␉␉␉␉out_data[4 * i + c] = in[3 * i + c];␊ |
833 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && (in[3 * i + 0] == info->key_r) &&␊ |
834 | ␉␉␉␉␉(in[3 * i + 1] == info->key_g) && (in[3 * i + 2] == info->key_b)) ? 0 : 255;␊ |
835 | ␉␉}␊ |
836 | ␉else if (bitDepth == 8 && colorType == 3) // indexed color (palette)␊ |
837 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
838 | ␉␉␉if (4U * in[i] >= info->palette->size)␊ |
839 | ␉␉␉␉return 46;␊ |
840 | ␉␉␉for (c = 0; c < 4; c++) // get rgb colors from the palette␊ |
841 | ␉␉␉␉out_data[4 * i + c] = info->palette->data[4 * in[i] + c];␊ |
842 | ␉␉}␊ |
843 | ␉else if (bitDepth == 8 && colorType == 4) // greyscale with alpha␊ |
844 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
845 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[2 * i + 0];␊ |
846 | ␉␉␉out_data[4 * i + 3] = in[2 * i + 1];␊ |
847 | ␉␉}␊ |
848 | ␉else if (bitDepth == 8 && colorType == 6)␊ |
849 | ␉␉for (i = 0; i < numpixels; i++)␊ |
850 | ␉␉␉for (c = 0; c < 4; c++)␊ |
851 | ␉␉␉␉out_data[4 * i + c] = in[4 * i + c]; // RGB with alpha␊ |
852 | ␉else if (bitDepth == 16 && colorType == 0) // greyscale␊ |
853 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
854 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[2 * i];␊ |
855 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && (256U * in[i] + in[i + 1] == info->key_r))␊ |
856 | ␉␉␉␉␉? 0 : 255;␊ |
857 | ␉␉}␊ |
858 | ␉else if (bitDepth == 16 && colorType == 2) // RGB color␊ |
859 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
860 | ␉␉␉for (c = 0; c < 3; c++)␊ |
861 | ␉␉␉␉out_data[4 * i + c] = in[6 * i + 2 * c];␊ |
862 | ␉␉␉out_data[4 * i + 3] = (info->key_defined &&␊ |
863 | ␉␉␉␉␉(256U * in[6 * i + 0] + in[6 * i + 1] == info->key_r) &&␊ |
864 | ␉␉␉␉␉(256U * in[6 * i + 2] + in[6 * i + 3] == info->key_g) &&␊ |
865 | ␉␉␉␉␉(256U * in[6 * i + 4] + in[6 * i + 5] == info->key_b)) ? 0 : 255;␊ |
866 | ␉␉}␊ |
867 | ␉else if (bitDepth == 16 && colorType == 4) // greyscale with alpha␊ |
868 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
869 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[4 * i]; // msb␊ |
870 | ␉␉␉out_data[4 * i + 3] = in[4 * i + 2];␊ |
871 | ␉␉}␊ |
872 | ␉else if (bitDepth == 16 && colorType == 6)␊ |
873 | ␉␉for (i = 0; i < numpixels; i++)␊ |
874 | ␉␉␉for (c = 0; c < 4; c++)␊ |
875 | ␉␉␉␉out_data[4 * i + c] = in[8 * i + 2 * c]; // RGB with alpha␊ |
876 | ␉else if (bitDepth < 8 && colorType == 0) // greyscale␊ |
877 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
878 | ␉␉␉uint32_t value = (PNG_readBitsFromReversedStream(&bp, in, bitDepth) * 255) /␊ |
879 | ␉␉␉␉␉((1 << bitDepth) - 1); // scale value from 0 to 255␊ |
880 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = (uint8_t) value;␊ |
881 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && value &&␊ |
882 | ␉␉␉␉␉(((1U << bitDepth) - 1U) == info->key_r) && ((1U << bitDepth) - 1U)) ? 0 : 255;␊ |
883 | ␉␉}␊ |
884 | ␉else if (bitDepth < 8 && colorType == 3) // palette␊ |
885 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
886 | ␉␉␉uint32_t value = PNG_readBitsFromReversedStream(&bp, in, bitDepth);␊ |
887 | ␉␉␉if (4 * value >= info->palette->size)␊ |
888 | ␉␉␉␉return 47;␊ |
889 | ␉␉␉for (c = 0; c < 4; c++) // get rgb colors from the palette␊ |
890 | ␉␉␉␉out_data[4 * i + c] = info->palette->data[4 * value + c];␊ |
891 | ␉␉}␊ |
892 | ␉return 0;␊ |
893 | }␊ |
894 | ␊ |
895 | PNG_info_t *PNG_info_new()␊ |
896 | {␊ |
897 | ␉PNG_info_t *info = png_alloc_malloc(sizeof (PNG_info_t));␊ |
898 | ␉uint32_t i;␊ |
899 | ␉for (i = 0; i < sizeof (PNG_info_t); i++)␊ |
900 | ␉␉((uint8_t *) info)[i] = 0;␊ |
901 | ␉info->palette = vector8_new(0, 0);␊ |
902 | ␉info->image = vector8_new(0, 0);␊ |
903 | ␉return info;␊ |
904 | }␊ |
905 | ␊ |
906 | PNG_info_t *PNG_decode(const uint8_t *in, uint32_t size)␊ |
907 | {␊ |
908 | ␉PNG_info_t *info;␊ |
909 | ␉PNG_error = 0;␊ |
910 | ␉if (size == 0 || in == 0) {␊ |
911 | ␉␉PNG_error = 48; // the given data is empty␊ |
912 | ␉␉return NULL;␊ |
913 | ␉}␊ |
914 | ␉info = PNG_info_new();␊ |
915 | ␉PNG_readPngHeader(info, in, size);␊ |
916 | ␉if (PNG_error)␊ |
917 | ␉␉return NULL;␊ |
918 | ␉size_t pos = 33; // first byte of the first chunk after the header␊ |
919 | ␉vector8_t *idat = NULL; // the data from idat chunks␊ |
920 | ␉bool IEND = false, known_type = true;␊ |
921 | ␉info->key_defined = false;␊ |
922 | ␉// loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. IDAT data is␊ |
923 | ␉// put at the start of the in buffer␊ |
924 | ␉while (!IEND) {␊ |
925 | ␉␉size_t i, j;␊ |
926 | ␉␉if (pos + 8 >= size) {␊ |
927 | ␉␉␉PNG_error = 30; // error: size of the in buffer too small to contain next chunk␊ |
928 | ␉␉␉return NULL;␊ |
929 | ␉␉}␊ |
930 | ␉␉size_t chunkLength = PNG_read32bitInt(&in[pos]);␊ |
931 | ␉␉pos += 4;␊ |
932 | ␉␉if (chunkLength > 0x7fffffff) {␊ |
933 | ␉␉␉PNG_error = 63;␊ |
934 | ␉␉␉return NULL;␊ |
935 | ␉␉}␊ |
936 | ␉␉if (pos + chunkLength >= size) {␊ |
937 | ␉␉␉PNG_error = 35; // error: size of the in buffer too small to contain next chunk␊ |
938 | ␉␉␉return NULL;␊ |
939 | ␉␉}␊ |
940 | ␉␉uint32_t chunkType = *(uint32_t *) &in[pos];␊ |
941 | ␉␉if (chunkType == CHUNK_IDAT) { // IDAT: compressed image data chunk␊ |
942 | ␉␉␉size_t offset = 0;␊ |
943 | ␉␉␉if (idat) {␊ |
944 | ␉␉␉␉offset = idat->size;␊ |
945 | ␉␉␉␉vector8_resize(idat, offset + chunkLength);␊ |
946 | ␉␉␉} else␊ |
947 | ␉␉␉␉idat = vector8_new(chunkLength, 0);␊ |
948 | ␉␉␉for (i = 0; i < chunkLength; i++)␊ |
949 | ␉␉␉␉idat->data[offset + i] = in[pos + 4 + i];␊ |
950 | ␉␉␉pos += (4 + chunkLength);␊ |
951 | ␉␉} else if (chunkType == CHUNK_IEND) { // IEND␊ |
952 | ␉␉␉pos += 4;␊ |
953 | ␉␉␉IEND = true;␊ |
954 | ␉␉} else if (chunkType == CHUNK_PLTE) { // PLTE: palette chunk␊ |
955 | ␉␉␉pos += 4; // go after the 4 letters␊ |
956 | ␉␉␉vector8_resize(info->palette, 4 * (chunkLength / 3));␊ |
957 | ␉␉␉if (info->palette->size > (4 * 256)) {␊ |
958 | ␉␉␉␉PNG_error = 38; // error: palette too big␊ |
959 | ␉␉␉␉return NULL;␊ |
960 | ␉␉␉}␊ |
961 | ␉␉␉for (i = 0; i < info->palette->size; i += 4) {␊ |
962 | ␉␉␉␉for (j = 0; j < 3; j++)␊ |
963 | ␉␉␉␉␉info->palette->data[i + j] = in[pos++]; // RGB␊ |
964 | ␉␉␉␉info->palette->data[i + 3] = 255; // alpha␊ |
965 | ␉␉␉}␊ |
966 | ␉␉} else if (chunkType == CHUNK_tRNS) { // tRNS: palette transparency chunk␊ |
967 | ␉␉␉pos += 4; // go after the 4 letters␊ |
968 | ␉␉␉if (info->colorType == 3) {␊ |
969 | ␉␉␉␉if (4 * chunkLength > info->palette->size) {␊ |
970 | ␉␉␉␉␉PNG_error = 39; // error: more alpha values given than there are palette entries␊ |
971 | ␉␉␉␉␉return NULL;␊ |
972 | ␉␉␉␉}␊ |
973 | ␉␉␉␉for (i = 0; i < chunkLength; i++)␊ |
974 | ␉␉␉␉␉info->palette->data[4 * i + 3] = in[pos++];␊ |
975 | ␉␉␉} else if (info->colorType == 0) {␊ |
976 | ␉␉␉␉if (chunkLength != 2) {␊ |
977 | ␉␉␉␉␉PNG_error = 40; // error: this chunk must be 2 bytes for greyscale image␊ |
978 | ␉␉␉␉␉return NULL;␊ |
979 | ␉␉␉␉}␊ |
980 | ␉␉␉␉info->key_defined = true;␊ |
981 | ␉␉␉␉info->key_r = info->key_g = info->key_b = 256 * in[pos] + in[pos + 1];␊ |
982 | ␉␉␉␉pos += 2;␊ |
983 | ␉␉␉} else if (info->colorType == 2) {␊ |
984 | ␉␉␉␉if (chunkLength != 6) {␊ |
985 | ␉␉␉␉␉PNG_error = 41; // error: this chunk must be 6 bytes for RGB image␊ |
986 | ␉␉␉␉␉return NULL;␊ |
987 | ␉␉␉␉}␊ |
988 | ␉␉␉␉info->key_defined = true;␊ |
989 | ␉␉␉␉info->key_r = 256 * in[pos] + in[pos + 1];␊ |
990 | ␉␉␉␉pos += 2;␊ |
991 | ␉␉␉␉info->key_g = 256 * in[pos] + in[pos + 1];␊ |
992 | ␉␉␉␉pos += 2;␊ |
993 | ␉␉␉␉info->key_b = 256 * in[pos] + in[pos + 1];␊ |
994 | ␉␉␉␉pos += 2;␊ |
995 | ␉␉␉} else {␊ |
996 | ␉␉␉␉PNG_error = 42; // error: tRNS chunk not allowed for other color models␊ |
997 | ␉␉␉␉return NULL;␊ |
998 | ␉␉␉}␊ |
999 | ␉␉} else { // it's not an implemented chunk type, so ignore it: skip over the data␊ |
1000 | ␉␉␉if (!(in[pos + 0] & 32)) {␊ |
1001 | ␉␉␉␉// error: unknown critical chunk (5th bit of first byte of chunk type is 0)␊ |
1002 | ␉␉␉␉PNG_error = 69;␊ |
1003 | ␉␉␉␉return NULL;␊ |
1004 | ␉␉␉}␊ |
1005 | ␉␉␉pos += (chunkLength + 4); // skip 4 letters and uninterpreted data of unimplemented chunk␊ |
1006 | ␉␉␉known_type = false;␊ |
1007 | ␉␉}␊ |
1008 | ␉␉pos += 4; // step over CRC (which is ignored)␊ |
1009 | ␉}␊ |
1010 | ␉uint32_t bpp = PNG_getBpp(info);␊ |
1011 | ␉vector8_t *scanlines; // now the out buffer will be filled␊ |
1012 | ␉scanlines = vector8_new(((info->width * (info->height * bpp + 7)) / 8) + info->height, 0);␊ |
1013 | ␉PNG_error = Zlib_decompress(scanlines, idat);␊ |
1014 | ␉if (PNG_error)␊ |
1015 | ␉␉return NULL; // stop if the zlib decompressor returned an error␊ |
1016 | ␉size_t bytewidth = (bpp + 7) / 8, outlength = (info->height * info->width * bpp + 7) / 8;␊ |
1017 | ␉vector8_resize(info->image, outlength); // time to fill the out buffer␊ |
1018 | ␉uint8_t *out_data = outlength ? info->image->data : 0;␊ |
1019 | ␉if (info->interlaceMethod == 0) { // no interlace, just filter␊ |
1020 | ␉␉size_t y, obp, bp;␊ |
1021 | ␉␉size_t linestart, linelength;␊ |
1022 | ␉␉linestart = 0;␊ |
1023 | ␉␉// length in bytes of a scanline, excluding the filtertype byte␊ |
1024 | ␉␉linelength = (info->width * bpp + 7) / 8;␊ |
1025 | ␉␉if (bpp >= 8) // byte per byte␊ |
1026 | ␉␉␉for (y = 0; y < info->height; y++) {␊ |
1027 | ␉␉␉␉uint32_t filterType = scanlines->data[linestart];␊ |
1028 | ␉␉␉␉const uint8_t *prevline;␊ |
1029 | ␉␉␉␉prevline = (y == 0) ? 0 : &out_data[(y - 1) * info->width * bytewidth];␊ |
1030 | ␉␉␉␉PNG_unFilterScanline(&out_data[linestart - y], &scanlines->data[linestart + 1],␊ |
1031 | ␉␉␉␉␉␉prevline, bytewidth, filterType, linelength);␊ |
1032 | ␉␉␉␉if (PNG_error)␊ |
1033 | ␉␉␉␉␉return NULL;␊ |
1034 | ␉␉␉␉linestart += (1 + linelength); // go to start of next scanline␊ |
1035 | ␉␉} else { // less than 8 bits per pixel, so fill it up bit per bit␊ |
1036 | ␉␉␉vector8_t *templine; // only used if bpp < 8␊ |
1037 | ␉␉␉templine = vector8_new((info->width * bpp + 7) >> 3, 0);␊ |
1038 | ␉␉␉for (y = 0, obp = 0; y < info->height; y++) {␊ |
1039 | ␉␉␉␉uint32_t filterType = scanlines->data[linestart];␊ |
1040 | ␉␉␉␉const uint8_t *prevline;␊ |
1041 | ␉␉␉␉prevline = (y == 0) ? 0 : &out_data[(y - 1) * info->width * bytewidth];␊ |
1042 | ␉␉␉␉PNG_unFilterScanline(templine->data, &scanlines->data[linestart + 1], prevline,␊ |
1043 | ␉␉␉␉␉␉bytewidth, filterType, linelength);␊ |
1044 | ␉␉␉␉if (PNG_error)␊ |
1045 | ␉␉␉␉␉return NULL;␊ |
1046 | ␉␉␉␉for (bp = 0; bp < info->width * bpp;)␊ |
1047 | ␉␉␉␉␉PNG_setBitOfReversedStream(&obp, out_data, PNG_readBitFromReversedStream(&bp,␊ |
1048 | ␉␉␉␉␉␉␉templine->data));␊ |
1049 | ␉␉␉␉linestart += (1 + linelength); // go to start of next scanline␊ |
1050 | ␉␉␉}␊ |
1051 | ␉␉}␊ |
1052 | ␉} else { // interlaceMethod is 1 (Adam7)␊ |
1053 | ␉␉int i;␊ |
1054 | ␉␉size_t passw[7] = {␊ |
1055 | ␉␉␉(info->width + 7) / 8, (info->width + 3) / 8, (info->width + 3) / 4,␊ |
1056 | ␉␉␉(info->width + 1) / 4, (info->width + 1) / 2, (info->width + 0) / 2,␊ |
1057 | ␉␉␉(info->width + 0) / 1␊ |
1058 | ␉␉};␊ |
1059 | ␉␉size_t passh[7] = {␊ |
1060 | ␉␉␉(info->height + 7) / 8, (info->height + 7) / 8, (info->height + 3) / 8,␊ |
1061 | ␉␉␉(info->height + 3) / 4, (info->height + 1) / 4, (info->height + 1) / 2,␊ |
1062 | ␉␉␉(info->height + 0) / 2␊ |
1063 | ␉␉};␊ |
1064 | ␉␉size_t passstart[7] = { 0 };␊ |
1065 | ␉␉size_t pattern[28] = { 0, 4, 0, 2, 0, 1, 0, 0, 0, 4, 0, 2, 0, 1, 8, 8, 4, 4, 2, 2, 1, 8, 8,␊ |
1066 | ␉␉␉␉8, 4, 4, 2, 2 }; // values for the adam7 passes␊ |
1067 | ␉␉for (i = 0; i < 6; i++)␊ |
1068 | ␉␉␉passstart[i + 1] = passstart[i] + passh[i] * ((passw[i] ? 1 : 0) + (passw[i] * bpp + 7) / 8);␊ |
1069 | ␉␉vector8_t *scanlineo, *scanlinen; // "old" and "new" scanline␊ |
1070 | ␉␉scanlineo = vector8_new((info->width * bpp + 7) / 8, 0);␊ |
1071 | ␉␉scanlinen = vector8_new((info->width * bpp + 7) / 8, 0);␊ |
1072 | ␉␉for (i = 0; i < 7; i++)␊ |
1073 | ␉␉␉PNG_adam7Pass(out_data, scanlinen->data, scanlineo->data, &scanlines->data[passstart[i]],␊ |
1074 | ␉␉␉␉␉info->width, pattern[i], pattern[i + 7], pattern[i + 14], pattern[i + 21],␊ |
1075 | ␉␉␉␉␉passw[i], passh[i], bpp);␊ |
1076 | ␉}␊ |
1077 | ␉if (info->colorType != 6 || info->bitDepth != 8) { // conversion needed␊ |
1078 | ␉␉vector8_t *copy = vector8_copy(info->image); // xxx: is this copy necessary?␊ |
1079 | ␉␉PNG_error = PNG_convert(info, info->image, copy->data);␊ |
1080 | ␉}␊ |
1081 | ␉return info;␊ |
1082 | }␊ |
1083 | ␊ |
1084 | /*************************************************************************************************/␊ |
1085 | ␊ |
1086 | #ifdef TEST␊ |
1087 | ␊ |
1088 | #include <stdio.h>␊ |
1089 | #include <sys/stat.h>␊ |
1090 | ␊ |
1091 | int main(int argc, char **argv)␊ |
1092 | {␊ |
1093 | ␉char *fname = (argc > 1) ? argv[1] : "test.png";␊ |
1094 | ␉PNG_info_t *info;␊ |
1095 | ␉struct stat statbuf;␊ |
1096 | ␉uint32_t insize, outsize;␊ |
1097 | ␉FILE *infp, *outfp;␊ |
1098 | ␉uint8_t *inbuf;␊ |
1099 | ␉uint32_t n;␊ |
1100 | ␊ |
1101 | ␉if (stat(fname, &statbuf) != 0) {␊ |
1102 | ␉␉perror("stat");␊ |
1103 | ␉␉return 1;␊ |
1104 | ␉} else if (!statbuf.st_size) {␊ |
1105 | ␉␉printf("file empty\n");␊ |
1106 | ␉␉return 1;␊ |
1107 | ␉}␊ |
1108 | ␉insize = (uint32_t) statbuf.st_size;␊ |
1109 | ␉inbuf = malloc(insize);␊ |
1110 | ␉infp = fopen(fname, "rb");␊ |
1111 | ␉if (!infp) {␊ |
1112 | ␉␉perror("fopen");␊ |
1113 | ␉␉return 1;␊ |
1114 | ␉} else if (fread(inbuf, 1, insize, infp) != insize) {␊ |
1115 | ␉␉perror("fread");␊ |
1116 | ␉␉return 1;␊ |
1117 | ␉}␊ |
1118 | ␉fclose(infp);␊ |
1119 | ␊ |
1120 | ␉printf("input file: %s (size: %d)\n", fname, insize);␊ |
1121 | ␊ |
1122 | ␉info = PNG_decode(inbuf, insize);␊ |
1123 | ␉free(inbuf);␊ |
1124 | ␉printf("PNG_error: %d\n", PNG_error);␊ |
1125 | ␉if (PNG_error != 0)␊ |
1126 | ␉␉return 1;␊ |
1127 | ␊ |
1128 | ␉printf("width: %d, height: %d\nfirst 16 bytes: ", info->width, info->height);␊ |
1129 | ␉for (n = 0; n < 16; n++)␊ |
1130 | ␉␉printf("%02x ", info->image->data[n]);␊ |
1131 | ␉printf("\n");␊ |
1132 | ␊ |
1133 | ␉outsize = info->width * info->height * 4;␊ |
1134 | ␉printf("image size: %d\n", outsize);␊ |
1135 | ␉if (outsize != info->image->size) {␊ |
1136 | ␉␉printf("error: image size doesn't match dimensions\n");␊ |
1137 | ␉␉return 1;␊ |
1138 | ␉}␊ |
1139 | ␉outfp = fopen("out.bin", "wb");␊ |
1140 | ␉if (!outfp) {␊ |
1141 | ␉␉perror("fopen");␊ |
1142 | ␉␉return 1;␊ |
1143 | ␉} else if (fwrite(info->image->data, 1, outsize, outfp) != outsize) {␊ |
1144 | ␉␉perror("fwrite");␊ |
1145 | ␉␉return 1;␊ |
1146 | ␉}␊ |
1147 | ␉fclose(outfp);␊ |
1148 | ␊ |
1149 | #ifdef ALLOC_DEBUG␊ |
1150 | ␉png_alloc_node_t *node;␊ |
1151 | ␉for (node = png_alloc_head, n = 1; node; node = node->next, n++)␊ |
1152 | ␉␉printf("node %d (%p) addr = %p, size = %ld\n", n, node, node->addr, node->size);␊ |
1153 | #endif␊ |
1154 | ␉png_alloc_free_all(); // also frees info and image data from PNG_decode␊ |
1155 | ␊ |
1156 | ␉return 0;␊ |
1157 | }␊ |
1158 | ␊ |
1159 | #endif␊ |
1160 | ␊ |
1161 | ␊ |
1162 | int loadPngImage(const char *filename, uint16_t *width, uint16_t *height,␊ |
1163 | ␉␉␉␉ uint8_t **imageData)␊ |
1164 | {␊ |
1165 | uint8_t *pngData = NULL;␊ |
1166 | int pngFile = -1/*0*/, pngSize;␊ |
1167 | PNG_info_t *info;␊ |
1168 | int error = 0;␊ |
1169 | ␉␊ |
1170 | //pngFile = open_bvdev("bt(0,0)", filename);␊ |
1171 | ␉pngFile = open(filename);␊ |
1172 | ␉//if (pngFile == -1) {␊ |
1173 | if (pngFile < 0) {␊ |
1174 | error = -1;␊ |
1175 | goto failed;␊ |
1176 | }␊ |
1177 | pngSize = file_size(pngFile);␊ |
1178 | if (!pngSize) {␊ |
1179 | error = -1;␊ |
1180 | goto failed;␊ |
1181 | }␊ |
1182 | pngData = malloc(pngSize);␊ |
1183 | if (read(pngFile, (char *) pngData, pngSize) != pngSize) {␊ |
1184 | error = -1;␊ |
1185 | goto failed;␊ |
1186 | }␊ |
1187 | ␉␊ |
1188 | PNG_error = -1;␊ |
1189 | info = PNG_decode(pngData, pngSize);␊ |
1190 | if (PNG_error != 0) {␊ |
1191 | error = PNG_error;␊ |
1192 | goto failed;␊ |
1193 | } else if ((info->width > 0xffff) || (info->height > 0xffff)) {␊ |
1194 | error = -1;␊ |
1195 | goto failed;␊ |
1196 | } else if ((info->width * info->height * 4) != info->image->size) {␊ |
1197 | error = -1;␊ |
1198 | goto failed;␊ |
1199 | }␊ |
1200 | ␉uint8_t *result = malloc(info->width*4*info->height);␊ |
1201 | *width = info->width;␊ |
1202 | *height = info->height;␊ |
1203 | ␉memcpy(result, info->image->data, info->width*4*info->height);␊ |
1204 | ␉*imageData = result;␊ |
1205 | ␉␊ |
1206 | failed:␊ |
1207 | ␉png_alloc_free_all();␊ |
1208 | if (pngData)␊ |
1209 | free(pngData);␊ |
1210 | if (pngFile != -1)␊ |
1211 | close(pngFile);␊ |
1212 | ␉␊ |
1213 | return error;␊ |
1214 | }␊ |
1215 | ␊ |
1216 | int loadEmbeddedPngImage(uint8_t *pngData, int pngSize, uint16_t *width, uint16_t *height, uint8_t **imageData) {␊ |
1217 | PNG_info_t *info;␊ |
1218 | int error = 0;␊ |
1219 | ␉␊ |
1220 | PNG_error = -1;␊ |
1221 | info = PNG_decode(pngData, pngSize);␊ |
1222 | ␉if (PNG_error != 0) {␊ |
1223 | error = PNG_error;␊ |
1224 | goto failed;␊ |
1225 | } else if ((info->width > 0xffff) || (info->height > 0xffff)) {␊ |
1226 | error = -1;␊ |
1227 | goto failed;␊ |
1228 | } else if ((info->width * info->height * 4) != info->image->size) {␊ |
1229 | error = -1;␊ |
1230 | goto failed;␊ |
1231 | }␊ |
1232 | ␉uint8_t *result = malloc(info->width*4*info->height);␊ |
1233 | ␉*width = info->width;␊ |
1234 | *height = info->height;␊ |
1235 | ␉memcpy(result, info->image->data, info->width*4*info->height);␊ |
1236 | ␉*imageData = result;␊ |
1237 | ␉␊ |
1238 | failed:␊ |
1239 | ␉png_alloc_free_all();␊ |
1240 | ␉␊ |
1241 | return error;␊ |
1242 | }␊ |
1243 | ␊ |
1244 | |