-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
1071 lines (849 loc) · 29.1 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
// ===============================================================
// Computer Graphics Homework Solutions
// Copyright (C) 2017 by George Wolberg
//
// MainWindow.cpp - MainWindow class
//
// Written by: George Wolberg, 2017
// ===============================================================
#include "MainWindow.h"
#include "Dummy.h"
#include "Threshold.h"
#include "Clip.h"
#include "Quantize.h"
#include "Gamma.h"
#include "Contrast.h"
#include "HistoStretch.h"
#include "HistoMatch.h"
#include "ErrDiffusion.h"
#include "Blur.h"
#include "Sharpen.h"
#include "Median.h"
#include "Convolve.h"
#include "Spectrum.h"
#include "Swap.h"
using namespace IP;
enum {RGB, R, G, B, GRAY};
QString GroupBoxStyle = "QGroupBox { \
border: 2px solid gray; \
border-radius: 9px; \
font-weight: bold; \
margin-top: 0.5em;} \
QGroupBox::title { \
subcontrol-origin: margin; \
left: 10px; \
padding: 0 3px 0 3px; \
}";
QString FrameStyle = "QFrame { \
border: 2px solid gray; \
border-radius: 9px; \
}";
MainWindow *g_mainWindowP = NULL;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::MainWindow:
//
// MainWindow constructor.
//
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_code(-1),
m_histoColor(GRAY),
m_flagGPU(false)
{
setWindowTitle("Image Processing");
// set global variable for main window pointer
g_mainWindowP = this;
// assemble user interface
createActions(); // insert your actions here
createMenus (); // insert your menus here
createWidgets();
#ifdef __APPLE__
m_currentDir = qApp->applicationDirPath();
QDir dir(m_currentDir);
dir.cdUp();
dir.cdUp();
dir.cdUp();
m_currentDir = dir.path();
#endif
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createActions:
//
// Create actions to associate with menu and toolbar selection.
//
void
MainWindow::createActions()
{
//////////////////////////////
// File Actions
//////////////////////////////
m_actionOpen = new QAction("&Open", this);
m_actionOpen->setShortcut(tr("Ctrl+O"));
connect(m_actionOpen, SIGNAL(triggered()), this, SLOT(open()));
m_actionSave = new QAction("&Save", this);
m_actionSave->setShortcut(tr("Ctrl+S"));
connect(m_actionSave, SIGNAL(triggered()), this, SLOT(save()));
m_actionQuit = new QAction("&Quit", this);
m_actionQuit->setShortcut(tr("Ctrl+Q"));
connect(m_actionQuit, SIGNAL(triggered()), this, SLOT(close()));
//////////////////////////////
// Point Ops Actions
//////////////////////////////
m_actionThreshold = new QAction("&Threshold", this);
m_actionThreshold->setShortcut(tr("Ctrl+T"));
m_actionThreshold->setData(THRESHOLD);
m_actionClip = new QAction("&Clip", this);
m_actionClip->setShortcut(tr("Ctrl+C"));
m_actionClip->setData(CLIP);
m_actionQuantize = new QAction("Quant&ization", this);
m_actionQuantize->setShortcut(tr("Ctrl+I"));
m_actionQuantize->setData(QUANTIZE);
m_actionGamma = new QAction("&Gamma Correction", this);
m_actionGamma->setShortcut(tr("Ctrl+G"));
m_actionGamma->setData(GAMMA);
m_actionContrast = new QAction("Co&ntrast Enhancement", this);
m_actionContrast->setShortcut(tr("Ctrl+N"));
m_actionContrast->setData(CONTRAST);
m_actionHistoStretch = new QAction("&Histogram Stretch", this);
m_actionHistoStretch->setShortcut(tr("Ctrl+H"));
m_actionHistoStretch->setData(HISTOSTRETCH);
m_actionHistoMatch = new QAction("Histogram &Match", this);
m_actionHistoMatch->setShortcut(tr("Ctrl+M"));
m_actionHistoMatch->setData(HISTOMATCH);
//////////////////////////////
// Neighborhood Ops Actions
//////////////////////////////
m_actionErrDiffusion = new QAction("&Error Diffusion", this);
m_actionErrDiffusion->setShortcut(tr("Ctrl+E"));
m_actionErrDiffusion->setData(ERRDIFFUSION);
m_actionBlur = new QAction("&Blur", this);
m_actionBlur->setShortcut(tr("Ctrl+B"));
m_actionBlur->setData(BLUR);
m_actionSharpen = new QAction("&Sharpen", this);
m_actionSharpen->setShortcut(tr("Ctrl+S"));
m_actionSharpen->setData(SHARPEN);
m_actionMedian = new QAction("Me&dian", this);
m_actionMedian->setShortcut(tr("Ctrl+D"));
m_actionMedian->setData(MEDIAN);
m_actionConvolve = new QAction("Con&volve", this);
m_actionConvolve->setShortcut(tr("Ctrl+V"));
m_actionConvolve->setData(CONVOLVE);
//////////////////////////////
// FFT Ops Actions
//////////////////////////////
m_actionSpectrum = new QAction("S&pectrum", this);
m_actionSpectrum->setShortcut(tr("Ctrl+P"));
m_actionSpectrum->setData(SPECTRUM);
m_actionSwap = new QAction("S&wap", this);
m_actionSwap->setShortcut(tr("Ctrl+W"));
m_actionSwap->setData(SWAP);
m_actionFilter = new QAction("&Filter", this);
m_actionFilter->setShortcut(tr("Ctrl+F"));
m_actionFilter->setData(FILTER);
// one signal-slot connection for all actions;
// execute() will resolve which action was triggered
connect(menuBar(), SIGNAL(triggered(QAction*)), this, SLOT(execute(QAction*)));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createMenus:
//
// Create menus and install in menubar.
//
void
MainWindow::createMenus()
{
// File menu
m_menuFile = menuBar()->addMenu("&File");
m_menuFile->addAction(m_actionOpen);
m_menuFile->addAction(m_actionSave);
m_menuFile->addAction(m_actionQuit);
// Point Ops menu
m_menuPtOps = menuBar()->addMenu("&Point Ops");
m_menuPtOps->addAction(m_actionThreshold );
m_menuPtOps->addAction(m_actionClip );
m_menuPtOps->addAction(m_actionQuantize );
m_menuPtOps->addAction(m_actionGamma );
m_menuPtOps->addAction(m_actionContrast );
m_menuPtOps->addAction(m_actionHistoStretch);
m_menuPtOps->addAction(m_actionHistoMatch );
// Neighborhood Ops menu
m_menuNbrOps = menuBar()->addMenu("&Neighborhood Ops");
m_menuNbrOps->addAction(m_actionErrDiffusion);
m_menuNbrOps->addAction(m_actionBlur );
m_menuNbrOps->addAction(m_actionSharpen );
m_menuNbrOps->addAction(m_actionMedian );
m_menuNbrOps->addAction(m_actionConvolve );
// Neighborhood Ops menu
m_menuFFT = menuBar()->addMenu("&Fourier Transform");
m_menuFFT->addAction(m_actionSpectrum);
m_menuFFT->addAction(m_actionSwap);
// disable the following menus until input image is read
m_menuPtOps ->setEnabled(false);
m_menuNbrOps->setEnabled(false);
m_menuFFT ->setEnabled(false);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createWidgets:
//
// Create widgets for image display and filter control panels.
//
void
MainWindow::createWidgets()
{
// assemble image display widget and control panel in horizontal layout
QHBoxLayout *hbox = new QHBoxLayout;
m_glwFrame = createGroupDisplay();
hbox->addWidget(m_glwFrame);
hbox->addWidget(createGroupPanel ());
hbox->setStretch(0, 1);
// create container widget and set its layout
QWidget *w = new QWidget;
w->setLayout(hbox);
// set central widget so that it can be displayed
setCentralWidget(w);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createGroupPanel:
//
// Create group box for control panel.
//
QGroupBox*
MainWindow::createGroupPanel()
{
// init group box
m_groupBoxPanels = new QGroupBox("Control Panel");
m_groupBoxPanels->setStyleSheet(GroupBoxStyle);
m_groupBoxPanels->setMinimumWidth(400);
// filter's enum indexes into container of image filters
m_imageFilter[DUMMY ] = new Dummy;
m_imageFilter[THRESHOLD ] = new Threshold;
m_imageFilter[CLIP ] = new Clip;
m_imageFilter[QUANTIZE ] = new Quantize;
m_imageFilter[GAMMA ] = new Gamma;
m_imageFilter[CONTRAST ] = new Contrast;
m_imageFilter[HISTOSTRETCH]=new HistoStretch;
m_imageFilter[HISTOMATCH] = new HistoMatch;
m_imageFilter[ERRDIFFUSION]=new ErrDiffusion;
m_imageFilter[BLUR ] = new Blur;
m_imageFilter[SHARPEN ] = new Sharpen;
m_imageFilter[MEDIAN ] = new Median;
m_imageFilter[CONVOLVE ] = new Convolve;
m_imageFilter[SPECTRUM ] = new Spectrum;
m_imageFilter[SWAP ] = new Swap;
// create a stacked widget to hold multiple control panels
m_stackWidgetPanels = new QStackedWidget;
// add filter control panels to stacked widget
m_stackWidgetPanels->addWidget(m_imageFilter[DUMMY ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[THRESHOLD ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[CLIP ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[QUANTIZE ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[GAMMA ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[CONTRAST ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[HISTOSTRETCH]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[HISTOMATCH ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[ERRDIFFUSION]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[BLUR ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[SHARPEN ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[MEDIAN ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[CONVOLVE ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[SPECTRUM ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilter[SWAP ]->controlPanel());
// display blank dummmy panel initially
m_stackWidgetPanels->setCurrentIndex(DUMMY);
// assemble display and mode groups into horizontal layout
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(createDisplayButtons());
hbox->addWidget(createModeButtons ());
hbox->addWidget(createOptionButtons());
// init timing label
m_labelTime = new QLabel;
QFont font;
font.setPointSize(10);
font.setBold(true);
m_labelTime->setFont(font);
m_labelTime->setText(QString("Execution time (ms):"));
// create histogram plot
m_histogram = new QCustomPlot;
// set histogram title
m_histogram->plotLayout()->insertRow(0);
m_histogram->plotLayout()->addElement(0, 0, new QCPPlotTitle(m_histogram,"Histogram"));
// assign label axes
m_histogram->xAxis->setLabel("Intensity");
m_histogram->yAxis->setLabel("Frequency");
m_histogram->xAxis->setAutoTickStep(0);
m_histogram->xAxis->setTickStep(32.);
// set axes ranges, so we see all the data
m_histogram->xAxis->setRange(0, MXGRAY);
m_histogram->yAxis->setRange(0, MXGRAY);
QRect desktopGeometry = QApplication::desktop()->availableGeometry();
int h = desktopGeometry.height();
int min_h = (h/1080.0f)*400;
min_h = CLIP(min_h, 300, 400);
m_histogram->setMinimumHeight(min_h);
m_histogram->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
m_histogram->axisRect()->setupFullAxesBox();
// move legend below histogram
QCPLayoutGrid *subLayout = new QCPLayoutGrid;
subLayout->addElement(0, 0, new QCPLayoutElement(m_histogram)); // spacer
subLayout->addElement(0, 1, m_histogram->legend); // legend
subLayout->addElement(0, 2, new QCPLayoutElement(m_histogram)); // spacer
subLayout->setMaximumSize(400, 30); // legend size
subLayout->setMargins(QMargins(5, 5, 5, 5)); // legend margin
m_histogram->plotLayout()->addElement(2, 0, subLayout); // plot legend
// create extension and insert histogram
m_extension = new QWidget;
QVBoxLayout *vboxExt = new QVBoxLayout(m_extension);
vboxExt->addWidget(m_histogram);
// start dialog with hidden histogram
m_extension->hide();
// assemble stacked widget in vertical layout
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addLayout(hbox);
vbox->addWidget(m_stackWidgetPanels);
vbox->addWidget(m_labelTime);
vbox->addWidget(m_extension);
vbox->addStretch(1);
vbox->addLayout(createExitButtons());
m_groupBoxPanels->setLayout(vbox);
// disable the control panel until input image is read
m_groupBoxPanels->setEnabled(false);
return m_groupBoxPanels;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createGroupDisplay:
//
// Create group box for displaying images.
//
QFrame*
MainWindow::createGroupDisplay()
{
// init group box
QFrame *frame = new QFrame;
frame->setContentsMargins(0, 0, 0, 0);
frame->setStyleSheet(FrameStyle);
// create glwidget for displaying input/output images
QGLFormat glf = QGLFormat::defaultFormat();
glf.setVersion(3, 3);
glf.setProfile(QGLFormat::CoreProfile);
glf.setSampleBuffers(true);
glf.setSamples(4);
glf.setSwapInterval(0);
glf.setDefaultFormat(glf);
m_glw = new GLWidget(glf, this);
// assemble stacked widget in vertical layout
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(m_glw);
frame->setLayout(vbox);
return frame;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createDisplayButtons:
//
// Create group box for the display buttons.
//
QGroupBox*
MainWindow::createDisplayButtons()
{
// init group box
QGroupBox *groupBox = new QGroupBox("Display");
// create radio buttons
m_radioDisplay[0] = new QRadioButton("Input");
m_radioDisplay[1] = new QRadioButton("Output");
// create button group and add radio buttons to it
QButtonGroup *bGroup = new QButtonGroup;
for(int i = 0; i<2; ++i)
bGroup->addButton(m_radioDisplay[i]);
// set input radio button to be default
m_radioDisplay[0]->setChecked(true);
// assemble radio buttons into vertical widget
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(m_radioDisplay[0]);
vbox->addWidget(m_radioDisplay[1]);
vbox->addStretch(1);
groupBox->setLayout(vbox);
// init signal/slot connections
connect(m_radioDisplay[0], SIGNAL(clicked()), this, SLOT(displayIn()));
connect(m_radioDisplay[1], SIGNAL(clicked()), this, SLOT(displayOut()));
return groupBox;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createModeButtons:
//
// Create group box for the mode buttons.
//
QGroupBox*
MainWindow::createModeButtons()
{
// init group box
QGroupBox *groupBox = new QGroupBox("Mode");
// create radio buttons
m_radioMode[0] = new QRadioButton("RGB");
m_radioMode[1] = new QRadioButton("Gray");
// create button group and add radio buttons to it
QButtonGroup *bGroup = new QButtonGroup;
for(int i = 0; i<2; ++i)
bGroup->addButton(m_radioMode[i]);
// set Gray radio button to be default
m_radioMode[1]->setChecked(true);
// assemble radio buttons into vertical widget
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(m_radioMode[0]);
vbox->addWidget(m_radioMode[1]);
vbox->addStretch(1);
groupBox->setLayout(vbox);
// init signal/slot connections
connect(m_radioMode[0], SIGNAL(clicked()), this, SLOT(modeRGB ()));
connect(m_radioMode[1], SIGNAL(clicked()), this, SLOT(modeGray()));
return groupBox;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createOptionButtons:
//
// Create group box for the options buttons.
//
QGroupBox*
MainWindow::createOptionButtons()
{
// init group box
QGroupBox *groupBox = new QGroupBox("Options");
// create radio buttons
m_radioOption[0] = new QRadioButton("Histogram");
m_radioOption[1] = new QRadioButton("GPU");
// create button group and add radio buttons to it
QButtonGroup *bGroup = new QButtonGroup;
for(int i = 0; i<2; ++i)
bGroup->addButton(m_radioOption[i]);
// set Histogram radio button to be default
m_radioOption[0]->setChecked(true);
// assemble radio buttons into vertical widget
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(m_radioOption[0]);
vbox->addWidget(m_radioOption[1]);
vbox->addStretch(1);
groupBox->setLayout(vbox);
// set signal-slot connection
connect(m_radioOption[0], SIGNAL(clicked()), this, SLOT(optionHisto()));
connect(m_radioOption[1], SIGNAL(clicked()), this, SLOT(optionGPU ()));
return groupBox;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createExitButtons:
//
// Create save/quit buttons.
//
QHBoxLayout*
MainWindow::createExitButtons()
{
// create pushbuttons
QPushButton *buttonReset = new QPushButton("Reset");
m_buttonTiming = new QPushButton("Timing");
QPushButton *buttonQuit = new QPushButton("Quit");
// init signal/slot connections
connect(buttonReset, SIGNAL(clicked()), this, SLOT(reset()));
connect(m_buttonTiming, SIGNAL(clicked()), this, SLOT(evalTiming()));
connect(buttonQuit, SIGNAL(clicked()), this, SLOT(quit ()));
// assemble pushbuttons in horizontal layout
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(buttonReset);
buttonLayout->addWidget(m_buttonTiming);
buttonLayout->addWidget(buttonQuit );
return buttonLayout;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::reset:
//
// Reset application.
//
void
MainWindow::reset()
{
m_imageFilter[m_code]->reset();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::quit:
//
// Quit application.
//
void
MainWindow::quit()
{
// close the dialog window
close();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::open:
//
// Open input image.
//
void
MainWindow::open()
{
QFileDialog dialog(this);
// open the last known working directory
// open the last known working directory
QSettings settings(QSettings::NativeFormat,
QSettings::UserScope,
"CCNY",
"CS470");
m_currentDir = settings.value("currentDir").toString();
if(!m_currentDir.isEmpty())
dialog.setDirectory(m_currentDir);
// display existing files and directories
dialog.setFileMode(QFileDialog::ExistingFile);
// invoke native file browser to select file
m_file = dialog.getOpenFileName(this,
"Open File", m_currentDir,
"Images (*.jpg *.png *.ppm *.pgm *.bmp);;All files (*)");
// verify that file selection was made
if(m_file.isNull()) return;
// save current directory
QFileInfo f(m_file);
m_currentDir = f.absolutePath();
settings.setValue("currentDir", m_currentDir);
// read input image
m_imageIn = IP_readImage(qPrintable(m_file));
int w = m_imageIn->width();
int h = m_imageIn->height();
QRect rect = m_glwFrame->contentsRect();
m_glwFrameW = rect.width();
m_glwFrameH = rect.height();
m_glw->allocateTextureFBO(w, h);
QImage q;
IP_IPtoQImage(m_imageIn, q);
m_glw->setInTexture(q);
// set input radio button to be default
m_radioDisplay[0]->setChecked(true);
// display histogram by default
m_extension->setVisible(true);
if(m_radioMode[1]->isChecked())
IP_castImage(m_imageIn, BW_IMAGE, m_imageSrc);
else IP_castImage(m_imageIn, RGB_IMAGE, m_imageSrc);
// init vars
m_width = m_imageSrc->width ();
m_height = m_imageSrc->height();
preview();
// enable the following now that input image is read
m_menuPtOps ->setEnabled(true);
m_menuNbrOps ->setEnabled(true);
m_menuFFT ->setEnabled(true);
m_groupBoxPanels->setEnabled(true);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::save:
//
// Save image.
//
void
MainWindow::save() {
QFileDialog dialog(this);
// open the last known working directory
if(!m_currentDir.isEmpty())
dialog.setDirectory(m_currentDir);
// display existing files and directories
dialog.setFileMode(QFileDialog::ExistingFile);
// invoke native file browser to select file
QString saveName = dialog.getSaveFileName(this,
"Save File", m_currentDir,
"Images (*.jpg *.png *.ppm *.pgm *.bmp);;All files (*)");
// verify that file selection was made
if(saveName.isEmpty()) return;
// save current directory
QFileInfo f(m_file);
m_currentDir = f.absolutePath();
// save file
QString tag = QFileInfo(saveName).suffix().toUpper();
IP_saveImage(g_mainWindowP->imageSrc(), qPrintable(saveName), qPrintable(tag));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::display:
//
// Slot functions to display input and output images.
//
void MainWindow::displayIn () { display(0); }
void MainWindow::displayOut() { display(1); }
void MainWindow::display(int flag)
{
// error checking
if(m_imageSrc.isNull()) return; // no input image
if(m_imageDst.isNull() && flag) return; // no output image
// set radio button
m_radioDisplay[flag]->setChecked(1);
// determine image to be displayed
ImagePtr I;
if(flag == 0)
I = m_imageSrc;
else I = m_imageDst;
// convert from ImagePtr to QImage to Pixmap
QImage q;
IP_IPtoQImage(I, q);
if(flag == 0)
m_glw->setInTexture(q);
else
m_glw->setOutTexture(q);
// compute histogram if histogram checkbox is set
displayHistogram(I);
m_glw->setViewport(I->width(), I->height(), m_glwFrameW, m_glwFrameH);
m_glw->update();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::displayHistogram:
//
// Display image histogram in control panel
//
void MainWindow::displayHistogram(ImagePtr I)
{
int color;
int histo[MXGRAY];
int yminChannel=0, ymaxChannel=0;
int yminHisto=0, ymaxHisto=0;
int xmin, xmax;
QVector<double> x, y;
char buf[MXSTRLEN];
// verify if histogram option is set
if(!m_radioOption[0]->isChecked()) return;
// clear any previous histogram plots
m_histogram->clearGraphs();
// visit all selected channels in I: RGB, R, G, B, or gray
for(int ch=0; ch<I->maxChannel(); ch++) {
// compute histogram
IP_histogram(I, ch, histo, MXGRAY, m_histoXmin[ch], m_histoXmax[ch]);
// init min and max for current channel
yminChannel = ymaxChannel = histo[0];
// init min and max histogram value among all channels
if(!ch) yminHisto = ymaxHisto = histo[0];
// clear vector of x- and y-coordinates
x.clear();
y.clear();
// visit all histogram entries and save into x and y vectors
for(int i=0; i<MXGRAY; ++i) {
x.push_back(i);
y.push_back(histo[i]);
// save channel min and max
yminChannel = MIN(yminChannel, histo[i]);
ymaxChannel = MAX(ymaxChannel, histo[i]);
}
// add new graph for histogram channel
m_histogram->addGraph();
// if single channel was selected, it is in channel 0 and should
// be drawn in corresponding color (based on m_histoColor).
// Else, color is based on ch value: 0,1,2 corresponds to R,G,B
// Add 1 to ch so that ch=1 corresponds to green, and ch=2 to blue
if(!ch) color = m_histoColor;
else color = ch + 1;
// convert xmin, xmax of channel into int
xmin = m_histoXmin[ch];
xmax = m_histoXmax[ch];
// set histogram name and color
switch(color) {
case RGB:
case R: sprintf(buf, "R: X[%d,%d] Y[%d,%d]",
xmin, xmax, yminChannel, ymaxChannel);
m_histogram->graph(ch)->setName (buf);
m_histogram->graph(ch)->setPen (QPen(Qt::red ));
m_histogram->graph(ch)->setBrush(QBrush(QColor(255,0,0,30)));
break;
case G: sprintf(buf, "G: X[%d,%d] Y[%d,%d]",
xmin, xmax, yminChannel, ymaxChannel);
m_histogram->graph(ch)->setName (buf);
m_histogram->graph(ch)->setPen (QPen(Qt::green));
m_histogram->graph(ch)->setBrush(QBrush(QColor(0,255,0,30)));
break;
case B: sprintf(buf, "B: X[%d,%d] Y[%d,%d]",
xmin, xmax, yminChannel, ymaxChannel);
m_histogram->graph(ch)->setName (buf);
m_histogram->graph(ch)->setPen (QPen(Qt::blue ));
m_histogram->graph(ch)->setBrush(QBrush(QColor(0,0,255,30)));
break;
case GRAY:
sprintf(buf, "Gray: X[%d,%d] Y[%d,%d]",
xmin, xmax, yminChannel, ymaxChannel);
m_histogram->graph(ch)->setName (buf);
m_histogram->graph(ch)->setPen (QPen(Qt::black ));
m_histogram->graph(ch)->setBrush(QBrush(QColor(80,80,80,80)));
break;
}
// set data
m_histogram->graph(ch)->setData(x,y);
// update min and max histogram values among all channels
yminHisto = MIN(yminChannel, yminHisto);
ymaxHisto = MAX(ymaxChannel, ymaxHisto);
}
// turn on legend to print histogram params
m_histogram->legend->setVisible(true);
// set y-axis range to [0, ymaxHisto + 5%]
m_histogram->yAxis->setRange(0, ymaxHisto*1.05);
// set x-axis range to add side margins
m_histogram->xAxis->setRange(-5, 260);
// replot
m_histogram->replot();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::evalTiming:
//
// Slot to average runtime of selected filter.
//
void
MainWindow::evalTiming()
{
// error checking: no filter is selected
if(m_code <= 0) return;
// disable timing button to prevent multiple clicks during runtime
m_buttonTiming->blockSignals(true);
m_buttonTiming->setDisabled (true);
// init current clock time
clock_t t = clock();
// init number of iterations for computing average runtime
int ntries = 100;
// special case: limit iterations for computationally expensive filters
if (m_code == MEDIAN)
ntries = 10;
// run filter ntries times; print percentages at 10% increments to indicate progress
int update = ntries / 10;
for(int i = 1; i <= ntries; ++i) {
m_imageFilter[m_code]->applyFilter(m_imageSrc, gpuFlag(), m_imageDst);
// update execution time string with dots in lieu of progress bar
if(i == update) {
m_labelTime->setText(QString("Execution time (ms): %1%").arg(100*i/ntries));
update += ntries/10; // next update value
//qApp->processEvents(); // display immediately
}
}
// compute average execution time: divide elapsed time by number of iterations
double dt = (clock() - t) / (double) ntries;
// convert to milliseconds: multiply microseconds by 1000
dt *= (1000. / CLOCKS_PER_SEC);
// print result
m_labelTime->setText(QString("Execution time (ms): %1").arg(dt, 5, 'd', 3));
// display output image and update histogram (if necessary)
preview();
// enable timing button
m_buttonTiming->setDisabled (false);
m_buttonTiming->blockSignals(false);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::mode:
//
// Slot functions to display RGB and grayscale images.
//
void MainWindow::modeRGB () { mode(0); }
void MainWindow::modeGray() { mode(1); }
void MainWindow::mode(int flag)
{
// error checking
if(m_imageSrc.isNull()) return; // no input image
if(flag)
IP_castImage(m_imageIn, BW_IMAGE, m_imageSrc);
else IP_castImage(m_imageIn, RGB_IMAGE, m_imageSrc);
QImage q;
IP_IPtoQImage(m_imageSrc, q);
m_glw->setInTexture(q);
if(m_imageSrc->imageType() == BW_IMAGE)
m_histoColor = GRAY; // gray
else m_histoColor = 0; // RGB
// display image
if(m_radioDisplay[0]->isChecked()) // displaying input image
display(0);
else preview();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::option:
//
// Slot functions to display histogram or use GPU mode (disable histogram).
//
void MainWindow::optionHisto() { option(0); }
void MainWindow::optionGPU () { option(1); }
void MainWindow::option(int flag)
{
// error checking
if(m_imageSrc.isNull()) return; // no input image
if(!flag) {
m_extension->setVisible(true);
m_flagGPU = false;
} else {
// check if GPU method is implemented for the current filter
if(!m_imageFilter[m_code]->gpuImplemented()) {
// reset back to Histogram option
m_radioOption[0]->setChecked(true);
m_radioOption[1]->setChecked(false);
// print message that GPU method is not implemented
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText("GPU method is not implemented.");
msgBox.exec();
return;
}
m_extension->setVisible(false);
m_flagGPU = true;
}
preview();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::preview:
//
// Display preview image.
//
void
MainWindow::preview()
{
// apply filter to source image; save result in destination image
if(m_code > 0) {
m_imageFilter[m_code]->applyFilter(m_imageSrc, gpuFlag(), m_imageDst);
display(1);