15 std::vector<std::vector<int32_t>> img;
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");
28 assert(!img_pass.empty());
29 height = img_pass.size();
30 width = img_pass[0].size();
39 explicit Image(
int height,
int width) {
42 this->height = height;
44 img = std::vector<std::vector<int32_t>>(height, std::vector<int32_t>(width, 0));
51 inline int _height()
const {
return this->height; }
57 inline int _width()
const {
return this->width; }
63 inline std::vector<std::vector<int32_t>>
get_2d_array()
const {
return this->img; }
71 inline int get_point(
const int x,
const int y)
const {
return this->img[x][y]; }
78 inline void set_point(
int x,
int y,
int val) { this->img[x][y] = val; }
86 template <
typename T>
inline void add_2_point(
const int x,
const int y,
const T val) {
96 inline int32_t
get_point(
const int x,
const int y) {
return img[x][y]; }
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) {
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());
127 assert(img2._height() == img.size());
128 assert(img2._width() == img[0].size());
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));
137 resulted_img.
add_2_point(x, y, img[x][y] + img2[x][y]);
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());
156 assert(img2._height() == img.size());
157 assert(img2._width() == img[0].size());
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));
166 resulted_img.
add_2_point(x, y, img[x][y] - img2[x][y]);
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());
185 assert(img2._height() == img.size());
186 assert(img2._width() == img[0].size());
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));
195 resulted_img.
add_2_point(x, y, img[x][y] * img2[x][y]);
208 assert(this->height > 0 && this->width > 0);
209 assert(filter.size() == 3 && filter[0].size() == 3);
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}}};
215 for (
int x = 0; x < height; ++x) {
216 for (
int y = 0; y < width; ++y) {
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];
223 if (nx >= 0 && nx < height && ny >= 0 && ny < width) {
224 value += img[nx][ny] * filter[fx][fy];
229 if constexpr (std::is_same_v<T, int32_t>) {
232 resulted_img.
set_point(x, y,
static_cast<int32_t
>(round(value)));
243 int height = img._height(), width = img._width();
245 auto write = [&](
const int& start_at,
const int& end_at) ->
void {
246 for (
int i = start_at; i <= end_at && i < height; i++) {
248 for (; j <= 10 && j < width; j++) {
249 if (j != std::min(10, width - 1)) {
250 out << img.get_point(i, j) <<
", ";
252 out << img.get_point(i, j);
263 write(height - 3, height - 1);
264 out <<
"(height: " << height <<
", width: " << width <<
')' <<
']';
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