diff --git a/src/core/transformations/convolution.cpp b/src/core/transformations/convolution.cpp index 72be966..108ee17 100644 --- a/src/core/transformations/convolution.cpp +++ b/src/core/transformations/convolution.cpp @@ -1,5 +1,7 @@ #include "convolution.h" +#include + /** Overloaded constructor */ Convolution::Convolution(PNM* img) : Transformation(img) @@ -39,6 +41,10 @@ math::matrix Convolution::getMask(int size, Mode mode = Normalize) return mask; } +int Convolution::clamp(int value, int min, int max) { + return (value < min) ? min : (value > max) ? max : value; +} + /** Does the convolution process for all pixels using the given mask. */ PNM* Convolution::convolute(math::matrix mask, Mode mode = RepeatEdge) { @@ -47,7 +53,40 @@ PNM* Convolution::convolute(math::matrix mask, Mode mode = RepeatEdge) PNM* newImage = new PNM(width, height, image->format()); - qDebug() << Q_FUNC_INFO << "Not implemented yet!"; + // Calcularing the mask weight + float weight = Convolution::sum(mask); + + // Going through with 'the mask' with all the pixels + for (int x=0; x < width ; x++) + for (int y=0; y < height; y++) + { + math::matrix reflection = Convolution::reflection(mask); + + // Calculate accumulator + math::matrix redAcc = Convolution::join(getWindow(x, y, mask.rowno(), Transformation::RChannel, mode), reflection); + math::matrix greenAcc = Convolution::join(getWindow(x, y, mask.rowno(), Transformation::GChannel, mode), reflection); + math::matrix blueAcc = Convolution::join(getWindow(x, y, mask.rowno(), Transformation::BChannel, mode), reflection); + + // Calculate accumulator sum + float redAccSum = Convolution::sum(redAcc); + float greenAccSum = Convolution::sum(greenAcc); + float blueAccSum = Convolution::sum(blueAcc); + + if (weight != 0) + { + redAccSum = redAccSum / weight; + greenAccSum = greenAccSum / weight; + blueAccSum = blueAccSum / weight; + } + + redAccSum = clamp(redAccSum, 0, 255); + greenAccSum = clamp(greenAccSum, 0, 255); + blueAccSum = clamp(blueAccSum, 0, 255); + + // Set new pixel value + QColor newPixel = QColor(redAccSum, greenAccSum, blueAccSum); + newImage->setPixel(x,y, newPixel.rgb()); + } return newImage; } diff --git a/src/core/transformations/convolution.h b/src/core/transformations/convolution.h index f7f1994..77d82e4 100644 --- a/src/core/transformations/convolution.h +++ b/src/core/transformations/convolution.h @@ -15,6 +15,7 @@ public: virtual PNM* transform(); protected: + int clamp(int value, int min, int max); const math::matrix join(math::matrix, math::matrix); const float sum(math::matrix); const math::matrix reflection(math::matrix);