Skip to content

Commit 21a89d9

Browse files
authored
[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. <!-- Optional --> 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
1 parent d9e3528 commit 21a89d9

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jetbrains.compose.resources
2+
3+
import androidx.compose.ui.graphics.ImageBitmap
4+
import androidx.compose.ui.graphics.vector.ImageVector
5+
import androidx.compose.ui.unit.Density
6+
import org.jetbrains.compose.resources.vector.toImageVector
7+
8+
/**
9+
* Decodes a byte array of a Bitmap to an ImageBitmap. Supports JPEG, PNG, BMP, WEBP
10+
*
11+
* Different platforms can support additional formats.
12+
*
13+
* @return The converted ImageBitmap.
14+
*/
15+
@ExperimentalResourceApi
16+
fun ByteArray.decodeToImageBitmap(): ImageBitmap {
17+
val dumbDensity = 0 //any equal source and target density disable scaling here
18+
return this.toImageBitmap(dumbDensity, dumbDensity)
19+
}
20+
21+
/**
22+
* Decodes a byte array of a vector XML file to an ImageVector.
23+
*
24+
* @param density density to apply during converting the source units to the [ImageVector] units.
25+
*
26+
* @return The converted ImageVector.
27+
*/
28+
@ExperimentalResourceApi
29+
fun ByteArray.decodeToImageVector(density: Density): ImageVector {
30+
return this.toXmlElement().toImageVector(density)
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.jetbrains.compose.resources
2+
3+
import androidx.compose.ui.graphics.painter.Painter
4+
import androidx.compose.ui.graphics.vector.ImageVector
5+
import androidx.compose.ui.unit.Density
6+
7+
/**
8+
* Decodes a byte array of an SVG file to a compose Painter.
9+
*
10+
* @param density density to apply during converting the source units to the [Painter] units.
11+
*
12+
* @return The converted Painter.
13+
*/
14+
@ExperimentalResourceApi
15+
fun ByteArray.decodeToSvgPainter(density: Density): Painter {
16+
return this.toSvgElement().toSvgPainter(density)
17+
}

0 commit comments

Comments
 (0)