From c8ecce95378407e8202c2d9d0b405a3ff37f9c96 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 3 Sep 2023 20:00:10 +0100 Subject: Shamelessly stole a write_to_png routine from abelian-sandpile Found and fixed a bug I would've never noticed in abelian sandpile: the png is technically rotated by 90 using the standard WIDTH then HEIGHT for loop, so I had to switch them. As the abelian sandpile fractal is 4 way symmetric this wasn't noticeable. --- main.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'main.c') diff --git a/main.c b/main.c index 4ae63d9..7a1fda2 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,8 @@ #include #include +#include + #define WIDTH 1024 #define HEIGHT 1024 #define MAX_THREADS 4 @@ -90,6 +92,25 @@ void threads_cancel_render(void) pthread_join(threads[i], NULL); } +bool write_to_png(const char *filepath) +{ + unsigned char *image_data = calloc(3 * WIDTH * HEIGHT, sizeof(*image_data)); + + size_t image_ptr = 0; + + for (size_t i = 0; i < HEIGHT; ++i) + for (size_t j = 0; j < WIDTH; ++j, image_ptr += 3) + { + Color c = cells[(j * WIDTH) + i]; + unsigned char colour[3] = {c.r, c.g, c.b}; + memcpy(image_data + image_ptr, colour, sizeof(*colour) * 3); + } + + stbi_write_png(filepath, WIDTH, HEIGHT, 3, image_data, 3 * WIDTH); + free(image_data); + return true; +} + #define ZOOM_INC 0.3f #define MOVE_INC WIDTH / 50 int main(void) -- cgit v1.2.3-13-gbd6f