-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrayscale.cpp
More file actions
28 lines (23 loc) · 985 Bytes
/
grayscale.cpp
File metadata and controls
28 lines (23 loc) · 985 Bytes
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
#include "grayscale.h"
#include <stdexcept>
void GrayscaleFilter::ApplyFilter(Image &image) const {
static const double BLUE_CONST = 0.114;
static const double GREEN_CONST = 0.587;
static const double RED_CONST = 0.299;
auto &image_channels = image.GetChannels();
size_t height = image_channels[0].size();
size_t width = image_channels[0][0].size();
if (image_channels.size() == 3) {
// сохраняем в первый канал
for (int32_t y = 0; y < height; ++y) {
for (int32_t x = 0; x < width; ++x) {
image_channels[0][y][x] = image_channels[0][y][x] * BLUE_CONST + image_channels[1][y][x] * GREEN_CONST +
image_channels[2][y][x] * RED_CONST;
}
}
image_channels.pop_back();
image_channels.pop_back();
} else if (image_channels.size() != 1) {
throw std::runtime_error("Something wrong with channels!");
}
}