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¶
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¶
Color management Convert to sRGB through ICC profiles, PNG color chunks and HDR tone mapping.
Animation Composited frames, play counts, frame delays and decoding only the first frame.
Writing images PNG, APNG, JPEG, GIF, BMP, TIFF, WebP, AVIF, JPEG XR and HEIC.
Resizing and orientation Rotate, crop and scale, or decode a JPEG straight to a smaller size.
Compose and Coil The KiteImage composable and the Coil 3 decoder.
Limits What ImageKodec does not do, and the bounds it puts on hostile input.