-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cpp
115 lines (95 loc) · 2.13 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
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
#include "Image.h"
Image::Image(const std::string &path)
{
ImageLoader img(path);
if (img) {
_width = img.Width();
_height = img.Height();
_data = new Pixel[_height * _width];
for (int y = 0; y < _height; ++y) {
for (int x = 0; x < _width; ++x) {
_data[y * _width + x] = img.GetPixel(x, y);
}
}
}
}
Image::Image(int width, int height)
{
_data = new Pixel[width * height]{};
if(_data != nullptr)
{
_width = width;
_height = height;
}
}
Image::Image(Image &&rhs)
: _width(rhs._width),
_height(rhs._height),
_data(rhs._data)
{
rhs._data = nullptr;
}
Image::Image(const Image &rhs)
: _width(rhs._width),
_height(rhs._height),
_data(new Pixel[rhs._height * rhs._width])
{
for (int y = 0; y < _height; ++y) {
for (int x = 0; x < _width; ++x) {
_data[y * _width + x] = rhs._data[y * _width + x];
}
}
}
Image::~Image()
{
if (_data != nullptr) {
delete[] _data;
}
}
const Pixel &Image::GetPixel(const Coords &coords) const
{
return _data[_width * coords.Y() + coords.X()];
}
void Image::PutPixel(const Coords &coords, const Pixel &pix)
{
_data[_width * coords.Y() + coords.X()] = pix;
}
// for now only draws if
// _height <= screen._height && _width <= screen._width
void Image::Draw(Image &screen) const
{
for (int y = 0; y < _height; ++y) {
for (int x = 0; x < _width; ++x) {
Coords img_coords{ x, y };
Pixel pix = GetPixel(img_coords);
screen.PutPixel(img_coords, pix);
}
}
}
Image &Image::operator*=(double mul)
{
for (int y = 0; y < _height; ++y) {
for (int x = 0; x < _width; ++x) {
Coords img_coords{ x, y };
Pixel old_pix = GetPixel(img_coords);
Pixel multed_pix{
.r = static_cast<uint8_t>(old_pix.r * mul),
.g = static_cast<uint8_t>(old_pix.g * mul),
.b = static_cast<uint8_t>(old_pix.b * mul),
.a = old_pix.a
};
PutPixel(img_coords, multed_pix);
}
}
return *this;
}
Image Image::operator*(double mul) const
{
Image multed_img{ *this };
multed_img *= mul;
return multed_img;
}
Pixel *Image::Data() const
{
return _data;
}