Work on dynamic huffman coding.
This commit is contained in:
parent
7f5009fb5e
commit
a6e31c8d39
16 changed files with 456 additions and 95 deletions
|
@ -1,7 +1,7 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "HuffmanStream.h"
|
||||
|
||||
#include "BufferBitStream.h"
|
||||
|
||||
void testHuffmanCodeLengthTable()
|
||||
{
|
||||
|
@ -31,13 +31,115 @@ void testHuffmanCodeLengthTable()
|
|||
{
|
||||
std::cout << "Slot " << idx << " length " << compressed_lengths[idx] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void testLiteralsTable()
|
||||
{
|
||||
std::vector<unsigned char> lengths = {7,4,4,7,5,5,7,7,6,6,7,6,6,6,8,6,6,8,
|
||||
6,6,7,6,8,7,7,7,7,7,7,6,6,7,7,6,6,7,7,8,8,7,7,7,6,6,7,7,7,7,6,7,7,7,
|
||||
7,7,7,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,4,4,5,6,7,7,9,8,8,9,9,8,8,7,
|
||||
7,8,7,6,8,9,9,11,10,8,7,7,8,8,7,3,0,9,9,9,9,9,8,6,7,7,7,7,9,5,7,4,7,4,4,4,3,4,4,4,4,4,5,5,6};
|
||||
|
||||
HuffmanCodeLengthTable table;
|
||||
table.setInputLengthSequence(lengths, false);
|
||||
|
||||
table.buildCompressedLengthSequence();
|
||||
|
||||
auto compressed_sequence = table.getCompressedLengthSequence();
|
||||
for (auto entry : compressed_sequence)
|
||||
{
|
||||
std::cout << "Code " << entry.first << " extra bits " << entry.second << std::endl;
|
||||
}
|
||||
|
||||
auto compressed_lengths = table.getCompressedLengthCounts();
|
||||
for(unsigned idx = 0; idx<compressed_lengths.size(); idx++)
|
||||
{
|
||||
std::cout << "Slot " << idx << " length " << compressed_lengths[idx] << std::endl;
|
||||
}
|
||||
|
||||
HuffmanCodeLengthTable codingTable;
|
||||
std::vector<unsigned char> coding_lengths{4, 0, 6, 7, 3, 2, 4, 2, 7, 4, 6, 3, 0, 6, 0, 0, 0, 0, 0};
|
||||
codingTable.setInputLengthSequence(coding_lengths, true);
|
||||
codingTable.buildPrefixCodes();
|
||||
|
||||
BufferBitStream out_stream;
|
||||
out_stream.writeNBits(1, 1);
|
||||
out_stream.writeNBits(2, 2);
|
||||
|
||||
out_stream.writeNBits(29, 5);
|
||||
out_stream.writeNBits(29, 5);
|
||||
out_stream.writeNBits(10, 4);
|
||||
|
||||
/*
|
||||
std::vector<unsigned char> permuted(19, 0);
|
||||
static constexpr unsigned DEFLATE_PERMUTATION[19]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
|
||||
unsigned count = 0;
|
||||
for (auto length : coding_lengths)
|
||||
{
|
||||
permuted[DEFLATE_PERMUTATION[count]] = length;
|
||||
count++;
|
||||
}
|
||||
|
||||
unsigned skip_count = 0;
|
||||
for(unsigned idx=0; idx<permuted.size();idx++)
|
||||
{
|
||||
if (permuted[permuted.size() - 1 - idx] == 0)
|
||||
{
|
||||
skip_count++;
|
||||
}
|
||||
}
|
||||
std::cout << "Got skip count " << skip_count << std::endl;
|
||||
|
||||
for(unsigned idx=0; idx<permuted.size() - skip_count;idx++)
|
||||
{
|
||||
out_stream.writeNBits(permuted[idx], 3);
|
||||
}
|
||||
*/
|
||||
|
||||
for(unsigned idx=0; idx<14;idx++)
|
||||
{
|
||||
out_stream.writeNBits(coding_lengths[idx], 3);
|
||||
}
|
||||
|
||||
for (const auto& entry : compressed_sequence)
|
||||
{
|
||||
auto prefix_code = *codingTable.getCodeForSymbol(entry.first);
|
||||
out_stream.writeNBits(prefix_code.getData(), prefix_code.getLength());
|
||||
|
||||
std::cout << "Stream count " << out_stream.getBuffer().size() << " for entry " << entry.first << std::endl;
|
||||
|
||||
if (entry.first == 16)
|
||||
{
|
||||
out_stream.writeNBits(entry.second, 2);
|
||||
}
|
||||
else if (entry.first == 17)
|
||||
{
|
||||
out_stream.writeNBits(entry.second, 3);
|
||||
}
|
||||
else if (entry.first == 18)
|
||||
{
|
||||
out_stream.writeNBits(entry.second, 7);
|
||||
}
|
||||
}
|
||||
out_stream.resetOffsets();
|
||||
|
||||
std::cout << "Output is: " << std::endl;
|
||||
auto dump = out_stream.logNextNBytes(out_stream.getBuffer().size());
|
||||
std::cout << dump << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
testHuffmanCodeLengthTable();
|
||||
//testHuffmanCodeLengthTable();
|
||||
|
||||
testLiteralsTable();
|
||||
//HuffmanStream stream(nullptr, nullptr);
|
||||
|
||||
//stream.setCodeLengthAlphabetLengths({3, 3, 3, 3, 3, 2, 4, 4});
|
||||
|
|
|
@ -29,5 +29,9 @@ int main()
|
|||
auto out = ByteUtils::mirror(byte);
|
||||
std::cout << "Mirror is " << ByteUtils::toString(out) << std::endl;
|
||||
|
||||
unsigned hold = byte;
|
||||
hold = (hold << 5) + 3;
|
||||
std::cout << "Big val is " << ByteUtils::toString(hold, 16) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
|
||||
void testThirdParty()
|
||||
{
|
||||
//const auto path = "/home/jmsgrogan/Downloads/test.png";
|
||||
const auto path = "/home/jmsgrogan/Downloads/test.png";
|
||||
|
||||
const auto path = "/home/jmsgrogan/Downloads/index.png";
|
||||
//const auto path = "/home/jmsgrogan/Downloads/index.png";
|
||||
|
||||
//const auto path = "/home/jmsgrogan/code/MediaTool-build/bin/test.png";
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue