Even more fixes to shifter

Fix to an odd page that could be mistaken as a start or end page, but is likely an end page. Added with the fact the first page is a spread, and the second is unknown and it gets funky.

source for these fixes: fix was applied for kanan-sama chap 40
This commit is contained in:
Jays2Kings 2023-03-12 16:55:49 -04:00
parent ddf97d0e9b
commit ea264d7ed0
2 changed files with 74 additions and 18 deletions

View file

@ -712,6 +712,7 @@ class PagerPageHolder(
val imageBytes2 by lazy { imageStream2.readBytes() }
val isLTR = (viewer !is R2LPagerViewer).xor(viewer.config.invertDoublePages)
val pages = page.chapter.pages
if (height < width) {
if (extraPage?.index == 1) {
setExtraPageBitmap(imageBytes2, isLTR)
@ -720,11 +721,15 @@ class PagerPageHolder(
val oldValue = page.fullPage
page.fullPage = true
delayPageUpdate {
val thirdPageIsStart = pages?.getOrNull(2)?.isStartPage == true
val extraPageIsEnd = extraPage?.isEndPage == true
if (page.index == 0 &&
(viewer.config.shiftDoublePage || extraPage?.isEndPage == true) &&
oldValue != true
(
(viewer.config.shiftDoublePage && !thirdPageIsStart) ||
extraPage?.isEndPage == true
) && oldValue != true
) {
viewer.activity.shiftDoublePages(extraPage?.isEndPage == true, extraPage)
viewer.activity.shiftDoublePages(extraPageIsEnd || thirdPageIsStart, extraPage)
} else {
viewer.splitDoublePages(page)
}
@ -734,7 +739,6 @@ class PagerPageHolder(
}
val isNotEndPage: ReaderPage.() -> Boolean =
{ isEndPage != true || page.paddedPageConfidence > paddedPageConfidence }
val pages = page.chapter.pages
var earlyImageBitmap2: Bitmap? = null
val isFirstPageNotEnd by lazy { pages?.get(0)?.let { it.isNotEndPage() } != false }
val isThirdPageNotEnd by lazy { pages?.getOrNull(2)?.let { it.isNotEndPage() } == true }
@ -808,16 +812,31 @@ class PagerPageHolder(
closeStreams(imageStream, imageStream2)
extraPage?.let { extraPage ->
if (extraPage.index <= 2 && extraPage.isEndPage == null &&
val shouldSubShiftAnyway = !viewer.activity.manuallyShiftedPages &&
extraPage.isStartPage == true && extraPage.paddedPageConfidence >= 2
if (extraPage.index <= 2 && extraPage.paddedPageConfidence != 3 &&
extraPage.isStartPage == null && extraPage.fullPage == null
) {
extraPage.paddedPageConfidence = imageBitmap2.isPagePadded(rightSide = isLTR)
extraPage.isStartPage = extraPage.paddedPageConfidence > 0
val startingConfidence = imageBitmap2.isPagePadded(rightSide = isLTR)
if (startingConfidence > extraPage.paddedPageConfidence) {
extraPage.paddedPageConfidence = startingConfidence
extraPage.isStartPage = extraPage.paddedPageConfidence > 0
if (extraPage.isEndPage == true) {
extraPage.isEndPage = false
}
} else {
extraPage.isStartPage = false
}
if (extraPage.isStartPage == true) {
shiftDoublePages(page.index == 0 || pages?.get(0)?.fullPage == true)
this.extraPage = null
return supportHingeIfThere(imageBytes.inputStream())
}
} else if (shouldSubShiftAnyway && page.index == 1 && extraPage.isEndPage == false &&
!viewer.config.shiftDoublePage
) {
shiftDoublePages(true)
return supportHingeIfThere(imageBytes.inputStream())
}
}
// If page has been removed in another thread, don't show it

View file

@ -646,34 +646,71 @@ object ImageUtil {
* @return An int based on confidence, 0 meaning not padded, 1 meaning barely padded,
* 2 meaning likely padded, 3 meaining definitely padded
* @param rightSide: When true, check if its a single left side page, else right side
* */
*/
fun Bitmap.isPagePadded(rightSide: Boolean): Int {
val booleans = listOf(true, false)
return when {
booleans.any { isSidePadded(!rightSide, checkWhite = it) } -> 0
booleans.any { isSidePadded(rightSide, checkWhite = it) } -> 3
booleans.any { isOneSideMorePadded(rightSide, checkWhite = it) } -> 2
booleans.any { isSidePadded(rightSide, checkWhite = it, halfCheck = true) } -> 1
booleans.any { isSidePadded(!rightSide, checkWhite = it) > 1 } -> 0
booleans.any {
when (isSidePadded(rightSide, checkWhite = it)) {
2 -> true
1 -> isSideLonger(rightSide, checkWhite = it)
else -> false
}
} -> 3
booleans.any { isSideLonger(rightSide, checkWhite = it) } -> 2
booleans.any { isOneSideMorePadded(rightSide, checkWhite = it) } -> 1
else -> 0
}
}
/** Returns if one side has a vertical padding and the other side does not */
private fun Bitmap.isSidePadded(rightSide: Boolean, checkWhite: Boolean, halfCheck: Boolean = false): Boolean {
/**
* Returns if one side has a vertical padding and the other side does not,
* 2 for def, 1 for maybe, 0 if not at all
*/
private fun Bitmap.isSidePadded(rightSide: Boolean, checkWhite: Boolean, halfCheck: Boolean = false): Int {
val left = (width * 0.0275).toInt()
val right = width - left
val paddedSide = if (rightSide) right else left
val unPaddedSide = if (!rightSide) right else left
return (1 until 30).count {
val paddedCount = (1 until 50).count {
// if all of a side is padded (the left page usually has a white padding on the right when scanned)
getPixel(paddedSide, (height * (it / 30f)).roundToInt()).isWhiteOrDark(checkWhite)
} >= (if (halfCheck) 15 else 27) && !(1 until 50).all {
getPixel(paddedSide, (height * (it / 50f)).roundToInt()).isWhiteOrDark(checkWhite)
}
val isNotFullyUnPadded = !(1 until 50).all {
// and if all of the other side isn't padded
getPixel(unPaddedSide, (height * (it / 50f)).roundToInt()).isWhiteOrDark(checkWhite)
}
return if (isNotFullyUnPadded) {
if (paddedCount == 49) 2 else if (paddedCount >= (if (halfCheck) 25 else 47)) 1 else 0
} else {
0
}
}
/** Returns if one side is more padded than the other */
/** Returns if one side has a longer streak (of white or black) than the other */
private fun Bitmap.isSideLonger(rightSide: Boolean, checkWhite: Boolean): Boolean {
if (isSidePadded(rightSide, checkWhite, true) == 0) return false
val left = (width * 0.0275).toInt()
val right = width - left
val step = 70
val list = listOf((1 until step), (1 until step).reversed())
val streakFunc: (Int) -> Int = { side ->
list.maxOf { range ->
var count = 0
for (it in range) {
val pixel = getPixel(side, (height * (it / step.toFloat())).roundToInt())
if (pixel.isWhiteOrDark(checkWhite)) ++count else return@maxOf count
}
count
}
}
val paddedSide = if (rightSide) right else left
val unPaddedSide = if (!rightSide) right else left
return streakFunc(paddedSide) > streakFunc(unPaddedSide)
}
/** Returns if one side is more horizontally padded than the other */
private fun Bitmap.isOneSideMorePadded(rightSide: Boolean, checkWhite: Boolean): Boolean {
val middle = (height * 0.475).roundToInt()
val middle2 = (height * 0.525).roundToInt()