Skip to content

Commit b9e3c26

Browse files
committed
example/std-imagedumper: add file size and timer
1 parent 8aa51df commit b9e3c26

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

example/stb-imagedumper/stb-imagedumper.c

+70-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ instead of ANSI color codes. For example:
5353
----
5454
$ stb-imagedumper -braille-art test/data/pjw-thumbnail.png
5555
56-
test/data/pjw-thumbnail.png
56+
test/data/pjw-thumbnail.png (208 bytes, 164 microseconds)
5757
⣿⣿⣿⣿⡿⡋⠉⠉⠉⠙⠻⢿⣿⣿⣿⣿
5858
⣿⣿⣿⡿⣷⣿⣿⣿⣆⠀⠀⠀⠈⠹⣿⣿
5959
⣿⣿⠃⢸⡿⢿⣿⣿⣿⠦⠀⠀⠀⠀⢼⣿
@@ -71,6 +71,7 @@ To run:
7171
for a C compiler $CC, such as clang or gcc.
7272
*/
7373

74+
#include <inttypes.h>
7475
#include <stdbool.h>
7576
#include <stdio.h>
7677

@@ -159,6 +160,51 @@ for a C compiler $CC, such as clang or gcc.
159160

160161
// ----------------
161162

163+
#if defined(__unix__) || defined(__MACH__)
164+
165+
#include <sys/stat.h>
166+
#include <time.h>
167+
168+
#define WUFFS_EXAMPLE_USE_TIMERS
169+
170+
uint64_t //
171+
get_filesize(const char* filename) {
172+
struct stat z;
173+
if (stat(filename, &z) == 0) {
174+
return z.st_size;
175+
}
176+
return 0;
177+
}
178+
179+
bool g_started = false;
180+
struct timespec g_start_time = {0};
181+
182+
int64_t //
183+
micros_since_start(struct timespec* now) {
184+
if (!g_started) {
185+
return 0;
186+
}
187+
int64_t nanos = (int64_t)(now->tv_sec - g_start_time.tv_sec) * 1000000000 +
188+
(int64_t)(now->tv_nsec - g_start_time.tv_nsec);
189+
if (nanos < 0) {
190+
return 0;
191+
}
192+
return nanos / 1000;
193+
}
194+
195+
#else // defined(__unix__) || defined(__MACH__)
196+
197+
#warning "TODO: stat and timers on non-POSIX systems"
198+
199+
uint64_t //
200+
get_filesize(const char* filename) {
201+
return 0;
202+
}
203+
204+
#endif // defined(__unix__) || defined(__MACH__)
205+
206+
// ----------------
207+
162208
const uint8_t g_src_ptr__mona_lisa_21_32_th[] = {
163209
0xc3, 0xbe, 0xfe, 0xd1, 0x18, 0x0a, 0x1d, 0x02, 0x78, 0x95, 0x7f, 0x88,
164210
0x87, 0x88, 0x87, 0x88, 0x77, 0x87, 0x47, 0x49, 0x7f, 0xa6, 0x02, 0x17,
@@ -599,14 +645,22 @@ parse_flags(int argc, char** argv) {
599645
#define BYTES_PER_COLOR_PIXEL 32
600646

601647
static void //
602-
handle(const char* filename, const uint8_t* src_ptr, const size_t src_len) {
648+
handle(const char* filename,
649+
uint64_t filesize,
650+
const uint8_t* src_ptr,
651+
const size_t src_len) {
603652
int w = 0;
604653
int h = 0;
605654
int channels_in_file = 0;
606655
int bytes_per_pixel =
607656
(g_flags.ascii_art || g_flags.braille_art) ? STBI_grey : STBI_rgb;
608657
unsigned char* pixels = NULL;
609658

659+
#if defined(WUFFS_EXAMPLE_USE_TIMERS)
660+
g_started = true;
661+
clock_gettime(CLOCK_MONOTONIC, &g_start_time);
662+
#endif
663+
610664
if (src_len > 0) {
611665
pixels = stbi_load_from_memory(src_ptr, src_len, //
612666
&w, &h, &channels_in_file, bytes_per_pixel);
@@ -615,6 +669,16 @@ handle(const char* filename, const uint8_t* src_ptr, const size_t src_len) {
615669
&w, &h, &channels_in_file, bytes_per_pixel);
616670
}
617671

672+
int64_t elapsed_micros = 0;
673+
#if defined(WUFFS_EXAMPLE_USE_TIMERS)
674+
struct timespec now;
675+
clock_gettime(CLOCK_MONOTONIC, &now);
676+
elapsed_micros = micros_since_start(&now);
677+
#endif
678+
679+
printf("\n%s (%" PRIu64 " bytes, %" PRIi64 " microseconds)\n", filename,
680+
filesize, elapsed_micros);
681+
618682
if (!pixels) {
619683
printf("%s\n", stbi_failure_reason());
620684
return;
@@ -687,6 +751,7 @@ handle(const char* filename, const uint8_t* src_ptr, const size_t src_len) {
687751

688752
fflush(stdout);
689753
stbi_image_free(pixels);
754+
printf("\n");
690755
}
691756

692757
int //
@@ -731,17 +796,14 @@ main(int argc, char** argv) {
731796

732797
if (g_flags.demo) {
733798
for (size_t c = 0; demos[c].filename != NULL; c++) {
734-
printf("\n%s (%zu bytes)\n", demos[c].filename, demos[c].src_len);
735-
handle(demos[c].filename, demos[c].src_ptr, demos[c].src_len);
736-
printf("\n");
799+
handle(demos[c].filename, demos[c].src_len, demos[c].src_ptr,
800+
demos[c].src_len);
737801
}
738802
}
739803

740804
for (int c = 0; c < g_flags.remaining_argc; c++) {
741805
const char* filename = g_flags.remaining_argv[c];
742-
printf("\n%s\n", filename);
743-
handle(filename, NULL, 0);
744-
printf("\n");
806+
handle(filename, get_filesize(filename), NULL, 0);
745807
}
746808
return 0;
747809
}

0 commit comments

Comments
 (0)