-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_creators.h
117 lines (115 loc) · 3.43 KB
/
filter_creators.h
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
//
// Created by Timofey Cherlenok on 22.03.2023.
//
#pragma once
#include "filter.h"
#include "crop.h"
#include "negative.h"
#include "sharpening.h"
#include "grayscale.h"
#include "gaussian_blur.h"
#include "edge_detection.h"
#include "custom_gradient.h"
Filter* CreateCrop(const std::vector<std::string>& args) {
if (args.size() != CropFilter::NUM_PARAM) {
return nullptr;
}
try {
std::string::const_iterator it = args[0].begin();
while (it != args[0].end() && std::isdigit(*it)) {
++it;
}
if (args[0].empty() || it != args[0].end()) {
return nullptr;
}
it = args[1].begin();
while (it != args[1].end() && std::isdigit(*it)) {
++it;
}
if (args[1].empty() || it != args[1].end()) {
return nullptr;
}
if (std::stoi(args[0]) <= 0 || std::stoi(args[1]) <= 0) {
return nullptr;
}
CropFilter* crop = new CropFilter(std::stoi(args[0]), std::stoi(args[1]));
return crop;
} catch (std::exception e) {
return nullptr;
}
}
Filter* CreateNegative(const std::vector<std::string>& args) {
if (args.size() != NegativeFilter::NUM_PARAM) {
return nullptr;
}
NegativeFilter* negative = new NegativeFilter();
return negative;
}
Filter* CreateSharpening(const std::vector<std::string>& args) {
if (args.size() != SharpeningFilter::NUM_PARAM) {
return nullptr;
}
SharpeningFilter* sharp = new SharpeningFilter();
return sharp;
}
Filter* CreateGray(const std::vector<std::string>& args) {
if (args.size() != GrayScaleFilter::NUM_PARAM) {
return nullptr;
}
GrayScaleFilter* gray = new GrayScaleFilter();
return gray;
}
Filter* CreateBlur(const std::vector<std::string>& args) {
if (args.size() != GaussianBlurFilter::NUM_PARAM) {
return nullptr;
}
try {
std::string::const_iterator it = args[0].begin();
size_t num_dots = 0;
while (it != args[0].end() && (std::isdigit(*it) || *it == '.')) {
if (*it == '.') {
++num_dots;
}
++it;
}
if (args[0].empty() || it != args[0].end() || num_dots > 1) {
return nullptr;
}
GaussianBlurFilter* blur = new GaussianBlurFilter(std::stod(args[0]));
return blur;
} catch (std::exception e) {
return nullptr;
}
}
Filter* CreateEdgeDetection(const std::vector<std::string>& args) {
if (args.size() != EdgeDetectionFilter::NUM_PARAM) {
return nullptr;
}
try {
std::string::const_iterator it = args[0].begin();
size_t num_dots = 0;
while (it != args[0].end() && (std::isdigit(*it) || *it == '.')) {
if (*it == '.') {
++num_dots;
}
++it;
}
if (args[0].empty() || it != args[0].end() || num_dots > 1) {
return nullptr;
}
EdgeDetectionFilter* edge = new EdgeDetectionFilter(std::stod(args[0]));
return edge;
} catch (std::exception e) {
return nullptr;
}
}
Filter* CreateCustom(const std::vector<std::string>& args) {
if (args.size() != EdgeDetectionFilter::NUM_PARAM) {
return nullptr;
}
if (args[0] == "r" || args[0] == "g" || args[0] == "b") {
CustomGradientFilter* custom = new CustomGradientFilter(args[0][0]);
return custom;
}
return nullptr;
}