-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
73 lines (56 loc) · 2.05 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
#include <QCoreApplication>
#include <QImageReader>
#include <QImageWriter>
#include <QPainter>
#include <QDir>
#include <QDebug>
void process(const QImage &img, const QString &path)
{
const auto img_size = img.size();
const auto img_ratio = (qreal)img_size.width() / img_size.height();
const auto files = QDir(path).entryList({"*.png"});
for (const auto &f: files)
{
const auto filePath = path + '/' + f;
QImageReader r(filePath);
const auto size = r.size();
const auto ratio = (qreal)size.width() / size.height();
auto img_scaled_size = size;
if (img_ratio > ratio)
img_scaled_size.setWidth(size.height() * img_ratio);
else
img_scaled_size.setHeight(size.width() / img_ratio);
const auto img_scaled = img.scaled(img_scaled_size, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
QImage img_new(size, img.format());
img_new.fill(QColor(0,0,0,0));
QPainter painter(&img_new);
painter.fillRect(img_new.rect(), QColor(0,0,0,0));
painter.drawImage( QRectF(0,0,size.width(), size.height()), img_scaled,
QRectF((img_scaled_size.width() - size.width())/2,
(img_scaled_size.height() - size.height())/2,
size.width(), size.height()));
QFile::remove(filePath);
QImageWriter w(filePath);
w.write(img_new);
qDebug() << f;
}
const auto dirs = QDir(path).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const auto &d: dirs)
process(img, path + '/' + d);
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
const auto args = app.arguments();
if (args.count() < 3)
{
qDebug() << app.applicationName().toStdString().c_str() << "[source_icon]" << "[path]";
return 0;
}
const auto source = args.at(1);
const auto path = args.at(2);
QImageReader r(source);
const auto img = r.read();
process(img, path);
return 0;
}