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