-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmainwindow.cpp
1207 lines (1003 loc) · 46.3 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
* Copyright (C) 2015 by Simon Wendsche *
* *
* This file is part of NormalmapGenerator. *
* *
* NormalmapGenerator is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* NormalmapGenerator is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* Sourcecode: https://github.com/Theverat/NormalmapGenerator *
********************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "graphicsscene.h"
#include "aboutdialog.h"
#include "src_generators/normalmapgenerator.h"
#include "src_generators/specularmapgenerator.h"
#include "src_generators/ssaogenerator.h"
#include "src_generators/intensitymap.h"
#include "src_generators/gaussianblur.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QElapsedTimer>
#include <QDesktopServices>
#include <QTreeView>
#include <QGraphicsPixmapItem>
#include <QSettings>
#include <QColorDialog>
#include <QPixmap>
#include <QShortcut>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
lastCalctime_normal(0),
lastCalctime_specular(0),
lastCalctime_displace(0),
lastCalctime_ssao(0),
stopQueue(false)
{
ui->setupUi(this);
supportedImageformats << "*.png" << "*.jpg" << "*.jpeg" << "*.tiff"
<< "*.tif" << "*.ppm" << "*.bmp" << "*.xpm"
<< "*.tga" << "*.gif";
//connect signals of GUI elements with slots of this class
connectSignalSlots();
//hide advanced settings and connect signals/slots to show them
hideAdvancedSettings();
//initialize graphicsview
GraphicsScene *scene = new GraphicsScene();
ui->graphicsView->setScene(scene);
ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
scene->addText("Start by dragging images here.");
ui->graphicsView->setRenderHints(QPainter::HighQualityAntialiasing
| QPainter::SmoothPixmapTransform);
ui->graphicsView->setAcceptDrops(true);
//show default status message
ui->statusBar->showMessage("Drag images into the empty preview window to load them.");
//hide queue progressbar
ui->progressBar_Queue->hide();
// SSAO map generator is not ready yet, remove it from the UI
ui->tabWidget->removeTab(4);
//default UI colors
useCustomUiColors = false;
uiColorMainDefault = QColor("#444");
uiColorTextDefault = QColor("#eee");
uiColorGraphicsViewDefault = QColor(Qt::darkGray);
uiColorMain = uiColorMainDefault;
uiColorText = uiColorTextDefault;
uiColorGraphicsView = uiColorGraphicsViewDefault;
//read last window position and color settings from registry
readSettings();
//set UI colors
setUiColors();
//if the program was opened via "open with" by the OS,
//extract the image paths from the arguments
QStringList args = QCoreApplication::arguments();
if(args.size() > 1) {
QList<QUrl> imageUrls;
for(int i = 1; i < args.size(); i++) {
imageUrls.append(QUrl::fromLocalFile(args[i]));
}
loadMultipleDropped(imageUrls);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loadSingleDropped(QUrl url) {
if(load(url))
addImageToQueue(url);
}
void MainWindow::loadMultipleDropped(QList<QUrl> urls) {
//test if image formats are supported
bool containedInvalidFormat = false;
bool loadedFirstValidImage = false;
for(int i = 0; i < urls.size(); i++) {
QFileInfo fileInfo(QFileInfo(urls.at(i).toLocalFile()));
QString suffix = fileInfo.suffix().toLower();
// The supportedImageformats list is of the form "*.jpg", "*.png", ...
if(supportedImageformats.contains("*." + suffix, Qt::CaseInsensitive)) {
//image format is supported, add to queue
addImageToQueue(urls.at(i));
//if it is the first valid image, load and preview it
if(!loadedFirstValidImage) {
load(urls.at(i));
loadedFirstValidImage = true;
}
}
else if(fileInfo.isDir()) {
loadAllFromDir(urls.at(i));
}
else {
containedInvalidFormat = true;
}
}
if(containedInvalidFormat)
QMessageBox::information(this, "Not All Images Loaded Into Queue",
"Some images had unsupported formats and where not loaded into the queue!");
}
void MainWindow::loadAllFromDir(QUrl url) {
// Load all images in the directory
QDir directory(url.toLocalFile());
QStringList fileList = directory.entryList(supportedImageformats, QDir::Files);
QList<QUrl> urls;
foreach(QString path, fileList) {
QUrl subUrl = QUrl::fromLocalFile(directory.absolutePath() + QDir::separator() + path);
urls.append(subUrl);
}
loadMultipleDropped(urls);
}
//load the image specified in the url
bool MainWindow::load(QUrl url) {
if(!url.isValid()) {
throw "[load] invalid url!";
return false;
}
QFileInfo file(url.toLocalFile());
if(file.isDir()) {
loadAllFromDir(url);
return false;
}
ui->statusBar->showMessage("loading Image: " + url.fileName());
//load the image
input = QImage(url.toLocalFile());
if(input.isNull()) {
QString errorMessage("Image not loaded!");
if(file.suffix().toLower() == "tga") {
errorMessage.append("\nOnly uncompressed TGA files are supported.");
}
else {
errorMessage.append("\nMost likely the image format is not supported.");
}
ui->statusBar->showMessage("Error: Image " + url.fileName() + " NOT loaded!", 5000);
QMessageBox::information(this, "Error while loading image", errorMessage);
return false;
}
//store the path the image was loaded from (for saving later)
if(exportPath.isEmpty())
setExportPath(url.adjusted(QUrl::RemoveFilename));
loadedImagePath = url;
//enable ui buttons
ui->pushButton_calcNormal->setEnabled(true);
ui->pushButton_calcSpec->setEnabled(true);
ui->pushButton_calcDisplace->setEnabled(true);
ui->pushButton_calcSsao->setEnabled(true);
ui->spinBox_normalmapSize->setEnabled(true);
enableAutoupdate(true);
//extract R/G/B/A channels
const int h = ui->label_channelRed->height();
QImage inputSmall(input.scaled(h, h, Qt::KeepAspectRatio));
IntensityMap red(inputSmall, IntensityMap::MAX, true, false, false, false);
IntensityMap green(inputSmall, IntensityMap::MAX, false, true, false, false);
IntensityMap blue(inputSmall, IntensityMap::MAX, false, false, true, false);
IntensityMap alpha(inputSmall, IntensityMap::MAX, false, false, false, true);
ui->label_channelRGBA->setPixmap(QPixmap::fromImage(inputSmall));
ui->label_channelRed->setPixmap(QPixmap::fromImage(red.convertToQImage()));
ui->label_channelGreen->setPixmap(QPixmap::fromImage(green.convertToQImage()));
ui->label_channelBlue->setPixmap(QPixmap::fromImage(blue.convertToQImage()));
ui->label_channelAlpha->setPixmap(QPixmap::fromImage(alpha.convertToQImage()));
//algorithm to find best settings for KeepLargeDetail
int imageSize = std::max(input.width(), input.height());
int largeDetailScale = -0.037 * imageSize + 100;
ui->checkBox_keepLargeDetail->setChecked(true);
if(imageSize < 300) {
ui->checkBox_keepLargeDetail->setChecked(false);
}
else if(imageSize > 2300) {
largeDetailScale = 20;
}
ui->spinBox_largeDetailScale->setValue(largeDetailScale);
//switch active tab to input
ui->tabWidget->setCurrentIndex(0);
//clear all previously generated images
channelIntensity = QImage();
normalmap = QImage();
specmap = QImage();
displacementmap = QImage();
ssaomap = QImage();
//display single image channels if the option was already chosen
if(ui->radioButton_displayRGBA->isChecked())
displayChannelIntensity();
else
preview(0);
//image smaller than graphicsview: fitInView, then resetZoom (this way it is centered)
//image larger than graphicsview: just fitInView
fitInView();
if(input.width() < ui->graphicsView->width() || input.height() < ui->graphicsView->height()) {
resetZoom();
}
ui->statusBar->clearMessage();
//enable button to save the maps
ui->pushButton_save->setEnabled(true);
return true;
}
//load images using the file dialog
void MainWindow::loadUserFilePath() {
QString filter = "Image Formats (" + supportedImageformats.join(" ") + ")";
QList<QUrl> urls = QFileDialog::getOpenFileUrls(this,
"Open Image File",
QDir::homePath(),
filter);
loadMultipleDropped(urls);
}
void MainWindow::calcNormal() {
if(input.isNull())
return;
//normalmap parameters
double strength = ui->doubleSpinBox_strength->value();
bool invert = ui->checkBox_invertHeight->isChecked();
bool tileable = ui->checkBox_tileable->isChecked();
//color channel mode
IntensityMap::Mode mode = IntensityMap::AVERAGE;
if(ui->comboBox_mode_normal->currentIndex() == 0)
mode = IntensityMap::AVERAGE;
else if(ui->comboBox_mode_normal->currentIndex() == 1)
mode = IntensityMap::MAX;
//color channels to use
double redMultiplier = ui->doubleSpinBox_normal_redMul->value();
double greenMultiplier = ui->doubleSpinBox_normal_greenMul->value();
double blueMultiplier = ui->doubleSpinBox_normal_blueMul->value();
double alphaMultiplier = ui->doubleSpinBox_normal_alphaMul->value();
//kernel to use
NormalmapGenerator::Kernel kernel = NormalmapGenerator::SOBEL;
if(ui->comboBox_method->currentIndex() == 0)
kernel = NormalmapGenerator::SOBEL;
else if(ui->comboBox_method->currentIndex() == 1)
kernel = NormalmapGenerator::PREWITT;
//keep large detail settings
bool keepLargeDetail = ui->checkBox_keepLargeDetail->isChecked();
int largeDetailScale = ui->spinBox_largeDetailScale->value();
double largeDetailHeight = ui->doubleSpinBox_largeDetailHeight->value();
//scale input image if not 100%
QImage inputScaled = input;
int sizePercent = ui->spinBox_normalmapSize->value();
if(sizePercent != 100) {
int scaledWidth = calcPercentage(input.width(), sizePercent);
int scaledHeight = calcPercentage(input.height(), sizePercent);
inputScaled = input.scaled(scaledWidth, scaledHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
//setup generator and calculate map
NormalmapGenerator normalmapGenerator(mode, redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier);
normalmap = normalmapGenerator.calculateNormalmap(inputScaled, kernel, strength, invert, tileable, keepLargeDetail, largeDetailScale, largeDetailHeight);
normalmapRawIntensity = normalmapGenerator.getIntensityMap().convertToQImage();
}
void MainWindow::calcSpec() {
if(input.isNull())
return;
//color channel mode
IntensityMap::Mode mode = IntensityMap::AVERAGE;
if(ui->comboBox_mode_spec->currentIndex() == 0)
mode = IntensityMap::AVERAGE;
else if(ui->comboBox_mode_spec->currentIndex() == 1)
mode = IntensityMap::MAX;
//color channel multipliers to use
double redMultiplier = ui->doubleSpinBox_spec_redMul->value();
double greenMultiplier = ui->doubleSpinBox_spec_greenMul->value();
double blueMultiplier = ui->doubleSpinBox_spec_blueMul->value();
double alphaMultiplier = ui->doubleSpinBox_spec_alphaMul->value();
double scale = ui->doubleSpinBox_spec_scale->value();
double contrast = ui->doubleSpinBox_spec_contrast->value();
//setup generator and calculate map
SpecularmapGenerator specularmapGenerator(mode, redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier);
specmap = specularmapGenerator.calculateSpecmap(input, scale, contrast);
}
//the displacement map is generated with the specularmapGenerator (similar controls and output needed)
void MainWindow::calcDisplace() {
if(input.isNull())
return;
//color channel mode
IntensityMap::Mode mode = IntensityMap::AVERAGE;
if(ui->comboBox_mode_displace->currentIndex() == 0)
mode = IntensityMap::AVERAGE;
else if(ui->comboBox_mode_displace->currentIndex() == 1)
mode = IntensityMap::MAX;
//color channel multipliers to use
double redMultiplier = ui->doubleSpinBox_displace_redMul->value();
double greenMultiplier = ui->doubleSpinBox_displace_greenMul->value();
double blueMultiplier = ui->doubleSpinBox_displace_blueMul->value();
double alphaMultiplier = 0.0;
double scale = ui->doubleSpinBox_displace_scale->value();
double contrast = ui->doubleSpinBox_displace_contrast->value();
//setup generator and calculate map
SpecularmapGenerator specularmapGenerator(mode, redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier);
displacementmap = specularmapGenerator.calculateSpecmap(input, scale, contrast);
if(ui->checkBox_displace_blur->isChecked()) {
int radius = ui->spinBox_displace_blurRadius->value();
bool tileable = ui->checkBox_displace_blur_tileable->isChecked();
IntensityMap inputMap(displacementmap, IntensityMap::AVERAGE);
GaussianBlur filter;
IntensityMap outputMap = filter.calculate(inputMap, radius, tileable);
displacementmap = outputMap.convertToQImage();
}
}
void MainWindow::calcSsao() {
if(input.isNull())
return;
//if no normalmap was created yet, calculate it
if(normalmap.isNull()) {
calcNormal();
}
//scale depthmap (can be smaller than normalmap because of KeepLargeDetail
normalmapRawIntensity = normalmapRawIntensity.scaled(normalmap.width(), normalmap.height());
float size = ui->doubleSpinBox_ssao_size->value();
unsigned int samples = ui->spinBox_ssao_samples->value();
unsigned int noiseTexSize = ui->spinBox_ssao_noiseTexSize->value();
//setup generator and calculate map
SsaoGenerator ssaoGenerator;
ssaomap = ssaoGenerator.calculateSsaomap(normalmap, normalmapRawIntensity, size, samples, noiseTexSize);
}
void MainWindow::calcNormalAndPreview() {
ui->statusBar->showMessage("calculating normalmap...");
//timer for measuring calculation time
QElapsedTimer timer;
timer.start();
//calculate map
calcNormal();
//display time it took to calculate the map
this->lastCalctime_normal = timer.elapsed();
displayCalcTime(lastCalctime_normal, "normalmap", 5000);
//preview in normalmap tab
preview(1);
//activate corresponding save checkbox
ui->checkBox_queue_generateNormal->setChecked(true);
}
void MainWindow::calcSpecAndPreview() {
ui->statusBar->showMessage("calculating specularmap...");
//timer for measuring calculation time
QElapsedTimer timer;
timer.start();
//calculate map
calcSpec();
//display time it took to calculate the map
this->lastCalctime_specular = timer.elapsed();
displayCalcTime(lastCalctime_specular, "specularmap", 5000);
//preview in specular map tab
preview(2);
//activate corresponding save checkbox
ui->checkBox_queue_generateSpec->setChecked(true);
}
void MainWindow::calcDisplaceAndPreview() {
ui->statusBar->showMessage("calculating displacementmap...");
//timer for measuring calculation time
QElapsedTimer timer;
timer.start();
//calculate map
calcDisplace();
//display time it took to calculate the map
this->lastCalctime_displace = timer.elapsed();
displayCalcTime(lastCalctime_displace, "displacementmap", 5000);
//preview in displacement map tab
preview(3);
//activate corresponding save checkbox
ui->checkBox_queue_generateDisplace->setChecked(true);
}
void MainWindow::calcSsaoAndPreview() {
ui->statusBar->showMessage("calculating ambient occlusion map...");
//timer for measuring calculation time
QElapsedTimer timer;
timer.start();
//calculate map
calcSsao();
//display time it took to calculate the map
this->lastCalctime_ssao = timer.elapsed();
displayCalcTime(lastCalctime_ssao, "ambient occlusion map", 5000);
//preview in ambient occlusion map tab
preview(4);
//activate corresponding save checkbox
//ui->checkBox_queue_generate ssao ->setChecked(true); //missing until finished
}
void MainWindow::processQueue() {
if(ui->listWidget_queue->count() == 0)
return;
if(!(ui->checkBox_queue_generateNormal->isChecked() ||
ui->checkBox_queue_generateSpec->isChecked() ||
ui->checkBox_queue_generateDisplace->isChecked())) {
QMessageBox::information(this, "Nothing to do", "Select at least one map type to generate from the \"Save\" section");
return;
}
if(!exportPath.isValid()) {
QMessageBox::information(this, "Invalid Export Path", "Export path is invalid!");
return;
}
//enable stop button
ui->pushButton_stopProcessingQueue->setEnabled(true);
//show progress bar and adjust maximum to queue size
ui->progressBar_Queue->show();
ui->progressBar_Queue->setMaximum(ui->listWidget_queue->count());
for(int i = 0; i < ui->listWidget_queue->count() && !stopQueue; i++)
{
QueueItem *item = (QueueItem*)(ui->listWidget_queue->item(i));
//display status
ui->statusBar->showMessage("Processing Image \"" + item->text() + "\"");
ui->progressBar_Queue->setValue(i + 1);
ui->listWidget_queue->item(i)->setSelected(true);
//load image
load(item->getUrl());
//save maps
QUrl exportUrl = QUrl::fromLocalFile(exportPath.toLocalFile() + "/" + item->text());
std::cout << "[Queue] Image " << i + 1 << " exported: "
<< exportUrl.toLocalFile().toStdString() << std::endl;
save(exportUrl);
//user interface should stay responsive
QCoreApplication::processEvents();
}
//disable stop button
ui->pushButton_stopProcessingQueue->setEnabled(false);
stopQueue = false;
//hide queue progress bar
ui->progressBar_Queue->hide();
//enable "Open Export Folder" gui button
ui->pushButton_openExportFolder->setEnabled(true);
}
//tell the queue to stop processing
void MainWindow::stopProcessingQueue() {
stopQueue = true;
}
//save maps using the file dialog
void MainWindow::saveUserFilePath() {
if(input.isNull())
return;
QFileDialog::Options options(QFileDialog::DontConfirmOverwrite);
QUrl url = QFileDialog::getSaveFileUrl(this, "Save as", loadedImagePath,
"Image Formats (*.png *.jpg *.jpeg *.tiff *.ppm *.bmp *.xpm)",
0, options);
if(!url.isValid() || url.toLocalFile().isEmpty())
return;
save(url);
}
void MainWindow::save(QUrl url) {
//if saving process was aborted or input image is empty
if(!url.isValid() || input.isNull())
return;
QString path = url.toLocalFile();
//if no file suffix was chosen, automatically use the PNG format
QFileInfo file(path);
if(!file.baseName().isEmpty() && file.suffix().isEmpty())
path += ".png";
QString suffix = file.suffix();
//Qt can only read tga, saving is not supported
if(suffix.toLower() == "tga")
suffix = "png";
//append a suffix to the map names (result: path/original_normal.png)
QString name_normal = file.absolutePath() + "/" + file.baseName() + "_normal." + suffix;
QString name_specular = file.absolutePath() + "/" + file.baseName() + "_spec." + suffix;
QString name_displace = file.absolutePath() + "/" + file.baseName() + "_displace." + suffix;
bool successfullySaved = true;
if(ui->checkBox_queue_generateNormal->isChecked()) {
if(normalmap.isNull()) {
ui->statusBar->showMessage("calculating normalmap...");
calcNormal();
}
successfullySaved &= normalmap.save(name_normal);
}
if(ui->checkBox_queue_generateSpec->isChecked()) {
if(specmap.isNull()) {
ui->statusBar->showMessage("calculating specularmap...");
calcSpec();
}
successfullySaved &= specmap.save(name_specular);
}
if(ui->checkBox_queue_generateDisplace->isChecked()) {
if(displacementmap.isNull()) {
ui->statusBar->showMessage("calculating displacementmap...");
calcDisplace();
}
successfullySaved &= displacementmap.save(name_displace);
}
if(successfullySaved)
ui->statusBar->showMessage("Maps successfully saved", 4000);
else
QMessageBox::information(this, "Maps not saved", "One or more of the maps was NOT saved!");
//store export path
setExportPath(url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash));
//enable "Open Export Folder" gui button
ui->pushButton_openExportFolder->setEnabled(true);
}
bool MainWindow::setExportPath(QUrl path) {
QDir testDir(path.toLocalFile());
if(testDir.exists() && !path.toLocalFile().isEmpty()) {
std::cout << "set export path to " + path.toLocalFile().toStdString() << std::endl;
this->exportPath = path;
//stop highlighting
ui->lineEdit_outputPath->setStyleSheet("");
ui->lineEdit_outputPath->setText(path.toLocalFile());
return true;
}
else if(path.toLocalFile().isEmpty()) {
//keep previous highlighting
return false;
}
else {
//hightlight wrong path in red
ui->lineEdit_outputPath->setStyleSheet("color: red;");
return false;
}
}
//change the path the queue exports the maps to
void MainWindow::changeOutputPathQueueDialog() {
QUrl startUrl = QDir::homePath();
if(exportPath.isValid())
startUrl = exportPath;
QUrl path = QFileDialog::getExistingDirectoryUrl(this,
"Choose Export Folder",
startUrl);
setExportPath(path);
}
void MainWindow::editOutputPathQueue() {
QString rawPath = ui->lineEdit_outputPath->text();
setExportPath(QUrl::fromLocalFile(rawPath));
}
//overloaded version of preview that chooses the map to preview automatically
void MainWindow::preview() {
preview(ui->tabWidget->currentIndex());
}
//preview the map in the selected tab
void MainWindow::preview(int tab) {
ui->graphicsView->scene()->clear();
switch(tab) {
case 0:
{
//input
if(!input.isNull()) {
if(ui->radioButton_displayRGBA->isChecked()) {
QGraphicsPixmapItem *pixmap = ui->graphicsView->scene()->addPixmap(QPixmap::fromImage(input));
pixmap->setTransformationMode(Qt::SmoothTransformation);
}
else {
QGraphicsPixmapItem *pixmap = ui->graphicsView->scene()->addPixmap(QPixmap::fromImage(channelIntensity));
pixmap->setTransformationMode(Qt::SmoothTransformation);
}
}
break;
}
case 1:
{
//normal
if(!input.isNull() && normalmap.isNull()) {
//if an image was loaded and a normalmap was not yet generated and the image is not too large
//automatically generate the normalmap
calcNormalAndPreview();
}
QGraphicsPixmapItem *pixmap = ui->graphicsView->scene()->addPixmap(QPixmap::fromImage(normalmap));
pixmap->setTransformationMode(Qt::SmoothTransformation);
//display size of the image
normalmapSizeChanged();
break;
}
case 2:
{
//spec
if(!input.isNull() && specmap.isNull()) {
//if an image was loaded and a specmap was not yet generated and the image is not too large
//automatically generate the specmap
calcSpecAndPreview();
}
QGraphicsPixmapItem *pixmap = ui->graphicsView->scene()->addPixmap(QPixmap::fromImage(specmap));
pixmap->setTransformationMode(Qt::SmoothTransformation);
break;
}
case 3:
{
//displacement
if(!input.isNull() && displacementmap.isNull()) {
//if an image was loaded and a dispmap was not yet generated and the image is not too large
//automatically generate the displacementmap
calcDisplaceAndPreview();
}
QGraphicsPixmapItem *pixmap = ui->graphicsView->scene()->addPixmap(QPixmap::fromImage(displacementmap));
pixmap->setTransformationMode(Qt::SmoothTransformation);
break;
}
case 4:
{
//ambient occlusion
if(!input.isNull() && ssaomap.isNull()) {
//if an image was loaded and a ssaomap was not yet generated and the image is not too large
//automatically generate the ambient occlusion map
calcSsaoAndPreview();
}
QGraphicsPixmapItem *pixmap = ui->graphicsView->scene()->addPixmap(QPixmap::fromImage(ssaomap));
pixmap->setTransformationMode(Qt::SmoothTransformation);
break;
}
}
}
void MainWindow::zoomIn() {
ui->graphicsView->scale(1.2, 1.2);
}
void MainWindow::zoomOut() {
ui->graphicsView->scale(0.8, 0.8);
}
//resets zoom to 1:1
void MainWindow::resetZoom() {
ui->graphicsView->resetTransform();
}
//fits the preview into the graphicsView
void MainWindow::fitInView() {
ui->graphicsView->scene()->setSceneRect(QRectF(0, 0, input.width(), input.height()));
ui->graphicsView->setSceneRect(ui->graphicsView->scene()->sceneRect());
ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
}
//displays single color channels of the image (handled by an intensitymap)
void MainWindow::displayChannelIntensity() {
if(input.isNull())
return;
IntensityMap temp;
if(ui->radioButton_displayRed->isChecked())
temp = IntensityMap(input, IntensityMap::AVERAGE, true, false, false, false);
else if(ui->radioButton_displayGreen->isChecked())
temp = IntensityMap(input, IntensityMap::AVERAGE, false, true, false, false);
else if(ui->radioButton_displayBlue->isChecked())
temp = IntensityMap(input, IntensityMap::AVERAGE, false, false, true, false);
else
temp = IntensityMap(input, IntensityMap::AVERAGE, false, false, false, true);
this->channelIntensity = temp.convertToQImage();
preview(0);
}
//automatically update the preview if the calculation took only a certain amount of time
//in milliseconds, e.g. 500 (0.5 seconds)
//this Slot is for parameter input fields/buttons in the gui
void MainWindow::autoUpdate() {
if(!ui->checkBox_autoUpdate->isChecked() || !ui->checkBox_autoUpdate->isEnabled())
return;
int autoUpdateThreshold_ms = ui->doubleSpinBox_autoUpdateThreshold->value() * 1000.0;
switch(ui->tabWidget->currentIndex()) {
case 0:
break;
case 1:
if(lastCalctime_normal < autoUpdateThreshold_ms)
calcNormalAndPreview();
break;
case 2:
if(lastCalctime_specular < autoUpdateThreshold_ms)
calcSpecAndPreview();
break;
case 3:
if(lastCalctime_displace < autoUpdateThreshold_ms)
calcDisplaceAndPreview();
break;
case 4:
if(lastCalctime_ssao < autoUpdateThreshold_ms)
calcSsaoAndPreview();
break;
default:
break;
}
}
//generate a message that shows the elapsed time of a calculation process
//example output: "calculated normalmap (1.542 seconds)"
QString MainWindow::generateElapsedTimeMsg(int calcTimeMs, QString mapType) {
double calcTimeS = (double)calcTimeMs / 1000.0;
QString elapsedTimeMsg("calculated ");
elapsedTimeMsg.append(mapType);
elapsedTimeMsg.append(" (");
elapsedTimeMsg.append(QString::number(calcTimeS));
elapsedTimeMsg.append(" seconds)");
return elapsedTimeMsg;
}
void MainWindow::openExportFolder() {
QDesktopServices::openUrl(exportPath);
}
//display the last calculation time in the statusbar
void MainWindow::displayCalcTime(int calcTime_ms, QString mapType, int duration_ms) {
std::cout << mapType.toStdString() << " for image " << loadedImagePath.fileName().toStdString()
<< " calculated (" << calcTime_ms << "ms)" << std::endl;
ui->statusBar->clearMessage();
QString msg = generateElapsedTimeMsg(calcTime_ms, mapType);
ui->statusBar->showMessage(msg, duration_ms);
ui->label_autoUpdate_lastCalcTime->setText("(Last Calc. Time: " + QString::number((double)calcTime_ms / 1000.0) + "s)");
if(calcTime_ms < ui->doubleSpinBox_autoUpdateThreshold->value() * 1000) {
//calcTime was below the threshold, set textcolor to green
ui->label_autoUpdate_lastCalcTime->setStyleSheet("QLabel {color: #00AA00;}");
}
else {
//calcTime was above threshold, set textcolor to red to signal user the time was too long for autoupdate
ui->label_autoUpdate_lastCalcTime->setStyleSheet("QLabel {color: red;}");
}
}
void MainWindow::enableAutoupdate(bool on) {
ui->checkBox_autoUpdate->setEnabled(on);
ui->label_autoUpdate_lastCalcTime->setEnabled(on);
ui->label_autoUpdate_text->setEnabled(on);
ui->doubleSpinBox_autoUpdateThreshold->setEnabled(on);
}
//add single image to queue
void MainWindow::addImageToQueue(QUrl url) {
QueueItem *item = new QueueItem(url, url.fileName(), ui->listWidget_queue, 0);
QIcon icon(QPixmap(url.toLocalFile()).scaled(64, 64, Qt::KeepAspectRatio));
item->setIcon(icon);
ui->listWidget_queue->addItem(item);
}
//add multiple images to queue
void MainWindow::addImageToQueue(QList<QUrl> urls) {
for(int i = 0; i < urls.size(); i++) {
addImageToQueue(urls.at(i));
}
}
void MainWindow::removeImagesFromQueue() {
qDeleteAll(ui->listWidget_queue->selectedItems());
}
void MainWindow::queueItemDoubleClicked(QListWidgetItem* item) {
//load image that was doubleclicked
load(((QueueItem*)item)->getUrl());
}
//calculates the size preview text (e.g. "1024 x 1024 px")
void MainWindow::normalmapSizeChanged() {
int sizePercent = ui->spinBox_normalmapSize->value();
QString text = QString::number(calcPercentage(input.width(), sizePercent));
text.append(" x ");
text.append(QString::number(calcPercentage(input.height(), sizePercent)));
text.append(" px");
ui->label_normalmapSize->setText(text);
}
// Used to scale down the input image if a value less than 100
// is set in spinBox_normalmapSize. We have to make sure size is at least 1
int MainWindow::calcPercentage(int value, int percentage) {
const int newValue = (((double)value / 100.0) * percentage);
return std::max(newValue, 1);
}
void MainWindow::showAboutDialog() {
AboutDialog *dialog = new AboutDialog(this, this);
dialog->show();
}
//connects gui buttons with Slots in this class
void MainWindow::connectSignalSlots() {
//connect signals/slots
//load/save/open export folder
connect(ui->pushButton_load, SIGNAL(clicked()), this, SLOT(loadUserFilePath()));
connect(ui->pushButton_save, SIGNAL(clicked()), this, SLOT(saveUserFilePath()));
connect(ui->pushButton_openExportFolder, SIGNAL(clicked()), this, SLOT(openExportFolder()));
//zoom
connect(ui->pushButton_zoomIn, SIGNAL(clicked()), this, SLOT(zoomIn()));
connect(ui->pushButton_zoomOut, SIGNAL(clicked()), this, SLOT(zoomOut()));
connect(ui->pushButton_resetZoom, SIGNAL(clicked()), this, SLOT(resetZoom()));
connect(ui->pushButton_fitInView, SIGNAL(clicked()), this, SLOT(fitInView()));
//calculate
connect(ui->pushButton_calcNormal, SIGNAL(clicked()), this, SLOT(calcNormalAndPreview()));
connect(ui->pushButton_calcSpec, SIGNAL(clicked()), this, SLOT(calcSpecAndPreview()));
connect(ui->pushButton_calcDisplace, SIGNAL(clicked()), this, SLOT(calcDisplaceAndPreview()));
connect(ui->pushButton_calcSsao, SIGNAL(clicked()), this, SLOT(calcSsaoAndPreview()));
//switch between tabs
connect(ui->tabWidget, SIGNAL(tabBarClicked(int)), this, SLOT(preview(int)));
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(preview(int)));
//display channel intensity
//radio buttons
connect(ui->radioButton_displayRGBA, SIGNAL(clicked()), this, SLOT(displayChannelIntensity()));
connect(ui->radioButton_displayRed, SIGNAL(clicked()), this, SLOT(displayChannelIntensity()));
connect(ui->radioButton_displayGreen, SIGNAL(clicked()), this, SLOT(displayChannelIntensity()));
connect(ui->radioButton_displayBlue, SIGNAL(clicked()), this, SLOT(displayChannelIntensity()));
connect(ui->radioButton_displayAlpha, SIGNAL(clicked()), this, SLOT(displayChannelIntensity()));
//labels (channel images)
connect(ui->label_channelRGBA, SIGNAL(clicked()), ui->radioButton_displayRGBA, SLOT(click()));
connect(ui->label_channelRed, SIGNAL(clicked()), ui->radioButton_displayRed, SLOT(click()));
connect(ui->label_channelGreen, SIGNAL(clicked()), ui->radioButton_displayGreen, SLOT(click()));
connect(ui->label_channelBlue, SIGNAL(clicked()), ui->radioButton_displayBlue, SLOT(click()));
connect(ui->label_channelAlpha, SIGNAL(clicked()), ui->radioButton_displayAlpha, SLOT(click()));
//autoupdate after changed values
// spec autoupdate
connect(ui->doubleSpinBox_spec_redMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_spec_greenMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_spec_blueMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_spec_alphaMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_spec_scale, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->comboBox_mode_spec, SIGNAL(currentIndexChanged(int)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_spec_contrast, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
// normal autoupdate
connect(ui->doubleSpinBox_normal_redMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_normal_greenMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_normal_blueMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_normal_alphaMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->comboBox_mode_normal, SIGNAL(currentIndexChanged(int)), this, SLOT(autoUpdate()));
connect(ui->comboBox_method, SIGNAL(currentIndexChanged(int)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_strength, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->checkBox_tileable, SIGNAL(clicked()), this, SLOT(autoUpdate()));
connect(ui->checkBox_invertHeight, SIGNAL(clicked()), this, SLOT(autoUpdate()));
connect(ui->spinBox_normalmapSize, SIGNAL(valueChanged(int)), this, SLOT(autoUpdate()));
connect(ui->checkBox_keepLargeDetail, SIGNAL(clicked()), this, SLOT(autoUpdate()));
connect(ui->spinBox_largeDetailScale, SIGNAL(valueChanged(int)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_largeDetailHeight, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
// displacement autoupdate
connect(ui->doubleSpinBox_displace_redMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_displace_greenMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_displace_blueMul, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_displace_scale, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->comboBox_mode_displace, SIGNAL(currentIndexChanged(int)), this, SLOT(autoUpdate()));
connect(ui->doubleSpinBox_displace_contrast, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
connect(ui->checkBox_displace_blur, SIGNAL(stateChanged(int)), this, SLOT(autoUpdate()));
connect(ui->checkBox_displace_blur_tileable, SIGNAL(stateChanged(int)), this, SLOT(autoUpdate()));
connect(ui->spinBox_displace_blurRadius, SIGNAL(valueChanged(int)), this, SLOT(autoUpdate()));
// ssao autoupdate
connect(ui->doubleSpinBox_ssao_size, SIGNAL(valueChanged(double)), this, SLOT(autoUpdate()));
//graphicsview drag and drop
connect(ui->graphicsView, SIGNAL(singleImageDropped(QUrl)), this, SLOT(loadSingleDropped(QUrl)));
connect(ui->graphicsView, SIGNAL(multipleImagesDropped(QList<QUrl>)), this, SLOT(loadMultipleDropped(QList<QUrl>)));
//graphicsview rightclick/middleclick/zoom
connect(ui->graphicsView, SIGNAL(rightClick()), this, SLOT(resetZoom()));
connect(ui->graphicsView, SIGNAL(middleClick()), this, SLOT(fitInView()));
connect(ui->graphicsView, SIGNAL(zoomIn()), this, SLOT(zoomIn()));
connect(ui->graphicsView, SIGNAL(zoomOut()), this, SLOT(zoomOut()));
//queue (item widget)
connect(ui->pushButton_removeImagesFromQueue, SIGNAL(clicked()), this, SLOT(removeImagesFromQueue()));
connect(ui->pushButton_processQueue, SIGNAL(clicked()), this, SLOT(processQueue()));
connect(ui->pushButton_stopProcessingQueue, SIGNAL(clicked()), this, SLOT(stopProcessingQueue()));
connect(ui->pushButton_changeOutputPath_Queue, SIGNAL(clicked()), this, SLOT(changeOutputPathQueueDialog()));
connect(ui->lineEdit_outputPath, SIGNAL(editingFinished()), this, SLOT(editOutputPathQueue()));
connect(ui->listWidget_queue, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(queueItemDoubleClicked(QListWidgetItem*)));
//queue drag and drop