-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
354 lines (315 loc) · 10 KB
/
mainwindow.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "imageprocessor.h"
#include "matrixmisc.h"
#include <QFileDialog>
#include <QThread>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
filter(3, 3, 0.0)
{
ui->setupUi(this);
emit this->writeLogToViewer(tr("Logging goes here."), LogLevel::Info);
unsigned concurentThreadsSupported = std::thread::hardware_concurrency();
if(concurentThreadsSupported == 0)
{
concurentThreadsSupported = 2;
emit this->writeLogToViewer(tr("Cannot detect avalible thread cound. Using 2."), LogLevel::Warning);
}
else
emit this->writeLogToViewer(tr("Setting threads to ") +
QString::number(concurentThreadsSupported) +
tr("."), LogLevel::Info);
ui->thread_spinBox->setValue(int(concurentThreadsSupported));
updateMatrix();
updateTable();
QObject::connect(&imgWorker, SIGNAL(finished()), this, SLOT(processingFinished()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::processingFinished()
{
ui->doit_button->setEnabled(true);
QImage img = imgWorker.getProcessedImage();
if(!img.save(targetPath, "PNG"))
{
emit this->writeLogToViewer(tr("Cannot save image."), LogLevel::Error);
return;
}
emit this->writeLogToViewer(tr("Done."), LogLevel::Info);
}
void MainWindow::on_actionExit_triggered()
{
QApplication::exit(0);
}
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open image"), "",
tr("Image (*.bmp *.jpg *.jpeg *.tiff *.png);;All Files (*)"));
if(fileName.length() == 0)
{
emit writeLogToViewer("Target image was not set.", LogLevel::Info);
return;
}
imagePath = fileName;
emit writeLogToViewer("Target image set.", LogLevel::Info);
}
void MainWindow::writeLogToViewer(const QString &text, const LogLevel level)
{
QString logLevelText = "";
switch(level)
{
case LogLevel::Critical:
logLevelText = "<b>CRITICAL</b>";
break;
case LogLevel::Error:
logLevelText = "<b>Error</b>";
break;
case LogLevel::Info:
logLevelText = "<b>Info</b>";
break;
case LogLevel::Warning:
logLevelText = "<b>Warning</b>";
break;
}
ui->logview->append(logLevelText + tr(": ") + text);
}
void MainWindow::on_actionOpen_matrix_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open matrix"), "",
tr("Text file (*.txt);;All Files (*)"));
if(fileName.length() == 0)
return;
try
{
filter = MatAlg::loadMatrix<double>(fileName.toUtf8().constData());
}
catch (const std::logic_error &e)
{
emit writeLogToViewer(tr("Error while loading matrix: ") + tr(e.what()), LogLevel::Error);
return;
}
uint filterRows = filter.getRowSize();
uint filterColumn = filter.getColumnSize();
ui->column_spinBox->setValue(int(filterColumn));
ui->row_spinBox->setValue(int(filterRows));
ui->preset_comboBox->setCurrentIndex(6);
updateTable();
emit writeLogToViewer("Loaded matrix successfully.", LogLevel::Info);
}
void MainWindow::on_actionSave_matrix_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save matrix"), "",
tr("Text file (*.txt);;All Files (*)"));
if(fileName.length() == 0)
{
emit writeLogToViewer("Matrix was not saved.", LogLevel::Info);
return;
}
updateMatrix();
MatAlg::saveMatrix(fileName.toUtf8().constData(), filter);
emit writeLogToViewer("Saved matrix successfully.", LogLevel::Info);
}
void MainWindow::updateTable()
{
const int columnCount = ui->column_spinBox->value();
const int rowCount = ui->row_spinBox->value();
ui->matrix_table->clear();
ui->matrix_table->setColumnCount(columnCount);
ui->matrix_table->setRowCount(rowCount);
for(int x = 0; x < rowCount; x++)
{
for(int y = 0; y < columnCount; y++)
{
double value = filter[uint(x)][uint(y)];
ui->matrix_table->setItem(x, y, new QTableWidgetItem(QString::number(value)));
}
}
}
void MainWindow::updateMatrix()
{
int columnCount = ui->column_spinBox->value();
int rowCount = ui->row_spinBox->value();
QString currentPreset = ui->preset_comboBox->currentText();
if(currentPreset.compare("Gauss blur") == 0)
{
filter = ConvMatFactory::gaussBlurMatrix(uint(columnCount), uint(rowCount));
return;
}
if(currentPreset.compare("Box blur") == 0)
{
filter = ConvMatFactory::blurMatrix(uint(columnCount), uint(rowCount));
return;
}
if(currentPreset.compare("Edge enhance") == 0)
{
filter = ConvMatFactory::edgeEnhanceMatrix();
return;
}
if(currentPreset.compare("Sharpen") == 0)
{
filter = ConvMatFactory::sharpenMatrix();
return;
}
if(currentPreset.compare("Edge detect") == 0)
{
filter = ConvMatFactory::edgeDetectMatrix();
return;
}
if(currentPreset.compare("Emboss") == 0)
{
filter = ConvMatFactory::embossMatrix();
return;
}
if(currentPreset.compare("Custom") == 0)
{
int rows = ui->row_spinBox->value();
int columns = ui->column_spinBox->value();
filter = MatAlg::Matrix<double>(uint(rows), uint(columns), 0.0);
for(int x = 0; x < ui->matrix_table->rowCount(); x++)
{
for(int y = 0; y < ui->matrix_table->columnCount(); y++)
{
bool ok = false;
QString data = ui->matrix_table->item(x, y)->text();
double value = data.toDouble(&ok);
if(!ok)
{
emit writeLogToViewer(tr("Error parsing data from table: ") + data);
return;
}
filter[uint(x)][uint(y)] = value;
}
}
}
}
void MainWindow::on_preset_comboBox_currentIndexChanged(const QString &arg1)
{
constexpr int maximumValue = 100500;
constexpr int minimumValue = 1;
if(arg1.compare("Gauss blur") == 0)
{
ui->column_spinBox->setMaximum(maximumValue);
ui->column_spinBox->setMinimum(minimumValue);
ui->row_spinBox->setMaximum(maximumValue);
ui->row_spinBox->setMinimum(minimumValue);
return;
}
if(arg1.compare("Box blur") == 0)
{
ui->column_spinBox->setMaximum(maximumValue);
ui->column_spinBox->setMinimum(minimumValue);
ui->row_spinBox->setMaximum(maximumValue);
ui->row_spinBox->setMinimum(minimumValue);
return;
}
if(arg1.compare("Edge enhance") == 0)
{
ui->column_spinBox->setValue(3);
ui->column_spinBox->setMaximum(3);
ui->column_spinBox->setMinimum(3);
ui->row_spinBox->setValue(3);
ui->row_spinBox->setMaximum(3);
ui->row_spinBox->setMinimum(3);
return;
}
if(arg1.compare("Sharpen") == 0)
{
ui->column_spinBox->setValue(3);
ui->column_spinBox->setMaximum(3);
ui->column_spinBox->setMinimum(3);
ui->row_spinBox->setValue(3);
ui->row_spinBox->setMaximum(3);
ui->row_spinBox->setMinimum(3);
return;
}
if(arg1.compare("Edge detect") == 0)
{
ui->column_spinBox->setValue(3);
ui->column_spinBox->setMaximum(3);
ui->column_spinBox->setMinimum(3);
ui->row_spinBox->setValue(3);
ui->row_spinBox->setMaximum(3);
ui->row_spinBox->setMinimum(3);
return;
}
if(arg1.compare("Emboss") == 0)
{
ui->column_spinBox->setValue(3);
ui->column_spinBox->setMaximum(3);
ui->column_spinBox->setMinimum(3);
ui->row_spinBox->setValue(3);
ui->row_spinBox->setMaximum(3);
ui->row_spinBox->setMinimum(3);
return;
}
if(arg1.compare("Custom") == 0)
{
ui->column_spinBox->setMaximum(maximumValue);
ui->column_spinBox->setMinimum(minimumValue);
ui->row_spinBox->setMaximum(maximumValue);
ui->row_spinBox->setMinimum(minimumValue);
return;
}
}
void MainWindow::on_genMatrix_button_clicked()
{
updateMatrix();
updateTable();
ui->tabWidget->setCurrentWidget(ui->tab_2);
}
void MainWindow::on_doit_button_clicked()
{
updateMatrix();
updateTable();
if(imagePath.length() == 0)
{
emit this->writeLogToViewer(tr("Target image is not set."), LogLevel::Info);
return;
}
ui->doit_button->setEnabled(false);
emit this->writeLogToViewer(tr("Processing image..."), LogLevel::Info);
double divisor = ui->divisor_spinBox->value();
if(divisor == 0.0)
{
divisor = MatAlg::elementSum(filter);
}
if(divisor == 0.0)
{
divisor = 1.0;
}
double offset = ui->offset_spinBox->value();
QImage img;
if(!img.load(imagePath))
{
emit this->writeLogToViewer(tr("Cannot open image."), LogLevel::Error);
return;
}
int threadNumber = ui->thread_spinBox->value();
int passes = ui->passes_spinBox->value();
imgWorker.setup(filter, offset, divisor, img, threadNumber, passes);
imgWorker.start();
}
void MainWindow::on_actionSave_image_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save image"), "",
tr("PNG image (*.png)"));
if(fileName.length() == 0)
{
emit writeLogToViewer("Save target image not set.", LogLevel::Info);
return;
}
targetPath = fileName;
emit writeLogToViewer("Save target image set.", LogLevel::Info);
}
void MainWindow::on_pushButton_clicked()
{
ui->logview->clear();
}