in test-app/src/main/java/com/google/android/renderscript_test/IntrinsicColorMatrix.kt [110:162]
fun intrinsicColorMatrix(
context: RenderScript,
conversion: Tester.ColorMatrixConversionType,
bitmap: Bitmap,
matrix: FloatArray,
addVector: FloatArray,
restriction: Range2d?
): ByteArray {
val scriptColorMatrix = ScriptIntrinsicColorMatrix.create(context)
val inputAllocation = Allocation.createFromBitmap(context, bitmap)
inputAllocation.copyFrom(bitmap)
val outAllocation = Allocation.createTyped(context, inputAllocation.type)
val intrinsicOutArray = ByteArray(bitmap.byteCount)
when (conversion) {
Tester.ColorMatrixConversionType.RGB_TO_YUV -> scriptColorMatrix.setRGBtoYUV()
Tester.ColorMatrixConversionType.YUV_TO_RGB -> scriptColorMatrix.setYUVtoRGB()
Tester.ColorMatrixConversionType.GREYSCALE -> scriptColorMatrix.setGreyscale()
Tester.ColorMatrixConversionType.RANDOM -> {
val m = Matrix4f()
var index = 0
// RS is column major
for (x in 0..3) {
for (y in 0..3) {
m.set(x, y, matrix[index++])
}
}
scriptColorMatrix.setColorMatrix(m)
}
}
val vector = Float4(
addVector[0],
addVector[1],
addVector[2],
addVector[3]
)
scriptColorMatrix.setAdd(vector)
if (restriction != null) {
outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
val options = Script.LaunchOptions()
options.setX(restriction.startX, restriction.endX)
options.setY(restriction.startY, restriction.endY)
scriptColorMatrix.forEach(inputAllocation, outAllocation, options)
} else {
scriptColorMatrix.forEach(inputAllocation, outAllocation)
}
outAllocation.copyTo(intrinsicOutArray)
inputAllocation.destroy()
outAllocation.destroy()
scriptColorMatrix.destroy()
return intrinsicOutArray
}