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