~ruther/CTU-FEE-B0B35APO-Semestral-project

e2a97607349d7348b7c5ff38944c8c1d642ddf0f — František Boháček 3 years ago 2c8dd44
feat: add image region move
2 files changed, 31 insertions(+), 1 deletions(-)

M image-viewer/include/image.h
M image-viewer/src/image.c
M image-viewer/include/image.h => image-viewer/include/image.h +5 -1
@@ 10,6 10,7 @@
#include <stdint.h>

#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__

M image-viewer/src/image.c => image-viewer/src/image.c +26 -0
@@ 1,6 1,9 @@
#include "image.h"
#include "display_utils.h"
#include <stdlib.h>



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];

Do not follow this link