Skip to content

ImageKodec

Image codecs written in Kotlin, for Kotlin Multiplatform. Decode PNG, JPEG, GIF, BMP, TIFF, JPEG 2000, WebP, AVIF, JPEG XL and JPEG XR from a ByteArray, and HEIC with the separate imagekodec-heic module. The same code runs on Android, iOS, desktop, native, the browser and Wasm.

Compose Multiplatform draws an image on every target, but getting the pixels still goes through the platform: BitmapFactory on Android, CoreGraphics on iOS, Skia on desktop and the browser on web. Common code that needs real pixels (a thumbnail hash, a server-side resize, the images inside a PDF) therefore has to call per-platform code.

ImageKodec decodes in common Kotlin instead. Every format comes back as non-premultiplied ARGB_8888 in a plain IntArray. The core artifact is pure Kotlin, and its one dependency is ArchiveKodec, which supplies DEFLATE, zlib, LZW, PackBits, Zstandard and LZMA.

Install

commonMain.dependencies {
    implementation("io.github.yuroyami:imagekodec:0.4.0")
}

The Compose and Coil bindings are separate, optional modules that build for 7 of the 22 targets of the core. Read Compose and Coil before you add them.

Decode something

import io.github.yuroyami.imagekodec.ImageKodec

val info = ImageKodec.probe(bytes)      // the header alone, no pixel buffer
val bitmap = ImageKodec.decode(bytes)   // format sniffed from the magic bytes
val pixel = bitmap[10, 20]              // 0xAARRGGBB

GIF, APNG, animated WebP, animated AVIF, HEIC image sequences and animated JPEG XL all come back as one type, with every frame already composited:

val anim = ImageKodec.decodeAnimation(bytes)
for (frame in anim.frames) draw(frame.bitmap, frame.delayMillis)

Guides