/
main.cpp
196 lines (168 loc) · 4.78 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <QObject>
#include <QApplication>
#include <QImage>
#include <QtDebug>
#include "exceptions/BadArgsException.hpp"
#include "widgets/MainWindow.hpp"
#include "filters/Filter.hpp"
#include "Arguments.hpp"
#include "Utils.hpp"
#define EXIT_FAIL 1
void helpWrite()
{
static const char help[] =
"Usage:\n"
"1. ImageProcessor --help | -h | -?\n"
"2. ImageProcessor source_img [filters] [destination_img]\n"
"3. ImageProcessor [source_img]\n"
"\n"
"First form write this help. Second simply process image with specified\n"
"filters. Third runs GUI.\n"
"\n"
"Filters are: { filter_action [area] }\n"
"\n"
"Area is \":X0xY0+W+H\", where\n"
" X0, Y0: uint, top-left corner coordinates\n"
" W, H: uint, positive, size of area\n"
" ':', 'x', '+': fixed symbols.\n"
"\n"
"Filter actions:\n"
" -l, --autolevels <fraction> <fraction>\n"
" fraction: double\n"
" -c, --autocontrast <fraction> <fraction>\n"
" fraction: double\n"
" -x, --grayscale\n"
" -a, --gray-world\n"
" -m, --median\n"
" -g, --gaussian\n"
" -s, --sobel <edge> <what_detect>\n"
" edge: several first symbols of the word\n"
" \"Horizontal\" or \"Vertical\" (case-insensetive)\n"
" what_detect: first several symbols of the word\n"
" \"Increment\", \"Decrement\", \"Both\" (case-insensetive)\n"
" -k, --custom <kernel>\n"
" kernel: line1; ... ; lineN\n"
" line: double1, ..., doubleN\n"
" Example: '-1, -2, -1; 0, 0, 0; 1, 2, 1'\n"
" -r, --rotate <degrees>\n"
" degrees: double\n"
" -z, --resize <ratio>\n"
" ratio: double\n"
" sigma: double\n"
" -w, --wave <orientation> <amplitude> <period>\n"
" orientation: several first symbols of the word\n"
" \"Horizontal\" or \"Vertical\" (case-insensetive)\n"
" amplitude: double\n"
" period: double, degrees\n"
" -q, --glass <radius>\n"
" radius: double\n"
" -i, --glowing-edge\n"
" -o, --avg-sobel <what_detect> <what_detect>\n"
" what_detect: several first symbols of the word\n"
" \"Increment\", \"Decrement\" or \"Both\" (case-insensetive)\n"
" -p, --sharpen\n"
" -e, --maximum <radius>\n"
" radius: double.\n";
std::cout << help;
}
/* qFatal allow only constant string (not generated)
* and so not allow use QObjject::tr */
void fatal(const QString & str, bool help = false)
{
qDebug() << str;
if (help)
{
helpWrite();
}
exit(EXIT_FAIL);
}
void processFilters(QImage *& image, QListIterator<Filter *> i)
{
while (i.hasNext())
{
Filter * f = i.next();
uint w = image->width();
uint h = image->height();
if (f->getArea().isNull())
{
f->setArea(QRect(QPoint(0, 0), QSize(w, h)));
}
if (! f->isApplicable(w, h))
{
QRect area = f->getArea();
fatal(QObject::tr("Too small image (%1x%2), pointed filter"
" with pointed area (%3x%4+%5+%6) not applicable")
.arg(w).arg(h).arg(area.x()).arg(area.y())
.arg(area.width()).arg(area.height()));
}
QImage * newImage = f->filter(*image);
delete image;
image = newImage;
}
}
int runCli(Arguments & args)
{
QImage * image = new QImage(args.getSource());
if (image->format() != QImage::Format_RGB32)
{
/* For working with image with QImage::bits()
* and qRed(), qGreen() and qBlue() functions. */
*image = image->convertToFormat(QImage::Format_RGB32);
}
if (image->isNull())
{
fatal(QObject::tr("Unable to load source image."));
}
processFilters(image, args.getFilters());
bool ok = image->save(args.getDestination());
if (!ok)
{
fatal(QObject::tr("Unable to save result image."));
}
return 0;
}
int runGui(Arguments & args)
{
if (! args.getFilters().isEmpty())
{
fatal(QObject::tr(
"Point destination or remove filters arguments."));
}
MainWindow mainWindow(args.getSource());
mainWindow.show();
return qApp->exec();
}
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
Arguments args;
try
{
args.parse(app.arguments());
} catch(BadArgsException & e)
{
fatal(e.toString(), true);
}
if (args.hasHelpOption())
{
if (! args.getSource().isNull() ||
! args.getDestination().isNull() ||
! args.getFilters().isEmpty())
{
qDebug() << "Option --help pointed,"
" all others are ignored.";
}
helpWrite();
return 0;
}
if (! args.getSource().isNull() &&
! args.getDestination().isNull())
{
return runCli(args);
}
else
{
return runGui(args);
}
}