forked from patricksheehan/Huffman-Compression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
132 lines (113 loc) · 2.98 KB
/
main.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
// Patrick Sheehan
#include "huff.h"
#include <iostream>
#include <cstring>
void compress();
void putOut();
void decompress();
Node * constructHeap();
unsigned int frequencies[256] = {0};
string codebook[256];
int main(int argc, char* argv[]) {
if(argc == 2){
if((argv[1][0] == '-') && (argv[1][1] == 'd'))
decompress();
}
else
compress();
return 0;
}
void compress(){
unsigned char nextChar;
// first, calculate the frequencies of each character
cin >> noskipws;
while(cin >> nextChar)
frequencies[nextChar]++;
Node * root = constructHeap();
string code;
root->fillCodebook(codebook,code);
putOut();
}
void putOut(){
cout<< "HUFFMA3" << '\0';
unsigned int i;
for(i = 0; i < 256; i++){
cout<<(char) (0x000000ff & frequencies[i]);
cout<<(char) ((0x0000ff00 & frequencies[i]) >> 8);
cout<<(char) ((0x00ff0000 & frequencies[i]) >> 16);
cout<<(char) ((0xff000000 & frequencies[i]) >> 24);
}
unsigned char nextChar;
char nextByte = 0;
int bitCounter = 0;
cin.clear();
cin.seekg(0);
cin >> noskipws;
while(cin >> nextChar){
for(i = 0; i < codebook[nextChar].size(); i++, bitCounter++){
if(bitCounter == 8){
cout<< nextByte;
nextByte = 0;
bitCounter = 0;
}
if(codebook[nextChar][i] == '1')
nextByte = nextByte | (0x01 << bitCounter);
}
}
if(bitCounter)
cout << nextByte;
}
void decompress(){
cin >> noskipws;
char magic[8];
cin.read(magic,8);
char nextByte;
for(int i = 0; i < 256; i++){
cin.read((char *)&frequencies[i],4);
}
Node * root = constructHeap();
string code;
root->fillCodebook(codebook,code);
while(cin>>nextByte){
for(int i = 0; i < 8; i++){
if((nextByte >> i) & 0x01)
code += '1';
else
code += '0';
for(int i = 0; i < 256; i++){
if(codebook[i] == code){
if(frequencies[i]){
cout << (unsigned char) i;
code.clear();
frequencies[i]--;
break;
}
else
return;
}
} // for
}
}
}
Node * constructHeap(){
Heap minHeap;
Node *nextNode;
for(int i = 0; i < 256; i++){
if (frequencies[i]){
nextNode = new Node(i, frequencies[i]);
minHeap.push(nextNode);
}
}
Node * node1;
Node * node2;
Node * merged;
while(minHeap.size() > 1){
node1 = minHeap.top();
minHeap.pop();
node2 = minHeap.top();
minHeap.pop();
merged = new Node(node1, node2);
minHeap.push(merged);
}
return minHeap.top();
}