Skip to content

Commit 635f88d

Browse files
committed
add scale parameter to Mat operations
1 parent 56a118a commit 635f88d

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212
}
1313

1414
dependencies {
15-
implementation("com.martmists.ndarray-simd:ndarray-simd:1.5.3")
15+
implementation("com.martmists.ndarray-simd:ndarray-simd:1.5.4")
1616
}
1717
```
1818

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ plugins {
1717
}
1818

1919
group = "com.martmists.ndarray-simd"
20-
version = "1.5.3"
20+
version = "1.5.4"
2121
val isProduction = (findProperty("production") ?: System.getProperty("production")) != null
2222

2323
repositories {

src/androidMain/kotlin/com/martmists/ndarray/simd/compat/image.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fun F64Array.Companion.fromImage(img: Bitmap): F64ImageArray {
3232
val hasAlpha = img.hasAlpha()
3333
val arr = F64Array(w, h, 4).image
3434

35+
// FIXME: Use IntArray for access instead for speed
3536
for (y in 0 until h) {
3637
for (x in 0 until w) {
3738
val px = img.getPixel(x, y)
@@ -64,6 +65,7 @@ fun F64ImageArray.toBitmap(): Bitmap {
6465
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
6566
val hasAlpha = channels == 4
6667

68+
// FIXME: Use IntArray for access instead for speed
6769
for (y in 0 until height) {
6870
for (x in 0 until width) {
6971
var pxInt = if (hasAlpha) ((this[x, y, 3] * 255).toInt().coerceIn(0, 255) shl 24) else 0xFF000000.toInt()

src/jvmMain/kotlin/com/martmists/ndarray/simd/compat/opencv.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ private fun getScaleInfo(type: Int): Pair<Double, Double> {
3636
*
3737
* The resulting F64Array will always contain 4 channels.
3838
*
39+
* @param scale Whether to scale the values (useful for images, not for other Mats)
3940
* @return The [F64Array] converted from the [Mat]. See [F64Array.fromImage][F64Array.Companion.fromImage] for the format.
4041
* @since 1.4.0
4142
*/
42-
fun Mat.toF64Array(): F64ImageArray {
43+
fun Mat.toF64Array(scale: Boolean = true): F64ImageArray {
4344
usesOpenCV()
4445

4546
val typ = if (type() == CvType.CV_64FC4) this else {
4647
Mat(size(), CvType.CV_64FC4).also {
47-
val (ai, bi) = getScaleInfo(type())
48-
convertTo(it, it.type(), 1 / ai, bi)
48+
if (scale) {
49+
val (ai, bi) = getScaleInfo(type())
50+
convertTo(it, it.type(), 1 / ai, bi)
51+
}
4952
}
5053
}
5154

@@ -69,7 +72,7 @@ fun Mat.toF64Array(): F64ImageArray {
6972
}
7073

7174
/**
72-
* Converts an [F64Array] to a [Mat].
75+
* Converts an [F64ImageArray] to a [Mat].
7376
*
7477
* The required shape must have either 1, 3 or 4 channels in the 3rd dimension:
7578
* If 1 channel, shape must be [width, height, 1]
@@ -84,7 +87,7 @@ fun Mat.toF64Array(): F64ImageArray {
8487
* @return The OpenCV Mat containing the given data.
8588
* @since 1.4.0
8689
*/
87-
fun F64ImageArray.toOpenCVMat(type: Int = CvType.CV_64FC4): Mat {
90+
fun F64ImageArray.toOpenCVMat(type: Int = CvType.CV_64FC4, scale: Boolean = true): Mat {
8891
usesOpenCV()
8992

9093
val w = shape[0]
@@ -118,8 +121,12 @@ fun F64ImageArray.toOpenCVMat(type: Int = CvType.CV_64FC4): Mat {
118121
}
119122

120123
val out = Mat(w, h, type)
121-
val (a, b) = getScaleInfo(type)
122-
tmp.convertTo(out, type, a, -b)
124+
if (scale) {
125+
val (a, b) = getScaleInfo(type)
126+
tmp.convertTo(out, type, a, -b)
127+
} else {
128+
tmp.convertTo(out, type)
129+
}
123130
return out
124131
}
125132

0 commit comments

Comments
 (0)