-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLZW.cpp
198 lines (145 loc) · 4.87 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "LZW.h"
using namespace std;
LZW::LZW(std::vector<std::string> choosen_formats) {
Formats = {"text"};
if(choosen_formats.size() != 0 ){
if(choosen_formats[0] != "") {
for(auto &it : choosen_formats) {
Formats.push_back(it);
}
}
}
}
void LZW::Encode(IInputStream &original, IOutputStream &compressed) {
unsigned char value;
while(original.Read(value)){}
const std::size_t buffer_size {1024 * 1024};
const std::unique_ptr<char[]> input_buffer(new char[buffer_size]);
const std::unique_ptr<char[]> output_buffer(new char[buffer_size]);
std::ifstream input_file;
std::ofstream output_file;
input_file.rdbuf()->pubsetbuf(input_buffer.get(), buffer_size);
input_file.open(original.GetFilePath(), std::ios_base::binary);
output_file.rdbuf()->pubsetbuf(output_buffer.get(), buffer_size);
output_file.open(compressed.GetFilePath(), std::ios_base::binary);
input_file.exceptions(std::ios_base::badbit);
output_file.exceptions(std::ios_base::badbit | std::ios_base::failbit);
InternalCompress(input_file, output_file);
}
void LZW::Decode(IInputStream &compressed, IOutputStream &original) {
const std::size_t buffer_size {1024 * 1024};
const std::unique_ptr<char[]> input_buffer(new char[buffer_size]);
const std::unique_ptr<char[]> output_buffer(new char[buffer_size]);
std::ifstream input_file;
std::ofstream output_file;
input_file.rdbuf()->pubsetbuf(input_buffer.get(), buffer_size);
input_file.open(compressed.GetFilePath(), std::ios_base::binary);
output_file.rdbuf()->pubsetbuf(output_buffer.get(), buffer_size);
output_file.open(original.GetFilePath(), ios_base::out);
input_file.exceptions(std::ios_base::badbit);
output_file.exceptions(std::ios_base::badbit | std::ios_base::failbit);
InternalDecompress(input_file, output_file);
}
bool LZW::ShouldChoose(string type_file) {
auto it = find(Formats.begin(), Formats.end(), type_file);
if(it != Formats.end()) {
return true;
}
else {
return false;
}
}
string LZW::GetName() {
return "LZW";
}
std::size_t LZW::GetRequiredBits(unsigned long int n) {
std::size_t r {1};
while ((n >>= 1) != 0)
++r;
return r;
}
void LZW::InternalCompress(std::istream &is, std::ostream &os){
EncoderDictionary ed;
CodeWriter cw(os);
CodeType i {globals::dms};
char c;
bool rbwf {false};
while (is.get(c)) {
if (ed.Size() == globals::dms) {
ed.Reset();
rbwf = true;
}
const CodeType temp {i};
if ((i = ed.SearchAndInsert(temp, c)) == globals::dms)
{
cw.Write(temp);
i = ed.SearchInitials(c);
if (GetRequiredBits(ed.Size() - 1) > cw.GetBits())
cw.IncreaseBits();
}
if (rbwf)
{
cw.ResetBits();
rbwf = false;
}
}
if (i != globals::dms)
cw.Write(i);
}
void LZW::InternalDecompress(std::istream &is, std::ostream &os){
std::vector<std::pair<CodeType, char>> dictionary;
const auto reset_dictionary = [&dictionary] {
dictionary.clear();
dictionary.reserve(globals::dms);
const long int minc = std::numeric_limits<char>::min();
const long int maxc = std::numeric_limits<char>::max();
for (long int c = minc; c <= maxc; ++c)
dictionary.push_back({globals::dms, static_cast<char> (c)});
dictionary.push_back({0, '\x00'});
};
const auto rebuild_string = [&dictionary](CodeType k) -> const std::vector<char> * {
static std::vector<char> s;
s.clear();
s.reserve(globals::dms);
while (k != globals::dms) {
s.push_back(dictionary[k].second);
k = dictionary[k].first;
}
std::reverse(s.begin(), s.end());
return &s;
};
reset_dictionary();
CodeReader cr(is);
CodeType i {globals::dms};
CodeType k;
while (true)
{
if (dictionary.size() == globals::dms)
{
reset_dictionary();
cr.ResetBits();
}
if (GetRequiredBits(dictionary.size()) > cr.GetBits())
cr.IncreaseBits();
if (!cr.Read(k))
break;
if (k > dictionary.size())
throw std::runtime_error("invalid compressed code");
const std::vector<char> *s; // String
if (k == dictionary.size())
{
dictionary.push_back({i, rebuild_string(i)->front()});
s = rebuild_string(k);
}
else
{
s = rebuild_string(k);
if (i != globals::dms)
dictionary.push_back({i, s->front()});
}
os.write(&s->front(), s->size());
i = k;
}
if (cr.Corrupted())
throw std::runtime_error("corrupted compressed file");
}