From b762a3a076a36d0b2d6aa2ad908fa5eee11d637f Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 11 Dec 2015 13:44:32 +0100 Subject: [PATCH] [TASK] Implement basic yuv handling. --- imageviewer-qt4.cpp | 17 ++++++++++------ lazy_image.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/imageviewer-qt4.cpp b/imageviewer-qt4.cpp index 6e6676e..e0906cc 100644 --- a/imageviewer-qt4.cpp +++ b/imageviewer-qt4.cpp @@ -235,11 +235,16 @@ void ImageViewer::adjustBrightness(int b) { for(int x=0; xgetImage()->width(); x++) { for(int y=0; ygetImage()->height(); y++) { QColor color = QColor::fromRgb(original->getImage()->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); +// 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); + struct yuv yuv = LazyImage::RGBToYUV(color.rgb()); + yuv.y = yuv.y + (delta / 255.0); + if(yuv.y > 1) yuv.y = 1; + if(yuv.y < 0) yuv.y = 0; + color = LazyImage::YUVToRGB(yuv); working_copy->getImage()->setPixel(x, y, color.rgb()); } } @@ -564,7 +569,7 @@ void ImageViewer::applyBasicFilter() { */ void ImageViewer::applyBasicFilterWithOverflowHandling() { QString chosen_mode = filter_mode_combobox->currentText(); - LazyImage::overflowMode overflow_mode; + LazyImage::overflowMode overflow_mode = LazyImage::CONSTANT; // default for compiler if(chosen_mode == "Zero padding") overflow_mode = LazyImage::ZERO_PADDING; else if(chosen_mode == "Constant") overflow_mode = LazyImage::CONSTANT; else if(chosen_mode == "Continuous") overflow_mode = LazyImage::CONTINUOUS; diff --git a/lazy_image.cpp b/lazy_image.cpp index b650411..432c163 100644 --- a/lazy_image.cpp +++ b/lazy_image.cpp @@ -9,6 +9,13 @@ #include +struct yuv { + // values in [0,1] + double y; // brightness + double u; // red/green part + double v; // green/blue part +}; + class LazyImage { private: @@ -255,6 +262,48 @@ class LazyImage { return this->img->pixel(x, y); } }; + + static struct yuv RGBToYUV(QColor input) { + struct yuv result; + int input_r, input_g, input_b; + input.getRgb(&input_r, &input_g, &input_b); + //std::cout << input_r << "," << input_g << "," << input_b << " (rgb) -> (yuv) "; + // normalize input rgb to values in [0,1] + double r, g, b; + r = input_r / 255.0; + g = input_g / 255.0; + b = input_b / 255.0; + // calculate yuv + result.y = 0.299 * r + 0.587 * g + 0.114 * b; + result.u = (b - result.y) * 0.493; + result.v = (r - result.y) * 0.877; + //std::cout << result.y << "," << result.u << "," << result.v << std::endl; + return result; + }; + + static QColor YUVToRGB(struct yuv input) { + double r, g, b; + // calculate rgb + //std::cout << input.y << "," << input.u << "," << input.v << " (yuv) -> (rgb) "; + b = (input.u / 0.493) + input.y; + r = (input.v / 0.877) + input.y; + // möp g = (input.y - 0.299*r - 0.144*b) / 0.587; + g = (1.0/0.587)*input.y - (0.299/0.587)*r - (0.114/0.585)*b; + // normalize into [0,255] + int output_r, output_g, output_b; + output_r = qRound(r * 255.0); + output_g = qRound(g * 255.0); + output_b = qRound(b * 255.0); + if(output_r < 0) output_r = 0; + if(output_g < 0) output_g = 0; + if(output_b < 0) output_b = 0; + if(output_r > 255) output_r = 255; + if(output_g > 255) output_g = 255; + if(output_b > 255) output_b = 255; + //std::cout << output_r << "," << output_g << "," << output_b << std::endl << std::endl; + QColor result = QColor(output_r, output_g, output_b); + return result; + }; };