[TASK] Decouple original and working copy, add reset button.

This commit is contained in:
Jan Philipp Timme 2015-11-13 15:08:51 +01:00
parent a6854a9625
commit c1f921d59f
2 changed files with 50 additions and 19 deletions

View File

@ -74,7 +74,6 @@ void ImageViewer::initializeImage() {
/**
* In case it is needed, convert the loaded image into grayscale.
* This is the only method modifying the original image!
*
* @brief ImageViewer::convertToMonochrome
*/
@ -82,8 +81,7 @@ void ImageViewer::convertToMonochrome() {
if(original->getImage() != NULL) {
logFile << "Converting image to monochrome...";
renewLogging();
original->convertToMonochrome();
working_copy->convertToMonochrome();
updateImageDisplay();
@ -92,6 +90,25 @@ void ImageViewer::convertToMonochrome() {
}
}
/**
* No big performance, but simple.
* Restore the working copy based on the existing original image.
*
* @brief ImageViewer::resetWorkingCopy
*/
void ImageViewer::resetWorkingCopy() {
logFile << "Resetting working copy ...";
renewLogging();
for(int x=0; x<original->getImage()->width(); x++) {
for(int y=0; y<original->getImage()->height(); y++) {
working_copy->getImage()->setPixel(x, y, original->getImage()->pixel(x, y));
}
}
updateImageDisplay();
logFile << "done." << std::endl;
renewLogging();
}
/**
* Draws a simple, black line.
* @brief ImageViewer::drawBlackLine
@ -237,24 +254,31 @@ void ImageViewer::adjustContrast(int c) {
* @brief ImageViewer::robustAutomaticContrastAdaption
*/
void ImageViewer::robustAutomaticContrastAdaption(void) {
int percentile = contrast_percentile_slider->value();
logFile << "Doing automatic robust contrast adaption (" << percentile << "%), hold tight... " << std::endl;
double* histogramm = original->getRelativeIntensityHistogramm();
double limit = contrast_percentile_slider->value() / 100.0;
logFile << "Doing automatic robust contrast adaption (" << limit << "), hold tight ..." << std::endl;
renewLogging();
// Determine upper and lower "borders"
double cursor;
int lower_border = -1;
int upper_border = -1;
double limit = percentile / 100.0;
double cursor = 0.0;
double* grayscale_relative_histogramm = original->getRelativeIntensityHistogramm();
int upper_border = -1;
cursor = 0.0;
for(int i=0; i<256; i++) {
cursor += grayscale_relative_histogramm[i];
if(cursor >= limit && lower_border == -1) {
lower_border = i-1;
cursor += histogramm[i];
if(cursor > limit) {
lower_border = i-1; // get last value before limit overflow
if(lower_border < 0) lower_border = 0;
break;
}
if(cursor >= 1-limit && upper_border == -1) {
upper_border = i;
}
cursor = 0.0;
for(int i=255; i>-1; i--) {
cursor += histogramm[i];
if(cursor > limit) {
upper_border = i+1; // get last value before limit overflow
if(upper_border > 255) upper_border = 255;
break;
}
@ -292,7 +316,7 @@ void ImageViewer::robustAutomaticContrastAdaption(void) {
*
* GUI elements related to the tasks are set up here.
*
**********************contrast_percentile_slider*******************************************************************/
*****************************************************************************************/
void ImageViewer::generateControlPanels() {
// Tab for first task
@ -304,6 +328,10 @@ void ImageViewer::generateControlPanels() {
monochrome->setText("Convert to monochrome");
QObject::connect(monochrome, SIGNAL(clicked()), this, SLOT (convertToMonochrome()));
reset_working_copy = new QPushButton();
reset_working_copy->setText("Reset working copy");
QObject::connect(reset_working_copy, SIGNAL(clicked()), this, SLOT (resetWorkingCopy()));
draw_black_line = new QPushButton();
draw_black_line->setText("Draw a black line");
QObject::connect(draw_black_line, SIGNAL(clicked()), this, SLOT (drawBlackLine()));
@ -321,7 +349,8 @@ void ImageViewer::generateControlPanels() {
line_slider->setMaximum(150);
line_slider->setValue(3);
//QObject::connect(lineSlider, SIGNAL(valueChanged(int)), this, SLOT (drawRainbowCross()));
task_tab1->addWidget(reset_working_copy);
task_tab1->addWidget(monochrome);
task_tab1->addWidget(new QLabel("Let's draw something!"));
task_tab1->addWidget(draw_black_line);
@ -332,7 +361,7 @@ void ImageViewer::generateControlPanels() {
task_tab1->addWidget(new QLabel("Sets the width of the cross."));
tabWidget->addTab(task_tab_widget1, "Task #1");
//Tab for second task
task_tab_widget2 = new QWidget();
@ -341,7 +370,7 @@ void ImageViewer::generateControlPanels() {
histogramm_label = new QLabel();
histogramm_label->setBackgroundRole(QPalette::Base);
histogramm_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
histogramm_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
histogramm_label->setScaledContents(false);
stats = new QLabel();
@ -379,6 +408,7 @@ void ImageViewer::generateControlPanels() {
task_tab2->addWidget(brightness_slider);
task_tab2->addWidget(new QLabel("Contrast"));
task_tab2->addWidget(contrast_slider);
task_tab2->addWidget(new QLabel("Automatic Contrast Percentile"));
task_tab2->addWidget(contrast_percentile_slider);
task_tab2->addWidget(automatic_robust_contrast);

View File

@ -79,7 +79,7 @@ class ImageViewer : public QMainWindow {
QPushButton* draw_rainbow_cross;
QPushButton* diagonal_cross;
QPushButton* monochrome;
QPushButton* reset_working_copy;
QSlider* line_slider;
@ -114,6 +114,7 @@ class ImageViewer : public QMainWindow {
void adjustBrightness(int b);
void adjustContrast(int c);
void convertToMonochrome();
void resetWorkingCopy();
void robustAutomaticContrastAdaption();
void open();