From 8b3babb3ad14f33c2067c1521bc5cb1502c339c7 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 20 Apr 2024 18:26:06 +0100 Subject: [PATCH] Add Image data structure --- src/image.c | 30 ++++++++++++++++++++++++++++++ src/image.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/image.c create mode 100644 src/image.h diff --git a/src/image.c b/src/image.c new file mode 100644 index 0000000..38b8d6e --- /dev/null +++ b/src/image.c @@ -0,0 +1,30 @@ +#include "image.h" +#include "mem_allocator.h" +#include "mem_libc.h" +#include +#include + +Image *create_image(u64 width, u64 height, Pixel *data, + const Allocator *allocator) { + Allocator alloc; + if (!allocator) { + alloc = wapp_mem_libc_allocator(); + } else { + alloc = *allocator; + } + + u64 buf_length = width * height; + u64 total_size = sizeof(Image) + sizeof(Pixel) * buf_length; + + Image *img = wapp_mem_allocator_alloc(&alloc, total_size); + if (!img) { + return NULL; + } + + img->width = width; + img->height = height; + img->buf_length = buf_length; + memcpy(img->data, data, buf_length); + + return img; +} diff --git a/src/image.h b/src/image.h new file mode 100644 index 0000000..eb27493 --- /dev/null +++ b/src/image.h @@ -0,0 +1,34 @@ +#ifndef IMAGE_H +#define IMAGE_H + +#include "mem_allocator.h" +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#include "aliases.h" + +typedef struct pixel Pixel; +struct pixel { + u8 r; + u8 g; + u8 b; + u8 a; +}; + +typedef struct image Image; +struct image { + u64 width; + u64 height; + u64 buf_length; + Pixel data[]; +}; + +Image *create_image(u64 width, u64 height, Pixel *data, + const Allocator *allocator); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // !IMAGE_H