-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
306 lines (238 loc) · 7.12 KB
/
code.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Project name: Huffman Coding
Project version: 1.0
File name: code.cpp
Project description: David Huffman algorithm based text files encoding/decoding as a command line utility.
Created by Michał Grochowski on 20 Feb 2022.
Copyright © 2022 Michał Grochowski. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "code.hpp"
HuffCode::HuffCode() {}
void HuffCode::fileEncode(const std::string &input, const std::string &output) {
// read the contents of input file
std::streampos size;
char *data;
std::ifstream inputFile(input,std::ios::binary | std::ios::in | std::ios::ate);
if (!inputFile.is_open()) {
std::cout << "failed to open the input file!" << std::endl;
return;
}
size = inputFile.tellg(); //getting the current read position
data = new char[size];
inputFile.seekg(0, std::ios::beg);
inputFile.read(data, size);
inputFile.close();
stringToEncode = data;
delete[] data;
// calculate byte frequencies
std::cout << "Byte frequencies calculating..." << std::endl;
calculateFrequencies();
// create the huffman tree
std::cout << "Huffman tree creating..." << std::endl;
createTree();
// create the huffman code from that tree
std::cout << "Huffman code generating ..." << std::endl;
std::cout << "Done!" << std::endl;
// std::vector<bool> v;
HuffVector<bool> v;
generateHuffCode(root, v);
// encode the tree data; source of the idea taken from:
// http://stackoverflow.com/questions/759707/efficient-way-of-storing-huffman-tree
HuffVector<bool> b;
// std::vector<bool> b;
encode(root, b);
bits = HuffVector<bool>(b);
// append the encoded string itself
appendEncodedBits();
// converting bits to bytes in purpose of saving
bitsToBytes();
// writing the output
std::ofstream outputFile;
outputFile.open(output, std::ofstream::out);
for (size_t i = 0; i < bytes.size(); ++i) {
const uint8_t byte = bytes[i];
outputFile << byte;
}
outputFile.close();
}
void HuffCode::fileDecode(const std::string &input, const std::string &output) {
// reading contents of the file
std::streampos size;
char *data;
std::ifstream file(input, std::ios::binary | std::ios::in | std::ios::ate);
if (!file.is_open()) {
std::cout << "failed to open" << std::endl;
return;
}
size = file.tellg();
data = new char[size];
file.seekg(0, std::ios::beg);
file.read(data, size);
file.close();
size_t fileSize = static_cast<size_t>(size);
// converting bytes to bits
for (size_t k = 0; k < fileSize; ++k) {
char c = data[k];
uint8_t byte = static_cast<uint8_t>(c);
size_t shiftCount = 7;
do {
bool bit = ((byte >> shiftCount) & 1);
bits.push_back(bit);
} while (shiftCount-- != 0);
}
delete[] data;
// rebuilding the tree
Node *decodedRoot = rebuildTree();
std::string s = "";
// decoding the encoded data with the rebuilt tree
while (true) {
// in case of hitting the end
if (decodingIndex >= bits.size()) {
break;
}
char c = getCharacter(decodedRoot);
s += c;
}
// writing the output
std::ofstream outputFile;
outputFile.open(output, std::ofstream::out);
outputFile << s;
outputFile.close();
}
// calculating character (byte) frequencies
void HuffCode::calculateFrequencies() {
for (const char &c : stringToEncode) {
++(frequencies[static_cast<uint8_t>(c)]);
}
}
// creating the initial leaves and internal leaves
void HuffCode::createTree() {
HuffQueue tree;
// loop through bytes ("characters") and pushing them into the queue
uint8_t i = 0;
do {
const auto frequency = frequencies[i];
if (frequency == 0) {
continue;
}
Node *node = new Node(frequency, i);
tree.push(node);
} while (i++ != UINT8_MAX);
// creating internal nodes
while (tree.size() > 1) {
Node *rightChild = tree.pop();
Node *leftChild = tree.pop();
Node *parent = new Node(leftChild, rightChild);
tree.push(parent);
}
root = tree.top();
}
// generating recursivly huffman coding
void HuffCode::generateHuffCode(const Node *node,
const HuffVector<bool> &code) {
// assigning a code
if (!node->left && !node->right) {
size_t index = static_cast<size_t>(node->character);
codes[index] = code;
return;
}
auto left = HuffVector<bool>(code);
left.push_back(false);
auto right = HuffVector<bool>(code);
right.push_back(true);
generateHuffCode(node->left, left);
generateHuffCode(node->right, right);
}
// encoding the codes and the string into bits for packaging into bytes
void HuffCode::encode(const Node *node, HuffVector<bool> &v) {
if (!node->left) {
// this is the code for this byte
v.push_back(true);
uint8_t b = static_cast<uint8_t>(node->character);
// byte in bits
// only works in little endian machines
v.push_back(((b >> 7) & 1));
v.push_back(((b >> 6) & 1));
v.push_back(((b >> 5) & 1));
v.push_back(((b >> 4) & 1));
v.push_back(((b >> 3) & 1));
v.push_back(((b >> 2) & 1));
v.push_back(((b >> 1) & 1));
v.push_back((b & 1));
} else {
v.push_back(false);
encode(node->left, v);
encode(node->right, v);
}
}
// converting bits to bytes for output purposes
void HuffCode::bitsToBytes() {
size_t bitCount = 0;
uint8_t byte = 0;
for (size_t i = 0; i < bits.size(); ++i) {
// shift the bit to the right position and or it in place
byte |= (bits[i] & 1) << (7 - bitCount);
++bitCount;
// byte ready state
if (bitCount == 8) {
bytes.push_back(byte);
byte = 0;
bitCount = 0;
}
if (i == bits.size() - 1) {
break;
}
}
}
void HuffCode::appendEncodedBits() {
for (const char &c : stringToEncode) {
uint8_t i = static_cast<uint8_t>(c);
auto code = codes[i];
// for (const bool &b : code) {
for (size_t index = 0; index < code.size(); ++index) {
bits.push_back(code[index]);
}
}
}
bool HuffCode::getBit() { return bits[decodingIndex++]; }
uint8_t HuffCode::getByte() {
uint8_t byte = 0;
for (size_t i = 0; i < 8; ++i) {
bool bit = getBit();
byte |= bit;
if (i != 7) {
byte <<= 1;
}
}
return byte;
}
Node *HuffCode::rebuildTree() {
if (getBit()) {
return new Node(0, getByte());
}
Node *left = rebuildTree();
Node *right = rebuildTree();
return new Node(left, right);
}
// find a character for the current code
char HuffCode::getCharacter(const Node *n) {
if (!n->left && !n->right) {
// if it's a leaf -> get the character
return n->character;
} else if (getBit()) {
// bit is 1 -> go right
return getCharacter(n->right);
} else {
// bit is 0 -> go left
return getCharacter(n->left);
}
}