fileformatsproject-legacy/lib/image.c

34 lines
945 B
C

#include "image.h"
struct image* image_initialization(size_t width, size_t height) {
struct image* image = malloc(sizeof(struct image));
if (image == NULL) {
perror("Error (image_initialization)");
exit(EXIT_FAILURE);
}
image->height = height;
image->width = width;
image->pixels = malloc(sizeof(struct pixel*) * image->height);
for (size_t row = 0; row < image->height; row++) {
image->pixels[row] = malloc(sizeof(struct pixel) * image->width);
for (size_t column = 0; column < image->width; column++) {
image->pixels[row][column].red = 0;
image->pixels[row][column].green = 0;
image->pixels[row][column].blue = 0;
}
}
return image;
}
void image_free(struct image* image) {
if (image == NULL) {
perror("Error (image_free)");
exit(EXIT_FAILURE);
}
for (size_t row = 0; row < image->height; row++) {
free(image->pixels[row]);
}
free(image->pixels);
free(image);
}