This commit is contained in:
Louis Heredero 2023-12-04 15:49:12 +01:00
parent 16e6e51472
commit 9454948799
2 changed files with 12 additions and 7 deletions

View File

@ -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)
}

View File

@ -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))
}