Skip to content

Commit aeb35f0

Browse files
committed
example/std-imagedumper: print width x height
1 parent 48d2ffc commit aeb35f0

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

example/stb-imagedumper/stb-imagedumper.c

+44-2
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,35 @@ u64min(uint64_t a, uint64_t b) {
671671
return (a < b) ? a : b;
672672
}
673673

674+
static bool //
675+
resize_wh(int* inout_w, int* inout_h, int dst_wh) {
676+
// Check arguments.
677+
if (!inout_w || !inout_h || (dst_wh <= 0) || (0x4000 < dst_wh) || //
678+
(*inout_w <= 0) || (0xffffff < *inout_w) || //
679+
(*inout_h <= 0) || (0xffffff < *inout_h)) {
680+
printf("main: resize failed: invalid argument\n");
681+
return false;
682+
}
683+
684+
// Compute dst width × height from src width × height.
685+
uint64_t dw = 0;
686+
uint64_t dh = 0;
687+
uint64_t sw = (uint64_t)(*inout_w);
688+
uint64_t sh = (uint64_t)(*inout_h);
689+
if (sw < sh) {
690+
dw = intmax(1, (int)(((sw * dst_wh * 2) + sh) / (2 * sh)));
691+
dh = dst_wh;
692+
} else {
693+
dw = dst_wh;
694+
dh = intmax(1, (int)(((sh * dst_wh * 2) + sw) / (2 * sw)));
695+
}
696+
697+
// Return width and height.
698+
*inout_w = (int)dw;
699+
*inout_h = (int)dh;
700+
return true;
701+
}
702+
674703
static unsigned char* //
675704
resize(int* inout_w,
676705
int* inout_h,
@@ -808,8 +837,21 @@ handle(const char* filename,
808837
elapsed_micros = micros_since_start(&now);
809838
#endif
810839

811-
printf("\n%s (%" PRIu64 " bytes, %" PRIi64 " microseconds)\n", filename,
812-
filesize, elapsed_micros);
840+
char resize_info_string[64];
841+
resize_info_string[0] = 0;
842+
if (g_flags.resize > 0) {
843+
int resize_w = src_w;
844+
int resize_h = src_h;
845+
if (!resize_wh(&resize_w, &resize_h, g_flags.resize)) {
846+
stbi_image_free(src_pixels);
847+
return;
848+
}
849+
snprintf(resize_info_string, sizeof(resize_info_string), " → %d×%d",
850+
resize_w, resize_h);
851+
}
852+
853+
printf("\n%s (%" PRIu64 " bytes, %d×%d%s pixels, %" PRIi64 " microseconds)\n",
854+
filename, filesize, src_w, src_h, resize_info_string, elapsed_micros);
813855

814856
if (!src_pixels) {
815857
printf("%s\n", stbi_failure_reason());

0 commit comments

Comments
 (0)