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