From 8e8394985dd06ade97b3f6dc4daa6fadaf286723 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:21:54 +0800 Subject: [PATCH] Don't crop borders for webtoons Webtoons are typically split across multiple pages, they require consistent width to maintain alignment. --- .../ehviewer/coil/CropBorderInterceptor.kt | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/coil/CropBorderInterceptor.kt b/app/src/main/kotlin/com/hippo/ehviewer/coil/CropBorderInterceptor.kt index 88b381905..19fcdec76 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/coil/CropBorderInterceptor.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/coil/CropBorderInterceptor.kt @@ -17,6 +17,7 @@ import com.hippo.ehviewer.image.detectBorder import com.hippo.ehviewer.util.isAtLeastO private const val CROP_THRESHOLD = 0.75f +private const val WEBTOON_THRESHOLD = 3 private val maybeCropBorderKey = Extras.Key(default = false) @@ -47,23 +48,28 @@ object CropBorderInterceptor : Interceptor { val image = result.image if (image is BitmapImage) { val bitmap = image.bitmap - val array = detectBorder(bitmap) - val r = IntRect(array[0], array[1], array[2], array[3]) - val minWidth = image.width * CROP_THRESHOLD - val minHeight = image.height * CROP_THRESHOLD - val maybeHwBitmap = if (hw) { + val rect = if (image.height / image.width < WEBTOON_THRESHOLD) { + val array = detectBorder(bitmap) + val minWidth = image.width * CROP_THRESHOLD + val minHeight = image.height * CROP_THRESHOLD + IntRect(array[0], array[1], array[2], array[3]).takeIf { + it.width > minWidth && it.height > minHeight + } + } else { + null + } + val maybeHwImage = if (hw) { // Upload to Graphical Buffer to accelerate render // May fail if the image is too long - bitmap.copy(Bitmap.Config.HARDWARE, false)?.apply { + bitmap.copy(Bitmap.Config.HARDWARE, false)?.run { bitmap.recycle() - } ?: bitmap + asCoilImage() + } ?: image } else { - bitmap + image } - val shouldOverride = r.width > minWidth && r.height > minHeight - val maybeHwImage = maybeHwBitmap.asCoilImage() - val new = if (shouldOverride) { - BitmapImageWithRect(r, maybeHwImage) + val new = if (rect != null) { + BitmapImageWithRect(rect, maybeHwImage) } else { maybeHwImage }