Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
comparison app/src/main/java/com/bartsent/simpleresizer/lib/LanczosKernel.kt @ 8:6ae738b8a814 memo
Memoize. Only modest improvement results.
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 17 Feb 2021 07:43:50 -0800 |
parents | e8059b166de1 |
children | 678adef4774f |
comparison
equal
deleted
inserted
replaced
6:e8059b166de1 | 8:6ae738b8a814 |
---|---|
4 import kotlin.math.abs | 4 import kotlin.math.abs |
5 import kotlin.math.sin | 5 import kotlin.math.sin |
6 | 6 |
7 object LanczosKernel: ScalingKernel { | 7 object LanczosKernel: ScalingKernel { |
8 override val size = 3.0 | 8 override val size = 3.0 |
9 private val memory = HashMap<Double, Double>() | |
10 init { | |
11 memory.put(0.0, 1.0) | |
12 } | |
9 | 13 |
10 private fun sinc(x: Double): Double { | 14 private fun sinc(x: Double): Double { |
11 if (x == 0.0) | 15 val remembered = memory.get(x) |
12 return 1.0 | 16 if (remembered != null) |
17 return remembered | |
13 val pix = PI * x | 18 val pix = PI * x |
14 return sin(pix) / pix | 19 val calculated = sin(pix) / pix |
20 memory.put(x, calculated) | |
21 return calculated | |
15 } | 22 } |
16 | 23 |
17 override fun weight(x: Double): Double = | 24 override fun weight(x: Double): Double = |
18 if (abs(x) < size) sinc(x) * sinc(x/size) else 0.0 | 25 if (abs(x) < size) sinc(x) * sinc(x/size) else 0.0 |
19 } | 26 } |