Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
annotate app/src/main/java/com/bartsent/simpleresizer/lib/LanczosKernel.kt @ 35:6607f675a5f7
Add licensing.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 11 Mar 2021 22:41:48 -0800 |
parents | 86740f593b6c |
children |
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 |
35 | 3 /* |
4 * Some code in this file has been adapted from: https://github.com/disintegration/imaging/ . | |
5 */ | |
6 | |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
7 import kotlin.math.PI |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 import kotlin.math.abs |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 import kotlin.math.sin |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
11
diff
changeset
|
11 class LanczosKernel: ScalingKernel { |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
12 override val size = 3.0 |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
13 private val memory = HashMap<Double, Double>() |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
14 |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
15 private fun sinc(x: Double): Double { |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
16 if (x == 0.0) |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
17 return 1.0 |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
18 val pix = PI * x |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
19 return sin(pix) / pix |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
20 } |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
22 override fun weight(x: Double): Double { |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
23 if (abs(x) >= size) |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
24 return 0.0 |
11
678adef4774f
Move more stuff into worker threads.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
25 val remembered = memory[x] |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
26 if (remembered != null) |
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
27 return remembered |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
28 val calculated = sinc(x) * sinc(x/size) |
11
678adef4774f
Move more stuff into worker threads.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
29 memory[x] = calculated |
8
6ae738b8a814
Memoize. Only modest improvement results.
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
30 return calculated |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
31 } |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
32 } |