|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "images.h"
|
|
|
|
|
|
|
|
|
|
|
|
int image_new(int width, int height, Image* img) {
|
|
|
|
//Image img = malloc(sizeof(Image));
|
|
|
|
|
|
|
|
if (img == NULL) return 0;
|
|
|
|
|
|
|
|
img->height = height;
|
|
|
|
img->width = width;
|
|
|
|
|
|
|
|
// initialize bitmap...
|
|
|
|
img->bitmap = malloc(height * sizeof(char*));
|
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
|
img->bitmap[i] = malloc(3 * width * sizeof(char));
|
|
|
|
}
|
|
|
|
|
|
|
|
//*image = *img;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int image_set_px(Image image, int x, int y, int r, int g, int b) {
|
|
|
|
if (!image_check_coords(image, x, y)) return 0;
|
|
|
|
|
|
|
|
image.bitmap[y][(3 * x) + 0] = r;
|
|
|
|
image.bitmap[y][(3 * x) + 1] = g;
|
|
|
|
image.bitmap[y][(3 * x) + 2] = b;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int image_set_px_c(Image image, int x, int y, Color color) {
|
|
|
|
if (!image_check_coords(image, x, y)) return 0;
|
|
|
|
|
|
|
|
if (color.alpha == 0) return 1;
|
|
|
|
|
|
|
|
if (color.alpha == 1) {
|
|
|
|
image.bitmap[y][(3 * x) + 0] = color.r;
|
|
|
|
image.bitmap[y][(3 * x) + 1] = color.g;
|
|
|
|
image.bitmap[y][(3 * x) + 2] = color.b;
|
|
|
|
} else {
|
|
|
|
image.bitmap[y][(3 * x) + 0] = (color.alpha * (color.r)) + ((1.0f - color.alpha) * (image.bitmap[y][(3 * x) + 0]));
|
|
|
|
image.bitmap[y][(3 * x) + 1] = (color.alpha * (color.g)) + ((1.0f - color.alpha) * (image.bitmap[y][(3 * x) + 1]));
|
|
|
|
image.bitmap[y][(3 * x) + 2] = (color.alpha * (color.b)) + ((1.0f - color.alpha) * (image.bitmap[y][(3 * x) + 2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int image_check_coords(Image image, int x, int y) {
|
|
|
|
return x >= 0 && x < image.width &&
|
|
|
|
y >= 0 && y < image.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
int image_destroy(Image image) {
|
|
|
|
for (int i = 0; i < image.height; i++) {
|
|
|
|
free(image.bitmap[i]);
|
|
|
|
}
|
|
|
|
free(image.bitmap);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|