-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
195 lines (176 loc) · 4.06 KB
/
Image.cpp
File metadata and controls
195 lines (176 loc) · 4.06 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "Image.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
Image::Image(u32 w, u32 h) : width{w}, height{h}
{
data = new rgb[w * h];
}
Image::Image(const char *a)
{
data = (rgb*)stbi_load(a, (int*)(&width), (int*)(&height), (int*)(&channels), 0);
}
Image::Image(const Image &source) //copy constructor
{
for (u32 y = 0; y < height; y++)
{
for (u32 x = 0; x < width; x++)
{
this->data[y * width + x] = source.data[y * width + x];
}
}
}
Image& Image::operator = (const Image &source)
{
for (u32 y = 0; y < height; y++)
{
for (u32 x = 0; x < width; x++)
{
this->data[y * width + x] = source.data[y * width + x];
}
}
return *this;
}
Image::~Image()
{
delete[] data;
}
void Image::fill(rgb c)
{
for (u32 y = 0; y < height; y++)
{
for (u32 x = 0; x < width; x++)
{
this->pixel(x, y, c);
}
}
}
void Image::pixel(u32 x, u32 y, rgb c)
{
if (x >= width || y >= height) return;
else
{
data[y * width + x] = c;
}
}
rgb Image::get_pixel(int x, int y)
{
if (x < 0) x = 0;
if (x >= width) x = width - 1;
if (y < 0) y = 0;
if (y >= height) y = height - 1;
return data[y * width + x];
}
Image Image::convolution(vector<double> *kernel)
{
Image buffer(width, height);
u8 size = sqrt(kernel->size());
for (u32 y = 0; y < height; y++)
{
for (u32 x = 0; x < width; x++)
{
double r = 0, g = 0, b = 0;
auto it = kernel->begin();
for (int ky = (int)y - size / 2; ky <= (int)y + size / 2; ky++)
{
for (int kx = (int)x - size / 2; kx <= (int)x + size / 2; kx++)
{
r += (double)this->get_pixel(kx, ky).r * (*it);
g += (double)this->get_pixel(kx, ky).g * (*it);
b += (double)this->get_pixel(kx, ky).b * (*it);
it++;
}
}
buffer.pixel(x, y, rgb((u8)r, (u8)g, (u8)b));
}
}
return buffer;
}
Image Image::blur(int size)
{
Image buffer(width, height);
for (u32 y = 0; y < height; y++)
{
for (u32 x = 0; x < width; x++)
{
double r = 0, g = 0, b = 0;
for (int ky = (int)y - size / 2; ky <= (int)y + size / 2; ky++)
{
for (int kx = (int)x - size / 2; kx <= (int)x + size / 2; kx++)
{
r += (double)this->get_pixel(kx, ky).r;
g += (double)this->get_pixel(kx, ky).g;
b += (double)this->get_pixel(kx, ky).b;
}
}
r /= (double)(size * size);
g /= (double)(size * size);
b /= (double)(size * size);
buffer.pixel(x, y, rgb((u8)r, (u8)g, (u8)b));
}
}
return buffer;
}
void Image::line(u32 x1, u32 y1, u32 x2, u32 y2, rgb c)
{
u32 xmax = max(x1, x2), xmin = min(x1, x2), ymax = max(y1, y2), ymin = min(y1, y2);
if (x1 == x2)
{
for (ymin; ymin <= ymax; ymin++) this->pixel(x1, ymin, c);
}
else if (y1 == y2)
{
for (xmin; xmin <= xmax; xmin++) this->pixel(xmin, y1, c);
}
else if ((xmax - xmin) == (ymax - ymin))
{
if ((x1 > x2 && y1 > y2) || (x2 > x1 && y2 > y1) )
{
for (xmin; xmin <= xmax; xmin++, ymin++) this->pixel(xmin, ymin, c);
}
else
{
for (xmin; xmin <= xmax; xmin++, ymax--) this->pixel(xmin, ymax, c);
}
}
else
{
double slope = ((double)y2 - y1) / ((double)x2 - x1);
double offset = ((double)x2 * y1 - (double)x1 * y2) / ((double)x2 - x1);
if (xmax - xmin > ymax - ymin)
{
for (xmin; xmin <= xmax; xmin++) this->pixel(xmin, slope * xmin + offset, c);
}
else
{
for (ymin; ymin <= ymax; ymin++) this->pixel((ymin - offset) / slope, ymin, c);
}
}
}
void Image::rect(u32 x, u32 y, u32 w, u32 h, rgb c)
{
for (u32 k = y; k < h + y; k++)
{
for (u32 m = x; m < w + x; m++)
{
this->pixel(m, k, c);
}
}
}
void Image::point(u32 x, u32 y, rgb c)
{
u8 add = 2;
for (int k = (int)y - add; k <= (int)y + add; k++)
{
for (int m = (int)x - add; m <= (int)x + add; m++)
{
if (k >= height || m >= width) continue;
this->pixel(m, k, c);
}
}
}
void Image::save_as(const char *s)
{
stbi_write_png(s, width, height, channels, data, width * channels);
}