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