diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-27 21:53:28 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-27 21:53:28 +0100 |
commit | 2d11304ab4726d3904344d908c93c84a76391707 (patch) | |
tree | aa6d1d2ddb4225fb57f10d4ab06eebc52e8bb92a /files.c | |
parent | 026aa887f99d87932fd5316152a15c1a79f5a6e2 (diff) | |
download | abelian-sandpile-2d11304ab4726d3904344d908c93c84a76391707.tar.gz abelian-sandpile-2d11304ab4726d3904344d908c93c84a76391707.tar.bz2 abelian-sandpile-2d11304ab4726d3904344d908c93c84a76391707.zip |
~file-handler->files,(main~>files)~migrated png save code to files.c
Diffstat (limited to 'files.c')
-rw-r--r-- | files.c | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -0,0 +1,50 @@ +/* file-handler.c + * Created: 2023-08-25 + * Author: Aryadev Chavali + * Description: Implementations of writing and loading state->from files + */ + +#include <stdio.h> +#include <string.h> + +#include <stb/stb_image_write.h> + +#include "./lib.h" + +#define CHUNK_SIZE 1024 + +bool write_to_png(state_t *state, const char *filepath) +{ + unsigned char *image_data = + calloc(3 * state->dwidth * state->dwidth, sizeof(*image_data)); + + size_t image_ptr = 0; + + for (size_t i = 0; i < state->dwidth; ++i) + for (size_t j = 0; j < state->dwidth; ++j, image_ptr += 3) + { + unsigned char colour[3] = {0}; + uint64_t sandpile = state->data[(i * state->dwidth) + j]; + if (sandpile == 0) + colour[0] = colour[1] = colour[2] = 0; + else if (sandpile == 1) + colour[0] = colour[2] = 255; + else if (sandpile == 2) + { + colour[0] = 255; + colour[1] = colour[2] = 20; + } + else if (sandpile == 3) + { + colour[2] = 255; + colour[0] = colour[1] = 20; + } + + memcpy(image_data + image_ptr, colour, sizeof(*colour) * 3); + } + + stbi_write_png(filepath, state->dwidth, state->dwidth, 3, image_data, + 3 * state->dwidth); + free(image_data); + return true; +} |