-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.cpp
262 lines (262 loc) · 8.42 KB
/
huffman.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#define BUFF_SIZE 2000
using namespace std;
//Huffman tree node
struct Huff
{
size_t weight{0};
char code; //code: 0~255
Huff *lchild{nullptr}, *rchild{nullptr};
};
//Huffman tree node for decoding
struct Huff_D
{
Huff_D *lchild{nullptr}, *rchild{nullptr};
char code; //code: -128~127c
};
//Append HuffTree to Standard I/O, and get codemap[source]=huffcode
void OutputHuffTree(ostream &out, Huff *root, vector<bool> &path, vector<vector<bool>> &codemap)
{
if (!root->lchild and !root->rchild) //leaf node
{
codemap[root->code + 128] = path;
out << char(path.size());
unsigned char code = 0;
int ind = 0;
for (bool c : path)
{
code = (code << 1) + c;
++ind;
if (ind == 8)
{
ind = 0;
out << code;
code = 0;
}
}
if (ind)
out << code;
out << root->code;
}
if (root->lchild)
{
path.push_back(0);
OutputHuffTree(out, root->lchild, path, codemap);
path.pop_back();
}
if (root->rchild)
{
path.push_back(1);
OutputHuffTree(out, root->rchild, path, codemap);
path.pop_back();
}
}
//Print error and exit with code 1
void Throw(const string &Error)
{
cerr << "\033[31mERROR: \033[0m" << Error << endl; //[red]ERROR: [/red]
exit(1);
}
int main(int argc, char **argv)
{
string InFilePath, OutFilePath;
bool WorkMode = 0; //0->zip, 1->unzip
//Parse Command
switch (argc)
{
case 1: //huff
Throw("No File Specified");
break;
case 2: //huff infile
InFilePath = argv[1];
OutFilePath = InFilePath + ".huff";
break;
case 3: //huff -u/z infile
if (argv[1][0] != '-')
Throw("No Option Specified");
if (argv[1][1] == 'u')
WorkMode = 1;
else if (argv[1][1] == 'z')
WorkMode = 0;
else
Throw("Cannot Read Augments");
InFilePath = argv[2];
OutFilePath = InFilePath + ((argv[1][1] == 'z') ? ".huff" : ".src");
break;
case 4: //huff -ur/zr infile outfile
if (argv[1][0] != '-')
Throw("No Option Specified");
if ((argv[1][1] == 'u' and argv[1][2] == 'r') or (argv[1][1] == 'r' and argv[1][2] == 'u'))
WorkMode = 1;
else if ((argv[1][1] == 'z' and argv[1][2] == 'r') or (argv[1][1] == 'r' and argv[1][2] == 'z'))
WorkMode = 0;
else
Throw("Cannot Read Augments");
InFilePath = argv[2];
OutFilePath = argv[3];
break;
default:
Throw("Too Many Augments Passed");
break;
}
//compress mode
if (!WorkMode)
{
//read file
ifstream in(InFilePath, ios::in | ios::binary);
if (!in)
Throw("Cannot Open File");
in.seekg(0, in.end);
const size_t FileSize = in.tellg();
in.seekg(0, in.beg);
if (!in)
Throw("Cannot Read File to Memory");
//count occurance
char *buffer = new char[BUFF_SIZE]();
size_t *Times = new size_t[256]();
for (size_t i = 0; i < FileSize; ++i)
{
if (!(i % BUFF_SIZE)) //read new buffer block
in.read(buffer, BUFF_SIZE);
Times[buffer[i % BUFF_SIZE] + 128]++; //char: -128~127 +128 -> 0~255
}
//construct huffman tree
struct cmp //use ascending priority queue
{
bool operator()(const Huff *n1, const Huff *n2) { return n1->weight > n2->weight; }
};
priority_queue<Huff *, vector<Huff *>, cmp> NodeList;
for (int i = 0; i < 256; ++i)
if (Times[i])
{
Huff *p = new Huff;
p->weight = Times[i];
p->code = i - 128;
NodeList.push(p);
}
ofstream out(OutFilePath, ios::out | ios::binary);
out << char(NodeList.size() - 1); //output treesize to head part, -1 for 256->255
while (NodeList.size() > 1)
{
Huff *p, *t;
p = NodeList.top();
NodeList.pop();
t = NodeList.top();
NodeList.pop();
Huff *h = new Huff;
h->weight = p->weight + t->weight;
h->lchild = p;
h->rchild = t;
NodeList.push(h);
}
Huff *root = NodeList.top();
//output huffman tree
vector<vector<bool>> codemap;
vector<bool> path;
codemap.resize(256);
if (!out)
Throw("Cannot Open Output File");
OutputHuffTree(out, root, path, codemap);
//output compressed bitstream
size_t bilen = 0;
char *outputbuffer = new char[BUFF_SIZE]();
in.close();
in.open(InFilePath, ios::in | ios::binary); //reopen file
for (size_t i = 0; i < FileSize; ++i)
{
if (!(i % BUFF_SIZE)) //read new buffer block
in.read(buffer, BUFF_SIZE);
for (bool c : codemap[buffer[i % BUFF_SIZE] + 128])
{
outputbuffer[(bilen / 8) % BUFF_SIZE] = (outputbuffer[(bilen / 8) % BUFF_SIZE] << 1) + c;
if (!(++bilen % (BUFF_SIZE * 8))) //buffer full, write to file
out.write(outputbuffer, BUFF_SIZE);
}
}
if (bilen % (8 * BUFF_SIZE))
out.write(outputbuffer, (bilen / 8) % BUFF_SIZE + 1);
//output align len
out << char(8 - bilen % 8);
}
else //uncompress mode
{
ifstream in(InFilePath, ios::in | ios::binary);
if (!in)
Throw("Cannot Open File");
//read file
in.seekg(-1, in.end);
size_t FileSize = in.tellg(); //ignore last and first node(align/treesize)
char align;
in.read(&align, 1); //read align size at last char
if (!in or align > 7)
Throw("File Corrupted");
in.seekg(0, in.beg);
char *buffer = new char[BUFF_SIZE](), hufflen;
//create huffman tree
Huff_D *root = new Huff_D, *t;
if (!in)
Throw("File Corrupted");
unsigned treesize = in.get() + 1, pos = 0; //position in buffer
for (; treesize > 0; --treesize) //read &create huffman tree
{
t = root;
if (!(pos % BUFF_SIZE))
in.read(buffer, BUFF_SIZE);
for (hufflen = buffer[(pos++) % BUFF_SIZE]; hufflen > 0; hufflen -= 8)
{
if (!(pos % BUFF_SIZE))
in.read(buffer, BUFF_SIZE);
for (int j = hufflen > 8 ? 7 : hufflen - 1; j >= 0; --j)
{
if ((buffer[pos % BUFF_SIZE] >> j) % 2)
{
if (!t->rchild)
t->rchild = new Huff_D;
t = t->rchild;
}
else
{
if (!t->lchild)
t->lchild = new Huff_D;
t = t->lchild;
}
}
++pos;
}
if (!(pos % BUFF_SIZE))
in.read(buffer, BUFF_SIZE);
t->code = buffer[(pos++) % BUFF_SIZE];
}
//read, uncompress and output bitdata
ofstream out(OutFilePath, ios::out | ios::binary);
char *outputbuffer = new char[BUFF_SIZE]();
unsigned outputpos = 0;
for (t = root; pos < FileSize - 1; ++pos)
{
if (!(pos % BUFF_SIZE))
in.read(buffer, BUFF_SIZE);
for (int j = (pos == FileSize - 2) ? 7 - align : 7; j >= 0; --j) //note align for last node
{
if ((buffer[pos % BUFF_SIZE] >> j) % 2)
t = t->rchild;
else
t = t->lchild;
if (!t->lchild and !t->rchild) //reach leaf node
{
outputbuffer[outputpos++ % BUFF_SIZE] = t->code;
if (!(outputpos % BUFF_SIZE)) //buffer full, write to file
out.write(outputbuffer, BUFF_SIZE);
t = root;
}
}
}
if (t != root)
Throw("File Corrupted");
out.write(outputbuffer, outputpos % BUFF_SIZE); //clean buffer
}
return 0;
}