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