From e2a97607349d7348b7c5ff38944c8c1d642ddf0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Tue, 15 Jun 2021 23:44:39 +0200 Subject: [PATCH] feat: add image region move --- image-viewer/include/image.h | 6 +++++- image-viewer/src/image.c | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/image-viewer/include/image.h b/image-viewer/include/image.h index e3ba0a7..0e63ffc 100644 --- a/image-viewer/include/image.h +++ b/image-viewer/include/image.h @@ -10,6 +10,7 @@ #include #include "display_utils.h" +#include "direction.h" typedef enum { IMG_UNKNOWN, @@ -53,8 +54,11 @@ display_pixel_t image_get_pixel(image_t *image, uint16_t x, uint16_t y); void image_set_pixel(image_t *image, uint16_t x, uint16_t y, display_pixel_t pixel); + image_region_t image_region_create(uint16_t x, uint16_t y, uint16_t width, uint16_t height); +bool image_region_move_within(image_region_t *to_move, direction_t direction, + int amount, image_region_t *border); -bool image_write_to_display(image_t *image, display_t *display, image_region_t region); +double image_write_to_display(image_t *image, display_t *display, image_region_t region); #endif // __IMAGE_H__ diff --git a/image-viewer/src/image.c b/image-viewer/src/image.c index 2bc2c91..d89884a 100644 --- a/image-viewer/src/image.c +++ b/image-viewer/src/image.c @@ -1,6 +1,9 @@ #include "image.h" +#include "display_utils.h" #include + + image_t image_create(char *path) { image_t image = { .path = path, @@ -31,6 +34,29 @@ image_region_t image_region_create(uint16_t x, uint16_t y, uint16_t width, 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]; -- 2.48.1