diff --git a/src/imagefilters/ImageFilters.scala b/src/imagefilters/ImageFilters.scala index f9d4691..de946c6 100644 --- a/src/imagefilters/ImageFilters.scala +++ b/src/imagefilters/ImageFilters.scala @@ -4,18 +4,33 @@ package imagefilters * This class implements the various image filters */ object ImageFilters { - def filter(src: Array[Array[Int]], func: (Int, Int, Int) => Int): Array[Array[Int]] = { + + def filter(src: Array[Array[Int]], func: (Int, Int, 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) { - dst(y)(x) = func(src(y)(x), x, y) + dst(y)(x) = func(src(y)(x), x, y, width, height) } } return dst } + def filter(src: Array[Array[Int]], func: (Int, Int, Int) => Int): Array[Array[Int]] = filter(src, (value, x, y, width, height) => func(value, x, y)) + def filter(src: Array[Array[Int]], func: (Int) => Int): Array[Array[Int]] = filter(src, (value, x, y, width, height) => func(value)) - 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) + def duplicate(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value) => value) + def threshold(a: Array[Array[Int]], thresh: Int): Array[Array[Int]] = filter(a, (value) => if (value > thresh) 255 else 0) + def mean(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value: Int, x, y, width, height) => { + if (x == 0 || x == width - 1 || y == 0 || y == height - 1) value + else { + var avg: Double = 0 + for (dy: Int <- -1 to 1) { + for (dx: Int <- -1 to 1) { + avg += a(y+dy)(x+dx) + } + } + (avg/9.0).toInt + } + }) } diff --git a/src/imagefilters/ImageProcessingApp.scala b/src/imagefilters/ImageProcessingApp.scala index 70a75e1..e92af5d 100644 --- a/src/imagefilters/ImageProcessingApp.scala +++ b/src/imagefilters/ImageProcessingApp.scala @@ -14,4 +14,7 @@ object ImageProcessingApp extends App { dest.setPixelsBW(newPixels) thresh.setPixelsBW(ImageFilters.threshold(newPixels, 128)) + + val mean = new ImageGraphics("./res/imageProcessing.jpg", "Mean", 0, 250) + mean.setPixelsBW(ImageFilters.mean(mean.getPixelsBW())) }