Changed file writing method to dynamic bit-packing
This commit is contained in:
parent
d901cd60f2
commit
6d779d8606
@ -40,7 +40,7 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin,
|
|||||||
vuchar ret{};
|
vuchar ret{};
|
||||||
|
|
||||||
// max value with current number of bits + 1
|
// max value with current number of bits + 1
|
||||||
const int max_value = ipow(2, t_n);
|
const int max_value = ipow(2, t_n) - 1;
|
||||||
|
|
||||||
uchar current_char = 0;
|
uchar current_char = 0;
|
||||||
int step = t_n / 8;
|
int step = t_n / 8;
|
||||||
|
@ -19,4 +19,7 @@ std::vector<unsigned char>
|
|||||||
pack_16(const std::vector<std::uint16_t>::const_iterator,
|
pack_16(const std::vector<std::uint16_t>::const_iterator,
|
||||||
const std::vector<std::uint16_t>::const_iterator);
|
const std::vector<std::uint16_t>::const_iterator);
|
||||||
|
|
||||||
|
/// \brief Bat-packs the input dynamically
|
||||||
|
std::vector<unsigned char> pack(const std::vector<std::uint16_t>);
|
||||||
|
|
||||||
#endif /* LZW_SRC_BITPACK_H_ */
|
#endif /* LZW_SRC_BITPACK_H_ */
|
||||||
|
22
src/io.cc
22
src/io.cc
@ -4,7 +4,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "io.hh"
|
#include "io.hh"
|
||||||
|
#include "bitpack.hh"
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#ifdef Debug
|
#ifdef Debug
|
||||||
constexpr bool debug_mode = true;
|
constexpr bool debug_mode = true;
|
||||||
@ -71,21 +73,9 @@ void write_file(FILE *const t_out, const vvuint16 &t_text) {
|
|||||||
* \param t_chunk Chunk to be written to \p t_out
|
* \param t_chunk Chunk to be written to \p t_out
|
||||||
*/
|
*/
|
||||||
void write_chunk(FILE *const t_out, const vuint16 &t_chunk) {
|
void write_chunk(FILE *const t_out, const vuint16 &t_chunk) {
|
||||||
const auto chunk_size = static_cast<uint16_t>(t_chunk.size());
|
const auto output = pack(t_chunk);
|
||||||
|
const auto chunk_size = static_cast<uint16_t>(output.size());
|
||||||
fwrite(&chunk_size, sizeof(chunk_size), 1, t_out);
|
fwrite(&chunk_size, sizeof(chunk_size), 1, t_out);
|
||||||
std::array<unsigned char, 3> data = {0, 0, 0};
|
fwrite((const char *)(output.data()), sizeof(output[0]), output.size(),
|
||||||
for (size_t i = 0; i < t_chunk.size(); ++i) {
|
t_out);
|
||||||
if (i % 2 == 0) {
|
|
||||||
data[0] = static_cast<unsigned char>(t_chunk[i] >> 4);
|
|
||||||
data[1] = static_cast<unsigned char>(t_chunk[i] << 4);
|
|
||||||
} else {
|
|
||||||
data[1] |= static_cast<unsigned char>(t_chunk[i] >> 8) & 0xC;
|
|
||||||
data[2] = static_cast<unsigned char>(t_chunk[i]);
|
|
||||||
fwrite(data.data(), sizeof(data[0]), 3, t_out);
|
|
||||||
data.fill(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (t_chunk.size() % 2 != 0) {
|
|
||||||
fwrite(data.data(), sizeof(data[0]), 3, t_out);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user