1 | // picoPNG version 20080503 (cleaned up and ported to c by kaitek)␊ |
2 | // Copyright (c) 2005-2008 Lode Vandevenne␊ |
3 | //␊ |
4 | // This software is provided 'as-is', without any express or implied␊ |
5 | // warranty. In no event will the authors be held liable for any damages␊ |
6 | // arising from the use of this software.␊ |
7 | //␊ |
8 | // Permission is granted to anyone to use this software for any purpose,␊ |
9 | // including commercial applications, and to alter it and redistribute it␊ |
10 | // freely, subject to the following restrictions:␊ |
11 | //␊ |
12 | // 1. The origin of this software must not be misrepresented; you must not␊ |
13 | // claim that you wrote the original software. If you use this software␊ |
14 | // in a product, an acknowledgment in the product documentation would be␊ |
15 | // appreciated but is not required.␊ |
16 | // 2. Altered source versions must be plainly marked as such, and must not be␊ |
17 | // misrepresented as being the original software.␊ |
18 | // 3. This notice may not be removed or altered from any source distribution.␊ |
19 | ␊ |
20 | #include <sys/types.h>␊ |
21 | #include "libsa.h"␊ |
22 | #include "picopng.h"␊ |
23 | ␊ |
24 | /*************************************************************************************************/␊ |
25 | ␊ |
26 | typedef struct png_alloc_node {␊ |
27 | ␉struct png_alloc_node *prev, *next;␊ |
28 | ␉void *addr;␊ |
29 | ␉size_t size;␊ |
30 | } png_alloc_node_t;␊ |
31 | ␊ |
32 | png_alloc_node_t *png_alloc_head = NULL;␊ |
33 | png_alloc_node_t *png_alloc_tail = NULL;␊ |
34 | ␊ |
35 | png_alloc_node_t *png_alloc_find_node(void *addr)␊ |
36 | {␊ |
37 | ␉png_alloc_node_t *node;␊ |
38 | ␉for (node = png_alloc_head; node; node = node->next)␊ |
39 | ␉␉if (node->addr == addr)␊ |
40 | ␉␉␉break;␊ |
41 | ␉return node;␊ |
42 | }␊ |
43 | ␊ |
44 | void png_alloc_add_node(void *addr, size_t size)␊ |
45 | {␊ |
46 | ␉png_alloc_node_t *node;␊ |
47 | ␉if (png_alloc_find_node(addr))␊ |
48 | ␉␉return;␊ |
49 | ␉node = malloc(sizeof (png_alloc_node_t));␊ |
50 | ␉node->addr = addr;␊ |
51 | ␉node->size = size;␊ |
52 | ␉node->prev = png_alloc_tail;␊ |
53 | ␉node->next = NULL;␊ |
54 | ␉png_alloc_tail = node;␊ |
55 | ␉if (node->prev)␊ |
56 | ␉␉node->prev->next = node;␊ |
57 | ␉if (!png_alloc_head)␊ |
58 | ␉␉png_alloc_head = node;␊ |
59 | }␊ |
60 | ␊ |
61 | void png_alloc_remove_node(png_alloc_node_t *node)␊ |
62 | {␊ |
63 | ␉if (!node)␊ |
64 | ␉{␊ |
65 | ␉␉return;␊ |
66 | ␉}␊ |
67 | ␉if (node->prev)␊ |
68 | ␉␉node->prev->next = node->next;␊ |
69 | ␉if (node->next)␊ |
70 | ␉␉node->next->prev = node->prev;␊ |
71 | ␉if (node == png_alloc_head)␊ |
72 | ␉␉png_alloc_head = node->next;␊ |
73 | ␉if (node == png_alloc_tail)␊ |
74 | ␉␉png_alloc_tail = node->prev;␊ |
75 | ␉node->prev = node->next = node->addr = NULL;␊ |
76 | ␉free(node);␊ |
77 | }␊ |
78 | ␊ |
79 | void *png_alloc_malloc(size_t size)␊ |
80 | {␊ |
81 | ␉void *addr = malloc(size);␊ |
82 | ␉png_alloc_add_node(addr, size);␊ |
83 | ␉return addr;␊ |
84 | }␊ |
85 | ␊ |
86 | void *png_alloc_realloc(void *addr, size_t size)␊ |
87 | {␊ |
88 | ␉void *new_addr;␊ |
89 | ␉if (!addr)␊ |
90 | ␉␉return png_alloc_malloc(size);␊ |
91 | ␉new_addr = realloc(addr, size);␊ |
92 | ␉if (new_addr && (new_addr != addr)) {␊ |
93 | ␉␉png_alloc_node_t *old_node;␊ |
94 | ␉␉old_node = png_alloc_find_node(addr);␊ |
95 | ␉␉png_alloc_remove_node(old_node);␊ |
96 | ␉␉png_alloc_add_node(new_addr, size);␊ |
97 | ␉}␊ |
98 | ␉return new_addr;␊ |
99 | }␊ |
100 | ␊ |
101 | void png_alloc_free(void *addr)␊ |
102 | {␊ |
103 | if (!addr) return;␊ |
104 | ␊ |
105 | ␉png_alloc_node_t *node = png_alloc_find_node(addr);␊ |
106 | ␉if (node)␊ |
107 | ␉␉png_alloc_remove_node(node);␊ |
108 | ␊ |
109 | ␉free(addr);␊ |
110 | }␊ |
111 | ␊ |
112 | void png_alloc_free_all()␊ |
113 | {␊ |
114 | ␉while (png_alloc_tail) {␊ |
115 | ␉␉void *addr = png_alloc_tail->addr;␊ |
116 | ␉␉png_alloc_remove_node(png_alloc_tail);␊ |
117 | ␉␉free(addr);␊ |
118 | ␉}␊ |
119 | }␊ |
120 | ␊ |
121 | /*************************************************************************************************/␊ |
122 | ␊ |
123 | __unused void vector32_cleanup(vector32_t *p)␊ |
124 | {␊ |
125 | ␉p->size = p->allocsize = 0;␊ |
126 | ␉if (p->data)␊ |
127 | ␉␉png_alloc_free(p->data);␊ |
128 | ␉p->data = NULL;␊ |
129 | }␊ |
130 | ␊ |
131 | uint32_t vector32_resize(vector32_t *p, size_t size)␊ |
132 | {␉// returns 1 if success, 0 if failure ==> nothing done␊ |
133 | ␉if (size * sizeof (uint32_t) > p->allocsize) {␊ |
134 | ␉␉size_t newsize = size * sizeof (uint32_t) * 2;␊ |
135 | ␉␉void *data = png_alloc_realloc(p->data, newsize);␊ |
136 | ␉␉if (data) {␊ |
137 | ␉␉␉p->allocsize = newsize;␊ |
138 | ␉␉␉p->data = (uint32_t *) data;␊ |
139 | ␉␉␉p->size = size;␊ |
140 | ␉␉} else␊ |
141 | ␉␉␉return 0;␊ |
142 | ␉} else␊ |
143 | ␉␉p->size = size;␊ |
144 | ␉return 1;␊ |
145 | }␊ |
146 | ␊ |
147 | uint32_t vector32_resizev(vector32_t *p, size_t size, uint32_t value)␊ |
148 | {␉// resize and give all new elements the value␊ |
149 | ␉size_t oldsize = p->size, i;␊ |
150 | ␉if (!vector32_resize(p, size))␊ |
151 | ␉␉return 0;␊ |
152 | ␉for (i = oldsize; i < size; i++)␊ |
153 | ␉␉p->data[i] = value;␊ |
154 | ␉return 1;␊ |
155 | }␊ |
156 | ␊ |
157 | void vector32_init(vector32_t *p)␊ |
158 | {␊ |
159 | ␉p->data = NULL;␊ |
160 | ␉p->size = p->allocsize = 0;␊ |
161 | }␊ |
162 | ␊ |
163 | vector32_t *vector32_new(size_t size, uint32_t value)␊ |
164 | {␊ |
165 | ␉vector32_t *p = png_alloc_malloc(sizeof (vector32_t));␊ |
166 | ␉if (!p)␊ |
167 | ␉{␊ |
168 | ␉␉return NULL;␊ |
169 | ␉}␊ |
170 | ␉vector32_init(p);␊ |
171 | ␉if (size && !vector32_resizev(p, size, value))␊ |
172 | ␉{␊ |
173 | ␉␉vector32_cleanup(p);␊ |
174 | ␉␉png_alloc_free(p);␊ |
175 | ␉␉return NULL;␊ |
176 | ␉}␊ |
177 | ␉return p;␊ |
178 | }␊ |
179 | ␊ |
180 | /*************************************************************************************************/␊ |
181 | ␊ |
182 | __unused void vector8_cleanup(vector8_t *p)␊ |
183 | {␊ |
184 | ␉p->size = p->allocsize = 0;␊ |
185 | ␉if (p->data)␊ |
186 | ␉␉png_alloc_free(p->data);␊ |
187 | ␉p->data = NULL;␊ |
188 | }␊ |
189 | ␊ |
190 | uint32_t vector8_resize(vector8_t *p, size_t size)␊ |
191 | {␉// returns 1 if success, 0 if failure ==> nothing done␊ |
192 | ␉// xxx: the use of sizeof uint32_t here seems like a bug (this descends from the lodepng vector␊ |
193 | ␉// compatibility functions which do the same). without this there is corruption in certain cases,␊ |
194 | ␉// so this was probably done to cover up allocation bug(s) in the original picopng code!␊ |
195 | ␉if (size * sizeof (uint32_t) > p->allocsize) {␊ |
196 | ␉␉size_t newsize = size * sizeof (uint32_t) * 2;␊ |
197 | ␉␉void *data = png_alloc_realloc(p->data, newsize);␊ |
198 | ␉␉if (data) {␊ |
199 | ␉␉␉p->allocsize = newsize;␊ |
200 | ␉␉␉p->data = (uint8_t *) data;␊ |
201 | ␉␉␉p->size = size;␊ |
202 | ␉␉} else␊ |
203 | ␉␉␉return 0; // error: not enough memory␊ |
204 | ␉} else␊ |
205 | ␉␉p->size = size;␊ |
206 | ␉return 1;␊ |
207 | }␊ |
208 | ␊ |
209 | uint32_t vector8_resizev(vector8_t *p, size_t size, uint8_t value)␊ |
210 | {␉// resize and give all new elements the value␊ |
211 | ␉size_t oldsize = p->size, i;␊ |
212 | ␉if (!vector8_resize(p, size))␊ |
213 | ␉␉return 0;␊ |
214 | ␉for (i = oldsize; i < size; i++)␊ |
215 | ␉␉p->data[i] = value;␊ |
216 | ␉return 1;␊ |
217 | }␊ |
218 | ␊ |
219 | void vector8_init(vector8_t *p)␊ |
220 | {␊ |
221 | ␉p->data = NULL;␊ |
222 | ␉p->size = p->allocsize = 0;␊ |
223 | }␊ |
224 | ␊ |
225 | vector8_t *vector8_new(size_t size, uint8_t value)␊ |
226 | {␊ |
227 | ␉vector8_t *p = png_alloc_malloc(sizeof (vector8_t));␊ |
228 | ␉if(!p)␊ |
229 | ␉{␊ |
230 | ␉␉return NULL;␊ |
231 | ␉}␊ |
232 | ␉vector8_init(p);␊ |
233 | ␉if (size && !vector8_resizev(p, size, value))␊ |
234 | ␉{␊ |
235 | ␉␉vector8_cleanup(p);␊ |
236 | ␉␉png_alloc_free(p);␊ |
237 | ␉␉return NULL;␊ |
238 | ␉}␊ |
239 | ␉return p;␊ |
240 | }␊ |
241 | ␊ |
242 | vector8_t *vector8_copy(vector8_t *p)␊ |
243 | {␊ |
244 | ␉vector8_t *q = vector8_new(p->size, 0);␊ |
245 | ␉uint32_t n;␊ |
246 | ␉if (!q)␊ |
247 | ␉{␊ |
248 | ␉return NULL;␊ |
249 | ␉}␊ |
250 | ␉for (n = 0; n < q->size; n++)␊ |
251 | ␉␉q->data[n] = p->data[n];␊ |
252 | ␉return q;␊ |
253 | }␊ |
254 | ␊ |
255 | /*************************************************************************************************/␊ |
256 | ␊ |
257 | const uint32_t LENBASE[29] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51,␊ |
258 | ␉59, 67, 83, 99, 115, 131, 163, 195, 227, 258 };␊ |
259 | 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,␊ |
260 | ␉4, 5, 5, 5, 5, 0 };␊ |
261 | const uint32_t DISTBASE[30] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,␊ |
262 | ␉513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 };␊ |
263 | 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,␊ |
264 | ␉10, 10, 11, 11, 12, 12, 13, 13 };␊ |
265 | // code length code lengths␊ |
266 | const uint32_t CLCL[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };␊ |
267 | ␊ |
268 | /*************************************************************************************************/␊ |
269 | ␊ |
270 | typedef struct {␊ |
271 | ␉// 2D representation of a huffman tree: The one dimension is "0" or "1", the other contains all␊ |
272 | ␉// nodes and leaves of the tree.␊ |
273 | ␉vector32_t *tree2d;␊ |
274 | } HuffmanTree;␊ |
275 | ␊ |
276 | HuffmanTree *HuffmanTree_new()␊ |
277 | {␊ |
278 | ␉HuffmanTree *tree = png_alloc_malloc(sizeof (HuffmanTree));␊ |
279 | ␉if (!tree)␊ |
280 | ␉{␊ |
281 | ␉␉return NULL;␊ |
282 | ␉}␊ |
283 | ␉tree->tree2d = NULL;␊ |
284 | ␉return tree;␊ |
285 | }␊ |
286 | ␊ |
287 | int HuffmanTree_makeFromLengths(HuffmanTree *tree, const vector32_t *bitlen, uint32_t maxbitlen)␊ |
288 | {␉// make tree given the lengths␊ |
289 | ␉uint32_t bits, n, i;␊ |
290 | ␉uint32_t numcodes = (uint32_t) bitlen->size, treepos = 0, nodefilled = 0;␊ |
291 | ␉vector32_t *tree1d, *blcount, *nextcode;␊ |
292 | ␉tree1d = vector32_new(numcodes, 0);␊ |
293 | ␉blcount = vector32_new(maxbitlen + 1, 0);␊ |
294 | ␉nextcode = vector32_new(maxbitlen + 1, 0);␊ |
295 | ␉if (!tree1d || !blcount || !nextcode || !nextcode->data)␊ |
296 | ␉{␊ |
297 | ␉␉goto error;␊ |
298 | ␉}␊ |
299 | ␉for (bits = 0; bits < numcodes; bits++)␊ |
300 | ␉␉blcount->data[bitlen->data[bits]]++; // count number of instances of each code length␊ |
301 | ␉for (bits = 1; bits <= maxbitlen; bits++)␊ |
302 | ␉␉nextcode->data[bits] = (nextcode->data[bits - 1] + blcount->data[bits - 1]) << 1;␊ |
303 | ␉for (n = 0; n < numcodes; n++)␊ |
304 | ␉␉if (bitlen->data[n] != 0)␊ |
305 | ␉␉␉tree1d->data[n] = nextcode->data[bitlen->data[n]]++; // generate all the codes␊ |
306 | ␉// 0x7fff here means the tree2d isn't filled there yet␊ |
307 | ␉vector32_t *tree2d = vector32_new(numcodes * 2, 0x7fff);␊ |
308 | ␉tree->tree2d = tree2d;␊ |
309 | ␉for (n = 0; n < numcodes; n++) // the codes␊ |
310 | ␉␉for (i = 0; i < bitlen->data[n]; i++) { // the bits for this code␊ |
311 | ␉␉␉uint32_t bit = (tree1d->data[n] >> (bitlen->data[n] - i - 1)) & 1;␊ |
312 | ␉␉␉if (treepos > numcodes - 2)␊ |
313 | ␉␉␉␉return 55;␊ |
314 | ␉␉␉if (tree2d->data[2 * treepos + bit] == 0x7fff) { // not yet filled in␊ |
315 | ␉␉␉␉if (i + 1 == bitlen->data[n]) { // last bit␊ |
316 | ␉␉␉␉␉tree2d->data[2 * treepos + bit] = n;␊ |
317 | ␉␉␉␉␉treepos = 0;␊ |
318 | ␉␉␉␉} else { // addresses are encoded as values > numcodes␊ |
319 | ␉␉␉␉␉tree2d->data[2 * treepos + bit] = ++nodefilled + numcodes;␊ |
320 | ␉␉␉␉␉treepos = nodefilled;␊ |
321 | ␉␉␉␉}␊ |
322 | ␉␉␉} else // subtract numcodes from address to get address value␊ |
323 | ␉␉␉␉treepos = tree2d->data[2 * treepos + bit] - numcodes;␊ |
324 | ␉␉}␊ |
325 | ␉return 0;␊ |
326 | error:␊ |
327 | ␉if (tree1d)␊ |
328 | ␉{␊ |
329 | ␉␉vector32_cleanup(tree1d);␊ |
330 | ␉␉png_alloc_free(tree1d);␊ |
331 | ␉}␊ |
332 | ␉if (blcount)␊ |
333 | ␉{␊ |
334 | ␉␉vector32_cleanup(blcount);␊ |
335 | ␉␉png_alloc_free(blcount);␊ |
336 | ␉}␊ |
337 | ␉if (nextcode)␊ |
338 | ␉{␊ |
339 | ␉␉vector32_cleanup(nextcode);␊ |
340 | ␉␉png_alloc_free(nextcode);␊ |
341 | ␉}␊ |
342 | ␉return 1;␊ |
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 | {␊ |
662 | ␉uint32_t result = (bits[*bitp >> 3] >> (7 - (*bitp & 0x7))) & 1;␊ |
663 | ␉(*bitp)++;␊ |
664 | ␉return result;␊ |
665 | }␊ |
666 | ␊ |
667 | uint32_t PNG_readBitsFromReversedStream(size_t *bitp, const uint8_t *bits, uint32_t nbits)␊ |
668 | {␊ |
669 | ␉uint32_t i, result = 0;␊ |
670 | ␉for (i = nbits - 1; i < nbits; i--)␊ |
671 | ␉␉result += ((PNG_readBitFromReversedStream(bitp, bits)) << i);␊ |
672 | ␉return result;␊ |
673 | }␊ |
674 | ␊ |
675 | void PNG_setBitOfReversedStream(size_t *bitp, uint8_t *bits, uint32_t bit)␊ |
676 | {␊ |
677 | ␉bits[*bitp >> 3] |= (bit << (7 - (*bitp & 0x7)));␊ |
678 | ␉(*bitp)++;␊ |
679 | }␊ |
680 | ␊ |
681 | uint32_t PNG_read32bitInt(const uint8_t *buffer)␊ |
682 | {␊ |
683 | ␉return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];␊ |
684 | }␊ |
685 | ␊ |
686 | int PNG_checkColorValidity(uint32_t colorType, uint32_t bd) // return type is a LodePNG error code␊ |
687 | {␊ |
688 | ␉if ((colorType == 2 || colorType == 4 || colorType == 6)) {␊ |
689 | ␉␉if (!(bd == 8 || bd == 16))␊ |
690 | ␉␉␉return 37;␊ |
691 | ␉␉else␊ |
692 | ␉␉␉return 0;␊ |
693 | ␉} else if (colorType == 0) {␊ |
694 | ␉␉if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16))␊ |
695 | ␉␉␉return 37;␊ |
696 | ␉␉else␊ |
697 | ␉␉␉return 0;␊ |
698 | ␉} else if (colorType == 3) {␊ |
699 | ␉␉if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8))␊ |
700 | ␉␉␉return 37;␊ |
701 | ␉␉else␊ |
702 | ␉␉␉return 0;␊ |
703 | ␉} else␊ |
704 | ␉␉return 31; // nonexistent color type␊ |
705 | }␊ |
706 | ␊ |
707 | uint32_t PNG_getBpp(const PNG_info_t *info)␊ |
708 | {␊ |
709 | ␉uint32_t bitDepth, colorType;␊ |
710 | ␉bitDepth = info->bitDepth;␊ |
711 | ␉colorType = info->colorType;␊ |
712 | ␉if (colorType == 2)␊ |
713 | ␉␉return (3 * bitDepth);␊ |
714 | ␉else if (colorType >= 4)␊ |
715 | ␉␉return (colorType - 2) * bitDepth;␊ |
716 | ␉else␊ |
717 | ␉␉return bitDepth;␊ |
718 | }␊ |
719 | ␊ |
720 | void PNG_readPngHeader(PNG_info_t *info, const uint8_t *in, size_t inlength)␊ |
721 | {␉// read the information from the header and store it in the Info␊ |
722 | ␉if (inlength < 29) {␊ |
723 | ␉␉PNG_error = 27; // error: the data length is smaller than the length of the header␊ |
724 | ␉␉return;␊ |
725 | ␉}␊ |
726 | ␉if (*(uint64_t *) in != PNG_SIGNATURE) {␊ |
727 | ␉␉PNG_error = 28; // no PNG signature␊ |
728 | ␉␉return;␊ |
729 | ␉}␊ |
730 | ␉if (*(uint32_t *) &in[12] != CHUNK_IHDR) {␊ |
731 | ␉␉PNG_error = 29; // error: it doesn't start with a IHDR chunk!␊ |
732 | ␉␉return;␊ |
733 | ␉}␊ |
734 | ␉info->width = PNG_read32bitInt(&in[16]);␊ |
735 | ␉info->height = PNG_read32bitInt(&in[20]);␊ |
736 | ␉info->bitDepth = in[24];␊ |
737 | ␉info->colorType = in[25];␊ |
738 | ␉info->compressionMethod = in[26];␊ |
739 | ␉if (in[26] != 0) {␊ |
740 | ␉␉PNG_error = 32; // error: only compression method 0 is allowed in the specification␊ |
741 | ␉␉return;␊ |
742 | ␉}␊ |
743 | ␉info->filterMethod = in[27];␊ |
744 | ␉if (in[27] != 0) {␊ |
745 | ␉␉PNG_error = 33; // error: only filter method 0 is allowed in the specification␊ |
746 | ␉␉return;␊ |
747 | ␉}␊ |
748 | ␉info->interlaceMethod = in[28];␊ |
749 | ␉if (in[28] > 1) {␊ |
750 | ␉␉PNG_error = 34; // error: only interlace methods 0 and 1 exist in the specification␊ |
751 | ␉␉return;␊ |
752 | ␉}␊ |
753 | ␉PNG_error = PNG_checkColorValidity(info->colorType, info->bitDepth);␊ |
754 | }␊ |
755 | ␊ |
756 | int PNG_paethPredictor(int a, int b, int c) // Paeth predicter, used by PNG filter type 4␊ |
757 | {␊ |
758 | ␉int p, pa, pb, pc;␊ |
759 | ␉p = a + b - c;␊ |
760 | ␉pa = p > a ? (p - a) : (a - p);␊ |
761 | ␉pb = p > b ? (p - b) : (b - p);␊ |
762 | ␉pc = p > c ? (p - c) : (c - p);␊ |
763 | ␉return (pa <= pb && pa <= pc) ? a : (pb <= pc ? b : c);␊ |
764 | }␊ |
765 | ␊ |
766 | void PNG_unFilterScanline(uint8_t *recon, const uint8_t *scanline, const uint8_t *precon,␊ |
767 | ␉␉size_t bytewidth, uint32_t filterType, size_t length)␊ |
768 | {␊ |
769 | ␉size_t i;␊ |
770 | ␉switch (filterType) {␊ |
771 | ␉case 0:␊ |
772 | ␉␉for (i = 0; i < length; i++)␊ |
773 | ␉␉␉recon[i] = scanline[i];␊ |
774 | ␉␉break;␊ |
775 | ␉case 1:␊ |
776 | ␉␉for (i = 0; i < bytewidth; i++)␊ |
777 | ␉␉␉recon[i] = scanline[i];␊ |
778 | ␉␉for (i = bytewidth; i < length; i++)␊ |
779 | ␉␉␉recon[i] = scanline[i] + recon[i - bytewidth];␊ |
780 | ␉␉break;␊ |
781 | ␉case 2:␊ |
782 | ␉␉if (precon)␊ |
783 | ␉␉␉for (i = 0; i < length; i++)␊ |
784 | ␉␉␉␉recon[i] = scanline[i] + precon[i];␊ |
785 | ␉␉else␊ |
786 | ␉␉␉for (i = 0; i < length; i++)␊ |
787 | ␉␉␉␉recon[i] = scanline[i];␊ |
788 | ␉␉break;␊ |
789 | ␉case 3:␊ |
790 | ␉␉if (precon) {␊ |
791 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
792 | ␉␉␉␉recon[i] = scanline[i] + precon[i] / 2;␊ |
793 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
794 | ␉␉␉␉recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);␊ |
795 | ␉␉} else {␊ |
796 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
797 | ␉␉␉␉recon[i] = scanline[i];␊ |
798 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
799 | ␉␉␉␉recon[i] = scanline[i] + recon[i - bytewidth] / 2;␊ |
800 | ␉␉}␊ |
801 | ␉␉break;␊ |
802 | ␉case 4:␊ |
803 | ␉␉if (precon) {␊ |
804 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
805 | ␉␉␉␉recon[i] = (uint8_t) (scanline[i] + PNG_paethPredictor(0, precon[i], 0));␊ |
806 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
807 | ␉␉␉␉recon[i] = (uint8_t) (scanline[i] + PNG_paethPredictor(recon[i - bytewidth],␊ |
808 | ␉␉␉␉␉␉precon[i], precon[i - bytewidth]));␊ |
809 | ␉␉} else {␊ |
810 | ␉␉␉for (i = 0; i < bytewidth; i++)␊ |
811 | ␉␉␉␉recon[i] = scanline[i];␊ |
812 | ␉␉␉for (i = bytewidth; i < length; i++)␊ |
813 | ␉␉␉␉recon[i] = (uint8_t) (scanline[i] + PNG_paethPredictor(recon[i - bytewidth], 0, 0));␊ |
814 | ␉␉}␊ |
815 | ␉␉break;␊ |
816 | ␉default:␊ |
817 | ␉␉PNG_error = 36; // error: nonexistent filter type given␊ |
818 | ␉␉return;␊ |
819 | ␉}␊ |
820 | }␊ |
821 | ␊ |
822 | void PNG_adam7Pass(uint8_t *out, uint8_t *linen, uint8_t *lineo, const uint8_t *in, uint32_t w,␊ |
823 | ␉␉size_t passleft, size_t passtop, size_t spacex, size_t spacey, size_t passw, size_t passh,␊ |
824 | ␉␉uint32_t bpp)␊ |
825 | {␉// filter and reposition the pixels into the output when the image is Adam7 interlaced. This␊ |
826 | ␉// function can only do it after the full image is already decoded. The out buffer must have␊ |
827 | ␉// the correct allocated memory size already.␊ |
828 | ␉if (passw == 0)␊ |
829 | ␉␉return;␊ |
830 | ␉size_t bytewidth = (bpp + 7) / 8, linelength = 1 + ((bpp * passw + 7) / 8);␊ |
831 | ␉uint32_t y;␊ |
832 | ␉for (y = 0; y < passh; y++) {␊ |
833 | ␉␉size_t i, b;␊ |
834 | ␉␉uint8_t filterType = in[y * linelength], *prevline = (y == 0) ? 0 : lineo;␊ |
835 | ␉␉PNG_unFilterScanline(linen, &in[y * linelength + 1], prevline, bytewidth, filterType,␊ |
836 | ␉␉␉␉(w * bpp + 7) / 8);␊ |
837 | ␉␉if (PNG_error)␊ |
838 | ␉␉␉return;␊ |
839 | ␉␉if (bpp >= 8)␊ |
840 | ␉␉␉for (i = 0; i < passw; i++)␊ |
841 | ␉␉␉␉for (b = 0; b < bytewidth; b++) // b = current byte of this pixel␊ |
842 | ␉␉␉␉␉out[bytewidth * w * (passtop + spacey * y) + bytewidth *␊ |
843 | ␉␉␉␉␉␉␉(passleft + spacex * i) + b] = linen[bytewidth * i + b];␊ |
844 | ␉␉else␊ |
845 | ␉␉␉for (i = 0; i < passw; i++) {␊ |
846 | ␉␉␉␉size_t obp, bp;␊ |
847 | ␉␉␉␉obp = bpp * w * (passtop + spacey * y) + bpp * (passleft + spacex * i);␊ |
848 | ␉␉␉␉bp = i * bpp;␊ |
849 | ␉␉␉␉for (b = 0; b < bpp; b++)␊ |
850 | ␉␉␉␉␉PNG_setBitOfReversedStream(&obp, out, PNG_readBitFromReversedStream(&bp, linen));␊ |
851 | ␉␉␉}␊ |
852 | ␉␉uint8_t *temp = linen;␊ |
853 | ␉␉linen = lineo;␊ |
854 | ␉␉lineo = temp; // swap the two buffer pointers "line old" and "line new"␊ |
855 | ␉}␊ |
856 | }␊ |
857 | ␊ |
858 | int PNG_convert(const PNG_info_t *info, vector8_t *out, const uint8_t *in)␊ |
859 | {␉// converts from any color type to 32-bit. return value = LodePNG error code␊ |
860 | ␉size_t i, c;␊ |
861 | ␉uint32_t bitDepth, colorType;␊ |
862 | ␉bitDepth = info->bitDepth;␊ |
863 | ␉colorType = info->colorType;␊ |
864 | ␉size_t numpixels = info->width * info->height, bp = 0;␊ |
865 | ␉vector8_resize(out, numpixels * 4);␊ |
866 | ␉uint8_t *out_data = out->size ? out->data : 0;␊ |
867 | ␉if (bitDepth == 8 && colorType == 0) // greyscale␊ |
868 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
869 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[i];␊ |
870 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && (in[i] == info->key_r)) ? 0 : 255;␊ |
871 | ␉␉}␊ |
872 | ␉else if (bitDepth == 8 && colorType == 2) // RGB color␊ |
873 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
874 | ␉␉␉for (c = 0; c < 3; c++)␊ |
875 | ␉␉␉␉out_data[4 * i + c] = in[3 * i + c];␊ |
876 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && (in[3 * i + 0] == info->key_r) &&␊ |
877 | ␉␉␉␉␉(in[3 * i + 1] == info->key_g) && (in[3 * i + 2] == info->key_b)) ? 0 : 255;␊ |
878 | ␉␉}␊ |
879 | ␉else if (bitDepth == 8 && colorType == 3) // indexed color (palette)␊ |
880 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
881 | ␉␉␉if (4U * in[i] >= info->palette->size)␊ |
882 | ␉␉␉␉return 46;␊ |
883 | ␉␉␉for (c = 0; c < 4; c++) // get rgb colors from the palette␊ |
884 | ␉␉␉␉out_data[4 * i + c] = info->palette->data[4 * in[i] + c];␊ |
885 | ␉␉}␊ |
886 | ␉else if (bitDepth == 8 && colorType == 4) // greyscale with alpha␊ |
887 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
888 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[2 * i + 0];␊ |
889 | ␉␉␉out_data[4 * i + 3] = in[2 * i + 1];␊ |
890 | ␉␉}␊ |
891 | ␉else if (bitDepth == 8 && colorType == 6)␊ |
892 | ␉␉for (i = 0; i < numpixels; i++)␊ |
893 | ␉␉␉for (c = 0; c < 4; c++)␊ |
894 | ␉␉␉␉out_data[4 * i + c] = in[4 * i + c]; // RGB with alpha␊ |
895 | ␉else if (bitDepth == 16 && colorType == 0) // greyscale␊ |
896 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
897 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[2 * i];␊ |
898 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && (256U * in[i] + in[i + 1] == info->key_r))␊ |
899 | ␉␉␉␉␉? 0 : 255;␊ |
900 | ␉␉}␊ |
901 | ␉else if (bitDepth == 16 && colorType == 2) // RGB color␊ |
902 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
903 | ␉␉␉for (c = 0; c < 3; c++)␊ |
904 | ␉␉␉␉out_data[4 * i + c] = in[6 * i + 2 * c];␊ |
905 | ␉␉␉out_data[4 * i + 3] = (info->key_defined &&␊ |
906 | ␉␉␉␉␉(256U * in[6 * i + 0] + in[6 * i + 1] == info->key_r) &&␊ |
907 | ␉␉␉␉␉(256U * in[6 * i + 2] + in[6 * i + 3] == info->key_g) &&␊ |
908 | ␉␉␉␉␉(256U * in[6 * i + 4] + in[6 * i + 5] == info->key_b)) ? 0 : 255;␊ |
909 | ␉␉}␊ |
910 | ␉else if (bitDepth == 16 && colorType == 4) // greyscale with alpha␊ |
911 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
912 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = in[4 * i]; // msb␊ |
913 | ␉␉␉out_data[4 * i + 3] = in[4 * i + 2];␊ |
914 | ␉␉}␊ |
915 | ␉else if (bitDepth == 16 && colorType == 6)␊ |
916 | ␉␉for (i = 0; i < numpixels; i++)␊ |
917 | ␉␉␉for (c = 0; c < 4; c++)␊ |
918 | ␉␉␉␉out_data[4 * i + c] = in[8 * i + 2 * c]; // RGB with alpha␊ |
919 | ␉else if (bitDepth < 8 && colorType == 0) // greyscale␊ |
920 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
921 | ␉␉␉uint32_t value = (PNG_readBitsFromReversedStream(&bp, in, bitDepth) * 255) /␊ |
922 | ␉␉␉␉␉((1 << bitDepth) - 1); // scale value from 0 to 255␊ |
923 | ␉␉␉out_data[4 * i + 0] = out_data[4 * i + 1] = out_data[4 * i + 2] = (uint8_t) value;␊ |
924 | ␉␉␉out_data[4 * i + 3] = (info->key_defined && value &&␊ |
925 | ␉␉␉␉␉(((1U << bitDepth) - 1U) == info->key_r) && ((1U << bitDepth) - 1U)) ? 0 : 255;␊ |
926 | ␉␉}␊ |
927 | ␉else if (bitDepth < 8 && colorType == 3) // palette␊ |
928 | ␉␉for (i = 0; i < numpixels; i++) {␊ |
929 | ␉␉␉uint32_t value = PNG_readBitsFromReversedStream(&bp, in, bitDepth);␊ |
930 | ␉␉␉if (4 * value >= info->palette->size)␊ |
931 | ␉␉␉␉return 47;␊ |
932 | ␉␉␉for (c = 0; c < 4; c++) // get rgb colors from the palette␊ |
933 | ␉␉␉␉out_data[4 * i + c] = info->palette->data[4 * value + c];␊ |
934 | ␉␉}␊ |
935 | ␉return 0;␊ |
936 | }␊ |
937 | ␊ |
938 | PNG_info_t *PNG_info_new()␊ |
939 | {␊ |
940 | ␉PNG_info_t *info = png_alloc_malloc(sizeof (PNG_info_t));␊ |
941 | ␉if (!info)␊ |
942 | ␉{␊ |
943 | ␉␉return NULL;␊ |
944 | ␉}␊ |
945 | ␉uint32_t i;␊ |
946 | ␉for (i = 0; i < sizeof (PNG_info_t); i++)␊ |
947 | ␉␉((uint8_t *) info)[i] = 0;␊ |
948 | ␉info->palette = vector8_new(0, 0);␊ |
949 | ␉info->image = vector8_new(0, 0);␊ |
950 | ␉return info;␊ |
951 | }␊ |
952 | ␊ |
953 | PNG_info_t *PNG_decode(const uint8_t *in, uint32_t size)␊ |
954 | {␊ |
955 | ␉PNG_info_t *info;␊ |
956 | ␉PNG_error = 0;␊ |
957 | ␉if (size == 0 || in == 0) {␊ |
958 | ␉␉PNG_error = 48; // the given data is empty␊ |
959 | ␉␉return NULL;␊ |
960 | ␉}␊ |
961 | ␉info = PNG_info_new();␊ |
962 | ␉PNG_readPngHeader(info, in, size);␊ |
963 | ␉if (PNG_error)␊ |
964 | ␉␉return NULL;␊ |
965 | ␉size_t pos = 33; // first byte of the first chunk after the header␊ |
966 | ␉vector8_t *idat = NULL; // the data from idat chunks␊ |
967 | ␉bool IEND = false, known_type = true;␊ |
968 | ␉info->key_defined = false;␊ |
969 | ␉// loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. IDAT data is␊ |
970 | ␉// put at the start of the in buffer␊ |
971 | ␉while (!IEND) {␊ |
972 | ␉␉size_t i, j;␊ |
973 | ␉␉if (pos + 8 >= size) {␊ |
974 | ␉␉␉PNG_error = 30; // error: size of the in buffer too small to contain next chunk␊ |
975 | ␉␉␉return NULL;␊ |
976 | ␉␉}␊ |
977 | ␉␉size_t chunkLength = PNG_read32bitInt(&in[pos]);␊ |
978 | ␉␉pos += 4;␊ |
979 | ␉␉if (chunkLength > 0x7fffffff) {␊ |
980 | ␉␉␉PNG_error = 63;␊ |
981 | ␉␉␉return NULL;␊ |
982 | ␉␉}␊ |
983 | ␉␉if (pos + chunkLength >= size) {␊ |
984 | ␉␉␉PNG_error = 35; // error: size of the in buffer too small to contain next chunk␊ |
985 | ␉␉␉return NULL;␊ |
986 | ␉␉}␊ |
987 | ␉␉uint32_t chunkType = *(uint32_t *) &in[pos];␊ |
988 | ␉␉if (chunkType == CHUNK_IDAT) { // IDAT: compressed image data chunk␊ |
989 | ␉␉␉size_t offset = 0;␊ |
990 | ␉␉␉if (idat) {␊ |
991 | ␉␉␉␉offset = idat->size;␊ |
992 | ␉␉␉␉vector8_resize(idat, offset + chunkLength);␊ |
993 | ␉␉␉} else␊ |
994 | ␉␉␉␉idat = vector8_new(chunkLength, 0);␊ |
995 | ␊ |
996 | ␉␉␉if (!idat)␊ |
997 | ␉␉␉{␊ |
998 | ␉␉␉␉PNG_error = 1;␊ |
999 | ␉␉␉␉return NULL;␊ |
1000 | ␉␉␉}␊ |
1001 | ␉␉␉for (i = 0; i < chunkLength; i++)␊ |
1002 | ␉␉␉␉idat->data[offset + i] = in[pos + 4 + i];␊ |
1003 | ␉␉␉pos += (4 + chunkLength);␊ |
1004 | ␉␉} else if (chunkType == CHUNK_IEND) { // IEND␊ |
1005 | ␉␉␉pos += 4;␊ |
1006 | ␉␉␉IEND = true;␊ |
1007 | ␉␉} else if (chunkType == CHUNK_PLTE) { // PLTE: palette chunk␊ |
1008 | ␉␉␉pos += 4; // go after the 4 letters␊ |
1009 | ␉␉␉vector8_resize(info->palette, 4 * (chunkLength / 3));␊ |
1010 | ␉␉␉if (info->palette->size > (4 * 256)) {␊ |
1011 | ␉␉␉␉PNG_error = 38; // error: palette too big␊ |
1012 | ␉␉␉␉return NULL;␊ |
1013 | ␉␉␉}␊ |
1014 | ␉␉␉for (i = 0; i < info->palette->size; i += 4) {␊ |
1015 | ␉␉␉␉for (j = 0; j < 3; j++)␊ |
1016 | ␉␉␉␉␉info->palette->data[i + j] = in[pos++]; // RGB␊ |
1017 | ␉␉␉␉info->palette->data[i + 3] = 255; // alpha␊ |
1018 | ␉␉␉}␊ |
1019 | ␉␉} else if (chunkType == CHUNK_tRNS) { // tRNS: palette transparency chunk␊ |
1020 | ␉␉␉pos += 4; // go after the 4 letters␊ |
1021 | ␉␉␉if (info->colorType == 3) {␊ |
1022 | ␉␉␉␉if (4 * chunkLength > info->palette->size) {␊ |
1023 | ␉␉␉␉␉PNG_error = 39; // error: more alpha values given than there are palette entries␊ |
1024 | ␉␉␉␉␉return NULL;␊ |
1025 | ␉␉␉␉}␊ |
1026 | ␉␉␉␉for (i = 0; i < chunkLength; i++)␊ |
1027 | ␉␉␉␉␉info->palette->data[4 * i + 3] = in[pos++];␊ |
1028 | ␉␉␉} else if (info->colorType == 0) {␊ |
1029 | ␉␉␉␉if (chunkLength != 2) {␊ |
1030 | ␉␉␉␉␉PNG_error = 40; // error: this chunk must be 2 bytes for greyscale image␊ |
1031 | ␉␉␉␉␉return NULL;␊ |
1032 | ␉␉␉␉}␊ |
1033 | ␉␉␉␉info->key_defined = true;␊ |
1034 | ␉␉␉␉info->key_r = info->key_g = info->key_b = 256 * in[pos] + in[pos + 1];␊ |
1035 | ␉␉␉␉pos += 2;␊ |
1036 | ␉␉␉} else if (info->colorType == 2) {␊ |
1037 | ␉␉␉␉if (chunkLength != 6) {␊ |
1038 | ␉␉␉␉␉PNG_error = 41; // error: this chunk must be 6 bytes for RGB image␊ |
1039 | ␉␉␉␉␉return NULL;␊ |
1040 | ␉␉␉␉}␊ |
1041 | ␉␉␉␉info->key_defined = true;␊ |
1042 | ␉␉␉␉info->key_r = 256 * in[pos] + in[pos + 1];␊ |
1043 | ␉␉␉␉pos += 2;␊ |
1044 | ␉␉␉␉info->key_g = 256 * in[pos] + in[pos + 1];␊ |
1045 | ␉␉␉␉pos += 2;␊ |
1046 | ␉␉␉␉info->key_b = 256 * in[pos] + in[pos + 1];␊ |
1047 | ␉␉␉␉pos += 2;␊ |
1048 | ␉␉␉} else {␊ |
1049 | ␉␉␉␉PNG_error = 42; // error: tRNS chunk not allowed for other color models␊ |
1050 | ␉␉␉␉return NULL;␊ |
1051 | ␉␉␉}␊ |
1052 | ␉␉} else { // it's not an implemented chunk type, so ignore it: skip over the data␊ |
1053 | ␉␉␉if (!(in[pos + 0] & 32)) {␊ |
1054 | ␉␉␉␉// error: unknown critical chunk (5th bit of first byte of chunk type is 0)␊ |
1055 | ␉␉␉␉PNG_error = 69;␊ |
1056 | ␉␉␉␉return NULL;␊ |
1057 | ␉␉␉}␊ |
1058 | ␉␉␉pos += (chunkLength + 4); // skip 4 letters and uninterpreted data of unimplemented chunk␊ |
1059 | ␉␉␉known_type = false;␊ |
1060 | ␉␉}␊ |
1061 | ␉␉pos += 4; // step over CRC (which is ignored)␊ |
1062 | ␉}␊ |
1063 | ␉uint32_t bpp = PNG_getBpp(info);␊ |
1064 | ␉vector8_t *scanlines; // now the out buffer will be filled␊ |
1065 | ␉scanlines = vector8_new(((info->width * (info->height * bpp + 7)) / 8) + info->height, 0);␊ |
1066 | ␉if (!scanlines)␊ |
1067 | ␉{␊ |
1068 | ␉␉PNG_error = 1;␊ |
1069 | ␉␉return NULL;␊ |
1070 | ␉}␊ |
1071 | ␉PNG_error = Zlib_decompress(scanlines, idat);␊ |
1072 | ␉if (PNG_error)␊ |
1073 | ␉␉return NULL; // stop if the zlib decompressor returned an error␊ |
1074 | ␉size_t bytewidth = (bpp + 7) / 8, outlength = (info->height * info->width * bpp + 7) / 8;␊ |
1075 | ␉vector8_resize(info->image, outlength); // time to fill the out buffer␊ |
1076 | ␉uint8_t *out_data = outlength ? info->image->data : 0;␊ |
1077 | ␉if (info->interlaceMethod == 0) { // no interlace, just filter␊ |
1078 | ␉␉size_t y, obp, bp;␊ |
1079 | ␉␉size_t linestart, linelength;␊ |
1080 | ␉␉linestart = 0;␊ |
1081 | ␉␉// length in bytes of a scanline, excluding the filtertype byte␊ |
1082 | ␉␉linelength = (info->width * bpp + 7) / 8;␊ |
1083 | ␉␉if (bpp >= 8) // byte per byte␊ |
1084 | ␉␉␉for (y = 0; y < info->height; y++) {␊ |
1085 | ␉␉␉␉uint32_t filterType = scanlines->data[linestart];␊ |
1086 | ␉␉␉␉const uint8_t *prevline;␊ |
1087 | ␉␉␉␉prevline = (y == 0) ? 0 : &out_data[(y - 1) * info->width * bytewidth];␊ |
1088 | ␉␉␉␉PNG_unFilterScanline(&out_data[linestart - y], &scanlines->data[linestart + 1],␊ |
1089 | ␉␉␉␉␉␉prevline, bytewidth, filterType, linelength);␊ |
1090 | ␉␉␉␉if (PNG_error)␊ |
1091 | ␉␉␉␉␉return NULL;␊ |
1092 | ␉␉␉␉linestart += (1 + linelength); // go to start of next scanline␊ |
1093 | ␉␉} else { // less than 8 bits per pixel, so fill it up bit per bit␊ |
1094 | ␉␉␉vector8_t *templine; // only used if bpp < 8␊ |
1095 | ␉␉␉templine = vector8_new((info->width * bpp + 7) >> 3, 0);␊ |
1096 | ␉␉␉for (y = 0, obp = 0; y < info->height; y++) {␊ |
1097 | ␉␉␉␉uint32_t filterType = scanlines->data[linestart];␊ |
1098 | ␉␉␉␉const uint8_t *prevline;␊ |
1099 | ␉␉␉␉prevline = (y == 0) ? 0 : &out_data[(y - 1) * info->width * bytewidth];␊ |
1100 | ␉␉␉␉PNG_unFilterScanline(templine->data, &scanlines->data[linestart + 1], prevline,␊ |
1101 | ␉␉␉␉␉␉bytewidth, filterType, linelength);␊ |
1102 | ␉␉␉␉if (PNG_error)␊ |
1103 | ␉␉␉␉␉return NULL;␊ |
1104 | ␉␉␉␉for (bp = 0; bp < info->width * bpp;)␊ |
1105 | ␉␉␉␉␉PNG_setBitOfReversedStream(&obp, out_data, PNG_readBitFromReversedStream(&bp,␊ |
1106 | ␉␉␉␉␉␉␉templine->data));␊ |
1107 | ␉␉␉␉linestart += (1 + linelength); // go to start of next scanline␊ |
1108 | ␉␉␉}␊ |
1109 | ␉␉}␊ |
1110 | ␉} else { // interlaceMethod is 1 (Adam7)␊ |
1111 | ␉␉int i;␊ |
1112 | ␉␉size_t passw[7] = {␊ |
1113 | ␉␉␉(info->width + 7) / 8, (info->width + 3) / 8, (info->width + 3) / 4,␊ |
1114 | ␉␉␉(info->width + 1) / 4, (info->width + 1) / 2, (info->width + 0) / 2,␊ |
1115 | ␉␉␉(info->width + 0) / 1␊ |
1116 | ␉␉};␊ |
1117 | ␉␉size_t passh[7] = {␊ |
1118 | ␉␉␉(info->height + 7) / 8, (info->height + 7) / 8, (info->height + 3) / 8,␊ |
1119 | ␉␉␉(info->height + 3) / 4, (info->height + 1) / 4, (info->height + 1) / 2,␊ |
1120 | ␉␉␉(info->height + 0) / 2␊ |
1121 | ␉␉};␊ |
1122 | ␉␉size_t passstart[7] = { 0 };␊ |
1123 | ␉␉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,␊ |
1124 | ␉␉␉␉8, 4, 4, 2, 2 }; // values for the adam7 passes␊ |
1125 | ␉␉for (i = 0; i < 6; i++)␊ |
1126 | ␉␉␉passstart[i + 1] = passstart[i] + passh[i] * ((passw[i] ? 1 : 0) + (passw[i] * bpp + 7) / 8);␊ |
1127 | ␉␉vector8_t *scanlineo, *scanlinen; // "old" and "new" scanline␊ |
1128 | ␉␉scanlineo = vector8_new((info->width * bpp + 7) / 8, 0);␊ |
1129 | ␉␉scanlinen = vector8_new((info->width * bpp + 7) / 8, 0);␊ |
1130 | ␉␉for (i = 0; i < 7; i++)␊ |
1131 | ␉␉␉PNG_adam7Pass(out_data, scanlinen->data, scanlineo->data, &scanlines->data[passstart[i]],␊ |
1132 | ␉␉␉␉␉info->width, pattern[i], pattern[i + 7], pattern[i + 14], pattern[i + 21],␊ |
1133 | ␉␉␉␉␉passw[i], passh[i], bpp);␊ |
1134 | ␉}␊ |
1135 | ␉if (info->colorType != 6 || info->bitDepth != 8) { // conversion needed␊ |
1136 | ␉␉vector8_t *copy = vector8_copy(info->image); // xxx: is this copy necessary?␊ |
1137 | if (!copy)␊ |
1138 | ␉{␊ |
1139 | return NULL;␊ |
1140 | }␊ |
1141 | ␉␉PNG_error = PNG_convert(info, info->image, copy->data);␊ |
1142 | ␉␉if (PNG_error)␊ |
1143 | ␉␉{␊ |
1144 | ␉␉␉return NULL;␊ |
1145 | ␉ }␊ |
1146 | ␉}␊ |
1147 | ␉return info;␊ |
1148 | }␊ |
1149 | ␊ |
1150 | /*************************************************************************************************/␊ |
1151 | ␊ |
1152 | #ifdef TEST␊ |
1153 | ␊ |
1154 | #include <stdio.h>␊ |
1155 | #include <sys/stat.h>␊ |
1156 | ␊ |
1157 | int main(int argc, char **argv)␊ |
1158 | {␊ |
1159 | ␉char *fname = (argc > 1) ? argv[1] : "test.png";␊ |
1160 | ␉PNG_info_t *info;␊ |
1161 | ␉struct stat statbuf;␊ |
1162 | ␉uint32_t insize, outsize;␊ |
1163 | ␉FILE *infp, *outfp;␊ |
1164 | ␉uint8_t *inbuf;␊ |
1165 | ␉uint32_t n;␊ |
1166 | ␊ |
1167 | ␉if (stat(fname, &statbuf) != 0) {␊ |
1168 | ␉␉perror("stat");␊ |
1169 | ␉␉return 1;␊ |
1170 | ␉} else if (!statbuf.st_size) {␊ |
1171 | ␉␉printf("file empty\n");␊ |
1172 | ␉␉return 1;␊ |
1173 | ␉}␊ |
1174 | ␉insize = (uint32_t) statbuf.st_size;␊ |
1175 | ␉inbuf = malloc(insize);␊ |
1176 | ␉infp = fopen(fname, "rb");␊ |
1177 | ␉if (!infp) {␊ |
1178 | ␉␉perror("fopen");␊ |
1179 | ␉␉return 1;␊ |
1180 | ␉} else if (fread(inbuf, 1, insize, infp) != insize) {␊ |
1181 | ␉␉perror("fread");␊ |
1182 | ␉␉return 1;␊ |
1183 | ␉}␊ |
1184 | ␉fclose(infp);␊ |
1185 | ␊ |
1186 | ␉printf("input file: %s (size: %d)\n", fname, insize);␊ |
1187 | ␊ |
1188 | ␉info = PNG_decode(inbuf, insize);␊ |
1189 | ␉free(inbuf);␊ |
1190 | ␉printf("PNG_error: %d\n", PNG_error);␊ |
1191 | ␉if (PNG_error != 0)␊ |
1192 | ␉␉return 1;␊ |
1193 | ␊ |
1194 | ␉printf("width: %d, height: %d\nfirst 16 bytes: ", info->width, info->height);␊ |
1195 | ␉for (n = 0; n < 16; n++)␊ |
1196 | ␉␉printf("%02x ", info->image->data[n]);␊ |
1197 | ␉printf("\n");␊ |
1198 | ␊ |
1199 | ␉outsize = info->width * info->height * 4;␊ |
1200 | ␉printf("image size: %d\n", outsize);␊ |
1201 | ␉if (outsize != info->image->size) {␊ |
1202 | ␉␉printf("error: image size doesn't match dimensions\n");␊ |
1203 | ␉␉return 1;␊ |
1204 | ␉}␊ |
1205 | ␉outfp = fopen("out.bin", "wb");␊ |
1206 | ␉if (!outfp) {␊ |
1207 | ␉␉perror("fopen");␊ |
1208 | ␉␉return 1;␊ |
1209 | ␉} else if (fwrite(info->image->data, 1, outsize, outfp) != outsize) {␊ |
1210 | ␉␉perror("fwrite");␊ |
1211 | ␉␉return 1;␊ |
1212 | ␉}␊ |
1213 | ␉fclose(outfp);␊ |
1214 | ␊ |
1215 | #ifdef ALLOC_DEBUG␊ |
1216 | ␉png_alloc_node_t *node;␊ |
1217 | ␉for (node = png_alloc_head, n = 1; node; node = node->next, n++)␊ |
1218 | ␉␉printf("node %d (%p) addr = %p, size = %ld\n", n, node, node->addr, node->size);␊ |
1219 | #endif␊ |
1220 | ␉png_alloc_free_all(); // also frees info and image data from PNG_decode␊ |
1221 | ␊ |
1222 | ␉return 0;␊ |
1223 | }␊ |
1224 | ␊ |
1225 | #endif␊ |
1226 | |