Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
annotate app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt @ 35:6607f675a5f7
Add licensing.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 11 Mar 2021 22:41:48 -0800 |
parents | 19c584b29679 |
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 android.graphics.Bitmap |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 |
15 | 9 /** |
34
19c584b29679
Add gradle files, bump rev to bypass installer cache misfeature.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
10 * A quality scaler, rather simpler than Image.getScaledInstance in that it |
19c584b29679
Add gradle files, bump rev to bypass installer cache misfeature.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
11 * currently has only one (slow, high-quality) option. |
15 | 12 * @param newWidth Width of new bitmap |
13 * @param newHeight Height of new bitmap | |
14 * @return New bitmap | |
15 */ | |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
16 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int, kernel: ScalingKernel = LanczosKernel()): Bitmap { |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 if (newWidth <= 0) |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 throw IllegalArgumentException("invalid width: $newWidth") |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 if (newHeight <= 0) |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 throw IllegalArgumentException("invalid height: $newHeight") |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 if (width == newWidth && height == newHeight) |
15 | 22 return this |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
23 return if (width != newWidth) { |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
24 Resizer.fromBitmap(this).horizontal(newWidth, kernel).let { |
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
25 if (height == newHeight) it else it.vertical(newHeight, kernel) |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
26 } |
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 } else { |
17
86740f593b6c
Better memoization, more rational API.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
28 Resizer.fromBitmap(this).vertical(newHeight, kernel) |
13
b1605be35bcc
Dumping Bitmap yields 2x improvement!
David Barts <n5jrn@me.com>
parents:
6
diff
changeset
|
29 } .toBitmap() |
6
e8059b166de1
Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
30 } |