Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/
| 1669 | |
| 1670 | /*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/ |
| 1671 | static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, |
| 1672 | const unsigned char* data, size_t datapos, size_t dataend, |
| 1673 | const LodePNGCompressSettings* settings, int final) |
| 1674 | { |
| 1675 | unsigned error = 0; |
| 1676 | |
| 1677 | /* |
| 1678 | A block is compressed as follows: The PNG data is lz77 encoded, resulting in |
| 1679 | literal bytes and length/distance pairs. This is then huffman compressed with |
| 1680 | two huffman trees. One huffman tree is used for the lit and len values ("ll"), |
| 1681 | another huffman tree is used for the dist values ("d"). These two trees are |
| 1682 | stored using their code lengths, and to compress even more these code lengths |
| 1683 | are also run-length encoded and huffman compressed. This gives a huffman tree |
| 1684 | of code lengths "cl". The code lenghts used to describe this third tree are |
| 1685 | the code length code lengths ("clcl"). |
| 1686 | */ |
| 1687 | |
| 1688 | /*The lz77 encoded data, represented with integers since there will also be length and distance codes in it*/ |
| 1689 | uivector lz77_encoded; |
| 1690 | HuffmanTree tree_ll; /*tree for lit,len values*/ |
| 1691 | HuffmanTree tree_d; /*tree for distance codes*/ |
| 1692 | HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/ |
| 1693 | uivector frequencies_ll; /*frequency of lit,len codes*/ |
| 1694 | uivector frequencies_d; /*frequency of dist codes*/ |
| 1695 | uivector frequencies_cl; /*frequency of code length codes*/ |
| 1696 | uivector bitlen_lld; /*lit,len,dist code lenghts (int bits), literally (without repeat codes).*/ |
| 1697 | uivector bitlen_lld_e; /*bitlen_lld encoded with repeat codes (this is a rudemtary run length compression)*/ |
| 1698 | /*bitlen_cl is the code length code lengths ("clcl"). The bit lengths of codes to represent tree_cl |
| 1699 | (these are written as is in the file, it would be crazy to compress these using yet another huffman |
| 1700 | tree that needs to be represented by yet another set of code lengths)*/ |
| 1701 | uivector bitlen_cl; |
| 1702 | size_t datasize = dataend - datapos; |
| 1703 | |
| 1704 | /* |
| 1705 | Due to the huffman compression of huffman tree representations ("two levels"), there are some anologies: |
| 1706 | bitlen_lld is to tree_cl what data is to tree_ll and tree_d. |
| 1707 | bitlen_lld_e is to bitlen_lld what lz77_encoded is to data. |
| 1708 | bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded. |
| 1709 | */ |
| 1710 | |
| 1711 | unsigned BFINAL = final; |
| 1712 | size_t numcodes_ll, numcodes_d, i; |
| 1713 | unsigned HLIT, HDIST, HCLEN; |
| 1714 | |
| 1715 | uivector_init(&lz77_encoded); |
| 1716 | HuffmanTree_init(&tree_ll); |
| 1717 | HuffmanTree_init(&tree_d); |
| 1718 | HuffmanTree_init(&tree_cl); |
| 1719 | uivector_init(&frequencies_ll); |
| 1720 | uivector_init(&frequencies_d); |
| 1721 | uivector_init(&frequencies_cl); |
| 1722 | uivector_init(&bitlen_lld); |
| 1723 | uivector_init(&bitlen_lld_e); |
| 1724 | uivector_init(&bitlen_cl); |
| 1725 | |
| 1726 | /*This while loop never loops due to a break at the end, it is here to |
| 1727 | allow breaking out of it to the cleanup phase on error conditions.*/ |
| 1728 | while(!error) |
no test coverage detected