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 633e7af31b..2a9cf66351 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 0ca303bf41..b5808f4bf6 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 a66c0bba4c..fc72f09748 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 61fdee35dc..b21efff0bf 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 } /**