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

ref: 2c8dd4443d11b69e67d48574bacfb9a136ca22f9 CTU-FEE-B0B35APO-Semestral-project/image-viewer/src/image.c -rw-r--r-- 1.7 KiB
2c8dd444 — František Boháček feat: add image set and get pixel functions 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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;
}


display_pixel_t image_get_pixel(image_t *image, uint16_t x, uint16_t y) {
  return image->pixels[y * image->width + x];
}

void image_set_pixel(image_t *image, uint16_t x, uint16_t y,
                       display_pixel_t pixel) {
  image->pixels[y * image->width + x] = pixel;
}
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;
}
Do not follow this link