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

Fix calling toBitmap with an underlying hardware bitmap. #2645

Merged
merged 1 commit into from
Nov 6, 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,29 @@
package coil3

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build.VERSION.SDK_INT
import coil3.core.test.R
import coil3.test.utils.assumeTrue
import coil3.test.utils.context
import kotlin.test.assertEquals
import kotlin.test.assertSame
import org.junit.Test

class ImageAndroidTest {

/** Regression test: https://github.com/coil-kt/coil/issues/2644 */
@Test
fun toBitmap_hardware() {
assumeTrue(SDK_INT >= 26)

// Decode a resource since we can't create a test hardware bitmap directly.
val options = BitmapFactory.Options().apply {
inPreferredConfig = Bitmap.Config.HARDWARE
}
val bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.normal, options)

assertEquals(Bitmap.Config.HARDWARE, bitmap.config)
assertSame(bitmap, bitmap.asImage().toBitmap())
}
}
14 changes: 12 additions & 2 deletions coil-core/src/androidMain/kotlin/coil3/Image.android.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coil3

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.ColorFilter
import android.graphics.PixelFormat
import android.graphics.drawable.BitmapDrawable
Expand All @@ -12,6 +13,7 @@ import coil3.util.allocationByteCountCompat
import coil3.util.height
import coil3.util.width

@Suppress("RemoveRedundantQualifierName")
actual typealias Bitmap = android.graphics.Bitmap

actual typealias Canvas = android.graphics.Canvas
Expand All @@ -25,12 +27,20 @@ actual fun Bitmap.asImage(shareable: Boolean): BitmapImage {
actual fun Image.toBitmap(
width: Int,
height: Int,
): Bitmap = toBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888)
): Bitmap {
var bitmapConfig: Bitmap.Config? = null

if (this is BitmapImage) {
bitmapConfig = bitmap.config
}

return toBitmap(width, height, bitmapConfig ?: Bitmap.Config.ARGB_8888)
}

fun Image.toBitmap(
width: Int,
height: Int,
config: android.graphics.Bitmap.Config,
config: Bitmap.Config,
): Bitmap {
if (this is BitmapImage &&
bitmap.width == width &&
Expand Down
18 changes: 17 additions & 1 deletion coil-core/src/nonAndroidMain/kotlin/coil3/Image.nonAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ actual fun Bitmap.asImage(shareable: Boolean): BitmapImage {
actual fun Image.toBitmap(
width: Int,
height: Int,
): Bitmap = toBitmap(width, height, ColorType.N32, ColorAlphaType.PREMUL, null)
): Bitmap {
val colorType: ColorType
val colorAlphaType: ColorAlphaType
val colorSpace: ColorSpace?

if (this is BitmapImage) {
colorType = bitmap.colorType
colorAlphaType = bitmap.imageInfo.colorAlphaType
colorSpace = bitmap.colorSpace
} else {
colorType = ColorType.N32
colorAlphaType = ColorAlphaType.PREMUL
colorSpace = null
}

return toBitmap(width, height, colorType, colorAlphaType, colorSpace)
}

fun Image.toBitmap(
width: Int,
Expand Down