git.haldean.org plotter / 8599c83
pntlsm v0 Haldean Brown a month ago
4 changed file(s) with 8632 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 _*
1 pntlsm
0 /*
1 LodePNG version 20191020
2
3 Copyright (c) 2005-2019 Lode Vandevenne
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24
25 /*
26 The manual and changelog are in the header file "lodepng.h"
27 Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for C.
28 */
29
30 #include "lodepng.h"
31
32 #ifdef LODEPNG_COMPILE_DISK
33 #include <limits.h> /* LONG_MAX */
34 #include <stdio.h> /* file handling */
35 #endif /* LODEPNG_COMPILE_DISK */
36
37 #ifdef LODEPNG_COMPILE_ALLOCATORS
38 #include <stdlib.h> /* allocations */
39 #endif /* LODEPNG_COMPILE_ALLOCATORS */
40
41 #if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/
42 #pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requires too much casts*/
43 #pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
44 #endif /*_MSC_VER */
45
46 const char* LODEPNG_VERSION_STRING = "20191020";
47
48 /*
49 This source file is built up in the following large parts. The code sections
50 with the "LODEPNG_COMPILE_" #defines divide this up further in an intermixed way.
51 -Tools for C and common code for PNG and Zlib
52 -C Code for Zlib (huffman, deflate, ...)
53 -C Code for PNG (file format chunks, adam7, PNG filters, color conversions, ...)
54 -The C++ wrapper around all of the above
55 */
56
57 /* ////////////////////////////////////////////////////////////////////////// */
58 /* ////////////////////////////////////////////////////////////////////////// */
59 /* // Tools for C, and common code for PNG and Zlib. // */
60 /* ////////////////////////////////////////////////////////////////////////// */
61 /* ////////////////////////////////////////////////////////////////////////// */
62
63 /*The malloc, realloc and free functions defined here with "lodepng_" in front
64 of the name, so that you can easily change them to others related to your
65 platform if needed. Everything else in the code calls these. Pass
66 -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler, or comment out
67 #define LODEPNG_COMPILE_ALLOCATORS in the header, to disable the ones here and
68 define them in your own project's source files without needing to change
69 lodepng source code. Don't forget to remove "static" if you copypaste them
70 from here.*/
71
72 #ifdef LODEPNG_COMPILE_ALLOCATORS
73 static void* lodepng_malloc(size_t size) {
74 #ifdef LODEPNG_MAX_ALLOC
75 if(size > LODEPNG_MAX_ALLOC) return 0;
76 #endif
77 return malloc(size);
78 }
79
80 static void* lodepng_realloc(void* ptr, size_t new_size) {
81 #ifdef LODEPNG_MAX_ALLOC
82 if(new_size > LODEPNG_MAX_ALLOC) return 0;
83 #endif
84 return realloc(ptr, new_size);
85 }
86
87 static void lodepng_free(void* ptr) {
88 free(ptr);
89 }
90 #else /*LODEPNG_COMPILE_ALLOCATORS*/
91 /* TODO: support giving additional void* payload to the custom allocators */
92 void* lodepng_malloc(size_t size);
93 void* lodepng_realloc(void* ptr, size_t new_size);
94 void lodepng_free(void* ptr);
95 #endif /*LODEPNG_COMPILE_ALLOCATORS*/
96
97 /* convince the compiler to inline a function, for use when this measurably improves performance */
98 /* inline is not available in C90, but use it when supported by the compiler */
99 #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || (defined(__cplusplus) && (__cplusplus >= 199711L))
100 #define LODEPNG_INLINE inline
101 #else
102 #define LODEPNG_INLINE /* not available */
103 #endif
104
105 /* restrict is not available in C90, but use it when supported by the compiler */
106 #if (defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1)) ||\
107 (defined(_MSC_VER) && (_MSC_VER >= 1400)) || (defined(__WATCOMC__) && (__WATCOMC__ >= 1250))
108 #define LODEPNG_RESTRICT __restrict
109 #else
110 #define LODEPNG_RESTRICT /* not available */
111 #endif
112
113 /* Replacements for C library functions memcpy and strlen, to support those platforms
114 where a full C library is not available. The compiler can recognize them and compile
115 to something as fast. */
116
117 static void lodepng_memcpy(void* LODEPNG_RESTRICT dst,
118 const void* LODEPNG_RESTRICT src, size_t size) {
119 size_t i;
120 for(i = 0; i < size; i++) ((char*)dst)[i] = ((const char*)src)[i];
121 }
122
123 /* does not check memory out of bounds, do not use on untrusted data */
124 static size_t lodepng_strlen(const char* a) {
125 const char* orig = a;
126 /* avoid warning about unused function in case of disabled COMPILE... macros */
127 (void)lodepng_strlen;
128 while(*a) a++;
129 return (size_t)(a - orig);
130 }
131
132 #define LODEPNG_MAX(a, b) (((a) > (b)) ? (a) : (b))
133 #define LODEPNG_MIN(a, b) (((a) < (b)) ? (a) : (b))
134 #define LODEPNG_ABS(x) ((x) < 0 ? -(x) : (x))
135
136
137 #ifdef LODEPNG_COMPILE_DECODER
138 /* Safely check if multiplying two integers will overflow (no undefined
139 behavior, compiler removing the code, etc...) and output result. */
140 static int lodepng_mulofl(size_t a, size_t b, size_t* result) {
141 *result = a * b; /* Unsigned multiplication is well defined and safe in C90 */
142 return (a != 0 && *result / a != b);
143 }
144
145 /* Safely check if adding two integers will overflow (no undefined
146 behavior, compiler removing the code, etc...) and output result. */
147 static int lodepng_addofl(size_t a, size_t b, size_t* result) {
148 *result = a + b; /* Unsigned addition is well defined and safe in C90 */
149 return *result < a;
150 }
151
152 #ifdef LODEPNG_COMPILE_ZLIB
153 /* Safely check if a + b > c, even if overflow could happen. */
154 static int lodepng_gtofl(size_t a, size_t b, size_t c) {
155 size_t d;
156 if(lodepng_addofl(a, b, &d)) return 1;
157 return d > c;
158 }
159 #endif /*LODEPNG_COMPILE_ZLIB*/
160 #endif /*LODEPNG_COMPILE_DECODER*/
161
162
163 /*
164 Often in case of an error a value is assigned to a variable and then it breaks
165 out of a loop (to go to the cleanup phase of a function). This macro does that.
166 It makes the error handling code shorter and more readable.
167
168 Example: if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83);
169 */
170 #define CERROR_BREAK(errorvar, code){\
171 errorvar = code;\
172 break;\
173 }
174
175 /*version of CERROR_BREAK that assumes the common case where the error variable is named "error"*/
176 #define ERROR_BREAK(code) CERROR_BREAK(error, code)
177
178 /*Set error var to the error code, and return it.*/
179 #define CERROR_RETURN_ERROR(errorvar, code){\
180 errorvar = code;\
181 return code;\
182 }
183
184 /*Try the code, if it returns error, also return the error.*/
185 #define CERROR_TRY_RETURN(call){\
186 unsigned error = call;\
187 if(error) return error;\
188 }
189
190 /*Set error var to the error code, and return from the void function.*/
191 #define CERROR_RETURN(errorvar, code){\
192 errorvar = code;\
193 return;\
194 }
195
196 /*
197 About uivector, ucvector and string:
198 -All of them wrap dynamic arrays or text strings in a similar way.
199 -LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the C++ version.
200 -The string tools are made to avoid problems with compilers that declare things like strncat as deprecated.
201 -They're not used in the interface, only internally in this file as static functions.
202 -As with many other structs in this file, the init and cleanup functions serve as ctor and dtor.
203 */
204
205 #ifdef LODEPNG_COMPILE_ZLIB
206 #ifdef LODEPNG_COMPILE_ENCODER
207 /*dynamic vector of unsigned ints*/
208 typedef struct uivector {
209 unsigned* data;
210 size_t size; /*size in number of unsigned longs*/
211 size_t allocsize; /*allocated size in bytes*/
212 } uivector;
213
214 static void uivector_cleanup(void* p) {
215 ((uivector*)p)->size = ((uivector*)p)->allocsize = 0;
216 lodepng_free(((uivector*)p)->data);
217 ((uivector*)p)->data = NULL;
218 }
219
220 /*returns 1 if success, 0 if failure ==> nothing done*/
221 static unsigned uivector_reserve(uivector* p, size_t allocsize) {
222 if(allocsize > p->allocsize) {
223 size_t newsize = (allocsize > p->allocsize * 2u) ? allocsize : ((allocsize * 3u) >> 1u);
224 void* data = lodepng_realloc(p->data, newsize);
225 if(data) {
226 p->allocsize = newsize;
227 p->data = (unsigned*)data;
228 }
229 else return 0; /*error: not enough memory*/
230 }
231 return 1;
232 }
233
234 /*returns 1 if success, 0 if failure ==> nothing done*/
235 static unsigned uivector_resize(uivector* p, size_t size) {
236 if(!uivector_reserve(p, size * sizeof(unsigned))) return 0;
237 p->size = size;
238 return 1; /*success*/
239 }
240
241 /*resize and give all new elements the value*/
242 static unsigned uivector_resizev(uivector* p, size_t size, unsigned value) {
243 size_t oldsize = p->size, i;
244 if(!uivector_resize(p, size)) return 0;
245 for(i = oldsize; i < size; ++i) p->data[i] = value;
246 return 1;
247 }
248
249 static void uivector_init(uivector* p) {
250 p->data = NULL;
251 p->size = p->allocsize = 0;
252 }
253
254 /*returns 1 if success, 0 if failure ==> nothing done*/
255 static unsigned uivector_push_back(uivector* p, unsigned c) {
256 if(!uivector_resize(p, p->size + 1)) return 0;
257 p->data[p->size - 1] = c;
258 return 1;
259 }
260 #endif /*LODEPNG_COMPILE_ENCODER*/
261 #endif /*LODEPNG_COMPILE_ZLIB*/
262
263 /* /////////////////////////////////////////////////////////////////////////// */
264
265 /*dynamic vector of unsigned chars*/
266 typedef struct ucvector {
267 unsigned char* data;
268 size_t size; /*used size*/
269 size_t allocsize; /*allocated size*/
270 } ucvector;
271
272 /*returns 1 if success, 0 if failure ==> nothing done*/
273 static unsigned ucvector_reserve(ucvector* p, size_t allocsize) {
274 if(allocsize > p->allocsize) {
275 size_t newsize = (allocsize > p->allocsize * 2u) ? allocsize : ((allocsize * 3u) >> 1u);
276 void* data = lodepng_realloc(p->data, newsize);
277 if(data) {
278 p->allocsize = newsize;
279 p->data = (unsigned char*)data;
280 }
281 else return 0; /*error: not enough memory*/
282 }
283 return 1;
284 }
285
286 /*returns 1 if success, 0 if failure ==> nothing done*/
287 static unsigned ucvector_resize(ucvector* p, size_t size) {
288 if(!ucvector_reserve(p, size * sizeof(unsigned char))) return 0;
289 p->size = size;
290 return 1; /*success*/
291 }
292
293 #ifdef LODEPNG_COMPILE_PNG
294
295 static void ucvector_cleanup(void* p) {
296 ((ucvector*)p)->size = ((ucvector*)p)->allocsize = 0;
297 lodepng_free(((ucvector*)p)->data);
298 ((ucvector*)p)->data = NULL;
299 }
300
301 static void ucvector_init(ucvector* p) {
302 p->data = NULL;
303 p->size = p->allocsize = 0;
304 }
305 #endif /*LODEPNG_COMPILE_PNG*/
306
307 #ifdef LODEPNG_COMPILE_ZLIB
308 /*you can both convert from vector to buffer&size and vice versa. If you use
309 init_buffer to take over a buffer and size, it is not needed to use cleanup*/
310 static void ucvector_init_buffer(ucvector* p, unsigned char* buffer, size_t size) {
311 p->data = buffer;
312 p->allocsize = p->size = size;
313 }
314 #endif /*LODEPNG_COMPILE_ZLIB*/
315
316 #if (defined(LODEPNG_COMPILE_PNG) && defined(LODEPNG_COMPILE_ANCILLARY_CHUNKS)) || defined(LODEPNG_COMPILE_ENCODER)
317 /*returns 1 if success, 0 if failure ==> nothing done*/
318 static unsigned ucvector_push_back(ucvector* p, unsigned char c) {
319 if(!ucvector_resize(p, p->size + 1)) return 0;
320 p->data[p->size - 1] = c;
321 return 1;
322 }
323 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/
324
325
326 /* ////////////////////////////////////////////////////////////////////////// */
327
328 #ifdef LODEPNG_COMPILE_PNG
329 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
330
331 /*free string pointer and set it to NULL*/
332 static void string_cleanup(char** out) {
333 lodepng_free(*out);
334 *out = NULL;
335 }
336
337 /* dynamically allocates a new string with a copy of the null terminated input text */
338 static char* alloc_string(const char* in) {
339 size_t insize = lodepng_strlen(in);
340 char* out = (char*)lodepng_malloc(insize + 1);
341 if(out) {
342 size_t i;
343 for(i = 0; i != insize; ++i) {
344 out[i] = in[i];
345 }
346 out[i] = 0;
347 }
348 return out;
349 }
350 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
351 #endif /*LODEPNG_COMPILE_PNG*/
352
353 /* ////////////////////////////////////////////////////////////////////////// */
354
355 #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)
356 static unsigned lodepng_read32bitInt(const unsigned char* buffer) {
357 return (((unsigned)buffer[0] << 24u) | ((unsigned)buffer[1] << 16u) |
358 ((unsigned)buffer[2] << 8u) | (unsigned)buffer[3]);
359 }
360 #endif /*defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)*/
361
362 #if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)
363 /*buffer must have at least 4 allocated bytes available*/
364 static void lodepng_set32bitInt(unsigned char* buffer, unsigned value) {
365 buffer[0] = (unsigned char)((value >> 24) & 0xff);
366 buffer[1] = (unsigned char)((value >> 16) & 0xff);
367 buffer[2] = (unsigned char)((value >> 8) & 0xff);
368 buffer[3] = (unsigned char)((value ) & 0xff);
369 }
370 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/
371
372 /* ////////////////////////////////////////////////////////////////////////// */
373 /* / File IO / */
374 /* ////////////////////////////////////////////////////////////////////////// */
375
376 #ifdef LODEPNG_COMPILE_DISK
377
378 /* returns negative value on error. This should be pure C compatible, so no fstat. */
379 static long lodepng_filesize(const char* filename) {
380 FILE* file;
381 long size;
382 file = fopen(filename, "rb");
383 if(!file) return -1;
384
385 if(fseek(file, 0, SEEK_END) != 0) {
386 fclose(file);
387 return -1;
388 }
389
390 size = ftell(file);
391 /* It may give LONG_MAX as directory size, this is invalid for us. */
392 if(size == LONG_MAX) size = -1;
393
394 fclose(file);
395 return size;
396 }
397
398 /* load file into buffer that already has the correct allocated size. Returns error code.*/
399 static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename) {
400 FILE* file;
401 size_t readsize;
402 file = fopen(filename, "rb");
403 if(!file) return 78;
404
405 readsize = fread(out, 1, size, file);
406 fclose(file);
407
408 if (readsize != size) return 78;
409 return 0;
410 }
411
412 unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) {
413 long size = lodepng_filesize(filename);
414 if (size < 0) return 78;
415 *outsize = (size_t)size;
416
417 *out = (unsigned char*)lodepng_malloc((size_t)size);
418 if(!(*out) && size > 0) return 83; /*the above malloc failed*/
419
420 return lodepng_buffer_file(*out, (size_t)size, filename);
421 }
422
423 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/
424 unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) {
425 FILE* file;
426 file = fopen(filename, "wb" );
427 if(!file) return 79;
428 fwrite(buffer, 1, buffersize, file);
429 fclose(file);
430 return 0;
431 }
432
433 #endif /*LODEPNG_COMPILE_DISK*/
434
435 /* ////////////////////////////////////////////////////////////////////////// */
436 /* ////////////////////////////////////////////////////////////////////////// */
437 /* // End of common code and tools. Begin of Zlib related code. // */
438 /* ////////////////////////////////////////////////////////////////////////// */
439 /* ////////////////////////////////////////////////////////////////////////// */
440
441 #ifdef LODEPNG_COMPILE_ZLIB
442 #ifdef LODEPNG_COMPILE_ENCODER
443
444 typedef struct {
445 ucvector* data;
446 size_t bp;
447 } LodePNGBitWriter;
448
449 void LodePNGBitWriter_init(LodePNGBitWriter* writer, ucvector* data) {
450 writer->data = data;
451 writer->bp = 0;
452 }
453
454 /*TODO: this ignores potential out of memory errors*/
455 #define WRITEBIT(/*size_t**/ writer, /*unsigned char*/ bit){\
456 /* append new byte */\
457 if(((writer->bp) & 7u) == 0) ucvector_push_back(writer->data, (unsigned char)0);\
458 (writer->data->data[writer->data->size - 1]) |= (bit << ((writer->bp) & 7u));\
459 ++writer->bp;\
460 }
461
462 /* LSB of value is written first, and LSB of bytes is used first */
463 static void writeBits(LodePNGBitWriter* writer, unsigned value, size_t nbits) {
464 if(nbits == 1) { /* compiler should statically compile this case if nbits == 1 */
465 WRITEBIT(writer, value);
466 } else {
467 /* TODO: increase output size nly once here rather than in each WRITEBIT */
468 size_t i;
469 for(i = 0; i != nbits; ++i) {
470 WRITEBIT(writer, (unsigned char)((value >> i) & 1));
471 }
472 }
473 }
474
475 /* This one is to use for adding huffman symbol, the value bits are written MSB first */
476 static void writeBitsReversed(LodePNGBitWriter* writer, unsigned value, size_t nbits) {
477 size_t i;
478 for(i = 0; i != nbits; ++i) {
479 /* TODO: increase output size only once here rather than in each WRITEBIT */
480 WRITEBIT(writer, (unsigned char)((value >> (nbits - 1u - i)) & 1u));
481 }
482 }
483 #endif /*LODEPNG_COMPILE_ENCODER*/
484
485 #ifdef LODEPNG_COMPILE_DECODER
486
487 typedef struct {
488 const unsigned char* data;
489 size_t size; /*size of data in bytes*/
490 size_t bitsize; /*size of data in bits, end of valid bp values, should be 8*size*/
491 size_t bp;
492 unsigned buffer; /*buffer for reading bits. NOTE: 'unsigned' must support at least 32 bits*/
493 } LodePNGBitReader;
494
495 /* data size argument is in bytes. Returns error if size too large causing overflow */
496 static unsigned LodePNGBitReader_init(LodePNGBitReader* reader, const unsigned char* data, size_t size) {
497 size_t temp;
498 reader->data = data;
499 reader->size = size;
500 /* size in bits, return error if overflow (if size_t is 32 bit this supports up to 500MB) */
501 if(lodepng_mulofl(size, 8u, &reader->bitsize)) return 105;
502 /*ensure incremented bp can be compared to bitsize without overflow even when it would be incremented 32 too much and
503 trying to ensure 32 more bits*/
504 if(lodepng_addofl(reader->bitsize, 64u, &temp)) return 105;
505 reader->bp = 0;
506 reader->buffer = 0;
507 return 0; /*ok*/
508 }
509
510 /*
511 ensureBits functions:
512 Ensures the reader can at least read nbits bits in one or more readBits calls,
513 safely even if not enough bits are available.
514 Returns 1 if there are enough bits available, 0 if not.
515 */
516
517 /*See ensureBits documentation above. This one ensures exactly 1 bit */
518 /*static unsigned ensureBits1(LodePNGBitReader* reader) {
519 if(reader->bp >= reader->bitsize) return 0;
520 reader->buffer = (unsigned)reader->data[reader->bp >> 3u] >> (reader->bp & 7u);
521 return 1;
522 }*/
523
524 /*See ensureBits documentation above. This one ensures up to 9 bits */
525 static unsigned ensureBits9(LodePNGBitReader* reader, size_t nbits) {
526 size_t start = reader->bp >> 3u;
527 size_t size = reader->size;
528 if(start + 1u < size) {
529 reader->buffer = (unsigned)(reader->data[start + 0]) | (unsigned)(reader->data[start + 1] << 8u);
530 reader->buffer >>= (reader->bp & 7u);
531 return 1;
532 } else {
533 reader->buffer = 0;
534 if(start + 0u < size) reader->buffer |= reader->data[start + 0];
535 reader->buffer >>= (reader->bp & 7u);
536 return reader->bp + nbits < reader->bitsize;
537 }
538 }
539
540 /*See ensureBits documentation above. This one ensures up to 17 bits */
541 static unsigned ensureBits17(LodePNGBitReader* reader, size_t nbits) {
542 size_t start = reader->bp >> 3u;
543 size_t size = reader->size;
544 if(start + 2u < size) {
545 reader->buffer = (unsigned)(reader->data[start + 0]) | (unsigned)(reader->data[start + 1] << 8u) |
546 (unsigned)(reader->data[start + 2] << 16u);
547 reader->buffer >>= (reader->bp & 7u);
548 return 1;
549 } else {
550 reader->buffer = 0;
551 if(start + 0u < size) reader->buffer |= reader->data[start + 0];
552 if(start + 1u < size) reader->buffer |= (unsigned)(reader->data[start + 1] << 8u);
553 reader->buffer >>= (reader->bp & 7u);
554 return reader->bp + nbits < reader->bitsize;
555 }
556 }
557
558 /*See ensureBits documentation above. This one ensures up to 25 bits */
559 static LODEPNG_INLINE unsigned ensureBits25(LodePNGBitReader* reader, size_t nbits) {
560 size_t start = reader->bp >> 3u;
561 size_t size = reader->size;
562 if(start + 3u < size) {
563 reader->buffer = (unsigned)(reader->data[start + 0]) | (unsigned)(reader->data[start + 1] << 8u) |
564 (unsigned)(reader->data[start + 2] << 16u) | (unsigned)(reader->data[start + 3] << 24u);
565 reader->buffer >>= (reader->bp & 7u);
566 return 1;
567 } else {
568 reader->buffer = 0;
569 if(start + 0u < size) reader->buffer |= reader->data[start + 0];
570 if(start + 1u < size) reader->buffer |= (unsigned)(reader->data[start + 1] << 8u);
571 if(start + 2u < size) reader->buffer |= (unsigned)(reader->data[start + 2] << 16u);
572 reader->buffer >>= (reader->bp & 7u);
573 return reader->bp + nbits < reader->bitsize;
574 }
575 }
576
577 /*See ensureBits documentation above. This one ensures up to 32 bits */
578 static LODEPNG_INLINE unsigned ensureBits32(LodePNGBitReader* reader, size_t nbits) {
579 size_t start = reader->bp >> 3u;
580 size_t size = reader->size;
581 if(start + 4u < size) {
582 reader->buffer = (unsigned)(reader->data[start + 0]) | (unsigned)(reader->data[start + 1] << 8u) |
583 (unsigned)(reader->data[start + 2] << 16u) | (unsigned)(reader->data[start + 3] << 24u);
584 reader->buffer >>= (reader->bp & 7u);
585 reader->buffer |= ((unsigned)(reader->data[start + 4] << 24u) << (7u - (reader->bp & 7u)));
586 return 1;
587 } else {
588 reader->buffer = 0;
589 if(start + 0u < size) reader->buffer |= reader->data[start + 0];
590 if(start + 1u < size) reader->buffer |= (unsigned)(reader->data[start + 1] << 8u);
591 if(start + 2u < size) reader->buffer |= (unsigned)(reader->data[start + 2] << 16u);
592 if(start + 3u < size) reader->buffer |= (unsigned)(reader->data[start + 3] << 24u);
593 reader->buffer >>= (reader->bp & 7u);
594 return reader->bp + nbits < reader->bitsize;
595 }
596 }
597
598 /* Get bits without advancing the bit pointer. Must have enough bits available with ensureBits */
599 static unsigned peekBits(LodePNGBitReader* reader, size_t nbits) {
600 return reader->buffer & ((1u << nbits) - 1u);
601 }
602
603 /* Must have enough bits available with ensureBits */
604 static void advanceBits(LodePNGBitReader* reader, size_t nbits) {
605 reader->buffer >>= nbits;
606 reader->bp += nbits;
607 }
608
609 /* Must have enough bits available with ensureBits */
610 static unsigned readBits(LodePNGBitReader* reader, size_t nbits) {
611 unsigned result = peekBits(reader, nbits);
612 advanceBits(reader, nbits);
613 return result;
614 }
615 #endif /*LODEPNG_COMPILE_DECODER*/
616
617 static unsigned reverseBits(unsigned bits, unsigned num) {
618 /*TODO: implement faster lookup table based version when needed*/
619 unsigned i, result = 0;
620 for(i = 0; i < num; i++) result |= ((bits >> (num - i - 1u)) & 1u) << i;
621 return result;
622 }
623
624 /* ////////////////////////////////////////////////////////////////////////// */
625 /* / Deflate - Huffman / */
626 /* ////////////////////////////////////////////////////////////////////////// */
627
628 #define FIRST_LENGTH_CODE_INDEX 257
629 #define LAST_LENGTH_CODE_INDEX 285
630 /*256 literals, the end code, some length codes, and 2 unused codes*/
631 #define NUM_DEFLATE_CODE_SYMBOLS 288
632 /*the distance codes have their own symbols, 30 used, 2 unused*/
633 #define NUM_DISTANCE_SYMBOLS 32
634 /*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-138 zeros*/
635 #define NUM_CODE_LENGTH_CODES 19
636
637 /*the base lengths represented by codes 257-285*/
638 static const unsigned LENGTHBASE[29]
639 = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
640 67, 83, 99, 115, 131, 163, 195, 227, 258};
641
642 /*the extra bits used by codes 257-285 (added to base length)*/
643 static const unsigned LENGTHEXTRA[29]
644 = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
645 4, 4, 4, 4, 5, 5, 5, 5, 0};
646
647 /*the base backwards distances (the bits of distance codes appear after length codes and use their own huffman tree)*/
648 static const unsigned DISTANCEBASE[30]
649 = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
650 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
651
652 /*the extra bits of backwards distances (added to base)*/
653 static const unsigned DISTANCEEXTRA[30]
654 = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
655 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
656
657 /*the order in which "code length alphabet code lengths" are stored, out of this
658 the huffman tree of the dynamic huffman tree lengths is generated*/
659 static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES]
660 = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
661
662 /* ////////////////////////////////////////////////////////////////////////// */
663
664 /*
665 Huffman tree struct, containing multiple representations of the tree
666 */
667 typedef struct HuffmanTree {
668 unsigned* codes; /*the huffman codes (bit patterns representing the symbols)*/
669 unsigned* lengths; /*the lengths of the huffman codes*/
670 unsigned maxbitlen; /*maximum number of bits a single code can get*/
671 unsigned numcodes; /*number of symbols in the alphabet = number of codes*/
672 /* for reading only */
673 unsigned char* table_len; /*length of symbol from lookup table, or max length if secondary lookup needed*/
674 unsigned short* table_value; /*value of symbol from lookup table, or pointer to secondary table if needed*/
675 } HuffmanTree;
676
677 static void HuffmanTree_init(HuffmanTree* tree) {
678 tree->codes = 0;
679 tree->lengths = 0;
680 tree->table_len = 0;
681 tree->table_value = 0;
682 }
683
684 static void HuffmanTree_cleanup(HuffmanTree* tree) {
685 lodepng_free(tree->codes);
686 lodepng_free(tree->lengths);
687 lodepng_free(tree->table_len);
688 lodepng_free(tree->table_value);
689 }
690
691 /* amount of bits for first huffman table lookup (aka root bits), see HuffmanTree_makeTable and huffmanDecodeSymbol.*/
692 /* values 8u and 9u work the fastest */
693 #define FIRSTBITS 9u
694
695 /* make table for huffman decoding */
696 static unsigned HuffmanTree_makeTable(HuffmanTree* tree) {
697 static const unsigned headsize = 1u << FIRSTBITS; /*size of the first table*/
698 static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u;
699 size_t i, pointer, size; /*total table size*/
700 unsigned* maxlens = (unsigned*)lodepng_malloc(headsize * sizeof(unsigned));
701 if(!maxlens) return 83; /*alloc fail*/
702
703 /* compute maxlens: max total bit length of symbols sharing prefix in the first table*/
704 for(i = 0; i < headsize; ++i) maxlens[i] = 0;
705 for(i = 0; i < tree->numcodes; i++) {
706 unsigned symbol = tree->codes[i];
707 unsigned l = tree->lengths[i];
708 unsigned index;
709 if(l <= FIRSTBITS) continue; /*symbols that fit in first table don't increase secondary table size*/
710 /*get the FIRSTBITS MSBs, the MSBs of the symbol are encoded first. See later comment about the reversing*/
711 index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS);
712 maxlens[index] = LODEPNG_MAX(maxlens[index], l);
713 }
714 /* compute total table size: size of first table plus all secondary tables for symbols longer than FIRSTBITS */
715 size = headsize;
716 for(i = 0; i < headsize; ++i) {
717 unsigned l = maxlens[i];
718 if(l > FIRSTBITS) size += (1u << (l - FIRSTBITS));
719 }
720 tree->table_len = (unsigned char*)lodepng_malloc(size * sizeof(*tree->table_len));
721 tree->table_value = (unsigned short*)lodepng_malloc(size * sizeof(*tree->table_value));
722 if(!tree->table_len || !tree->table_value) {
723 lodepng_free(maxlens);
724 /* freeing tree->table values is done at a higher scope */
725 return 83; /*alloc fail*/
726 }
727 /*initialize with an invalid length to indicate unused entries*/
728 for(i = 0; i < size; ++i) tree->table_len[i] = 16;
729
730 /*fill in the first table for long symbols: max prefix size and pointer to secondary tables*/
731 pointer = headsize;
732 for(i = 0; i < headsize; ++i) {
733 unsigned l = maxlens[i];
734 if(l <= FIRSTBITS) continue;
735 tree->table_len[i] = l;
736 tree->table_value[i] = pointer;
737 pointer += (1u << (l - FIRSTBITS));
738 }
739 lodepng_free(maxlens);
740
741 /*fill in the first table for short symbols, or secondary table for long symbols*/
742 for(i = 0; i < tree->numcodes; ++i) {
743 unsigned l = tree->lengths[i];
744 unsigned symbol = tree->codes[i]; /*the huffman bit pattern. i itself is the value.*/
745 /*reverse bits, because the huffman bits are given in MSB first order but the bit reader reads LSB first*/
746 unsigned reverse = reverseBits(symbol, l);
747 if(l == 0) {
748 continue;
749 } else if(l <= FIRSTBITS) {
750 /*short symbol, fully in first table, replicated num times if l < FIRSTBITS*/
751 unsigned num = 1u << (FIRSTBITS - l);
752 unsigned j;
753 for(j = 0; j < num; ++j) {
754 /*bit reader will read the l bits of symbol first, the remaining FIRSTBITS - l bits go to the MSB's*/
755 unsigned index = reverse | (j << l);
756 if(tree->table_len[index] != 16) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
757 tree->table_len[index] = l;
758 tree->table_value[index] = i;
759 }
760 } else {
761 /*long symbol, shares prefix with other long symbols in first lookup table, needs second lookup*/
762 /*the FIRSTBITS MSBs of the symbol are the first table index*/
763 unsigned index = reverse & mask;
764 unsigned maxlen = tree->table_len[index];
765 /*log2 of secondary table length, should be >= l - FIRSTBITS*/
766 unsigned tablelen = maxlen - FIRSTBITS;
767 unsigned start = tree->table_value[index]; /*starting index in secondary table*/
768 unsigned num = 1u << (tablelen - (l - FIRSTBITS)); /*amount of entries of this symbol in secondary table*/
769 unsigned j;
770 if(maxlen < l) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
771 for(j = 0; j < num; ++j) {
772 unsigned reverse2 = reverse >> FIRSTBITS; /* l - FIRSTBITS bits */
773 unsigned index2 = start + (reverse2 | (j << (l - FIRSTBITS)));
774 tree->table_len[index2] = l;
775 tree->table_value[index2] = i;
776 }
777 }
778 }
779
780 /* A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes.
781 If that is not the case (due to too long length codes), the table will not
782 have been fully used, and this is an error (not all bit combinations can be
783 decoded): an oversubscribed huffman tree, indicated by error 55. */
784 for(i = 0; i < size; ++i) {
785 if(tree->table_len[i] == 16) return 55;
786 }
787
788 return 0;
789 }
790
791 /*
792 Second step for the ...makeFromLengths and ...makeFromFrequencies functions.
793 numcodes, lengths and maxbitlen must already be filled in correctly. return
794 value is error.
795 */
796 static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) {
797 unsigned* blcount;
798 unsigned* nextcode;
799 unsigned error = 0;
800 unsigned bits, n;
801
802 tree->codes = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned));
803 blcount = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
804 nextcode = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
805 if(!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/
806
807 if(!error) {
808 for(n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0;
809 /*step 1: count number of instances of each code length*/
810 for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]];
811 /*step 2: generate the nextcode values*/
812 for(bits = 1; bits <= tree->maxbitlen; ++bits) {
813 nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1;
814 }
815 /*step 3: generate all the codes*/
816 for(n = 0; n != tree->numcodes; ++n) {
817 if(tree->lengths[n] != 0) {
818 tree->codes[n] = nextcode[tree->lengths[n]]++;
819 /*remove superfluous bits from the code*/
820 tree->codes[n] &= ((1u << tree->lengths[n]) - 1u);
821 }
822 }
823 }
824
825 lodepng_free(blcount);
826 lodepng_free(nextcode);
827
828 if(!error) error = HuffmanTree_makeTable(tree);
829 return error;
830 }
831
832 /*
833 given the code lengths (as stored in the PNG file), generate the tree as defined
834 by Deflate. maxbitlen is the maximum bits that a code in the tree can have.
835 return value is error.
836 */
837 static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen,
838 size_t numcodes, unsigned maxbitlen) {
839 unsigned i;
840 tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned));
841 if(!tree->lengths) return 83; /*alloc fail*/
842 for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i];
843 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
844 tree->maxbitlen = maxbitlen;
845 return HuffmanTree_makeFromLengths2(tree);
846 }
847
848 #ifdef LODEPNG_COMPILE_ENCODER
849
850 /*BPM: Boundary Package Merge, see "A Fast and Space-Economical Algorithm for Length-Limited Coding",
851 Jyrki Katajainen, Alistair Moffat, Andrew Turpin, 1995.*/
852
853 /*chain node for boundary package merge*/
854 typedef struct BPMNode {
855 int weight; /*the sum of all weights in this chain*/
856 unsigned index; /*index of this leaf node (called "count" in the paper)*/
857 struct BPMNode* tail; /*the next nodes in this chain (null if last)*/
858 int in_use;
859 } BPMNode;
860
861 /*lists of chains*/
862 typedef struct BPMLists {
863 /*memory pool*/
864 unsigned memsize;
865 BPMNode* memory;
866 unsigned numfree;
867 unsigned nextfree;
868 BPMNode** freelist;
869 /*two heads of lookahead chains per list*/
870 unsigned listsize;
871 BPMNode** chains0;
872 BPMNode** chains1;
873 } BPMLists;
874
875 /*creates a new chain node with the given parameters, from the memory in the lists */
876 static BPMNode* bpmnode_create(BPMLists* lists, int weight, unsigned index, BPMNode* tail) {
877 unsigned i;
878 BPMNode* result;
879
880 /*memory full, so garbage collect*/
881 if(lists->nextfree >= lists->numfree) {
882 /*mark only those that are in use*/
883 for(i = 0; i != lists->memsize; ++i) lists->memory[i].in_use = 0;
884 for(i = 0; i != lists->listsize; ++i) {
885 BPMNode* node;
886 for(node = lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1;
887 for(node = lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1;
888 }
889 /*collect those that are free*/
890 lists->numfree = 0;
891 for(i = 0; i != lists->memsize; ++i) {
892 if(!lists->memory[i].in_use) lists->freelist[lists->numfree++] = &lists->memory[i];
893 }
894 lists->nextfree = 0;
895 }
896
897 result = lists->freelist[lists->nextfree++];
898 result->weight = weight;
899 result->index = index;
900 result->tail = tail;
901 return result;
902 }
903
904 /*sort the leaves with stable mergesort*/
905 static void bpmnode_sort(BPMNode* leaves, size_t num) {
906 BPMNode* mem = (BPMNode*)lodepng_malloc(sizeof(*leaves) * num);
907 size_t width, counter = 0;
908 for(width = 1; width < num; width *= 2) {
909 BPMNode* a = (counter & 1) ? mem : leaves;
910 BPMNode* b = (counter & 1) ? leaves : mem;
911 size_t p;
912 for(p = 0; p < num; p += 2 * width) {
913 size_t q = (p + width > num) ? num : (p + width);
914 size_t r = (p + 2 * width > num) ? num : (p + 2 * width);
915 size_t i = p, j = q, k;
916 for(k = p; k < r; k++) {
917 if(i < q && (j >= r || a[i].weight <= a[j].weight)) b[k] = a[i++];
918 else b[k] = a[j++];
919 }
920 }
921 counter++;
922 }
923 if(counter & 1) lodepng_memcpy(leaves, mem, sizeof(*leaves) * num);
924 lodepng_free(mem);
925 }
926
927 /*Boundary Package Merge step, numpresent is the amount of leaves, and c is the current chain.*/
928 static void boundaryPM(BPMLists* lists, BPMNode* leaves, size_t numpresent, int c, int num) {
929 unsigned lastindex = lists->chains1[c]->index;
930
931 if(c == 0) {
932 if(lastindex >= numpresent) return;
933 lists->chains0[c] = lists->chains1[c];
934 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, 0);
935 } else {
936 /*sum of the weights of the head nodes of the previous lookahead chains.*/
937 int sum = lists->chains0[c - 1]->weight + lists->chains1[c - 1]->weight;
938 lists->chains0[c] = lists->chains1[c];
939 if(lastindex < numpresent && sum > leaves[lastindex].weight) {
940 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, lists->chains1[c]->tail);
941 return;
942 }
943 lists->chains1[c] = bpmnode_create(lists, sum, lastindex, lists->chains1[c - 1]);
944 /*in the end we are only interested in the chain of the last list, so no
945 need to recurse if we're at the last one (this gives measurable speedup)*/
946 if(num + 1 < (int)(2 * numpresent - 2)) {
947 boundaryPM(lists, leaves, numpresent, c - 1, num);
948 boundaryPM(lists, leaves, numpresent, c - 1, num);
949 }
950 }
951 }
952
953 unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies,
954 size_t numcodes, unsigned maxbitlen) {
955 unsigned error = 0;
956 unsigned i;
957 size_t numpresent = 0; /*number of symbols with non-zero frequency*/
958 BPMNode* leaves; /*the symbols, only those with > 0 frequency*/
959
960 if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
961 if((1u << maxbitlen) < (unsigned)numcodes) return 80; /*error: represent all symbols*/
962
963 leaves = (BPMNode*)lodepng_malloc(numcodes * sizeof(*leaves));
964 if(!leaves) return 83; /*alloc fail*/
965
966 for(i = 0; i != numcodes; ++i) {
967 if(frequencies[i] > 0) {
968 leaves[numpresent].weight = (int)frequencies[i];
969 leaves[numpresent].index = i;
970 ++numpresent;
971 }
972 }
973
974 for(i = 0; i != numcodes; ++i) lengths[i] = 0;
975
976 /*ensure at least two present symbols. There should be at least one symbol
977 according to RFC 1951 section 3.2.7. Some decoders incorrectly require two. To
978 make these work as well ensure there are at least two symbols. The
979 Package-Merge code below also doesn't work correctly if there's only one
980 symbol, it'd give it the theoretical 0 bits but in practice zlib wants 1 bit*/
981 if(numpresent == 0) {
982 lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/
983 } else if(numpresent == 1) {
984 lengths[leaves[0].index] = 1;
985 lengths[leaves[0].index == 0 ? 1 : 0] = 1;
986 } else {
987 BPMLists lists;
988 BPMNode* node;
989
990 bpmnode_sort(leaves, numpresent);
991
992 lists.listsize = maxbitlen;
993 lists.memsize = 2 * maxbitlen * (maxbitlen + 1);
994 lists.nextfree = 0;
995 lists.numfree = lists.memsize;
996 lists.memory = (BPMNode*)lodepng_malloc(lists.memsize * sizeof(*lists.memory));
997 lists.freelist = (BPMNode**)lodepng_malloc(lists.memsize * sizeof(BPMNode*));
998 lists.chains0 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
999 lists.chains1 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
1000 if(!lists.memory || !lists.freelist || !lists.chains0 || !lists.chains1) error = 83; /*alloc fail*/
1001
1002 if(!error) {
1003 for(i = 0; i != lists.memsize; ++i) lists.freelist[i] = &lists.memory[i];
1004
1005 bpmnode_create(&lists, leaves[0].weight, 1, 0);
1006 bpmnode_create(&lists, leaves[1].weight, 2, 0);
1007
1008 for(i = 0; i != lists.listsize; ++i) {
1009 lists.chains0[i] = &lists.memory[0];
1010 lists.chains1[i] = &lists.memory[1];
1011 }
1012
1013 /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/
1014 for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1, (int)i);
1015
1016 for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail) {
1017 for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index];
1018 }
1019 }
1020
1021 lodepng_free(lists.memory);
1022 lodepng_free(lists.freelist);
1023 lodepng_free(lists.chains0);
1024 lodepng_free(lists.chains1);
1025 }
1026
1027 lodepng_free(leaves);
1028 return error;
1029 }
1030
1031 /*Create the Huffman tree given the symbol frequencies*/
1032 static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies,
1033 size_t mincodes, size_t numcodes, unsigned maxbitlen) {
1034 size_t i;
1035 unsigned error = 0;
1036 while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/
1037 tree->maxbitlen = maxbitlen;
1038 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
1039 tree->lengths = (unsigned*)lodepng_realloc(tree->lengths, numcodes * sizeof(unsigned));
1040 if(!tree->lengths) return 83; /*alloc fail*/
1041 /*initialize all lengths to 0*/
1042 for(i = 0; i < numcodes; i++) tree->lengths[i] = 0;
1043
1044 error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen);
1045 if(!error) error = HuffmanTree_makeFromLengths2(tree);
1046 return error;
1047 }
1048
1049 static unsigned HuffmanTree_getCode(const HuffmanTree* tree, unsigned index) {
1050 return tree->codes[index];
1051 }
1052
1053 static unsigned HuffmanTree_getLength(const HuffmanTree* tree, unsigned index) {
1054 return tree->lengths[index];
1055 }
1056 #endif /*LODEPNG_COMPILE_ENCODER*/
1057
1058 /*get the literal and length code tree of a deflated block with fixed tree, as per the deflate specification*/
1059 static unsigned generateFixedLitLenTree(HuffmanTree* tree) {
1060 unsigned i, error = 0;
1061 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
1062 if(!bitlen) return 83; /*alloc fail*/
1063
1064 /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/
1065 for(i = 0; i <= 143; ++i) bitlen[i] = 8;
1066 for(i = 144; i <= 255; ++i) bitlen[i] = 9;
1067 for(i = 256; i <= 279; ++i) bitlen[i] = 7;
1068 for(i = 280; i <= 287; ++i) bitlen[i] = 8;
1069
1070 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15);
1071
1072 lodepng_free(bitlen);
1073 return error;
1074 }
1075
1076 /*get the distance code tree of a deflated block with fixed tree, as specified in the deflate specification*/
1077 static unsigned generateFixedDistanceTree(HuffmanTree* tree) {
1078 unsigned i, error = 0;
1079 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
1080 if(!bitlen) return 83; /*alloc fail*/
1081
1082 /*there are 32 distance codes, but 30-31 are unused*/
1083 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5;
1084 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15);
1085
1086 lodepng_free(bitlen);
1087 return error;
1088 }
1089
1090 #ifdef LODEPNG_COMPILE_DECODER
1091
1092 /*
1093 returns the code. The bit reader must already have been ensured at least 15 bits
1094 */
1095 static unsigned huffmanDecodeSymbol(LodePNGBitReader* reader, const HuffmanTree* codetree) {
1096 unsigned short code = peekBits(reader, FIRSTBITS);
1097 unsigned short l = codetree->table_len[code];
1098 unsigned short value = codetree->table_value[code];
1099 if(l <= FIRSTBITS) {
1100 advanceBits(reader, l);
1101 return value;
1102 } else {
1103 unsigned index2;
1104 advanceBits(reader, FIRSTBITS);
1105 index2 = value + peekBits(reader, l - FIRSTBITS);
1106 advanceBits(reader, codetree->table_len[index2] - FIRSTBITS);
1107 return codetree->table_value[index2];
1108 }
1109 }
1110 #endif /*LODEPNG_COMPILE_DECODER*/
1111
1112 #ifdef LODEPNG_COMPILE_DECODER
1113
1114 /* ////////////////////////////////////////////////////////////////////////// */
1115 /* / Inflator (Decompressor) / */
1116 /* ////////////////////////////////////////////////////////////////////////// */
1117
1118 /*get the tree of a deflated block with fixed tree, as specified in the deflate specification*/
1119 static void getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d) {
1120 /*TODO: check for out of memory errors*/
1121 generateFixedLitLenTree(tree_ll);
1122 generateFixedDistanceTree(tree_d);
1123 }
1124
1125 /*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/
1126 static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
1127 LodePNGBitReader* reader) {
1128 /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/
1129 unsigned error = 0;
1130 unsigned n, HLIT, HDIST, HCLEN, i;
1131
1132 /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/
1133 unsigned* bitlen_ll = 0; /*lit,len code lengths*/
1134 unsigned* bitlen_d = 0; /*dist code lengths*/
1135 /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/
1136 unsigned* bitlen_cl = 0;
1137 HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/
1138
1139 if(!ensureBits17(reader, 14)) return 49; /*error: the bit pointer is or will go past the memory*/
1140
1141 /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/
1142 HLIT = readBits(reader, 5) + 257;
1143 /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/
1144 HDIST = readBits(reader, 5) + 1;
1145 /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/
1146 HCLEN = readBits(reader, 4) + 4;
1147
1148 bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned));
1149 if(!bitlen_cl) return 83 /*alloc fail*/;
1150
1151 HuffmanTree_init(&tree_cl);
1152
1153 while(!error) {
1154 /*read the code length codes out of 3 * (amount of code length codes) bits*/
1155 if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) {
1156 ERROR_BREAK(50); /*error: the bit pointer is or will go past the memory*/
1157 }
1158 for(i = 0; i != HCLEN; ++i) {
1159 ensureBits9(reader, 3); /*out of bounds already checked above */
1160 bitlen_cl[CLCL_ORDER[i]] = readBits(reader, 3);
1161 }
1162 for(i = HCLEN; i != NUM_CODE_LENGTH_CODES; ++i) {
1163 bitlen_cl[CLCL_ORDER[i]] = 0;
1164 }
1165
1166 error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7);
1167 if(error) break;
1168
1169 /*now we can use this tree to read the lengths for the tree that this function will return*/
1170 bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
1171 bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
1172 if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/);
1173 for(i = 0; i != NUM_DEFLATE_CODE_SYMBOLS; ++i) bitlen_ll[i] = 0;
1174 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen_d[i] = 0;
1175
1176 /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/
1177 i = 0;
1178 while(i < HLIT + HDIST) {
1179 unsigned code;
1180 ensureBits25(reader, 22); /* up to 15 bits for huffman code, up to 7 extra bits below*/
1181 code = huffmanDecodeSymbol(reader, &tree_cl);
1182 if(code <= 15) /*a length code*/ {
1183 if(i < HLIT) bitlen_ll[i] = code;
1184 else bitlen_d[i - HLIT] = code;
1185 ++i;
1186 } else if(code == 16) /*repeat previous*/ {
1187 unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/
1188 unsigned value; /*set value to the previous code*/
1189
1190 if(i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/
1191
1192 replength += readBits(reader, 2);
1193
1194 if(i < HLIT + 1) value = bitlen_ll[i - 1];
1195 else value = bitlen_d[i - HLIT - 1];
1196 /*repeat this value in the next lengths*/
1197 for(n = 0; n < replength; ++n) {
1198 if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/
1199 if(i < HLIT) bitlen_ll[i] = value;
1200 else bitlen_d[i - HLIT] = value;
1201 ++i;
1202 }
1203 } else if(code == 17) /*repeat "0" 3-10 times*/ {
1204 unsigned replength = 3; /*read in the bits that indicate repeat length*/
1205 replength += readBits(reader, 3);
1206
1207 /*repeat this value in the next lengths*/
1208 for(n = 0; n < replength; ++n) {
1209 if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/
1210
1211 if(i < HLIT) bitlen_ll[i] = 0;
1212 else bitlen_d[i - HLIT] = 0;
1213 ++i;
1214 }
1215 } else if(code == 18) /*repeat "0" 11-138 times*/ {
1216 unsigned replength = 11; /*read in the bits that indicate repeat length*/
1217 replength += readBits(reader, 7);
1218
1219 /*repeat this value in the next lengths*/
1220 for(n = 0; n < replength; ++n) {
1221 if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/
1222
1223 if(i < HLIT) bitlen_ll[i] = 0;
1224 else bitlen_d[i - HLIT] = 0;
1225 ++i;
1226 }
1227 } else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ {
1228 ERROR_BREAK(16); /*nonexistent code, this can never happen*/
1229 }
1230 /*check if any of the ensureBits above went out of bounds*/
1231 if(reader->bp > reader->bitsize) {
1232 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
1233 (10=no endcode, 11=wrong jump outside of tree)*/
1234 /* TODO: revise error codes 10,11,50: the above comment is no longer valid */
1235 ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
1236 }
1237 }
1238 if(error) break;
1239
1240 if(bitlen_ll[256] == 0) ERROR_BREAK(64); /*the length of the end code 256 must be larger than 0*/
1241
1242 /*now we've finally got HLIT and HDIST, so generate the code trees, and the function is done*/
1243 error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15);
1244 if(error) break;
1245 error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15);
1246
1247 break; /*end of error-while*/
1248 }
1249
1250 lodepng_free(bitlen_cl);
1251 lodepng_free(bitlen_ll);
1252 lodepng_free(bitlen_d);
1253 HuffmanTree_cleanup(&tree_cl);
1254
1255 return error;
1256 }
1257
1258 /*inflate a block with dynamic of fixed Huffman tree. btype must be 1 or 2.*/
1259 static unsigned inflateHuffmanBlock(ucvector* out, size_t* pos, LodePNGBitReader* reader,
1260 unsigned btype) {
1261 unsigned error = 0;
1262 HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/
1263 HuffmanTree tree_d; /*the huffman tree for distance codes*/
1264
1265 HuffmanTree_init(&tree_ll);
1266 HuffmanTree_init(&tree_d);
1267
1268 if(btype == 1) getTreeInflateFixed(&tree_ll, &tree_d);
1269 else /*if(btype == 2)*/ error = getTreeInflateDynamic(&tree_ll, &tree_d, reader);
1270
1271 while(!error) /*decode all symbols until end reached, breaks at end code*/ {
1272 /*code_ll is literal, length or end code*/
1273 unsigned code_ll;
1274 ensureBits25(reader, 20); /* up to 15 for the huffman symbol, up to 5 for the length extra bits */
1275 code_ll = huffmanDecodeSymbol(reader, &tree_ll);
1276 if(code_ll <= 255) /*literal symbol*/ {
1277 /*ucvector_push_back would do the same, but for some reason the two lines below run 10% faster*/
1278 if(!ucvector_resize(out, (*pos) + 1)) ERROR_BREAK(83 /*alloc fail*/);
1279 out->data[*pos] = (unsigned char)code_ll;
1280 ++(*pos);
1281 } else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/ {
1282 unsigned code_d, distance;
1283 unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/
1284 size_t start, backward, length;
1285
1286 /*part 1: get length base*/
1287 length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX];
1288
1289 /*part 2: get extra bits and add the value of that to length*/
1290 numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX];
1291 if(numextrabits_l != 0) {
1292 /* bits already ensured above */
1293 length += readBits(reader, numextrabits_l);
1294 }
1295
1296 /*part 3: get distance code*/
1297 ensureBits32(reader, 28); /* up to 15 for the huffman symbol, up to 13 for the extra bits */
1298 code_d = huffmanDecodeSymbol(reader, &tree_d);
1299 if(code_d > 29) {
1300 if(code_d == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ {
1301 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
1302 (10=no endcode, 11=wrong jump outside of tree)*/
1303 ERROR_BREAK((reader->bp > reader->bitsize) ? 10 : 11);
1304 } else {
1305 ERROR_BREAK(18); /*error: invalid distance code (30-31 are never used)*/
1306 }
1307 }
1308 distance = DISTANCEBASE[code_d];
1309
1310 /*part 4: get extra bits from distance*/
1311 numextrabits_d = DISTANCEEXTRA[code_d];
1312 if(numextrabits_d != 0) {
1313 /* bits already ensured above */
1314 distance += readBits(reader, numextrabits_d);
1315 }
1316
1317 /*part 5: fill in all the out[n] values based on the length and dist*/
1318 start = (*pos);
1319 if(distance > start) ERROR_BREAK(52); /*too long backward distance*/
1320 backward = start - distance;
1321
1322 if(!ucvector_resize(out, (*pos) + length)) ERROR_BREAK(83 /*alloc fail*/);
1323 if (distance < length) {
1324 size_t forward;
1325 lodepng_memcpy(out->data + *pos, out->data + backward, distance);
1326 *pos += distance;
1327 for(forward = distance; forward < length; ++forward) {
1328 out->data[(*pos)++] = out->data[backward++];
1329 }
1330 } else {
1331 lodepng_memcpy(out->data + *pos, out->data + backward, length);
1332 *pos += length;
1333 }
1334 } else if(code_ll == 256) {
1335 break; /*end code, break the loop*/
1336 } else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ {
1337 ERROR_BREAK(16) /* impossible */
1338 }
1339 /*check if any of the ensureBits above went out of bounds*/
1340 if(reader->bp > reader->bitsize) {
1341 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
1342 (10=no endcode, 11=wrong jump outside of tree)*/
1343 /* TODO: revise error codes 10,11,50: the above comment is no longer valid */
1344 ERROR_BREAK(51); /*error, bit pointer jumps past memory*/
1345 }
1346 }
1347
1348 HuffmanTree_cleanup(&tree_ll);
1349 HuffmanTree_cleanup(&tree_d);
1350
1351 return error;
1352 }
1353
1354 static unsigned inflateNoCompression(ucvector* out, size_t* pos,
1355 LodePNGBitReader* reader, const LodePNGDecompressSettings* settings) {
1356 size_t bytepos;
1357 size_t size = reader->size;
1358 unsigned LEN, NLEN, error = 0;
1359
1360 /*go to first boundary of byte*/
1361 bytepos = (reader->bp + 7u) >> 3u;
1362
1363 /*read LEN (2 bytes) and NLEN (2 bytes)*/
1364 if(bytepos + 4 >= size) return 52; /*error, bit pointer will jump past memory*/
1365 LEN = (unsigned)reader->data[bytepos] + (unsigned)(reader->data[bytepos + 1] << 8u); bytepos += 2;
1366 NLEN = (unsigned)reader->data[bytepos] + (unsigned)(reader->data[bytepos + 1] << 8u); bytepos += 2;
1367
1368 /*check if 16-bit NLEN is really the one's complement of LEN*/
1369 if(!settings->ignore_nlen && LEN + NLEN != 65535) {
1370 return 21; /*error: NLEN is not one's complement of LEN*/
1371 }
1372
1373 if(!ucvector_resize(out, (*pos) + LEN)) return 83; /*alloc fail*/
1374
1375 /*read the literal data: LEN bytes are now stored in the out buffer*/
1376 if(bytepos + LEN > size) return 23; /*error: reading outside of in buffer*/
1377
1378 lodepng_memcpy(out->data + *pos, reader->data + bytepos, LEN);
1379 *pos += LEN;
1380 bytepos += LEN;
1381
1382 reader->bp = bytepos << 3u;
1383
1384 return error;
1385 }
1386
1387 static unsigned lodepng_inflatev(ucvector* out,
1388 const unsigned char* in, size_t insize,
1389 const LodePNGDecompressSettings* settings) {
1390 unsigned BFINAL = 0;
1391 size_t pos = 0; /*byte position in the out buffer*/
1392 LodePNGBitReader reader;
1393 unsigned error = LodePNGBitReader_init(&reader, in, insize);
1394
1395 if(error) return error;
1396
1397 while(!BFINAL) {
1398 unsigned BTYPE;
1399 if(!ensureBits9(&reader, 3)) return 52; /*error, bit pointer will jump past memory*/
1400 BFINAL = readBits(&reader, 1);
1401 BTYPE = readBits(&reader, 2);
1402
1403 if(BTYPE == 3) return 20; /*error: invalid BTYPE*/
1404 else if(BTYPE == 0) error = inflateNoCompression(out, &pos, &reader, settings); /*no compression*/
1405 else error = inflateHuffmanBlock(out, &pos, &reader, BTYPE); /*compression, BTYPE 01 or 10*/
1406
1407 if(error) return error;
1408 }
1409
1410 return error;
1411 }
1412
1413 unsigned lodepng_inflate(unsigned char** out, size_t* outsize,
1414 const unsigned char* in, size_t insize,
1415 const LodePNGDecompressSettings* settings) {
1416 unsigned error;
1417 ucvector v;
1418 ucvector_init_buffer(&v, *out, *outsize);
1419 error = lodepng_inflatev(&v, in, insize, settings);
1420 *out = v.data;
1421 *outsize = v.size;
1422 return error;
1423 }
1424
1425 static unsigned inflate(unsigned char** out, size_t* outsize,
1426 const unsigned char* in, size_t insize,
1427 const LodePNGDecompressSettings* settings) {
1428 if(settings->custom_inflate) {
1429 return settings->custom_inflate(out, outsize, in, insize, settings);
1430 } else {
1431 return lodepng_inflate(out, outsize, in, insize, settings);
1432 }
1433 }
1434
1435 #endif /*LODEPNG_COMPILE_DECODER*/
1436
1437 #ifdef LODEPNG_COMPILE_ENCODER
1438
1439 /* ////////////////////////////////////////////////////////////////////////// */
1440 /* / Deflator (Compressor) / */
1441 /* ////////////////////////////////////////////////////////////////////////// */
1442
1443 static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258;
1444
1445 /*search the index in the array, that has the largest value smaller than or equal to the given value,
1446 given array must be sorted (if no value is smaller, it returns the size of the given array)*/
1447 static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value) {
1448 /*binary search (only small gain over linear). TODO: use CPU log2 instruction for getting symbols instead*/
1449 size_t left = 1;
1450 size_t right = array_size - 1;
1451
1452 while(left <= right) {
1453 size_t mid = (left + right) >> 1;
1454 if (array[mid] >= value) right = mid - 1;
1455 else left = mid + 1;
1456 }
1457 if(left >= array_size || array[left] > value) left--;
1458 return left;
1459 }
1460
1461 static void addLengthDistance(uivector* values, size_t length, size_t distance) {
1462 /*values in encoded vector are those used by deflate:
1463 0-255: literal bytes
1464 256: end
1465 257-285: length/distance pair (length code, followed by extra length bits, distance code, extra distance bits)
1466 286-287: invalid*/
1467
1468 unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29, length);
1469 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]);
1470 unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance);
1471 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]);
1472
1473 uivector_push_back(values, length_code + FIRST_LENGTH_CODE_INDEX);
1474 uivector_push_back(values, extra_length);
1475 uivector_push_back(values, dist_code);
1476 uivector_push_back(values, extra_distance);
1477 }
1478
1479 /*3 bytes of data get encoded into two bytes. The hash cannot use more than 3
1480 bytes as input because 3 is the minimum match length for deflate*/
1481 static const unsigned HASH_NUM_VALUES = 65536;
1482 static const unsigned HASH_BIT_MASK = 65535; /*HASH_NUM_VALUES - 1, but C90 does not like that as initializer*/
1483
1484 typedef struct Hash {
1485 int* head; /*hash value to head circular pos - can be outdated if went around window*/
1486 /*circular pos to prev circular pos*/
1487 unsigned short* chain;
1488 int* val; /*circular pos to hash value*/
1489
1490 /*TODO: do this not only for zeros but for any repeated byte. However for PNG
1491 it's always going to be the zeros that dominate, so not important for PNG*/
1492 int* headz; /*similar to head, but for chainz*/
1493 unsigned short* chainz; /*those with same amount of zeros*/
1494 unsigned short* zeros; /*length of zeros streak, used as a second hash chain*/
1495 } Hash;
1496
1497 static unsigned hash_init(Hash* hash, unsigned windowsize) {
1498 unsigned i;
1499 hash->head = (int*)lodepng_malloc(sizeof(int) * HASH_NUM_VALUES);
1500 hash->val = (int*)lodepng_malloc(sizeof(int) * windowsize);
1501 hash->chain = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
1502
1503 hash->zeros = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
1504 hash->headz = (int*)lodepng_malloc(sizeof(int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1));
1505 hash->chainz = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
1506
1507 if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros) {
1508 return 83; /*alloc fail*/
1509 }
1510
1511 /*initialize hash table*/
1512 for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1;
1513 for(i = 0; i != windowsize; ++i) hash->val[i] = -1;
1514 for(i = 0; i != windowsize; ++i) hash->chain[i] = i; /*same value as index indicates uninitialized*/
1515
1516 for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1;
1517 for(i = 0; i != windowsize; ++i) hash->chainz[i] = i; /*same value as index indicates uninitialized*/
1518
1519 return 0;
1520 }
1521
1522 static void hash_cleanup(Hash* hash) {
1523 lodepng_free(hash->head);
1524 lodepng_free(hash->val);
1525 lodepng_free(hash->chain);
1526
1527 lodepng_free(hash->zeros);
1528 lodepng_free(hash->headz);
1529 lodepng_free(hash->chainz);
1530 }
1531
1532
1533
1534 static unsigned getHash(const unsigned char* data, size_t size, size_t pos) {
1535 unsigned result = 0;
1536 if(pos + 2 < size) {
1537 /*A simple shift and xor hash is used. Since the data of PNGs is dominated
1538 by zeroes due to the filters, a better hash does not have a significant
1539 effect on speed in traversing the chain, and causes more time spend on
1540 calculating the hash.*/
1541 result ^= (unsigned)(data[pos + 0] << 0u);
1542 result ^= (unsigned)(data[pos + 1] << 4u);
1543 result ^= (unsigned)(data[pos + 2] << 8u);
1544 } else {
1545 size_t amount, i;
1546 if(pos >= size) return 0;
1547 amount = size - pos;
1548 for(i = 0; i != amount; ++i) result ^= (unsigned)(data[pos + i] << (i * 8u));
1549 }
1550 return result & HASH_BIT_MASK;
1551 }
1552
1553 static unsigned countZeros(const unsigned char* data, size_t size, size_t pos) {
1554 const unsigned char* start = data + pos;
1555 const unsigned char* end = start + MAX_SUPPORTED_DEFLATE_LENGTH;
1556 if(end > data + size) end = data + size;
1557 data = start;
1558 while(data != end && *data == 0) ++data;
1559 /*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/
1560 return (unsigned)(data - start);
1561 }
1562
1563 /*wpos = pos & (windowsize - 1)*/
1564 static void updateHashChain(Hash* hash, size_t wpos, unsigned hashval, unsigned short numzeros) {
1565 hash->val[wpos] = (int)hashval;
1566 if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval];
1567 hash->head[hashval] = (int)wpos;
1568
1569 hash->zeros[wpos] = numzeros;
1570 if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros];
1571 hash->headz[numzeros] = (int)wpos;
1572 }
1573
1574 /*
1575 LZ77-encode the data. Return value is error code. The input are raw bytes, the output
1576 is in the form of unsigned integers with codes representing for example literal bytes, or
1577 length/distance pairs.
1578 It uses a hash table technique to let it encode faster. When doing LZ77 encoding, a
1579 sliding window (of windowsize) is used, and all past bytes in that window can be used as
1580 the "dictionary". A brute force search through all possible distances would be slow, and
1581 this hash technique is one out of several ways to speed this up.
1582 */
1583 static unsigned encodeLZ77(uivector* out, Hash* hash,
1584 const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize,
1585 unsigned minmatch, unsigned nicematch, unsigned lazymatching) {
1586 size_t pos;
1587 unsigned i, error = 0;
1588 /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/
1589 unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8u;
1590 unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64;
1591
1592 unsigned usezeros = 1; /*not sure if setting it to false for windowsize < 8192 is better or worse*/
1593 unsigned numzeros = 0;
1594
1595 unsigned offset; /*the offset represents the distance in LZ77 terminology*/
1596 unsigned length;
1597 unsigned lazy = 0;
1598 unsigned lazylength = 0, lazyoffset = 0;
1599 unsigned hashval;
1600 unsigned current_offset, current_length;
1601 unsigned prev_offset;
1602 const unsigned char *lastptr, *foreptr, *backptr;
1603 unsigned hashpos;
1604
1605 if(windowsize == 0 || windowsize > 32768) return 60; /*error: windowsize smaller/larger than allowed*/
1606 if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/
1607
1608 if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH;
1609
1610 for(pos = inpos; pos < insize; ++pos) {
1611 size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/
1612 unsigned chainlength = 0;
1613
1614 hashval = getHash(in, insize, pos);
1615
1616 if(usezeros && hashval == 0) {
1617 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
1618 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
1619 } else {
1620 numzeros = 0;
1621 }
1622
1623 updateHashChain(hash, wpos, hashval, numzeros);
1624
1625 /*the length and offset found for the current position*/
1626 length = 0;
1627 offset = 0;
1628
1629 hashpos = hash->chain[wpos];
1630
1631 lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];
1632
1633 /*search for the longest string*/
1634 prev_offset = 0;
1635 for(;;) {
1636 if(chainlength++ >= maxchainlength) break;
1637 current_offset = (unsigned)(hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize);
1638
1639 if(current_offset < prev_offset) break; /*stop when went completely around the circular buffer*/
1640 prev_offset = current_offset;
1641 if(current_offset > 0) {
1642 /*test the next characters*/
1643 foreptr = &in[pos];
1644 backptr = &in[pos - current_offset];
1645
1646 /*common case in PNGs is lots of zeros. Quickly skip over them as a speedup*/
1647 if(numzeros >= 3) {
1648 unsigned skip = hash->zeros[hashpos];
1649 if(skip > numzeros) skip = numzeros;
1650 backptr += skip;
1651 foreptr += skip;
1652 }
1653
1654 while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/ {
1655 ++backptr;
1656 ++foreptr;
1657 }
1658 current_length = (unsigned)(foreptr - &in[pos]);
1659
1660 if(current_length > length) {
1661 length = current_length; /*the longest length*/
1662 offset = current_offset; /*the offset that is related to this longest length*/
1663 /*jump out once a length of max length is found (speed gain). This also jumps
1664 out if length is MAX_SUPPORTED_DEFLATE_LENGTH*/
1665 if(current_length >= nicematch) break;
1666 }
1667 }
1668
1669 if(hashpos == hash->chain[hashpos]) break;
1670
1671 if(numzeros >= 3 && length > numzeros) {
1672 hashpos = hash->chainz[hashpos];
1673 if(hash->zeros[hashpos] != numzeros) break;
1674 } else {
1675 hashpos = hash->chain[hashpos];
1676 /*outdated hash value, happens if particular value was not encountered in whole last window*/
1677 if(hash->val[hashpos] != (int)hashval) break;
1678 }
1679 }
1680
1681 if(lazymatching) {
1682 if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH) {
1683 lazy = 1;
1684 lazylength = length;
1685 lazyoffset = offset;
1686 continue; /*try the next byte*/
1687 }
1688 if(lazy) {
1689 lazy = 0;
1690 if(pos == 0) ERROR_BREAK(81);
1691 if(length > lazylength + 1) {
1692 /*push the previous character as literal*/
1693 if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/);
1694 } else {
1695 length = lazylength;
1696 offset = lazyoffset;
1697 hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/
1698 hash->headz[numzeros] = -1; /*idem*/
1699 --pos;
1700 }
1701 }
1702 }
1703 if(length >= 3 && offset > windowsize) ERROR_BREAK(86 /*too big (or overflown negative) offset*/);
1704
1705 /*encode it as length/distance pair or literal value*/
1706 if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/ {
1707 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
1708 } else if(length < minmatch || (length == 3 && offset > 4096)) {
1709 /*compensate for the fact that longer offsets have more extra bits, a
1710 length of only 3 may be not worth it then*/
1711 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
1712 } else {
1713 addLengthDistance(out, length, offset);
1714 for(i = 1; i < length; ++i) {
1715 ++pos;
1716 wpos = pos & (windowsize - 1);
1717 hashval = getHash(in, insize, pos);
1718 if(usezeros && hashval == 0) {
1719 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
1720 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
1721 } else {
1722 numzeros = 0;
1723 }
1724 updateHashChain(hash, wpos, hashval, numzeros);
1725 }
1726 }
1727 } /*end of the loop through each character of input*/
1728
1729 return error;
1730 }
1731
1732 /* /////////////////////////////////////////////////////////////////////////// */
1733
1734 static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize) {
1735 /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte,
1736 2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/
1737
1738 size_t i, j, numdeflateblocks = (datasize + 65534u) / 65535u;
1739 unsigned datapos = 0;
1740 for(i = 0; i != numdeflateblocks; ++i) {
1741 unsigned BFINAL, BTYPE, LEN, NLEN;
1742 unsigned char firstbyte;
1743
1744 BFINAL = (i == numdeflateblocks - 1);
1745 BTYPE = 0;
1746
1747 firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1) << 1) + ((BTYPE & 2) << 1));
1748 ucvector_push_back(out, firstbyte);
1749
1750 LEN = 65535;
1751 if(datasize - datapos < 65535u) LEN = (unsigned)datasize - datapos;
1752 NLEN = 65535 - LEN;
1753
1754 ucvector_push_back(out, (unsigned char)(LEN & 255));
1755 ucvector_push_back(out, (unsigned char)(LEN >> 8u));
1756 ucvector_push_back(out, (unsigned char)(NLEN & 255));
1757 ucvector_push_back(out, (unsigned char)(NLEN >> 8u));
1758
1759 /*Decompressed data*/
1760 for(j = 0; j < 65535 && datapos < datasize; ++j) {
1761 ucvector_push_back(out, data[datapos++]);
1762 }
1763 }
1764
1765 return 0;
1766 }
1767
1768 /*
1769 write the lz77-encoded data, which has lit, len and dist codes, to compressed stream using huffman trees.
1770 tree_ll: the tree for lit and len codes.
1771 tree_d: the tree for distance codes.
1772 */
1773 static void writeLZ77data(LodePNGBitWriter* writer, const uivector* lz77_encoded,
1774 const HuffmanTree* tree_ll, const HuffmanTree* tree_d) {
1775 size_t i = 0;
1776 for(i = 0; i != lz77_encoded->size; ++i) {
1777 unsigned val = lz77_encoded->data[i];
1778 writeBitsReversed(writer, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val));
1779 if(val > 256) /*for a length code, 3 more things have to be added*/ {
1780 unsigned length_index = val - FIRST_LENGTH_CODE_INDEX;
1781 unsigned n_length_extra_bits = LENGTHEXTRA[length_index];
1782 unsigned length_extra_bits = lz77_encoded->data[++i];
1783
1784 unsigned distance_code = lz77_encoded->data[++i];
1785
1786 unsigned distance_index = distance_code;
1787 unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index];
1788 unsigned distance_extra_bits = lz77_encoded->data[++i];
1789
1790 writeBits(writer, length_extra_bits, n_length_extra_bits);
1791 writeBitsReversed(writer, HuffmanTree_getCode(tree_d, distance_code),
1792 HuffmanTree_getLength(tree_d, distance_code));
1793 writeBits(writer, distance_extra_bits, n_distance_extra_bits);
1794 }
1795 }
1796 }
1797
1798 /*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/
1799 static unsigned deflateDynamic(LodePNGBitWriter* writer, Hash* hash,
1800 const unsigned char* data, size_t datapos, size_t dataend,
1801 const LodePNGCompressSettings* settings, unsigned final) {
1802 unsigned error = 0;
1803
1804 /*
1805 A block is compressed as follows: The PNG data is lz77 encoded, resulting in
1806 literal bytes and length/distance pairs. This is then huffman compressed with
1807 two huffman trees. One huffman tree is used for the lit and len values ("ll"),
1808 another huffman tree is used for the dist values ("d"). These two trees are
1809 stored using their code lengths, and to compress even more these code lengths
1810 are also run-length encoded and huffman compressed. This gives a huffman tree
1811 of code lengths "cl". The code lengths used to describe this third tree are
1812 the code length code lengths ("clcl").
1813 */
1814
1815 /*The lz77 encoded data, represented with integers since there will also be length and distance codes in it*/
1816 uivector lz77_encoded;
1817 HuffmanTree tree_ll; /*tree for lit,len values*/
1818 HuffmanTree tree_d; /*tree for distance codes*/
1819 HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/
1820 uivector frequencies_ll; /*frequency of lit,len codes*/
1821 uivector frequencies_d; /*frequency of dist codes*/
1822 uivector frequencies_cl; /*frequency of code length codes*/
1823 uivector bitlen_lld; /*lit,len,dist code lengths (int bits), literally (without repeat codes).*/
1824 uivector bitlen_lld_e; /*bitlen_lld encoded with repeat codes (this is a rudimentary run length compression)*/
1825 /*bitlen_cl is the code length code lengths ("clcl"). The bit lengths of codes to represent tree_cl
1826 (these are written as is in the file, it would be crazy to compress these using yet another huffman
1827 tree that needs to be represented by yet another set of code lengths)*/
1828 uivector bitlen_cl;
1829 size_t datasize = dataend - datapos;
1830
1831 /*
1832 Due to the huffman compression of huffman tree representations ("two levels"), there are some analogies:
1833 bitlen_lld is to tree_cl what data is to tree_ll and tree_d.
1834 bitlen_lld_e is to bitlen_lld what lz77_encoded is to data.
1835 bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded.
1836 */
1837
1838 unsigned BFINAL = final;
1839 size_t numcodes_ll, numcodes_d, i;
1840 unsigned HLIT, HDIST, HCLEN;
1841
1842 uivector_init(&lz77_encoded);
1843 HuffmanTree_init(&tree_ll);
1844 HuffmanTree_init(&tree_d);
1845 HuffmanTree_init(&tree_cl);
1846 uivector_init(&frequencies_ll);
1847 uivector_init(&frequencies_d);
1848 uivector_init(&frequencies_cl);
1849 uivector_init(&bitlen_lld);
1850 uivector_init(&bitlen_lld_e);
1851 uivector_init(&bitlen_cl);
1852
1853 /*This while loop never loops due to a break at the end, it is here to
1854 allow breaking out of it to the cleanup phase on error conditions.*/
1855 while(!error) {
1856 if(settings->use_lz77) {
1857 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize,
1858 settings->minmatch, settings->nicematch, settings->lazymatching);
1859 if(error) break;
1860 } else {
1861 if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83 /*alloc fail*/);
1862 for(i = datapos; i < dataend; ++i) lz77_encoded.data[i - datapos] = data[i]; /*no LZ77, but still will be Huffman compressed*/
1863 }
1864
1865 if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83 /*alloc fail*/);
1866 if(!uivector_resizev(&frequencies_d, 30, 0)) ERROR_BREAK(83 /*alloc fail*/);
1867
1868 /*Count the frequencies of lit, len and dist codes*/
1869 for(i = 0; i != lz77_encoded.size; ++i) {
1870 unsigned symbol = lz77_encoded.data[i];
1871 ++frequencies_ll.data[symbol];
1872 if(symbol > 256) {
1873 unsigned dist = lz77_encoded.data[i + 2];
1874 ++frequencies_d.data[dist];
1875 i += 3;
1876 }
1877 }
1878 frequencies_ll.data[256] = 1; /*there will be exactly 1 end code, at the end of the block*/
1879
1880 /*Make both huffman trees, one for the lit and len codes, one for the dist codes*/
1881 error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll.data, 257, frequencies_ll.size, 15);
1882 if(error) break;
1883 /*2, not 1, is chosen for mincodes: some buggy PNG decoders require at least 2 symbols in the dist tree*/
1884 error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d.data, 2, frequencies_d.size, 15);
1885 if(error) break;
1886
1887 numcodes_ll = tree_ll.numcodes; if(numcodes_ll > 286) numcodes_ll = 286;
1888 numcodes_d = tree_d.numcodes; if(numcodes_d > 30) numcodes_d = 30;
1889 /*store the code lengths of both generated trees in bitlen_lld*/
1890 for(i = 0; i != numcodes_ll; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (unsigned)i));
1891 for(i = 0; i != numcodes_d; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (unsigned)i));
1892
1893 /*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times),
1894 17 (3-10 zeroes), 18 (11-138 zeroes)*/
1895 for(i = 0; i != (unsigned)bitlen_lld.size; ++i) {
1896 unsigned j = 0; /*amount of repetitions*/
1897 while(i + j + 1 < (unsigned)bitlen_lld.size && bitlen_lld.data[i + j + 1] == bitlen_lld.data[i]) ++j;
1898
1899 if(bitlen_lld.data[i] == 0 && j >= 2) /*repeat code for zeroes*/ {
1900 ++j; /*include the first zero*/
1901 if(j <= 10) /*repeat code 17 supports max 10 zeroes*/ {
1902 uivector_push_back(&bitlen_lld_e, 17);
1903 uivector_push_back(&bitlen_lld_e, j - 3);
1904 } else /*repeat code 18 supports max 138 zeroes*/ {
1905 if(j > 138) j = 138;
1906 uivector_push_back(&bitlen_lld_e, 18);
1907 uivector_push_back(&bitlen_lld_e, j - 11);
1908 }
1909 i += (j - 1);
1910 } else if(j >= 3) /*repeat code for value other than zero*/ {
1911 size_t k;
1912 unsigned num = j / 6u, rest = j % 6u;
1913 uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]);
1914 for(k = 0; k < num; ++k) {
1915 uivector_push_back(&bitlen_lld_e, 16);
1916 uivector_push_back(&bitlen_lld_e, 6 - 3);
1917 }
1918 if(rest >= 3) {
1919 uivector_push_back(&bitlen_lld_e, 16);
1920 uivector_push_back(&bitlen_lld_e, rest - 3);
1921 }
1922 else j -= rest;
1923 i += j;
1924 } else /*too short to benefit from repeat code*/ {
1925 uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]);
1926 }
1927 }
1928
1929 /*generate tree_cl, the huffmantree of huffmantrees*/
1930
1931 if(!uivector_resizev(&frequencies_cl, NUM_CODE_LENGTH_CODES, 0)) ERROR_BREAK(83 /*alloc fail*/);
1932 for(i = 0; i != bitlen_lld_e.size; ++i) {
1933 ++frequencies_cl.data[bitlen_lld_e.data[i]];
1934 /*after a repeat code come the bits that specify the number of repetitions,
1935 those don't need to be in the frequencies_cl calculation*/
1936 if(bitlen_lld_e.data[i] >= 16) ++i;
1937 }
1938
1939 error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl.data,
1940 frequencies_cl.size, frequencies_cl.size, 7);
1941 if(error) break;
1942
1943 if(!uivector_resize(&bitlen_cl, tree_cl.numcodes)) ERROR_BREAK(83 /*alloc fail*/);
1944 for(i = 0; i != tree_cl.numcodes; ++i) {
1945 /*lengths of code length tree is in the order as specified by deflate*/
1946 bitlen_cl.data[i] = HuffmanTree_getLength(&tree_cl, CLCL_ORDER[i]);
1947 }
1948 while(bitlen_cl.data[bitlen_cl.size - 1] == 0 && bitlen_cl.size > 4) {
1949 /*remove zeros at the end, but minimum size must be 4*/
1950 if(!uivector_resize(&bitlen_cl, bitlen_cl.size - 1)) ERROR_BREAK(83 /*alloc fail*/);
1951 }
1952 if(error) break;
1953
1954 /*
1955 Write everything into the output
1956
1957 After the BFINAL and BTYPE, the dynamic block consists out of the following:
1958 - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN
1959 - (HCLEN+4)*3 bits code lengths of code length alphabet
1960 - HLIT + 257 code lengths of lit/length alphabet (encoded using the code length
1961 alphabet, + possible repetition codes 16, 17, 18)
1962 - HDIST + 1 code lengths of distance alphabet (encoded using the code length
1963 alphabet, + possible repetition codes 16, 17, 18)
1964 - compressed data
1965 - 256 (end code)
1966 */
1967
1968 /*Write block type*/
1969 writeBits(writer, BFINAL, 1);
1970 writeBits(writer, 0, 1); /*first bit of BTYPE "dynamic"*/
1971 writeBits(writer, 1, 1); /*second bit of BTYPE "dynamic"*/
1972
1973 /*write the HLIT, HDIST and HCLEN values*/
1974 HLIT = (unsigned)(numcodes_ll - 257);
1975 HDIST = (unsigned)(numcodes_d - 1);
1976 HCLEN = (unsigned)bitlen_cl.size - 4;
1977 /*trim zeroes for HCLEN. HLIT and HDIST were already trimmed at tree creation*/
1978 while(!bitlen_cl.data[HCLEN + 4 - 1] && HCLEN > 0) --HCLEN;
1979 writeBits(writer, HLIT, 5);
1980 writeBits(writer, HDIST, 5);
1981 writeBits(writer, HCLEN, 4);
1982
1983 /*write the code lengths of the code length alphabet*/
1984 for(i = 0; i != HCLEN + 4; ++i) writeBits(writer, bitlen_cl.data[i], 3);
1985
1986 /*write the lengths of the lit/len AND the dist alphabet*/
1987 for(i = 0; i != bitlen_lld_e.size; ++i) {
1988 writeBitsReversed(writer, HuffmanTree_getCode(&tree_cl, bitlen_lld_e.data[i]),
1989 HuffmanTree_getLength(&tree_cl, bitlen_lld_e.data[i]));
1990 /*extra bits of repeat codes*/
1991 if(bitlen_lld_e.data[i] == 16) writeBits(writer, bitlen_lld_e.data[++i], 2);
1992 else if(bitlen_lld_e.data[i] == 17) writeBits(writer, bitlen_lld_e.data[++i], 3);
1993 else if(bitlen_lld_e.data[i] == 18) writeBits(writer, bitlen_lld_e.data[++i], 7);
1994 }
1995
1996 /*write the compressed data symbols*/
1997 writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d);
1998 /*error: the length of the end code 256 must be larger than 0*/
1999 if(HuffmanTree_getLength(&tree_ll, 256) == 0) ERROR_BREAK(64);
2000
2001 /*write the end code*/
2002 writeBitsReversed(writer, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256));
2003
2004 break; /*end of error-while*/
2005 }
2006
2007 /*cleanup*/
2008 uivector_cleanup(&lz77_encoded);
2009 HuffmanTree_cleanup(&tree_ll);
2010 HuffmanTree_cleanup(&tree_d);
2011 HuffmanTree_cleanup(&tree_cl);
2012 uivector_cleanup(&frequencies_ll);
2013 uivector_cleanup(&frequencies_d);
2014 uivector_cleanup(&frequencies_cl);
2015 uivector_cleanup(&bitlen_lld_e);
2016 uivector_cleanup(&bitlen_lld);
2017 uivector_cleanup(&bitlen_cl);
2018
2019 return error;
2020 }
2021
2022 static unsigned deflateFixed(LodePNGBitWriter* writer, Hash* hash,
2023 const unsigned char* data,
2024 size_t datapos, size_t dataend,
2025 const LodePNGCompressSettings* settings, unsigned final) {
2026 HuffmanTree tree_ll; /*tree for literal values and length codes*/
2027 HuffmanTree tree_d; /*tree for distance codes*/
2028
2029 unsigned BFINAL = final;
2030 unsigned error = 0;
2031 size_t i;
2032
2033 HuffmanTree_init(&tree_ll);
2034 HuffmanTree_init(&tree_d);
2035
2036 generateFixedLitLenTree(&tree_ll);
2037 generateFixedDistanceTree(&tree_d);
2038
2039 writeBits(writer, BFINAL, 1);
2040 writeBits(writer, 1, 1); /*first bit of BTYPE*/
2041 writeBits(writer, 0, 1); /*second bit of BTYPE*/
2042
2043 if(settings->use_lz77) /*LZ77 encoded*/ {
2044 uivector lz77_encoded;
2045 uivector_init(&lz77_encoded);
2046 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize,
2047 settings->minmatch, settings->nicematch, settings->lazymatching);
2048 if(!error) writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d);
2049 uivector_cleanup(&lz77_encoded);
2050 } else /*no LZ77, but still will be Huffman compressed*/ {
2051 for(i = datapos; i < dataend; ++i) {
2052 writeBitsReversed(writer, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i]));
2053 }
2054 }
2055 /*add END code*/
2056 if(!error) writeBitsReversed(writer, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256));
2057
2058 /*cleanup*/
2059 HuffmanTree_cleanup(&tree_ll);
2060 HuffmanTree_cleanup(&tree_d);
2061
2062 return error;
2063 }
2064
2065 static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t insize,
2066 const LodePNGCompressSettings* settings) {
2067 unsigned error = 0;
2068 size_t i, blocksize, numdeflateblocks;
2069 Hash hash;
2070 LodePNGBitWriter writer;
2071
2072 LodePNGBitWriter_init(&writer, out);
2073
2074 if(settings->btype > 2) return 61;
2075 else if(settings->btype == 0) return deflateNoCompression(out, in, insize);
2076 else if(settings->btype == 1) blocksize = insize;
2077 else /*if(settings->btype == 2)*/ {
2078 /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/
2079 blocksize = insize / 8u + 8;
2080 if(blocksize < 65536) blocksize = 65536;
2081 if(blocksize > 262144) blocksize = 262144;
2082 }
2083
2084 numdeflateblocks = (insize + blocksize - 1) / blocksize;
2085 if(numdeflateblocks == 0) numdeflateblocks = 1;
2086
2087 error = hash_init(&hash, settings->windowsize);
2088 if(error) return error;
2089
2090 for(i = 0; i != numdeflateblocks && !error; ++i) {
2091 unsigned final = (i == numdeflateblocks - 1);
2092 size_t start = i * blocksize;
2093 size_t end = start + blocksize;
2094 if(end > insize) end = insize;
2095
2096 if(settings->btype == 1) error = deflateFixed(&writer, &hash, in, start, end, settings, final);
2097 else if(settings->btype == 2) error = deflateDynamic(&writer, &hash, in, start, end, settings, final);
2098 }
2099
2100 hash_cleanup(&hash);
2101
2102 return error;
2103 }
2104
2105 unsigned lodepng_deflate(unsigned char** out, size_t* outsize,
2106 const unsigned char* in, size_t insize,
2107 const LodePNGCompressSettings* settings) {
2108 unsigned error;
2109 ucvector v;
2110 ucvector_init_buffer(&v, *out, *outsize);
2111 error = lodepng_deflatev(&v, in, insize, settings);
2112 *out = v.data;
2113 *outsize = v.size;
2114 return error;
2115 }
2116
2117 static unsigned deflate(unsigned char** out, size_t* outsize,
2118 const unsigned char* in, size_t insize,
2119 const LodePNGCompressSettings* settings) {
2120 if(settings->custom_deflate) {
2121 return settings->custom_deflate(out, outsize, in, insize, settings);
2122 } else {
2123 return lodepng_deflate(out, outsize, in, insize, settings);
2124 }
2125 }
2126
2127 #endif /*LODEPNG_COMPILE_DECODER*/
2128
2129 /* ////////////////////////////////////////////////////////////////////////// */
2130 /* / Adler32 / */
2131 /* ////////////////////////////////////////////////////////////////////////// */
2132
2133 static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len) {
2134 unsigned s1 = adler & 0xffffu;
2135 unsigned s2 = (adler >> 16u) & 0xffffu;
2136
2137 while(len != 0u) {
2138 unsigned i;
2139 /*at least 5552 sums can be done before the sums overflow, saving a lot of module divisions*/
2140 unsigned amount = len > 5552u ? 5552u : len;
2141 len -= amount;
2142 for(i = 0; i != amount; ++i) {
2143 s1 += (*data++);
2144 s2 += s1;
2145 }
2146 s1 %= 65521u;
2147 s2 %= 65521u;
2148 }
2149
2150 return (s2 << 16u) | s1;
2151 }
2152
2153 /*Return the adler32 of the bytes data[0..len-1]*/
2154 static unsigned adler32(const unsigned char* data, unsigned len) {
2155 return update_adler32(1u, data, len);
2156 }
2157
2158 /* ////////////////////////////////////////////////////////////////////////// */
2159 /* / Zlib / */
2160 /* ////////////////////////////////////////////////////////////////////////// */
2161
2162 #ifdef LODEPNG_COMPILE_DECODER
2163
2164 unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
2165 size_t insize, const LodePNGDecompressSettings* settings) {
2166 unsigned error = 0;
2167 unsigned CM, CINFO, FDICT;
2168
2169 if(insize < 2) return 53; /*error, size of zlib data too small*/
2170 /*read information from zlib header*/
2171 if((in[0] * 256 + in[1]) % 31 != 0) {
2172 /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/
2173 return 24;
2174 }
2175
2176 CM = in[0] & 15;
2177 CINFO = (in[0] >> 4) & 15;
2178 /*FCHECK = in[1] & 31;*/ /*FCHECK is already tested above*/
2179 FDICT = (in[1] >> 5) & 1;
2180 /*FLEVEL = (in[1] >> 6) & 3;*/ /*FLEVEL is not used here*/
2181
2182 if(CM != 8 || CINFO > 7) {
2183 /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/
2184 return 25;
2185 }
2186 if(FDICT != 0) {
2187 /*error: the specification of PNG says about the zlib stream:
2188 "The additional flags shall not specify a preset dictionary."*/
2189 return 26;
2190 }
2191
2192 error = inflate(out, outsize, in + 2, insize - 2, settings);
2193 if(error) return error;
2194
2195 if(!settings->ignore_adler32) {
2196 unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]);
2197 unsigned checksum = adler32(*out, (unsigned)(*outsize));
2198 if(checksum != ADLER32) return 58; /*error, adler checksum not correct, data must be corrupted*/
2199 }
2200
2201 return 0; /*no error*/
2202 }
2203
2204 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
2205 size_t insize, const LodePNGDecompressSettings* settings) {
2206 if(settings->custom_zlib) {
2207 return settings->custom_zlib(out, outsize, in, insize, settings);
2208 } else {
2209 return lodepng_zlib_decompress(out, outsize, in, insize, settings);
2210 }
2211 }
2212
2213 #endif /*LODEPNG_COMPILE_DECODER*/
2214
2215 #ifdef LODEPNG_COMPILE_ENCODER
2216
2217 unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
2218 size_t insize, const LodePNGCompressSettings* settings) {
2219 size_t i;
2220 unsigned error;
2221 unsigned char* deflatedata = 0;
2222 size_t deflatesize = 0;
2223
2224 error = deflate(&deflatedata, &deflatesize, in, insize, settings);
2225
2226 *out = NULL;
2227 *outsize = 0;
2228 if(!error) {
2229 *outsize = deflatesize + 6;
2230 *out = (unsigned char*)lodepng_malloc(*outsize);
2231 if(!out) error = 83; /*alloc fail*/
2232 }
2233
2234 if(!error) {
2235 unsigned ADLER32 = adler32(in, (unsigned)insize);
2236 /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
2237 unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
2238 unsigned FLEVEL = 0;
2239 unsigned FDICT = 0;
2240 unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
2241 unsigned FCHECK = 31 - CMFFLG % 31;
2242 CMFFLG += FCHECK;
2243
2244 (*out)[0] = (unsigned char)(CMFFLG >> 8);
2245 (*out)[1] = (unsigned char)(CMFFLG & 255);
2246 for(i = 0; i != deflatesize; ++i) (*out)[i + 2] = deflatedata[i];
2247 lodepng_set32bitInt(&(*out)[*outsize - 4], ADLER32);
2248 }
2249
2250 lodepng_free(deflatedata);
2251 return error;
2252 }
2253
2254 /* compress using the default or custom zlib function */
2255 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
2256 size_t insize, const LodePNGCompressSettings* settings) {
2257 if(settings->custom_zlib) {
2258 return settings->custom_zlib(out, outsize, in, insize, settings);
2259 } else {
2260 return lodepng_zlib_compress(out, outsize, in, insize, settings);
2261 }
2262 }
2263
2264 #endif /*LODEPNG_COMPILE_ENCODER*/
2265
2266 #else /*no LODEPNG_COMPILE_ZLIB*/
2267
2268 #ifdef LODEPNG_COMPILE_DECODER
2269 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
2270 size_t insize, const LodePNGDecompressSettings* settings) {
2271 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */
2272 return settings->custom_zlib(out, outsize, in, insize, settings);
2273 }
2274 #endif /*LODEPNG_COMPILE_DECODER*/
2275 #ifdef LODEPNG_COMPILE_ENCODER
2276 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
2277 size_t insize, const LodePNGCompressSettings* settings) {
2278 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */
2279 return settings->custom_zlib(out, outsize, in, insize, settings);
2280 }
2281 #endif /*LODEPNG_COMPILE_ENCODER*/
2282
2283 #endif /*LODEPNG_COMPILE_ZLIB*/
2284
2285 /* ////////////////////////////////////////////////////////////////////////// */
2286
2287 #ifdef LODEPNG_COMPILE_ENCODER
2288
2289 /*this is a good tradeoff between speed and compression ratio*/
2290 #define DEFAULT_WINDOWSIZE 2048
2291
2292 void lodepng_compress_settings_init(LodePNGCompressSettings* settings) {
2293 /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/
2294 settings->btype = 2;
2295 settings->use_lz77 = 1;
2296 settings->windowsize = DEFAULT_WINDOWSIZE;
2297 settings->minmatch = 3;
2298 settings->nicematch = 128;
2299 settings->lazymatching = 1;
2300
2301 settings->custom_zlib = 0;
2302 settings->custom_deflate = 0;
2303 settings->custom_context = 0;
2304 }
2305
2306 const LodePNGCompressSettings lodepng_default_compress_settings = {2, 1, DEFAULT_WINDOWSIZE, 3, 128, 1, 0, 0, 0};
2307
2308
2309 #endif /*LODEPNG_COMPILE_ENCODER*/
2310
2311 #ifdef LODEPNG_COMPILE_DECODER
2312
2313 void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings) {
2314 settings->ignore_adler32 = 0;
2315 settings->ignore_nlen = 0;
2316
2317 settings->custom_zlib = 0;
2318 settings->custom_inflate = 0;
2319 settings->custom_context = 0;
2320 }
2321
2322 const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0, 0, 0};
2323
2324 #endif /*LODEPNG_COMPILE_DECODER*/
2325
2326 /* ////////////////////////////////////////////////////////////////////////// */
2327 /* ////////////////////////////////////////////////////////////////////////// */
2328 /* // End of Zlib related code. Begin of PNG related code. // */
2329 /* ////////////////////////////////////////////////////////////////////////// */
2330 /* ////////////////////////////////////////////////////////////////////////// */
2331
2332 #ifdef LODEPNG_COMPILE_PNG
2333
2334 /* ////////////////////////////////////////////////////////////////////////// */
2335 /* / CRC32 / */
2336 /* ////////////////////////////////////////////////////////////////////////// */
2337
2338
2339 #ifndef LODEPNG_NO_COMPILE_CRC
2340 /* CRC polynomial: 0xedb88320 */
2341 static unsigned lodepng_crc32_table[256] = {
2342 0u, 1996959894u, 3993919788u, 2567524794u, 124634137u, 1886057615u, 3915621685u, 2657392035u,
2343 249268274u, 2044508324u, 3772115230u, 2547177864u, 162941995u, 2125561021u, 3887607047u, 2428444049u,
2344 498536548u, 1789927666u, 4089016648u, 2227061214u, 450548861u, 1843258603u, 4107580753u, 2211677639u,
2345 325883990u, 1684777152u, 4251122042u, 2321926636u, 335633487u, 1661365465u, 4195302755u, 2366115317u,
2346 997073096u, 1281953886u, 3579855332u, 2724688242u, 1006888145u, 1258607687u, 3524101629u, 2768942443u,
2347 901097722u, 1119000684u, 3686517206u, 2898065728u, 853044451u, 1172266101u, 3705015759u, 2882616665u,
2348 651767980u, 1373503546u, 3369554304u, 3218104598u, 565507253u, 1454621731u, 3485111705u, 3099436303u,
2349 671266974u, 1594198024u, 3322730930u, 2970347812u, 795835527u, 1483230225u, 3244367275u, 3060149565u,
2350 1994146192u, 31158534u, 2563907772u, 4023717930u, 1907459465u, 112637215u, 2680153253u, 3904427059u,
2351 2013776290u, 251722036u, 2517215374u, 3775830040u, 2137656763u, 141376813u, 2439277719u, 3865271297u,
2352 1802195444u, 476864866u, 2238001368u, 4066508878u, 1812370925u, 453092731u, 2181625025u, 4111451223u,
2353 1706088902u, 314042704u, 2344532202u, 4240017532u, 1658658271u, 366619977u, 2362670323u, 4224994405u,
2354 1303535960u, 984961486u, 2747007092u, 3569037538u, 1256170817u, 1037604311u, 2765210733u, 3554079995u,
2355 1131014506u, 879679996u, 2909243462u, 3663771856u, 1141124467u, 855842277u, 2852801631u, 3708648649u,
2356 1342533948u, 654459306u, 3188396048u, 3373015174u, 1466479909u, 544179635u, 3110523913u, 3462522015u,
2357 1591671054u, 702138776u, 2966460450u, 3352799412u, 1504918807u, 783551873u, 3082640443u, 3233442989u,
2358 3988292384u, 2596254646u, 62317068u, 1957810842u, 3939845945u, 2647816111u, 81470997u, 1943803523u,
2359 3814918930u, 2489596804u, 225274430u, 2053790376u, 3826175755u, 2466906013u, 167816743u, 2097651377u,
2360 4027552580u, 2265490386u, 503444072u, 1762050814u, 4150417245u, 2154129355u, 426522225u, 1852507879u,
2361 4275313526u, 2312317920u, 282753626u, 1742555852u, 4189708143u, 2394877945u, 397917763u, 1622183637u,
2362 3604390888u, 2714866558u, 953729732u, 1340076626u, 3518719985u, 2797360999u, 1068828381u, 1219638859u,
2363 3624741850u, 2936675148u, 906185462u, 1090812512u, 3747672003u, 2825379669u, 829329135u, 1181335161u,
2364 3412177804u, 3160834842u, 628085408u, 1382605366u, 3423369109u, 3138078467u, 570562233u, 1426400815u,
2365 3317316542u, 2998733608u, 733239954u, 1555261956u, 3268935591u, 3050360625u, 752459403u, 1541320221u,
2366 2607071920u, 3965973030u, 1969922972u, 40735498u, 2617837225u, 3943577151u, 1913087877u, 83908371u,
2367 2512341634u, 3803740692u, 2075208622u, 213261112u, 2463272603u, 3855990285u, 2094854071u, 198958881u,
2368 2262029012u, 4057260610u, 1759359992u, 534414190u, 2176718541u, 4139329115u, 1873836001u, 414664567u,
2369 2282248934u, 4279200368u, 1711684554u, 285281116u, 2405801727u, 4167216745u, 1634467795u, 376229701u,
2370 2685067896u, 3608007406u, 1308918612u, 956543938u, 2808555105u, 3495958263u, 1231636301u, 1047427035u,
2371 2932959818u, 3654703836u, 1088359270u, 936918000u, 2847714899u, 3736837829u, 1202900863u, 817233897u,
2372 3183342108u, 3401237130u, 1404277552u, 615818150u, 3134207493u, 3453421203u, 1423857449u, 601450431u,
2373 3009837614u, 3294710456u, 1567103746u, 711928724u, 3020668471u, 3272380065u, 1510334235u, 755167117u
2374 };
2375
2376 /*Return the CRC of the bytes buf[0..len-1].*/
2377 unsigned lodepng_crc32(const unsigned char* data, size_t length) {
2378 unsigned r = 0xffffffffu;
2379 size_t i;
2380 for(i = 0; i < length; ++i) {
2381 r = lodepng_crc32_table[(r ^ data[i]) & 0xffu] ^ (r >> 8u);
2382 }
2383 return r ^ 0xffffffffu;
2384 }
2385 #else /* !LODEPNG_NO_COMPILE_CRC */
2386 unsigned lodepng_crc32(const unsigned char* data, size_t length);
2387 #endif /* !LODEPNG_NO_COMPILE_CRC */
2388
2389 /* ////////////////////////////////////////////////////////////////////////// */
2390 /* / Reading and writing PNG color channel bits / */
2391 /* ////////////////////////////////////////////////////////////////////////// */
2392
2393 /* The color channel bits of less-than-8-bit pixels are read with the MSB of bytes first,
2394 so LodePNGBitWriter and LodePNGBitReader can't be used for those. */
2395
2396 static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream) {
2397 unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
2398 ++(*bitpointer);
2399 return result;
2400 }
2401
2402 /* TODO: make this faster */
2403 static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) {
2404 unsigned result = 0;
2405 size_t i;
2406 for(i = 0 ; i < nbits; ++i) {
2407 result <<= 1;
2408 result |= (unsigned)readBitFromReversedStream(bitpointer, bitstream);
2409 }
2410 return result;
2411 }
2412
2413 static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) {
2414 /*the current bit in bitstream may be 0 or 1 for this to work*/
2415 if(bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7))));
2416 else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7)));
2417 ++(*bitpointer);
2418 }
2419
2420 /* ////////////////////////////////////////////////////////////////////////// */
2421 /* / PNG chunks / */
2422 /* ////////////////////////////////////////////////////////////////////////// */
2423
2424 unsigned lodepng_chunk_length(const unsigned char* chunk) {
2425 return lodepng_read32bitInt(&chunk[0]);
2426 }
2427
2428 void lodepng_chunk_type(char type[5], const unsigned char* chunk) {
2429 unsigned i;
2430 for(i = 0; i != 4; ++i) type[i] = (char)chunk[4 + i];
2431 type[4] = 0; /*null termination char*/
2432 }
2433
2434 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type) {
2435 if(lodepng_strlen(type) != 4) return 0;
2436 return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
2437 }
2438
2439 unsigned char lodepng_chunk_ancillary(const unsigned char* chunk) {
2440 return((chunk[4] & 32) != 0);
2441 }
2442
2443 unsigned char lodepng_chunk_private(const unsigned char* chunk) {
2444 return((chunk[6] & 32) != 0);
2445 }
2446
2447 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk) {
2448 return((chunk[7] & 32) != 0);
2449 }
2450
2451 unsigned char* lodepng_chunk_data(unsigned char* chunk) {
2452 return &chunk[8];
2453 }
2454
2455 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk) {
2456 return &chunk[8];
2457 }
2458
2459 unsigned lodepng_chunk_check_crc(const unsigned char* chunk) {
2460 unsigned length = lodepng_chunk_length(chunk);
2461 unsigned CRC = lodepng_read32bitInt(&chunk[length + 8]);
2462 /*the CRC is taken of the data and the 4 chunk type letters, not the length*/
2463 unsigned checksum = lodepng_crc32(&chunk[4], length + 4);
2464 if(CRC != checksum) return 1;
2465 else return 0;
2466 }
2467
2468 void lodepng_chunk_generate_crc(unsigned char* chunk) {
2469 unsigned length = lodepng_chunk_length(chunk);
2470 unsigned CRC = lodepng_crc32(&chunk[4], length + 4);
2471 lodepng_set32bitInt(chunk + 8 + length, CRC);
2472 }
2473
2474 unsigned char* lodepng_chunk_next(unsigned char* chunk) {
2475 if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47
2476 && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) {
2477 /* Is PNG magic header at start of PNG file. Jump to first actual chunk. */
2478 return chunk + 8;
2479 } else {
2480 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12;
2481 return chunk + total_chunk_length;
2482 }
2483 }
2484
2485 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk) {
2486 if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47
2487 && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) {
2488 /* Is PNG magic header at start of PNG file. Jump to first actual chunk. */
2489 return chunk + 8;
2490 } else {
2491 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12;
2492 return chunk + total_chunk_length;
2493 }
2494 }
2495
2496 unsigned char* lodepng_chunk_find(unsigned char* chunk, const unsigned char* end, const char type[5]) {
2497 for(;;) {
2498 if(chunk + 12 >= end) return 0;
2499 if(lodepng_chunk_type_equals(chunk, type)) return chunk;
2500 chunk = lodepng_chunk_next(chunk);
2501 }
2502 }
2503
2504 const unsigned char* lodepng_chunk_find_const(const unsigned char* chunk, const unsigned char* end, const char type[5]) {
2505 for(;;) {
2506 if(chunk + 12 >= end) return 0;
2507 if(lodepng_chunk_type_equals(chunk, type)) return chunk;
2508 chunk = lodepng_chunk_next_const(chunk);
2509 }
2510 }
2511
2512 unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk) {
2513 unsigned i;
2514 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12;
2515 unsigned char *chunk_start, *new_buffer;
2516 size_t new_length = (*outlength) + total_chunk_length;
2517 if(new_length < total_chunk_length || new_length < (*outlength)) return 77; /*integer overflow happened*/
2518
2519 new_buffer = (unsigned char*)lodepng_realloc(*out, new_length);
2520 if(!new_buffer) return 83; /*alloc fail*/
2521 (*out) = new_buffer;
2522 (*outlength) = new_length;
2523 chunk_start = &(*out)[new_length - total_chunk_length];
2524
2525 for(i = 0; i != total_chunk_length; ++i) chunk_start[i] = chunk[i];
2526
2527 return 0;
2528 }
2529
2530 unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length,
2531 const char* type, const unsigned char* data) {
2532 unsigned i;
2533 unsigned char *chunk, *new_buffer;
2534 size_t new_length = (*outlength) + length + 12;
2535 if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/
2536 new_buffer = (unsigned char*)lodepng_realloc(*out, new_length);
2537 if(!new_buffer) return 83; /*alloc fail*/
2538 (*out) = new_buffer;
2539 (*outlength) = new_length;
2540 chunk = &(*out)[(*outlength) - length - 12];
2541
2542 /*1: length*/
2543 lodepng_set32bitInt(chunk, (unsigned)length);
2544
2545 /*2: chunk name (4 letters)*/
2546 chunk[4] = (unsigned char)type[0];
2547 chunk[5] = (unsigned char)type[1];
2548 chunk[6] = (unsigned char)type[2];
2549 chunk[7] = (unsigned char)type[3];
2550
2551 /*3: the data*/
2552 for(i = 0; i != length; ++i) chunk[8 + i] = data[i];
2553
2554 /*4: CRC (of the chunkname characters and the data)*/
2555 lodepng_chunk_generate_crc(chunk);
2556
2557 return 0;
2558 }
2559
2560 /* ////////////////////////////////////////////////////////////////////////// */
2561 /* / Color types, channels, bits / */
2562 /* ////////////////////////////////////////////////////////////////////////// */
2563
2564 /*checks if the colortype is valid and the bitdepth bd is allowed for this colortype.
2565 Return value is a LodePNG error code.*/
2566 static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) {
2567 switch(colortype) {
2568 case LCT_GREY: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break;
2569 case LCT_RGB: if(!( bd == 8 || bd == 16)) return 37; break;
2570 case LCT_PALETTE: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break;
2571 case LCT_GREY_ALPHA: if(!( bd == 8 || bd == 16)) return 37; break;
2572 case LCT_RGBA: if(!( bd == 8 || bd == 16)) return 37; break;
2573 default: return 31; /* invalid color type */
2574 }
2575 return 0; /*allowed color type / bits combination*/
2576 }
2577
2578 static unsigned getNumColorChannels(LodePNGColorType colortype) {
2579 switch(colortype) {
2580 case LCT_GREY: return 1;
2581 case LCT_RGB: return 3;
2582 case LCT_PALETTE: return 1;
2583 case LCT_GREY_ALPHA: return 2;
2584 case LCT_RGBA: return 4;
2585 default: return 0; /*invalid color type*/
2586 }
2587 }
2588
2589 static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth) {
2590 /*bits per pixel is amount of channels * bits per channel*/
2591 return getNumColorChannels(colortype) * bitdepth;
2592 }
2593
2594 /* ////////////////////////////////////////////////////////////////////////// */
2595
2596 void lodepng_color_mode_init(LodePNGColorMode* info) {
2597 info->key_defined = 0;
2598 info->key_r = info->key_g = info->key_b = 0;
2599 info->colortype = LCT_RGBA;
2600 info->bitdepth = 8;
2601 info->palette = 0;
2602 info->palettesize = 0;
2603 }
2604
2605 void lodepng_color_mode_alloc_palette(LodePNGColorMode* info) {
2606 size_t i;
2607 /*room for 256 colors with 4 bytes each. Using realloc to avoid leak if it is being overwritten*/
2608 info->palette = (unsigned char*)lodepng_realloc(info->palette, 1024);
2609 if(!info->palette) return; /*alloc fail*/
2610 for(i = 0; i != 256; ++i) {
2611 /*Initialize all unused colors with black, the value used for invalid palette indices.
2612 This is an error according to the PNG spec, but common PNG decoders make it black instead.
2613 That makes color conversion slightly faster due to no error handling needed.*/
2614 info->palette[i * 4 + 0] = 0;
2615 info->palette[i * 4 + 1] = 0;
2616 info->palette[i * 4 + 2] = 0;
2617 info->palette[i * 4 + 3] = 255;
2618 }
2619 }
2620
2621 void lodepng_color_mode_cleanup(LodePNGColorMode* info) {
2622 lodepng_palette_clear(info);
2623 }
2624
2625 unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source) {
2626 size_t i;
2627 lodepng_color_mode_cleanup(dest);
2628 *dest = *source;
2629 if(source->palette) {
2630 dest->palette = (unsigned char*)lodepng_malloc(1024);
2631 if(!dest->palette && source->palettesize) return 83; /*alloc fail*/
2632 for(i = 0; i != source->palettesize * 4; ++i) dest->palette[i] = source->palette[i];
2633 }
2634 return 0;
2635 }
2636
2637 LodePNGColorMode lodepng_color_mode_make(LodePNGColorType colortype, unsigned bitdepth) {
2638 LodePNGColorMode result;
2639 lodepng_color_mode_init(&result);
2640 result.colortype = colortype;
2641 result.bitdepth = bitdepth;
2642 return result;
2643 }
2644
2645 static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b) {
2646 size_t i;
2647 if(a->colortype != b->colortype) return 0;
2648 if(a->bitdepth != b->bitdepth) return 0;
2649 if(a->key_defined != b->key_defined) return 0;
2650 if(a->key_defined) {
2651 if(a->key_r != b->key_r) return 0;
2652 if(a->key_g != b->key_g) return 0;
2653 if(a->key_b != b->key_b) return 0;
2654 }
2655 if(a->palettesize != b->palettesize) return 0;
2656 for(i = 0; i != a->palettesize * 4; ++i) {
2657 if(a->palette[i] != b->palette[i]) return 0;
2658 }
2659 return 1;
2660 }
2661
2662 void lodepng_palette_clear(LodePNGColorMode* info) {
2663 if(info->palette) lodepng_free(info->palette);
2664 info->palette = 0;
2665 info->palettesize = 0;
2666 }
2667
2668 unsigned lodepng_palette_add(LodePNGColorMode* info,
2669 unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
2670 if(!info->palette) /*allocate palette if empty*/ {
2671 lodepng_color_mode_alloc_palette(info);
2672 if(!info->palette) return 83; /*alloc fail*/
2673 }
2674 if(info->palettesize >= 256) {
2675 return 108; /*too many palette values*/
2676 }
2677 info->palette[4 * info->palettesize + 0] = r;
2678 info->palette[4 * info->palettesize + 1] = g;
2679 info->palette[4 * info->palettesize + 2] = b;
2680 info->palette[4 * info->palettesize + 3] = a;
2681 ++info->palettesize;
2682 return 0;
2683 }
2684
2685 /*calculate bits per pixel out of colortype and bitdepth*/
2686 unsigned lodepng_get_bpp(const LodePNGColorMode* info) {
2687 return lodepng_get_bpp_lct(info->colortype, info->bitdepth);
2688 }
2689
2690 unsigned lodepng_get_channels(const LodePNGColorMode* info) {
2691 return getNumColorChannels(info->colortype);
2692 }
2693
2694 unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info) {
2695 return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA;
2696 }
2697
2698 unsigned lodepng_is_alpha_type(const LodePNGColorMode* info) {
2699 return (info->colortype & 4) != 0; /*4 or 6*/
2700 }
2701
2702 unsigned lodepng_is_palette_type(const LodePNGColorMode* info) {
2703 return info->colortype == LCT_PALETTE;
2704 }
2705
2706 unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info) {
2707 size_t i;
2708 for(i = 0; i != info->palettesize; ++i) {
2709 if(info->palette[i * 4 + 3] < 255) return 1;
2710 }
2711 return 0;
2712 }
2713
2714 unsigned lodepng_can_have_alpha(const LodePNGColorMode* info) {
2715 return info->key_defined
2716 || lodepng_is_alpha_type(info)
2717 || lodepng_has_palette_alpha(info);
2718 }
2719
2720 static size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) {
2721 size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth);
2722 size_t n = (size_t)w * (size_t)h;
2723 return ((n / 8u) * bpp) + ((n & 7u) * bpp + 7u) / 8u;
2724 }
2725
2726 size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color) {
2727 return lodepng_get_raw_size_lct(w, h, color->colortype, color->bitdepth);
2728 }
2729
2730
2731 #ifdef LODEPNG_COMPILE_PNG
2732 #ifdef LODEPNG_COMPILE_DECODER
2733
2734 /*in an idat chunk, each scanline is a multiple of 8 bits, unlike the lodepng output buffer,
2735 and in addition has one extra byte per line: the filter byte. So this gives a larger
2736 result than lodepng_get_raw_size. */
2737 static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, const LodePNGColorMode* color) {
2738 size_t bpp = lodepng_get_bpp(color);
2739 /* + 1 for the filter byte, and possibly plus padding bits per line */
2740 size_t line = ((size_t)(w / 8u) * bpp) + 1u + ((w & 7u) * bpp + 7u) / 8u;
2741 return (size_t)h * line;
2742 }
2743
2744 /*Safely checks whether size_t overflow can be caused due to amount of pixels.
2745 This check is overcautious rather than precise. If this check indicates no overflow,
2746 you can safely compute in a size_t (but not an unsigned):
2747 -(size_t)w * (size_t)h * 8
2748 -amount of bytes in IDAT (including filter, padding and Adam7 bytes)
2749 -amount of bytes in raw color model
2750 Returns 1 if overflow possible, 0 if not.
2751 */
2752 static int lodepng_pixel_overflow(unsigned w, unsigned h,
2753 const LodePNGColorMode* pngcolor, const LodePNGColorMode* rawcolor) {
2754 size_t bpp = LODEPNG_MAX(lodepng_get_bpp(pngcolor), lodepng_get_bpp(rawcolor));
2755 size_t numpixels, total;
2756 size_t line; /* bytes per line in worst case */
2757
2758 if(lodepng_mulofl((size_t)w, (size_t)h, &numpixels)) return 1;
2759 if(lodepng_mulofl(numpixels, 8, &total)) return 1; /* bit pointer with 8-bit color, or 8 bytes per channel color */
2760
2761 /* Bytes per scanline with the expression "(w / 8u) * bpp) + ((w & 7u) * bpp + 7u) / 8u" */
2762 if(lodepng_mulofl((size_t)(w / 8u), bpp, &line)) return 1;
2763 if(lodepng_addofl(line, ((w & 7u) * bpp + 7u) / 8u, &line)) return 1;
2764
2765 if(lodepng_addofl(line, 5, &line)) return 1; /* 5 bytes overhead per line: 1 filterbyte, 4 for Adam7 worst case */
2766 if(lodepng_mulofl(line, h, &total)) return 1; /* Total bytes in worst case */
2767
2768 return 0; /* no overflow */
2769 }
2770 #endif /*LODEPNG_COMPILE_DECODER*/
2771 #endif /*LODEPNG_COMPILE_PNG*/
2772
2773 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2774
2775 static void LodePNGUnknownChunks_init(LodePNGInfo* info) {
2776 unsigned i;
2777 for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0;
2778 for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0;
2779 }
2780
2781 static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info) {
2782 unsigned i;
2783 for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]);
2784 }
2785
2786 static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src) {
2787 unsigned i;
2788
2789 LodePNGUnknownChunks_cleanup(dest);
2790
2791 for(i = 0; i != 3; ++i) {
2792 size_t j;
2793 dest->unknown_chunks_size[i] = src->unknown_chunks_size[i];
2794 dest->unknown_chunks_data[i] = (unsigned char*)lodepng_malloc(src->unknown_chunks_size[i]);
2795 if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/
2796 for(j = 0; j < src->unknown_chunks_size[i]; ++j) {
2797 dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j];
2798 }
2799 }
2800
2801 return 0;
2802 }
2803
2804 /******************************************************************************/
2805
2806 static void LodePNGText_init(LodePNGInfo* info) {
2807 info->text_num = 0;
2808 info->text_keys = NULL;
2809 info->text_strings = NULL;
2810 }
2811
2812 static void LodePNGText_cleanup(LodePNGInfo* info) {
2813 size_t i;
2814 for(i = 0; i != info->text_num; ++i) {
2815 string_cleanup(&info->text_keys[i]);
2816 string_cleanup(&info->text_strings[i]);
2817 }
2818 lodepng_free(info->text_keys);
2819 lodepng_free(info->text_strings);
2820 }
2821
2822 static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source) {
2823 size_t i = 0;
2824 dest->text_keys = 0;
2825 dest->text_strings = 0;
2826 dest->text_num = 0;
2827 for(i = 0; i != source->text_num; ++i) {
2828 CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i]));
2829 }
2830 return 0;
2831 }
2832
2833 void lodepng_clear_text(LodePNGInfo* info) {
2834 LodePNGText_cleanup(info);
2835 }
2836
2837 unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str) {
2838 char** new_keys = (char**)(lodepng_realloc(info->text_keys, sizeof(char*) * (info->text_num + 1)));
2839 char** new_strings = (char**)(lodepng_realloc(info->text_strings, sizeof(char*) * (info->text_num + 1)));
2840 if(!new_keys || !new_strings) {
2841 lodepng_free(new_keys);
2842 lodepng_free(new_strings);
2843 return 83; /*alloc fail*/
2844 }
2845
2846 ++info->text_num;
2847 info->text_keys = new_keys;
2848 info->text_strings = new_strings;
2849
2850 info->text_keys[info->text_num - 1] = alloc_string(key);
2851 info->text_strings[info->text_num - 1] = alloc_string(str);
2852
2853 return 0;
2854 }
2855
2856 /******************************************************************************/
2857
2858 static void LodePNGIText_init(LodePNGInfo* info) {
2859 info->itext_num = 0;
2860 info->itext_keys = NULL;
2861 info->itext_langtags = NULL;
2862 info->itext_transkeys = NULL;
2863 info->itext_strings = NULL;
2864 }
2865
2866 static void LodePNGIText_cleanup(LodePNGInfo* info) {
2867 size_t i;
2868 for(i = 0; i != info->itext_num; ++i) {
2869 string_cleanup(&info->itext_keys[i]);
2870 string_cleanup(&info->itext_langtags[i]);
2871 string_cleanup(&info->itext_transkeys[i]);
2872 string_cleanup(&info->itext_strings[i]);
2873 }
2874 lodepng_free(info->itext_keys);
2875 lodepng_free(info->itext_langtags);
2876 lodepng_free(info->itext_transkeys);
2877 lodepng_free(info->itext_strings);
2878 }
2879
2880 static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source) {
2881 size_t i = 0;
2882 dest->itext_keys = 0;
2883 dest->itext_langtags = 0;
2884 dest->itext_transkeys = 0;
2885 dest->itext_strings = 0;
2886 dest->itext_num = 0;
2887 for(i = 0; i != source->itext_num; ++i) {
2888 CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i],
2889 source->itext_transkeys[i], source->itext_strings[i]));
2890 }
2891 return 0;
2892 }
2893
2894 void lodepng_clear_itext(LodePNGInfo* info) {
2895 LodePNGIText_cleanup(info);
2896 }
2897
2898 unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag,
2899 const char* transkey, const char* str) {
2900 char** new_keys = (char**)(lodepng_realloc(info->itext_keys, sizeof(char*) * (info->itext_num + 1)));
2901 char** new_langtags = (char**)(lodepng_realloc(info->itext_langtags, sizeof(char*) * (info->itext_num + 1)));
2902 char** new_transkeys = (char**)(lodepng_realloc(info->itext_transkeys, sizeof(char*) * (info->itext_num + 1)));
2903 char** new_strings = (char**)(lodepng_realloc(info->itext_strings, sizeof(char*) * (info->itext_num + 1)));
2904 if(!new_keys || !new_langtags || !new_transkeys || !new_strings) {
2905 lodepng_free(new_keys);
2906 lodepng_free(new_langtags);
2907 lodepng_free(new_transkeys);
2908 lodepng_free(new_strings);
2909 return 83; /*alloc fail*/
2910 }
2911
2912 ++info->itext_num;
2913 info->itext_keys = new_keys;
2914 info->itext_langtags = new_langtags;
2915 info->itext_transkeys = new_transkeys;
2916 info->itext_strings = new_strings;
2917
2918 info->itext_keys[info->itext_num - 1] = alloc_string(key);
2919 info->itext_langtags[info->itext_num - 1] = alloc_string(langtag);
2920 info->itext_transkeys[info->itext_num - 1] = alloc_string(transkey);
2921 info->itext_strings[info->itext_num - 1] = alloc_string(str);
2922
2923 return 0;
2924 }
2925
2926 /* same as set but does not delete */
2927 static unsigned lodepng_assign_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size) {
2928 if(profile_size == 0) return 100; /*invalid ICC profile size*/
2929
2930 info->iccp_name = alloc_string(name);
2931 info->iccp_profile = (unsigned char*)lodepng_malloc(profile_size);
2932
2933 if(!info->iccp_name || !info->iccp_profile) return 83; /*alloc fail*/
2934
2935 lodepng_memcpy(info->iccp_profile, profile, profile_size);
2936 info->iccp_profile_size = profile_size;
2937
2938 return 0; /*ok*/
2939 }
2940
2941 unsigned lodepng_set_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size) {
2942 if(info->iccp_name) lodepng_clear_icc(info);
2943 info->iccp_defined = 1;
2944
2945 return lodepng_assign_icc(info, name, profile, profile_size);
2946 }
2947
2948 void lodepng_clear_icc(LodePNGInfo* info) {
2949 string_cleanup(&info->iccp_name);
2950 lodepng_free(info->iccp_profile);
2951 info->iccp_profile = NULL;
2952 info->iccp_profile_size = 0;
2953 info->iccp_defined = 0;
2954 }
2955 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2956
2957 void lodepng_info_init(LodePNGInfo* info) {
2958 lodepng_color_mode_init(&info->color);
2959 info->interlace_method = 0;
2960 info->compression_method = 0;
2961 info->filter_method = 0;
2962 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2963 info->background_defined = 0;
2964 info->background_r = info->background_g = info->background_b = 0;
2965
2966 LodePNGText_init(info);
2967 LodePNGIText_init(info);
2968
2969 info->time_defined = 0;
2970 info->phys_defined = 0;
2971
2972 info->gama_defined = 0;
2973 info->chrm_defined = 0;
2974 info->srgb_defined = 0;
2975 info->iccp_defined = 0;
2976 info->iccp_name = NULL;
2977 info->iccp_profile = NULL;
2978
2979 LodePNGUnknownChunks_init(info);
2980 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2981 }
2982
2983 void lodepng_info_cleanup(LodePNGInfo* info) {
2984 lodepng_color_mode_cleanup(&info->color);
2985 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2986 LodePNGText_cleanup(info);
2987 LodePNGIText_cleanup(info);
2988
2989 lodepng_clear_icc(info);
2990
2991 LodePNGUnknownChunks_cleanup(info);
2992 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2993 }
2994
2995 unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) {
2996 lodepng_info_cleanup(dest);
2997 *dest = *source;
2998 lodepng_color_mode_init(&dest->color);
2999 CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color));
3000
3001 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3002 CERROR_TRY_RETURN(LodePNGText_copy(dest, source));
3003 CERROR_TRY_RETURN(LodePNGIText_copy(dest, source));
3004 if(source->iccp_defined) {
3005 CERROR_TRY_RETURN(lodepng_assign_icc(dest, source->iccp_name, source->iccp_profile, source->iccp_profile_size));
3006 }
3007
3008 LodePNGUnknownChunks_init(dest);
3009 CERROR_TRY_RETURN(LodePNGUnknownChunks_copy(dest, source));
3010 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
3011 return 0;
3012 }
3013
3014 /* ////////////////////////////////////////////////////////////////////////// */
3015
3016 /*index: bitgroup index, bits: bitgroup size(1, 2 or 4), in: bitgroup value, out: octet array to add bits to*/
3017 static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in) {
3018 unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/
3019 /*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for second half*/
3020 unsigned p = index & m;
3021 in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/
3022 in = in << (bits * (m - p));
3023 if(p == 0) out[index * bits / 8u] = in;
3024 else out[index * bits / 8u] |= in;
3025 }
3026
3027 typedef struct ColorTree ColorTree;
3028
3029 /*
3030 One node of a color tree
3031 This is the data structure used to count the number of unique colors and to get a palette
3032 index for a color. It's like an octree, but because the alpha channel is used too, each
3033 node has 16 instead of 8 children.
3034 */
3035 struct ColorTree {
3036 ColorTree* children[16]; /*up to 16 pointers to ColorTree of next level*/
3037 int index; /*the payload. Only has a meaningful value if this is in the last level*/
3038 };
3039
3040 static void color_tree_init(ColorTree* tree) {
3041 int i;
3042 for(i = 0; i != 16; ++i) tree->children[i] = 0;
3043 tree->index = -1;