comparison 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
comparison
equal deleted inserted replaced
6:e8059b166de1 7:9374d044a132
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 4 import android.graphics.Canvas
5 import android.graphics.Color
6 import android.graphics.Matrix 5 import android.graphics.Matrix
7 import kotlin.math.ceil
8 import kotlin.math.floor
9 6
10 private data class IndexWeight(var index: Int, var weight: Double) 7 private data class IndexWeight(var index: Int, var weight: Double)
11 8
12 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int, kernel: ScalingKernel = LanczosKernel): Bitmap { 9 fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int, kernel: ScalingKernel = LanczosKernel): Bitmap {
13 if (newWidth <= 0) 10 if (newWidth <= 0)
21 else { 18 else {
22 Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also { 19 Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also {
23 Canvas(it).drawBitmap(this, Matrix(), null) 20 Canvas(it).drawBitmap(this, Matrix(), null)
24 } 21 }
25 } 22 }
26 if (width != newWidth) { 23 Resizer().use { resize ->
27 if (height != newHeight) 24 if (width != newWidth) {
28 return resizeVertical(resizeHorizontal(input, newWidth, kernel), newHeight, kernel) 25 if (height != newHeight)
29 else 26 return resize.vertical(resize.horizontal(input, newWidth, kernel), newHeight, kernel)
30 return resizeHorizontal(input, newWidth, kernel) 27 else
31 } else { 28 return resize.horizontal(input, newWidth, kernel)
32 return resizeVertical(input, newHeight, kernel) 29 } else {
30 return resize.vertical(input, newHeight, kernel)
31 }
33 } 32 }
34 } 33 }
35
36 private fun precomputeWeights(dstSize: Int, srcSize: Int, filter: ScalingKernel): Array<Array<IndexWeight>> {
37 val du = srcSize.toDouble() / dstSize.toDouble()
38 val scale = maxOf(1.0, du)
39 val ru = ceil(scale * filter.size)
40 val TEMPLATE = arrayOf<IndexWeight>()
41 val out = Array<Array<IndexWeight>>(dstSize) { TEMPLATE }
42 val tmp = ArrayList<IndexWeight>((ru.toInt()+2)*2)
43 val emax = srcSize - 1
44
45 for (v in 0 until dstSize) {
46 val fu = (v.toDouble()+0.5)*du - 0.5
47 val begin = maxOf(0, ceil(fu - ru).toInt())
48 val end = minOf(emax, floor(fu + ru).toInt())
49 var sum: Double = 0.0
50 for (u in begin..end) {
51 val w = filter.weight((u.toDouble() - fu) / scale)
52 if (w != 0.0) {
53 sum += w
54 tmp.add(IndexWeight(u, w))
55 }
56 }
57 if (sum != 0.0) {
58 tmp.forEach {
59 it.weight /= sum
60 }
61 }
62 out[v] = tmp.toArray(TEMPLATE)
63 tmp.clear()
64 }
65 return out
66 }
67
68 private fun clamp(v: Double): Int = minOf(255, maxOf(0, (v + 0.5).toInt()))
69
70 private fun resample(target: Bitmap, x: Int, y: Int, weights: Array<IndexWeight>, scanLine: IntArray): Unit {
71 var r = 0.0; var g = 0.0; var b = 0.0; var a = 0.0
72 weights.forEach {
73 val c = scanLine[it.index]
74 val aw = Color.alpha(c).toDouble() * it.weight
75 r += Color.red(c).toDouble() * aw
76 g += Color.green(c).toDouble() * aw
77 b += Color.blue(c).toDouble() * aw
78 a += aw
79 }
80 if (a == 0.0)
81 return
82 target.setPixel(x, y, Color.argb(clamp(a), clamp(r/a), clamp(g/a), clamp(b/a)))
83 }
84
85 private fun resizeHorizontal(image: Bitmap, newWidth: Int, kernel: ScalingKernel): Bitmap {
86 val dst = Bitmap.createBitmap(newWidth, image.height, Bitmap.Config.ARGB_8888)
87 val weights = precomputeWeights(newWidth, image.width, kernel)
88 val scanLine = IntArray(image.width)
89 for (y in 0 until image.height) {
90 for (x in 0 until image.width) {
91 scanLine[x] = image.getPixel(x, y)
92 }
93 for (x in weights.indices) {
94 resample(dst, x, y, weights[x], scanLine)
95 }
96 }
97 return dst
98 }
99
100 private fun resizeVertical(image: Bitmap, newHeight: Int, kernel: ScalingKernel): Bitmap {
101 val dst = Bitmap.createBitmap(image.width, newHeight, Bitmap.Config.ARGB_8888)
102 val weights = precomputeWeights(newHeight, image.height, kernel)
103 val scanLine = IntArray(image.height)
104 for (x in 0 until image.width) {
105 for (y in 0 until image.height) {
106 scanLine[y] = image.getPixel(x, y)
107 }
108 for (y in weights.indices) {
109 resample(dst, x, y, weights[y], scanLine)
110 }
111 }
112 return dst
113 }