KiteImage

@Composable
fun KiteImage(data: ByteArray, contentDescription: String?, modifier: Modifier = Modifier, alignment: Alignment = Alignment.Center, contentScale: ContentScale = ContentScale.Fit, alpha: Float = DefaultAlpha, colorFilter: ColorFilter? = null, filterQuality: FilterQuality = DrawScope.DefaultFilterQuality, animate: Boolean = true, colorTarget: ColorTarget = ColorTarget.Srgb(), onError: (ImageDecodeException) -> Unit? = null)(source)

Display any image ImageKodec can decode: and it decides on its own whether to animate. Feed it a GIF and it plays (correct delays, disposal compositing, NETSCAPE loop count, holding the last frame after a finite loop ends); feed it a PNG/BMP and it just draws. One composable, no format branching at call sites.

Decoding runs off the UI thread (Dispatchers.Default) after the first layout, and re-runs when data changes. A still decodes at the size it draws at in that layout, as contentScale fits it, through ImageKodec.decodeDownscaledTo: a JPEG, JPEG 2000 or JPEG XR reduces inside its inverse transform, and a HEIC or AVIF grid one row of tiles at a time, so a large photo shown small never exists at full size. When the layout later gives it a larger box, it decodes again at the new size. A decode stops once the composable leaves composition or data changes: a still at the decoder's next step, such as a row of JPEG blocks or a HEIC tile, and an animation between frames, so a list scrolled past large images does not queue their abandoned decodes ahead of the visible ones.

HEIC (HEVC) needs the imagekodec-heic module and a call to ImageKodecHeic.install() at startup, which this module does not make; without it a HEIC file fails with an UnsupportedImageException that onError receives. AVIF needs nothing more.

The first frame converts to an ImageBitmap in the same background step, and the rest convert there ahead of playback, as KiteAnimatedImage describes. Until the first frame is ready (or on failure) the composable occupies its layout slot but draws nothing; failures also reach onError if provided.

Playback is KiteAnimatedImage: frame selection by elapsed frame-clock time, so long loops never drift and janky frames get skipped, not stretched.

animate is the escape hatch: false pins the first composited frame of an animated input (thumbnails, previews), and only that frame is decoded. Turning it on later decodes the rest while the pinned frame stays on screen; turning it off keeps the frames already decoded. Static inputs ignore it.

The colours are converted to sRGB by default, through the ICC profile or the colour chunks the file carries, as a browser draws it. A file that declares nothing, or sRGB, is not touched. Pass ColorTarget.Source as colorTarget to draw the samples as stored.


@Composable
fun KiteImage(bitmap: KiteBitmap, contentDescription: String?, modifier: Modifier = Modifier, alignment: Alignment = Alignment.Center, contentScale: ContentScale = ContentScale.Fit, alpha: Float = DefaultAlpha, colorFilter: ColorFilter? = null, filterQuality: FilterQuality = DrawScope.DefaultFilterQuality)(source)

Display an already-decoded KiteBitmap (for callers running their own decode pipeline). Conversion to ImageBitmap is remembered per instance and runs on Dispatchers.Default, except for a bitmap of at most 65,536 pixels, which converts at once because that costs less than a frame of nothing. Until a larger one is ready, the composable lays out at the bitmap's size and draws nothing.