view app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt @ 7:9374d044a132 concur

Concurrency attempt, NOT WORKING.
author David Barts <n5jrn@me.com>
date Wed, 17 Feb 2021 07:24:26 -0800
parents e8059b166de1
children 884092efe31a
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)
        }
    }
}