revert(PagerPageHolder): Revert "Split mergeOrSplitPages code into several functions"

This reverts commit 1ce2663dfa.
This commit is contained in:
Ahmad Ansori Palembani 2024-08-07 13:40:23 +07:00
parent d3a47bdfe3
commit e6eff2c3d0
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -586,45 +586,91 @@ class PagerPageHolder(
return errorLayout!!.configureView(imageUrl) return errorLayout!!.configureView(imageUrl)
} }
private fun BufferedSource.bitmapSafe(): Bitmap? { private suspend fun mergeOrSplitPages(imageSource: BufferedSource, imageSource2: BufferedSource?): BufferedSource {
return try { if (ImageUtil.isAnimatedAndSupported(imageSource)) {
BitmapFactory.decodeStream(this.peek().inputStream()) // FIXME: Animated images is duplicating instead of being split
} catch (e: Exception) { if (page.longPage == null) {
Logger.e { "Failed to decode image ${e.message}" } page.longPage = true
null /*
if (viewer.config.splitPages || imageSource2 != null) {
splitDoublePages()
}
*/
}
scope.launchUI { progressIndicator.completeAndFadeOut() }
return imageSource
} }
} if (page.longPage == true && viewer.config.splitPages) {
val imageBitmap = try {
private suspend fun splitPage(imageSource: BufferedSource, isLTR: Boolean): BufferedSource { BitmapFactory.decodeStream(imageSource.peek().inputStream())
val imageBitmap = imageSource.bitmapSafe() ?: return imageSource } catch (e: Exception) {
closeSources(imageSource) Logger.e { "Cannot split page ${e.message}" }
return ImageUtil.splitBitmap(imageBitmap, (page.firstHalf == false).xor(!isLTR)) { return imageSource
scope.launchUI { }
if (it == 100) { val isLTR = (viewer !is R2LPagerViewer).xor(viewer.config.invertDoublePages)
progressIndicator.completeAndFadeOut() return ImageUtil.splitBitmap(imageBitmap, (page.firstHalf == false).xor(!isLTR)) {
} else { scope.launchUI {
progressIndicator.setProgress(it) if (it == 100) {
progressIndicator.completeAndFadeOut()
} else {
progressIndicator.setProgress(it)
}
} }
} }
} }
} if (imageSource2 == null) {
if (viewer.config.splitPages && page.longPage == null) {
private suspend fun mergePages(imageSource: BufferedSource, imageSource2: BufferedSource, isLTR: Boolean): BufferedSource { val imageBitmap = try {
BitmapFactory.decodeStream(imageSource.peek().inputStream())
} catch (e: Exception) {
page.longPage = true
splitDoublePages()
Logger.e { "Cannot split page ${e.message}" }
return imageSource
}
val height = imageBitmap.height
val width = imageBitmap.width
return if (height < width) {
closeSources(imageSource)
page.longPage = true
splitDoublePages()
val isLTR = (viewer !is R2LPagerViewer).xor(viewer.config.invertDoublePages)
return ImageUtil.splitBitmap(imageBitmap, !isLTR) {
scope.launchUI {
if (it == 100) {
progressIndicator.completeAndFadeOut()
} else {
progressIndicator.setProgress(it)
}
}
}
} else {
page.longPage = false
imageSource
}
}
return supportHingeIfThere(imageSource)
}
if (page.fullPage == true) return supportHingeIfThere(imageSource) if (page.fullPage == true) return supportHingeIfThere(imageSource)
val imageBitmap = try {
val imageBitmap = imageSource.bitmapSafe() BitmapFactory.decodeStream(imageSource.peek().inputStream())
if (imageBitmap == null) { } catch (e: Exception) {
closeSources(imageSource2) closeSources(imageSource2)
page.fullPage = true page.fullPage = true
splitDoublePages() splitDoublePages()
Logger.e { "Cannot combine pages ${e.message}" }
return supportHingeIfThere(imageSource) return supportHingeIfThere(imageSource)
} }
scope.launchUI { progressIndicator.setProgress(96) } scope.launchUI { progressIndicator.setProgress(96) }
val height = imageBitmap.height
val width = imageBitmap.width
val isLTR = (viewer !is R2LPagerViewer).xor(viewer.config.invertDoublePages)
val pages = page.chapter.pages val pages = page.chapter.pages
if (imageBitmap.height < imageBitmap.width) { if (height < width) {
if (extraPage?.index == 1) { if (extraPage?.index == 1) {
setExtraPageBitmap(imageSource2, isLTR) setExtraPageBitmap(imageSource2.inputStream(), isLTR)
} }
closeSources(imageSource2) closeSources(imageSource2)
val oldValue = page.fullPage val oldValue = page.fullPage
@ -646,19 +692,16 @@ class PagerPageHolder(
} }
return supportHingeIfThere(imageSource) return supportHingeIfThere(imageSource)
} }
var earlyImageBitmap2: Bitmap? = null
val isNotEndPage: ReaderPage.() -> Boolean = val isNotEndPage: ReaderPage.() -> Boolean =
{ isEndPage != true || (page.endPageConfidence ?: 0) > (endPageConfidence ?: 0) } { isEndPage != true || (page.endPageConfidence ?: 0) > (endPageConfidence ?: 0) }
val isFirstPageNotEnd by lazy { pages?.get(0)?.isNotEndPage() != false } var earlyImageBitmap2: Bitmap? = null
val isThirdPageNotEnd by lazy { pages?.getOrNull(2)?.isNotEndPage() == true } val isFirstPageNotEnd by lazy { pages?.get(0)?.let { it.isNotEndPage() } != false }
val isThirdPageNotEnd by lazy { pages?.getOrNull(2)?.let { it.isNotEndPage() } == true }
val shouldShiftAnyway = !viewer.activity.manuallyShiftedPages && page.endPageConfidence == 3 val shouldShiftAnyway = !viewer.activity.manuallyShiftedPages && page.endPageConfidence == 3
if (page.index <= 2 && page.isEndPage == null && page.fullPage == null) { if (page.index <= 2 && page.isEndPage == null && page.fullPage == null) {
page.endPageConfidence = imageBitmap.isPagePadded(rightSide = !isLTR) page.endPageConfidence = imageBitmap.isPagePadded(rightSide = !isLTR)
if (extraPage?.index == 1 && extraPage?.isEndPage == null) { if (extraPage?.index == 1 && extraPage?.isEndPage == null) {
earlyImageBitmap2 = setExtraPageBitmap(imageSource2, isLTR) earlyImageBitmap2 = setExtraPageBitmap(imageSource2.peek().inputStream(), isLTR)
} }
if (page.index == 1 && page.isEndPage == true && viewer.config.shiftDoublePage && if (page.index == 1 && page.isEndPage == true && viewer.config.shiftDoublePage &&
(isFirstPageNotEnd || isThirdPageNotEnd) (isFirstPageNotEnd || isThirdPageNotEnd)
@ -692,18 +735,29 @@ class PagerPageHolder(
return supportHingeIfThere(imageSource) return supportHingeIfThere(imageSource)
} }
val imageBitmap2 = earlyImageBitmap2 ?: imageSource2.bitmapSafe() val imageBitmap2 = earlyImageBitmap2 ?: try {
BitmapFactory.decodeStream(imageSource2.peek().inputStream())
closeSources(imageSource2) } catch (e: Exception) {
closeSources(imageSource2)
extraPage?.fullPage = true
page.isolatedPage = true
splitDoublePages()
Logger.e { "Cannot combine pages ${e.message}" }
return supportHingeIfThere(imageSource)
}
scope.launchUI { progressIndicator.setProgress(97) } scope.launchUI { progressIndicator.setProgress(97) }
val height2 = imageBitmap2.height
val width2 = imageBitmap2.width
if (imageBitmap2 == null || imageBitmap2.height < imageBitmap2.width) { if (height2 < width2) {
closeSources(imageSource2)
extraPage?.fullPage = true extraPage?.fullPage = true
page.isolatedPage = true page.isolatedPage = true
splitDoublePages() splitDoublePages()
return supportHingeIfThere(imageSource) return supportHingeIfThere(imageSource)
} }
val bg = ThemeUtil.readerBackgroundColor(viewer.config.readerTheme)
closeSources(imageSource2)
extraPage?.let { extraPage -> extraPage?.let { extraPage ->
val shouldSubShiftAnyway = !viewer.activity.manuallyShiftedPages && val shouldSubShiftAnyway = !viewer.activity.manuallyShiftedPages &&
extraPage.isStartPage == true && extraPage.endPageConfidence == 0 extraPage.isStartPage == true && extraPage.endPageConfidence == 0
@ -725,16 +779,10 @@ class PagerPageHolder(
} }
} }
// If page has been removed in another thread, don't show it // If page has been removed in another thread, don't show it
if (extraPage == null) return supportHingeIfThere(imageSource) if (extraPage == null) {
return supportHingeIfThere(imageSource)
return ImageUtil.mergeBitmaps( }
imageBitmap, return ImageUtil.mergeBitmaps(imageBitmap, imageBitmap2, isLTR, bg, viewer.config.hingeGapSize, context) {
imageBitmap2,
isLTR,
ThemeUtil.readerBackgroundColor(viewer.config.readerTheme),
viewer.config.hingeGapSize,
context,
) {
scope.launchUI { scope.launchUI {
if (it == 100) { if (it == 100) {
progressIndicator.completeAndFadeOut() progressIndicator.completeAndFadeOut()
@ -745,59 +793,12 @@ class PagerPageHolder(
} }
} }
private suspend fun handleSinglePage(imageSource: BufferedSource, isLTR: Boolean): BufferedSource { private fun setExtraPageBitmap(imageStream: InputStream, isLTR: Boolean): Bitmap? {
if (viewer.config.splitPages && page.longPage == null) { val earlyImageBitmap2 = try {
val imageBitmap = imageSource.bitmapSafe() BitmapFactory.decodeStream(imageStream)
if (imageBitmap == null) { } catch (_: Exception) {
page.longPage = true return null
splitDoublePages()
return imageSource
}
return if (imageBitmap.height < imageBitmap.width) {
closeSources(imageSource)
page.longPage = true
splitDoublePages()
return ImageUtil.splitBitmap(imageBitmap, !isLTR) {
scope.launchUI {
if (it == 100) {
progressIndicator.completeAndFadeOut()
} else {
progressIndicator.setProgress(it)
}
}
}
} else {
page.longPage = false
imageSource
}
} }
return supportHingeIfThere(imageSource)
}
private suspend fun mergeOrSplitPages(imageSource: BufferedSource, imageSource2: BufferedSource?): BufferedSource {
if (ImageUtil.isAnimatedAndSupported(imageSource)) {
// Don't do anything with animated images, splitting may cause the animation to de-sync with one another,
// and merging would only cause lag
scope.launchUI { progressIndicator.completeAndFadeOut() }
return imageSource
}
val isLTR = (viewer !is R2LPagerViewer).xor(viewer.config.invertDoublePages)
if (page.longPage == true && viewer.config.splitPages) {
return splitPage(imageSource, isLTR)
}
if (imageSource2 != null) {
mergePages(imageSource, imageSource2, isLTR)
}
return handleSinglePage(imageSource, isLTR)
}
private fun setExtraPageBitmap(imageSource: BufferedSource, isLTR: Boolean): Bitmap? {
val earlyImageBitmap2 = imageSource.bitmapSafe() ?: return null
val paddedPageConfidence = earlyImageBitmap2.isPagePadded(rightSide = !isLTR) val paddedPageConfidence = earlyImageBitmap2.isPagePadded(rightSide = !isLTR)
if (paddedPageConfidence == 3) { if (paddedPageConfidence == 3) {
extraPage?.endPageConfidence = paddedPageConfidence extraPage?.endPageConfidence = paddedPageConfidence