-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuff.c
More file actions
235 lines (181 loc) · 5.61 KB
/
huff.c
File metadata and controls
235 lines (181 loc) · 5.61 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
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
#include "bitwriter.h"
#include "pq.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define OPTIONS "i:o:h"
typedef struct Code {
uint64_t code;
uint8_t code_length;
} Code;
uint64_t fill_histogram(Buffer *inbuf, double *histogram) {
uint8_t byte;
for (int i = 0; i < 256; i++) {
histogram[i] = 0.0;
}
// Initialize the file size counter
uint64_t filesize = 0;
histogram[0x00]++;
histogram[0xff]++;
// Read bytes from the input buffer and update the histogram
while (read_uint8(inbuf, &byte)) {
// read_uint8(inbuf, &byte);
histogram[byte]++;
filesize++;
}
// Apply the important hack to ensure at least two non-zero values in the histogram
// histogram[0x00]++;
// histogram[0xff]++;
return filesize;
}
Node *create_tree(double *histogram, uint16_t *num_leaves) {
PriorityQueue *pq = pq_create();
// init_priority_queue(&pq);
uint16_t x;
for (int i = 0; i < 256; i++) {
if (histogram[i] != 0.0) {
Node *node = node_create(i, histogram[i]);
enqueue(pq, node);
(*num_leaves)++;
}
}
// Step 2: Run the Huffman Coding algorithm
while (!pq_size_is_1(pq) && !pq_is_empty(pq)) {
Node *left;
Node *right;
dequeue(pq, &left);
dequeue(pq, &right);
Node *new_node = node_create(0x00, left->weight + right->weight);
new_node->left = left;
new_node->right = right;
enqueue(pq, new_node);
}
// Step 3: Dequeue the only entry remaining and return it as the Huffman Tree
// num_leaves = &x;
Node *huffman_tree;
dequeue(pq, &huffman_tree);
pq_free(&pq);
return huffman_tree;
}
// probably correct, check for the casting thing in front of the one
void fill_code_table(Code *code_table, Node *node, uint64_t code, uint8_t code_length) {
// code_table = calloc(256, sizeof(Code));
if (node->left != NULL && node->right != NULL) {
// Internal node
fill_code_table(code_table, node->left, code, code_length + 1);
code |= (1 << code_length);
fill_code_table(code_table, node->right, code, code_length + 1);
} else {
// Leaf node: store the Huffman code
code_table[node->symbol].code = code;
code_table[node->symbol].code_length = code_length;
}
// free(code_table);
}
void huff_write_tree(BitWriter *outbuf, Node *node) {
if (node->left != NULL && node->right != NULL) {
// Internal node
huff_write_tree(outbuf, node->left);
huff_write_tree(outbuf, node->right);
bit_write_bit(outbuf, 0);
} else {
// Leaf node
bit_write_bit(outbuf, 1);
bit_write_uint8(outbuf, node->symbol);
}
}
void node_clear(Node *n) {
if (n->left) {
node_clear(n->left);
}
if (n->right) {
node_clear(n->right);
}
free(n);
}
void huff_compress_file(BitWriter *outbuf, Buffer *inbuf, uint32_t filesize, uint16_t num_leaves,
Node *code_tree, Code *code_table) {
uint8_t byte;
bit_write_uint8(outbuf, 'H');
bit_write_uint8(outbuf, 'C');
bit_write_uint32(outbuf, filesize);
bit_write_uint16(outbuf, num_leaves);
// Write the code tree
huff_write_tree(outbuf, code_tree);
// Read the input file and write the Huffman codes
while (read_uint8(inbuf, &byte)) {
// read_uint8(inbuf, &byte);
//
uint64_t code = code_table[byte].code;
uint8_t code_length = code_table[byte].code_length;
for (int i = 0; i <= code_length - 1; i++) {
bit_write_bit(outbuf, code & 1);
code >>= 1;
}
}
}
int main(int argc, char *argv[]) {
// FILE *infile = stdin;
// FILE *outfile = stdout;
char *file_in;
char *file_out;
//int i = 0;
//int o = 0;
int h = 0;
int opt = 0;
while ((opt = getopt(argc, argv, OPTIONS)) != -1) {
switch (opt) {
case 'i':
file_in = optarg;
if (file_in == NULL) {
printf("error!");
exit(1);
}
// i = 1;
break;
case 'o':
file_out = optarg;
// if (file_out == NULL) {
// printf("error!");
// exit(1);
// }
//printf("error!");
// exit(1);
// o = 1;
break;
case 'h':
h = 1;
printf("i: set the name of the file to be read from.\no: set the name of the file to "
"be written to.\n h: print a help message.");
}
}
if (h != 1) {
Buffer *b = read_open(file_in);
// Code *c = calloc(256, sizeof(Code));
double histogram[256];
// BitWriter *bw = bit_write_open(b);
uint32_t filesize = (uint32_t) fill_histogram(b, histogram);
uint16_t num_leaves = 0;
Node *n = create_tree(histogram, &num_leaves);
Code *c = calloc(256, sizeof(Code));
// create_tree(histogram, &num_leaves);
fill_code_table(c, n, 0, 0);
read_close(&b);
//open outfile into buffer
b = read_open(file_in);
BitWriter *out = bit_write_open(file_out);
//huff compress with outfile and infile buffer
huff_compress_file(out, b, filesize, num_leaves, n, c);
//and then write_close and read_close()
node_clear(n);
free(c);
read_close(&b);
// write_close(&b1);
// node_clear(n);
bit_write_close(&out);
// free(c);
// free(n);
}
return 0;
}