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