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