-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.cpp
More file actions
153 lines (131 loc) · 3.38 KB
/
encoder.cpp
File metadata and controls
153 lines (131 loc) · 3.38 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
#include "encoder.h"
Encoder::Encoder(){}
Encoder::~Encoder(){}
void Encoder::createHistogram(ifstream &fin)
{
char input;
unordered_map<char,int>::iterator it;
for(int i = -1; i < 128; i++)
{
histogram[i] = 0;
}
//read till end of file
while(!fin.eof())
{
input = fin.get();
it = histogram.find(input);
//get character and if found in histogram increment else add
if(it == histogram.end())
{
histogram.insert(pair<char,int>(input,1));
}
else
{
histogram[it->first]++;
}
}
}
void Encoder::createEncodedFile(ifstream &fin, ofstream &fout, unordered_map<char,string> huffmanCodes)
{
unordered_map<char,int>::iterator it;
for(int i = -1; i < 128; i++)
{
fout<<histogram[i]<<endl;
}
unsigned char buffer;
char input;
string temp = "";
//Pass through input file once again
//If character is found then look it up in the codes
//Store code in buffer
bytesRead = 0;
bytesWritten = 0;
while(!fin.eof())
{
input = fin.get();
temp += huffmanCodes[input];
bytesRead++;
//If size of string is greater than 8 convert it to
//binary and print to file
if(temp.size() >= 8)
{
string binary = temp.substr(0,7);
temp.erase(0,7);
buffer = storeInChar(binary);
fout<<buffer;
bytesWritten++;
}
}
//Left over characters
if(temp.size() != 0)
{
while(temp.size() != 8)
temp += '0';
string binary = temp.substr(0,7);
temp.erase(0,7);
buffer = storeInChar(binary);
fout<<buffer;
bytesWritten++;
}
}
//Converts string to decimal and returns character representing decimal
unsigned char Encoder::storeInChar(string binary)
{
reverse(binary.begin(),binary.end());
unsigned int decimal = 0;
for(int i = 0; i < 8; i++)
{
if(binary[i] == '1')
{
decimal += pow(2,i);
}
}
unsigned char character = decimal;
return character;
}
void Encoder::printEncoderInformation(unordered_map<char,string> huffmanCodes,ofstream &fout)
{
cout<<"Huffman Encoder pass one"<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"Read "<<bytesRead<<" bytes from input file, "<<histogram.size()<<" code words found"<<endl;
cout<<"\n\n";
cout<<"Huffman code table"<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"ASCII Code"<<setw(30)<<" Huffman Codes"<<endl;
unordered_map<char,string>::iterator it;
char seperator = ' ';
for(it = huffmanCodes.begin(); it != huffmanCodes.end(); it++)
{
if(histogram[it->first] != 0)
{
if(int(it->first) == -1)
{
cout<<"EOF "<<" ( "<<int(it->first)<<" ) ";
cout<<right<<setw(20)<<setfill(seperator)<<it->second<<endl;
}
else if(int(it->first) == 10)
{
cout<<"newline "<<"( "<<int(it->first)<<" ) ";
cout<<right<<setw(20)<<setfill(seperator)<<it->second<<endl;
}
else
{
cout<<" "<<it->first<<" ( "<<int(it->first)<<" )";
cout<<right<<setw(20)<<setfill(seperator)<<it->second<<endl;
}
}
}
cout<<"\n\n";
cout<<"Huffman Encoder Pass two"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"Wrote "<<bytesWritten<<" bytes to output file excluding histogram at the begining(258 bytes)"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"Compression ratio is "<<(double(bytesWritten)/double(bytesRead))*100;
cout<<" percent"<<endl;
cout<<"--------------------------------------"<<endl;
}
//getter for histogram
unordered_map<char,int> Encoder:: getHistogram()
{
return histogram;
}