From 683fbd912fd7c4bb2f9fe805ecbc6f761f58bb9f Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 13 Dec 2023 08:25:41 +0100 Subject: [PATCH] made mask stretch to image size --- src/imagefilters/ImageFilters.scala | 17 ++++++++--------- src/imagefilters/ImageProcessingApp.scala | 3 +++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/imagefilters/ImageFilters.scala b/src/imagefilters/ImageFilters.scala index ec66cb9..a2be426 100644 --- a/src/imagefilters/ImageFilters.scala +++ b/src/imagefilters/ImageFilters.scala @@ -115,15 +115,14 @@ object ImageFilters { val maskHeight: Int = if (maskWidth == 0) 0 else maskImg(0).length colorFilter(a, (col, x, y, width, height) => { - if (x >= maskWidth || y >= maskHeight) col - else { - val factor: Double = maskImg(x)(y)/255.0 - new Color( - (col.getRed * factor).toInt, - (col.getGreen * factor).toInt, - (col.getBlue * factor).toInt - ) - } + val maskX: Int = (x.toDouble / width * maskWidth).toInt + val maskY: Int = (y.toDouble / height * maskHeight).toInt + val factor: Double = maskImg(maskX)(maskY)/255.0 + new Color( + (col.getRed * factor).toInt, + (col.getGreen * factor).toInt, + (col.getBlue * factor).toInt + ) }) } diff --git a/src/imagefilters/ImageProcessingApp.scala b/src/imagefilters/ImageProcessingApp.scala index 32977bc..0898d8b 100644 --- a/src/imagefilters/ImageProcessingApp.scala +++ b/src/imagefilters/ImageProcessingApp.scala @@ -56,6 +56,9 @@ object ImageProcessingApp extends App { val org = new ImageGraphics(imageFile, "Original", -768, -512) val sepia = new ImageGraphics(imageFile, "Sepia", -256, -512) val noise = new ImageGraphics(imageFile, "Noise", 256, -512) + val mask = new ImageGraphics("./res/mask.png", "Mask", -256, -256) + val masked = new ImageGraphics(imageFile, "Masked", 256, -256) sepia.setPixelsColor(ImageFilters.sepia(org.getPixelsColor())) noise.setPixelsColor(ImageFilters.noise(org.getPixelsColor(), 50)) + masked.setPixelsColor(ImageFilters.mask(org.getPixelsColor(), mask.getPixelsBW())) }