Minor cleanup image decode codes

This commit is contained in:
tarsin committed 2022-12-28 09:57:50 +08:00
1 parent b317bc334e
commit a15dc4afdf
3 files changed
+35 -53

No files matched your search

@@ -29,8 +29,6 @@ class UriArchiveAccessor(ctx: Context, uri: Uri) {
private external fun openArchive(fd: Int, size: Long): Int
external fun extractToByteBuffer(index: Int): ByteBuffer?
external fun releaseByteBuffer(buffer: ByteBuffer)
external fun extractToFd(index: Int, fd: Int)
external fun getFilename(index: Int): String
external fun needPassword(): Boolean
@@ -40,4 +38,9 @@ class UriArchiveAccessor(ctx: Context, uri: Uri) {
closeArchive()
pfd.close()
}
companion object {
@JvmStatic
external fun releaseByteBuffer(buffer: ByteBuffer)
}
}
@@ -335,12 +335,9 @@ public class ArchivePageLoader extends PageLoader2 {
Image image = null;
if (buffer != null) {
try {
image = Image.decode(buffer, () -> {
archiveAccessor.releaseByteBuffer(buffer);
return null;
});
image = Image.decode(buffer.asReadOnlyBuffer());
} catch (ImageDecoder.DecodeException e) {
archiveAccessor.releaseByteBuffer(buffer);
UriArchiveAccessor.releaseByteBuffer(buffer);
e.printStackTrace();
}
}
+28 -46
View File
@@ -19,76 +19,60 @@ package com.hippo.image
import android.graphics.ColorSpace
import android.graphics.ImageDecoder
import android.graphics.ImageDecoder.ALLOCATOR_HARDWARE
import android.graphics.ImageDecoder.DecodeException
import android.graphics.ImageDecoder.ImageInfo
import android.graphics.ImageDecoder.Source
import android.graphics.drawable.AnimatedImageDrawable
import android.graphics.drawable.Animatable
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import com.hippo.UriArchiveAccessor
import com.hippo.ehviewer.EhApplication
import java.io.FileInputStream
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import kotlin.math.max
import kotlin.math.min
class Image private constructor(source: Source?, val release: () -> Unit = {}) {
class Image private constructor(source: Source, private val byteBuffer: ByteBuffer? = null) {
var mObtainedDrawable: Drawable?
init {
mObtainedDrawable = null
source?.let {
mObtainedDrawable =
ImageDecoder.decodeDrawable(source) { decoder: ImageDecoder, info: ImageInfo, src: Source ->
decoder.allocator = ALLOCATOR_HARDWARE
decoder.setTargetColorSpace(colorSpace)
decoder.setTargetSampleSize(
max(
min(
info.size.width / (2 * screenWidth),
info.size.height / (2 * screenHeight)
), 1
)
)
} // Should we lazy decode it?
}
mObtainedDrawable =
ImageDecoder.decodeDrawable(source) { decoder: ImageDecoder, info: ImageInfo, _: Source ->
decoder.setTargetColorSpace(colorSpace)
decoder.setTargetSampleSize(
min(
info.size.width / (2 * screenWidth),
info.size.height / (2 * screenHeight)
).coerceAtLeast(1)
)
}
if (mObtainedDrawable is BitmapDrawable)
release()
byteBuffer?.let { UriArchiveAccessor.releaseByteBuffer(it) }
}
val width = mObtainedDrawable!!.intrinsicWidth
val height = mObtainedDrawable!!.intrinsicHeight
var started = false
val width: Int
get() = mObtainedDrawable?.intrinsicWidth ?: 0
val height: Int
get() = mObtainedDrawable?.intrinsicHeight ?: 0
@Synchronized
fun recycle() {
if (mObtainedDrawable is AnimatedImageDrawable) {
(mObtainedDrawable as AnimatedImageDrawable?)?.stop()
}
if (mObtainedDrawable is BitmapDrawable) {
(mObtainedDrawable as BitmapDrawable?)?.bitmap?.recycle()
(mObtainedDrawable as? Animatable)?.run {
stop()
byteBuffer?.let { UriArchiveAccessor.releaseByteBuffer(it) }
}
(mObtainedDrawable as? BitmapDrawable)?.run { bitmap.recycle() }
mObtainedDrawable?.callback = null
if (mObtainedDrawable is AnimatedImageDrawable)
release()
mObtainedDrawable = null
}
fun start() {
if (!started) {
(mObtainedDrawable as AnimatedImageDrawable?)?.start()
}
}
val isRecycled: Boolean
get() = (mObtainedDrawable as? BitmapDrawable)?.bitmap?.isRecycled ?: (mObtainedDrawable as? AnimatedImageDrawable)?.isRunning?.not() ?: true
companion object {
val screenWidth = EhApplication.application.resources.displayMetrics.widthPixels
val screenHeight = EhApplication.application.resources.displayMetrics.heightPixels
val isWideColorGamut = EhApplication.application.resources.configuration.isScreenWideColorGamut
val colorSpace = ColorSpace.get(if (isWideColorGamut) ColorSpace.Named.DISPLAY_P3 else ColorSpace.Named.SRGB)
private val isWideColorGamut =
EhApplication.application.resources.configuration.isScreenWideColorGamut
val colorSpace =
ColorSpace.get(if (isWideColorGamut) ColorSpace.Named.DISPLAY_P3 else ColorSpace.Named.SRGB)
@Throws(DecodeException::class)
@JvmStatic
@@ -104,11 +88,9 @@ class Image private constructor(source: Source?, val release: () -> Unit = {}) {
@Throws(DecodeException::class)
@JvmStatic
fun decode(buffer: ByteBuffer, release: () -> Unit? = {}): Image {
fun decode(buffer: ByteBuffer): Image {
val src = ImageDecoder.createSource(buffer)
return Image(src) {
release()
}
return Image(src, buffer)
}
}
}