-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.c
More file actions
165 lines (127 loc) · 3.04 KB
/
encoder.c
File metadata and controls
165 lines (127 loc) · 3.04 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
/*
* @file encoder.c
* @author Fabjan Sukalia <fsukalia@gmail.com>
* @date 2016-02-21
* @brief Tool to encode data using huffman codes in JPEG format
*/
#define _POSIX_C_SOURCE 1
#define _DEFAULT_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include "bit_writer.h"
#include "huff_enc.h"
#define JPG_DHT (0xC4)
static const char *prog_name = "huffenc";
void usage(void)
{
fprintf(stderr, "USAGE: %s FILE_IN FILE_OUT\n", prog_name);
exit(EXIT_FAILURE);
}
static off_t get_file_size(FILE *file)
{
int fd = fileno(file);
if (fd == -1)
return 0;
struct stat buf;
if (fstat(fd, &buf) != 0)
return 0;
if (S_ISREG(buf.st_mode) == 0)
return 0;
return buf.st_size;
}
void encode(FILE *in, FILE *out)
{
off_t size = get_file_size(in);
if (size == 0)
return;
uint8_t *data = malloc(size);
if (data == NULL)
return;
if (fread(data, size, 1, in) != 1) {
fprintf(stderr, "Couldn't read input data\n");
exit(EXIT_FAILURE);
}
uint32_t freq[256];
huff_get_freq(data, size, freq);
struct huff_enc enc;
struct huff_enc_info info;
if (!huff_gen_enc(freq, &enc, &info)) {
fprintf(stderr, "Couldn't create encoder\n");
exit(EXIT_FAILURE);
}
uint8_t header[21];
header[0] = 0xFF;
header[1] = JPG_DHT;
uint16_t header_length = 19 + info.num_codes;
header[2] = header_length >> 8;
header[3] = header_length & 0xFF;
header[4] = 0;
for (int i = 0; i < 16; i++) {
header[5 + i] = info.codes_per_len[i];
}
if (fwrite(header, sizeof(header), 1, out) != 1) {
fprintf(stderr, "Couldn't write header\n");
exit(EXIT_FAILURE);
}
/* write symbols */
for (int i = 0; i < 16; i++) {
uint16_t num_symbols = info.codes_per_len[i];
if (num_symbols == 0)
continue;
uint8_t symbols[num_symbols];
uint16_t sym_index = 0;
for (int j = 0; j < info.num_codes; j++) {
if (enc.codes[j].code_len != (i + 1))
continue;
symbols[sym_index] = enc.codes[j].symbol;
sym_index++;
}
if (fwrite(symbols, num_symbols, 1, out) != 1) {
fprintf(stderr, "Couldn't write header\n");
exit(EXIT_FAILURE);
}
}
/* write uint32_t num_data_symbols */
uint8_t num_bytes[4];
num_bytes[0] = (size >> 24) & 0xFF;
num_bytes[1] = (size >> 16) & 0xFF;
num_bytes[2] = (size >> 8) & 0xFF;
num_bytes[3] = size & 0xFF;
if (fwrite(num_bytes, 4, 1, out) != 1) {
fprintf(stderr, "Couldn't write number of bytes\n");
exit(EXIT_FAILURE);
}
struct bit_writer *writer = bit_writer_create(out);
huff_encode(&enc, size, data, writer);
bit_writer_destroy(writer);
huff_enc_destroy(&enc);
free(data);
}
int main(int argc, char *argv[])
{
errno = 0;
if (argc > 1)
prog_name = argv[0];
if (argc != 3)
usage();
FILE *in = fopen(argv[1], "rb");
if (in == NULL) {
perror("Couldn't open input file");
return EXIT_FAILURE;
}
FILE *out = fopen(argv[2], "wb");
if (out == NULL) {
perror("Couldn't open output file");
return EXIT_FAILURE;
}
encode(in, out);
fclose(in);
fclose(out);
return 0;
}