From 0aee9aee5b2ea264d6fe89d05784ba3837469995 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Fri, 22 Mar 2024 00:25:48 +0000 Subject: [PATCH] Implement basic PPM image writer --- ppm.c | 22 ++++++++++++++++++++++ ppm.h | 9 +++++++++ 2 files changed, 31 insertions(+) create mode 100644 ppm.c create mode 100644 ppm.h diff --git a/ppm.c b/ppm.c new file mode 100644 index 0000000..9286e7c --- /dev/null +++ b/ppm.c @@ -0,0 +1,22 @@ +#include "ppm.h" +#include "aliases.h" +#include +#include + +void write_p6_ppm(const char *filepath, u64 width, u64 height, Pixel *data) { + FILE *fp = fopen(filepath, "wb"); + if (!fp) { + return; + } + + char header[1024] = {0}; + sprintf(header, "P6\n%lu %lu\n255\n", width, height); + + u64 length = strlen(header); + fwrite(&header, length, 1, fp); + + u64 pixel_count = width * height; + fwrite(data, sizeof(Pixel), pixel_count, fp); + + fclose(fp); +} diff --git a/ppm.h b/ppm.h new file mode 100644 index 0000000..d8ac00b --- /dev/null +++ b/ppm.h @@ -0,0 +1,9 @@ +#include "aliases.h" + +typedef struct { + u8 r; + u8 g; + u8 b; +} Pixel; + +void write_p6_ppm(const char *filepath, u64 width, u64 height, Pixel *data);