-
Notifications
You must be signed in to change notification settings - Fork 1
/
huffman.cpp
149 lines (131 loc) · 3.31 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
#include "huffman.hpp"
HuffmanCoding::HuffmanCoding(std::string fileName)
{
IO.setTargetName(fileName);
}
void HuffmanCoding::constructFreqTable()
{
for (auto value : input)
{
freqTable[value]++;
}
}
void HuffmanCoding::constructTree()
{
struct freqCompare
{
bool operator()(Node *l, Node *r)
{
return (l->freq > r->freq);
}
};
std::priority_queue<Node *, std::vector<Node *>, freqCompare> pq;
for (auto pixel : freqTable)
{
Node *n = new Node{pixel.first, pixel.second};
pq.push(n);
}
Node *parent;
while (pq.size() != 1)
{
Node *left = pq.top();
pq.pop();
Node *right = pq.top();
pq.pop();
parent = new Node;
parent->freq = left->freq + right->freq;
parent->left = left;
parent->right = right;
pq.push(parent);
}
builtTree = parent;
char word[255];
constructCodeWords(builtTree, word, 0);
}
void HuffmanCoding::constructCodeWords(Node *parent, char *word, int index)
{
if (parent->left)
{
word[index] = '0';
constructCodeWords(parent->left, word, index + 1);
}
if (parent->right)
{
word[index] = '1';
constructCodeWords(parent->right, word, index + 1);
}
if (!(parent->right || parent->left))
{
std::string codeWord(word, word + index);
leafCodeWord[parent->greyScaleValue] = codeWord;
}
}
void HuffmanCoding::createEncoded()
{
for (auto element : input)
{
std::string binary = leafCodeWord[element];
strEncoded += binary;
}
encodedLength = strEncoded.length();
}
void HuffmanCoding::toDecimal()
{
decimalPacked.clear();
std::string str = "";
for (auto e : strEncoded)
{
str.push_back(e);
if (str.size() == 8)
{
decimalPacked.push_back(std::bitset<8>(str).to_ulong());
str = "";
}
}
int numOfZeros = strEncoded.length() % 8;
if (numOfZeros != 0)
{
for (int i = 0; i < 8 - numOfZeros; ++i)
{
str.push_back('0');
}
if (str.size() == 8)
{
decimalPacked.push_back(std::bitset<8>(str).to_ulong());
}
}
}
void HuffmanCoding::traverseDown()
{
for (int i = 0; i < encodedLength;)
{
Node *current = builtTree;
while (current->left != nullptr && current->right != nullptr)
{
if (strToDecode[i] == '0')
current = current->left;
else
current = current->right;
i++;
}
decoded.push_back(current->greyScaleValue);
}
}
void HuffmanCoding::encode(std::string filePath)
{
IO.pgmRead(filePath, filetype, rows, cols, greyscaleMax, size, input);
constructFreqTable();
constructTree();
createEncoded();
toDecimal();
IO.freqWrite(filetype, rows, cols, greyscaleMax, encodedLength, freqTable);
IO.hufWrite(decimalPacked);
}
void HuffmanCoding::decode(std::string hufDestination, std::string freqDestination)
{
IO.freqRead(freqDestination, filetype, rows, cols, greyscaleMax, size, encodedLength, freqTable);
constructTree();
IO.hufRead(hufDestination, strToDecode);
traverseDown();
IO.pgmWrite(filetype, rows, cols, greyscaleMax, decoded);
}