Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
comparison app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt @ 15:20da616dcda0
Add preferences.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 18 Feb 2021 22:12:19 -0800 |
parents | b1605be35bcc |
children | 86740f593b6c |
comparison
equal
deleted
inserted
replaced
14:73beb7c973ae | 15:20da616dcda0 |
---|---|
1 package com.bartsent.simpleresizer.lib | 1 package com.bartsent.simpleresizer.lib |
2 | 2 |
3 import android.graphics.Bitmap | 3 import android.graphics.Bitmap |
4 import android.graphics.Canvas | |
5 import android.graphics.Color | |
6 import android.graphics.Matrix | |
7 import kotlin.math.ceil | |
8 import kotlin.math.floor | |
9 | 4 |
10 private data class IndexWeight(var index: Int, var weight: Double) | 5 private data class IndexWeight(var index: Int, var weight: Double) |
11 | 6 |
7 /** | |
8 * A quality scaler, rather simpler than Image.getScaledInstance in that it has only | |
9 * one (slow, high-quality) option. | |
10 * @param newWidth Width of new bitmap | |
11 * @param newHeight Height of new bitmap | |
12 * @return New bitmap | |
13 */ | |
12 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int): Bitmap { | 14 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int): Bitmap { |
13 if (newWidth <= 0) | 15 if (newWidth <= 0) |
14 throw IllegalArgumentException("invalid width: $newWidth") | 16 throw IllegalArgumentException("invalid width: $newWidth") |
15 if (newHeight <= 0) | 17 if (newHeight <= 0) |
16 throw IllegalArgumentException("invalid height: $newHeight") | 18 throw IllegalArgumentException("invalid height: $newHeight") |
17 if (width == newWidth && height == newHeight) | 19 if (width == newWidth && height == newHeight) |
18 return Bitmap.createBitmap(this) | 20 return this |
19 return if (width != newWidth) { | 21 return if (width != newWidth) { |
20 Resizer.fromBitmap(this).horizontal(newWidth).let { | 22 Resizer.fromBitmap(this).horizontal(newWidth).let { |
21 if (height == newHeight) it else it.vertical(newHeight) | 23 if (height == newHeight) it else it.vertical(newHeight) |
22 } | 24 } |
23 } else { | 25 } else { |