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