-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman_Compress_2020.cpp
380 lines (356 loc) · 9.4 KB
/
Huffman_Compress_2020.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include<iostream>
#include<vector>
#include<fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;
class Node
{
public:
friend class List;
friend void walk_through(Node *root , string encode);
Node(long int Freq , int Ch)
{
freq=Freq;
ch=Ch;
next=NULL;
left=NULL;
right=NULL;
}
private:
long int freq;
int ch;
Node *next,*left,*right;
};
class List
{
public:
List() { first=NULL; };
Node *first;
int makeList();
void push(int index);
void sorted();
void huffman();
}list;
#define DEBUG false
#define offset 128
long table[256]={0};
string encoding[256]={"\0"};
fstream tmpEncodingTable,tmpEncoding,inputfile,outputfile;
void checkfile(fstream &inputfile);
int frequency(fstream &inputfile);
void Encoding(int count, int OriginalSize,char *outputfileNAME);
void walk_through(Node *root , string encode);
int tmpWrite();
void tmpWrite(fstream &inputfile);
int output(int OriginalSize,int bitNUM);
int toDecimal(string binaryNUM);
char toDecimal(string bytes,int count);
int creatTable(fstream &inputfile);
string toBinary(int data,int size);
void decoding(int num,char *outputfileNAME);
int main(int argc,char **argv)
{
if( (argc!=6) || ((argv[1][1]!='c') && (argv[1][1]!='u')) )
{
cout<<"Error!"<<endl;
cout<<"huffman [-c|-u] –i InputFile –o OutputFile"<<endl;
exit(1);
}
if(argv[1][1]=='c' ) //huffman –c –i infile –o outfile (進行壓縮,infile為輸入檔案,outfile為輸出檔案)
{
inputfile.open(argv[3], ios::in | ios::binary );
checkfile(inputfile);
int OriginalSize = frequency(inputfile);
inputfile.close();
inputfile.seekg(0 ,ios::beg);
int count = list.makeList();
list.sorted();
list.huffman();
Encoding(count,OriginalSize,argv[5]);
}
else if(argv[1][1]=='u' )//huffman –u –i infile –o outfile (進行解壓縮,infile為輸入檔案,outfile為輸出檔案)
{
inputfile.open(argv[3], ios::in | ios::binary );
checkfile(inputfile);
int bit = creatTable(inputfile);
tmpWrite(inputfile);
decoding(bit,argv[5]);
inputfile.close();
}
}
void checkfile(fstream &inputfile)
{
if(!inputfile)
{
cout<<"File did't exit!"<<endl;
exit(1);
}
}
int frequency(fstream &inputfile)
{
char index;
int check=0;
while(inputfile.get(index))
{
check=1;
table[(int)index+offset]++;
}
if(!check)
{
cout<<"File is empty!"<<endl;
exit(1);
}
cout<<"Frequency Table:"<<endl;
for(int i=0 ; i<256 ;i++)
{
cout<<"ascii["<<setw(4)<<i-offset<<"]="<<setw(8)<<table[i]<<" ";
if((i+1)%8==0)
cout<<endl;
}
ifstream::pos_type eof = inputfile.tellg();
return eof;
}
int List::makeList()
{
int count=0;
for(int i=0;i<256;i++)
if(table[i])
{
count++;
push(i);
}
if(DEBUG)
{
cout<<endl<<"Before sort:"<<endl;
for(Node *i=first ; i ; i=i->next)
cout<<"table["<<i->ch-offset<<"]= "<<i->freq<<" ";
cout<<endl;
}
return count;
}
void List::push(int index)
{
Node *tmp=new Node(table[index],index);
if(first)
{
tmp->next=first;
first=tmp;
}
else
first=tmp;
}
void List::sorted()
{
for(Node *i=first; i ; i=i->next)
{
Node *small = i;
for(Node *j=i->next ; j ; j=j->next)
if(j->freq < small->freq)
small=j;
int chTmp;
long freqTmp;
Node *Ltmp,*Rtmp;
chTmp = i->ch;
freqTmp = i->freq;
Ltmp = i->left;
Rtmp = i->right;
i->ch = small->ch;
i->freq = small->freq;
i->left = small->left;
i->right = small->right;
small->ch = chTmp;
small->freq = freqTmp;
small->left = Ltmp;
small->right = Rtmp;
}
if(DEBUG)
{
cout<<"After sort:"<<endl;
for(Node *i=first ; i ; i=i->next)
{
cout<<"table["<<setw(4)<<i->ch-offset<<"]= "<<setw(4)<<i->freq<<" Next= "<<setw(15)<<i->next;
cout<<" Left= "<<setw(15)<<i->left;
cout<<" Right= "<<setw(15)<<i->right<<endl;
}
cout<<endl;
}
}
void List::huffman()
{
while(first->next)
{
Node *tmp = new Node(first->freq + first->next->freq,-871);
Node *L= first ,*R= first->next;
tmp->next = first->next->next;
tmp->left = L;
tmp->right = R;
first->next = NULL;
first = tmp;
sorted();
if(DEBUG)
cout<<"tmp->freq= "<<tmp->freq<<"\ttmp->ch= "<<tmp->ch<<"\ttmp->next= "<<tmp->next<<endl;
}
}
void Encoding(int count, int OriginalSize,char *outputfileNAME)
{
tmpEncodingTable.open("tmpEncodingTable",ios::out | ios::binary );
tmpEncoding.open("tmpEncoding",ios::out | ios::binary );
outputfile.open(outputfileNAME, ios::out | ios::binary);
if(DEBUG)
{
if(!tmpEncodingTable) cout<<"~~~~~~~~~~~~~~~~~~~FUCK 1~~~~~~~~~~~~~~~~~~~"<<endl;
if(!tmpEncoding) cout<<"~~~~~~~~~~~~~~~~~~~FUCK 2~~~~~~~~~~~~~~~~~~~"<<endl;
}
tmpEncodingTable<<count<<" ";
walk_through(list.first , "\0");
tmpEncodingTable<<endl;
int bitNUM = tmpWrite();
tmpEncodingTable.close();
tmpEncoding.close();
tmpEncodingTable.open("tmpEncodingTable",ios::in | ios::binary );
tmpEncoding.open("tmpEncoding",ios::in | ios::binary );
int CompressedSize = output(OriginalSize,bitNUM);
tmpEncodingTable.close();
tmpEncoding.close();
if(DEBUG) cout<<"OriginalSize= "<<OriginalSize<<" CompressedSize= "<<CompressedSize<<endl;
cout<<endl<<"Retio: "<<(double)OriginalSize/(double)CompressedSize<<endl;
}
int toDecimal(string binaryNUM)
{
int i, two=1,decimal=0;
for(i=binaryNUM.size()-1; i>=0 ; i--,two*=2)
decimal+=((int)binaryNUM[i]-48)*two;
return decimal;
}
char toDecimal(string bytes,int count)
{
char ans;
int i, two=1,decimal=0;
for(i=0 ; i<8-count ; i++)
bytes+="0";
for(i=7; i>=0 ; i--,two*=2 )
decimal+=((int)bytes[i]-48)*two;
ans=(char)(decimal-offset);
return ans;
}
void walk_through(Node *root , string encode)
{
if(!root) return;
walk_through(root->left, encode+'0');
if(root->left==NULL && root->right==NULL)
{
tmpEncodingTable<<root->ch<<" "<<encode.size()<<" "<<toDecimal(encode)<<" ";
encoding[root->ch]=encode;
if(DEBUG)
cout<<"root->ch= "<<root->ch-offset<<" encode.size()= "<<encode.size()<<" encode="<<setw(7)<<encode<<" toDecimal(encode)= "<<toDecimal(encode)<<endl;
}
walk_through(root->right, encode+'1');
}
int tmpWrite()
{
char index;
int count=0;
while(inputfile.get(index))
{
tmpEncoding<<encoding[(int)index+offset];
count+=encoding[(int)index+offset].size();
if(DEBUG)
cout<<encoding[(int)index+offset]<<" ";
}
if(DEBUG) cout<<endl;
return count;
}
int output(int OriginalSize,int bitNUM)
{
char tmp;
while(tmpEncodingTable.get(tmp))
outputfile<<tmp;
outputfile<<bitNUM<<" ";
system("rm tmpEncodingTable");
string bytes="";
while(tmpEncoding.get(tmp))
{
bytes+=tmp;
if(bytes.size()==8)
{
outputfile<<toDecimal(bytes,8);
bytes="";
}
}
outputfile<<toDecimal(bytes,bytes.size());
system("rm tmpEncoding");
ifstream::pos_type eof = outputfile.tellg();
return eof;
}
int creatTable(fstream &inputfile)
{
int i,count,num;
inputfile>>count;
if(DEBUG) cout<<count<<endl;
for(i=0;i<count;i++)
{
int index,size,data;
inputfile>>index>>size>>data;
if(DEBUG) cout<<index<<endl;
encoding[index]=toBinary(data,size);
}
inputfile>>num;
char trash;
inputfile.get(trash);
return num;
}
string toBinary(int data,int size)
{
string ans="\0";
int i;
long int t=data;
char input[size]= {0};
for(i=0; i<size; i++)
{
input[i]=data%2;
data/=2;
}
for(i=size-1 ; i>=0 ; i--)
{
if(input[i]==1)
ans+='1';
else
ans+='0';
}
return ans;
}
void tmpWrite(fstream &inputfile)
{
char index;
tmpEncoding.open("tmpEncoding",ios::out | ios::binary );
while(inputfile.get(index))
tmpEncoding<<toBinary(index+offset,8);
tmpEncoding.close();
}
void decoding(int num,char *outputfileNAME)
{
outputfile.open(outputfileNAME, ios::out | ios::binary);
tmpEncoding.open("tmpEncoding",ios::in | ios::binary );
int i,j;
char get;
string decode="\0";
cout<<"Decoding......"<<endl;
for(i=0; i<num; i++)
{
tmpEncoding.get(get);
decode+=get;
for(j=0 ; j<256 ; j++)
if(encoding[j]==decode)
{
if(DEBUG) cout<<"decode= "<<decode<<endl;
outputfile<<(char)(j-offset);
decode="\0";
break;
}
}
cout<<"Complete!"<<endl;
tmpEncoding.close();
outputfile.close();
system("rm tmpEncoding");
}