@@ -53,7 +53,7 @@ instead of ANSI color codes. For example:
53
53
----
54
54
$ stb-imagedumper -braille-art test/data/pjw-thumbnail.png
55
55
56
- test/data/pjw-thumbnail.png
56
+ test/data/pjw-thumbnail.png (208 bytes, 164 microseconds)
57
57
⣿⣿⣿⣿⡿⡋⠉⠉⠉⠙⠻⢿⣿⣿⣿⣿
58
58
⣿⣿⣿⡿⣷⣿⣿⣿⣆⠀⠀⠀⠈⠹⣿⣿
59
59
⣿⣿⠃⢸⡿⢿⣿⣿⣿⠦⠀⠀⠀⠀⢼⣿
@@ -71,6 +71,7 @@ To run:
71
71
for a C compiler $CC, such as clang or gcc.
72
72
*/
73
73
74
+ #include <inttypes.h>
74
75
#include <stdbool.h>
75
76
#include <stdio.h>
76
77
@@ -159,6 +160,51 @@ for a C compiler $CC, such as clang or gcc.
159
160
160
161
// ----------------
161
162
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
+
162
208
const uint8_t g_src_ptr__mona_lisa_21_32_th [] = {
163
209
0xc3 , 0xbe , 0xfe , 0xd1 , 0x18 , 0x0a , 0x1d , 0x02 , 0x78 , 0x95 , 0x7f , 0x88 ,
164
210
0x87 , 0x88 , 0x87 , 0x88 , 0x77 , 0x87 , 0x47 , 0x49 , 0x7f , 0xa6 , 0x02 , 0x17 ,
@@ -599,14 +645,22 @@ parse_flags(int argc, char** argv) {
599
645
#define BYTES_PER_COLOR_PIXEL 32
600
646
601
647
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 ) {
603
652
int w = 0 ;
604
653
int h = 0 ;
605
654
int channels_in_file = 0 ;
606
655
int bytes_per_pixel =
607
656
(g_flags .ascii_art || g_flags .braille_art ) ? STBI_grey : STBI_rgb ;
608
657
unsigned char * pixels = NULL ;
609
658
659
+ #if defined(WUFFS_EXAMPLE_USE_TIMERS )
660
+ g_started = true;
661
+ clock_gettime (CLOCK_MONOTONIC , & g_start_time );
662
+ #endif
663
+
610
664
if (src_len > 0 ) {
611
665
pixels = stbi_load_from_memory (src_ptr , src_len , //
612
666
& 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) {
615
669
& w , & h , & channels_in_file , bytes_per_pixel );
616
670
}
617
671
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
+
618
682
if (!pixels ) {
619
683
printf ("%s\n" , stbi_failure_reason ());
620
684
return ;
@@ -687,6 +751,7 @@ handle(const char* filename, const uint8_t* src_ptr, const size_t src_len) {
687
751
688
752
fflush (stdout );
689
753
stbi_image_free (pixels );
754
+ printf ("\n" );
690
755
}
691
756
692
757
int //
@@ -731,17 +796,14 @@ main(int argc, char** argv) {
731
796
732
797
if (g_flags .demo ) {
733
798
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 );
737
801
}
738
802
}
739
803
740
804
for (int c = 0 ; c < g_flags .remaining_argc ; c ++ ) {
741
805
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 );
745
807
}
746
808
return 0 ;
747
809
}
0 commit comments