I'm trying to convert a KorIM `Bitmap` to an `Imag...
# compose-desktop
s
I'm trying to convert a KorIM
Bitmap
to an
ImageBitmap
. My first "brilliant" idea was to write an implementation of
ImageBitmap
with a backing from KorIM. That didn't work on Android or JVM because they both expect a specific backing. Is there a way around this? Failing that, I guess I need to write a converter from KorIM to Skia for JVM/JS. For JVM, I can use AWT as an interim format, but I don't think JS has anything similar.
My usage is something like:
Copy code
val bitmap: Bitmap32 = ...
Image(bitmap.toComposeImageBitmap(), contentDescription = ..)
where
toComposeImageBitmap()
creates an
ImageBitmap
backed by the Korim bitmap.
Maybe I could write a painter for korim bitmaps instead?
It really feels like
ImageBitmap
is a fake interface. I'm not sure where else it's used, but not being able to use a custom implementation in
Image
means that there's not much reason for being able to implement it.
I wrote a function to convert Korim to Skia bitmaps. If anyone finds it useful:
Copy code
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeImageBitmap
import com.soywiz.korim.bitmap.Bitmap
import com.soywiz.korim.bitmap.Bitmap32
import org.jetbrains.skia.ColorAlphaType
import org.jetbrains.skia.ColorInfo
import org.jetbrains.skia.ColorType
import org.jetbrains.skia.ImageInfo

internal actual fun Bitmap.toComposeImageBitmap(): ImageBitmap {
    return toBMP32IfRequired().toSkiaBitmap().asComposeImageBitmap()
}

internal fun Bitmap32.toSkiaBitmap(): org.jetbrains.skia.Bitmap {
    val colorType = ColorType.N32
    val alphaType = if (premultiplied) ColorAlphaType.PREMUL else ColorAlphaType.OPAQUE
    val skiaColorSpace = null
    val colorInfo = ColorInfo(colorType, alphaType, skiaColorSpace)
    val imageInfo = ImageInfo(colorInfo, width, height)
    val bitmap = org.jetbrains.skia.Bitmap()
    val pixels = extractBytes()
    bitmap.allocPixels(imageInfo)
    bitmap.installPixels(pixels)
    return bitmap
}