From 662b71436eea6ddda4d15d18ec0946e946c6e4e9 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 25 Apr 2021 17:08:51 +0200 Subject: [PATCH] Cleanup dual page split (#4956) * Cleanup Dual Page Split * Move where images is processed * Change parameter name to imageStream * Use available instead of Int.MAX_VALUE * Update JavaDoc --- .../ui/reader/viewer/pager/PagerPageHolder.kt | 39 ++++++++++--------- .../ui/reader/viewer/pager/PagerViewer.kt | 5 ++- .../viewer/webtoon/WebtoonPageHolder.kt | 25 +++++++----- .../kanade/tachiyomi/util/system/ImageUtil.kt | 11 ++++-- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt index 633e7af31..2a9cf6635 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt @@ -235,16 +235,13 @@ class PagerPageHolder( readImageHeaderSubscription = Observable .fromCallable { val stream = streamFn().buffered(16) - openStream = stream + openStream = process(stream) ImageUtil.findImageType(stream) == ImageUtil.ImageType.GIF } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnNext { isAnimated -> - if (viewer.config.dualPageSplit) { - openStream = processDualPageSplit(openStream!!) - } if (!isAnimated) { initSubsamplingImageView().setImage(ImageSource.inputStream(openStream!!)) } else { @@ -257,21 +254,31 @@ class PagerPageHolder( .subscribe({}, {}) } - private fun processDualPageSplit(openStream: InputStream): InputStream { - var inputStream = openStream - val (isDoublePage, stream) = when (page) { - is InsertPage -> Pair(true, inputStream) - else -> ImageUtil.isDoublePage(inputStream) + private fun process(imageStream: InputStream): InputStream { + if (!viewer.config.dualPageSplit) { + return imageStream } - inputStream = stream - if (!isDoublePage) return inputStream + if (page is InsertPage) { + return splitInHalf(imageStream) + } + val isDoublePage = ImageUtil.isDoublePage(imageStream) + if (!isDoublePage) { + return imageStream + } + + onPageSplit() + + return splitInHalf(imageStream) + } + + private fun splitInHalf(imageStream: InputStream): InputStream { var side = when { viewer is L2RPagerViewer && page is InsertPage -> ImageUtil.Side.RIGHT - (viewer is R2LPagerViewer || viewer is VerticalPagerViewer) && page is InsertPage -> ImageUtil.Side.LEFT + viewer !is L2RPagerViewer && page is InsertPage -> ImageUtil.Side.LEFT viewer is L2RPagerViewer && page !is InsertPage -> ImageUtil.Side.LEFT - (viewer is R2LPagerViewer || viewer is VerticalPagerViewer) && page !is InsertPage -> ImageUtil.Side.RIGHT + viewer !is L2RPagerViewer && page !is InsertPage -> ImageUtil.Side.RIGHT else -> error("We should choose a side!") } @@ -282,11 +289,7 @@ class PagerPageHolder( } } - if (page !is InsertPage) { - onPageSplit() - } - - return ImageUtil.splitInHalf(inputStream, side) + return ImageUtil.splitInHalf(imageStream, side) } private fun onPageSplit() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt index 0ca303bf4..b5808f4bf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt @@ -385,7 +385,10 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { } fun onPageSplit(currentPage: ReaderPage, newPage: InsertPage) { - adapter.onPageSplit(currentPage, newPage, this::class.java) + activity.runOnUiThread { + // Need to insert on UI thread else images will go blank + adapter.onPageSplit(currentPage, newPage, this::class.java) + } } private fun cleanupPageSplit() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt index a66c0bba4..fc72f0974 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt @@ -281,22 +281,13 @@ class WebtoonPageHolder( readImageHeaderSubscription = Observable .fromCallable { val stream = streamFn().buffered(16) - openStream = stream + openStream = process(stream) ImageUtil.findImageType(stream) == ImageUtil.ImageType.GIF } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnNext { isAnimated -> - if (viewer.config.dualPageSplit) { - val (isDoublePage, stream) = ImageUtil.isDoublePage(openStream!!) - openStream = if (!isDoublePage) { - stream - } else { - val upperSide = if (viewer.config.dualPageInvert) ImageUtil.Side.LEFT else ImageUtil.Side.RIGHT - ImageUtil.splitAndMerge(stream, upperSide) - } - } if (!isAnimated) { val subsamplingView = initSubsamplingImageView() subsamplingView.isVisible = true @@ -315,6 +306,20 @@ class WebtoonPageHolder( addSubscription(readImageHeaderSubscription) } + private fun process(imageStream: InputStream): InputStream { + if (!viewer.config.dualPageSplit) { + return imageStream + } + + val isDoublePage = ImageUtil.isDoublePage(imageStream) + if (!isDoublePage) { + return imageStream + } + + val upperSide = if (viewer.config.dualPageInvert) ImageUtil.Side.LEFT else ImageUtil.Side.RIGHT + return ImageUtil.splitAndMerge(imageStream, upperSide) + } + /** * Called when the page has an error. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt index 61fdee35d..b21efff0b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt @@ -77,15 +77,20 @@ object ImageUtil { } /** - * Check whether the image is a double image (width > height), return the result and original stream + * Check whether the image is a double-page spread + * @return true if the width is greater than the height */ - fun isDoublePage(imageStream: InputStream): Pair { + fun isDoublePage(imageStream: InputStream): Boolean { + imageStream.mark(imageStream.available() + 1) + val imageBytes = imageStream.readBytes() val options = BitmapFactory.Options().apply { inJustDecodeBounds = true } BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size, options) - return Pair(options.outWidth > options.outHeight, ByteArrayInputStream(imageBytes)) + imageStream.reset() + + return options.outWidth > options.outHeight } /**