Skip to content

Commit

Permalink
[resources] Add image bitmap/xml/svg bytearray converters to the corr…
Browse files Browse the repository at this point in the history
…esponding 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
  • Loading branch information
terrakok authored Jul 26, 2024
1 parent d9e3528 commit 21a89d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 21a89d9

Please # to comment.