view app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt @ 12:62780d38a879 concur

Fix botched commit.
author David Barts <n5jrn@me.com>
date Wed, 17 Feb 2021 14:35:52 -0800
parents 884092efe31a
children
line wrap: on
line source

package com.bartsent.simpleresizer.lib

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix

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

fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int, kernel: ScalingKernel = LanczosKernel): 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)
    val input = if (config == Bitmap.Config.ARGB_8888)
        this
    else {
        Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also {
            Canvas(it).drawBitmap(this, Matrix(), null)
        }
    }
    Resizer().use { resize ->
        if (width != newWidth) {
            if (height != newHeight)
                return resize.vertical(resize.horizontal(input, newWidth, kernel), newHeight, kernel)
            else
                return resize.horizontal(input, newWidth, kernel)
        } else {
            return resize.vertical(input, newHeight, kernel)
        }
    }
}