Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
annotate 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 |
rev | line source |
---|---|
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
1 package com.bartsent.simpleresizer.lib |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
2 |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
3 import kotlin.math.PI |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
4 import kotlin.math.abs |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
5 import kotlin.math.sin |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
6 |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
11
diff
changeset
|
7 class LanczosKernel: ScalingKernel { |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 override val size = 3.0 |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
9 private val memory = HashMap<Double, Double>() |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
10 |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
11 private fun sinc(x: Double): Double { |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
12 if (x == 0.0) |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
13 return 1.0 |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
14 val pix = PI * x |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
15 return sin(pix) / pix |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
16 } |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
18 override fun weight(x: Double): Double { |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
19 if (abs(x) >= size) |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
20 return 0.0 |
11
678adef4774f
Move more stuff into worker threads.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
21 val remembered = memory[x] |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
22 if (remembered != null) |
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
23 return remembered |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
24 val calculated = sinc(x) * sinc(x/size) |
11
678adef4774f
Move more stuff into worker threads.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
25 memory[x] = calculated |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
26 return calculated |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 } |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
28 } |