diff app/src/main/java/com/bartsent/simpleresizer/lib/Resizer.kt @ 18:eedf995462d9 concur2

Parallalized, but ConstraintLayout started hosing the edit window.
author David Barts <n5jrn@me.com>
date Mon, 22 Feb 2021 07:04:31 -0800
parents 86740f593b6c
children 6607f675a5f7
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/lib/Resizer.kt	Sun Feb 21 22:14:07 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/lib/Resizer.kt	Mon Feb 22 07:04:31 2021 -0800
@@ -2,6 +2,8 @@
 
 import android.graphics.Bitmap
 import android.graphics.Color
+import android.util.Log
+import java.util.concurrent.Callable
 import kotlin.math.ceil
 import kotlin.math.floor
 
@@ -111,9 +113,24 @@
         target[i] = Color.argb(clamp(a), clamp(r/a), clamp(g/a), clamp(b/a))
     }
 
-    private fun resampleBand(target: Band, weights: Array<Array<IndexWeight>>, source: Band) {
-        for (i in 0 until target.size) {
-            resample(target, i, weights[i], source)
+    private fun makeResampler(target: Band, weights: Array<Array<IndexWeight>>, source: Band): Callable<Exception?> {
+        return Callable<Exception?> {
+            try {
+                for (i in 0 until target.size) {
+                    resample(target, i, weights[i], source)
+                }
+                null
+            } catch (e: Exception) {
+                e
+            }
+        }
+    }
+
+    private fun runAndReap(jobs: ArrayList<Callable<Exception?>>) {
+        ThreadPools.WORKERS.invokeAll(jobs).forEach {
+            val exc = it.get()
+            if (exc != null)
+                Log.e("Resizer", "worker thread threw exception", exc)
         }
     }
 
@@ -126,9 +143,11 @@
     fun horizontal(newWidth: Int, kernel: ScalingKernel): Resizer {
         val dst = Resizer(newWidth, height)
         val weights = precomputeWeights(newWidth, width, kernel)
+        val jobs = ArrayList<Callable<Exception?>>(height)
         for (y in 0 until height) {
-            resampleBand(Row(dst, y), weights, Row(this, y))
+            jobs.add(makeResampler(Row(dst, y), weights, Row(this, y)))
         }
+        runAndReap(jobs)
         return dst
     }
 
@@ -141,9 +160,11 @@
     fun vertical(newHeight: Int, kernel: ScalingKernel): Resizer {
         val dst = Resizer(width, newHeight)
         val weights = precomputeWeights(newHeight, height, kernel)
+        val jobs = ArrayList<Callable<Exception?>>(width)
         for (x in 0 until width) {
-            resampleBand(Column(dst, x), weights, Column(this, x))
+            jobs.add(makeResampler(Column(dst, x), weights, Column(this, x)))
         }
+        runAndReap(jobs)
         return dst
     }
 }
\ No newline at end of file