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 {
bindTo(preferences.longStripSplitWebtoon())
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? {
try {
return when (getImageType(stream)?.format) {
return try {
when (getImageType(stream)?.format) {
Format.Avif -> ImageType.AVIF
Format.Gif -> ImageType.GIF
Format.Heif -> ImageType.HEIF
@ -62,8 +62,8 @@ object ImageUtil {
else -> null
}
} catch (e: Exception) {
null
}
return null
}
fun getExtensionFromMimeType(mime: String?): String {
@ -185,7 +185,8 @@ object ImageUtil {
}
enum class Side {
RIGHT, LEFT
RIGHT,
LEFT,
}
/**
@ -206,24 +207,20 @@ object ImageUtil {
return true
}
val bitmapRegionDecoder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
BitmapRegionDecoder.newInstance(imageFile.openInputStream())
} else {
@Suppress("DEPRECATION")
BitmapRegionDecoder.newInstance(imageFile.openInputStream(), false)
}
val bitmapRegionDecoder = getBitmapRegionDecoder(imageFile.openInputStream())
if (bitmapRegionDecoder == null) {
logcat { "Failed to create new instance of BitmapRegionDecoder" }
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
val imageWidth = options.outWidth
val splitDataList = getSplitDataForOptions(options)
val splitDataList = options.splitData
return try {
splitDataList.forEach { splitData ->
@ -264,8 +261,8 @@ object ImageUtil {
*/
fun isStripSplitNeeded(imageStream: BufferedInputStream): Boolean {
if (isAnimatedAndSupported(imageStream)) return false
val options = extractImageOptions(imageStream)
val options = extractImageOptions(imageStream)
val imageHeightIsBiggerThanWidth = options.outHeight > options.outWidth
val imageHeightBiggerThanScreenHeight = options.outHeight > getDisplayMaxHeightInPx
return imageHeightIsBiggerThanWidth && imageHeightBiggerThanScreenHeight
@ -275,16 +272,8 @@ object ImageUtil {
* Split the imageStream according to the provided splitData
*/
fun splitStrip(imageStream: InputStream, splitData: SplitData): InputStream {
val bitmapRegionDecoder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
BitmapRegionDecoder.newInstance(imageStream)
} else {
@Suppress("DEPRECATION")
BitmapRegionDecoder.newInstance(imageStream, false)
}
if (bitmapRegionDecoder == null) {
throw Exception("Failed to create new instance of BitmapRegionDecoder")
}
val bitmapRegionDecoder = getBitmapRegionDecoder(imageStream)
?: throw Exception("Failed to create new instance of BitmapRegionDecoder")
logcat {
"WebtoonSplit #${splitData.index} with topOffset=${splitData.topOffset} " +
@ -308,42 +297,41 @@ object ImageUtil {
}
fun getSplitDataForStream(imageStream: InputStream): List<SplitData> {
val options = extractImageOptions(imageStream)
return getSplitDataForOptions(options)
return extractImageOptions(imageStream).splitData
}
private fun getSplitDataForOptions(options: BitmapFactory.Options): List<SplitData> {
val imageHeight = options.outHeight
private val BitmapFactory.Options.splitData
get(): List<SplitData> {
val imageHeight = outHeight
val splitHeight = (getDisplayMaxHeightInPx * 1.5).toInt()
// -1 so it doesn't try to split when imageHeight = splitHeight
val partCount = (imageHeight - 1) / splitHeight + 1
val splitHeight = (getDisplayMaxHeightInPx * 1.5).toInt()
// -1 so it doesn't try to split when imageHeight = splitHeight
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 {
"Generating SplitData for image with height of $imageHeight. " +
"Estimated $partCount part and ${optimalSplitHeight}px height per part"
}
return mutableListOf<SplitData>().apply {
for (index in (0 until partCount)) {
// Only continue if the list is empty or there is image remaining
if (isNotEmpty() && imageHeight <= last().bottomOffset) break
return mutableListOf<SplitData>().apply {
for (index in (0 until partCount)) {
// Only continue if the list is empty or there is image remaining
if (isNotEmpty() && imageHeight <= last().bottomOffset) break
val topOffset = index * optimalSplitHeight
var outputImageHeight = min(optimalSplitHeight, imageHeight - topOffset)
val topOffset = index * optimalSplitHeight
var outputImageHeight = min(optimalSplitHeight, imageHeight - topOffset)
val remainingHeight = imageHeight - (topOffset + outputImageHeight)
// If remaining height is smaller or equal to 1/10th of
// optimal split height then include it in current page
if (remainingHeight <= (optimalSplitHeight / 10)) {
outputImageHeight += remainingHeight
val remainingHeight = imageHeight - (topOffset + outputImageHeight)
// If remaining height is smaller or equal to 1/10th of
// optimal split height then include it in current page
if (remainingHeight <= (optimalSplitHeight / 10)) {
outputImageHeight += remainingHeight
}
add(SplitData(index, topOffset, outputImageHeight))
}
add(SplitData(index, topOffset, outputImageHeight))
}
}
}
data class SplitData(
val index: Int,
@ -584,6 +572,15 @@ object ImageUtil {
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
private val SUPPLEMENTARY_MIMETYPE_MAPPING = mapOf(
// 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_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_long_strip_split">Split tall images (Alpha)</string>
<string name="pref_long_strip_split_summary">Improves reader performance</string>
<string name="pref_long_strip_split">Split tall images (BETA)</string>
<string name="pref_cutout_short">Show content in cutout area</string>
<string name="pref_page_transitions">Animate page transitions</string>
<string name="pref_double_tap_anim_speed">Double tap animation speed</string>