in WorkManagerSample/lib/src/main/java/com/example/background/workers/filters/GrayScaleFilterWorker.kt [29:54]
override fun applyFilter(input: Bitmap): Bitmap {
var rsContext: RenderScript? = null
return try {
rsContext = RenderScript.create(applicationContext, RenderScript.ContextType.DEBUG)
val inAlloc = Allocation.createFromBitmap(rsContext, input)
val outAlloc = Allocation.createTyped(rsContext, inAlloc.type)
// The Renderscript function that computes gray scale pixel values is defined in
// `src/main/rs/grayscale.rs`. We compute a new pixel value for every pixel which is
// out = (r + g + b) / 3 where r, g, b are the red, green and blue channels in the
// input image.
ScriptC_grayscale(rsContext).run {
_script = this
_width = input.width.toLong()
_height = input.height.toLong()
_in = inAlloc
_out = outAlloc
invoke_filter()
}
Bitmap.createBitmap(input.width, input.height, input.config).apply {
outAlloc.copyTo(this)
}
} finally {
rsContext?.finish()
}
}