task 8.2
This commit is contained in:
@@ -102,4 +102,48 @@ object ImageFilters {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
def getAvgInPixel(a: Array[Array[Color]], pixelSize: Int, px: Int, py: Int): Color = {
|
||||
val width: Int = a.length
|
||||
val height: Int = if (width == 0) 0 else a(0).length
|
||||
var r: Double = 0
|
||||
var g: Double = 0
|
||||
var b: Double = 0
|
||||
var cnt: Int = 0
|
||||
for (x: Int <- px*pixelSize until (px+1)*pixelSize) {
|
||||
for (y: Int <- py*pixelSize until (py+1)*pixelSize) {
|
||||
if (x < width && y < height) {
|
||||
cnt += 1
|
||||
r += a(x)(y).getRed
|
||||
g += a(x)(y).getGreen
|
||||
b += a(x)(y).getBlue
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Color((r/cnt).toInt, (g/cnt).toInt, (b/cnt).toInt)
|
||||
}
|
||||
def pixelize(a: Array[Array[Color]], pixelSize: Int): Array[Array[Color]] = {
|
||||
val width: Int = a.length
|
||||
val height: Int = if (width == 0) 0 else a(0).length
|
||||
val hPixels: Int = Math.ceil(width.toDouble/pixelSize).toInt
|
||||
val vPixels: Int = Math.ceil(height.toDouble/pixelSize).toInt
|
||||
val pixels: Array[Array[Color]] = Array.ofDim[Color](hPixels, vPixels)
|
||||
for (x: Int <- 0 until hPixels) {
|
||||
for (y: Int <- 0 until vPixels) {
|
||||
pixels(x)(y) = getAvgInPixel(a, pixelSize, x, y)
|
||||
}
|
||||
}
|
||||
|
||||
colorFilter(a, (col, x, y, w, h) => pixels(x/pixelSize)(y/pixelSize))
|
||||
}
|
||||
|
||||
def pixelize(a: Array[Array[Color]], pixelSize: Int, x1: Int, y1: Int, x2: Int, y2: Int): Array[Array[Color]] = {
|
||||
val pixelated: Array[Array[Color]] = pixelize(a, pixelSize)
|
||||
colorFilter(a, (col, x, y, w, h) => {
|
||||
if (x1 <= x && x <= x2 && y1 <= y && y <= y2) {
|
||||
pixelated(x)(y)
|
||||
}
|
||||
else col
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user