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