Parcourir la source

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
Andreas il y a 4 ans
Parent
commit
662b71436e

+ 21 - 18
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
+        }
+
+        if (page is InsertPage) {
+            return splitInHalf(imageStream)
+        }
+
+        val isDoublePage = ImageUtil.isDoublePage(imageStream)
+        if (!isDoublePage) {
+            return imageStream
         }
-        inputStream = stream
 
-        if (!isDoublePage) return inputStream
+        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() {

+ 4 - 1
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() {

+ 15 - 10
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.
      */

+ 8 - 3
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<Boolean, InputStream> {
+    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
     }
 
     /**