view app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt @ 46:8205f3c17500 default tip

Tweak messages a little.
author David Barts <n5jrn@me.com>
date Wed, 14 Apr 2021 08:18:23 -0700
parents 6607f675a5f7
children
line wrap: on
line source

package com.bartsent.simpleresizer.lib

/*
 * Some code in this file has been adapted from: https://github.com/disintegration/imaging/ .
 */

import android.graphics.Bitmap

/**
 * A quality scaler, rather simpler than Image.getScaledInstance in that it
 * currently has only one (slow, high-quality) option.
 * @param       newWidth        Width of new bitmap
 * @param       newHeight       Height of new bitmap
 * @return                      New bitmap
 */
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 this
    return if (width != newWidth) {
        Resizer.fromBitmap(this).horizontal(newWidth, kernel).let {
            if (height == newHeight) it else it.vertical(newHeight, kernel)
        }
    } else {
        Resizer.fromBitmap(this).vertical(newHeight, kernel)
    } .toBitmap()
}