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