Skip to content

Commit

Permalink
dht size auto
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Poslavskiy committed Aug 16, 2023
1 parent bb90596 commit 9dbbf10
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
30 changes: 29 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,35 @@
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp"
"xtr1common": "cpp",
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"__verbose_abort": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cwctype": "cpp",
"locale": "cpp",
"mutex": "cpp",
"optional": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"string_view": "cpp",
"variant": "cpp",
"__nullptr": "cpp",
"__string": "cpp",
"chrono": "cpp",
"*.tcc": "cpp",
"memory_resource": "cpp"
}

}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(dwt53 CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
if(CMAKE_BUILD_TYPE MATCHES Debug)
add_definitions(-D_ITERATOR_DEBUG_LEVEL=0)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
Expand Down
16 changes: 7 additions & 9 deletions huffman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace pack {

// found a leaf node
if (!node->left && !node->right) {
assert(len <= 16 && len > 0);
//assert(len <= 16 && len > 0);
if (DHT.size() < len) DHT.resize(len);
DHT[len - 1].push_back(node->ch);
}
Expand All @@ -72,7 +72,9 @@ namespace pack {
void encodeHuffmanTree(BitWriter& dest) {
if (DHT.size() == 0) throw std::domain_error("No DHT found!");

vector<bool> str;


dest.writeBits(8,DHT.size());
for (int i = 0; i < DHT.size(); i++) {
dest.writeBits(8, DHT[i].size());
for (auto symbol : DHT[i]) {
Expand All @@ -82,21 +84,17 @@ namespace pack {
dest.writeBits(catindex & 0xf, catindex >> 4);
}
}
// rest of DHT (16 bytes)
for (int i = DHT.size(); i < 16; i++) {
dest.writeBits(8, 0);
}
}

void decodeHuffmanTree(BitReader& src) {
// decode huffman table
DHT.clear();
DHT.resize(16);
DHT.resize(src.readBits(8));

for (int i = 0; i < 16; i++) {
for (int i = 0; i < DHT.size(); i++) {
DHT[i].resize(src.readBits(8));
if (DHT[i].size() == 0) continue;
cout << i << ":";
cout << i << " (" << DHT[i].size() << ") :";
// cout << "DHT_" << i << ":" << DHT[i].size() << endl;
for (auto& value : DHT[i]) {
auto cat = src.readBits(4);
Expand Down
33 changes: 17 additions & 16 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,26 @@ int main() {
// cubicBlur3x3(data);
// cubicBlur3x3(data);
//auto data = make_gradient(64,64,0,111,111,222);
auto data = lenna;
//auto data = lenna;
//auto data = make_sky(128,96);
//auto data = make_random(128);
// cubicBlur3x3(data);
// cubicBlur3x3(data);
auto data = make_random(512);
//cubicBlur3x3(data);
// cubicBlur3x3(data);

std::cout << "original: pw=" << matrix_energy(data) << std::endl;;

try {
auto data_comp = compress(data);
std::cout << "packed by huffman size = " << data_comp.size() << std::endl;
auto depacked = decompress(data_comp);
// std::cout << "data unpacked:" << std::endl;
}
catch(std::exception &e){
std::cout << "original pack fail: " << e.what() << std::endl;
}
// auto data_decomp = huffman::decompress(data_comp);
// std::cout << data_decomp;

// auto test = make_matrix(10,10, [](int x,int y,int&v){ v = x + y*10; });
// auto all_img = make_block( data );
// quantization_block(data,all_img, true);
// quantization_block(data,all_img, false);
// std::cout << "quantized:" << raster(data);
// std::cout << "psnr=" << psnr(lenna, data) << std::endl;

dwt2d::Transform codec;
codec.prepare_transform(max_levels, wavelet, data);
Expand All @@ -88,23 +90,22 @@ int main() {
std::cout << "packed by huffman size = " << haar_data_comp.size() << std::endl;

codec.quantization(1);
std::cout << "quantized: pw=" << matrix_energy(haar_data) << std::endl;
std::cout << "quantized: pw=" << matrix_energy(haar_data) << std::endl;


auto haar_data_vq_comp = compress(haar_data);
// std::cout << "hufman-codes:";
// for (auto v : haar_data_vq_comp )
// std::cout << static_cast<int>(v) << " ";
decompress(haar_data_vq_comp);
std::cout << "packed by huffman size = " << haar_data_vq_comp.size() << std::endl;

// codec.vq_inverse();
// std::cout << "quantized-invert: pw=" << matrix_energy(haar_data) << std::endl;

// std::cout << haar_data;
auto& reconstructed = codec.inverse();



std::cout << "psnr=" << psnr(data, reconstructed) << std::endl;

auto depacked = decompress(data_comp);
std::cout << "data unpacked:" << std::endl;

}

0 comments on commit 9dbbf10

Please sign in to comment.