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
|
#include <QtGui>
#include "imageViewer.h"
ImageViewer::ImageViewer() {
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
setCentralWidget(scrollArea);
createActions();
createMenus();
setWindowTitle(tr("Image Viewer"));
resize(500, 400);
}
void ImageViewer::open() {
fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
image = new QImage(fileName);
if (image->isNull()) {
QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
return;
}
cout << "QImage is null ?" << image->isNull() << "\n";
imageLabel->setPixmap(QPixmap::fromImage(*image));
imageLabel->adjustSize();
grayScaleAct->setEnabled(true);
}
}
void ImageViewer::grayScale() {
cout << "QImage is null ? " << image->isNull() << "\n";
cout << "Height : " << image->height() << " Width : " << image->width() << "\n";
for (int y = 0; y < image->height(); y++) {
for (int x = 0; x < image->width(); x++) {
image->setPixel(x,y,(0.299*qRed(image->pixel(x,y)) + 0.587*qGreen(image->pixel(x,y)) + 0.114*qBlue(image->pixel(x,y))));
}
}
imageLabel->setPixmap(QPixmap::fromImage(*image));
imageLabel->adjustSize();
//image->save("~/Desktop/grayscale.jpg");
}
void ImageViewer::createActions() {
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
grayScaleAct = new QAction(tr("&GrayScale Conversion"), this);
grayScaleAct->setShortcut(tr("Ctrl+G"));
grayScaleAct->setEnabled(false);
connect(grayScaleAct, SIGNAL(triggered()), this, SLOT(grayScale()));
}
void ImageViewer::createMenus() {
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
convertMenu = new QMenu(tr("&Convert"), this);
convertMenu->addAction(grayScaleAct);
menuBar()->addMenu(fileMenu);
menuBar()->addMenu(convertMenu);
} |
Partager