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

d455f917b833f921d421099418c31ba355de90ff — František Boháček 3 years ago 08e7267
feat: add outline for cursor
2 files changed, 67 insertions(+), 25 deletions(-)

M image-viewer/include/cursor.h
M image-viewer/src/cursor.c
M image-viewer/include/cursor.h => image-viewer/include/cursor.h +3 -2
@@ 7,7 7,7 @@
#include <time.h>
#include <stdint.h>

#define CURSOR_SIZE 9
#define CURSOR_WIDTH 11

typedef struct {
  uint16_t x;


@@ 16,10 16,11 @@ typedef struct {
  bool shown;
  time_t shown_at;

  display_pixel_t previous_display_data[CURSOR_SIZE * 2 - 1];
  display_pixel_t previous_display_data[CURSOR_WIDTH * CURSOR_WIDTH];
} cursor_t;

extern const display_pixel_t CURSOR_COLOR;
extern const display_pixel_t CURSOR_OUTLINE_COLOR;

cursor_t cursor_create();
void cursor_center(cursor_t *cursor, image_region_t region);

M image-viewer/src/cursor.c => image-viewer/src/cursor.c +64 -23
@@ 4,7 4,26 @@
#include "image.h"
#include "coords.h"

const display_pixel_t CURSOR_COLOR = {.fields = {.r = (uint8_t)DISPLAY_MAX_RED, .g = 0, .b = 0}};
const uint8_t CURSOR[] = {
  0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 1st line
  0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 2nd line
  0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 3rd line
  0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 4th line
  0x2, 0x2, 0x2, 0x2, 0x2, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, // end of 5th line
  0x2, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2, // end of 6th line
  0x2, 0x2, 0x2, 0x2, 0x2, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, // end of 7th line
  0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 8th line
  0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 9th line
  0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 10th line
  0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 11th line
  0x0, 0x0
};

const display_pixel_t CURSOR_OUTLINE_COLOR = {.fields = {.r = (uint8_t)DISPLAY_MAX_RED,
                                                 .g = DISPLAY_MAX_GREEN,
                                                 .b = DISPLAY_MAX_BLUE}};
const display_pixel_t CURSOR_COLOR = {
    .fields = {.r = 0, .g = 0, .b = 0}};

cursor_t cursor_create() {
  cursor_t cursor = {


@@ 55,18 74,30 @@ void cursor_show(cursor_t *cursor, image_t *image, image_zoom_t zoom,
  uint16_t base_x = screen_coords.x;
  uint16_t base_y = screen_coords.y;

  uint16_t first_x = base_x - CURSOR_SIZE / 2;
  uint16_t first_y = base_y - CURSOR_SIZE / 2;

  for (int i = 0; i < CURSOR_SIZE; i++) {
    uint16_t x = first_x + i;
    uint16_t y = first_y + i;

    cursor->previous_display_data[i] = display_get_pixel(display, base_x, y);
    cursor->previous_display_data[i + CURSOR_SIZE] = display_get_pixel(display, x, base_y);

    display_set_pixel(display, base_x, y, CURSOR_COLOR);
    display_set_pixel(display, x, base_y, CURSOR_COLOR);
  uint16_t first_x = base_x - CURSOR_WIDTH / 2;
  uint16_t first_y = base_y - CURSOR_WIDTH / 2;

  for (uint16_t iy = 0; iy < CURSOR_WIDTH; iy++) {
    for (uint16_t ix = 0; ix < CURSOR_WIDTH; ix++) {
      uint16_t x = first_x + ix;
      uint16_t y = first_y + iy;

      uint8_t colorn = CURSOR[iy * CURSOR_WIDTH + ix];
      display_pixel_t color;
      switch (colorn) {
      case 0x1:
        color = CURSOR_COLOR;
        break;
      case 0x2:
        color = CURSOR_OUTLINE_COLOR;
        break;
      default:
        continue;
      }

      cursor->previous_display_data[iy * CURSOR_WIDTH + ix] = display_get_pixel(display, x, y);
      display_set_pixel(display, x, y, color);
    }
  }
}



@@ 80,16 111,26 @@ void cursor_hide(cursor_t *cursor, image_t *image, image_zoom_t zoom,
      image_get_screen_coords(image, zoom, coords_create(cursor->x, cursor->y));
  uint16_t base_x = screen_coords.x;
  uint16_t base_y = screen_coords.y;

  uint16_t first_x = base_x - CURSOR_SIZE / 2;
  uint16_t first_y = base_y - CURSOR_SIZE / 2;

  for (int i = 0; i < CURSOR_SIZE; i++) {
    uint16_t x = first_x + i;
    uint16_t y = first_y + i;

    display_set_pixel(display, base_x, y, cursor->previous_display_data[i]);
    display_set_pixel(display, x, base_y, cursor->previous_display_data[i + CURSOR_SIZE]);
  uint16_t first_x = base_x - CURSOR_WIDTH / 2;
  uint16_t first_y = base_y - CURSOR_WIDTH / 2;

  for (int iy = 0; iy < CURSOR_WIDTH; iy++) {
    for (int ix = 0; ix < CURSOR_WIDTH; ix++) {
      uint16_t x = first_x + ix;
      uint16_t y = first_y + iy;

      uint8_t colorn = CURSOR[iy * CURSOR_WIDTH + ix];
      switch (colorn) {
      case 0x1:
      case 0x2:
        break;
      default:
        continue;
      }

      display_set_pixel(display, x, y,
                        cursor->previous_display_data[iy * CURSOR_WIDTH + ix]);
    }
  }

  cursor->shown = false;

Do not follow this link