Minor cleanup

This commit is contained in:
arkon 2022-08-27 11:50:51 -04:00
parent 88b56121a3
commit 31b62b2779
3 changed files with 49 additions and 53 deletions

View File

@ -292,7 +292,7 @@ class SettingsReaderController : SettingsController() {
switchPreference { switchPreference {
bindTo(preferences.longStripSplitWebtoon()) bindTo(preferences.longStripSplitWebtoon())
titleRes = R.string.pref_long_strip_split titleRes = R.string.pref_long_strip_split
summaryRes = R.string.pref_long_strip_split_summary summaryRes = R.string.split_tall_images_summary
} }
} }

View File

@ -50,8 +50,8 @@ object ImageUtil {
} }
fun findImageType(stream: InputStream): ImageType? { fun findImageType(stream: InputStream): ImageType? {
try { return try {
return when (getImageType(stream)?.format) { when (getImageType(stream)?.format) {
Format.Avif -> ImageType.AVIF Format.Avif -> ImageType.AVIF
Format.Gif -> ImageType.GIF Format.Gif -> ImageType.GIF
Format.Heif -> ImageType.HEIF Format.Heif -> ImageType.HEIF
@ -62,8 +62,8 @@ object ImageUtil {
else -> null else -> null
} }
} catch (e: Exception) { } catch (e: Exception) {
null
} }
return null
} }
fun getExtensionFromMimeType(mime: String?): String { fun getExtensionFromMimeType(mime: String?): String {
@ -185,7 +185,8 @@ object ImageUtil {
} }
enum class Side { enum class Side {
RIGHT, LEFT RIGHT,
LEFT,
} }
/** /**
@ -206,24 +207,20 @@ object ImageUtil {
return true return true
} }
val bitmapRegionDecoder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val bitmapRegionDecoder = getBitmapRegionDecoder(imageFile.openInputStream())
BitmapRegionDecoder.newInstance(imageFile.openInputStream())
} else {
@Suppress("DEPRECATION")
BitmapRegionDecoder.newInstance(imageFile.openInputStream(), false)
}
if (bitmapRegionDecoder == null) { if (bitmapRegionDecoder == null) {
logcat { "Failed to create new instance of BitmapRegionDecoder" } logcat { "Failed to create new instance of BitmapRegionDecoder" }
return false return false
} }
val options = extractImageOptions(imageFile.openInputStream(), resetAfterExtraction = false).apply { inJustDecodeBounds = false } val options = extractImageOptions(imageFile.openInputStream(), resetAfterExtraction = false).apply {
inJustDecodeBounds = false
}
// Values are stored as they get modified during split loop // Values are stored as they get modified during split loop
val imageWidth = options.outWidth val imageWidth = options.outWidth
val splitDataList = getSplitDataForOptions(options) val splitDataList = options.splitData
return try { return try {
splitDataList.forEach { splitData -> splitDataList.forEach { splitData ->
@ -264,8 +261,8 @@ object ImageUtil {
*/ */
fun isStripSplitNeeded(imageStream: BufferedInputStream): Boolean { fun isStripSplitNeeded(imageStream: BufferedInputStream): Boolean {
if (isAnimatedAndSupported(imageStream)) return false if (isAnimatedAndSupported(imageStream)) return false
val options = extractImageOptions(imageStream)
val options = extractImageOptions(imageStream)
val imageHeightIsBiggerThanWidth = options.outHeight > options.outWidth val imageHeightIsBiggerThanWidth = options.outHeight > options.outWidth
val imageHeightBiggerThanScreenHeight = options.outHeight > getDisplayMaxHeightInPx val imageHeightBiggerThanScreenHeight = options.outHeight > getDisplayMaxHeightInPx
return imageHeightIsBiggerThanWidth && imageHeightBiggerThanScreenHeight return imageHeightIsBiggerThanWidth && imageHeightBiggerThanScreenHeight
@ -275,16 +272,8 @@ object ImageUtil {
* Split the imageStream according to the provided splitData * Split the imageStream according to the provided splitData
*/ */
fun splitStrip(imageStream: InputStream, splitData: SplitData): InputStream { fun splitStrip(imageStream: InputStream, splitData: SplitData): InputStream {
val bitmapRegionDecoder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val bitmapRegionDecoder = getBitmapRegionDecoder(imageStream)
BitmapRegionDecoder.newInstance(imageStream) ?: throw Exception("Failed to create new instance of BitmapRegionDecoder")
} else {
@Suppress("DEPRECATION")
BitmapRegionDecoder.newInstance(imageStream, false)
}
if (bitmapRegionDecoder == null) {
throw Exception("Failed to create new instance of BitmapRegionDecoder")
}
logcat { logcat {
"WebtoonSplit #${splitData.index} with topOffset=${splitData.topOffset} " + "WebtoonSplit #${splitData.index} with topOffset=${splitData.topOffset} " +
@ -308,42 +297,41 @@ object ImageUtil {
} }
fun getSplitDataForStream(imageStream: InputStream): List<SplitData> { fun getSplitDataForStream(imageStream: InputStream): List<SplitData> {
val options = extractImageOptions(imageStream) return extractImageOptions(imageStream).splitData
return getSplitDataForOptions(options)
} }
private fun getSplitDataForOptions(options: BitmapFactory.Options): List<SplitData> { private val BitmapFactory.Options.splitData
val imageHeight = options.outHeight get(): List<SplitData> {
val imageHeight = outHeight
val splitHeight = (getDisplayMaxHeightInPx * 1.5).toInt() val splitHeight = (getDisplayMaxHeightInPx * 1.5).toInt()
// -1 so it doesn't try to split when imageHeight = splitHeight // -1 so it doesn't try to split when imageHeight = splitHeight
val partCount = (imageHeight - 1) / splitHeight + 1 val partCount = (imageHeight - 1) / splitHeight + 1
val optimalSplitHeight = imageHeight / partCount
val optimalSplitHeight = imageHeight / partCount logcat {
"Generating SplitData for image (height: $imageHeight): " +
"$partCount parts @ ${optimalSplitHeight}px height per part"
}
logcat { return mutableListOf<SplitData>().apply {
"Generating SplitData for image with height of $imageHeight. " + for (index in (0 until partCount)) {
"Estimated $partCount part and ${optimalSplitHeight}px height per part" // Only continue if the list is empty or there is image remaining
} if (isNotEmpty() && imageHeight <= last().bottomOffset) break
return mutableListOf<SplitData>().apply { val topOffset = index * optimalSplitHeight
for (index in (0 until partCount)) { var outputImageHeight = min(optimalSplitHeight, imageHeight - topOffset)
// Only continue if the list is empty or there is image remaining
if (isNotEmpty() && imageHeight <= last().bottomOffset) break
val topOffset = index * optimalSplitHeight val remainingHeight = imageHeight - (topOffset + outputImageHeight)
var outputImageHeight = min(optimalSplitHeight, imageHeight - topOffset) // If remaining height is smaller or equal to 1/10th of
// optimal split height then include it in current page
val remainingHeight = imageHeight - (topOffset + outputImageHeight) if (remainingHeight <= (optimalSplitHeight / 10)) {
// If remaining height is smaller or equal to 1/10th of outputImageHeight += remainingHeight
// optimal split height then include it in current page }
if (remainingHeight <= (optimalSplitHeight / 10)) { add(SplitData(index, topOffset, outputImageHeight))
outputImageHeight += remainingHeight
} }
add(SplitData(index, topOffset, outputImageHeight))
} }
} }
}
data class SplitData( data class SplitData(
val index: Int, val index: Int,
@ -584,6 +572,15 @@ object ImageUtil {
return options return options
} }
private fun getBitmapRegionDecoder(imageStream: InputStream): BitmapRegionDecoder? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
BitmapRegionDecoder.newInstance(imageStream)
} else {
@Suppress("DEPRECATION")
BitmapRegionDecoder.newInstance(imageStream, false)
}
}
// Android doesn't include some mappings // Android doesn't include some mappings
private val SUPPLEMENTARY_MIMETYPE_MAPPING = mapOf( private val SUPPLEMENTARY_MIMETYPE_MAPPING = mapOf(
// https://issuetracker.google.com/issues/182703810 // https://issuetracker.google.com/issues/182703810

View File

@ -297,8 +297,7 @@
<string name="pref_dual_page_split">Dual page split</string> <string name="pref_dual_page_split">Dual page split</string>
<string name="pref_dual_page_invert">Invert dual page split placement</string> <string name="pref_dual_page_invert">Invert dual page split placement</string>
<string name="pref_dual_page_invert_summary">If the placement of the dual page split doesn\'t match reading direction</string> <string name="pref_dual_page_invert_summary">If the placement of the dual page split doesn\'t match reading direction</string>
<string name="pref_long_strip_split">Split tall images (Alpha)</string> <string name="pref_long_strip_split">Split tall images (BETA)</string>
<string name="pref_long_strip_split_summary">Improves reader performance</string>
<string name="pref_cutout_short">Show content in cutout area</string> <string name="pref_cutout_short">Show content in cutout area</string>
<string name="pref_page_transitions">Animate page transitions</string> <string name="pref_page_transitions">Animate page transitions</string>
<string name="pref_double_tap_anim_speed">Double tap animation speed</string> <string name="pref_double_tap_anim_speed">Double tap animation speed</string>