-
Notifications
You must be signed in to change notification settings - Fork 0
/
enqoi.c
183 lines (163 loc) · 4.71 KB
/
enqoi.c
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
#include "container.h"
#include "encoder.h"
#include "stb_image.h"
#include <getopt.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#define OPTIONS "hlvi:o:t:"
#define MAX_THREADS 256
#define DEFAULT_THREADS 1
FILE *infile = NULL, *outfile = NULL;
unsigned char *data = NULL;
Encoder *e = NULL;
void cleanup() {
if (infile) {
fclose(infile);
}
if (outfile) {
fclose(outfile);
}
if (data) {
stbi_image_free(data);
}
if (e) {
encoder_delete(&e);
}
}
void usage(const char *program_name) {
fprintf(stderr,
"usage: %s [-hv] [-i input] [-o output]\n"
" -h: show usage\n"
" -l: indicate that output file is linear sRGB as opposed to gamma. note that "
"no colorspace conversion is performed.\n"
" -v: print encoding statistics\n"
" -i input: specify input file. default is stdin.\n"
" -o output: specify output file. default is stdout.\n"
" -t threads: specify number of threads to use. 1-%d, default %d.\n",
program_name, MAX_THREADS, DEFAULT_THREADS);
}
typedef struct {
qoi_desc_t *desc;
pixel_t *pixels;
uint64_t n_pixels;
uint8_t *output_buffer;
uint64_t output_capacity;
} thread_args_t;
int main(int argc, char **argv) {
infile = stdin;
outfile = stdout;
bool verbose = false, linear_srgb = false;
int opt = 0;
uint32_t n_threads = DEFAULT_THREADS;
while ((opt = getopt(argc, argv, OPTIONS)) != -1) {
switch (opt) {
case 'h':
usage(argv[0]);
return 1;
case 'l':
linear_srgb = true;
break;
case 'v':
verbose = true;
break;
case 'i':
infile = fopen(optarg, "rb");
if (!infile) {
fprintf(stderr, "%s: %s: ", argv[0], optarg);
perror("");
cleanup();
return 1;
}
break;
case 'o':
outfile = fopen(optarg, "wb");
if (!outfile) {
fprintf(stderr, "%s: %s: ", argv[0], optarg);
perror("");
cleanup();
return 1;
}
break;
case 't':
n_threads = strtoul(optarg, NULL, 10);
if (n_threads < 1 || n_threads > MAX_THREADS) {
fprintf(stderr,
"%s: invalid number of threads %" PRIu32 ". must be between 1 and %d.\n",
argv[0], n_threads, MAX_THREADS);
cleanup();
return 1;
}
break;
default:
usage(argv[0]);
return 1;
}
}
(void) verbose;
int x, y, n;
data = stbi_load_from_file(infile, &x, &y, &n, 4);
if (!data) {
fprintf(stderr, "%s: failed to decode input: %s\n", argv[0], stbi_failure_reason());
cleanup();
return 1;
}
// 2 channels = gray + alpha
// 4 channels = RGBA
qoi_channels_t channels = (n == 2 || n == 4) ? RGBA : RGB;
qoi_desc_t desc = {
.width = x,
.height = y,
.colorspace = linear_srgb ? QOI_LINEAR : QOI_SRGB,
.channels = channels,
};
// pthread_t threads[MAX_THREADS];
e = encoder_create(verbose, &desc);
if (!e) {
fprintf(stderr, "%s: failed to create QOI encoder\n", argv[0]);
cleanup();
return 1;
}
write_header(outfile, &desc);
clock_t start = clock();
encoder_encode_pixels(outfile, e, (pixel_t *) data, ((uint64_t) x) * ((uint64_t) y));
clock_t end = clock();
encoder_finish(outfile, e);
write_end_marker(outfile);
if (verbose) {
qoi_stats_t *stats = encoder_get_stats(e);
double bpp = (double) stats->total_bits / stats->total_pixels,
percent_rgb = 100.0 * stats->op_to_pixels.rgb / stats->total_pixels,
percent_rgba = 100.0 * stats->op_to_pixels.rgba / stats->total_pixels,
percent_index = 100.0 * stats->op_to_pixels.index / stats->total_pixels,
percent_diff = 100.0 * stats->op_to_pixels.diff / stats->total_pixels,
percent_luma = 100.0 * stats->op_to_pixels.luma / stats->total_pixels,
percent_run = 100.0 * stats->op_to_pixels.run / stats->total_pixels;
double encode_time = ((double) (end - start)) / CLOCKS_PER_SEC,
speed = stats->total_pixels / encode_time / 1000000.0;
uint64_t total_size = QOI_HEADER_LENGTH + (stats->total_bits / 8) + QOI_END_MARKER_LENGTH;
if (total_size < 1024) {
fprintf(stderr, "file size: %6" PRIu64 " B\n", total_size);
} else if (total_size < 1048576) {
fprintf(stderr, "file size: %9.2f KiB\n", total_size / 1024.0);
} else {
fprintf(stderr, "file size: %9.2f MiB\n", total_size / 1048576.0);
}
fprintf(stderr,
"BPP: %11.4f\n"
"speed: %9.2f MP/s\n"
"operator usage by number of pixels:\n"
" QOI_OP_RGB: %6.2f%%\n"
" QOI_OP_RGBA: %6.2f%%\n"
" QOI_OP_INDEX: %6.2f%%\n"
" QOI_OP_DIFF: %6.2f%%\n"
" QOI_OP_LUMA: %6.2f%%\n"
" QOI_OP_RUN: %6.2f%%\n",
bpp, speed, percent_rgb, percent_rgba, percent_index, percent_diff, percent_luma,
percent_run);
}
cleanup();
return 0;
}