33#ifdef LODEPNG_COMPILE_DISK
38#ifdef LODEPNG_COMPILE_ALLOCATORS
42#if defined(_MSC_VER) && (_MSC_VER >= 1310)
43#pragma warning( disable : 4244 )
44#pragma warning( disable : 4996 )
73#ifdef LODEPNG_COMPILE_ALLOCATORS
75#ifdef LODEPNG_MAX_ALLOC
76 if(
size > LODEPNG_MAX_ALLOC)
return 0;
83#ifdef LODEPNG_MAX_ALLOC
84 if(new_size > LODEPNG_MAX_ALLOC)
return 0;
86 return realloc(ptr, new_size);
101#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || (defined(__cplusplus) && (__cplusplus >= 199711L))
102#define LODEPNG_INLINE inline
104#define LODEPNG_INLINE
108#if (defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))) ||\
109 (defined(_MSC_VER) && (_MSC_VER >= 1400)) || \
110 (defined(__WATCOMC__) && (__WATCOMC__ >= 1250) && !defined(__cplusplus))
111#define LODEPNG_RESTRICT __restrict
113#define LODEPNG_RESTRICT
123 for(i = 0; i <
size; i++) ((
char*)
dst)[i] = ((
const char*)
src)[i];
127 int value,
size_t num) {
129 for(i = 0; i < num; i++) ((
char*)
dst)[i] = (char)
value;
134 const char* orig =
a;
138 return (
size_t)(
a - orig);
141#define LODEPNG_MAX(a, b) (((a) > (b)) ? (a) : (b))
142#define LODEPNG_MIN(a, b) (((a) < (b)) ? (a) : (b))
144#if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_DECODER)
147static int lodepng_addofl(
size_t a,
size_t b,
size_t* result) {
153#ifdef LODEPNG_COMPILE_DECODER
156static int lodepng_mulofl(
size_t a,
size_t b,
size_t* result) {
158 return (
a != 0 && *result /
a !=
b);
161#ifdef LODEPNG_COMPILE_ZLIB
163static int lodepng_gtofl(
size_t a,
size_t b,
size_t c) {
165 if(lodepng_addofl(
a,
b, &d))
return 1;
179#define CERROR_BREAK(errorvar, code){\
185#define ERROR_BREAK(code) CERROR_BREAK(error, code)
188#define CERROR_RETURN_ERROR(errorvar, code){\
194#define CERROR_TRY_RETURN(call){\
195 unsigned error = call;\
196 if(error) return error;\
200#define CERROR_RETURN(errorvar, code){\
214#ifdef LODEPNG_COMPILE_ZLIB
215#ifdef LODEPNG_COMPILE_ENCODER
217typedef struct uivector {
223static void uivector_cleanup(
void* p) {
224 ((uivector*)p)->size = ((uivector*)p)->allocsize = 0;
226 ((uivector*)p)->data = NULL;
230static unsigned uivector_resize(uivector* p,
size_t size) {
231 size_t allocsize =
size *
sizeof(unsigned);
232 if(allocsize > p->allocsize) {
233 size_t newsize = allocsize + (p->allocsize >> 1u);
236 p->allocsize = newsize;
237 p->data = (
unsigned*)
data;
245static void uivector_init(uivector* p) {
247 p->size = p->allocsize = 0;
251static unsigned uivector_push_back(uivector* p,
unsigned c) {
252 if(!uivector_resize(p, p->size + 1))
return 0;
253 p->data[p->size - 1] = c;
291 v.allocsize =
v.size =
size;
297#ifdef LODEPNG_COMPILE_PNG
298#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
301static void string_cleanup(
char** out) {
307static char* alloc_string_sized(
const char* in,
size_t insize) {
317static char* alloc_string(
const char* in) {
325#if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)
326static unsigned lodepng_read32bitInt(
const unsigned char*
buffer) {
327 return (((
unsigned)
buffer[0] << 24u) | ((
unsigned)
buffer[1] << 16u) |
332#if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)
334static void lodepng_set32bitInt(
unsigned char*
buffer,
unsigned value) {
346#ifdef LODEPNG_COMPILE_DISK
349static long lodepng_filesize(
const char* filename) {
352 file = fopen(filename,
"rb");
355 if(fseek(file, 0, SEEK_END) != 0) {
369static unsigned lodepng_buffer_file(
unsigned char* out,
size_t size,
const char* filename) {
372 file = fopen(filename,
"rb");
375 readsize = fread(out, 1,
size, file);
378 if(readsize !=
size)
return 78;
382unsigned lodepng_load_file(
unsigned char** out,
size_t* outsize,
const char* filename) {
383 long size = lodepng_filesize(filename);
384 if(
size < 0)
return 78;
385 *outsize = (size_t)
size;
388 if(!(*out) &&
size > 0)
return 83;
390 return lodepng_buffer_file(*out, (
size_t)
size, filename);
396 file = fopen(filename,
"wb" );
398 fwrite(
buffer, 1, buffersize, file);
411#ifdef LODEPNG_COMPILE_ZLIB
412#ifdef LODEPNG_COMPILE_ENCODER
419static void LodePNGBitWriter_init(LodePNGBitWriter* writer,
ucvector*
data) {
425#define WRITEBIT(writer, bit){\
427 if(((writer->bp) & 7u) == 0) {\
428 if(!ucvector_resize(writer->data, writer->data->size + 1)) return;\
429 writer->data->data[writer->data->size - 1] = 0;\
431 (writer->data->data[writer->data->size - 1]) |= (bit << ((writer->bp) & 7u));\
436static void writeBits(LodePNGBitWriter* writer,
unsigned value,
size_t nbits) {
438 WRITEBIT(writer,
value);
442 for(i = 0; i != nbits; ++i) {
443 WRITEBIT(writer, (
unsigned char)((
value >> i) & 1));
449static void writeBitsReversed(LodePNGBitWriter* writer,
unsigned value,
size_t nbits) {
451 for(i = 0; i != nbits; ++i) {
453 WRITEBIT(writer, (
unsigned char)((
value >> (nbits - 1u - i)) & 1u));
458#ifdef LODEPNG_COMPILE_DECODER
461 const unsigned char*
data;
469static unsigned LodePNGBitReader_init(LodePNGBitReader* reader,
const unsigned char*
data,
size_t size) {
474 if(lodepng_mulofl(
size, 8u, &reader->bitsize))
return 105;
477 if(lodepng_addofl(reader->bitsize, 64u, &temp))
return 105;
492static LODEPNG_INLINE void ensureBits9(LodePNGBitReader* reader,
size_t nbits) {
493 size_t start = reader->bp >> 3u;
494 size_t size = reader->size;
496 reader->buffer = (unsigned)reader->data[
start + 0] | ((
unsigned)reader->data[
start + 1] << 8u);
497 reader->buffer >>= (reader->bp & 7u);
501 reader->buffer >>= (reader->bp & 7u);
507static LODEPNG_INLINE void ensureBits17(LodePNGBitReader* reader,
size_t nbits) {
508 size_t start = reader->bp >> 3u;
509 size_t size = reader->size;
511 reader->buffer = (unsigned)reader->data[
start + 0] | ((
unsigned)reader->data[
start + 1] << 8u) |
512 ((
unsigned)reader->data[
start + 2] << 16u);
513 reader->buffer >>= (reader->bp & 7u);
517 if(
start + 1u <
size) reader->buffer |= ((unsigned)reader->data[
start + 1] << 8u);
518 reader->buffer >>= (reader->bp & 7u);
524static LODEPNG_INLINE void ensureBits25(LodePNGBitReader* reader,
size_t nbits) {
525 size_t start = reader->bp >> 3u;
526 size_t size = reader->size;
528 reader->buffer = (unsigned)reader->data[
start + 0] | ((
unsigned)reader->data[
start + 1] << 8u) |
529 ((
unsigned)reader->data[
start + 2] << 16u) | ((
unsigned)reader->data[
start + 3] << 24u);
530 reader->buffer >>= (reader->bp & 7u);
534 if(
start + 1u <
size) reader->buffer |= ((unsigned)reader->data[
start + 1] << 8u);
535 if(
start + 2u <
size) reader->buffer |= ((unsigned)reader->data[
start + 2] << 16u);
536 reader->buffer >>= (reader->bp & 7u);
542static LODEPNG_INLINE void ensureBits32(LodePNGBitReader* reader,
size_t nbits) {
543 size_t start = reader->bp >> 3u;
544 size_t size = reader->size;
546 reader->buffer = (unsigned)reader->data[
start + 0] | ((
unsigned)reader->data[
start + 1] << 8u) |
547 ((
unsigned)reader->data[
start + 2] << 16u) | ((
unsigned)reader->data[
start + 3] << 24u);
548 reader->buffer >>= (reader->bp & 7u);
549 reader->buffer |= (((unsigned)reader->data[
start + 4] << 24u) << (8u - (reader->bp & 7u)));
553 if(
start + 1u <
size) reader->buffer |= ((unsigned)reader->data[
start + 1] << 8u);
554 if(
start + 2u <
size) reader->buffer |= ((unsigned)reader->data[
start + 2] << 16u);
555 if(
start + 3u <
size) reader->buffer |= ((unsigned)reader->data[
start + 3] << 24u);
556 reader->buffer >>= (reader->bp & 7u);
562static LODEPNG_INLINE unsigned peekBits(LodePNGBitReader* reader,
size_t nbits) {
564 return reader->buffer & ((1u << nbits) - 1u);
568static LODEPNG_INLINE void advanceBits(LodePNGBitReader* reader,
size_t nbits) {
569 reader->buffer >>= nbits;
574static LODEPNG_INLINE unsigned readBits(LodePNGBitReader* reader,
size_t nbits) {
575 unsigned result = peekBits(reader, nbits);
576 advanceBits(reader, nbits);
581static unsigned reverseBits(
unsigned bits,
unsigned num) {
583 unsigned i, result = 0;
584 for(i = 0; i < num; i++) result |= ((bits >> (num - i - 1u)) & 1u) << i;
592#define FIRST_LENGTH_CODE_INDEX 257
593#define LAST_LENGTH_CODE_INDEX 285
595#define NUM_DEFLATE_CODE_SYMBOLS 288
597#define NUM_DISTANCE_SYMBOLS 32
599#define NUM_CODE_LENGTH_CODES 19
602static const unsigned LENGTHBASE[29]
603 = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
604 67, 83, 99, 115, 131, 163, 195, 227, 258};
607static const unsigned LENGTHEXTRA[29]
608 = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
609 4, 4, 4, 4, 5, 5, 5, 5, 0};
612static const unsigned DISTANCEBASE[30]
613 = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
614 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
617static const unsigned DISTANCEEXTRA[30]
618 = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
619 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
623static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES]
624 = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
631typedef struct HuffmanTree {
637 unsigned char* table_len;
638 unsigned short* table_value;
641static void HuffmanTree_init(HuffmanTree* tree) {
645 tree->table_value = 0;
648static void HuffmanTree_cleanup(HuffmanTree* tree) {
661#define INVALIDSYMBOL 65535u
664static unsigned HuffmanTree_makeTable(HuffmanTree* tree) {
665 static const unsigned headsize = 1u << FIRSTBITS;
666 static const unsigned mask = (1u << FIRSTBITS) - 1u;
668 unsigned* maxlens = (
unsigned*)
lodepng_malloc(headsize *
sizeof(
unsigned));
669 if(!maxlens)
return 83;
673 for(i = 0; i < tree->numcodes; i++) {
674 unsigned symbol = tree->codes[i];
675 unsigned l = tree->lengths[i];
677 if(l <= FIRSTBITS)
continue;
679 index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS);
684 for(i = 0; i < headsize; ++i) {
685 unsigned l = maxlens[i];
686 if(l > FIRSTBITS)
size += (1u << (l - FIRSTBITS));
689 tree->table_value = (
unsigned short*)
lodepng_malloc(
size *
sizeof(*tree->table_value));
690 if(!tree->table_len || !tree->table_value) {
696 for(i = 0; i <
size; ++i) tree->table_len[i] = 16;
700 for(i = 0; i < headsize; ++i) {
701 unsigned l = maxlens[i];
702 if(l <= FIRSTBITS)
continue;
703 tree->table_len[i] = l;
704 tree->table_value[i] =
pointer;
705 pointer += (1u << (l - FIRSTBITS));
711 for(i = 0; i < tree->numcodes; ++i) {
712 unsigned l = tree->lengths[i];
713 unsigned symbol, reverse;
715 symbol = tree->codes[i];
717 reverse = reverseBits(symbol, l);
722 unsigned num = 1u << (FIRSTBITS - l);
724 for(
j = 0;
j < num; ++
j) {
726 unsigned index = reverse | (
j << l);
727 if(tree->table_len[
index] != 16)
return 55;
728 tree->table_len[
index] = l;
729 tree->table_value[
index] = i;
735 unsigned maxlen = tree->table_len[
index];
737 unsigned tablelen = maxlen - FIRSTBITS;
739 unsigned num = 1u << (tablelen - (l - FIRSTBITS));
741 if(maxlen < l)
return 55;
742 for(
j = 0;
j < num; ++
j) {
743 unsigned reverse2 = reverse >> FIRSTBITS;
744 unsigned index2 =
start + (reverse2 | (
j << (l - FIRSTBITS)));
745 tree->table_len[index2] = l;
746 tree->table_value[index2] = i;
758 for(i = 0; i <
size; ++i) {
759 if(tree->table_len[i] == 16) {
763 tree->table_len[i] = (i < headsize) ? 1 : (FIRSTBITS + 1);
764 tree->table_value[i] = INVALIDSYMBOL;
772 for(i = 0; i <
size; ++i) {
773 if(tree->table_len[i] == 16)
return 55;
785static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) {
791 tree->codes = (
unsigned*)
lodepng_malloc(tree->numcodes *
sizeof(
unsigned));
792 blcount = (
unsigned*)
lodepng_malloc((tree->maxbitlen + 1) *
sizeof(unsigned));
793 nextcode = (
unsigned*)
lodepng_malloc((tree->maxbitlen + 1) *
sizeof(unsigned));
794 if(!tree->codes || !blcount || !nextcode) error = 83;
797 for(
n = 0;
n != tree->maxbitlen + 1;
n++) blcount[
n] = nextcode[
n] = 0;
799 for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]];
801 for(bits = 1; bits <= tree->maxbitlen; ++bits) {
802 nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u;
805 for(
n = 0;
n != tree->numcodes; ++
n) {
806 if(tree->lengths[
n] != 0) {
807 tree->codes[
n] = nextcode[tree->lengths[
n]]++;
809 tree->codes[
n] &= ((1u << tree->lengths[
n]) - 1u);
817 if(!error) error = HuffmanTree_makeTable(tree);
826static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree,
const unsigned* bitlen,
827 size_t numcodes,
unsigned maxbitlen) {
829 tree->lengths = (
unsigned*)
lodepng_malloc(numcodes *
sizeof(
unsigned));
830 if(!tree->lengths)
return 83;
831 for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i];
832 tree->numcodes = (unsigned)numcodes;
833 tree->maxbitlen = maxbitlen;
834 return HuffmanTree_makeFromLengths2(tree);
837#ifdef LODEPNG_COMPILE_ENCODER
843typedef struct BPMNode {
846 struct BPMNode* tail;
851typedef struct BPMLists {
865static BPMNode* bpmnode_create(BPMLists*
lists,
int weight,
unsigned index, BPMNode* tail) {
872 for(i = 0; i !=
lists->memsize; ++i)
lists->memory[i].in_use = 0;
873 for(i = 0; i !=
lists->listsize; ++i) {
875 for(node =
lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1;
876 for(node =
lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1;
880 for(i = 0; i !=
lists->memsize; ++i) {
887 result->weight = weight;
888 result->index =
index;
894static void bpmnode_sort(BPMNode* leaves,
size_t num) {
896 size_t width, counter = 0;
898 BPMNode*
a = (counter & 1) ? mem : leaves;
899 BPMNode*
b = (counter & 1) ? leaves : mem;
901 for(p = 0; p < num; p += 2 *
width) {
903 size_t r = (p + 2 *
width > num) ? num : (p + 2 *
width);
904 size_t i = p,
j =
q, k;
905 for(k = p; k <
r; k++) {
906 if(i <
q && (
j >=
r ||
a[i].weight <=
a[
j].weight))
b[k] =
a[i++];
912 if(counter & 1)
lodepng_memcpy(leaves, mem,
sizeof(*leaves) * num);
917static void boundaryPM(BPMLists*
lists, BPMNode* leaves,
size_t numpresent,
int c,
int num) {
918 unsigned lastindex =
lists->chains1[c]->index;
921 if(lastindex >= numpresent)
return;
923 lists->chains1[c] = bpmnode_create(
lists, leaves[lastindex].weight, lastindex + 1, 0);
926 int sum =
lists->chains0[c - 1]->weight +
lists->chains1[c - 1]->weight;
928 if(lastindex < numpresent && sum > leaves[lastindex].weight) {
929 lists->chains1[c] = bpmnode_create(
lists, leaves[lastindex].weight, lastindex + 1,
lists->chains1[c]->tail);
932 lists->chains1[c] = bpmnode_create(
lists, sum, lastindex,
lists->chains1[c - 1]);
935 if(num + 1 < (
int)(2 * numpresent - 2)) {
936 boundaryPM(
lists, leaves, numpresent, c - 1, num);
937 boundaryPM(
lists, leaves, numpresent, c - 1, num);
943 size_t numcodes,
unsigned maxbitlen) {
946 size_t numpresent = 0;
949 if(numcodes == 0)
return 80;
950 if((1u << maxbitlen) < (
unsigned)numcodes)
return 80;
953 if(!leaves)
return 83;
955 for(i = 0; i != numcodes; ++i) {
956 if(frequencies[i] > 0) {
957 leaves[numpresent].weight = (int)frequencies[i];
958 leaves[numpresent].index = i;
970 if(numpresent == 0) {
972 }
else if(numpresent == 1) {
974 lengths[leaves[0].index == 0 ? 1 : 0] = 1;
979 bpmnode_sort(leaves, numpresent);
981 lists.listsize = maxbitlen;
982 lists.memsize = 2 * maxbitlen * (maxbitlen + 1);
992 for(i = 0; i !=
lists.memsize; ++i)
lists.freelist[i] = &
lists.memory[i];
994 bpmnode_create(&
lists, leaves[0].weight, 1, 0);
995 bpmnode_create(&
lists, leaves[1].weight, 2, 0);
997 for(i = 0; i !=
lists.listsize; ++i) {
1003 for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&
lists, leaves, numpresent, (
int)maxbitlen - 1, (
int)i);
1005 for(node =
lists.chains1[maxbitlen - 1]; node; node = node->tail) {
1006 for(i = 0; i != node->index; ++i) ++
lengths[leaves[i].
index];
1021static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree,
const unsigned* frequencies,
1022 size_t mincodes,
size_t numcodes,
unsigned maxbitlen) {
1024 while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes;
1025 tree->lengths = (
unsigned*)
lodepng_malloc(numcodes *
sizeof(
unsigned));
1026 if(!tree->lengths)
return 83;
1027 tree->maxbitlen = maxbitlen;
1028 tree->numcodes = (unsigned)numcodes;
1031 if(!error) error = HuffmanTree_makeFromLengths2(tree);
1037static unsigned generateFixedLitLenTree(HuffmanTree* tree) {
1038 unsigned i, error = 0;
1039 unsigned* bitlen = (
unsigned*)
lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS *
sizeof(
unsigned));
1040 if(!bitlen)
return 83;
1043 for(i = 0; i <= 143; ++i) bitlen[i] = 8;
1044 for(i = 144; i <= 255; ++i) bitlen[i] = 9;
1045 for(i = 256; i <= 279; ++i) bitlen[i] = 7;
1046 for(i = 280; i <= 287; ++i) bitlen[i] = 8;
1048 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15);
1055static unsigned generateFixedDistanceTree(HuffmanTree* tree) {
1056 unsigned i, error = 0;
1057 unsigned* bitlen = (
unsigned*)
lodepng_malloc(NUM_DISTANCE_SYMBOLS *
sizeof(
unsigned));
1058 if(!bitlen)
return 83;
1061 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5;
1062 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15);
1068#ifdef LODEPNG_COMPILE_DECODER
1073static unsigned huffmanDecodeSymbol(LodePNGBitReader* reader,
const HuffmanTree* codetree) {
1074 unsigned short code = peekBits(reader, FIRSTBITS);
1075 unsigned short l = codetree->table_len[code];
1076 unsigned short value = codetree->table_value[code];
1077 if(l <= FIRSTBITS) {
1078 advanceBits(reader, l);
1081 advanceBits(reader, FIRSTBITS);
1082 value += peekBits(reader, l - FIRSTBITS);
1083 advanceBits(reader, codetree->table_len[
value] - FIRSTBITS);
1084 return codetree->table_value[
value];
1089#ifdef LODEPNG_COMPILE_DECODER
1097static unsigned getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d) {
1098 unsigned error = generateFixedLitLenTree(tree_ll);
1099 if(error)
return error;
1100 return generateFixedDistanceTree(tree_d);
1104static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
1105 LodePNGBitReader* reader) {
1108 unsigned n, HLIT, HDIST, HCLEN, i;
1111 unsigned* bitlen_ll = 0;
1112 unsigned* bitlen_d = 0;
1114 unsigned* bitlen_cl = 0;
1115 HuffmanTree tree_cl;
1117 if(reader->bitsize - reader->bp < 14)
return 49;
1118 ensureBits17(reader, 14);
1121 HLIT = readBits(reader, 5) + 257;
1123 HDIST = readBits(reader, 5) + 1;
1125 HCLEN = readBits(reader, 4) + 4;
1127 bitlen_cl = (
unsigned*)
lodepng_malloc(NUM_CODE_LENGTH_CODES *
sizeof(
unsigned));
1128 if(!bitlen_cl)
return 83 ;
1130 HuffmanTree_init(&tree_cl);
1134 if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) {
1137 for(i = 0; i != HCLEN; ++i) {
1138 ensureBits9(reader, 3);
1139 bitlen_cl[CLCL_ORDER[i]] = readBits(reader, 3);
1141 for(i = HCLEN; i != NUM_CODE_LENGTH_CODES; ++i) {
1142 bitlen_cl[CLCL_ORDER[i]] = 0;
1145 error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7);
1149 bitlen_ll = (
unsigned*)
lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS *
sizeof(
unsigned));
1150 bitlen_d = (
unsigned*)
lodepng_malloc(NUM_DISTANCE_SYMBOLS *
sizeof(
unsigned));
1152 lodepng_memset(bitlen_ll, 0, NUM_DEFLATE_CODE_SYMBOLS *
sizeof(*bitlen_ll));
1153 lodepng_memset(bitlen_d, 0, NUM_DISTANCE_SYMBOLS *
sizeof(*bitlen_d));
1157 while(i < HLIT + HDIST) {
1159 ensureBits25(reader, 22);
1160 code = huffmanDecodeSymbol(reader, &tree_cl);
1162 if(i < HLIT) bitlen_ll[i] = code;
1163 else bitlen_d[i - HLIT] = code;
1165 }
else if(code == 16) {
1166 unsigned replength = 3;
1171 replength += readBits(reader, 2);
1173 if(i < HLIT + 1)
value = bitlen_ll[i - 1];
1174 else value = bitlen_d[i - HLIT - 1];
1176 for(
n = 0;
n < replength; ++
n) {
1178 if(i < HLIT) bitlen_ll[i] =
value;
1179 else bitlen_d[i - HLIT] =
value;
1182 }
else if(code == 17) {
1183 unsigned replength = 3;
1184 replength += readBits(reader, 3);
1187 for(
n = 0;
n < replength; ++
n) {
1190 if(i < HLIT) bitlen_ll[i] = 0;
1191 else bitlen_d[i - HLIT] = 0;
1194 }
else if(code == 18) {
1195 unsigned replength = 11;
1196 replength += readBits(reader, 7);
1199 for(
n = 0;
n < replength; ++
n) {
1202 if(i < HLIT) bitlen_ll[i] = 0;
1203 else bitlen_d[i - HLIT] = 0;
1210 if(reader->bp > reader->bitsize) {
1222 error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15);
1224 error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15);
1232 HuffmanTree_cleanup(&tree_cl);
1238static unsigned inflateHuffmanBlock(
ucvector* out, LodePNGBitReader* reader,
1239 unsigned btype,
size_t max_output_size) {
1241 HuffmanTree tree_ll;
1243 const size_t reserved_size = 260;
1248 HuffmanTree_init(&tree_ll);
1249 HuffmanTree_init(&tree_d);
1251 if(btype == 1) error = getTreeInflateFixed(&tree_ll, &tree_d);
1252 else error = getTreeInflateDynamic(&tree_ll, &tree_d, reader);
1255 while(!error && !done) {
1260 ensureBits32(reader, 30);
1261 code_ll = huffmanDecodeSymbol(reader, &tree_ll);
1262 if(code_ll <= 255) {
1264 out->
data[out->
size++] = (
unsigned char)code_ll;
1265 code_ll = huffmanDecodeSymbol(reader, &tree_ll);
1267 if(code_ll <= 255) {
1268 out->
data[out->
size++] = (
unsigned char)code_ll;
1269 }
else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) {
1270 unsigned code_d, distance;
1271 unsigned numextrabits_l, numextrabits_d;
1275 length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX];
1278 numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX];
1279 if(numextrabits_l != 0) {
1281 ensureBits25(reader, 5);
1282 length += readBits(reader, numextrabits_l);
1286 ensureBits32(reader, 28);
1287 code_d = huffmanDecodeSymbol(reader, &tree_d);
1295 distance = DISTANCEBASE[code_d];
1298 numextrabits_d = DISTANCEEXTRA[code_d];
1299 if(numextrabits_d != 0) {
1301 distance += readBits(reader, numextrabits_d);
1307 backward =
start - distance;
1314 for(forward = distance; forward <
length; ++forward) {
1320 }
else if(code_ll == 256) {
1329 if(reader->bp > reader->bitsize) {
1335 if(max_output_size && out->
size > max_output_size) {
1340 HuffmanTree_cleanup(&tree_ll);
1341 HuffmanTree_cleanup(&tree_d);
1346static unsigned inflateNoCompression(
ucvector* out, LodePNGBitReader* reader,
1349 size_t size = reader->size;
1350 unsigned LEN, NLEN, error = 0;
1353 bytepos = (reader->bp + 7u) >> 3u;
1356 if(bytepos + 4 >=
size)
return 52;
1357 LEN = (unsigned)reader->data[bytepos] + ((
unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2;
1358 NLEN = (unsigned)reader->data[bytepos] + ((
unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2;
1361 if(!settings->
ignore_nlen && LEN + NLEN != 65535) {
1368 if(bytepos + LEN >
size)
return 23;
1376 reader->bp = bytepos << 3u;
1381static unsigned lodepng_inflatev(
ucvector* out,
1382 const unsigned char* in,
size_t insize,
1384 unsigned BFINAL = 0;
1385 LodePNGBitReader reader;
1386 unsigned error = LodePNGBitReader_init(&reader, in, insize);
1388 if(error)
return error;
1392 if(reader.bitsize - reader.bp < 3)
return 52;
1393 ensureBits9(&reader, 3);
1394 BFINAL = readBits(&reader, 1);
1395 BTYPE = readBits(&reader, 2);
1397 if(BTYPE == 3)
return 20;
1398 else if(BTYPE == 0) error = inflateNoCompression(out, &reader, settings);
1399 else error = inflateHuffmanBlock(out, &reader, BTYPE, settings->
max_output_size);
1408 const unsigned char* in,
size_t insize,
1411 unsigned error = lodepng_inflatev(&
v, in, insize, settings);
1417static unsigned inflatev(
ucvector* out,
const unsigned char* in,
size_t insize,
1430 return lodepng_inflatev(out, in, insize, settings);
1436#ifdef LODEPNG_COMPILE_ENCODER
1442static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258;
1446static size_t searchCodeIndex(
const unsigned* array,
size_t array_size,
size_t value) {
1449 size_t right = array_size - 1;
1454 else left = mid + 1;
1460static void addLengthDistance(uivector*
values,
size_t length,
size_t distance) {
1467 unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29,
length);
1468 unsigned extra_length = (unsigned)(
length - LENGTHBASE[length_code]);
1469 unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance);
1470 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]);
1472 size_t pos =
values->size;
1474 unsigned ok = uivector_resize(
values,
values->size + 4);
1476 values->data[pos + 0] = length_code + FIRST_LENGTH_CODE_INDEX;
1477 values->data[pos + 1] = extra_length;
1478 values->data[pos + 2] = dist_code;
1479 values->data[pos + 3] = extra_distance;
1485static const unsigned HASH_NUM_VALUES = 65536;
1486static const unsigned HASH_BIT_MASK = 65535;
1488typedef struct Hash {
1491 unsigned short* chain;
1497 unsigned short* chainz;
1498 unsigned short* zeros;
1501static unsigned hash_init(Hash* hash,
unsigned windowsize) {
1503 hash->head = (
int*)
lodepng_malloc(
sizeof(
int) * HASH_NUM_VALUES);
1505 hash->chain = (
unsigned short*)
lodepng_malloc(
sizeof(
unsigned short) * windowsize);
1507 hash->zeros = (
unsigned short*)
lodepng_malloc(
sizeof(
unsigned short) * windowsize);
1508 hash->headz = (
int*)
lodepng_malloc(
sizeof(
int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1));
1509 hash->chainz = (
unsigned short*)
lodepng_malloc(
sizeof(
unsigned short) * windowsize);
1511 if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros) {
1516 for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1;
1517 for(i = 0; i != windowsize; ++i) hash->val[i] = -1;
1518 for(i = 0; i != windowsize; ++i) hash->chain[i] = i;
1520 for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1;
1521 for(i = 0; i != windowsize; ++i) hash->chainz[i] = i;
1526static void hash_cleanup(Hash* hash) {
1538static unsigned getHash(
const unsigned char*
data,
size_t size,
size_t pos) {
1539 unsigned result = 0;
1540 if(pos + 2 <
size) {
1545 result ^= ((unsigned)
data[pos + 0] << 0u);
1546 result ^= ((unsigned)
data[pos + 1] << 4u);
1547 result ^= ((unsigned)
data[pos + 2] << 8u);
1550 if(pos >=
size)
return 0;
1551 amount =
size - pos;
1552 for(i = 0; i != amount; ++i) result ^= ((
unsigned)
data[pos + i] << (i * 8u));
1554 return result & HASH_BIT_MASK;
1557static unsigned countZeros(
const unsigned char*
data,
size_t size,
size_t pos) {
1558 const unsigned char*
start =
data + pos;
1559 const unsigned char*
end =
start + MAX_SUPPORTED_DEFLATE_LENGTH;
1568static void updateHashChain(Hash* hash,
size_t wpos,
unsigned hashval,
unsigned short numzeros) {
1569 hash->val[wpos] = (int)hashval;
1570 if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval];
1571 hash->head[hashval] = (int)wpos;
1573 hash->zeros[wpos] = numzeros;
1574 if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros];
1575 hash->headz[numzeros] = (int)wpos;
1587static unsigned encodeLZ77(uivector* out, Hash* hash,
1588 const unsigned char* in,
size_t inpos,
size_t insize,
unsigned windowsize,
1589 unsigned minmatch,
unsigned nicematch,
unsigned lazymatching) {
1591 unsigned i, error = 0;
1593 unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8u;
1594 unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64;
1596 unsigned usezeros = 1;
1597 unsigned numzeros = 0;
1602 unsigned lazylength = 0, lazyoffset = 0;
1604 unsigned current_offset, current_length;
1605 unsigned prev_offset;
1606 const unsigned char *lastptr, *foreptr, *backptr;
1609 if(windowsize == 0 || windowsize > 32768)
return 60;
1610 if((windowsize & (windowsize - 1)) != 0)
return 90;
1612 if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH;
1614 for(pos = inpos; pos < insize; ++pos) {
1615 size_t wpos = pos & (windowsize - 1);
1616 unsigned chainlength = 0;
1618 hashval = getHash(in, insize, pos);
1620 if(usezeros && hashval == 0) {
1621 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
1622 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
1627 updateHashChain(hash, wpos, hashval, numzeros);
1633 hashpos = hash->chain[wpos];
1635 lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];
1640 if(chainlength++ >= maxchainlength)
break;
1641 current_offset = (unsigned)(hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize);
1643 if(current_offset < prev_offset)
break;
1644 prev_offset = current_offset;
1645 if(current_offset > 0) {
1648 backptr = &in[pos - current_offset];
1652 unsigned skip = hash->zeros[hashpos];
1653 if(skip > numzeros) skip = numzeros;
1658 while(foreptr != lastptr && *backptr == *foreptr) {
1662 current_length = (unsigned)(foreptr - &in[pos]);
1664 if(current_length >
length) {
1669 if(current_length >= nicematch)
break;
1673 if(hashpos == hash->chain[hashpos])
break;
1675 if(numzeros >= 3 &&
length > numzeros) {
1676 hashpos = hash->chainz[hashpos];
1677 if(hash->zeros[hashpos] != numzeros)
break;
1679 hashpos = hash->chain[hashpos];
1681 if(hash->val[hashpos] != (
int)hashval)
break;
1686 if(!lazy &&
length >= 3 &&
length <= maxlazymatch &&
length < MAX_SUPPORTED_DEFLATE_LENGTH) {
1695 if(
length > lazylength + 1) {
1697 if(!uivector_push_back(out, in[pos - 1]))
ERROR_BREAK(83 );
1701 hash->head[hashval] = -1;
1702 hash->headz[numzeros] = -1;
1711 if(!uivector_push_back(out, in[pos]))
ERROR_BREAK(83 );
1715 if(!uivector_push_back(out, in[pos]))
ERROR_BREAK(83 );
1718 for(i = 1; i <
length; ++i) {
1720 wpos = pos & (windowsize - 1);
1721 hashval = getHash(in, insize, pos);
1722 if(usezeros && hashval == 0) {
1723 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
1724 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
1728 updateHashChain(hash, wpos, hashval, numzeros);
1738static unsigned deflateNoCompression(
ucvector* out,
const unsigned char*
data,
size_t datasize) {
1742 size_t i, numdeflateblocks = (datasize + 65534u) / 65535u;
1743 unsigned datapos = 0;
1744 for(i = 0; i != numdeflateblocks; ++i) {
1745 unsigned BFINAL, BTYPE, LEN, NLEN;
1746 unsigned char firstbyte;
1747 size_t pos = out->
size;
1749 BFINAL = (i == numdeflateblocks - 1);
1753 if(datasize - datapos < 65535u) LEN = (unsigned)datasize - datapos;
1758 firstbyte = (
unsigned char)(BFINAL + ((BTYPE & 1u) << 1u) + ((BTYPE & 2u) << 1u));
1759 out->
data[pos + 0] = firstbyte;
1760 out->
data[pos + 1] = (
unsigned char)(LEN & 255);
1761 out->
data[pos + 2] = (
unsigned char)(LEN >> 8u);
1762 out->
data[pos + 3] = (
unsigned char)(NLEN & 255);
1763 out->
data[pos + 4] = (
unsigned char)(NLEN >> 8u);
1776static void writeLZ77data(LodePNGBitWriter* writer,
const uivector* lz77_encoded,
1777 const HuffmanTree* tree_ll,
const HuffmanTree* tree_d) {
1779 for(i = 0; i != lz77_encoded->size; ++i) {
1780 unsigned val = lz77_encoded->data[i];
1781 writeBitsReversed(writer, tree_ll->codes[
val], tree_ll->lengths[
val]);
1783 unsigned length_index =
val - FIRST_LENGTH_CODE_INDEX;
1784 unsigned n_length_extra_bits = LENGTHEXTRA[length_index];
1785 unsigned length_extra_bits = lz77_encoded->data[++i];
1787 unsigned distance_code = lz77_encoded->data[++i];
1789 unsigned distance_index = distance_code;
1790 unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index];
1791 unsigned distance_extra_bits = lz77_encoded->data[++i];
1793 writeBits(writer, length_extra_bits, n_length_extra_bits);
1794 writeBitsReversed(writer, tree_d->codes[distance_code], tree_d->lengths[distance_code]);
1795 writeBits(writer, distance_extra_bits, n_distance_extra_bits);
1801static unsigned deflateDynamic(LodePNGBitWriter* writer, Hash* hash,
1802 const unsigned char*
data,
size_t datapos,
size_t dataend,
1818 uivector lz77_encoded;
1819 HuffmanTree tree_ll;
1821 HuffmanTree tree_cl;
1822 unsigned* frequencies_ll = 0;
1823 unsigned* frequencies_d = 0;
1824 unsigned* frequencies_cl = 0;
1825 unsigned* bitlen_lld = 0;
1826 unsigned* bitlen_lld_e = 0;
1827 size_t datasize = dataend - datapos;
1838 unsigned BFINAL =
final;
1840 size_t numcodes_ll, numcodes_d, numcodes_lld, numcodes_lld_e, numcodes_cl;
1841 unsigned HLIT, HDIST, HCLEN;
1843 uivector_init(&lz77_encoded);
1844 HuffmanTree_init(&tree_ll);
1845 HuffmanTree_init(&tree_d);
1846 HuffmanTree_init(&tree_cl);
1848 frequencies_ll = (
unsigned*)
lodepng_malloc(286 *
sizeof(*frequencies_ll));
1849 frequencies_d = (
unsigned*)
lodepng_malloc(30 *
sizeof(*frequencies_d));
1850 frequencies_cl = (
unsigned*)
lodepng_malloc(NUM_CODE_LENGTH_CODES *
sizeof(*frequencies_cl));
1852 if(!frequencies_ll || !frequencies_d || !frequencies_cl) error = 83;
1857 lodepng_memset(frequencies_ll, 0, 286 *
sizeof(*frequencies_ll));
1859 lodepng_memset(frequencies_cl, 0, NUM_CODE_LENGTH_CODES *
sizeof(*frequencies_cl));
1862 error = encodeLZ77(&lz77_encoded, hash,
data, datapos, dataend, settings->
windowsize,
1866 if(!uivector_resize(&lz77_encoded, datasize))
ERROR_BREAK(83 );
1867 for(i = datapos; i < dataend; ++i) lz77_encoded.data[i - datapos] =
data[i];
1871 for(i = 0; i != lz77_encoded.size; ++i) {
1872 unsigned symbol = lz77_encoded.data[i];
1873 ++frequencies_ll[symbol];
1875 unsigned dist = lz77_encoded.data[i + 2];
1876 ++frequencies_d[dist];
1880 frequencies_ll[256] = 1;
1883 error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll, 257, 286, 15);
1886 error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d, 2, 30, 15);
1892 numcodes_lld = numcodes_ll + numcodes_d;
1893 bitlen_lld = (
unsigned*)
lodepng_malloc(numcodes_lld *
sizeof(*bitlen_lld));
1895 bitlen_lld_e = (
unsigned*)
lodepng_malloc(numcodes_lld *
sizeof(*bitlen_lld_e));
1899 for(i = 0; i != numcodes_ll; ++i) bitlen_lld[i] = tree_ll.lengths[i];
1900 for(i = 0; i != numcodes_d; ++i) bitlen_lld[numcodes_ll + i] = tree_d.lengths[i];
1904 for(i = 0; i != numcodes_lld; ++i) {
1906 while(i +
j + 1 < numcodes_lld && bitlen_lld[i +
j + 1] == bitlen_lld[i]) ++
j;
1908 if(bitlen_lld[i] == 0 &&
j >= 2) {
1911 bitlen_lld_e[numcodes_lld_e++] = 17;
1912 bitlen_lld_e[numcodes_lld_e++] =
j - 3;
1914 if(
j > 138)
j = 138;
1915 bitlen_lld_e[numcodes_lld_e++] = 18;
1916 bitlen_lld_e[numcodes_lld_e++] =
j - 11;
1921 unsigned num =
j / 6u, rest =
j % 6u;
1922 bitlen_lld_e[numcodes_lld_e++] = bitlen_lld[i];
1923 for(k = 0; k < num; ++k) {
1924 bitlen_lld_e[numcodes_lld_e++] = 16;
1925 bitlen_lld_e[numcodes_lld_e++] = 6 - 3;
1928 bitlen_lld_e[numcodes_lld_e++] = 16;
1929 bitlen_lld_e[numcodes_lld_e++] = rest - 3;
1934 bitlen_lld_e[numcodes_lld_e++] = bitlen_lld[i];
1939 for(i = 0; i != numcodes_lld_e; ++i) {
1940 ++frequencies_cl[bitlen_lld_e[i]];
1943 if(bitlen_lld_e[i] >= 16) ++i;
1946 error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl,
1947 NUM_CODE_LENGTH_CODES, NUM_CODE_LENGTH_CODES, 7);
1951 numcodes_cl = NUM_CODE_LENGTH_CODES;
1953 while(numcodes_cl > 4u && tree_cl.lengths[CLCL_ORDER[numcodes_cl - 1u]] == 0) {
1972 writeBits(writer, BFINAL, 1);
1973 writeBits(writer, 0, 1);
1974 writeBits(writer, 1, 1);
1979 HLIT = (unsigned)(numcodes_ll - 257);
1980 HDIST = (unsigned)(numcodes_d - 1);
1981 HCLEN = (unsigned)(numcodes_cl - 4);
1982 writeBits(writer, HLIT, 5);
1983 writeBits(writer, HDIST, 5);
1984 writeBits(writer, HCLEN, 4);
1987 for(i = 0; i != numcodes_cl; ++i) writeBits(writer, tree_cl.lengths[CLCL_ORDER[i]], 3);
1990 for(i = 0; i != numcodes_lld_e; ++i) {
1991 writeBitsReversed(writer, tree_cl.codes[bitlen_lld_e[i]], tree_cl.lengths[bitlen_lld_e[i]]);
1993 if(bitlen_lld_e[i] == 16) writeBits(writer, bitlen_lld_e[++i], 2);
1994 else if(bitlen_lld_e[i] == 17) writeBits(writer, bitlen_lld_e[++i], 3);
1995 else if(bitlen_lld_e[i] == 18) writeBits(writer, bitlen_lld_e[++i], 7);
1999 writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d);
2004 writeBitsReversed(writer, tree_ll.codes[256], tree_ll.lengths[256]);
2010 uivector_cleanup(&lz77_encoded);
2011 HuffmanTree_cleanup(&tree_ll);
2012 HuffmanTree_cleanup(&tree_d);
2013 HuffmanTree_cleanup(&tree_cl);
2023static unsigned deflateFixed(LodePNGBitWriter* writer, Hash* hash,
2024 const unsigned char*
data,
2025 size_t datapos,
size_t dataend,
2027 HuffmanTree tree_ll;
2030 unsigned BFINAL =
final;
2034 HuffmanTree_init(&tree_ll);
2035 HuffmanTree_init(&tree_d);
2037 error = generateFixedLitLenTree(&tree_ll);
2038 if(!error) error = generateFixedDistanceTree(&tree_d);
2041 writeBits(writer, BFINAL, 1);
2042 writeBits(writer, 1, 1);
2043 writeBits(writer, 0, 1);
2046 uivector lz77_encoded;
2047 uivector_init(&lz77_encoded);
2048 error = encodeLZ77(&lz77_encoded, hash,
data, datapos, dataend, settings->
windowsize,
2050 if(!error) writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d);
2051 uivector_cleanup(&lz77_encoded);
2053 for(i = datapos; i < dataend; ++i) {
2054 writeBitsReversed(writer, tree_ll.codes[
data[i]], tree_ll.lengths[
data[i]]);
2058 if(!error) writeBitsReversed(writer,tree_ll.codes[256], tree_ll.lengths[256]);
2062 HuffmanTree_cleanup(&tree_ll);
2063 HuffmanTree_cleanup(&tree_d);
2068static unsigned lodepng_deflatev(
ucvector* out,
const unsigned char* in,
size_t insize,
2071 size_t i, blocksize, numdeflateblocks;
2073 LodePNGBitWriter writer;
2075 LodePNGBitWriter_init(&writer, out);
2077 if(settings->
btype > 2)
return 61;
2078 else if(settings->
btype == 0)
return deflateNoCompression(out, in, insize);
2079 else if(settings->
btype == 1) blocksize = insize;
2082 blocksize = insize / 8u + 8;
2083 if(blocksize < 65536) blocksize = 65536;
2084 if(blocksize > 262144) blocksize = 262144;
2087 numdeflateblocks = (insize + blocksize - 1) / blocksize;
2088 if(numdeflateblocks == 0) numdeflateblocks = 1;
2090 error = hash_init(&hash, settings->
windowsize);
2093 for(i = 0; i != numdeflateblocks && !error; ++i) {
2094 unsigned final = (i == numdeflateblocks - 1);
2095 size_t start = i * blocksize;
2097 if(
end > insize)
end = insize;
2099 if(settings->
btype == 1) error = deflateFixed(&writer, &hash, in,
start,
end, settings,
final);
2100 else if(settings->
btype == 2) error = deflateDynamic(&writer, &hash, in,
start,
end, settings,
final);
2104 hash_cleanup(&hash);
2110 const unsigned char* in,
size_t insize,
2113 unsigned error = lodepng_deflatev(&
v, in, insize, settings);
2119static unsigned deflate(
unsigned char** out,
size_t* outsize,
2120 const unsigned char* in,
size_t insize,
2123 unsigned error = settings->
custom_deflate(out, outsize, in, insize, settings);
2125 return error ? 111 : 0;
2137static unsigned update_adler32(
unsigned adler,
const unsigned char*
data,
unsigned len) {
2138 unsigned s1 = adler & 0xffffu;
2139 unsigned s2 = (adler >> 16u) & 0xffffu;
2144 unsigned amount = len > 5552u ? 5552u : len;
2146 for(i = 0; i != amount; ++i) {
2154 return (s2 << 16u) | s1;
2158static unsigned adler32(
const unsigned char*
data,
unsigned len) {
2159 return update_adler32(1u,
data, len);
2166#ifdef LODEPNG_COMPILE_DECODER
2168static unsigned lodepng_zlib_decompressv(
ucvector* out,
2169 const unsigned char* in,
size_t insize,
2172 unsigned CM, CINFO, FDICT;
2174 if(insize < 2)
return 53;
2176 if((in[0] * 256 + in[1]) % 31 != 0) {
2182 CINFO = (in[0] >> 4) & 15;
2184 FDICT = (in[1] >> 5) & 1;
2187 if(CM != 8 || CINFO > 7) {
2197 error = inflatev(out, in + 2, insize - 2, settings);
2198 if(error)
return error;
2201 unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]);
2202 unsigned checksum = adler32(out->
data, (
unsigned)(out->
size));
2203 if(checksum != ADLER32)
return 58;
2213 unsigned error = lodepng_zlib_decompressv(&
v, in, insize, settings);
2220static unsigned zlib_decompress(
unsigned char** out,
size_t* outsize,
size_t expected_size,
2224 error = settings->
custom_zlib(out, outsize, in, insize, settings);
2238 error = lodepng_zlib_decompressv(&
v, in, insize, settings);
2247#ifdef LODEPNG_COMPILE_ENCODER
2253 unsigned char* deflatedata = 0;
2254 size_t deflatesize = 0;
2256 error = deflate(&deflatedata, &deflatesize, in, insize, settings);
2261 *outsize = deflatesize + 6;
2263 if(!*out) error = 83;
2267 unsigned ADLER32 = adler32(in, (
unsigned)insize);
2270 unsigned FLEVEL = 0;
2272 unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
2273 unsigned FCHECK = 31 - CMFFLG % 31;
2276 (*out)[0] = (
unsigned char)(CMFFLG >> 8);
2277 (*out)[1] = (
unsigned char)(CMFFLG & 255);
2278 for(i = 0; i != deflatesize; ++i) (*out)[i + 2] = deflatedata[i];
2279 lodepng_set32bitInt(&(*out)[*outsize - 4], ADLER32);
2287static unsigned zlib_compress(
unsigned char** out,
size_t* outsize,
const unsigned char* in,
2290 unsigned error = settings->
custom_zlib(out, outsize, in, insize, settings);
2292 return error ? 111 : 0;
2302#ifdef LODEPNG_COMPILE_DECODER
2303static unsigned zlib_decompress(
unsigned char** out,
size_t* outsize,
size_t expected_size,
2306 (
void)expected_size;
2307 return settings->
custom_zlib(out, outsize, in, insize, settings);
2310#ifdef LODEPNG_COMPILE_ENCODER
2311static unsigned zlib_compress(
unsigned char** out,
size_t* outsize,
const unsigned char* in,
2314 return settings->
custom_zlib(out, outsize, in, insize, settings);
2322#ifdef LODEPNG_COMPILE_ENCODER
2325#define DEFAULT_WINDOWSIZE 2048
2329 settings->
btype = 2;
2346#ifdef LODEPNG_COMPILE_DECODER
2368#ifdef LODEPNG_COMPILE_PNG
2375#ifdef LODEPNG_COMPILE_CRC
2377static const unsigned lodepng_crc32_table0[256] = {
2378 0x00000000u, 0x77073096u, 0xee0e612cu, 0x990951bau, 0x076dc419u, 0x706af48fu, 0xe963a535u, 0x9e6495a3u,
2379 0x0edb8832u, 0x79dcb8a4u, 0xe0d5e91eu, 0x97d2d988u, 0x09b64c2bu, 0x7eb17cbdu, 0xe7b82d07u, 0x90bf1d91u,
2380 0x1db71064u, 0x6ab020f2u, 0xf3b97148u, 0x84be41deu, 0x1adad47du, 0x6ddde4ebu, 0xf4d4b551u, 0x83d385c7u,
2381 0x136c9856u, 0x646ba8c0u, 0xfd62f97au, 0x8a65c9ecu, 0x14015c4fu, 0x63066cd9u, 0xfa0f3d63u, 0x8d080df5u,
2382 0x3b6e20c8u, 0x4c69105eu, 0xd56041e4u, 0xa2677172u, 0x3c03e4d1u, 0x4b04d447u, 0xd20d85fdu, 0xa50ab56bu,
2383 0x35b5a8fau, 0x42b2986cu, 0xdbbbc9d6u, 0xacbcf940u, 0x32d86ce3u, 0x45df5c75u, 0xdcd60dcfu, 0xabd13d59u,
2384 0x26d930acu, 0x51de003au, 0xc8d75180u, 0xbfd06116u, 0x21b4f4b5u, 0x56b3c423u, 0xcfba9599u, 0xb8bda50fu,
2385 0x2802b89eu, 0x5f058808u, 0xc60cd9b2u, 0xb10be924u, 0x2f6f7c87u, 0x58684c11u, 0xc1611dabu, 0xb6662d3du,
2386 0x76dc4190u, 0x01db7106u, 0x98d220bcu, 0xefd5102au, 0x71b18589u, 0x06b6b51fu, 0x9fbfe4a5u, 0xe8b8d433u,
2387 0x7807c9a2u, 0x0f00f934u, 0x9609a88eu, 0xe10e9818u, 0x7f6a0dbbu, 0x086d3d2du, 0x91646c97u, 0xe6635c01u,
2388 0x6b6b51f4u, 0x1c6c6162u, 0x856530d8u, 0xf262004eu, 0x6c0695edu, 0x1b01a57bu, 0x8208f4c1u, 0xf50fc457u,
2389 0x65b0d9c6u, 0x12b7e950u, 0x8bbeb8eau, 0xfcb9887cu, 0x62dd1ddfu, 0x15da2d49u, 0x8cd37cf3u, 0xfbd44c65u,
2390 0x4db26158u, 0x3ab551ceu, 0xa3bc0074u, 0xd4bb30e2u, 0x4adfa541u, 0x3dd895d7u, 0xa4d1c46du, 0xd3d6f4fbu,
2391 0x4369e96au, 0x346ed9fcu, 0xad678846u, 0xda60b8d0u, 0x44042d73u, 0x33031de5u, 0xaa0a4c5fu, 0xdd0d7cc9u,
2392 0x5005713cu, 0x270241aau, 0xbe0b1010u, 0xc90c2086u, 0x5768b525u, 0x206f85b3u, 0xb966d409u, 0xce61e49fu,
2393 0x5edef90eu, 0x29d9c998u, 0xb0d09822u, 0xc7d7a8b4u, 0x59b33d17u, 0x2eb40d81u, 0xb7bd5c3bu, 0xc0ba6cadu,
2394 0xedb88320u, 0x9abfb3b6u, 0x03b6e20cu, 0x74b1d29au, 0xead54739u, 0x9dd277afu, 0x04db2615u, 0x73dc1683u,
2395 0xe3630b12u, 0x94643b84u, 0x0d6d6a3eu, 0x7a6a5aa8u, 0xe40ecf0bu, 0x9309ff9du, 0x0a00ae27u, 0x7d079eb1u,
2396 0xf00f9344u, 0x8708a3d2u, 0x1e01f268u, 0x6906c2feu, 0xf762575du, 0x806567cbu, 0x196c3671u, 0x6e6b06e7u,
2397 0xfed41b76u, 0x89d32be0u, 0x10da7a5au, 0x67dd4accu, 0xf9b9df6fu, 0x8ebeeff9u, 0x17b7be43u, 0x60b08ed5u,
2398 0xd6d6a3e8u, 0xa1d1937eu, 0x38d8c2c4u, 0x4fdff252u, 0xd1bb67f1u, 0xa6bc5767u, 0x3fb506ddu, 0x48b2364bu,
2399 0xd80d2bdau, 0xaf0a1b4cu, 0x36034af6u, 0x41047a60u, 0xdf60efc3u, 0xa867df55u, 0x316e8eefu, 0x4669be79u,
2400 0xcb61b38cu, 0xbc66831au, 0x256fd2a0u, 0x5268e236u, 0xcc0c7795u, 0xbb0b4703u, 0x220216b9u, 0x5505262fu,
2401 0xc5ba3bbeu, 0xb2bd0b28u, 0x2bb45a92u, 0x5cb36a04u, 0xc2d7ffa7u, 0xb5d0cf31u, 0x2cd99e8bu, 0x5bdeae1du,
2402 0x9b64c2b0u, 0xec63f226u, 0x756aa39cu, 0x026d930au, 0x9c0906a9u, 0xeb0e363fu, 0x72076785u, 0x05005713u,
2403 0x95bf4a82u, 0xe2b87a14u, 0x7bb12baeu, 0x0cb61b38u, 0x92d28e9bu, 0xe5d5be0du, 0x7cdcefb7u, 0x0bdbdf21u,
2404 0x86d3d2d4u, 0xf1d4e242u, 0x68ddb3f8u, 0x1fda836eu, 0x81be16cdu, 0xf6b9265bu, 0x6fb077e1u, 0x18b74777u,
2405 0x88085ae6u, 0xff0f6a70u, 0x66063bcau, 0x11010b5cu, 0x8f659effu, 0xf862ae69u, 0x616bffd3u, 0x166ccf45u,
2406 0xa00ae278u, 0xd70dd2eeu, 0x4e048354u, 0x3903b3c2u, 0xa7672661u, 0xd06016f7u, 0x4969474du, 0x3e6e77dbu,
2407 0xaed16a4au, 0xd9d65adcu, 0x40df0b66u, 0x37d83bf0u, 0xa9bcae53u, 0xdebb9ec5u, 0x47b2cf7fu, 0x30b5ffe9u,
2408 0xbdbdf21cu, 0xcabac28au, 0x53b39330u, 0x24b4a3a6u, 0xbad03605u, 0xcdd70693u, 0x54de5729u, 0x23d967bfu,
2409 0xb3667a2eu, 0xc4614ab8u, 0x5d681b02u, 0x2a6f2b94u, 0xb40bbe37u, 0xc30c8ea1u, 0x5a05df1bu, 0x2d02ef8du
2412static const unsigned lodepng_crc32_table1[256] = {
2413 0x00000000u, 0x191b3141u, 0x32366282u, 0x2b2d53c3u, 0x646cc504u, 0x7d77f445u, 0x565aa786u, 0x4f4196c7u,
2414 0xc8d98a08u, 0xd1c2bb49u, 0xfaefe88au, 0xe3f4d9cbu, 0xacb54f0cu, 0xb5ae7e4du, 0x9e832d8eu, 0x87981ccfu,
2415 0x4ac21251u, 0x53d92310u, 0x78f470d3u, 0x61ef4192u, 0x2eaed755u, 0x37b5e614u, 0x1c98b5d7u, 0x05838496u,
2416 0x821b9859u, 0x9b00a918u, 0xb02dfadbu, 0xa936cb9au, 0xe6775d5du, 0xff6c6c1cu, 0xd4413fdfu, 0xcd5a0e9eu,
2417 0x958424a2u, 0x8c9f15e3u, 0xa7b24620u, 0xbea97761u, 0xf1e8e1a6u, 0xe8f3d0e7u, 0xc3de8324u, 0xdac5b265u,
2418 0x5d5daeaau, 0x44469febu, 0x6f6bcc28u, 0x7670fd69u, 0x39316baeu, 0x202a5aefu, 0x0b07092cu, 0x121c386du,
2419 0xdf4636f3u, 0xc65d07b2u, 0xed705471u, 0xf46b6530u, 0xbb2af3f7u, 0xa231c2b6u, 0x891c9175u, 0x9007a034u,
2420 0x179fbcfbu, 0x0e848dbau, 0x25a9de79u, 0x3cb2ef38u, 0x73f379ffu, 0x6ae848beu, 0x41c51b7du, 0x58de2a3cu,
2421 0xf0794f05u, 0xe9627e44u, 0xc24f2d87u, 0xdb541cc6u, 0x94158a01u, 0x8d0ebb40u, 0xa623e883u, 0xbf38d9c2u,
2422 0x38a0c50du, 0x21bbf44cu, 0x0a96a78fu, 0x138d96ceu, 0x5ccc0009u, 0x45d73148u, 0x6efa628bu, 0x77e153cau,
2423 0xbabb5d54u, 0xa3a06c15u, 0x888d3fd6u, 0x91960e97u, 0xded79850u, 0xc7cca911u, 0xece1fad2u, 0xf5facb93u,
2424 0x7262d75cu, 0x6b79e61du, 0x4054b5deu, 0x594f849fu, 0x160e1258u, 0x0f152319u, 0x243870dau, 0x3d23419bu,
2425 0x65fd6ba7u, 0x7ce65ae6u, 0x57cb0925u, 0x4ed03864u, 0x0191aea3u, 0x188a9fe2u, 0x33a7cc21u, 0x2abcfd60u,
2426 0xad24e1afu, 0xb43fd0eeu, 0x9f12832du, 0x8609b26cu, 0xc94824abu, 0xd05315eau, 0xfb7e4629u, 0xe2657768u,
2427 0x2f3f79f6u, 0x362448b7u, 0x1d091b74u, 0x04122a35u, 0x4b53bcf2u, 0x52488db3u, 0x7965de70u, 0x607eef31u,
2428 0xe7e6f3feu, 0xfefdc2bfu, 0xd5d0917cu, 0xcccba03du, 0x838a36fau, 0x9a9107bbu, 0xb1bc5478u, 0xa8a76539u,
2429 0x3b83984bu, 0x2298a90au, 0x09b5fac9u, 0x10aecb88u, 0x5fef5d4fu, 0x46f46c0eu, 0x6dd93fcdu, 0x74c20e8cu,
2430 0xf35a1243u, 0xea412302u, 0xc16c70c1u, 0xd8774180u, 0x9736d747u, 0x8e2de606u, 0xa500b5c5u, 0xbc1b8484u,
2431 0x71418a1au, 0x685abb5bu, 0x4377e898u, 0x5a6cd9d9u, 0x152d4f1eu, 0x0c367e5fu, 0x271b2d9cu, 0x3e001cddu,
2432 0xb9980012u, 0xa0833153u, 0x8bae6290u, 0x92b553d1u, 0xddf4c516u, 0xc4eff457u, 0xefc2a794u, 0xf6d996d5u,
2433 0xae07bce9u, 0xb71c8da8u, 0x9c31de6bu, 0x852aef2au, 0xca6b79edu, 0xd37048acu, 0xf85d1b6fu, 0xe1462a2eu,
2434 0x66de36e1u, 0x7fc507a0u, 0x54e85463u, 0x4df36522u, 0x02b2f3e5u, 0x1ba9c2a4u, 0x30849167u, 0x299fa026u,
2435 0xe4c5aeb8u, 0xfdde9ff9u, 0xd6f3cc3au, 0xcfe8fd7bu, 0x80a96bbcu, 0x99b25afdu, 0xb29f093eu, 0xab84387fu,
2436 0x2c1c24b0u, 0x350715f1u, 0x1e2a4632u, 0x07317773u, 0x4870e1b4u, 0x516bd0f5u, 0x7a468336u, 0x635db277u,
2437 0xcbfad74eu, 0xd2e1e60fu, 0xf9ccb5ccu, 0xe0d7848du, 0xaf96124au, 0xb68d230bu, 0x9da070c8u, 0x84bb4189u,
2438 0x03235d46u, 0x1a386c07u, 0x31153fc4u, 0x280e0e85u, 0x674f9842u, 0x7e54a903u, 0x5579fac0u, 0x4c62cb81u,
2439 0x8138c51fu, 0x9823f45eu, 0xb30ea79du, 0xaa1596dcu, 0xe554001bu, 0xfc4f315au, 0xd7626299u, 0xce7953d8u,
2440 0x49e14f17u, 0x50fa7e56u, 0x7bd72d95u, 0x62cc1cd4u, 0x2d8d8a13u, 0x3496bb52u, 0x1fbbe891u, 0x06a0d9d0u,
2441 0x5e7ef3ecu, 0x4765c2adu, 0x6c48916eu, 0x7553a02fu, 0x3a1236e8u, 0x230907a9u, 0x0824546au, 0x113f652bu,
2442 0x96a779e4u, 0x8fbc48a5u, 0xa4911b66u, 0xbd8a2a27u, 0xf2cbbce0u, 0xebd08da1u, 0xc0fdde62u, 0xd9e6ef23u,
2443 0x14bce1bdu, 0x0da7d0fcu, 0x268a833fu, 0x3f91b27eu, 0x70d024b9u, 0x69cb15f8u, 0x42e6463bu, 0x5bfd777au,
2444 0xdc656bb5u, 0xc57e5af4u, 0xee530937u, 0xf7483876u, 0xb809aeb1u, 0xa1129ff0u, 0x8a3fcc33u, 0x9324fd72u
2447static const unsigned lodepng_crc32_table2[256] = {
2448 0x00000000u, 0x01c26a37u, 0x0384d46eu, 0x0246be59u, 0x0709a8dcu, 0x06cbc2ebu, 0x048d7cb2u, 0x054f1685u,
2449 0x0e1351b8u, 0x0fd13b8fu, 0x0d9785d6u, 0x0c55efe1u, 0x091af964u, 0x08d89353u, 0x0a9e2d0au, 0x0b5c473du,
2450 0x1c26a370u, 0x1de4c947u, 0x1fa2771eu, 0x1e601d29u, 0x1b2f0bacu, 0x1aed619bu, 0x18abdfc2u, 0x1969b5f5u,
2451 0x1235f2c8u, 0x13f798ffu, 0x11b126a6u, 0x10734c91u, 0x153c5a14u, 0x14fe3023u, 0x16b88e7au, 0x177ae44du,
2452 0x384d46e0u, 0x398f2cd7u, 0x3bc9928eu, 0x3a0bf8b9u, 0x3f44ee3cu, 0x3e86840bu, 0x3cc03a52u, 0x3d025065u,
2453 0x365e1758u, 0x379c7d6fu, 0x35dac336u, 0x3418a901u, 0x3157bf84u, 0x3095d5b3u, 0x32d36beau, 0x331101ddu,
2454 0x246be590u, 0x25a98fa7u, 0x27ef31feu, 0x262d5bc9u, 0x23624d4cu, 0x22a0277bu, 0x20e69922u, 0x2124f315u,
2455 0x2a78b428u, 0x2bbade1fu, 0x29fc6046u, 0x283e0a71u, 0x2d711cf4u, 0x2cb376c3u, 0x2ef5c89au, 0x2f37a2adu,
2456 0x709a8dc0u, 0x7158e7f7u, 0x731e59aeu, 0x72dc3399u, 0x7793251cu, 0x76514f2bu, 0x7417f172u, 0x75d59b45u,
2457 0x7e89dc78u, 0x7f4bb64fu, 0x7d0d0816u, 0x7ccf6221u, 0x798074a4u, 0x78421e93u, 0x7a04a0cau, 0x7bc6cafdu,
2458 0x6cbc2eb0u, 0x6d7e4487u, 0x6f38fadeu, 0x6efa90e9u, 0x6bb5866cu, 0x6a77ec5bu, 0x68315202u, 0x69f33835u,
2459 0x62af7f08u, 0x636d153fu, 0x612bab66u, 0x60e9c151u, 0x65a6d7d4u, 0x6464bde3u, 0x662203bau, 0x67e0698du,
2460 0x48d7cb20u, 0x4915a117u, 0x4b531f4eu, 0x4a917579u, 0x4fde63fcu, 0x4e1c09cbu, 0x4c5ab792u, 0x4d98dda5u,
2461 0x46c49a98u, 0x4706f0afu, 0x45404ef6u, 0x448224c1u, 0x41cd3244u, 0x400f5873u, 0x4249e62au, 0x438b8c1du,
2462 0x54f16850u, 0x55330267u, 0x5775bc3eu, 0x56b7d609u, 0x53f8c08cu, 0x523aaabbu, 0x507c14e2u, 0x51be7ed5u,
2463 0x5ae239e8u, 0x5b2053dfu, 0x5966ed86u, 0x58a487b1u, 0x5deb9134u, 0x5c29fb03u, 0x5e6f455au, 0x5fad2f6du,
2464 0xe1351b80u, 0xe0f771b7u, 0xe2b1cfeeu, 0xe373a5d9u, 0xe63cb35cu, 0xe7fed96bu, 0xe5b86732u, 0xe47a0d05u,
2465 0xef264a38u, 0xeee4200fu, 0xeca29e56u, 0xed60f461u, 0xe82fe2e4u, 0xe9ed88d3u, 0xebab368au, 0xea695cbdu,
2466 0xfd13b8f0u, 0xfcd1d2c7u, 0xfe976c9eu, 0xff5506a9u, 0xfa1a102cu, 0xfbd87a1bu, 0xf99ec442u, 0xf85cae75u,
2467 0xf300e948u, 0xf2c2837fu, 0xf0843d26u, 0xf1465711u, 0xf4094194u, 0xf5cb2ba3u, 0xf78d95fau, 0xf64fffcdu,
2468 0xd9785d60u, 0xd8ba3757u, 0xdafc890eu, 0xdb3ee339u, 0xde71f5bcu, 0xdfb39f8bu, 0xddf521d2u, 0xdc374be5u,
2469 0xd76b0cd8u, 0xd6a966efu, 0xd4efd8b6u, 0xd52db281u, 0xd062a404u, 0xd1a0ce33u, 0xd3e6706au, 0xd2241a5du,
2470 0xc55efe10u, 0xc49c9427u, 0xc6da2a7eu, 0xc7184049u, 0xc25756ccu, 0xc3953cfbu, 0xc1d382a2u, 0xc011e895u,
2471 0xcb4dafa8u, 0xca8fc59fu, 0xc8c97bc6u, 0xc90b11f1u, 0xcc440774u, 0xcd866d43u, 0xcfc0d31au, 0xce02b92du,
2472 0x91af9640u, 0x906dfc77u, 0x922b422eu, 0x93e92819u, 0x96a63e9cu, 0x976454abu, 0x9522eaf2u, 0x94e080c5u,
2473 0x9fbcc7f8u, 0x9e7eadcfu, 0x9c381396u, 0x9dfa79a1u, 0x98b56f24u, 0x99770513u, 0x9b31bb4au, 0x9af3d17du,
2474 0x8d893530u, 0x8c4b5f07u, 0x8e0de15eu, 0x8fcf8b69u, 0x8a809decu, 0x8b42f7dbu, 0x89044982u, 0x88c623b5u,
2475 0x839a6488u, 0x82580ebfu, 0x801eb0e6u, 0x81dcdad1u, 0x8493cc54u, 0x8551a663u, 0x8717183au, 0x86d5720du,
2476 0xa9e2d0a0u, 0xa820ba97u, 0xaa6604ceu, 0xaba46ef9u, 0xaeeb787cu, 0xaf29124bu, 0xad6fac12u, 0xacadc625u,
2477 0xa7f18118u, 0xa633eb2fu, 0xa4755576u, 0xa5b73f41u, 0xa0f829c4u, 0xa13a43f3u, 0xa37cfdaau, 0xa2be979du,
2478 0xb5c473d0u, 0xb40619e7u, 0xb640a7beu, 0xb782cd89u, 0xb2cddb0cu, 0xb30fb13bu, 0xb1490f62u, 0xb08b6555u,
2479 0xbbd72268u, 0xba15485fu, 0xb853f606u, 0xb9919c31u, 0xbcde8ab4u, 0xbd1ce083u, 0xbf5a5edau, 0xbe9834edu
2482static const unsigned lodepng_crc32_table3[256] = {
2483 0x00000000u, 0xb8bc6765u, 0xaa09c88bu, 0x12b5afeeu, 0x8f629757u, 0x37def032u, 0x256b5fdcu, 0x9dd738b9u,
2484 0xc5b428efu, 0x7d084f8au, 0x6fbde064u, 0xd7018701u, 0x4ad6bfb8u, 0xf26ad8ddu, 0xe0df7733u, 0x58631056u,
2485 0x5019579fu, 0xe8a530fau, 0xfa109f14u, 0x42acf871u, 0xdf7bc0c8u, 0x67c7a7adu, 0x75720843u, 0xcdce6f26u,
2486 0x95ad7f70u, 0x2d111815u, 0x3fa4b7fbu, 0x8718d09eu, 0x1acfe827u, 0xa2738f42u, 0xb0c620acu, 0x087a47c9u,
2487 0xa032af3eu, 0x188ec85bu, 0x0a3b67b5u, 0xb28700d0u, 0x2f503869u, 0x97ec5f0cu, 0x8559f0e2u, 0x3de59787u,
2488 0x658687d1u, 0xdd3ae0b4u, 0xcf8f4f5au, 0x7733283fu, 0xeae41086u, 0x525877e3u, 0x40edd80du, 0xf851bf68u,
2489 0xf02bf8a1u, 0x48979fc4u, 0x5a22302au, 0xe29e574fu, 0x7f496ff6u, 0xc7f50893u, 0xd540a77du, 0x6dfcc018u,
2490 0x359fd04eu, 0x8d23b72bu, 0x9f9618c5u, 0x272a7fa0u, 0xbafd4719u, 0x0241207cu, 0x10f48f92u, 0xa848e8f7u,
2491 0x9b14583du, 0x23a83f58u, 0x311d90b6u, 0x89a1f7d3u, 0x1476cf6au, 0xaccaa80fu, 0xbe7f07e1u, 0x06c36084u,
2492 0x5ea070d2u, 0xe61c17b7u, 0xf4a9b859u, 0x4c15df3cu, 0xd1c2e785u, 0x697e80e0u, 0x7bcb2f0eu, 0xc377486bu,
2493 0xcb0d0fa2u, 0x73b168c7u, 0x6104c729u, 0xd9b8a04cu, 0x446f98f5u, 0xfcd3ff90u, 0xee66507eu, 0x56da371bu,
2494 0x0eb9274du, 0xb6054028u, 0xa4b0efc6u, 0x1c0c88a3u, 0x81dbb01au, 0x3967d77fu, 0x2bd27891u, 0x936e1ff4u,
2495 0x3b26f703u, 0x839a9066u, 0x912f3f88u, 0x299358edu, 0xb4446054u, 0x0cf80731u, 0x1e4da8dfu, 0xa6f1cfbau,
2496 0xfe92dfecu, 0x462eb889u, 0x549b1767u, 0xec277002u, 0x71f048bbu, 0xc94c2fdeu, 0xdbf98030u, 0x6345e755u,
2497 0x6b3fa09cu, 0xd383c7f9u, 0xc1366817u, 0x798a0f72u, 0xe45d37cbu, 0x5ce150aeu, 0x4e54ff40u, 0xf6e89825u,
2498 0xae8b8873u, 0x1637ef16u, 0x048240f8u, 0xbc3e279du, 0x21e91f24u, 0x99557841u, 0x8be0d7afu, 0x335cb0cau,
2499 0xed59b63bu, 0x55e5d15eu, 0x47507eb0u, 0xffec19d5u, 0x623b216cu, 0xda874609u, 0xc832e9e7u, 0x708e8e82u,
2500 0x28ed9ed4u, 0x9051f9b1u, 0x82e4565fu, 0x3a58313au, 0xa78f0983u, 0x1f336ee6u, 0x0d86c108u, 0xb53aa66du,
2501 0xbd40e1a4u, 0x05fc86c1u, 0x1749292fu, 0xaff54e4au, 0x322276f3u, 0x8a9e1196u, 0x982bbe78u, 0x2097d91du,
2502 0x78f4c94bu, 0xc048ae2eu, 0xd2fd01c0u, 0x6a4166a5u, 0xf7965e1cu, 0x4f2a3979u, 0x5d9f9697u, 0xe523f1f2u,
2503 0x4d6b1905u, 0xf5d77e60u, 0xe762d18eu, 0x5fdeb6ebu, 0xc2098e52u, 0x7ab5e937u, 0x680046d9u, 0xd0bc21bcu,
2504 0x88df31eau, 0x3063568fu, 0x22d6f961u, 0x9a6a9e04u, 0x07bda6bdu, 0xbf01c1d8u, 0xadb46e36u, 0x15080953u,
2505 0x1d724e9au, 0xa5ce29ffu, 0xb77b8611u, 0x0fc7e174u, 0x9210d9cdu, 0x2aacbea8u, 0x38191146u, 0x80a57623u,
2506 0xd8c66675u, 0x607a0110u, 0x72cfaefeu, 0xca73c99bu, 0x57a4f122u, 0xef189647u, 0xfdad39a9u, 0x45115eccu,
2507 0x764dee06u, 0xcef18963u, 0xdc44268du, 0x64f841e8u, 0xf92f7951u, 0x41931e34u, 0x5326b1dau, 0xeb9ad6bfu,
2508 0xb3f9c6e9u, 0x0b45a18cu, 0x19f00e62u, 0xa14c6907u, 0x3c9b51beu, 0x842736dbu, 0x96929935u, 0x2e2efe50u,
2509 0x2654b999u, 0x9ee8defcu, 0x8c5d7112u, 0x34e11677u, 0xa9362eceu, 0x118a49abu, 0x033fe645u, 0xbb838120u,
2510 0xe3e09176u, 0x5b5cf613u, 0x49e959fdu, 0xf1553e98u, 0x6c820621u, 0xd43e6144u, 0xc68bceaau, 0x7e37a9cfu,
2511 0xd67f4138u, 0x6ec3265du, 0x7c7689b3u, 0xc4caeed6u, 0x591dd66fu, 0xe1a1b10au, 0xf3141ee4u, 0x4ba87981u,
2512 0x13cb69d7u, 0xab770eb2u, 0xb9c2a15cu, 0x017ec639u, 0x9ca9fe80u, 0x241599e5u, 0x36a0360bu, 0x8e1c516eu,
2513 0x866616a7u, 0x3eda71c2u, 0x2c6fde2cu, 0x94d3b949u, 0x090481f0u, 0xb1b8e695u, 0xa30d497bu, 0x1bb12e1eu,
2514 0x43d23e48u, 0xfb6e592du, 0xe9dbf6c3u, 0x516791a6u, 0xccb0a91fu, 0x740cce7au, 0x66b96194u, 0xde0506f1u
2517static const unsigned lodepng_crc32_table4[256] = {
2518 0x00000000u, 0x3d6029b0u, 0x7ac05360u, 0x47a07ad0u, 0xf580a6c0u, 0xc8e08f70u, 0x8f40f5a0u, 0xb220dc10u,
2519 0x30704bc1u, 0x0d106271u, 0x4ab018a1u, 0x77d03111u, 0xc5f0ed01u, 0xf890c4b1u, 0xbf30be61u, 0x825097d1u,
2520 0x60e09782u, 0x5d80be32u, 0x1a20c4e2u, 0x2740ed52u, 0x95603142u, 0xa80018f2u, 0xefa06222u, 0xd2c04b92u,
2521 0x5090dc43u, 0x6df0f5f3u, 0x2a508f23u, 0x1730a693u, 0xa5107a83u, 0x98705333u, 0xdfd029e3u, 0xe2b00053u,
2522 0xc1c12f04u, 0xfca106b4u, 0xbb017c64u, 0x866155d4u, 0x344189c4u, 0x0921a074u, 0x4e81daa4u, 0x73e1f314u,
2523 0xf1b164c5u, 0xccd14d75u, 0x8b7137a5u, 0xb6111e15u, 0x0431c205u, 0x3951ebb5u, 0x7ef19165u, 0x4391b8d5u,
2524 0xa121b886u, 0x9c419136u, 0xdbe1ebe6u, 0xe681c256u, 0x54a11e46u, 0x69c137f6u, 0x2e614d26u, 0x13016496u,
2525 0x9151f347u, 0xac31daf7u, 0xeb91a027u, 0xd6f18997u, 0x64d15587u, 0x59b17c37u, 0x1e1106e7u, 0x23712f57u,
2526 0x58f35849u, 0x659371f9u, 0x22330b29u, 0x1f532299u, 0xad73fe89u, 0x9013d739u, 0xd7b3ade9u, 0xead38459u,
2527 0x68831388u, 0x55e33a38u, 0x124340e8u, 0x2f236958u, 0x9d03b548u, 0xa0639cf8u, 0xe7c3e628u, 0xdaa3cf98u,
2528 0x3813cfcbu, 0x0573e67bu, 0x42d39cabu, 0x7fb3b51bu, 0xcd93690bu, 0xf0f340bbu, 0xb7533a6bu, 0x8a3313dbu,
2529 0x0863840au, 0x3503adbau, 0x72a3d76au, 0x4fc3fedau, 0xfde322cau, 0xc0830b7au, 0x872371aau, 0xba43581au,
2530 0x9932774du, 0xa4525efdu, 0xe3f2242du, 0xde920d9du, 0x6cb2d18du, 0x51d2f83du, 0x167282edu, 0x2b12ab5du,
2531 0xa9423c8cu, 0x9422153cu, 0xd3826fecu, 0xeee2465cu, 0x5cc29a4cu, 0x61a2b3fcu, 0x2602c92cu, 0x1b62e09cu,
2532 0xf9d2e0cfu, 0xc4b2c97fu, 0x8312b3afu, 0xbe729a1fu, 0x0c52460fu, 0x31326fbfu, 0x7692156fu, 0x4bf23cdfu,
2533 0xc9a2ab0eu, 0xf4c282beu, 0xb362f86eu, 0x8e02d1deu, 0x3c220dceu, 0x0142247eu, 0x46e25eaeu, 0x7b82771eu,
2534 0xb1e6b092u, 0x8c869922u, 0xcb26e3f2u, 0xf646ca42u, 0x44661652u, 0x79063fe2u, 0x3ea64532u, 0x03c66c82u,
2535 0x8196fb53u, 0xbcf6d2e3u, 0xfb56a833u, 0xc6368183u, 0x74165d93u, 0x49767423u, 0x0ed60ef3u, 0x33b62743u,
2536 0xd1062710u, 0xec660ea0u, 0xabc67470u, 0x96a65dc0u, 0x248681d0u, 0x19e6a860u, 0x5e46d2b0u, 0x6326fb00u,
2537 0xe1766cd1u, 0xdc164561u, 0x9bb63fb1u, 0xa6d61601u, 0x14f6ca11u, 0x2996e3a1u, 0x6e369971u, 0x5356b0c1u,
2538 0x70279f96u, 0x4d47b626u, 0x0ae7ccf6u, 0x3787e546u, 0x85a73956u, 0xb8c710e6u, 0xff676a36u, 0xc2074386u,
2539 0x4057d457u, 0x7d37fde7u, 0x3a978737u, 0x07f7ae87u, 0xb5d77297u, 0x88b75b27u, 0xcf1721f7u, 0xf2770847u,
2540 0x10c70814u, 0x2da721a4u, 0x6a075b74u, 0x576772c4u, 0xe547aed4u, 0xd8278764u, 0x9f87fdb4u, 0xa2e7d404u,
2541 0x20b743d5u, 0x1dd76a65u, 0x5a7710b5u, 0x67173905u, 0xd537e515u, 0xe857cca5u, 0xaff7b675u, 0x92979fc5u,
2542 0xe915e8dbu, 0xd475c16bu, 0x93d5bbbbu, 0xaeb5920bu, 0x1c954e1bu, 0x21f567abu, 0x66551d7bu, 0x5b3534cbu,
2543 0xd965a31au, 0xe4058aaau, 0xa3a5f07au, 0x9ec5d9cau, 0x2ce505dau, 0x11852c6au, 0x562556bau, 0x6b457f0au,
2544 0x89f57f59u, 0xb49556e9u, 0xf3352c39u, 0xce550589u, 0x7c75d999u, 0x4115f029u, 0x06b58af9u, 0x3bd5a349u,
2545 0xb9853498u, 0x84e51d28u, 0xc34567f8u, 0xfe254e48u, 0x4c059258u, 0x7165bbe8u, 0x36c5c138u, 0x0ba5e888u,
2546 0x28d4c7dfu, 0x15b4ee6fu, 0x521494bfu, 0x6f74bd0fu, 0xdd54611fu, 0xe03448afu, 0xa794327fu, 0x9af41bcfu,
2547 0x18a48c1eu, 0x25c4a5aeu, 0x6264df7eu, 0x5f04f6ceu, 0xed242adeu, 0xd044036eu, 0x97e479beu, 0xaa84500eu,
2548 0x4834505du, 0x755479edu, 0x32f4033du, 0x0f942a8du, 0xbdb4f69du, 0x80d4df2du, 0xc774a5fdu, 0xfa148c4du,
2549 0x78441b9cu, 0x4524322cu, 0x028448fcu, 0x3fe4614cu, 0x8dc4bd5cu, 0xb0a494ecu, 0xf704ee3cu, 0xca64c78cu
2552static const unsigned lodepng_crc32_table5[256] = {
2553 0x00000000u, 0xcb5cd3a5u, 0x4dc8a10bu, 0x869472aeu, 0x9b914216u, 0x50cd91b3u, 0xd659e31du, 0x1d0530b8u,
2554 0xec53826du, 0x270f51c8u, 0xa19b2366u, 0x6ac7f0c3u, 0x77c2c07bu, 0xbc9e13deu, 0x3a0a6170u, 0xf156b2d5u,
2555 0x03d6029bu, 0xc88ad13eu, 0x4e1ea390u, 0x85427035u, 0x9847408du, 0x531b9328u, 0xd58fe186u, 0x1ed33223u,
2556 0xef8580f6u, 0x24d95353u, 0xa24d21fdu, 0x6911f258u, 0x7414c2e0u, 0xbf481145u, 0x39dc63ebu, 0xf280b04eu,
2557 0x07ac0536u, 0xccf0d693u, 0x4a64a43du, 0x81387798u, 0x9c3d4720u, 0x57619485u, 0xd1f5e62bu, 0x1aa9358eu,
2558 0xebff875bu, 0x20a354feu, 0xa6372650u, 0x6d6bf5f5u, 0x706ec54du, 0xbb3216e8u, 0x3da66446u, 0xf6fab7e3u,
2559 0x047a07adu, 0xcf26d408u, 0x49b2a6a6u, 0x82ee7503u, 0x9feb45bbu, 0x54b7961eu, 0xd223e4b0u, 0x197f3715u,
2560 0xe82985c0u, 0x23755665u, 0xa5e124cbu, 0x6ebdf76eu, 0x73b8c7d6u, 0xb8e41473u, 0x3e7066ddu, 0xf52cb578u,
2561 0x0f580a6cu, 0xc404d9c9u, 0x4290ab67u, 0x89cc78c2u, 0x94c9487au, 0x5f959bdfu, 0xd901e971u, 0x125d3ad4u,
2562 0xe30b8801u, 0x28575ba4u, 0xaec3290au, 0x659ffaafu, 0x789aca17u, 0xb3c619b2u, 0x35526b1cu, 0xfe0eb8b9u,
2563 0x0c8e08f7u, 0xc7d2db52u, 0x4146a9fcu, 0x8a1a7a59u, 0x971f4ae1u, 0x5c439944u, 0xdad7ebeau, 0x118b384fu,
2564 0xe0dd8a9au, 0x2b81593fu, 0xad152b91u, 0x6649f834u, 0x7b4cc88cu, 0xb0101b29u, 0x36846987u, 0xfdd8ba22u,
2565 0x08f40f5au, 0xc3a8dcffu, 0x453cae51u, 0x8e607df4u, 0x93654d4cu, 0x58399ee9u, 0xdeadec47u, 0x15f13fe2u,
2566 0xe4a78d37u, 0x2ffb5e92u, 0xa96f2c3cu, 0x6233ff99u, 0x7f36cf21u, 0xb46a1c84u, 0x32fe6e2au, 0xf9a2bd8fu,
2567 0x0b220dc1u, 0xc07ede64u, 0x46eaaccau, 0x8db67f6fu, 0x90b34fd7u, 0x5bef9c72u, 0xdd7beedcu, 0x16273d79u,
2568 0xe7718facu, 0x2c2d5c09u, 0xaab92ea7u, 0x61e5fd02u, 0x7ce0cdbau, 0xb7bc1e1fu, 0x31286cb1u, 0xfa74bf14u,
2569 0x1eb014d8u, 0xd5ecc77du, 0x5378b5d3u, 0x98246676u, 0x852156ceu, 0x4e7d856bu, 0xc8e9f7c5u, 0x03b52460u,
2570 0xf2e396b5u, 0x39bf4510u, 0xbf2b37beu, 0x7477e41bu, 0x6972d4a3u, 0xa22e0706u, 0x24ba75a8u, 0xefe6a60du,
2571 0x1d661643u, 0xd63ac5e6u, 0x50aeb748u, 0x9bf264edu, 0x86f75455u, 0x4dab87f0u, 0xcb3ff55eu, 0x006326fbu,
2572 0xf135942eu, 0x3a69478bu, 0xbcfd3525u, 0x77a1e680u, 0x6aa4d638u, 0xa1f8059du, 0x276c7733u, 0xec30a496u,
2573 0x191c11eeu, 0xd240c24bu, 0x54d4b0e5u, 0x9f886340u, 0x828d53f8u, 0x49d1805du, 0xcf45f2f3u, 0x04192156u,
2574 0xf54f9383u, 0x3e134026u, 0xb8873288u, 0x73dbe12du, 0x6eded195u, 0xa5820230u, 0x2316709eu, 0xe84aa33bu,
2575 0x1aca1375u, 0xd196c0d0u, 0x5702b27eu, 0x9c5e61dbu, 0x815b5163u, 0x4a0782c6u, 0xcc93f068u, 0x07cf23cdu,
2576 0xf6999118u, 0x3dc542bdu, 0xbb513013u, 0x700de3b6u, 0x6d08d30eu, 0xa65400abu, 0x20c07205u, 0xeb9ca1a0u,
2577 0x11e81eb4u, 0xdab4cd11u, 0x5c20bfbfu, 0x977c6c1au, 0x8a795ca2u, 0x41258f07u, 0xc7b1fda9u, 0x0ced2e0cu,
2578 0xfdbb9cd9u, 0x36e74f7cu, 0xb0733dd2u, 0x7b2fee77u, 0x662adecfu, 0xad760d6au, 0x2be27fc4u, 0xe0beac61u,
2579 0x123e1c2fu, 0xd962cf8au, 0x5ff6bd24u, 0x94aa6e81u, 0x89af5e39u, 0x42f38d9cu, 0xc467ff32u, 0x0f3b2c97u,
2580 0xfe6d9e42u, 0x35314de7u, 0xb3a53f49u, 0x78f9ececu, 0x65fcdc54u, 0xaea00ff1u, 0x28347d5fu, 0xe368aefau,
2581 0x16441b82u, 0xdd18c827u, 0x5b8cba89u, 0x90d0692cu, 0x8dd55994u, 0x46898a31u, 0xc01df89fu, 0x0b412b3au,
2582 0xfa1799efu, 0x314b4a4au, 0xb7df38e4u, 0x7c83eb41u, 0x6186dbf9u, 0xaada085cu, 0x2c4e7af2u, 0xe712a957u,
2583 0x15921919u, 0xdececabcu, 0x585ab812u, 0x93066bb7u, 0x8e035b0fu, 0x455f88aau, 0xc3cbfa04u, 0x089729a1u,
2584 0xf9c19b74u, 0x329d48d1u, 0xb4093a7fu, 0x7f55e9dau, 0x6250d962u, 0xa90c0ac7u, 0x2f987869u, 0xe4c4abccu
2587static const unsigned lodepng_crc32_table6[256] = {
2588 0x00000000u, 0xa6770bb4u, 0x979f1129u, 0x31e81a9du, 0xf44f2413u, 0x52382fa7u, 0x63d0353au, 0xc5a73e8eu,
2589 0x33ef4e67u, 0x959845d3u, 0xa4705f4eu, 0x020754fau, 0xc7a06a74u, 0x61d761c0u, 0x503f7b5du, 0xf64870e9u,
2590 0x67de9cceu, 0xc1a9977au, 0xf0418de7u, 0x56368653u, 0x9391b8ddu, 0x35e6b369u, 0x040ea9f4u, 0xa279a240u,
2591 0x5431d2a9u, 0xf246d91du, 0xc3aec380u, 0x65d9c834u, 0xa07ef6bau, 0x0609fd0eu, 0x37e1e793u, 0x9196ec27u,
2592 0xcfbd399cu, 0x69ca3228u, 0x582228b5u, 0xfe552301u, 0x3bf21d8fu, 0x9d85163bu, 0xac6d0ca6u, 0x0a1a0712u,
2593 0xfc5277fbu, 0x5a257c4fu, 0x6bcd66d2u, 0xcdba6d66u, 0x081d53e8u, 0xae6a585cu, 0x9f8242c1u, 0x39f54975u,
2594 0xa863a552u, 0x0e14aee6u, 0x3ffcb47bu, 0x998bbfcfu, 0x5c2c8141u, 0xfa5b8af5u, 0xcbb39068u, 0x6dc49bdcu,
2595 0x9b8ceb35u, 0x3dfbe081u, 0x0c13fa1cu, 0xaa64f1a8u, 0x6fc3cf26u, 0xc9b4c492u, 0xf85cde0fu, 0x5e2bd5bbu,
2596 0x440b7579u, 0xe27c7ecdu, 0xd3946450u, 0x75e36fe4u, 0xb044516au, 0x16335adeu, 0x27db4043u, 0x81ac4bf7u,
2597 0x77e43b1eu, 0xd19330aau, 0xe07b2a37u, 0x460c2183u, 0x83ab1f0du, 0x25dc14b9u, 0x14340e24u, 0xb2430590u,
2598 0x23d5e9b7u, 0x85a2e203u, 0xb44af89eu, 0x123df32au, 0xd79acda4u, 0x71edc610u, 0x4005dc8du, 0xe672d739u,
2599 0x103aa7d0u, 0xb64dac64u, 0x87a5b6f9u, 0x21d2bd4du, 0xe47583c3u, 0x42028877u, 0x73ea92eau, 0xd59d995eu,
2600 0x8bb64ce5u, 0x2dc14751u, 0x1c295dccu, 0xba5e5678u, 0x7ff968f6u, 0xd98e6342u, 0xe86679dfu, 0x4e11726bu,
2601 0xb8590282u, 0x1e2e0936u, 0x2fc613abu, 0x89b1181fu, 0x4c162691u, 0xea612d25u, 0xdb8937b8u, 0x7dfe3c0cu,
2602 0xec68d02bu, 0x4a1fdb9fu, 0x7bf7c102u, 0xdd80cab6u, 0x1827f438u, 0xbe50ff8cu, 0x8fb8e511u, 0x29cfeea5u,
2603 0xdf879e4cu, 0x79f095f8u, 0x48188f65u, 0xee6f84d1u, 0x2bc8ba5fu, 0x8dbfb1ebu, 0xbc57ab76u, 0x1a20a0c2u,
2604 0x8816eaf2u, 0x2e61e146u, 0x1f89fbdbu, 0xb9fef06fu, 0x7c59cee1u, 0xda2ec555u, 0xebc6dfc8u, 0x4db1d47cu,
2605 0xbbf9a495u, 0x1d8eaf21u, 0x2c66b5bcu, 0x8a11be08u, 0x4fb68086u, 0xe9c18b32u, 0xd82991afu, 0x7e5e9a1bu,
2606 0xefc8763cu, 0x49bf7d88u, 0x78576715u, 0xde206ca1u, 0x1b87522fu, 0xbdf0599bu, 0x8c184306u, 0x2a6f48b2u,
2607 0xdc27385bu, 0x7a5033efu, 0x4bb82972u, 0xedcf22c6u, 0x28681c48u, 0x8e1f17fcu, 0xbff70d61u, 0x198006d5u,
2608 0x47abd36eu, 0xe1dcd8dau, 0xd034c247u, 0x7643c9f3u, 0xb3e4f77du, 0x1593fcc9u, 0x247be654u, 0x820cede0u,
2609 0x74449d09u, 0xd23396bdu, 0xe3db8c20u, 0x45ac8794u, 0x800bb91au, 0x267cb2aeu, 0x1794a833u, 0xb1e3a387u,
2610 0x20754fa0u, 0x86024414u, 0xb7ea5e89u, 0x119d553du, 0xd43a6bb3u, 0x724d6007u, 0x43a57a9au, 0xe5d2712eu,
2611 0x139a01c7u, 0xb5ed0a73u, 0x840510eeu, 0x22721b5au, 0xe7d525d4u, 0x41a22e60u, 0x704a34fdu, 0xd63d3f49u,
2612 0xcc1d9f8bu, 0x6a6a943fu, 0x5b828ea2u, 0xfdf58516u, 0x3852bb98u, 0x9e25b02cu, 0xafcdaab1u, 0x09baa105u,
2613 0xfff2d1ecu, 0x5985da58u, 0x686dc0c5u, 0xce1acb71u, 0x0bbdf5ffu, 0xadcafe4bu, 0x9c22e4d6u, 0x3a55ef62u,
2614 0xabc30345u, 0x0db408f1u, 0x3c5c126cu, 0x9a2b19d8u, 0x5f8c2756u, 0xf9fb2ce2u, 0xc813367fu, 0x6e643dcbu,
2615 0x982c4d22u, 0x3e5b4696u, 0x0fb35c0bu, 0xa9c457bfu, 0x6c636931u, 0xca146285u, 0xfbfc7818u, 0x5d8b73acu,
2616 0x03a0a617u, 0xa5d7ada3u, 0x943fb73eu, 0x3248bc8au, 0xf7ef8204u, 0x519889b0u, 0x6070932du, 0xc6079899u,
2617 0x304fe870u, 0x9638e3c4u, 0xa7d0f959u, 0x01a7f2edu, 0xc400cc63u, 0x6277c7d7u, 0x539fdd4au, 0xf5e8d6feu,
2618 0x647e3ad9u, 0xc209316du, 0xf3e12bf0u, 0x55962044u, 0x90311ecau, 0x3646157eu, 0x07ae0fe3u, 0xa1d90457u,
2619 0x579174beu, 0xf1e67f0au, 0xc00e6597u, 0x66796e23u, 0xa3de50adu, 0x05a95b19u, 0x34414184u, 0x92364a30u
2622static const unsigned lodepng_crc32_table7[256] = {
2623 0x00000000u, 0xccaa009eu, 0x4225077du, 0x8e8f07e3u, 0x844a0efau, 0x48e00e64u, 0xc66f0987u, 0x0ac50919u,
2624 0xd3e51bb5u, 0x1f4f1b2bu, 0x91c01cc8u, 0x5d6a1c56u, 0x57af154fu, 0x9b0515d1u, 0x158a1232u, 0xd92012acu,
2625 0x7cbb312bu, 0xb01131b5u, 0x3e9e3656u, 0xf23436c8u, 0xf8f13fd1u, 0x345b3f4fu, 0xbad438acu, 0x767e3832u,
2626 0xaf5e2a9eu, 0x63f42a00u, 0xed7b2de3u, 0x21d12d7du, 0x2b142464u, 0xe7be24fau, 0x69312319u, 0xa59b2387u,
2627 0xf9766256u, 0x35dc62c8u, 0xbb53652bu, 0x77f965b5u, 0x7d3c6cacu, 0xb1966c32u, 0x3f196bd1u, 0xf3b36b4fu,
2628 0x2a9379e3u, 0xe639797du, 0x68b67e9eu, 0xa41c7e00u, 0xaed97719u, 0x62737787u, 0xecfc7064u, 0x205670fau,
2629 0x85cd537du, 0x496753e3u, 0xc7e85400u, 0x0b42549eu, 0x01875d87u, 0xcd2d5d19u, 0x43a25afau, 0x8f085a64u,
2630 0x562848c8u, 0x9a824856u, 0x140d4fb5u, 0xd8a74f2bu, 0xd2624632u, 0x1ec846acu, 0x9047414fu, 0x5ced41d1u,
2631 0x299dc2edu, 0xe537c273u, 0x6bb8c590u, 0xa712c50eu, 0xadd7cc17u, 0x617dcc89u, 0xeff2cb6au, 0x2358cbf4u,
2632 0xfa78d958u, 0x36d2d9c6u, 0xb85dde25u, 0x74f7debbu, 0x7e32d7a2u, 0xb298d73cu, 0x3c17d0dfu, 0xf0bdd041u,
2633 0x5526f3c6u, 0x998cf358u, 0x1703f4bbu, 0xdba9f425u, 0xd16cfd3cu, 0x1dc6fda2u, 0x9349fa41u, 0x5fe3fadfu,
2634 0x86c3e873u, 0x4a69e8edu, 0xc4e6ef0eu, 0x084cef90u, 0x0289e689u, 0xce23e617u, 0x40ace1f4u, 0x8c06e16au,
2635 0xd0eba0bbu, 0x1c41a025u, 0x92cea7c6u, 0x5e64a758u, 0x54a1ae41u, 0x980baedfu, 0x1684a93cu, 0xda2ea9a2u,
2636 0x030ebb0eu, 0xcfa4bb90u, 0x412bbc73u, 0x8d81bcedu, 0x8744b5f4u, 0x4beeb56au, 0xc561b289u, 0x09cbb217u,
2637 0xac509190u, 0x60fa910eu, 0xee7596edu, 0x22df9673u, 0x281a9f6au, 0xe4b09ff4u, 0x6a3f9817u, 0xa6959889u,
2638 0x7fb58a25u, 0xb31f8abbu, 0x3d908d58u, 0xf13a8dc6u, 0xfbff84dfu, 0x37558441u, 0xb9da83a2u, 0x7570833cu,
2639 0x533b85dau, 0x9f918544u, 0x111e82a7u, 0xddb48239u, 0xd7718b20u, 0x1bdb8bbeu, 0x95548c5du, 0x59fe8cc3u,
2640 0x80de9e6fu, 0x4c749ef1u, 0xc2fb9912u, 0x0e51998cu, 0x04949095u, 0xc83e900bu, 0x46b197e8u, 0x8a1b9776u,
2641 0x2f80b4f1u, 0xe32ab46fu, 0x6da5b38cu, 0xa10fb312u, 0xabcaba0bu, 0x6760ba95u, 0xe9efbd76u, 0x2545bde8u,
2642 0xfc65af44u, 0x30cfafdau, 0xbe40a839u, 0x72eaa8a7u, 0x782fa1beu, 0xb485a120u, 0x3a0aa6c3u, 0xf6a0a65du,
2643 0xaa4de78cu, 0x66e7e712u, 0xe868e0f1u, 0x24c2e06fu, 0x2e07e976u, 0xe2ade9e8u, 0x6c22ee0bu, 0xa088ee95u,
2644 0x79a8fc39u, 0xb502fca7u, 0x3b8dfb44u, 0xf727fbdau, 0xfde2f2c3u, 0x3148f25du, 0xbfc7f5beu, 0x736df520u,
2645 0xd6f6d6a7u, 0x1a5cd639u, 0x94d3d1dau, 0x5879d144u, 0x52bcd85du, 0x9e16d8c3u, 0x1099df20u, 0xdc33dfbeu,
2646 0x0513cd12u, 0xc9b9cd8cu, 0x4736ca6fu, 0x8b9ccaf1u, 0x8159c3e8u, 0x4df3c376u, 0xc37cc495u, 0x0fd6c40bu,
2647 0x7aa64737u, 0xb60c47a9u, 0x3883404au, 0xf42940d4u, 0xfeec49cdu, 0x32464953u, 0xbcc94eb0u, 0x70634e2eu,
2648 0xa9435c82u, 0x65e95c1cu, 0xeb665bffu, 0x27cc5b61u, 0x2d095278u, 0xe1a352e6u, 0x6f2c5505u, 0xa386559bu,
2649 0x061d761cu, 0xcab77682u, 0x44387161u, 0x889271ffu, 0x825778e6u, 0x4efd7878u, 0xc0727f9bu, 0x0cd87f05u,
2650 0xd5f86da9u, 0x19526d37u, 0x97dd6ad4u, 0x5b776a4au, 0x51b26353u, 0x9d1863cdu, 0x1397642eu, 0xdf3d64b0u,
2651 0x83d02561u, 0x4f7a25ffu, 0xc1f5221cu, 0x0d5f2282u, 0x079a2b9bu, 0xcb302b05u, 0x45bf2ce6u, 0x89152c78u,
2652 0x50353ed4u, 0x9c9f3e4au, 0x121039a9u, 0xdeba3937u, 0xd47f302eu, 0x18d530b0u, 0x965a3753u, 0x5af037cdu,
2653 0xff6b144au, 0x33c114d4u, 0xbd4e1337u, 0x71e413a9u, 0x7b211ab0u, 0xb78b1a2eu, 0x39041dcdu, 0xf5ae1d53u,
2654 0x2c8e0fffu, 0xe0240f61u, 0x6eab0882u, 0xa201081cu, 0xa8c40105u, 0x646e019bu, 0xeae10678u, 0x264b06e6u
2660 unsigned r = 0xffffffffu;
2662 r = lodepng_crc32_table7[(
data[0] ^ (
r & 0xffu))] ^
2663 lodepng_crc32_table6[(
data[1] ^ ((
r >> 8) & 0xffu))] ^
2664 lodepng_crc32_table5[(
data[2] ^ ((
r >> 16) & 0xffu))] ^
2665 lodepng_crc32_table4[(
data[3] ^ ((
r >> 24) & 0xffu))] ^
2666 lodepng_crc32_table3[
data[4]] ^
2667 lodepng_crc32_table2[
data[5]] ^
2668 lodepng_crc32_table1[
data[6]] ^
2669 lodepng_crc32_table0[
data[7]];
2674 r = lodepng_crc32_table0[(
r ^ *
data++) & 0xffu] ^ (
r >> 8);
2676 return r ^ 0xffffffffu;
2708static unsigned char readBitFromReversedStream(
size_t* bitpointer,
const unsigned char* bitstream) {
2709 unsigned char result = (
unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
2715static unsigned readBitsFromReversedStream(
size_t* bitpointer,
const unsigned char* bitstream,
size_t nbits) {
2716 unsigned result = 0;
2718 for(i = 0 ; i < nbits; ++i) {
2720 result |= (unsigned)readBitFromReversedStream(bitpointer, bitstream);
2725static void setBitOfReversedStream(
size_t* bitpointer,
unsigned char* bitstream,
unsigned char bit) {
2727 if(bit == 0) bitstream[(*bitpointer) >> 3u] &= (
unsigned char)(~(1u << (7u - ((*bitpointer) & 7u))));
2728 else bitstream[(*bitpointer) >> 3u] |= (1u << (7u - ((*bitpointer) & 7u)));
2737 return lodepng_read32bitInt(chunk);
2742 for(i = 0; i != 4; ++i)
type[i] = (
char)chunk[4 + i];
2748 return (chunk[4] ==
type[0] && chunk[5] ==
type[1] && chunk[6] ==
type[2] && chunk[7] ==
type[3]);
2752 return((chunk[4] & 32) != 0);
2756 return((chunk[6] & 32) != 0);
2760 return((chunk[7] & 32) != 0);
2773 unsigned CRC = lodepng_read32bitInt(&chunk[
length + 8]);
2776 if(CRC != checksum)
return 1;
2783 lodepng_set32bitInt(chunk + 8 +
length, CRC);
2787 size_t available_size = (size_t)(
end - chunk);
2788 if(chunk >=
end || available_size < 12)
return end;
2789 if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47
2790 && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) {
2794 size_t total_chunk_length;
2796 if(total_chunk_length > available_size)
return end;
2797 return chunk + total_chunk_length;
2802 size_t available_size = (size_t)(
end - chunk);
2803 if(chunk >=
end || available_size < 12)
return end;
2804 if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47
2805 && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) {
2809 size_t total_chunk_length;
2811 if(total_chunk_length > available_size)
return end;
2812 return chunk + total_chunk_length;
2818 if(chunk >=
end ||
end - chunk < 12)
return 0;
2826 if(chunk >=
end ||
end - chunk < 12)
return 0;
2832unsigned lodepng_chunk_append(
unsigned char** out,
size_t* outsize,
const unsigned char* chunk) {
2834 size_t total_chunk_length, new_length;
2835 unsigned char *chunk_start, *new_buffer;
2838 if(lodepng_addofl(*outsize, total_chunk_length, &new_length))
return 77;
2841 if(!new_buffer)
return 83;
2842 (*out) = new_buffer;
2843 (*outsize) = new_length;
2844 chunk_start = &(*out)[new_length - total_chunk_length];
2846 for(i = 0; i != total_chunk_length; ++i) chunk_start[i] = chunk[i];
2855static unsigned lodepng_chunk_init(
unsigned char** chunk,
2858 size_t new_length = out->
size;
2859 if(lodepng_addofl(new_length,
length, &new_length))
return 77;
2860 if(lodepng_addofl(new_length, 12, &new_length))
return 77;
2862 *chunk = out->
data + new_length -
length - 12u;
2865 lodepng_set32bitInt(*chunk,
length);
2874static unsigned lodepng_chunk_createv(
ucvector* out,
2876 unsigned char* chunk;
2903static unsigned checkColorValidity(
LodePNGColorType colortype,
unsigned bd) {
2905 case LCT_GREY:
if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16))
return 37;
break;
2906 case LCT_RGB:
if(!( bd == 8 || bd == 16))
return 37;
break;
2907 case LCT_PALETTE:
if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 ))
return 37;
break;
2908 case LCT_GREY_ALPHA:
if(!( bd == 8 || bd == 16))
return 37;
break;
2909 case LCT_RGBA:
if(!( bd == 8 || bd == 16))
return 37;
break;
2928static unsigned lodepng_get_bpp_lct(
LodePNGColorType colortype,
unsigned bitdepth) {
2930 return getNumColorChannels(colortype) * bitdepth;
2951 for(i = 0; i != 256; ++i) {
2958 info->
palette[i * 4 + 3] = 255;
2987 if(
a->colortype !=
b->colortype)
return 0;
2988 if(
a->bitdepth !=
b->bitdepth)
return 0;
2989 if(
a->key_defined !=
b->key_defined)
return 0;
2990 if(
a->key_defined) {
2991 if(
a->key_r !=
b->key_r)
return 0;
2992 if(
a->key_g !=
b->key_g)
return 0;
2993 if(
a->key_b !=
b->key_b)
return 0;
2995 if(
a->palettesize !=
b->palettesize)
return 0;
2996 for(i = 0; i !=
a->palettesize * 4; ++i) {
2997 if(
a->palette[i] !=
b->palette[i])
return 0;
3009 unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a) {
3011 lodepng_color_mode_alloc_palette(info);
3031 return getNumColorChannels(info->
colortype);
3049 if(info->
palette[i * 4 + 3] < 255)
return 1;
3060static size_t lodepng_get_raw_size_lct(
unsigned w,
unsigned h,
LodePNGColorType colortype,
unsigned bitdepth) {
3061 size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth);
3062 size_t n = (size_t)
w * (
size_t)
h;
3063 return ((
n / 8u) * bpp) + ((
n & 7u) * bpp + 7u) / 8u;
3067 return lodepng_get_raw_size_lct(
w,
h,
color->colortype,
color->bitdepth);
3071#ifdef LODEPNG_COMPILE_PNG
3076static size_t lodepng_get_raw_size_idat(
unsigned w,
unsigned h,
unsigned bpp) {
3079 size_t line = ((size_t)(
w / 8u) * bpp) + 1u + ((
w & 7u) * bpp + 7u) / 8u;
3080 return (
size_t)
h * line;
3083#ifdef LODEPNG_COMPILE_DECODER
3092static int lodepng_pixel_overflow(
unsigned w,
unsigned h,
3095 size_t numpixels, total;
3098 if(lodepng_mulofl((
size_t)
w, (
size_t)
h, &numpixels))
return 1;
3099 if(lodepng_mulofl(numpixels, 8, &total))
return 1;
3102 if(lodepng_mulofl((
size_t)(
w / 8u), bpp, &line))
return 1;
3103 if(lodepng_addofl(line, ((
w & 7u) * bpp + 7u) / 8u, &line))
return 1;
3105 if(lodepng_addofl(line, 5, &line))
return 1;
3106 if(lodepng_mulofl(line,
h, &total))
return 1;
3113#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3115static void LodePNGUnknownChunks_init(
LodePNGInfo* info) {
3121static void LodePNGUnknownChunks_cleanup(
LodePNGInfo* info) {
3129 LodePNGUnknownChunks_cleanup(dest);
3131 for(i = 0; i != 3; ++i) {
3136 for(
j = 0;
j <
src->unknown_chunks_size[i]; ++
j) {
3152static void LodePNGText_cleanup(
LodePNGInfo* info) {
3154 for(i = 0; i != info->
text_num; ++i) {
3167 for(i = 0; i !=
source->text_num; ++i) {
3173static unsigned lodepng_add_text_sized(
LodePNGInfo* info,
const char* key,
const char* str,
size_t size) {
3177 if(new_keys) info->
text_keys = new_keys;
3180 if(!new_keys || !new_strings)
return 83;
3191 return lodepng_add_text_sized(info, key, str,
lodepng_strlen(str));
3195 LodePNGText_cleanup(info);
3208static void LodePNGIText_cleanup(
LodePNGInfo* info) {
3229 for(i = 0; i !=
source->itext_num; ++i) {
3231 source->itext_transkeys[i],
source->itext_strings[i]));
3237 LodePNGIText_cleanup(info);
3240static unsigned lodepng_add_itext_sized(
LodePNGInfo* info,
const char* key,
const char* langtag,
3241 const char* transkey,
const char* str,
size_t size) {
3252 if(!new_keys || !new_langtags || !new_transkeys || !new_strings)
return 83;
3265 const char* transkey,
const char* str) {
3266 return lodepng_add_itext_sized(info, key, langtag, transkey, str,
lodepng_strlen(str));
3270static unsigned lodepng_assign_icc(
LodePNGInfo* info,
const char*
name,
const unsigned char* profile,
unsigned profile_size) {
3271 if(profile_size == 0)
return 100;
3288 return lodepng_assign_icc(info,
name, profile, profile_size);
3305#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3309 LodePNGText_init(info);
3310 LodePNGIText_init(info);
3325 LodePNGUnknownChunks_init(info);
3331#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3332 LodePNGText_cleanup(info);
3333 LodePNGIText_cleanup(info);
3337 LodePNGUnknownChunks_cleanup(info);
3347#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3350 if(
source->iccp_defined) {
3354 LodePNGUnknownChunks_init(dest);
3363static void addColorBits(
unsigned char* out,
size_t index,
unsigned bits,
unsigned in) {
3364 unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1;
3366 unsigned p =
index & m;
3367 in &= (1u << bits) - 1u;
3368 in = in << (bits * (m - p));
3369 if(p == 0) out[
index * bits / 8u] = in;
3370 else out[
index * bits / 8u] |= in;
3373typedef struct ColorTree ColorTree;
3382 ColorTree* children[16];
3386static void color_tree_init(ColorTree* tree) {
3391static void color_tree_cleanup(ColorTree* tree) {
3393 for(i = 0; i != 16; ++i) {
3394 if(tree->children[i]) {
3395 color_tree_cleanup(tree->children[i]);
3402static int color_tree_get(ColorTree* tree,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a) {
3404 for(bit = 0; bit < 8; ++bit) {
3405 int i = 8 * ((
r >> bit) & 1) + 4 * ((
g >> bit) & 1) + 2 * ((
b >> bit) & 1) + 1 * ((
a >> bit) & 1);
3406 if(!tree->children[i])
return -1;
3407 else tree = tree->children[i];
3409 return tree ? tree->index : -1;
3412#ifdef LODEPNG_COMPILE_ENCODER
3413static int color_tree_has(ColorTree* tree,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a) {
3414 return color_tree_get(tree,
r,
g,
b,
a) >= 0;
3421static unsigned color_tree_add(ColorTree* tree,
3422 unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a,
unsigned index) {
3424 for(bit = 0; bit < 8; ++bit) {
3425 int i = 8 * ((
r >> bit) & 1) + 4 * ((
g >> bit) & 1) + 2 * ((
b >> bit) & 1) + 1 * ((
a >> bit) & 1);
3426 if(!tree->children[i]) {
3427 tree->children[i] = (ColorTree*)
lodepng_malloc(
sizeof(ColorTree));
3428 if(!tree->children[i])
return 83;
3429 color_tree_init(tree->children[i]);
3431 tree = tree->children[i];
3433 tree->index = (int)
index;
3438static unsigned rgba8ToPixel(
unsigned char* out,
size_t i,
3440 unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a) {
3442 unsigned char gray =
r;
3443 if(
mode->bitdepth == 8) out[i] = gray;
3444 else if(
mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray;
3447 gray = ((unsigned)gray >> (8u -
mode->bitdepth)) & ((1u <<
mode->bitdepth) - 1u);
3448 addColorBits(out, i,
mode->bitdepth, gray);
3451 if(
mode->bitdepth == 8) {
3456 out[i * 6 + 0] = out[i * 6 + 1] =
r;
3457 out[i * 6 + 2] = out[i * 6 + 3] =
g;
3458 out[i * 6 + 4] = out[i * 6 + 5] =
b;
3461 int index = color_tree_get(tree,
r,
g,
b,
a);
3462 if(
index < 0)
return 82;
3464 else addColorBits(out, i,
mode->bitdepth, (
unsigned)
index);
3466 unsigned char gray =
r;
3467 if(
mode->bitdepth == 8) {
3468 out[i * 2 + 0] = gray;
3470 }
else if(
mode->bitdepth == 16) {
3471 out[i * 4 + 0] = out[i * 4 + 1] = gray;
3472 out[i * 4 + 2] = out[i * 4 + 3] =
a;
3475 if(
mode->bitdepth == 8) {
3481 out[i * 8 + 0] = out[i * 8 + 1] =
r;
3482 out[i * 8 + 2] = out[i * 8 + 3] =
g;
3483 out[i * 8 + 4] = out[i * 8 + 5] =
b;
3484 out[i * 8 + 6] = out[i * 8 + 7] =
a;
3492static void rgba16ToPixel(
unsigned char* out,
size_t i,
3494 unsigned short r,
unsigned short g,
unsigned short b,
unsigned short a) {
3496 unsigned short gray =
r;
3497 out[i * 2 + 0] = (gray >> 8) & 255;
3498 out[i * 2 + 1] = gray & 255;
3500 out[i * 6 + 0] = (
r >> 8) & 255;
3501 out[i * 6 + 1] =
r & 255;
3502 out[i * 6 + 2] = (
g >> 8) & 255;
3503 out[i * 6 + 3] =
g & 255;
3504 out[i * 6 + 4] = (
b >> 8) & 255;
3505 out[i * 6 + 5] =
b & 255;
3507 unsigned short gray =
r;
3508 out[i * 4 + 0] = (gray >> 8) & 255;
3509 out[i * 4 + 1] = gray & 255;
3510 out[i * 4 + 2] = (
a >> 8) & 255;
3511 out[i * 4 + 3] =
a & 255;
3513 out[i * 8 + 0] = (
r >> 8) & 255;
3514 out[i * 8 + 1] =
r & 255;
3515 out[i * 8 + 2] = (
g >> 8) & 255;
3516 out[i * 8 + 3] =
g & 255;
3517 out[i * 8 + 4] = (
b >> 8) & 255;
3518 out[i * 8 + 5] =
b & 255;
3519 out[i * 8 + 6] = (
a >> 8) & 255;
3520 out[i * 8 + 7] =
a & 255;
3525static void getPixelColorRGBA8(
unsigned char*
r,
unsigned char*
g,
3526 unsigned char*
b,
unsigned char*
a,
3527 const unsigned char* in,
size_t i,
3530 if(
mode->bitdepth == 8) {
3531 *
r = *
g = *
b = in[i];
3532 if(
mode->key_defined && *
r ==
mode->key_r) *
a = 0;
3534 }
else if(
mode->bitdepth == 16) {
3535 *
r = *
g = *
b = in[i * 2 + 0];
3536 if(
mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] ==
mode->key_r) *
a = 0;
3539 unsigned highest = ((1U <<
mode->bitdepth) - 1U);
3540 size_t j = i *
mode->bitdepth;
3541 unsigned value = readBitsFromReversedStream(&
j, in,
mode->bitdepth);
3542 *
r = *
g = *
b = (
value * 255) / highest;
3547 if(
mode->bitdepth == 8) {
3548 *
r = in[i * 3 + 0]; *
g = in[i * 3 + 1]; *
b = in[i * 3 + 2];
3555 if(
mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] ==
mode->key_r
3556 && 256U * in[i * 6 + 2] + in[i * 6 + 3] ==
mode->key_g
3557 && 256U * in[i * 6 + 4] + in[i * 6 + 5] ==
mode->key_b) *
a = 0;
3564 size_t j = i *
mode->bitdepth;
3565 index = readBitsFromReversedStream(&
j, in,
mode->bitdepth);
3573 if(
mode->bitdepth == 8) {
3574 *
r = *
g = *
b = in[i * 2 + 0];
3577 *
r = *
g = *
b = in[i * 4 + 0];
3581 if(
mode->bitdepth == 8) {
3602 unsigned num_channels = 4;
3605 if(
mode->bitdepth == 8) {
3606 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3610 if(
mode->key_defined) {
3611 buffer -= numpixels * num_channels;
3612 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3616 }
else if(
mode->bitdepth == 16) {
3617 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3619 buffer[3] =
mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] ==
mode->key_r ? 0 : 255;
3622 unsigned highest = ((1U <<
mode->bitdepth) - 1U);
3624 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3625 unsigned value = readBitsFromReversedStream(&
j, in,
mode->bitdepth);
3631 if(
mode->bitdepth == 8) {
3632 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3636 if(
mode->key_defined) {
3637 buffer -= numpixels * num_channels;
3638 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3643 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3644 buffer[0] = in[i * 6 + 0];
3645 buffer[1] = in[i * 6 + 2];
3646 buffer[2] = in[i * 6 + 4];
3648 && 256U * in[i * 6 + 0] + in[i * 6 + 1] ==
mode->key_r
3649 && 256U * in[i * 6 + 2] + in[i * 6 + 3] ==
mode->key_g
3650 && 256U * in[i * 6 + 4] + in[i * 6 + 5] ==
mode->key_b ? 0 : 255;
3654 if(
mode->bitdepth == 8) {
3655 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3656 unsigned index = in[i];
3662 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3663 unsigned index = readBitsFromReversedStream(&
j, in,
mode->bitdepth);
3669 if(
mode->bitdepth == 8) {
3670 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3672 buffer[3] = in[i * 2 + 1];
3675 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3677 buffer[3] = in[i * 4 + 2];
3681 if(
mode->bitdepth == 8) {
3684 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3685 buffer[0] = in[i * 8 + 0];
3686 buffer[1] = in[i * 8 + 2];
3687 buffer[2] = in[i * 8 + 4];
3688 buffer[3] = in[i * 8 + 6];
3698 const unsigned num_channels = 3;
3701 if(
mode->bitdepth == 8) {
3702 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3705 }
else if(
mode->bitdepth == 16) {
3706 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3710 unsigned highest = ((1U <<
mode->bitdepth) - 1U);
3712 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3713 unsigned value = readBitsFromReversedStream(&
j, in,
mode->bitdepth);
3718 if(
mode->bitdepth == 8) {
3721 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3722 buffer[0] = in[i * 6 + 0];
3723 buffer[1] = in[i * 6 + 2];
3724 buffer[2] = in[i * 6 + 4];
3728 if(
mode->bitdepth == 8) {
3729 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3730 unsigned index = in[i];
3736 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3737 unsigned index = readBitsFromReversedStream(&
j, in,
mode->bitdepth);
3743 if(
mode->bitdepth == 8) {
3744 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3748 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3753 if(
mode->bitdepth == 8) {
3754 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3758 for(i = 0; i != numpixels; ++i,
buffer += num_channels) {
3759 buffer[0] = in[i * 8 + 0];
3760 buffer[1] = in[i * 8 + 2];
3761 buffer[2] = in[i * 8 + 4];
3769static void getPixelColorRGBA16(
unsigned short*
r,
unsigned short*
g,
unsigned short*
b,
unsigned short*
a,
3772 *
r = *
g = *
b = 256 * in[i * 2 + 0] + in[i * 2 + 1];
3773 if(
mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] ==
mode->key_r) *
a = 0;
3776 *
r = 256u * in[i * 6 + 0] + in[i * 6 + 1];
3777 *
g = 256u * in[i * 6 + 2] + in[i * 6 + 3];
3778 *
b = 256u * in[i * 6 + 4] + in[i * 6 + 5];
3779 if(
mode->key_defined
3780 && 256u * in[i * 6 + 0] + in[i * 6 + 1] ==
mode->key_r
3781 && 256u * in[i * 6 + 2] + in[i * 6 + 3] ==
mode->key_g
3782 && 256u * in[i * 6 + 4] + in[i * 6 + 5] ==
mode->key_b) *
a = 0;
3785 *
r = *
g = *
b = 256u * in[i * 4 + 0] + in[i * 4 + 1];
3786 *
a = 256u * in[i * 4 + 2] + in[i * 4 + 3];
3788 *
r = 256u * in[i * 8 + 0] + in[i * 8 + 1];
3789 *
g = 256u * in[i * 8 + 2] + in[i * 8 + 3];
3790 *
b = 256u * in[i * 8 + 4] + in[i * 8 + 5];
3791 *
a = 256u * in[i * 8 + 6] + in[i * 8 + 7];
3797 unsigned w,
unsigned h) {
3800 size_t numpixels = (size_t)
w * (
size_t)
h;
3807 if(lodepng_color_mode_equal(mode_out, mode_in)) {
3815 const unsigned char* palette = mode_out->
palette;
3816 size_t palsize = (size_t)1u << mode_out->
bitdepth;
3820 if(palettesize == 0) {
3832 if(palettesize < palsize) palsize = palettesize;
3833 color_tree_init(&tree);
3834 for(i = 0; i != palsize; ++i) {
3835 const unsigned char* p = &palette[i * 4];
3836 error = color_tree_add(&tree, p[0], p[1], p[2], p[3], (
unsigned)i);
3843 for(i = 0; i != numpixels; ++i) {
3844 unsigned short r = 0,
g = 0,
b = 0,
a = 0;
3845 getPixelColorRGBA16(&
r, &
g, &
b, &
a, in, i, mode_in);
3846 rgba16ToPixel(out, i, mode_out,
r,
g,
b,
a);
3849 getPixelColorsRGBA8(out, numpixels, in, mode_in);
3851 getPixelColorsRGB8(out, numpixels, in, mode_in);
3853 unsigned char r = 0,
g = 0,
b = 0,
a = 0;
3854 for(i = 0; i != numpixels; ++i) {
3855 getPixelColorRGBA8(&
r, &
g, &
b, &
a, in, i, mode_in);
3856 error = rgba8ToPixel(out, i, mode_out, &tree,
r,
g,
b,
a);
3863 color_tree_cleanup(&tree);
3876unsigned lodepng_convert_rgb(
3877 unsigned* r_out,
unsigned* g_out,
unsigned* b_out,
3878 unsigned r_in,
unsigned g_in,
unsigned b_in,
3880 unsigned r = 0,
g = 0,
b = 0;
3881 unsigned mul = 65535 / ((1u << mode_in->
bitdepth) - 1u);
3882 unsigned shift = 16 - mode_out->
bitdepth;
3885 r =
g =
b = r_in * mul;
3892 r = mode_in->
palette[r_in * 4 + 0] * 257u;
3893 g = mode_in->
palette[r_in * 4 + 1] * 257u;
3894 b = mode_in->
palette[r_in * 4 + 2] * 257u;
3901 *r_out =
r >> shift ;
3903 *r_out =
r >> shift ;
3904 *g_out =
g >> shift ;
3905 *b_out =
b >> shift ;
3909 if((
r >> 8) != (
r & 255) || (
g >> 8) != (
g & 255) || (
b >> 8) != (
b & 255))
return 82;
3926#ifdef LODEPNG_COMPILE_ENCODER
3955static unsigned getValueRequiredBits(
unsigned char value) {
3958 if(
value % 17 == 0)
return value % 85 == 0 ? 2 : 4;
3964 const unsigned char* in,
unsigned w,
unsigned h,
3968 size_t numpixels = (size_t)
w * (
size_t)
h;
3974 unsigned numcolors_done = 0;
3976 unsigned bits_done = (stats->
bits == 1 && bpp == 1) ? 1 : 0;
3977 unsigned sixteen = 0;
3978 unsigned maxnumcolors = 257;
3986 color_tree_init(&tree);
3990 if(stats->
alpha) alpha_done = 1;
3991 if(stats->
colored) colored_done = 1;
3992 if(stats->
bits == 16) numcolors_done = 1;
3993 if(stats->
bits >= bpp) bits_done = 1;
3994 if(stats->
numcolors >= maxnumcolors) numcolors_done = 1;
3996 if(!numcolors_done) {
4000 if(error)
goto cleanup;
4005 if(mode_in->
bitdepth == 16 && !sixteen) {
4006 unsigned short r = 0,
g = 0,
b = 0,
a = 0;
4007 for(i = 0; i != numpixels; ++i) {
4008 getPixelColorRGBA16(&
r, &
g, &
b, &
a, in, i, mode_in);
4009 if((
r & 255) != ((
r >> 8) & 255) || (
g & 255) != ((
g >> 8) & 255) ||
4010 (
b & 255) != ((
b >> 8) & 255) || (
a & 255) != ((
a >> 8) & 255)) {
4021 unsigned short r = 0,
g = 0,
b = 0,
a = 0;
4023 for(i = 0; i != numpixels; ++i) {
4024 getPixelColorRGBA16(&
r, &
g, &
b, &
a, in, i, mode_in);
4026 if(!colored_done && (
r !=
g ||
r !=
b)) {
4033 if(
a != 65535 && (
a != 0 || (stats->
key && !matchkey))) {
4037 }
else if(
a == 0 && !stats->
alpha && !stats->
key) {
4042 }
else if(
a == 65535 && stats->
key && matchkey) {
4049 if(alpha_done && numcolors_done && colored_done && bits_done)
break;
4053 for(i = 0; i != numpixels; ++i) {
4054 getPixelColorRGBA16(&
r, &
g, &
b, &
a, in, i, mode_in);
4064 unsigned char r = 0,
g = 0,
b = 0,
a = 0;
4065 for(i = 0; i != numpixels; ++i) {
4066 getPixelColorRGBA8(&
r, &
g, &
b, &
a, in, i, mode_in);
4068 if(!bits_done && stats->
bits < 8) {
4070 unsigned bits = getValueRequiredBits(
r);
4071 if(bits > stats->
bits) stats->
bits = bits;
4073 bits_done = (stats->
bits >= bpp);
4075 if(!colored_done && (
r !=
g ||
r !=
b)) {
4078 if(stats->
bits < 8) stats->
bits = 8;
4083 if(
a != 255 && (
a != 0 || (stats->
key && !matchkey))) {
4087 if(stats->
bits < 8) stats->
bits = 8;
4088 }
else if(
a == 0 && !stats->
alpha && !stats->
key) {
4093 }
else if(
a == 255 && stats->
key && matchkey) {
4098 if(stats->
bits < 8) stats->
bits = 8;
4102 if(!numcolors_done) {
4103 if(!color_tree_has(&tree,
r,
g,
b,
a)) {
4104 error = color_tree_add(&tree,
r,
g,
b,
a, stats->
numcolors);
4105 if(error)
goto cleanup;
4107 unsigned char* p = stats->
palette;
4115 numcolors_done = stats->
numcolors >= maxnumcolors;
4119 if(alpha_done && numcolors_done && colored_done && bits_done)
break;
4123 for(i = 0; i != numpixels; ++i) {
4124 getPixelColorRGBA8(&
r, &
g, &
b, &
a, in, i, mode_in);
4130 if(stats->
bits < 8) stats->
bits = 8;
4142 color_tree_cleanup(&tree);
4146#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4151 unsigned r,
unsigned g,
unsigned b,
unsigned a) {
4153 unsigned char image[8];
4177 unsigned palettebits;
4180 unsigned palette_ok, gray_ok;
4183 unsigned key = stats->
key;
4184 unsigned bits = stats->
bits;
4188 if(key && numpixels <= 16) {
4191 if(bits < 8) bits = 8;
4196 if(!gray_ok && bits < 8) bits = 8;
4199 palettebits =
n <= 2 ? 1 : (
n <= 4 ? 2 : (
n <= 16 ? 4 : 8));
4200 palette_ok =
n <= 256 && bits <= 8 &&
n != 0;
4201 if(numpixels <
n * 2) palette_ok = 0;
4202 if(gray_ok && !
alpha && bits <= palettebits) palette_ok = 0;
4206 const unsigned char* p = stats->
palette;
4208 for(i = 0; i != stats->
numcolors; ++i) {
4209 error =
lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]);
4241static unsigned char paethPredictor(
unsigned char a,
unsigned char b,
unsigned char c) {
4244 short pa = (
b - c) < 0 ? -(
b - c) : (
b - c);
4245 short pb = (
a - c) < 0 ? -(
a - c) : (
a - c);
4247 short pc = (
a +
b - c - c) < 0 ? -(
a +
b - c - c) : (
a +
b - c - c);
4249 if(pb < pa) {
a =
b; pa = pb; }
4250 return (pc < pa) ? c :
a;
4255static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 };
4256static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 };
4257static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 };
4258static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 };
4275static void Adam7_getpassvalues(
unsigned passw[7],
unsigned passh[7],
size_t filter_passstart[8],
4276 size_t padded_passstart[8],
size_t passstart[8],
unsigned w,
unsigned h,
unsigned bpp) {
4281 for(i = 0; i != 7; ++i) {
4282 passw[i] = (
w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
4283 passh[i] = (
h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
4284 if(passw[i] == 0) passh[i] = 0;
4285 if(passh[i] == 0) passw[i] = 0;
4288 filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
4289 for(i = 0; i != 7; ++i) {
4291 filter_passstart[i + 1] = filter_passstart[i]
4292 + ((passw[i] && passh[i]) ? passh[i] * (1u + (passw[i] * bpp + 7u) / 8u) : 0);
4294 padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7u) / 8u);
4296 passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7u) / 8u;
4300#ifdef LODEPNG_COMPILE_DECODER
4308 const unsigned char* in,
size_t insize) {
4311 if(insize == 0 || in == 0) {
4323 if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71
4324 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) {
4335 width = lodepng_read32bitInt(&in[16]);
4336 height = lodepng_read32bitInt(&in[20]);
4361 unsigned CRC = lodepng_read32bitInt(&in[29]);
4363 if(CRC != checksum) {
4368 return state->
error;
4371static unsigned unfilterScanline(
unsigned char* recon,
const unsigned char* scanline,
const unsigned char* precon,
4372 size_t bytewidth,
unsigned char filterType,
size_t length) {
4383 switch(filterType) {
4385 for(i = 0; i !=
length; ++i) recon[i] = scanline[i];
4389 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
4390 for(i = bytewidth; i !=
length; ++i, ++
j) recon[i] = scanline[i] + recon[
j];
4395 for(i = 0; i !=
length; ++i) recon[i] = scanline[i] + precon[i];
4397 for(i = 0; i !=
length; ++i) recon[i] = scanline[i];
4403 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + (precon[i] >> 1u);
4406 if(bytewidth >= 4) {
4407 for(; i + 3 <
length; i += 4,
j += 4) {
4408 unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2], s3 = scanline[i + 3];
4409 unsigned char r0 = recon[
j + 0], r1 = recon[
j + 1], r2 = recon[
j + 2], r3 = recon[
j + 3];
4410 unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2], p3 = precon[i + 3];
4411 recon[i + 0] = s0 + ((r0 + p0) >> 1u);
4412 recon[i + 1] = s1 + ((r1 + p1) >> 1u);
4413 recon[i + 2] = s2 + ((r2 + p2) >> 1u);
4414 recon[i + 3] = s3 + ((r3 + p3) >> 1u);
4416 }
else if(bytewidth >= 3) {
4417 for(; i + 2 <
length; i += 3,
j += 3) {
4418 unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2];
4419 unsigned char r0 = recon[
j + 0], r1 = recon[
j + 1], r2 = recon[
j + 2];
4420 unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2];
4421 recon[i + 0] = s0 + ((r0 + p0) >> 1u);
4422 recon[i + 1] = s1 + ((r1 + p1) >> 1u);
4423 recon[i + 2] = s2 + ((r2 + p2) >> 1u);
4425 }
else if(bytewidth >= 2) {
4426 for(; i + 1 <
length; i += 2,
j += 2) {
4427 unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1];
4428 unsigned char r0 = recon[
j + 0], r1 = recon[
j + 1];
4429 unsigned char p0 = precon[i + 0], p1 = precon[i + 1];
4430 recon[i + 0] = s0 + ((r0 + p0) >> 1u);
4431 recon[i + 1] = s1 + ((r1 + p1) >> 1u);
4434 for(; i !=
length; ++i, ++
j) recon[i] = scanline[i] + ((recon[
j] + precon[i]) >> 1u);
4437 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
4438 for(i = bytewidth; i !=
length; ++i, ++
j) recon[i] = scanline[i] + (recon[
j] >> 1u);
4445 if(bytewidth == 8) {
4446 unsigned char a0, b0 = 0, c0, d0 = 0, a1, b1 = 0, c1, d1 = 0;
4447 unsigned char a2, b2 = 0, c2, d2 = 0, a3, b3 = 0, c3, d3 = 0;
4448 unsigned char a4, b4 = 0, c4, d4 = 0, a5, b5 = 0, c5, d5 = 0;
4449 unsigned char a6, b6 = 0, c6, d6 = 0, a7, b7 = 0, c7, d7 = 0;
4450 for(i = 0; i + 7 <
length; i += 8) {
4451 c0 = b0; c1 = b1; c2 = b2; c3 = b3;
4452 c4 = b4; c5 = b5; c6 = b6; c7 = b7;
4453 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2]; b3 = precon[i + 3];
4454 b4 = precon[i + 4]; b5 = precon[i + 5]; b6 = precon[i + 6]; b7 = precon[i + 7];
4455 a0 = d0; a1 = d1; a2 = d2; a3 = d3;
4456 a4 = d4; a5 = d5; a6 = d6; a7 = d7;
4457 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
4458 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
4459 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
4460 d3 = scanline[i + 3] + paethPredictor(a3, b3, c3);
4461 d4 = scanline[i + 4] + paethPredictor(a4, b4, c4);
4462 d5 = scanline[i + 5] + paethPredictor(a5, b5, c5);
4463 d6 = scanline[i + 6] + paethPredictor(a6, b6, c6);
4464 d7 = scanline[i + 7] + paethPredictor(a7, b7, c7);
4465 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2; recon[i + 3] = d3;
4466 recon[i + 4] = d4; recon[i + 5] = d5; recon[i + 6] = d6; recon[i + 7] = d7;
4468 }
else if(bytewidth == 6) {
4469 unsigned char a0, b0 = 0, c0, d0 = 0, a1, b1 = 0, c1, d1 = 0;
4470 unsigned char a2, b2 = 0, c2, d2 = 0, a3, b3 = 0, c3, d3 = 0;
4471 unsigned char a4, b4 = 0, c4, d4 = 0, a5, b5 = 0, c5, d5 = 0;
4472 for(i = 0; i + 5 <
length; i += 6) {
4473 c0 = b0; c1 = b1; c2 = b2;
4474 c3 = b3; c4 = b4; c5 = b5;
4475 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2];
4476 b3 = precon[i + 3]; b4 = precon[i + 4]; b5 = precon[i + 5];
4477 a0 = d0; a1 = d1; a2 = d2;
4478 a3 = d3; a4 = d4; a5 = d5;
4479 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
4480 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
4481 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
4482 d3 = scanline[i + 3] + paethPredictor(a3, b3, c3);
4483 d4 = scanline[i + 4] + paethPredictor(a4, b4, c4);
4484 d5 = scanline[i + 5] + paethPredictor(a5, b5, c5);
4485 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2;
4486 recon[i + 3] = d3; recon[i + 4] = d4; recon[i + 5] = d5;
4488 }
else if(bytewidth == 4) {
4489 unsigned char a0, b0 = 0, c0, d0 = 0, a1, b1 = 0, c1, d1 = 0;
4490 unsigned char a2, b2 = 0, c2, d2 = 0, a3, b3 = 0, c3, d3 = 0;
4491 for(i = 0; i + 3 <
length; i += 4) {
4492 c0 = b0; c1 = b1; c2 = b2; c3 = b3;
4493 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2]; b3 = precon[i + 3];
4494 a0 = d0; a1 = d1; a2 = d2; a3 = d3;
4495 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
4496 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
4497 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
4498 d3 = scanline[i + 3] + paethPredictor(a3, b3, c3);
4499 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2; recon[i + 3] = d3;
4501 }
else if(bytewidth == 3) {
4502 unsigned char a0, b0 = 0, c0, d0 = 0;
4503 unsigned char a1, b1 = 0, c1, d1 = 0;
4504 unsigned char a2, b2 = 0, c2, d2 = 0;
4505 for(i = 0; i + 2 <
length; i += 3) {
4506 c0 = b0; c1 = b1; c2 = b2;
4507 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2];
4508 a0 = d0; a1 = d1; a2 = d2;
4509 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
4510 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
4511 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
4512 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2;
4514 }
else if(bytewidth == 2) {
4515 unsigned char a0, b0 = 0, c0, d0 = 0;
4516 unsigned char a1, b1 = 0, c1, d1 = 0;
4517 for(i = 0; i + 1 <
length; i += 2) {
4522 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
4523 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
4527 }
else if(bytewidth == 1) {
4528 unsigned char a,
b = 0, c, d = 0;
4529 for(i = 0; i !=
length; ++i) {
4533 d = scanline[i] + paethPredictor(
a,
b, c);
4538 for(i = 0; i != bytewidth; ++i) {
4539 recon[i] = (scanline[i] + precon[i]);
4543 for(; i !=
length; ++i) {
4544 recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
4548 for(i = 0; i != bytewidth; ++i) {
4549 recon[i] = scanline[i];
4551 for(i = bytewidth; i !=
length; ++i, ++
j) {
4553 recon[i] = (scanline[i] + recon[
j]);
4562static unsigned unfilter(
unsigned char* out,
const unsigned char* in,
unsigned w,
unsigned h,
unsigned bpp) {
4572 unsigned char* prevline = 0;
4575 size_t bytewidth = (bpp + 7u) / 8u;
4577 size_t linebytes = lodepng_get_raw_size_idat(
w, 1, bpp) - 1u;
4579 for(
y = 0;
y <
h; ++
y) {
4580 size_t outindex = linebytes *
y;
4581 size_t inindex = (1 + linebytes) *
y;
4582 unsigned char filterType = in[inindex];
4584 CERROR_TRY_RETURN(unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes));
4586 prevline = &out[outindex];
4603static void Adam7_deinterlace(
unsigned char* out,
const unsigned char* in,
unsigned w,
unsigned h,
unsigned bpp) {
4604 unsigned passw[7], passh[7];
4605 size_t filter_passstart[8], padded_passstart[8], passstart[8];
4608 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart,
w,
h, bpp);
4611 for(i = 0; i != 7; ++i) {
4613 size_t bytewidth = bpp / 8u;
4614 for(
y = 0;
y < passh[i]; ++
y)
4615 for(
x = 0;
x < passw[i]; ++
x) {
4616 size_t pixelinstart = passstart[i] + (
y * passw[i] +
x) * bytewidth;
4617 size_t pixeloutstart = ((ADAM7_IY[i] + (size_t)
y * ADAM7_DY[i]) * (size_t)
w
4618 + ADAM7_IX[i] + (
size_t)
x * ADAM7_DX[i]) * bytewidth;
4619 for(
b = 0;
b < bytewidth; ++
b) {
4620 out[pixeloutstart +
b] = in[pixelinstart +
b];
4625 for(i = 0; i != 7; ++i) {
4627 unsigned ilinebits = bpp * passw[i];
4628 unsigned olinebits = bpp *
w;
4630 for(
y = 0;
y < passh[i]; ++
y)
4631 for(
x = 0;
x < passw[i]; ++
x) {
4632 ibp = (8 * passstart[i]) + (
y * ilinebits +
x * bpp);
4633 obp = (ADAM7_IY[i] + (size_t)
y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + (size_t)
x * ADAM7_DX[i]) * bpp;
4634 for(
b = 0;
b < bpp; ++
b) {
4635 unsigned char bit = readBitFromReversedStream(&ibp, in);
4636 setBitOfReversedStream(&obp, out, bit);
4643static void removePaddingBits(
unsigned char* out,
const unsigned char* in,
4644 size_t olinebits,
size_t ilinebits,
unsigned h) {
4655 size_t diff = ilinebits - olinebits;
4656 size_t ibp = 0, obp = 0;
4657 for(
y = 0;
y <
h; ++
y) {
4659 for(
x = 0;
x < olinebits; ++
x) {
4660 unsigned char bit = readBitFromReversedStream(&ibp, in);
4661 setBitOfReversedStream(&obp, out, bit);
4670static unsigned postProcessScanlines(
unsigned char* out,
unsigned char* in,
4680 if(bpp == 0)
return 31;
4683 if(bpp < 8 &&
w * bpp != ((
w * bpp + 7u) / 8u) * 8u) {
4685 removePaddingBits(out, in,
w * bpp, ((
w * bpp + 7u) / 8u) * 8u,
h);
4690 unsigned passw[7], passh[7];
size_t filter_passstart[8], padded_passstart[8], passstart[8];
4693 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart,
w,
h, bpp);
4695 for(i = 0; i != 7; ++i) {
4696 CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp));
4702 removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp,
4703 ((passw[i] * bpp + 7u) / 8u) * 8u, passh[i]);
4707 Adam7_deinterlace(out, in,
w,
h, bpp);
4714 unsigned pos = 0, i;
4715 color->palettesize = chunkLength / 3u;
4716 if(
color->palettesize == 0 ||
color->palettesize > 256)
return 38;
4717 lodepng_color_mode_alloc_palette(
color);
4719 color->palettesize = 0;
4723 for(i = 0; i !=
color->palettesize; ++i) {
4724 color->palette[4 * i + 0] =
data[pos++];
4725 color->palette[4 * i + 1] =
data[pos++];
4726 color->palette[4 * i + 2] =
data[pos++];
4727 color->palette[4 * i + 3] = 255;
4737 if(chunkLength >
color->palettesize)
return 39;
4739 for(i = 0; i != chunkLength; ++i)
color->palette[4 * i + 3] =
data[i];
4742 if(chunkLength != 2)
return 30;
4744 color->key_defined = 1;
4748 if(chunkLength != 6)
return 41;
4750 color->key_defined = 1;
4761#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4763static unsigned readChunk_bKGD(
LodePNGInfo* info,
const unsigned char*
data,
size_t chunkLength) {
4766 if(chunkLength != 1)
return 43;
4775 if(chunkLength != 2)
return 44;
4782 if(chunkLength != 6)
return 45;
4795static unsigned readChunk_tEXt(
LodePNGInfo* info,
const unsigned char*
data,
size_t chunkLength) {
4797 char *key = 0, *str = 0;
4800 unsigned length, string2_begin;
4844 unsigned char*
str = 0;
4944 unsigned char*
str = 0;
4971 info->time_defined = 1;
4985 info->phys_defined = 1;
4996 info->gama_defined = 1;
5005 info->chrm_defined = 1;
5021 info->srgb_defined = 1;
5037 info->iccp_defined = 1;
5045 if(!
info->iccp_name)
return 83;
5063 if(!error && !
info->iccp_profile_size) error = 100;
5073 if(
data[0] == 0 ||
data[0] > bitdepth)
return 115;
5074 info->sbit_defined = 1;
5079 if(
data[0] == 0 ||
data[1] == 0 ||
data[2] == 0)
return 115;
5080 if(
data[0] > bitdepth ||
data[1] > bitdepth ||
data[2] > bitdepth)
return 115;
5081 info->sbit_defined = 1;
5088 if(
data[0] == 0 ||
data[1] == 0)
return 115;
5089 if(
data[0] > bitdepth ||
data[1] > bitdepth)
return 115;
5090 info->sbit_defined = 1;
5096 if(
data[0] == 0 ||
data[1] == 0 ||
data[2] == 0 ||
data[3] == 0)
return 115;
5097 if(
data[0] > bitdepth ||
data[1] > bitdepth ||
data[2] > bitdepth ||
data[3] > bitdepth)
return 115;
5098 info->sbit_defined = 1;
5110 const unsigned char*
in,
size_t insize) {
5113 const unsigned char*
data;
5127#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5166 const unsigned char*
in,
size_t insize) {
5167 unsigned char IEND = 0;
5168 const unsigned char*
chunk;
5169 unsigned char*
idat;
5177#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5187 if(
state->error)
return;
5203 const unsigned char*
data;
5208 if(
state->decoder.ignore_end)
break;
5216 if(
state->decoder.ignore_end)
break;
5235#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5244 if(
state->error)
break;
5245#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5253 if(
state->error)
break;
5254#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5258 if(
state->error)
break;
5261 if(
state->decoder.read_text_chunks) {
5263 if(
state->error)
break;
5267 if(
state->decoder.read_text_chunks) {
5269 if(
state->error)
break;
5273 if(
state->decoder.read_text_chunks) {
5275 if(
state->error)
break;
5279 if(
state->error)
break;
5282 if(
state->error)
break;
5285 if(
state->error)
break;
5288 if(
state->error)
break;
5291 if(
state->error)
break;
5294 if(
state->error)
break;
5297 if(
state->error)
break;
5306#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5307 if(
state->decoder.remember_unknown_chunks) {
5310 if(
state->error)
break;
5329 if(
state->info_png.interlace_method == 0) {
5364 const unsigned char*
in,
size_t insize) {
5372 if(!
state->decoder.color_convert) {
5383 && !(
state->info_raw.bitdepth == 8)) {
5393 &
state->info_png.color, *
w, *
h);
5396 return state->error;
5404 state.info_raw.colortype = colortype;
5405 state.info_raw.bitdepth = bitdepth;
5406#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5408 state.decoder.read_text_chunks = 0;
5409 state.decoder.remember_unknown_chunks = 0;
5424#ifdef LODEPNG_COMPILE_DISK
5427 unsigned char*
buffer = 0;
5450#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5452 settings->remember_unknown_chunks = 0;
5453 settings->max_text_size = 16777216;
5464#if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER)
5467#ifdef LODEPNG_COMPILE_DECODER
5470#ifdef LODEPNG_COMPILE_ENCODER
5494#ifdef LODEPNG_COMPILE_ENCODER
5503 const unsigned char signature[] = {137, 80, 78, 71, 13, 10, 26, 10};
5511 LodePNGColorType colortype,
unsigned bitdepth,
unsigned interlace_method) {
5519 data[9] = (
unsigned char)colortype;
5522 data[12] = interlace_method;
5530 unsigned char*
chunk;
5533 if(
info->palettesize == 0 ||
info->palettesize > 256) {
5539 for(
i = 0;
i !=
info->palettesize; ++
i) {
5551 unsigned char*
chunk = 0;
5556 for(
i =
info->palettesize;
i != 0; --
i) {
5557 if(
info->palette[4 * (
i - 1) + 3] != 255)
break;
5566 if(
info->key_defined) {
5572 if(
info->key_defined) {
5590 unsigned char*
zlib = 0;
5605#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5608 unsigned char*
chunk = 0;
5623 unsigned char*
chunk = 0;
5651 unsigned char*
chunk = 0;
5693 unsigned char*
chunk = 0;
5715 unsigned char*
chunk;
5723 chunk[14] = (
unsigned char)time->second;
5729 unsigned char*
chunk;
5739 unsigned char*
chunk;
5747 unsigned char*
chunk;
5762 unsigned char data =
info->srgb_intent;
5768 unsigned char*
chunk = 0;
5775 info->iccp_profile,
info->iccp_profile_size, zlibsettings);
5794 unsigned char*
chunk = 0;
5796 if(
info->sbit_r == 0 ||
info->sbit_r > bitdepth)
return 115;
5800 if(
info->sbit_r == 0 ||
info->sbit_g == 0 ||
info->sbit_b == 0)
return 115;
5801 if(
info->sbit_r > bitdepth ||
info->sbit_g > bitdepth ||
info->sbit_b > bitdepth)
return 115;
5807 if(
info->sbit_r == 0 ||
info->sbit_a == 0)
return 115;
5808 if(
info->sbit_r > bitdepth ||
info->sbit_a > bitdepth)
return 115;
5813 if(
info->sbit_r == 0 ||
info->sbit_g == 0 ||
info->sbit_b == 0 ||
info->sbit_a == 0 ||
5814 info->sbit_r > bitdepth ||
info->sbit_g > bitdepth ||
5815 info->sbit_b > bitdepth ||
info->sbit_a > bitdepth) {
5875static size_t ilog2(
size_t i) {
5877 if(
i >= 65536) {
result += 16;
i >>= 16; }
5878 if(
i >= 256) {
result += 8;
i >>= 8; }
5879 if(
i >= 16) {
result += 4;
i >>= 4; }
5880 if(
i >= 4) {
result += 2;
i >>= 2; }
5886static size_t ilog2i(
size_t i) {
5888 if(
i == 0)
return 0;
5892 return i *
l + ((
i - (1u <<
l)) << 1u);
5895static unsigned filter(
unsigned char*
out,
const unsigned char*
in,
unsigned w,
unsigned h,
5927 if(
settings->filter_palette_zero &&
5930 if(bpp == 0)
return 31;
5934 for(
y = 0;
y !=
h; ++
y) {
5953 for(
y = 0;
y !=
h; ++
y) {
5968 sum +=
s < 128 ?
s : (255U -
s);
5992 unsigned count[256];
6000 for(
y = 0;
y !=
h; ++
y) {
6008 for(
x = 0;
x != 256; ++
x) {
6028 for(
y = 0;
y !=
h; ++
y) {
6044 unsigned char*
dummy;
6051 zlibsettings.
btype = 1;
6061 for(
y = 0;
y !=
h; ++
y) {
6096 for(
y = 0;
y !=
h; ++
y) {
6119static void Adam7_interlace(
unsigned char*
out,
const unsigned char*
in,
unsigned w,
unsigned h,
unsigned bpp) {
6127 for(
i = 0;
i != 7; ++
i) {
6140 for(
i = 0;
i != 7; ++
i) {
6149 for(
b = 0;
b < bpp; ++
b) {
6161 unsigned w,
unsigned h,
6174 if(!(*
out) && (*outsize)) error = 83;
6178 if(bpp < 8 &&
w * bpp != ((
w * bpp + 7u) / 8u) * 8u) {
6194 unsigned char*
adam7;
6200 if(!(*
out)) error = 83;
6209 for(
i = 0;
i != 7; ++
i) {
6233#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6238 out->allocsize =
out->size;
6254 if(
size < 20)
return 0;
6260 if(
size < 20)
return 0;
6266 const unsigned char*
image,
unsigned w,
unsigned h,
6268 unsigned char*
data = 0;
6293 if(
state->encoder.zlibsettings.btype > 2) {
6308 if(
state->encoder.auto_convert) {
6312#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6317 stats.allow_palette = 0;
6322 stats.allow_greyscale = 0;
6327#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6330 unsigned r = 0,
g = 0,
b = 0;
6340#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6390 if(
state->encoder.force_palette) {
6400#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6412#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6424 state->error =
state->encoder.auto_convert ? 102 : 101;
6449#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6458#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6460 if(
info.unknown_chunks_data[0]) {
6465 if(
info.iccp_defined) {
6469 if(
info.srgb_defined) {
6473 if(
info.gama_defined) {
6477 if(
info.chrm_defined) {
6499#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6501 if(
info.background_defined) {
6506 if(
info.phys_defined) {
6512 if(
info.unknown_chunks_data[1]) {
6520#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6522 if(
info.time_defined) {
6527 for(
i = 0;
i !=
info.text_num; ++
i) {
6536 if(
state->encoder.text_compression) {
6545 if(
state->encoder.add_id) {
6547 for(
i = 0;
i !=
info.text_num; ++
i) {
6548 const char*
k =
info.text_keys[
i];
6550 if(
k[0] ==
'L' &&
k[1] ==
'o' &&
k[2] ==
'd' &&
k[3] ==
'e' &&
6551 k[4] ==
'P' &&
k[5] ==
'N' &&
k[6] ==
'G' &&
k[7] ==
'\0') {
6562 for(
i = 0;
i !=
info.itext_num; ++
i) {
6574 &
state->encoder.zlibsettings);
6579 if(
info.unknown_chunks_data[2]) {
6597 return state->error;
6605 state.info_raw.colortype = colortype;
6606 state.info_raw.bitdepth = bitdepth;
6607 state.info_png.color.colortype = colortype;
6608 state.info_png.color.bitdepth = bitdepth;
6610 error =
state.error;
6623#ifdef LODEPNG_COMPILE_DISK
6650#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
6659#ifdef LODEPNG_COMPILE_ERROR_TEXT
6666 case 0:
return "no error, everything went ok";
6667 case 1:
return "nothing done yet";
6668 case 10:
return "end of input memory reached without huffman end code";
6669 case 11:
return "error in code tree made it jump outside of huffman tree";
6670 case 13:
return "problem while processing dynamic deflate block";
6671 case 14:
return "problem while processing dynamic deflate block";
6672 case 15:
return "problem while processing dynamic deflate block";
6674 case 16:
return "invalid code while processing dynamic deflate block";
6675 case 17:
return "end of out buffer memory reached while inflating";
6676 case 18:
return "invalid distance code while inflating";
6677 case 19:
return "end of out buffer memory reached while inflating";
6678 case 20:
return "invalid deflate block BTYPE encountered while decoding";
6679 case 21:
return "NLEN is not ones complement of LEN in a deflate block";
6685 case 22:
return "end of out buffer memory reached while inflating";
6686 case 23:
return "end of in buffer memory reached while inflating";
6687 case 24:
return "invalid FCHECK in zlib header";
6688 case 25:
return "invalid compression method in zlib header";
6689 case 26:
return "FDICT encountered in zlib header while it's not used for PNG";
6690 case 27:
return "PNG file is smaller than a PNG header";
6692 case 28:
return "incorrect PNG signature, it's no PNG or corrupted";
6693 case 29:
return "first chunk is not the header chunk";
6694 case 30:
return "chunk length too large, chunk broken off at end of file";
6695 case 31:
return "illegal PNG color type or bpp";
6696 case 32:
return "illegal PNG compression method";
6697 case 33:
return "illegal PNG filter method";
6698 case 34:
return "illegal PNG interlace method";
6699 case 35:
return "chunk length of a chunk is too large or the chunk too small";
6700 case 36:
return "illegal PNG filter type encountered";
6701 case 37:
return "illegal bit depth for this color type given";
6702 case 38:
return "the palette is too small or too big";
6703 case 39:
return "tRNS chunk before PLTE or has more entries than palette size";
6704 case 40:
return "tRNS chunk has wrong size for grayscale image";
6705 case 41:
return "tRNS chunk has wrong size for RGB image";
6706 case 42:
return "tRNS chunk appeared while it was not allowed for this color type";
6707 case 43:
return "bKGD chunk has wrong size for palette image";
6708 case 44:
return "bKGD chunk has wrong size for grayscale image";
6709 case 45:
return "bKGD chunk has wrong size for RGB image";
6710 case 48:
return "empty input buffer given to decoder. Maybe caused by non-existing file?";
6711 case 49:
return "jumped past memory while generating dynamic huffman tree";
6712 case 50:
return "jumped past memory while generating dynamic huffman tree";
6713 case 51:
return "jumped past memory while inflating huffman block";
6714 case 52:
return "jumped past memory while inflating";
6715 case 53:
return "size of zlib data too small";
6716 case 54:
return "repeat symbol in tree while there was no value symbol yet";
6720 case 55:
return "jumped past tree while generating huffman tree";
6721 case 56:
return "given output image colortype or bitdepth not supported for color conversion";
6722 case 57:
return "invalid CRC encountered (checking CRC can be disabled)";
6723 case 58:
return "invalid ADLER32 encountered (checking ADLER32 can be disabled)";
6724 case 59:
return "requested color conversion not supported";
6725 case 60:
return "invalid window size given in the settings of the encoder (must be 0-32768)";
6726 case 61:
return "invalid BTYPE given in the settings of the encoder (only 0, 1 and 2 are allowed)";
6728 case 62:
return "conversion from color to grayscale not supported";
6730 case 63:
return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk";
6732 case 64:
return "the length of the END symbol 256 in the Huffman tree is 0";
6733 case 66:
return "the length of a text chunk keyword given to the encoder is longer than the maximum of 79 bytes";
6734 case 67:
return "the length of a text chunk keyword given to the encoder is smaller than the minimum of 1 byte";
6735 case 68:
return "tried to encode a PLTE chunk with a palette that has less than 1 or more than 256 colors";
6736 case 69:
return "unknown chunk type with 'critical' flag encountered by the decoder";
6737 case 71:
return "invalid interlace mode given to encoder (must be 0 or 1)";
6738 case 72:
return "while decoding, invalid compression method encountering in zTXt or iTXt chunk (it must be 0)";
6739 case 73:
return "invalid tIME chunk size";
6740 case 74:
return "invalid pHYs chunk size";
6742 case 75:
return "no null termination char found while decoding text chunk";
6743 case 76:
return "iTXt chunk too short to contain required bytes";
6744 case 77:
return "integer overflow in buffer size";
6745 case 78:
return "failed to open file for reading";
6746 case 79:
return "failed to open file for writing";
6747 case 80:
return "tried creating a tree of 0 symbols";
6748 case 81:
return "lazy matching at pos 0 is impossible";
6749 case 82:
return "color conversion to palette requested while a color isn't in palette, or index out of bounds";
6750 case 83:
return "memory allocation failed";
6751 case 84:
return "given image too small to contain all pixels to be encoded";
6752 case 86:
return "impossible offset in lz77 encoding (internal bug)";
6753 case 87:
return "must provide custom zlib function pointer if LODEPNG_COMPILE_ZLIB is not defined";
6754 case 88:
return "invalid filter strategy given for LodePNGEncoderSettings.filter_strategy";
6755 case 89:
return "text chunk keyword too short or long: must have size 1-79";
6757 case 90:
return "windowsize must be a power of two";
6758 case 91:
return "invalid decompressed idat size";
6759 case 92:
return "integer overflow due to too many pixels";
6760 case 93:
return "zero width or height is invalid";
6761 case 94:
return "header chunk must have a size of 13 bytes";
6762 case 95:
return "integer overflow with combined idat chunk size";
6763 case 96:
return "invalid gAMA chunk size";
6764 case 97:
return "invalid cHRM chunk size";
6765 case 98:
return "invalid sRGB chunk size";
6766 case 99:
return "invalid sRGB rendering intent";
6767 case 100:
return "invalid ICC profile color type, the PNG specification only allows RGB or GRAY";
6768 case 101:
return "PNG specification does not allow RGB ICC profile on gray color types and vice versa";
6769 case 102:
return "not allowed to set grayscale ICC profile with colored pixels by PNG specification";
6770 case 103:
return "invalid palette index in bKGD chunk. Maybe it came before PLTE chunk?";
6771 case 104:
return "invalid bKGD color while encoding (e.g. palette index out of range)";
6772 case 105:
return "integer overflow of bitsize";
6773 case 106:
return "PNG file must have PLTE chunk if color type is palette";
6774 case 107:
return "color convert from palette mode requested without setting the palette data in it";
6775 case 108:
return "tried to add more than 256 values to a palette";
6777 case 109:
return "tried to decompress zlib or deflate data larger than desired max_output_size";
6778 case 110:
return "custom zlib or inflate decompression failed";
6779 case 111:
return "custom zlib or deflate compression failed";
6782 case 112:
return "compressed text unreasonably large";
6785 case 113:
return "ICC profile unreasonably large";
6786 case 114:
return "sBIT chunk has wrong size for the color type of the image";
6787 case 115:
return "sBIT value out of range";
6789 return "unknown error code";
6799#ifdef LODEPNG_COMPILE_CPP
6802#ifdef LODEPNG_COMPILE_DISK
6805 if(
size < 0)
return 78;
6816#ifdef LODEPNG_COMPILE_ZLIB
6817#ifdef LODEPNG_COMPILE_DECODER
6820 unsigned char*
buffer = 0;
6830unsigned decompress(std::vector<unsigned char>&
out,
const std::vector<unsigned char>&
in,
6836#ifdef LODEPNG_COMPILE_ENCODER
6839 unsigned char*
buffer = 0;
6849unsigned compress(std::vector<unsigned char>&
out,
const std::vector<unsigned char>&
in,
6857#ifdef LODEPNG_COMPILE_PNG
6877#ifdef LODEPNG_COMPILE_DECODER
6879unsigned decode(std::vector<unsigned char>&
out,
unsigned&
w,
unsigned&
h,
const unsigned char*
in,
6881 unsigned char*
buffer = 0;
6885 state.info_raw.colortype = colortype;
6886 state.info_raw.bitdepth = bitdepth;
6894unsigned decode(std::vector<unsigned char>&
out,
unsigned&
w,
unsigned&
h,
6895 const std::vector<unsigned char>&
in,
LodePNGColorType colortype,
unsigned bitdepth) {
6899unsigned decode(std::vector<unsigned char>&
out,
unsigned&
w,
unsigned&
h,
6901 const unsigned char*
in,
size_t insize) {
6912unsigned decode(std::vector<unsigned char>&
out,
unsigned&
w,
unsigned&
h,
6914 const std::vector<unsigned char>&
in) {
6918#ifdef LODEPNG_COMPILE_DISK
6919unsigned decode(std::vector<unsigned char>&
out,
unsigned&
w,
unsigned&
h,
const std::string&
filename,
6921 std::vector<unsigned char>
buffer;
6925 if(error)
return error;
6931#ifdef LODEPNG_COMPILE_ENCODER
6932unsigned encode(std::vector<unsigned char>&
out,
const unsigned char*
in,
unsigned w,
unsigned h,
6944unsigned encode(std::vector<unsigned char>&
out,
6945 const std::vector<unsigned char>&
in,
unsigned w,
unsigned h,
6948 return encode(
out,
in.empty() ? 0 : &
in[0],
w,
h, colortype, bitdepth);
6951unsigned encode(std::vector<unsigned char>&
out,
6952 const unsigned char*
in,
unsigned w,
unsigned h,
6964unsigned encode(std::vector<unsigned char>&
out,
6965 const std::vector<unsigned char>&
in,
unsigned w,
unsigned h,
6971#ifdef LODEPNG_COMPILE_DISK
6973 const unsigned char*
in,
unsigned w,
unsigned h,
6975 std::vector<unsigned char>
buffer;
6982 const std::vector<unsigned char>&
in,
unsigned w,
unsigned h,
GLfloat GLfloat GLfloat alpha
GLsizei const GLfloat * values
GLenum const void * lists
GLsizei GLenum GLenum GLuint GLenum GLsizei * lengths
GLenum GLsizei const void * pointer
GLdouble GLdouble GLdouble q
GLint GLsizei GLsizei height
GLboolean GLboolean GLboolean b
GLsizei GLsizei GLchar * source
GLenum GLenum GLsizei void * image
GLint GLint GLsizei GLint GLenum GLenum type
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLuint const GLchar * name
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLuint GLsizei GLsizei * length
GLboolean GLboolean GLboolean GLboolean a
GLfloat GLfloat GLfloat GLfloat h
GLdouble GLdouble GLdouble w
#define LODEPNG_MAX(a, b)
#define CERROR_RETURN_ERROR(errorvar, code)
static void lodepng_memcpy(void *LODEPNG_RESTRICT dst, const void *LODEPNG_RESTRICT src, size_t size)
static unsigned ucvector_reserve(ucvector *p, size_t size)
void * lodepng_realloc(void *ptr, size_t new_size)
static ucvector ucvector_init(unsigned char *buffer, size_t size)
#define ERROR_BREAK(code)
#define CERROR_TRY_RETURN(call)
static size_t lodepng_strlen(const char *a)
const char * LODEPNG_VERSION_STRING
#define CERROR_RETURN(errorvar, code)
static void lodepng_memset(void *LODEPNG_RESTRICT dst, int value, size_t num)
#define LODEPNG_MIN(a, b)
void lodepng_free(void *ptr)
static unsigned ucvector_resize(ucvector *p, size_t size)
#define CERROR_BREAK(errorvar, code)
void * lodepng_malloc(size_t size)
unsigned lodepng_encode_file(const char *filename, const unsigned char *image, unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth)
unsigned lodepng_encode32_file(const char *filename, const unsigned char *image, unsigned w, unsigned h)
unsigned lodepng_chunk_check_crc(const unsigned char *chunk)
unsigned lodepng_inspect_chunk(LodePNGState *state, size_t pos, const unsigned char *in, size_t insize)
unsigned lodepng_palette_add(LodePNGColorMode *info, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
const LodePNGCompressSettings lodepng_default_compress_settings
unsigned lodepng_inspect(unsigned *w, unsigned *h, LodePNGState *state, const unsigned char *in, size_t insize)
unsigned lodepng_add_text(LodePNGInfo *info, const char *key, const char *str)
unsigned lodepng_info_copy(LodePNGInfo *dest, const LodePNGInfo *source)
unsigned lodepng_crc32(const unsigned char *buf, size_t len)
void lodepng_clear_icc(LodePNGInfo *info)
unsigned char * lodepng_chunk_next(unsigned char *chunk, unsigned char *end)
unsigned char * lodepng_chunk_find(unsigned char *chunk, unsigned char *end, const char type[5])
unsigned char lodepng_chunk_private(const unsigned char *chunk)
void lodepng_palette_clear(LodePNGColorMode *info)
void lodepng_chunk_type(char type[5], const unsigned char *chunk)
unsigned char lodepng_chunk_safetocopy(const unsigned char *chunk)
unsigned lodepng_decode32_file(unsigned char **out, unsigned *w, unsigned *h, const char *filename)
unsigned lodepng_encode24(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
LodePNGColorMode lodepng_color_mode_make(LodePNGColorType colortype, unsigned bitdepth)
unsigned lodepng_has_palette_alpha(const LodePNGColorMode *info)
unsigned lodepng_encode24_file(const char *filename, const unsigned char *image, unsigned w, unsigned h)
unsigned char lodepng_chunk_type_equals(const unsigned char *chunk, const char *type)
void lodepng_clear_text(LodePNGInfo *info)
size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode *color)
const unsigned char * lodepng_chunk_data_const(const unsigned char *chunk)
void lodepng_chunk_generate_crc(unsigned char *chunk)
unsigned char * lodepng_chunk_data(unsigned char *chunk)
unsigned lodepng_decode_memory(unsigned char **out, unsigned *w, unsigned *h, const unsigned char *in, size_t insize, LodePNGColorType colortype, unsigned bitdepth)
void lodepng_color_mode_init(LodePNGColorMode *info)
unsigned lodepng_decode32(unsigned char **out, unsigned *w, unsigned *h, const unsigned char *in, size_t insize)
unsigned lodepng_decode_file(unsigned char **out, unsigned *w, unsigned *h, const char *filename, LodePNGColorType colortype, unsigned bitdepth)
unsigned lodepng_deflate(unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodePNGCompressSettings *settings)
void lodepng_compress_settings_init(LodePNGCompressSettings *settings)
unsigned lodepng_decode(unsigned char **out, unsigned *w, unsigned *h, LodePNGState *state, const unsigned char *in, size_t insize)
unsigned lodepng_zlib_compress(unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodePNGCompressSettings *settings)
void lodepng_state_cleanup(LodePNGState *state)
unsigned lodepng_encode32(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
unsigned lodepng_color_mode_copy(LodePNGColorMode *dest, const LodePNGColorMode *source)
void lodepng_color_stats_init(LodePNGColorStats *stats)
unsigned lodepng_huffman_code_lengths(unsigned *lengths, const unsigned *frequencies, size_t numcodes, unsigned maxbitlen)
unsigned lodepng_get_bpp(const LodePNGColorMode *info)
unsigned lodepng_save_file(const unsigned char *buffer, size_t buffersize, const char *filename)
void lodepng_encoder_settings_init(LodePNGEncoderSettings *settings)
void lodepng_state_copy(LodePNGState *dest, const LodePNGState *source)
const unsigned char * lodepng_chunk_next_const(const unsigned char *chunk, const unsigned char *end)
unsigned lodepng_encode(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h, LodePNGState *state)
void lodepng_decoder_settings_init(LodePNGDecoderSettings *settings)
unsigned lodepng_can_have_alpha(const LodePNGColorMode *info)
unsigned lodepng_inflate(unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodePNGDecompressSettings *settings)
unsigned lodepng_is_alpha_type(const LodePNGColorMode *info)
const LodePNGDecompressSettings lodepng_default_decompress_settings
unsigned char lodepng_chunk_ancillary(const unsigned char *chunk)
unsigned lodepng_chunk_create(unsigned char **out, size_t *outsize, unsigned length, const char *type, const unsigned char *data)
unsigned lodepng_add_itext(LodePNGInfo *info, const char *key, const char *langtag, const char *transkey, const char *str)
unsigned lodepng_decode24_file(unsigned char **out, unsigned *w, unsigned *h, const char *filename)
void lodepng_color_mode_cleanup(LodePNGColorMode *info)
void lodepng_decompress_settings_init(LodePNGDecompressSettings *settings)
unsigned lodepng_get_channels(const LodePNGColorMode *info)
unsigned lodepng_convert(unsigned char *out, const unsigned char *in, const LodePNGColorMode *mode_out, const LodePNGColorMode *mode_in, unsigned w, unsigned h)
unsigned lodepng_chunk_append(unsigned char **out, size_t *outsize, const unsigned char *chunk)
const char * lodepng_error_text(unsigned code)
void lodepng_info_cleanup(LodePNGInfo *info)
unsigned lodepng_chunk_length(const unsigned char *chunk)
unsigned lodepng_decode24(unsigned char **out, unsigned *w, unsigned *h, const unsigned char *in, size_t insize)
unsigned lodepng_is_palette_type(const LodePNGColorMode *info)
unsigned lodepng_encode_memory(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth)
unsigned lodepng_is_greyscale_type(const LodePNGColorMode *info)
void lodepng_state_init(LodePNGState *state)
void lodepng_info_init(LodePNGInfo *info)
unsigned lodepng_zlib_decompress(unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodePNGDecompressSettings *settings)
const unsigned char * lodepng_chunk_find_const(const unsigned char *chunk, const unsigned char *end, const char type[5])
unsigned lodepng_compute_color_stats(LodePNGColorStats *stats, const unsigned char *image, unsigned w, unsigned h, const LodePNGColorMode *mode_in)
void lodepng_clear_itext(LodePNGInfo *info)
unsigned lodepng_set_icc(LodePNGInfo *info, const char *name, const unsigned char *profile, unsigned profile_size)
unsigned lodepng_load_file(unsigned char **out, size_t *outsize, const char *filename)
LodePNGColorType colortype
unsigned char palette[1024]
unsigned(* custom_zlib)(unsigned char **, size_t *, const unsigned char *, size_t, const LodePNGCompressSettings *)
unsigned(* custom_deflate)(unsigned char **, size_t *, const unsigned char *, size_t, const LodePNGCompressSettings *)
const void * custom_context
LodePNGDecompressSettings zlibsettings
unsigned(* custom_inflate)(unsigned char **, size_t *, const unsigned char *, size_t, const LodePNGDecompressSettings *)
const void * custom_context
unsigned(* custom_zlib)(unsigned char **, size_t *, const unsigned char *, size_t, const LodePNGDecompressSettings *)
unsigned iccp_profile_size
size_t unknown_chunks_size[3]
unsigned compression_method
unsigned char * iccp_profile
unsigned interlace_method
unsigned char * unknown_chunks_data[3]
unsigned background_defined
LodePNGDecoderSettings decoder