From aeb35f0449dc9b5a6c2017f524e2e99e95dc016d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 12 Sep 2024 09:06:46 +1000 Subject: [PATCH] example/std-imagedumper: print width x height --- example/stb-imagedumper/stb-imagedumper.c | 46 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/example/stb-imagedumper/stb-imagedumper.c b/example/stb-imagedumper/stb-imagedumper.c index 6f736a07..21eda116 100644 --- a/example/stb-imagedumper/stb-imagedumper.c +++ b/example/stb-imagedumper/stb-imagedumper.c @@ -671,6 +671,35 @@ u64min(uint64_t a, uint64_t b) { return (a < b) ? a : b; } +static bool // +resize_wh(int* inout_w, int* inout_h, int dst_wh) { + // Check arguments. + if (!inout_w || !inout_h || (dst_wh <= 0) || (0x4000 < dst_wh) || // + (*inout_w <= 0) || (0xffffff < *inout_w) || // + (*inout_h <= 0) || (0xffffff < *inout_h)) { + printf("main: resize failed: invalid argument\n"); + return false; + } + + // Compute dst width × height from src width × height. + uint64_t dw = 0; + uint64_t dh = 0; + uint64_t sw = (uint64_t)(*inout_w); + uint64_t sh = (uint64_t)(*inout_h); + if (sw < sh) { + dw = intmax(1, (int)(((sw * dst_wh * 2) + sh) / (2 * sh))); + dh = dst_wh; + } else { + dw = dst_wh; + dh = intmax(1, (int)(((sh * dst_wh * 2) + sw) / (2 * sw))); + } + + // Return width and height. + *inout_w = (int)dw; + *inout_h = (int)dh; + return true; +} + static unsigned char* // resize(int* inout_w, int* inout_h, @@ -808,8 +837,21 @@ handle(const char* filename, elapsed_micros = micros_since_start(&now); #endif - printf("\n%s (%" PRIu64 " bytes, %" PRIi64 " microseconds)\n", filename, - filesize, elapsed_micros); + char resize_info_string[64]; + resize_info_string[0] = 0; + if (g_flags.resize > 0) { + int resize_w = src_w; + int resize_h = src_h; + if (!resize_wh(&resize_w, &resize_h, g_flags.resize)) { + stbi_image_free(src_pixels); + return; + } + snprintf(resize_info_string, sizeof(resize_info_string), " → %d×%d", + resize_w, resize_h); + } + + printf("\n%s (%" PRIu64 " bytes, %d×%d%s pixels, %" PRIi64 " microseconds)\n", + filename, filesize, src_w, src_h, resize_info_string, elapsed_micros); if (!src_pixels) { printf("%s\n", stbi_failure_reason());