-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress.cpp
More file actions
258 lines (236 loc) · 7.82 KB
/
Compress.cpp
File metadata and controls
258 lines (236 loc) · 7.82 KB
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
//
// Created by Jerry Zhang on 2018/11/29.
//
#include "Compress.h"
#include <queue>
using std::cout;
using std::cerr;
using std::queue;
vector<byte> longlong2bytes(long long paramInt) {
vector<unsigned char> arrayOfByte(sizeof(long long));
for (int i = 0; i < sizeof(long long); i++)
arrayOfByte[sizeof(long long) - 1 - i] = (paramInt >> (i * 8));
return arrayOfByte;
}
void compress(string inputFileName,string outFileName) {
ifstream inputFile(inputFileName,std::ifstream::binary);
// TODO: do something if failing in file opening.
// Find file size;
long long filesize = 0;
inputFile.seekg(0,inputFile.end);
filesize = inputFile.tellg();
inputFile.seekg(0,inputFile.beg);
map<byte,long long> char_freq;
for(int i = 0;i < filesize;i++) {
byte byte1 = inputFile.get();
if(char_freq.find(byte1) != char_freq.end()) {
char_freq.at(byte1)++;
} else {
char_freq.insert(std::pair<byte,long long>(byte1,1));
}
}
// Printing out the whole frequency list.
for (auto it = char_freq.begin(); it != char_freq.end(); it++) {
std::cout << it->first << " " << it->second << std::endl;
}
vector<Huffman *> tree_list;
for(auto it = char_freq.begin();it != char_freq.end();it++) {
Huffman *tree = new Huffman(true,it->first,it->second);
tree_list.push_back(tree);
}
Huffman *tree = Huffman::build_tree(tree_list);
map<byte, string> char_map;
tree->traverse_tree(tree->get_root(), "", char_map);
// Print out the char_map for testing.
for (auto it = char_map.begin(); it != char_map.end(); it++) {
std::cout << it->first << " " << it->second << std::endl;
}
ofstream outputFile(outFileName,std::ofstream::binary);
// TODO: do something if failing in opening file.
auto bytes = longlong2bytes(filesize);
for(auto it = bytes.begin();it != bytes.end();it++) {
byte temp = *it;
outputFile.write((const char*)&temp, sizeof(temp));
}
inputFile.seekg(0,inputFile.beg);
// Write the byte and it's frequency to the file.
for(auto it = char_freq.begin();it != char_freq.end();it++) {
byte temp = it->first;
auto bytes = longlong2bytes(it->second);
outputFile.write((const char*)&temp, sizeof(temp));// Write the byte to file
// Writing it's frequency.
for(auto i = bytes.begin();i != bytes.end();i++) {
byte temp = *i;
outputFile.write((const char*)&temp, sizeof(temp));
}
}
string code = "";
// Write the compressed data to the file.
for (long long i = 0; i < filesize; i++) {
byte key = inputFile.get();
code += char_map[key];
while (code.length() > 8) {
byte out = 0;
for(int j = 0;j < 8;j++) {
out = out<<1;
if(code[j] == '1') {
out = out|0x1;
}
}
outputFile.write((const char*)&out, sizeof(out));
out = 0;
code = code.substr(8);
}
}
//处理不满 8 位的 code
byte out = code.length();
cout<<"Compressing last code len = " <<code.length()<<std::endl;
outputFile.write((const char *)&out, sizeof(out));
/*
* for i in range(len(code)):
out = out<<1
if code[i]=='1':
out = out|1
*/
out = 0;
int i;
for(i = 0;i < code.length();i++) {
out = out<<1;
if(code[i] == '1')
out = out|0x1;
}
for(;i<8;i++) {
out = out<<1;
}
outputFile.write((const char*)&out, sizeof(out));
outputFile.close();
inputFile.close();
// // Testing reading binary from file
// inputFile.open("out.txt",ifstream::binary);
// std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(inputFile), {});
// for(auto it = buffer.begin();it != buffer.end();it++) {
// cout<<"it= "<<(int)*it<<std::endl;
// }
// inputFile.close();
}
void decompress(string inputFileName,string outputFileName) {
ifstream inputFile(inputFileName,ifstream::binary);
std::vector<unsigned char> data(std::istreambuf_iterator<char>(inputFile), {});
long long filesize = 0;
int i;
for(i = 0;i < sizeof(long long);i++) {
filesize = filesize<<8; // TODO: determine 8 or 1
filesize = filesize | data[i];
}
cout<<"Got filesize = "<<filesize<<std::endl;
long long count = 0;
map<byte,long long> char_freq;
while(count < filesize) {
byte temp = data[i];
long long freq = 0;
i++;
for(int j = 0;j < sizeof(long long);j++,i++) {
freq = freq << 8;
freq = freq | data[i];
}
char_freq.insert(std::pair<byte,long long>(temp,freq));
count += freq;
}
// This test is passed zo~
std::cout<<"decompress freq map:"<<std::endl;
for (auto it = char_freq.begin(); it != char_freq.end(); it++) {
std::cout << it->first << " " << it->second << std::endl;
}
// Build Huffman tree
vector<Huffman *> tree_list;
for(auto it = char_freq.begin();it != char_freq.end();it++) {
Huffman *tree = new Huffman(true,it->first,it->second);
tree_list.push_back(tree);
}
Huffman *tree = Huffman::build_tree(tree_list);
ofstream outputFile(outputFileName,ofstream::binary);
map<byte, string> char_map;
tree->traverse_tree(tree->get_root(), "", char_map);
HuffmanNode *node = tree->get_root();
// TODO: 反向建立 map? 即,<string,byte> 型?
// string code = "";
queue<char> code;
for(;i < data.size();i++) {
byte temp = data[i];
int j = 0;
for(j = 0;j < 8;j++) {
if((temp & 128) == 128) {
code.push('1');
} else {
code.push('0');
};
temp <<= 1;
}
while(code.size() > 255) {
if(node->isleaf()) {
byte temp = node->get_value();
outputFile.write((const char*)&temp, sizeof(temp));
node = tree->get_root();
}
if(code.front() == '1') {
node = node->get_right_child();
} else {
node = node->get_left_child();
}
code.pop();
}
}
cout<<"code size: "<<code.size()<<std::endl;
// First byte: the remaining bits length
while(code.size() > 16) {
if(node->isleaf()) {
byte temp = node->get_value();
outputFile.write((const char*)&temp, sizeof(temp));
node = tree->get_root();
}
if(code.front() == '1') {
node = node->get_right_child();
} else {
node = node->get_left_child();
}
code.pop();
}
// HuffmanNode *node = tree->get_root();
// for(int i = 0;i < f)
// string sub_code = code.substr(code.length()-16,8);
queue<char> sub_code;
for(int i = 0;i < 8;i++) {
sub_code.push(code.front());
code.pop();
}
int last_len = 0;
for(int j = 0;j < 8;j++) {
last_len <<= 1;
if(sub_code.front() == '1' ) {
last_len = last_len | 1;
}
sub_code.pop();
}
cout<<"last len = "<<last_len<<std::endl;
for(int i = 0;i < last_len;i++) {
if(node->isleaf()) {
byte temp = node->get_value();
outputFile.write((const char *)&temp, sizeof(temp));
node = tree->get_root();
}
if(code.front() == '1') {
node = node->get_right_child();
}
else {
node = node->get_left_child();
}
code.pop();
}
if(node->isleaf()) {
byte temp = node->get_value();
outputFile.write((const char *)&temp, sizeof(temp));
node = tree->get_root();
}
outputFile.close();
inputFile.close();
}