From 5eb33fb04f8adb78587d019a611343c04adc4c7c Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Sun, 10 Jun 2018 23:44:10 +0200 Subject: [PATCH] I think I fixed the lzw algo, but somehow uncompression still broken --- src/bitpack.cc | 2 +- src/common.cc | 35 ++++++++++++++++++++++++++++++----- src/uncompress.cc | 10 ++++++++-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/bitpack.cc b/src/bitpack.cc index cd63317..4ebed43 100644 --- a/src/bitpack.cc +++ b/src/bitpack.cc @@ -49,7 +49,7 @@ pack_n(const vuint16::const_iterator t_input_begin, if (left_shift >= t_n) { left_shift = (left_shift - t_n) + step; } - ret.push_back((current_char | (*it >> left_shift)) & 0xFF); + ret.push_back((current_char | (*it >> left_shift)) & 0xFFu); current_char = 0; bool zero_right_shift = (right_shift == 0); diff --git a/src/common.cc b/src/common.cc index caddc51..1198876 100644 --- a/src/common.cc +++ b/src/common.cc @@ -5,6 +5,13 @@ #include "common.hh" +#ifdef Debug +constexpr bool debug_mode = true; +#include +#else +constexpr bool debug_mode = false; +#endif + using std::uint8_t; using std::uint16_t; using dic_comp_t = std::map, uint16_t>; @@ -59,18 +66,36 @@ std::pair dico(dic_comp_t &t_dictionary, 255)); } -ustring dico_uncompress(std ::map &t_dict, +ustring dico_uncompress(std::map &t_dict, const uint16_t t_code, const uint16_t t_old) { - auto &e = t_dict[t_code]; + if constexpr(debug_mode) { + std::printf("Code: %d\tOld code: %d\n", t_code, t_old); + } + + auto e = (t_code < 256) ? ustring{static_cast(t_code)} + : t_dict[t_code]; + + // Si le code n'existe pas + if(e.empty()) { - e = t_dict[t_old]; + e = (t_old < 256) ? ustring{static_cast(t_old)} + : t_dict[t_old]; const auto temp = e[0]; e += temp; + t_dict[t_code] = std::move(e); + if constexpr(debug_mode) { + std::printf("String: %s\n", e.c_str()); + } return e; } - auto str = t_dict[t_old]; + // auto str = t_dict[t_old]; + auto str = (t_old < 256) ? ustring{static_cast(t_old)} + : t_dict[t_old]; str += str[0]; - t_dict[static_cast(t_dict.size())] = std::move(str); + t_dict[static_cast(t_dict.size() + 255)] = std::move(str); + if constexpr(debug_mode) { + std::printf("String: %s\n", e.c_str()); + } return e; } diff --git a/src/uncompress.cc b/src/uncompress.cc index 43a7273..cb65d09 100644 --- a/src/uncompress.cc +++ b/src/uncompress.cc @@ -26,8 +26,14 @@ using vuint16 = vector; uint16_t code = t_compressed[0]; std::map dict{}; ret.push_back(static_cast(code)); + if constexpr(debug_mode) { + std::printf("%d\n", code); + } old = code; for (auto it = t_compressed.begin() + 1; it != t_compressed.end(); ++it) { + if constexpr(debug_mode) { + std::printf("%d\n", *it); + } code = *it; const auto uncompressed{dico_uncompress(dict, code, old)}; ret.insert(ret.end(), uncompressed.begin(), uncompressed.end()); @@ -67,8 +73,8 @@ void uncompress(const string &t_input_name, const char *t_output_name) { auto unpacked = unpack(ustring{chunk, chunk + size_chunk}); if constexpr(debug_mode) { - for(const auto val : unpacked) { - std::printf("%d\n", val); + for(const auto c : unpacked) { + std::printf("%d\n", c); } } const auto uncompressed_chunk = lzw_uncompress(std::move(unpacked));