Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
annotate app/src/main/java/com/bartsent/simpleresizer/lib/Resizer.kt @ 18:eedf995462d9 concur2
Parallalized, but ConstraintLayout started hosing the edit window.
author | David Barts <n5jrn@me.com> |
---|---|
date | Mon, 22 Feb 2021 07:04:31 -0800 |
parents | 86740f593b6c |
children | 6607f675a5f7 |
rev | line source |
---|---|
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
1 package com.bartsent.simpleresizer.lib |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
2 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
3 import android.graphics.Bitmap |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
4 import android.graphics.Color |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
5 import android.util.Log |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
6 import java.util.concurrent.Callable |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
7 import kotlin.math.ceil |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 import kotlin.math.floor |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 /** |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
11 * Bitmapped images designed for efficient resampling and per-pixel access via |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
12 * subscripting. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
13 */ |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
14 class Resizer(private val bits: IntArray, val width: Int, val height: Int) { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
15 constructor(width: Int, height: Int) : this(IntArray(width * height), width, height) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
16 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 companion object { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 /** |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 * Make a Resizer from a standard Bitmap. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 * @param image Bitmap object. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 * @return A new Resizer. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 */ |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 fun fromBitmap(image: Bitmap): Resizer { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 val basedOn = IntArray(image.width * image.height) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
25 image.getPixels(basedOn, 0, image.width, 0, 0, image.width, image.height) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
26 return Resizer(basedOn, image.width, image.height) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
28 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
29 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
30 private interface Band { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
31 operator fun get(i: Int): Int |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
32 operator fun set(i: Int, v: Int): Unit |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
33 val index: Int |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
34 val size: Int |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
35 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
36 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
37 private class Row(val basedOn: Resizer, override val index: Int): Band { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
38 override operator fun get(i: Int) = basedOn[i, index] |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
39 override operator fun set(i: Int, v: Int): Unit { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
40 basedOn[i, index] = v |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
41 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
42 override val size; get() = basedOn.width |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
43 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
44 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
45 private class Column(val basedOn: Resizer, override val index: Int): Band { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 override operator fun get(i: Int) = basedOn[index, i] |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
47 override operator fun set(i: Int, v: Int): Unit { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
48 basedOn[index, i] = v |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
50 override val size; get() = basedOn.height |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 private data class IndexWeight(var index: Int, var weight: Double) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 operator fun get(x: Int, y: Int): Int = bits[x + y*width] |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
56 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
57 operator fun set(x: Int, y: Int, v: Int): Unit { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
58 bits[x + y*width] = v |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
59 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
60 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 /** |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 * Return a standard Bitmap based on this Resizer's image. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
63 * @return A new Bitmap, which may share this Resizer's image storage. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
64 */ |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
65 fun toBitmap(): Bitmap = Bitmap.createBitmap(bits, width, height, Bitmap.Config.ARGB_8888) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
66 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
67 private fun precomputeWeights(dstSize: Int, srcSize: Int, filter: ScalingKernel): Array<Array<IndexWeight>> { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
68 val du = srcSize.toDouble() / dstSize.toDouble() |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
69 val scale = maxOf(1.0, du) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
70 val ru = ceil(scale * filter.size) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
71 val TEMPLATE = arrayOf<IndexWeight>() |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
72 val out = Array<Array<IndexWeight>>(dstSize) { TEMPLATE } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
73 val tmp = ArrayList<IndexWeight>((ru.toInt()+2)*2) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
74 val emax = srcSize - 1 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
75 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
76 for (v in 0 until dstSize) { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
77 val fu = (v.toDouble()+0.5)*du - 0.5 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
78 val begin = maxOf(0, ceil(fu - ru).toInt()) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
79 val end = minOf(emax, floor(fu + ru).toInt()) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
80 var sum: Double = 0.0 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
81 for (u in begin..end) { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
82 val w = filter.weight((u.toDouble() - fu) / scale) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
83 if (w != 0.0) { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
84 sum += w |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
85 tmp.add(IndexWeight(u, w)) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
86 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
87 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
88 if (sum != 0.0) { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
89 tmp.forEach { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
90 it.weight /= sum |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
91 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
92 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
93 out[v] = tmp.toArray(TEMPLATE) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
94 tmp.clear() |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
95 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
96 return out |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
97 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
98 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
99 private fun clamp(v: Double): Int = minOf(255, maxOf(0, (v + 0.5).toInt())) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
100 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
101 private fun resample(target: Band, i: Int, weights: Array<IndexWeight>, source: Band): Unit { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
102 var r = 0.0; var g = 0.0; var b = 0.0; var a = 0.0 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
103 weights.forEach { |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
104 val c = source[it.index] |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
105 val aw = Color.alpha(c).toDouble() * it.weight |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
106 r += Color.red(c).toDouble() * aw |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
107 g += Color.green(c).toDouble() * aw |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
108 b += Color.blue(c).toDouble() * aw |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
109 a += aw |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
110 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
111 if (a == 0.0) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
112 return |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
113 target[i] = Color.argb(clamp(a), clamp(r/a), clamp(g/a), clamp(b/a)) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
114 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
115 |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
116 private fun makeResampler(target: Band, weights: Array<Array<IndexWeight>>, source: Band): Callable<Exception?> { |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
117 return Callable<Exception?> { |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
118 try { |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
119 for (i in 0 until target.size) { |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
120 resample(target, i, weights[i], source) |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
121 } |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
122 null |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
123 } catch (e: Exception) { |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
124 e |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
125 } |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
126 } |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
127 } |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
128 |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
129 private fun runAndReap(jobs: ArrayList<Callable<Exception?>>) { |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
130 ThreadPools.WORKERS.invokeAll(jobs).forEach { |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
131 val exc = it.get() |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
132 if (exc != null) |
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
133 Log.e("Resizer", "worker thread threw exception", exc) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
134 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
135 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
136 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
137 /** |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
138 * Resize the horizontal aspect. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
139 * @param newWidth Width of new image |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
140 * @param kernel Scaling kernel to use (default: LanczosKernel) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
141 * @return A new Resizer object, resampled per specifications |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
142 */ |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
143 fun horizontal(newWidth: Int, kernel: ScalingKernel): Resizer { |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
144 val dst = Resizer(newWidth, height) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
145 val weights = precomputeWeights(newWidth, width, kernel) |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
146 val jobs = ArrayList<Callable<Exception?>>(height) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
147 for (y in 0 until height) { |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
148 jobs.add(makeResampler(Row(dst, y), weights, Row(this, y))) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
149 } |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
150 runAndReap(jobs) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
151 return dst |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
152 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
153 |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
154 /** |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
155 * Resize the vertical aspect. |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
156 * @param newHeight Height of new image |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
157 * @param kernel Scaling kernel to use (default: LanczosKernel) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
158 * @return A new Resizer object, resampled per specifications |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
159 */ |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
13
diff
changeset
|
160 fun vertical(newHeight: Int, kernel: ScalingKernel): Resizer { |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
161 val dst = Resizer(width, newHeight) |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
162 val weights = precomputeWeights(newHeight, height, kernel) |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
163 val jobs = ArrayList<Callable<Exception?>>(width) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
164 for (x in 0 until width) { |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
165 jobs.add(makeResampler(Column(dst, x), weights, Column(this, x))) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
166 } |
18
eedf995462d9
Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
167 runAndReap(jobs) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
168 return dst |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
169 } |
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
170 } |