From d5cc9708556191b7da3119c0398d05c9e5b2317d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Mon, 14 Jun 2021 21:37:50 +0200 Subject: [PATCH] feat: add basic image functions --- image-viewer/include/image.h | 56 +++++++++++++++++++++++++++++++++ image-viewer/src/image.c | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 image-viewer/include/image.h create mode 100644 image-viewer/src/image.c diff --git a/image-viewer/include/image.h b/image-viewer/include/image.h new file mode 100644 index 0000000..c7d1a8d --- /dev/null +++ b/image-viewer/include/image.h @@ -0,0 +1,56 @@ +#ifndef __IMAGE_H__ +#define __IMAGE_H__ + +#include +#include +#include +#include + +#include +#include + +#include "display_utils.h" + +typedef enum { + IMG_UNKNOWN, + IMG_PNG, + IMG_JPG, + IMG_PPM, +} image_type_t; + +typedef enum { + IMERR_SUCCESS, + IMERR_UNKNOWN_FORMAT, + IMERR_FILE_NOT_FOUND, + IMERR_FILE_NO_PERMISSIONS, + IMERR_FILE_CANT_OPEN, + IMERR_WRONG_FORMAT, + IMERR_UNKNOWN, +} image_error_t; + +typedef struct { + char *path; + image_type_t type; + + display_pixel_t *pixels; + + uint16_t width; + uint16_t height; +} image_t; + +typedef struct { + uint16_t x; + uint16_t y; + + uint16_t width; + uint16_t height; +} image_region_t; + +image_t image_create(char *path); +void image_destroy(image_t *image); + +image_region_t image_region_create(uint16_t x, uint16_t y, uint16_t width, uint16_t height); + +bool 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 new file mode 100644 index 0000000..792b768 --- /dev/null +++ b/image-viewer/src/image.c @@ -0,0 +1,61 @@ +#include "image.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; +} + +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; +} -- 2.48.1