diff --git a/src/core/transformations/histogram_stretching.cpp b/src/core/transformations/histogram_stretching.cpp index b865773..2a03e5d 100644 --- a/src/core/transformations/histogram_stretching.cpp +++ b/src/core/transformations/histogram_stretching.cpp @@ -20,42 +20,76 @@ PNM* HistogramStretching::transform() PNM* newImage = new PNM(width, height, image->format()); Histogram* histogram = image->getHistogram(); - int minValue = -1; - int maxValue = -1; + int maxRed = 255, maxGreen = 255, maxBlue = 255, maxGrey = 255; - 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; - } + int minRed = 0, minGreen = 0, minBlue = 0, minGrey = 0; + + QHash* R = histogram->get(histogram->RChannel); + QHash* G = histogram->get(histogram->GChannel); + QHash* B = histogram->get(histogram->BChannel); + QHash* L = histogram->get(histogram->LChannel); + + for (int i=0; i<256; i++) + { + if (R->value(minRed) == 0) { + minRed++; + } + + if (G->value(minGreen) == 0) { + minGreen++; + } + + if (B->value(minBlue) == 0) { + minBlue++; + } + + if (L->value(minGrey) == 0) { + minGrey++; } } - 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 i=256; i>0; i--) + { + if (R->value(maxRed) == 0) { + maxRed--; + } + + if (G->value(maxGreen) == 0) { + maxGreen--; + } + + if (B->value(maxBlue) == 0) { + maxBlue--; + } + + if (L->value(maxGrey) == 0) { + maxGrey--; } } - for (int y = 0; y < image->height(); ++y) { - for (int x = 0; x < image->width(); ++x) { - QRgb pixel = image->pixel(x, y); + for (int x = 0; x < image->width(); x++) { + for (int y = 0; y < image->height(); y++) { + QRgb p = image->pixel(x, y); - int red = qRed(pixel); - int green = qGreen(pixel); - int blue = qBlue(pixel); + if(image->format() == QImage::Format_RGB32) { + int red = qRed(p); + int green = qGreen(p); + int blue = qBlue(p); - 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)); + newImage->setPixel( + x, + y, + QColor( + (255 / (maxRed - minRed)) * (red - minRed), + (255 / (maxGreen - minGreen)) * (green - minGreen), + (255 / (maxBlue - minBlue)) * (blue - minBlue) + ).rgb()); + } else { + newImage->setPixel( + x, + y, + (255 / (maxGrey - minGrey)) * (qGray(p) - minGrey)); + } } }