fix: use integer division (floor) in calculateInSampleSize

fastRoundToInt() can round the inSampleSize ratio UP, causing the
decoded bitmap to be smaller than the target screen dimensions and
requiring upscaling.  Integer division always floors, guaranteeing
the decoded bitmap is at least as large as needed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Anthonyy232andClaude Sonnet 4.6 committed 2026-03-28 11:24:09 -07:00
1 parent c93e2f71f1
commit c72cdc39a8
1 file changed
+6 -3
@@ -109,9 +109,12 @@ fun calculateInSampleSize(imageSize: Size, width: Int, height: Int): Int {
if (width == 0 || height == 0) return 1
if (imageSize.width > width || imageSize.height > height) {
val heightRatio = (imageSize.height.toFloat() / height.toFloat()).fastRoundToInt()
val widthRatio = (imageSize.width.toFloat() / width.toFloat()).fastRoundToInt()
return (if (heightRatio < widthRatio) heightRatio else widthRatio).coerceAtLeast(1)
// Integer division (floor) ensures inSampleSize is never larger than needed,
// guaranteeing the decoded bitmap is at least as wide/tall as the target.
// fastRoundToInt() can round UP, producing a bitmap smaller than the target.
val heightRatio = imageSize.height / height
val widthRatio = imageSize.width / width
return minOf(heightRatio, widthRatio).coerceAtLeast(1)
}
return 1
}