Lines Matching +full:- +full:r

4 Copyright (c) 2005-2023 Lode Vandevenne
6 This software is provided 'as-is', without any express or implied
35 #define image_cache_draw_buf_handlers &(LV_GLOBAL_DEFAULT()->image_cache_draw_buf_handlers)
47 …#pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requ…
56 -Tools for C and common code for PNG and Zlib
57 -C Code for Zlib (huffman, deflate, ...)
58 -C Code for PNG (file format chunks, adam7, PNG filters, color conversions, ...)
59 -The C++ wrapper around all of the above
71 -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler, or comment out
146 return (size_t)(a - orig); in lodepng_strlen()
218 -All of them wrap dynamic arrays or text strings in a similar way.
219 -LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the …
220 -The string tools are made to avoid problems with compilers that declare things like strncat as dep…
221 -They're not used in the interface, only internally in this file as static functions.
222 -As with many other structs in this file, the init and cleanup functions serve as ctor and dtor.
236 ((uivector *)p)->size = ((uivector *)p)->allocsize = 0; in uivector_cleanup()
237 lodepng_free(((uivector *)p)->data); in uivector_cleanup()
238 ((uivector *)p)->data = NULL; in uivector_cleanup()
245 if(allocsize > p->allocsize) { in uivector_resize()
246 size_t newsize = allocsize + (p->allocsize >> 1u); in uivector_resize()
247 void * data = lodepng_realloc(p->data, newsize); in uivector_resize()
249 p->allocsize = newsize; in uivector_resize()
250 p->data = (unsigned *)data; in uivector_resize()
254 p->size = size; in uivector_resize()
260 p->data = NULL; in uivector_init()
261 p->size = p->allocsize = 0; in uivector_init()
267 if(!uivector_resize(p, p->size + 1)) return 0; in uivector_push_back()
268 p->data[p->size - 1] = c; in uivector_push_back()
286 if(size > p->allocsize) { in ucvector_reserve()
287 size_t newsize = size + (p->allocsize >> 1u); in ucvector_reserve()
288 void * data = lodepng_realloc(p->data, newsize); in ucvector_reserve()
290 p->allocsize = newsize; in ucvector_reserve()
291 p->data = (unsigned char *)data; in ucvector_reserve()
301 p->size = size; in ucvector_resize()
376 if(res != LV_FS_RES_OK) return -1; in lodepng_filesize()
380 return -1; in lodepng_filesize()
448 writer->data = data; in LodePNGBitWriter_init()
449 writer->bp = 0; in LodePNGBitWriter_init()
455 if(((writer->bp) & 7u) == 0) {\
456 if(!ucvector_resize(writer->data, writer->data->size + 1)) return;\
457 writer->data->data[writer->data->size - 1] = 0;\
459 (writer->data->data[writer->data->size - 1]) |= (bit << ((writer->bp) & 7u));\
460 ++writer->bp;\
484 WRITEBIT(writer, (unsigned char)((value >> (nbits - 1u - i)) & 1u)); in writeBitsReversed()
503 reader->data = data; in LodePNGBitReader_init()
504 reader->size = size; in LodePNGBitReader_init()
506 if(lodepng_mulofl(size, 8u, &reader->bitsize)) return 105; in LodePNGBitReader_init()
509 if(lodepng_addofl(reader->bitsize, 64u, &temp)) return 105; in LodePNGBitReader_init()
510 reader->bp = 0; in LodePNGBitReader_init()
511 reader->buffer = 0; in LodePNGBitReader_init()
526 size_t start = reader->bp >> 3u; in ensureBits9()
527 size_t size = reader->size; in ensureBits9()
529 … reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u); in ensureBits9()
530 reader->buffer >>= (reader->bp & 7u); in ensureBits9()
533 reader->buffer = 0; in ensureBits9()
534 if(start + 0u < size) reader->buffer = reader->data[start + 0]; in ensureBits9()
535 reader->buffer >>= (reader->bp & 7u); in ensureBits9()
543 size_t start = reader->bp >> 3u; in ensureBits17()
544 size_t size = reader->size; in ensureBits17()
546 … reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | in ensureBits17()
547 ((unsigned)reader->data[start + 2] << 16u); in ensureBits17()
548 reader->buffer >>= (reader->bp & 7u); in ensureBits17()
551 reader->buffer = 0; in ensureBits17()
552 if(start + 0u < size) reader->buffer |= reader->data[start + 0]; in ensureBits17()
553 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); in ensureBits17()
554 reader->buffer >>= (reader->bp & 7u); in ensureBits17()
562 size_t start = reader->bp >> 3u; in ensureBits25()
563 size_t size = reader->size; in ensureBits25()
565 … reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | in ensureBits25()
566 … ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u); in ensureBits25()
567 reader->buffer >>= (reader->bp & 7u); in ensureBits25()
570 reader->buffer = 0; in ensureBits25()
571 if(start + 0u < size) reader->buffer |= reader->data[start + 0]; in ensureBits25()
572 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); in ensureBits25()
573 if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u); in ensureBits25()
574 reader->buffer >>= (reader->bp & 7u); in ensureBits25()
582 size_t start = reader->bp >> 3u; in ensureBits32()
583 size_t size = reader->size; in ensureBits32()
585 … reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | in ensureBits32()
586 … ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u); in ensureBits32()
587 reader->buffer >>= (reader->bp & 7u); in ensureBits32()
588 reader->buffer |= (((unsigned)reader->data[start + 4] << 24u) << (8u - (reader->bp & 7u))); in ensureBits32()
591 reader->buffer = 0; in ensureBits32()
592 if(start + 0u < size) reader->buffer |= reader->data[start + 0]; in ensureBits32()
593 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u); in ensureBits32()
594 if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u); in ensureBits32()
595 if(start + 3u < size) reader->buffer |= ((unsigned)reader->data[start + 3] << 24u); in ensureBits32()
596 reader->buffer >>= (reader->bp & 7u); in ensureBits32()
605 return reader->buffer & ((1u << nbits) - 1u); in peekBits()
611 reader->buffer >>= nbits; in advanceBits()
612 reader->bp += nbits; in advanceBits()
628 for(i = 0; i < num; i++) result |= ((bits >> (num - i - 1u)) & 1u) << i; in reverseBits()
633 /* / Deflate - Huffman / */
642 /*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-13…
645 /*the base lengths represented by codes 257-285*/
651 /*the extra bits used by codes 257-285 (added to base length)*/
691 tree->codes = 0; in HuffmanTree_init()
692 tree->lengths = 0; in HuffmanTree_init()
693 tree->table_len = 0; in HuffmanTree_init()
694 tree->table_value = 0; in HuffmanTree_init()
699 lodepng_free(tree->codes); in HuffmanTree_cleanup()
700 lodepng_free(tree->lengths); in HuffmanTree_cleanup()
701 lodepng_free(tree->table_len); in HuffmanTree_cleanup()
702 lodepng_free(tree->table_value); in HuffmanTree_cleanup()
717 static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u; in HuffmanTree_makeTable()
724 for(i = 0; i < tree->numcodes; i++) { in HuffmanTree_makeTable()
725 unsigned symbol = tree->codes[i]; in HuffmanTree_makeTable()
726 unsigned l = tree->lengths[i]; in HuffmanTree_makeTable()
730 index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS); in HuffmanTree_makeTable()
737 if(l > FIRSTBITS) size += (((size_t)1) << (l - FIRSTBITS)); in HuffmanTree_makeTable()
739 tree->table_len = (unsigned char *)lodepng_malloc(size * sizeof(*tree->table_len)); in HuffmanTree_makeTable()
740 tree->table_value = (unsigned short *)lodepng_malloc(size * sizeof(*tree->table_value)); in HuffmanTree_makeTable()
741 if(!tree->table_len || !tree->table_value) { in HuffmanTree_makeTable()
743 /* freeing tree->table values is done at a higher scope */ in HuffmanTree_makeTable()
747 for(i = 0; i < size; ++i) tree->table_len[i] = 16; in HuffmanTree_makeTable()
754 tree->table_len[i] = l; in HuffmanTree_makeTable()
755 tree->table_value[i] = (unsigned short)pointer; in HuffmanTree_makeTable()
756 pointer += (((size_t)1) << (l - FIRSTBITS)); in HuffmanTree_makeTable()
762 for(i = 0; i < tree->numcodes; ++i) { in HuffmanTree_makeTable()
763 unsigned l = tree->lengths[i]; in HuffmanTree_makeTable()
766 symbol = tree->codes[i]; /*the huffman bit pattern. i itself is the value.*/ in HuffmanTree_makeTable()
773 unsigned num = 1u << (FIRSTBITS - l); in HuffmanTree_makeTable()
776 …/*bit reader will read the l bits of symbol first, the remaining FIRSTBITS - l bits go to the MSB'… in HuffmanTree_makeTable()
778 …if(tree->table_len[index] != 16) return 55; /*invalid tree: long symbol shares prefix with short s… in HuffmanTree_makeTable()
779 tree->table_len[index] = l; in HuffmanTree_makeTable()
780 tree->table_value[index] = (unsigned short)i; in HuffmanTree_makeTable()
787 unsigned maxlen = tree->table_len[index]; in HuffmanTree_makeTable()
788 /*log2 of secondary table length, should be >= l - FIRSTBITS*/ in HuffmanTree_makeTable()
789 unsigned tablelen = maxlen - FIRSTBITS; in HuffmanTree_makeTable()
790 unsigned start = tree->table_value[index]; /*starting index in secondary table*/ in HuffmanTree_makeTable()
791 …unsigned num = 1u << (tablelen - (l - FIRSTBITS)); /*amount of entries of this symbol in secondary… in HuffmanTree_makeTable()
795 unsigned reverse2 = reverse >> FIRSTBITS; /* l - FIRSTBITS bits */ in HuffmanTree_makeTable()
796 unsigned index2 = start + (reverse2 | (j << (l - FIRSTBITS))); in HuffmanTree_makeTable()
797 tree->table_len[index2] = l; in HuffmanTree_makeTable()
798 tree->table_value[index2] = (unsigned short)i; in HuffmanTree_makeTable()
811 if(tree->table_len[i] == 16) { in HuffmanTree_makeTable()
815 tree->table_len[i] = (i < headsize) ? 1 : (FIRSTBITS + 1); in HuffmanTree_makeTable()
816 tree->table_value[i] = INVALIDSYMBOL; in HuffmanTree_makeTable()
821 /* A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes. in HuffmanTree_makeTable()
826 if(tree->table_len[i] == 16) return 55; in HuffmanTree_makeTable()
845 tree->codes = (unsigned *)lodepng_malloc(tree->numcodes * sizeof(unsigned)); in HuffmanTree_makeFromLengths2()
846 blcount = (unsigned *)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned)); in HuffmanTree_makeFromLengths2()
847 nextcode = (unsigned *)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned)); in HuffmanTree_makeFromLengths2()
848 if(!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/ in HuffmanTree_makeFromLengths2()
851 for(n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0; in HuffmanTree_makeFromLengths2()
853 for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]]; in HuffmanTree_makeFromLengths2()
855 for(bits = 1; bits <= tree->maxbitlen; ++bits) { in HuffmanTree_makeFromLengths2()
856 nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u; in HuffmanTree_makeFromLengths2()
859 for(n = 0; n != tree->numcodes; ++n) { in HuffmanTree_makeFromLengths2()
860 if(tree->lengths[n] != 0) { in HuffmanTree_makeFromLengths2()
861 tree->codes[n] = nextcode[tree->lengths[n]]++; in HuffmanTree_makeFromLengths2()
863 tree->codes[n] &= ((1u << tree->lengths[n]) - 1u); in HuffmanTree_makeFromLengths2()
884 tree->lengths = (unsigned *)lodepng_malloc(numcodes * sizeof(unsigned)); in HuffmanTree_makeFromLengths()
885 if(!tree->lengths) return 83; /*alloc fail*/ in HuffmanTree_makeFromLengths()
886 for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i]; in HuffmanTree_makeFromLengths()
887 tree->numcodes = (unsigned)numcodes; /*number of symbols*/ in HuffmanTree_makeFromLengths()
888 tree->maxbitlen = maxbitlen; in HuffmanTree_makeFromLengths()
894 /*BPM: Boundary Package Merge, see "A Fast and Space-Economical Algorithm for Length-Limited Coding…
926 if(lists->nextfree >= lists->numfree) { in bpmnode_create()
928 for(i = 0; i != lists->memsize; ++i) lists->memory[i].in_use = 0; in bpmnode_create()
929 for(i = 0; i != lists->listsize; ++i) { in bpmnode_create()
931 for(node = lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1; in bpmnode_create()
932 for(node = lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1; in bpmnode_create()
935 lists->numfree = 0; in bpmnode_create()
936 for(i = 0; i != lists->memsize; ++i) { in bpmnode_create()
937 if(!lists->memory[i].in_use) lists->freelist[lists->numfree++] = &lists->memory[i]; in bpmnode_create()
939 lists->nextfree = 0; in bpmnode_create()
942 result = lists->freelist[lists->nextfree++]; in bpmnode_create()
943 result->weight = weight; in bpmnode_create()
944 result->index = index; in bpmnode_create()
945 result->tail = tail; in bpmnode_create()
960 size_t r = (p + 2 * width > num) ? num : (p + 2 * width); in bpmnode_sort() local
962 for(k = p; k < r; k++) { in bpmnode_sort()
963 if(i < q && (j >= r || a[i].weight <= a[j].weight)) b[k] = a[i++]; in bpmnode_sort()
976 unsigned lastindex = lists->chains1[c]->index; in boundaryPM()
980 lists->chains0[c] = lists->chains1[c]; in boundaryPM()
981 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, 0); in boundaryPM()
985 int sum = lists->chains0[c - 1]->weight + lists->chains1[c - 1]->weight; in boundaryPM()
986 lists->chains0[c] = lists->chains1[c]; in boundaryPM()
988 …lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, lists->chains1[… in boundaryPM()
991 lists->chains1[c] = bpmnode_create(lists, sum, lastindex, lists->chains1[c - 1]); in boundaryPM()
994 if(num + 1 < (int)(2 * numpresent - 2)) { in boundaryPM()
995 boundaryPM(lists, leaves, numpresent, c - 1, num); in boundaryPM()
996 boundaryPM(lists, leaves, numpresent, c - 1, num); in boundaryPM()
1006 size_t numpresent = 0; /*number of symbols with non-zero frequency*/ in lodepng_huffman_code_lengths()
1028 Package-Merge code below also doesn't work correctly if there's only one in lodepng_huffman_code_lengths()
1064 … /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/ in lodepng_huffman_code_lengths()
1065 …for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1… in lodepng_huffman_code_lengths()
1067 for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail) { in lodepng_huffman_code_lengths()
1068 for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index]; in lodepng_huffman_code_lengths()
1087 while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/ in HuffmanTree_makeFromFrequencies()
1088 tree->lengths = (unsigned *)lodepng_malloc(numcodes * sizeof(unsigned)); in HuffmanTree_makeFromFrequencies()
1089 if(!tree->lengths) return 83; /*alloc fail*/ in HuffmanTree_makeFromFrequencies()
1090 tree->maxbitlen = maxbitlen; in HuffmanTree_makeFromFrequencies()
1091 tree->numcodes = (unsigned)numcodes; /*number of symbols*/ in HuffmanTree_makeFromFrequencies()
1093 error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen); in HuffmanTree_makeFromFrequencies()
1106 /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/ in generateFixedLitLenTree()
1125 /*there are 32 distance codes, but 30-31 are unused*/ in generateFixedDistanceTree()
1141 unsigned short l = codetree->table_len[code]; in huffmanDecodeSymbol()
1142 unsigned short value = codetree->table_value[code]; in huffmanDecodeSymbol()
1149 value += peekBits(reader, l - FIRSTBITS); in huffmanDecodeSymbol()
1150 advanceBits(reader, codetree->table_len[value] - FIRSTBITS); in huffmanDecodeSymbol()
1151 return codetree->table_value[value]; in huffmanDecodeSymbol()
1186 …if(reader->bitsize - reader->bp < 14) return 49; /*error: the bit pointer is or will go past the m… in getTreeInflateDynamic()
1203 if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) { in getTreeInflateDynamic()
1232 else bitlen_d[i - HLIT] = code; in getTreeInflateDynamic()
1236 unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/ in getTreeInflateDynamic()
1243 if(i < HLIT + 1) value = bitlen_ll[i - 1]; in getTreeInflateDynamic()
1244 else value = bitlen_d[i - HLIT - 1]; in getTreeInflateDynamic()
1249 else bitlen_d[i - HLIT] = value; in getTreeInflateDynamic()
1253 else if(code == 17) { /*repeat "0" 3-10 times*/ in getTreeInflateDynamic()
1262 else bitlen_d[i - HLIT] = 0; in getTreeInflateDynamic()
1266 else if(code == 18) { /*repeat "0" 11-138 times*/ in getTreeInflateDynamic()
1275 else bitlen_d[i - HLIT] = 0; in getTreeInflateDynamic()
1283 if(reader->bp > reader->bitsize) { in getTreeInflateDynamic()
1299 break; /*end of error-while*/ in getTreeInflateDynamic()
1321 if(!ucvector_reserve(out, out->size + reserved_size)) return 83; /*alloc fail*/ in inflateHuffmanBlock()
1339 out->data[out->size++] = (unsigned char)code_ll; in inflateHuffmanBlock()
1343 out->data[out->size++] = (unsigned char)code_ll; in inflateHuffmanBlock()
1351 length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX]; in inflateHuffmanBlock()
1354 numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX]; in inflateHuffmanBlock()
1366 ERROR_BREAK(18); /*error: invalid distance code (30-31 are never used)*/ in inflateHuffmanBlock()
1382 start = out->size; in inflateHuffmanBlock()
1384 backward = start - distance; in inflateHuffmanBlock()
1386 out->size += length; in inflateHuffmanBlock()
1389 lodepng_memcpy(out->data + start, out->data + backward, distance); in inflateHuffmanBlock()
1392 out->data[start++] = out->data[backward++]; in inflateHuffmanBlock()
1396 lodepng_memcpy(out->data + start, out->data + backward, length); in inflateHuffmanBlock()
1405 if(out->allocsize - out->size < reserved_size) { in inflateHuffmanBlock()
1406 if(!ucvector_reserve(out, out->size + reserved_size)) ERROR_BREAK(83); /*alloc fail*/ in inflateHuffmanBlock()
1409 if(reader->bp > reader->bitsize) { in inflateHuffmanBlock()
1415 if(max_output_size && out->size > max_output_size) { in inflateHuffmanBlock()
1430 size_t size = reader->size; in inflateNoCompression()
1434 bytepos = (reader->bp + 7u) >> 3u; in inflateNoCompression()
1438 LEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); in inflateNoCompression()
1440 NLEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); in inflateNoCompression()
1443 /*check if 16-bit NLEN is really the one's complement of LEN*/ in inflateNoCompression()
1444 if(!settings->ignore_nlen && LEN + NLEN != 65535) { in inflateNoCompression()
1448 if(!ucvector_resize(out, out->size + LEN)) return 83; /*alloc fail*/ in inflateNoCompression()
1453 /*out->data can be NULL (when LEN is zero), and arithmetics on NULL ptr is undefined*/ in inflateNoCompression()
1455 lodepng_memcpy(out->data + out->size - LEN, reader->data + bytepos, LEN); in inflateNoCompression()
1459 reader->bp = bytepos << 3u; in inflateNoCompression()
1476 if(reader.bitsize - reader.bp < 3) return 52; /*error, bit pointer will jump past memory*/ in lodepng_inflatev()
1483 …else error = inflateHuffmanBlock(out, &reader, BTYPE, settings->max_output_size); /*compression, B… in lodepng_inflatev()
1484 … if(!error && settings->max_output_size && out->size > settings->max_output_size) error = 109; in lodepng_inflatev()
1505 if(settings->custom_inflate) { in inflatev()
1506 unsigned error = settings->custom_inflate(&out->data, &out->size, in, insize, settings); in inflatev()
1507 out->allocsize = out->size; in inflatev()
1512 if(settings->max_output_size && out->size > settings->max_output_size) error = 109; in inflatev()
1537 size_t right = array_size - 1; in searchCodeIndex()
1541 if(array[mid] >= value) right = mid - 1; in searchCodeIndex()
1544 if(left >= array_size || array[left] > value) left--; in searchCodeIndex()
1551 0-255: literal bytes in addLengthDistance()
1553 …257-285: length/distance pair (length code, followed by extra length bits, distance code, extra di… in addLengthDistance()
1554 286-287: invalid*/ in addLengthDistance()
1557 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]); in addLengthDistance()
1559 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]); in addLengthDistance()
1561 size_t pos = values->size; in addLengthDistance()
1563 unsigned ok = uivector_resize(values, values->size + 4); in addLengthDistance()
1565 values->data[pos + 0] = length_code + FIRST_LENGTH_CODE_INDEX; in addLengthDistance()
1566 values->data[pos + 1] = extra_length; in addLengthDistance()
1567 values->data[pos + 2] = dist_code; in addLengthDistance()
1568 values->data[pos + 3] = extra_distance; in addLengthDistance()
1575 static const unsigned HASH_BIT_MASK = 65535; /*HASH_NUM_VALUES - 1, but C90 does not like that as i…
1578 int * head; /*hash value to head circular pos - can be outdated if went around window*/
1593 hash->head = (int *)lodepng_malloc(sizeof(int) * HASH_NUM_VALUES); in hash_init()
1594 hash->val = (int *)lodepng_malloc(sizeof(int) * windowsize); in hash_init()
1595 hash->chain = (unsigned short *)lodepng_malloc(sizeof(unsigned short) * windowsize); in hash_init()
1597 hash->zeros = (unsigned short *)lodepng_malloc(sizeof(unsigned short) * windowsize); in hash_init()
1598 hash->headz = (int *)lodepng_malloc(sizeof(int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1)); in hash_init()
1599 hash->chainz = (unsigned short *)lodepng_malloc(sizeof(unsigned short) * windowsize); in hash_init()
1601 … if(!hash->head || !hash->chain || !hash->val || !hash->headz || !hash->chainz || !hash->zeros) { in hash_init()
1606 for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1; in hash_init()
1607 for(i = 0; i != windowsize; ++i) hash->val[i] = -1; in hash_init()
1608 …for(i = 0; i != windowsize; ++i) hash->chain[i] = i; /*same value as index indicates uninitialized… in hash_init()
1610 for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1; in hash_init()
1611 …for(i = 0; i != windowsize; ++i) hash->chainz[i] = i; /*same value as index indicates uninitialize… in hash_init()
1618 lodepng_free(hash->head); in hash_cleanup()
1619 lodepng_free(hash->val); in hash_cleanup()
1620 lodepng_free(hash->chain); in hash_cleanup()
1622 lodepng_free(hash->zeros); in hash_cleanup()
1623 lodepng_free(hash->headz); in hash_cleanup()
1624 lodepng_free(hash->chainz); in hash_cleanup()
1644 amount = size - pos; in getHash()
1657 …/*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/ in countZeros()
1658 return (unsigned)(data - start); in countZeros()
1661 /*wpos = pos & (windowsize - 1)*/
1664 hash->val[wpos] = (int)hashval; in updateHashChain()
1665 if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval]; in updateHashChain()
1666 hash->head[hashval] = (int)wpos; in updateHashChain()
1668 hash->zeros[wpos] = numzeros; in updateHashChain()
1669 if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros]; in updateHashChain()
1670 hash->headz[numzeros] = (int)wpos; in updateHashChain()
1674 LZ77-encode the data. Return value is error code. The input are raw bytes, the output
1706 if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/ in encodeLZ77()
1711 size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/ in encodeLZ77()
1718 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; in encodeLZ77()
1730 hashpos = hash->chain[wpos]; in encodeLZ77()
1738 … current_offset = (unsigned)(hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize); in encodeLZ77()
1745 backptr = &in[pos - current_offset]; in encodeLZ77()
1749 unsigned skip = hash->zeros[hashpos]; in encodeLZ77()
1759 current_length = (unsigned)(foreptr - &in[pos]); in encodeLZ77()
1770 if(hashpos == hash->chain[hashpos]) break; in encodeLZ77()
1773 hashpos = hash->chainz[hashpos]; in encodeLZ77()
1774 if(hash->zeros[hashpos] != numzeros) break; in encodeLZ77()
1777 hashpos = hash->chain[hashpos]; in encodeLZ77()
1779 if(hash->val[hashpos] != (int)hashval) break; in encodeLZ77()
1795 if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/); in encodeLZ77()
1800 …hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alterati… in encodeLZ77()
1801 hash->headz[numzeros] = -1; /*idem*/ in encodeLZ77()
1802 --pos; in encodeLZ77()
1821 wpos = pos & (windowsize - 1); in encodeLZ77()
1825 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; in encodeLZ77()
1850 size_t pos = out->size; in deflateNoCompression()
1852 BFINAL = (i == numdeflateblocks - 1); in deflateNoCompression()
1856 if(datasize - datapos < 65535u) LEN = (unsigned)datasize - datapos; in deflateNoCompression()
1857 NLEN = 65535 - LEN; in deflateNoCompression()
1859 if(!ucvector_resize(out, out->size + LEN + 5)) return 83; /*alloc fail*/ in deflateNoCompression()
1862 out->data[pos + 0] = firstbyte; in deflateNoCompression()
1863 out->data[pos + 1] = (unsigned char)(LEN & 255); in deflateNoCompression()
1864 out->data[pos + 2] = (unsigned char)(LEN >> 8u); in deflateNoCompression()
1865 out->data[pos + 3] = (unsigned char)(NLEN & 255); in deflateNoCompression()
1866 out->data[pos + 4] = (unsigned char)(NLEN >> 8u); in deflateNoCompression()
1867 lodepng_memcpy(out->data + pos + 5, data + datapos, LEN); in deflateNoCompression()
1875 write the lz77-encoded data, which has lit, len and dist codes, to compressed stream using huffman …
1883 for(i = 0; i != lz77_encoded->size; ++i) { in writeLZ77data()
1884 unsigned val = lz77_encoded->data[i]; in writeLZ77data()
1885 writeBitsReversed(writer, tree_ll->codes[val], tree_ll->lengths[val]); in writeLZ77data()
1887 unsigned length_index = val - FIRST_LENGTH_CODE_INDEX; in writeLZ77data()
1889 unsigned length_extra_bits = lz77_encoded->data[++i]; in writeLZ77data()
1891 unsigned distance_code = lz77_encoded->data[++i]; in writeLZ77data()
1895 unsigned distance_extra_bits = lz77_encoded->data[++i]; in writeLZ77data()
1898 writeBitsReversed(writer, tree_d->codes[distance_code], tree_d->lengths[distance_code]); in writeLZ77data()
1917 are also run-length encoded and huffman compressed. This gives a huffman tree in deflateDynamic()
1932 size_t datasize = dataend - datapos; in deflateDynamic()
1966 if(settings->use_lz77) { in deflateDynamic()
1967 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, in deflateDynamic()
1968 settings->minmatch, settings->nicematch, settings->lazymatching); in deflateDynamic()
1974 … ++i) lz77_encoded.data[i - datapos] = data[i]; /*no LZ77, but still will be Huffman compressed*/ in deflateDynamic()
2009 …/*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times… in deflateDynamic()
2010 17 (3-10 zeroes), 18 (11-138 zeroes)*/ in deflateDynamic()
2019 bitlen_lld_e[numcodes_lld_e++] = j - 3; in deflateDynamic()
2024 bitlen_lld_e[numcodes_lld_e++] = j - 11; in deflateDynamic()
2026 i += (j - 1); in deflateDynamic()
2034 bitlen_lld_e[numcodes_lld_e++] = 6 - 3; in deflateDynamic()
2038 bitlen_lld_e[numcodes_lld_e++] = rest - 3; in deflateDynamic()
2040 else j -= rest; in deflateDynamic()
2060 /*compute amount of code-length-code-lengths to output*/ in deflateDynamic()
2063 while(numcodes_cl > 4u && tree_cl.lengths[CLCL_ORDER[numcodes_cl - 1u]] == 0) { in deflateDynamic()
2064 numcodes_cl--; in deflateDynamic()
2071 - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN in deflateDynamic()
2072 - (HCLEN+4)*3 bits code lengths of code length alphabet in deflateDynamic()
2073 - HLIT + 257 code lengths of lit/length alphabet (encoded using the code length in deflateDynamic()
2075 - HDIST + 1 code lengths of distance alphabet (encoded using the code length in deflateDynamic()
2077 - compressed data in deflateDynamic()
2078 - 256 (end code) in deflateDynamic()
2089 HLIT = (unsigned)(numcodes_ll - 257); in deflateDynamic()
2090 HDIST = (unsigned)(numcodes_d - 1); in deflateDynamic()
2091 HCLEN = (unsigned)(numcodes_cl - 4); in deflateDynamic()
2116 break; /*end of error-while*/ in deflateDynamic()
2156 if(settings->use_lz77) { /*LZ77 encoded*/ in deflateFixed()
2159 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, in deflateFixed()
2160 settings->minmatch, settings->nicematch, settings->lazymatching); in deflateFixed()
2190 if(settings->btype > 2) return 61; in lodepng_deflatev()
2191 else if(settings->btype == 0) return deflateNoCompression(out, in, insize); in lodepng_deflatev()
2192 else if(settings->btype == 1) blocksize = insize; in lodepng_deflatev()
2193 else { /*if(settings->btype == 2)*/ in lodepng_deflatev()
2194 /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/ in lodepng_deflatev()
2200 numdeflateblocks = (insize + blocksize - 1) / blocksize; in lodepng_deflatev()
2203 error = hash_init(&hash, settings->windowsize); in lodepng_deflatev()
2207 unsigned final = (i == numdeflateblocks - 1); in lodepng_deflatev()
2212 … if(settings->btype == 1) error = deflateFixed(&writer, &hash, in, start, end, settings, final); in lodepng_deflatev()
2213 …else if(settings->btype == 2) error = deflateDynamic(&writer, &hash, in, start, end, settings, fin… in lodepng_deflatev()
2237 if(settings->custom_deflate) { in deflate()
2238 unsigned error = settings->custom_deflate(out, outsize, in, insize, settings); in deflate()
2262 len -= amount; in update_adler32()
2274 /*Return the adler32 of the bytes data[0..len-1]*/
2316 error = inflatev(out, in + 2, insize - 2, settings); in lodepng_zlib_decompressv()
2319 if(!settings->ignore_adler32) { in lodepng_zlib_decompressv()
2320 unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]); in lodepng_zlib_decompressv()
2321 unsigned checksum = adler32(out->data, (unsigned)(out->size)); in lodepng_zlib_decompressv()
2344 if(settings->custom_zlib) { in zlib_decompress()
2345 error = settings->custom_zlib(out, outsize, in, insize, settings); in zlib_decompress()
2350 if(settings->max_output_size && *outsize > settings->max_output_size) error = 109; in zlib_decompress()
2396 unsigned FCHECK = 31 - CMFFLG % 31; in lodepng_zlib_compress()
2402 lodepng_set32bitInt(&(*out)[*outsize - 4], ADLER32); in lodepng_zlib_compress()
2413 if(settings->custom_zlib) { in zlib_compress()
2414 unsigned error = settings->custom_zlib(out, outsize, in, insize, settings); in zlib_compress()
2431 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ in zlib_decompress()
2433 return settings->custom_zlib(out, outsize, in, insize, settings); in zlib_decompress()
2440 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ in zlib_compress()
2441 return settings->custom_zlib(out, outsize, in, insize, settings); in zlib_compress()
2457 settings->btype = 2; in lodepng_compress_settings_init()
2458 settings->use_lz77 = 1; in lodepng_compress_settings_init()
2459 settings->windowsize = DEFAULT_WINDOWSIZE; in lodepng_compress_settings_init()
2460 settings->minmatch = 3; in lodepng_compress_settings_init()
2461 settings->nicematch = 128; in lodepng_compress_settings_init()
2462 settings->lazymatching = 1; in lodepng_compress_settings_init()
2464 settings->custom_zlib = 0; in lodepng_compress_settings_init()
2465 settings->custom_deflate = 0; in lodepng_compress_settings_init()
2466 settings->custom_context = 0; in lodepng_compress_settings_init()
2478 settings->ignore_adler32 = 0; in lodepng_decompress_settings_init()
2479 settings->ignore_nlen = 0; in lodepng_decompress_settings_init()
2480 settings->max_output_size = 0; in lodepng_decompress_settings_init()
2482 settings->custom_zlib = 0; in lodepng_decompress_settings_init()
2483 settings->custom_inflate = 0; in lodepng_decompress_settings_init()
2484 settings->custom_context = 0; in lodepng_decompress_settings_init()
2790 unsigned r = 0xffffffffu; in lodepng_crc32() local
2792 r = lodepng_crc32_table7[(data[0] ^ (r & 0xffu))] ^ in lodepng_crc32()
2793 lodepng_crc32_table6[(data[1] ^ ((r >> 8) & 0xffu))] ^ in lodepng_crc32()
2794 lodepng_crc32_table5[(data[2] ^ ((r >> 16) & 0xffu))] ^ in lodepng_crc32()
2795 lodepng_crc32_table4[(data[3] ^ ((r >> 24) & 0xffu))] ^ in lodepng_crc32()
2801 length -= 8; in lodepng_crc32()
2803 while(length--) { in lodepng_crc32()
2804 r = lodepng_crc32_table0[(r ^ *data++) & 0xffu] ^ (r >> 8); in lodepng_crc32()
2806 return r ^ 0xffffffffu; in lodepng_crc32()
2815 unsigned r = 0xffffffffu;
2820 while(length--) {
2821 r = table[(r ^ *data) & 0xf] ^ (r >> 4);
2822 r = table[(r ^ (*data >> 4)) & 0xf] ^ (r >> 4);
2825 return r ^ 0xffffffffu;
2835 /* The color channel bits of less-than-8-bit pixels are read with the MSB of bytes first,
2840 …unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7… in readBitFromReversedStream()
2860 …if(bit == 0) bitstream[(*bitpointer) >> 3u] &= (unsigned char)(~(1u << (7u - ((*bitpointer) & 7u))… in setBitOfReversedStream()
2861 else bitstream[(*bitpointer) >> 3u] |= (1u << (7u - ((*bitpointer) & 7u))); in setBitOfReversedStream()
2931 size_t available_size = (size_t)(end - chunk); in lodepng_chunk_next()
2948 size_t available_size = (size_t)(end - chunk); in lodepng_chunk_next_const()
2966 if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */ in lodepng_chunk_find()
2976 if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */ in lodepng_chunk_find_const()
2995 chunk_start = &(*out)[new_length - total_chunk_length]; in lodepng_chunk_append()
3010 size_t new_length = out->size; in lodepng_chunk_init()
3014 *chunk = out->data + new_length - length - 12u; in lodepng_chunk_init()
3113 info->key_defined = 0; in lodepng_color_mode_init()
3114 info->key_r = info->key_g = info->key_b = 0; in lodepng_color_mode_init()
3115 info->colortype = LCT_RGBA; in lodepng_color_mode_init()
3116 info->bitdepth = 8; in lodepng_color_mode_init()
3117 info->palette = 0; in lodepng_color_mode_init()
3118 info->palettesize = 0; in lodepng_color_mode_init()
3127 if(!info->palette) info->palette = (unsigned char *)lodepng_malloc(1024); in lodepng_color_mode_alloc_palette()
3128 if(!info->palette) return; /*alloc fail*/ in lodepng_color_mode_alloc_palette()
3133 info->palette[i * 4 + 0] = 0; in lodepng_color_mode_alloc_palette()
3134 info->palette[i * 4 + 1] = 0; in lodepng_color_mode_alloc_palette()
3135 info->palette[i * 4 + 2] = 0; in lodepng_color_mode_alloc_palette()
3136 info->palette[i * 4 + 3] = 255; in lodepng_color_mode_alloc_palette()
3149 if(source->palette) { in lodepng_color_mode_copy()
3150 dest->palette = (unsigned char *)lodepng_malloc(1024); in lodepng_color_mode_copy()
3151 if(!dest->palette && source->palettesize) return 83; /*alloc fail*/ in lodepng_color_mode_copy()
3152 lodepng_memcpy(dest->palette, source->palette, source->palettesize * 4); in lodepng_color_mode_copy()
3169 if(a->colortype != b->colortype) return 0; in lodepng_color_mode_equal()
3170 if(a->bitdepth != b->bitdepth) return 0; in lodepng_color_mode_equal()
3171 if(a->key_defined != b->key_defined) return 0; in lodepng_color_mode_equal()
3172 if(a->key_defined) { in lodepng_color_mode_equal()
3173 if(a->key_r != b->key_r) return 0; in lodepng_color_mode_equal()
3174 if(a->key_g != b->key_g) return 0; in lodepng_color_mode_equal()
3175 if(a->key_b != b->key_b) return 0; in lodepng_color_mode_equal()
3177 if(a->palettesize != b->palettesize) return 0; in lodepng_color_mode_equal()
3178 for(i = 0; i != a->palettesize * 4; ++i) { in lodepng_color_mode_equal()
3179 if(a->palette[i] != b->palette[i]) return 0; in lodepng_color_mode_equal()
3186 if(info->palette) lodepng_free(info->palette); in lodepng_palette_clear()
3187 info->palette = 0; in lodepng_palette_clear()
3188 info->palettesize = 0; in lodepng_palette_clear()
3192 unsigned char r, unsigned char g, unsigned char b, unsigned char a) in lodepng_palette_add() argument
3194 if(!info->palette) { /*allocate palette if empty*/ in lodepng_palette_add()
3196 if(!info->palette) return 83; /*alloc fail*/ in lodepng_palette_add()
3198 if(info->palettesize >= 256) { in lodepng_palette_add()
3201 info->palette[4 * info->palettesize + 0] = r; in lodepng_palette_add()
3202 info->palette[4 * info->palettesize + 1] = g; in lodepng_palette_add()
3203 info->palette[4 * info->palettesize + 2] = b; in lodepng_palette_add()
3204 info->palette[4 * info->palettesize + 3] = a; in lodepng_palette_add()
3205 ++info->palettesize; in lodepng_palette_add()
3212 return lodepng_get_bpp_lct(info->colortype, info->bitdepth); in lodepng_get_bpp()
3217 return getNumColorChannels(info->colortype); in lodepng_get_channels()
3222 return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA; in lodepng_is_greyscale_type()
3227 return (info->colortype & 4) != 0; /*4 or 6*/ in lodepng_is_alpha_type()
3232 return info->colortype == LCT_PALETTE; in lodepng_is_palette_type()
3238 for(i = 0; i != info->palettesize; ++i) { in lodepng_has_palette_alpha()
3239 if(info->palette[i * 4 + 3] < 255) return 1; in lodepng_has_palette_alpha()
3246 return info->key_defined in lodepng_can_have_alpha()
3260 return lodepng_get_raw_size_lct(w, h, color->colortype, color->bitdepth); in lodepng_get_raw_size()
3281 -(size_t)w * (size_t)h * 8
3282 -amount of bytes in IDAT (including filter, padding and Adam7 bytes)
3283 -amount of bytes in raw color model
3294 …if(lodepng_mulofl(numpixels, 8, &total)) return 1; /* bit pointer with 8-bit color, or 8 bytes per… in lodepng_pixel_overflow()
3313 for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0; in LodePNGUnknownChunks_init()
3314 for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0; in LodePNGUnknownChunks_init()
3320 for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]); in LodePNGUnknownChunks_cleanup()
3331 dest->unknown_chunks_size[i] = src->unknown_chunks_size[i]; in LodePNGUnknownChunks_copy()
3332 dest->unknown_chunks_data[i] = (unsigned char *)lodepng_malloc(src->unknown_chunks_size[i]); in LodePNGUnknownChunks_copy()
3333 if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/ in LodePNGUnknownChunks_copy()
3334 for(j = 0; j < src->unknown_chunks_size[i]; ++j) { in LodePNGUnknownChunks_copy()
3335 dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j]; in LodePNGUnknownChunks_copy()
3346 info->text_num = 0; in LodePNGText_init()
3347 info->text_keys = NULL; in LodePNGText_init()
3348 info->text_strings = NULL; in LodePNGText_init()
3354 for(i = 0; i != info->text_num; ++i) { in LodePNGText_cleanup()
3355 string_cleanup(&info->text_keys[i]); in LodePNGText_cleanup()
3356 string_cleanup(&info->text_strings[i]); in LodePNGText_cleanup()
3358 lodepng_free(info->text_keys); in LodePNGText_cleanup()
3359 lodepng_free(info->text_strings); in LodePNGText_cleanup()
3365 dest->text_keys = NULL; in LodePNGText_copy()
3366 dest->text_strings = NULL; in LodePNGText_copy()
3367 dest->text_num = 0; in LodePNGText_copy()
3368 for(i = 0; i != source->text_num; ++i) { in LodePNGText_copy()
3369 CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i])); in LodePNGText_copy()
3376 …char ** new_keys = (char **)(lodepng_realloc(info->text_keys, sizeof(char *) * (info->text_num + 1… in lodepng_add_text_sized()
3377 …char ** new_strings = (char **)(lodepng_realloc(info->text_strings, sizeof(char *) * (info->text_n… in lodepng_add_text_sized()
3379 if(new_keys) info->text_keys = new_keys; in lodepng_add_text_sized()
3380 if(new_strings) info->text_strings = new_strings; in lodepng_add_text_sized()
3384 ++info->text_num; in lodepng_add_text_sized()
3385 info->text_keys[info->text_num - 1] = alloc_string(key); in lodepng_add_text_sized()
3386 info->text_strings[info->text_num - 1] = alloc_string_sized(str, size); in lodepng_add_text_sized()
3387 …if(!info->text_keys[info->text_num - 1] || !info->text_strings[info->text_num - 1]) return 83; /*a… in lodepng_add_text_sized()
3406 info->itext_num = 0; in LodePNGIText_init()
3407 info->itext_keys = NULL; in LodePNGIText_init()
3408 info->itext_langtags = NULL; in LodePNGIText_init()
3409 info->itext_transkeys = NULL; in LodePNGIText_init()
3410 info->itext_strings = NULL; in LodePNGIText_init()
3416 for(i = 0; i != info->itext_num; ++i) { in LodePNGIText_cleanup()
3417 string_cleanup(&info->itext_keys[i]); in LodePNGIText_cleanup()
3418 string_cleanup(&info->itext_langtags[i]); in LodePNGIText_cleanup()
3419 string_cleanup(&info->itext_transkeys[i]); in LodePNGIText_cleanup()
3420 string_cleanup(&info->itext_strings[i]); in LodePNGIText_cleanup()
3422 lodepng_free(info->itext_keys); in LodePNGIText_cleanup()
3423 lodepng_free(info->itext_langtags); in LodePNGIText_cleanup()
3424 lodepng_free(info->itext_transkeys); in LodePNGIText_cleanup()
3425 lodepng_free(info->itext_strings); in LodePNGIText_cleanup()
3431 dest->itext_keys = NULL; in LodePNGIText_copy()
3432 dest->itext_langtags = NULL; in LodePNGIText_copy()
3433 dest->itext_transkeys = NULL; in LodePNGIText_copy()
3434 dest->itext_strings = NULL; in LodePNGIText_copy()
3435 dest->itext_num = 0; in LodePNGIText_copy()
3436 for(i = 0; i != source->itext_num; ++i) { in LodePNGIText_copy()
3437 CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i], in LodePNGIText_copy()
3438 source->itext_transkeys[i], source->itext_strings[i])); in LodePNGIText_copy()
3451 …char ** new_keys = (char **)(lodepng_realloc(info->itext_keys, sizeof(char *) * (info->itext_num +… in lodepng_add_itext_sized()
3452 …char ** new_langtags = (char **)(lodepng_realloc(info->itext_langtags, sizeof(char *) * (info->ite… in lodepng_add_itext_sized()
3453 …char ** new_transkeys = (char **)(lodepng_realloc(info->itext_transkeys, sizeof(char *) * (info->i… in lodepng_add_itext_sized()
3454 …char ** new_strings = (char **)(lodepng_realloc(info->itext_strings, sizeof(char *) * (info->itext… in lodepng_add_itext_sized()
3456 if(new_keys) info->itext_keys = new_keys; in lodepng_add_itext_sized()
3457 if(new_langtags) info->itext_langtags = new_langtags; in lodepng_add_itext_sized()
3458 if(new_transkeys) info->itext_transkeys = new_transkeys; in lodepng_add_itext_sized()
3459 if(new_strings) info->itext_strings = new_strings; in lodepng_add_itext_sized()
3463 ++info->itext_num; in lodepng_add_itext_sized()
3465 info->itext_keys[info->itext_num - 1] = alloc_string(key); in lodepng_add_itext_sized()
3466 info->itext_langtags[info->itext_num - 1] = alloc_string(langtag); in lodepng_add_itext_sized()
3467 info->itext_transkeys[info->itext_num - 1] = alloc_string(transkey); in lodepng_add_itext_sized()
3468 info->itext_strings[info->itext_num - 1] = alloc_string_sized(str, size); in lodepng_add_itext_sized()
3485 info->iccp_name = alloc_string(name); in lodepng_assign_icc()
3486 info->iccp_profile = (unsigned char *)lodepng_malloc(profile_size); in lodepng_assign_icc()
3488 if(!info->iccp_name || !info->iccp_profile) return 83; /*alloc fail*/ in lodepng_assign_icc()
3490 lodepng_memcpy(info->iccp_profile, profile, profile_size); in lodepng_assign_icc()
3491 info->iccp_profile_size = profile_size; in lodepng_assign_icc()
3498 if(info->iccp_name) lodepng_clear_icc(info); in lodepng_set_icc()
3499 info->iccp_defined = 1; in lodepng_set_icc()
3506 string_cleanup(&info->iccp_name); in lodepng_clear_icc()
3507 lodepng_free(info->iccp_profile); in lodepng_clear_icc()
3508 info->iccp_profile = NULL; in lodepng_clear_icc()
3509 info->iccp_profile_size = 0; in lodepng_clear_icc()
3510 info->iccp_defined = 0; in lodepng_clear_icc()
3516 lodepng_color_mode_init(&info->color); in lodepng_info_init()
3517 info->interlace_method = 0; in lodepng_info_init()
3518 info->compression_method = 0; in lodepng_info_init()
3519 info->filter_method = 0; in lodepng_info_init()
3521 info->background_defined = 0; in lodepng_info_init()
3522 info->background_r = info->background_g = info->background_b = 0; in lodepng_info_init()
3527 info->time_defined = 0; in lodepng_info_init()
3528 info->phys_defined = 0; in lodepng_info_init()
3530 info->gama_defined = 0; in lodepng_info_init()
3531 info->chrm_defined = 0; in lodepng_info_init()
3532 info->srgb_defined = 0; in lodepng_info_init()
3533 info->iccp_defined = 0; in lodepng_info_init()
3534 info->iccp_name = NULL; in lodepng_info_init()
3535 info->iccp_profile = NULL; in lodepng_info_init()
3537 info->sbit_defined = 0; in lodepng_info_init()
3538 info->sbit_r = info->sbit_g = info->sbit_b = info->sbit_a = 0; in lodepng_info_init()
3546 lodepng_color_mode_cleanup(&info->color); in lodepng_info_cleanup()
3561 lodepng_color_mode_init(&dest->color); in lodepng_info_copy()
3562 CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color)); in lodepng_info_copy()
3567 if(source->iccp_defined) { in lodepng_info_copy()
3568 …CERROR_TRY_RETURN(lodepng_assign_icc(dest, source->iccp_name, source->iccp_profile, source->iccp_p… in lodepng_info_copy()
3582 unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/ in addColorBits()
3585 in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/ in addColorBits()
3586 in = in << (bits * (m - p)); in addColorBits()
3606 lodepng_memset(tree->children, 0, 16 * sizeof(*tree->children)); in color_tree_init()
3607 tree->index = -1; in color_tree_init()
3614 if(tree->children[i]) { in color_tree_cleanup()
3615 color_tree_cleanup(tree->children[i]); in color_tree_cleanup()
3616 lodepng_free(tree->children[i]); in color_tree_cleanup()
3621 /*returns -1 if color not present, its index otherwise*/
3622 static int color_tree_get(ColorTree * tree, unsigned char r, unsigned char g, unsigned char b, unsi… in color_tree_get() argument
3626 … int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); in color_tree_get()
3627 if(!tree->children[i]) return -1; in color_tree_get()
3628 else tree = tree->children[i]; in color_tree_get()
3630 return tree ? tree->index : -1; in color_tree_get()
3634 static int color_tree_has(ColorTree * tree, unsigned char r, unsigned char g, unsigned char b, unsi… in color_tree_has() argument
3636 return color_tree_get(tree, r, g, b, a) >= 0; in color_tree_has()
3641 Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")
3644 … unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index) in color_tree_add() argument
3648 … int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); in color_tree_add()
3649 if(!tree->children[i]) { in color_tree_add()
3650 tree->children[i] = (ColorTree *)lodepng_malloc(sizeof(ColorTree)); in color_tree_add()
3651 if(!tree->children[i]) return 83; /*alloc fail*/ in color_tree_add()
3652 color_tree_init(tree->children[i]); in color_tree_add()
3654 tree = tree->children[i]; in color_tree_add()
3656 tree->index = (int)index; in color_tree_add()
3663 unsigned char r, unsigned char g, unsigned char b, unsigned char a) in rgba8ToPixel() argument
3665 if(mode->colortype == LCT_GREY) { in rgba8ToPixel()
3666 unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/ in rgba8ToPixel()
3667 if(mode->bitdepth == 8) out[i] = gray; in rgba8ToPixel()
3668 else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray; in rgba8ToPixel()
3671 gray = ((unsigned)gray >> (8u - mode->bitdepth)) & ((1u << mode->bitdepth) - 1u); in rgba8ToPixel()
3672 addColorBits(out, i, mode->bitdepth, gray); in rgba8ToPixel()
3675 else if(mode->colortype == LCT_RGB) { in rgba8ToPixel()
3676 if(mode->bitdepth == 8) { in rgba8ToPixel()
3677 out[i * 3 + 0] = r; in rgba8ToPixel()
3682 out[i * 6 + 0] = out[i * 6 + 1] = r; in rgba8ToPixel()
3687 else if(mode->colortype == LCT_PALETTE) { in rgba8ToPixel()
3688 int index = color_tree_get(tree, r, g, b, a); in rgba8ToPixel()
3690 if(mode->bitdepth == 8) out[i] = index; in rgba8ToPixel()
3691 else addColorBits(out, i, mode->bitdepth, (unsigned)index); in rgba8ToPixel()
3693 else if(mode->colortype == LCT_GREY_ALPHA) { in rgba8ToPixel()
3694 unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/ in rgba8ToPixel()
3695 if(mode->bitdepth == 8) { in rgba8ToPixel()
3699 else if(mode->bitdepth == 16) { in rgba8ToPixel()
3704 else if(mode->colortype == LCT_RGBA) { in rgba8ToPixel()
3705 if(mode->bitdepth == 8) { in rgba8ToPixel()
3706 out[i * 4 + 0] = r; in rgba8ToPixel()
3712 out[i * 8 + 0] = out[i * 8 + 1] = r; in rgba8ToPixel()
3722 /*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/
3725 unsigned short r, unsigned short g, unsigned short b, unsigned short a) in rgba16ToPixel() argument
3727 if(mode->colortype == LCT_GREY) { in rgba16ToPixel()
3728 unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/ in rgba16ToPixel()
3732 else if(mode->colortype == LCT_RGB) { in rgba16ToPixel()
3733 out[i * 6 + 0] = (r >> 8) & 255; in rgba16ToPixel()
3734 out[i * 6 + 1] = r & 255; in rgba16ToPixel()
3740 else if(mode->colortype == LCT_GREY_ALPHA) { in rgba16ToPixel()
3741 unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/ in rgba16ToPixel()
3747 else if(mode->colortype == LCT_RGBA) { in rgba16ToPixel()
3748 out[i * 8 + 0] = (r >> 8) & 255; in rgba16ToPixel()
3749 out[i * 8 + 1] = r & 255; in rgba16ToPixel()
3760 static void getPixelColorRGBA8(unsigned char * r, unsigned char * g, in getPixelColorRGBA8() argument
3765 if(mode->colortype == LCT_GREY) { in getPixelColorRGBA8()
3766 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3767 *r = *g = *b = in[i]; in getPixelColorRGBA8()
3768 if(mode->key_defined && *r == mode->key_r) *a = 0; in getPixelColorRGBA8()
3771 else if(mode->bitdepth == 16) { in getPixelColorRGBA8()
3772 *r = *g = *b = in[i * 2 + 0]; in getPixelColorRGBA8()
3773 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; in getPixelColorRGBA8()
3777 … unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ in getPixelColorRGBA8()
3778 size_t j = i * mode->bitdepth; in getPixelColorRGBA8()
3779 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorRGBA8()
3780 *r = *g = *b = (value * 255) / highest; in getPixelColorRGBA8()
3781 if(mode->key_defined && value == mode->key_r) *a = 0; in getPixelColorRGBA8()
3785 else if(mode->colortype == LCT_RGB) { in getPixelColorRGBA8()
3786 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3787 *r = in[i * 3 + 0]; in getPixelColorRGBA8()
3790 … if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0; in getPixelColorRGBA8()
3794 *r = in[i * 6 + 0]; in getPixelColorRGBA8()
3797 if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r in getPixelColorRGBA8()
3798 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g in getPixelColorRGBA8()
3799 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; in getPixelColorRGBA8()
3803 else if(mode->colortype == LCT_PALETTE) { in getPixelColorRGBA8()
3805 if(mode->bitdepth == 8) index = in[i]; in getPixelColorRGBA8()
3807 size_t j = i * mode->bitdepth; in getPixelColorRGBA8()
3808 index = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorRGBA8()
3811 *r = mode->palette[index * 4 + 0]; in getPixelColorRGBA8()
3812 *g = mode->palette[index * 4 + 1]; in getPixelColorRGBA8()
3813 *b = mode->palette[index * 4 + 2]; in getPixelColorRGBA8()
3814 *a = mode->palette[index * 4 + 3]; in getPixelColorRGBA8()
3816 else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorRGBA8()
3817 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3818 *r = *g = *b = in[i * 2 + 0]; in getPixelColorRGBA8()
3822 *r = *g = *b = in[i * 4 + 0]; in getPixelColorRGBA8()
3826 else if(mode->colortype == LCT_RGBA) { in getPixelColorRGBA8()
3827 if(mode->bitdepth == 8) { in getPixelColorRGBA8()
3828 *r = in[i * 4 + 0]; in getPixelColorRGBA8()
3834 *r = in[i * 8 + 0]; in getPixelColorRGBA8()
3852 if(mode->colortype == LCT_GREY) { in getPixelColorsRGBA8()
3853 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3858 if(mode->key_defined) { in getPixelColorsRGBA8()
3859 buffer -= numpixels * num_channels; in getPixelColorsRGBA8()
3861 if(buffer[0] == mode->key_r) buffer[3] = 0; in getPixelColorsRGBA8()
3865 else if(mode->bitdepth == 16) { in getPixelColorsRGBA8()
3868 … buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255; in getPixelColorsRGBA8()
3872 … unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ in getPixelColorsRGBA8()
3875 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGBA8()
3877 buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255; in getPixelColorsRGBA8()
3881 else if(mode->colortype == LCT_RGB) { in getPixelColorsRGBA8()
3882 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3887 if(mode->key_defined) { in getPixelColorsRGBA8()
3888 buffer -= numpixels * num_channels; in getPixelColorsRGBA8()
3890 …if(buffer[0] == mode->key_r && buffer[1] == mode->key_g && buffer[2] == mode->key_b) buffer[3] = 0; in getPixelColorsRGBA8()
3899 buffer[3] = mode->key_defined in getPixelColorsRGBA8()
3900 && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r in getPixelColorsRGBA8()
3901 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g in getPixelColorsRGBA8()
3902 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255; in getPixelColorsRGBA8()
3906 else if(mode->colortype == LCT_PALETTE) { in getPixelColorsRGBA8()
3907 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3911 lodepng_memcpy(buffer, &mode->palette[index * 4], 4); in getPixelColorsRGBA8()
3917 unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGBA8()
3919 lodepng_memcpy(buffer, &mode->palette[index * 4], 4); in getPixelColorsRGBA8()
3923 else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorsRGBA8()
3924 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3937 else if(mode->colortype == LCT_RGBA) { in getPixelColorsRGBA8()
3938 if(mode->bitdepth == 8) { in getPixelColorsRGBA8()
3952 /*Similar to getPixelColorsRGBA8, but with 3-channel RGB output.*/
3959 if(mode->colortype == LCT_GREY) { in getPixelColorsRGB8()
3960 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3965 else if(mode->bitdepth == 16) { in getPixelColorsRGB8()
3971 … unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ in getPixelColorsRGB8()
3974 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGB8()
3979 else if(mode->colortype == LCT_RGB) { in getPixelColorsRGB8()
3980 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3991 else if(mode->colortype == LCT_PALETTE) { in getPixelColorsRGB8()
3992 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
3996 lodepng_memcpy(buffer, &mode->palette[index * 4], 3); in getPixelColorsRGB8()
4002 unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth); in getPixelColorsRGB8()
4004 lodepng_memcpy(buffer, &mode->palette[index * 4], 3); in getPixelColorsRGB8()
4008 else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorsRGB8()
4009 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
4020 else if(mode->colortype == LCT_RGBA) { in getPixelColorsRGB8()
4021 if(mode->bitdepth == 8) { in getPixelColorsRGB8()
4037 given color type, but the given color type must be 16-bit itself.*/
4038 static void getPixelColorRGBA16(unsigned short * r, unsigned short * g, unsigned short * b, unsigne… in getPixelColorRGBA16() argument
4041 if(mode->colortype == LCT_GREY) { in getPixelColorRGBA16()
4042 *r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1]; in getPixelColorRGBA16()
4043 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; in getPixelColorRGBA16()
4046 else if(mode->colortype == LCT_RGB) { in getPixelColorRGBA16()
4047 *r = 256u * in[i * 6 + 0] + in[i * 6 + 1]; in getPixelColorRGBA16()
4050 if(mode->key_defined in getPixelColorRGBA16()
4051 && 256u * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r in getPixelColorRGBA16()
4052 && 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g in getPixelColorRGBA16()
4053 && 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; in getPixelColorRGBA16()
4056 else if(mode->colortype == LCT_GREY_ALPHA) { in getPixelColorRGBA16()
4057 *r = *g = *b = 256u * in[i * 4 + 0] + in[i * 4 + 1]; in getPixelColorRGBA16()
4060 else if(mode->colortype == LCT_RGBA) { in getPixelColorRGBA16()
4061 *r = 256u * in[i * 8 + 0] + in[i * 8 + 1]; in getPixelColorRGBA16()
4077 if(mode_in->colortype == LCT_PALETTE && !mode_in->palette) { in lodepng_convert()
4087 if(mode_out->colortype == LCT_PALETTE) { in lodepng_convert()
4088 size_t palettesize = mode_out->palettesize; in lodepng_convert()
4089 const unsigned char * palette = mode_out->palette; in lodepng_convert()
4090 size_t palsize = (size_t)1u << mode_out->bitdepth; in lodepng_convert()
4095 palettesize = mode_in->palettesize; in lodepng_convert()
4096 palette = mode_in->palette; in lodepng_convert()
4100 if(mode_in->colortype == LCT_PALETTE && mode_in->bitdepth == mode_out->bitdepth) { in lodepng_convert()
4116 if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) { in lodepng_convert()
4118 unsigned short r = 0, g = 0, b = 0, a = 0; in lodepng_convert() local
4119 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); in lodepng_convert()
4120 rgba16ToPixel(out, i, mode_out, r, g, b, a); in lodepng_convert()
4123 else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) { in lodepng_convert()
4126 else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) { in lodepng_convert()
4130 unsigned char r = 0, g = 0, b = 0, a = 0; in lodepng_convert() local
4132 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); in lodepng_convert()
4133 error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a); in lodepng_convert()
4139 if(mode_out->colortype == LCT_PALETTE) { in lodepng_convert()
4148 their bitdepth. In case of single channel (gray or palette), only the r channel is used. Slow
4158 unsigned r = 0, g = 0, b = 0; in lodepng_convert_rgb() local
4159 unsigned mul = 65535 / ((1u << mode_in->bitdepth) - 1u); /*65535, 21845, 4369, 257, 1*/ in lodepng_convert_rgb()
4160 unsigned shift = 16 - mode_out->bitdepth; in lodepng_convert_rgb()
4162 if(mode_in->colortype == LCT_GREY || mode_in->colortype == LCT_GREY_ALPHA) { in lodepng_convert_rgb()
4163 r = g = b = r_in * mul; in lodepng_convert_rgb()
4165 else if(mode_in->colortype == LCT_RGB || mode_in->colortype == LCT_RGBA) { in lodepng_convert_rgb()
4166 r = r_in * mul; in lodepng_convert_rgb()
4170 else if(mode_in->colortype == LCT_PALETTE) { in lodepng_convert_rgb()
4171 if(r_in >= mode_in->palettesize) return 82; in lodepng_convert_rgb()
4172 r = mode_in->palette[r_in * 4 + 0] * 257u; in lodepng_convert_rgb()
4173 g = mode_in->palette[r_in * 4 + 1] * 257u; in lodepng_convert_rgb()
4174 b = mode_in->palette[r_in * 4 + 2] * 257u; in lodepng_convert_rgb()
4181 if(mode_out->colortype == LCT_GREY || mode_out->colortype == LCT_GREY_ALPHA) { in lodepng_convert_rgb()
4182 *r_out = r >> shift ; in lodepng_convert_rgb()
4184 else if(mode_out->colortype == LCT_RGB || mode_out->colortype == LCT_RGBA) { in lodepng_convert_rgb()
4185 *r_out = r >> shift ; in lodepng_convert_rgb()
4189 else if(mode_out->colortype == LCT_PALETTE) { in lodepng_convert_rgb()
4191 /* a 16-bit color cannot be in the palette */ in lodepng_convert_rgb()
4192 if((r >> 8) != (r & 255) || (g >> 8) != (g & 255) || (b >> 8) != (b & 255)) return 82; in lodepng_convert_rgb()
4193 for(i = 0; i < mode_out->palettesize; i++) { in lodepng_convert_rgb()
4195 if((r >> 8) == mode_out->palette[j + 0] && (g >> 8) == mode_out->palette[j + 1] && in lodepng_convert_rgb()
4196 (b >> 8) == mode_out->palette[j + 2]) { in lodepng_convert_rgb()
4215 stats->colored = 0; in lodepng_color_stats_init()
4216 stats->key = 0; in lodepng_color_stats_init()
4217 stats->key_r = stats->key_g = stats->key_b = 0; in lodepng_color_stats_init()
4218 stats->alpha = 0; in lodepng_color_stats_init()
4219 stats->numcolors = 0; in lodepng_color_stats_init()
4220 stats->bits = 1; in lodepng_color_stats_init()
4221 stats->numpixels = 0; in lodepng_color_stats_init()
4223 stats->allow_palette = 1; in lodepng_color_stats_init()
4224 stats->allow_greyscale = 1; in lodepng_color_stats_init()
4229 std::cout << "colored: " << (int)p->colored << ", ";
4230 std::cout << "key: " << (int)p->key << ", ";
4231 std::cout << "key_r: " << (int)p->key_r << ", ";
4232 std::cout << "key_g: " << (int)p->key_g << ", ";
4233 std::cout << "key_b: " << (int)p->key_b << ", ";
4234 std::cout << "alpha: " << (int)p->alpha << ", ";
4235 std::cout << "numcolors: " << (int)p->numcolors << ", ";
4236 std::cout << "bits: " << (int)p->bits << std::endl;
4243 /*The scaling of 2-bit and 4-bit values uses multiples of 85 and 17*/ in getValueRequiredBits()
4263 unsigned bits_done = (stats->bits == 1 && bpp == 1) ? 1 : 0; in lodepng_compute_color_stats()
4266 if(bpp <= 8) maxnumcolors = LODEPNG_MIN(257, stats->numcolors + (1u << bpp)); in lodepng_compute_color_stats()
4268 stats->numpixels += numpixels; in lodepng_compute_color_stats()
4271 if(!stats->allow_palette) numcolors_done = 1; in lodepng_compute_color_stats()
4277 if(stats->alpha) alpha_done = 1; in lodepng_compute_color_stats()
4278 if(stats->colored) colored_done = 1; in lodepng_compute_color_stats()
4279 if(stats->bits == 16) numcolors_done = 1; in lodepng_compute_color_stats()
4280 if(stats->bits >= bpp) bits_done = 1; in lodepng_compute_color_stats()
4281 if(stats->numcolors >= maxnumcolors) numcolors_done = 1; in lodepng_compute_color_stats()
4284 for(i = 0; i < stats->numcolors; i++) { in lodepng_compute_color_stats()
4285 const unsigned char * color = &stats->palette[i * 4]; in lodepng_compute_color_stats()
4291 /*Check if the 16-bit input is truly 16-bit*/ in lodepng_compute_color_stats()
4292 if(mode_in->bitdepth == 16 && !sixteen) { in lodepng_compute_color_stats()
4293 unsigned short r = 0, g = 0, b = 0, a = 0; in lodepng_compute_color_stats() local
4295 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); in lodepng_compute_color_stats()
4296 if((r & 255) != ((r >> 8) & 255) || (g & 255) != ((g >> 8) & 255) || in lodepng_compute_color_stats()
4298 stats->bits = 16; in lodepng_compute_color_stats()
4301 … numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/ in lodepng_compute_color_stats()
4308 unsigned short r = 0, g = 0, b = 0, a = 0; in lodepng_compute_color_stats() local
4311 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); in lodepng_compute_color_stats()
4313 if(!colored_done && (r != g || r != b)) { in lodepng_compute_color_stats()
4314 stats->colored = 1; in lodepng_compute_color_stats()
4319 unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b); in lodepng_compute_color_stats()
4320 if(a != 65535 && (a != 0 || (stats->key && !matchkey))) { in lodepng_compute_color_stats()
4321 stats->alpha = 1; in lodepng_compute_color_stats()
4322 stats->key = 0; in lodepng_compute_color_stats()
4325 else if(a == 0 && !stats->alpha && !stats->key) { in lodepng_compute_color_stats()
4326 stats->key = 1; in lodepng_compute_color_stats()
4327 stats->key_r = r; in lodepng_compute_color_stats()
4328 stats->key_g = g; in lodepng_compute_color_stats()
4329 stats->key_b = b; in lodepng_compute_color_stats()
4331 else if(a == 65535 && stats->key && matchkey) { in lodepng_compute_color_stats()
4333 stats->alpha = 1; in lodepng_compute_color_stats()
4334 stats->key = 0; in lodepng_compute_color_stats()
4341 if(stats->key && !stats->alpha) { in lodepng_compute_color_stats()
4343 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); in lodepng_compute_color_stats()
4344 if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) { in lodepng_compute_color_stats()
4346 stats->alpha = 1; in lodepng_compute_color_stats()
4347 stats->key = 0; in lodepng_compute_color_stats()
4353 else { /* < 16-bit */ in lodepng_compute_color_stats()
4354 unsigned char r = 0, g = 0, b = 0, a = 0; in lodepng_compute_color_stats() local
4356 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); in lodepng_compute_color_stats()
4358 if(!bits_done && stats->bits < 8) { in lodepng_compute_color_stats()
4359 /*only r is checked, < 8 bits is only relevant for grayscale*/ in lodepng_compute_color_stats()
4360 unsigned bits = getValueRequiredBits(r); in lodepng_compute_color_stats()
4361 if(bits > stats->bits) stats->bits = bits; in lodepng_compute_color_stats()
4363 bits_done = (stats->bits >= bpp); in lodepng_compute_color_stats()
4365 if(!colored_done && (r != g || r != b)) { in lodepng_compute_color_stats()
4366 stats->colored = 1; in lodepng_compute_color_stats()
4368 … if(stats->bits < 8) stats->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/ in lodepng_compute_color_stats()
4372 unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b); in lodepng_compute_color_stats()
4373 if(a != 255 && (a != 0 || (stats->key && !matchkey))) { in lodepng_compute_color_stats()
4374 stats->alpha = 1; in lodepng_compute_color_stats()
4375 stats->key = 0; in lodepng_compute_color_stats()
4377 …if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per chan… in lodepng_compute_color_stats()
4379 else if(a == 0 && !stats->alpha && !stats->key) { in lodepng_compute_color_stats()
4380 stats->key = 1; in lodepng_compute_color_stats()
4381 stats->key_r = r; in lodepng_compute_color_stats()
4382 stats->key_g = g; in lodepng_compute_color_stats()
4383 stats->key_b = b; in lodepng_compute_color_stats()
4385 else if(a == 255 && stats->key && matchkey) { in lodepng_compute_color_stats()
4387 stats->alpha = 1; in lodepng_compute_color_stats()
4388 stats->key = 0; in lodepng_compute_color_stats()
4390 …if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per chan… in lodepng_compute_color_stats()
4395 if(!color_tree_has(&tree, r, g, b, a)) { in lodepng_compute_color_stats()
4396 error = color_tree_add(&tree, r, g, b, a, stats->numcolors); in lodepng_compute_color_stats()
4398 if(stats->numcolors < 256) { in lodepng_compute_color_stats()
4399 unsigned char * p = stats->palette; in lodepng_compute_color_stats()
4400 unsigned n = stats->numcolors; in lodepng_compute_color_stats()
4401 p[n * 4 + 0] = r; in lodepng_compute_color_stats()
4406 ++stats->numcolors; in lodepng_compute_color_stats()
4407 numcolors_done = stats->numcolors >= maxnumcolors; in lodepng_compute_color_stats()
4414 if(stats->key && !stats->alpha) { in lodepng_compute_color_stats()
4416 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); in lodepng_compute_color_stats()
4417 if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) { in lodepng_compute_color_stats()
4419 stats->alpha = 1; in lodepng_compute_color_stats()
4420 stats->key = 0; in lodepng_compute_color_stats()
4422 …if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per chan… in lodepng_compute_color_stats()
4427 /*make the stats's key always 16-bit for consistency - repeat each byte twice*/ in lodepng_compute_color_stats()
4428 stats->key_r += (stats->key_r << 8); in lodepng_compute_color_stats()
4429 stats->key_g += (stats->key_g << 8); in lodepng_compute_color_stats()
4430 stats->key_b += (stats->key_b << 8); in lodepng_compute_color_stats()
4439 …olor to the color stats. The stats must already have been inited. The color must be given as 16-bit
4440 (with 2 bytes repeating for 8-bit and 65535 for opaque alpha channel). This function is expensive, …
4443 unsigned r, unsigned g, unsigned b, unsigned a) in lodepng_color_stats_add() argument
4449 image[0] = r >> 8; in lodepng_color_stats_add()
4450 image[1] = r; in lodepng_color_stats_add()
4479 size_t numpixels = stats->numpixels; in auto_choose_color()
4482 unsigned alpha = stats->alpha; in auto_choose_color()
4483 unsigned key = stats->key; in auto_choose_color()
4484 unsigned bits = stats->bits; in auto_choose_color()
4486 mode_out->key_defined = 0; in auto_choose_color()
4491 if(bits < 8) bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ in auto_choose_color()
4494 gray_ok = !stats->colored; in auto_choose_color()
4495 if(!stats->allow_greyscale) gray_ok = 0; in auto_choose_color()
4498 n = stats->numcolors; in auto_choose_color()
4503 if(!stats->allow_palette) palette_ok = 0; in auto_choose_color()
4506 const unsigned char * p = stats->palette; in auto_choose_color()
4508 for(i = 0; i != stats->numcolors; ++i) { in auto_choose_color()
4513 mode_out->colortype = LCT_PALETTE; in auto_choose_color()
4514 mode_out->bitdepth = palettebits; in auto_choose_color()
4516 if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize in auto_choose_color()
4517 && mode_in->bitdepth == mode_out->bitdepth) { in auto_choose_color()
4519 …mode_cleanup(mode_out); /*clears palette, keeps the above set colortype and bitdepth fields as-is*/ in auto_choose_color()
4523 else { /*8-bit or 16-bit per channel*/ in auto_choose_color()
4524 mode_out->bitdepth = bits; in auto_choose_color()
4525 mode_out->colortype = alpha ? (gray_ok ? LCT_GREY_ALPHA : LCT_RGBA) in auto_choose_color()
4528 … unsigned mask = (1u << mode_out->bitdepth) - 1u; /*stats always uses 16-bit, mask converts it*/ in auto_choose_color()
4529 mode_out->key_r = stats->key_r & mask; in auto_choose_color()
4530 mode_out->key_g = stats->key_g & mask; in auto_choose_color()
4531 mode_out->key_b = stats->key_b & mask; in auto_choose_color()
4532 mode_out->key_defined = 1; in auto_choose_color()
4546 short pa = (b - c) < 0 ? -(b - c) : (b - c); in paethPredictor()
4547 short pb = (a - c) < 0 ? -(a - c) : (a - c); in paethPredictor()
4549 short pc = (a + b - c - c) < 0 ? -(a + b - c - c) : (a + b - c - c); in paethPredictor()
4575 w, h: width and height of non-interlaced image
4588 passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i]; in Adam7_getpassvalues()
4589 passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i]; in Adam7_getpassvalues()
4596 /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/ in Adam7_getpassvalues()
4617 LodePNGInfo * info = &state->info_png; in lodepng_inspect()
4619 CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/ in lodepng_inspect()
4622 …CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PN… in lodepng_inspect()
4632 …CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signatur… in lodepng_inspect()
4635 CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/ in lodepng_inspect()
4638 CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/ in lodepng_inspect()
4647 info->color.bitdepth = in[24]; in lodepng_inspect()
4648 info->color.colortype = (LodePNGColorType)in[25]; in lodepng_inspect()
4649 info->compression_method = in[26]; in lodepng_inspect()
4650 info->filter_method = in[27]; in lodepng_inspect()
4651 info->interlace_method = in[28]; in lodepng_inspect()
4656 if(width == 0 || height == 0) CERROR_RETURN_ERROR(state->error, 93); in lodepng_inspect()
4658 state->error = checkColorValidity(info->color.colortype, info->color.bitdepth); in lodepng_inspect()
4659 if(state->error) return state->error; in lodepng_inspect()
4661 if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32); in lodepng_inspect()
4663 if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33); in lodepng_inspect()
4665 if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34); in lodepng_inspect()
4667 if(!state->decoder.ignore_crc) { in lodepng_inspect()
4671 CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/ in lodepng_inspect()
4675 return state->error; in lodepng_inspect()
4924 … recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth])); in unfilterScanline()
4933 /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/ in unfilterScanline()
4960 size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u; in unfilter()
4978 out: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h
5033 …to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image bu… in removePaddingBits()
5038 only useful if (ilinebits - olinebits) is a value in the range 1..7 in removePaddingBits()
5041 size_t diff = ilinebits - olinebits; in removePaddingBits()
5060 …This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG'… in postProcessScanlines()
5066 unsigned bpp = lodepng_get_bpp(&info_png->color); in postProcessScanlines()
5069 if(info_png->interlace_method == 0) { in postProcessScanlines()
5105 color->palettesize = chunkLength / 3u; in readChunk_PLTE()
5106 …if(color->palettesize == 0 || color->palettesize > 256) return 38; /*error: palette too small or b… in readChunk_PLTE()
5108 if(!color->palette && color->palettesize) { in readChunk_PLTE()
5109 color->palettesize = 0; in readChunk_PLTE()
5113 for(i = 0; i != color->palettesize; ++i) { in readChunk_PLTE()
5114 color->palette[4 * i + 0] = data[pos++]; /*R*/ in readChunk_PLTE()
5115 color->palette[4 * i + 1] = data[pos++]; /*G*/ in readChunk_PLTE()
5116 color->palette[4 * i + 2] = data[pos++]; /*B*/ in readChunk_PLTE()
5117 color->palette[4 * i + 3] = 255; /*alpha*/ in readChunk_PLTE()
5126 if(color->colortype == LCT_PALETTE) { in readChunk_tRNS()
5128 if(chunkLength > color->palettesize) return 39; in readChunk_tRNS()
5130 for(i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i]; in readChunk_tRNS()
5132 else if(color->colortype == LCT_GREY) { in readChunk_tRNS()
5136 color->key_defined = 1; in readChunk_tRNS()
5137 color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1]; in readChunk_tRNS()
5139 else if(color->colortype == LCT_RGB) { in readChunk_tRNS()
5143 color->key_defined = 1; in readChunk_tRNS()
5144 color->key_r = 256u * data[0] + data[1]; in readChunk_tRNS()
5145 color->key_g = 256u * data[2] + data[3]; in readChunk_tRNS()
5146 color->key_b = 256u * data[4] + data[5]; in readChunk_tRNS()
5158 if(info->color.colortype == LCT_PALETTE) { in readChunk_bKGD()
5163 if(data[0] >= info->color.palettesize) return 103; in readChunk_bKGD()
5165 info->background_defined = 1; in readChunk_bKGD()
5166 info->background_r = info->background_g = info->background_b = data[0]; in readChunk_bKGD()
5168 else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) { in readChunk_bKGD()
5173 info->background_defined = 1; in readChunk_bKGD()
5174 info->background_r = info->background_g = info->background_b = 256u * data[0] + data[1]; in readChunk_bKGD()
5176 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) { in readChunk_bKGD()
5181 info->background_defined = 1; in readChunk_bKGD()
5182 info->background_r = 256u * data[0] + data[1]; in readChunk_bKGD()
5183 info->background_g = 256u * data[2] + data[3]; in readChunk_bKGD()
5184 info->background_b = 256u * data[4] + data[5]; in readChunk_bKGD()
5213 length = (unsigned)(chunkLength < string2_begin ? 0 : chunkLength - string2_begin); in readChunk_tEXt()
5238 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings; in readChunk_zTXt()
5261 length = (unsigned)chunkLength - string2_begin; in readChunk_zTXt()
5262 zlibsettings.max_output_size = decoder->max_text_size; in readChunk_zTXt()
5266 /*error: compressed text larger than decoder->max_text_size*/ in readChunk_zTXt()
5287 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings; in readChunk_iTXt()
5340 length = (unsigned)chunkLength < begin ? 0 : (unsigned)chunkLength - begin; in readChunk_iTXt()
5345 zlibsettings.max_output_size = decoder->max_text_size; in readChunk_iTXt()
5349 /*error: compressed text larger than decoder->max_text_size*/ in readChunk_iTXt()
5372 info->time_defined = 1; in readChunk_tIME()
5373 info->time.year = 256u * data[0] + data[1]; in readChunk_tIME()
5374 info->time.month = data[2]; in readChunk_tIME()
5375 info->time.day = data[3]; in readChunk_tIME()
5376 info->time.hour = data[4]; in readChunk_tIME()
5377 info->time.minute = data[5]; in readChunk_tIME()
5378 info->time.second = data[6]; in readChunk_tIME()
5387 info->phys_defined = 1; in readChunk_pHYs()
5388 info->phys_x = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3]; in readChunk_pHYs()
5389 info->phys_y = 16777216u * data[4] + 65536u * data[5] + 256u * data[6] + data[7]; in readChunk_pHYs()
5390 info->phys_unit = data[8]; in readChunk_pHYs()
5399 info->gama_defined = 1; in readChunk_gAMA()
5400 info->gama_gamma = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3]; in readChunk_gAMA()
5409 info->chrm_defined = 1; in readChunk_cHRM()
5410 info->chrm_white_x = 16777216u * data[ 0] + 65536u * data[ 1] + 256u * data[ 2] + data[ 3]; in readChunk_cHRM()
5411 info->chrm_white_y = 16777216u * data[ 4] + 65536u * data[ 5] + 256u * data[ 6] + data[ 7]; in readChunk_cHRM()
5412 info->chrm_red_x = 16777216u * data[ 8] + 65536u * data[ 9] + 256u * data[10] + data[11]; in readChunk_cHRM()
5413 info->chrm_red_y = 16777216u * data[12] + 65536u * data[13] + 256u * data[14] + data[15]; in readChunk_cHRM()
5414 info->chrm_green_x = 16777216u * data[16] + 65536u * data[17] + 256u * data[18] + data[19]; in readChunk_cHRM()
5415 info->chrm_green_y = 16777216u * data[20] + 65536u * data[21] + 256u * data[22] + data[23]; in readChunk_cHRM()
5416 info->chrm_blue_x = 16777216u * data[24] + 65536u * data[25] + 256u * data[26] + data[27]; in readChunk_cHRM()
5417 info->chrm_blue_y = 16777216u * data[28] + 65536u * data[29] + 256u * data[30] + data[31]; in readChunk_cHRM()
5426 info->srgb_defined = 1; in readChunk_sRGB()
5427 info->srgb_intent = data[0]; in readChunk_sRGB()
5439 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings; in readChunk_iCCP()
5443 info->iccp_defined = 1; in readChunk_iCCP()
5444 if(info->iccp_name) lodepng_clear_icc(info); in readChunk_iCCP()
5450 info->iccp_name = (char *)lodepng_malloc(length + 1); in readChunk_iCCP()
5451 if(!info->iccp_name) return 83; /*alloc fail*/ in readChunk_iCCP()
5453 info->iccp_name[length] = 0; in readChunk_iCCP()
5454 for(i = 0; i != length; ++i) info->iccp_name[i] = (char)data[i]; in readChunk_iCCP()
5461 length = (unsigned)chunkLength - string2_begin; in readChunk_iCCP()
5462 zlibsettings.max_output_size = decoder->max_icc_size; in readChunk_iCCP()
5463 error = zlib_decompress(&info->iccp_profile, &size, 0, in readChunk_iCCP()
5466 /*error: ICC profile larger than decoder->max_icc_size*/ in readChunk_iCCP()
5468 info->iccp_profile_size = (unsigned)size; in readChunk_iCCP()
5469 if(!error && !info->iccp_profile_size) error = 100; /*invalid ICC profile size*/ in readChunk_iCCP()
5476 unsigned bitdepth = (info->color.colortype == LCT_PALETTE) ? 8 : info->color.bitdepth; in readChunk_sBIT()
5477 if(info->color.colortype == LCT_GREY) { in readChunk_sBIT()
5481 info->sbit_defined = 1; in readChunk_sBIT()
5482 …info->sbit_r = info->sbit_g = info->sbit_b = data[0]; /*setting g and b is not required, but sensi… in readChunk_sBIT()
5484 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_PALETTE) { in readChunk_sBIT()
5489 info->sbit_defined = 1; in readChunk_sBIT()
5490 info->sbit_r = data[0]; in readChunk_sBIT()
5491 info->sbit_g = data[1]; in readChunk_sBIT()
5492 info->sbit_b = data[2]; in readChunk_sBIT()
5494 else if(info->color.colortype == LCT_GREY_ALPHA) { in readChunk_sBIT()
5499 info->sbit_defined = 1; in readChunk_sBIT()
5500 …info->sbit_r = info->sbit_g = info->sbit_b = data[0]; /*setting g and b is not required, but sensi… in readChunk_sBIT()
5501 info->sbit_a = data[1]; in readChunk_sBIT()
5503 else if(info->color.colortype == LCT_RGBA) { in readChunk_sBIT()
5508 info->sbit_defined = 1; in readChunk_sBIT()
5509 info->sbit_r = data[0]; in readChunk_sBIT()
5510 info->sbit_g = data[1]; in readChunk_sBIT()
5511 info->sbit_b = data[2]; in readChunk_sBIT()
5512 info->sbit_a = data[3]; in readChunk_sBIT()
5532 if(chunkLength + 12 > insize - pos) return 30; in lodepng_inspect_chunk()
5535 error = readChunk_PLTE(&state->info_png.color, data, chunkLength); in lodepng_inspect_chunk()
5538 error = readChunk_tRNS(&state->info_png.color, data, chunkLength); in lodepng_inspect_chunk()
5542 error = readChunk_bKGD(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5545 error = readChunk_tEXt(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5548 error = readChunk_zTXt(&state->info_png, &state->decoder, data, chunkLength); in lodepng_inspect_chunk()
5551 error = readChunk_iTXt(&state->info_png, &state->decoder, data, chunkLength); in lodepng_inspect_chunk()
5554 error = readChunk_tIME(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5557 error = readChunk_pHYs(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5560 error = readChunk_gAMA(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5563 error = readChunk_cHRM(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5566 error = readChunk_sRGB(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5569 error = readChunk_iCCP(&state->info_png, &state->decoder, data, chunkLength); in lodepng_inspect_chunk()
5572 error = readChunk_sBIT(&state->info_png, data, chunkLength); in lodepng_inspect_chunk()
5580 if(!error && !unhandled && !state->decoder.ignore_crc) { in lodepng_inspect_chunk()
5611 …state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameter… in decodeGeneric()
5612 if(state->error) return; in decodeGeneric()
5614 if(lodepng_pixel_overflow(*w, *h, &state->info_png.color, &state->info_raw)) { in decodeGeneric()
5615 CERROR_RETURN(state->error, 92); /*overflow possible due to amount of pixels*/ in decodeGeneric()
5620 if(!idat) CERROR_RETURN(state->error, 83); /*alloc fail*/ in decodeGeneric()
5626 while(!IEND && !state->error) { in decodeGeneric()
5629 size_t pos = (size_t)(chunk - in); in decodeGeneric()
5633 if(state->decoder.ignore_end) break; /*other errors may still happen though*/ in decodeGeneric()
5634 CERROR_BREAK(state->error, 30); in decodeGeneric()
5641 if(state->decoder.ignore_end) break; /*other errors may still happen though*/ in decodeGeneric()
5642 CERROR_BREAK(state->error, 63); in decodeGeneric()
5646 …CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk (or… in decodeGeneric()
5656 if(lodepng_addofl(idatsize, chunkLength, &newsize)) CERROR_BREAK(state->error, 95); in decodeGeneric()
5657 if(newsize > insize) CERROR_BREAK(state->error, 95); in decodeGeneric()
5670 state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength); in decodeGeneric()
5671 if(state->error) break; in decodeGeneric()
5680 state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength); in decodeGeneric()
5681 if(state->error) break; in decodeGeneric()
5686 state->error = readChunk_bKGD(&state->info_png, data, chunkLength); in decodeGeneric()
5687 if(state->error) break; in decodeGeneric()
5691 if(state->decoder.read_text_chunks) { in decodeGeneric()
5692 state->error = readChunk_tEXt(&state->info_png, data, chunkLength); in decodeGeneric()
5693 if(state->error) break; in decodeGeneric()
5698 if(state->decoder.read_text_chunks) { in decodeGeneric()
5699 state->error = readChunk_zTXt(&state->info_png, &state->decoder, data, chunkLength); in decodeGeneric()
5700 if(state->error) break; in decodeGeneric()
5705 if(state->decoder.read_text_chunks) { in decodeGeneric()
5706 state->error = readChunk_iTXt(&state->info_png, &state->decoder, data, chunkLength); in decodeGeneric()
5707 if(state->error) break; in decodeGeneric()
5711 state->error = readChunk_tIME(&state->info_png, data, chunkLength); in decodeGeneric()
5712 if(state->error) break; in decodeGeneric()
5715 state->error = readChunk_pHYs(&state->info_png, data, chunkLength); in decodeGeneric()
5716 if(state->error) break; in decodeGeneric()
5719 state->error = readChunk_gAMA(&state->info_png, data, chunkLength); in decodeGeneric()
5720 if(state->error) break; in decodeGeneric()
5723 state->error = readChunk_cHRM(&state->info_png, data, chunkLength); in decodeGeneric()
5724 if(state->error) break; in decodeGeneric()
5727 state->error = readChunk_sRGB(&state->info_png, data, chunkLength); in decodeGeneric()
5728 if(state->error) break; in decodeGeneric()
5731 state->error = readChunk_iCCP(&state->info_png, &state->decoder, data, chunkLength); in decodeGeneric()
5732 if(state->error) break; in decodeGeneric()
5735 state->error = readChunk_sBIT(&state->info_png, data, chunkLength); in decodeGeneric()
5736 if(state->error) break; in decodeGeneric()
5741 if(!state->decoder.ignore_critical && !lodepng_chunk_ancillary(chunk)) { in decodeGeneric()
5742 CERROR_BREAK(state->error, 69); in decodeGeneric()
5747 if(state->decoder.remember_unknown_chunks) { in decodeGeneric()
5748 … state->error = lodepng_chunk_append(&state->info_png.unknown_chunks_data[critical_pos - 1], in decodeGeneric()
5749 … &state->info_png.unknown_chunks_size[critical_pos - 1], chunk); in decodeGeneric()
5750 if(state->error) break; in decodeGeneric()
5755 … if(!state->decoder.ignore_crc && !unknown) { /*check CRC if wanted, only on known chunk types*/ in decodeGeneric()
5756 if(lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/ in decodeGeneric()
5762 …if(!state->error && state->info_png.color.colortype == LCT_PALETTE && !state->info_png.color.palet… in decodeGeneric()
5763 state->error = 106; /* error: PNG file must have PLTE chunk if color type is palette */ in decodeGeneric()
5766 if(!state->error) { in decodeGeneric()
5769 if(state->info_png.interlace_method == 0) { in decodeGeneric()
5770 unsigned bpp = lodepng_get_bpp(&state->info_png.color); in decodeGeneric()
5774 unsigned bpp = lodepng_get_bpp(&state->info_png.color); in decodeGeneric()
5775 /*Adam-7 interlaced: expected size is the sum of the 7 sub-images sizes*/ in decodeGeneric()
5786 state->error = zlib_decompress(&scanlines, &scanlines_size, expected_size, idat, idatsize, in decodeGeneric()
5787 &state->decoder.zlibsettings); in decodeGeneric()
5789 …if(!state->error && scanlines_size != expected_size) state->error = 91; /*decompressed size doesn'… in decodeGeneric()
5792 if(!state->error) { in decodeGeneric()
5796 outsize = decoded->data_size; in decodeGeneric()
5798 else state->error = 83; /*alloc fail*/ in decodeGeneric()
5800 if(!state->error) { in decodeGeneric()
5802 lodepng_memset(decoded->data, 0, outsize); in decodeGeneric()
5803 state->error = postProcessScanlines(decoded->data, scanlines, *w, *h, &state->info_png); in decodeGeneric()
5814 if(state->error) return state->error; in lodepng_decode()
5815 …if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.co… in lodepng_decode()
5819 if(!state->decoder.color_convert) { in lodepng_decode()
5820 state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color); in lodepng_decode()
5821 if(state->error) return state->error; in lodepng_decode()
5828 from grayscale input color type, to 8-bit grayscale or grayscale with alpha"*/ in lodepng_decode()
5829 if(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA) in lodepng_decode()
5830 && !(state->info_raw.bitdepth == 8)) { in lodepng_decode()
5836 state->error = 83; /*alloc fail*/ in lodepng_decode()
5839 state->error = lodepng_convert(new_buf->data, old_buf->data, in lodepng_decode()
5840 &state->info_raw, &state->info_png.color, *w, *h); in lodepng_decode()
5842 if (state->error) { in lodepng_decode()
5851 return state->error; in lodepng_decode()
5911 settings->color_convert = 1; in lodepng_decoder_settings_init()
5913 settings->read_text_chunks = 1; in lodepng_decoder_settings_init()
5914 settings->remember_unknown_chunks = 0; in lodepng_decoder_settings_init()
5915 settings->max_text_size = 16777216; in lodepng_decoder_settings_init()
5916 …settings->max_icc_size = 16777216; /* 16MB is much more than enough for any reasonable ICC profile… in lodepng_decoder_settings_init()
5918 settings->ignore_crc = 0; in lodepng_decoder_settings_init()
5919 settings->ignore_critical = 0; in lodepng_decoder_settings_init()
5920 settings->ignore_end = 0; in lodepng_decoder_settings_init()
5921 lodepng_decompress_settings_init(&settings->zlibsettings); in lodepng_decoder_settings_init()
5931 lodepng_decoder_settings_init(&state->decoder); in lodepng_state_init()
5934 lodepng_encoder_settings_init(&state->encoder); in lodepng_state_init()
5936 lodepng_color_mode_init(&state->info_raw); in lodepng_state_init()
5937 lodepng_info_init(&state->info_png); in lodepng_state_init()
5938 state->error = 1; in lodepng_state_init()
5943 lodepng_color_mode_cleanup(&state->info_raw); in lodepng_state_cleanup()
5944 lodepng_info_cleanup(&state->info_png); in lodepng_state_cleanup()
5951 lodepng_color_mode_init(&dest->info_raw); in lodepng_state_copy()
5952 lodepng_info_init(&dest->info_png); in lodepng_state_copy()
5953 dest->error = lodepng_color_mode_copy(&dest->info_raw, &source->info_raw); in lodepng_state_copy()
5954 if(dest->error) return; in lodepng_state_copy()
5955 dest->error = lodepng_info_copy(&dest->info_png, &source->info_png); in lodepng_state_copy()
5956 if(dest->error) return; in lodepng_state_copy()
5970 size_t pos = out->size; in writeSignature()
5973 if(!ucvector_resize(out, out->size + 8)) return 83; /*alloc fail*/ in writeSignature()
5974 lodepng_memcpy(out->data + pos, signature, 8); in writeSignature()
6003 if(info->palettesize == 0 || info->palettesize > 256) { in addChunk_PLTE()
6004 return 68; /*invalid palette size, it is only allowed to be 1-256*/ in addChunk_PLTE()
6007 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, info->palettesize * 3, "PLTE")); in addChunk_PLTE()
6009 for(i = 0; i != info->palettesize; ++i) { in addChunk_PLTE()
6011 chunk[j++] = info->palette[i * 4 + 0]; in addChunk_PLTE()
6012 chunk[j++] = info->palette[i * 4 + 1]; in addChunk_PLTE()
6013 chunk[j++] = info->palette[i * 4 + 2]; in addChunk_PLTE()
6024 if(info->colortype == LCT_PALETTE) { in addChunk_tRNS()
6025 size_t i, amount = info->palettesize; in addChunk_tRNS()
6027 for(i = info->palettesize; i != 0; --i) { in addChunk_tRNS()
6028 if(info->palette[4 * (i - 1) + 3] != 255) break; in addChunk_tRNS()
6029 --amount; in addChunk_tRNS()
6034 for(i = 0; i != amount; ++i) chunk[8 + i] = info->palette[4 * i + 3]; in addChunk_tRNS()
6037 else if(info->colortype == LCT_GREY) { in addChunk_tRNS()
6038 if(info->key_defined) { in addChunk_tRNS()
6040 chunk[8] = (unsigned char)(info->key_r >> 8); in addChunk_tRNS()
6041 chunk[9] = (unsigned char)(info->key_r & 255); in addChunk_tRNS()
6044 else if(info->colortype == LCT_RGB) { in addChunk_tRNS()
6045 if(info->key_defined) { in addChunk_tRNS()
6047 chunk[8] = (unsigned char)(info->key_r >> 8); in addChunk_tRNS()
6048 chunk[9] = (unsigned char)(info->key_r & 255); in addChunk_tRNS()
6049 chunk[10] = (unsigned char)(info->key_g >> 8); in addChunk_tRNS()
6050 chunk[11] = (unsigned char)(info->key_g & 255); in addChunk_tRNS()
6051 chunk[12] = (unsigned char)(info->key_b >> 8); in addChunk_tRNS()
6052 chunk[13] = (unsigned char)(info->key_b & 255); in addChunk_tRNS()
6174 if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) { in addChunk_bKGD()
6176 chunk[8] = (unsigned char)(info->background_r >> 8); in addChunk_bKGD()
6177 chunk[9] = (unsigned char)(info->background_r & 255); in addChunk_bKGD()
6179 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) { in addChunk_bKGD()
6181 chunk[8] = (unsigned char)(info->background_r >> 8); in addChunk_bKGD()
6182 chunk[9] = (unsigned char)(info->background_r & 255); in addChunk_bKGD()
6183 chunk[10] = (unsigned char)(info->background_g >> 8); in addChunk_bKGD()
6184 chunk[11] = (unsigned char)(info->background_g & 255); in addChunk_bKGD()
6185 chunk[12] = (unsigned char)(info->background_b >> 8); in addChunk_bKGD()
6186 chunk[13] = (unsigned char)(info->background_b & 255); in addChunk_bKGD()
6188 else if(info->color.colortype == LCT_PALETTE) { in addChunk_bKGD()
6190 chunk[8] = (unsigned char)(info->background_r & 255); /*palette index*/ in addChunk_bKGD()
6200 chunk[8] = (unsigned char)(time->year >> 8); in addChunk_tIME()
6201 chunk[9] = (unsigned char)(time->year & 255); in addChunk_tIME()
6202 chunk[10] = (unsigned char)time->month; in addChunk_tIME()
6203 chunk[11] = (unsigned char)time->day; in addChunk_tIME()
6204 chunk[12] = (unsigned char)time->hour; in addChunk_tIME()
6205 chunk[13] = (unsigned char)time->minute; in addChunk_tIME()
6206 chunk[14] = (unsigned char)time->second; in addChunk_tIME()
6215 lodepng_set32bitInt(chunk + 8, info->phys_x); in addChunk_pHYs()
6216 lodepng_set32bitInt(chunk + 12, info->phys_y); in addChunk_pHYs()
6217 chunk[16] = info->phys_unit; in addChunk_pHYs()
6226 lodepng_set32bitInt(chunk + 8, info->gama_gamma); in addChunk_gAMA()
6235 lodepng_set32bitInt(chunk + 8, info->chrm_white_x); in addChunk_cHRM()
6236 lodepng_set32bitInt(chunk + 12, info->chrm_white_y); in addChunk_cHRM()
6237 lodepng_set32bitInt(chunk + 16, info->chrm_red_x); in addChunk_cHRM()
6238 lodepng_set32bitInt(chunk + 20, info->chrm_red_y); in addChunk_cHRM()
6239 lodepng_set32bitInt(chunk + 24, info->chrm_green_x); in addChunk_cHRM()
6240 lodepng_set32bitInt(chunk + 28, info->chrm_green_y); in addChunk_cHRM()
6241 lodepng_set32bitInt(chunk + 32, info->chrm_blue_x); in addChunk_cHRM()
6242 lodepng_set32bitInt(chunk + 36, info->chrm_blue_y); in addChunk_cHRM()
6249 unsigned char data = info->srgb_intent; in addChunk_sRGB()
6259 size_t keysize = lodepng_strlen(info->iccp_name); in addChunk_iCCP()
6263 info->iccp_profile, info->iccp_profile_size, zlibsettings); in addChunk_iCCP()
6269 lodepng_memcpy(chunk + 8, info->iccp_name, keysize); in addChunk_iCCP()
6282 unsigned bitdepth = (info->color.colortype == LCT_PALETTE) ? 8 : info->color.bitdepth; in addChunk_sBIT()
6284 if(info->color.colortype == LCT_GREY) { in addChunk_sBIT()
6285 if(info->sbit_r == 0 || info->sbit_r > bitdepth) return 115; in addChunk_sBIT()
6287 chunk[8] = info->sbit_r; in addChunk_sBIT()
6289 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_PALETTE) { in addChunk_sBIT()
6290 if(info->sbit_r == 0 || info->sbit_g == 0 || info->sbit_b == 0) return 115; in addChunk_sBIT()
6291 … if(info->sbit_r > bitdepth || info->sbit_g > bitdepth || info->sbit_b > bitdepth) return 115; in addChunk_sBIT()
6293 chunk[8] = info->sbit_r; in addChunk_sBIT()
6294 chunk[9] = info->sbit_g; in addChunk_sBIT()
6295 chunk[10] = info->sbit_b; in addChunk_sBIT()
6297 else if(info->color.colortype == LCT_GREY_ALPHA) { in addChunk_sBIT()
6298 if(info->sbit_r == 0 || info->sbit_a == 0) return 115; in addChunk_sBIT()
6299 if(info->sbit_r > bitdepth || info->sbit_a > bitdepth) return 115; in addChunk_sBIT()
6301 chunk[8] = info->sbit_r; in addChunk_sBIT()
6302 chunk[9] = info->sbit_a; in addChunk_sBIT()
6304 else if(info->color.colortype == LCT_RGBA) { in addChunk_sBIT()
6305 if(info->sbit_r == 0 || info->sbit_g == 0 || info->sbit_b == 0 || info->sbit_a == 0 || in addChunk_sBIT()
6306 info->sbit_r > bitdepth || info->sbit_g > bitdepth || in addChunk_sBIT()
6307 info->sbit_b > bitdepth || info->sbit_a > bitdepth) { in addChunk_sBIT()
6311 chunk[8] = info->sbit_r; in addChunk_sBIT()
6312 chunk[9] = info->sbit_g; in addChunk_sBIT()
6313 chunk[10] = info->sbit_b; in addChunk_sBIT()
6314 chunk[11] = info->sbit_a; in addChunk_sBIT()
6332 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth]; in filterScanline()
6336 for(i = 0; i != length; ++i) out[i] = scanline[i] - prevline[i]; in filterScanline()
6344 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - (prevline[i] >> 1); in filterScanline()
6345 …for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]… in filterScanline()
6349 … for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - (scanline[i - bytewidth] >> 1); in filterScanline()
6355 for(i = 0; i != bytewidth; ++i) out[i] = (scanline[i] - prevline[i]); in filterScanline()
6357 …out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidt… in filterScanline()
6362 /*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/ in filterScanline()
6363 … for(i = bytewidth; i < length; ++i) out[i] = (scanline[i] - scanline[i - bytewidth]); in filterScanline()
6403 /* approximate i*log2(i): l is integer logarithm, ((i - (1u << l)) << 1u) in ilog2i()
6405 return i * l + ((i - (((size_t)1) << l)) << 1u); in ilog2i()
6419 size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u; in filter()
6426 LodePNGFilterStrategy strategy = settings->filter_strategy; in filter()
6441 if(settings->filter_palette_zero && in filter()
6442 (color->colortype == LCT_PALETTE || color->bitdepth < 8)) strategy = LFS_ZERO; in filter()
6484 sum += s < 128 ? s : (255U - s); in filter()
6549 unsigned char type = settings->predefined_filters[y]; in filter()
6565 lodepng_memcpy(&zlibsettings, &settings->zlibsettings, sizeof(LodePNGCompressSettings)); in filter()
6614 size_t diff = olinebits - ilinebits; in addPaddingBits()
6622 /*obp += diff; --> no, fill in some value in the padding bits too, to avoid in addPaddingBits()
6629 in: non-interlaced image with size w*h
6630 out: the same pixels, but re-ordered according to PNG's Adam7 interlacing, with
6687 …This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced… in preProcessScanlines()
6691 unsigned bpp = lodepng_get_bpp(&info_png->color); in preProcessScanlines()
6694 if(info_png->interlace_method == 0) { in preProcessScanlines()
6706 error = filter(*out, padded, w, h, &info_png->color, settings); in preProcessScanlines()
6712 error = filter(*out, in, w, h, &info_png->color, settings); in preProcessScanlines()
6736 …ned char * padded = (unsigned char *)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[i]); in preProcessScanlines()
6741 passw[i], passh[i], &info_png->color, settings); in preProcessScanlines()
6746 passw[i], passh[i], &info_png->color, settings); in preProcessScanlines()
6763 while((size_t)(inchunk - data) < datasize) { in addUnknownChunks()
6764 CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk)); in addUnknownChunks()
6765 out->allocsize = out->size; /*fix the allocsize again*/ in addUnknownChunks()
6774 It is a gray profile if bytes 16-19 are "GRAY", rgb profile if bytes 16-19 in isGrayICCProfile()
6778 requires using a non-gray color model if there is an ICC profile with "RGB " in isGrayICCProfile()
6783 return profile[16] == 'G' && profile[17] == 'R' && profile[18] == 'A' && profile[19] == 'Y'; in isGrayICCProfile()
6790 return profile[16] == 'R' && profile[17] == 'G' && profile[18] == 'B' && profile[19] == ' '; in isRGBICCProfile()
6802 const LodePNGInfo * info_png = &state->info_png; in lodepng_encode()
6811 state->error = 0; in lodepng_encode()
6814 if((info_png->color.colortype == LCT_PALETTE || state->encoder.force_palette) in lodepng_encode()
6815 && (info_png->color.palettesize == 0 || info_png->color.palettesize > 256)) { in lodepng_encode()
6820 state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ in lodepng_encode()
6823 if(state->encoder.zlibsettings.btype > 2) { in lodepng_encode()
6824 state->error = 61; /*error: invalid btype*/ in lodepng_encode()
6827 if(info_png->interlace_method > 1) { in lodepng_encode()
6828 state->error = 71; /*error: invalid interlace mode*/ in lodepng_encode()
6831 state->error = checkColorValidity(info_png->color.colortype, info_png->color.bitdepth); in lodepng_encode()
6832 if(state->error) goto cleanup; /*error: invalid color type given*/ in lodepng_encode()
6833 state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); in lodepng_encode()
6834 if(state->error) goto cleanup; /*error: invalid color type given*/ in lodepng_encode()
6837 lodepng_info_copy(&info, &state->info_png); in lodepng_encode()
6838 if(state->encoder.auto_convert) { in lodepng_encode()
6843 if(info_png->iccp_defined && in lodepng_encode()
6844 isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { in lodepng_encode()
6849 if(info_png->iccp_defined && in lodepng_encode()
6850 isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { in lodepng_encode()
6855 state->error = lodepng_compute_color_stats(&stats, image, w, h, &state->info_raw); in lodepng_encode()
6856 if(state->error) goto cleanup; in lodepng_encode()
6858 if(info_png->background_defined) { in lodepng_encode()
6860 unsigned r = 0, g = 0, b = 0; in lodepng_encode() local
6862 lodepng_convert_rgb(&r, &g, &b, in lodepng_encode()
6863 …info_png->background_r, info_png->background_g, info_png->background_b, &mode16, &info_png->color); in lodepng_encode()
6864 state->error = lodepng_color_stats_add(&stats, r, g, b, 65535); in lodepng_encode()
6865 if(state->error) goto cleanup; in lodepng_encode()
6868 state->error = auto_choose_color(&auto_color, &state->info_raw, &stats); in lodepng_encode()
6869 if(state->error) goto cleanup; in lodepng_encode()
6871 if(info_png->sbit_defined) { in lodepng_encode()
6877 … unsigned sbit_max = LODEPNG_MAX(LODEPNG_MAX(LODEPNG_MAX(info_png->sbit_r, info_png->sbit_g), in lodepng_encode()
6878 info_png->sbit_b), info_png->sbit_a); in lodepng_encode()
6879 unsigned equal = (!info_png->sbit_g || info_png->sbit_g == info_png->sbit_r) in lodepng_encode()
6880 && (!info_png->sbit_b || info_png->sbit_b == info_png->sbit_r) in lodepng_encode()
6881 && (!info_png->sbit_a || info_png->sbit_a == info_png->sbit_r); in lodepng_encode()
6889 /*going from 8-bit RGB to palette (or 16-bit as long as sbit_max <= 8) is possible in lodepng_encode()
6890 since both are 8-bit RGB for sBIT's purposes*/ in lodepng_encode()
6895 /*going from 8-bit RGBA to palette is also ok but only if sbit_a is exactly 8*/ in lodepng_encode()
6897 info_png->sbit_a == 8 && sbit_max <= 8) { in lodepng_encode()
6900 /*going from 16-bit RGB(A) to 8-bit RGB(A) is ok if all sbit values are <= 8*/ in lodepng_encode()
6910 … info_png->sbit_r < auto_color.bitdepth, because specifically for alpha, non-presence of in lodepng_encode()
6912 … than the bit depths set in the r, g and b sbit values, by how the PNG specification describes in lodepng_encode()
6915 equal && info_png->sbit_r == auto_color.bitdepth) { in lodepng_encode()
6920 if(state->encoder.force_palette) { in lodepng_encode()
6932 if(info_png->background_defined) { in lodepng_encode()
6934 …info_png->background_r, info_png->background_g, info_png->background_b, &info.color, &info_png->co… in lodepng_encode()
6935 state->error = 104; in lodepng_encode()
6943 if(info_png->iccp_defined) { in lodepng_encode()
6944 unsigned gray_icc = isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size); in lodepng_encode()
6945 unsigned rgb_icc = isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size); in lodepng_encode()
6948 state->error = 100; /* Disallowed profile color type for PNG */ in lodepng_encode()
6954 state->error = state->encoder.auto_convert ? 102 : 101; in lodepng_encode()
6959 if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) { in lodepng_encode()
6964 if(!converted && size) state->error = 83; /*alloc fail*/ in lodepng_encode()
6965 if(!state->error) { in lodepng_encode()
6966 state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h); in lodepng_encode()
6968 if(!state->error) { in lodepng_encode()
6969 … state->error = preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); in lodepng_encode()
6972 if(state->error) goto cleanup; in lodepng_encode()
6975 state->error = preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder); in lodepng_encode()
6976 if(state->error) goto cleanup; in lodepng_encode()
6984 state->error = writeSignature(&outv); in lodepng_encode()
6985 if(state->error) goto cleanup; in lodepng_encode()
6987 …state->error = addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlac… in lodepng_encode()
6988 if(state->error) goto cleanup; in lodepng_encode()
6992 … state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]); in lodepng_encode()
6993 if(state->error) goto cleanup; in lodepng_encode()
6997 state->error = addChunk_iCCP(&outv, &info, &state->encoder.zlibsettings); in lodepng_encode()
6998 if(state->error) goto cleanup; in lodepng_encode()
7001 state->error = addChunk_sRGB(&outv, &info); in lodepng_encode()
7002 if(state->error) goto cleanup; in lodepng_encode()
7005 state->error = addChunk_gAMA(&outv, &info); in lodepng_encode()
7006 if(state->error) goto cleanup; in lodepng_encode()
7009 state->error = addChunk_cHRM(&outv, &info); in lodepng_encode()
7010 if(state->error) goto cleanup; in lodepng_encode()
7012 if(info_png->sbit_defined) { in lodepng_encode()
7013 state->error = addChunk_sBIT(&outv, &info); in lodepng_encode()
7014 if(state->error) goto cleanup; in lodepng_encode()
7019 state->error = addChunk_PLTE(&outv, &info.color); in lodepng_encode()
7020 if(state->error) goto cleanup; in lodepng_encode()
7022 …if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT… in lodepng_encode()
7024 state->error = addChunk_PLTE(&outv, &info.color); in lodepng_encode()
7025 if(state->error) goto cleanup; in lodepng_encode()
7028 state->error = addChunk_tRNS(&outv, &info.color); in lodepng_encode()
7029 if(state->error) goto cleanup; in lodepng_encode()
7033 state->error = addChunk_bKGD(&outv, &info); in lodepng_encode()
7034 if(state->error) goto cleanup; in lodepng_encode()
7038 state->error = addChunk_pHYs(&outv, &info); in lodepng_encode()
7039 if(state->error) goto cleanup; in lodepng_encode()
7044 … state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]); in lodepng_encode()
7045 if(state->error) goto cleanup; in lodepng_encode()
7049 state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings); in lodepng_encode()
7050 if(state->error) goto cleanup; in lodepng_encode()
7054 state->error = addChunk_tIME(&outv, &info.time); in lodepng_encode()
7055 if(state->error) goto cleanup; in lodepng_encode()
7060 state->error = 66; /*text chunk too large*/ in lodepng_encode()
7064 state->error = 67; /*text chunk too small*/ in lodepng_encode()
7067 if(state->encoder.text_compression) { in lodepng_encode()
7068 …state->error = addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibs… in lodepng_encode()
7069 if(state->error) goto cleanup; in lodepng_encode()
7072 state->error = addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]); in lodepng_encode()
7073 if(state->error) goto cleanup; in lodepng_encode()
7077 if(state->encoder.add_id) { in lodepng_encode()
7089 …state->error = addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt tha… in lodepng_encode()
7090 if(state->error) goto cleanup; in lodepng_encode()
7096 state->error = 66; /*text chunk too large*/ in lodepng_encode()
7100 state->error = 67; /*text chunk too small*/ in lodepng_encode()
7103 state->error = addChunk_iTXt( in lodepng_encode()
7104 &outv, state->encoder.text_compression, in lodepng_encode()
7106 &state->encoder.zlibsettings); in lodepng_encode()
7107 if(state->error) goto cleanup; in lodepng_encode()
7112 … state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]); in lodepng_encode()
7113 if(state->error) goto cleanup; in lodepng_encode()
7116 state->error = addChunk_IEND(&outv); in lodepng_encode()
7117 if(state->error) goto cleanup; in lodepng_encode()
7129 return state->error; in lodepng_encode()
7183 lodepng_compress_settings_init(&settings->zlibsettings); in lodepng_encoder_settings_init()
7184 settings->filter_palette_zero = 1; in lodepng_encoder_settings_init()
7185 settings->filter_strategy = LFS_MINSUM; in lodepng_encoder_settings_init()
7186 settings->auto_convert = 1; in lodepng_encoder_settings_init()
7187 settings->force_palette = 0; in lodepng_encoder_settings_init()
7188 settings->predefined_filters = 0; in lodepng_encoder_settings_init()
7190 settings->add_id = 0; in lodepng_encoder_settings_init()
7191 settings->text_compression = 1; in lodepng_encoder_settings_init()
7288 return "empty input buffer given to decoder. Maybe caused by non-existing file?"; in lodepng_error_text()
7315 return "invalid window size given in the settings of the encoder (must be 0-32768)"; in lodepng_error_text()
7321 /*(2^31-1)*/ in lodepng_error_text()
7371 return "text chunk keyword too short or long: must have size 1-79"; in lodepng_error_text()