Skip to content

Instantly share code, notes, and snippets.

@farmaker47
Created April 17, 2021 05:44
Show Gist options
  • Save farmaker47/ab5d8e3e6c7e5da306968c67338687a2 to your computer and use it in GitHub Desktop.
Save farmaker47/ab5d8e3e6c7e5da306968c67338687a2 to your computer and use it in GitHub Desktop.
fun bitmapToFloatArray(bitmap: Bitmap):
Array<Array<Array<FloatArray>>> {
val width: Int = bitmap.width
val height: Int = bitmap.height
val intValues = IntArray(width * height)
bitmap.getPixels(intValues, 0, width, 0, 0, width, height)
val floatArray = Array(1) {
Array(3) {
Array(width) {
FloatArray(height)
}
}
}
for (i in 0 until width - 1) {
for (j in 0 until height - 1) {
val pixelValue: Int = intValues[i * width + j]
floatArray[0][0][i][j] =
Color.red(pixelValue) / 255.0f // or (pixelValue shr 16 and 0xff).toFloat() / 255.0f
floatArray[0][1][i][j] =
Color.green(pixelValue) / 255.0f // or (pixelValue shr 8 and 0xff).toFloat() / 255.0f
floatArray[0][2][i][j] =
Color.blue(pixelValue) / 255.0f // or (pixelValue and 0xff).toFloat() / 255.0f
}
}
return floatArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment