view 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
line wrap: on
line source

package com.bartsent.simpleresizer.lib

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import kotlin.math.ceil
import kotlin.math.floor

private data class IndexWeight(var index: Int, var weight: Double)

fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int): Bitmap {
    if (newWidth <= 0)
        throw IllegalArgumentException("invalid width: $newWidth")
    if (newHeight <= 0)
        throw IllegalArgumentException("invalid height: $newHeight")
    if (width == newWidth && height == newHeight)
        return Bitmap.createBitmap(this)
    return if (width != newWidth) {
        Resizer.fromBitmap(this).horizontal(newWidth).let {
            if (height == newHeight) it else it.vertical(newHeight)
        }
    } else {
        Resizer.fromBitmap(this).vertical(newHeight)
    } .toBitmap()
}