From 3d2a2c058fa6583059ca45a8aed51fd0c9878056 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 24 Mar 2019 20:34:57 -0700 Subject: [PATCH] Updated auto check --- .../ui/reader/viewer/pager/PagerConfig.kt | 6 ++ .../ui/reader/viewer/pager/PagerPageHolder.kt | 19 +++--- .../eu/kanade/tachiyomi/util/ImageUtil.kt | 66 +++++++++++++------ 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt index c07c7b65fb..b2ba6e9cd9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt @@ -43,6 +43,9 @@ class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelpe var doubleTapAnimDuration = 500 private set + var readerTheme = 0 + private set + init { preferences.readWithTapping() .register({ tappingEnabled = it }) @@ -70,6 +73,9 @@ class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelpe preferences.readWithVolumeKeysInverted() .register({ volumeKeysInverted = it }) + + preferences.readerTheme() + .register({ readerTheme = it }) } fun unsubscribe() { 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 eb68cd48cf..b72f9ea66f 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 @@ -28,7 +28,6 @@ import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView import com.github.chrisbanes.photoview.PhotoView import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.glide.GlideApp -import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressBar @@ -100,8 +99,6 @@ class PagerPageHolder( */ private var readImageHeaderSubscription: Subscription? = null - private val preferences by injectLazy() - init { addView(progressBar) observeStatus() @@ -246,18 +243,22 @@ class PagerPageHolder( .observeOn(AndroidSchedulers.mainThread()) .doOnNext { isAnimated -> if (!isAnimated) { - if (preferences.readerTheme().get() == 2) { + if (viewer.config.readerTheme == 2) { val bytesArray = openStream!!.readBytes() val imageView = initSubsamplingImageView() - val bytesStream = bytesArray.inputStream() - imageView.setImage(ImageSource.inputStream(bytesStream)) + if (viewer.config.imageCropBorders) { + val bytesStream = bytesArray.inputStream() + imageView.setImage(ImageSource.inputStream(bytesStream)) + bytesStream.close() + } launchUI { - val backgroundD = async { ImageUtil.autoSetBackground(BitmapFactory.decodeByteArray(bytesArray, 0, bytesArray.size)) } - imageView.background = backgroundD.await() + val image = async { BitmapFactory.decodeByteArray(bytesArray, 0, bytesArray.size) } + imageView.background = ImageUtil.autoSetBackground(image.await()) + if (!viewer.config.imageCropBorders) + imageView.setImage(ImageSource.bitmap(image.await())) } - bytesStream.close() } else { initSubsamplingImageView().setImage(ImageSource.inputStream(openStream!!)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt index 8a5814458f..2c24206eb0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt @@ -5,6 +5,7 @@ import android.graphics.Color import android.graphics.drawable.* import java.io.InputStream import java.net.URLConnection +import kotlin.math.abs object ImageUtil { @@ -55,25 +56,43 @@ object ImageUtil { fun autoSetBackground(image: Bitmap): Drawable { if (image.width < 50 || image.height < 50) return ColorDrawable(Color.WHITE) - val topLeftIsDark = isDark(image.getPixel(2,2)) - val topRightIsDark = isDark(image.getPixel(image.width - 2,2)) - val midLeftIsDark = isDark(image.getPixel(2,image.height/2)) - val midRightIsDark = isDark(image.getPixel(image.width - 2,image.height/2)) - val topMidIsDark = isDark(image.getPixel(image.width/2, 2)) - val botLeftIsDark = isDark(image.getPixel(2,image.height - 2)) - val botRightIsDark = isDark(image.getPixel(image.width - 2,image.height - 2)) + val top = 5 + val bot = image.height - 5 + val left = (image.width * 0.025).toInt() + val right = image.width - left + val midX = image.width / 2 + val midY = image.height / 2 + val topLeftIsDark = isDark(image.getPixel(left,top)) + val topRightIsDark = isDark(image.getPixel(right,top)) + val midLeftIsDark = isDark(image.getPixel(left,midY)) + val midRightIsDark = isDark(image.getPixel(right,midY)) + val topMidIsDark = isDark(image.getPixel(midX, top)) + val botLeftIsDark = isDark(image.getPixel(left,bot)) + val botRightIsDark = isDark(image.getPixel(right,bot)) var darkBG = (topLeftIsDark && (botLeftIsDark || botRightIsDark || topRightIsDark || midLeftIsDark || topMidIsDark)) || (topRightIsDark && (botRightIsDark || botLeftIsDark || midRightIsDark || topMidIsDark)) - if (darkBG) { - if (isWhite(image.getPixel(2,2)).toInt() + - isWhite(image.getPixel(image.width - 2,2)).toInt() + - isWhite(image.getPixel(2,image.height - 2)).toInt() + - isWhite(image.getPixel(image.width - 2,image.height - 2)).toInt() > 2) + if (pixelIsClose(image.getPixel(left,top), image.getPixel(midX,top)) && // top left & top mid + pixelIsClose(image.getPixel(midX,top), image.getPixel(right,top)) && //top mid & top right + pixelIsClose(image.getPixel(right,top), image.getPixel(right,bot)) && // top right & bot right + pixelIsClose(image.getPixel(right,bot), image.getPixel(midX,bot)) && // bot right & bot mid + pixelIsClose(image.getPixel(midX,bot), image.getPixel(left,bot))) // bot mid & bot left + return ColorDrawable(image.getPixel(left,top)) + val blackPixel = when { + topLeftIsDark -> image.getPixel (left, top) + topRightIsDark -> image.getPixel(right, top) + botLeftIsDark ->image.getPixel(left, bot) + else -> image.getPixel(right, bot) + } + + if (isWhite(image.getPixel(left,top)).toInt() + + isWhite(image.getPixel(right,top)).toInt() + + isWhite(image.getPixel(left,bot)).toInt() + + isWhite(image.getPixel(right,bot)).toInt() > 2) darkBG = false var overallWhitePixels = 0 var overallBlackPixels = 0 - outer@ for (x in intArrayOf(2,image.width-2)) { + outer@ for (x in intArrayOf(left,right)) { var whitePixelsStreak = 0 var whitePixels = 0 var blackPixelsStreak = 0 @@ -107,30 +126,37 @@ object ImageUtil { } } when { - blackPixels > 22 -> return ColorDrawable(Color.BLACK) + blackPixels > 22 -> { darkBG = true; overallWhitePixels = 0; break@outer } blackStreak -> darkBG = true whiteStrak || whitePixels > 22 -> darkBG = false } } - if (overallWhitePixels > 9 && overallWhitePixels >= overallBlackPixels) + if (overallWhitePixels > 9 && overallWhitePixels > overallBlackPixels) darkBG = false - } if (darkBG) { - if (isWhite(image.getPixel(2,image.height - 2)) && isWhite(image.getPixel(image.width - 2,image.height - 2))) + if (isWhite(image.getPixel(left,bot)) && isWhite(image.getPixel(right,bot))) return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, - intArrayOf(Color.BLACK, Color.BLACK, Color.WHITE, Color.WHITE)) + intArrayOf(blackPixel, blackPixel, Color.WHITE, Color.WHITE)) + else if (isWhite(image.getPixel(left,top)) && isWhite(image.getPixel(right,top))) + return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, + intArrayOf(Color.WHITE, Color.WHITE, blackPixel, blackPixel)) else - return ColorDrawable(Color.BLACK) + return ColorDrawable(blackPixel) } return ColorDrawable(Color.WHITE) } fun Boolean.toInt() = if (this) 1 else 0 private fun isDark(color: Int): Boolean { - return Color.red(color) < 33 && Color.blue(color) < 33 && Color.green(color) < 33 + return Color.red(color) < 40 && Color.blue(color) < 40 && Color.green(color) < 40 } + private fun pixelIsClose(color1: Int, color2: Int): Boolean { + return abs(Color.red(color1) - Color.red(color2)) < 30 && + abs(Color.green(color1) - Color.green(color2)) < 30 && + abs(Color.blue(color1) - Color.blue(color2)) < 30 + } private fun isWhite(color: Int): Boolean { return Color.red(color) + Color.blue(color) + Color.green(color) > 740