Repository navigation
Expand file tree
/
Copy pathImage.cpp
More file actions
125 lines (103 loc) · 3.04 KB
/
Copy pathImage.cpp
File metadata and controls
125 lines (103 loc) · 3.04 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Created by Дарья Фомина on 21.03.2023.
//
#include "Image.h"
Image::Color::Color() {
}
Image::Color::Color(double r, double g, double b) : r_(r), g_(g), b_(b) {
}
Image::Color Image::Color::operator+(const Image::Color& other) const {
return Image::Color(r_ + other.r_, g_ + other.g_, b_ + other.b_);
}
Image::Color Image::Color::operator-(const Image::Color& other) const {
return Image::Color(r_ - other.r_, g_ - other.g_, b_ - other.b_);
}
Image::Color Image::Color::operator*(const double val) const {
return Image::Color(r_ * val, g_ * val, b_ * val);
}
Image::Color Image::Color::operator+=(const Image::Color& other) {
r_ += other.r_;
g_ += other.g_;
b_ += other.b_;
return *this;
}
Image::Color Image::Color::operator-=(const Image::Color& other) {
r_ -= other.r_;
g_ -= other.g_;
b_ -= other.b_;
return *this;
}
Image::Color Image::Color::operator*=(const double val) {
r_ *= val;
g_ *= val;
b_ *= val;
return *this;
}
bool Image::Color::operator==(const Image::Color& other) const {
if (std::abs(other.r_ - r_) > EPS) {
return false;
}
if (std::abs(other.b_ - b_) > EPS) {
return false;
}
if (std::abs(other.g_ - g_) > EPS) {
return false;
}
return true;
}
Image::Image() {
}
Image::Image(size_t height, size_t width)
: height_(height),
width_(width),
image_colors_(std::vector<std::vector<Color>>(height, std::vector<Color>(width))) {
}
size_t Image::GetHeight() const {
return height_;
}
size_t Image::GetWidth() const {
return width_;
}
Image::Color& Image::GetColor(const ssize_t x, const ssize_t y) {
std::pair<ssize_t, ssize_t> new_coord = NormalizeCoordinates(y, x);
return image_colors_[new_coord.first][new_coord.second];
}
const Image::Color& Image::GetColor(const ssize_t x, const ssize_t y) const {
std::pair<ssize_t, ssize_t> new_coord = NormalizeCoordinates(y, x);
return image_colors_[new_coord.first][new_coord.second];
}
void Image::SetColor(const ssize_t x, const ssize_t y, const Image::Color& my_color) {
std::pair<ssize_t, ssize_t> new_coord = NormalizeCoordinates(y, x);
image_colors_[new_coord.first][new_coord.second] = my_color;
}
std::pair<ssize_t, ssize_t> Image::NormalizeCoordinates(const ssize_t x, const ssize_t y) const {
std::pair<ssize_t, ssize_t> ans = {x, y};
if (x < 0) {
ans.first = 0;
} else if (x >= height_) {
ans.first = static_cast<ssize_t>(height_) - 1;
}
if (y < 0) {
ans.second = 0;
} else if (y >= width_) {
ans.second = static_cast<ssize_t>(width_) - 1;
}
return ans;
}
size_t Image::GetFileSize() const {
return file_size_;
}
void Image::SetHeight(const size_t height) {
height_ = height;
}
void Image::SetWidth(const size_t width) {
width_ = width;
}
void Image::SetFileSize(const size_t file_size) {
file_size_ = file_size;
}
void Image::EraseColor(size_t x, size_t y) {
auto it = image_colors_[y].begin();
std::advance(it, x);
image_colors_[y].erase(it);
}