Skip to content

Instantly share code, notes, and snippets.

@orllewin
Created January 16, 2022 23:37
Show Gist options
  • Select an option

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

Select an option

Save orllewin/e6791956fd81e5f2eb9877a5cfc3a081 to your computer and use it in GitHub Desktop.
Use Unreal format 256x16 LUTs with Google's RenderScript Intrinsic replacement Toolkit native library
package orllewin.haldclut_impl
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import com.google.android.renderscript.Rgba3dArray
import com.google.android.renderscript.Toolkit
/**
* Converts Unreal format 256x16 LUT image to 3d Cube LUT
*/
class UnrealLutToolkit {
private val unrealCube = LutUtils.Dimension(16, 16, 16)
private var cubeArray: ByteArray? = null
private var toolkitCube: Rgba3dArray? = null
fun loadLut(context: Context, resourceId: Int){
val lutBitmap = BitmapFactory.decodeResource(context.resources, resourceId)
cubeArray = generateLutCube(lutBitmap, unrealCube)
toolkitCube = Rgba3dArray(cubeArray!!, unrealCube.sizeX, unrealCube.sizeY, unrealCube.sizeZ)
}
fun process(source: Bitmap): Bitmap = Toolkit.lut3d(source, toolkitCube!!)
private fun generateLutCube(lutBitmap: Bitmap, cubeSize: LutUtils.Dimension): ByteArray {
val lutWidth = lutBitmap.width
val lutHeight = lutBitmap.height
val lutPixels = IntArray(lutWidth * lutHeight)
lutBitmap.getPixels(lutPixels, 0, lutWidth, 0, 0, lutWidth, lutHeight)
lutBitmap.recycle()//Done with Lut bitmap
val data = ByteArray(cubeSize.sizeX * cubeSize.sizeY * cubeSize.sizeZ * 4)
val cube = Rgba3dArray(data, cubeSize.sizeX, cubeSize.sizeY, cubeSize.sizeZ)
for (red in 0 until 16) {
for (green in 0 until 16) {
val p = red + green * lutWidth
for (blue in 0 until 16) {
val c = lutPixels[p + blue * 16]
cube[red, green, blue] = byteArrayOf(Color.red(c).toByte(), Color.green(c).toByte(), Color.blue(c).toByte(), (255).toByte())
}
}
}
return data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment