Review Changes

This commit is contained in:
AntsyLich 2022-08-27 20:38:34 +06:00
parent a666258a28
commit caef7a4548
3 changed files with 56 additions and 61 deletions

View File

@ -302,7 +302,7 @@ class WebtoonPageHolder(
private fun onStripSplit(imageStream: BufferedInputStream): SplitData? {
val page = page ?: return null
val splitData = ImageUtil.stripSplitData(imageStream).toMutableList()
val splitData = ImageUtil.getSplitDataForStream(imageStream).toMutableList()
val toReturn = splitData.removeFirstOrNull()
val newPages = splitData.map { StencilPage(page, it) }
viewer.onLongStripSplit(page, newPages)

View File

@ -220,21 +220,10 @@ object ImageUtil {
val options = extractImageOptions(imageFile.openInputStream(), resetAfterExtraction = false).apply { inJustDecodeBounds = false }
val imageHeight = options.outHeight
// Values are stored as they get modified during split loop
val imageWidth = options.outWidth
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 splitDataList = getSplitDataForStripSplit(partCount, imageHeight, optimalSplitHeight)
logcat {
"Splitting image with height of $imageHeight into $partCount part " +
"with estimated ${optimalSplitHeight}px height per split"
}
val splitDataList = getSplitDataForOptions(options)
return try {
splitDataList.forEach { splitData ->
@ -266,42 +255,15 @@ object ImageUtil {
}
}
private fun getSplitDataForStripSplit(partCount: Int, imageHeight: Int, optimalSplitHeight: Int): List<SplitData> {
return (0 until partCount).fold(mutableListOf()) { list, index ->
list.apply {
// Only continue if the list is empty or there is image remaining
if (isEmpty() || imageHeight > last().bottomOffset) {
val topOffset = index * optimalSplitHeight
var outputImageHeight = min(optimalSplitHeight, imageHeight - topOffset)
val remainingHeight = imageHeight - (topOffset + outputImageHeight)
// If remaining height is smaller or equal to 1/3th of
// optimal split height then include it in current page
if (remainingHeight <= (optimalSplitHeight / 3)) {
outputImageHeight += remainingHeight
}
add(SplitData(index, topOffset, outputImageHeight))
}
}
}
}
private fun splitImagePath(imageFilePath: String, index: Int) =
imageFilePath.substringBeforeLast(".") + "__${"%03d".format(index + 1)}.jpg"
data class SplitData(
val index: Int,
val topOffset: Int,
val outputImageHeight: Int,
) {
val bottomOffset = topOffset + outputImageHeight
}
/**
* Check whether the image is a long Strip that needs splitting
* @return true if the image height is greater than image width and screen height
* @return true if the image is not animated and it's height is greater than image width and screen height
*/
fun isStripSplitNeeded(imageStream: BufferedInputStream): Boolean {
if (isAnimatedAndSupported(imageStream)) return false
val options = extractImageOptions(imageStream)
val imageHeightIsBiggerThanWidth = options.outHeight > options.outWidth
@ -309,20 +271,9 @@ object ImageUtil {
return imageHeightIsBiggerThanWidth && imageHeightBiggerThanScreenHeight
}
fun stripSplitData(imageStream: InputStream): List<SplitData> {
val options = extractImageOptions(imageStream)
val imageHeight = options.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 optimalSplitHeight = imageHeight / partCount
return getSplitDataForStripSplit(partCount, imageHeight, optimalSplitHeight)
}
/**
* 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)
@ -356,6 +307,52 @@ object ImageUtil {
}
}
fun getSplitDataForStream(imageStream: InputStream): List<SplitData> {
val options = extractImageOptions(imageStream)
return getSplitDataForOptions(options)
}
private fun getSplitDataForOptions(options: BitmapFactory.Options): List<SplitData> {
val imageHeight = options.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 optimalSplitHeight = imageHeight / partCount
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
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
}
add(SplitData(index, topOffset, outputImageHeight))
}
}
}
data class SplitData(
val index: Int,
val topOffset: Int,
val outputImageHeight: Int,
) {
val bottomOffset = topOffset + outputImageHeight
}
/**
* Algorithm for determining what background to accompany a comic/manga page
*/
@ -583,9 +580,7 @@ object ImageUtil {
val imageBytes = imageStream.readBytes()
val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size, options)
if (resetAfterExtraction) imageStream.reset()
return options
}

View File

@ -297,8 +297,8 @@
<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">Long Strip Split (Alpha)</string>
<string name="pref_long_strip_split_summary">Impore reader performance by splitting long strips</string>
<string name="pref_long_strip_split">Split tall images (Alpha)</string>
<string name="pref_long_strip_split_summary">@string/split_tall_images_summary</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>