fix: Handle InputStream

This commit is contained in:
Ahmad Ansori Palembani 2024-05-23 16:09:25 +07:00
parent 2228a6eea3
commit 020dfba5c3
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
3 changed files with 56 additions and 2 deletions

View file

@ -30,6 +30,7 @@ class CoilSetup(context: Context) {
add(TachiyomiImageDecoder.Factory())
add(MangaCoverFetcher.Factory(lazy(callFactoryInit), lazy(diskCacheInit)))
add(MangaCoverKeyer())
add(InputStreamFetcher.Factory())
}
callFactory(callFactoryInit)
diskCache(diskCacheInit)

View file

@ -0,0 +1,33 @@
package eu.kanade.tachiyomi.data.image.coil
import coil.ImageLoader
import coil.decode.DataSource
import coil.decode.ImageSource
import coil.fetch.FetchResult
import coil.fetch.Fetcher
import coil.fetch.SourceResult
import coil.request.Options
import okio.Buffer
import java.io.InputStream
class InputStreamFetcher(
private val stream: InputStream,
private val options: Options,
) : Fetcher {
override suspend fun fetch(): FetchResult {
return SourceResult(
source = ImageSource(
source = stream.use { Buffer().readFrom(it) },
context = options.context,
),
mimeType = null,
dataSource = DataSource.MEMORY,
)
}
class Factory : Fetcher.Factory<InputStream> {
override fun create(data: InputStream, options: Options, imageLoader: ImageLoader): Fetcher {
return InputStreamFetcher(data, options)
}
}
}

View file

@ -55,6 +55,26 @@ class TachiyomiImageDecoder(private val resources: ImageSource, private val opti
}
}
if (maxOf(bitmap.width, bitmap.height) > GLUtil.maxTextureSize) {
val widthRatio = bitmap.width / GLUtil.maxTextureSize.toFloat()
val heightRatio = bitmap.height / GLUtil.maxTextureSize.toFloat()
val targetWidth: Float
val targetHeight: Float
if (widthRatio >= heightRatio) {
targetWidth = GLUtil.maxTextureSize.toFloat()
targetHeight = (targetWidth / bitmap.width) * bitmap.height
} else {
targetHeight = GLUtil.maxTextureSize.toFloat()
targetWidth = (targetHeight / bitmap.height) * bitmap.width
}
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth.toInt(), targetHeight.toInt(), false)
bitmap.recycle()
bitmap = scaledBitmap
}
return DecodeResult(
drawable = bitmap.toDrawable(options.context.resources),
isSampled = sampleSize > 1,
@ -64,8 +84,8 @@ class TachiyomiImageDecoder(private val resources: ImageSource, private val opti
class Factory : Decoder.Factory {
override fun create(result: SourceResult, options: Options, imageLoader: ImageLoader): Decoder? {
if (!isApplicable(result.source.source()) || !options.customDecoder) return null
return TachiyomiImageDecoder(result.source, options)
if (isApplicable(result.source.source()) || options.customDecoder) return TachiyomiImageDecoder(result.source, options)
return null
}
private fun isApplicable(source: BufferedSource): Boolean {