-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
164 lines (149 loc) · 4.91 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <cstring>
#include <getopt.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "SeamCarver.h"
using namespace std;
using namespace cv;
enum class Energy { gradient, sobel };
static constexpr char gradientStr[] = "gradient";
static constexpr char sobelStr[] = "sobel";
void printArgparse(bool vertical, int seams, const Energy &energy);
void printUsage();
SeamCarver::Energy convertEnergy(const Energy &energy);
int main(int argc, char *argv[]) {
// options and defaults
int logging = 0;
bool vertical = true;
int seams = -1;
Energy energy = Energy::gradient;
opterr = 0;
int c;
while ((c = getopt(argc, argv, "l:hvs:e:")) != -1) {
switch (c) {
case 'l':
logging = std::stoi(optarg);
if (logging < 0 || logging >= 3) {
std::cerr << "Logging (-l) must be set to value 0, 1, or 2."
<< std::endl;
return 1;
}
break;
case 'h':
vertical = false;
break;
case 'v':
vertical = true;
break;
case 's':
seams = std::stoi(optarg);
if (seams < 0) {
std::cerr << "-s requires positive integers." << std::endl;
return 1;
}
break;
case 'e':
if (strcmp(optarg, gradientStr) == 0) {
energy = Energy::gradient;
} else if (strcmp(optarg, sobelStr) == 0) {
energy = Energy::sobel;
} else {
std::cerr << "Unknown energy option " << optarg << ". Try "
<< std::endl;
return 1;
}
break;
case '?':
std::cout << "Unknown option -" << (char)optopt << std::endl;
printUsage();
default:
return 1;
}
}
if (seams <= 0) {
std::cerr << "The number of seams needs to be larger than 0"
<< std::endl;
return 1;
}
if (logging > 1) {
printArgparse(vertical, seams, energy);
}
for (int index = optind; index < argc; index++) {
char *path = argv[index];
if (logging > 0) {
std::cout << "Processing " << path << std::endl;
}
// convert args for SeamCarver constructor
SeamCarver::Dimension dim = vertical
? SeamCarver::Dimension::Vertical
: SeamCarver::Dimension::Horizontal;
SeamCarver::Energy en = convertEnergy(energy);
// read image and check size against seams
Mat im = imread(path);
if (vertical && im.rows <= seams) {
std::cerr << "Seams must be less than image width." << std::endl;
return 1;
}
if (!vertical && im.cols <= seams) {
std::cerr << "Seams must be less than image height." << std::endl;
return 1;
}
// seam carving
SeamCarver seamCarver(im, dim, en);
seamCarver.setLogLevel(logging);
seamCarver.reduce(seams);
seamCarver.showImage();
string outPath = format("%s-out-%d.png", path, seams);
if (seamCarver.writeImage(outPath) && logging > 0) {
std::cout << "Written to" << outPath << std::endl;
} else if (logging > 0) {
std::cerr << "Could not write to" << outPath << std::endl;
}
}
return 0;
}
SeamCarver::Energy convertEnergy(const Energy &energy) {
SeamCarver::Energy en;
switch (energy) {
case Energy::gradient:
en = SeamCarver::Gradient;
break;
case Energy::sobel:
en = SeamCarver::Sobel3;
break;
}
return en;
}
void printArgparse(bool vertical, int seams, const Energy &energy) {
cout << "Performing seamcarving with options: " << endl
<< "\tlogging: enabled" << endl
<< "\tdirection: " << (vertical ? "vertical" : "horizontal") << endl
<< "\tseams: " << seams << endl
<< "\tenergy: ";
if (energy == Energy::gradient) {
cout << gradientStr;
} else if (energy == Energy::sobel) {
cout << sobelStr;
}
cout << std::endl;
}
void printUsage() {
std::cout
<< "Usage: seamcarving [OPTION]... [FILE]..." << std::endl
<< "Options:" << std::endl
<< " -l level"
<< " Logging level. 0 off, 1 info, 2 verbose." << std::endl
<< " -h"
<< " Reduce horizontal." << std::endl
<< " -v"
<< " Reduce vertical." << std::endl
<< " -s seams"
<< " Number of seams to remove." << std::endl
<< " -e algorithm"
<< " Select energy calculation from gradient, dualGradient, and sobel."
<< std::endl
<< std::endl
<< "This tool implements seam carving for content-aware image "
"downsizing."
<< std::endl;
}