aboutsummaryrefslogtreecommitdiff
path: root/lib/data
diff options
context:
space:
mode:
authorChristian C <cc@localhost>2025-03-23 17:47:36 -0700
committerChristian C <cc@localhost>2025-03-23 17:47:36 -0700
commit848a0f6e2d634001e30cbfebef05d93f7301facd (patch)
tree45b062c90d59e6f48284bc73c42d17d7fde19740 /lib/data
parent2fe858c22d27722130339c0d26de00aa78ef1f4d (diff)
Migration to Mask
Diffstat (limited to 'lib/data')
-rw-r--r--lib/data/image_types.c48
1 files changed, 44 insertions, 4 deletions
diff --git a/lib/data/image_types.c b/lib/data/image_types.c
index d58b8c6..0e40ad1 100644
--- a/lib/data/image_types.c
+++ b/lib/data/image_types.c
@@ -1,15 +1,43 @@
#include <lib/data/image_types.h>
#include <stdlib.h>
+#include <stdio.h>
-Image* create_image(size_t width, size_t height)
+Image* create_image(size_t width, size_t height, size_t channels)
{
Image* ip = (Image*)malloc(sizeof(Image));
+ if (ip == NULL) {
+ return NULL;
+ }
ip->width = width;
ip->height = height;
- ip->image = (ImageData_t**)malloc(sizeof(ImageData_t*) * ip->height);
- ImageData_t* image_data = calloc(width*height, sizeof(ImageData_t));
+ ip->depth = channels;
+ ip->image = (ImageData_t ***)malloc(sizeof(ImageData_t **) * ip->height);
+ if (ip->image == NULL) {
+ fprintf(stderr, "Memory allocation error\n");
+ free(ip);
+ return NULL;
+ }
+ ImageData_t *image_data = calloc(width * height*channels, sizeof(ImageData_t));
+ if (image_data == NULL) {
+ fprintf(stderr, "Memory allocation error\n");
+ free(ip->image);
+ free(ip);
+ return NULL;
+ }
for (size_t y = 0; y < height; y++) {
- ip->image[y] = &image_data[y*width];
+ ip->image[y] = (ImageData_t **)malloc(sizeof(ImageData_t *) * ip->width);
+ if (ip->image[y] == NULL) {
+ fprintf(stderr, "Memory allocation error\n");
+ for (size_t ty = 0; ty < y; ty++) {
+ free(ip->image[ty]);
+ }
+ free(image_data);
+ free(ip->image);
+ free(ip);
+ }
+ for (size_t x = 0; x < width; x++) {
+ ip->image[y][x] = &image_data[(y * width + x)*channels];
+ }
}
return ip;
}
@@ -17,10 +45,22 @@ Image* create_image(size_t width, size_t height)
Mask* create_image_mask(size_t width, size_t height)
{
Mask* ip = (Mask*)malloc(sizeof(Mask));
+ if (ip == NULL) {
+ return NULL;
+ }
ip->width = width;
ip->height = height;
ip->image = (MaskData_t**)malloc(sizeof(MaskData_t*) * ip->height);
+ if (ip->image == NULL) {
+ free(ip);
+ return NULL;
+ }
MaskData_t* image_data = calloc(width*height, sizeof(MaskData_t));
+ if (image_data == NULL) {
+ free(ip->image);
+ free(ip);
+ return NULL;
+ }
for (size_t y = 0; y < height; y++) {
ip->image[y] = &image_data[y*width];
}