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