Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
comparison app/src/main/java/com/bartsent/simpleresizer/lib/Resizer.kt @ 18:eedf995462d9 concur2
Parallalized, but ConstraintLayout started hosing the edit window.
author | David Barts <n5jrn@me.com> |
---|---|
date | Mon, 22 Feb 2021 07:04:31 -0800 |
parents | 86740f593b6c |
children | 6607f675a5f7 |
comparison
equal
deleted
inserted
replaced
17:86740f593b6c | 18:eedf995462d9 |
---|---|
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.Color | 4 import android.graphics.Color |
5 import android.util.Log | |
6 import java.util.concurrent.Callable | |
5 import kotlin.math.ceil | 7 import kotlin.math.ceil |
6 import kotlin.math.floor | 8 import kotlin.math.floor |
7 | 9 |
8 /** | 10 /** |
9 * Bitmapped images designed for efficient resampling and per-pixel access via | 11 * Bitmapped images designed for efficient resampling and per-pixel access via |
109 if (a == 0.0) | 111 if (a == 0.0) |
110 return | 112 return |
111 target[i] = Color.argb(clamp(a), clamp(r/a), clamp(g/a), clamp(b/a)) | 113 target[i] = Color.argb(clamp(a), clamp(r/a), clamp(g/a), clamp(b/a)) |
112 } | 114 } |
113 | 115 |
114 private fun resampleBand(target: Band, weights: Array<Array<IndexWeight>>, source: Band) { | 116 private fun makeResampler(target: Band, weights: Array<Array<IndexWeight>>, source: Band): Callable<Exception?> { |
115 for (i in 0 until target.size) { | 117 return Callable<Exception?> { |
116 resample(target, i, weights[i], source) | 118 try { |
119 for (i in 0 until target.size) { | |
120 resample(target, i, weights[i], source) | |
121 } | |
122 null | |
123 } catch (e: Exception) { | |
124 e | |
125 } | |
126 } | |
127 } | |
128 | |
129 private fun runAndReap(jobs: ArrayList<Callable<Exception?>>) { | |
130 ThreadPools.WORKERS.invokeAll(jobs).forEach { | |
131 val exc = it.get() | |
132 if (exc != null) | |
133 Log.e("Resizer", "worker thread threw exception", exc) | |
117 } | 134 } |
118 } | 135 } |
119 | 136 |
120 /** | 137 /** |
121 * Resize the horizontal aspect. | 138 * Resize the horizontal aspect. |
124 * @return A new Resizer object, resampled per specifications | 141 * @return A new Resizer object, resampled per specifications |
125 */ | 142 */ |
126 fun horizontal(newWidth: Int, kernel: ScalingKernel): Resizer { | 143 fun horizontal(newWidth: Int, kernel: ScalingKernel): Resizer { |
127 val dst = Resizer(newWidth, height) | 144 val dst = Resizer(newWidth, height) |
128 val weights = precomputeWeights(newWidth, width, kernel) | 145 val weights = precomputeWeights(newWidth, width, kernel) |
146 val jobs = ArrayList<Callable<Exception?>>(height) | |
129 for (y in 0 until height) { | 147 for (y in 0 until height) { |
130 resampleBand(Row(dst, y), weights, Row(this, y)) | 148 jobs.add(makeResampler(Row(dst, y), weights, Row(this, y))) |
131 } | 149 } |
150 runAndReap(jobs) | |
132 return dst | 151 return dst |
133 } | 152 } |
134 | 153 |
135 /** | 154 /** |
136 * Resize the vertical aspect. | 155 * Resize the vertical aspect. |
139 * @return A new Resizer object, resampled per specifications | 158 * @return A new Resizer object, resampled per specifications |
140 */ | 159 */ |
141 fun vertical(newHeight: Int, kernel: ScalingKernel): Resizer { | 160 fun vertical(newHeight: Int, kernel: ScalingKernel): Resizer { |
142 val dst = Resizer(width, newHeight) | 161 val dst = Resizer(width, newHeight) |
143 val weights = precomputeWeights(newHeight, height, kernel) | 162 val weights = precomputeWeights(newHeight, height, kernel) |
163 val jobs = ArrayList<Callable<Exception?>>(width) | |
144 for (x in 0 until width) { | 164 for (x in 0 until width) { |
145 resampleBand(Column(dst, x), weights, Column(this, x)) | 165 jobs.add(makeResampler(Column(dst, x), weights, Column(this, x))) |
146 } | 166 } |
167 runAndReap(jobs) | |
147 return dst | 168 return dst |
148 } | 169 } |
149 } | 170 } |