diff --git a/src/main/scala/scalashop/HorizontalBoxBlur.scala b/src/main/scala/scalashop/HorizontalBoxBlur.scala index 5022bae..bcc2e1c 100644 --- a/src/main/scala/scalashop/HorizontalBoxBlur.scala +++ b/src/main/scala/scalashop/HorizontalBoxBlur.scala @@ -41,8 +41,11 @@ object HorizontalBoxBlur extends HorizontalBoxBlurInterface { */ def blur(src: Img, dst: Img, from: Int, end: Int, radius: Int): Unit = { // TODO implement this method using the `boxBlurKernel` method - - ??? + for(y <- from until end){ + for(x <- 0 until src.width){ + dst(x,y) = boxBlurKernel(src, x, y, radius) + } + } } /** Blurs the rows of the source image in parallel using `numTasks` tasks. @@ -53,8 +56,10 @@ object HorizontalBoxBlur extends HorizontalBoxBlurInterface { */ def parBlur(src: Img, dst: Img, numTasks: Int, radius: Int): Unit = { // TODO implement using the `task` construct and the `blur` method - - ??? + val r = 0 to src.height by (src.height/(Math.min(numTasks, src.height))) + var ranges = r zip r.tail + val tasks = ranges.map( { case (from, to) => task(blur(src, dst, from, to, radius)) } ) + tasks foreach {_.join} } } diff --git a/src/main/scala/scalashop/VerticalBoxBlur.scala b/src/main/scala/scalashop/VerticalBoxBlur.scala index f406e40..25de78f 100644 --- a/src/main/scala/scalashop/VerticalBoxBlur.scala +++ b/src/main/scala/scalashop/VerticalBoxBlur.scala @@ -43,7 +43,11 @@ object VerticalBoxBlur extends VerticalBoxBlurInterface { */ def blur(src: Img, dst: Img, from: Int, end: Int, radius: Int): Unit = { // TODO implement this method using the `boxBlurKernel` method - ??? + for(x <- from until end){ + for(y <- 0 until src.height){ + dst(x,y) = boxBlurKernel(src, x, y, radius) + } + } } /** Blurs the columns of the source image in parallel using `numTasks` tasks. @@ -54,7 +58,10 @@ object VerticalBoxBlur extends VerticalBoxBlurInterface { */ def parBlur(src: Img, dst: Img, numTasks: Int, radius: Int): Unit = { // TODO implement using the `task` construct and the `blur` method - ??? + val r = 0 to src.width by (src.width/(Math.min(numTasks, src.width))) + var ranges = r zip r.tail + val tasks = ranges.map( { case (from, to) => task(blur(src, dst, from, to, radius)) } ) + tasks foreach {_.join} } } diff --git a/src/main/scala/scalashop/package.scala b/src/main/scala/scalashop/package.scala index ae29e42..2525021 100644 --- a/src/main/scala/scalashop/package.scala +++ b/src/main/scala/scalashop/package.scala @@ -43,7 +43,28 @@ package object scalashop extends BoxBlurKernelInterface { def boxBlurKernel(src: Img, x: Int, y: Int, radius: Int): RGBA = { // TODO implement using while loops - ??? + var r = 0 + var g = 0 + var b = 0 + var a = 0 + + var count = 0 + + var xi = clamp(x-radius, 0, src.width-1) + while(xi<=clamp(x+radius, 0, src.width-1)) { + var yi = clamp(y-radius, 0, src.height-1) + while(yi<=clamp(y+radius, 0, src.height-1)) { + r = r + red(src(xi, yi)) + g = g + green(src(xi, yi)) + b = b + blue(src(xi, yi)) + a = a + alpha(src(xi, yi)) + count = count + 1 + yi = yi + 1 + } + xi = xi + 1 + } + + rgba(r/count, g/count, b/count, a/count) } val forkJoinPool = new ForkJoinPool