-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.cpp
47 lines (39 loc) · 1.3 KB
/
image.cpp
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
#include "image.h"
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
#include <array>
void RGB::SetColors(std::array<unsigned char, 3> &rgb_colors) {
this->blue = rgb_colors[0] / 255.0;
this->green = rgb_colors[1] / 255.0;
this->red = rgb_colors[2] / 255.0;
}
void RGB::GetColors(std::array<unsigned char, 3> &rgb_colors) const {
rgb_colors[0] = static_cast<unsigned char>(this->blue * 255.0);
rgb_colors[1] = static_cast<unsigned char>(this->green * 255.0);
rgb_colors[2] = static_cast<unsigned char>(this->red * 255.0);
}
Image::Image(int32_t height, int32_t width) {
this->Resize(height, width);
}
RGB Image::GetPixel(int32_t row, int32_t idx) const {
return this->_matrix[row][idx];
}
void Image::SetPixel(RGB pixel, int32_t row, int32_t idx) {
this->_matrix[row][idx] = pixel;
}
std::unordered_map<std::string, int32_t> Image::GetSize() const {
std::unordered_map<std::string, int32_t> ans;
ans["height"] = this->_height;
ans["width"] = this->_width;
return ans;
}
void Image::Resize(int32_t new_height, int32_t new_width) {
this->_matrix.resize(new_height, std::vector<RGB>(new_width));
for (int i = 0; i < std::min(this->_height, new_height); ++i) {
this->_matrix[i].resize(new_width);
}
this->_height = new_height;
this->_width = new_width;
}