-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
687 lines (502 loc) · 20 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//Name: Mohamed Ahmed Mokhtar
//ID: 18105910
//name of input file for compress is: input.txt
//name of output file of compress is: output.dat
//name of input file for decompress is: output.dat
//name of output file for decompress is: output.txt
//codes.dat conatain number of extra bits used in compression and variable length table
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <math.h>
//used for creating Huffman tree
struct HuffmanTreeNode {
int frequency;
unsigned char character;
HuffmanTreeNode *right;
HuffmanTreeNode *left;
};
//used for creating sorted list for HuffmanTreeNode
struct HuffmanLinkedListNode {
HuffmanTreeNode *huffmanNode;
HuffmanLinkedListNode *next;
};
struct IntLinkedListNode{
int data;
IntLinkedListNode *next;
};
//used for storing variable length (Binary)
struct Stack {
IntLinkedListNode *top;
int size;
Stack() {
top = NULL;
size = 0;
}
};
//store variable length code for each char
struct VariableLengthNode {
int *variableLength;
int size;
char character;
};
//list contain variable length of each char
//sorted from least size
struct VariableLengthLinkedList {
VariableLengthNode *variableLengthNode;
VariableLengthLinkedList *next;
};
HuffmanTreeNode *CreateHuffmanNode(int frequency, char character, HuffmanTreeNode *left, HuffmanTreeNode *right) {
HuffmanTreeNode *newNode = (HuffmanTreeNode *) malloc(sizeof(HuffmanTreeNode));
newNode->frequency = frequency;
newNode->character = character;
newNode->right = right;
newNode->left = left;
return newNode;
}
//create Huffman node for the lowest frequency in the ascii array
HuffmanLinkedListNode *CreateMinFrequencyNode(int ascii[]) {
int minIndex = 0;
for (int i = 0; i < 256; i++) {
if (ascii[i] > 0) {
if (minIndex == 0)
minIndex = i;
else if (ascii[i] < ascii[minIndex])
minIndex = i;
}
}
//all nodes frequency are stored
if (minIndex == 0)
return NULL;
else {
HuffmanLinkedListNode *newNode = (HuffmanLinkedListNode *) malloc(sizeof(HuffmanLinkedListNode));
newNode->huffmanNode = CreateHuffmanNode(ascii[minIndex], minIndex, NULL, NULL);
newNode->next = NULL;
ascii[minIndex] = 0; //to avoid using it again
return newNode;
}
}
//create sorted linked list from lowest frequency to the highest one
HuffmanLinkedListNode *CreateSortedLinkedList(int ascii[]) {
HuffmanLinkedListNode *headNode;
headNode = CreateMinFrequencyNode(ascii);
HuffmanLinkedListNode *currentListNode = headNode;
//if all frequency are stored CreateMinFrequency returns NULL
while (currentListNode != NULL) {
currentListNode->next = CreateMinFrequencyNode(ascii);
currentListNode = currentListNode->next;
}
return headNode;
}
//this function use sorted linked list to make huffman tree
//return head of huffman tree
HuffmanTreeNode *BuildHuffmanTree(HuffmanLinkedListNode **parentNode) {
//currentNode used for iteration
HuffmanLinkedListNode *currentNode = *parentNode;
//if the list has only one node end loop
while (currentNode->next != NULL) {
//newNode has the sum of the lowest 2 frequency in the sorted linked list
//and point to both of them
//and has NULL character
HuffmanLinkedListNode *newNode = (HuffmanLinkedListNode *) malloc(sizeof(HuffmanLinkedListNode));
newNode->next = NULL;
newNode->huffmanNode = CreateHuffmanNode(
currentNode->huffmanNode->frequency + currentNode->next->huffmanNode->frequency, NULL,
currentNode->huffmanNode, currentNode->next->huffmanNode);
//to make the current node the second node
currentNode = currentNode->next;
//loop to sort the new node in the sorted linked list
while (currentNode->next != NULL &&
currentNode->next->huffmanNode->frequency <= newNode->huffmanNode->frequency)
currentNode = currentNode->next;
//if it reached the end of the list then the frequency of the new node is bigger than the frequency of all nodes in the list
if (currentNode->next == NULL)
currentNode->next = newNode;
else {
newNode->next = currentNode->next;
currentNode->next = newNode;
}
//remove the the 2 nodes used (first two nodes)
for (int i = 1; i <= 2; i++) {
currentNode = *parentNode;
HuffmanLinkedListNode *tempNode = currentNode;
currentNode = currentNode->next;
*parentNode = currentNode;
free(tempNode);
}
}
return currentNode->huffmanNode;
}
HuffmanTreeNode *CreateHuffmanTree(char inputFileName[]) {
FILE *inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("file not found");
exit(0);
}
unsigned char character = fgetc(inputFile);
int ascii[256];
//initialize all frequencys in array by 0
for (int i = 0; i < 256; i++)
ascii[i] = 0;
HuffmanLinkedListNode *linkedListParentNode = NULL;
while (character != 255) {
ascii[character]++;
character = fgetc(inputFile);
}
linkedListParentNode = CreateSortedLinkedList(ascii);
HuffmanTreeNode *huffmanHeadNode = BuildHuffmanTree(&linkedListParentNode);
free(linkedListParentNode);
fclose(inputFile);
return huffmanHeadNode;
}
IntLinkedListNode *CreateIntLinkedListNode(int data, IntLinkedListNode *next) {
IntLinkedListNode *intLinkedListNode = (IntLinkedListNode *) malloc(sizeof(IntLinkedListNode));
intLinkedListNode->data = data;
intLinkedListNode->next = next;
return intLinkedListNode;
}
void Push(Stack *stack, int data) {
if (stack->top == NULL)
stack->top = CreateIntLinkedListNode(data, NULL);
else {
IntLinkedListNode *newNode = CreateIntLinkedListNode(data, stack->top);
stack->top = newNode;
}
stack->size++;
}
void Pop(Stack *stack) {
if (stack->top == NULL)
return;
else {
IntLinkedListNode *tempNode = stack->top;
stack->top = stack->top->next;
free(tempNode);
stack->size--;
}
}
//used recursion to print from the end of stack
//at the beginning, node is the top of the stack
void CreateVariableLengthArray(IntLinkedListNode *node, VariableLengthNode *variableLengthNode) {
int static index = 0;
if (node->next == NULL) {
variableLengthNode->size = index + 1;
variableLengthNode->variableLength = (int *) malloc(sizeof(int) * index + 1);
variableLengthNode->variableLength[0] = node->data;
index--;
} else {
index++;
CreateVariableLengthArray(node->next, variableLengthNode);
variableLengthNode->variableLength[variableLengthNode->size - index - 1] = node->data;
index--;
}
//reset index to 0 again for the next use
if (index == -1)
index = 0;
}
void AddVariableLengthNodeToList(VariableLengthLinkedList **headnode, VariableLengthNode *variableLengthNode) {
VariableLengthLinkedList *newNode;
newNode = (VariableLengthLinkedList *) malloc(sizeof(VariableLengthLinkedList));
newNode->variableLengthNode = variableLengthNode;
newNode->next = NULL;
VariableLengthLinkedList *currentNode = *headnode;
if (*headnode == NULL)
*headnode = newNode;
else if (currentNode->variableLengthNode->size > variableLengthNode->size) {
newNode->next = *headnode;
*headnode = newNode;
}
//sort the new node
else {
while (currentNode->next != NULL && currentNode->next->variableLengthNode->size <= variableLengthNode->size)
currentNode = currentNode->next;
//if it reached the end of the list then the frequency of the new node is bigger than the frequency of all nodes in the list
if (currentNode->next == NULL)
currentNode->next = newNode;
else {
newNode->next = currentNode->next;
currentNode->next = newNode;
}
}
}
//create list of nodes where each node has an array of variable length
void CreateVariableLengthList(HuffmanTreeNode *node, Stack *stack, VariableLengthLinkedList **headnode) {
VariableLengthNode *variableLengthNode;
variableLengthNode = (VariableLengthNode *) malloc(sizeof(variableLengthNode));
if (node->right == NULL && node->left == NULL) {
CreateVariableLengthArray(stack->top, variableLengthNode);
variableLengthNode->character = node->character;
AddVariableLengthNodeToList(&*headnode, variableLengthNode);
} else {
if (node->right != NULL) {
Push(stack, 1);
CreateVariableLengthList(node->right, stack, &*headnode);
}
if (node->left != NULL) {
Push(stack, 0);
CreateVariableLengthList(node->left, stack, &*headnode);
}
}
Pop(stack);
}
//%d this number to know how many extra bits added
//"%c %d array", character, size of Variable length code, variable length code
void PrintVariableLengthTable(VariableLengthLinkedList *node, int extraBits) {
FILE *codes = fopen("codes.dat", "w");
fprintf(codes, "%d\n", extraBits);
while (node != NULL) {
fprintf(codes, "%c\t%d\t", node->variableLengthNode->character, node->variableLengthNode->size);
for (int i = 0; i < node->variableLengthNode->size; i++) {
fprintf(codes, "%d ", node->variableLengthNode->variableLength[i]);
}
fprintf(codes, "\n");
node = node->next;
}
fclose(codes);
}
void ReadVariableLength(VariableLengthLinkedList **node, int *extraBits) {
FILE *codes = fopen("codes.dat", "r");
if (codes == NULL) {
printf("codes.dat file not found");
return;
}
VariableLengthLinkedList *currentNode = *node;
currentNode->variableLengthNode = (VariableLengthNode *) malloc(sizeof(VariableLengthNode));
int flag = 1;
fscanf(codes, "%d\n", &*extraBits);
char character = getc(codes);
currentNode->variableLengthNode->character = character;
getc(codes);
currentNode->variableLengthNode->size = getc(codes) - 48;
getc(codes);
currentNode->variableLengthNode->variableLength = (int *) malloc(
sizeof(int) * currentNode->variableLengthNode->size);
int *array = (int *) malloc(sizeof(int) * currentNode->variableLengthNode->size);
for (int i = 0; i < currentNode->variableLengthNode->size; i++) {
array[i] = getc(codes) - 48;
getc(codes);
}
currentNode->variableLengthNode->variableLength = array;
getc(codes);
*node = currentNode;
character = getc(codes);
while (character != EOF) {
VariableLengthLinkedList *newNode = (VariableLengthLinkedList *) malloc(sizeof(VariableLengthLinkedList));
newNode->variableLengthNode = (VariableLengthNode *) malloc(sizeof(VariableLengthNode));
newNode->variableLengthNode->character = character;
getc(codes);
newNode->variableLengthNode->size = getc(codes) - 48;
getc(codes);
newNode->variableLengthNode->variableLength = (int *) malloc(sizeof(int) * newNode->variableLengthNode->size);
for (int i = 0; i < newNode->variableLengthNode->size; i++) {
newNode->variableLengthNode->variableLength[i] = getc(codes) - 48;
getc(codes);
}
getc(codes);
currentNode->next = newNode;
newNode->next = NULL;
currentNode = currentNode->next;
character = getc(codes);
}
}
//return node of the biggest variable length code
int SizeOfBiggestVL(VariableLengthLinkedList *node) {
while (node->next != NULL)
node = node->next;
return node->variableLengthNode->size;
}
//convert 8 bits to char
unsigned char Convert8BitToChar(int binary8Bit[]) {
unsigned char charAsciiCode = 0;
for (int i = 7; i >= 0; i--)
if (binary8Bit[i] == 1)
charAsciiCode += pow(2, 7 - i);
return charAsciiCode;
}
//array carry binary numbers
//index show the number of indices reserved in the array
void ConvertCharTo8Bit(unsigned char character, int array[], int *index) {
for (int i = (*index + 8); i > *index; i--) {
if (character % 2 == 1)
array[i] = 1;
else
array[i] = 0;
character = character / 2;
}
*index += 8;
}
int Compress(char inputFileName[], VariableLengthLinkedList *headNode) {
FILE *inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("file not found");
exit(0);
}
FILE *outputFile = fopen("output.dat", "w");
VariableLengthLinkedList *currentNode;
//bit8 is an array has 8 indices for 8 bits
//bit8Index is the last index in the bit8 array
int bit8[8];
int bit8Index = -1;
int signedChar = fgetc(inputFile);
unsigned char character = signedChar;
while (signedChar != EOF) {
currentNode = headNode;
while (currentNode->variableLengthNode->character != character)
currentNode = currentNode->next;
for (int j = 0; j < currentNode->variableLengthNode->size; j++) {
bit8Index++;
bit8[bit8Index] = currentNode->variableLengthNode->variableLength[j];
if (bit8Index == 7) {
fprintf(outputFile, "%c", Convert8BitToChar(bit8));
bit8Index = -1;
}
}
signedChar = fgetc(inputFile);
character = signedChar;
}
//if EOF reached and still there bits in the bit8 array
//then add zero until it become 8 bits
if (bit8Index > -1) {
for (int i = bit8Index + 1; i <= 7; i++)
bit8[i] = 0;
fprintf(outputFile, "%c", Convert8BitToChar(bit8));
}
fclose(outputFile);
fclose(inputFile);
//return number of bits added
return (7 - bit8Index);
}
//use for deletion of bits
void ShiftLeft(int array[], int numberOfTimes, int size) {
for (int j = 1; j <= numberOfTimes; j++)
for (int i = 0; i < ((size - 1) - j); i++)
array[i] = array[i + 1];
}
void Decompress(char inputFileName[], VariableLengthLinkedList *variableLengthListHeadNode, int extraBits) {
FILE *inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("file not found");
exit(0);
}
FILE *outputFile = fopen("output.txt", "w");
unsigned char character;
VariableLengthLinkedList *currentNode;
int size =
SizeOfBiggestVL(variableLengthListHeadNode) + (8 - (SizeOfBiggestVL(variableLengthListHeadNode) % 8)) + 8;
int *binaryArray = (int *) malloc(sizeof(int) * size);
int indexOfBinaryArray = -1;
int nextChar = fgetc(inputFile);
for (int k = 0; k < size; ++k) {
binaryArray[k] = 0;
}
do {
character = nextChar;
nextChar = fgetc(inputFile);
currentNode = variableLengthListHeadNode;
int flag = 1;
ConvertCharTo8Bit(character, binaryArray, &indexOfBinaryArray);
while (currentNode != NULL && indexOfBinaryArray != -1) {
if ((indexOfBinaryArray + 1) >= currentNode->variableLengthNode->size) {
for (int i = 0; i < currentNode->variableLengthNode->size; i++)
if (currentNode->variableLengthNode->variableLength[i] != binaryArray[i])
flag = 0;
if (currentNode->next != NULL && flag == 0) {
currentNode = currentNode->next;
flag = 1;
} else if (flag == 1) {
if (nextChar == EOF)
indexOfBinaryArray -= extraBits;
ShiftLeft(binaryArray, currentNode->variableLengthNode->size, size);
indexOfBinaryArray -= currentNode->variableLengthNode->size;
char x = currentNode->variableLengthNode->character;
fprintf(outputFile, "%c", currentNode->variableLengthNode->character);
currentNode = variableLengthListHeadNode;
}
} else
currentNode = NULL;
}
} while (nextChar != EOF);
fclose(outputFile);
}
int LeafSum(HuffmanTreeNode *node) {
int static leafSum = 0;
if (!node)
return 0;
if (!node->left && !node->right)
return (leafSum += node->frequency);
leafSum = LeafSum(node->left);
leafSum = LeafSum(node->right);
}
int searchForNode(HuffmanTreeNode *node, unsigned char character) {
int static sum = 0;
if (!node)
return sum;
if (!node->left && !node->right)
if (node->character == character)
return node->frequency;
sum = searchForNode(node->left, character);
sum = searchForNode(node->right, character);
return sum;
}
void PrintRatioBetweenCompressedOriginal(VariableLengthLinkedList *node, HuffmanTreeNode *huffmanTreeNode) {
int sumBeforCompression;
sumBeforCompression = LeafSum(huffmanTreeNode);
int sumAfterCompression = 0;
while (node != NULL) {
sumAfterCompression += (node->variableLengthNode->size *
searchForNode(huffmanTreeNode, node->variableLengthNode->character));
node = node->next;
}
sumBeforCompression *= 8;
printf("original size = %d bits\n", sumBeforCompression);
printf("compressed size = %d bits\n", sumAfterCompression);
printf("ration between original and compressed: original = %f of the compressed file\n",
((float) sumBeforCompression / sumAfterCompression));
}
int main() {
char inputFileName[] = "input.txt";
char compressedInputFileName[] = "output.dat";
printf("Enter number of your choice\n"
"1-compress\n"
"2-decompress(must be compressed first by this program)\n"
"3-both\n");
int choice;
scanf("%d", &choice);
printf("This may take few seconds...\n");
int extraBits;
//parent node for list contains all characters with its variable length
VariableLengthLinkedList *variableLengthLinkedListNode;
variableLengthLinkedListNode = (VariableLengthLinkedList *) malloc(sizeof(VariableLengthLinkedList));
if (choice == 1) {
variableLengthLinkedListNode = NULL;
HuffmanTreeNode *huffmanHeadNode = CreateHuffmanTree(inputFileName);
//this stack store the steps moved inside huffman tree
Stack *variableLengthSteps = (Stack *) malloc(sizeof(Stack));
variableLengthSteps->top = NULL;
CreateVariableLengthList(huffmanHeadNode, variableLengthSteps, &variableLengthLinkedListNode);
free(variableLengthSteps);
extraBits = Compress(inputFileName, variableLengthLinkedListNode);
PrintVariableLengthTable(variableLengthLinkedListNode, extraBits);
PrintRatioBetweenCompressedOriginal(variableLengthLinkedListNode, huffmanHeadNode);
} else if (choice == 2) {
ReadVariableLength(&variableLengthLinkedListNode, &extraBits);
Decompress(compressedInputFileName, variableLengthLinkedListNode, extraBits);
} else if (choice == 3) {
HuffmanTreeNode *huffmanHeadNode = CreateHuffmanTree(inputFileName);
//this stack store the steps moved inside huffman tree
Stack *variableLengthSteps = (Stack *) malloc(sizeof(Stack));
variableLengthSteps->top = NULL;
//parent node for list contains all characters with its variable length
VariableLengthLinkedList *variableLengthLinkedListNode;
variableLengthLinkedListNode = (VariableLengthLinkedList *) malloc(sizeof(VariableLengthLinkedList));
variableLengthLinkedListNode = NULL;
CreateVariableLengthList(huffmanHeadNode, variableLengthSteps, &variableLengthLinkedListNode);
free(variableLengthSteps);
int extraBits = Compress(inputFileName, variableLengthLinkedListNode);
PrintVariableLengthTable(variableLengthLinkedListNode, extraBits);
Decompress(compressedInputFileName, variableLengthLinkedListNode, extraBits);
} else {
printf("Wrong choice");
return 0;
}
return 1;
}