-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtersCpuMultithread.cpp
150 lines (126 loc) · 3.53 KB
/
filtersCpuMultithread.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
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
#include <iostream>
#include <vector>
#include <thread>
#include <future>
#include <functional>
#include <map>
using namespace std;
inline void filterBlueMultithread(
int parteInicio,
int parteFinal,
int channels,
unsigned char* img) {
for (int i = parteInicio; i < parteFinal; i += channels) {
img[i] = 0; // red
img[i + 1] = 0; // green
}
}
inline void filterGrayScaleMultithread(
int parteInicio,
int parteFinal,
int channels,
unsigned char* img) {
for (int i = parteInicio; i < parteFinal; i += channels) {
int gray = (img[i] + img[i + 1] + img[i + 2]) / 3;
img[i] = gray;
img[i + 1] = gray;
img[i + 2] = gray;
}
}
inline void filterDarknessMultithread(
int parteInicio,
int parteFinal,
int channels,
unsigned char* img) {
for (int i = parteInicio; i < parteFinal; i++) {
if (img[i] >= 50) {
img[i] -= 50;
} else {
img[i] = 0;
}
}
}
inline void filterSaltAndPepperMultithread(
int parteInicio,
int parteFinal,
int channels,
unsigned char* img) {
for (int i = parteInicio; i < parteFinal; i++) {
int num = rand() % 100;
if (num < 4) {
if (num % 2 == 0) {
img[i] = 255;
}
else {
img[i] = 0;
}
}
}
}
inline void filterMultithread(int filterNumber,
int width,
int height,
int channels,
unsigned char *img) {
unsigned nbThreadsHint = thread::hardware_concurrency();
unsigned nbThreads = nbThreadsHint == 0 ? 8 : (nbThreadsHint);
unsigned nbElements = width * height * channels;
unsigned batchSize = (nbElements / nbThreads);
unsigned batchRemainder = (nbElements % nbThreads);
std::vector<std::thread> myThreads(nbThreads);
// blue
if (filterNumber == 1) {
for (unsigned i = 0; i < nbThreads; i++) {
int parteInicio = i * batchSize;
myThreads[i] = std::thread(
filterBlueMultithread,
parteInicio,
parteInicio + batchSize,
channels,
img
);
}
}
// grayscale
if (filterNumber == 2) {
for (unsigned i = 0; i < nbThreads; ++i) {
int parteInicio = i * batchSize;
myThreads[i] = std::thread(
filterGrayScaleMultithread,
parteInicio,
parteInicio + batchSize,
channels,
img
);
}
}
// darkness
if (filterNumber == 3) {
for (unsigned i = 0; i < nbThreads; ++i) {
/*int parteFinal = (i == nbThreads - 1)
? parteInicio + batchWidthSize : parteInicio + batchWidthSizeRemainder;*/
int parteInicio = i * batchSize;
myThreads[i] = std::thread(
filterDarknessMultithread,
parteInicio,
parteInicio + batchSize,
channels,
img
);
}
}
// salt and pepper
if (filterNumber == 4) {
for (unsigned i = 0; i < nbThreads; ++i) {
int parteInicio = i * batchSize;
myThreads[i] = std::thread(
filterSaltAndPepperMultithread,
parteInicio,
parteInicio + batchSize,
channels,
img
);
}
}
std::for_each(myThreads.begin(), myThreads.end(), std::mem_fn(&std::thread::join));
}