-
Notifications
You must be signed in to change notification settings - Fork 1
/
HuffmanDeCompressor.hpp
374 lines (302 loc) · 9.21 KB
/
HuffmanDeCompressor.hpp
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
#include <iostream>
#include <string>
#include <string.h>
#include "include/library/cppBL.hpp"
#include "include/HuffmanSetup.hpp"
#include "include/Progress.hpp"
// Retorna la cantidad de bytes de un archivo
int fileBytes(FILE* f)
{
return fileSize<unsigned char>(f);
}
// init function
HuffmanTable huffmanTable(unsigned int n, string cod)
{
HuffmanTable hT;
hT.n=n;
hT.cod=cod;
return hT;
}
int cmpHuffmanTreeInfoOrder(HuffmanTreeInfo* a,HuffmanTreeInfo* b)
{
int frst = a->n-b->n;
int scnd = a->c-b->c;
if(frst==0)
{
return scnd;
}
return frst;
}
int cmpHuffmanTreeInfoFind(HuffmanTreeInfo* a, unsigned int b)
{
return a->c-b;
}
int cmpHuffmanTableFind(HuffmanTable a, unsigned char b)
{
return a.n-b;
}
int cmpHuffmanTableFind2(HuffmanTable a, string cod)
{
return cmpString(a.cod,cod);
}
int cmpHuffmanTableOrder(HuffmanTable a, HuffmanTable b)
{
return length(a.cod)-length(b.cod);
}
void byteDiscover(unsigned int c,List<HuffmanTreeInfo*>& lByte)
{
HuffmanTreeInfo** hti = listFind<HuffmanTreeInfo*,unsigned int>(lByte,c,cmpHuffmanTreeInfoFind);
if(hti==NULL)
{
HuffmanTreeInfo* x = huffmanTreeInfo(c,0,NULL,NULL);
hti = listAdd<HuffmanTreeInfo*>(lByte,x);
}
(*hti)->n++;
}
List<HuffmanTreeInfo*> createByteList(FILE* f)
{
List<HuffmanTreeInfo*> lByte = list<HuffmanTreeInfo*>();
unsigned int c = (unsigned int)read<unsigned char>(f);
while(!feof(f))
{
byteDiscover(c,lByte);
c = (unsigned int)read<unsigned char>(f);
}
return lByte;
}
HuffmanTreeInfo* byteListToHuffmanTree(List<HuffmanTreeInfo*> lByte)
{
listSort<HuffmanTreeInfo*>(lByte,cmpHuffmanTreeInfoOrder);
while(listSize(lByte)>1)
{
HuffmanTreeInfo* lft = listRemoveFirst<HuffmanTreeInfo*>(lByte);
HuffmanTreeInfo* rgt = listRemoveFirst<HuffmanTreeInfo*>(lByte);
HuffmanTreeInfo* ast = huffmanTreeInfo(256,rgt->n+lft->n,lft,rgt);
listOrderedInsert<HuffmanTreeInfo*>(lByte,ast,cmpHuffmanTreeInfoOrder);
}
HuffmanTreeInfo* root = lByte.p->info;
listFree<HuffmanTreeInfo*>(lByte);
return root;
}
List<HuffmanTable> rootToHuffmanTable(HuffmanTreeInfo* root)
{
HuffmanTree hTree = huffmanTree(root);
List<HuffmanTable> lHTable = list<HuffmanTable>();
string cod;
while(huffmanTreeHasNext(hTree))
{
HuffmanTreeInfo* hTINext = huffmanTreeNext(hTree,cod);
HuffmanTable hTable = huffmanTable(hTINext->c,cod);
listAdd<HuffmanTable>(lHTable,hTable);
}
listSort<HuffmanTable>(lHTable,cmpHuffmanTableOrder);
return lHTable;
}
unsigned char codSize(HuffmanTable* x)
{
int a = length(x->cod);
return (unsigned char)a;
}
void generateMetadata(FILE* f,FILE* fHuf, List<HuffmanTable> lHTable)
{
unsigned short leafCount = listSize<HuffmanTable>(lHTable);
write<unsigned short>(fHuf,leafCount);
listReset<HuffmanTable>(lHTable);
while(listHasNext<HuffmanTable>(lHTable))
{
HuffmanTable* x = listNext<HuffmanTable>(lHTable);
unsigned short cS = codSize(x);
unsigned int codNum = stringToInt(x->cod,2);
write<unsigned char>(fHuf,x->n);
write<unsigned short>(fHuf,cS);
write<unsigned int>(fHuf,codNum);
}
unsigned int fSize = fileBytes(f);
write<unsigned int>(fHuf,fSize);
}
string compressByte(unsigned char c, List<HuffmanTable> lHTable)
{
HuffmanTable* hTable = listFind<HuffmanTable,unsigned char>(lHTable,c,cmpHuffmanTableFind);
return hTable->cod;
}
void loadCompressedData(FILE* f, FILE* fHuf, List<HuffmanTable> lHTable) {
Progress p = progress("Estado de la compresion: "); // Inicializar el objeto de progreso
progressInit(p);
seek<int>(f, 0);
BitWriter bW = bitWriter(fHuf);
unsigned char c = read<unsigned char>(f);
// Obtener el tamaño total del archivo
unsigned int fileSize = fileBytes(f);
// Definir una constante para el tamaño del búfer de progreso
const int progressBufferSize = 1024;
unsigned int bytesProcessed = 0;
int lastProgress = 0;
while (!feof(f)) {
string cod = compressByte(c, lHTable);
bitWriterWrite(bW, cod);
// Actualizar el progreso
bytesProcessed++;
if (bytesProcessed % progressBufferSize == 0 || bytesProcessed == fileSize) {
int currProgress = (bytesProcessed * 100) / fileSize;
if (currProgress != lastProgress) {
progressShow(p, currProgress);
lastProgress = currProgress;
}
}
if (length(bW.s) % 8 == 0) {
bitWriterFlush(bW);
}
c = read<unsigned char>(f);
}
progressDestroy(p,"Compresion");
bitWriterFlush(bW);
}
void generateHuffmanFile(FILE* f, List<HuffmanTable> lHTable,string fName)
{
fName+=".huf";
char arr[length(fName)];
strcpy(arr,fName.c_str());
FILE* fHuf = fopen(arr,"w+b");
// Se generan y escriben los datos del arbol
generateMetadata(f,fHuf,lHTable);
// Se carga la información recodificada
cout << "Comprimiendo archivo..." << endl;
loadCompressedData(f,fHuf,lHTable);
fclose(fHuf);
}
void huffmanCompressor(string fName)
{
char arr[length(fName)];
strcpy(arr,fName.c_str());
FILE* f = fopen(arr,"r+b");
cout << "Leyendo archivo..." << endl;
// Se crea la lista de bytes con sus ocurrencias
List<HuffmanTreeInfo*> lByte = createByteList(f);
// Se obtiene, a partir de la lista, la raíz del árbol de Huffman
HuffmanTreeInfo* root = byteListToHuffmanTree(lByte);
// Se convierte a la raiz en una tabla con los datos de las hojas
List<HuffmanTable> lHTable = rootToHuffmanTable(root);
// Se genera el archivo comprimido
generateHuffmanFile(f,lHTable,fName);
fclose(f);
}
string regToCod(unsigned short cS,unsigned int codNum)
{
string cod = _binToString<unsigned int>(codNum);
return substring(cod,(int)(length(cod)-cS));
}
List<HuffmanTable> readHuffmanTable(FILE* f)
{
List<HuffmanTable> lHTable = list<HuffmanTable>();
int leafCount = (int)read<unsigned short>(f);
for(int i=0;i<leafCount;i++)
{
unsigned char byte = read<unsigned char>(f);
unsigned short cS = read<unsigned short>(f);
unsigned int codNum = read<unsigned int>(f);
string cod = regToCod(cS,codNum);
listAdd<HuffmanTable>(lHTable,huffmanTable(byte,cod));
}
listSort<HuffmanTable>(lHTable,cmpHuffmanTableOrder);
return lHTable;
}
bool isLeaf(HuffmanTreeInfo* aux)
{
if(aux->left==NULL && aux->right==NULL)
{
return true;
}
return false;
}
unsigned char decode(HuffmanTreeInfo* root,BitReader& bR)
{
string provCod;
HuffmanTreeInfo* aux = root;
while(!isLeaf(aux))
{
provCod+=intToString(bitReaderRead(bR));
aux = provCod[length(provCod)-1]=='1'?aux->right:aux->left;
}
return aux->c;
}
void decompress(FILE* f, HuffmanTreeInfo* root, string fName) {
fName = substring(fName, 0, length(fName) - 4);
char arr[length(fName)];
strcpy(arr, fName.c_str());
FILE* fD = fopen(arr, "w+b");
unsigned int fSize = read<unsigned int>(f);
BitReader bR = bitReader(f);
cout << "Descomprimiendo archivo..." << endl;
Progress p = progress("Estado de la descompresion:"); // Tamaño de la barra de progreso: 50 caracteres
progressInit(p);
for (int i = 0; i < (int)fSize; i++) {
unsigned char leafByte = decode(root, bR);
write<unsigned char>(fD, leafByte);
// Mostrar progreso
int currProgress = (i * 100) / fSize;
progressShow(p, currProgress);
}
progressDestroy(p,"Descompresion");
fclose(fD);
}
void leafPut(HuffmanTreeInfo* aux,HuffmanTable* hT,string cod)
{
if(cod=="1")
{
aux->right = huffmanTreeInfo(hT->n,0,NULL,NULL);
}
else
{
aux->left = huffmanTreeInfo(hT->n,0,NULL,NULL);
}
}
void leafDiscover(HuffmanTable* hT,HuffmanTreeInfo* root)
{
string cod = hT->cod;
HuffmanTreeInfo* aux = root;
HuffmanTreeInfo* ant = NULL;
while(length(cod)>1)
{
char num = cod[0];
cod = removeAt(cod,0);
ant = aux;
aux = num=='1'?aux->right:aux->left;
if(aux==NULL)
{
aux = huffmanTreeInfo(256,0,NULL,NULL);
}
if(num=='1')
{
ant->right = aux;
}
else
{
ant->left = aux;
}
}
leafPut(aux,hT,cod);
}
HuffmanTreeInfo* huffmanTableToRoot(List<HuffmanTable> lHTable)
{
HuffmanTreeInfo* root = huffmanTreeInfo(256,0,NULL,NULL);
listReset<HuffmanTable>(lHTable);
while(listHasNext<HuffmanTable>(lHTable))
{
HuffmanTable* hT = listNext<HuffmanTable>(lHTable);
leafDiscover(hT,root);
}
return root;
}
void huffmanDecompressor(string fName)
{
char arr[length(fName)];
strcpy(arr,fName.c_str());
FILE* f = fopen(arr,"r+b");
// Se interpretan los primeros registros del archivo y se cargan en una tabla
List<HuffmanTable> lHTable = readHuffmanTable(f);
// Se convierte a la tabla en raíz del árbol de Huffman
HuffmanTreeInfo* root = huffmanTableToRoot(lHTable);
// Se descomprimen y escriben los datos
decompress(f,root,fName);
fclose(f);
}