-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMainWindow.cpp
566 lines (456 loc) · 17 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
#include "MainWindow.h"
MainWindow::MainWindow(QRect screenRect)
{
workspace = new QWorkspace;
setCentralWidget(workspace);
//connect(workspace, SIGNAL(windowActivated(QWidget *)),
// this, SLOT(updateMenus()));
createActions();
createMenus();
createToolBars();
createStatusBar();
setWindowTitle(tr("DCT & FFT Editor"));
setWindowIcon(QPixmap(":/images/app.png"));
resize((screenRect.right() - screenRect.left())/2, (screenRect.bottom() - screenRect.top())/2);
move((screenRect.right() - screenRect.left())/4, (screenRect.bottom() - screenRect.top())/4);
}
//The closeEvent() function is reimplemented to close all child windows, causing each child to
//receive a close event.
void MainWindow::closeEvent(QCloseEvent *event)
{
workspace->closeAllWindows();
if (activeImageDisplay())
{
event->ignore();
}
else
{
event->accept();
}
}
//The activeEditor() private function returns the active child window as an ImageDisplay pointer,
//or a null pointer if there isn't one.
ImageDisplay *MainWindow::activeImageDisplay()
{
return qobject_cast<ImageDisplay *>(workspace->activeWindow());
}
void MainWindow::createActions()
{
openAction = new QAction(QIcon(":/images/open.png"), tr("&Open..."),
this);
openAction->setShortcut(tr("Ctrl+O"));
openAction->setStatusTip(tr("Open an existing file"));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
saveAction = new QAction(QIcon(":/images/save.png"), tr("&Save"),
this);
saveAction->setShortcut(tr("Ctrl+S"));
saveAction->setStatusTip(tr("Save the file to disk"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
saveAsAction = new QAction(tr("Save &As..."), this);
saveAsAction->setStatusTip(tr("Save the file under a new name"));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
closeAction = new QAction(tr("Cl&ose"), this);
closeAction->setShortcut(tr("Ctrl+F4"));
closeAction->setStatusTip(tr("Close the active window"));
connect(closeAction, SIGNAL(triggered()),
workspace, SLOT(closeActiveWindow()));
closeAllAction = new QAction(tr("Close &All"), this);
closeAllAction->setStatusTip(tr("Close all windows"));
connect(closeAllAction, SIGNAL(triggered()),
workspace, SLOT(closeAllWindows()));
tileAction = new QAction(tr("&Tile"), this);
tileAction->setStatusTip(tr("Tile the windows"));
connect(tileAction, SIGNAL(triggered()), workspace, SLOT(tile()));
cascadeAction = new QAction(tr("&Cascade"), this);
cascadeAction->setStatusTip(tr("Cascade the windows"));
connect(cascadeAction, SIGNAL(triggered()),
workspace, SLOT(cascade()));
nextAction = new QAction(tr("Ne&xt"), this);
nextAction->setShortcut(tr("Ctrl+F6"));
nextAction->setStatusTip(tr("Move the focus to the next window"));
connect(nextAction, SIGNAL(triggered()),
workspace, SLOT(activateNextWindow()));
previousAction = new QAction(tr("Pre&vious"), this);
previousAction->setShortcut(tr("Ctrl+Shift+F6"));
previousAction->setStatusTip(tr("Move the focus to the previous "
"window"));
connect(previousAction, SIGNAL(triggered()),
workspace, SLOT(activatePreviousWindow()));
separatorAction = new QAction(this);
separatorAction->setSeparator(true);
aboutAction = new QAction(tr("&About"), this);
aboutAction->setStatusTip(tr("Show the application's About box"));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
powerSpectrumFFTAction = new QAction(tr("Power spectrum with &FFT"), this);
powerSpectrumFFTAction->setIcon(QIcon(":/images/fft.png"));
powerSpectrumFFTAction->setShortcut(tr("Ctrl+F"));
powerSpectrumFFTAction->setStatusTip(tr("Get power spectrum using FFT"));
connect(powerSpectrumFFTAction, SIGNAL(triggered()), this, SLOT(getPowerSpectrumUsingFFT()));
powerSpectrumDCTAction = new QAction(tr("Power spectrum with &DCT"), this);
powerSpectrumDCTAction->setIcon(QIcon(":/images/dct.png"));
powerSpectrumDCTAction->setShortcut(tr("Ctrl+D"));
powerSpectrumDCTAction->setStatusTip(tr("Get power spectrum using DCT"));
connect(powerSpectrumDCTAction, SIGNAL(triggered()), this, SLOT(getPowerSpectrumUsingDCT()));
linearConvolutionFFTAction = new QAction(tr("Convolution using FFT"), this);
linearConvolutionFFTAction->setIcon(QIcon(":/images/cfft.png"));
linearConvolutionFFTAction->setShortcut(tr("Ctrl+L"));
linearConvolutionFFTAction->setStatusTip(tr("Convolution using FFT"));
connect(linearConvolutionFFTAction, SIGNAL(triggered()), this, SLOT(convolutionFFTDialog()));
linearConvolutionDCTAction = new QAction(tr("Convolution using DCT"), this);
linearConvolutionDCTAction->setIcon(QIcon(":/images/cdct.png"));
linearConvolutionDCTAction->setShortcut(tr("Ctrl+K"));
linearConvolutionDCTAction->setStatusTip(tr("Convolution using DCT"));
connect(linearConvolutionDCTAction, SIGNAL(triggered()), this, SLOT(convolutionDCTDialog()));
windowActionGroup = new QActionGroup(this);
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
calcMenu = menuBar()->addMenu(tr("&Calculations"));
calcMenu->addAction(powerSpectrumFFTAction);
calcMenu->addAction(powerSpectrumDCTAction);
calcMenu->addSeparator();
calcMenu->addAction(linearConvolutionFFTAction);
calcMenu->addSeparator();
calcMenu->addAction(linearConvolutionDCTAction);
windowMenu = menuBar()->addMenu(tr("&Window"));
windowMenu->addAction(closeAction);
windowMenu->addAction(closeAllAction);
windowMenu->addSeparator();
windowMenu->addAction(tileAction);
windowMenu->addAction(cascadeAction);
windowMenu->addSeparator();
windowMenu->addAction(nextAction);
windowMenu->addAction(previousAction);
windowMenu->addAction(separatorAction);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction);
calcToolBar = addToolBar(tr("Calculations"));
calcToolBar->addAction(powerSpectrumFFTAction);
calcToolBar->addAction(powerSpectrumDCTAction);
calcToolBar->addAction(linearConvolutionFFTAction);
calcToolBar->addAction(linearConvolutionDCTAction);
}
void MainWindow::createStatusBar()
{
imageFormatLabel = new QLabel("");
imageFormatLabel->setAlignment(Qt::AlignHCenter);
imageFormatLabel->setMinimumSize(imageFormatLabel->sizeHint());
imageSizeLabel = new QLabel("");
statusBar()->addWidget(imageFormatLabel);
statusBar()->addWidget(imageSizeLabel, 1);
statusBar()->setBackgroundRole(QPalette::Light);
connect(workspace, SIGNAL(windowActivated(QWidget *)),
this, SLOT(updateStatusBar()));
updateStatusBar();
}
void MainWindow::updateStatusBar()
{
if (activeImageDisplay())
{
int format = (int)activeImageDisplay()->getImage().format();
QString string = imageFormatToString(format);
imageFormatLabel->setText(string);
int height = (int)activeImageDisplay()->getImage().height();
int width = (int)activeImageDisplay()->getImage().width();
string = tr("Image size: %1 x %2").arg(width).arg(height);
imageSizeLabel->setText(string);
}
else
{
imageFormatLabel->setText("");
imageSizeLabel->setText("");
}
}
ImageDisplay *MainWindow::createImageDisplay(int maxCurrentHeight, int maxCurrentWidth)
{
ImageDisplay *imageDisplay = new ImageDisplay(this, maxCurrentHeight, maxCurrentWidth);
workspace->addWindow(imageDisplay);
windowMenu->addAction(imageDisplay->windowMenuAction());
windowActionGroup->addAction(imageDisplay->windowMenuAction());
return imageDisplay;
}
void MainWindow::newFile()
{
ImageDisplay *imageDisplay = createImageDisplay(workspace->height(), workspace->width());
imageDisplay->newFile();
imageDisplay->show();
}
void MainWindow::open()
{
ImageDisplay *imageDisplay = createImageDisplay(workspace->height(), workspace->width());
if (imageDisplay->open())
{
imageDisplay->show();
statusBar()->showMessage(tr("File loaded"), 2000);
}
else
{
imageDisplay->close();
statusBar()->showMessage(tr("Loading failed"), 2000);
}
}
void MainWindow::save()
{
if (activeImageDisplay())
{
bool success = activeImageDisplay()->save();
if (success)
statusBar()->showMessage(tr("File saved"), 2000);
else
statusBar()->showMessage(tr("Saving failed"), 2000);
}
}
void MainWindow::saveAs()
{
if (activeImageDisplay())
{
bool success = activeImageDisplay()->saveAs();
if (success)
statusBar()->showMessage(tr("File saved"), 2000);
else
statusBar()->showMessage(tr("Saving failed"), 2000);
}
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About DCT & FFT Editor"),
tr("<h2>DCT & FFT Editor 1.1</h2>"
"<p>Copyright © Blaz Poje"
"<p>DCT & FFT Editor is a small application that demonstrates "
"image processing in the Fourier Domain."));
}
QString MainWindow::imageFormatToString(int format)
{
QString string(tr("Image format: "));
switch (format)
{
case 0:
string += tr("Format_Invalid");
break;
case 1:
string += tr("Format_Mono");
break;
case 2:
string += tr("Format_MonoLSB");
break;
case 3:
string += tr("Format_Indexed8");
break;
case 5:
string += tr("Format_ARGB32");
break;
default:
string += tr("Format_RGB32");
break;
}
return string;
}
void MainWindow::getPowerSpectrumUsingFFT()
{
if (activeImageDisplay())
{
QImage image = activeImageDisplay()->getImage();
QString curFile = activeImageDisplay()->getCurFileWithStrippedName();
ImageOperations imageOperations(image);
if (image.allGray())
{
QImage RImage = imageOperations.FFTPowerSpectrum(Red);
if (RImage.height() == 0)
return;
ImageDisplay *redImageDisplay = createImageDisplay(workspace->height(), workspace->width());
redImageDisplay->newFile("FFT Gray power spectrum of " + curFile);
redImageDisplay->setImage(RImage);
redImageDisplay->show();
}
else
{
QImage RImage = imageOperations.FFTPowerSpectrum(Red);
QImage GImage = imageOperations.FFTPowerSpectrum(Green);
QImage BImage = imageOperations.FFTPowerSpectrum(Blue);
if (RImage.height() == 0 || GImage.height() == 0 || BImage.height() == 0)
return;
ImageDisplay *redImageDisplay = createImageDisplay(workspace->height(), workspace->width());
redImageDisplay->newFile("FFT Red power spectrum of " + curFile);
redImageDisplay->setImage(RImage);
redImageDisplay->show();
ImageDisplay *greenImageDisplay = createImageDisplay(workspace->height(),workspace->width());
greenImageDisplay->newFile("FFT Green power spectrum of " + curFile);
greenImageDisplay->setImage(GImage);
greenImageDisplay->show();
ImageDisplay *blueImageDisplay = createImageDisplay(workspace->height(), workspace->width());
blueImageDisplay->newFile("FFT Blue power spectrum of " + curFile);
blueImageDisplay->setImage(BImage);
blueImageDisplay->show();
}
}
}
void MainWindow::getPowerSpectrumUsingDCT()
{
if (activeImageDisplay())
{
QImage image = activeImageDisplay()->getImage();
QString curFile = activeImageDisplay()->getCurFileWithStrippedName();
ImageOperations imageOperations(image);
if (image.allGray())
{
QImage RImage = imageOperations.DCTPowerSpectrum(Red);
if (RImage.height() == 0)
return;
ImageDisplay *redImageDisplay = createImageDisplay(workspace->height(), workspace->width());
redImageDisplay->newFile("DCT Gray power spectrum of " + curFile);
redImageDisplay->setImage(RImage);
redImageDisplay->show();
}
else
{
QImage RImage = imageOperations.DCTPowerSpectrum(Red);
QImage GImage = imageOperations.DCTPowerSpectrum(Green);
QImage BImage = imageOperations.DCTPowerSpectrum(Blue);
if (RImage.height() == 0 || GImage.height() == 0 || BImage.height() == 0)
return;
ImageDisplay *redImageDisplay = createImageDisplay(workspace->height(), workspace->width());
redImageDisplay->newFile("DCT Red power spectrum of " + curFile);
redImageDisplay->setImage(RImage);
redImageDisplay->show();
ImageDisplay *greenImageDisplay = createImageDisplay(workspace->height(),workspace->width());
greenImageDisplay->newFile("DCT Green power spectrum of " + curFile);
greenImageDisplay->setImage(GImage);
greenImageDisplay->show();
ImageDisplay *blueImageDisplay = createImageDisplay(workspace->height(), workspace->width());
blueImageDisplay->newFile("DCT Blue power spectrum of " + curFile);
blueImageDisplay->setImage(BImage);
blueImageDisplay->show();
}
}
}
void MainWindow::convolutionFFTDialog()
{
if (activeImageDisplay())
{
ConvolutionDialog convolutionDialog(this);
connect(&convolutionDialog, SIGNAL(finalSelected(ImageDisplay *)),
this, SLOT(performConvolutionFFT(ImageDisplay *)));
QImage currentImage = activeImageDisplay()->getImage();
int imageHeight = currentImage.height();
int imageWidth = currentImage.width();
int newHeightAndWidth = ImageOperations::firstPowerOfTwoOfLargerDimension(imageWidth, imageHeight);
int newHeight = newHeightAndWidth;
int newWidth = newHeightAndWidth;
QList<QAction*> list = windowActionGroup->actions();
QList<QAction*>::const_iterator stlIter1;
for ( stlIter1 = list.begin(); stlIter1 != list.end(); ++stlIter1 )
{
QAction* action = (*stlIter1);
//QWidget * QAction::parentWidget ()
QWidget *widget = action->parentWidget();
ImageDisplay *imageDisplay = (ImageDisplay*) widget;
if (activeImageDisplay() != imageDisplay)
{
QImage image = imageDisplay->getImage();
int maskHeight = image.height();
int maskWidth = image.width();
if (newHeight > 0 && newWidth > 0 && maskHeight > 0 && maskWidth > 0 &&
newHeight == maskHeight && newWidth == maskWidth)
{
convolutionDialog.addSelectCandidate(imageDisplay);
}
}
}
convolutionDialog.defineTargetMaskSize(newHeight, newWidth);
convolutionDialog.refreshComboBox();
int result = convolutionDialog.exec();
if (result == QDialog::Rejected)
return;
}
}
void MainWindow::convolutionDCTDialog()
{
if (activeImageDisplay())
{
ConvolutionDialog convolutionDialog(this);
connect(&convolutionDialog, SIGNAL(finalSelected(ImageDisplay *)),
this, SLOT(performConvolutionDCT(ImageDisplay *)));
QImage currentImage = activeImageDisplay()->getImage();
int imageHeight = currentImage.height();
int imageWidth = currentImage.width();
int newHeightAndWidth = ImageOperations::firstPowerOfTwoOfLargerDimension(imageWidth, imageHeight);
int newHeight = newHeightAndWidth;
int newWidth = newHeightAndWidth;
QList<QAction*> list = windowActionGroup->actions();
QList<QAction*>::const_iterator stlIter1;
for ( stlIter1 = list.begin(); stlIter1 != list.end(); ++stlIter1 )
{
QAction* action = (*stlIter1);
//QWidget * QAction::parentWidget ()
QWidget *widget = action->parentWidget();
ImageDisplay *imageDisplay = (ImageDisplay*) widget;
if (activeImageDisplay() != imageDisplay)
{
QImage image = imageDisplay->getImage();
int maskHeight = image.height();
int maskWidth = image.width();
if (newHeight > 0 && newWidth > 0 && maskHeight > 0 && maskWidth > 0 &&
newHeight == maskHeight && newWidth == maskWidth)
{
convolutionDialog.addSelectCandidate(imageDisplay);
}
}
}
convolutionDialog.defineTargetMaskSize(newHeight, newWidth);
convolutionDialog.refreshComboBox();
int result = convolutionDialog.exec();
if (result == QDialog::Rejected)
return;
}
}
void MainWindow::performConvolutionFFT(ImageDisplay *final)
{
QString curFile = activeImageDisplay()->getCurFileWithStrippedName();
QString maskFile = final->getCurFileWithStrippedName();
QImage image = activeImageDisplay()->getImage();
QImage mask = final->getImage();
ImageOperations imageOperations(image);
QImage retImage = imageOperations.FFTConvolution(mask);
if (retImage.height() == 0)
return;
ImageDisplay *imageDisplay = createImageDisplay(workspace->height(), workspace->width());
imageDisplay->newFile("FFT Convolution of " + curFile + " with mask " + maskFile);
imageDisplay->setImage(retImage);
imageDisplay->show();
}
void MainWindow::performConvolutionDCT(ImageDisplay *final)
{
QString curFile = activeImageDisplay()->getCurFileWithStrippedName();
QString maskFile = final->getCurFileWithStrippedName();
QImage image = activeImageDisplay()->getImage();
QImage mask = final->getImage();
ImageOperations imageOperations(image);
QImage retImage = imageOperations.DCTConvolution(mask);
if (retImage.height() == 0)
return;
ImageDisplay *imageDisplay = createImageDisplay(workspace->height(), workspace->width());
imageDisplay->newFile("DCT Convolution of " + curFile + " with mask " + maskFile);
imageDisplay->setImage(retImage);
imageDisplay->show();
}
void MainWindow::showMessageOnStatusBar( QString & message, int timeout )
{
statusBar()->showMessage(message, timeout);
}