-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
161 lines (138 loc) · 4.84 KB
/
mainwindow.cpp
File metadata and controls
161 lines (138 loc) · 4.84 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "entropy.hpp"
#include "entropyfast.hpp"
#include <QDir>
#include <QImage>
#include <QFileDialog>
#include <QFileInfo>
#include <QtConcurrent>
#include <ciso646>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(this, &MainWindow::openImageSucceeded, this, &MainWindow::onOpenImageSuccess);
connect(this, &MainWindow::openImageFailed, this, &MainWindow::onOpenImageFailure);
connect(this, &MainWindow::entropyImageReady, this, &MainWindow::onEntropyImageReady);
connect(this, &MainWindow::imageSaved, this, &MainWindow::onImageSaved);
}
MainWindow::~MainWindow()
{
delete ui;
}
////////////////////////////////////////////////////////////////////////////////
void MainWindow::asyncOpenImageFrom(const QString &imageFilename)
{
ui->wdgButtons->setEnabled(false);
ui->statusbar->showMessage("Loading " + imageFilename);
QtConcurrent::run( [=]() {
if( QImage image(imageFilename); not image.isNull() )
emit openImageSucceeded(imageFilename, image);
else
emit openImageFailed(imageFilename);
});
}
void MainWindow::onOpenImageSuccess(const QString &imageFilename, const QImage & image)
{
setInputImage(imageFilename, image);
ui->statusbar->showMessage("Loaded " + imageFilename, 5000);
ui->wdgButtons->setEnabled(true);
}
void MainWindow::onOpenImageFailure(const QString & imageFilename)
{
ui->statusbar->showMessage("Failed to load image: " + imageFilename);
ui->wdgButtons->setEnabled(true);
}
void MainWindow::setInputImage(const QString &imageFilename, const QImage &image)
{
ui->btnSaveEntropyImage->setEnabled(false);
this->inputImageFilename = imageFilename;
this->inputImage = image;
entropyImage = QImage();
showInputImage();
}
void MainWindow::showInputImage()
{
ui->btnShowOriginal->setEnabled(false);
ui->lblImage->setPixmap(QPixmap::fromImage(this->inputImage));
ui->btnShowEntropy->setEnabled(true);
}
////////////////////////////////////////////////////////////////////////////////
void MainWindow::asyncCalculateEntropyImage()
{
ui->wdgButtons->setEnabled(false);
ui->statusbar->showMessage("Calculating entropy...");
QtConcurrent::run( [=]() {
QElapsedTimer elapsedTimer;
elapsedTimer.start();
QImage entropyImage = EntropyFast::calculateEntropyImageFrom( this->inputImage );
emit entropyImageReady( entropyImage, elapsedTimer.nsecsElapsed() );
});
}
void MainWindow::onEntropyImageReady(const QImage &image, qint64 nsecsElapsed)
{
this->entropyImage = image;
showEntropyImage();
ui->statusbar->showMessage(QString("Entropy calculated in %1 milliseconds").arg(static_cast<double>(nsecsElapsed) / 1000000), 5000);
ui->btnSaveEntropyImage->setEnabled(true);
ui->wdgButtons->setEnabled(true);
}
void MainWindow::showEntropyImage()
{
ui->btnShowEntropy->setEnabled(false);
if( entropyImage.isNull() )
asyncCalculateEntropyImage();
else
{
ui->lblImage->setPixmap(QPixmap::fromImage(this->entropyImage));
ui->btnShowOriginal->setEnabled(true);
}
}
////////////////////////////////////////////////////////////////////////////////
QString MainWindow::getSuggestedEntropyImageFilename() const
{
QFileInfo inputFileInfo( inputImageFilename );
return inputFileInfo.path() + QDir::separator() + inputFileInfo.completeBaseName() + "-entropy.jpg";
}
void MainWindow::asyncSaveImageTo(const QImage &image, const QString &imageFilename)
{
ui->wdgButtons->setEnabled(false);
ui->statusbar->showMessage("Saving entropy image to " + imageFilename);
QtConcurrent::run( [=]() {
image.save(imageFilename);
emit imageSaved(imageFilename);
});
}
void MainWindow::onImageSaved(const QString &imageFilename)
{
ui->statusbar->showMessage("Image saved to " + imageFilename, 5000);
ui->wdgButtons->setEnabled(true);
}
////////////////////////////////////////////////////////////////////////////////
void MainWindow::on_btnChooseImage_clicked()
{
QString imageFilename = QFileDialog::getOpenFileName(
this, "Choose an image file", QDir::homePath(),
"Images (*.bmp *.gif *.jpg *.png)");
if( not imageFilename.isEmpty() )
asyncOpenImageFrom(imageFilename);
}
void MainWindow::on_btnShowEntropy_clicked()
{
showEntropyImage();
}
void MainWindow::on_btnShowOriginal_clicked()
{
showInputImage();
}
void MainWindow::on_btnSaveEntropyImage_clicked()
{
QString imageFilename = QFileDialog::getSaveFileName(
this, "Choose a location and new file name",
getSuggestedEntropyImageFilename(),
"Images (*.bmp *.gif *.jpg *.png)");
if( not imageFilename.isEmpty() )
asyncSaveImageTo( entropyImage, imageFilename );
}