Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[resources] Add image bitmap/xml/svg bytearray converters to the corresponding source sets. #5098

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
igordmn marked this conversation as resolved.
Show resolved Hide resolved
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 {
igordmn marked this conversation as resolved.
Show resolved Hide resolved
return this.toSvgElement().toSvgPainter(density)
}