task 8.1
This commit is contained in:
		| @@ -1,5 +1,7 @@ | ||||
| package imagefilters | ||||
|  | ||||
| import java.awt.Color | ||||
|  | ||||
| /** | ||||
|  * This class implements the various image filters | ||||
|  */ | ||||
| @@ -16,8 +18,19 @@ object ImageFilters { | ||||
|     } | ||||
|     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 colorFilter(src: Array[Array[Color]], func: (Color, Int, Int, Int, Int) => Color): Array[Array[Color]] = { | ||||
|     val height: Int = src.length | ||||
|     val width: Int = if (height == 0) 0 else src(0).length | ||||
|     val dst: Array[Array[Color]] = 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, width, height) | ||||
|       } | ||||
|     } | ||||
|     return dst | ||||
|   } | ||||
|   def colorFilter(src: Array[Array[Color]], func: (Color) => Color): Array[Array[Color]] = colorFilter(src, (value, x, y, width, height) => func(value)) | ||||
|  | ||||
|   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) | ||||
| @@ -72,4 +85,21 @@ object ImageFilters { | ||||
|   def noise(a: Array[Array[Int]], intensity: Double): Array[Array[Int]] = filter(a, (value) => { | ||||
|     Math.max(0, Math.min(255, value + (Math.random()*2-1)*intensity)).toInt | ||||
|   }) | ||||
|  | ||||
|   def mask(a: Array[Array[Color]], maskImg: Array[Array[Int]]): Array[Array[Color]] = { | ||||
|     val maskHeight: Int = maskImg.length | ||||
|     val maskWidth: Int = if (maskHeight == 0) 0 else maskImg(0).length | ||||
|  | ||||
|     colorFilter(a, (col, x, y, width, height) => { | ||||
|       if (x >= maskWidth || y >= maskHeight) col | ||||
|       else { | ||||
|         val factor: Double = maskImg(y)(x)/255.0 | ||||
|         new Color( | ||||
|           (col.getRed * factor).toInt, | ||||
|           (col.getGreen * factor).toInt, | ||||
|           (col.getBlue * factor).toInt | ||||
|         ) | ||||
|       } | ||||
|     }) | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user