Lines Matching +full:- +full:p

4 Copyright (c) 2005-2020 Lode Vandevenne
6 This software is provided 'as-is', without any express or implied
43 #pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requi…
52 -Tools for C and common code for PNG and Zlib
53 -C Code for Zlib (huffman, deflate, ...)
54 -C Code for PNG (file format chunks, adam7, PNG filters, color conversions, ...)
55 -The C++ wrapper around all of the above
67 -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler, or comment out
136 return (size_t)(a - orig); in lodepng_strlen()
141 #define LODEPNG_ABS(x) ((x) < 0 ? -(x) : (x))
206 -All of them wrap dynamic arrays or text strings in a similar way.
207 -LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the …
208 -The string tools are made to avoid problems with compilers that declare things like strncat as dep…
209 -They're not used in the interface, only internally in this file as static functions.
210 -As with many other structs in this file, the init and cleanup functions serve as ctor and dtor.
222 static void uivector_cleanup(void* p) { in uivector_cleanup() argument
223 ((uivector*)p)->size = ((uivector*)p)->allocsize = 0; in uivector_cleanup()
224 lodepng_free(((uivector*)p)->data); in uivector_cleanup()
225 ((uivector*)p)->data = NULL; in uivector_cleanup()
229 static unsigned uivector_resize(uivector* p, size_t size) { in uivector_resize() argument
231 if(allocsize > p->allocsize) { in uivector_resize()
232 size_t newsize = allocsize + (p->allocsize >> 1u); in uivector_resize()
233 void* data = lodepng_realloc(p->data, newsize); in uivector_resize()
235 p->allocsize = newsize; in uivector_resize()
236 p->data = (unsigned*)data; in uivector_resize()
240 p->size = size; in uivector_resize()
244 static void uivector_init(uivector* p) { in uivector_init() argument
245 p->data = NULL; in uivector_init()
246 p->size = p->allocsize = 0; in uivector_init()
250 static unsigned uivector_push_back(uivector* p, unsigned c) { in uivector_push_back() argument
251 if(!uivector_resize(p, p->size + 1)) return 0; in uivector_push_back()
252 p->data[p->size - 1] = c; in uivector_push_back()
268 static unsigned ucvector_resize(ucvector* p, size_t size) { in ucvector_resize() argument
269 if(size > p->allocsize) { in ucvector_resize()
270 size_t newsize = size + (p->allocsize >> 1u); in ucvector_resize()
271 void* data = lodepng_realloc(p->data, newsize); in ucvector_resize()
273 p->allocsize = newsize; in ucvector_resize()
274 p->data = (unsigned char*)data; in ucvector_resize()
278 p->size = size; in ucvector_resize()
346 if(res != LV_FS_RES_OK) return -1; in lodepng_filesize()
350 return -1; in lodepng_filesize()
412 writer->data = data; in LodePNGBitWriter_init()
413 writer->bp = 0; in LodePNGBitWriter_init()
419 if(((writer->bp) & 7u) == 0) {\
420 if(!ucvector_resize(writer->data, writer->data->size + 1)) return;\
421 writer->data->data[writer->data->size - 1] = 0;\
423 (writer->data->data[writer->data->size - 1]) |= (bit << ((writer->bp) & 7u));\
424 ++writer->bp;\
445 WRITEBIT(writer, (unsigned char)((value >> (nbits - 1u - i)) & 1u)); in writeBitsReversed()
463 reader->data = data; in LodePNGBitReader_init()
464 reader->size = size; in LodePNGBitReader_init()
466 if(lodepng_mulofl(size, 8u, &reader->bitsize)) return 105; in LodePNGBitReader_init()
469 if(lodepng_addofl(reader->bitsize, 64u, &temp)) return 105; in LodePNGBitReader_init()
470 reader->bp = 0; in LodePNGBitReader_init()
471 reader->buffer = 0; in LodePNGBitReader_init()
484 if(reader->bp >= reader->bitsize) return 0;
485 reader->buffer = (unsigned)reader->data[reader->bp >> 3u] >> (reader->bp & 7u);
491 size_t start = reader->bp >> 3u; in ensureBits9()
492 size_t size = reader->size; in ensureBits9()
494 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u); in ensureBits9()
495 reader->buffer >>= (reader->bp & 7u); in ensureBits9()
498 reader->buffer = 0; in ensureBits9()
499 if(start + 0u < size) reader->buffer |= reader->data[start + 0]; in ensureBits9()
500 reader->buffer >>= (reader->bp & 7u); in ensureBits9()
501 return reader->bp + nbits <= reader->bitsize; in ensureBits9()
507 size_t start = reader->bp >> 3u; in ensureBits17()
508 size_t size = reader->size; in ensureBits17()
510 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | in ensureBits17()
511 ((unsigned)reader->data[start + 2] << 16u); in ensureBits17()
512 reader->buffer >>= (reader->bp & 7u); in ensureBits17()
515 reader->buffer = 0; in ensureBits17()
516 if(start + 0u < size) reader->buffer |= reader->data[start + 0]; in ensureBits17()
517 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); in ensureBits17()
518 reader->buffer >>= (reader->bp & 7u); in ensureBits17()
519 return reader->bp + nbits <= reader->bitsize; in ensureBits17()
525 size_t start = reader->bp >> 3u; in ensureBits25()
526 size_t size = reader->size; in ensureBits25()
528 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | in ensureBits25()
529 … ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u); in ensureBits25()
530 reader->buffer >>= (reader->bp & 7u); in ensureBits25()
533 reader->buffer = 0; in ensureBits25()
534 if(start + 0u < size) reader->buffer |= reader->data[start + 0]; in ensureBits25()
535 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); in ensureBits25()
536 if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u); in ensureBits25()
537 reader->buffer >>= (reader->bp & 7u); in ensureBits25()
538 return reader->bp + nbits <= reader->bitsize; in ensureBits25()
544 size_t start = reader->bp >> 3u; in ensureBits32()
545 size_t size = reader->size; in ensureBits32()
547 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | in ensureBits32()
548 … ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u); in ensureBits32()
549 reader->buffer >>= (reader->bp & 7u); in ensureBits32()
550 reader->buffer |= (((unsigned)reader->data[start + 4] << 24u) << (8u - (reader->bp & 7u))); in ensureBits32()
553 reader->buffer = 0; in ensureBits32()
554 if(start + 0u < size) reader->buffer |= reader->data[start + 0]; in ensureBits32()
555 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); in ensureBits32()
556 if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u); in ensureBits32()
557 if(start + 3u < size) reader->buffer |= ((unsigned)reader->data[start + 3] << 24u); in ensureBits32()
558 reader->buffer >>= (reader->bp & 7u); in ensureBits32()
559 return reader->bp + nbits <= reader->bitsize; in ensureBits32()
566 return reader->buffer & ((1u << nbits) - 1u); in peekBits()
571 reader->buffer >>= nbits; in advanceBits()
572 reader->bp += nbits; in advanceBits()
609 for(i = 0; i < num; i++) result |= ((bits >> (num - i - 1u)) & 1u) << i; in reverseBits()
614 /* / Deflate - Huffman / */
623 /*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-13…
626 /*the base lengths represented by codes 257-285*/
631 /*the extra bits used by codes 257-285 (added to base length)*/
667 tree->codes = 0; in HuffmanTree_init()
668 tree->lengths = 0; in HuffmanTree_init()
669 tree->table_len = 0; in HuffmanTree_init()
670 tree->table_value = 0; in HuffmanTree_init()
674 lodepng_free(tree->codes); in HuffmanTree_cleanup()
675 lodepng_free(tree->lengths); in HuffmanTree_cleanup()
676 lodepng_free(tree->table_len); in HuffmanTree_cleanup()
677 lodepng_free(tree->table_value); in HuffmanTree_cleanup()
691 static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u; in HuffmanTree_makeTable()
698 for(i = 0; i < tree->numcodes; i++) { in HuffmanTree_makeTable()
699 unsigned symbol = tree->codes[i]; in HuffmanTree_makeTable()
700 unsigned l = tree->lengths[i]; in HuffmanTree_makeTable()
704 index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS); in HuffmanTree_makeTable()
711 if(l > FIRSTBITS) size += (1u << (l - FIRSTBITS)); in HuffmanTree_makeTable()
713 tree->table_len = (unsigned char*)lodepng_malloc(size * sizeof(*tree->table_len)); in HuffmanTree_makeTable()
714 tree->table_value = (unsigned short*)lodepng_malloc(size * sizeof(*tree->table_value)); in HuffmanTree_makeTable()
715 if(!tree->table_len || !tree->table_value) { in HuffmanTree_makeTable()
717 /* freeing tree->table values is done at a higher scope */ in HuffmanTree_makeTable()
721 for(i = 0; i < size; ++i) tree->table_len[i] = 16; in HuffmanTree_makeTable()
728 tree->table_len[i] = l; in HuffmanTree_makeTable()
729 tree->table_value[i] = pointer; in HuffmanTree_makeTable()
730 pointer += (1u << (l - FIRSTBITS)); in HuffmanTree_makeTable()
736 for(i = 0; i < tree->numcodes; ++i) { in HuffmanTree_makeTable()
737 unsigned l = tree->lengths[i]; in HuffmanTree_makeTable()
738 unsigned symbol = tree->codes[i]; /*the huffman bit pattern. i itself is the value.*/ in HuffmanTree_makeTable()
746 unsigned num = 1u << (FIRSTBITS - l); in HuffmanTree_makeTable()
749 …/*bit reader will read the l bits of symbol first, the remaining FIRSTBITS - l bits go to the MSB'… in HuffmanTree_makeTable()
751 …if(tree->table_len[index] != 16) return 55; /*invalid tree: long symbol shares prefix with short s… in HuffmanTree_makeTable()
752 tree->table_len[index] = l; in HuffmanTree_makeTable()
753 tree->table_value[index] = i; in HuffmanTree_makeTable()
759 unsigned maxlen = tree->table_len[index]; in HuffmanTree_makeTable()
760 /*log2 of secondary table length, should be >= l - FIRSTBITS*/ in HuffmanTree_makeTable()
761 unsigned tablelen = maxlen - FIRSTBITS; in HuffmanTree_makeTable()
762 unsigned start = tree->table_value[index]; /*starting index in secondary table*/ in HuffmanTree_makeTable()
763 …unsigned num = 1u << (tablelen - (l - FIRSTBITS)); /*amount of entries of this symbol in secondary… in HuffmanTree_makeTable()
767 unsigned reverse2 = reverse >> FIRSTBITS; /* l - FIRSTBITS bits */ in HuffmanTree_makeTable()
768 unsigned index2 = start + (reverse2 | (j << (l - FIRSTBITS))); in HuffmanTree_makeTable()
769 tree->table_len[index2] = l; in HuffmanTree_makeTable()
770 tree->table_value[index2] = i; in HuffmanTree_makeTable()
783 if(tree->table_len[i] == 16) { in HuffmanTree_makeTable()
787 tree->table_len[i] = (i < headsize) ? 1 : (FIRSTBITS + 1); in HuffmanTree_makeTable()
788 tree->table_value[i] = INVALIDSYMBOL; in HuffmanTree_makeTable()
792 /* A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes. in HuffmanTree_makeTable()
797 if(tree->table_len[i] == 16) return 55; in HuffmanTree_makeTable()
815 tree->codes = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned)); in HuffmanTree_makeFromLengths2()
816 blcount = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned)); in HuffmanTree_makeFromLengths2()
817 nextcode = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned)); in HuffmanTree_makeFromLengths2()
818 if(!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/ in HuffmanTree_makeFromLengths2()
821 for(n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0; in HuffmanTree_makeFromLengths2()
823 for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]]; in HuffmanTree_makeFromLengths2()
825 for(bits = 1; bits <= tree->maxbitlen; ++bits) { in HuffmanTree_makeFromLengths2()
826 nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u; in HuffmanTree_makeFromLengths2()
829 for(n = 0; n != tree->numcodes; ++n) { in HuffmanTree_makeFromLengths2()
830 if(tree->lengths[n] != 0) { in HuffmanTree_makeFromLengths2()
831 tree->codes[n] = nextcode[tree->lengths[n]]++; in HuffmanTree_makeFromLengths2()
833 tree->codes[n] &= ((1u << tree->lengths[n]) - 1u); in HuffmanTree_makeFromLengths2()
853 tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned)); in HuffmanTree_makeFromLengths()
854 if(!tree->lengths) return 83; /*alloc fail*/ in HuffmanTree_makeFromLengths()
855 for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i]; in HuffmanTree_makeFromLengths()
856 tree->numcodes = (unsigned)numcodes; /*number of symbols*/ in HuffmanTree_makeFromLengths()
857 tree->maxbitlen = maxbitlen; in HuffmanTree_makeFromLengths()
863 /*BPM: Boundary Package Merge, see "A Fast and Space-Economical Algorithm for Length-Limited Coding…
894 if(lists->nextfree >= lists->numfree) { in bpmnode_create()
896 for(i = 0; i != lists->memsize; ++i) lists->memory[i].in_use = 0; in bpmnode_create()
897 for(i = 0; i != lists->listsize; ++i) { in bpmnode_create()
899 for(node = lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1; in bpmnode_create()
900 for(node = lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1; in bpmnode_create()
903 lists->numfree = 0; in bpmnode_create()
904 for(i = 0; i != lists->memsize; ++i) { in bpmnode_create()
905 if(!lists->memory[i].in_use) lists->freelist[lists->numfree++] = &lists->memory[i]; in bpmnode_create()
907 lists->nextfree = 0; in bpmnode_create()
910 result = lists->freelist[lists->nextfree++]; in bpmnode_create()
911 result->weight = weight; in bpmnode_create()
912 result->index = index; in bpmnode_create()
913 result->tail = tail; in bpmnode_create()
924 size_t p; in bpmnode_sort() local
925 for(p = 0; p < num; p += 2 * width) { in bpmnode_sort()
926 size_t q = (p + width > num) ? num : (p + width); in bpmnode_sort()
927 size_t r = (p + 2 * width > num) ? num : (p + 2 * width); in bpmnode_sort()
928 size_t i = p, j = q, k; in bpmnode_sort()
929 for(k = p; k < r; k++) { in bpmnode_sort()
942 unsigned lastindex = lists->chains1[c]->index; in boundaryPM()
946 lists->chains0[c] = lists->chains1[c]; in boundaryPM()
947 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, 0); in boundaryPM()
950 int sum = lists->chains0[c - 1]->weight + lists->chains1[c - 1]->weight; in boundaryPM()
951 lists->chains0[c] = lists->chains1[c]; in boundaryPM()
953 …lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, lists->chains1[… in boundaryPM()
956 lists->chains1[c] = bpmnode_create(lists, sum, lastindex, lists->chains1[c - 1]); in boundaryPM()
959 if(num + 1 < (int)(2 * numpresent - 2)) { in boundaryPM()
960 boundaryPM(lists, leaves, numpresent, c - 1, num); in boundaryPM()
961 boundaryPM(lists, leaves, numpresent, c - 1, num); in boundaryPM()
970 size_t numpresent = 0; /*number of symbols with non-zero frequency*/ in lodepng_huffman_code_lengths()
992 Package-Merge code below also doesn't work correctly if there's only one in lodepng_huffman_code_lengths()
1026 … /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/ in lodepng_huffman_code_lengths()
1027 …for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1… in lodepng_huffman_code_lengths()
1029 for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail) { in lodepng_huffman_code_lengths()
1030 for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index]; in lodepng_huffman_code_lengths()
1048 while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/ in HuffmanTree_makeFromFrequencies()
1049 tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned)); in HuffmanTree_makeFromFrequencies()
1050 if(!tree->lengths) return 83; /*alloc fail*/ in HuffmanTree_makeFromFrequencies()
1051 tree->maxbitlen = maxbitlen; in HuffmanTree_makeFromFrequencies()
1052 tree->numcodes = (unsigned)numcodes; /*number of symbols*/ in HuffmanTree_makeFromFrequencies()
1054 error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen); in HuffmanTree_makeFromFrequencies()
1066 /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/ in generateFixedLitLenTree()
1084 /*there are 32 distance codes, but 30-31 are unused*/ in generateFixedDistanceTree()
1099 unsigned short l = codetree->table_len[code]; in huffmanDecodeSymbol()
1100 unsigned short value = codetree->table_value[code]; in huffmanDecodeSymbol()
1107 index2 = value + peekBits(reader, l - FIRSTBITS); in huffmanDecodeSymbol()
1108 advanceBits(reader, codetree->table_len[index2] - FIRSTBITS); in huffmanDecodeSymbol()
1109 return codetree->table_value[index2]; in huffmanDecodeSymbol()
1158 if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) { in getTreeInflateDynamic()
1187 else bitlen_d[i - HLIT] = code; in getTreeInflateDynamic()
1190 unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/ in getTreeInflateDynamic()
1197 if(i < HLIT + 1) value = bitlen_ll[i - 1]; in getTreeInflateDynamic()
1198 else value = bitlen_d[i - HLIT - 1]; in getTreeInflateDynamic()
1203 else bitlen_d[i - HLIT] = value; in getTreeInflateDynamic()
1206 } else if(code == 17) /*repeat "0" 3-10 times*/ { in getTreeInflateDynamic()
1215 else bitlen_d[i - HLIT] = 0; in getTreeInflateDynamic()
1218 } else if(code == 18) /*repeat "0" 11-138 times*/ { in getTreeInflateDynamic()
1227 else bitlen_d[i - HLIT] = 0; in getTreeInflateDynamic()
1234 if(reader->bp > reader->bitsize) { in getTreeInflateDynamic()
1250 break; /*end of error-while*/ in getTreeInflateDynamic()
1280 if(!ucvector_resize(out, out->size + 1)) ERROR_BREAK(83 /*alloc fail*/); in inflateHuffmanBlock()
1281 out->data[out->size - 1] = (unsigned char)code_ll; in inflateHuffmanBlock()
1288 length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX]; in inflateHuffmanBlock()
1291 numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX]; in inflateHuffmanBlock()
1302 ERROR_BREAK(18); /*error: invalid distance code (30-31 are never used)*/ in inflateHuffmanBlock()
1317 start = out->size; in inflateHuffmanBlock()
1319 backward = start - distance; in inflateHuffmanBlock()
1321 if(!ucvector_resize(out, out->size + length)) ERROR_BREAK(83 /*alloc fail*/); in inflateHuffmanBlock()
1324 lodepng_memcpy(out->data + start, out->data + backward, distance); in inflateHuffmanBlock()
1327 out->data[start++] = out->data[backward++]; in inflateHuffmanBlock()
1330 lodepng_memcpy(out->data + start, out->data + backward, length); in inflateHuffmanBlock()
1338 if(reader->bp > reader->bitsize) { in inflateHuffmanBlock()
1344 if(max_output_size && out->size > max_output_size) { in inflateHuffmanBlock()
1358 size_t size = reader->size; in inflateNoCompression()
1362 bytepos = (reader->bp + 7u) >> 3u; in inflateNoCompression()
1366 LEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2; in inflateNoCompression()
1367 …NLEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2; in inflateNoCompression()
1369 /*check if 16-bit NLEN is really the one's complement of LEN*/ in inflateNoCompression()
1370 if(!settings->ignore_nlen && LEN + NLEN != 65535) { in inflateNoCompression()
1374 if(!ucvector_resize(out, out->size + LEN)) return 83; /*alloc fail*/ in inflateNoCompression()
1379 lodepng_memcpy(out->data + out->size - LEN, reader->data + bytepos, LEN); in inflateNoCompression()
1382 reader->bp = bytepos << 3u; in inflateNoCompression()
1404 …else error = inflateHuffmanBlock(out, &reader, BTYPE, settings->max_output_size); /*compression, B… in lodepng_inflatev()
1405 if(!error && settings->max_output_size && out->size > settings->max_output_size) error = 109; in lodepng_inflatev()
1424 if(settings->custom_inflate) { in inflatev()
1425 unsigned error = settings->custom_inflate(&out->data, &out->size, in, insize, settings); in inflatev()
1426 out->allocsize = out->size; in inflatev()
1431 if(settings->max_output_size && out->size > settings->max_output_size) error = 109; in inflatev()
1454 size_t right = array_size - 1; in searchCodeIndex()
1458 if(array[mid] >= value) right = mid - 1; in searchCodeIndex()
1461 if(left >= array_size || array[left] > value) left--; in searchCodeIndex()
1467 0-255: literal bytes in addLengthDistance()
1469 …257-285: length/distance pair (length code, followed by extra length bits, distance code, extra di… in addLengthDistance()
1470 286-287: invalid*/ in addLengthDistance()
1473 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]); in addLengthDistance()
1475 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]); in addLengthDistance()
1477 size_t pos = values->size; in addLengthDistance()
1479 unsigned ok = uivector_resize(values, values->size + 4); in addLengthDistance()
1481 values->data[pos + 0] = length_code + FIRST_LENGTH_CODE_INDEX; in addLengthDistance()
1482 values->data[pos + 1] = extra_length; in addLengthDistance()
1483 values->data[pos + 2] = dist_code; in addLengthDistance()
1484 values->data[pos + 3] = extra_distance; in addLengthDistance()
1491 static const unsigned HASH_BIT_MASK = 65535; /*HASH_NUM_VALUES - 1, but C90 does not like that as i…
1494 int* head; /*hash value to head circular pos - can be outdated if went around window*/
1508 hash->head = (int*)lodepng_malloc(sizeof(int) * HASH_NUM_VALUES); in hash_init()
1509 hash->val = (int*)lodepng_malloc(sizeof(int) * windowsize); in hash_init()
1510 hash->chain = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize); in hash_init()
1512 hash->zeros = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize); in hash_init()
1513 hash->headz = (int*)lodepng_malloc(sizeof(int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1)); in hash_init()
1514 hash->chainz = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize); in hash_init()
1516 if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros) { in hash_init()
1521 for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1; in hash_init()
1522 for(i = 0; i != windowsize; ++i) hash->val[i] = -1; in hash_init()
1523 …for(i = 0; i != windowsize; ++i) hash->chain[i] = i; /*same value as index indicates uninitialized… in hash_init()
1525 for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1; in hash_init()
1526 …for(i = 0; i != windowsize; ++i) hash->chainz[i] = i; /*same value as index indicates uninitialize… in hash_init()
1532 lodepng_free(hash->head); in hash_cleanup()
1533 lodepng_free(hash->val); in hash_cleanup()
1534 lodepng_free(hash->chain); in hash_cleanup()
1536 lodepng_free(hash->zeros); in hash_cleanup()
1537 lodepng_free(hash->headz); in hash_cleanup()
1538 lodepng_free(hash->chainz); in hash_cleanup()
1556 amount = size - pos; in getHash()
1568 …/*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/ in countZeros()
1569 return (unsigned)(data - start); in countZeros()
1572 /*wpos = pos & (windowsize - 1)*/
1574 hash->val[wpos] = (int)hashval; in updateHashChain()
1575 if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval]; in updateHashChain()
1576 hash->head[hashval] = (int)wpos; in updateHashChain()
1578 hash->zeros[wpos] = numzeros; in updateHashChain()
1579 if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros]; in updateHashChain()
1580 hash->headz[numzeros] = (int)wpos; in updateHashChain()
1584 LZ77-encode the data. Return value is error code. The input are raw bytes, the output
1615 if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/ in encodeLZ77()
1620 size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/ in encodeLZ77()
1627 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; in encodeLZ77()
1638 hashpos = hash->chain[wpos]; in encodeLZ77()
1646 current_offset = (unsigned)(hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize); in encodeLZ77()
1653 backptr = &in[pos - current_offset]; in encodeLZ77()
1657 unsigned skip = hash->zeros[hashpos]; in encodeLZ77()
1667 current_length = (unsigned)(foreptr - &in[pos]); in encodeLZ77()
1678 if(hashpos == hash->chain[hashpos]) break; in encodeLZ77()
1681 hashpos = hash->chainz[hashpos]; in encodeLZ77()
1682 if(hash->zeros[hashpos] != numzeros) break; in encodeLZ77()
1684 hashpos = hash->chain[hashpos]; in encodeLZ77()
1686 if(hash->val[hashpos] != (int)hashval) break; in encodeLZ77()
1702 if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/); in encodeLZ77()
1706 …hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alterati… in encodeLZ77()
1707 hash->headz[numzeros] = -1; /*idem*/ in encodeLZ77()
1708 --pos; in encodeLZ77()
1725 wpos = pos & (windowsize - 1); in encodeLZ77()
1729 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; in encodeLZ77()
1752 size_t pos = out->size; in deflateNoCompression()
1754 BFINAL = (i == numdeflateblocks - 1); in deflateNoCompression()
1758 if(datasize - datapos < 65535u) LEN = (unsigned)datasize - datapos; in deflateNoCompression()
1759 NLEN = 65535 - LEN; in deflateNoCompression()
1761 if(!ucvector_resize(out, out->size + LEN + 5)) return 83; /*alloc fail*/ in deflateNoCompression()
1764 out->data[pos + 0] = firstbyte; in deflateNoCompression()
1765 out->data[pos + 1] = (unsigned char)(LEN & 255); in deflateNoCompression()
1766 out->data[pos + 2] = (unsigned char)(LEN >> 8u); in deflateNoCompression()
1767 out->data[pos + 3] = (unsigned char)(NLEN & 255); in deflateNoCompression()
1768 out->data[pos + 4] = (unsigned char)(NLEN >> 8u); in deflateNoCompression()
1769 lodepng_memcpy(out->data + pos + 5, data + datapos, LEN); in deflateNoCompression()
1777 write the lz77-encoded data, which has lit, len and dist codes, to compressed stream using huffman …
1784 for(i = 0; i != lz77_encoded->size; ++i) { in writeLZ77data()
1785 unsigned val = lz77_encoded->data[i]; in writeLZ77data()
1786 writeBitsReversed(writer, tree_ll->codes[val], tree_ll->lengths[val]); in writeLZ77data()
1788 unsigned length_index = val - FIRST_LENGTH_CODE_INDEX; in writeLZ77data()
1790 unsigned length_extra_bits = lz77_encoded->data[++i]; in writeLZ77data()
1792 unsigned distance_code = lz77_encoded->data[++i]; in writeLZ77data()
1796 unsigned distance_extra_bits = lz77_encoded->data[++i]; in writeLZ77data()
1799 writeBitsReversed(writer, tree_d->codes[distance_code], tree_d->lengths[distance_code]); in writeLZ77data()
1817 are also run-length encoded and huffman compressed. This gives a huffman tree in deflateDynamic()
1832 size_t datasize = dataend - datapos; in deflateDynamic()
1866 if(settings->use_lz77) { in deflateDynamic()
1867 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, in deflateDynamic()
1868 settings->minmatch, settings->nicematch, settings->lazymatching); in deflateDynamic()
1872 …for(i = datapos; i < dataend; ++i) lz77_encoded.data[i - datapos] = data[i]; /*no LZ77, but still … in deflateDynamic()
1907 …/*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times… in deflateDynamic()
1908 17 (3-10 zeroes), 18 (11-138 zeroes)*/ in deflateDynamic()
1917 bitlen_lld_e[numcodes_lld_e++] = j - 3; in deflateDynamic()
1921 bitlen_lld_e[numcodes_lld_e++] = j - 11; in deflateDynamic()
1923 i += (j - 1); in deflateDynamic()
1930 bitlen_lld_e[numcodes_lld_e++] = 6 - 3; in deflateDynamic()
1934 bitlen_lld_e[numcodes_lld_e++] = rest - 3; in deflateDynamic()
1936 else j -= rest; in deflateDynamic()
1955 /*compute amount of code-length-code-lengths to output*/ in deflateDynamic()
1958 while(numcodes_cl > 4u && tree_cl.lengths[CLCL_ORDER[numcodes_cl - 1u]] == 0) { in deflateDynamic()
1959 numcodes_cl--; in deflateDynamic()
1966 - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN in deflateDynamic()
1967 - (HCLEN+4)*3 bits code lengths of code length alphabet in deflateDynamic()
1968 - HLIT + 257 code lengths of lit/length alphabet (encoded using the code length in deflateDynamic()
1970 - HDIST + 1 code lengths of distance alphabet (encoded using the code length in deflateDynamic()
1972 - compressed data in deflateDynamic()
1973 - 256 (end code) in deflateDynamic()
1984 HLIT = (unsigned)(numcodes_ll - 257); in deflateDynamic()
1985 HDIST = (unsigned)(numcodes_d - 1); in deflateDynamic()
1986 HCLEN = (unsigned)(numcodes_cl - 4); in deflateDynamic()
2011 break; /*end of error-while*/ in deflateDynamic()
2050 if(settings->use_lz77) /*LZ77 encoded*/ { in deflateFixed()
2053 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, in deflateFixed()
2054 settings->minmatch, settings->nicematch, settings->lazymatching); in deflateFixed()
2082 if(settings->btype > 2) return 61; in lodepng_deflatev()
2083 else if(settings->btype == 0) return deflateNoCompression(out, in, insize); in lodepng_deflatev()
2084 else if(settings->btype == 1) blocksize = insize; in lodepng_deflatev()
2085 else /*if(settings->btype == 2)*/ { in lodepng_deflatev()
2086 /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/ in lodepng_deflatev()
2092 numdeflateblocks = (insize + blocksize - 1) / blocksize; in lodepng_deflatev()
2095 error = hash_init(&hash, settings->windowsize); in lodepng_deflatev()
2099 unsigned final = (i == numdeflateblocks - 1); in lodepng_deflatev()
2104 … if(settings->btype == 1) error = deflateFixed(&writer, &hash, in, start, end, settings, final); in lodepng_deflatev()
2105 …else if(settings->btype == 2) error = deflateDynamic(&writer, &hash, in, start, end, settings, fin… in lodepng_deflatev()
2127 if(settings->custom_deflate) { in deflate()
2128 unsigned error = settings->custom_deflate(out, outsize, in, insize, settings); in deflate()
2150 len -= amount; in update_adler32()
2162 /*Return the adler32 of the bytes data[0..len-1]*/
2202 error = inflatev(out, in + 2, insize - 2, settings); in lodepng_zlib_decompressv()
2205 if(!settings->ignore_adler32) { in lodepng_zlib_decompressv()
2206 unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]); in lodepng_zlib_decompressv()
2207 unsigned checksum = adler32(out->data, (unsigned)(out->size)); in lodepng_zlib_decompressv()
2228 if(settings->custom_zlib) { in zlib_decompress()
2229 error = settings->custom_zlib(out, outsize, in, insize, settings); in zlib_decompress()
2234 if(settings->max_output_size && *outsize > settings->max_output_size) error = 109; in zlib_decompress()
2278 unsigned FCHECK = 31 - CMFFLG % 31; in lodepng_zlib_compress()
2284 lodepng_set32bitInt(&(*out)[*outsize - 4], ADLER32); in lodepng_zlib_compress()
2294 if(settings->custom_zlib) { in zlib_compress()
2295 unsigned error = settings->custom_zlib(out, outsize, in, insize, settings); in zlib_compress()
2310 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ in zlib_decompress()
2312 return settings->custom_zlib(out, outsize, in, insize, settings); in zlib_decompress()
2318 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ in zlib_compress()
2319 return settings->custom_zlib(out, outsize, in, insize, settings); in zlib_compress()
2334 settings->btype = 2; in lodepng_compress_settings_init()
2335 settings->use_lz77 = 1; in lodepng_compress_settings_init()
2336 settings->windowsize = DEFAULT_WINDOWSIZE; in lodepng_compress_settings_init()
2337 settings->minmatch = 3; in lodepng_compress_settings_init()
2338 settings->nicematch = 128; in lodepng_compress_settings_init()
2339 settings->lazymatching = 1; in lodepng_compress_settings_init()
2341 settings->custom_zlib = 0; in lodepng_compress_settings_init()
2342 settings->custom_deflate = 0; in lodepng_compress_settings_init()
2343 settings->custom_context = 0; in lodepng_compress_settings_init()
2354 settings->ignore_adler32 = 0; in lodepng_decompress_settings_init()
2355 settings->ignore_nlen = 0; in lodepng_decompress_settings_init()
2356 settings->max_output_size = 0; in lodepng_decompress_settings_init()
2358 settings->custom_zlib = 0; in lodepng_decompress_settings_init()
2359 settings->custom_inflate = 0; in lodepng_decompress_settings_init()
2360 settings->custom_context = 0; in lodepng_decompress_settings_init()
2417 /*Return the CRC of the bytes buf[0..len-1].*/
2434 /* The color channel bits of less-than-8-bit pixels are read with the MSB of bytes first,
2438 …unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7… in readBitFromReversedStream()
2456 …if(bit == 0) bitstream[(*bitpointer) >> 3u] &= (unsigned char)(~(1u << (7u - ((*bitpointer) & 7u)… in setBitOfReversedStream()
2457 else bitstream[(*bitpointer) >> 3u] |= (1u << (7u - ((*bitpointer) & 7u))); in setBitOfReversedStream()
2516 if(chunk >= end || end - chunk < 12) return end; /*too small to contain a chunk*/ in lodepng_chunk_next()
2532 if(chunk >= end || end - chunk < 12) return end; /*too small to contain a chunk*/ in lodepng_chunk_next_const()
2549 if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */ in lodepng_chunk_find()
2559 if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */ in lodepng_chunk_find_const()
2579 chunk_start = &(*out)[new_length - total_chunk_length]; in lodepng_chunk_append()
2593 size_t new_length = out->size; in lodepng_chunk_init()
2597 *chunk = out->data + new_length - length - 12u; in lodepng_chunk_init()
2671 info->key_defined = 0; in lodepng_color_mode_init()
2672 info->key_r = info->key_g = info->key_b = 0; in lodepng_color_mode_init()
2673 info->colortype = LCT_RGBA; in lodepng_color_mode_init()
2674 info->bitdepth = 8; in lodepng_color_mode_init()
2675 info->palette = 0; in lodepng_color_mode_init()
2676 info->palettesize = 0; in lodepng_color_mode_init()
2684 if(!info->palette) info->palette = (unsigned char*)lodepng_malloc(1024); in lodepng_color_mode_alloc_palette()
2685 if(!info->palette) return; /*alloc fail*/ in lodepng_color_mode_alloc_palette()
2690 info->palette[i * 4 + 0] = 0; in lodepng_color_mode_alloc_palette()
2691 info->palette[i * 4 + 1] = 0; in lodepng_color_mode_alloc_palette()
2692 info->palette[i * 4 + 2] = 0; in lodepng_color_mode_alloc_palette()
2693 info->palette[i * 4 + 3] = 255; in lodepng_color_mode_alloc_palette()
2704 if(source->palette) { in lodepng_color_mode_copy()
2705 dest->palette = (unsigned char*)lodepng_malloc(1024); in lodepng_color_mode_copy()
2706 if(!dest->palette && source->palettesize) return 83; /*alloc fail*/ in lodepng_color_mode_copy()
2707 lodepng_memcpy(dest->palette, source->palette, source->palettesize * 4); in lodepng_color_mode_copy()
2722 if(a->colortype != b->colortype) return 0; in lodepng_color_mode_equal()
2723 if(a->bitdepth != b->bitdepth) return 0; in lodepng_color_mode_equal()
2724 if(a->key_defined != b->key_defined) return 0; in lodepng_color_mode_equal()
2725 if(a->key_defined) { in lodepng_color_mode_equal()
2726 if(a->key_r != b->key_r) return 0; in lodepng_color_mode_equal()
2727 if(a->key_g != b->key_g) return 0; in lodepng_color_mode_equal()
2728 if(a->key_b != b->key_b) return 0; in lodepng_color_mode_equal()
2730 if(a->palettesize != b->palettesize) return 0; in lodepng_color_mode_equal()
2731 for(i = 0; i != a->palettesize * 4; ++i) { in lodepng_color_mode_equal()
2732 if(a->palette[i] != b->palette[i]) return 0; in lodepng_color_mode_equal()
2738 if(info->palette) lodepng_free(info->palette); in lodepng_palette_clear()
2739 info->palette = 0; in lodepng_palette_clear()
2740 info->palettesize = 0; in lodepng_palette_clear()
2745 if(!info->palette) /*allocate palette if empty*/ { in lodepng_palette_add()
2747 if(!info->palette) return 83; /*alloc fail*/ in lodepng_palette_add()
2749 if(info->palettesize >= 256) { in lodepng_palette_add()
2752 info->palette[4 * info->palettesize + 0] = r; in lodepng_palette_add()
2753 info->palette[4 * info->palettesize + 1] = g; in lodepng_palette_add()
2754 info->palette[4 * info->palettesize + 2] = b; in lodepng_palette_add()
2755 info->palette[4 * info->palettesize + 3] = a; in lodepng_palette_add()
2756 ++info->palettesize; in lodepng_palette_add()
2762 return lodepng_get_bpp_lct(info->colortype, info->bitdepth); in lodepng_get_bpp()
2766 return getNumColorChannels(info->colortype); in lodepng_get_channels()
2770 return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA; in lodepng_is_greyscale_type()
2774 return (info->colortype & 4) != 0; /*4 or 6*/ in lodepng_is_alpha_type()
2778 return info->colortype == LCT_PALETTE; in lodepng_is_palette_type()
2783 for(i = 0; i != info->palettesize; ++i) { in lodepng_has_palette_alpha()
2784 if(info->palette[i * 4 + 3] < 255) return 1; in lodepng_has_palette_alpha()
2790 return info->key_defined in lodepng_can_have_alpha()
2802 return lodepng_get_raw_size_lct(w, h, color->colortype, color->bitdepth); in lodepng_get_raw_size()
2822 -(size_t)w * (size_t)h * 8
2823 -amount of bytes in IDAT (including filter, padding and Adam7 bytes)
2824 -amount of bytes in raw color model
2834 …if(lodepng_mulofl(numpixels, 8, &total)) return 1; /* bit pointer with 8-bit color, or 8 bytes per… in lodepng_pixel_overflow()
2852 for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0; in LodePNGUnknownChunks_init()
2853 for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0; in LodePNGUnknownChunks_init()
2858 for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]); in LodePNGUnknownChunks_cleanup()
2868 dest->unknown_chunks_size[i] = src->unknown_chunks_size[i]; in LodePNGUnknownChunks_copy()
2869 dest->unknown_chunks_data[i] = (unsigned char*)lodepng_malloc(src->unknown_chunks_size[i]); in LodePNGUnknownChunks_copy()
2870 if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/ in LodePNGUnknownChunks_copy()
2871 for(j = 0; j < src->unknown_chunks_size[i]; ++j) { in LodePNGUnknownChunks_copy()
2872 dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j]; in LodePNGUnknownChunks_copy()
2882 info->text_num = 0; in LodePNGText_init()
2883 info->text_keys = NULL; in LodePNGText_init()
2884 info->text_strings = NULL; in LodePNGText_init()
2889 for(i = 0; i != info->text_num; ++i) { in LodePNGText_cleanup()
2890 string_cleanup(&info->text_keys[i]); in LodePNGText_cleanup()
2891 string_cleanup(&info->text_strings[i]); in LodePNGText_cleanup()
2893 lodepng_free(info->text_keys); in LodePNGText_cleanup()
2894 lodepng_free(info->text_strings); in LodePNGText_cleanup()
2899 dest->text_keys = NULL; in LodePNGText_copy()
2900 dest->text_strings = NULL; in LodePNGText_copy()
2901 dest->text_num = 0; in LodePNGText_copy()
2902 for(i = 0; i != source->text_num; ++i) { in LodePNGText_copy()
2903 CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i])); in LodePNGText_copy()
2909 …char** new_keys = (char**)(lodepng_realloc(info->text_keys, sizeof(char*) * (info->text_num + 1))); in lodepng_add_text_sized()
2910 …char** new_strings = (char**)(lodepng_realloc(info->text_strings, sizeof(char*) * (info->text_num … in lodepng_add_text_sized()
2912 if(new_keys) info->text_keys = new_keys; in lodepng_add_text_sized()
2913 if(new_strings) info->text_strings = new_strings; in lodepng_add_text_sized()
2917 ++info->text_num; in lodepng_add_text_sized()
2918 info->text_keys[info->text_num - 1] = alloc_string(key); in lodepng_add_text_sized()
2919 info->text_strings[info->text_num - 1] = alloc_string_sized(str, size); in lodepng_add_text_sized()
2920 …if(!info->text_keys[info->text_num - 1] || !info->text_strings[info->text_num - 1]) return 83; /*a… in lodepng_add_text_sized()
2936 info->itext_num = 0; in LodePNGIText_init()
2937 info->itext_keys = NULL; in LodePNGIText_init()
2938 info->itext_langtags = NULL; in LodePNGIText_init()
2939 info->itext_transkeys = NULL; in LodePNGIText_init()
2940 info->itext_strings = NULL; in LodePNGIText_init()
2945 for(i = 0; i != info->itext_num; ++i) { in LodePNGIText_cleanup()
2946 string_cleanup(&info->itext_keys[i]); in LodePNGIText_cleanup()
2947 string_cleanup(&info->itext_langtags[i]); in LodePNGIText_cleanup()
2948 string_cleanup(&info->itext_transkeys[i]); in LodePNGIText_cleanup()
2949 string_cleanup(&info->itext_strings[i]); in LodePNGIText_cleanup()
2951 lodepng_free(info->itext_keys); in LodePNGIText_cleanup()
2952 lodepng_free(info->itext_langtags); in LodePNGIText_cleanup()
2953 lodepng_free(info->itext_transkeys); in LodePNGIText_cleanup()
2954 lodepng_free(info->itext_strings); in LodePNGIText_cleanup()
2959 dest->itext_keys = NULL; in LodePNGIText_copy()
2960 dest->itext_langtags = NULL; in LodePNGIText_copy()
2961 dest->itext_transkeys = NULL; in LodePNGIText_copy()
2962 dest->itext_strings = NULL; in LodePNGIText_copy()
2963 dest->itext_num = 0; in LodePNGIText_copy()
2964 for(i = 0; i != source->itext_num; ++i) { in LodePNGIText_copy()
2965 CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i], in LodePNGIText_copy()
2966 source->itext_transkeys[i], source->itext_strings[i])); in LodePNGIText_copy()
2977 …char** new_keys = (char**)(lodepng_realloc(info->itext_keys, sizeof(char*) * (info->itext_num + 1)… in lodepng_add_itext_sized()
2978 …char** new_langtags = (char**)(lodepng_realloc(info->itext_langtags, sizeof(char*) * (info->itext_… in lodepng_add_itext_sized()
2979 …char** new_transkeys = (char**)(lodepng_realloc(info->itext_transkeys, sizeof(char*) * (info->itex… in lodepng_add_itext_sized()
2980 …char** new_strings = (char**)(lodepng_realloc(info->itext_strings, sizeof(char*) * (info->itext_nu… in lodepng_add_itext_sized()
2982 if(new_keys) info->itext_keys = new_keys; in lodepng_add_itext_sized()
2983 if(new_langtags) info->itext_langtags = new_langtags; in lodepng_add_itext_sized()
2984 if(new_transkeys) info->itext_transkeys = new_transkeys; in lodepng_add_itext_sized()
2985 if(new_strings) info->itext_strings = new_strings; in lodepng_add_itext_sized()
2989 ++info->itext_num; in lodepng_add_itext_sized()
2991 info->itext_keys[info->itext_num - 1] = alloc_string(key); in lodepng_add_itext_sized()
2992 info->itext_langtags[info->itext_num - 1] = alloc_string(langtag); in lodepng_add_itext_sized()
2993 info->itext_transkeys[info->itext_num - 1] = alloc_string(transkey); in lodepng_add_itext_sized()
2994 info->itext_strings[info->itext_num - 1] = alloc_string_sized(str, size); in lodepng_add_itext_sized()
3008 info->iccp_name = alloc_string(name); in lodepng_assign_icc()
3009 info->iccp_profile = (unsigned char*)lodepng_malloc(profile_size); in lodepng_assign_icc()
3011 if(!info->iccp_name || !info->iccp_profile) return 83; /*alloc fail*/ in lodepng_assign_icc()
3013 lodepng_memcpy(info->iccp_profile, profile, profile_size); in lodepng_assign_icc()
3014 info->iccp_profile_size = profile_size; in lodepng_assign_icc()
3020 if(info->iccp_name) lodepng_clear_icc(info); in lodepng_set_icc()
3021 info->iccp_defined = 1; in lodepng_set_icc()
3027 string_cleanup(&info->iccp_name); in lodepng_clear_icc()
3028 lodepng_free(info->iccp_profile); in lodepng_clear_icc()
3029 info->iccp_profile = NULL; in lodepng_clear_icc()
3030 info->iccp_profile_size = 0; in lodepng_clear_icc()
3031 info->iccp_defined = 0; in lodepng_clear_icc()
3036 lodepng_color_mode_init(&info->color); in lodepng_info_init()
3037 info->interlace_method = 0; in lodepng_info_init()
3038 info->compression_method = 0; in lodepng_info_init()
3039 info->filter_method = 0; in lodepng_info_init()
3041 info->background_defined = 0; in lodepng_info_init()
3042 info->background_r = info->background_g = info->background_b = 0; in lodepng_info_init()
3047 info->time_defined = 0; in lodepng_info_init()
3048 info->phys_defined = 0; in lodepng_info_init()
3050 info->gama_defined = 0; in lodepng_info_init()
3051 info->chrm_defined = 0; in lodepng_info_init()
3052 info->srgb_defined = 0; in lodepng_info_init()
3053 info->iccp_defined = 0; in lodepng_info_init()
3054 info->iccp_name = NULL; in lodepng_info_init()
3055 info->iccp_profile = NULL; in lodepng_info_init()
3062 lodepng_color_mode_cleanup(&info->color); in lodepng_info_cleanup()
3076 lodepng_color_mode_init(&dest->color); in lodepng_info_copy()
3077 CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color)); in lodepng_info_copy()
3082 if(source->iccp_defined) { in lodepng_info_copy()
3083 …CERROR_TRY_RETURN(lodepng_assign_icc(dest, source->iccp_name, source->iccp_profile, source->iccp_p… in lodepng_info_copy()
3096 unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/ in addColorBits()
3097 …/*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for secon… in addColorBits()
3098 unsigned p = index & m; in addColorBits() local
3099 in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/ in addColorBits()
3100 in = in << (bits * (m - p)); in addColorBits()
3101 if(p == 0) out[index * bits / 8u] = in; in addColorBits()
3119 lodepng_memset(tree->children, 0, 16 * sizeof(*tree->children)); in color_tree_init()
3120 tree->index = -1; in color_tree_init()
3126 if(tree->children[i]) { in color_tree_cleanup()
3127 color_tree_cleanup(tree->children[i]); in color_tree_cleanup()
3128 lodepng_free(tree->children[i]); in color_tree_cleanup()
3133 /*returns -1 if color not present, its index otherwise*/
3138 if(!tree->children[i]) return -1; in color_tree_get()
3139 else tree = tree->children[i]; in color_tree_get()
3141 return tree ? tree->index : -1; in color_tree_get()
3151 Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")
3158 if(!tree->children[i]) { in color_tree_add()
3159 tree->children[i] = (ColorTree*)lodepng_malloc(sizeof(ColorTree)); in color_tree_add()
3160 if(!tree->children[i]) return 83; /*alloc fail*/ in color_tree_add()
3161 color_tree_init(tree->children[i]); in color_tree_add()
3163 tree = tree->children[i]; in color_tree_add()
3165 tree->index = (int)index; in color_tree_add()
3173 if(mode->colortype == LCT_GREY) { in rgba8ToPixel()
3175 if(mode->bitdepth == 8) out[i] = gray; in rgba8ToPixel()
3176 else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray; in rgba8ToPixel()
3179 gray = ((unsigned)gray >> (8u - mode->bitdepth)) & ((1u << mode->bitdepth) - 1u); in rgba8ToPixel()
3180 addColorBits(out, i, mode->bitdepth, gray); in rgba8ToPixel()
3182 } else if(mode->colortype == LCT_RGB) { in rgba8ToPixel()
3183 if(mode->bitdepth == 8) { in rgba8ToPixel()
3192 } else if(mode->colortype == LCT_PALETTE) { in rgba8ToPixel()
3195 if(mode->bitdepth == 8) out[i] = index; in rgba8ToPixel()
3196 else addColorBits(out, i, mode->bitdepth, (unsigned)index); in rgba8ToPixel()
3197 } else if(mode->colortype == LCT_GREY_ALPHA) { in rgba8ToPixel()
3199 if(mode->bitdepth == 8) { in rgba8ToPixel()
3202 } else if(mode->bitdepth == 16) { in rgba8ToPixel()
3206 } else if(mode->colortype == LCT_RGBA) { in rgba8ToPixel()
3207 if(mode->bitdepth == 8) { in rgba8ToPixel()
3223 /*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/
3227 if(mode->colortype == LCT_GREY) { in rgba16ToPixel()
3231 } else if(mode->colortype == LCT_RGB) { in rgba16ToPixel()
3238 } else if(mode->colortype == LCT_GREY_ALPHA) { in rgba16ToPixel()
3244 } else if(mode->colortype == LCT_RGBA) { in rgba16ToPixel()
3261 if(mode->colortype == LCT_GREY) { in getPixelColorRGBA8()
3262 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3264 if(mode->key_defined && *r == mode->key_r) *a = 0; in getPixelColorRGBA8()
3266 } else if(mode->bitdepth == 16) { in getPixelColorRGBA8()
3268 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; in getPixelColorRGBA8()
3271 … unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ in getPixelColorRGBA8()
3272 size_t j = i * mode->bitdepth; in getPixelColorRGBA8()
3273 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorRGBA8()
3275 if(mode->key_defined && value == mode->key_r) *a = 0; in getPixelColorRGBA8()
3278 } else if(mode->colortype == LCT_RGB) { in getPixelColorRGBA8()
3279 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3281 if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0; in getPixelColorRGBA8()
3287 if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r in getPixelColorRGBA8()
3288 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g in getPixelColorRGBA8()
3289 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; in getPixelColorRGBA8()
3292 } else if(mode->colortype == LCT_PALETTE) { in getPixelColorRGBA8()
3294 if(mode->bitdepth == 8) index = in[i]; in getPixelColorRGBA8()
3296 size_t j = i * mode->bitdepth; in getPixelColorRGBA8()
3297 index = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorRGBA8()
3300 *r = mode->palette[index * 4 + 0]; in getPixelColorRGBA8()
3301 *g = mode->palette[index * 4 + 1]; in getPixelColorRGBA8()
3302 *b = mode->palette[index * 4 + 2]; in getPixelColorRGBA8()
3303 *a = mode->palette[index * 4 + 3]; in getPixelColorRGBA8()
3304 } else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorRGBA8()
3305 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3312 } else if(mode->colortype == LCT_RGBA) { in getPixelColorRGBA8()
3313 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3336 if(mode->colortype == LCT_GREY) { in getPixelColorsRGBA8()
3337 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3342 if(mode->key_defined) { in getPixelColorsRGBA8()
3343 buffer -= numpixels * num_channels; in getPixelColorsRGBA8()
3345 if(buffer[0] == mode->key_r) buffer[3] = 0; in getPixelColorsRGBA8()
3348 } else if(mode->bitdepth == 16) { in getPixelColorsRGBA8()
3351 … buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255; in getPixelColorsRGBA8()
3354 … unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ in getPixelColorsRGBA8()
3357 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGBA8()
3359 buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255; in getPixelColorsRGBA8()
3362 } else if(mode->colortype == LCT_RGB) { in getPixelColorsRGBA8()
3363 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3368 if(mode->key_defined) { in getPixelColorsRGBA8()
3369 buffer -= numpixels * num_channels; in getPixelColorsRGBA8()
3371 … if(buffer[0] == mode->key_r && buffer[1]== mode->key_g && buffer[2] == mode->key_b) buffer[3] = 0; in getPixelColorsRGBA8()
3379 buffer[3] = mode->key_defined in getPixelColorsRGBA8()
3380 && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r in getPixelColorsRGBA8()
3381 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g in getPixelColorsRGBA8()
3382 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255; in getPixelColorsRGBA8()
3385 } else if(mode->colortype == LCT_PALETTE) { in getPixelColorsRGBA8()
3386 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3390 lodepng_memcpy(buffer, &mode->palette[index * 4], 4); in getPixelColorsRGBA8()
3395 unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGBA8()
3397 lodepng_memcpy(buffer, &mode->palette[index * 4], 4); in getPixelColorsRGBA8()
3400 } else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorsRGBA8()
3401 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3412 } else if(mode->colortype == LCT_RGBA) { in getPixelColorsRGBA8()
3413 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3426 /*Similar to getPixelColorsRGBA8, but with 3-channel RGB output.*/
3432 if(mode->colortype == LCT_GREY) { in getPixelColorsRGB8()
3433 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3437 } else if(mode->bitdepth == 16) { in getPixelColorsRGB8()
3442 … unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ in getPixelColorsRGB8()
3445 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGB8()
3449 } else if(mode->colortype == LCT_RGB) { in getPixelColorsRGB8()
3450 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3459 } else if(mode->colortype == LCT_PALETTE) { in getPixelColorsRGB8()
3460 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3464 lodepng_memcpy(buffer, &mode->palette[index * 4], 3); in getPixelColorsRGB8()
3469 unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGB8()
3471 lodepng_memcpy(buffer, &mode->palette[index * 4], 3); in getPixelColorsRGB8()
3474 } else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorsRGB8()
3475 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3484 } else if(mode->colortype == LCT_RGBA) { in getPixelColorsRGB8()
3485 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3500 given color type, but the given color type must be 16-bit itself.*/
3503 if(mode->colortype == LCT_GREY) { in getPixelColorRGBA16()
3505 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; in getPixelColorRGBA16()
3507 } else if(mode->colortype == LCT_RGB) { in getPixelColorRGBA16()
3511 if(mode->key_defined in getPixelColorRGBA16()
3512 && 256u * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r in getPixelColorRGBA16()
3513 && 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g in getPixelColorRGBA16()
3514 && 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; in getPixelColorRGBA16()
3516 } else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorRGBA16()
3519 } else if(mode->colortype == LCT_RGBA) { in getPixelColorRGBA16()
3535 if(mode_in->colortype == LCT_PALETTE && !mode_in->palette) { in lodepng_convert()
3545 if(mode_out->colortype == LCT_PALETTE) { in lodepng_convert()
3546 size_t palettesize = mode_out->palettesize; in lodepng_convert()
3547 const unsigned char* palette = mode_out->palette; in lodepng_convert()
3548 size_t palsize = (size_t)1u << mode_out->bitdepth; in lodepng_convert()
3553 palettesize = mode_in->palettesize; in lodepng_convert()
3554 palette = mode_in->palette; in lodepng_convert()
3558 if(mode_in->colortype == LCT_PALETTE && mode_in->bitdepth == mode_out->bitdepth) { in lodepng_convert()
3567 const unsigned char* p = &palette[i * 4]; in lodepng_convert() local
3568 error = color_tree_add(&tree, p[0], p[1], p[2], p[3], (unsigned)i); in lodepng_convert()
3574 if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) { in lodepng_convert()
3580 } else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) { in lodepng_convert()
3582 } else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) { in lodepng_convert()
3594 if(mode_out->colortype == LCT_PALETTE) { in lodepng_convert()
3613 unsigned mul = 65535 / ((1u << mode_in->bitdepth) - 1u); /*65535, 21845, 4369, 257, 1*/ in lodepng_convert_rgb()
3614 unsigned shift = 16 - mode_out->bitdepth; in lodepng_convert_rgb()
3616 if(mode_in->colortype == LCT_GREY || mode_in->colortype == LCT_GREY_ALPHA) { in lodepng_convert_rgb()
3618 } else if(mode_in->colortype == LCT_RGB || mode_in->colortype == LCT_RGBA) { in lodepng_convert_rgb()
3622 } else if(mode_in->colortype == LCT_PALETTE) { in lodepng_convert_rgb()
3623 if(r_in >= mode_in->palettesize) return 82; in lodepng_convert_rgb()
3624 r = mode_in->palette[r_in * 4 + 0] * 257u; in lodepng_convert_rgb()
3625 g = mode_in->palette[r_in * 4 + 1] * 257u; in lodepng_convert_rgb()
3626 b = mode_in->palette[r_in * 4 + 2] * 257u; in lodepng_convert_rgb()
3632 if(mode_out->colortype == LCT_GREY || mode_out->colortype == LCT_GREY_ALPHA) { in lodepng_convert_rgb()
3634 } else if(mode_out->colortype == LCT_RGB || mode_out->colortype == LCT_RGBA) { in lodepng_convert_rgb()
3638 } else if(mode_out->colortype == LCT_PALETTE) { in lodepng_convert_rgb()
3640 /* a 16-bit color cannot be in the palette */ in lodepng_convert_rgb()
3642 for(i = 0; i < mode_out->palettesize; i++) { in lodepng_convert_rgb()
3644 if((r >> 8) == mode_out->palette[j + 0] && (g >> 8) == mode_out->palette[j + 1] && in lodepng_convert_rgb()
3645 (b >> 8) == mode_out->palette[j + 2]) { in lodepng_convert_rgb()
3662 stats->colored = 0; in lodepng_color_stats_init()
3663 stats->key = 0; in lodepng_color_stats_init()
3664 stats->key_r = stats->key_g = stats->key_b = 0; in lodepng_color_stats_init()
3665 stats->alpha = 0; in lodepng_color_stats_init()
3666 stats->numcolors = 0; in lodepng_color_stats_init()
3667 stats->bits = 1; in lodepng_color_stats_init()
3668 stats->numpixels = 0; in lodepng_color_stats_init()
3670 stats->allow_palette = 1; in lodepng_color_stats_init()
3671 stats->allow_greyscale = 1; in lodepng_color_stats_init()
3675 /*void printColorStats(LodePNGColorStats* p) {
3676 std::cout << "colored: " << (int)p->colored << ", ";
3677 std::cout << "key: " << (int)p->key << ", ";
3678 std::cout << "key_r: " << (int)p->key_r << ", ";
3679 std::cout << "key_g: " << (int)p->key_g << ", ";
3680 std::cout << "key_b: " << (int)p->key_b << ", ";
3681 std::cout << "alpha: " << (int)p->alpha << ", ";
3682 std::cout << "numcolors: " << (int)p->numcolors << ", ";
3683 std::cout << "bits: " << (int)p->bits << std::endl;
3689 /*The scaling of 2-bit and 4-bit values uses multiples of 85 and 17*/ in getValueRequiredBits()
3708 unsigned bits_done = (stats->bits == 1 && bpp == 1) ? 1 : 0; in lodepng_compute_color_stats()
3711 if(bpp <= 8) maxnumcolors = LODEPNG_MIN(257, stats->numcolors + (1u << bpp)); in lodepng_compute_color_stats()
3713 stats->numpixels += numpixels; in lodepng_compute_color_stats()
3716 if(!stats->allow_palette) numcolors_done = 1; in lodepng_compute_color_stats()
3722 if(stats->alpha) alpha_done = 1; in lodepng_compute_color_stats()
3723 if(stats->colored) colored_done = 1; in lodepng_compute_color_stats()
3724 if(stats->bits == 16) numcolors_done = 1; in lodepng_compute_color_stats()
3725 if(stats->bits >= bpp) bits_done = 1; in lodepng_compute_color_stats()
3726 if(stats->numcolors >= maxnumcolors) numcolors_done = 1; in lodepng_compute_color_stats()
3729 for(i = 0; i < stats->numcolors; i++) { in lodepng_compute_color_stats()
3730 const unsigned char* color = &stats->palette[i * 4]; in lodepng_compute_color_stats()
3736 /*Check if the 16-bit input is truly 16-bit*/ in lodepng_compute_color_stats()
3737 if(mode_in->bitdepth == 16 && !sixteen) { in lodepng_compute_color_stats()
3743 stats->bits = 16; in lodepng_compute_color_stats()
3746 numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/ in lodepng_compute_color_stats()
3759 stats->colored = 1; in lodepng_compute_color_stats()
3764 unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b); in lodepng_compute_color_stats()
3765 if(a != 65535 && (a != 0 || (stats->key && !matchkey))) { in lodepng_compute_color_stats()
3766 stats->alpha = 1; in lodepng_compute_color_stats()
3767 stats->key = 0; in lodepng_compute_color_stats()
3769 } else if(a == 0 && !stats->alpha && !stats->key) { in lodepng_compute_color_stats()
3770 stats->key = 1; in lodepng_compute_color_stats()
3771 stats->key_r = r; in lodepng_compute_color_stats()
3772 stats->key_g = g; in lodepng_compute_color_stats()
3773 stats->key_b = b; in lodepng_compute_color_stats()
3774 } else if(a == 65535 && stats->key && matchkey) { in lodepng_compute_color_stats()
3776 stats->alpha = 1; in lodepng_compute_color_stats()
3777 stats->key = 0; in lodepng_compute_color_stats()
3784 if(stats->key && !stats->alpha) { in lodepng_compute_color_stats()
3787 if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) { in lodepng_compute_color_stats()
3789 stats->alpha = 1; in lodepng_compute_color_stats()
3790 stats->key = 0; in lodepng_compute_color_stats()
3795 } else /* < 16-bit */ { in lodepng_compute_color_stats()
3800 if(!bits_done && stats->bits < 8) { in lodepng_compute_color_stats()
3803 if(bits > stats->bits) stats->bits = bits; in lodepng_compute_color_stats()
3805 bits_done = (stats->bits >= bpp); in lodepng_compute_color_stats()
3808 stats->colored = 1; in lodepng_compute_color_stats()
3810 … if(stats->bits < 8) stats->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/ in lodepng_compute_color_stats()
3814 unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b); in lodepng_compute_color_stats()
3815 if(a != 255 && (a != 0 || (stats->key && !matchkey))) { in lodepng_compute_color_stats()
3816 stats->alpha = 1; in lodepng_compute_color_stats()
3817 stats->key = 0; in lodepng_compute_color_stats()
3819 …if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per chan… in lodepng_compute_color_stats()
3820 } else if(a == 0 && !stats->alpha && !stats->key) { in lodepng_compute_color_stats()
3821 stats->key = 1; in lodepng_compute_color_stats()
3822 stats->key_r = r; in lodepng_compute_color_stats()
3823 stats->key_g = g; in lodepng_compute_color_stats()
3824 stats->key_b = b; in lodepng_compute_color_stats()
3825 } else if(a == 255 && stats->key && matchkey) { in lodepng_compute_color_stats()
3827 stats->alpha = 1; in lodepng_compute_color_stats()
3828 stats->key = 0; in lodepng_compute_color_stats()
3830 …if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per chan… in lodepng_compute_color_stats()
3836 error = color_tree_add(&tree, r, g, b, a, stats->numcolors); in lodepng_compute_color_stats()
3838 if(stats->numcolors < 256) { in lodepng_compute_color_stats()
3839 unsigned char* p = stats->palette; in lodepng_compute_color_stats() local
3840 unsigned n = stats->numcolors; in lodepng_compute_color_stats()
3841 p[n * 4 + 0] = r; in lodepng_compute_color_stats()
3842 p[n * 4 + 1] = g; in lodepng_compute_color_stats()
3843 p[n * 4 + 2] = b; in lodepng_compute_color_stats()
3844 p[n * 4 + 3] = a; in lodepng_compute_color_stats()
3846 ++stats->numcolors; in lodepng_compute_color_stats()
3847 numcolors_done = stats->numcolors >= maxnumcolors; in lodepng_compute_color_stats()
3854 if(stats->key && !stats->alpha) { in lodepng_compute_color_stats()
3857 if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) { in lodepng_compute_color_stats()
3859 stats->alpha = 1; in lodepng_compute_color_stats()
3860 stats->key = 0; in lodepng_compute_color_stats()
3862 …if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per chan… in lodepng_compute_color_stats()
3867 /*make the stats's key always 16-bit for consistency - repeat each byte twice*/ in lodepng_compute_color_stats()
3868 stats->key_r += (stats->key_r << 8); in lodepng_compute_color_stats()
3869 stats->key_g += (stats->key_g << 8); in lodepng_compute_color_stats()
3870 stats->key_b += (stats->key_b << 8); in lodepng_compute_color_stats()
3879 …olor to the color stats. The stats must already have been inited. The color must be given as 16-bit
3880 (with 2 bytes repeating for 8-bit and 65535 for opaque alpha channel). This function is expensive, …
3911 size_t numpixels = stats->numpixels; in auto_choose_color()
3914 unsigned alpha = stats->alpha; in auto_choose_color()
3915 unsigned key = stats->key; in auto_choose_color()
3916 unsigned bits = stats->bits; in auto_choose_color()
3918 mode_out->key_defined = 0; in auto_choose_color()
3923 if(bits < 8) bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ in auto_choose_color()
3926 gray_ok = !stats->colored; in auto_choose_color()
3927 if(!stats->allow_greyscale) gray_ok = 0; in auto_choose_color()
3930 n = stats->numcolors; in auto_choose_color()
3935 if(!stats->allow_palette) palette_ok = 0; in auto_choose_color()
3938 const unsigned char* p = stats->palette; in auto_choose_color() local
3940 for(i = 0; i != stats->numcolors; ++i) { in auto_choose_color()
3941 error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]); in auto_choose_color()
3945 mode_out->colortype = LCT_PALETTE; in auto_choose_color()
3946 mode_out->bitdepth = palettebits; in auto_choose_color()
3948 if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize in auto_choose_color()
3949 && mode_in->bitdepth == mode_out->bitdepth) { in auto_choose_color()
3954 } else /*8-bit or 16-bit per channel*/ { in auto_choose_color()
3955 mode_out->bitdepth = bits; in auto_choose_color()
3956 mode_out->colortype = alpha ? (gray_ok ? LCT_GREY_ALPHA : LCT_RGBA) in auto_choose_color()
3959 … unsigned mask = (1u << mode_out->bitdepth) - 1u; /*stats always uses 16-bit, mask converts it*/ in auto_choose_color()
3960 mode_out->key_r = stats->key_r & mask; in auto_choose_color()
3961 mode_out->key_g = stats->key_g & mask; in auto_choose_color()
3962 mode_out->key_b = stats->key_b & mask; in auto_choose_color()
3963 mode_out->key_defined = 1; in auto_choose_color()
3978 short pa = LODEPNG_ABS(b - c); in paethPredictor()
3979 short pb = LODEPNG_ABS(a - c); in paethPredictor()
3980 short pc = LODEPNG_ABS(a + b - c - c); in paethPredictor()
4003 w, h: width and height of non-interlaced image
4015 passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i]; in Adam7_getpassvalues()
4016 passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i]; in Adam7_getpassvalues()
4023 /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/ in Adam7_getpassvalues()
4043 LodePNGInfo* info = &state->info_png; in lodepng_inspect()
4045 CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/ in lodepng_inspect()
4048 …CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PN… in lodepng_inspect()
4058 …CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signatur… in lodepng_inspect()
4061 CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/ in lodepng_inspect()
4064 CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/ in lodepng_inspect()
4073 info->color.bitdepth = in[24]; in lodepng_inspect()
4074 info->color.colortype = (LodePNGColorType)in[25]; in lodepng_inspect()
4075 info->compression_method = in[26]; in lodepng_inspect()
4076 info->filter_method = in[27]; in lodepng_inspect()
4077 info->interlace_method = in[28]; in lodepng_inspect()
4082 if(width == 0 || height == 0) CERROR_RETURN_ERROR(state->error, 93); in lodepng_inspect()
4084 state->error = checkColorValidity(info->color.colortype, info->color.bitdepth); in lodepng_inspect()
4085 if(state->error) return state->error; in lodepng_inspect()
4087 if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32); in lodepng_inspect()
4089 if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33); in lodepng_inspect()
4091 if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34); in lodepng_inspect()
4093 if(!state->decoder.ignore_crc) { in lodepng_inspect()
4097 CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/ in lodepng_inspect()
4101 return state->error; in lodepng_inspect()
4122 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + recon[i - bytewidth]; in unfilterScanline()
4134 …for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) >… in unfilterScanline()
4137 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + (recon[i - bytewidth] >> 1u); in unfilterScanline()
4150 size_t j = i - bytewidth; in unfilterScanline()
4162 size_t j = i - bytewidth; in unfilterScanline()
4173 size_t j = i - bytewidth; in unfilterScanline()
4184 … recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth])); in unfilterScanline()
4191 /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/ in unfilterScanline()
4192 recon[i] = (scanline[i] + recon[i - bytewidth]); in unfilterScanline()
4216 size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u; in unfilter()
4234 out: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h
4286 …to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image bu… in removePaddingBits()
4291 only useful if (ilinebits - olinebits) is a value in the range 1..7 in removePaddingBits()
4294 size_t diff = ilinebits - olinebits; in removePaddingBits()
4312 …This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG'… in postProcessScanlines()
4318 unsigned bpp = lodepng_get_bpp(&info_png->color); in postProcessScanlines()
4321 if(info_png->interlace_method == 0) { in postProcessScanlines()
4354 color->palettesize = chunkLength / 3u; in readChunk_PLTE()
4355 …if(color->palettesize == 0 || color->palettesize > 256) return 38; /*error: palette too small or b… in readChunk_PLTE()
4357 if(!color->palette && color->palettesize) { in readChunk_PLTE()
4358 color->palettesize = 0; in readChunk_PLTE()
4362 for(i = 0; i != color->palettesize; ++i) { in readChunk_PLTE()
4363 color->palette[4 * i + 0] = data[pos++]; /*R*/ in readChunk_PLTE()
4364 color->palette[4 * i + 1] = data[pos++]; /*G*/ in readChunk_PLTE()
4365 color->palette[4 * i + 2] = data[pos++]; /*B*/ in readChunk_PLTE()
4366 color->palette[4 * i + 3] = 255; /*alpha*/ in readChunk_PLTE()
4374 if(color->colortype == LCT_PALETTE) { in readChunk_tRNS()
4376 if(chunkLength > color->palettesize) return 39; in readChunk_tRNS()
4378 for(i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i]; in readChunk_tRNS()
4379 } else if(color->colortype == LCT_GREY) { in readChunk_tRNS()
4383 color->key_defined = 1; in readChunk_tRNS()
4384 color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1]; in readChunk_tRNS()
4385 } else if(color->colortype == LCT_RGB) { in readChunk_tRNS()
4389 color->key_defined = 1; in readChunk_tRNS()
4390 color->key_r = 256u * data[0] + data[1]; in readChunk_tRNS()
4391 color->key_g = 256u * data[2] + data[3]; in readChunk_tRNS()
4392 color->key_b = 256u * data[4] + data[5]; in readChunk_tRNS()
4403 if(info->color.colortype == LCT_PALETTE) { in readChunk_bKGD()
4408 if(data[0] >= info->color.palettesize) return 103; in readChunk_bKGD()
4410 info->background_defined = 1; in readChunk_bKGD()
4411 info->background_r = info->background_g = info->background_b = data[0]; in readChunk_bKGD()
4412 } else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) { in readChunk_bKGD()
4417 info->background_defined = 1; in readChunk_bKGD()
4418 info->background_r = info->background_g = info->background_b = 256u * data[0] + data[1]; in readChunk_bKGD()
4419 } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) { in readChunk_bKGD()
4424 info->background_defined = 1; in readChunk_bKGD()
4425 info->background_r = 256u * data[0] + data[1]; in readChunk_bKGD()
4426 info->background_g = 256u * data[2] + data[3]; in readChunk_bKGD()
4427 info->background_b = 256u * data[4] + data[5]; in readChunk_bKGD()
4455 length = (unsigned)(chunkLength < string2_begin ? 0 : chunkLength - string2_begin); in readChunk_tEXt()
4479 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings; in readChunk_zTXt()
4502 length = (unsigned)chunkLength - string2_begin; in readChunk_zTXt()
4503 zlibsettings.max_output_size = decoder->max_text_size; in readChunk_zTXt()
4507 /*error: compressed text larger than decoder->max_text_size*/ in readChunk_zTXt()
4527 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings; in readChunk_iTXt()
4580 length = (unsigned)chunkLength < begin ? 0 : (unsigned)chunkLength - begin; in readChunk_iTXt()
4585 zlibsettings.max_output_size = decoder->max_text_size; in readChunk_iTXt()
4589 /*error: compressed text larger than decoder->max_text_size*/ in readChunk_iTXt()
4610 info->time_defined = 1; in readChunk_tIME()
4611 info->time.year = 256u * data[0] + data[1]; in readChunk_tIME()
4612 info->time.month = data[2]; in readChunk_tIME()
4613 info->time.day = data[3]; in readChunk_tIME()
4614 info->time.hour = data[4]; in readChunk_tIME()
4615 info->time.minute = data[5]; in readChunk_tIME()
4616 info->time.second = data[6]; in readChunk_tIME()
4624 info->phys_defined = 1; in readChunk_pHYs()
4625 info->phys_x = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3]; in readChunk_pHYs()
4626 info->phys_y = 16777216u * data[4] + 65536u * data[5] + 256u * data[6] + data[7]; in readChunk_pHYs()
4627 info->phys_unit = data[8]; in readChunk_pHYs()
4635 info->gama_defined = 1; in readChunk_gAMA()
4636 info->gama_gamma = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3]; in readChunk_gAMA()
4644 info->chrm_defined = 1; in readChunk_cHRM()
4645 info->chrm_white_x = 16777216u * data[ 0] + 65536u * data[ 1] + 256u * data[ 2] + data[ 3]; in readChunk_cHRM()
4646 info->chrm_white_y = 16777216u * data[ 4] + 65536u * data[ 5] + 256u * data[ 6] + data[ 7]; in readChunk_cHRM()
4647 info->chrm_red_x = 16777216u * data[ 8] + 65536u * data[ 9] + 256u * data[10] + data[11]; in readChunk_cHRM()
4648 info->chrm_red_y = 16777216u * data[12] + 65536u * data[13] + 256u * data[14] + data[15]; in readChunk_cHRM()
4649 info->chrm_green_x = 16777216u * data[16] + 65536u * data[17] + 256u * data[18] + data[19]; in readChunk_cHRM()
4650 info->chrm_green_y = 16777216u * data[20] + 65536u * data[21] + 256u * data[22] + data[23]; in readChunk_cHRM()
4651 info->chrm_blue_x = 16777216u * data[24] + 65536u * data[25] + 256u * data[26] + data[27]; in readChunk_cHRM()
4652 info->chrm_blue_y = 16777216u * data[28] + 65536u * data[29] + 256u * data[30] + data[31]; in readChunk_cHRM()
4660 info->srgb_defined = 1; in readChunk_sRGB()
4661 info->srgb_intent = data[0]; in readChunk_sRGB()
4672 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings; in readChunk_iCCP()
4676 info->iccp_defined = 1; in readChunk_iCCP()
4677 if(info->iccp_name) lodepng_clear_icc(info); in readChunk_iCCP()
4683 info->iccp_name = (char*)lodepng_malloc(length + 1); in readChunk_iCCP()
4684 if(!info->iccp_name) return 83; /*alloc fail*/ in readChunk_iCCP()
4686 info->iccp_name[length] = 0; in readChunk_iCCP()
4687 for(i = 0; i != length; ++i) info->iccp_name[i] = (char)data[i]; in readChunk_iCCP()
4694 length = (unsigned)chunkLength - string2_begin; in readChunk_iCCP()
4695 zlibsettings.max_output_size = decoder->max_icc_size; in readChunk_iCCP()
4696 error = zlib_decompress(&info->iccp_profile, &size, 0, in readChunk_iCCP()
4699 /*error: ICC profile larger than decoder->max_icc_size*/ in readChunk_iCCP()
4701 info->iccp_profile_size = size; in readChunk_iCCP()
4702 if(!error && !info->iccp_profile_size) error = 100; /*invalid ICC profile size*/ in readChunk_iCCP()
4722 error = readChunk_PLTE(&state->info_png.color, data, chunkLength); in lodepng_inspect_chunk()
4724 error = readChunk_tRNS(&state->info_png.color, data, chunkLength); in lodepng_inspect_chunk()
4727 error = readChunk_bKGD(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
4729 error = readChunk_tEXt(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
4731 error = readChunk_zTXt(&state->info_png, &state->decoder, data, chunkLength); in lodepng_inspect_chunk()
4733 error = readChunk_iTXt(&state->info_png, &state->decoder, data, chunkLength); in lodepng_inspect_chunk()
4735 error = readChunk_tIME(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
4737 error = readChunk_pHYs(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
4739 error = readChunk_gAMA(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
4741 error = readChunk_cHRM(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
4743 error = readChunk_sRGB(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
4745 error = readChunk_iCCP(&state->info_png, &state->decoder, data, chunkLength); in lodepng_inspect_chunk()
4752 if(!error && !unhandled && !state->decoder.ignore_crc) { in lodepng_inspect_chunk()
4782 …state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameter… in decodeGeneric()
4783 if(state->error) return; in decodeGeneric()
4785 if(lodepng_pixel_overflow(*w, *h, &state->info_png.color, &state->info_raw)) { in decodeGeneric()
4786 CERROR_RETURN(state->error, 92); /*overflow possible due to amount of pixels*/ in decodeGeneric()
4791 if(!idat) CERROR_RETURN(state->error, 83); /*alloc fail*/ in decodeGeneric()
4797 while(!IEND && !state->error) { in decodeGeneric()
4802 if((size_t)((chunk - in) + 12) > insize || chunk < in) { in decodeGeneric()
4803 if(state->decoder.ignore_end) break; /*other errors may still happen though*/ in decodeGeneric()
4804 CERROR_BREAK(state->error, 30); in decodeGeneric()
4811 if(state->decoder.ignore_end) break; /*other errors may still happen though*/ in decodeGeneric()
4812 CERROR_BREAK(state->error, 63); in decodeGeneric()
4815 if((size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in) { in decodeGeneric()
4816 … CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk*/ in decodeGeneric()
4826 if(lodepng_addofl(idatsize, chunkLength, &newsize)) CERROR_BREAK(state->error, 95); in decodeGeneric()
4827 if(newsize > insize) CERROR_BREAK(state->error, 95); in decodeGeneric()
4838 state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength); in decodeGeneric()
4839 if(state->error) break; in decodeGeneric()
4847 state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength); in decodeGeneric()
4848 if(state->error) break; in decodeGeneric()
4852 state->error = readChunk_bKGD(&state->info_png, data, chunkLength); in decodeGeneric()
4853 if(state->error) break; in decodeGeneric()
4856 if(state->decoder.read_text_chunks) { in decodeGeneric()
4857 state->error = readChunk_tEXt(&state->info_png, data, chunkLength); in decodeGeneric()
4858 if(state->error) break; in decodeGeneric()
4862 if(state->decoder.read_text_chunks) { in decodeGeneric()
4863 state->error = readChunk_zTXt(&state->info_png, &state->decoder, data, chunkLength); in decodeGeneric()
4864 if(state->error) break; in decodeGeneric()
4868 if(state->decoder.read_text_chunks) { in decodeGeneric()
4869 state->error = readChunk_iTXt(&state->info_png, &state->decoder, data, chunkLength); in decodeGeneric()
4870 if(state->error) break; in decodeGeneric()
4873 state->error = readChunk_tIME(&state->info_png, data, chunkLength); in decodeGeneric()
4874 if(state->error) break; in decodeGeneric()
4876 state->error = readChunk_pHYs(&state->info_png, data, chunkLength); in decodeGeneric()
4877 if(state->error) break; in decodeGeneric()
4879 state->error = readChunk_gAMA(&state->info_png, data, chunkLength); in decodeGeneric()
4880 if(state->error) break; in decodeGeneric()
4882 state->error = readChunk_cHRM(&state->info_png, data, chunkLength); in decodeGeneric()
4883 if(state->error) break; in decodeGeneric()
4885 state->error = readChunk_sRGB(&state->info_png, data, chunkLength); in decodeGeneric()
4886 if(state->error) break; in decodeGeneric()
4888 state->error = readChunk_iCCP(&state->info_png, &state->decoder, data, chunkLength); in decodeGeneric()
4889 if(state->error) break; in decodeGeneric()
4893 if(!state->decoder.ignore_critical && !lodepng_chunk_ancillary(chunk)) { in decodeGeneric()
4894 CERROR_BREAK(state->error, 69); in decodeGeneric()
4899 if(state->decoder.remember_unknown_chunks) { in decodeGeneric()
4900 state->error = lodepng_chunk_append(&state->info_png.unknown_chunks_data[critical_pos - 1], in decodeGeneric()
4901 … &state->info_png.unknown_chunks_size[critical_pos - 1], chunk); in decodeGeneric()
4902 if(state->error) break; in decodeGeneric()
4907 if(!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/ { in decodeGeneric()
4908 if(lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/ in decodeGeneric()
4914 …if(!state->error && state->info_png.color.colortype == LCT_PALETTE && !state->info_png.color.palet… in decodeGeneric()
4915 state->error = 106; /* error: PNG file must have PLTE chunk if color type is palette */ in decodeGeneric()
4918 if(!state->error) { in decodeGeneric()
4921 if(state->info_png.interlace_method == 0) { in decodeGeneric()
4922 size_t bpp = lodepng_get_bpp(&state->info_png.color); in decodeGeneric()
4925 size_t bpp = lodepng_get_bpp(&state->info_png.color); in decodeGeneric()
4926 /*Adam-7 interlaced: expected size is the sum of the 7 sub-images sizes*/ in decodeGeneric()
4937 …state->error = zlib_decompress(&scanlines, &scanlines_size, expected_size, idat, idatsize, &state- in decodeGeneric()
4939 …if(!state->error && scanlines_size != expected_size) state->error = 91; /*decompressed size doesn'… in decodeGeneric()
4942 if(!state->error) { in decodeGeneric()
4943 outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color); in decodeGeneric()
4945 if(!*out) state->error = 83; /*alloc fail*/ in decodeGeneric()
4947 if(!state->error) { in decodeGeneric()
4949 state->error = postProcessScanlines(*out, scanlines, *w, *h, &state->info_png); in decodeGeneric()
4959 if(state->error) return state->error; in lodepng_decode()
4960 …if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.co… in lodepng_decode()
4964 if(!state->decoder.color_convert) { in lodepng_decode()
4965 state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color); in lodepng_decode()
4966 if(state->error) return state->error; in lodepng_decode()
4973 from grayscale input color type, to 8-bit grayscale or grayscale with alpha"*/ in lodepng_decode()
4974 if(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA) in lodepng_decode()
4975 && !(state->info_raw.bitdepth == 8)) { in lodepng_decode()
4979 outsize = lodepng_get_raw_size(*w, *h, &state->info_raw); in lodepng_decode()
4982 state->error = 83; /*alloc fail*/ in lodepng_decode()
4984 else state->error = lodepng_convert(*out, data, &state->info_raw, in lodepng_decode()
4985 &state->info_png.color, *w, *h); in lodepng_decode()
4988 return state->error; in lodepng_decode()
5041 settings->color_convert = 1; in lodepng_decoder_settings_init()
5043 settings->read_text_chunks = 1; in lodepng_decoder_settings_init()
5044 settings->remember_unknown_chunks = 0; in lodepng_decoder_settings_init()
5045 settings->max_text_size = 16777216; in lodepng_decoder_settings_init()
5046 …settings->max_icc_size = 16777216; /* 16MB is much more than enough for any reasonable ICC profile… in lodepng_decoder_settings_init()
5048 settings->ignore_crc = 0; in lodepng_decoder_settings_init()
5049 settings->ignore_critical = 0; in lodepng_decoder_settings_init()
5050 settings->ignore_end = 0; in lodepng_decoder_settings_init()
5051 lodepng_decompress_settings_init(&settings->zlibsettings); in lodepng_decoder_settings_init()
5060 lodepng_decoder_settings_init(&state->decoder); in lodepng_state_init()
5063 lodepng_encoder_settings_init(&state->encoder); in lodepng_state_init()
5065 lodepng_color_mode_init(&state->info_raw); in lodepng_state_init()
5066 lodepng_info_init(&state->info_png); in lodepng_state_init()
5067 state->error = 1; in lodepng_state_init()
5071 lodepng_color_mode_cleanup(&state->info_raw); in lodepng_state_cleanup()
5072 lodepng_info_cleanup(&state->info_png); in lodepng_state_cleanup()
5078 lodepng_color_mode_init(&dest->info_raw); in lodepng_state_copy()
5079 lodepng_info_init(&dest->info_png); in lodepng_state_copy()
5080 dest->error = lodepng_color_mode_copy(&dest->info_raw, &source->info_raw); if(dest->error) return; in lodepng_state_copy()
5081 dest->error = lodepng_info_copy(&dest->info_png, &source->info_png); if(dest->error) return; in lodepng_state_copy()
5094 size_t pos = out->size; in writeSignature()
5097 if(!ucvector_resize(out, out->size + 8)) return 83; /*alloc fail*/ in writeSignature()
5098 lodepng_memcpy(out->data + pos, signature, 8); in writeSignature()
5125 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, info->palettesize * 3, "PLTE")); in addChunk_PLTE()
5127 for(i = 0; i != info->palettesize; ++i) { in addChunk_PLTE()
5129 chunk[j++] = info->palette[i * 4 + 0]; in addChunk_PLTE()
5130 chunk[j++] = info->palette[i * 4 + 1]; in addChunk_PLTE()
5131 chunk[j++] = info->palette[i * 4 + 2]; in addChunk_PLTE()
5141 if(info->colortype == LCT_PALETTE) { in addChunk_tRNS()
5142 size_t i, amount = info->palettesize; in addChunk_tRNS()
5144 for(i = info->palettesize; i != 0; --i) { in addChunk_tRNS()
5145 if(info->palette[4 * (i - 1) + 3] != 255) break; in addChunk_tRNS()
5146 --amount; in addChunk_tRNS()
5151 for(i = 0; i != amount; ++i) chunk[8 + i] = info->palette[4 * i + 3]; in addChunk_tRNS()
5153 } else if(info->colortype == LCT_GREY) { in addChunk_tRNS()
5154 if(info->key_defined) { in addChunk_tRNS()
5156 chunk[8] = (unsigned char)(info->key_r >> 8); in addChunk_tRNS()
5157 chunk[9] = (unsigned char)(info->key_r & 255); in addChunk_tRNS()
5159 } else if(info->colortype == LCT_RGB) { in addChunk_tRNS()
5160 if(info->key_defined) { in addChunk_tRNS()
5162 chunk[8] = (unsigned char)(info->key_r >> 8); in addChunk_tRNS()
5163 chunk[9] = (unsigned char)(info->key_r & 255); in addChunk_tRNS()
5164 chunk[10] = (unsigned char)(info->key_g >> 8); in addChunk_tRNS()
5165 chunk[11] = (unsigned char)(info->key_g & 255); in addChunk_tRNS()
5166 chunk[12] = (unsigned char)(info->key_b >> 8); in addChunk_tRNS()
5167 chunk[13] = (unsigned char)(info->key_b & 255); in addChunk_tRNS()
5282 if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) { in addChunk_bKGD()
5284 chunk[8] = (unsigned char)(info->background_r >> 8); in addChunk_bKGD()
5285 chunk[9] = (unsigned char)(info->background_r & 255); in addChunk_bKGD()
5286 } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) { in addChunk_bKGD()
5288 chunk[8] = (unsigned char)(info->background_r >> 8); in addChunk_bKGD()
5289 chunk[9] = (unsigned char)(info->background_r & 255); in addChunk_bKGD()
5290 chunk[10] = (unsigned char)(info->background_g >> 8); in addChunk_bKGD()
5291 chunk[11] = (unsigned char)(info->background_g & 255); in addChunk_bKGD()
5292 chunk[12] = (unsigned char)(info->background_b >> 8); in addChunk_bKGD()
5293 chunk[13] = (unsigned char)(info->background_b & 255); in addChunk_bKGD()
5294 } else if(info->color.colortype == LCT_PALETTE) { in addChunk_bKGD()
5296 chunk[8] = (unsigned char)(info->background_r & 255); /*palette index*/ in addChunk_bKGD()
5305 chunk[8] = (unsigned char)(time->year >> 8); in addChunk_tIME()
5306 chunk[9] = (unsigned char)(time->year & 255); in addChunk_tIME()
5307 chunk[10] = (unsigned char)time->month; in addChunk_tIME()
5308 chunk[11] = (unsigned char)time->day; in addChunk_tIME()
5309 chunk[12] = (unsigned char)time->hour; in addChunk_tIME()
5310 chunk[13] = (unsigned char)time->minute; in addChunk_tIME()
5311 chunk[14] = (unsigned char)time->second; in addChunk_tIME()
5319 lodepng_set32bitInt(chunk + 8, info->phys_x); in addChunk_pHYs()
5320 lodepng_set32bitInt(chunk + 12, info->phys_y); in addChunk_pHYs()
5321 chunk[16] = info->phys_unit; in addChunk_pHYs()
5329 lodepng_set32bitInt(chunk + 8, info->gama_gamma); in addChunk_gAMA()
5337 lodepng_set32bitInt(chunk + 8, info->chrm_white_x); in addChunk_cHRM()
5338 lodepng_set32bitInt(chunk + 12, info->chrm_white_y); in addChunk_cHRM()
5339 lodepng_set32bitInt(chunk + 16, info->chrm_red_x); in addChunk_cHRM()
5340 lodepng_set32bitInt(chunk + 20, info->chrm_red_y); in addChunk_cHRM()
5341 lodepng_set32bitInt(chunk + 24, info->chrm_green_x); in addChunk_cHRM()
5342 lodepng_set32bitInt(chunk + 28, info->chrm_green_y); in addChunk_cHRM()
5343 lodepng_set32bitInt(chunk + 32, info->chrm_blue_x); in addChunk_cHRM()
5344 lodepng_set32bitInt(chunk + 36, info->chrm_blue_y); in addChunk_cHRM()
5350 unsigned char data = info->srgb_intent; in addChunk_sRGB()
5359 size_t keysize = lodepng_strlen(info->iccp_name); in addChunk_iCCP()
5363 info->iccp_profile, info->iccp_profile_size, zlibsettings); in addChunk_iCCP()
5369 lodepng_memcpy(chunk + 8, info->iccp_name, keysize); in addChunk_iCCP()
5391 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth]; in filterScanline()
5395 for(i = 0; i != length; ++i) out[i] = scanline[i] - prevline[i]; in filterScanline()
5402 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - (prevline[i] >> 1); in filterScanline()
5403 …for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]… in filterScanline()
5406 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - (scanline[i - bytewidth] >> 1); in filterScanline()
5412 for(i = 0; i != bytewidth; ++i) out[i] = (scanline[i] - prevline[i]); in filterScanline()
5414 …out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidt… in filterScanline()
5418 /*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/ in filterScanline()
5419 for(i = bytewidth; i < length; ++i) out[i] = (scanline[i] - scanline[i - bytewidth]); in filterScanline()
5442 /* approximate i*log2(i): l is integer logarithm, ((i - (1u << l)) << 1u) in ilog2i()
5444 return i * l + ((i - (1u << l)) << 1u); in ilog2i()
5457 size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u; in filter()
5464 LodePNGFilterStrategy strategy = settings->filter_strategy; in filter()
5479 if(settings->filter_palette_zero && in filter()
5480 (color->colortype == LCT_PALETTE || color->bitdepth < 8)) strategy = LFS_ZERO; in filter()
5520 sum += s < 128 ? s : (255U - s); in filter()
5583 unsigned char type = settings->predefined_filters[y]; in filter()
5598 lodepng_memcpy(&zlibsettings, &settings->zlibsettings, sizeof(LodePNGCompressSettings)); in filter()
5646 size_t diff = olinebits - ilinebits; in addPaddingBits()
5654 /*obp += diff; --> no, fill in some value in the padding bits too, to avoid in addPaddingBits()
5661 in: non-interlaced image with size w*h
5662 out: the same pixels, but re-ordered according to PNG's Adam7 interlacing, with
5716 …This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced… in preProcessScanlines()
5720 unsigned bpp = lodepng_get_bpp(&info_png->color); in preProcessScanlines()
5723 if(info_png->interlace_method == 0) { in preProcessScanlines()
5735 error = filter(*out, padded, w, h, &info_png->color, settings); in preProcessScanlines()
5740 error = filter(*out, in, w, h, &info_png->color, settings); in preProcessScanlines()
5763 …unsigned char* padded = (unsigned char*)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[… in preProcessScanlines()
5768 passw[i], passh[i], &info_png->color, settings); in preProcessScanlines()
5772 passw[i], passh[i], &info_png->color, settings); in preProcessScanlines()
5788 while((size_t)(inchunk - data) < datasize) { in addUnknownChunks()
5789 CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk)); in addUnknownChunks()
5790 out->allocsize = out->size; /*fix the allocsize again*/ in addUnknownChunks()
5798 It is a gray profile if bytes 16-19 are "GRAY", rgb profile if bytes 16-19 in isGrayICCProfile()
5802 requires using a non-gray color model if there is an ICC profile with "RGB " in isGrayICCProfile()
5824 const LodePNGInfo* info_png = &state->info_png; in lodepng_encode()
5831 state->error = 0; in lodepng_encode()
5834 if((info_png->color.colortype == LCT_PALETTE || state->encoder.force_palette) in lodepng_encode()
5835 && (info_png->color.palettesize == 0 || info_png->color.palettesize > 256)) { in lodepng_encode()
5836 state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ in lodepng_encode()
5839 if(state->encoder.zlibsettings.btype > 2) { in lodepng_encode()
5840 state->error = 61; /*error: invalid btype*/ in lodepng_encode()
5843 if(info_png->interlace_method > 1) { in lodepng_encode()
5844 state->error = 71; /*error: invalid interlace mode*/ in lodepng_encode()
5847 state->error = checkColorValidity(info_png->color.colortype, info_png->color.bitdepth); in lodepng_encode()
5848 if(state->error) goto cleanup; /*error: invalid color type given*/ in lodepng_encode()
5849 state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); in lodepng_encode()
5850 if(state->error) goto cleanup; /*error: invalid color type given*/ in lodepng_encode()
5853 lodepng_info_copy(&info, &state->info_png); in lodepng_encode()
5854 if(state->encoder.auto_convert) { in lodepng_encode()
5858 if(info_png->iccp_defined && in lodepng_encode()
5859 isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { in lodepng_encode()
5864 if(info_png->iccp_defined && in lodepng_encode()
5865 isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { in lodepng_encode()
5870 state->error = lodepng_compute_color_stats(&stats, image, w, h, &state->info_raw); in lodepng_encode()
5871 if(state->error) goto cleanup; in lodepng_encode()
5873 if(info_png->background_defined) { in lodepng_encode()
5877 …ng_convert_rgb(&r, &g, &b, info_png->background_r, info_png->background_g, info_png->background_b,… in lodepng_encode()
5878 state->error = lodepng_color_stats_add(&stats, r, g, b, 65535); in lodepng_encode()
5879 if(state->error) goto cleanup; in lodepng_encode()
5882 state->error = auto_choose_color(&info.color, &state->info_raw, &stats); in lodepng_encode()
5883 if(state->error) goto cleanup; in lodepng_encode()
5886 if(info_png->background_defined) { in lodepng_encode()
5888 …info_png->background_r, info_png->background_g, info_png->background_b, &info.color, &info_png->co… in lodepng_encode()
5889 state->error = 104; in lodepng_encode()
5896 if(info_png->iccp_defined) { in lodepng_encode()
5897 unsigned gray_icc = isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size); in lodepng_encode()
5898 unsigned rgb_icc = isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size); in lodepng_encode()
5901 state->error = 100; /* Disallowed profile color type for PNG */ in lodepng_encode()
5907 state->error = state->encoder.auto_convert ? 102 : 101; in lodepng_encode()
5912 if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) { in lodepng_encode()
5917 if(!converted && size) state->error = 83; /*alloc fail*/ in lodepng_encode()
5918 if(!state->error) { in lodepng_encode()
5919 state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h); in lodepng_encode()
5921 if(!state->error) { in lodepng_encode()
5922 state->error = preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); in lodepng_encode()
5925 if(state->error) goto cleanup; in lodepng_encode()
5927 state->error = preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder); in lodepng_encode()
5928 if(state->error) goto cleanup; in lodepng_encode()
5936 state->error = writeSignature(&outv); in lodepng_encode()
5937 if(state->error) goto cleanup; in lodepng_encode()
5939 …state->error = addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlac… in lodepng_encode()
5940 if(state->error) goto cleanup; in lodepng_encode()
5944 … state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]); in lodepng_encode()
5945 if(state->error) goto cleanup; in lodepng_encode()
5949 state->error = addChunk_iCCP(&outv, &info, &state->encoder.zlibsettings); in lodepng_encode()
5950 if(state->error) goto cleanup; in lodepng_encode()
5953 state->error = addChunk_sRGB(&outv, &info); in lodepng_encode()
5954 if(state->error) goto cleanup; in lodepng_encode()
5957 state->error = addChunk_gAMA(&outv, &info); in lodepng_encode()
5958 if(state->error) goto cleanup; in lodepng_encode()
5961 state->error = addChunk_cHRM(&outv, &info); in lodepng_encode()
5962 if(state->error) goto cleanup; in lodepng_encode()
5967 state->error = addChunk_PLTE(&outv, &info.color); in lodepng_encode()
5968 if(state->error) goto cleanup; in lodepng_encode()
5970 …if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT… in lodepng_encode()
5972 state->error = addChunk_PLTE(&outv, &info.color); in lodepng_encode()
5973 if(state->error) goto cleanup; in lodepng_encode()
5976 state->error = addChunk_tRNS(&outv, &info.color); in lodepng_encode()
5977 if(state->error) goto cleanup; in lodepng_encode()
5981 state->error = addChunk_bKGD(&outv, &info); in lodepng_encode()
5982 if(state->error) goto cleanup; in lodepng_encode()
5986 state->error = addChunk_pHYs(&outv, &info); in lodepng_encode()
5987 if(state->error) goto cleanup; in lodepng_encode()
5992 … state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]); in lodepng_encode()
5993 if(state->error) goto cleanup; in lodepng_encode()
5997 state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings); in lodepng_encode()
5998 if(state->error) goto cleanup; in lodepng_encode()
6002 state->error = addChunk_tIME(&outv, &info.time); in lodepng_encode()
6003 if(state->error) goto cleanup; in lodepng_encode()
6008 state->error = 66; /*text chunk too large*/ in lodepng_encode()
6012 state->error = 67; /*text chunk too small*/ in lodepng_encode()
6015 if(state->encoder.text_compression) { in lodepng_encode()
6016 …state->error = addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibs… in lodepng_encode()
6017 if(state->error) goto cleanup; in lodepng_encode()
6019 state->error = addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]); in lodepng_encode()
6020 if(state->error) goto cleanup; in lodepng_encode()
6024 if(state->encoder.add_id) { in lodepng_encode()
6030 k[4] == 'P' && k[5] == 'N' && k[6] == 'G' && k[7] == '\0') { in lodepng_encode()
6036 …state->error = addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt tha… in lodepng_encode()
6037 if(state->error) goto cleanup; in lodepng_encode()
6043 state->error = 66; /*text chunk too large*/ in lodepng_encode()
6047 state->error = 67; /*text chunk too small*/ in lodepng_encode()
6050 state->error = addChunk_iTXt( in lodepng_encode()
6051 &outv, state->encoder.text_compression, in lodepng_encode()
6053 &state->encoder.zlibsettings); in lodepng_encode()
6054 if(state->error) goto cleanup; in lodepng_encode()
6059 … state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]); in lodepng_encode()
6060 if(state->error) goto cleanup; in lodepng_encode()
6063 state->error = addChunk_IEND(&outv); in lodepng_encode()
6064 if(state->error) goto cleanup; in lodepng_encode()
6075 return state->error; in lodepng_encode()
6122 lodepng_compress_settings_init(&settings->zlibsettings); in lodepng_encoder_settings_init()
6123 settings->filter_palette_zero = 1; in lodepng_encoder_settings_init()
6124 settings->filter_strategy = LFS_MINSUM; in lodepng_encoder_settings_init()
6125 settings->auto_convert = 1; in lodepng_encoder_settings_init()
6126 settings->force_palette = 0; in lodepng_encoder_settings_init()
6127 settings->predefined_filters = 0; in lodepng_encoder_settings_init()
6129 settings->add_id = 0; in lodepng_encoder_settings_init()
6130 settings->text_compression = 1; in lodepng_encoder_settings_init()
6188 case 48: return "empty input buffer given to decoder. Maybe caused by non-existing file?"; in lodepng_error_text()
6203 case 60: return "invalid window size given in the settings of the encoder (must be 0-32768)"; in lodepng_error_text()
6207 /*(2^31-1)*/ in lodepng_error_text()
6233 case 89: return "text chunk keyword too short or long: must have size 1-79"; in lodepng_error_text()