Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
comparison 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 |
comparison
equal
deleted
inserted
replaced
16:3ed74dc0e34a | 17:86740f593b6c |
---|---|
5 import kotlin.math.sin | 5 import kotlin.math.sin |
6 | 6 |
7 class LanczosKernel: ScalingKernel { | 7 class LanczosKernel: ScalingKernel { |
8 override val size = 3.0 | 8 override val size = 3.0 |
9 private val memory = HashMap<Double, Double>() | 9 private val memory = HashMap<Double, Double>() |
10 init { | 10 |
11 memory.put(0.0, 1.0) | 11 private fun sinc(x: Double): Double { |
12 if (x == 0.0) | |
13 return 1.0 | |
14 val pix = PI * x | |
15 return sin(pix) / pix | |
12 } | 16 } |
13 | 17 |
14 private fun sinc(x: Double): Double { | 18 override fun weight(x: Double): Double { |
19 if (abs(x) >= size) | |
20 return 0.0 | |
15 val remembered = memory[x] | 21 val remembered = memory[x] |
16 if (remembered != null) | 22 if (remembered != null) |
17 return remembered | 23 return remembered |
18 val pix = PI * x | 24 val calculated = sinc(x) * sinc(x/size) |
19 val calculated = sin(pix) / pix | |
20 memory[x] = calculated | 25 memory[x] = calculated |
21 return calculated | 26 return calculated |
22 } | 27 } |
23 | |
24 override fun weight(x: Double): Double = | |
25 if (abs(x) < size) sinc(x) * sinc(x/size) else 0.0 | |
26 } | 28 } |