AlgoPlus v0.1.0
Loading...
Searching...
No Matches
image.h
1#ifndef img_H
2#define img_H
3
4#ifdef __cplusplus
5#include <cassert>
6#include <cinttypes>
7#include <cmath>
8#include <cstdint>
9#include <iostream>
10#include <vector>
11#endif
12
13class Image {
14 private:
15 std::vector<std::vector<int32_t>> img;
16 int height;
17 int width;
18
19 public:
24 explicit Image(std::vector<std::vector<int32_t>> img_pass = {}) {
25 if (img_pass.empty()) {
26 throw std::invalid_argument("Image dimensions can't be 0");
27 }
28 assert(!img_pass.empty());
29 height = img_pass.size();
30 width = img_pass[0].size();
31 img = img_pass;
32 }
33
39 explicit Image(int height, int width) {
40 assert(height > 0);
41 assert(width > 0);
42 this->height = height;
43 this->width = width;
44 img = std::vector<std::vector<int32_t>>(height, std::vector<int32_t>(width, 0));
45 }
46
51 inline int _height() const { return this->height; }
52
57 inline int _width() const { return this->width; }
58
63 inline std::vector<std::vector<int32_t>> get_2d_array() const { return this->img; }
64
71 inline int get_point(const int x, const int y) const { return this->img[x][y]; }
72
78 inline void set_point(int x, int y, int val) { this->img[x][y] = val; }
79
86 template <typename T> inline void add_2_point(const int x, const int y, const T val) {
87 img[x][y] += val;
88 }
89
96 inline int32_t get_point(const int x, const int y) { return img[x][y]; }
97
104 inline bool binary() const {
105 for (int i = 0; i < height; i++) {
106 for (int j = 0; j < width; j++) {
107 if (img[i][j] != 0 && img[i][j] != 255) {
108 return false;
109 }
110 }
111 }
112 return true;
113 }
114
121 template <typename T> inline Image add(const T img2) const {
122 if constexpr (std::is_same_v<T, std::vector<std::vector<int32_t>>>) {
123 assert(!img2.empty());
124 assert(img2.size() == img.size());
125 assert(img2[0].size() == img[0].size());
126 } else {
127 assert(img2._height() == img.size());
128 assert(img2._width() == img[0].size());
129 }
130
131 Image resulted_img(height, width);
132 for (int x = 0; x < height; x++) {
133 for (int y = 0; y < width; y++) {
134 if constexpr (std::is_same_v<T, Image>) {
135 resulted_img.add_2_point(x, y, img[x][y] + img2.get_point(x, y));
136 } else {
137 resulted_img.add_2_point(x, y, img[x][y] + img2[x][y]);
138 }
139 }
140 }
141 return resulted_img;
142 }
143
150 template <typename T> inline Image sub(const T img2) const {
151 if constexpr (std::is_same_v<T, std::vector<std::vector<int32_t>>>) {
152 assert(!img2.empty());
153 assert(img2.size() == img.size());
154 assert(img2[0].size() == img[0].size());
155 } else {
156 assert(img2._height() == img.size());
157 assert(img2._width() == img[0].size());
158 }
159
160 Image resulted_img(height, width);
161 for (int x = 0; x < height; x++) {
162 for (int y = 0; y < width; y++) {
163 if constexpr (std::is_same_v<T, Image>) {
164 resulted_img.add_2_point(x, y, img[x][y] - img2.get_point(x, y));
165 } else {
166 resulted_img.add_2_point(x, y, img[x][y] - img2[x][y]);
167 }
168 }
169 }
170 return resulted_img;
171 }
172
179 template <typename T> inline Image mul(const T img2) const {
180 if constexpr (std::is_same_v<T, std::vector<std::vector<int32_t>>>) {
181 assert(!img2.empty());
182 assert(img2.size() == img.size());
183 assert(img2[0].size() == img[0].size());
184 } else {
185 assert(img2._height() == img.size());
186 assert(img2._width() == img[0].size());
187 }
188
189 Image resulted_img(height, width);
190 for (int x = 0; x < height; x++) {
191 for (int y = 0; y < width; y++) {
192 if constexpr (std::is_same_v<T, Image>) {
193 resulted_img.add_2_point(x, y, img[x][y] * img2.get_point(x, y));
194 } else {
195 resulted_img.add_2_point(x, y, img[x][y] * img2[x][y]);
196 }
197 }
198 }
199 return resulted_img;
200 }
201
207 template <typename T> inline Image apply_filter2d(std::vector<std::vector<T>>& filter) const {
208 assert(this->height > 0 && this->width > 0);
209 assert(filter.size() == 3 && filter[0].size() == 3);
210
211 Image resulted_img(height, width);
212 int offsets[3][3][2] = {
213 {{-1, -1}, {-1, 0}, {-1, 1}}, {{0, -1}, {0, 0}, {0, 1}}, {{1, -1}, {1, 0}, {1, 1}}};
214
215 for (int x = 0; x < height; ++x) {
216 for (int y = 0; y < width; ++y) {
217 T value = 0;
218 for (int fx = 0; fx < 3; ++fx) {
219 for (int fy = 0; fy < 3; ++fy) {
220 int nx = x + offsets[fx][fy][0];
221 int ny = y + offsets[fx][fy][1];
222
223 if (nx >= 0 && nx < height && ny >= 0 && ny < width) {
224 value += img[nx][ny] * filter[fx][fy];
225 }
226 }
227 }
228
229 if constexpr (std::is_same_v<T, int32_t>) {
230 resulted_img.set_point(x, y, value);
231 } else {
232 resulted_img.set_point(x, y, static_cast<int32_t>(round(value)));
233 }
234 }
235 }
236 return resulted_img;
237 }
238
242 inline friend std::ostream& operator<<(std::ostream& out, const Image& img) {
243 int height = img._height(), width = img._width();
244
245 auto write = [&](const int& start_at, const int& end_at) -> void {
246 for (int i = start_at; i <= end_at && i < height; i++) {
247 int j = 0;
248 for (; j <= 10 && j < width; j++) {
249 if (j != std::min(10, width - 1)) {
250 out << img.get_point(i, j) << ", ";
251 } else {
252 out << img.get_point(i, j);
253 }
254 }
255 out << '\n';
256 }
257 };
258
259 out << '[';
260 write(0, 2);
261 out << "...";
262 out << '\n';
263 write(height - 3, height - 1);
264 out << "(height: " << height << ", width: " << width << ')' << ']';
265 out << '\n';
266 return out;
267 }
268};
269#endif
int get_point(const int x, const int y) const
get_point function
Definition image.h:71
Image(int height, int width)
Image secondary constructor.
Definition image.h:39
int _width() const
width function
Definition image.h:57
void set_point(int x, int y, int val)
set_point function
Definition image.h:78
Image sub(const T img2) const
sub function adds the img2 to img
Definition image.h:150
Image add(const T img2) const
add function adds the img2 to img
Definition image.h:121
bool binary() const
binary function. Check if an image is black and white
Definition image.h:104
int32_t get_point(const int x, const int y)
get_point function
Definition image.h:96
friend std::ostream & operator<<(std::ostream &out, const Image &img)
overloaded operator << for Image class
Definition image.h:242
void add_2_point(const int x, const int y, const T val)
add_2_point function
Definition image.h:86
std::vector< std::vector< int32_t > > get_2d_array() const
get_2d_array function
Definition image.h:63
Image(std::vector< std::vector< int32_t > > img_pass={})
image default constructor
Definition image.h:24
Image mul(const T img2) const
mul function multiplies the img2 to img
Definition image.h:179
Image apply_filter2d(std::vector< std::vector< T > > &filter) const
apply_filter2d function
Definition image.h:207
int _height() const
_height function
Definition image.h:51