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