From 0657c28d7d29cb6ea2e200cd0e60ac2840dde514 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 5 Dec 2023 00:03:53 +0100 Subject: [PATCH] task 8.3 --- src/imagefilters/ImageFilters.scala | 29 +++++++++++++++++++++++ src/imagefilters/ImageProcessingApp.scala | 9 +++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/imagefilters/ImageFilters.scala b/src/imagefilters/ImageFilters.scala index 67c4503..19818b7 100644 --- a/src/imagefilters/ImageFilters.scala +++ b/src/imagefilters/ImageFilters.scala @@ -6,6 +6,19 @@ import java.awt.Color * This class implements the various image filters */ object ImageFilters { + // Default factors (reference values) + /*val SEPIA_FACTORS: Array[Array[Double]] = Array( + Array( 0.393, 0.769, 0.189), + Array( 0.349, 0.686, 0.168), + Array( 0.272, 0.534, 0.131) + )*/ + + // Adapted factors (matches the example in the instructions) + val SEPIA_FACTORS: Array[Array[Double]] = Array( + Array( 0.883, 0.004, 0.003), + Array(-0.058, 0.791,-0.097), + Array( 0.034,-0.244, 0.583) + ) def filter(src: Array[Array[Int]], func: (Int, Int, Int, Int, Int) => Int): Array[Array[Int]] = { val width: Int = src.length @@ -146,4 +159,20 @@ object ImageFilters { else col }) } + + def sepia(a: Array[Array[Color]]): Array[Array[Color]] = colorFilter(a, (col) => { + val r: Double = Math.max(0, Math.min(255, col.getRed * SEPIA_FACTORS(0)(0) + + col.getGreen * SEPIA_FACTORS(0)(1) + + col.getBlue * SEPIA_FACTORS(0)(2))) + + val g: Double = Math.max(0, Math.min(255, col.getRed * SEPIA_FACTORS(1)(0) + + col.getGreen * SEPIA_FACTORS(1)(1) + + col.getBlue * SEPIA_FACTORS(1)(2))) + + val b: Double = Math.max(0, Math.min(255, col.getRed * SEPIA_FACTORS(2)(0) + + col.getGreen * SEPIA_FACTORS(2)(1) + + col.getBlue * SEPIA_FACTORS(2)(2))) + + new Color(r.toInt, g.toInt, b.toInt) + }) } diff --git a/src/imagefilters/ImageProcessingApp.scala b/src/imagefilters/ImageProcessingApp.scala index 2cdff3b..f75cbb8 100644 --- a/src/imagefilters/ImageProcessingApp.scala +++ b/src/imagefilters/ImageProcessingApp.scala @@ -47,8 +47,13 @@ object ImageProcessingApp extends App { val masked = new ImageGraphics(imageFile, "Masked", 256, -512) masked.setPixelsColor(ImageFilters.mask(org.getPixelsColor(), mask.getPixelsBW()))*/ - val imageFile: String = "./res/grace_hopper.jpg" + /*val imageFile: String = "./res/grace_hopper.jpg" val org = new ImageGraphics(imageFile, "Original", -768, -512) val pixelated = new ImageGraphics(imageFile, "Pixelated", -256, -512) - pixelated.setPixelsColor(ImageFilters.pixelize(org.getPixelsColor(), 30, 130, 120, 400, 230)) + pixelated.setPixelsColor(ImageFilters.pixelize(org.getPixelsColor(), 30, 130, 120, 400, 230))*/ + + val imageFile: String = "./res/collins_eileen.png" + val org = new ImageGraphics(imageFile, "Original", -768, -512) + val sepia = new ImageGraphics(imageFile, "Sepia", -256, -512) + sepia.setPixelsColor(ImageFilters.sepia(org.getPixelsColor())) }