diff app/src/main/java/com/bartsent/simpleresizer/lib/LanczosKernel.kt @ 17:86740f593b6c

Better memoization, more rational API.
author David Barts <n5jrn@me.com>
date Sun, 21 Feb 2021 22:14:07 -0800
parents b1605be35bcc
children 6607f675a5f7
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/lib/LanczosKernel.kt	Sun Feb 21 21:43:54 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/lib/LanczosKernel.kt	Sun Feb 21 22:14:07 2021 -0800
@@ -7,20 +7,22 @@
 class LanczosKernel: ScalingKernel {
     override val size = 3.0
     private val memory = HashMap<Double, Double>()
-    init {
-        memory.put(0.0, 1.0)
+
+    private fun sinc(x: Double): Double {
+        if (x == 0.0)
+            return 1.0
+        val pix = PI * x
+        return  sin(pix) / pix
     }
 
-    private fun sinc(x: Double): Double {
+    override fun weight(x: Double): Double {
+        if (abs(x) >= size)
+            return 0.0
         val remembered = memory[x]
         if (remembered != null)
             return remembered
-        val pix = PI * x
-        val calculated = sin(pix) / pix
+        val calculated = sinc(x) * sinc(x/size)
         memory[x] = calculated
         return calculated
     }
-
-    override fun weight(x: Double): Double =
-        if (abs(x) < size) sinc(x) * sinc(x/size) else 0.0
 }
\ No newline at end of file