From c92c83bacdff52b9668e84d70648d4cdfc87d79e Mon Sep 17 00:00:00 2001 From: itycodes Date: Wed, 22 Oct 2025 11:44:19 +0200 Subject: [PATCH] Add a C encoder It only works on little endian systems atm, because handling of endianess is a pain in C. The C# and Python encoders handle it fine. --- rif.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 rif.h diff --git a/rif.h b/rif.h new file mode 100644 index 0000000..1f6a85a --- /dev/null +++ b/rif.h @@ -0,0 +1,30 @@ +typedef struct rif { + // "lifV0001" + // "rifV0001" + char magic_num[8]; // Big Endian. UTF-8 Encoded. 6C 69 66 56 30 30 30 31 + uint64_t width; // Big Endian + uint8_t format; + uint8_t data[]; +} rif_t; + +#define RIF_MAGIC_LITTLE {'l', 'i', 'f', 'V', '0', '0', '0', '1'} +#define RIF_MAGIC_BIG {'r', 'i', 'f', 'V', '0', '0', '0', '1'} + +#define RIF_FORMAT_R8G8B8 0 +#define RIF_FORMAT_R8G8B8A8 1 + +// TODO detect endianess - this breaks on big endian systems. +void write_rif_little(char* path, uint32_t width, uint32_t size, uint8_t format, void* data) { + // TODO is this better than just hardcoding 17? + const uint32_t header_size = sizeof(rif_t) - sizeof(uint8_t*); + const uint64_t img_size = header_size + size; + rif_t* img = malloc(img_size); + char magic[] = RIF_MAGIC_LITTLE; + memcpy(&img->magic_num, magic, 8); + img->width = width; + img->format = format; + memcpy(&img->data, data, size); + FILE* output_file = fopen(path, "w"); + fwrite(img, 1, img_size, output_file); + free(img); +}