42 lines
1.0 KiB
C
42 lines
1.0 KiB
C
#define STB_IMAGE_IMPLEMENTATION
|
|
#include "stb_image.h"
|
|
#include "image.h"
|
|
#include <stdlib.h>
|
|
|
|
int image_load(image_t *img, const char *filename) {
|
|
int ch;
|
|
img->data = stbi_load(filename, &img->width, &img->height, &ch, 3);
|
|
return img->data != NULL;
|
|
}
|
|
|
|
int image_resize(image_t *src, image_t *dst, int new_w, int new_h) {
|
|
dst->width = new_w;
|
|
dst->height = new_h;
|
|
dst->data = malloc(new_w * new_h * 3);
|
|
if (!dst->data) return 0;
|
|
|
|
for (int y = 0; y < new_h; y++) {
|
|
for (int x = 0; x < new_w; x++) {
|
|
int sx = x * src->width / new_w;
|
|
int sy = y * src->height / new_h;
|
|
for (int c = 0; c < 3; c++) {
|
|
dst->data[(y*new_w + x)*3 + c] =
|
|
src->data[(sy*src->width + sx)*3 + c];
|
|
}
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void image_free(image_t *img) {
|
|
if (img->data)
|
|
stbi_image_free(img->data);
|
|
img->data = NULL;
|
|
}
|
|
|
|
void image_free_raw(image_t *img) {
|
|
if (img->data)
|
|
free(img->data);
|
|
img->data = NULL;
|
|
}
|