I think I fixed the lzw algo, but somehow uncompression still broken

This commit is contained in:
Phuntsok Drak-pa 2018-06-10 23:44:10 +02:00
parent fcfe944c5d
commit 5eb33fb04f
3 changed files with 39 additions and 8 deletions

View File

@ -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);

View File

@ -5,6 +5,13 @@
#include "common.hh"
#ifdef Debug
constexpr bool debug_mode = true;
#include <cstdio>
#else
constexpr bool debug_mode = false;
#endif
using std::uint8_t;
using std::uint16_t;
using dic_comp_t = std::map<std::pair<uint16_t, uint8_t>, uint16_t>;
@ -59,18 +66,36 @@ std::pair<bool, uint16_t> dico(dic_comp_t &t_dictionary,
255));
}
ustring dico_uncompress(std ::map<uint16_t, ustring> &t_dict,
ustring dico_uncompress(std::map<uint16_t, ustring> &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<unsigned char>(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<unsigned char>(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<unsigned char>(t_old)}
: t_dict[t_old];
str += str[0];
t_dict[static_cast<uint16_t>(t_dict.size())] = std::move(str);
t_dict[static_cast<uint16_t>(t_dict.size() + 255)] = std::move(str);
if constexpr(debug_mode) {
std::printf("String: %s\n", e.c_str());
}
return e;
}

View File

@ -26,8 +26,14 @@ using vuint16 = vector<uint16_t>;
uint16_t code = t_compressed[0];
std::map<uint16_t, ustring> dict{};
ret.push_back(static_cast<unsigned char>(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));