blob: 6e8f6b4fa925683eba3900bb69b19995d1967291 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#include <lib/data/image_types.h>
#include <lib/mem/galloc.h>
#include <stdio.h>
#include <stdlib.h>
Image *create_image(size_t width, size_t height, size_t channels) {
Image *ip = (Image *)g_malloc(sizeof(Image));
if (ip == NULL) {
return NULL;
}
ip->width = width;
ip->height = height;
ip->depth = channels;
ip->image = (ImageData_t ***)g_malloc(sizeof(ImageData_t **) * ip->height);
if (ip->image == NULL) {
fprintf(stderr, "Memory allocation error\n");
g_free(ip);
return NULL;
}
ImageData_t *image_data =
g_calloc(width * height * channels, sizeof(ImageData_t));
if (image_data == NULL) {
fprintf(stderr, "Memory allocation error\n");
g_free(ip->image);
g_free(ip);
return NULL;
}
for (size_t y = 0; y < height; y++) {
ip->image[y] = (ImageData_t **)g_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++) {
g_free(ip->image[ty]);
}
g_free(image_data);
g_free(ip->image);
g_free(ip);
}
for (size_t x = 0; x < width; x++) {
ip->image[y][x] = &image_data[(y * width + x) * channels];
}
}
return ip;
}
Mask *create_image_mask(size_t width, size_t height) {
Mask *ip = (Mask *)g_malloc(sizeof(Mask));
if (ip == NULL) {
return NULL;
}
ip->width = width;
ip->height = height;
ip->image = (MaskData_t **)g_malloc(sizeof(MaskData_t *) * ip->height);
if (ip->image == NULL) {
g_free(ip);
return NULL;
}
MaskData_t *image_data = g_calloc(width * height, sizeof(MaskData_t));
if (image_data == NULL) {
g_free(ip->image);
g_free(ip);
return NULL;
}
for (size_t y = 0; y < height; y++) {
ip->image[y] = &image_data[y * width];
}
return ip;
}
void free_image(Image *image) {
if (image == NULL) {
return;
}
if (image->image == NULL) {
g_free(image);
return;
}
if (image->image[0] != NULL) {
if (image->image[0][0] != NULL) {
g_free(image->image[0][0]);
}
} else {
g_free(image->image);
g_free(image);
return;
}
for (size_t y = 0; y < image->height; y++) {
if (image->image[y] != NULL) {
g_free(image->image[y]);
}
}
g_free(image->image);
image->image = NULL;
g_free(image);
}
void free_image_mask(Mask *image_mask) {
if (image_mask == NULL) {
return;
}
if (image_mask->image == NULL) {
g_free(image_mask);
return;
}
if (image_mask->image[0] != NULL) {
g_free(image_mask->image[0]);
image_mask->image[0] = NULL;
}
g_free(image_mask->image);
image_mask->image = NULL;
g_free(image_mask);
}
|