1 /*
2 LodePNG version 20160418
3
4 Copyright (c) 2005-2016 Lode Vandevenne
5
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
9
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
13
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21
22 3. This notice may not be removed or altered from any source
23 distribution.
24 */
25
26 /*
27 The manual and changelog are in the header file "lodepng.h"
28 Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for C.
29 */
30
31 #include "lodepng.h"
32
33 #include <limits.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/
38 #pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requires too much casts*/
39 #pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
40 #endif /*_MSC_VER */
41
42 const char* LODEPNG_VERSION_STRING = "20160418";
43
44 /*
45 This source file is built up in the following large parts. The code sections
46 with the "LODEPNG_COMPILE_" #defines divide this up further in an intermixed way.
47 -Tools for C and common code for PNG and Zlib
48 -C Code for Zlib (huffman, deflate, ...)
49 -C Code for PNG (file format chunks, adam7, PNG filters, color conversions, ...)
50 -The C++ wrapper around all of the above
51 */
52
53 /*The malloc, realloc and free functions defined here with "lodepng_" in front
54 of the name, so that you can easily change them to others related to your
55 platform if needed. Everything else in the code calls these. Pass
56 -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler, or comment out
57 #define LODEPNG_COMPILE_ALLOCATORS in the header, to disable the ones here and
58 define them in your own project's source files without needing to change
59 lodepng source code. Don't forget to remove "static" if you copypaste them
60 from here.*/
61
62 #ifdef LODEPNG_COMPILE_ALLOCATORS
lodepng_malloc(size_t size)63 static void* lodepng_malloc(size_t size)
64 {
65 return malloc(size);
66 }
67
lodepng_realloc(void * ptr,size_t new_size)68 static void* lodepng_realloc(void* ptr, size_t new_size)
69 {
70 return realloc(ptr, new_size);
71 }
72
lodepng_free(void * ptr)73 static void lodepng_free(void* ptr)
74 {
75 free(ptr);
76 }
77 #else /*LODEPNG_COMPILE_ALLOCATORS*/
78 void* lodepng_malloc(size_t size);
79 void* lodepng_realloc(void* ptr, size_t new_size);
80 void lodepng_free(void* ptr);
81 #endif /*LODEPNG_COMPILE_ALLOCATORS*/
82
83 /* ////////////////////////////////////////////////////////////////////////// */
84 /* ////////////////////////////////////////////////////////////////////////// */
85 /* // Tools for C, and common code for PNG and Zlib. // */
86 /* ////////////////////////////////////////////////////////////////////////// */
87 /* ////////////////////////////////////////////////////////////////////////// */
88
89 /*
90 Often in case of an error a value is assigned to a variable and then it breaks
91 out of a loop (to go to the cleanup phase of a function). This macro does that.
92 It makes the error handling code shorter and more readable.
93
94 Example: if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83);
95 */
96 #define CERROR_BREAK(errorvar, code)\
97 {\
98 errorvar = code;\
99 break;\
100 }
101
102 /*version of CERROR_BREAK that assumes the common case where the error variable is named "error"*/
103 #define ERROR_BREAK(code) CERROR_BREAK(error, code)
104
105 /*Set error var to the error code, and return it.*/
106 #define CERROR_RETURN_ERROR(errorvar, code)\
107 {\
108 errorvar = code;\
109 return code;\
110 }
111
112 /*Try the code, if it returns error, also return the error.*/
113 #define CERROR_TRY_RETURN(call)\
114 {\
115 unsigned error = call;\
116 if(error) return error;\
117 }
118
119 /*Set error var to the error code, and return from the void function.*/
120 #define CERROR_RETURN(errorvar, code)\
121 {\
122 errorvar = code;\
123 return;\
124 }
125
126 /*
127 About uivector, ucvector and string:
128 -All of them wrap dynamic arrays or text strings in a similar way.
129 -LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the C++ version.
130 -The string tools are made to avoid problems with compilers that declare things like strncat as deprecated.
131 -They're not used in the interface, only internally in this file as static functions.
132 -As with many other structs in this file, the init and cleanup functions serve as ctor and dtor.
133 */
134
135 #ifdef LODEPNG_COMPILE_ZLIB
136 /*dynamic vector of unsigned ints*/
137 typedef struct uivector
138 {
139 unsigned* data;
140 size_t size; /*size in number of unsigned longs*/
141 size_t allocsize; /*allocated size in bytes*/
142 } uivector;
143
uivector_cleanup(void * p)144 static void uivector_cleanup(void* p)
145 {
146 ((uivector*)p)->size = ((uivector*)p)->allocsize = 0;
147 lodepng_free(((uivector*)p)->data);
148 ((uivector*)p)->data = NULL;
149 }
150
151 /*returns 1 if success, 0 if failure ==> nothing done*/
uivector_reserve(uivector * p,size_t allocsize)152 static unsigned uivector_reserve(uivector* p, size_t allocsize)
153 {
154 if(allocsize > p->allocsize)
155 {
156 size_t newsize = (allocsize > p->allocsize * 2) ? allocsize : (allocsize * 3 / 2);
157 void* data = lodepng_realloc(p->data, newsize);
158 if(data)
159 {
160 p->allocsize = newsize;
161 p->data = (unsigned*)data;
162 }
163 else return 0; /*error: not enough memory*/
164 }
165 return 1;
166 }
167
168 /*returns 1 if success, 0 if failure ==> nothing done*/
uivector_resize(uivector * p,size_t size)169 static unsigned uivector_resize(uivector* p, size_t size)
170 {
171 if(!uivector_reserve(p, size * sizeof(unsigned))) return 0;
172 p->size = size;
173 return 1; /*success*/
174 }
175
176 /*resize and give all new elements the value*/
uivector_resizev(uivector * p,size_t size,unsigned value)177 static unsigned uivector_resizev(uivector* p, size_t size, unsigned value)
178 {
179 size_t oldsize = p->size, i;
180 if(!uivector_resize(p, size)) return 0;
181 for(i = oldsize; i < size; ++i) p->data[i] = value;
182 return 1;
183 }
184
uivector_init(uivector * p)185 static void uivector_init(uivector* p)
186 {
187 p->data = NULL;
188 p->size = p->allocsize = 0;
189 }
190
191 #ifdef LODEPNG_COMPILE_ENCODER
192 /*returns 1 if success, 0 if failure ==> nothing done*/
uivector_push_back(uivector * p,unsigned c)193 static unsigned uivector_push_back(uivector* p, unsigned c)
194 {
195 if(!uivector_resize(p, p->size + 1)) return 0;
196 p->data[p->size - 1] = c;
197 return 1;
198 }
199 #endif /*LODEPNG_COMPILE_ENCODER*/
200 #endif /*LODEPNG_COMPILE_ZLIB*/
201
202 /* /////////////////////////////////////////////////////////////////////////// */
203
204 /*dynamic vector of unsigned chars*/
205 typedef struct ucvector
206 {
207 unsigned char* data;
208 size_t size; /*used size*/
209 size_t allocsize; /*allocated size*/
210 } ucvector;
211
212 /*returns 1 if success, 0 if failure ==> nothing done*/
ucvector_reserve(ucvector * p,size_t allocsize)213 static unsigned ucvector_reserve(ucvector* p, size_t allocsize)
214 {
215 if(allocsize > p->allocsize)
216 {
217 size_t newsize = (allocsize > p->allocsize * 2) ? allocsize : (allocsize * 3 / 2);
218 void* data = lodepng_realloc(p->data, newsize);
219 if(data)
220 {
221 p->allocsize = newsize;
222 p->data = (unsigned char*)data;
223 }
224 else return 0; /*error: not enough memory*/
225 }
226 return 1;
227 }
228
229 /*returns 1 if success, 0 if failure ==> nothing done*/
ucvector_resize(ucvector * p,size_t size)230 static unsigned ucvector_resize(ucvector* p, size_t size)
231 {
232 if(!ucvector_reserve(p, size * sizeof(unsigned char))) return 0;
233 p->size = size;
234 return 1; /*success*/
235 }
236
237 #ifdef LODEPNG_COMPILE_PNG
238
ucvector_cleanup(void * p)239 static void ucvector_cleanup(void* p)
240 {
241 ((ucvector*)p)->size = ((ucvector*)p)->allocsize = 0;
242 lodepng_free(((ucvector*)p)->data);
243 ((ucvector*)p)->data = NULL;
244 }
245
ucvector_init(ucvector * p)246 static void ucvector_init(ucvector* p)
247 {
248 p->data = NULL;
249 p->size = p->allocsize = 0;
250 }
251 #endif /*LODEPNG_COMPILE_PNG*/
252
253 #ifdef LODEPNG_COMPILE_ZLIB
254 /*you can both convert from vector to buffer&size and vica versa. If you use
255 init_buffer to take over a buffer and size, it is not needed to use cleanup*/
ucvector_init_buffer(ucvector * p,unsigned char * buffer,size_t size)256 static void ucvector_init_buffer(ucvector* p, unsigned char* buffer, size_t size)
257 {
258 p->data = buffer;
259 p->allocsize = p->size = size;
260 }
261 #endif /*LODEPNG_COMPILE_ZLIB*/
262
263 #if (defined(LODEPNG_COMPILE_PNG) && defined(LODEPNG_COMPILE_ANCILLARY_CHUNKS)) || defined(LODEPNG_COMPILE_ENCODER)
264 /*returns 1 if success, 0 if failure ==> nothing done*/
ucvector_push_back(ucvector * p,unsigned char c)265 static unsigned ucvector_push_back(ucvector* p, unsigned char c)
266 {
267 if(!ucvector_resize(p, p->size + 1)) return 0;
268 p->data[p->size - 1] = c;
269 return 1;
270 }
271 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/
272
273
274 /* ////////////////////////////////////////////////////////////////////////// */
275
276 #ifdef LODEPNG_COMPILE_PNG
277 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
278 /*returns 1 if success, 0 if failure ==> nothing done*/
string_resize(char ** out,size_t size)279 static unsigned string_resize(char** out, size_t size)
280 {
281 char* data = (char*)lodepng_realloc(*out, size + 1);
282 if(data)
283 {
284 data[size] = 0; /*null termination char*/
285 *out = data;
286 }
287 return data != 0;
288 }
289
290 /*init a {char*, size_t} pair for use as string*/
string_init(char ** out)291 static void string_init(char** out)
292 {
293 *out = NULL;
294 string_resize(out, 0);
295 }
296
297 /*free the above pair again*/
string_cleanup(char ** out)298 static void string_cleanup(char** out)
299 {
300 lodepng_free(*out);
301 *out = NULL;
302 }
303
string_set(char ** out,const char * in)304 static void string_set(char** out, const char* in)
305 {
306 size_t insize = strlen(in), i;
307 if(string_resize(out, insize))
308 {
309 for(i = 0; i != insize; ++i)
310 {
311 (*out)[i] = in[i];
312 }
313 }
314 }
315 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
316 #endif /*LODEPNG_COMPILE_PNG*/
317
318 /* ////////////////////////////////////////////////////////////////////////// */
319
lodepng_read32bitInt(const unsigned char * buffer)320 unsigned lodepng_read32bitInt(const unsigned char* buffer)
321 {
322 return (unsigned)((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
323 }
324
325 #if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)
326 /*buffer must have at least 4 allocated bytes available*/
lodepng_set32bitInt(unsigned char * buffer,unsigned value)327 static void lodepng_set32bitInt(unsigned char* buffer, unsigned value)
328 {
329 buffer[0] = (unsigned char)((value >> 24) & 0xff);
330 buffer[1] = (unsigned char)((value >> 16) & 0xff);
331 buffer[2] = (unsigned char)((value >> 8) & 0xff);
332 buffer[3] = (unsigned char)((value ) & 0xff);
333 }
334 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/
335
336 #ifdef LODEPNG_COMPILE_ENCODER
lodepng_add32bitInt(ucvector * buffer,unsigned value)337 static void lodepng_add32bitInt(ucvector* buffer, unsigned value)
338 {
339 ucvector_resize(buffer, buffer->size + 4); /*todo: give error if resize failed*/
340 lodepng_set32bitInt(&buffer->data[buffer->size - 4], value);
341 }
342 #endif /*LODEPNG_COMPILE_ENCODER*/
343
344 /* ////////////////////////////////////////////////////////////////////////// */
345 /* / File IO / */
346 /* ////////////////////////////////////////////////////////////////////////// */
347
348 #ifdef LODEPNG_COMPILE_DISK
349
350 /* returns negative value on error. This should be pure C compatible, so no fstat. */
lodepng_filesize(const char * filename)351 static long lodepng_filesize(const char* filename)
352 {
353 FILE* file;
354 long size;
355 file = fopen(filename, "rb");
356 if(!file) return -1;
357
358 if(fseek(file, 0, SEEK_END) != 0)
359 {
360 fclose(file);
361 return -1;
362 }
363
364 size = ftell(file);
365 /* It may give LONG_MAX as directory size, this is invalid for us. */
366 if(size == LONG_MAX) size = -1;
367
368 fclose(file);
369 return size;
370 }
371
372 /* load file into buffer that already has the correct allocated size. Returns error code.*/
lodepng_buffer_file(unsigned char * out,size_t size,const char * filename)373 static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename)
374 {
375 FILE* file;
376 size_t readsize;
377 file = fopen(filename, "rb");
378 if(!file) return 78;
379
380 readsize = fread(out, 1, size, file);
381 fclose(file);
382
383 if (readsize != size) return 78;
384 return 0;
385 }
386
lodepng_load_file(unsigned char ** out,size_t * outsize,const char * filename)387 unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename)
388 {
389 long size = lodepng_filesize(filename);
390 if (size < 0) return 78;
391 *outsize = (size_t)size;
392
393 *out = (unsigned char*)lodepng_malloc((size_t)size);
394 if(!(*out) && size > 0) return 83; /*the above malloc failed*/
395
396 return lodepng_buffer_file(*out, (size_t)size, filename);
397 }
398
399 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/
lodepng_save_file(const unsigned char * buffer,size_t buffersize,const char * filename)400 unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename)
401 {
402 FILE* file;
403 file = fopen(filename, "wb" );
404 if(!file) return 79;
405 fwrite((char*)buffer , 1 , buffersize, file);
406 fclose(file);
407 return 0;
408 }
409
410 #endif /*LODEPNG_COMPILE_DISK*/
411
412 /* ////////////////////////////////////////////////////////////////////////// */
413 /* ////////////////////////////////////////////////////////////////////////// */
414 /* // End of common code and tools. Begin of Zlib related code. // */
415 /* ////////////////////////////////////////////////////////////////////////// */
416 /* ////////////////////////////////////////////////////////////////////////// */
417
418 #ifdef LODEPNG_COMPILE_ZLIB
419 #ifdef LODEPNG_COMPILE_ENCODER
420 /*TODO: this ignores potential out of memory errors*/
421 #define addBitToStream(/*size_t**/ bitpointer, /*ucvector**/ bitstream, /*unsigned char*/ bit)\
422 {\
423 /*add a new byte at the end*/\
424 if(((*bitpointer) & 7) == 0) ucvector_push_back(bitstream, (unsigned char)0);\
425 /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/\
426 (bitstream->data[bitstream->size - 1]) |= (bit << ((*bitpointer) & 0x7));\
427 ++(*bitpointer);\
428 }
429
addBitsToStream(size_t * bitpointer,ucvector * bitstream,unsigned value,size_t nbits)430 static void addBitsToStream(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits)
431 {
432 size_t i;
433 for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1));
434 }
435
addBitsToStreamReversed(size_t * bitpointer,ucvector * bitstream,unsigned value,size_t nbits)436 static void addBitsToStreamReversed(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits)
437 {
438 size_t i;
439 for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1));
440 }
441 #endif /*LODEPNG_COMPILE_ENCODER*/
442
443 #ifdef LODEPNG_COMPILE_DECODER
444
445 #define READBIT(bitpointer, bitstream) ((bitstream[bitpointer >> 3] >> (bitpointer & 0x7)) & (unsigned char)1)
446
readBitFromStream(size_t * bitpointer,const unsigned char * bitstream)447 static unsigned char readBitFromStream(size_t* bitpointer, const unsigned char* bitstream)
448 {
449 unsigned char result = (unsigned char)(READBIT(*bitpointer, bitstream));
450 ++(*bitpointer);
451 return result;
452 }
453
readBitsFromStream(size_t * bitpointer,const unsigned char * bitstream,size_t nbits)454 static unsigned readBitsFromStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits)
455 {
456 unsigned result = 0, i;
457 for(i = 0; i != nbits; ++i)
458 {
459 result += ((unsigned)READBIT(*bitpointer, bitstream)) << i;
460 ++(*bitpointer);
461 }
462 return result;
463 }
464 #endif /*LODEPNG_COMPILE_DECODER*/
465
466 /* ////////////////////////////////////////////////////////////////////////// */
467 /* / Deflate - Huffman / */
468 /* ////////////////////////////////////////////////////////////////////////// */
469
470 #define FIRST_LENGTH_CODE_INDEX 257
471 #define LAST_LENGTH_CODE_INDEX 285
472 /*256 literals, the end code, some length codes, and 2 unused codes*/
473 #define NUM_DEFLATE_CODE_SYMBOLS 288
474 /*the distance codes have their own symbols, 30 used, 2 unused*/
475 #define NUM_DISTANCE_SYMBOLS 32
476 /*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-138 zeros*/
477 #define NUM_CODE_LENGTH_CODES 19
478
479 /*the base lengths represented by codes 257-285*/
480 static const unsigned LENGTHBASE[29]
481 = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
482 67, 83, 99, 115, 131, 163, 195, 227, 258};
483
484 /*the extra bits used by codes 257-285 (added to base length)*/
485 static const unsigned LENGTHEXTRA[29]
486 = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
487 4, 4, 4, 4, 5, 5, 5, 5, 0};
488
489 /*the base backwards distances (the bits of distance codes appear after length codes and use their own huffman tree)*/
490 static const unsigned DISTANCEBASE[30]
491 = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
492 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
493
494 /*the extra bits of backwards distances (added to base)*/
495 static const unsigned DISTANCEEXTRA[30]
496 = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
497 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
498
499 /*the order in which "code length alphabet code lengths" are stored, out of this
500 the huffman tree of the dynamic huffman tree lengths is generated*/
501 static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES]
502 = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
503
504 /* ////////////////////////////////////////////////////////////////////////// */
505
506 /*
507 Huffman tree struct, containing multiple representations of the tree
508 */
509 typedef struct HuffmanTree
510 {
511 unsigned* tree2d;
512 unsigned* tree1d;
513 unsigned* lengths; /*the lengths of the codes of the 1d-tree*/
514 unsigned maxbitlen; /*maximum number of bits a single code can get*/
515 unsigned numcodes; /*number of symbols in the alphabet = number of codes*/
516 } HuffmanTree;
517
518 /*function used for debug purposes to draw the tree in ascii art with C++*/
519 /*
520 static void HuffmanTree_draw(HuffmanTree* tree)
521 {
522 std::cout << "tree. length: " << tree->numcodes << " maxbitlen: " << tree->maxbitlen << std::endl;
523 for(size_t i = 0; i != tree->tree1d.size; ++i)
524 {
525 if(tree->lengths.data[i])
526 std::cout << i << " " << tree->tree1d.data[i] << " " << tree->lengths.data[i] << std::endl;
527 }
528 std::cout << std::endl;
529 }*/
530
HuffmanTree_init(HuffmanTree * tree)531 static void HuffmanTree_init(HuffmanTree* tree)
532 {
533 tree->tree2d = 0;
534 tree->tree1d = 0;
535 tree->lengths = 0;
536 }
537
HuffmanTree_cleanup(HuffmanTree * tree)538 static void HuffmanTree_cleanup(HuffmanTree* tree)
539 {
540 lodepng_free(tree->tree2d);
541 lodepng_free(tree->tree1d);
542 lodepng_free(tree->lengths);
543 }
544
545 /*the tree representation used by the decoder. return value is error*/
HuffmanTree_make2DTree(HuffmanTree * tree)546 static unsigned HuffmanTree_make2DTree(HuffmanTree* tree)
547 {
548 unsigned nodefilled = 0; /*up to which node it is filled*/
549 unsigned treepos = 0; /*position in the tree (1 of the numcodes columns)*/
550 unsigned n, i;
551
552 tree->tree2d = (unsigned*)lodepng_malloc(tree->numcodes * 2 * sizeof(unsigned));
553 if(!tree->tree2d) return 83; /*alloc fail*/
554
555 /*
556 convert tree1d[] to tree2d[][]. In the 2D array, a value of 32767 means
557 uninited, a value >= numcodes is an address to another bit, a value < numcodes
558 is a code. The 2 rows are the 2 possible bit values (0 or 1), there are as
559 many columns as codes - 1.
560 A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes.
561 Here, the internal nodes are stored (what their 0 and 1 option point to).
562 There is only memory for such good tree currently, if there are more nodes
563 (due to too long length codes), error 55 will happen
564 */
565 for(n = 0; n < tree->numcodes * 2; ++n)
566 {
567 tree->tree2d[n] = 32767; /*32767 here means the tree2d isn't filled there yet*/
568 }
569
570 for(n = 0; n < tree->numcodes; ++n) /*the codes*/
571 {
572 for(i = 0; i != tree->lengths[n]; ++i) /*the bits for this code*/
573 {
574 unsigned char bit = (unsigned char)((tree->tree1d[n] >> (tree->lengths[n] - i - 1)) & 1);
575 /*oversubscribed, see comment in lodepng_error_text*/
576 if(treepos > 2147483647 || treepos + 2 > tree->numcodes) return 55;
577 if(tree->tree2d[2 * treepos + bit] == 32767) /*not yet filled in*/
578 {
579 if(i + 1 == tree->lengths[n]) /*last bit*/
580 {
581 tree->tree2d[2 * treepos + bit] = n; /*put the current code in it*/
582 treepos = 0;
583 }
584 else
585 {
586 /*put address of the next step in here, first that address has to be found of course
587 (it's just nodefilled + 1)...*/
588 ++nodefilled;
589 /*addresses encoded with numcodes added to it*/
590 tree->tree2d[2 * treepos + bit] = nodefilled + tree->numcodes;
591 treepos = nodefilled;
592 }
593 }
594 else treepos = tree->tree2d[2 * treepos + bit] - tree->numcodes;
595 }
596 }
597
598 for(n = 0; n < tree->numcodes * 2; ++n)
599 {
600 if(tree->tree2d[n] == 32767) tree->tree2d[n] = 0; /*remove possible remaining 32767's*/
601 }
602
603 return 0;
604 }
605
606 /*
607 Second step for the ...makeFromLengths and ...makeFromFrequencies functions.
608 numcodes, lengths and maxbitlen must already be filled in correctly. return
609 value is error.
610 */
HuffmanTree_makeFromLengths2(HuffmanTree * tree)611 static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
612 {
613 uivector blcount;
614 uivector nextcode;
615 unsigned error = 0;
616 unsigned bits, n;
617
618 uivector_init(&blcount);
619 uivector_init(&nextcode);
620
621 tree->tree1d = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned));
622 if(!tree->tree1d) error = 83; /*alloc fail*/
623
624 if(!uivector_resizev(&blcount, tree->maxbitlen + 1, 0)
625 || !uivector_resizev(&nextcode, tree->maxbitlen + 1, 0))
626 error = 83; /*alloc fail*/
627
628 if(!error)
629 {
630 /*step 1: count number of instances of each code length*/
631 for(bits = 0; bits != tree->numcodes; ++bits) ++blcount.data[tree->lengths[bits]];
632 /*step 2: generate the nextcode values*/
633 for(bits = 1; bits <= tree->maxbitlen; ++bits)
634 {
635 nextcode.data[bits] = (nextcode.data[bits - 1] + blcount.data[bits - 1]) << 1;
636 }
637 /*step 3: generate all the codes*/
638 for(n = 0; n != tree->numcodes; ++n)
639 {
640 if(tree->lengths[n] != 0) tree->tree1d[n] = nextcode.data[tree->lengths[n]]++;
641 }
642 }
643
644 uivector_cleanup(&blcount);
645 uivector_cleanup(&nextcode);
646
647 if(!error) return HuffmanTree_make2DTree(tree);
648 else return error;
649 }
650
651 /*
652 given the code lengths (as stored in the PNG file), generate the tree as defined
653 by Deflate. maxbitlen is the maximum bits that a code in the tree can have.
654 return value is error.
655 */
HuffmanTree_makeFromLengths(HuffmanTree * tree,const unsigned * bitlen,size_t numcodes,unsigned maxbitlen)656 static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen,
657 size_t numcodes, unsigned maxbitlen)
658 {
659 unsigned i;
660 tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned));
661 if(!tree->lengths) return 83; /*alloc fail*/
662 for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i];
663 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
664 tree->maxbitlen = maxbitlen;
665 return HuffmanTree_makeFromLengths2(tree);
666 }
667
668 #ifdef LODEPNG_COMPILE_ENCODER
669
670 /*BPM: Boundary Package Merge, see "A Fast and Space-Economical Algorithm for Length-Limited Coding",
671 Jyrki Katajainen, Alistair Moffat, Andrew Turpin, 1995.*/
672
673 /*chain node for boundary package merge*/
674 typedef struct BPMNode
675 {
676 int weight; /*the sum of all weights in this chain*/
677 unsigned index; /*index of this leaf node (called "count" in the paper)*/
678 struct BPMNode* tail; /*the next nodes in this chain (null if last)*/
679 int in_use;
680 } BPMNode;
681
682 /*lists of chains*/
683 typedef struct BPMLists
684 {
685 /*memory pool*/
686 unsigned memsize;
687 BPMNode* memory;
688 unsigned numfree;
689 unsigned nextfree;
690 BPMNode** freelist;
691 /*two heads of lookahead chains per list*/
692 unsigned listsize;
693 BPMNode** chains0;
694 BPMNode** chains1;
695 } BPMLists;
696
697 /*creates a new chain node with the given parameters, from the memory in the lists */
bpmnode_create(BPMLists * lists,int weight,unsigned index,BPMNode * tail)698 static BPMNode* bpmnode_create(BPMLists* lists, int weight, unsigned index, BPMNode* tail)
699 {
700 unsigned i;
701 BPMNode* result;
702
703 /*memory full, so garbage collect*/
704 if(lists->nextfree >= lists->numfree)
705 {
706 /*mark only those that are in use*/
707 for(i = 0; i != lists->memsize; ++i) lists->memory[i].in_use = 0;
708 for(i = 0; i != lists->listsize; ++i)
709 {
710 BPMNode* node;
711 for(node = lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1;
712 for(node = lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1;
713 }
714 /*collect those that are free*/
715 lists->numfree = 0;
716 for(i = 0; i != lists->memsize; ++i)
717 {
718 if(!lists->memory[i].in_use) lists->freelist[lists->numfree++] = &lists->memory[i];
719 }
720 lists->nextfree = 0;
721 }
722
723 result = lists->freelist[lists->nextfree++];
724 result->weight = weight;
725 result->index = index;
726 result->tail = tail;
727 return result;
728 }
729
730 /*sort the leaves with stable mergesort*/
bpmnode_sort(BPMNode * leaves,size_t num)731 static void bpmnode_sort(BPMNode* leaves, size_t num)
732 {
733 BPMNode* mem = (BPMNode*)lodepng_malloc(sizeof(*leaves) * num);
734 size_t width, counter = 0;
735 for(width = 1; width < num; width *= 2)
736 {
737 BPMNode* a = (counter & 1) ? mem : leaves;
738 BPMNode* b = (counter & 1) ? leaves : mem;
739 size_t p;
740 for(p = 0; p < num; p += 2 * width)
741 {
742 size_t q = (p + width > num) ? num : (p + width);
743 size_t r = (p + 2 * width > num) ? num : (p + 2 * width);
744 size_t i = p, j = q, k;
745 for(k = p; k < r; k++)
746 {
747 if(i < q && (j >= r || a[i].weight <= a[j].weight)) b[k] = a[i++];
748 else b[k] = a[j++];
749 }
750 }
751 counter++;
752 }
753 if(counter & 1) memcpy(leaves, mem, sizeof(*leaves) * num);
754 lodepng_free(mem);
755 }
756
757 /*Boundary Package Merge step, numpresent is the amount of leaves, and c is the current chain.*/
boundaryPM(BPMLists * lists,BPMNode * leaves,size_t numpresent,int c,int num)758 static void boundaryPM(BPMLists* lists, BPMNode* leaves, size_t numpresent, int c, int num)
759 {
760 unsigned lastindex = lists->chains1[c]->index;
761
762 if(c == 0)
763 {
764 if(lastindex >= numpresent) return;
765 lists->chains0[c] = lists->chains1[c];
766 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, 0);
767 }
768 else
769 {
770 /*sum of the weights of the head nodes of the previous lookahead chains.*/
771 int sum = lists->chains0[c - 1]->weight + lists->chains1[c - 1]->weight;
772 lists->chains0[c] = lists->chains1[c];
773 if(lastindex < numpresent && sum > leaves[lastindex].weight)
774 {
775 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, lists->chains1[c]->tail);
776 return;
777 }
778 lists->chains1[c] = bpmnode_create(lists, sum, lastindex, lists->chains1[c - 1]);
779 /*in the end we are only interested in the chain of the last list, so no
780 need to recurse if we're at the last one (this gives measurable speedup)*/
781 if(num + 1 < (int)(2 * numpresent - 2))
782 {
783 boundaryPM(lists, leaves, numpresent, c - 1, num);
784 boundaryPM(lists, leaves, numpresent, c - 1, num);
785 }
786 }
787 }
788
lodepng_huffman_code_lengths(unsigned * lengths,const unsigned * frequencies,size_t numcodes,unsigned maxbitlen)789 unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies,
790 size_t numcodes, unsigned maxbitlen)
791 {
792 unsigned error = 0;
793 unsigned i;
794 size_t numpresent = 0; /*number of symbols with non-zero frequency*/
795 BPMNode* leaves; /*the symbols, only those with > 0 frequency*/
796
797 if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
798 if((1u << maxbitlen) < numcodes) return 80; /*error: represent all symbols*/
799
800 leaves = (BPMNode*)lodepng_malloc(numcodes * sizeof(*leaves));
801 if(!leaves) return 83; /*alloc fail*/
802
803 for(i = 0; i != numcodes; ++i)
804 {
805 if(frequencies[i] > 0)
806 {
807 leaves[numpresent].weight = (int)frequencies[i];
808 leaves[numpresent].index = i;
809 ++numpresent;
810 }
811 }
812
813 for(i = 0; i != numcodes; ++i) lengths[i] = 0;
814
815 /*ensure at least two present symbols. There should be at least one symbol
816 according to RFC 1951 section 3.2.7. Some decoders incorrectly require two. To
817 make these work as well ensure there are at least two symbols. The
818 Package-Merge code below also doesn't work correctly if there's only one
819 symbol, it'd give it the theoritical 0 bits but in practice zlib wants 1 bit*/
820 if(numpresent == 0)
821 {
822 lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/
823 }
824 else if(numpresent == 1)
825 {
826 lengths[leaves[0].index] = 1;
827 lengths[leaves[0].index == 0 ? 1 : 0] = 1;
828 }
829 else
830 {
831 BPMLists lists;
832 BPMNode* node;
833
834 bpmnode_sort(leaves, numpresent);
835
836 lists.listsize = maxbitlen;
837 lists.memsize = 2 * maxbitlen * (maxbitlen + 1);
838 lists.nextfree = 0;
839 lists.numfree = lists.memsize;
840 lists.memory = (BPMNode*)lodepng_malloc(lists.memsize * sizeof(*lists.memory));
841 lists.freelist = (BPMNode**)lodepng_malloc(lists.memsize * sizeof(BPMNode*));
842 lists.chains0 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
843 lists.chains1 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
844 if(!lists.memory || !lists.freelist || !lists.chains0 || !lists.chains1) error = 83; /*alloc fail*/
845
846 if(!error)
847 {
848 for(i = 0; i != lists.memsize; ++i) lists.freelist[i] = &lists.memory[i];
849
850 bpmnode_create(&lists, leaves[0].weight, 1, 0);
851 bpmnode_create(&lists, leaves[1].weight, 2, 0);
852
853 for(i = 0; i != lists.listsize; ++i)
854 {
855 lists.chains0[i] = &lists.memory[0];
856 lists.chains1[i] = &lists.memory[1];
857 }
858
859 /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/
860 for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1, (int)i);
861
862 for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail)
863 {
864 for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index];
865 }
866 }
867
868 lodepng_free(lists.memory);
869 lodepng_free(lists.freelist);
870 lodepng_free(lists.chains0);
871 lodepng_free(lists.chains1);
872 }
873
874 lodepng_free(leaves);
875 return error;
876 }
877
878 /*Create the Huffman tree given the symbol frequencies*/
HuffmanTree_makeFromFrequencies(HuffmanTree * tree,const unsigned * frequencies,size_t mincodes,size_t numcodes,unsigned maxbitlen)879 static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies,
880 size_t mincodes, size_t numcodes, unsigned maxbitlen)
881 {
882 unsigned error = 0;
883 while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/
884 tree->maxbitlen = maxbitlen;
885 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
886 tree->lengths = (unsigned*)lodepng_realloc(tree->lengths, numcodes * sizeof(unsigned));
887 if(!tree->lengths) return 83; /*alloc fail*/
888 /*initialize all lengths to 0*/
889 memset(tree->lengths, 0, numcodes * sizeof(unsigned));
890
891 error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen);
892 if(!error) error = HuffmanTree_makeFromLengths2(tree);
893 return error;
894 }
895
HuffmanTree_getCode(const HuffmanTree * tree,unsigned index)896 static unsigned HuffmanTree_getCode(const HuffmanTree* tree, unsigned index)
897 {
898 return tree->tree1d[index];
899 }
900
HuffmanTree_getLength(const HuffmanTree * tree,unsigned index)901 static unsigned HuffmanTree_getLength(const HuffmanTree* tree, unsigned index)
902 {
903 return tree->lengths[index];
904 }
905 #endif /*LODEPNG_COMPILE_ENCODER*/
906
907 /*get the literal and length code tree of a deflated block with fixed tree, as per the deflate specification*/
generateFixedLitLenTree(HuffmanTree * tree)908 static unsigned generateFixedLitLenTree(HuffmanTree* tree)
909 {
910 unsigned i, error = 0;
911 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
912 if(!bitlen) return 83; /*alloc fail*/
913
914 /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/
915 for(i = 0; i <= 143; ++i) bitlen[i] = 8;
916 for(i = 144; i <= 255; ++i) bitlen[i] = 9;
917 for(i = 256; i <= 279; ++i) bitlen[i] = 7;
918 for(i = 280; i <= 287; ++i) bitlen[i] = 8;
919
920 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15);
921
922 lodepng_free(bitlen);
923 return error;
924 }
925
926 /*get the distance code tree of a deflated block with fixed tree, as specified in the deflate specification*/
generateFixedDistanceTree(HuffmanTree * tree)927 static unsigned generateFixedDistanceTree(HuffmanTree* tree)
928 {
929 unsigned i, error = 0;
930 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
931 if(!bitlen) return 83; /*alloc fail*/
932
933 /*there are 32 distance codes, but 30-31 are unused*/
934 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5;
935 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15);
936
937 lodepng_free(bitlen);
938 return error;
939 }
940
941 #ifdef LODEPNG_COMPILE_DECODER
942
943 /*
944 returns the code, or (unsigned)(-1) if error happened
945 inbitlength is the length of the complete buffer, in bits (so its byte length times 8)
946 */
huffmanDecodeSymbol(const unsigned char * in,size_t * bp,const HuffmanTree * codetree,size_t inbitlength)947 static unsigned huffmanDecodeSymbol(const unsigned char* in, size_t* bp,
948 const HuffmanTree* codetree, size_t inbitlength)
949 {
950 unsigned treepos = 0, ct;
951 for(;;)
952 {
953 if(*bp >= inbitlength) return (unsigned)(-1); /*error: end of input memory reached without endcode*/
954 /*
955 decode the symbol from the tree. The "readBitFromStream" code is inlined in
956 the expression below because this is the biggest bottleneck while decoding
957 */
958 ct = codetree->tree2d[(treepos << 1) + READBIT(*bp, in)];
959 ++(*bp);
960 if(ct < codetree->numcodes) return ct; /*the symbol is decoded, return it*/
961 else treepos = ct - codetree->numcodes; /*symbol not yet decoded, instead move tree position*/
962
963 if(treepos >= codetree->numcodes) return (unsigned)(-1); /*error: it appeared outside the codetree*/
964 }
965 }
966 #endif /*LODEPNG_COMPILE_DECODER*/
967
968 #ifdef LODEPNG_COMPILE_DECODER
969
970 /* ////////////////////////////////////////////////////////////////////////// */
971 /* / Inflator (Decompressor) / */
972 /* ////////////////////////////////////////////////////////////////////////// */
973
974 /*get the tree of a deflated block with fixed tree, as specified in the deflate specification*/
getTreeInflateFixed(HuffmanTree * tree_ll,HuffmanTree * tree_d)975 static void getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d)
976 {
977 /*TODO: check for out of memory errors*/
978 generateFixedLitLenTree(tree_ll);
979 generateFixedDistanceTree(tree_d);
980 }
981
982 /*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/
getTreeInflateDynamic(HuffmanTree * tree_ll,HuffmanTree * tree_d,const unsigned char * in,size_t * bp,size_t inlength)983 static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
984 const unsigned char* in, size_t* bp, size_t inlength)
985 {
986 /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/
987 unsigned error = 0;
988 unsigned n, HLIT, HDIST, HCLEN, i;
989 size_t inbitlength = inlength * 8;
990
991 /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/
992 unsigned* bitlen_ll = 0; /*lit,len code lengths*/
993 unsigned* bitlen_d = 0; /*dist code lengths*/
994 /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/
995 unsigned* bitlen_cl = 0;
996 HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/
997
998 if((*bp) + 14 > (inlength << 3)) return 49; /*error: the bit pointer is or will go past the memory*/
999
1000 /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/
1001 HLIT = readBitsFromStream(bp, in, 5) + 257;
1002 /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/
1003 HDIST = readBitsFromStream(bp, in, 5) + 1;
1004 /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/
1005 HCLEN = readBitsFromStream(bp, in, 4) + 4;
1006
1007 if((*bp) + HCLEN * 3 > (inlength << 3)) return 50; /*error: the bit pointer is or will go past the memory*/
1008
1009 HuffmanTree_init(&tree_cl);
1010
1011 while(!error)
1012 {
1013 /*read the code length codes out of 3 * (amount of code length codes) bits*/
1014
1015 bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned));
1016 if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/);
1017
1018 for(i = 0; i != NUM_CODE_LENGTH_CODES; ++i)
1019 {
1020 if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3);
1021 else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/
1022 }
1023
1024 error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7);
1025 if(error) break;
1026
1027 /*now we can use this tree to read the lengths for the tree that this function will return*/
1028 bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
1029 bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
1030 if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/);
1031 for(i = 0; i != NUM_DEFLATE_CODE_SYMBOLS; ++i) bitlen_ll[i] = 0;
1032 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen_d[i] = 0;
1033
1034 /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/
1035 i = 0;
1036 while(i < HLIT + HDIST)
1037 {
1038 unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength);
1039 if(code <= 15) /*a length code*/
1040 {
1041 if(i < HLIT) bitlen_ll[i] = code;
1042 else bitlen_d[i - HLIT] = code;
1043 ++i;
1044 }
1045 else if(code == 16) /*repeat previous*/
1046 {
1047 unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/
1048 unsigned value; /*set value to the previous code*/
1049
1050 if(i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/
1051
1052 if((*bp + 2) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
1053 replength += readBitsFromStream(bp, in, 2);
1054
1055 if(i < HLIT + 1) value = bitlen_ll[i - 1];
1056 else value = bitlen_d[i - HLIT - 1];
1057 /*repeat this value in the next lengths*/
1058 for(n = 0; n < replength; ++n)
1059 {
1060 if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/
1061 if(i < HLIT) bitlen_ll[i] = value;
1062 else bitlen_d[i - HLIT] = value;
1063 ++i;
1064 }
1065 }
1066 else if(code == 17) /*repeat "0" 3-10 times*/
1067 {
1068 unsigned replength = 3; /*read in the bits that indicate repeat length*/
1069 if((*bp + 3) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
1070 replength += readBitsFromStream(bp, in, 3);
1071
1072 /*repeat this value in the next lengths*/
1073 for(n = 0; n < replength; ++n)
1074 {
1075 if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/
1076
1077 if(i < HLIT) bitlen_ll[i] = 0;
1078 else bitlen_d[i - HLIT] = 0;
1079 ++i;
1080 }
1081 }
1082 else if(code == 18) /*repeat "0" 11-138 times*/
1083 {
1084 unsigned replength = 11; /*read in the bits that indicate repeat length*/
1085 if((*bp + 7) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
1086 replength += readBitsFromStream(bp, in, 7);
1087
1088 /*repeat this value in the next lengths*/
1089 for(n = 0; n < replength; ++n)
1090 {
1091 if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/
1092
1093 if(i < HLIT) bitlen_ll[i] = 0;
1094 else bitlen_d[i - HLIT] = 0;
1095 ++i;
1096 }
1097 }
1098 else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/
1099 {
1100 if(code == (unsigned)(-1))
1101 {
1102 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
1103 (10=no endcode, 11=wrong jump outside of tree)*/
1104 error = (*bp) > inbitlength ? 10 : 11;
1105 }
1106 else error = 16; /*unexisting code, this can never happen*/
1107 break;
1108 }
1109 }
1110 if(error) break;
1111
1112 if(bitlen_ll[256] == 0) ERROR_BREAK(64); /*the length of the end code 256 must be larger than 0*/
1113
1114 /*now we've finally got HLIT and HDIST, so generate the code trees, and the function is done*/
1115 error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15);
1116 if(error) break;
1117 error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15);
1118
1119 break; /*end of error-while*/
1120 }
1121
1122 lodepng_free(bitlen_cl);
1123 lodepng_free(bitlen_ll);
1124 lodepng_free(bitlen_d);
1125 HuffmanTree_cleanup(&tree_cl);
1126
1127 return error;
1128 }
1129
1130 /*inflate a block with dynamic of fixed Huffman tree*/
inflateHuffmanBlock(ucvector * out,const unsigned char * in,size_t * bp,size_t * pos,size_t inlength,unsigned btype)1131 static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size_t* bp,
1132 size_t* pos, size_t inlength, unsigned btype)
1133 {
1134 unsigned error = 0;
1135 HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/
1136 HuffmanTree tree_d; /*the huffman tree for distance codes*/
1137 size_t inbitlength = inlength * 8;
1138
1139 HuffmanTree_init(&tree_ll);
1140 HuffmanTree_init(&tree_d);
1141
1142 if(btype == 1) getTreeInflateFixed(&tree_ll, &tree_d);
1143 else if(btype == 2) error = getTreeInflateDynamic(&tree_ll, &tree_d, in, bp, inlength);
1144
1145 while(!error) /*decode all symbols until end reached, breaks at end code*/
1146 {
1147 /*code_ll is literal, length or end code*/
1148 unsigned code_ll = huffmanDecodeSymbol(in, bp, &tree_ll, inbitlength);
1149 if(code_ll <= 255) /*literal symbol*/
1150 {
1151 /*ucvector_push_back would do the same, but for some reason the two lines below run 10% faster*/
1152 if(!ucvector_resize(out, (*pos) + 1)) ERROR_BREAK(83 /*alloc fail*/);
1153 out->data[*pos] = (unsigned char)code_ll;
1154 ++(*pos);
1155 }
1156 else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/
1157 {
1158 unsigned code_d, distance;
1159 unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/
1160 size_t start, forward, backward, length;
1161
1162 /*part 1: get length base*/
1163 length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX];
1164
1165 /*part 2: get extra bits and add the value of that to length*/
1166 numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX];
1167 if((*bp + numextrabits_l) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/
1168 length += readBitsFromStream(bp, in, numextrabits_l);
1169
1170 /*part 3: get distance code*/
1171 code_d = huffmanDecodeSymbol(in, bp, &tree_d, inbitlength);
1172 if(code_d > 29)
1173 {
1174 if(code_ll == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/
1175 {
1176 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
1177 (10=no endcode, 11=wrong jump outside of tree)*/
1178 error = (*bp) > inlength * 8 ? 10 : 11;
1179 }
1180 else error = 18; /*error: invalid distance code (30-31 are never used)*/
1181 break;
1182 }
1183 distance = DISTANCEBASE[code_d];
1184
1185 /*part 4: get extra bits from distance*/
1186 numextrabits_d = DISTANCEEXTRA[code_d];
1187 if((*bp + numextrabits_d) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/
1188 distance += readBitsFromStream(bp, in, numextrabits_d);
1189
1190 /*part 5: fill in all the out[n] values based on the length and dist*/
1191 start = (*pos);
1192 if(distance > start) ERROR_BREAK(52); /*too long backward distance*/
1193 backward = start - distance;
1194
1195 if(!ucvector_resize(out, (*pos) + length)) ERROR_BREAK(83 /*alloc fail*/);
1196 if (distance < length) {
1197 for(forward = 0; forward < length; ++forward)
1198 {
1199 out->data[(*pos)++] = out->data[backward++];
1200 }
1201 } else {
1202 memcpy(out->data + *pos, out->data + backward, length);
1203 *pos += length;
1204 }
1205 }
1206 else if(code_ll == 256)
1207 {
1208 break; /*end code, break the loop*/
1209 }
1210 else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/
1211 {
1212 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
1213 (10=no endcode, 11=wrong jump outside of tree)*/
1214 error = ((*bp) > inlength * 8) ? 10 : 11;
1215 break;
1216 }
1217 }
1218
1219 HuffmanTree_cleanup(&tree_ll);
1220 HuffmanTree_cleanup(&tree_d);
1221
1222 return error;
1223 }
1224
inflateNoCompression(ucvector * out,const unsigned char * in,size_t * bp,size_t * pos,size_t inlength)1225 static unsigned inflateNoCompression(ucvector* out, const unsigned char* in, size_t* bp, size_t* pos, size_t inlength)
1226 {
1227 size_t p;
1228 unsigned LEN, NLEN, n, error = 0;
1229
1230 /*go to first boundary of byte*/
1231 while(((*bp) & 0x7) != 0) ++(*bp);
1232 p = (*bp) / 8; /*byte position*/
1233
1234 /*read LEN (2 bytes) and NLEN (2 bytes)*/
1235 if(p + 4 >= inlength) return 52; /*error, bit pointer will jump past memory*/
1236 LEN = in[p] + 256u * in[p + 1]; p += 2;
1237 NLEN = in[p] + 256u * in[p + 1]; p += 2;
1238
1239 /*check if 16-bit NLEN is really the one's complement of LEN*/
1240 if(LEN + NLEN != 65535) return 21; /*error: NLEN is not one's complement of LEN*/
1241
1242 if(!ucvector_resize(out, (*pos) + LEN)) return 83; /*alloc fail*/
1243
1244 /*read the literal data: LEN bytes are now stored in the out buffer*/
1245 if(p + LEN > inlength) return 23; /*error: reading outside of in buffer*/
1246 for(n = 0; n < LEN; ++n) out->data[(*pos)++] = in[p++];
1247
1248 (*bp) = p * 8;
1249
1250 return error;
1251 }
1252
lodepng_inflatev(ucvector * out,const unsigned char * in,size_t insize,const LodePNGDecompressSettings * settings)1253 static unsigned lodepng_inflatev(ucvector* out,
1254 const unsigned char* in, size_t insize,
1255 const LodePNGDecompressSettings* settings)
1256 {
1257 /*bit pointer in the "in" data, current byte is bp >> 3, current bit is bp & 0x7 (from lsb to msb of the byte)*/
1258 size_t bp = 0;
1259 unsigned BFINAL = 0;
1260 size_t pos = 0; /*byte position in the out buffer*/
1261 unsigned error = 0;
1262
1263 (void)settings;
1264
1265 while(!BFINAL)
1266 {
1267 unsigned BTYPE;
1268 if(bp + 2 >= insize * 8) return 52; /*error, bit pointer will jump past memory*/
1269 BFINAL = readBitFromStream(&bp, in);
1270 BTYPE = 1u * readBitFromStream(&bp, in);
1271 BTYPE += 2u * readBitFromStream(&bp, in);
1272
1273 if(BTYPE == 3) return 20; /*error: invalid BTYPE*/
1274 else if(BTYPE == 0) error = inflateNoCompression(out, in, &bp, &pos, insize); /*no compression*/
1275 else error = inflateHuffmanBlock(out, in, &bp, &pos, insize, BTYPE); /*compression, BTYPE 01 or 10*/
1276
1277 if(error) return error;
1278 }
1279
1280 return error;
1281 }
1282
lodepng_inflate(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGDecompressSettings * settings)1283 unsigned lodepng_inflate(unsigned char** out, size_t* outsize,
1284 const unsigned char* in, size_t insize,
1285 const LodePNGDecompressSettings* settings)
1286 {
1287 unsigned error;
1288 ucvector v;
1289 ucvector_init_buffer(&v, *out, *outsize);
1290 error = lodepng_inflatev(&v, in, insize, settings);
1291 *out = v.data;
1292 *outsize = v.size;
1293 return error;
1294 }
1295
inflate(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGDecompressSettings * settings)1296 static unsigned inflate(unsigned char** out, size_t* outsize,
1297 const unsigned char* in, size_t insize,
1298 const LodePNGDecompressSettings* settings)
1299 {
1300 if(settings->custom_inflate)
1301 {
1302 return settings->custom_inflate(out, outsize, in, insize, settings);
1303 }
1304 else
1305 {
1306 return lodepng_inflate(out, outsize, in, insize, settings);
1307 }
1308 }
1309
1310 #endif /*LODEPNG_COMPILE_DECODER*/
1311
1312 #ifdef LODEPNG_COMPILE_ENCODER
1313
1314 /* ////////////////////////////////////////////////////////////////////////// */
1315 /* / Deflator (Compressor) / */
1316 /* ////////////////////////////////////////////////////////////////////////// */
1317
1318 static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258;
1319
1320 /*bitlen is the size in bits of the code*/
addHuffmanSymbol(size_t * bp,ucvector * compressed,unsigned code,unsigned bitlen)1321 static void addHuffmanSymbol(size_t* bp, ucvector* compressed, unsigned code, unsigned bitlen)
1322 {
1323 addBitsToStreamReversed(bp, compressed, code, bitlen);
1324 }
1325
1326 /*search the index in the array, that has the largest value smaller than or equal to the given value,
1327 given array must be sorted (if no value is smaller, it returns the size of the given array)*/
searchCodeIndex(const unsigned * array,size_t array_size,size_t value)1328 static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value)
1329 {
1330 /*binary search (only small gain over linear). TODO: use CPU log2 instruction for getting symbols instead*/
1331 size_t left = 1;
1332 size_t right = array_size - 1;
1333
1334 while(left <= right) {
1335 size_t mid = (left + right) >> 1;
1336 if (array[mid] >= value) right = mid - 1;
1337 else left = mid + 1;
1338 }
1339 if(left >= array_size || array[left] > value) left--;
1340 return left;
1341 }
1342
addLengthDistance(uivector * values,size_t length,size_t distance)1343 static void addLengthDistance(uivector* values, size_t length, size_t distance)
1344 {
1345 /*values in encoded vector are those used by deflate:
1346 0-255: literal bytes
1347 256: end
1348 257-285: length/distance pair (length code, followed by extra length bits, distance code, extra distance bits)
1349 286-287: invalid*/
1350
1351 unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29, length);
1352 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]);
1353 unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance);
1354 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]);
1355
1356 uivector_push_back(values, length_code + FIRST_LENGTH_CODE_INDEX);
1357 uivector_push_back(values, extra_length);
1358 uivector_push_back(values, dist_code);
1359 uivector_push_back(values, extra_distance);
1360 }
1361
1362 /*3 bytes of data get encoded into two bytes. The hash cannot use more than 3
1363 bytes as input because 3 is the minimum match length for deflate*/
1364 static const unsigned HASH_NUM_VALUES = 65536;
1365 static const unsigned HASH_BIT_MASK = 65535; /*HASH_NUM_VALUES - 1, but C90 does not like that as initializer*/
1366
1367 typedef struct Hash
1368 {
1369 int* head; /*hash value to head circular pos - can be outdated if went around window*/
1370 /*circular pos to prev circular pos*/
1371 unsigned short* chain;
1372 int* val; /*circular pos to hash value*/
1373
1374 /*TODO: do this not only for zeros but for any repeated byte. However for PNG
1375 it's always going to be the zeros that dominate, so not important for PNG*/
1376 int* headz; /*similar to head, but for chainz*/
1377 unsigned short* chainz; /*those with same amount of zeros*/
1378 unsigned short* zeros; /*length of zeros streak, used as a second hash chain*/
1379 } Hash;
1380
hash_init(Hash * hash,unsigned windowsize)1381 static unsigned hash_init(Hash* hash, unsigned windowsize)
1382 {
1383 unsigned i;
1384 hash->head = (int*)lodepng_malloc(sizeof(int) * HASH_NUM_VALUES);
1385 hash->val = (int*)lodepng_malloc(sizeof(int) * windowsize);
1386 hash->chain = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
1387
1388 hash->zeros = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
1389 hash->headz = (int*)lodepng_malloc(sizeof(int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1));
1390 hash->chainz = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
1391
1392 if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros)
1393 {
1394 return 83; /*alloc fail*/
1395 }
1396
1397 /*initialize hash table*/
1398 for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1;
1399 for(i = 0; i != windowsize; ++i) hash->val[i] = -1;
1400 for(i = 0; i != windowsize; ++i) hash->chain[i] = i; /*same value as index indicates uninitialized*/
1401
1402 for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1;
1403 for(i = 0; i != windowsize; ++i) hash->chainz[i] = i; /*same value as index indicates uninitialized*/
1404
1405 return 0;
1406 }
1407
hash_cleanup(Hash * hash)1408 static void hash_cleanup(Hash* hash)
1409 {
1410 lodepng_free(hash->head);
1411 lodepng_free(hash->val);
1412 lodepng_free(hash->chain);
1413
1414 lodepng_free(hash->zeros);
1415 lodepng_free(hash->headz);
1416 lodepng_free(hash->chainz);
1417 }
1418
1419
1420
getHash(const unsigned char * data,size_t size,size_t pos)1421 static unsigned getHash(const unsigned char* data, size_t size, size_t pos)
1422 {
1423 unsigned result = 0;
1424 if(pos + 2 < size)
1425 {
1426 /*A simple shift and xor hash is used. Since the data of PNGs is dominated
1427 by zeroes due to the filters, a better hash does not have a significant
1428 effect on speed in traversing the chain, and causes more time spend on
1429 calculating the hash.*/
1430 result ^= (unsigned)(data[pos + 0] << 0u);
1431 result ^= (unsigned)(data[pos + 1] << 4u);
1432 result ^= (unsigned)(data[pos + 2] << 8u);
1433 } else {
1434 size_t amount, i;
1435 if(pos >= size) return 0;
1436 amount = size - pos;
1437 for(i = 0; i != amount; ++i) result ^= (unsigned)(data[pos + i] << (i * 8u));
1438 }
1439 return result & HASH_BIT_MASK;
1440 }
1441
countZeros(const unsigned char * data,size_t size,size_t pos)1442 static unsigned countZeros(const unsigned char* data, size_t size, size_t pos)
1443 {
1444 const unsigned char* start = data + pos;
1445 const unsigned char* end = start + MAX_SUPPORTED_DEFLATE_LENGTH;
1446 if(end > data + size) end = data + size;
1447 data = start;
1448 while(data != end && *data == 0) ++data;
1449 /*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/
1450 return (unsigned)(data - start);
1451 }
1452
1453 /*wpos = pos & (windowsize - 1)*/
updateHashChain(Hash * hash,size_t wpos,unsigned hashval,unsigned short numzeros)1454 static void updateHashChain(Hash* hash, size_t wpos, unsigned hashval, unsigned short numzeros)
1455 {
1456 hash->val[wpos] = (int)hashval;
1457 if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval];
1458 hash->head[hashval] = wpos;
1459
1460 hash->zeros[wpos] = numzeros;
1461 if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros];
1462 hash->headz[numzeros] = wpos;
1463 }
1464
1465 /*
1466 LZ77-encode the data. Return value is error code. The input are raw bytes, the output
1467 is in the form of unsigned integers with codes representing for example literal bytes, or
1468 length/distance pairs.
1469 It uses a hash table technique to let it encode faster. When doing LZ77 encoding, a
1470 sliding window (of windowsize) is used, and all past bytes in that window can be used as
1471 the "dictionary". A brute force search through all possible distances would be slow, and
1472 this hash technique is one out of several ways to speed this up.
1473 */
encodeLZ77(uivector * out,Hash * hash,const unsigned char * in,size_t inpos,size_t insize,unsigned windowsize,unsigned minmatch,unsigned nicematch,unsigned lazymatching)1474 static unsigned encodeLZ77(uivector* out, Hash* hash,
1475 const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize,
1476 unsigned minmatch, unsigned nicematch, unsigned lazymatching)
1477 {
1478 size_t pos;
1479 unsigned i, error = 0;
1480 /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/
1481 unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8;
1482 unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64;
1483
1484 unsigned usezeros = 1; /*not sure if setting it to false for windowsize < 8192 is better or worse*/
1485 unsigned numzeros = 0;
1486
1487 unsigned offset; /*the offset represents the distance in LZ77 terminology*/
1488 unsigned length;
1489 unsigned lazy = 0;
1490 unsigned lazylength = 0, lazyoffset = 0;
1491 unsigned hashval;
1492 unsigned current_offset, current_length;
1493 unsigned prev_offset;
1494 const unsigned char *lastptr, *foreptr, *backptr;
1495 unsigned hashpos;
1496
1497 if(windowsize == 0 || windowsize > 32768) return 60; /*error: windowsize smaller/larger than allowed*/
1498 if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/
1499
1500 if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH;
1501
1502 for(pos = inpos; pos < insize; ++pos)
1503 {
1504 size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/
1505 unsigned chainlength = 0;
1506
1507 hashval = getHash(in, insize, pos);
1508
1509 if(usezeros && hashval == 0)
1510 {
1511 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
1512 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
1513 }
1514 else
1515 {
1516 numzeros = 0;
1517 }
1518
1519 updateHashChain(hash, wpos, hashval, numzeros);
1520
1521 /*the length and offset found for the current position*/
1522 length = 0;
1523 offset = 0;
1524
1525 hashpos = hash->chain[wpos];
1526
1527 lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];
1528
1529 /*search for the longest string*/
1530 prev_offset = 0;
1531 for(;;)
1532 {
1533 if(chainlength++ >= maxchainlength) break;
1534 current_offset = hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize;
1535
1536 if(current_offset < prev_offset) break; /*stop when went completely around the circular buffer*/
1537 prev_offset = current_offset;
1538 if(current_offset > 0)
1539 {
1540 /*test the next characters*/
1541 foreptr = &in[pos];
1542 backptr = &in[pos - current_offset];
1543
1544 /*common case in PNGs is lots of zeros. Quickly skip over them as a speedup*/
1545 if(numzeros >= 3)
1546 {
1547 unsigned skip = hash->zeros[hashpos];
1548 if(skip > numzeros) skip = numzeros;
1549 backptr += skip;
1550 foreptr += skip;
1551 }
1552
1553 while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/
1554 {
1555 ++backptr;
1556 ++foreptr;
1557 }
1558 current_length = (unsigned)(foreptr - &in[pos]);
1559
1560 if(current_length > length)
1561 {
1562 length = current_length; /*the longest length*/
1563 offset = current_offset; /*the offset that is related to this longest length*/
1564 /*jump out once a length of max length is found (speed gain). This also jumps
1565 out if length is MAX_SUPPORTED_DEFLATE_LENGTH*/
1566 if(current_length >= nicematch) break;
1567 }
1568 }
1569
1570 if(hashpos == hash->chain[hashpos]) break;
1571
1572 if(numzeros >= 3 && length > numzeros)
1573 {
1574 hashpos = hash->chainz[hashpos];
1575 if(hash->zeros[hashpos] != numzeros) break;
1576 }
1577 else
1578 {
1579 hashpos = hash->chain[hashpos];
1580 /*outdated hash value, happens if particular value was not encountered in whole last window*/
1581 if(hash->val[hashpos] != (int)hashval) break;
1582 }
1583 }
1584
1585 if(lazymatching)
1586 {
1587 if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH)
1588 {
1589 lazy = 1;
1590 lazylength = length;
1591 lazyoffset = offset;
1592 continue; /*try the next byte*/
1593 }
1594 if(lazy)
1595 {
1596 lazy = 0;
1597 if(pos == 0) ERROR_BREAK(81);
1598 if(length > lazylength + 1)
1599 {
1600 /*push the previous character as literal*/
1601 if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/);
1602 }
1603 else
1604 {
1605 length = lazylength;
1606 offset = lazyoffset;
1607 hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/
1608 hash->headz[numzeros] = -1; /*idem*/
1609 --pos;
1610 }
1611 }
1612 }
1613 if(length >= 3 && offset > windowsize) ERROR_BREAK(86 /*too big (or overflown negative) offset*/);
1614
1615 /*encode it as length/distance pair or literal value*/
1616 if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/
1617 {
1618 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
1619 }
1620 else if(length < minmatch || (length == 3 && offset > 4096))
1621 {
1622 /*compensate for the fact that longer offsets have more extra bits, a
1623 length of only 3 may be not worth it then*/
1624 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
1625 }
1626 else
1627 {
1628 addLengthDistance(out, length, offset);
1629 for(i = 1; i < length; ++i)
1630 {
1631 ++pos;
1632 wpos = pos & (windowsize - 1);
1633 hashval = getHash(in, insize, pos);
1634 if(usezeros && hashval == 0)
1635 {
1636 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
1637 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
1638 }
1639 else
1640 {
1641 numzeros = 0;
1642 }
1643 updateHashChain(hash, wpos, hashval, numzeros);
1644 }
1645 }
1646 } /*end of the loop through each character of input*/
1647
1648 return error;
1649 }
1650
1651 /* /////////////////////////////////////////////////////////////////////////// */
1652
deflateNoCompression(ucvector * out,const unsigned char * data,size_t datasize)1653 static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize)
1654 {
1655 /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte,
1656 2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/
1657
1658 size_t i, j, numdeflateblocks = (datasize + 65534) / 65535;
1659 unsigned datapos = 0;
1660 for(i = 0; i != numdeflateblocks; ++i)
1661 {
1662 unsigned BFINAL, BTYPE, LEN, NLEN;
1663 unsigned char firstbyte;
1664
1665 BFINAL = (i == numdeflateblocks - 1);
1666 BTYPE = 0;
1667
1668 firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1) << 1) + ((BTYPE & 2) << 1));
1669 ucvector_push_back(out, firstbyte);
1670
1671 LEN = 65535;
1672 if(datasize - datapos < 65535) LEN = (unsigned)datasize - datapos;
1673 NLEN = 65535 - LEN;
1674
1675 ucvector_push_back(out, (unsigned char)(LEN & 255));
1676 ucvector_push_back(out, (unsigned char)(LEN >> 8));
1677 ucvector_push_back(out, (unsigned char)(NLEN & 255));
1678 ucvector_push_back(out, (unsigned char)(NLEN >> 8));
1679
1680 /*Decompressed data*/
1681 for(j = 0; j < 65535 && datapos < datasize; ++j)
1682 {
1683 ucvector_push_back(out, data[datapos++]);
1684 }
1685 }
1686
1687 return 0;
1688 }
1689
1690 /*
1691 write the lz77-encoded data, which has lit, len and dist codes, to compressed stream using huffman trees.
1692 tree_ll: the tree for lit and len codes.
1693 tree_d: the tree for distance codes.
1694 */
writeLZ77data(size_t * bp,ucvector * out,const uivector * lz77_encoded,const HuffmanTree * tree_ll,const HuffmanTree * tree_d)1695 static void writeLZ77data(size_t* bp, ucvector* out, const uivector* lz77_encoded,
1696 const HuffmanTree* tree_ll, const HuffmanTree* tree_d)
1697 {
1698 size_t i = 0;
1699 for(i = 0; i != lz77_encoded->size; ++i)
1700 {
1701 unsigned val = lz77_encoded->data[i];
1702 addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val));
1703 if(val > 256) /*for a length code, 3 more things have to be added*/
1704 {
1705 unsigned length_index = val - FIRST_LENGTH_CODE_INDEX;
1706 unsigned n_length_extra_bits = LENGTHEXTRA[length_index];
1707 unsigned length_extra_bits = lz77_encoded->data[++i];
1708
1709 unsigned distance_code = lz77_encoded->data[++i];
1710
1711 unsigned distance_index = distance_code;
1712 unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index];
1713 unsigned distance_extra_bits = lz77_encoded->data[++i];
1714
1715 addBitsToStream(bp, out, length_extra_bits, n_length_extra_bits);
1716 addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_d, distance_code),
1717 HuffmanTree_getLength(tree_d, distance_code));
1718 addBitsToStream(bp, out, distance_extra_bits, n_distance_extra_bits);
1719 }
1720 }
1721 }
1722
1723 /*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/
deflateDynamic(ucvector * out,size_t * bp,Hash * hash,const unsigned char * data,size_t datapos,size_t dataend,const LodePNGCompressSettings * settings,unsigned final)1724 static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash,
1725 const unsigned char* data, size_t datapos, size_t dataend,
1726 const LodePNGCompressSettings* settings, unsigned final)
1727 {
1728 unsigned error = 0;
1729
1730 /*
1731 A block is compressed as follows: The PNG data is lz77 encoded, resulting in
1732 literal bytes and length/distance pairs. This is then huffman compressed with
1733 two huffman trees. One huffman tree is used for the lit and len values ("ll"),
1734 another huffman tree is used for the dist values ("d"). These two trees are
1735 stored using their code lengths, and to compress even more these code lengths
1736 are also run-length encoded and huffman compressed. This gives a huffman tree
1737 of code lengths "cl". The code lenghts used to describe this third tree are
1738 the code length code lengths ("clcl").
1739 */
1740
1741 /*The lz77 encoded data, represented with integers since there will also be length and distance codes in it*/
1742 uivector lz77_encoded;
1743 HuffmanTree tree_ll; /*tree for lit,len values*/
1744 HuffmanTree tree_d; /*tree for distance codes*/
1745 HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/
1746 uivector frequencies_ll; /*frequency of lit,len codes*/
1747 uivector frequencies_d; /*frequency of dist codes*/
1748 uivector frequencies_cl; /*frequency of code length codes*/
1749 uivector bitlen_lld; /*lit,len,dist code lenghts (int bits), literally (without repeat codes).*/
1750 uivector bitlen_lld_e; /*bitlen_lld encoded with repeat codes (this is a rudemtary run length compression)*/
1751 /*bitlen_cl is the code length code lengths ("clcl"). The bit lengths of codes to represent tree_cl
1752 (these are written as is in the file, it would be crazy to compress these using yet another huffman
1753 tree that needs to be represented by yet another set of code lengths)*/
1754 uivector bitlen_cl;
1755 size_t datasize = dataend - datapos;
1756
1757 /*
1758 Due to the huffman compression of huffman tree representations ("two levels"), there are some anologies:
1759 bitlen_lld is to tree_cl what data is to tree_ll and tree_d.
1760 bitlen_lld_e is to bitlen_lld what lz77_encoded is to data.
1761 bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded.
1762 */
1763
1764 unsigned BFINAL = final;
1765 size_t numcodes_ll, numcodes_d, i;
1766 unsigned HLIT, HDIST, HCLEN;
1767
1768 uivector_init(&lz77_encoded);
1769 HuffmanTree_init(&tree_ll);
1770 HuffmanTree_init(&tree_d);
1771 HuffmanTree_init(&tree_cl);
1772 uivector_init(&frequencies_ll);
1773 uivector_init(&frequencies_d);
1774 uivector_init(&frequencies_cl);
1775 uivector_init(&bitlen_lld);
1776 uivector_init(&bitlen_lld_e);
1777 uivector_init(&bitlen_cl);
1778
1779 /*This while loop never loops due to a break at the end, it is here to
1780 allow breaking out of it to the cleanup phase on error conditions.*/
1781 while(!error)
1782 {
1783 if(settings->use_lz77)
1784 {
1785 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize,
1786 settings->minmatch, settings->nicematch, settings->lazymatching);
1787 if(error) break;
1788 }
1789 else
1790 {
1791 if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83 /*alloc fail*/);
1792 for(i = datapos; i < dataend; ++i) lz77_encoded.data[i - datapos] = data[i]; /*no LZ77, but still will be Huffman compressed*/
1793 }
1794
1795 if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83 /*alloc fail*/);
1796 if(!uivector_resizev(&frequencies_d, 30, 0)) ERROR_BREAK(83 /*alloc fail*/);
1797
1798 /*Count the frequencies of lit, len and dist codes*/
1799 for(i = 0; i != lz77_encoded.size; ++i)
1800 {
1801 unsigned symbol = lz77_encoded.data[i];
1802 ++frequencies_ll.data[symbol];
1803 if(symbol > 256)
1804 {
1805 unsigned dist = lz77_encoded.data[i + 2];
1806 ++frequencies_d.data[dist];
1807 i += 3;
1808 }
1809 }
1810 frequencies_ll.data[256] = 1; /*there will be exactly 1 end code, at the end of the block*/
1811
1812 /*Make both huffman trees, one for the lit and len codes, one for the dist codes*/
1813 error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll.data, 257, frequencies_ll.size, 15);
1814 if(error) break;
1815 /*2, not 1, is chosen for mincodes: some buggy PNG decoders require at least 2 symbols in the dist tree*/
1816 error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d.data, 2, frequencies_d.size, 15);
1817 if(error) break;
1818
1819 numcodes_ll = tree_ll.numcodes; if(numcodes_ll > 286) numcodes_ll = 286;
1820 numcodes_d = tree_d.numcodes; if(numcodes_d > 30) numcodes_d = 30;
1821 /*store the code lengths of both generated trees in bitlen_lld*/
1822 for(i = 0; i != numcodes_ll; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (unsigned)i));
1823 for(i = 0; i != numcodes_d; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (unsigned)i));
1824
1825 /*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times),
1826 17 (3-10 zeroes), 18 (11-138 zeroes)*/
1827 for(i = 0; i != (unsigned)bitlen_lld.size; ++i)
1828 {
1829 unsigned j = 0; /*amount of repititions*/
1830 while(i + j + 1 < (unsigned)bitlen_lld.size && bitlen_lld.data[i + j + 1] == bitlen_lld.data[i]) ++j;
1831
1832 if(bitlen_lld.data[i] == 0 && j >= 2) /*repeat code for zeroes*/
1833 {
1834 ++j; /*include the first zero*/
1835 if(j <= 10) /*repeat code 17 supports max 10 zeroes*/
1836 {
1837 uivector_push_back(&bitlen_lld_e, 17);
1838 uivector_push_back(&bitlen_lld_e, j - 3);
1839 }
1840 else /*repeat code 18 supports max 138 zeroes*/
1841 {
1842 if(j > 138) j = 138;
1843 uivector_push_back(&bitlen_lld_e, 18);
1844 uivector_push_back(&bitlen_lld_e, j - 11);
1845 }
1846 i += (j - 1);
1847 }
1848 else if(j >= 3) /*repeat code for value other than zero*/
1849 {
1850 size_t k;
1851 unsigned num = j / 6, rest = j % 6;
1852 uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]);
1853 for(k = 0; k < num; ++k)
1854 {
1855 uivector_push_back(&bitlen_lld_e, 16);
1856 uivector_push_back(&bitlen_lld_e, 6 - 3);
1857 }
1858 if(rest >= 3)
1859 {
1860 uivector_push_back(&bitlen_lld_e, 16);
1861 uivector_push_back(&bitlen_lld_e, rest - 3);
1862 }
1863 else j -= rest;
1864 i += j;
1865 }
1866 else /*too short to benefit from repeat code*/
1867 {
1868 uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]);
1869 }
1870 }
1871
1872 /*generate tree_cl, the huffmantree of huffmantrees*/
1873
1874 if(!uivector_resizev(&frequencies_cl, NUM_CODE_LENGTH_CODES, 0)) ERROR_BREAK(83 /*alloc fail*/);
1875 for(i = 0; i != bitlen_lld_e.size; ++i)
1876 {
1877 ++frequencies_cl.data[bitlen_lld_e.data[i]];
1878 /*after a repeat code come the bits that specify the number of repetitions,
1879 those don't need to be in the frequencies_cl calculation*/
1880 if(bitlen_lld_e.data[i] >= 16) ++i;
1881 }
1882
1883 error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl.data,
1884 frequencies_cl.size, frequencies_cl.size, 7);
1885 if(error) break;
1886
1887 if(!uivector_resize(&bitlen_cl, tree_cl.numcodes)) ERROR_BREAK(83 /*alloc fail*/);
1888 for(i = 0; i != tree_cl.numcodes; ++i)
1889 {
1890 /*lenghts of code length tree is in the order as specified by deflate*/
1891 bitlen_cl.data[i] = HuffmanTree_getLength(&tree_cl, CLCL_ORDER[i]);
1892 }
1893 while(bitlen_cl.data[bitlen_cl.size - 1] == 0 && bitlen_cl.size > 4)
1894 {
1895 /*remove zeros at the end, but minimum size must be 4*/
1896 if(!uivector_resize(&bitlen_cl, bitlen_cl.size - 1)) ERROR_BREAK(83 /*alloc fail*/);
1897 }
1898 if(error) break;
1899
1900 /*
1901 Write everything into the output
1902
1903 After the BFINAL and BTYPE, the dynamic block consists out of the following:
1904 - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN
1905 - (HCLEN+4)*3 bits code lengths of code length alphabet
1906 - HLIT + 257 code lenghts of lit/length alphabet (encoded using the code length
1907 alphabet, + possible repetition codes 16, 17, 18)
1908 - HDIST + 1 code lengths of distance alphabet (encoded using the code length
1909 alphabet, + possible repetition codes 16, 17, 18)
1910 - compressed data
1911 - 256 (end code)
1912 */
1913
1914 /*Write block type*/
1915 addBitToStream(bp, out, BFINAL);
1916 addBitToStream(bp, out, 0); /*first bit of BTYPE "dynamic"*/
1917 addBitToStream(bp, out, 1); /*second bit of BTYPE "dynamic"*/
1918
1919 /*write the HLIT, HDIST and HCLEN values*/
1920 HLIT = (unsigned)(numcodes_ll - 257);
1921 HDIST = (unsigned)(numcodes_d - 1);
1922 HCLEN = (unsigned)bitlen_cl.size - 4;
1923 /*trim zeroes for HCLEN. HLIT and HDIST were already trimmed at tree creation*/
1924 while(!bitlen_cl.data[HCLEN + 4 - 1] && HCLEN > 0) --HCLEN;
1925 addBitsToStream(bp, out, HLIT, 5);
1926 addBitsToStream(bp, out, HDIST, 5);
1927 addBitsToStream(bp, out, HCLEN, 4);
1928
1929 /*write the code lenghts of the code length alphabet*/
1930 for(i = 0; i != HCLEN + 4; ++i) addBitsToStream(bp, out, bitlen_cl.data[i], 3);
1931
1932 /*write the lenghts of the lit/len AND the dist alphabet*/
1933 for(i = 0; i != bitlen_lld_e.size; ++i)
1934 {
1935 addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_cl, bitlen_lld_e.data[i]),
1936 HuffmanTree_getLength(&tree_cl, bitlen_lld_e.data[i]));
1937 /*extra bits of repeat codes*/
1938 if(bitlen_lld_e.data[i] == 16) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 2);
1939 else if(bitlen_lld_e.data[i] == 17) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 3);
1940 else if(bitlen_lld_e.data[i] == 18) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 7);
1941 }
1942
1943 /*write the compressed data symbols*/
1944 writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d);
1945 /*error: the length of the end code 256 must be larger than 0*/
1946 if(HuffmanTree_getLength(&tree_ll, 256) == 0) ERROR_BREAK(64);
1947
1948 /*write the end code*/
1949 addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256));
1950
1951 break; /*end of error-while*/
1952 }
1953
1954 /*cleanup*/
1955 uivector_cleanup(&lz77_encoded);
1956 HuffmanTree_cleanup(&tree_ll);
1957 HuffmanTree_cleanup(&tree_d);
1958 HuffmanTree_cleanup(&tree_cl);
1959 uivector_cleanup(&frequencies_ll);
1960 uivector_cleanup(&frequencies_d);
1961 uivector_cleanup(&frequencies_cl);
1962 uivector_cleanup(&bitlen_lld_e);
1963 uivector_cleanup(&bitlen_lld);
1964 uivector_cleanup(&bitlen_cl);
1965
1966 return error;
1967 }
1968
deflateFixed(ucvector * out,size_t * bp,Hash * hash,const unsigned char * data,size_t datapos,size_t dataend,const LodePNGCompressSettings * settings,unsigned final)1969 static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash,
1970 const unsigned char* data,
1971 size_t datapos, size_t dataend,
1972 const LodePNGCompressSettings* settings, unsigned final)
1973 {
1974 HuffmanTree tree_ll; /*tree for literal values and length codes*/
1975 HuffmanTree tree_d; /*tree for distance codes*/
1976
1977 unsigned BFINAL = final;
1978 unsigned error = 0;
1979 size_t i;
1980
1981 HuffmanTree_init(&tree_ll);
1982 HuffmanTree_init(&tree_d);
1983
1984 generateFixedLitLenTree(&tree_ll);
1985 generateFixedDistanceTree(&tree_d);
1986
1987 addBitToStream(bp, out, BFINAL);
1988 addBitToStream(bp, out, 1); /*first bit of BTYPE*/
1989 addBitToStream(bp, out, 0); /*second bit of BTYPE*/
1990
1991 if(settings->use_lz77) /*LZ77 encoded*/
1992 {
1993 uivector lz77_encoded;
1994 uivector_init(&lz77_encoded);
1995 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize,
1996 settings->minmatch, settings->nicematch, settings->lazymatching);
1997 if(!error) writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d);
1998 uivector_cleanup(&lz77_encoded);
1999 }
2000 else /*no LZ77, but still will be Huffman compressed*/
2001 {
2002 for(i = datapos; i < dataend; ++i)
2003 {
2004 addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i]));
2005 }
2006 }
2007 /*add END code*/
2008 if(!error) addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256));
2009
2010 /*cleanup*/
2011 HuffmanTree_cleanup(&tree_ll);
2012 HuffmanTree_cleanup(&tree_d);
2013
2014 return error;
2015 }
2016
lodepng_deflatev(ucvector * out,const unsigned char * in,size_t insize,const LodePNGCompressSettings * settings)2017 static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t insize,
2018 const LodePNGCompressSettings* settings)
2019 {
2020 unsigned error = 0;
2021 size_t i, blocksize, numdeflateblocks;
2022 size_t bp = 0; /*the bit pointer*/
2023 Hash hash;
2024
2025 if(settings->btype > 2) return 61;
2026 else if(settings->btype == 0) return deflateNoCompression(out, in, insize);
2027 else if(settings->btype == 1) blocksize = insize;
2028 else /*if(settings->btype == 2)*/
2029 {
2030 /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/
2031 blocksize = insize / 8 + 8;
2032 if(blocksize < 65536) blocksize = 65536;
2033 if(blocksize > 262144) blocksize = 262144;
2034 }
2035
2036 numdeflateblocks = (insize + blocksize - 1) / blocksize;
2037 if(numdeflateblocks == 0) numdeflateblocks = 1;
2038
2039 error = hash_init(&hash, settings->windowsize);
2040 if(error) return error;
2041
2042 for(i = 0; i != numdeflateblocks && !error; ++i)
2043 {
2044 unsigned final = (i == numdeflateblocks - 1);
2045 size_t start = i * blocksize;
2046 size_t end = start + blocksize;
2047 if(end > insize) end = insize;
2048
2049 if(settings->btype == 1) error = deflateFixed(out, &bp, &hash, in, start, end, settings, final);
2050 else if(settings->btype == 2) error = deflateDynamic(out, &bp, &hash, in, start, end, settings, final);
2051 }
2052
2053 hash_cleanup(&hash);
2054
2055 return error;
2056 }
2057
lodepng_deflate(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGCompressSettings * settings)2058 unsigned lodepng_deflate(unsigned char** out, size_t* outsize,
2059 const unsigned char* in, size_t insize,
2060 const LodePNGCompressSettings* settings)
2061 {
2062 unsigned error;
2063 ucvector v;
2064 ucvector_init_buffer(&v, *out, *outsize);
2065 error = lodepng_deflatev(&v, in, insize, settings);
2066 *out = v.data;
2067 *outsize = v.size;
2068 return error;
2069 }
2070
deflate(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGCompressSettings * settings)2071 static unsigned deflate(unsigned char** out, size_t* outsize,
2072 const unsigned char* in, size_t insize,
2073 const LodePNGCompressSettings* settings)
2074 {
2075 if(settings->custom_deflate)
2076 {
2077 return settings->custom_deflate(out, outsize, in, insize, settings);
2078 }
2079 else
2080 {
2081 return lodepng_deflate(out, outsize, in, insize, settings);
2082 }
2083 }
2084
2085 #endif /*LODEPNG_COMPILE_DECODER*/
2086
2087 /* ////////////////////////////////////////////////////////////////////////// */
2088 /* / Adler32 */
2089 /* ////////////////////////////////////////////////////////////////////////// */
2090
update_adler32(unsigned adler,const unsigned char * data,unsigned len)2091 static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len)
2092 {
2093 unsigned s1 = adler & 0xffff;
2094 unsigned s2 = (adler >> 16) & 0xffff;
2095
2096 while(len > 0)
2097 {
2098 /*at least 5550 sums can be done before the sums overflow, saving a lot of module divisions*/
2099 unsigned amount = len > 5550 ? 5550 : len;
2100 len -= amount;
2101 while(amount > 0)
2102 {
2103 s1 += (*data++);
2104 s2 += s1;
2105 --amount;
2106 }
2107 s1 %= 65521;
2108 s2 %= 65521;
2109 }
2110
2111 return (s2 << 16) | s1;
2112 }
2113
2114 /*Return the adler32 of the bytes data[0..len-1]*/
adler32(const unsigned char * data,unsigned len)2115 static unsigned adler32(const unsigned char* data, unsigned len)
2116 {
2117 return update_adler32(1L, data, len);
2118 }
2119
2120 /* ////////////////////////////////////////////////////////////////////////// */
2121 /* / Zlib / */
2122 /* ////////////////////////////////////////////////////////////////////////// */
2123
2124 #ifdef LODEPNG_COMPILE_DECODER
2125
lodepng_zlib_decompress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGDecompressSettings * settings)2126 unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
2127 size_t insize, const LodePNGDecompressSettings* settings)
2128 {
2129 unsigned error = 0;
2130 unsigned CM, CINFO, FDICT;
2131
2132 if(insize < 2) return 53; /*error, size of zlib data too small*/
2133 /*read information from zlib header*/
2134 if((in[0] * 256 + in[1]) % 31 != 0)
2135 {
2136 /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/
2137 return 24;
2138 }
2139
2140 CM = in[0] & 15;
2141 CINFO = (in[0] >> 4) & 15;
2142 /*FCHECK = in[1] & 31;*/ /*FCHECK is already tested above*/
2143 FDICT = (in[1] >> 5) & 1;
2144 /*FLEVEL = (in[1] >> 6) & 3;*/ /*FLEVEL is not used here*/
2145
2146 if(CM != 8 || CINFO > 7)
2147 {
2148 /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/
2149 return 25;
2150 }
2151 if(FDICT != 0)
2152 {
2153 /*error: the specification of PNG says about the zlib stream:
2154 "The additional flags shall not specify a preset dictionary."*/
2155 return 26;
2156 }
2157
2158 error = inflate(out, outsize, in + 2, insize - 2, settings);
2159 if(error) return error;
2160
2161 if(!settings->ignore_adler32)
2162 {
2163 unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]);
2164 unsigned checksum = adler32(*out, (unsigned)(*outsize));
2165 if(checksum != ADLER32) return 58; /*error, adler checksum not correct, data must be corrupted*/
2166 }
2167
2168 return 0; /*no error*/
2169 }
2170
zlib_decompress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGDecompressSettings * settings)2171 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
2172 size_t insize, const LodePNGDecompressSettings* settings)
2173 {
2174 if(settings->custom_zlib)
2175 {
2176 return settings->custom_zlib(out, outsize, in, insize, settings);
2177 }
2178 else
2179 {
2180 return lodepng_zlib_decompress(out, outsize, in, insize, settings);
2181 }
2182 }
2183
2184 #endif /*LODEPNG_COMPILE_DECODER*/
2185
2186 #ifdef LODEPNG_COMPILE_ENCODER
2187
lodepng_zlib_compress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGCompressSettings * settings)2188 unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
2189 size_t insize, const LodePNGCompressSettings* settings)
2190 {
2191 /*initially, *out must be NULL and outsize 0, if you just give some random *out
2192 that's pointing to a non allocated buffer, this'll crash*/
2193 ucvector outv;
2194 size_t i;
2195 unsigned error;
2196 unsigned char* deflatedata = 0;
2197 size_t deflatesize = 0;
2198
2199 /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
2200 unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
2201 unsigned FLEVEL = 0;
2202 unsigned FDICT = 0;
2203 unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
2204 unsigned FCHECK = 31 - CMFFLG % 31;
2205 CMFFLG += FCHECK;
2206
2207 /*ucvector-controlled version of the output buffer, for dynamic array*/
2208 ucvector_init_buffer(&outv, *out, *outsize);
2209
2210 ucvector_push_back(&outv, (unsigned char)(CMFFLG >> 8));
2211 ucvector_push_back(&outv, (unsigned char)(CMFFLG & 255));
2212
2213 error = deflate(&deflatedata, &deflatesize, in, insize, settings);
2214
2215 if(!error)
2216 {
2217 unsigned ADLER32 = adler32(in, (unsigned)insize);
2218 for(i = 0; i != deflatesize; ++i) ucvector_push_back(&outv, deflatedata[i]);
2219 lodepng_free(deflatedata);
2220 lodepng_add32bitInt(&outv, ADLER32);
2221 }
2222
2223 *out = outv.data;
2224 *outsize = outv.size;
2225
2226 return error;
2227 }
2228
2229 /* compress using the default or custom zlib function */
zlib_compress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGCompressSettings * settings)2230 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
2231 size_t insize, const LodePNGCompressSettings* settings)
2232 {
2233 if(settings->custom_zlib)
2234 {
2235 return settings->custom_zlib(out, outsize, in, insize, settings);
2236 }
2237 else
2238 {
2239 return lodepng_zlib_compress(out, outsize, in, insize, settings);
2240 }
2241 }
2242
2243 #endif /*LODEPNG_COMPILE_ENCODER*/
2244
2245 #else /*no LODEPNG_COMPILE_ZLIB*/
2246
2247 #ifdef LODEPNG_COMPILE_DECODER
zlib_decompress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGDecompressSettings * settings)2248 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
2249 size_t insize, const LodePNGDecompressSettings* settings)
2250 {
2251 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */
2252 return settings->custom_zlib(out, outsize, in, insize, settings);
2253 }
2254 #endif /*LODEPNG_COMPILE_DECODER*/
2255 #ifdef LODEPNG_COMPILE_ENCODER
zlib_compress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodePNGCompressSettings * settings)2256 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
2257 size_t insize, const LodePNGCompressSettings* settings)
2258 {
2259 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */
2260 return settings->custom_zlib(out, outsize, in, insize, settings);
2261 }
2262 #endif /*LODEPNG_COMPILE_ENCODER*/
2263
2264 #endif /*LODEPNG_COMPILE_ZLIB*/
2265
2266 /* ////////////////////////////////////////////////////////////////////////// */
2267
2268 #ifdef LODEPNG_COMPILE_ENCODER
2269
2270 /*this is a good tradeoff between speed and compression ratio*/
2271 #define DEFAULT_WINDOWSIZE 2048
2272
lodepng_compress_settings_init(LodePNGCompressSettings * settings)2273 void lodepng_compress_settings_init(LodePNGCompressSettings* settings)
2274 {
2275 /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/
2276 settings->btype = 2;
2277 settings->use_lz77 = 1;
2278 settings->windowsize = DEFAULT_WINDOWSIZE;
2279 settings->minmatch = 3;
2280 settings->nicematch = 128;
2281 settings->lazymatching = 1;
2282
2283 settings->custom_zlib = 0;
2284 settings->custom_deflate = 0;
2285 settings->custom_context = 0;
2286 }
2287
2288 const LodePNGCompressSettings lodepng_default_compress_settings = {2, 1, DEFAULT_WINDOWSIZE, 3, 128, 1, 0, 0, 0};
2289
2290
2291 #endif /*LODEPNG_COMPILE_ENCODER*/
2292
2293 #ifdef LODEPNG_COMPILE_DECODER
2294
lodepng_decompress_settings_init(LodePNGDecompressSettings * settings)2295 void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings)
2296 {
2297 settings->ignore_adler32 = 0;
2298
2299 settings->custom_zlib = 0;
2300 settings->custom_inflate = 0;
2301 settings->custom_context = 0;
2302 }
2303
2304 const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0, 0};
2305
2306 #endif /*LODEPNG_COMPILE_DECODER*/
2307
2308 /* ////////////////////////////////////////////////////////////////////////// */
2309 /* ////////////////////////////////////////////////////////////////////////// */
2310 /* // End of Zlib related code. Begin of PNG related code. // */
2311 /* ////////////////////////////////////////////////////////////////////////// */
2312 /* ////////////////////////////////////////////////////////////////////////// */
2313
2314 #ifdef LODEPNG_COMPILE_PNG
2315
2316 /* ////////////////////////////////////////////////////////////////////////// */
2317 /* / CRC32 / */
2318 /* ////////////////////////////////////////////////////////////////////////// */
2319
2320
2321 #ifndef LODEPNG_NO_COMPILE_CRC
2322 /* CRC polynomial: 0xedb88320 */
2323 static unsigned lodepng_crc32_table[256] = {
2324 0u, 1996959894u, 3993919788u, 2567524794u, 124634137u, 1886057615u, 3915621685u, 2657392035u,
2325 249268274u, 2044508324u, 3772115230u, 2547177864u, 162941995u, 2125561021u, 3887607047u, 2428444049u,
2326 498536548u, 1789927666u, 4089016648u, 2227061214u, 450548861u, 1843258603u, 4107580753u, 2211677639u,
2327 325883990u, 1684777152u, 4251122042u, 2321926636u, 335633487u, 1661365465u, 4195302755u, 2366115317u,
2328 997073096u, 1281953886u, 3579855332u, 2724688242u, 1006888145u, 1258607687u, 3524101629u, 2768942443u,
2329 901097722u, 1119000684u, 3686517206u, 2898065728u, 853044451u, 1172266101u, 3705015759u, 2882616665u,
2330 651767980u, 1373503546u, 3369554304u, 3218104598u, 565507253u, 1454621731u, 3485111705u, 3099436303u,
2331 671266974u, 1594198024u, 3322730930u, 2970347812u, 795835527u, 1483230225u, 3244367275u, 3060149565u,
2332 1994146192u, 31158534u, 2563907772u, 4023717930u, 1907459465u, 112637215u, 2680153253u, 3904427059u,
2333 2013776290u, 251722036u, 2517215374u, 3775830040u, 2137656763u, 141376813u, 2439277719u, 3865271297u,
2334 1802195444u, 476864866u, 2238001368u, 4066508878u, 1812370925u, 453092731u, 2181625025u, 4111451223u,
2335 1706088902u, 314042704u, 2344532202u, 4240017532u, 1658658271u, 366619977u, 2362670323u, 4224994405u,
2336 1303535960u, 984961486u, 2747007092u, 3569037538u, 1256170817u, 1037604311u, 2765210733u, 3554079995u,
2337 1131014506u, 879679996u, 2909243462u, 3663771856u, 1141124467u, 855842277u, 2852801631u, 3708648649u,
2338 1342533948u, 654459306u, 3188396048u, 3373015174u, 1466479909u, 544179635u, 3110523913u, 3462522015u,
2339 1591671054u, 702138776u, 2966460450u, 3352799412u, 1504918807u, 783551873u, 3082640443u, 3233442989u,
2340 3988292384u, 2596254646u, 62317068u, 1957810842u, 3939845945u, 2647816111u, 81470997u, 1943803523u,
2341 3814918930u, 2489596804u, 225274430u, 2053790376u, 3826175755u, 2466906013u, 167816743u, 2097651377u,
2342 4027552580u, 2265490386u, 503444072u, 1762050814u, 4150417245u, 2154129355u, 426522225u, 1852507879u,
2343 4275313526u, 2312317920u, 282753626u, 1742555852u, 4189708143u, 2394877945u, 397917763u, 1622183637u,
2344 3604390888u, 2714866558u, 953729732u, 1340076626u, 3518719985u, 2797360999u, 1068828381u, 1219638859u,
2345 3624741850u, 2936675148u, 906185462u, 1090812512u, 3747672003u, 2825379669u, 829329135u, 1181335161u,
2346 3412177804u, 3160834842u, 628085408u, 1382605366u, 3423369109u, 3138078467u, 570562233u, 1426400815u,
2347 3317316542u, 2998733608u, 733239954u, 1555261956u, 3268935591u, 3050360625u, 752459403u, 1541320221u,
2348 2607071920u, 3965973030u, 1969922972u, 40735498u, 2617837225u, 3943577151u, 1913087877u, 83908371u,
2349 2512341634u, 3803740692u, 2075208622u, 213261112u, 2463272603u, 3855990285u, 2094854071u, 198958881u,
2350 2262029012u, 4057260610u, 1759359992u, 534414190u, 2176718541u, 4139329115u, 1873836001u, 414664567u,
2351 2282248934u, 4279200368u, 1711684554u, 285281116u, 2405801727u, 4167216745u, 1634467795u, 376229701u,
2352 2685067896u, 3608007406u, 1308918612u, 956543938u, 2808555105u, 3495958263u, 1231636301u, 1047427035u,
2353 2932959818u, 3654703836u, 1088359270u, 936918000u, 2847714899u, 3736837829u, 1202900863u, 817233897u,
2354 3183342108u, 3401237130u, 1404277552u, 615818150u, 3134207493u, 3453421203u, 1423857449u, 601450431u,
2355 3009837614u, 3294710456u, 1567103746u, 711928724u, 3020668471u, 3272380065u, 1510334235u, 755167117u
2356 };
2357
2358 /*Return the CRC of the bytes buf[0..len-1].*/
lodepng_crc32(const unsigned char * data,size_t length)2359 unsigned lodepng_crc32(const unsigned char* data, size_t length)
2360 {
2361 unsigned r = 0xffffffffu;
2362 size_t i;
2363 for(i = 0; i < length; ++i)
2364 {
2365 r = lodepng_crc32_table[(r ^ data[i]) & 0xff] ^ (r >> 8);
2366 }
2367 return r ^ 0xffffffffu;
2368 }
2369 #else /* !LODEPNG_NO_COMPILE_CRC */
2370 unsigned lodepng_crc32(const unsigned char* data, size_t length);
2371 #endif /* !LODEPNG_NO_COMPILE_CRC */
2372
2373 /* ////////////////////////////////////////////////////////////////////////// */
2374 /* / Reading and writing single bits and bytes from/to stream for LodePNG / */
2375 /* ////////////////////////////////////////////////////////////////////////// */
2376
readBitFromReversedStream(size_t * bitpointer,const unsigned char * bitstream)2377 static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream)
2378 {
2379 unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
2380 ++(*bitpointer);
2381 return result;
2382 }
2383
readBitsFromReversedStream(size_t * bitpointer,const unsigned char * bitstream,size_t nbits)2384 static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits)
2385 {
2386 unsigned result = 0;
2387 size_t i;
2388 for(i = nbits - 1; i < nbits; --i)
2389 {
2390 result += (unsigned)readBitFromReversedStream(bitpointer, bitstream) << i;
2391 }
2392 return result;
2393 }
2394
2395 #ifdef LODEPNG_COMPILE_DECODER
setBitOfReversedStream0(size_t * bitpointer,unsigned char * bitstream,unsigned char bit)2396 static void setBitOfReversedStream0(size_t* bitpointer, unsigned char* bitstream, unsigned char bit)
2397 {
2398 /*the current bit in bitstream must be 0 for this to work*/
2399 if(bit)
2400 {
2401 /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/
2402 bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7)));
2403 }
2404 ++(*bitpointer);
2405 }
2406 #endif /*LODEPNG_COMPILE_DECODER*/
2407
setBitOfReversedStream(size_t * bitpointer,unsigned char * bitstream,unsigned char bit)2408 static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit)
2409 {
2410 /*the current bit in bitstream may be 0 or 1 for this to work*/
2411 if(bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7))));
2412 else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7)));
2413 ++(*bitpointer);
2414 }
2415
2416 /* ////////////////////////////////////////////////////////////////////////// */
2417 /* / PNG chunks / */
2418 /* ////////////////////////////////////////////////////////////////////////// */
2419
lodepng_chunk_length(const unsigned char * chunk)2420 unsigned lodepng_chunk_length(const unsigned char* chunk)
2421 {
2422 return lodepng_read32bitInt(&chunk[0]);
2423 }
2424
lodepng_chunk_type(char type[5],const unsigned char * chunk)2425 void lodepng_chunk_type(char type[5], const unsigned char* chunk)
2426 {
2427 unsigned i;
2428 for(i = 0; i != 4; ++i) type[i] = (char)chunk[4 + i];
2429 type[4] = 0; /*null termination char*/
2430 }
2431
lodepng_chunk_type_equals(const unsigned char * chunk,const char * type)2432 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type)
2433 {
2434 if(strlen(type) != 4) return 0;
2435 return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
2436 }
2437
lodepng_chunk_ancillary(const unsigned char * chunk)2438 unsigned char lodepng_chunk_ancillary(const unsigned char* chunk)
2439 {
2440 return((chunk[4] & 32) != 0);
2441 }
2442
lodepng_chunk_private(const unsigned char * chunk)2443 unsigned char lodepng_chunk_private(const unsigned char* chunk)
2444 {
2445 return((chunk[6] & 32) != 0);
2446 }
2447
lodepng_chunk_safetocopy(const unsigned char * chunk)2448 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk)
2449 {
2450 return((chunk[7] & 32) != 0);
2451 }
2452
lodepng_chunk_data(unsigned char * chunk)2453 unsigned char* lodepng_chunk_data(unsigned char* chunk)
2454 {
2455 return &chunk[8];
2456 }
2457
lodepng_chunk_data_const(const unsigned char * chunk)2458 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk)
2459 {
2460 return &chunk[8];
2461 }
2462
lodepng_chunk_check_crc(const unsigned char * chunk)2463 unsigned lodepng_chunk_check_crc(const unsigned char* chunk)
2464 {
2465 unsigned length = lodepng_chunk_length(chunk);
2466 unsigned CRC = lodepng_read32bitInt(&chunk[length + 8]);
2467 /*the CRC is taken of the data and the 4 chunk type letters, not the length*/
2468 unsigned checksum = lodepng_crc32(&chunk[4], length + 4);
2469 if(CRC != checksum) return 1;
2470 else return 0;
2471 }
2472
lodepng_chunk_generate_crc(unsigned char * chunk)2473 void lodepng_chunk_generate_crc(unsigned char* chunk)
2474 {
2475 unsigned length = lodepng_chunk_length(chunk);
2476 unsigned CRC = lodepng_crc32(&chunk[4], length + 4);
2477 lodepng_set32bitInt(chunk + 8 + length, CRC);
2478 }
2479
lodepng_chunk_next(unsigned char * chunk)2480 unsigned char* lodepng_chunk_next(unsigned char* chunk)
2481 {
2482 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12;
2483 return &chunk[total_chunk_length];
2484 }
2485
lodepng_chunk_next_const(const unsigned char * chunk)2486 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk)
2487 {
2488 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12;
2489 return &chunk[total_chunk_length];
2490 }
2491
lodepng_chunk_append(unsigned char ** out,size_t * outlength,const unsigned char * chunk)2492 unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk)
2493 {
2494 unsigned i;
2495 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12;
2496 unsigned char *chunk_start, *new_buffer;
2497 size_t new_length = (*outlength) + total_chunk_length;
2498 if(new_length < total_chunk_length || new_length < (*outlength)) return 77; /*integer overflow happened*/
2499
2500 new_buffer = (unsigned char*)lodepng_realloc(*out, new_length);
2501 if(!new_buffer) return 83; /*alloc fail*/
2502 (*out) = new_buffer;
2503 (*outlength) = new_length;
2504 chunk_start = &(*out)[new_length - total_chunk_length];
2505
2506 for(i = 0; i != total_chunk_length; ++i) chunk_start[i] = chunk[i];
2507
2508 return 0;
2509 }
2510
lodepng_chunk_create(unsigned char ** out,size_t * outlength,unsigned length,const char * type,const unsigned char * data)2511 unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length,
2512 const char* type, const unsigned char* data)
2513 {
2514 unsigned i;
2515 unsigned char *chunk, *new_buffer;
2516 size_t new_length = (*outlength) + length + 12;
2517 if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/
2518 new_buffer = (unsigned char*)lodepng_realloc(*out, new_length);
2519 if(!new_buffer) return 83; /*alloc fail*/
2520 (*out) = new_buffer;
2521 (*outlength) = new_length;
2522 chunk = &(*out)[(*outlength) - length - 12];
2523
2524 /*1: length*/
2525 lodepng_set32bitInt(chunk, (unsigned)length);
2526
2527 /*2: chunk name (4 letters)*/
2528 chunk[4] = (unsigned char)type[0];
2529 chunk[5] = (unsigned char)type[1];
2530 chunk[6] = (unsigned char)type[2];
2531 chunk[7] = (unsigned char)type[3];
2532
2533 /*3: the data*/
2534 for(i = 0; i != length; ++i) chunk[8 + i] = data[i];
2535
2536 /*4: CRC (of the chunkname characters and the data)*/
2537 lodepng_chunk_generate_crc(chunk);
2538
2539 return 0;
2540 }
2541
2542 /* ////////////////////////////////////////////////////////////////////////// */
2543 /* / Color types and such / */
2544 /* ////////////////////////////////////////////////////////////////////////// */
2545
2546 /*return type is a LodePNG error code*/
checkColorValidity(LodePNGColorType colortype,unsigned bd)2547 static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) /*bd = bitdepth*/
2548 {
2549 switch(colortype)
2550 {
2551 case 0: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; /*grey*/
2552 case 2: if(!( bd == 8 || bd == 16)) return 37; break; /*RGB*/
2553 case 3: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; /*palette*/
2554 case 4: if(!( bd == 8 || bd == 16)) return 37; break; /*grey + alpha*/
2555 case 6: if(!( bd == 8 || bd == 16)) return 37; break; /*RGBA*/
2556 default: return 31;
2557 }
2558 return 0; /*allowed color type / bits combination*/
2559 }
2560
getNumColorChannels(LodePNGColorType colortype)2561 static unsigned getNumColorChannels(LodePNGColorType colortype)
2562 {
2563 switch(colortype)
2564 {
2565 case 0: return 1; /*grey*/
2566 case 2: return 3; /*RGB*/
2567 case 3: return 1; /*palette*/
2568 case 4: return 2; /*grey + alpha*/
2569 case 6: return 4; /*RGBA*/
2570 }
2571 return 0; /*unexisting color type*/
2572 }
2573
lodepng_get_bpp_lct(LodePNGColorType colortype,unsigned bitdepth)2574 static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth)
2575 {
2576 /*bits per pixel is amount of channels * bits per channel*/
2577 return getNumColorChannels(colortype) * bitdepth;
2578 }
2579
2580 /* ////////////////////////////////////////////////////////////////////////// */
2581
lodepng_color_mode_init(LodePNGColorMode * info)2582 void lodepng_color_mode_init(LodePNGColorMode* info)
2583 {
2584 info->key_defined = 0;
2585 info->key_r = info->key_g = info->key_b = 0;
2586 info->colortype = LCT_RGBA;
2587 info->bitdepth = 8;
2588 info->palette = 0;
2589 info->palettesize = 0;
2590 }
2591
lodepng_color_mode_cleanup(LodePNGColorMode * info)2592 void lodepng_color_mode_cleanup(LodePNGColorMode* info)
2593 {
2594 lodepng_palette_clear(info);
2595 }
2596
lodepng_color_mode_copy(LodePNGColorMode * dest,const LodePNGColorMode * source)2597 unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source)
2598 {
2599 size_t i;
2600 lodepng_color_mode_cleanup(dest);
2601 *dest = *source;
2602 if(source->palette)
2603 {
2604 dest->palette = (unsigned char*)lodepng_malloc(1024);
2605 if(!dest->palette && source->palettesize) return 83; /*alloc fail*/
2606 for(i = 0; i != source->palettesize * 4; ++i) dest->palette[i] = source->palette[i];
2607 }
2608 return 0;
2609 }
2610
lodepng_color_mode_equal(const LodePNGColorMode * a,const LodePNGColorMode * b)2611 static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b)
2612 {
2613 size_t i;
2614 if(a->colortype != b->colortype) return 0;
2615 if(a->bitdepth != b->bitdepth) return 0;
2616 if(a->key_defined != b->key_defined) return 0;
2617 if(a->key_defined)
2618 {
2619 if(a->key_r != b->key_r) return 0;
2620 if(a->key_g != b->key_g) return 0;
2621 if(a->key_b != b->key_b) return 0;
2622 }
2623 /*if one of the palette sizes is 0, then we consider it to be the same as the
2624 other: it means that e.g. the palette was not given by the user and should be
2625 considered the same as the palette inside the PNG.*/
2626 if(1/*a->palettesize != 0 && b->palettesize != 0*/) {
2627 if(a->palettesize != b->palettesize) return 0;
2628 for(i = 0; i != a->palettesize * 4; ++i)
2629 {
2630 if(a->palette[i] != b->palette[i]) return 0;
2631 }
2632 }
2633 return 1;
2634 }
2635
lodepng_palette_clear(LodePNGColorMode * info)2636 void lodepng_palette_clear(LodePNGColorMode* info)
2637 {
2638 if(info->palette) lodepng_free(info->palette);
2639 info->palette = 0;
2640 info->palettesize = 0;
2641 }
2642
lodepng_palette_add(LodePNGColorMode * info,unsigned char r,unsigned char g,unsigned char b,unsigned char a)2643 unsigned lodepng_palette_add(LodePNGColorMode* info,
2644 unsigned char r, unsigned char g, unsigned char b, unsigned char a)
2645 {
2646 unsigned char* data;
2647 /*the same resize technique as C++ std::vectors is used, and here it's made so that for a palette with
2648 the max of 256 colors, it'll have the exact alloc size*/
2649 if(!info->palette) /*allocate palette if empty*/
2650 {
2651 /*room for 256 colors with 4 bytes each*/
2652 data = (unsigned char*)lodepng_realloc(info->palette, 1024);
2653 if(!data) return 83; /*alloc fail*/
2654 else info->palette = data;
2655 }
2656 info->palette[4 * info->palettesize + 0] = r;
2657 info->palette[4 * info->palettesize + 1] = g;
2658 info->palette[4 * info->palettesize + 2] = b;
2659 info->palette[4 * info->palettesize + 3] = a;
2660 ++info->palettesize;
2661 return 0;
2662 }
2663
lodepng_get_bpp(const LodePNGColorMode * info)2664 unsigned lodepng_get_bpp(const LodePNGColorMode* info)
2665 {
2666 /*calculate bits per pixel out of colortype and bitdepth*/
2667 return lodepng_get_bpp_lct(info->colortype, info->bitdepth);
2668 }
2669
lodepng_get_channels(const LodePNGColorMode * info)2670 unsigned lodepng_get_channels(const LodePNGColorMode* info)
2671 {
2672 return getNumColorChannels(info->colortype);
2673 }
2674
lodepng_is_greyscale_type(const LodePNGColorMode * info)2675 unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info)
2676 {
2677 return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA;
2678 }
2679
lodepng_is_alpha_type(const LodePNGColorMode * info)2680 unsigned lodepng_is_alpha_type(const LodePNGColorMode* info)
2681 {
2682 return (info->colortype & 4) != 0; /*4 or 6*/
2683 }
2684
lodepng_is_palette_type(const LodePNGColorMode * info)2685 unsigned lodepng_is_palette_type(const LodePNGColorMode* info)
2686 {
2687 return info->colortype == LCT_PALETTE;
2688 }
2689
lodepng_has_palette_alpha(const LodePNGColorMode * info)2690 unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info)
2691 {
2692 size_t i;
2693 for(i = 0; i != info->palettesize; ++i)
2694 {
2695 if(info->palette[i * 4 + 3] < 255) return 1;
2696 }
2697 return 0;
2698 }
2699
lodepng_can_have_alpha(const LodePNGColorMode * info)2700 unsigned lodepng_can_have_alpha(const LodePNGColorMode* info)
2701 {
2702 return info->key_defined
2703 || lodepng_is_alpha_type(info)
2704 || lodepng_has_palette_alpha(info);
2705 }
2706
lodepng_get_raw_size(unsigned w,unsigned h,const LodePNGColorMode * color)2707 size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color)
2708 {
2709 /*will not overflow for any color type if roughly w * h < 268435455*/
2710 size_t bpp = lodepng_get_bpp(color);
2711 size_t n = w * h;
2712 return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8;
2713 }
2714
lodepng_get_raw_size_lct(unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth)2715 size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth)
2716 {
2717 /*will not overflow for any color type if roughly w * h < 268435455*/
2718 size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth);
2719 size_t n = w * h;
2720 return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8;
2721 }
2722
2723
2724 #ifdef LODEPNG_COMPILE_PNG
2725 #ifdef LODEPNG_COMPILE_DECODER
2726 /*in an idat chunk, each scanline is a multiple of 8 bits, unlike the lodepng output buffer*/
lodepng_get_raw_size_idat(unsigned w,unsigned h,const LodePNGColorMode * color)2727 static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, const LodePNGColorMode* color)
2728 {
2729 /*will not overflow for any color type if roughly w * h < 268435455*/
2730 size_t bpp = lodepng_get_bpp(color);
2731 size_t line = ((w / 8) * bpp) + ((w & 7) * bpp + 7) / 8;
2732 return h * line;
2733 }
2734 #endif /*LODEPNG_COMPILE_DECODER*/
2735 #endif /*LODEPNG_COMPILE_PNG*/
2736
2737 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2738
LodePNGUnknownChunks_init(LodePNGInfo * info)2739 static void LodePNGUnknownChunks_init(LodePNGInfo* info)
2740 {
2741 unsigned i;
2742 for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0;
2743 for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0;
2744 }
2745
LodePNGUnknownChunks_cleanup(LodePNGInfo * info)2746 static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info)
2747 {
2748 unsigned i;
2749 for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]);
2750 }
2751
LodePNGUnknownChunks_copy(LodePNGInfo * dest,const LodePNGInfo * src)2752 static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src)
2753 {
2754 unsigned i;
2755
2756 LodePNGUnknownChunks_cleanup(dest);
2757
2758 for(i = 0; i != 3; ++i)
2759 {
2760 size_t j;
2761 dest->unknown_chunks_size[i] = src->unknown_chunks_size[i];
2762 dest->unknown_chunks_data[i] = (unsigned char*)lodepng_malloc(src->unknown_chunks_size[i]);
2763 if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/
2764 for(j = 0; j < src->unknown_chunks_size[i]; ++j)
2765 {
2766 dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j];
2767 }
2768 }
2769
2770 return 0;
2771 }
2772
2773 /******************************************************************************/
2774
LodePNGText_init(LodePNGInfo * info)2775 static void LodePNGText_init(LodePNGInfo* info)
2776 {
2777 info->text_num = 0;
2778 info->text_keys = NULL;
2779 info->text_strings = NULL;
2780 }
2781
LodePNGText_cleanup(LodePNGInfo * info)2782 static void LodePNGText_cleanup(LodePNGInfo* info)
2783 {
2784 size_t i;
2785 for(i = 0; i != info->text_num; ++i)
2786 {
2787 string_cleanup(&info->text_keys[i]);
2788 string_cleanup(&info->text_strings[i]);
2789 }
2790 lodepng_free(info->text_keys);
2791 lodepng_free(info->text_strings);
2792 }
2793
LodePNGText_copy(LodePNGInfo * dest,const LodePNGInfo * source)2794 static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source)
2795 {
2796 size_t i = 0;
2797 dest->text_keys = 0;
2798 dest->text_strings = 0;
2799 dest->text_num = 0;
2800 for(i = 0; i != source->text_num; ++i)
2801 {
2802 CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i]));
2803 }
2804 return 0;
2805 }
2806
lodepng_clear_text(LodePNGInfo * info)2807 void lodepng_clear_text(LodePNGInfo* info)
2808 {
2809 LodePNGText_cleanup(info);
2810 }
2811
lodepng_add_text(LodePNGInfo * info,const char * key,const char * str)2812 unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str)
2813 {
2814 char** new_keys = (char**)(lodepng_realloc(info->text_keys, sizeof(char*) * (info->text_num + 1)));
2815 char** new_strings = (char**)(lodepng_realloc(info->text_strings, sizeof(char*) * (info->text_num + 1)));
2816 if(!new_keys || !new_strings)
2817 {
2818 lodepng_free(new_keys);
2819 lodepng_free(new_strings);
2820 return 83; /*alloc fail*/
2821 }
2822
2823 ++info->text_num;
2824 info->text_keys = new_keys;
2825 info->text_strings = new_strings;
2826
2827 string_init(&info->text_keys[info->text_num - 1]);
2828 string_set(&info->text_keys[info->text_num - 1], key);
2829
2830 string_init(&info->text_strings[info->text_num - 1]);
2831 string_set(&info->text_strings[info->text_num - 1], str);
2832
2833 return 0;
2834 }
2835
2836 /******************************************************************************/
2837
LodePNGIText_init(LodePNGInfo * info)2838 static void LodePNGIText_init(LodePNGInfo* info)
2839 {
2840 info->itext_num = 0;
2841 info->itext_keys = NULL;
2842 info->itext_langtags = NULL;
2843 info->itext_transkeys = NULL;
2844 info->itext_strings = NULL;
2845 }
2846
LodePNGIText_cleanup(LodePNGInfo * info)2847 static void LodePNGIText_cleanup(LodePNGInfo* info)
2848 {
2849 size_t i;
2850 for(i = 0; i != info->itext_num; ++i)
2851 {
2852 string_cleanup(&info->itext_keys[i]);
2853 string_cleanup(&info->itext_langtags[i]);
2854 string_cleanup(&info->itext_transkeys[i]);
2855 string_cleanup(&info->itext_strings[i]);
2856 }
2857 lodepng_free(info->itext_keys);
2858 lodepng_free(info->itext_langtags);
2859 lodepng_free(info->itext_transkeys);
2860 lodepng_free(info->itext_strings);
2861 }
2862
LodePNGIText_copy(LodePNGInfo * dest,const LodePNGInfo * source)2863 static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source)
2864 {
2865 size_t i = 0;
2866 dest->itext_keys = 0;
2867 dest->itext_langtags = 0;
2868 dest->itext_transkeys = 0;
2869 dest->itext_strings = 0;
2870 dest->itext_num = 0;
2871 for(i = 0; i != source->itext_num; ++i)
2872 {
2873 CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i],
2874 source->itext_transkeys[i], source->itext_strings[i]));
2875 }
2876 return 0;
2877 }
2878
lodepng_clear_itext(LodePNGInfo * info)2879 void lodepng_clear_itext(LodePNGInfo* info)
2880 {
2881 LodePNGIText_cleanup(info);
2882 }
2883
lodepng_add_itext(LodePNGInfo * info,const char * key,const char * langtag,const char * transkey,const char * str)2884 unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag,
2885 const char* transkey, const char* str)
2886 {
2887 char** new_keys = (char**)(lodepng_realloc(info->itext_keys, sizeof(char*) * (info->itext_num + 1)));
2888 char** new_langtags = (char**)(lodepng_realloc(info->itext_langtags, sizeof(char*) * (info->itext_num + 1)));
2889 char** new_transkeys = (char**)(lodepng_realloc(info->itext_transkeys, sizeof(char*) * (info->itext_num + 1)));
2890 char** new_strings = (char**)(lodepng_realloc(info->itext_strings, sizeof(char*) * (info->itext_num + 1)));
2891 if(!new_keys || !new_langtags || !new_transkeys || !new_strings)
2892 {
2893 lodepng_free(new_keys);
2894 lodepng_free(new_langtags);
2895 lodepng_free(new_transkeys);
2896 lodepng_free(new_strings);
2897 return 83; /*alloc fail*/
2898 }
2899
2900 ++info->itext_num;
2901 info->itext_keys = new_keys;
2902 info->itext_langtags = new_langtags;
2903 info->itext_transkeys = new_transkeys;
2904 info->itext_strings = new_strings;
2905
2906 string_init(&info->itext_keys[info->itext_num - 1]);
2907 string_set(&info->itext_keys[info->itext_num - 1], key);
2908
2909 string_init(&info->itext_langtags[info->itext_num - 1]);
2910 string_set(&info->itext_langtags[info->itext_num - 1], langtag);
2911
2912 string_init(&info->itext_transkeys[info->itext_num - 1]);
2913 string_set(&info->itext_transkeys[info->itext_num - 1], transkey);
2914
2915 string_init(&info->itext_strings[info->itext_num - 1]);
2916 string_set(&info->itext_strings[info->itext_num - 1], str);
2917
2918 return 0;
2919 }
2920 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2921
lodepng_info_init(LodePNGInfo * info)2922 void lodepng_info_init(LodePNGInfo* info)
2923 {
2924 lodepng_color_mode_init(&info->color);
2925 info->interlace_method = 0;
2926 info->compression_method = 0;
2927 info->filter_method = 0;
2928 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2929 info->background_defined = 0;
2930 info->background_r = info->background_g = info->background_b = 0;
2931
2932 LodePNGText_init(info);
2933 LodePNGIText_init(info);
2934
2935 info->time_defined = 0;
2936 info->phys_defined = 0;
2937
2938 LodePNGUnknownChunks_init(info);
2939 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2940 }
2941
lodepng_info_cleanup(LodePNGInfo * info)2942 void lodepng_info_cleanup(LodePNGInfo* info)
2943 {
2944 lodepng_color_mode_cleanup(&info->color);
2945 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2946 LodePNGText_cleanup(info);
2947 LodePNGIText_cleanup(info);
2948
2949 LodePNGUnknownChunks_cleanup(info);
2950 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2951 }
2952
lodepng_info_copy(LodePNGInfo * dest,const LodePNGInfo * source)2953 unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source)
2954 {
2955 lodepng_info_cleanup(dest);
2956 *dest = *source;
2957 lodepng_color_mode_init(&dest->color);
2958 CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color));
2959
2960 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2961 CERROR_TRY_RETURN(LodePNGText_copy(dest, source));
2962 CERROR_TRY_RETURN(LodePNGIText_copy(dest, source));
2963
2964 LodePNGUnknownChunks_init(dest);
2965 CERROR_TRY_RETURN(LodePNGUnknownChunks_copy(dest, source));
2966 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2967 return 0;
2968 }
2969
lodepng_info_swap(LodePNGInfo * a,LodePNGInfo * b)2970 void lodepng_info_swap(LodePNGInfo* a, LodePNGInfo* b)
2971 {
2972 LodePNGInfo temp = *a;
2973 *a = *b;
2974 *b = temp;
2975 }
2976
2977 /* ////////////////////////////////////////////////////////////////////////// */
2978
2979 /*index: bitgroup index, bits: bitgroup size(1, 2 or 4), in: bitgroup value, out: octet array to add bits to*/
addColorBits(unsigned char * out,size_t index,unsigned bits,unsigned in)2980 static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in)
2981 {
2982 unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/
2983 /*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for second half*/
2984 unsigned p = index & m;
2985 in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/
2986 in = in << (bits * (m - p));
2987 if(p == 0) out[index * bits / 8] = in;
2988 else out[index * bits / 8] |= in;
2989 }
2990
2991 typedef struct ColorTree ColorTree;
2992
2993 /*
2994 One node of a color tree
2995 This is the data structure used to count the number of unique colors and to get a palette
2996 index for a color. It's like an octree, but because the alpha channel is used too, each
2997 node has 16 instead of 8 children.
2998 */
2999 struct ColorTree
3000 {
3001 ColorTree* children[16]; /*up to 16 pointers to ColorTree of next level*/
3002 int index; /*the payload. Only has a meaningful value if this is in the last level*/
3003 };
3004
color_tree_init(ColorTree * tree)3005 static void color_tree_init(ColorTree* tree)
3006 {
3007 int i;
3008 for(i = 0; i != 16; ++i) tree->children[i] = 0;
3009 tree->index = -1;
3010 }
3011
color_tree_cleanup(ColorTree * tree)3012 static void color_tree_cleanup(ColorTree* tree)
3013 {
3014 int i;
3015 for(i = 0; i != 16; ++i)
3016 {
3017 if(tree->children[i])
3018 {
3019 color_tree_cleanup(tree->children[i]);
3020 lodepng_free(tree->children[i]);
3021 }
3022 }
3023 }
3024
3025 /*returns -1 if color not present, its index otherwise*/
color_tree_get(ColorTree * tree,unsigned char r,unsigned char g,unsigned char b,unsigned char a)3026 static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
3027 {
3028 int bit = 0;
3029 for(bit = 0; bit < 8; ++bit)
3030 {
3031 int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
3032 if(!tree->children[i]) return -1;
3033 else tree = tree->children[i];
3034 }
3035 return tree ? tree->index : -1;
3036 }
3037
3038 #ifdef LODEPNG_COMPILE_ENCODER
color_tree_has(ColorTree * tree,unsigned char r,unsigned char g,unsigned char b,unsigned char a)3039 static int color_tree_has(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
3040 {
3041 return color_tree_get(tree, r, g, b, a) >= 0;
3042 }
3043 #endif /*LODEPNG_COMPILE_ENCODER*/
3044
3045 /*color is not allowed to already exist.
3046 Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")*/
color_tree_add(ColorTree * tree,unsigned char r,unsigned char g,unsigned char b,unsigned char a,unsigned index)3047 static void color_tree_add(ColorTree* tree,
3048 unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index)
3049 {
3050 int bit;
3051 for(bit = 0; bit < 8; ++bit)
3052 {
3053 int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
3054 if(!tree->children[i])
3055 {
3056 tree->children[i] = (ColorTree*)lodepng_malloc(sizeof(ColorTree));
3057 color_tree_init(tree->children[i]);
3058 }
3059 tree = tree->children[i];
3060 }
3061 tree->index = (int)index;
3062 }
3063
3064 /*put a pixel, given its RGBA color, into image of any color type*/
rgba8ToPixel(unsigned char * out,size_t i,const LodePNGColorMode * mode,ColorTree * tree,unsigned char r,unsigned char g,unsigned char b,unsigned char a)3065 static unsigned rgba8ToPixel(unsigned char* out, size_t i,
3066 const LodePNGColorMode* mode, ColorTree* tree /*for palette*/,
3067 unsigned char r, unsigned char g, unsigned char b, unsigned char a)
3068 {
3069 if(mode->colortype == LCT_GREY)
3070 {
3071 unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/;
3072 if(mode->bitdepth == 8) out[i] = grey;
3073 else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = grey;
3074 else
3075 {
3076 /*take the most significant bits of grey*/
3077 grey = (grey >> (8 - mode->bitdepth)) & ((1 << mode->bitdepth) - 1);
3078 addColorBits(out, i, mode->bitdepth, grey);
3079 }
3080 }
3081 else if(mode->colortype == LCT_RGB)
3082 {
3083 if(mode->bitdepth == 8)
3084 {
3085 out[i * 3 + 0] = r;
3086 out[i * 3 + 1] = g;
3087 out[i * 3 + 2] = b;
3088 }
3089 else
3090 {
3091 out[i * 6 + 0] = out[i * 6 + 1] = r;
3092 out[i * 6 + 2] = out[i * 6 + 3] = g;
3093 out[i * 6 + 4] = out[i * 6 + 5] = b;
3094 }
3095 }
3096 else if(mode->colortype == LCT_PALETTE)
3097 {
3098 int index = color_tree_get(tree, r, g, b, a);
3099 if(index < 0) return 82; /*color not in palette*/
3100 if(mode->bitdepth == 8) out[i] = index;
3101 else addColorBits(out, i, mode->bitdepth, (unsigned)index);
3102 }
3103 else if(mode->colortype == LCT_GREY_ALPHA)
3104 {
3105 unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/;
3106 if(mode->bitdepth == 8)
3107 {
3108 out[i * 2 + 0] = grey;
3109 out[i * 2 + 1] = a;
3110 }
3111 else if(mode->bitdepth == 16)
3112 {
3113 out[i * 4 + 0] = out[i * 4 + 1] = grey;
3114 out[i * 4 + 2] = out[i * 4 + 3] = a;
3115 }
3116 }
3117 else if(mode->colortype == LCT_RGBA)
3118 {
3119 if(mode->bitdepth == 8)
3120 {
3121 out[i * 4 + 0] = r;
3122 out[i * 4 + 1] = g;
3123 out[i * 4 + 2] = b;
3124 out[i * 4 + 3] = a;
3125 }
3126 else
3127 {
3128 out[i * 8 + 0] = out[i * 8 + 1] = r;
3129 out[i * 8 + 2] = out[i * 8 + 3] = g;
3130 out[i * 8 + 4] = out[i * 8 + 5] = b;
3131 out[i * 8 + 6] = out[i * 8 + 7] = a;
3132 }
3133 }
3134
3135 return 0; /*no error*/
3136 }
3137
3138 /*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/
rgba16ToPixel(unsigned char * out,size_t i,const LodePNGColorMode * mode,unsigned short r,unsigned short g,unsigned short b,unsigned short a)3139 static void rgba16ToPixel(unsigned char* out, size_t i,
3140 const LodePNGColorMode* mode,
3141 unsigned short r, unsigned short g, unsigned short b, unsigned short a)
3142 {
3143 if(mode->colortype == LCT_GREY)
3144 {
3145 unsigned short grey = r; /*((unsigned)r + g + b) / 3*/;
3146 out[i * 2 + 0] = (grey >> 8) & 255;
3147 out[i * 2 + 1] = grey & 255;
3148 }
3149 else if(mode->colortype == LCT_RGB)
3150 {
3151 out[i * 6 + 0] = (r >> 8) & 255;
3152 out[i * 6 + 1] = r & 255;
3153 out[i * 6 + 2] = (g >> 8) & 255;
3154 out[i * 6 + 3] = g & 255;
3155 out[i * 6 + 4] = (b >> 8) & 255;
3156 out[i * 6 + 5] = b & 255;
3157 }
3158 else if(mode->colortype == LCT_GREY_ALPHA)
3159 {
3160 unsigned short grey = r; /*((unsigned)r + g + b) / 3*/;
3161 out[i * 4 + 0] = (grey >> 8) & 255;
3162 out[i * 4 + 1] = grey & 255;
3163 out[i * 4 + 2] = (a >> 8) & 255;
3164 out[i * 4 + 3] = a & 255;
3165 }
3166 else if(mode->colortype == LCT_RGBA)
3167 {
3168 out[i * 8 + 0] = (r >> 8) & 255;
3169 out[i * 8 + 1] = r & 255;
3170 out[i * 8 + 2] = (g >> 8) & 255;
3171 out[i * 8 + 3] = g & 255;
3172 out[i * 8 + 4] = (b >> 8) & 255;
3173 out[i * 8 + 5] = b & 255;
3174 out[i * 8 + 6] = (a >> 8) & 255;
3175 out[i * 8 + 7] = a & 255;
3176 }
3177 }
3178
3179 /*Get RGBA8 color of pixel with index i (y * width + x) from the raw image with given color type.*/
getPixelColorRGBA8(unsigned char * r,unsigned char * g,unsigned char * b,unsigned char * a,const unsigned char * in,size_t i,const LodePNGColorMode * mode)3180 static void getPixelColorRGBA8(unsigned char* r, unsigned char* g,
3181 unsigned char* b, unsigned char* a,
3182 const unsigned char* in, size_t i,
3183 const LodePNGColorMode* mode)
3184 {
3185 if(mode->colortype == LCT_GREY)
3186 {
3187 if(mode->bitdepth == 8)
3188 {
3189 *r = *g = *b = in[i];
3190 if(mode->key_defined && *r == mode->key_r) *a = 0;
3191 else *a = 255;
3192 }
3193 else if(mode->bitdepth == 16)
3194 {
3195 *r = *g = *b = in[i * 2 + 0];
3196 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
3197 else *a = 255;
3198 }
3199 else
3200 {
3201 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
3202 size_t j = i * mode->bitdepth;
3203 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
3204 *r = *g = *b = (value * 255) / highest;
3205 if(mode->key_defined && value == mode->key_r) *a = 0;
3206 else *a = 255;
3207 }
3208 }
3209 else if(mode->colortype == LCT_RGB)
3210 {
3211 if(mode->bitdepth == 8)
3212 {
3213 *r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2];
3214 if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0;
3215 else *a = 255;
3216 }
3217 else
3218 {
3219 *r = in[i * 6 + 0];
3220 *g = in[i * 6 + 2];
3221 *b = in[i * 6 + 4];
3222 if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
3223 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
3224 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
3225 else *a = 255;
3226 }
3227 }
3228 else if(mode->colortype == LCT_PALETTE)
3229 {
3230 unsigned index;
3231 if(mode->bitdepth == 8) index = in[i];
3232 else
3233 {
3234 size_t j = i * mode->bitdepth;
3235 index = readBitsFromReversedStream(&j, in, mode->bitdepth);
3236 }
3237
3238 if(index >= mode->palettesize)
3239 {
3240 /*This is an error according to the PNG spec, but common PNG decoders make it black instead.
3241 Done here too, slightly faster due to no error handling needed.*/
3242 *r = *g = *b = 0;
3243 *a = 255;
3244 }
3245 else
3246 {
3247 *r = mode->palette[index * 4 + 0];
3248 *g = mode->palette[index * 4 + 1];
3249 *b = mode->palette[index * 4 + 2];
3250 *a = mode->palette[index * 4 + 3];
3251 }
3252 }
3253 else if(mode->colortype == LCT_GREY_ALPHA)
3254 {
3255 if(mode->bitdepth == 8)
3256 {
3257 *r = *g = *b = in[i * 2 + 0];
3258 *a = in[i * 2 + 1];
3259 }
3260 else
3261 {
3262 *r = *g = *b = in[i * 4 + 0];
3263 *a = in[i * 4 + 2];
3264 }
3265 }
3266 else if(mode->colortype == LCT_RGBA)
3267 {
3268 if(mode->bitdepth == 8)
3269 {
3270 *r = in[i * 4 + 0];
3271 *g = in[i * 4 + 1];
3272 *b = in[i * 4 + 2];
3273 *a = in[i * 4 + 3];
3274 }
3275 else
3276 {
3277 *r = in[i * 8 + 0];
3278 *g = in[i * 8 + 2];
3279 *b = in[i * 8 + 4];
3280 *a = in[i * 8 + 6];
3281 }
3282 }
3283 }
3284
3285 /*Similar to getPixelColorRGBA8, but with all the for loops inside of the color
3286 mode test cases, optimized to convert the colors much faster, when converting
3287 to RGBA or RGB with 8 bit per cannel. buffer must be RGBA or RGB output with
3288 enough memory, if has_alpha is true the output is RGBA. mode has the color mode
3289 of the input buffer.*/
getPixelColorsRGBA8(unsigned char * buffer,size_t numpixels,unsigned has_alpha,const unsigned char * in,const LodePNGColorMode * mode)3290 static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
3291 unsigned has_alpha, const unsigned char* in,
3292 const LodePNGColorMode* mode)
3293 {
3294 unsigned num_channels = has_alpha ? 4 : 3;
3295 size_t i;
3296 if(mode->colortype == LCT_GREY)
3297 {
3298 if(mode->bitdepth == 8)
3299 {
3300 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3301 {
3302 buffer[0] = buffer[1] = buffer[2] = in[i];
3303 if(has_alpha) buffer[3] = mode->key_defined && in[i] == mode->key_r ? 0 : 255;
3304 }
3305 }
3306 else if(mode->bitdepth == 16)
3307 {
3308 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3309 {
3310 buffer[0] = buffer[1] = buffer[2] = in[i * 2];
3311 if(has_alpha) buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255;
3312 }
3313 }
3314 else
3315 {
3316 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
3317 size_t j = 0;
3318 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3319 {
3320 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
3321 buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest;
3322 if(has_alpha) buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255;
3323 }
3324 }
3325 }
3326 else if(mode->colortype == LCT_RGB)
3327 {
3328 if(mode->bitdepth == 8)
3329 {
3330 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3331 {
3332 buffer[0] = in[i * 3 + 0];
3333 buffer[1] = in[i * 3 + 1];
3334 buffer[2] = in[i * 3 + 2];
3335 if(has_alpha) buffer[3] = mode->key_defined && buffer[0] == mode->key_r
3336 && buffer[1]== mode->key_g && buffer[2] == mode->key_b ? 0 : 255;
3337 }
3338 }
3339 else
3340 {
3341 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3342 {
3343 buffer[0] = in[i * 6 + 0];
3344 buffer[1] = in[i * 6 + 2];
3345 buffer[2] = in[i * 6 + 4];
3346 if(has_alpha) buffer[3] = mode->key_defined
3347 && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
3348 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
3349 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255;
3350 }
3351 }
3352 }
3353 else if(mode->colortype == LCT_PALETTE)
3354 {
3355 unsigned index;
3356 size_t j = 0;
3357 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3358 {
3359 if(mode->bitdepth == 8) index = in[i];
3360 else index = readBitsFromReversedStream(&j, in, mode->bitdepth);
3361
3362 if(index >= mode->palettesize)
3363 {
3364 /*This is an error according to the PNG spec, but most PNG decoders make it black instead.
3365 Done here too, slightly faster due to no error handling needed.*/
3366 buffer[0] = buffer[1] = buffer[2] = 0;
3367 if(has_alpha) buffer[3] = 255;
3368 }
3369 else
3370 {
3371 buffer[0] = mode->palette[index * 4 + 0];
3372 buffer[1] = mode->palette[index * 4 + 1];
3373 buffer[2] = mode->palette[index * 4 + 2];
3374 if(has_alpha) buffer[3] = mode->palette[index * 4 + 3];
3375 }
3376 }
3377 }
3378 else if(mode->colortype == LCT_GREY_ALPHA)
3379 {
3380 if(mode->bitdepth == 8)
3381 {
3382 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3383 {
3384 buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0];
3385 if(has_alpha) buffer[3] = in[i * 2 + 1];
3386 }
3387 }
3388 else
3389 {
3390 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3391 {
3392 buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0];
3393 if(has_alpha) buffer[3] = in[i * 4 + 2];
3394 }
3395 }
3396 }
3397 else if(mode->colortype == LCT_RGBA)
3398 {
3399 if(mode->bitdepth == 8)
3400 {
3401 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3402 {
3403 buffer[0] = in[i * 4 + 0];
3404 buffer[1] = in[i * 4 + 1];
3405 buffer[2] = in[i * 4 + 2];
3406 if(has_alpha) buffer[3] = in[i * 4 + 3];
3407 }
3408 }
3409 else
3410 {
3411 for(i = 0; i != numpixels; ++i, buffer += num_channels)
3412 {
3413 buffer[0] = in[i * 8 + 0];
3414 buffer[1] = in[i * 8 + 2];
3415 buffer[2] = in[i * 8 + 4];
3416 if(has_alpha) buffer[3] = in[i * 8 + 6];
3417 }
3418 }
3419 }
3420 }
3421
3422 /*Get RGBA16 color of pixel with index i (y * width + x) from the raw image with
3423 given color type, but the given color type must be 16-bit itself.*/
getPixelColorRGBA16(unsigned short * r,unsigned short * g,unsigned short * b,unsigned short * a,const unsigned char * in,size_t i,const LodePNGColorMode * mode)3424 static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned short* b, unsigned short* a,
3425 const unsigned char* in, size_t i, const LodePNGColorMode* mode)
3426 {
3427 if(mode->colortype == LCT_GREY)
3428 {
3429 *r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1];
3430 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
3431 else *a = 65535;
3432 }
3433 else if(mode->colortype == LCT_RGB)
3434 {
3435 *r = 256u * in[i * 6 + 0] + in[i * 6 + 1];
3436 *g = 256u * in[i * 6 + 2] + in[i * 6 + 3];
3437 *b = 256u * in[i * 6 + 4] + in[i * 6 + 5];
3438 if(mode->key_defined
3439 && 256u * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
3440 && 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
3441 && 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
3442 else *a = 65535;
3443 }
3444 else if(mode->colortype == LCT_GREY_ALPHA)
3445 {
3446 *r = *g = *b = 256u * in[i * 4 + 0] + in[i * 4 + 1];
3447 *a = 256u * in[i * 4 + 2] + in[i * 4 + 3];
3448 }
3449 else if(mode->colortype == LCT_RGBA)
3450 {
3451 *r = 256u * in[i * 8 + 0] + in[i * 8 + 1];
3452 *g = 256u * in[i * 8 + 2] + in[i * 8 + 3];
3453 *b = 256u * in[i * 8 + 4] + in[i * 8 + 5];
3454 *a = 256u * in[i * 8 + 6] + in[i * 8 + 7];
3455 }
3456 }
3457
lodepng_convert(unsigned char * out,const unsigned char * in,const LodePNGColorMode * mode_out,const LodePNGColorMode * mode_in,unsigned w,unsigned h)3458 unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
3459 const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in,
3460 unsigned w, unsigned h)
3461 {
3462 size_t i;
3463 ColorTree tree;
3464 size_t numpixels = w * h;
3465
3466 if(lodepng_color_mode_equal(mode_out, mode_in))
3467 {
3468 size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
3469 for(i = 0; i != numbytes; ++i) out[i] = in[i];
3470 return 0;
3471 }
3472
3473 if(mode_out->colortype == LCT_PALETTE)
3474 {
3475 size_t palettesize = mode_out->palettesize;
3476 const unsigned char* palette = mode_out->palette;
3477 size_t palsize = 1u << mode_out->bitdepth;
3478 /*if the user specified output palette but did not give the values, assume
3479 they want the values of the input color type (assuming that one is palette).
3480 Note that we never create a new palette ourselves.*/
3481 if(palettesize == 0)
3482 {
3483 palettesize = mode_in->palettesize;
3484 palette = mode_in->palette;
3485 }
3486 if(palettesize < palsize) palsize = palettesize;
3487 color_tree_init(&tree);
3488 for(i = 0; i != palsize; ++i)
3489 {
3490 const unsigned char* p = &palette[i * 4];
3491 color_tree_add(&tree, p[0], p[1], p[2], p[3], i);
3492 }
3493 }
3494
3495 if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16)
3496 {
3497 for(i = 0; i != numpixels; ++i)
3498 {
3499 unsigned short r = 0, g = 0, b = 0, a = 0;
3500 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
3501 rgba16ToPixel(out, i, mode_out, r, g, b, a);
3502 }
3503 }
3504 else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA)
3505 {
3506 getPixelColorsRGBA8(out, numpixels, 1, in, mode_in);
3507 }
3508 else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB)
3509 {
3510 getPixelColorsRGBA8(out, numpixels, 0, in, mode_in);
3511 }
3512 else
3513 {
3514 unsigned char r = 0, g = 0, b = 0, a = 0;
3515 for(i = 0; i != numpixels; ++i)
3516 {
3517 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
3518 CERROR_TRY_RETURN(rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a));
3519 }
3520 }
3521
3522 if(mode_out->colortype == LCT_PALETTE)
3523 {
3524 color_tree_cleanup(&tree);
3525 }
3526
3527 return 0; /*no error*/
3528 }
3529
3530 #ifdef LODEPNG_COMPILE_ENCODER
3531
lodepng_color_profile_init(LodePNGColorProfile * profile)3532 void lodepng_color_profile_init(LodePNGColorProfile* profile)
3533 {
3534 profile->colored = 0;
3535 profile->key = 0;
3536 profile->alpha = 0;
3537 profile->key_r = profile->key_g = profile->key_b = 0;
3538 profile->numcolors = 0;
3539 profile->bits = 1;
3540 }
3541
3542 /*function used for debug purposes with C++*/
3543 /*void printColorProfile(LodePNGColorProfile* p)
3544 {
3545 std::cout << "colored: " << (int)p->colored << ", ";
3546 std::cout << "key: " << (int)p->key << ", ";
3547 std::cout << "key_r: " << (int)p->key_r << ", ";
3548 std::cout << "key_g: " << (int)p->key_g << ", ";
3549 std::cout << "key_b: " << (int)p->key_b << ", ";
3550 std::cout << "alpha: " << (int)p->alpha << ", ";
3551 std::cout << "numcolors: " << (int)p->numcolors << ", ";
3552 std::cout << "bits: " << (int)p->bits << std::endl;
3553 }*/
3554
3555 /*Returns how many bits needed to represent given value (max 8 bit)*/
getValueRequiredBits(unsigned char value)3556 static unsigned getValueRequiredBits(unsigned char value)
3557 {
3558 if(value == 0 || value == 255) return 1;
3559 /*The scaling of 2-bit and 4-bit values uses multiples of 85 and 17*/
3560 if(value % 17 == 0) return value % 85 == 0 ? 2 : 4;
3561 return 8;
3562 }
3563
3564 /*profile must already have been inited with mode.
3565 It's ok to set some parameters of profile to done already.*/
lodepng_get_color_profile(LodePNGColorProfile * profile,const unsigned char * in,unsigned w,unsigned h,const LodePNGColorMode * mode)3566 unsigned lodepng_get_color_profile(LodePNGColorProfile* profile,
3567 const unsigned char* in, unsigned w, unsigned h,
3568 const LodePNGColorMode* mode)
3569 {
3570 unsigned error = 0;
3571 size_t i;
3572 ColorTree tree;
3573 size_t numpixels = w * h;
3574
3575 unsigned colored_done = lodepng_is_greyscale_type(mode) ? 1 : 0;
3576 unsigned alpha_done = lodepng_can_have_alpha(mode) ? 0 : 1;
3577 unsigned numcolors_done = 0;
3578 unsigned bpp = lodepng_get_bpp(mode);
3579 unsigned bits_done = bpp == 1 ? 1 : 0;
3580 unsigned maxnumcolors = 257;
3581 unsigned sixteen = 0;
3582 if(bpp <= 8) maxnumcolors = bpp == 1 ? 2 : (bpp == 2 ? 4 : (bpp == 4 ? 16 : 256));
3583
3584 color_tree_init(&tree);
3585
3586 /*Check if the 16-bit input is truly 16-bit*/
3587 if(mode->bitdepth == 16)
3588 {
3589 unsigned short r, g, b, a;
3590 for(i = 0; i != numpixels; ++i)
3591 {
3592 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);
3593 if((r & 255) != ((r >> 8) & 255) || (g & 255) != ((g >> 8) & 255) ||
3594 (b & 255) != ((b >> 8) & 255) || (a & 255) != ((a >> 8) & 255)) /*first and second byte differ*/
3595 {
3596 sixteen = 1;
3597 break;
3598 }
3599 }
3600 }
3601
3602 if(sixteen)
3603 {
3604 unsigned short r = 0, g = 0, b = 0, a = 0;
3605 profile->bits = 16;
3606 bits_done = numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/
3607
3608 for(i = 0; i != numpixels; ++i)
3609 {
3610 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);
3611
3612 if(!colored_done && (r != g || r != b))
3613 {
3614 profile->colored = 1;
3615 colored_done = 1;
3616 }
3617
3618 if(!alpha_done)
3619 {
3620 unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b);
3621 if(a != 65535 && (a != 0 || (profile->key && !matchkey)))
3622 {
3623 profile->alpha = 1;
3624 alpha_done = 1;
3625 if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
3626 }
3627 else if(a == 0 && !profile->alpha && !profile->key)
3628 {
3629 profile->key = 1;
3630 profile->key_r = r;
3631 profile->key_g = g;
3632 profile->key_b = b;
3633 }
3634 else if(a == 65535 && profile->key && matchkey)
3635 {
3636 /* Color key cannot be used if an opaque pixel also has that RGB color. */
3637 profile->alpha = 1;
3638 alpha_done = 1;
3639 }
3640 }
3641 if(alpha_done && numcolors_done && colored_done && bits_done) break;
3642 }
3643
3644 if(profile->key && !profile->alpha)
3645 {
3646 for(i = 0; i != numpixels; ++i)
3647 {
3648 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);
3649 if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b)
3650 {
3651 /* Color key cannot be used if an opaque pixel also has that RGB color. */
3652 profile->alpha = 1;
3653 alpha_done = 1;
3654 }
3655 }
3656 }
3657 }
3658 else /* < 16-bit */
3659 {
3660 unsigned char r = 0, g = 0, b = 0, a = 0;
3661 for(i = 0; i != numpixels; ++i)
3662 {
3663 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode);
3664
3665 if(!bits_done && profile->bits < 8)
3666 {
3667 /*only r is checked, < 8 bits is only relevant for greyscale*/
3668 unsigned bits = getValueRequiredBits(r);
3669 if(bits > profile->bits) profile->bits = bits;
3670 }
3671 bits_done = (profile->bits >= bpp);
3672
3673 if(!colored_done && (r != g || r != b))
3674 {
3675 profile->colored = 1;
3676 colored_done = 1;
3677 if(profile->bits < 8) profile->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/
3678 }
3679
3680 if(!alpha_done)
3681 {
3682 unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b);
3683 if(a != 255 && (a != 0 || (profile->key && !matchkey)))
3684 {
3685 profile->alpha = 1;
3686 alpha_done = 1;
3687 if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
3688 }
3689 else if(a == 0 && !profile->alpha && !profile->key)
3690 {
3691 profile->key = 1;
3692 profile->key_r = r;
3693 profile->key_g = g;
3694 profile->key_b = b;
3695 }
3696 else if(a == 255 && profile->key && matchkey)
3697 {
3698 /* Color key cannot be used if an opaque pixel also has that RGB color. */
3699 profile->alpha = 1;
3700 alpha_done = 1;
3701 if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
3702 }
3703 }
3704
3705 if(!numcolors_done)
3706 {
3707 if(!color_tree_has(&tree, r, g, b, a))
3708 {
3709 color_tree_add(&tree, r, g, b, a, profile->numcolors);
3710 if(profile->numcolors < 256)
3711 {
3712 unsigned char* p = profile->palette;
3713 unsigned n = profile->numcolors;
3714 p[n * 4 + 0] = r;
3715 p[n * 4 + 1] = g;
3716 p[n * 4 + 2] = b;
3717 p[n * 4 + 3] = a;
3718 }
3719 ++profile->numcolors;
3720 numcolors_done = profile->numcolors >= maxnumcolors;
3721 }
3722 }
3723
3724 if(alpha_done && numcolors_done && colored_done && bits_done) break;
3725 }
3726
3727 if(profile->key && !profile->alpha)
3728 {
3729 for(i = 0; i != numpixels; ++i)
3730 {
3731 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode);
3732 if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b)
3733 {
3734 /* Color key cannot be used if an opaque pixel also has that RGB color. */
3735 profile->alpha = 1;
3736 alpha_done = 1;
3737 }
3738 }
3739 }
3740
3741 /*make the profile's key always 16-bit for consistency - repeat each byte twice*/
3742 profile->key_r += (profile->key_r << 8);
3743 profile->key_g += (profile->key_g << 8);
3744 profile->key_b += (profile->key_b << 8);
3745 }
3746
3747 color_tree_cleanup(&tree);
3748 return error;
3749 }
3750
3751 /*Automatically chooses color type that gives smallest amount of bits in the
3752 output image, e.g. grey if there are only greyscale pixels, palette if there
3753 are less than 256 colors, ...
3754 Updates values of mode with a potentially smaller color model. mode_out should
3755 contain the user chosen color model, but will be overwritten with the new chosen one.*/
lodepng_auto_choose_color(LodePNGColorMode * mode_out,const unsigned char * image,unsigned w,unsigned h,const LodePNGColorMode * mode_in)3756 unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out,
3757 const unsigned char* image, unsigned w, unsigned h,
3758 const LodePNGColorMode* mode_in)
3759 {
3760 LodePNGColorProfile prof;
3761 unsigned error = 0;
3762 unsigned i, n, palettebits, grey_ok, palette_ok;
3763
3764 lodepng_color_profile_init(&prof);
3765 error = lodepng_get_color_profile(&prof, image, w, h, mode_in);
3766 if(error) return error;
3767 mode_out->key_defined = 0;
3768
3769 if(prof.key && w * h <= 16)
3770 {
3771 prof.alpha = 1; /*too few pixels to justify tRNS chunk overhead*/
3772 if(prof.bits < 8) prof.bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
3773 }
3774 grey_ok = !prof.colored && !prof.alpha; /*grey without alpha, with potentially low bits*/
3775 n = prof.numcolors;
3776 palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8));
3777 palette_ok = n <= 256 && (n * 2 < w * h) && prof.bits <= 8;
3778 if(w * h < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/
3779 if(grey_ok && prof.bits <= palettebits) palette_ok = 0; /*grey is less overhead*/
3780
3781 if(palette_ok)
3782 {
3783 unsigned char* p = prof.palette;
3784 lodepng_palette_clear(mode_out); /*remove potential earlier palette*/
3785 for(i = 0; i != prof.numcolors; ++i)
3786 {
3787 error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]);
3788 if(error) break;
3789 }
3790
3791 mode_out->colortype = LCT_PALETTE;
3792 mode_out->bitdepth = palettebits;
3793
3794 if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize
3795 && mode_in->bitdepth == mode_out->bitdepth)
3796 {
3797 /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/
3798 lodepng_color_mode_cleanup(mode_out);
3799 lodepng_color_mode_copy(mode_out, mode_in);
3800 }
3801 }
3802 else /*8-bit or 16-bit per channel*/
3803 {
3804 mode_out->bitdepth = prof.bits;
3805 mode_out->colortype = prof.alpha ? (prof.colored ? LCT_RGBA : LCT_GREY_ALPHA)
3806 : (prof.colored ? LCT_RGB : LCT_GREY);
3807
3808 if(prof.key && !prof.alpha)
3809 {
3810 unsigned mask = (1u << mode_out->bitdepth) - 1u; /*profile always uses 16-bit, mask converts it*/
3811 mode_out->key_r = prof.key_r & mask;
3812 mode_out->key_g = prof.key_g & mask;
3813 mode_out->key_b = prof.key_b & mask;
3814 mode_out->key_defined = 1;
3815 }
3816 }
3817
3818 return error;
3819 }
3820
3821 #endif /* #ifdef LODEPNG_COMPILE_ENCODER */
3822
3823 /*
3824 Paeth predicter, used by PNG filter type 4
3825 The parameters are of type short, but should come from unsigned chars, the shorts
3826 are only needed to make the paeth calculation correct.
3827 */
paethPredictor(short a,short b,short c)3828 static unsigned char paethPredictor(short a, short b, short c)
3829 {
3830 short pa = abs(b - c);
3831 short pb = abs(a - c);
3832 short pc = abs(a + b - c - c);
3833
3834 if(pc < pa && pc < pb) return (unsigned char)c;
3835 else if(pb < pa) return (unsigned char)b;
3836 else return (unsigned char)a;
3837 }
3838
3839 /*shared values used by multiple Adam7 related functions*/
3840
3841 static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/
3842 static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/
3843 static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/
3844 static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/
3845
3846 /*
3847 Outputs various dimensions and positions in the image related to the Adam7 reduced images.
3848 passw: output containing the width of the 7 passes
3849 passh: output containing the height of the 7 passes
3850 filter_passstart: output containing the index of the start and end of each
3851 reduced image with filter bytes
3852 padded_passstart output containing the index of the start and end of each
3853 reduced image when without filter bytes but with padded scanlines
3854 passstart: output containing the index of the start and end of each reduced
3855 image without padding between scanlines, but still padding between the images
3856 w, h: width and height of non-interlaced image
3857 bpp: bits per pixel
3858 "padded" is only relevant if bpp is less than 8 and a scanline or image does not
3859 end at a full byte
3860 */
Adam7_getpassvalues(unsigned passw[7],unsigned passh[7],size_t filter_passstart[8],size_t padded_passstart[8],size_t passstart[8],unsigned w,unsigned h,unsigned bpp)3861 static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8],
3862 size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp)
3863 {
3864 /*the passstart values have 8 values: the 8th one indicates the byte after the end of the 7th (= last) pass*/
3865 unsigned i;
3866
3867 /*calculate width and height in pixels of each pass*/
3868 for(i = 0; i != 7; ++i)
3869 {
3870 passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
3871 passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
3872 if(passw[i] == 0) passh[i] = 0;
3873 if(passh[i] == 0) passw[i] = 0;
3874 }
3875
3876 filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
3877 for(i = 0; i != 7; ++i)
3878 {
3879 /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/
3880 filter_passstart[i + 1] = filter_passstart[i]
3881 + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0);
3882 /*bits padded if needed to fill full byte at end of each scanline*/
3883 padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8);
3884 /*only padded at end of reduced image*/
3885 passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8;
3886 }
3887 }
3888
3889 #ifdef LODEPNG_COMPILE_DECODER
3890
3891 /* ////////////////////////////////////////////////////////////////////////// */
3892 /* / PNG Decoder / */
3893 /* ////////////////////////////////////////////////////////////////////////// */
3894
3895 /*read the information from the header and store it in the LodePNGInfo. return value is error*/
lodepng_inspect(unsigned * w,unsigned * h,LodePNGState * state,const unsigned char * in,size_t insize)3896 unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state,
3897 const unsigned char* in, size_t insize)
3898 {
3899 LodePNGInfo* info = &state->info_png;
3900 if(insize == 0 || in == 0)
3901 {
3902 CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/
3903 }
3904 if(insize < 33)
3905 {
3906 CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/
3907 }
3908
3909 /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/
3910 lodepng_info_cleanup(info);
3911 lodepng_info_init(info);
3912
3913 if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71
3914 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10)
3915 {
3916 CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/
3917 }
3918 if(lodepng_chunk_length(in + 8) != 13)
3919 {
3920 CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/
3921 }
3922 if(!lodepng_chunk_type_equals(in + 8, "IHDR"))
3923 {
3924 CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/
3925 }
3926
3927 /*read the values given in the header*/
3928 *w = lodepng_read32bitInt(&in[16]);
3929 *h = lodepng_read32bitInt(&in[20]);
3930 info->color.bitdepth = in[24];
3931 info->color.colortype = (LodePNGColorType)in[25];
3932 info->compression_method = in[26];
3933 info->filter_method = in[27];
3934 info->interlace_method = in[28];
3935
3936 if(*w == 0 || *h == 0)
3937 {
3938 CERROR_RETURN_ERROR(state->error, 93);
3939 }
3940
3941 if(!state->decoder.ignore_crc)
3942 {
3943 unsigned CRC = lodepng_read32bitInt(&in[29]);
3944 unsigned checksum = lodepng_crc32(&in[12], 17);
3945 if(CRC != checksum)
3946 {
3947 CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/
3948 }
3949 }
3950
3951 /*error: only compression method 0 is allowed in the specification*/
3952 if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32);
3953 /*error: only filter method 0 is allowed in the specification*/
3954 if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33);
3955 /*error: only interlace methods 0 and 1 exist in the specification*/
3956 if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34);
3957
3958 state->error = checkColorValidity(info->color.colortype, info->color.bitdepth);
3959 return state->error;
3960 }
3961
unfilterScanline(unsigned char * recon,const unsigned char * scanline,const unsigned char * precon,size_t bytewidth,unsigned char filterType,size_t length)3962 static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon,
3963 size_t bytewidth, unsigned char filterType, size_t length)
3964 {
3965 /*
3966 For PNG filter method 0
3967 unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte,
3968 the filter works byte per byte (bytewidth = 1)
3969 precon is the previous unfiltered scanline, recon the result, scanline the current one
3970 the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead
3971 recon and scanline MAY be the same memory address! precon must be disjoint.
3972 */
3973
3974 size_t i;
3975 switch(filterType)
3976 {
3977 case 0:
3978 for(i = 0; i != length; ++i) recon[i] = scanline[i];
3979 break;
3980 case 1:
3981 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
3982 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + recon[i - bytewidth];
3983 break;
3984 case 2:
3985 if(precon)
3986 {
3987 for(i = 0; i != length; ++i) recon[i] = scanline[i] + precon[i];
3988 }
3989 else
3990 {
3991 for(i = 0; i != length; ++i) recon[i] = scanline[i];
3992 }
3993 break;
3994 case 3:
3995 if(precon)
3996 {
3997 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + (precon[i] >> 1);
3998 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) >> 1);
3999 }
4000 else
4001 {
4002 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
4003 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + (recon[i - bytewidth] >> 1);
4004 }
4005 break;
4006 case 4:
4007 if(precon)
4008 {
4009 for(i = 0; i != bytewidth; ++i)
4010 {
4011 recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/
4012 }
4013 for(i = bytewidth; i < length; ++i)
4014 {
4015 recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
4016 }
4017 }
4018 else
4019 {
4020 for(i = 0; i != bytewidth; ++i)
4021 {
4022 recon[i] = scanline[i];
4023 }
4024 for(i = bytewidth; i < length; ++i)
4025 {
4026 /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/
4027 recon[i] = (scanline[i] + recon[i - bytewidth]);
4028 }
4029 }
4030 break;
4031 default: return 36; /*error: unexisting filter type given*/
4032 }
4033 return 0;
4034 }
4035
unfilter(unsigned char * out,const unsigned char * in,unsigned w,unsigned h,unsigned bpp)4036 static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
4037 {
4038 /*
4039 For PNG filter method 0
4040 this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 seven times)
4041 out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline
4042 w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel
4043 in and out are allowed to be the same memory address (but aren't the same size since in has the extra filter bytes)
4044 */
4045
4046 unsigned y;
4047 unsigned char* prevline = 0;
4048
4049 /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/
4050 size_t bytewidth = (bpp + 7) / 8;
4051 size_t linebytes = (w * bpp + 7) / 8;
4052
4053 for(y = 0; y < h; ++y)
4054 {
4055 size_t outindex = linebytes * y;
4056 size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
4057 unsigned char filterType = in[inindex];
4058
4059 CERROR_TRY_RETURN(unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes));
4060
4061 prevline = &out[outindex];
4062 }
4063
4064 return 0;
4065 }
4066
4067 /*
4068 in: Adam7 interlaced image, with no padding bits between scanlines, but between
4069 reduced images so that each reduced image starts at a byte.
4070 out: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h
4071 bpp: bits per pixel
4072 out has the following size in bits: w * h * bpp.
4073 in is possibly bigger due to padding bits between reduced images.
4074 out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation
4075 (because that's likely a little bit faster)
4076 NOTE: comments about padding bits are only relevant if bpp < 8
4077 */
Adam7_deinterlace(unsigned char * out,const unsigned char * in,unsigned w,unsigned h,unsigned bpp)4078 static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
4079 {
4080 unsigned passw[7], passh[7];
4081 size_t filter_passstart[8], padded_passstart[8], passstart[8];
4082 unsigned i;
4083
4084 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
4085
4086 if(bpp >= 8)
4087 {
4088 for(i = 0; i != 7; ++i)
4089 {
4090 unsigned x, y, b;
4091 size_t bytewidth = bpp / 8;
4092 for(y = 0; y < passh[i]; ++y)
4093 for(x = 0; x < passw[i]; ++x)
4094 {
4095 size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth;
4096 size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
4097 for(b = 0; b < bytewidth; ++b)
4098 {
4099 out[pixeloutstart + b] = in[pixelinstart + b];
4100 }
4101 }
4102 }
4103 }
4104 else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/
4105 {
4106 for(i = 0; i != 7; ++i)
4107 {
4108 unsigned x, y, b;
4109 unsigned ilinebits = bpp * passw[i];
4110 unsigned olinebits = bpp * w;
4111 size_t obp, ibp; /*bit pointers (for out and in buffer)*/
4112 for(y = 0; y < passh[i]; ++y)
4113 for(x = 0; x < passw[i]; ++x)
4114 {
4115 ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
4116 obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
4117 for(b = 0; b < bpp; ++b)
4118 {
4119 unsigned char bit = readBitFromReversedStream(&ibp, in);
4120 /*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/
4121 setBitOfReversedStream0(&obp, out, bit);
4122 }
4123 }
4124 }
4125 }
4126 }
4127
removePaddingBits(unsigned char * out,const unsigned char * in,size_t olinebits,size_t ilinebits,unsigned h)4128 static void removePaddingBits(unsigned char* out, const unsigned char* in,
4129 size_t olinebits, size_t ilinebits, unsigned h)
4130 {
4131 /*
4132 After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need
4133 to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers
4134 for the Adam7 code, the color convert code and the output to the user.
4135 in and out are allowed to be the same buffer, in may also be higher but still overlapping; in must
4136 have >= ilinebits*h bits, out must have >= olinebits*h bits, olinebits must be <= ilinebits
4137 also used to move bits after earlier such operations happened, e.g. in a sequence of reduced images from Adam7
4138 only useful if (ilinebits - olinebits) is a value in the range 1..7
4139 */
4140 unsigned y;
4141 size_t diff = ilinebits - olinebits;
4142 size_t ibp = 0, obp = 0; /*input and output bit pointers*/
4143 for(y = 0; y < h; ++y)
4144 {
4145 size_t x;
4146 for(x = 0; x < olinebits; ++x)
4147 {
4148 unsigned char bit = readBitFromReversedStream(&ibp, in);
4149 setBitOfReversedStream(&obp, out, bit);
4150 }
4151 ibp += diff;
4152 }
4153 }
4154
4155 /*out must be buffer big enough to contain full image, and in must contain the full decompressed data from
4156 the IDAT chunks (with filter index bytes and possible padding bits)
4157 return value is error*/
postProcessScanlines(unsigned char * out,unsigned char * in,unsigned w,unsigned h,const LodePNGInfo * info_png)4158 static unsigned postProcessScanlines(unsigned char* out, unsigned char* in,
4159 unsigned w, unsigned h, const LodePNGInfo* info_png)
4160 {
4161 /*
4162 This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype.
4163 Steps:
4164 *) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8)
4165 *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace
4166 NOTE: the in buffer will be overwritten with intermediate data!
4167 */
4168 unsigned bpp = lodepng_get_bpp(&info_png->color);
4169 if(bpp == 0) return 31; /*error: invalid colortype*/
4170
4171 if(info_png->interlace_method == 0)
4172 {
4173 if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
4174 {
4175 CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp));
4176 removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h);
4177 }
4178 /*we can immediately filter into the out buffer, no other steps needed*/
4179 else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp));
4180 }
4181 else /*interlace_method is 1 (Adam7)*/
4182 {
4183 unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
4184 unsigned i;
4185
4186 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
4187
4188 for(i = 0; i != 7; ++i)
4189 {
4190 CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp));
4191 /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline,
4192 move bytes instead of bits or move not at all*/
4193 if(bpp < 8)
4194 {
4195 /*remove padding bits in scanlines; after this there still may be padding
4196 bits between the different reduced images: each reduced image still starts nicely at a byte*/
4197 removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp,
4198 ((passw[i] * bpp + 7) / 8) * 8, passh[i]);
4199 }
4200 }
4201
4202 Adam7_deinterlace(out, in, w, h, bpp);
4203 }
4204
4205 return 0;
4206 }
4207
readChunk_PLTE(LodePNGColorMode * color,const unsigned char * data,size_t chunkLength)4208 static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength)
4209 {
4210 unsigned pos = 0, i;
4211 if(color->palette) lodepng_free(color->palette);
4212 color->palettesize = chunkLength / 3;
4213 color->palette = (unsigned char*)lodepng_malloc(4 * color->palettesize);
4214 if(!color->palette && color->palettesize)
4215 {
4216 color->palettesize = 0;
4217 return 83; /*alloc fail*/
4218 }
4219 if(color->palettesize > 256) return 38; /*error: palette too big*/
4220
4221 for(i = 0; i != color->palettesize; ++i)
4222 {
4223 color->palette[4 * i + 0] = data[pos++]; /*R*/
4224 color->palette[4 * i + 1] = data[pos++]; /*G*/
4225 color->palette[4 * i + 2] = data[pos++]; /*B*/
4226 color->palette[4 * i + 3] = 255; /*alpha*/
4227 }
4228
4229 return 0; /* OK */
4230 }
4231
readChunk_tRNS(LodePNGColorMode * color,const unsigned char * data,size_t chunkLength)4232 static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength)
4233 {
4234 unsigned i;
4235 if(color->colortype == LCT_PALETTE)
4236 {
4237 /*error: more alpha values given than there are palette entries*/
4238 if(chunkLength > color->palettesize) return 38;
4239
4240 for(i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i];
4241 }
4242 else if(color->colortype == LCT_GREY)
4243 {
4244 /*error: this chunk must be 2 bytes for greyscale image*/
4245 if(chunkLength != 2) return 30;
4246
4247 color->key_defined = 1;
4248 color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1];
4249 }
4250 else if(color->colortype == LCT_RGB)
4251 {
4252 /*error: this chunk must be 6 bytes for RGB image*/
4253 if(chunkLength != 6) return 41;
4254
4255 color->key_defined = 1;
4256 color->key_r = 256u * data[0] + data[1];
4257 color->key_g = 256u * data[2] + data[3];
4258 color->key_b = 256u * data[4] + data[5];
4259 }
4260 else return 42; /*error: tRNS chunk not allowed for other color models*/
4261
4262 return 0; /* OK */
4263 }
4264
4265
4266 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4267 /*background color chunk (bKGD)*/
readChunk_bKGD(LodePNGInfo * info,const unsigned char * data,size_t chunkLength)4268 static unsigned readChunk_bKGD(LodePNGInfo* info, const unsigned char* data, size_t chunkLength)
4269 {
4270 if(info->color.colortype == LCT_PALETTE)
4271 {
4272 /*error: this chunk must be 1 byte for indexed color image*/
4273 if(chunkLength != 1) return 43;
4274
4275 info->background_defined = 1;
4276 info->background_r = info->background_g = info->background_b = data[0];
4277 }
4278 else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA)
4279 {
4280 /*error: this chunk must be 2 bytes for greyscale image*/
4281 if(chunkLength != 2) return 44;
4282
4283 info->background_defined = 1;
4284 info->background_r = info->background_g = info->background_b = 256u * data[0] + data[1];
4285 }
4286 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA)
4287 {
4288 /*error: this chunk must be 6 bytes for greyscale image*/
4289 if(chunkLength != 6) return 45;
4290
4291 info->background_defined = 1;
4292 info->background_r = 256u * data[0] + data[1];
4293 info->background_g = 256u * data[2] + data[3];
4294 info->background_b = 256u * data[4] + data[5];
4295 }
4296
4297 return 0; /* OK */
4298 }
4299
4300 /*text chunk (tEXt)*/
readChunk_tEXt(LodePNGInfo * info,const unsigned char * data,size_t chunkLength)4301 static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, size_t chunkLength)
4302 {
4303 unsigned error = 0;
4304 char *key = 0, *str = 0;
4305 unsigned i;
4306
4307 while(!error) /*not really a while loop, only used to break on error*/
4308 {
4309 unsigned length, string2_begin;
4310
4311 length = 0;
4312 while(length < chunkLength && data[length] != 0) ++length;
4313 /*even though it's not allowed by the standard, no error is thrown if
4314 there's no null termination char, if the text is empty*/
4315 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
4316
4317 key = (char*)lodepng_malloc(length + 1);
4318 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
4319
4320 key[length] = 0;
4321 for(i = 0; i != length; ++i) key[i] = (char)data[i];
4322
4323 string2_begin = length + 1; /*skip keyword null terminator*/
4324
4325 length = chunkLength < string2_begin ? 0 : chunkLength - string2_begin;
4326 str = (char*)lodepng_malloc(length + 1);
4327 if(!str) CERROR_BREAK(error, 83); /*alloc fail*/
4328
4329 str[length] = 0;
4330 for(i = 0; i != length; ++i) str[i] = (char)data[string2_begin + i];
4331
4332 error = lodepng_add_text(info, key, str);
4333
4334 break;
4335 }
4336
4337 lodepng_free(key);
4338 lodepng_free(str);
4339
4340 return error;
4341 }
4342
4343 /*compressed text chunk (zTXt)*/
readChunk_zTXt(LodePNGInfo * info,const LodePNGDecompressSettings * zlibsettings,const unsigned char * data,size_t chunkLength)4344 static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings,
4345 const unsigned char* data, size_t chunkLength)
4346 {
4347 unsigned error = 0;
4348 unsigned i;
4349
4350 unsigned length, string2_begin;
4351 char *key = 0;
4352 ucvector decoded;
4353
4354 ucvector_init(&decoded);
4355
4356 while(!error) /*not really a while loop, only used to break on error*/
4357 {
4358 for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
4359 if(length + 2 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/
4360 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
4361
4362 key = (char*)lodepng_malloc(length + 1);
4363 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
4364
4365 key[length] = 0;
4366 for(i = 0; i != length; ++i) key[i] = (char)data[i];
4367
4368 if(data[length + 1] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/
4369
4370 string2_begin = length + 2;
4371 if(string2_begin > chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/
4372
4373 length = chunkLength - string2_begin;
4374 /*will fail if zlib error, e.g. if length is too small*/
4375 error = zlib_decompress(&decoded.data, &decoded.size,
4376 (unsigned char*)(&data[string2_begin]),
4377 length, zlibsettings);
4378 if(error) break;
4379 ucvector_push_back(&decoded, 0);
4380
4381 error = lodepng_add_text(info, key, (char*)decoded.data);
4382
4383 break;
4384 }
4385
4386 lodepng_free(key);
4387 ucvector_cleanup(&decoded);
4388
4389 return error;
4390 }
4391
4392 /*international text chunk (iTXt)*/
readChunk_iTXt(LodePNGInfo * info,const LodePNGDecompressSettings * zlibsettings,const unsigned char * data,size_t chunkLength)4393 static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings,
4394 const unsigned char* data, size_t chunkLength)
4395 {
4396 unsigned error = 0;
4397 unsigned i;
4398
4399 unsigned length, begin, compressed;
4400 char *key = 0, *langtag = 0, *transkey = 0;
4401 ucvector decoded;
4402 ucvector_init(&decoded);
4403
4404 while(!error) /*not really a while loop, only used to break on error*/
4405 {
4406 /*Quick check if the chunk length isn't too small. Even without check
4407 it'd still fail with other error checks below if it's too short. This just gives a different error code.*/
4408 if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/
4409
4410 /*read the key*/
4411 for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
4412 if(length + 3 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char, corrupt?*/
4413 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
4414
4415 key = (char*)lodepng_malloc(length + 1);
4416 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
4417
4418 key[length] = 0;
4419 for(i = 0; i != length; ++i) key[i] = (char)data[i];
4420
4421 /*read the compression method*/
4422 compressed = data[length + 1];
4423 if(data[length + 2] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/
4424
4425 /*even though it's not allowed by the standard, no error is thrown if
4426 there's no null termination char, if the text is empty for the next 3 texts*/
4427
4428 /*read the langtag*/
4429 begin = length + 3;
4430 length = 0;
4431 for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
4432
4433 langtag = (char*)lodepng_malloc(length + 1);
4434 if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/
4435
4436 langtag[length] = 0;
4437 for(i = 0; i != length; ++i) langtag[i] = (char)data[begin + i];
4438
4439 /*read the transkey*/
4440 begin += length + 1;
4441 length = 0;
4442 for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
4443
4444 transkey = (char*)lodepng_malloc(length + 1);
4445 if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/
4446
4447 transkey[length] = 0;
4448 for(i = 0; i != length; ++i) transkey[i] = (char)data[begin + i];
4449
4450 /*read the actual text*/
4451 begin += length + 1;
4452
4453 length = chunkLength < begin ? 0 : chunkLength - begin;
4454
4455 if(compressed)
4456 {
4457 /*will fail if zlib error, e.g. if length is too small*/
4458 error = zlib_decompress(&decoded.data, &decoded.size,
4459 (unsigned char*)(&data[begin]),
4460 length, zlibsettings);
4461 if(error) break;
4462 if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size;
4463 ucvector_push_back(&decoded, 0);
4464 }
4465 else
4466 {
4467 if(!ucvector_resize(&decoded, length + 1)) CERROR_BREAK(error, 83 /*alloc fail*/);
4468
4469 decoded.data[length] = 0;
4470 for(i = 0; i != length; ++i) decoded.data[i] = data[begin + i];
4471 }
4472
4473 error = lodepng_add_itext(info, key, langtag, transkey, (char*)decoded.data);
4474
4475 break;
4476 }
4477
4478 lodepng_free(key);
4479 lodepng_free(langtag);
4480 lodepng_free(transkey);
4481 ucvector_cleanup(&decoded);
4482
4483 return error;
4484 }
4485
readChunk_tIME(LodePNGInfo * info,const unsigned char * data,size_t chunkLength)4486 static unsigned readChunk_tIME(LodePNGInfo* info, const unsigned char* data, size_t chunkLength)
4487 {
4488 if(chunkLength != 7) return 73; /*invalid tIME chunk size*/
4489
4490 info->time_defined = 1;
4491 info->time.year = 256u * data[0] + data[1];
4492 info->time.month = data[2];
4493 info->time.day = data[3];
4494 info->time.hour = data[4];
4495 info->time.minute = data[5];
4496 info->time.second = data[6];
4497
4498 return 0; /* OK */
4499 }
4500
readChunk_pHYs(LodePNGInfo * info,const unsigned char * data,size_t chunkLength)4501 static unsigned readChunk_pHYs(LodePNGInfo* info, const unsigned char* data, size_t chunkLength)
4502 {
4503 if(chunkLength != 9) return 74; /*invalid pHYs chunk size*/
4504
4505 info->phys_defined = 1;
4506 info->phys_x = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3];
4507 info->phys_y = 16777216u * data[4] + 65536u * data[5] + 256u * data[6] + data[7];
4508 info->phys_unit = data[8];
4509
4510 return 0; /* OK */
4511 }
4512 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4513
4514 /*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/
decodeGeneric(unsigned char ** out,unsigned * w,unsigned * h,LodePNGState * state,const unsigned char * in,size_t insize)4515 static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
4516 LodePNGState* state,
4517 const unsigned char* in, size_t insize)
4518 {
4519 unsigned char IEND = 0;
4520 const unsigned char* chunk;
4521 size_t i;
4522 ucvector idat; /*the data from idat chunks*/
4523 ucvector scanlines;
4524 size_t predict;
4525 size_t numpixels;
4526 size_t outsize;
4527
4528 /*for unknown chunk order*/
4529 unsigned unknown = 0;
4530 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4531 unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/
4532 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4533
4534 /*provide some proper output values if error will happen*/
4535 *out = 0;
4536
4537 state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/
4538 if(state->error) return;
4539
4540 numpixels = *w * *h;
4541
4542 /*multiplication overflow*/
4543 if(*h != 0 && numpixels / *h != *w) CERROR_RETURN(state->error, 92);
4544 /*multiplication overflow possible further below. Allows up to 2^31-1 pixel
4545 bytes with 16-bit RGBA, the rest is room for filter bytes.*/
4546 if(numpixels > 268435455) CERROR_RETURN(state->error, 92);
4547
4548 ucvector_init(&idat);
4549 chunk = &in[33]; /*first byte of the first chunk after the header*/
4550
4551 /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk.
4552 IDAT data is put at the start of the in buffer*/
4553 while(!IEND && !state->error)
4554 {
4555 unsigned chunkLength;
4556 const unsigned char* data; /*the data in the chunk*/
4557
4558 /*error: size of the in buffer too small to contain next chunk*/
4559 if((size_t)((chunk - in) + 12) > insize || chunk < in) CERROR_BREAK(state->error, 30);
4560
4561 /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/
4562 chunkLength = lodepng_chunk_length(chunk);
4563 /*error: chunk length larger than the max PNG chunk size*/
4564 if(chunkLength > 2147483647) CERROR_BREAK(state->error, 63);
4565
4566 if((size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in)
4567 {
4568 CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk*/
4569 }
4570
4571 data = lodepng_chunk_data_const(chunk);
4572
4573 /*IDAT chunk, containing compressed image data*/
4574 if(lodepng_chunk_type_equals(chunk, "IDAT"))
4575 {
4576 size_t oldsize = idat.size;
4577 if(!ucvector_resize(&idat, oldsize + chunkLength)) CERROR_BREAK(state->error, 83 /*alloc fail*/);
4578 for(i = 0; i != chunkLength; ++i) idat.data[oldsize + i] = data[i];
4579 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4580 critical_pos = 3;
4581 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4582 }
4583 /*IEND chunk*/
4584 else if(lodepng_chunk_type_equals(chunk, "IEND"))
4585 {
4586 IEND = 1;
4587 }
4588 /*palette chunk (PLTE)*/
4589 else if(lodepng_chunk_type_equals(chunk, "PLTE"))
4590 {
4591 state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength);
4592 if(state->error) break;
4593 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4594 critical_pos = 2;
4595 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4596 }
4597 /*palette transparency chunk (tRNS)*/
4598 else if(lodepng_chunk_type_equals(chunk, "tRNS"))
4599 {
4600 state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength);
4601 if(state->error) break;
4602 }
4603 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4604 /*background color chunk (bKGD)*/
4605 else if(lodepng_chunk_type_equals(chunk, "bKGD"))
4606 {
4607 state->error = readChunk_bKGD(&state->info_png, data, chunkLength);
4608 if(state->error) break;
4609 }
4610 /*text chunk (tEXt)*/
4611 else if(lodepng_chunk_type_equals(chunk, "tEXt"))
4612 {
4613 if(state->decoder.read_text_chunks)
4614 {
4615 state->error = readChunk_tEXt(&state->info_png, data, chunkLength);
4616 if(state->error) break;
4617 }
4618 }
4619 /*compressed text chunk (zTXt)*/
4620 else if(lodepng_chunk_type_equals(chunk, "zTXt"))
4621 {
4622 if(state->decoder.read_text_chunks)
4623 {
4624 state->error = readChunk_zTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength);
4625 if(state->error) break;
4626 }
4627 }
4628 /*international text chunk (iTXt)*/
4629 else if(lodepng_chunk_type_equals(chunk, "iTXt"))
4630 {
4631 if(state->decoder.read_text_chunks)
4632 {
4633 state->error = readChunk_iTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength);
4634 if(state->error) break;
4635 }
4636 }
4637 else if(lodepng_chunk_type_equals(chunk, "tIME"))
4638 {
4639 state->error = readChunk_tIME(&state->info_png, data, chunkLength);
4640 if(state->error) break;
4641 }
4642 else if(lodepng_chunk_type_equals(chunk, "pHYs"))
4643 {
4644 state->error = readChunk_pHYs(&state->info_png, data, chunkLength);
4645 if(state->error) break;
4646 }
4647 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4648 else /*it's not an implemented chunk type, so ignore it: skip over the data*/
4649 {
4650 /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/
4651 if(!lodepng_chunk_ancillary(chunk)) CERROR_BREAK(state->error, 69);
4652
4653 unknown = 1;
4654 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4655 if(state->decoder.remember_unknown_chunks)
4656 {
4657 state->error = lodepng_chunk_append(&state->info_png.unknown_chunks_data[critical_pos - 1],
4658 &state->info_png.unknown_chunks_size[critical_pos - 1], chunk);
4659 if(state->error) break;
4660 }
4661 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4662 }
4663
4664 if(!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/
4665 {
4666 if(lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/
4667 }
4668
4669 if(!IEND) chunk = lodepng_chunk_next_const(chunk);
4670 }
4671
4672 ucvector_init(&scanlines);
4673 /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation.
4674 If the decompressed size does not match the prediction, the image must be corrupt.*/
4675 if(state->info_png.interlace_method == 0)
4676 {
4677 /*The extra *h is added because this are the filter bytes every scanline starts with*/
4678 predict = lodepng_get_raw_size_idat(*w, *h, &state->info_png.color) + *h;
4679 }
4680 else
4681 {
4682 /*Adam-7 interlaced: predicted size is the sum of the 7 sub-images sizes*/
4683 const LodePNGColorMode* color = &state->info_png.color;
4684 predict = 0;
4685 predict += lodepng_get_raw_size_idat((*w + 7) >> 3, (*h + 7) >> 3, color) + ((*h + 7) >> 3);
4686 if(*w > 4) predict += lodepng_get_raw_size_idat((*w + 3) >> 3, (*h + 7) >> 3, color) + ((*h + 7) >> 3);
4687 predict += lodepng_get_raw_size_idat((*w + 3) >> 2, (*h + 3) >> 3, color) + ((*h + 3) >> 3);
4688 if(*w > 2) predict += lodepng_get_raw_size_idat((*w + 1) >> 2, (*h + 3) >> 2, color) + ((*h + 3) >> 2);
4689 predict += lodepng_get_raw_size_idat((*w + 1) >> 1, (*h + 1) >> 2, color) + ((*h + 1) >> 2);
4690 if(*w > 1) predict += lodepng_get_raw_size_idat((*w + 0) >> 1, (*h + 1) >> 1, color) + ((*h + 1) >> 1);
4691 predict += lodepng_get_raw_size_idat((*w + 0), (*h + 0) >> 1, color) + ((*h + 0) >> 1);
4692 }
4693 if(!state->error && !ucvector_reserve(&scanlines, predict)) state->error = 83; /*alloc fail*/
4694 if(!state->error)
4695 {
4696 state->error = zlib_decompress(&scanlines.data, &scanlines.size, idat.data,
4697 idat.size, &state->decoder.zlibsettings);
4698 if(!state->error && scanlines.size != predict) state->error = 91; /*decompressed size doesn't match prediction*/
4699 }
4700 ucvector_cleanup(&idat);
4701
4702 if(!state->error)
4703 {
4704 outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color);
4705 *out = (unsigned char*)lodepng_malloc(outsize);
4706 if(!*out) state->error = 83; /*alloc fail*/
4707 }
4708 if(!state->error)
4709 {
4710 for(i = 0; i < outsize; i++) (*out)[i] = 0;
4711 state->error = postProcessScanlines(*out, scanlines.data, *w, *h, &state->info_png);
4712 }
4713 ucvector_cleanup(&scanlines);
4714 }
4715
lodepng_decode(unsigned char ** out,unsigned * w,unsigned * h,LodePNGState * state,const unsigned char * in,size_t insize)4716 unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h,
4717 LodePNGState* state,
4718 const unsigned char* in, size_t insize)
4719 {
4720 *out = 0;
4721 decodeGeneric(out, w, h, state, in, insize);
4722 if(state->error) return state->error;
4723 if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color))
4724 {
4725 /*same color type, no copying or converting of data needed*/
4726 /*store the info_png color settings on the info_raw so that the info_raw still reflects what colortype
4727 the raw image has to the end user*/
4728 if(!state->decoder.color_convert)
4729 {
4730 state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color);
4731 if(state->error) return state->error;
4732 }
4733 }
4734 else
4735 {
4736 /*color conversion needed; sort of copy of the data*/
4737 unsigned char* data = *out;
4738 size_t outsize;
4739
4740 /*TODO: check if this works according to the statement in the documentation: "The converter can convert
4741 from greyscale input color type, to 8-bit greyscale or greyscale with alpha"*/
4742 if(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA)
4743 && !(state->info_raw.bitdepth == 8))
4744 {
4745 return 56; /*unsupported color mode conversion*/
4746 }
4747
4748 outsize = lodepng_get_raw_size(*w, *h, &state->info_raw);
4749 *out = (unsigned char*)lodepng_malloc(outsize);
4750 if(!(*out))
4751 {
4752 state->error = 83; /*alloc fail*/
4753 }
4754 else state->error = lodepng_convert(*out, data, &state->info_raw,
4755 &state->info_png.color, *w, *h);
4756 lodepng_free(data);
4757 }
4758 return state->error;
4759 }
4760
lodepng_decode_memory(unsigned char ** out,unsigned * w,unsigned * h,const unsigned char * in,size_t insize,LodePNGColorType colortype,unsigned bitdepth)4761 unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in,
4762 size_t insize, LodePNGColorType colortype, unsigned bitdepth)
4763 {
4764 unsigned error;
4765 LodePNGState state;
4766 lodepng_state_init(&state);
4767 state.info_raw.colortype = colortype;
4768 state.info_raw.bitdepth = bitdepth;
4769 error = lodepng_decode(out, w, h, &state, in, insize);
4770 lodepng_state_cleanup(&state);
4771 return error;
4772 }
4773
lodepng_decode32(unsigned char ** out,unsigned * w,unsigned * h,const unsigned char * in,size_t insize)4774 unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize)
4775 {
4776 return lodepng_decode_memory(out, w, h, in, insize, LCT_RGBA, 8);
4777 }
4778
lodepng_decode24(unsigned char ** out,unsigned * w,unsigned * h,const unsigned char * in,size_t insize)4779 unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize)
4780 {
4781 return lodepng_decode_memory(out, w, h, in, insize, LCT_RGB, 8);
4782 }
4783
4784 #ifdef LODEPNG_COMPILE_DISK
lodepng_decode_file(unsigned char ** out,unsigned * w,unsigned * h,const char * filename,LodePNGColorType colortype,unsigned bitdepth)4785 unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename,
4786 LodePNGColorType colortype, unsigned bitdepth)
4787 {
4788 unsigned char* buffer = 0;
4789 size_t buffersize;
4790 unsigned error;
4791 error = lodepng_load_file(&buffer, &buffersize, filename);
4792 if(!error) error = lodepng_decode_memory(out, w, h, buffer, buffersize, colortype, bitdepth);
4793 lodepng_free(buffer);
4794 return error;
4795 }
4796
lodepng_decode32_file(unsigned char ** out,unsigned * w,unsigned * h,const char * filename)4797 unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename)
4798 {
4799 return lodepng_decode_file(out, w, h, filename, LCT_RGBA, 8);
4800 }
4801
lodepng_decode24_file(unsigned char ** out,unsigned * w,unsigned * h,const char * filename)4802 unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename)
4803 {
4804 return lodepng_decode_file(out, w, h, filename, LCT_RGB, 8);
4805 }
4806 #endif /*LODEPNG_COMPILE_DISK*/
4807
lodepng_decoder_settings_init(LodePNGDecoderSettings * settings)4808 void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings)
4809 {
4810 settings->color_convert = 1;
4811 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4812 settings->read_text_chunks = 1;
4813 settings->remember_unknown_chunks = 0;
4814 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4815 settings->ignore_crc = 0;
4816 lodepng_decompress_settings_init(&settings->zlibsettings);
4817 }
4818
4819 #endif /*LODEPNG_COMPILE_DECODER*/
4820
4821 #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER)
4822
lodepng_state_init(LodePNGState * state)4823 void lodepng_state_init(LodePNGState* state)
4824 {
4825 #ifdef LODEPNG_COMPILE_DECODER
4826 lodepng_decoder_settings_init(&state->decoder);
4827 #endif /*LODEPNG_COMPILE_DECODER*/
4828 #ifdef LODEPNG_COMPILE_ENCODER
4829 lodepng_encoder_settings_init(&state->encoder);
4830 #endif /*LODEPNG_COMPILE_ENCODER*/
4831 lodepng_color_mode_init(&state->info_raw);
4832 lodepng_info_init(&state->info_png);
4833 state->error = 1;
4834 }
4835
lodepng_state_cleanup(LodePNGState * state)4836 void lodepng_state_cleanup(LodePNGState* state)
4837 {
4838 lodepng_color_mode_cleanup(&state->info_raw);
4839 lodepng_info_cleanup(&state->info_png);
4840 }
4841
lodepng_state_copy(LodePNGState * dest,const LodePNGState * source)4842 void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source)
4843 {
4844 lodepng_state_cleanup(dest);
4845 *dest = *source;
4846 lodepng_color_mode_init(&dest->info_raw);
4847 lodepng_info_init(&dest->info_png);
4848 dest->error = lodepng_color_mode_copy(&dest->info_raw, &source->info_raw); if(dest->error) return;
4849 dest->error = lodepng_info_copy(&dest->info_png, &source->info_png); if(dest->error) return;
4850 }
4851
4852 #endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */
4853
4854 #ifdef LODEPNG_COMPILE_ENCODER
4855
4856 /* ////////////////////////////////////////////////////////////////////////// */
4857 /* / PNG Encoder / */
4858 /* ////////////////////////////////////////////////////////////////////////// */
4859
4860 /*chunkName must be string of 4 characters*/
addChunk(ucvector * out,const char * chunkName,const unsigned char * data,size_t length)4861 static unsigned addChunk(ucvector* out, const char* chunkName, const unsigned char* data, size_t length)
4862 {
4863 CERROR_TRY_RETURN(lodepng_chunk_create(&out->data, &out->size, (unsigned)length, chunkName, data));
4864 out->allocsize = out->size; /*fix the allocsize again*/
4865 return 0;
4866 }
4867
writeSignature(ucvector * out)4868 static void writeSignature(ucvector* out)
4869 {
4870 /*8 bytes PNG signature, aka the magic bytes*/
4871 ucvector_push_back(out, 137);
4872 ucvector_push_back(out, 80);
4873 ucvector_push_back(out, 78);
4874 ucvector_push_back(out, 71);
4875 ucvector_push_back(out, 13);
4876 ucvector_push_back(out, 10);
4877 ucvector_push_back(out, 26);
4878 ucvector_push_back(out, 10);
4879 }
4880
addChunk_IHDR(ucvector * out,unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth,unsigned interlace_method)4881 static unsigned addChunk_IHDR(ucvector* out, unsigned w, unsigned h,
4882 LodePNGColorType colortype, unsigned bitdepth, unsigned interlace_method)
4883 {
4884 unsigned error = 0;
4885 ucvector header;
4886 ucvector_init(&header);
4887
4888 lodepng_add32bitInt(&header, w); /*width*/
4889 lodepng_add32bitInt(&header, h); /*height*/
4890 ucvector_push_back(&header, (unsigned char)bitdepth); /*bit depth*/
4891 ucvector_push_back(&header, (unsigned char)colortype); /*color type*/
4892 ucvector_push_back(&header, 0); /*compression method*/
4893 ucvector_push_back(&header, 0); /*filter method*/
4894 ucvector_push_back(&header, interlace_method); /*interlace method*/
4895
4896 error = addChunk(out, "IHDR", header.data, header.size);
4897 ucvector_cleanup(&header);
4898
4899 return error;
4900 }
4901
addChunk_PLTE(ucvector * out,const LodePNGColorMode * info)4902 static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info)
4903 {
4904 unsigned error = 0;
4905 size_t i;
4906 ucvector PLTE;
4907 ucvector_init(&PLTE);
4908 for(i = 0; i != info->palettesize * 4; ++i)
4909 {
4910 /*add all channels except alpha channel*/
4911 if(i % 4 != 3) ucvector_push_back(&PLTE, info->palette[i]);
4912 }
4913 error = addChunk(out, "PLTE", PLTE.data, PLTE.size);
4914 ucvector_cleanup(&PLTE);
4915
4916 return error;
4917 }
4918
addChunk_tRNS(ucvector * out,const LodePNGColorMode * info)4919 static unsigned addChunk_tRNS(ucvector* out, const LodePNGColorMode* info)
4920 {
4921 unsigned error = 0;
4922 size_t i;
4923 ucvector tRNS;
4924 ucvector_init(&tRNS);
4925 if(info->colortype == LCT_PALETTE)
4926 {
4927 size_t amount = info->palettesize;
4928 /*the tail of palette values that all have 255 as alpha, does not have to be encoded*/
4929 for(i = info->palettesize; i != 0; --i)
4930 {
4931 if(info->palette[4 * (i - 1) + 3] == 255) --amount;
4932 else break;
4933 }
4934 /*add only alpha channel*/
4935 for(i = 0; i != amount; ++i) ucvector_push_back(&tRNS, info->palette[4 * i + 3]);
4936 }
4937 else if(info->colortype == LCT_GREY)
4938 {
4939 if(info->key_defined)
4940 {
4941 ucvector_push_back(&tRNS, (unsigned char)(info->key_r >> 8));
4942 ucvector_push_back(&tRNS, (unsigned char)(info->key_r & 255));
4943 }
4944 }
4945 else if(info->colortype == LCT_RGB)
4946 {
4947 if(info->key_defined)
4948 {
4949 ucvector_push_back(&tRNS, (unsigned char)(info->key_r >> 8));
4950 ucvector_push_back(&tRNS, (unsigned char)(info->key_r & 255));
4951 ucvector_push_back(&tRNS, (unsigned char)(info->key_g >> 8));
4952 ucvector_push_back(&tRNS, (unsigned char)(info->key_g & 255));
4953 ucvector_push_back(&tRNS, (unsigned char)(info->key_b >> 8));
4954 ucvector_push_back(&tRNS, (unsigned char)(info->key_b & 255));
4955 }
4956 }
4957
4958 error = addChunk(out, "tRNS", tRNS.data, tRNS.size);
4959 ucvector_cleanup(&tRNS);
4960
4961 return error;
4962 }
4963
addChunk_IDAT(ucvector * out,const unsigned char * data,size_t datasize,LodePNGCompressSettings * zlibsettings)4964 static unsigned addChunk_IDAT(ucvector* out, const unsigned char* data, size_t datasize,
4965 LodePNGCompressSettings* zlibsettings)
4966 {
4967 ucvector zlibdata;
4968 unsigned error = 0;
4969
4970 /*compress with the Zlib compressor*/
4971 ucvector_init(&zlibdata);
4972 error = zlib_compress(&zlibdata.data, &zlibdata.size, data, datasize, zlibsettings);
4973 if(!error) error = addChunk(out, "IDAT", zlibdata.data, zlibdata.size);
4974 ucvector_cleanup(&zlibdata);
4975
4976 return error;
4977 }
4978
addChunk_IEND(ucvector * out)4979 static unsigned addChunk_IEND(ucvector* out)
4980 {
4981 unsigned error = 0;
4982 error = addChunk(out, "IEND", 0, 0);
4983 return error;
4984 }
4985
4986 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4987
addChunk_tEXt(ucvector * out,const char * keyword,const char * textstring)4988 static unsigned addChunk_tEXt(ucvector* out, const char* keyword, const char* textstring)
4989 {
4990 unsigned error = 0;
4991 size_t i;
4992 ucvector text;
4993 ucvector_init(&text);
4994 for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)keyword[i]);
4995 if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/
4996 ucvector_push_back(&text, 0); /*0 termination char*/
4997 for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)textstring[i]);
4998 error = addChunk(out, "tEXt", text.data, text.size);
4999 ucvector_cleanup(&text);
5000
5001 return error;
5002 }
5003
addChunk_zTXt(ucvector * out,const char * keyword,const char * textstring,LodePNGCompressSettings * zlibsettings)5004 static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* textstring,
5005 LodePNGCompressSettings* zlibsettings)
5006 {
5007 unsigned error = 0;
5008 ucvector data, compressed;
5009 size_t i, textsize = strlen(textstring);
5010
5011 ucvector_init(&data);
5012 ucvector_init(&compressed);
5013 for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]);
5014 if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/
5015 ucvector_push_back(&data, 0); /*0 termination char*/
5016 ucvector_push_back(&data, 0); /*compression method: 0*/
5017
5018 error = zlib_compress(&compressed.data, &compressed.size,
5019 (unsigned char*)textstring, textsize, zlibsettings);
5020 if(!error)
5021 {
5022 for(i = 0; i != compressed.size; ++i) ucvector_push_back(&data, compressed.data[i]);
5023 error = addChunk(out, "zTXt", data.data, data.size);
5024 }
5025
5026 ucvector_cleanup(&compressed);
5027 ucvector_cleanup(&data);
5028 return error;
5029 }
5030
addChunk_iTXt(ucvector * out,unsigned compressed,const char * keyword,const char * langtag,const char * transkey,const char * textstring,LodePNGCompressSettings * zlibsettings)5031 static unsigned addChunk_iTXt(ucvector* out, unsigned compressed, const char* keyword, const char* langtag,
5032 const char* transkey, const char* textstring, LodePNGCompressSettings* zlibsettings)
5033 {
5034 unsigned error = 0;
5035 ucvector data;
5036 size_t i, textsize = strlen(textstring);
5037
5038 ucvector_init(&data);
5039
5040 for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]);
5041 if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/
5042 ucvector_push_back(&data, 0); /*null termination char*/
5043 ucvector_push_back(&data, compressed ? 1 : 0); /*compression flag*/
5044 ucvector_push_back(&data, 0); /*compression method*/
5045 for(i = 0; langtag[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)langtag[i]);
5046 ucvector_push_back(&data, 0); /*null termination char*/
5047 for(i = 0; transkey[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)transkey[i]);
5048 ucvector_push_back(&data, 0); /*null termination char*/
5049
5050 if(compressed)
5051 {
5052 ucvector compressed_data;
5053 ucvector_init(&compressed_data);
5054 error = zlib_compress(&compressed_data.data, &compressed_data.size,
5055 (unsigned char*)textstring, textsize, zlibsettings);
5056 if(!error)
5057 {
5058 for(i = 0; i != compressed_data.size; ++i) ucvector_push_back(&data, compressed_data.data[i]);
5059 }
5060 ucvector_cleanup(&compressed_data);
5061 }
5062 else /*not compressed*/
5063 {
5064 for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)textstring[i]);
5065 }
5066
5067 if(!error) error = addChunk(out, "iTXt", data.data, data.size);
5068 ucvector_cleanup(&data);
5069 return error;
5070 }
5071
addChunk_bKGD(ucvector * out,const LodePNGInfo * info)5072 static unsigned addChunk_bKGD(ucvector* out, const LodePNGInfo* info)
5073 {
5074 unsigned error = 0;
5075 ucvector bKGD;
5076 ucvector_init(&bKGD);
5077 if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA)
5078 {
5079 ucvector_push_back(&bKGD, (unsigned char)(info->background_r >> 8));
5080 ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255));
5081 }
5082 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA)
5083 {
5084 ucvector_push_back(&bKGD, (unsigned char)(info->background_r >> 8));
5085 ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255));
5086 ucvector_push_back(&bKGD, (unsigned char)(info->background_g >> 8));
5087 ucvector_push_back(&bKGD, (unsigned char)(info->background_g & 255));
5088 ucvector_push_back(&bKGD, (unsigned char)(info->background_b >> 8));
5089 ucvector_push_back(&bKGD, (unsigned char)(info->background_b & 255));
5090 }
5091 else if(info->color.colortype == LCT_PALETTE)
5092 {
5093 ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255)); /*palette index*/
5094 }
5095
5096 error = addChunk(out, "bKGD", bKGD.data, bKGD.size);
5097 ucvector_cleanup(&bKGD);
5098
5099 return error;
5100 }
5101
addChunk_tIME(ucvector * out,const LodePNGTime * time)5102 static unsigned addChunk_tIME(ucvector* out, const LodePNGTime* time)
5103 {
5104 unsigned error = 0;
5105 unsigned char* data = (unsigned char*)lodepng_malloc(7);
5106 if(!data) return 83; /*alloc fail*/
5107 data[0] = (unsigned char)(time->year >> 8);
5108 data[1] = (unsigned char)(time->year & 255);
5109 data[2] = (unsigned char)time->month;
5110 data[3] = (unsigned char)time->day;
5111 data[4] = (unsigned char)time->hour;
5112 data[5] = (unsigned char)time->minute;
5113 data[6] = (unsigned char)time->second;
5114 error = addChunk(out, "tIME", data, 7);
5115 lodepng_free(data);
5116 return error;
5117 }
5118
addChunk_pHYs(ucvector * out,const LodePNGInfo * info)5119 static unsigned addChunk_pHYs(ucvector* out, const LodePNGInfo* info)
5120 {
5121 unsigned error = 0;
5122 ucvector data;
5123 ucvector_init(&data);
5124
5125 lodepng_add32bitInt(&data, info->phys_x);
5126 lodepng_add32bitInt(&data, info->phys_y);
5127 ucvector_push_back(&data, info->phys_unit);
5128
5129 error = addChunk(out, "pHYs", data.data, data.size);
5130 ucvector_cleanup(&data);
5131
5132 return error;
5133 }
5134
5135 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
5136
filterScanline(unsigned char * out,const unsigned char * scanline,const unsigned char * prevline,size_t length,size_t bytewidth,unsigned char filterType)5137 static void filterScanline(unsigned char* out, const unsigned char* scanline, const unsigned char* prevline,
5138 size_t length, size_t bytewidth, unsigned char filterType)
5139 {
5140 size_t i;
5141 switch(filterType)
5142 {
5143 case 0: /*None*/
5144 for(i = 0; i != length; ++i) out[i] = scanline[i];
5145 break;
5146 case 1: /*Sub*/
5147 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
5148 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth];
5149 break;
5150 case 2: /*Up*/
5151 if(prevline)
5152 {
5153 for(i = 0; i != length; ++i) out[i] = scanline[i] - prevline[i];
5154 }
5155 else
5156 {
5157 for(i = 0; i != length; ++i) out[i] = scanline[i];
5158 }
5159 break;
5160 case 3: /*Average*/
5161 if(prevline)
5162 {
5163 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - (prevline[i] >> 1);
5164 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) >> 1);
5165 }
5166 else
5167 {
5168 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
5169 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - (scanline[i - bytewidth] >> 1);
5170 }
5171 break;
5172 case 4: /*Paeth*/
5173 if(prevline)
5174 {
5175 /*paethPredictor(0, prevline[i], 0) is always prevline[i]*/
5176 for(i = 0; i != bytewidth; ++i) out[i] = (scanline[i] - prevline[i]);
5177 for(i = bytewidth; i < length; ++i)
5178 {
5179 out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth]));
5180 }
5181 }
5182 else
5183 {
5184 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
5185 /*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/
5186 for(i = bytewidth; i < length; ++i) out[i] = (scanline[i] - scanline[i - bytewidth]);
5187 }
5188 break;
5189 default: return; /*unexisting filter type given*/
5190 }
5191 }
5192
5193 /* log2 approximation. A slight bit faster than std::log. */
flog2(float f)5194 static float flog2(float f)
5195 {
5196 float result = 0;
5197 while(f > 32) { result += 4; f /= 16; }
5198 while(f > 2) { ++result; f /= 2; }
5199 return result + 1.442695f * (f * f * f / 3 - 3 * f * f / 2 + 3 * f - 1.83333f);
5200 }
5201
filter(unsigned char * out,const unsigned char * in,unsigned w,unsigned h,const LodePNGColorMode * info,const LodePNGEncoderSettings * settings)5202 static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h,
5203 const LodePNGColorMode* info, const LodePNGEncoderSettings* settings)
5204 {
5205 /*
5206 For PNG filter method 0
5207 out must be a buffer with as size: h + (w * h * bpp + 7) / 8, because there are
5208 the scanlines with 1 extra byte per scanline
5209 */
5210
5211 unsigned bpp = lodepng_get_bpp(info);
5212 /*the width of a scanline in bytes, not including the filter type*/
5213 size_t linebytes = (w * bpp + 7) / 8;
5214 /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/
5215 size_t bytewidth = (bpp + 7) / 8;
5216 const unsigned char* prevline = 0;
5217 unsigned x, y;
5218 unsigned error = 0;
5219 LodePNGFilterStrategy strategy = settings->filter_strategy;
5220
5221 /*
5222 There is a heuristic called the minimum sum of absolute differences heuristic, suggested by the PNG standard:
5223 * If the image type is Palette, or the bit depth is smaller than 8, then do not filter the image (i.e.
5224 use fixed filtering, with the filter None).
5225 * (The other case) If the image type is Grayscale or RGB (with or without Alpha), and the bit depth is
5226 not smaller than 8, then use adaptive filtering heuristic as follows: independently for each row, apply
5227 all five filters and select the filter that produces the smallest sum of absolute values per row.
5228 This heuristic is used if filter strategy is LFS_MINSUM and filter_palette_zero is true.
5229
5230 If filter_palette_zero is true and filter_strategy is not LFS_MINSUM, the above heuristic is followed,
5231 but for "the other case", whatever strategy filter_strategy is set to instead of the minimum sum
5232 heuristic is used.
5233 */
5234 if(settings->filter_palette_zero &&
5235 (info->colortype == LCT_PALETTE || info->bitdepth < 8)) strategy = LFS_ZERO;
5236
5237 if(bpp == 0) return 31; /*error: invalid color type*/
5238
5239 if(strategy == LFS_ZERO)
5240 {
5241 for(y = 0; y != h; ++y)
5242 {
5243 size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
5244 size_t inindex = linebytes * y;
5245 out[outindex] = 0; /*filter type byte*/
5246 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, 0);
5247 prevline = &in[inindex];
5248 }
5249 }
5250 else if(strategy == LFS_MINSUM)
5251 {
5252 /*adaptive filtering*/
5253 size_t sum[5];
5254 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/
5255 size_t smallest = 0;
5256 unsigned char type, bestType = 0;
5257
5258 for(type = 0; type != 5; ++type)
5259 {
5260 attempt[type] = (unsigned char*)lodepng_malloc(linebytes);
5261 if(!attempt[type]) return 83; /*alloc fail*/
5262 }
5263
5264 if(!error)
5265 {
5266 for(y = 0; y != h; ++y)
5267 {
5268 /*try the 5 filter types*/
5269 for(type = 0; type != 5; ++type)
5270 {
5271 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type);
5272
5273 /*calculate the sum of the result*/
5274 sum[type] = 0;
5275 if(type == 0)
5276 {
5277 for(x = 0; x != linebytes; ++x) sum[type] += (unsigned char)(attempt[type][x]);
5278 }
5279 else
5280 {
5281 for(x = 0; x != linebytes; ++x)
5282 {
5283 /*For differences, each byte should be treated as signed, values above 127 are negative
5284 (converted to signed char). Filtertype 0 isn't a difference though, so use unsigned there.
5285 This means filtertype 0 is almost never chosen, but that is justified.*/
5286 unsigned char s = attempt[type][x];
5287 sum[type] += s < 128 ? s : (255U - s);
5288 }
5289 }
5290
5291 /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/
5292 if(type == 0 || sum[type] < smallest)
5293 {
5294 bestType = type;
5295 smallest = sum[type];
5296 }
5297 }
5298
5299 prevline = &in[y * linebytes];
5300
5301 /*now fill the out values*/
5302 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
5303 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x];
5304 }
5305 }
5306
5307 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]);
5308 }
5309 else if(strategy == LFS_ENTROPY)
5310 {
5311 float sum[5];
5312 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/
5313 float smallest = 0;
5314 unsigned type, bestType = 0;
5315 unsigned count[256];
5316
5317 for(type = 0; type != 5; ++type)
5318 {
5319 attempt[type] = (unsigned char*)lodepng_malloc(linebytes);
5320 if(!attempt[type]) return 83; /*alloc fail*/
5321 }
5322
5323 for(y = 0; y != h; ++y)
5324 {
5325 /*try the 5 filter types*/
5326 for(type = 0; type != 5; ++type)
5327 {
5328 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type);
5329 for(x = 0; x != 256; ++x) count[x] = 0;
5330 for(x = 0; x != linebytes; ++x) ++count[attempt[type][x]];
5331 ++count[type]; /*the filter type itself is part of the scanline*/
5332 sum[type] = 0;
5333 for(x = 0; x != 256; ++x)
5334 {
5335 float p = count[x] / (float)(linebytes + 1);
5336 sum[type] += count[x] == 0 ? 0 : flog2(1 / p) * p;
5337 }
5338 /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/
5339 if(type == 0 || sum[type] < smallest)
5340 {
5341 bestType = type;
5342 smallest = sum[type];
5343 }
5344 }
5345
5346 prevline = &in[y * linebytes];
5347
5348 /*now fill the out values*/
5349 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
5350 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x];
5351 }
5352
5353 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]);
5354 }
5355 else if(strategy == LFS_PREDEFINED)
5356 {
5357 for(y = 0; y != h; ++y)
5358 {
5359 size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
5360 size_t inindex = linebytes * y;
5361 unsigned char type = settings->predefined_filters[y];
5362 out[outindex] = type; /*filter type byte*/
5363 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, type);
5364 prevline = &in[inindex];
5365 }
5366 }
5367 else if(strategy == LFS_BRUTE_FORCE)
5368 {
5369 /*brute force filter chooser.
5370 deflate the scanline after every filter attempt to see which one deflates best.
5371 This is very slow and gives only slightly smaller, sometimes even larger, result*/
5372 size_t size[5];
5373 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/
5374 size_t smallest = 0;
5375 unsigned type = 0, bestType = 0;
5376 unsigned char* dummy;
5377 LodePNGCompressSettings zlibsettings = settings->zlibsettings;
5378 /*use fixed tree on the attempts so that the tree is not adapted to the filtertype on purpose,
5379 to simulate the true case where the tree is the same for the whole image. Sometimes it gives
5380 better result with dynamic tree anyway. Using the fixed tree sometimes gives worse, but in rare
5381 cases better compression. It does make this a bit less slow, so it's worth doing this.*/
5382 zlibsettings.btype = 1;
5383 /*a custom encoder likely doesn't read the btype setting and is optimized for complete PNG
5384 images only, so disable it*/
5385 zlibsettings.custom_zlib = 0;
5386 zlibsettings.custom_deflate = 0;
5387 for(type = 0; type != 5; ++type)
5388 {
5389 attempt[type] = (unsigned char*)lodepng_malloc(linebytes);
5390 if(!attempt[type]) return 83; /*alloc fail*/
5391 }
5392 for(y = 0; y != h; ++y) /*try the 5 filter types*/
5393 {
5394 for(type = 0; type != 5; ++type)
5395 {
5396 unsigned testsize = linebytes;
5397 /*if(testsize > 8) testsize /= 8;*/ /*it already works good enough by testing a part of the row*/
5398
5399 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type);
5400 size[type] = 0;
5401 dummy = 0;
5402 zlib_compress(&dummy, &size[type], attempt[type], testsize, &zlibsettings);
5403 lodepng_free(dummy);
5404 /*check if this is smallest size (or if type == 0 it's the first case so always store the values)*/
5405 if(type == 0 || size[type] < smallest)
5406 {
5407 bestType = type;
5408 smallest = size[type];
5409 }
5410 }
5411 prevline = &in[y * linebytes];
5412 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
5413 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x];
5414 }
5415 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]);
5416 }
5417 else return 88; /* unknown filter strategy */
5418
5419 return error;
5420 }
5421
addPaddingBits(unsigned char * out,const unsigned char * in,size_t olinebits,size_t ilinebits,unsigned h)5422 static void addPaddingBits(unsigned char* out, const unsigned char* in,
5423 size_t olinebits, size_t ilinebits, unsigned h)
5424 {
5425 /*The opposite of the removePaddingBits function
5426 olinebits must be >= ilinebits*/
5427 unsigned y;
5428 size_t diff = olinebits - ilinebits;
5429 size_t obp = 0, ibp = 0; /*bit pointers*/
5430 for(y = 0; y != h; ++y)
5431 {
5432 size_t x;
5433 for(x = 0; x < ilinebits; ++x)
5434 {
5435 unsigned char bit = readBitFromReversedStream(&ibp, in);
5436 setBitOfReversedStream(&obp, out, bit);
5437 }
5438 /*obp += diff; --> no, fill in some value in the padding bits too, to avoid
5439 "Use of uninitialised value of size ###" warning from valgrind*/
5440 for(x = 0; x != diff; ++x) setBitOfReversedStream(&obp, out, 0);
5441 }
5442 }
5443
5444 /*
5445 in: non-interlaced image with size w*h
5446 out: the same pixels, but re-ordered according to PNG's Adam7 interlacing, with
5447 no padding bits between scanlines, but between reduced images so that each
5448 reduced image starts at a byte.
5449 bpp: bits per pixel
5450 there are no padding bits, not between scanlines, not between reduced images
5451 in has the following size in bits: w * h * bpp.
5452 out is possibly bigger due to padding bits between reduced images
5453 NOTE: comments about padding bits are only relevant if bpp < 8
5454 */
Adam7_interlace(unsigned char * out,const unsigned char * in,unsigned w,unsigned h,unsigned bpp)5455 static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
5456 {
5457 unsigned passw[7], passh[7];
5458 size_t filter_passstart[8], padded_passstart[8], passstart[8];
5459 unsigned i;
5460
5461 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
5462
5463 if(bpp >= 8)
5464 {
5465 for(i = 0; i != 7; ++i)
5466 {
5467 unsigned x, y, b;
5468 size_t bytewidth = bpp / 8;
5469 for(y = 0; y < passh[i]; ++y)
5470 for(x = 0; x < passw[i]; ++x)
5471 {
5472 size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
5473 size_t pixeloutstart = passstart[i] + (y * passw[i] + x) * bytewidth;
5474 for(b = 0; b < bytewidth; ++b)
5475 {
5476 out[pixeloutstart + b] = in[pixelinstart + b];
5477 }
5478 }
5479 }
5480 }
5481 else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/
5482 {
5483 for(i = 0; i != 7; ++i)
5484 {
5485 unsigned x, y, b;
5486 unsigned ilinebits = bpp * passw[i];
5487 unsigned olinebits = bpp * w;
5488 size_t obp, ibp; /*bit pointers (for out and in buffer)*/
5489 for(y = 0; y < passh[i]; ++y)
5490 for(x = 0; x < passw[i]; ++x)
5491 {
5492 ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
5493 obp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
5494 for(b = 0; b < bpp; ++b)
5495 {
5496 unsigned char bit = readBitFromReversedStream(&ibp, in);
5497 setBitOfReversedStream(&obp, out, bit);
5498 }
5499 }
5500 }
5501 }
5502 }
5503
5504 /*out must be buffer big enough to contain uncompressed IDAT chunk data, and in must contain the full image.
5505 return value is error**/
preProcessScanlines(unsigned char ** out,size_t * outsize,const unsigned char * in,unsigned w,unsigned h,const LodePNGInfo * info_png,const LodePNGEncoderSettings * settings)5506 static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in,
5507 unsigned w, unsigned h,
5508 const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings)
5509 {
5510 /*
5511 This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps:
5512 *) if no Adam7: 1) add padding bits (= posible extra bits per scanline if bpp < 8) 2) filter
5513 *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter
5514 */
5515 unsigned bpp = lodepng_get_bpp(&info_png->color);
5516 unsigned error = 0;
5517
5518 if(info_png->interlace_method == 0)
5519 {
5520 *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/
5521 *out = (unsigned char*)lodepng_malloc(*outsize);
5522 if(!(*out) && (*outsize)) error = 83; /*alloc fail*/
5523
5524 if(!error)
5525 {
5526 /*non multiple of 8 bits per scanline, padding bits needed per scanline*/
5527 if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
5528 {
5529 unsigned char* padded = (unsigned char*)lodepng_malloc(h * ((w * bpp + 7) / 8));
5530 if(!padded) error = 83; /*alloc fail*/
5531 if(!error)
5532 {
5533 addPaddingBits(padded, in, ((w * bpp + 7) / 8) * 8, w * bpp, h);
5534 error = filter(*out, padded, w, h, &info_png->color, settings);
5535 }
5536 lodepng_free(padded);
5537 }
5538 else
5539 {
5540 /*we can immediately filter into the out buffer, no other steps needed*/
5541 error = filter(*out, in, w, h, &info_png->color, settings);
5542 }
5543 }
5544 }
5545 else /*interlace_method is 1 (Adam7)*/
5546 {
5547 unsigned passw[7], passh[7];
5548 size_t filter_passstart[8], padded_passstart[8], passstart[8];
5549 unsigned char* adam7;
5550
5551 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
5552
5553 *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/
5554 *out = (unsigned char*)lodepng_malloc(*outsize);
5555 if(!(*out)) error = 83; /*alloc fail*/
5556
5557 adam7 = (unsigned char*)lodepng_malloc(passstart[7]);
5558 if(!adam7 && passstart[7]) error = 83; /*alloc fail*/
5559
5560 if(!error)
5561 {
5562 unsigned i;
5563
5564 Adam7_interlace(adam7, in, w, h, bpp);
5565 for(i = 0; i != 7; ++i)
5566 {
5567 if(bpp < 8)
5568 {
5569 unsigned char* padded = (unsigned char*)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[i]);
5570 if(!padded) ERROR_BREAK(83); /*alloc fail*/
5571 addPaddingBits(padded, &adam7[passstart[i]],
5572 ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]);
5573 error = filter(&(*out)[filter_passstart[i]], padded,
5574 passw[i], passh[i], &info_png->color, settings);
5575 lodepng_free(padded);
5576 }
5577 else
5578 {
5579 error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]],
5580 passw[i], passh[i], &info_png->color, settings);
5581 }
5582
5583 if(error) break;
5584 }
5585 }
5586
5587 lodepng_free(adam7);
5588 }
5589
5590 return error;
5591 }
5592
5593 /*
5594 palette must have 4 * palettesize bytes allocated, and given in format RGBARGBARGBARGBA...
5595 returns 0 if the palette is opaque,
5596 returns 1 if the palette has a single color with alpha 0 ==> color key
5597 returns 2 if the palette is semi-translucent.
5598 */
getPaletteTranslucency(const unsigned char * palette,size_t palettesize)5599 static unsigned getPaletteTranslucency(const unsigned char* palette, size_t palettesize)
5600 {
5601 size_t i;
5602 unsigned key = 0;
5603 unsigned r = 0, g = 0, b = 0; /*the value of the color with alpha 0, so long as color keying is possible*/
5604 for(i = 0; i != palettesize; ++i)
5605 {
5606 if(!key && palette[4 * i + 3] == 0)
5607 {
5608 r = palette[4 * i + 0]; g = palette[4 * i + 1]; b = palette[4 * i + 2];
5609 key = 1;
5610 i = (size_t)(-1); /*restart from beginning, to detect earlier opaque colors with key's value*/
5611 }
5612 else if(palette[4 * i + 3] != 255) return 2;
5613 /*when key, no opaque RGB may have key's RGB*/
5614 else if(key && r == palette[i * 4 + 0] && g == palette[i * 4 + 1] && b == palette[i * 4 + 2]) return 2;
5615 }
5616 return key;
5617 }
5618
5619 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
addUnknownChunks(ucvector * out,unsigned char * data,size_t datasize)5620 static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize)
5621 {
5622 unsigned char* inchunk = data;
5623 while((size_t)(inchunk - data) < datasize)
5624 {
5625 CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk));
5626 out->allocsize = out->size; /*fix the allocsize again*/
5627 inchunk = lodepng_chunk_next(inchunk);
5628 }
5629 return 0;
5630 }
5631 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
5632
lodepng_encode(unsigned char ** out,size_t * outsize,const unsigned char * image,unsigned w,unsigned h,LodePNGState * state)5633 unsigned lodepng_encode(unsigned char** out, size_t* outsize,
5634 const unsigned char* image, unsigned w, unsigned h,
5635 LodePNGState* state)
5636 {
5637 LodePNGInfo info;
5638 ucvector outv;
5639 unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/
5640 size_t datasize = 0;
5641
5642 /*provide some proper output values if error will happen*/
5643 *out = 0;
5644 *outsize = 0;
5645 state->error = 0;
5646
5647 lodepng_info_init(&info);
5648 lodepng_info_copy(&info, &state->info_png);
5649
5650 if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette)
5651 && (info.color.palettesize == 0 || info.color.palettesize > 256))
5652 {
5653 state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/
5654 return state->error;
5655 }
5656
5657 if(state->encoder.auto_convert)
5658 {
5659 state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw);
5660 }
5661 if(state->error) return state->error;
5662
5663 if(state->encoder.zlibsettings.btype > 2)
5664 {
5665 CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/
5666 }
5667 if(state->info_png.interlace_method > 1)
5668 {
5669 CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/
5670 }
5671
5672 state->error = checkColorValidity(info.color.colortype, info.color.bitdepth);
5673 if(state->error) return state->error; /*error: unexisting color type given*/
5674 state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth);
5675 if(state->error) return state->error; /*error: unexisting color type given*/
5676
5677 if(!lodepng_color_mode_equal(&state->info_raw, &info.color))
5678 {
5679 unsigned char* converted;
5680 size_t size = (w * h * lodepng_get_bpp(&info.color) + 7) / 8;
5681
5682 converted = (unsigned char*)lodepng_malloc(size);
5683 if(!converted && size) state->error = 83; /*alloc fail*/
5684 if(!state->error)
5685 {
5686 state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h);
5687 }
5688 if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder);
5689 lodepng_free(converted);
5690 }
5691 else preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder);
5692
5693 ucvector_init(&outv);
5694 while(!state->error) /*while only executed once, to break on error*/
5695 {
5696 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5697 size_t i;
5698 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
5699 /*write signature and chunks*/
5700 writeSignature(&outv);
5701 /*IHDR*/
5702 addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlace_method);
5703 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5704 /*unknown chunks between IHDR and PLTE*/
5705 if(info.unknown_chunks_data[0])
5706 {
5707 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]);
5708 if(state->error) break;
5709 }
5710 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
5711 /*PLTE*/
5712 if(info.color.colortype == LCT_PALETTE)
5713 {
5714 addChunk_PLTE(&outv, &info.color);
5715 }
5716 if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA))
5717 {
5718 addChunk_PLTE(&outv, &info.color);
5719 }
5720 /*tRNS*/
5721 if(info.color.colortype == LCT_PALETTE && getPaletteTranslucency(info.color.palette, info.color.palettesize) != 0)
5722 {
5723 addChunk_tRNS(&outv, &info.color);
5724 }
5725 if((info.color.colortype == LCT_GREY || info.color.colortype == LCT_RGB) && info.color.key_defined)
5726 {
5727 addChunk_tRNS(&outv, &info.color);
5728 }
5729 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5730 /*bKGD (must come between PLTE and the IDAt chunks*/
5731 if(info.background_defined) addChunk_bKGD(&outv, &info);
5732 /*pHYs (must come before the IDAT chunks)*/
5733 if(info.phys_defined) addChunk_pHYs(&outv, &info);
5734
5735 /*unknown chunks between PLTE and IDAT*/
5736 if(info.unknown_chunks_data[1])
5737 {
5738 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]);
5739 if(state->error) break;
5740 }
5741 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
5742 /*IDAT (multiple IDAT chunks must be consecutive)*/
5743 state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings);
5744 if(state->error) break;
5745 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5746 /*tIME*/
5747 if(info.time_defined) addChunk_tIME(&outv, &info.time);
5748 /*tEXt and/or zTXt*/
5749 for(i = 0; i != info.text_num; ++i)
5750 {
5751 if(strlen(info.text_keys[i]) > 79)
5752 {
5753 state->error = 66; /*text chunk too large*/
5754 break;
5755 }
5756 if(strlen(info.text_keys[i]) < 1)
5757 {
5758 state->error = 67; /*text chunk too small*/
5759 break;
5760 }
5761 if(state->encoder.text_compression)
5762 {
5763 addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibsettings);
5764 }
5765 else
5766 {
5767 addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]);
5768 }
5769 }
5770 /*LodePNG version id in text chunk*/
5771 if(state->encoder.add_id)
5772 {
5773 unsigned alread_added_id_text = 0;
5774 for(i = 0; i != info.text_num; ++i)
5775 {
5776 if(!strcmp(info.text_keys[i], "LodePNG"))
5777 {
5778 alread_added_id_text = 1;
5779 break;
5780 }
5781 }
5782 if(alread_added_id_text == 0)
5783 {
5784 addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/
5785 }
5786 }
5787 /*iTXt*/
5788 for(i = 0; i != info.itext_num; ++i)
5789 {
5790 if(strlen(info.itext_keys[i]) > 79)
5791 {
5792 state->error = 66; /*text chunk too large*/
5793 break;
5794 }
5795 if(strlen(info.itext_keys[i]) < 1)
5796 {
5797 state->error = 67; /*text chunk too small*/
5798 break;
5799 }
5800 addChunk_iTXt(&outv, state->encoder.text_compression,
5801 info.itext_keys[i], info.itext_langtags[i], info.itext_transkeys[i], info.itext_strings[i],
5802 &state->encoder.zlibsettings);
5803 }
5804
5805 /*unknown chunks between IDAT and IEND*/
5806 if(info.unknown_chunks_data[2])
5807 {
5808 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]);
5809 if(state->error) break;
5810 }
5811 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
5812 addChunk_IEND(&outv);
5813
5814 break; /*this isn't really a while loop; no error happened so break out now!*/
5815 }
5816
5817 lodepng_info_cleanup(&info);
5818 lodepng_free(data);
5819 /*instead of cleaning the vector up, give it to the output*/
5820 *out = outv.data;
5821 *outsize = outv.size;
5822
5823 return state->error;
5824 }
5825
lodepng_encode_memory(unsigned char ** out,size_t * outsize,const unsigned char * image,unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth)5826 unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, const unsigned char* image,
5827 unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth)
5828 {
5829 unsigned error;
5830 LodePNGState state;
5831 lodepng_state_init(&state);
5832 state.info_raw.colortype = colortype;
5833 state.info_raw.bitdepth = bitdepth;
5834 state.info_png.color.colortype = colortype;
5835 state.info_png.color.bitdepth = bitdepth;
5836 lodepng_encode(out, outsize, image, w, h, &state);
5837 error = state.error;
5838 lodepng_state_cleanup(&state);
5839 return error;
5840 }
5841
lodepng_encode32(unsigned char ** out,size_t * outsize,const unsigned char * image,unsigned w,unsigned h)5842 unsigned lodepng_encode32(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h)
5843 {
5844 return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGBA, 8);
5845 }
5846
lodepng_encode24(unsigned char ** out,size_t * outsize,const unsigned char * image,unsigned w,unsigned h)5847 unsigned lodepng_encode24(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h)
5848 {
5849 return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGB, 8);
5850 }
5851
5852 #ifdef LODEPNG_COMPILE_DISK
lodepng_encode_file(const char * filename,const unsigned char * image,unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth)5853 unsigned lodepng_encode_file(const char* filename, const unsigned char* image, unsigned w, unsigned h,
5854 LodePNGColorType colortype, unsigned bitdepth)
5855 {
5856 unsigned char* buffer;
5857 size_t buffersize;
5858 unsigned error = lodepng_encode_memory(&buffer, &buffersize, image, w, h, colortype, bitdepth);
5859 if(!error) error = lodepng_save_file(buffer, buffersize, filename);
5860 lodepng_free(buffer);
5861 return error;
5862 }
5863
lodepng_encode32_file(const char * filename,const unsigned char * image,unsigned w,unsigned h)5864 unsigned lodepng_encode32_file(const char* filename, const unsigned char* image, unsigned w, unsigned h)
5865 {
5866 return lodepng_encode_file(filename, image, w, h, LCT_RGBA, 8);
5867 }
5868
lodepng_encode24_file(const char * filename,const unsigned char * image,unsigned w,unsigned h)5869 unsigned lodepng_encode24_file(const char* filename, const unsigned char* image, unsigned w, unsigned h)
5870 {
5871 return lodepng_encode_file(filename, image, w, h, LCT_RGB, 8);
5872 }
5873 #endif /*LODEPNG_COMPILE_DISK*/
5874
lodepng_encoder_settings_init(LodePNGEncoderSettings * settings)5875 void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings)
5876 {
5877 lodepng_compress_settings_init(&settings->zlibsettings);
5878 settings->filter_palette_zero = 1;
5879 settings->filter_strategy = LFS_MINSUM;
5880 settings->auto_convert = 1;
5881 settings->force_palette = 0;
5882 settings->predefined_filters = 0;
5883 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5884 settings->add_id = 0;
5885 settings->text_compression = 1;
5886 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
5887 }
5888
5889 #endif /*LODEPNG_COMPILE_ENCODER*/
5890 #endif /*LODEPNG_COMPILE_PNG*/
5891
5892 #ifdef LODEPNG_COMPILE_ERROR_TEXT
5893 /*
5894 This returns the description of a numerical error code in English. This is also
5895 the documentation of all the error codes.
5896 */
lodepng_error_text(unsigned code)5897 const char* lodepng_error_text(unsigned code)
5898 {
5899 switch(code)
5900 {
5901 case 0: return "no error, everything went ok";
5902 case 1: return "nothing done yet"; /*the Encoder/Decoder has done nothing yet, error checking makes no sense yet*/
5903 case 10: return "end of input memory reached without huffman end code"; /*while huffman decoding*/
5904 case 11: return "error in code tree made it jump outside of huffman tree"; /*while huffman decoding*/
5905 case 13: return "problem while processing dynamic deflate block";
5906 case 14: return "problem while processing dynamic deflate block";
5907 case 15: return "problem while processing dynamic deflate block";
5908 case 16: return "unexisting code while processing dynamic deflate block";
5909 case 17: return "end of out buffer memory reached while inflating";
5910 case 18: return "invalid distance code while inflating";
5911 case 19: return "end of out buffer memory reached while inflating";
5912 case 20: return "invalid deflate block BTYPE encountered while decoding";
5913 case 21: return "NLEN is not ones complement of LEN in a deflate block";
5914 /*end of out buffer memory reached while inflating:
5915 This can happen if the inflated deflate data is longer than the amount of bytes required to fill up
5916 all the pixels of the image, given the color depth and image dimensions. Something that doesn't
5917 happen in a normal, well encoded, PNG image.*/
5918 case 22: return "end of out buffer memory reached while inflating";
5919 case 23: return "end of in buffer memory reached while inflating";
5920 case 24: return "invalid FCHECK in zlib header";
5921 case 25: return "invalid compression method in zlib header";
5922 case 26: return "FDICT encountered in zlib header while it's not used for PNG";
5923 case 27: return "PNG file is smaller than a PNG header";
5924 /*Checks the magic file header, the first 8 bytes of the PNG file*/
5925 case 28: return "incorrect PNG signature, it's no PNG or corrupted";
5926 case 29: return "first chunk is not the header chunk";
5927 case 30: return "chunk length too large, chunk broken off at end of file";
5928 case 31: return "illegal PNG color type or bpp";
5929 case 32: return "illegal PNG compression method";
5930 case 33: return "illegal PNG filter method";
5931 case 34: return "illegal PNG interlace method";
5932 case 35: return "chunk length of a chunk is too large or the chunk too small";
5933 case 36: return "illegal PNG filter type encountered";
5934 case 37: return "illegal bit depth for this color type given";
5935 case 38: return "the palette is too big"; /*more than 256 colors*/
5936 case 39: return "more palette alpha values given in tRNS chunk than there are colors in the palette";
5937 case 40: return "tRNS chunk has wrong size for greyscale image";
5938 case 41: return "tRNS chunk has wrong size for RGB image";
5939 case 42: return "tRNS chunk appeared while it was not allowed for this color type";
5940 case 43: return "bKGD chunk has wrong size for palette image";
5941 case 44: return "bKGD chunk has wrong size for greyscale image";
5942 case 45: return "bKGD chunk has wrong size for RGB image";
5943 case 48: return "empty input buffer given to decoder. Maybe caused by non-existing file?";
5944 case 49: return "jumped past memory while generating dynamic huffman tree";
5945 case 50: return "jumped past memory while generating dynamic huffman tree";
5946 case 51: return "jumped past memory while inflating huffman block";
5947 case 52: return "jumped past memory while inflating";
5948 case 53: return "size of zlib data too small";
5949 case 54: return "repeat symbol in tree while there was no value symbol yet";
5950 /*jumped past tree while generating huffman tree, this could be when the
5951 tree will have more leaves than symbols after generating it out of the
5952 given lenghts. They call this an oversubscribed dynamic bit lengths tree in zlib.*/
5953 case 55: return "jumped past tree while generating huffman tree";
5954 case 56: return "given output image colortype or bitdepth not supported for color conversion";
5955 case 57: return "invalid CRC encountered (checking CRC can be disabled)";
5956 case 58: return "invalid ADLER32 encountered (checking ADLER32 can be disabled)";
5957 case 59: return "requested color conversion not supported";
5958 case 60: return "invalid window size given in the settings of the encoder (must be 0-32768)";
5959 case 61: return "invalid BTYPE given in the settings of the encoder (only 0, 1 and 2 are allowed)";
5960 /*LodePNG leaves the choice of RGB to greyscale conversion formula to the user.*/
5961 case 62: return "conversion from color to greyscale not supported";
5962 case 63: return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk"; /*(2^31-1)*/
5963 /*this would result in the inability of a deflated block to ever contain an end code. It must be at least 1.*/
5964 case 64: return "the length of the END symbol 256 in the Huffman tree is 0";
5965 case 66: return "the length of a text chunk keyword given to the encoder is longer than the maximum of 79 bytes";
5966 case 67: return "the length of a text chunk keyword given to the encoder is smaller than the minimum of 1 byte";
5967 case 68: return "tried to encode a PLTE chunk with a palette that has less than 1 or more than 256 colors";
5968 case 69: return "unknown chunk type with 'critical' flag encountered by the decoder";
5969 case 71: return "unexisting interlace mode given to encoder (must be 0 or 1)";
5970 case 72: return "while decoding, unexisting compression method encountering in zTXt or iTXt chunk (it must be 0)";
5971 case 73: return "invalid tIME chunk size";
5972 case 74: return "invalid pHYs chunk size";
5973 /*length could be wrong, or data chopped off*/
5974 case 75: return "no null termination char found while decoding text chunk";
5975 case 76: return "iTXt chunk too short to contain required bytes";
5976 case 77: return "integer overflow in buffer size";
5977 case 78: return "failed to open file for reading"; /*file doesn't exist or couldn't be opened for reading*/
5978 case 79: return "failed to open file for writing";
5979 case 80: return "tried creating a tree of 0 symbols";
5980 case 81: return "lazy matching at pos 0 is impossible";
5981 case 82: return "color conversion to palette requested while a color isn't in palette";
5982 case 83: return "memory allocation failed";
5983 case 84: return "given image too small to contain all pixels to be encoded";
5984 case 86: return "impossible offset in lz77 encoding (internal bug)";
5985 case 87: return "must provide custom zlib function pointer if LODEPNG_COMPILE_ZLIB is not defined";
5986 case 88: return "invalid filter strategy given for LodePNGEncoderSettings.filter_strategy";
5987 case 89: return "text chunk keyword too short or long: must have size 1-79";
5988 /*the windowsize in the LodePNGCompressSettings. Requiring POT(==> & instead of %) makes encoding 12% faster.*/
5989 case 90: return "windowsize must be a power of two";
5990 case 91: return "invalid decompressed idat size";
5991 case 92: return "too many pixels, not supported";
5992 case 93: return "zero width or height is invalid";
5993 case 94: return "header chunk must have a size of 13 bytes";
5994 }
5995 return "unknown error code";
5996 }
5997 #endif /*LODEPNG_COMPILE_ERROR_TEXT*/
5998
5999 /* ////////////////////////////////////////////////////////////////////////// */
6000 /* ////////////////////////////////////////////////////////////////////////// */
6001 /* // C++ Wrapper // */
6002 /* ////////////////////////////////////////////////////////////////////////// */
6003 /* ////////////////////////////////////////////////////////////////////////// */
6004
6005 #ifdef LODEPNG_COMPILE_CPP
6006 namespace lodepng
6007 {
6008
6009 #ifdef LODEPNG_COMPILE_DISK
load_file(std::vector<unsigned char> & buffer,const std::string & filename)6010 unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filename)
6011 {
6012 long size = lodepng_filesize(filename.c_str());
6013 if(size < 0) return 78;
6014 buffer.resize((size_t)size);
6015 return size == 0 ? 0 : lodepng_buffer_file(&buffer[0], (size_t)size, filename.c_str());
6016 }
6017
6018 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/
save_file(const std::vector<unsigned char> & buffer,const std::string & filename)6019 unsigned save_file(const std::vector<unsigned char>& buffer, const std::string& filename)
6020 {
6021 return lodepng_save_file(buffer.empty() ? 0 : &buffer[0], buffer.size(), filename.c_str());
6022 }
6023 #endif /* LODEPNG_COMPILE_DISK */
6024
6025 #ifdef LODEPNG_COMPILE_ZLIB
6026 #ifdef LODEPNG_COMPILE_DECODER
decompress(std::vector<unsigned char> & out,const unsigned char * in,size_t insize,const LodePNGDecompressSettings & settings)6027 unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize,
6028 const LodePNGDecompressSettings& settings)
6029 {
6030 unsigned char* buffer = 0;
6031 size_t buffersize = 0;
6032 unsigned error = zlib_decompress(&buffer, &buffersize, in, insize, &settings);
6033 if(buffer)
6034 {
6035 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
6036 lodepng_free(buffer);
6037 }
6038 return error;
6039 }
6040
decompress(std::vector<unsigned char> & out,const std::vector<unsigned char> & in,const LodePNGDecompressSettings & settings)6041 unsigned decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in,
6042 const LodePNGDecompressSettings& settings)
6043 {
6044 return decompress(out, in.empty() ? 0 : &in[0], in.size(), settings);
6045 }
6046 #endif /* LODEPNG_COMPILE_DECODER */
6047
6048 #ifdef LODEPNG_COMPILE_ENCODER
compress(std::vector<unsigned char> & out,const unsigned char * in,size_t insize,const LodePNGCompressSettings & settings)6049 unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize,
6050 const LodePNGCompressSettings& settings)
6051 {
6052 unsigned char* buffer = 0;
6053 size_t buffersize = 0;
6054 unsigned error = zlib_compress(&buffer, &buffersize, in, insize, &settings);
6055 if(buffer)
6056 {
6057 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
6058 lodepng_free(buffer);
6059 }
6060 return error;
6061 }
6062
compress(std::vector<unsigned char> & out,const std::vector<unsigned char> & in,const LodePNGCompressSettings & settings)6063 unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in,
6064 const LodePNGCompressSettings& settings)
6065 {
6066 return compress(out, in.empty() ? 0 : &in[0], in.size(), settings);
6067 }
6068 #endif /* LODEPNG_COMPILE_ENCODER */
6069 #endif /* LODEPNG_COMPILE_ZLIB */
6070
6071
6072 #ifdef LODEPNG_COMPILE_PNG
6073
State()6074 State::State()
6075 {
6076 lodepng_state_init(this);
6077 }
6078
State(const State & other)6079 State::State(const State& other)
6080 {
6081 lodepng_state_init(this);
6082 lodepng_state_copy(this, &other);
6083 }
6084
~State()6085 State::~State()
6086 {
6087 lodepng_state_cleanup(this);
6088 }
6089
operator =(const State & other)6090 State& State::operator=(const State& other)
6091 {
6092 lodepng_state_copy(this, &other);
6093 return *this;
6094 }
6095
6096 #ifdef LODEPNG_COMPILE_DECODER
6097
decode(std::vector<unsigned char> & out,unsigned & w,unsigned & h,const unsigned char * in,size_t insize,LodePNGColorType colortype,unsigned bitdepth)6098 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const unsigned char* in,
6099 size_t insize, LodePNGColorType colortype, unsigned bitdepth)
6100 {
6101 unsigned char* buffer;
6102 unsigned error = lodepng_decode_memory(&buffer, &w, &h, in, insize, colortype, bitdepth);
6103 if(buffer && !error)
6104 {
6105 State state;
6106 state.info_raw.colortype = colortype;
6107 state.info_raw.bitdepth = bitdepth;
6108 size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw);
6109 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
6110 lodepng_free(buffer);
6111 }
6112 return error;
6113 }
6114
decode(std::vector<unsigned char> & out,unsigned & w,unsigned & h,const std::vector<unsigned char> & in,LodePNGColorType colortype,unsigned bitdepth)6115 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
6116 const std::vector<unsigned char>& in, LodePNGColorType colortype, unsigned bitdepth)
6117 {
6118 return decode(out, w, h, in.empty() ? 0 : &in[0], (unsigned)in.size(), colortype, bitdepth);
6119 }
6120
decode(std::vector<unsigned char> & out,unsigned & w,unsigned & h,State & state,const unsigned char * in,size_t insize)6121 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
6122 State& state,
6123 const unsigned char* in, size_t insize)
6124 {
6125 unsigned char* buffer = NULL;
6126 unsigned error = lodepng_decode(&buffer, &w, &h, &state, in, insize);
6127 if(buffer && !error)
6128 {
6129 size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw);
6130 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
6131 }
6132 lodepng_free(buffer);
6133 return error;
6134 }
6135
decode(std::vector<unsigned char> & out,unsigned & w,unsigned & h,State & state,const std::vector<unsigned char> & in)6136 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
6137 State& state,
6138 const std::vector<unsigned char>& in)
6139 {
6140 return decode(out, w, h, state, in.empty() ? 0 : &in[0], in.size());
6141 }
6142
6143 #ifdef LODEPNG_COMPILE_DISK
decode(std::vector<unsigned char> & out,unsigned & w,unsigned & h,const std::string & filename,LodePNGColorType colortype,unsigned bitdepth)6144 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const std::string& filename,
6145 LodePNGColorType colortype, unsigned bitdepth)
6146 {
6147 std::vector<unsigned char> buffer;
6148 unsigned error = load_file(buffer, filename);
6149 if(error) return error;
6150 return decode(out, w, h, buffer, colortype, bitdepth);
6151 }
6152 #endif /* LODEPNG_COMPILE_DECODER */
6153 #endif /* LODEPNG_COMPILE_DISK */
6154
6155 #ifdef LODEPNG_COMPILE_ENCODER
encode(std::vector<unsigned char> & out,const unsigned char * in,unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth)6156 unsigned encode(std::vector<unsigned char>& out, const unsigned char* in, unsigned w, unsigned h,
6157 LodePNGColorType colortype, unsigned bitdepth)
6158 {
6159 unsigned char* buffer;
6160 size_t buffersize;
6161 unsigned error = lodepng_encode_memory(&buffer, &buffersize, in, w, h, colortype, bitdepth);
6162 if(buffer)
6163 {
6164 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
6165 lodepng_free(buffer);
6166 }
6167 return error;
6168 }
6169
encode(std::vector<unsigned char> & out,const std::vector<unsigned char> & in,unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth)6170 unsigned encode(std::vector<unsigned char>& out,
6171 const std::vector<unsigned char>& in, unsigned w, unsigned h,
6172 LodePNGColorType colortype, unsigned bitdepth)
6173 {
6174 if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84;
6175 return encode(out, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth);
6176 }
6177
encode(std::vector<unsigned char> & out,const unsigned char * in,unsigned w,unsigned h,State & state)6178 unsigned encode(std::vector<unsigned char>& out,
6179 const unsigned char* in, unsigned w, unsigned h,
6180 State& state)
6181 {
6182 unsigned char* buffer;
6183 size_t buffersize;
6184 unsigned error = lodepng_encode(&buffer, &buffersize, in, w, h, &state);
6185 if(buffer)
6186 {
6187 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
6188 lodepng_free(buffer);
6189 }
6190 return error;
6191 }
6192
encode(std::vector<unsigned char> & out,const std::vector<unsigned char> & in,unsigned w,unsigned h,State & state)6193 unsigned encode(std::vector<unsigned char>& out,
6194 const std::vector<unsigned char>& in, unsigned w, unsigned h,
6195 State& state)
6196 {
6197 if(lodepng_get_raw_size(w, h, &state.info_raw) > in.size()) return 84;
6198 return encode(out, in.empty() ? 0 : &in[0], w, h, state);
6199 }
6200
6201 #ifdef LODEPNG_COMPILE_DISK
encode(const std::string & filename,const unsigned char * in,unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth)6202 unsigned encode(const std::string& filename,
6203 const unsigned char* in, unsigned w, unsigned h,
6204 LodePNGColorType colortype, unsigned bitdepth)
6205 {
6206 std::vector<unsigned char> buffer;
6207 unsigned error = encode(buffer, in, w, h, colortype, bitdepth);
6208 if(!error) error = save_file(buffer, filename);
6209 return error;
6210 }
6211
encode(const std::string & filename,const std::vector<unsigned char> & in,unsigned w,unsigned h,LodePNGColorType colortype,unsigned bitdepth)6212 unsigned encode(const std::string& filename,
6213 const std::vector<unsigned char>& in, unsigned w, unsigned h,
6214 LodePNGColorType colortype, unsigned bitdepth)
6215 {
6216 if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84;
6217 return encode(filename, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth);
6218 }
6219 #endif /* LODEPNG_COMPILE_DISK */
6220 #endif /* LODEPNG_COMPILE_ENCODER */
6221 #endif /* LODEPNG_COMPILE_PNG */
6222 } /* namespace lodepng */
6223 #endif /*LODEPNG_COMPILE_CPP*/
6224