Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/
| 1730 | |
| 1731 | /*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/ |
| 1732 | static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, |
| 1733 | const unsigned char* data, size_t datapos, size_t dataend, |
| 1734 | const LodePNGCompressSettings* settings, unsigned final) |
| 1735 | { |
| 1736 | unsigned error = 0; |
| 1737 | |
| 1738 | /* |
| 1739 | A block is compressed as follows: The PNG data is lz77 encoded, resulting in |
| 1740 | literal bytes and length/distance pairs. This is then huffman compressed with |
| 1741 | two huffman trees. One huffman tree is used for the lit and len values ("ll"), |
| 1742 | another huffman tree is used for the dist values ("d"). These two trees are |
| 1743 | stored using their code lengths, and to compress even more these code lengths |
| 1744 | are also run-length encoded and huffman compressed. This gives a huffman tree |
| 1745 | of code lengths "cl". The code lenghts used to describe this third tree are |
| 1746 | the code length code lengths ("clcl"). |
| 1747 | */ |
| 1748 | |
| 1749 | /*The lz77 encoded data, represented with integers since there will also be length and distance codes in it*/ |
| 1750 | uivector lz77_encoded; |
| 1751 | HuffmanTree tree_ll; /*tree for lit,len values*/ |
| 1752 | HuffmanTree tree_d; /*tree for distance codes*/ |
| 1753 | HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/ |
| 1754 | uivector frequencies_ll; /*frequency of lit,len codes*/ |
| 1755 | uivector frequencies_d; /*frequency of dist codes*/ |
| 1756 | uivector frequencies_cl; /*frequency of code length codes*/ |
| 1757 | uivector bitlen_lld; /*lit,len,dist code lenghts (int bits), literally (without repeat codes).*/ |
| 1758 | uivector bitlen_lld_e; /*bitlen_lld encoded with repeat codes (this is a rudemtary run length compression)*/ |
| 1759 | /*bitlen_cl is the code length code lengths ("clcl"). The bit lengths of codes to represent tree_cl |
| 1760 | (these are written as is in the file, it would be crazy to compress these using yet another huffman |
| 1761 | tree that needs to be represented by yet another set of code lengths)*/ |
| 1762 | uivector bitlen_cl; |
| 1763 | size_t datasize = dataend - datapos; |
| 1764 | |
| 1765 | /* |
| 1766 | Due to the huffman compression of huffman tree representations ("two levels"), there are some anologies: |
| 1767 | bitlen_lld is to tree_cl what data is to tree_ll and tree_d. |
| 1768 | bitlen_lld_e is to bitlen_lld what lz77_encoded is to data. |
| 1769 | bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded. |
| 1770 | */ |
| 1771 | |
| 1772 | unsigned BFINAL = final; |
| 1773 | size_t numcodes_ll, numcodes_d, i; |
| 1774 | unsigned HLIT, HDIST, HCLEN; |
| 1775 | |
| 1776 | uivector_init(&lz77_encoded); |
| 1777 | HuffmanTree_init(&tree_ll); |
| 1778 | HuffmanTree_init(&tree_d); |
| 1779 | HuffmanTree_init(&tree_cl); |
| 1780 | uivector_init(&frequencies_ll); |
| 1781 | uivector_init(&frequencies_d); |
| 1782 | uivector_init(&frequencies_cl); |
| 1783 | uivector_init(&bitlen_lld); |
| 1784 | uivector_init(&bitlen_lld_e); |
| 1785 | uivector_init(&bitlen_cl); |
| 1786 | |
| 1787 | /*This while loop never loops due to a break at the end, it is here to |
| 1788 | allow breaking out of it to the cleanup phase on error conditions.*/ |
| 1789 | while(!error) |
no test coverage detected