ntrop

2D binary entropy visualization inspired by ..cantor.dust..
git clone https://git.sinitax.com/sinitax/ntrop
Log | Files | Refs | sfeed.txt

commit 5b72cec00a5f0c5f8a77dde65271fb90b844f779
parent 32acf8e0eec26560c3025a3a503428b2e5f5c9af
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun, 12 Feb 2023 15:21:46 +0100

Fix floating point error in zoom division and improve navigation

Diffstat:
MMakefile | 2+-
Mntrop.c | 60+++++++++++++++++++++++++++++++++++++++---------------------
2 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -Wunused-variable -Wunused-function +CFLAGS = -Wunused-variable -Wunused-function -g LDLIBS = -lraylib -lm all: ntrop diff --git a/ntrop.c b/ntrop.c @@ -24,7 +24,7 @@ Color *entropy_colors; int entropy_ctx; -double zoom; +uint64_t zoom; double zoom_x, zoom_y; int window_width; @@ -39,6 +39,8 @@ int drag_mouse_x, drag_mouse_y; double drag_zoom_x, drag_zoom_y; bool drag; +uint64_t hold_times[337] = { 0 }; + char fmtbuf[256]; void @@ -49,12 +51,12 @@ usage(void) } uint8_t * -readfile(const char *path, size_t *len) +read_file(const char *path, size_t *len) { FILE *file; char *chunk; uint8_t *data; - size_t nread; + ssize_t nread; size_t cap; chunk = malloc(CHUNK_SIZE); @@ -68,7 +70,7 @@ readfile(const char *path, size_t *len) if (!data) err(1, "malloc"); *len = 0; - while ((nread = fread(chunk, 1, CHUNK_SIZE, file))) { + while ((nread = fread(chunk, 1, CHUNK_SIZE, file)) > 0) { if (*len + nread > cap) { cap *= 2; data = realloc(data, cap); @@ -142,8 +144,8 @@ init_entropy_colors(Color *data_colors) } entropy /= - 1.F * log2(1.F / ctx_count); entropy *= 255; - c.r = entropy; - c.g = entropy; + c.r = file_data[pos]; + c.g = 0; c.b = entropy; data_colors[pos] = c; } @@ -152,6 +154,21 @@ init_entropy_colors(Color *data_colors) free(counts); } +bool +key_press_hold(int key) +{ + if (!IsKeyDown(key)) { + hold_times[key] = 0; + return false; + } + + if (IsKeyPressed(key)) + return true; + hold_times[key] += 1; + + return hold_times[key] > 10; +} + void vis(void) { @@ -159,7 +176,7 @@ vis(void) Image window_image; size_t pos, len; double mouse_move; - size_t data_x, data_y; + ssize_t data_x, data_y; size_t data_pos; size_t x, y; @@ -194,10 +211,10 @@ vis(void) if (IsWindowResized()) { window_width = GetScreenWidth(); window_height = GetScreenHeight(); - data_width = MIN(window_width / 2, 400); + data_width = MAX(MIN(data_len, MIN(window_width / 2, 400)), 1); data_height = data_len / data_width + !!(data_len % data_width); - zoom_x = (data_width - window_width / zoom) / 2.F; - zoom_y = (data_height - window_height / zoom) / 2.F; + zoom_x = (data_width - 1.F * window_width / zoom) / 2.F; + zoom_y = (data_height - 1.F * window_height / zoom) / 2.F; ImageResize(&window_image, window_width, window_height); } @@ -236,21 +253,22 @@ vis(void) show_pos ^= IsKeyPressed(KEY_T); if (IsKeyDown(KEY_LEFT_ALT)) { - if (IsKeyDown(KEY_LEFT) && data_width > 0) + if (key_press_hold(KEY_LEFT) && data_width > 0) data_width -= 1; - else if (IsKeyDown(KEY_RIGHT)) + else if (key_press_hold(KEY_RIGHT)) data_width += 1; } else { - if (IsKeyDown(KEY_LEFT)) + if (key_press_hold(KEY_LEFT)) zoom_x -= 20.F / zoom; - else if (IsKeyDown(KEY_RIGHT)) + else if (key_press_hold(KEY_RIGHT)) zoom_x += 20.F / zoom; - if (IsKeyDown(KEY_UP)) + if (key_press_hold(KEY_UP)) zoom_y -= 20.F / zoom; - else if (IsKeyDown(KEY_DOWN)) + else if (key_press_hold(KEY_DOWN)) zoom_y += 20.F / zoom; } + data_height = data_len / data_width + !!(data_len % data_width); if (IsKeyPressed(KEY_P)) { show_value = !show_value; @@ -264,12 +282,12 @@ vis(void) ImageClearBackground(&window_image, BLACK); for (y = 0; y < window_height; y++) { - data_y = (size_t) (zoom_y + y / zoom); + data_y = (ssize_t) (zoom_y) + (y / zoom); if (data_y < 0 || data_y >= data_height) continue; for (x = 0; x < window_width; x++) { - data_x = (size_t) (zoom_x + x / zoom); + data_x = (ssize_t) (zoom_x) + (x / zoom); if (data_x < 0 || data_x >= data_width) continue; @@ -290,8 +308,8 @@ vis(void) DrawTexture(tex, 0, 0, WHITE); if (show_pos) { - data_x = zoom_x + mouse_x / zoom; - data_y = zoom_y + mouse_y / zoom; + data_x = (ssize_t) zoom_x + (mouse_x / zoom); + data_y = (ssize_t) zoom_y + (mouse_y / zoom); if (data_x >= 0 && data_x < data_width && data_y >= 0 && data_y < data_height) { pos = data_y * data_width + data_x; @@ -342,7 +360,7 @@ main(int argc, const char **argv) if (!strcmp(file_path, "-")) file_path = "/dev/stdin"; - file_data = readfile(file_path, &data_len); + file_data = read_file(file_path, &data_len); value_colors = malloc(sizeof(Color) * data_len); if (!value_colors) err(1, "malloc");