From 99f6b596a10a3fefbde194b4d9a081a7e5ceaa14 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 6 Nov 2015 13:49:02 +0100 Subject: [PATCH] [TASK] Implement brightness slider logic. --- imageviewer-qt4.cpp | 74 ++++++++++++++++++++++++++++++++++++--------- imageviewer-qt4.h | 9 ++++-- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index c2fc7e3..2add697 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -45,8 +45,9 @@ * @brief ImageViewer::ImageViewer */ ImageViewer::ImageViewer() { - image=NULL; - histogramm=NULL; + original_image = NULL; + image = NULL; + histogramm = NULL; startLogging(); generateMainGui(); renewLogging(); @@ -56,6 +57,23 @@ ImageViewer::ImageViewer() { resize(1600, 600); } +/** + * Once an image is loaded, automatically perform these operations. + * + * analyze the image + * allocate brightness map + * + * @brief ImageViewer::initializeImage + */ +void ImageViewer::initializeImage() { + if(image==NULL) { + std::cout << "Error! No image provided!" << std::endl; + return; + } + analyzeImage(); +} + + /** * Draws a simple, black line. * @brief ImageViewer::drawBlackLine @@ -176,7 +194,7 @@ void ImageViewer::analyzeImage() { logFile << "done" << std::endl; renewLogging(); - QString result = QString("Intensity - average : %1, variance: %2").arg(getAverageIntensity()).arg(getIntensityVariance()); + QString result = QString("Intensity: Average: %1, Variance: %2").arg(getAverageIntensity()).arg(getIntensityVariance()); task_tab2_stats->setText(result); updateHistogramm(); @@ -244,6 +262,30 @@ void ImageViewer::updateHistogramm() { histogramm_label->setPixmap(QPixmap::fromImage(*histogramm)); } +/** + * Fired by the brightness_slider, this one adjusts the brightness on image. + * @brief ImageViewer::adjustBrightness + */ +void ImageViewer::adjustBrightness(int b) { + int h, s, old_l; + int new_l = 0; + int delta = b - 255; + for(int x=0; xwidth(); x++) { + for(int y=0; yheight(); y++) { + QColor color = QColor::fromRgb(original_image->pixel(x, y)); + color.getHsl(&h, &s, &old_l); + new_l = old_l + delta; + if(new_l > 255) new_l = 255; + if(new_l < 0) new_l = 0; + color.setHsl(h, s, new_l); + image->setPixel(x, y, color.rgba()); + } + } + updateImageDisplay(); + logFile << "Brightness adjusted to: " << old_l << " + " << delta << " -> (" << h << ", " << s << ", " << new_l << ")" << std::endl; + renewLogging(); +} + /**************************************************************************************** * * GUI elements related to the tasks are set up here. @@ -294,14 +336,10 @@ void ImageViewer::generateControlPanels() { task_tab_widget2 = new QWidget(); task_tab2 = new QVBoxLayout(); task_tab_widget2->setLayout(task_tab2); - - task_tab2_button1 = new QPushButton(); - task_tab2_button1->setText("Analyze image"); - QObject::connect(task_tab2_button1, SIGNAL(clicked()), this, SLOT (analyzeImage())); - + histogramm_label = new QLabel(); histogramm_label->setBackgroundRole(QPalette::Base); - histogramm_label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); + histogramm_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); histogramm_label->setScaledContents(false); task_tab2_stats = new QLabel(); @@ -309,15 +347,15 @@ void ImageViewer::generateControlPanels() { brightness_slider = new QSlider(Qt::Horizontal); brightness_slider->setMinimum(0); - brightness_slider->setMaximum(255); - brightness_slider->setValue(127); + brightness_slider->setMaximum(510); + brightness_slider->setValue(255); + QObject::connect(brightness_slider, SIGNAL(valueChanged(int)), this, SLOT (adjustBrightness(int))); contrast_slider = new QSlider(Qt::Horizontal); contrast_slider->setMinimum(0); contrast_slider->setMaximum(255); contrast_slider->setValue(127); - - task_tab2->addWidget(task_tab2_button1); + task_tab2->addWidget(task_tab2_stats); task_tab2->addWidget(new QLabel("Histogramm")); task_tab2->addWidget(histogramm_label); @@ -344,7 +382,7 @@ void ImageViewer::generateControlPanels() { /**************************************************************************************** * -* The following methods are about the generic image viewer application and do not contain +* The following methods are about the adjustBrightness(int)generic image viewer application and do not contain * any special graphics logic. * *****************************************************************************************/ @@ -442,8 +480,13 @@ void ImageViewer::open() { if(image->isNull()) { QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName)); return; + } else { + if(original_image!=NULL) { + delete original_image; + original_image = NULL; + } + original_image = new QImage(fileName); } - scaleFactor = 1.0; updateImageDisplay(); printAct->setEnabled(true); @@ -453,6 +496,7 @@ void ImageViewer::open() { setWindowFilePath(fileName); logFile << "geladen: " << fileName.toStdString().c_str() << std::endl; renewLogging(); + initializeImage(); //Now hook to prepare certain datastructures after loading. } } diff --git a/imageviewer-qt4.h b/imageviewer-qt4.h index a4e5c4b..e108d64 100644 --- a/imageviewer-qt4.h +++ b/imageviewer-qt4.h @@ -51,6 +51,7 @@ #include #include "fstream" +#include class QAction; class QLabel; @@ -82,7 +83,6 @@ class ImageViewer : public QMainWindow { QWidget* task_tab_widget2; QVBoxLayout* task_tab2; - QPushButton* task_tab2_button1; QLabel* task_tab2_stats; QLabel* histogramm_label; @@ -95,8 +95,9 @@ class ImageViewer : public QMainWindow { // "My" space for storing data/results int grayscale_absolute_histogramm[256]; - double grayscale_relative_histogramm[256]; + double grayscale_relative_histogramm[256]; QImage* histogramm; + QImage* original_image; private slots: // Custom slots @@ -106,6 +107,7 @@ class ImageViewer : public QMainWindow { void acidTrippin(); void analyzeImage(); void updateHistogramm(); + void adjustBrightness(int b); void open(); void print(); @@ -128,7 +130,7 @@ class ImageViewer : public QMainWindow { void drawRainbowCross(int h); int getAverageIntensity(); int getIntensityVariance(); - + void generateControlPanels(); void startLogging(); void generateMainGui(); @@ -138,6 +140,7 @@ class ImageViewer : public QMainWindow { void scaleImage(double factor); void adjustScrollBar(QScrollBar *scrollBar, double factor); + void initializeImage(); void renewLogging(); QTabWidget* tabWidget;