Skip to content

Commit e8d1903

Browse files
committed
print sizes human friendly (comma-delimited)
sizes: allow consumation of leading spaces If the result is too big (like 300 MB in case of 10k*10k RGB) do not (needlesly) insist on 10 characters.
1 parent 212966d commit e8d1903

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

src/gpujpeg_common.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,4 +2170,44 @@ coder_process_stats_overall(struct gpujpeg_coder* coder) {
21702170
printf("\n");
21712171
}
21722172

2173+
char*
2174+
format_number_with_delim(size_t num, char* buf, size_t buflen)
2175+
{
2176+
assert(buflen >= 1);
2177+
buf[buflen - 1] = '\0';
2178+
char* ptr = buf + buflen - 1;
2179+
while ( 1 ) {
2180+
int tmp = num % 1000;
2181+
num /= 1000;
2182+
if ( num == 0 ) {
2183+
ptr -= 1;
2184+
if ( tmp >= 10 ) {
2185+
ptr -= 1;
2186+
if ( tmp >= 100 ) {
2187+
ptr -= 1;
2188+
}
2189+
}
2190+
if ( ptr < buf ) {
2191+
snprintf(buf, buflen, "%s", "ERR");
2192+
return buf;
2193+
}
2194+
char numbuf[4];
2195+
snprintf(numbuf, sizeof numbuf, "%i", tmp);
2196+
// NOLINTNEXTLINE(bugprone-not-null-terminated-result): preending, no '\0' (already terminated)
2197+
memcpy(ptr, numbuf, strlen(numbuf));
2198+
return ptr;
2199+
}
2200+
ptr -= 4;
2201+
if ( ptr < buf ) {
2202+
snprintf(buf, buflen, "%s", "ERR");
2203+
return buf;
2204+
}
2205+
char numbuf[5];
2206+
snprintf(numbuf, sizeof numbuf, ",%03d", tmp);
2207+
// NOLINTNEXTLINE(bugprone-not-null-terminated-result): preending, no '\0' (already terminated)
2208+
memcpy(ptr, numbuf, 4);
2209+
}
2210+
return ptr;
2211+
}
2212+
21732213
/* vi: set expandtab sw=4 : */

src/gpujpeg_decoder.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,9 @@ gpujpeg_decoder_decode(struct gpujpeg_decoder* decoder, uint8_t* image, size_t i
447447

448448
coder_process_stats(coder);
449449
if ( coder->param.verbose >= GPUJPEG_LL_STATUS ) {
450-
printf("Decompressed Size: %10.zu bytes %dx%d %s %s\n", output->data_size, output->param_image.width,
450+
char buf[21];
451+
char* comp_size_delim = format_number_with_delim(output->data_size, buf, sizeof buf);
452+
printf("Decompressed Size: %10s bytes %dx%d %s %s\n",comp_size_delim, output->param_image.width,
451453
output->param_image.height, gpujpeg_pixel_format_get_name(output->param_image.pixel_format),
452454
gpujpeg_color_space_get_name(output->param_image.color_space));
453455
}

src/gpujpeg_encoder.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ gpujpeg_encoder_encode(struct gpujpeg_encoder* encoder, const struct gpujpeg_par
628628
if (coder->param.verbose >= GPUJPEG_LL_STATUS ) {
629629
const char* interleaved =
630630
coder->param.comp_count == 1 ? "" : (coder->param.interleaved ? " interleaved" : " non-interleaved");
631-
printf("Compressed Size: %10.zu bytes %dx%d %s %s%s\n", *image_compressed_size, coder->param_image.width,
631+
char buf[21];
632+
char* comp_size_delim = format_number_with_delim(*image_compressed_size, buf, sizeof buf);
633+
printf("Compressed Size: %10s bytes %dx%d %s %s%s\n", comp_size_delim, coder->param_image.width,
632634
coder->param_image.height, gpujpeg_color_space_get_name(coder->param.color_space_internal),
633635
gpujpeg_subsampling_get_name(coder->param.comp_count, coder->param.sampling_factor), interleaved);
634636
}

src/gpujpeg_util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ struct { int x; int y; int z; } gridDim;
8181
fprintf(stderr, "[GPUJPEG] [Error] Can't use OpenGL. The codec was compiled without OpenGL!\n"); \
8282
action; \
8383

84+
#define ARR_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
85+
86+
/**
87+
* formats num with thousand delimitered by a comma (,)
88+
* @param buf output buffer, must not be NULL of zero bytes long
89+
* @param buflen buffer len, should be long enough to hold the result
90+
* @returns pointer to buf (not necessarily the beginning) containng the represented number of "ERR"
91+
* if not enough space
92+
*/
93+
char*
94+
format_number_with_delim(size_t num, char* buf, size_t buflen);
95+
8496
#ifdef __cplusplus
8597
}
8698
#endif

test/unit/run_tests.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <libgpujpeg/gpujpeg_common.h>
44
#include <libgpujpeg/gpujpeg_encoder.h>
55
#include "../../src/gpujpeg_common_internal.h"
6+
#include "../../src/gpujpeg_util.h"
67
#include <stdbool.h>
78
#include <stdio.h>
89
#include <stdlib.h>
@@ -79,10 +80,49 @@ static void encode_gpu_mem_as_cpu() {
7980
void
8081
test_gh_95();
8182

83+
static char*
84+
test_format_number_with_delim() {
85+
struct {
86+
size_t val;
87+
char exp_res[1024];
88+
} test_cases[] = {
89+
{0, "0" },
90+
{1, "1" },
91+
{10, "10" },
92+
{11, "11" },
93+
{100, "100" },
94+
{101, "101" },
95+
{1000, "1,000" },
96+
{1001, "1,001" },
97+
{1100, "1,100" },
98+
{101100, "101,100" },
99+
{10LLU * 1000 * 1000 * 1000, "10,000,000,000"},
100+
};
101+
char buf[1024];
102+
for ( unsigned i = 0; i < ARR_SIZE(test_cases); i++ ) {
103+
char* res = format_number_with_delim(test_cases[i].val, buf, sizeof buf);
104+
ASSERT_STR_EQ(test_cases[i].exp_res, res);
105+
// "tight" buffer
106+
res = format_number_with_delim(test_cases[i].val, buf, strlen(test_cases[i].exp_res) + 1);
107+
ASSERT_STR_EQ(test_cases[i].exp_res, res);
108+
}
109+
110+
// test not enough space
111+
char* res = format_number_with_delim(1000000, buf, 4);
112+
ASSERT_STR_EQ(res, "ERR");
113+
res = format_number_with_delim(1000000, buf, 2);
114+
ASSERT_STR_EQ(res, "E");
115+
res = format_number_with_delim(1000000, buf, 1);
116+
ASSERT_STR_EQ(res, "");
117+
118+
printf("Ok\n");
119+
}
120+
82121
int main() {
83122
subsampling_name_test();
84123
encode_gpu_mem_as_cpu();
85124
test_gh_95();
125+
test_format_number_with_delim();
86126
printf("PASSED\n");
87127
}
88128

0 commit comments

Comments
 (0)