Created
April 17, 2021 05:44
-
-
Save farmaker47/ab5d8e3e6c7e5da306968c67338687a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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