Skip to content

Commit

Permalink
1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Feb 26, 2024
1 parent c93bfed commit 93c88d8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion image-toolbox/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ afterEvaluate {
create<MavenPublication>("mavenJava") {
groupId = "com.github.t8rin"
artifactId = "imageToolboxLibs"
version = "1.1.1"
version = "1.1.2"
from(components["release"])
}
}
Expand Down
65 changes: 52 additions & 13 deletions libs/apng/src/main/java/oupson/apng/coil/AnimatedPngDecoder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package oupson.apng.coil

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import coil.ImageLoader
Expand All @@ -9,6 +10,8 @@ import coil.decode.Decoder
import coil.decode.ImageSource
import coil.fetch.SourceResult
import coil.request.Options
import coil.size.Size
import coil.size.pxOrElse
import okio.BufferedSource
import okio.ByteString.Companion.toByteString
import oupson.apng.decoder.ApngDecoder
Expand All @@ -18,32 +21,67 @@ import java.io.ByteArrayInputStream

class AnimatedPngDecoder private constructor(
private val source: ImageSource,
private val options: Options,
private val context: Context
) : Decoder {

override suspend fun decode(): DecodeResult? {
override suspend fun decode(): DecodeResult {
val array = source.source().readByteArray()
val inputStream = ByteArrayInputStream(array)

val drawable: ApngDrawable = ApngDecoder(
input = inputStream
).decodeApng(context).getOrNull() as? ApngDrawable ?: return null
return if (drawable.numberOfFrames == 1) DecodeResult(
drawable = BitmapDrawable(
context.resources,
BitmapFactory.decodeByteArray(
array, 0, array.size
)
),
false
) else {
val drawable: ApngDrawable? = ApngDecoder(
input = inputStream,
config = ApngDecoder.Config(
bitmapConfig = options.config
)
).decodeApng(context).getOrNull() as? ApngDrawable

return if (drawable?.numberOfFrames == 1 || drawable == null) {
DecodeResult(
drawable = BitmapDrawable(
context.resources,
BitmapFactory.decodeByteArray(
array, 0, array.size
).apply {
config = options.config
}.createScaledBitmap(options.size)
),
isSampled = true
)
} else {
DecodeResult(
drawable = drawable,
isSampled = false
)
}
}

private fun Bitmap.createScaledBitmap(
size: Size
): Bitmap {
if (size == Size.ORIGINAL) return this

return flexibleResize(maxOf(size.width.pxOrElse { 1 }, size.height.pxOrElse { 1 }))
}

private fun Bitmap.flexibleResize(
max: Int
): Bitmap {
val image = this

return runCatching {
if (image.height >= image.width) {
val aspectRatio = image.width.toDouble() / image.height.toDouble()
val targetWidth = (max * aspectRatio).toInt()
Bitmap.createScaledBitmap(image, targetWidth, max, true)
} else {
val aspectRatio = image.height.toDouble() / image.width.toDouble()
val targetHeight = (max * aspectRatio).toInt()
Bitmap.createScaledBitmap(image, max, targetHeight, true)
}
}.getOrNull() ?: image
}

class Factory(
private val context: Context
) : Decoder.Factory {
Expand All @@ -55,6 +93,7 @@ class AnimatedPngDecoder private constructor(
): Decoder? = if (isAPNG(result.source.source())) {
AnimatedPngDecoder(
source = result.source,
options = options,
context = context
)
} else null
Expand Down

0 comments on commit 93c88d8

Please sign in to comment.