-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm_lzw.cpp
53 lines (47 loc) · 1.53 KB
/
algorithm_lzw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "algorithm_lzw.hpp"
void lzw_compress(std::ifstream& input_file,std::ofstream& output_file) {
int input_file_size = input_file.tellg();
std::cout << "Original filesize = " << input_file_size << "\n";
input_file.seekg(0, std::ios::beg);
std::string input_text;
if (input_file.is_open()) {
while (getline (input_file,input_text)) {
std::cout << input_text << "\n";
// TODO: Read whole file at once with \n chars into input_text
}
}
std::vector<int> output_text = lzw_compress(input_text, input_file_size);
// Write output_text to file
// Use Huffmann coding
return;
}
std::vector<int> lzw_compress(std::string input_string, int input_size) {
// compression
std::map<std::string,int> lzw_dict;
std::string chars_in_dict;
std::vector<int> result;
// int len_uncompressed_data;
// initialize
for (int i=0; i<256; i++) {
std::string ch = "";
ch += char(i);
lzw_dict[ch] = i;
// std::cout << ch;
}
std::string section = "";
int next_code = 256;
for (int i=0; i<input_size; i++) {
section = section + input_string[i];
if (lzw_dict.find(section)==lzw_dict.end()) {
// Not found in dict
lzw_dict[section] = next_code;
next_code++;
}
// std::cout << lzw_dict[std::to_string(i)];
}
return result;
}
void lzw_decompress(std::ifstream& input_file,std::ofstream& output_file) {
std::cout << "Not yet implemented";
return;
}