Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
comparison app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt @ 17:86740f593b6c
Better memoization, more rational API.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 21 Feb 2021 22:14:07 -0800 |
parents | 20da616dcda0 |
children | 19c584b29679 |
comparison
equal
deleted
inserted
replaced
16:3ed74dc0e34a | 17:86740f593b6c |
---|---|
1 package com.bartsent.simpleresizer.lib | 1 package com.bartsent.simpleresizer.lib |
2 | 2 |
3 import android.graphics.Bitmap | 3 import android.graphics.Bitmap |
4 | |
5 private data class IndexWeight(var index: Int, var weight: Double) | |
6 | 4 |
7 /** | 5 /** |
8 * A quality scaler, rather simpler than Image.getScaledInstance in that it has only | 6 * A quality scaler, rather simpler than Image.getScaledInstance in that it has only |
9 * one (slow, high-quality) option. | 7 * one (slow, high-quality) option. |
10 * @param newWidth Width of new bitmap | 8 * @param newWidth Width of new bitmap |
11 * @param newHeight Height of new bitmap | 9 * @param newHeight Height of new bitmap |
12 * @return New bitmap | 10 * @return New bitmap |
13 */ | 11 */ |
14 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int): Bitmap { | 12 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int, kernel: ScalingKernel = LanczosKernel()): Bitmap { |
15 if (newWidth <= 0) | 13 if (newWidth <= 0) |
16 throw IllegalArgumentException("invalid width: $newWidth") | 14 throw IllegalArgumentException("invalid width: $newWidth") |
17 if (newHeight <= 0) | 15 if (newHeight <= 0) |
18 throw IllegalArgumentException("invalid height: $newHeight") | 16 throw IllegalArgumentException("invalid height: $newHeight") |
19 if (width == newWidth && height == newHeight) | 17 if (width == newWidth && height == newHeight) |
20 return this | 18 return this |
21 return if (width != newWidth) { | 19 return if (width != newWidth) { |
22 Resizer.fromBitmap(this).horizontal(newWidth).let { | 20 Resizer.fromBitmap(this).horizontal(newWidth, kernel).let { |
23 if (height == newHeight) it else it.vertical(newHeight) | 21 if (height == newHeight) it else it.vertical(newHeight, kernel) |
24 } | 22 } |
25 } else { | 23 } else { |
26 Resizer.fromBitmap(this).vertical(newHeight) | 24 Resizer.fromBitmap(this).vertical(newHeight, kernel) |
27 } .toBitmap() | 25 } .toBitmap() |
28 } | 26 } |