Skip to content

Commit e5705e1

Browse files
committed
check color channels in BufferedImage
1 parent 6ad1570 commit e5705e1

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
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.0")
15+
implementation("com.martmists.ndarray-simd:ndarray-simd:1.5.1")
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.0"
20+
version = "1.5.1"
2121
val isProduction = (findProperty("production") ?: System.getProperty("production")) != null
2222

2323
repositories {

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ fun F64Array.Companion.fromImage(img: BufferedImage): F64ImageArray {
3030
val h = img.height
3131
val hasAlpha = img.alphaRaster != null
3232
val pxData = (img.raster.dataBuffer as DataBufferByte).data
33+
val (r, g, b) = when (img.type) {
34+
BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB -> Triple(0, 1, 2)
35+
BufferedImage.TYPE_INT_BGR, BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_4BYTE_ABGR -> Triple(2, 1, 0)
36+
else -> throw UnsupportedOperationException("Unknown image type ${img.type}, please open an issue!")
37+
}
3338
val arr = F64Array(w, h, 4).image
3439
val pxSize = if (hasAlpha) 4 else 3
3540

@@ -38,14 +43,14 @@ fun F64Array.Companion.fromImage(img: BufferedImage): F64ImageArray {
3843
val idx = (x + y * w) * pxSize
3944
if (hasAlpha) {
4045
arr[x, y, 3] = pxData[idx].toInt().asColorDouble()
41-
arr[x, y, 0] = pxData[idx + 1].toInt().asColorDouble()
42-
arr[x, y, 1] = pxData[idx + 2].toInt().asColorDouble()
43-
arr[x, y, 2] = pxData[idx + 3].toInt().asColorDouble()
46+
arr[x, y, r] = pxData[idx + 1].toInt().asColorDouble()
47+
arr[x, y, g] = pxData[idx + 2].toInt().asColorDouble()
48+
arr[x, y, b] = pxData[idx + 3].toInt().asColorDouble()
4449
} else {
4550
arr[x, y, 3] = 1.0
46-
arr[x, y, 0] = pxData[idx].toInt().asColorDouble()
47-
arr[x, y, 1] = pxData[idx + 1].toInt().asColorDouble()
48-
arr[x, y, 2] = pxData[idx + 2].toInt().asColorDouble()
51+
arr[x, y, r] = pxData[idx].toInt().asColorDouble()
52+
arr[x, y, g] = pxData[idx + 1].toInt().asColorDouble()
53+
arr[x, y, b] = pxData[idx + 2].toInt().asColorDouble()
4954
}
5055
}
5156
}

0 commit comments

Comments
 (0)