From 718517590b1ba33b26ba4d6917fdb7fe79942c55 Mon Sep 17 00:00:00 2001 From: Damian Kowalski Date: Sat, 6 Apr 2024 22:57:33 +0200 Subject: [PATCH] histogram stretching --- .../transformations/histogram_stretching.cpp | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/core/transformations/histogram_stretching.cpp b/src/core/transformations/histogram_stretching.cpp index 25440de..b865773 100644 --- a/src/core/transformations/histogram_stretching.cpp +++ b/src/core/transformations/histogram_stretching.cpp @@ -14,14 +14,51 @@ HistogramStretching::HistogramStretching(PNM* img, ImageViewer* iv) : PNM* HistogramStretching::transform() { - int width = image->width(); + int width = image->width(); int height = image->height(); PNM* newImage = new PNM(width, height, image->format()); + Histogram* histogram = image->getHistogram(); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + int minValue = -1; + int maxValue = -1; + + for (int y = 0; y < image->height(); ++y) { + for (int x = 0; x < image->width(); ++x) { + if (histogram->get(Histogram::LChannel)->value(x + y * x) != 0) + { + minValue = histogram->get(Histogram::LChannel)->value(x + y * x); + break; + } + } + } + + for (int y = image->height(); y > 0; --y) { + for (int x = image->width(); x < 0; --x) { + if (histogram->get(Histogram::LChannel)->value(x + y * x) != 0) + { + minValue = histogram->get(Histogram::LChannel)->value(x + y * x); + break; + } + } + } + + for (int y = 0; y < image->height(); ++y) { + for (int x = 0; x < image->width(); ++x) { + QRgb pixel = image->pixel(x, y); + + int red = qRed(pixel); + int green = qGreen(pixel); + int blue = qBlue(pixel); + + int stretchedRed = (255 * (red - minValue)) / (maxValue - minValue); + int stretchedGreen = (255 * (green - minValue)) / (maxValue - minValue); + int stretchedBlue = (255 * (blue - minValue)) / (maxValue - minValue); + + newImage->setPixel(x, y, qRgb(stretchedRed, stretchedGreen, stretchedBlue)); + } + } return newImage; } -