Skip to content

Commit

Permalink
Prevent crashes by ColorSpace.toComposeColorSpace()
Browse files Browse the repository at this point in the history
Fixes #129
  • Loading branch information
saket committed Jan 31, 2025
1 parent 3873610 commit 7593f13
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
3 changes: 3 additions & 0 deletions zoomable-image/coil3/src/androidTest/assets/p3_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
Expand Down Expand Up @@ -632,6 +631,37 @@ class Coil3ImageSourceTest {
assertThat(loadCount).isEqualTo(1)
}

// Regression test for https://github.com/saket/telephoto/issues/129.
@Test fun do_not_crash_if_the_color_space_cannot_be_parsed_by_compose_ui() = runTest {
serverRule.server.dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest) = assetAsResponse("p3_image.jpg")
}
val imageUrl = withContext(Dispatchers.IO) {
serverRule.server.url("ignored").toString()
}

lateinit var imageState: ZoomableImageState
rule.setContent {
ZoomableAsyncImage(
state = rememberZoomableImageState().also { imageState = it },
modifier = Modifier.fillMaxSize(),
model = ImageRequest.Builder(LocalContext.current)
.data(imageUrl)
.allowHardware(false) // Unsupported by Screenshot.capture()
.listener(onError = { _, res ->
res.throwable.printStackTrace()
})
.build(),
contentDescription = null
)
}

rule.waitUntil { imageState.isImageDisplayed }
rule.runOnIdle {
dropshots.assertSnapshot(rule.activity)
}
}

context(TestScope)
private fun resolve(
canvasSize: Size = Size(1080f, 1920f),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.graphics.colorspace.ColorSpace
import androidx.compose.ui.graphics.toAndroidColorSpace
import androidx.compose.ui.graphics.toComposeColorSpace
import dev.drewhamilton.poko.Poko
import kotlin.LazyThreadSafetyMode.NONE
import android.graphics.ColorSpace as AndroidColorSpace
import androidx.compose.ui.graphics.colorspace.ColorSpace as ComposeColorSpace

Expand All @@ -19,16 +20,21 @@ import androidx.compose.ui.graphics.colorspace.ColorSpace as ComposeColorSpace
@Immutable
class ImageBitmapOptions internal constructor(
val config: ImageBitmapConfig = ImageBitmapConfig.Argb8888,
val colorSpace: ColorSpace? = null,
internal val androidColorSpace: AndroidColorSpace? = null,
colorSpace: () -> ColorSpace?,
) {
// The compose color space is computed lazily to prevent crashes by bad Android <> Compose UI
// conversions. Example: https://issuetracker.google.com/issues/377021410#comment3
@Suppress("unused")
val colorSpace: ColorSpace? by lazy(NONE) { colorSpace() }

// TODO: remove when https://issuetracker.google.com/issues/377021410 is resolved.
constructor(
config: ImageBitmapConfig = ImageBitmapConfig.Argb8888,
colorSpace: ComposeColorSpace? = null,
) : this(
config = config,
colorSpace = colorSpace,
colorSpace = { colorSpace },
androidColorSpace = if (SDK_INT >= 26) colorSpace?.toAndroidColorSpace() else null,
)

Expand All @@ -44,7 +50,7 @@ fun ImageBitmapOptions(from: Bitmap): ImageBitmapOptions {
val androidColorSpace = if (SDK_INT >= 26) from.colorSpace else null
return ImageBitmapOptions(
config = from.config!!.toComposeConfig(),
colorSpace = if (SDK_INT >= 26) androidColorSpace?.toComposeColorSpace() else null,
colorSpace = { if (SDK_INT >= 26) androidColorSpace?.toComposeColorSpace() else null },
androidColorSpace = androidColorSpace,
)
}
Expand Down

0 comments on commit 7593f13

Please sign in to comment.