#include "image.h" #include "display_utils.h" #include image_t image_create(char *path) { image_t image = { .path = path, .width = 0, .height = 0, .pixels = NULL, .type = IMG_UNKNOWN, }; return image; } void image_destroy(image_t *image) { if (image->pixels == NULL) { free(image->pixels); } } image_region_t image_region_create(uint16_t x, uint16_t y, uint16_t width, uint16_t height) { image_region_t region = { .x = x, .y = y, .width = width, .height = height, }; return region; } bool image_region_move_within(image_region_t *to_move, direction_t direction, int amount, image_region_t *border) { uint16_t x = to_move->x; uint16_t y = to_move->y; if (x < border->x) { x = border->x; } else if (x + to_move->width >= border->width + border->x) { x = border->x + border->width - 1; } if (y < border->y) { y = border->y; } else if (y + to_move->height >= border->height + border->y) { y = border->x + border->height - 1; } bool changed = to_move->x != x || to_move->y != y; to_move->x = x; to_move->y = y; return changed; } display_pixel_t image_get_pixel(image_t *image, uint16_t x, uint16_t y) { return image->pixels[y * image->width + x]; } void image_set_pixel(image_t *image, uint16_t x, uint16_t y, display_pixel_t pixel) { image->pixels[y * image->width + x] = pixel; } double get_scale_factor(uint16_t w, uint16_t h, image_region_t display_region) { double scale_x = (double)display_region.width / (double)w; double scale_y = (double)display_region.height / (double)h; double max = scale_x > scale_y ? scale_x : scale_y; double min = scale_x <= scale_y ? scale_x : scale_y; double scale = max; if (w * max > display_region.width || h * max > display_region.height) { scale = min; } return scale; } bool image_write_to_display(image_t *image, display_t *display, image_region_t region) { uint16_t w = region.width, h = region.height; uint16_t x = region.x, y = region.y; image_region_t display_region = image_region_create(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT); double scale = get_scale_factor(w, h, display_region); uint16_t sw = (uint16_t)(scale * w), sh = (uint16_t)(scale * h); // scaling return true; }