2018-04-05 16:47:07 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file compress.cc
|
|
|
|
|
* \brief Implementation of compression
|
|
|
|
|
*/
|
|
|
|
|
|
2018-04-26 09:54:02 +00:00
|
|
|
|
#ifdef Debug
|
|
|
|
|
constexpr bool debug_mode = true;
|
|
|
|
|
#else
|
|
|
|
|
constexpr bool debug_mode = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-04-05 16:47:07 +00:00
|
|
|
|
#include "compress.hh"
|
2018-04-10 08:39:41 +00:00
|
|
|
|
#include "utf8.hh"
|
2018-04-05 16:47:07 +00:00
|
|
|
|
#include <cstdlib>
|
2018-04-10 08:39:41 +00:00
|
|
|
|
#include <fstream>
|
2018-04-26 09:54:02 +00:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <thread>
|
2018-04-05 16:47:07 +00:00
|
|
|
|
|
2018-04-26 09:54:02 +00:00
|
|
|
|
using dict_t = std::map<std::pair<uint16_t, uint8_t>, uint16_t>;
|
2018-04-05 16:47:07 +00:00
|
|
|
|
using ustring = std::basic_string<uint8_t>; // chaine non encodée
|
2018-04-26 09:54:02 +00:00
|
|
|
|
using uvec = std::vector<uint16_t>; // chaine encodée
|
2018-04-10 08:39:41 +00:00
|
|
|
|
using std::printf;
|
2018-04-05 16:47:07 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* La chaine de caractère \p text est lue caractère par caractère, et est et
|
2018-04-10 08:39:41 +00:00
|
|
|
|
* selon la valeur de retour de la fonction \ref dico (permettant dans le même
|
|
|
|
|
* temps la création du dictionnaire), on rajoute un mot ou pas dans le vecteur
|
|
|
|
|
* de caractères UTF-8 représentant des mots de chars compressés. La fonction
|
|
|
|
|
* renvoie ledit vecteur de uint32_t.
|
2018-04-05 16:47:07 +00:00
|
|
|
|
*
|
|
|
|
|
* \param t_text Chaine de caractères uint8_t représentant le fichier d'entrée
|
|
|
|
|
* \param t_dictionary Dictionnaire de compression
|
|
|
|
|
* \return std::vector<uint16_t>
|
|
|
|
|
*/
|
2018-04-26 09:54:02 +00:00
|
|
|
|
void lzw_compress(const std::vector<char> &t_text, uvec &t_res) {
|
|
|
|
|
dict_t dictionary{};
|
2018-04-05 16:47:07 +00:00
|
|
|
|
std::puts("Compressing...");
|
2018-04-26 09:54:02 +00:00
|
|
|
|
// uvec res{};
|
|
|
|
|
uint16_t w = 0xFFFF;
|
|
|
|
|
uint16_t len = 0;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
|
|
|
|
constexpr size_t DICT_MAX = 7936; /* 12 bits */
|
|
|
|
|
|
2018-04-05 16:47:07 +00:00
|
|
|
|
size_t progress = 0;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
2018-04-05 16:47:07 +00:00
|
|
|
|
for (const auto &c : t_text) {
|
|
|
|
|
++len;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
2018-04-26 09:54:02 +00:00
|
|
|
|
if constexpr (debug_mode) {
|
|
|
|
|
printf("\rprogress: %zu / %zu", ++progress, t_text.size());
|
|
|
|
|
}
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
2018-04-26 09:54:02 +00:00
|
|
|
|
if (/* len > LENGTH_MAX || */ dictionary.size() >= DICT_MAX) {
|
|
|
|
|
t_res.push_back(static_cast<uint16_t>(w));
|
|
|
|
|
w = static_cast<uint16_t>(c);
|
2018-04-05 16:47:07 +00:00
|
|
|
|
len = 0;
|
2018-04-26 09:54:02 +00:00
|
|
|
|
} else if (const auto &[exists, pos] =
|
|
|
|
|
dico(dictionary, w, static_cast<std::uint8_t>(c));
|
|
|
|
|
exists) {
|
2018-04-05 16:47:07 +00:00
|
|
|
|
w = pos;
|
|
|
|
|
} else {
|
2018-04-26 09:54:02 +00:00
|
|
|
|
t_res.push_back(static_cast<uint16_t>(w));
|
|
|
|
|
w = static_cast<std::uint8_t>(c);
|
2018-04-05 16:47:07 +00:00
|
|
|
|
len = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
2018-04-26 09:54:02 +00:00
|
|
|
|
// return res;
|
2018-04-05 16:47:07 +00:00
|
|
|
|
}
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \brief function description
|
|
|
|
|
*
|
|
|
|
|
* Wrapper de la fonction \ref lzw_compress gérant l'ouverture, la lecture,
|
|
|
|
|
* l'écriture et la fermeture des fichiers d’entrée et de sortie. Si \p
|
|
|
|
|
* t_out_file est nul (chemin non spécifié), il prendra alors la valeur de
|
|
|
|
|
* \p t_in_file à laquelle sera annexé l’extension `.lzw`.
|
|
|
|
|
*
|
|
|
|
|
* \param t_in_file Chemin vers le fichier d’entrée
|
|
|
|
|
* \param t_out_file Chemin vers le fichier de sortie
|
|
|
|
|
*/
|
|
|
|
|
void compress(const std::string &t_in_file, const char *t_out_file) {
|
|
|
|
|
// Fichier d’entrée
|
|
|
|
|
std::ifstream input_file{t_in_file};
|
2018-04-26 09:54:02 +00:00
|
|
|
|
if(!input_file.is_open()) {
|
|
|
|
|
std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 2
|
|
|
|
|
<< ": could not open output file \"" << t_in_file << "\". Aborting...\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
|
|
|
|
// Fichier de sortie
|
2018-04-26 09:54:02 +00:00
|
|
|
|
const char *filename =
|
|
|
|
|
(t_out_file) ? t_out_file : "output.lzw";
|
|
|
|
|
FILE *out = fopen(filename, "wb");
|
|
|
|
|
if(!out) {
|
|
|
|
|
std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 4
|
|
|
|
|
<< ": could not open output file \"" << filename << "\". Aborting...\n";
|
|
|
|
|
input_file.close();
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
2018-04-26 09:54:02 +00:00
|
|
|
|
// input_file.seekg(0, std::ios::end);
|
|
|
|
|
// // string contenant le fichier d’entrée
|
|
|
|
|
// ustring str(static_cast<unsigned long>(input_file.tellg()),
|
|
|
|
|
// static_cast<unsigned char>(0));
|
|
|
|
|
// input_file.seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
|
|
// // assignation du contenu du fichier à str
|
|
|
|
|
// str.assign((std::istreambuf_iterator<char>(input_file)),
|
|
|
|
|
// std::istreambuf_iterator<char>());
|
|
|
|
|
|
|
|
|
|
// printf("Size of input file: %zu\n", str.size());
|
|
|
|
|
|
|
|
|
|
// dict_t dictionary{};
|
|
|
|
|
|
|
|
|
|
// const auto comp_str{lzw_compress(str, dictionary)};
|
|
|
|
|
|
|
|
|
|
// thread pool
|
|
|
|
|
std::vector<std::pair<std::thread, uvec>> threads{};
|
|
|
|
|
|
|
|
|
|
// char chunk[32768];
|
|
|
|
|
std::vector<char> chunk{};
|
|
|
|
|
chunk.reserve(32768);
|
|
|
|
|
while (input_file.read(chunk.data(), 32768)) {
|
|
|
|
|
threads.push_back(std::make_pair(std::thread{}, uvec{}));
|
|
|
|
|
threads.back().first =
|
|
|
|
|
std::thread{lzw_compress, chunk, ref(threads.back().second)};
|
|
|
|
|
if (threads.size() >= 8) {
|
|
|
|
|
for (auto &elem : threads) {
|
|
|
|
|
elem.first.join();
|
|
|
|
|
}
|
|
|
|
|
for (auto &elem : threads) {
|
|
|
|
|
for (const auto c : elem.second) {
|
|
|
|
|
write_utf8(out, c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
threads.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
2018-04-26 09:54:02 +00:00
|
|
|
|
if(input_file.tellg() != std::ios::end) {
|
|
|
|
|
std::puts("Leftovers...");
|
|
|
|
|
}
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
2018-04-26 09:54:02 +00:00
|
|
|
|
// for (const auto c : comp_str)
|
|
|
|
|
// write_utf8(out, c);
|
2018-04-10 08:39:41 +00:00
|
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
|
input_file.close();
|
|
|
|
|
}
|