From dc936797c75b05bab5cab52ee2c1b4039a1c274b Mon Sep 17 00:00:00 2001 From: itycodes Date: Thu, 3 Oct 2024 07:44:37 +0200 Subject: [PATCH] Clean up rif.h a bit --- rif.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/rif.h b/rif.h index 27f2ccd..1f6a85a 100644 --- a/rif.h +++ b/rif.h @@ -13,15 +13,18 @@ typedef struct rif { #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) { -#define IMG_SIZE 17 + size // 17 is the size of raw_img excluding the data field - rif_t* img = malloc(IMG_SIZE); + // 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(((char*)img)+0, magic, 8); + memcpy(&img->magic_num, magic, 8); img->width = width; img->format = format; - memcpy(((char*)img)+17, data, size); + memcpy(&img->data, data, size); FILE* output_file = fopen(path, "w"); - fwrite(img, 1, IMG_SIZE, output_file); + fwrite(img, 1, img_size, output_file); free(img); }