Rewrite the GIF frame delay if it's below a threshold (coil-kt/coil#783) (#672)

* Rewrite the GIF frame delay if it's below a threshold (coil-kt/coil#783)

Resolve #641

* fixup! Rewrite the GIF frame delay if it's below a threshold (coil-kt/coil#783)
This commit is contained in:
FooIbar authored and GitHub committed 2023-01-14 17:19:48 +08:00
1 parent 20bce419c9
commit 0ebb55551c
3 files changed
+179 -3

No files matched your search

+25 -3
View File
@@ -25,8 +25,15 @@ import android.graphics.ImageDecoder.Source
import android.graphics.drawable.Animatable
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import coil.decode.DecodeUtils
import coil.decode.FrameDelayRewritingSource
import coil.decode.isGif
import com.hippo.UriArchiveAccessor
import com.hippo.ehviewer.EhApplication
import okio.Buffer
import okio.BufferedSource
import okio.buffer
import okio.source
import java.io.FileInputStream
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
@@ -80,20 +87,35 @@ class Image private constructor(source: Source, private val byteBuffer: ByteBuff
@Throws(DecodeException::class)
@JvmStatic
fun decode(stream: FileInputStream): Image {
val src = ImageDecoder.createSource(
val source = stream.source().buffer()
val buffer = if (DecodeUtils.isGif(source)) {
rewriteSource(source)
} else {
stream.channel.map(
FileChannel.MapMode.READ_ONLY, 0,
stream.available().toLong()
)
)
}
val src = ImageDecoder.createSource(buffer)
return Image(src)
}
@Throws(DecodeException::class)
@JvmStatic
fun decode(buffer: ByteBuffer): Image {
val src = ImageDecoder.createSource(buffer)
val source = Buffer().apply { write(buffer) }
val byteBuffer = if (DecodeUtils.isGif(source)) {
rewriteSource(source)
} else {
buffer.position(0) as ByteBuffer
}
val src = ImageDecoder.createSource(byteBuffer)
return Image(src, buffer)
}
private fun rewriteSource(source: BufferedSource): ByteBuffer {
val bufferedSource = FrameDelayRewritingSource(source).buffer()
return ByteBuffer.wrap(bufferedSource.use { it.readByteArray() })
}
}
}