Skip to content

Commit

Permalink
Reworked exception logger
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Sep 2, 2024
1 parent fdbc6c7 commit 0cd3e41
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 96 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.4.0-rc02" apply false
id("com.android.application") version "8.5.2" apply false
id("org.jetbrains.kotlin.android") version "1.8.10" apply false
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Sep 18 22:33:25 MYT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -55,59 +55,65 @@ public class AnimatedJxlDecoder(
private val source: SourceResult,
private val options: Options,
private val context: Context,
private val preheatFrames: Int
private val preheatFrames: Int,
private val exceptionLogger: ((Exception) -> Unit)? = null,
) : Decoder {

override suspend fun decode(): DecodeResult = runInterruptible {
// ColorSpace is preferred to be ignored due to lib is trying to handle all color profile by itself
val sourceData = source.source.source().readByteArray()
override suspend fun decode(): DecodeResult? = runInterruptible {
try {
// ColorSpace is preferred to be ignored due to lib is trying to handle all color profile by itself
val sourceData = source.source.source().readByteArray()

var mPreferredColorConfig: PreferredColorConfig = when (options.config) {
Bitmap.Config.ALPHA_8 -> PreferredColorConfig.RGBA_8888
Bitmap.Config.RGB_565 -> if (options.allowRgb565) PreferredColorConfig.RGB_565 else PreferredColorConfig.DEFAULT
Bitmap.Config.ARGB_8888 -> PreferredColorConfig.RGBA_8888
else -> PreferredColorConfig.DEFAULT
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && options.config == Bitmap.Config.RGBA_F16) {
mPreferredColorConfig = PreferredColorConfig.RGBA_F16
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && options.config == Bitmap.Config.HARDWARE) {
mPreferredColorConfig = PreferredColorConfig.HARDWARE
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && options.config == Bitmap.Config.RGBA_1010102) {
mPreferredColorConfig = PreferredColorConfig.RGBA_1010102
}
var mPreferredColorConfig: PreferredColorConfig = when (options.config) {
Bitmap.Config.ALPHA_8 -> PreferredColorConfig.RGBA_8888
Bitmap.Config.RGB_565 -> if (options.allowRgb565) PreferredColorConfig.RGB_565 else PreferredColorConfig.DEFAULT
Bitmap.Config.ARGB_8888 -> PreferredColorConfig.RGBA_8888
else -> PreferredColorConfig.DEFAULT
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && options.config == Bitmap.Config.RGBA_F16) {
mPreferredColorConfig = PreferredColorConfig.RGBA_F16
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && options.config == Bitmap.Config.HARDWARE) {
mPreferredColorConfig = PreferredColorConfig.HARDWARE
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && options.config == Bitmap.Config.RGBA_1010102) {
mPreferredColorConfig = PreferredColorConfig.RGBA_1010102
}

if (options.size == Size.ORIGINAL) {
val originalImage = JxlAnimatedImage(
byteArray = sourceData,
preferredColorConfig = mPreferredColorConfig
)
return@runInterruptible DecodeResult(
drawable = originalImage.drawable(),
isSampled = false
)
}

val dstWidth = options.size.width.pxOrElse { 0 }
val dstHeight = options.size.height.pxOrElse { 0 }
val scaleMode = when (options.scale) {
Scale.FILL -> ScaleMode.FILL
Scale.FIT -> ScaleMode.FIT
}

if (options.size == Size.ORIGINAL) {
val originalImage = JxlAnimatedImage(
byteArray = sourceData,
preferredColorConfig = mPreferredColorConfig
)
return@runInterruptible DecodeResult(
drawable = originalImage.drawable(),
isSampled = false
preferredColorConfig = mPreferredColorConfig,
scaleMode = scaleMode,
jxlResizeFilter = JxlResizeFilter.BILINEAR
)
}

val dstWidth = options.size.width.pxOrElse { 0 }
val dstHeight = options.size.height.pxOrElse { 0 }
val scaleMode = when (options.scale) {
Scale.FILL -> ScaleMode.FILL
Scale.FIT -> ScaleMode.FIT
DecodeResult(
drawable = originalImage.drawable(
dstWidth = dstWidth,
dstHeight = dstHeight
),
isSampled = true
)
} catch (e: Exception) {
exceptionLogger?.invoke(e)
return@runInterruptible null
}

val originalImage = JxlAnimatedImage(
byteArray = sourceData,
preferredColorConfig = mPreferredColorConfig,
scaleMode = scaleMode,
jxlResizeFilter = JxlResizeFilter.BILINEAR
)

DecodeResult(
drawable = originalImage.drawable(
dstWidth = dstWidth,
dstHeight = dstHeight
),
isSampled = true
)
}

private fun JxlAnimatedImage.drawable(
Expand Down Expand Up @@ -136,18 +142,20 @@ public class AnimatedJxlDecoder(

public class Factory(
private val context: Context,
private val preheatFrames: Int = 6
private val preheatFrames: Int = 6,
private val exceptionLogger: ((Exception) -> Unit)? = null,
) : Decoder.Factory {
override fun create(
result: SourceResult,
options: Options,
imageLoader: ImageLoader
imageLoader: ImageLoader,
) = if (isJXL(result.source.source())) {
AnimatedJxlDecoder(
source = result,
options = options,
context = context,
preheatFrames = preheatFrames
preheatFrames = preheatFrames,
exceptionLogger = exceptionLogger,
)
} else null

Expand Down
96 changes: 51 additions & 45 deletions jxlcodercoil/src/main/java/com/awxkee/jxlcoder/coil/JxlDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,72 +49,78 @@ import okio.ByteString.Companion.toByteString
class JxlDecoder(
private val source: SourceResult,
private val options: Options,
private val imageLoader: ImageLoader
private val imageLoader: ImageLoader,
private val exceptionLogger: ((Exception) -> Unit)? = null
) : Decoder {

override suspend fun decode(): DecodeResult? = runInterruptible {
// ColorSpace is preferred to be ignored due to lib is trying to handle all color profile by itself
val sourceData = source.source.source().readByteArray()
try {
// ColorSpace is preferred to be ignored due to lib is trying to handle all color profile by itself
val sourceData = source.source.source().readByteArray()

var mPreferredColorConfig: PreferredColorConfig = when (options.config) {
Bitmap.Config.ALPHA_8 -> PreferredColorConfig.RGBA_8888
Bitmap.Config.RGB_565 -> if (options.allowRgb565) PreferredColorConfig.RGB_565 else PreferredColorConfig.DEFAULT
Bitmap.Config.ARGB_8888 -> PreferredColorConfig.RGBA_8888
else -> PreferredColorConfig.DEFAULT
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && options.config == Bitmap.Config.RGBA_F16) {
mPreferredColorConfig = PreferredColorConfig.RGBA_F16
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && options.config == Bitmap.Config.HARDWARE) {
mPreferredColorConfig = PreferredColorConfig.HARDWARE
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && options.config == Bitmap.Config.RGBA_1010102) {
mPreferredColorConfig = PreferredColorConfig.RGBA_1010102
}
var mPreferredColorConfig: PreferredColorConfig = when (options.config) {
Bitmap.Config.ALPHA_8 -> PreferredColorConfig.RGBA_8888
Bitmap.Config.RGB_565 -> if (options.allowRgb565) PreferredColorConfig.RGB_565 else PreferredColorConfig.DEFAULT
Bitmap.Config.ARGB_8888 -> PreferredColorConfig.RGBA_8888
else -> PreferredColorConfig.DEFAULT
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && options.config == Bitmap.Config.RGBA_F16) {
mPreferredColorConfig = PreferredColorConfig.RGBA_F16
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && options.config == Bitmap.Config.HARDWARE) {
mPreferredColorConfig = PreferredColorConfig.HARDWARE
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && options.config == Bitmap.Config.RGBA_1010102) {
mPreferredColorConfig = PreferredColorConfig.RGBA_1010102
}

if (options.size == coil.size.Size.ORIGINAL) {
val originalImage =
JxlCoder.decode(
sourceData,
preferredColorConfig = mPreferredColorConfig
)
return@runInterruptible DecodeResult(
BitmapDrawable(
options.context.resources,
originalImage
), false
)
}

val dstWidth = options.size.width.pxOrElse { 0 }
val dstHeight = options.size.height.pxOrElse { 0 }
val scaleMode = when (options.scale) {
Scale.FILL -> ScaleMode.FILL
Scale.FIT -> ScaleMode.FIT
}

if (options.size == coil.size.Size.ORIGINAL) {
val originalImage =
JxlCoder.decode(
JxlCoder.decodeSampled(
sourceData,
preferredColorConfig = mPreferredColorConfig
dstWidth,
dstHeight,
preferredColorConfig = mPreferredColorConfig,
scaleMode,
JxlResizeFilter.BILINEAR,
)
return@runInterruptible DecodeResult(
BitmapDrawable(
options.context.resources,
originalImage
), false
), true
)
} catch (e: Exception) {
exceptionLogger?.invoke(e)
return@runInterruptible null
}

val dstWidth = options.size.width.pxOrElse { 0 }
val dstHeight = options.size.height.pxOrElse { 0 }
val scaleMode = when (options.scale) {
Scale.FILL -> ScaleMode.FILL
Scale.FIT -> ScaleMode.FIT
}

val originalImage =
JxlCoder.decodeSampled(
sourceData,
dstWidth,
dstHeight,
preferredColorConfig = mPreferredColorConfig,
scaleMode,
JxlResizeFilter.BILINEAR,
)
return@runInterruptible DecodeResult(
BitmapDrawable(
options.context.resources,
originalImage
), true
)
}

class Factory : Decoder.Factory {
class Factory(private val exceptionLogger: ((Exception) -> Unit)? = null) : Decoder.Factory {
override fun create(
result: SourceResult,
options: Options,
imageLoader: ImageLoader
) = if (isJXL(result.source.source())) {
JxlDecoder(result, options, imageLoader)
JxlDecoder(result, options, imageLoader, exceptionLogger = exceptionLogger)
} else null

private val MAGIC_1 = byteArrayOf(0xFF.toByte(), 0x0A).toByteString()
Expand Down
3 changes: 1 addition & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ pluginManagement {
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven("https://jitpack.io")
google()
}
}

Expand Down

0 comments on commit 0cd3e41

Please sign in to comment.