Skip to content

Instantly share code, notes, and snippets.

@orllewin
Created January 25, 2022 11:11
Show Gist options
  • Select an option

  • Save orllewin/10fc98ea504ed2fd126091538747c91b to your computer and use it in GitHub Desktop.

Select an option

Save orllewin/10fc98ea504ed2fd126091538747c91b to your computer and use it in GitHub Desktop.
package orllewin.lutcreator
import android.graphics.Bitmap
import com.google.android.renderscript.LookupTable
import com.google.android.renderscript.Toolkit
/**
*
* Fast Bitmap colour channel filtering
* For use with the RenderScript intrinsics replacement Toolkit:
* https://github.com/android/renderscript-intrinsics-replacement-toolkit
*
* This offers basic linear RGB filtering, but the toolkit LookupTable would support more
* complex RGB channel curves too.
*
*/
class RGBToolkit {
private val rgbTable = LookupTable()
fun updateRed(red: Int){
repeat(256){ i ->
rgbTable.red[i] = ((i/100f) * red).toInt().toByte()
}
}
fun updateGreen(green: Int){
repeat(256){ i ->
rgbTable.green[i] = ((i/100f) * green).toInt().toByte()
}
}
fun updateBlue(blue: Int){
repeat(256){ i ->
rgbTable.blue[i] = ((i/100f) * blue).toInt().toByte()
}
}
fun updateRGB(r: Int, g: Int, b: Int){
repeat(256){ i ->
rgbTable.red[i] = ((i/100f) * r).toInt().toByte()
rgbTable.green[i] = ((i/100f) * g).toInt().toByte()
rgbTable.blue[i] = ((i/100f) * b).toInt().toByte()
}
}
fun process(source: Bitmap): Bitmap = Toolkit.lut(source, rgbTable)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment