-
Notifications
You must be signed in to change notification settings - Fork 2
/
spngt.c
423 lines (331 loc) · 9.89 KB
/
spngt.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
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
#if defined(_WIN32)
#include "windows.h"
#else
#define _POSIX_C_SOURCE 199309L
#include <time.h>
#endif
#include <spngt.h>
typedef int spngt_fn(struct spngt_params *params);
struct spngt__library
{
const char *name;
spngt_fn *decode_fn;
spngt_fn *encode_fn;
uint64_t best;
uint64_t encoded_size;
};
static int decode_runs = SPNGT_DEFAULT_DECODE_RUNS;
static int encode_runs = SPNGT_DEFAULT_ENCODE_RUNS;
static struct spngt__library libraries[10];
static int library_count = 0;
static void add_library_struct(const struct spngt__library *lib)
{
libraries[library_count] = *lib;
libraries[library_count].best = UINT64_MAX;
library_count++;
}
#define add_library(lib_name) add_library_struct(&(struct spngt__library)\
{\
.name = #lib_name, \
.decode_fn = spngt_decode_##lib_name, \
.encode_fn = spngt_encode_##lib_name, \
.best = UINT64_MAX, \
.encoded_size = UINT64_MAX \
})
static void print_times(void)
{
const struct spngt__library *lib = libraries;
int i;
for(i=0; i < library_count; i++, lib++)
{
if(lib->best == UINT64_MAX) continue;
char pad_str[] = " ";
pad_str[sizeof(pad_str) - strlen(lib->name) + 2] = '\0';
printf("%s %s%" PRIu64 " usec\n", lib->name, pad_str, lib->best / (uint64_t)1000);
}
}
static void print_encode_results(void)
{
const struct spngt__library *lib = libraries;
int i;
for(i=0; i < library_count; i++, lib++)
{
if(lib->best == UINT64_MAX) continue;
char pad_str[] = " ";
pad_str[sizeof(pad_str) - strlen(lib->name) + 2] = '\0';
printf("%s %s", lib->name, pad_str);
printf("%" PRIu64 " usec ", lib->best / (uint64_t)1000);
printf("%" PRIu64 " KB\n", lib->encoded_size / (uint64_t)1024);
}
}
static const char *zstrategy_str(enum spngt_zlib_strategy strategy)
{
switch(strategy)
{
case SPNGT_Z_FILTERED: return "Z_FILTERED";
case SPNGT_Z_HUFFMAN_ONLY: return "Z_HUFFMAN_ONLY";
case SPNGT_Z_RLE: return "Z_RLE";
case SPNGT_Z_FIXED: return "Z_FIXED";
case SPNGT_Z_DEFAULT_STRATEGY: return "Z_DEFAULT_STRATEGY";
default: return "(UNKNOWN)";
}
}
static void print_encode_params(struct spngt_params *params)
{
if(params->override_defaults)
{
printf("compression level: %d\n", params->compression_level);
printf("zlib strategy: %s\n", zstrategy_str(params->strategy));
printf("filters: ");
if(params->filter_choice != SPNG_FILTER_CHOICE_ALL)
{
if(params->filter_choice & SPNG_FILTER_CHOICE_NONE) printf("NONE ");
if(params->filter_choice & SPNG_FILTER_CHOICE_SUB) printf("SUB ");
if(params->filter_choice & SPNG_FILTER_CHOICE_UP) printf("UP ");
if(params->filter_choice & SPNG_FILTER_CHOICE_AVG) printf("AVG ");
if(params->filter_choice & SPNG_FILTER_CHOICE_PAETH) printf("PAETH ");
if(params->filter_choice == SPNG_DISABLE_FILTERING) printf("(NO FILTERING)");
}
else printf("ALL");
printf("\n\n");
}
else printf("Encode parameters: (library defaults)\n\n");
}
uint64_t spngt_time(void)
{
#if defined(_WIN32)
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
ULARGE_INTEGER time = { .LowPart = ft.dwLowDateTime, .HighPart = ft.dwHighDateTime };
return time.QuadPart * 100;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000 + (uint64_t)ts.tv_nsec;
#endif
}
void spngt_measure(uint64_t start, uint64_t end, uint64_t *best)
{
uint64_t elapsed = end - start;
if(*best > elapsed) *best = elapsed;
}
const char *spngt_strerror(enum spngt_errno err)
{
switch(err)
{
case SPNGT_OK: return "success";
case SPNGT_EINVAL: return "invalid argument";
case SPNGT_EMEM: return "out of memory";
case SPNGT_ERROR: return "generic error";
case SPNGT_ENOTSUPP: return "operation not supported";
default: return "unknown error";
}
return "unknown error";
}
static int decode_and_measure(enum spngt_library library, struct spngt_params *params)
{
uint64_t a, b;
enum spngt_errno e = 0;
struct spngt__library *lib = &libraries[library];
int i;
for(i=0; i < decode_runs; i++)
{
a = spngt_time();
e = lib->decode_fn(params);
b = spngt_time();
spngt_measure(a, b, &lib->best);
free(params->image);
params->image = NULL;
if(e)
{
printf("ERROR: %s decode failed: %s\n", lib->name, spngt_strerror(e));
return e;
}
}
return 0;
}
int encode_and_measure(enum spngt_library library, struct spngt_params *params)
{
uint64_t a, b;
enum spngt_errno e = 0;
struct spngt__library *lib = &libraries[library];
size_t encode_buffer_size = params->png_size;
int i;
for(i=0; i < encode_runs; i++)
{
a = spngt_time();
e = lib->encode_fn(params);
b = spngt_time();
if(e)
{
printf("ERROR: %s encode failed: %s\n", lib->name, spngt_strerror(e));
return e;
}
spngt_measure(a, b, &lib->best);
lib->encoded_size = params->png_size;
params->png_size = encode_buffer_size;
}
return 0;
}
static int decode_benchmark(struct spngt_params *params)
{
enum spngt_errno e = 0;
int i;
for(i=0; i < library_count; i++)
{
e = decode_and_measure(i, params);
if(e) return e;
}
print_times();
return 0;
}
static int encode_benchmark(struct spngt_params *params)
{
enum spngt_errno ret = 0;
print_encode_params(params);
/* Conservative estimate that doesn't require realloc()'s */
size_t encode_buffer_size = params->image_size + (1 << 20);
params->png_size = encode_buffer_size;
params->png = malloc(params->png_size);
if(params->png == NULL)
{
printf("ERROR: failed to preallocate encode buffer\n");
return SPNGT_EMEM;
}
int i;
for(i=0; i < library_count; i++)
{
encode_and_measure(i, params);
}
free(params->png);
print_encode_results();
return ret;
}
static void print_png_info(struct spng_ihdr *ihdr, const char *path, size_t siz_pngbuf)
{
const char *clr_type_str;
if(ihdr->color_type == SPNG_COLOR_TYPE_GRAYSCALE)
clr_type_str = "GRAY";
else if(ihdr->color_type == SPNG_COLOR_TYPE_TRUECOLOR)
clr_type_str = "RGB";
else if(ihdr->color_type == SPNG_COLOR_TYPE_INDEXED)
clr_type_str = "INDEXED";
else if(ihdr->color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA)
clr_type_str = "GRAY-ALPHA";
else
clr_type_str = "RGBA";
const char *name = strrchr(path, '/');
if(!name) name = strrchr(path, '\\');
if(name) name++;
if(!name) name = path;
const char *unit = "B";
if(siz_pngbuf > 1024)
{
unit = "KB";
siz_pngbuf /= 1024;
}
printf("%s %zu %s %s %" PRIu8 "-bit, %" PRIu32 "x%" PRIu32 " %s",
name, siz_pngbuf, unit, clr_type_str, ihdr->bit_depth, ihdr->width, ihdr->height,
ihdr->interlace_method ? "interlaced" : "non-interlaced\n\n");
}
int spngt_prefetch_file(const char *path, struct spngt_params *params)
{
FILE *png;
unsigned char *pngbuf;
png = fopen(path, "rb");
if(png == NULL)
{
printf("error opening input file %s\n", path);
return 1;
}
fseek(png, 0, SEEK_END);
long siz_pngbuf = ftell(png);
rewind(png);
if(siz_pngbuf < 1) return 1;
pngbuf = malloc(siz_pngbuf);
if(pngbuf == NULL)
{
printf("malloc() failed\n");
return 1;
}
if(fread(pngbuf, siz_pngbuf, 1, png) != 1)
{
printf("fread() failed\n");
return 1;
}
params->png = pngbuf;
params->png_size = siz_pngbuf;
return 0;
}
int main(int argc, char **argv)
{
if(argc < 2)
{
printf("no input file\n");
return 1;
}
int do_encode = 0;
const char *filepath = argv[1];
const char *dot = strrchr(filepath, '.');
if(dot && !strcmp(dot, ".lua"))
{
return spngt_exec_script(argc, argv);
}
if(argc > 2)
{
if(!strcmp(argv[2], "enc")) do_encode = 1;
else printf("unrecognized option: %s\n", argv[2]);
}
if(!strcmp(argv[1], "info"))
{
#define XX(lib) spngt_print_version_##lib();
SPNGT_LIBS(XX)
#undef XX
if(do_encode) printf("\nencode times are the best of %d runs\n", encode_runs);
printf("\ndecode times are the best of %d runs\n", decode_runs);
return 0;
}
enum spngt_errno ret = 0;
struct spngt_params params =
{
.fmt = SPNG_FMT_PNG,
.encode_runs = encode_runs
};
ret = spngt_prefetch_file(filepath, ¶ms);
if(ret) goto err;
#define XX(lib) add_library(lib);
SPNGT_LIBS(XX)
#undef XX
ret = spngt_decode_spng(¶ms);
if(ret)
{
printf("failed to fetch PNG info: %s\n", spngt_strerror(ret));
goto err;
}
print_png_info(¶ms.ihdr, argv[1], params.png_size);
if(do_encode)
{
free(params.png); // not needed at this point, will be overwriten by encode_benchmark()
params.override_defaults = 0;
params.compression_level = 6;
params.window_bits = 15;
params.mem_level = 8;
params.strategy = SPNGT_Z_FILTERED;
params.filter_choice = SPNG_FILTER_CHOICE_ALL;
ret = encode_benchmark(¶ms);
free(params.image);
params.png = NULL;
}
else
{
free(params.image);
params.image = NULL;
params.image_size = 0;
params.fmt = SPNG_FMT_RGBA8;
ret = decode_benchmark(¶ms);
}
if(ret) printf("%s benchmark failed (%s)\n", do_encode ? "encode" : "decode", spngt_strerror(ret));
err:
free(params.png);
return ret;
}