Euphoria
image.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 
5 #include <memory>
6 
7 #include "base/rgb.h"
8 #include "base/memorychunk.h"
9 #include "base/rect.h"
10 
11 namespace eu::io
12 {
13  struct FileSystem;
14  struct FilePath;
15 }
16 
17 namespace eu::core
18 {
19  enum class ImageWriteFormat
20  {
21  png,
22  bmp,
23  tga,
24  jpeg
25  };
26 
27 
28  struct Image
29  {
30  // todo(Gustav): replace with a array instead of a vector
31  std::vector<unsigned char> components;
32  int width = 0;
33  int height = 0;
34  bool has_alpha = false;
35 
36  // todo(Gustav): Add create function that will create empty image and run setup
37 
38 
39  void make_invalid();
40 
41  void setup(int image_width, int image_height, bool alpha, int default_value);
42 
45  (
46  int image_width,
47  int image_height,
48  int default_value = 0
49  );
50 
52  (
53  int image_width,
54  int image_height,
55  int default_value = 0
56  );
57 
58  void set_pixel(int x, int y, const Rgbai& color);
59 
60  void set_pixel
61  (
62  int x,
63  int y,
64  unsigned char r,
65  unsigned char g,
66  unsigned char b,
67  unsigned char a
68  );
69 
70  template <typename TFunc>
71  void run_image_filter(TFunc f)
72  {
73  for(int y = 0; y < height; y += 1)
74  {
75  for(int x = 0; x < width; x += 1)
76  {
77  set_pixel(x, y, f(get_pixel(x, y)));
78  }
79  }
80  }
81 
82  template <typename TFunc>
83  void run_for_all_top_bottom(TFunc f)
84  {
85  for(int y = height; y > 0; y -= 1)
86  {
87  for(int x = 0; x < width; x += 1)
88  {
89  f(x, y - 1, get_pixel(x, y - 1));
90  }
91  }
92  }
93 
94  template <typename TFunc>
95  void set_all_top_bottom(TFunc f)
96  {
97  for(int y = height; y > 0; y -= 1)
98  {
99  for(int x = 0; x < width; x += 1)
100  {
101  set_pixel(x, y - 1, f(x, y - 1));
102  }
103  }
104  }
105 
106  template <typename TFunc>
107  void set_all_bottom_top(TFunc f)
108  {
109  for(int y = 0; y < height; y += 1)
110  {
111  for(int x = 0; x < width; x += 1)
112  {
113  set_pixel(x, y, f(x, y));
114  }
115  }
116  }
117 
118  [[nodiscard]] Rgbai get_pixel(int x, int y) const;
119  [[nodiscard]] std::shared_ptr<MemoryChunk> write(ImageWriteFormat format, int jpeg_quality = 100) const;
120  [[nodiscard]] int get_pixel_index(int x, int y) const;
121 
122  [[nodiscard]] bool is_valid() const;
123  [[nodiscard]] Recti get_indices() const;
124  [[nodiscard]] const unsigned char* get_pixel_data() const;
125  [[nodiscard]] int get_pixel_byte_size() const;
126 
127 
128  };
129 
130 
132  {
134  std::string error;
135  };
136 
137 
138  enum class AlphaLoad
139  {
140  remove,
141  keep
142  };
143 
144 
145  // todo(Gustav): move image loading to a io library instead
146  ImageLoadResult
147  load_image(io::FileSystem* fs, const io::FilePath& path, AlphaLoad alpha);
148 
149 
150  ImageLoadResult
151  load_image
152  (
153  std::shared_ptr<MemoryChunk> channel,
154  const std::string& path,
155  AlphaLoad alpha
156  );
157 
158 
159  ImageLoadResult
160  load_image
161  (
162  void* compressed_data,
163  int compressed_size,
164  const std::string& path,
165  AlphaLoad alpha
166  );
167 }
ImageWriteFormat
Definition: image.h:20
AlphaLoad
Definition: image.h:139
ImageLoadResult load_image(io::FileSystem *fs, const io::FilePath &path, AlphaLoad alpha)
Definition: image.cc:295
Definition: enum.h:8
Definition: rgb.h:45
core::Image image
Definition: image.h:133
std::string error
Definition: image.h:134
bool has_alpha
Definition: image.h:34
Recti get_indices() const
Definition: image.cc:168
std::vector< unsigned char > components
Definition: image.h:31
void set_pixel(int x, int y, const Rgbai &color)
Definition: image.cc:104
int get_pixel_byte_size() const
Definition: image.cc:32
void setup_with_alpha_support(int image_width, int image_height, int default_value=0)
if default value is negative, default value is ignored, otherwise its the default value for both R,...
Definition: image.cc:40
bool is_valid() const
Definition: image.cc:161
int get_pixel_index(int x, int y) const
Definition: image.cc:94
void run_image_filter(TFunc f)
Definition: image.h:71
Rgbai get_pixel(int x, int y) const
Definition: image.cc:137
void set_all_bottom_top(TFunc f)
Definition: image.h:107
void setup_no_alpha_support(int image_width, int image_height, int default_value=0)
Definition: image.cc:52
void set_all_top_bottom(TFunc f)
Definition: image.h:95
const unsigned char * get_pixel_data() const
Definition: image.cc:178
void run_for_all_top_bottom(TFunc f)
Definition: image.h:83
void setup(int image_width, int image_height, bool alpha, int default_value)
Definition: image.cc:63
void make_invalid()
Definition: image.cc:20
std::shared_ptr< MemoryChunk > write(ImageWriteFormat format, int jpeg_quality=100) const
Definition: image.cc:233
int height
Definition: image.h:33