Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.
Rust androiddocument-scannerdocximage-editorjetpack-composekotlinliquid-glassofflineopen-sourcepdfpdf-compressorpdf-editorpdf-mergerpdf-readerpdf-splitterpdf-toolspdf-viewerpptxspreadsheet-editorxlsx
Fix crash opening any image; recents menu visibly follows Liquid Glass profiles
chethan61671d233ff8a06
diff --git a/app/src/main/java/com/chethan616/clearpdf/imageeditor/engine/ImageIo.kt b/app/src/main/java/com/chethan616/clearpdf/imageeditor/engine/ImageIo.ktindex 306285c..5644d10 100644--- a/app/src/main/java/com/chethan616/clearpdf/imageeditor/engine/ImageIo.kt+++ b/app/src/main/java/com/chethan616/clearpdf/imageeditor/engine/ImageIo.kt@@ -40,6 +40,23 @@ object ImageIo {data class Bounds(val width: Int, val height: Int)+ /**+ * Power-of-two decode sample for a [w] x [h] image to land within twice [maxDim] and four times+ * [maxPixels] (the exact downscale happens after decoding). Overflow-safe: `maxPixels * 4` with+ * the default Long.MAX_VALUE used to wrap negative, so the "too big" test was always true,+ * the sample doubled until it wrapped to 0, and the next division crashed opening ANY image.+ */+ internal fun sampleSize(w: Int, h: Int, maxDim: Int, maxPixels: Long): Int {+ if (w <= 0 || h <= 0) return 1+ val dimBudget = maxDim.toLong().coerceAtLeast(1) * 2+ val pixelBudget = if (maxPixels > Long.MAX_VALUE / 4) Long.MAX_VALUE else maxPixels.coerceAtLeast(1) * 4+ var sample = 1+ while (sample < (1 shl 20) &&+ (max(w, h).toLong() / sample > dimBudget || (w.toLong() / sample) * (h.toLong() / sample) > pixelBudget)+ ) sample *= 2+ return sample+ }+/** Pixel size after EXIF orientation is applied. */fun bounds(context: Context, uri: Uri): Bounds? {val o = BitmapFactory.Options().apply { inJustDecodeBounds = true }@@ -59,8 +76,7 @@ object ImageIo {val w = o.outWidthval h = o.outHeightif (w <= 0 || h <= 0) return null- var sample = 1- while (max(w, h) / sample > maxDim * 2 || (w.toLong() / sample) * (h.toLong() / sample) > maxPixels * 4) sample *= 2+ val sample = sampleSize(w, h, maxDim, maxPixels)val opts = BitmapFactory.Options().apply {inSampleSize = sampleinPreferredConfig = Bitmap.Config.ARGB_8888diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/RecentContextMenu.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/RecentContextMenu.ktindex 770cc4d..3e0fd58 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/RecentContextMenu.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/RecentContextMenu.kt@@ -243,7 +243,7 @@ private fun MenuPanel(Column(Modifier.width(MenuWidth)- .glassMenu(backdrop, dark = com.chethan616.clearpdf.ui.theme.LocalIsDarkMode.current, shape = { RoundedRectangle(22.dp * com.chethan616.clearpdf.ui.components.GlassSettings.style.corners) }, surfaceAlpha = 0.88f)+ .glassMenu(backdrop, dark = com.chethan616.clearpdf.ui.theme.LocalIsDarkMode.current, shape = { RoundedRectangle(22.dp * com.chethan616.clearpdf.ui.components.GlassSettings.style.corners) })// Swallow taps on the panel's padding so they don't fall through to the scrim..clickable(interactionSource = remember { MutableInteractionSource() }, indication = null) {}.semantics { contentDescription = title }@@ -280,7 +280,14 @@ private fun QuickTile(action: RecentMenuAction, fg: Color, modifier: Modifier, escaleX = s; scaleY = s}.liquidRowClick(corner = 14.dp, onClick = action.onClick)- .drawBehind { drawRoundRect(action.tint.copy(alpha = 0.14f), cornerRadius = CornerRadius(14.dp.toPx())) }+ .drawBehind {+ // Tile wash follows the Liquid Glass Tint, like every glass surface.+ val t = com.chethan616.clearpdf.ui.components.GlassSettings.style.tint+ drawRoundRect(+ action.tint.copy(alpha = (0.14f * t).coerceIn(0f, 0.5f)),+ cornerRadius = CornerRadius(14.dp.toPx() * com.chethan616.clearpdf.ui.components.GlassSettings.style.corners)+ )+ }.padding(vertical = 7.dp, horizontal = 2.dp),horizontalAlignment = Alignment.CenterHorizontally,verticalArrangement = Arrangement.spacedBy(3.dp, Alignment.CenterVertically)diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImageEditorViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImageEditorViewModel.ktindex 9cc7ace..780e0e3 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImageEditorViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImageEditorViewModel.kt@@ -171,10 +171,12 @@ class ImageEditorViewModel(app: Application) : AndroidViewModel(app) {started = truesourceUri = uriviewModelScope.launch {- val name = withContext(Dispatchers.IO) { ImageIo.displayName(context, uri) }- val bounds = withContext(Dispatchers.IO) { ImageIo.bounds(context, uri) }- val bmp = withContext(Dispatchers.IO) { ImageIo.decode(context, uri, PREVIEW_MAX) }- val exif = withContext(Dispatchers.IO) { ImageIo.readExif(context, uri) }+ // Any failure (unreadable file, a format the decoder rejects, OOM on a huge image) lands on+ // the "Couldn't open" state instead of taking the app down.+ val name = withContext(Dispatchers.IO) { runCatching { ImageIo.displayName(context, uri) }.getOrDefault("") }+ val bounds = withContext(Dispatchers.IO) { runCatching { ImageIo.bounds(context, uri) }.getOrNull() }+ val bmp = withContext(Dispatchers.IO) { runCatching { ImageIo.decode(context, uri, PREVIEW_MAX) }.getOrNull() }+ val exif = withContext(Dispatchers.IO) { runCatching { ImageIo.readExif(context, uri) }.getOrDefault(emptyList()) }if (bmp == null || bounds == null) {_state.value = UiState(fileName = name, isLoading = false, error = "Couldn't open this image.")return@launchdiff --git a/app/src/test/java/com/chethan616/clearpdf/imageeditor/engine/ImageIoSampleSizeTest.kt b/app/src/test/java/com/chethan616/clearpdf/imageeditor/engine/ImageIoSampleSizeTest.ktnew file mode 100644index 0000000..162f159--- /dev/null+++ b/app/src/test/java/com/chethan616/clearpdf/imageeditor/engine/ImageIoSampleSizeTest.kt@@ -0,0 +1,31 @@+package com.chethan616.clearpdf.imageeditor.engine++import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test++/** Opening any image used to crash: the default pixel budget overflowed and the sample hit 0. */+class ImageIoSampleSizeTest {++ @Test fun defaultPixelBudgetDoesNotOverflow() {+ // The image editor's open path: only a dimension cap, pixel budget left at its default.+ val s = ImageIo.sampleSize(4000, 3000, maxDim = 2048, maxPixels = Long.MAX_VALUE)+ assertEquals(1, s)+ }++ @Test fun downsamplesHugeImagesToTheDimensionBudget() {+ val s = ImageIo.sampleSize(16000, 12000, maxDim = 2048, maxPixels = Long.MAX_VALUE)+ assertTrue(s > 1)+ assertTrue(16000 / s <= 4096)+ }++ @Test fun respectsAPixelBudget() {+ val s = ImageIo.sampleSize(8000, 8000, maxDim = Int.MAX_VALUE, maxPixels = 4_000_000)+ assertTrue((8000L / s) * (8000L / s) <= 16_000_000)+ }++ @Test fun neverReturnsZeroEvenForAbsurdInputs() {+ assertTrue(ImageIo.sampleSize(Int.MAX_VALUE, Int.MAX_VALUE, 1, 1) > 0)+ assertEquals(1, ImageIo.sampleSize(0, 100, 10, 10))+ }+}