bugfix: fixed writing mask for charsize increase

This commit is contained in:
Phuntsok Drak-pa 2018-06-18 16:51:06 +02:00
parent 8777183821
commit 5830f4225c
2 changed files with 27 additions and 42 deletions

View File

@ -52,12 +52,6 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin,
}
const int max_value = max(t_n); // max value held within t_n bits
#ifdef Debug
std::printf("%d bits! %ld chars remaining\n", t_n,
std::distance(t_input_begin, t_input_end));
std::printf("max: %d\n", max_value);
#endif
int step = t_n % 8;
int left_shift = 0;
int right_shift = 0;
@ -66,28 +60,30 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin,
// pour chaque élément
for (auto it = t_input_begin; it != t_input_end; ++it) {
// si on a atteint ou dépassé la valeur maximale, on change de nombre de bits
if (*it >= max_value) {
// écriture du masque pour notifier à la décompression du changement de bits
if ((left_shift += step) >= t_n) {
left_shift = (left_shift - t_n) + step;
}
const auto mask = masks[t_n] >> left_shift;
t_res.push_back(
static_cast<uchar>(current_char | mask));
bool zero_rs = (right_shift == 0);
// si on a atteint ou dépassé la valeur maximale, on change de nombre de bits
if (*it >= max_value) {
// écriture du masque pour notifier à la décompression du changement de bits
if ((left_shift += step) >= t_n) {
left_shift = (left_shift - t_n) + step;
}
const auto mask = masks[t_n] >> left_shift;
t_res.push_back(
static_cast<uchar>(current_char | mask));
bool zero_rs = (right_shift == 0);
right_shift -= 0;
if(right_shift < 0) {
if(!zero_rs) {
current_char = static_cast<uchar>(masks[t_n] >> (-right_shift) & 0xffu);
t_res.push_back(current_char);
}
}
t_res.push_back(static_cast<uchar>(masks[t_n]));
return pack_n(it, t_input_end, t_res, t_n + 1);
}
right_shift -= step;
if(right_shift < 0) { // si right_shift est inférieur à zéro
// si right_shift était différent de zéro, alors extra octet
if (!zero_rs) {
current_char = static_cast<uchar>(masks[t_n] >> (-right_shift) & 0xffu);
t_res.push_back(current_char);
}
}
t_res.push_back(static_cast<uchar>(masks[t_n]));
return pack_n(it, t_input_end, t_res, t_n + 1);
}
// écriture normale
if ((left_shift += step) >= t_n) {
left_shift = (left_shift - t_n) + step;
}
@ -122,13 +118,9 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin,
vuchar pack_16(const vuint16::const_iterator t_input_begin,
const vuint16::const_iterator t_input_end, vuchar t_res) {
#ifdef Debug
std::printf("16 bits! %ld chars remaining\n",
std::distance(t_input_begin, t_input_end));
#endif
std::for_each(t_input_begin, t_input_end, [&](const auto value) {
t_res.push_back((value >> 8) & 0xFFu);
t_res.push_back(value & 0xFFu);
t_res.push_back(static_cast<uchar>(value >> 8 & 0xFFu));
t_res.push_back(static_cast<uchar>(value & 0xFFu));
});
return t_res;
}
@ -143,10 +135,6 @@ vuint16 unpack(ustring &&t_input) {
vuint16 unpack_n(const ustring::const_iterator t_begin,
const ustring::const_iterator t_end, vuint16 t_res, int t_n) {
#ifdef Debug
std::printf("Chunk! %d bits, %ld compressed chars\n", t_n,
std::distance(t_begin, t_end));
#endif
if (t_n == 16) {
return unpack_16(t_begin, t_end, vector<uint16_t>());
}
@ -157,9 +145,9 @@ vuint16 unpack_n(const ustring::const_iterator t_begin,
for (auto it = t_begin; it < t_end - 1; /* nope */) {
uint16_t current_char = 0;
// left bits
if ((left_shift += step) >= t_n) {
left_shift = (left_shift - t_n) + step;
}
if ((left_shift += step) >= t_n) {
left_shift = (left_shift - t_n) + step;
}
current_char = static_cast<uint16_t>(*it << left_shift) & masks[t_n];
// right bits
bool zero_rs = (right_shift == 0);

View File

@ -24,9 +24,6 @@ ustring lzw_uncompress(vuint16 &&t_compressed) {
for (auto it = t_compressed.begin() + 1; it != t_compressed.end(); ++it) {
v = *it;
const auto uncompressed{dico_uncompress(dict, v, old)};
#ifdef Debug
std::printf("%d = %s\n", v, uncompressed.c_str());
#endif
ret.insert(ret.end(), uncompressed.begin(), uncompressed.end());
old = v;
}