Modern Apps git

← Modern-Apps.git

pdf: copy warpQuadToBitmap fallback so result outlives source recycle

Commit
003327c1d697850faef978f80b085aa1cec42c29
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:11 UTC (28 days ago)
Parents
05b6fca
The degenerate-quad fallback returned Bitmap.createBitmap(src, ...), a
subset that shares the source's pixel storage. Every caller recycles the
source immediately after this returns (SubcroppedImage for the crop
preview, drawCropped before the export write), so drawing the returned
bitmap later crashed with 'trying to use a recycled bitmap'. Reachable by
dragging crop corners coincident/collinear, which makes setPolyToPoly fail.

Return an independent ARGB_8888 copy instead; the success branch already
returns a fresh bitmap, so caller ownership is now uniform.

Diff

pdf/src/main/java/com/vayunmathur/pdf/util/PdfExporter.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/util/PdfExporter.kt b/pdf/src/main/java/com/vayunmathur/pdf/util/PdfExporter.ktindex 1667a3870e..4c04ffbc89 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/util/PdfExporter.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/util/PdfExporter.kt@@ -269,6 +269,11 @@ fun warpQuadToBitmap(src: Bitmap, quad: Quadrilateral, width: Int, height: Int):         val top = (bounds.top * src.height).roundToInt().coerceIn(0, src.height - 1)         val w = targetWidth.coerceAtMost(src.width - left)         val h = targetHeight.coerceAtMost(src.height - top)-        Bitmap.createBitmap(src, left, top, w, h)+        // The subset shares the source's pixel storage, so hand back an+        // independent copy: every caller recycles the source right after this+        // returns (and the result later), which would otherwise free pixels+        // the returned bitmap still points at.+        val subset = Bitmap.createBitmap(src, left, top, w, h)+        subset.copy(Bitmap.Config.ARGB_8888, true).also { subset.recycle() }     } }