A image-viewer/include/image.h => image-viewer/include/image.h +56 -0
@@ 0,0 1,56 @@
+#ifndef __IMAGE_H__
+#define __IMAGE_H__
+
+#include <magic.h>
+#include <stdio.h>
+#include <jpeglib.h>
+#include <png.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#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__
A image-viewer/src/image.c => image-viewer/src/image.c +61 -0
@@ 0,0 1,61 @@
+#include "image.h"
+#include <stdlib.h>
+
+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;
+}