[TASK] Implement brightness slider logic.

This commit is contained in:
Jan Philipp Timme 2015-11-06 13:49:02 +01:00
parent 352fc1cf80
commit 99f6b596a1
2 changed files with 65 additions and 18 deletions

View File

@ -45,8 +45,9 @@
* @brief ImageViewer::ImageViewer * @brief ImageViewer::ImageViewer
*/ */
ImageViewer::ImageViewer() { ImageViewer::ImageViewer() {
image=NULL; original_image = NULL;
histogramm=NULL; image = NULL;
histogramm = NULL;
startLogging(); startLogging();
generateMainGui(); generateMainGui();
renewLogging(); renewLogging();
@ -56,6 +57,23 @@ ImageViewer::ImageViewer() {
resize(1600, 600); 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. * Draws a simple, black line.
* @brief ImageViewer::drawBlackLine * @brief ImageViewer::drawBlackLine
@ -176,7 +194,7 @@ void ImageViewer::analyzeImage() {
logFile << "done" << std::endl; logFile << "done" << std::endl;
renewLogging(); 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); task_tab2_stats->setText(result);
updateHistogramm(); updateHistogramm();
@ -244,6 +262,30 @@ void ImageViewer::updateHistogramm() {
histogramm_label->setPixmap(QPixmap::fromImage(*histogramm)); 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; x<image->width(); x++) {
for(int y=0; y<image->height(); 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. * GUI elements related to the tasks are set up here.
@ -295,13 +337,9 @@ void ImageViewer::generateControlPanels() {
task_tab2 = new QVBoxLayout(); task_tab2 = new QVBoxLayout();
task_tab_widget2->setLayout(task_tab2); 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 = new QLabel();
histogramm_label->setBackgroundRole(QPalette::Base); histogramm_label->setBackgroundRole(QPalette::Base);
histogramm_label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); histogramm_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
histogramm_label->setScaledContents(false); histogramm_label->setScaledContents(false);
task_tab2_stats = new QLabel(); task_tab2_stats = new QLabel();
@ -309,15 +347,15 @@ void ImageViewer::generateControlPanels() {
brightness_slider = new QSlider(Qt::Horizontal); brightness_slider = new QSlider(Qt::Horizontal);
brightness_slider->setMinimum(0); brightness_slider->setMinimum(0);
brightness_slider->setMaximum(255); brightness_slider->setMaximum(510);
brightness_slider->setValue(127); brightness_slider->setValue(255);
QObject::connect(brightness_slider, SIGNAL(valueChanged(int)), this, SLOT (adjustBrightness(int)));
contrast_slider = new QSlider(Qt::Horizontal); contrast_slider = new QSlider(Qt::Horizontal);
contrast_slider->setMinimum(0); contrast_slider->setMinimum(0);
contrast_slider->setMaximum(255); contrast_slider->setMaximum(255);
contrast_slider->setValue(127); contrast_slider->setValue(127);
task_tab2->addWidget(task_tab2_button1);
task_tab2->addWidget(task_tab2_stats); task_tab2->addWidget(task_tab2_stats);
task_tab2->addWidget(new QLabel("Histogramm")); task_tab2->addWidget(new QLabel("Histogramm"));
task_tab2->addWidget(histogramm_label); 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. * any special graphics logic.
* *
*****************************************************************************************/ *****************************************************************************************/
@ -442,8 +480,13 @@ void ImageViewer::open() {
if(image->isNull()) { if(image->isNull()) {
QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName)); QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
return; return;
} else {
if(original_image!=NULL) {
delete original_image;
original_image = NULL;
}
original_image = new QImage(fileName);
} }
scaleFactor = 1.0; scaleFactor = 1.0;
updateImageDisplay(); updateImageDisplay();
printAct->setEnabled(true); printAct->setEnabled(true);
@ -453,6 +496,7 @@ void ImageViewer::open() {
setWindowFilePath(fileName); setWindowFilePath(fileName);
logFile << "geladen: " << fileName.toStdString().c_str() << std::endl; logFile << "geladen: " << fileName.toStdString().c_str() << std::endl;
renewLogging(); renewLogging();
initializeImage(); //Now hook to prepare certain datastructures after loading.
} }
} }

View File

@ -51,6 +51,7 @@
#include <cmath> #include <cmath>
#include "fstream" #include "fstream"
#include <iostream>
class QAction; class QAction;
class QLabel; class QLabel;
@ -82,7 +83,6 @@ class ImageViewer : public QMainWindow {
QWidget* task_tab_widget2; QWidget* task_tab_widget2;
QVBoxLayout* task_tab2; QVBoxLayout* task_tab2;
QPushButton* task_tab2_button1;
QLabel* task_tab2_stats; QLabel* task_tab2_stats;
QLabel* histogramm_label; QLabel* histogramm_label;
@ -97,6 +97,7 @@ class ImageViewer : public QMainWindow {
int grayscale_absolute_histogramm[256]; int grayscale_absolute_histogramm[256];
double grayscale_relative_histogramm[256]; double grayscale_relative_histogramm[256];
QImage* histogramm; QImage* histogramm;
QImage* original_image;
private slots: private slots:
// Custom slots // Custom slots
@ -106,6 +107,7 @@ class ImageViewer : public QMainWindow {
void acidTrippin(); void acidTrippin();
void analyzeImage(); void analyzeImage();
void updateHistogramm(); void updateHistogramm();
void adjustBrightness(int b);
void open(); void open();
void print(); void print();
@ -138,6 +140,7 @@ class ImageViewer : public QMainWindow {
void scaleImage(double factor); void scaleImage(double factor);
void adjustScrollBar(QScrollBar *scrollBar, double factor); void adjustScrollBar(QScrollBar *scrollBar, double factor);
void initializeImage();
void renewLogging(); void renewLogging();
QTabWidget* tabWidget; QTabWidget* tabWidget;