-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
140 lines (130 loc) · 5.08 KB
/
main.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
#include <getopt.h>
#include "glitch.hpp"
#include "config.h"
void read_cli_parameters(int, char**);
std::string input_file, output_file = "output.png";
std::vector<std::string> filter_sequence = {"psh"};
bool verbose = false;
int main(int argc, char** argv){
// reading arguments and loading image
read_cli_parameters(argc, argv);
glitch::Image image;
image.load(&input_file[0]);
// verbose printing
if(verbose){
printf("Input: %s | Output: %s\n", input_file.c_str(), output_file.c_str());
printf("width: %d height: %d total pixels: %d\n", image.width, image.height, image.width * image.height);
std::string flow_string;
for(int i = 0; i < filter_sequence.size(); i++){
flow_string += filter_sequence[i];
if(i < filter_sequence.size() - 1){
flow_string += " -> ";
}
}
printf("flow: [image input] -> { %s } -> [image output]\n", flow_string.c_str());
if(glitch::parallel_enabled){
printf("parallel enabled with [%d] threads\n", glitch::threads);
}
}
// applying glitch algorithm(s) sequentially, and saving image
for(std::string filter : filter_sequence){
glitch::filters.at(filter)(&image);
}
image.save(&output_file[0]);
exit(EXIT_SUCCESS);
}
void read_cli_parameters(int argc, char** argv){
const char* const short_opts = "i:o:f:c:pt:vh";
const option long_options[] = {
{"input", required_argument, nullptr, 'i'},
{"output", required_argument, nullptr, 'o'},
{"filters", required_argument, nullptr, 'f'},
{"criteria", required_argument, nullptr, 'c'},
{"parallel", no_argument, nullptr, 'p'},
{"threads", required_argument, nullptr, 't'},
{"verbose", no_argument, nullptr, 'v'},
{"help", no_argument, nullptr, 'h'},
{0, no_argument, nullptr, 0 }
};
bool failure = false;
auto print_help = [](){
printf(
"\nOptions:\n"
" -i, --input <path>: Set relative path to the input image\n"
" -o, --output <path>: Set relative path to save the output image. (default = './output.png')\n"
" -f, --filters: Set filter(s) to be applied sequentally separeted by comma (e.g. psv,psh,sv,...). (default = psh)\n"
" psv: pixel sort vertical\n"
" psh: pixel sort horizontal\n"
" sv: swap vertical\n"
" sh: swap horiztonal\n"
" s: sort pixels\n\n"
" -c, --criteria <int>: Set criteria integer for [psv, psh] filters. Recommended value between 0 - 255. (default = 100)\n"
" -p, --parallel: A flag to enable parallel computing. (default disabled; default threads = 4)\n"
" -t, --threads <int>: Set number of threads. (default = 4)\n"
" -v, --verbose: A flag to enable verbose mode. (default disabled)\n"
" -h, --help: Display this message\n\n"
);
};
while (true) {
const int opt = getopt_long(argc, argv, short_opts, long_options, nullptr);
std::string filter, filter_arg;
if (opt == -1)
break;
switch (opt) {
case 'i':
input_file = std::string(optarg);
break;
case 'o':
output_file = std::string(optarg);
break;
case 'f':
filter_sequence.clear();
filter_arg = std::string(optarg);
for(int i=0; i < filter_arg.size(); i++){
if (filter_arg[i] == ','){
filter_sequence.push_back(filter);
filter = "";
continue;
}
filter += filter_arg[i];
if (i == filter_arg.size()-1) filter_sequence.push_back(filter);
}
break;
case 'c':
glitch::PixelSorting::criteria = std::stoi(optarg);
break;
case 'p':
glitch::parallel_enabled = true;
break;
case 't':
glitch::threads = std::stoi(optarg);
break;
case 'v':
verbose = true;
break;
case 'h':
print_help();
exit(EXIT_SUCCESS);
case '?':
print_help();
failure = true;
break;
default:
print_help();
break;
}
}
if (input_file.empty()){
printf("--input/-i must be specified\n"); failure = true;
}
if (output_file.empty()){
printf("--output/-o must be specified\n"); failure = true;
}
for(const std::string filter : filter_sequence){
if(!glitch::filters.count(filter)){
printf("filter { %s } does not exist\n", filter.c_str()); failure = true;
break;
}
}
if (failure) exit(EXIT_FAILURE);
}