-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_cpu.cpp
78 lines (62 loc) · 1.93 KB
/
main_cpu.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
#include <vector>
#include "bitmap.h"
using namespace std;
/*
* NOTE!
*
* This only works with 24-bit based uncompressed Bitmap format.
* Use this tool below to convert your image into compatible format.
* https://online-converting.com/image/convert2bmp/
* also, don't forget to choose "Color" option to "24 Bit (True Color)."
*
* The algorithm is based on this tutorial (with few changes)
* https://lodev.org/cgtutor/filtering.html#Sharpen
*
*/
#define filterWidth 5
#define filterHeight 5
double filter[filterHeight][filterWidth] =
{
-1, -1, -1, -1, -1,
-1, 2, 2, 2, -1,
-1, 2, 8, 2, -1,
-1, 2, 2, 2, -1,
-1, -1, -1, -1, -1,
};
double factor = 1.0 / 8.0;
double bias = 0.0;
int main () {
Bitmap image;
vector <vector <Pixel> > bmp;
image.open("image_source.bmp");
bool validBmp = image.isImage();
if (validBmp == true) {
bmp = image.toPixelMatrix();
int h = bmp.size();
int w = bmp[0].size();
// SHARPENING!
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
double red = 0.0;
double green = 0.0;
double blue = 0.0;
for (int filterY = 0; filterY < filterHeight; filterY++) {
for (int filterX = 0; filterX < filterWidth; filterX++) {
int imageX = (x - filterWidth / 2 + filterX + w) % w;
int imageY = (y - filterHeight / 2 + filterY + h) % h;
red += bmp[imageY][imageX].red * filter[filterY][filterX];
green += bmp[imageY][imageX].green * filter[filterY][filterX];
blue += bmp[imageY][imageX].blue * filter[filterY][filterX];
}
}
bmp[y][x].red = min(max(int(factor * red + bias), 0), 255);
bmp[y][x].green = min(max(int(factor * green + bias), 0), 255);
bmp[y][x].blue = min(max(int(factor * blue + bias), 0), 255);
}
}
// END SHARPENING!
image.fromPixelMatrix(bmp);
image.save("output.bmp");
}
return 0;
}