From f1e4e73833d1215bfacaea4993d630b16b521556 Mon Sep 17 00:00:00 2001 From: Damian Kowalski Date: Sat, 6 Apr 2024 21:28:58 +0200 Subject: [PATCH] histogram --- src/core/histogram.cpp | 53 +++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/src/core/histogram.cpp b/src/core/histogram.cpp index 1287666..bef28e1 100644 --- a/src/core/histogram.cpp +++ b/src/core/histogram.cpp @@ -22,21 +22,56 @@ Histogram::~Histogram() delete L; } -void Histogram::generate(QImage* image) -{ - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; +void Histogram::generate(QImage* image) { + R->clear(); + G->clear(); + B->clear(); + L->clear(); + + if (image == nullptr) { + qDebug() << "Invalid image provided."; + return; + } + + for (int y = 0; y < image->height(); ++y) { + for (int x = 0; x < image->width(); ++x) { + QColor pixelColor = image->pixelColor(x, y); + + R->insert(pixelColor.red(), R->value(pixelColor.red()) + 1); + G->insert(pixelColor.green(), G->value(pixelColor.green()) + 1); + B->insert(pixelColor.blue(), B->value(pixelColor.blue()) + 1); + + int luminance = qRound(0.299 * pixelColor.red() + 0.587 * pixelColor.green() + 0.114 * pixelColor.blue()); + L->insert(luminance, L->value(luminance) + 1); + } + } } -/** Returns the maximal value of the histogram in the given channel */ -int Histogram::maximumValue(Channel selectedChannel = RGB) +int Histogram::maximumValue(Channel selectedChannel) { - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + int maxVal = 0; - return 0; + if (selectedChannel == RGB) { + int maxR = maximumValue(RChannel); + int maxG = maximumValue(GChannel); + int maxB = maximumValue(BChannel); + maxVal = std::max(maxR, std::max(maxG, maxB)); + } + else { + QHash* hist = get(selectedChannel); + + QHash::const_iterator cit = hist->constBegin(); + while (cit != hist->constEnd()) { + if (cit.value() > maxVal) { + maxVal = cit.value(); + } + ++cit; + } + } + + return maxVal; } - -/** Returns a pointer to the given channel QHash */ QHash* Histogram::get(Channel channel = LChannel) { if (channel==LChannel) return L;