From 9454948799385269af003ee4324f1d12f4ffde16 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 4 Dec 2023 15:49:12 +0100 Subject: [PATCH] task 3 --- src/imagefilters/ImageFilters.scala | 16 +++++++++------- src/imagefilters/ImageProcessingApp.scala | 3 +++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/imagefilters/ImageFilters.scala b/src/imagefilters/ImageFilters.scala index 18c71c1..f9d4691 100644 --- a/src/imagefilters/ImageFilters.scala +++ b/src/imagefilters/ImageFilters.scala @@ -4,16 +4,18 @@ package imagefilters * This class implements the various image filters */ object ImageFilters { - - def duplicate(a: Array[Array[Int]]): Array[Array[Int]] = { - val height: Int = a.length - val width: Int = if (height == 0) 0 else a(0).length - val res: Array[Array[Int]] = Array.ofDim(height, width) + def filter(src: Array[Array[Int]], func: (Int, Int, Int) => Int): Array[Array[Int]] = { + val height: Int = src.length + val width: Int = if (height == 0) 0 else src(0).length + val dst: Array[Array[Int]] = Array.ofDim(height, width) for (y: Int <- 0 until height) { for (x: Int <- 0 until width) { - res(y)(x) = a(y)(x) + dst(y)(x) = func(src(y)(x), x, y) } } - return res + return dst } + + def duplicate(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value, x, y) => value) + def threshold(a: Array[Array[Int]], thresh: Int): Array[Array[Int]] = filter(a, (value, x, y) => if (value > thresh) 255 else 0) } diff --git a/src/imagefilters/ImageProcessingApp.scala b/src/imagefilters/ImageProcessingApp.scala index 3fcbaa0..70a75e1 100644 --- a/src/imagefilters/ImageProcessingApp.scala +++ b/src/imagefilters/ImageProcessingApp.scala @@ -7,8 +7,11 @@ object ImageProcessingApp extends App { val imageFile = "./res/collins_eileen.png" val org = new ImageGraphics(imageFile, "Original", -500, -250) val dest = new ImageGraphics(imageFile, "Duplicate", 0, -250) + val thresh = new ImageGraphics(imageFile, "Threshold", -500, 250) // Simple copy and display val newPixels = ImageFilters.duplicate(org.getPixelsBW()) dest.setPixelsBW(newPixels) + + thresh.setPixelsBW(ImageFilters.threshold(newPixels, 128)) }