Add Image data structure

This commit is contained in:
Abdelrahman Said 2024-04-20 18:26:06 +01:00
parent 17263bf215
commit 8b3babb3ad
2 changed files with 64 additions and 0 deletions

30
src/image.c Normal file
View File

@ -0,0 +1,30 @@
#include "image.h"
#include "mem_allocator.h"
#include "mem_libc.h"
#include <stddef.h>
#include <string.h>
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;
}

34
src/image.h Normal file
View File

@ -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