From 21a89d989f2859eca3f440cebd4d282f6d753b93 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 26 Jul 2024 12:54:00 +0200 Subject: [PATCH] [resources] Add image bitmap/xml/svg bytearray converters to the corresponding source sets. (#5098) The PR adds a method to convert a bitmap bytearray to the ImageBitma, a vector XML content bytearray to the ImageVector in the common code. And the same for an SVG files on non-android targets. Fixes https://youtrack.jetbrains.com/issue/CMP-3869 Fixes https://youtrack.jetbrains.com/issue/CMP-4925 ## Release Notes ### Features - Resources - Added utility functions to decode `Bitmap ByteArray as ImageVector` and `XML ByteArray as ImageVector` in the common code and `SVG ByteArray as Painter` in the non-android code --- .../compose/resources/ImageDecoders.kt | 31 +++++++++++++++++++ .../compose/resources/ImageDecoders.skiko.kt | 17 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.kt create mode 100644 components/resources/library/src/skikoMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.skiko.kt diff --git a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.kt b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.kt new file mode 100644 index 0000000000..b393c1e912 --- /dev/null +++ b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.kt @@ -0,0 +1,31 @@ +package org.jetbrains.compose.resources + +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.unit.Density +import org.jetbrains.compose.resources.vector.toImageVector + +/** + * Decodes a byte array of a Bitmap to an ImageBitmap. Supports JPEG, PNG, BMP, WEBP + * + * Different platforms can support additional formats. + * + * @return The converted ImageBitmap. + */ +@ExperimentalResourceApi +fun ByteArray.decodeToImageBitmap(): ImageBitmap { + val dumbDensity = 0 //any equal source and target density disable scaling here + return this.toImageBitmap(dumbDensity, dumbDensity) +} + +/** + * Decodes a byte array of a vector XML file to an ImageVector. + * + * @param density density to apply during converting the source units to the [ImageVector] units. + * + * @return The converted ImageVector. + */ +@ExperimentalResourceApi +fun ByteArray.decodeToImageVector(density: Density): ImageVector { + return this.toXmlElement().toImageVector(density) +} diff --git a/components/resources/library/src/skikoMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.skiko.kt b/components/resources/library/src/skikoMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.skiko.kt new file mode 100644 index 0000000000..fe5e92eab7 --- /dev/null +++ b/components/resources/library/src/skikoMain/kotlin/org/jetbrains/compose/resources/ImageDecoders.skiko.kt @@ -0,0 +1,17 @@ +package org.jetbrains.compose.resources + +import androidx.compose.ui.graphics.painter.Painter +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.unit.Density + +/** + * Decodes a byte array of an SVG file to a compose Painter. + * + * @param density density to apply during converting the source units to the [Painter] units. + * + * @return The converted Painter. + */ +@ExperimentalResourceApi +fun ByteArray.decodeToSvgPainter(density: Density): Painter { + return this.toSvgElement().toSvgPainter(density) +} \ No newline at end of file