Skip to content

Commit

Permalink
Fix calling toBitmap with an underlying hardware bitmap.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Nov 6, 2024
1 parent dd7ddf3 commit d228aad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
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

0 comments on commit d228aad

Please sign in to comment.