annotate app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt @ 13:b1605be35bcc memo.oo

Dumping Bitmap yields 2x improvement!
author David Barts <n5jrn@me.com>
date Thu, 18 Feb 2021 14:15:26 -0800
parents e8059b166de1
children 20da616dcda0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 android.graphics.Bitmap
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 import android.graphics.Canvas
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 import android.graphics.Color
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import android.graphics.Matrix
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import kotlin.math.ceil
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import kotlin.math.floor
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
9
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 private data class IndexWeight(var index: Int, var weight: Double)
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
11
13
b1605be35bcc Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents: 6
diff changeset
12 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int): Bitmap {
6
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 if (newWidth <= 0)
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 throw IllegalArgumentException("invalid width: $newWidth")
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 if (newHeight <= 0)
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 throw IllegalArgumentException("invalid height: $newHeight")
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 if (width == newWidth && height == newHeight)
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 return Bitmap.createBitmap(this)
13
b1605be35bcc Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents: 6
diff changeset
19 return if (width != newWidth) {
b1605be35bcc Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents: 6
diff changeset
20 Resizer.fromBitmap(this).horizontal(newWidth).let {
b1605be35bcc Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents: 6
diff changeset
21 if (height == newHeight) it else it.vertical(newHeight)
6
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 }
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 } else {
13
b1605be35bcc Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents: 6
diff changeset
24 Resizer.fromBitmap(this).vertical(newHeight)
b1605be35bcc Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents: 6
diff changeset
25 } .toBitmap()
6
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 }