diff 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
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt	Sun Feb 21 21:43:54 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt	Sun Feb 21 22:14:07 2021 -0800
@@ -2,8 +2,6 @@
 
 import android.graphics.Bitmap
 
-private data class IndexWeight(var index: Int, var weight: Double)
-
 /**
  * A quality scaler, rather simpler than Image.getScaledInstance in that it has only
  * one (slow, high-quality) option.
@@ -11,7 +9,7 @@
  * @param       newHeight       Height of new bitmap
  * @return                      New bitmap
  */
-fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int): Bitmap {
+fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int, kernel: ScalingKernel = LanczosKernel()): Bitmap {
     if (newWidth <= 0)
         throw IllegalArgumentException("invalid width: $newWidth")
     if (newHeight <= 0)
@@ -19,10 +17,10 @@
     if (width == newWidth && height == newHeight)
         return this
     return if (width != newWidth) {
-        Resizer.fromBitmap(this).horizontal(newWidth).let {
-            if (height == newHeight) it else it.vertical(newHeight)
+        Resizer.fromBitmap(this).horizontal(newWidth, kernel).let {
+            if (height == newHeight) it else it.vertical(newHeight, kernel)
         }
     } else {
-        Resizer.fromBitmap(this).vertical(newHeight)
+        Resizer.fromBitmap(this).vertical(newHeight, kernel)
     } .toBitmap()
 }