/
MainWindow.cpp
532 lines (416 loc) · 13.5 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
// ===============================================================
// Computer Graphics Homework Solutions
// Copyright (C) 2016 by George Wolberg
//
// MainWindow.cpp - MainWindow class
//
// Written by: George Wolberg, 2016
// ===============================================================
#include "MainWindow.h"
#include "Dummy.h"
#include "Threshold.h"
#include "Contrast.h"
#include "Quantization.h"
#include "Gamma.h"
#include "Intensity.h"
enum {DUMMY, THRESHOLD, CONTRAST, QUANTIZATION, GAMMA, INTENSITY};
QString GroupBoxStyle = "QGroupBox { \
border: 2px solid gray; \
border-radius: 9px; \
margin-top: 0.5em;}";
MainWindow *g_mainWindowP = NULL;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::MainWindow:
//
// MainWindow constructor.
//
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_code(-1)
{
setWindowTitle("Capstone Project");
// set global variable for main window pointer
g_mainWindowP = this;
// INSERT YOUR ACTIONS AND MENUS
createActions();
createMenus ();
createWidgets();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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_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_actionContrast = new QAction("&Contrast", this);
m_actionContrast->setShortcut(tr("Ctrl+C"));
m_actionContrast->setData(CONTRAST);
m_actionQuantization = new QAction("Q&uantization", this);
m_actionQuantization->setShortcut(tr("Ctrl+U"));
m_actionQuantization->setData(QUANTIZATION);
m_actionGamma = new QAction("&Gamma", this);
m_actionGamma->setShortcut(tr("Ctrl+G"));
m_actionGamma->setData(GAMMA);
m_actionIntensity = new QAction("&Intensity", this);
m_actionIntensity->setShortcut(tr("Ctrl+I"));
m_actionIntensity->setData(INTENSITY);
// 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_actionQuit);
// Point Ops menu
m_menuPtOps = menuBar()->addMenu("&Point Ops");
m_menuPtOps->addAction(m_actionThreshold);
m_menuPtOps->addAction(m_actionContrast );
m_menuPtOps->addAction(m_actionQuantization);
m_menuPtOps->addAction(m_actionGamma);
m_menuPtOps->addAction(m_actionIntensity);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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;
hbox->addWidget(createGroupDisplay());
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
QGroupBox *groupBox = new QGroupBox;
groupBox->setMinimumWidth(350);
// filter's enum indexes into container of image filters
m_imageFilterType[DUMMY ] = new Dummy;
m_imageFilterType[THRESHOLD] = new Threshold;
m_imageFilterType[CONTRAST ] = new Contrast;
m_imageFilterType[QUANTIZATION] = new Quantization;
m_imageFilterType[GAMMA] = new Gamma;
m_imageFilterType[INTENSITY] = new Intensity;
// create a stacked widget to hold multiple control panels
m_stackWidgetPanels = new QStackedWidget;
// add filter control panels to stacked widget
m_stackWidgetPanels->addWidget(m_imageFilterType[DUMMY ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilterType[THRESHOLD]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilterType[CONTRAST ]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilterType[QUANTIZATION]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilterType[GAMMA]->controlPanel());
m_stackWidgetPanels->addWidget(m_imageFilterType[INTENSITY]->controlPanel());
// display blank dummmy panel initially
m_stackWidgetPanels->setCurrentIndex(0);
// assemble display and mode groups into horizontal layout
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(createDisplayButtons());
hbox->addWidget(createModeButtons ());
// assemble stacked widget in vertical layout
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addLayout(hbox);
vbox->addWidget(m_stackWidgetPanels);
vbox->addStretch(1);
vbox->addLayout(createExitButtons());
groupBox->setLayout(vbox);
return groupBox;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createGroupDisplay:
//
// Create group box for displaying images.
//
QGroupBox*
MainWindow::createGroupDisplay()
{
// init group box
QGroupBox *groupBox = new QGroupBox;
groupBox->setMinimumWidth (700);
groupBox->setMinimumHeight(700);
groupBox->setStyleSheet(GroupBoxStyle);
// create stacked widget for input/output images
m_stackWidgetImages = new QStackedWidget;
// add QLabels to image stacked widget to display input/output images
for(int i = 0; i<2; ++i)
m_stackWidgetImages->addWidget(new QLabel);
// add centering alignment on both labels
QLabel *label;
label = (QLabel *) m_stackWidgetImages->widget(0); label->setAlignment(Qt::AlignCenter);
label = (QLabel *) m_stackWidgetImages->widget(1); label->setAlignment(Qt::AlignCenter);
// set stacked widget to default setting: input image
m_stackWidgetImages->setCurrentIndex(0);
// assemble stacked widget in vertical layout
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(m_stackWidgetImages);
groupBox->setLayout(vbox);
return groupBox;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::createDisplayButtons:
//
// Create preview window groupbox.
//
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]);
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 preview window groupbox.
//
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 RGB radio button to be default
m_radioMode[0]->setChecked(true);
// assemble radio buttons into vertical widget
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(m_radioMode[0]);
vbox->addWidget(m_radioMode[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::createExitButtons:
//
// Create save/quit buttons.
//
QHBoxLayout*
MainWindow::createExitButtons()
{
// create pushbuttons
QPushButton *buttonReset = new QPushButton("Reset");
QPushButton *buttonQuit = new QPushButton("Quit");
// init signal/slot connections
connect(buttonReset, SIGNAL(clicked()), this, SLOT(reset()));
connect(buttonQuit , SIGNAL(clicked()), this, SLOT(quit ()));
// assemble pushbuttons in horizontal layout
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(buttonReset);
buttonLayout->addWidget(buttonQuit );
return buttonLayout;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::reset:
//
// Reset application.
//
void
MainWindow::reset()
{
// m_imageFilterType[ m_imageFilterName[index] ]->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
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();
// DISABLE IMAGING FUNCTIONALITY FOR NOW....
// read input image
m_imageIn = IP_readImage(qPrintable(m_file));
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();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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
// raise the appropriate widget from the stack
m_stackWidgetImages->setCurrentIndex(flag);
// 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;
// init image dimensions
int w = m_imageDst->width();
int h = m_imageDst->height();
// init view window dimensions
int ww = m_stackWidgetImages->width();
int hh = m_stackWidgetImages->height();
// convert from ImagePtr to QImage to Pixmap
QImage q;
IP_IPtoQImage(I, q);
// convert from QImage to Pixmap; rescale if image is larger than view window
QPixmap p;
if(MIN(w, h) > MIN(ww, hh))
p = QPixmap::fromImage(q.scaled(QSize(ww, hh), Qt::KeepAspectRatio));
else p = QPixmap::fromImage(q);
// assign pixmap to label widget for display
QLabel *widget = (QLabel *) m_stackWidgetImages->currentWidget();
widget->setPixmap(p);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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);
// re-apply filter for changed mode
if(m_code > 0)
m_imageFilterType[m_code]->applyFilter(m_imageSrc, m_imageDst);
// display image
preview();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::preview:
//
// Display preview image.
//
void
MainWindow::preview()
{
// display requested image
if(m_radioDisplay[0]->isChecked())
display(0);
else display(1);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// MainWindow::execute:
//
// Determine which action was triggered and execute respective action.
// Skip this if action is not image-processing related: open(), quit()
//
void
MainWindow::execute(QAction* action)
{
// skip over menu ops that don't require image processing
QString name = action->text();
if(name == QString("&Open") || name == QString("&Quit")) {
m_code = -1;
return;
}
// get code from action
m_code = action->data().toInt();
// set output radio button to true
m_radioDisplay[1]->setChecked(true);
// use code to index into stack widget and array of filters
m_stackWidgetPanels->setCurrentIndex(m_code);
m_imageFilterType[m_code]->applyFilter(m_imageSrc, m_imageDst);
preview();
}