Fixed loading of background via httppageloader

more auto work
This commit is contained in:
Jay 2019-05-14 00:03:25 -07:00
parent dc4555f032
commit b36703a91f
2 changed files with 34 additions and 18 deletions

View File

@ -217,11 +217,13 @@ class HttpPageLoader(
} }
} }
.doOnNext { .doOnNext {
page.stream = { chapterCache.getImageFile(imageUrl).inputStream() }
if (preferences.readerTheme().get() == 2) { if (preferences.readerTheme().get() == 2) {
val image = BitmapFactory.decodeStream(chapterCache.getImageFile(imageUrl).inputStream()) val stream = chapterCache.getImageFile(imageUrl).inputStream()
val image = BitmapFactory.decodeStream(stream)
page.bg = ImageUtil.autoSetBackground(image) page.bg = ImageUtil.autoSetBackground(image)
stream.close()
} }
page.stream = { chapterCache.getImageFile(imageUrl).inputStream() }
page.status = Page.READY page.status = Page.READY
} }
.doOnError { page.status = Page.ERROR } .doOnError { page.status = Page.ERROR }

View File

@ -98,6 +98,10 @@ object ImageUtil {
var overallWhitePixels = 0 var overallWhitePixels = 0
var overallBlackPixels = 0 var overallBlackPixels = 0
var topBlackStreak = 0
var topWhiteStreak = 0
var botBlackStreak = 0
var botWhiteStreak = 0
outer@ for (x in intArrayOf(left, right, left - offsetX, right + offsetX)) { outer@ for (x in intArrayOf(left, right, left - offsetX, right + offsetX)) {
var whitePixelsStreak = 0 var whitePixelsStreak = 0
var whitePixels = 0 var whitePixels = 0
@ -106,18 +110,18 @@ object ImageUtil {
var blackStreak = false var blackStreak = false
var whiteStrak = false var whiteStrak = false
val notOffset = x == left || x == right val notOffset = x == left || x == right
for (y in (0 until image.height step image.height / 25)) { for ((index, y) in (0 until image.height step image.height / 25).withIndex()) {
val pixel = image.getPixel(x, y) val pixel = image.getPixel(x, y)
val pixelOff = image.getPixel(x + (if (x == left) -offsetX else offsetX), y) val pixelOff = image.getPixel(x + (if (x < image.width/2) -offsetX else offsetX), y)
if (isWhite(pixel)) { if (isWhite(pixel)) {
blackPixelsStreak = 0
whitePixelsStreak++ whitePixelsStreak++
whitePixels++ whitePixels++
if (notOffset) if (notOffset)
overallWhitePixels++ overallWhitePixels++
if (whitePixelsStreak > 14) { if (whitePixelsStreak > 14)
whiteStrak = true whiteStrak = true
} if (whitePixelsStreak > 6 && whitePixelsStreak >= index - 1)
topWhiteStreak = whitePixelsStreak
} else { } else {
whitePixelsStreak = 0 whitePixelsStreak = 0
if (isDark(pixel) && isDark(pixelOff)) { if (isDark(pixel) && isDark(pixelOff)) {
@ -125,14 +129,20 @@ object ImageUtil {
if (notOffset) if (notOffset)
overallBlackPixels++ overallBlackPixels++
blackPixelsStreak++ blackPixelsStreak++
if (blackPixelsStreak >= 14) { if (blackPixelsStreak >= 14)
blackStreak = true blackStreak = true
continue
} }
} else { }
if (blackPixelsStreak > 6 && blackPixelsStreak >= index - 1)
topBlackStreak = blackPixelsStreak
blackPixelsStreak = 0 blackPixelsStreak = 0
} }
} if (blackPixelsStreak > 6)
} botBlackStreak = blackPixelsStreak
else if (whitePixelsStreak > 6)
botWhiteStreak = whitePixelsStreak
when { when {
blackPixels > 22 -> { blackPixels > 22 -> {
if (x == right || x == right + offsetX) if (x == right || x == right + offsetX)
@ -162,10 +172,12 @@ object ImageUtil {
} }
} }
val topIsBlackStreak = topBlackStreak > topWhiteStreak
if (overallWhitePixels > 9 && overallWhitePixels > overallBlackPixels) { val bottomIsBlackStreak = botBlackStreak > botWhiteStreak
if (overallWhitePixels > 9 && overallWhitePixels > overallBlackPixels)
darkBG = false darkBG = false
} if (topIsBlackStreak && bottomIsBlackStreak)
darkBG = true
if (darkBG) { if (darkBG) {
if (isWhite(image.getPixel(left, bot)) && isWhite(image.getPixel(right, bot))) if (isWhite(image.getPixel(left, bot)) && isWhite(image.getPixel(right, bot)))
return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
@ -176,12 +188,14 @@ object ImageUtil {
else else
return ColorDrawable(blackPixel) return ColorDrawable(blackPixel)
} }
if (topLeftIsDark && topRightIsDark && (topMidIsDark || overallBlackPixels > 9)) if (topIsBlackStreak || (topLeftIsDark && topRightIsDark
&& isDark(image.getPixel(left - offsetX, top)) && isDark(image.getPixel(right + offsetX, top))
&& (topMidIsDark || overallBlackPixels > 9)))
return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(blackPixel, blackPixel, Color.WHITE, Color.WHITE)) intArrayOf(blackPixel, blackPixel, Color.WHITE, Color.WHITE))
else if (botLeftIsDark && botRightIsDark else if (bottomIsBlackStreak || (botLeftIsDark && botRightIsDark
&& isDark(image.getPixel(left - offsetX, bot)) && isDark(image.getPixel(right + offsetX, bot)) && isDark(image.getPixel(left - offsetX, bot)) && isDark(image.getPixel(right + offsetX, bot))
&& (isDark(image.getPixel(midX, bot)) || overallBlackPixels > 9)) && (isDark(image.getPixel(midX, bot)) || overallBlackPixels > 9)))
return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(Color.WHITE, Color.WHITE, blackPixel, blackPixel)) intArrayOf(Color.WHITE, Color.WHITE, blackPixel, blackPixel))
return ColorDrawable(Color.WHITE) return ColorDrawable(Color.WHITE)