diff --git a/.vscode/settings.json b/.vscode/settings.json index 59b4a4e..c600bc3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a9a6263..69ea523 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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$<$:Debug>") diff --git a/huffman.hpp b/huffman.hpp index 196987a..d4e4777 100644 --- a/huffman.hpp +++ b/huffman.hpp @@ -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); } @@ -72,7 +72,9 @@ namespace pack { void encodeHuffmanTree(BitWriter& dest) { if (DHT.size() == 0) throw std::domain_error("No DHT found!"); - vector 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]) { @@ -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); diff --git a/main.cpp b/main.cpp index 3a174d5..d4f763f 100644 --- a/main.cpp +++ b/main.cpp @@ -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); @@ -88,16 +90,16 @@ 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(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(); @@ -105,6 +107,5 @@ int main() { std::cout << "psnr=" << psnr(data, reconstructed) << std::endl; - auto depacked = decompress(data_comp); - std::cout << "data unpacked:" << std::endl; + }