@@ -50,63 +50,69 @@ class HeifDecoder(
5050 private val source : SourceResult ,
5151 private val options : Options ,
5252 private val imageLoader : ImageLoader ,
53+ private val exceptionLogger : ((Exception ) -> Unit )? = null ,
5354) : Decoder {
5455
5556 private val coder = HeifCoder (context)
5657
5758 override suspend fun decode (): DecodeResult ? = runInterruptible {
58- // ColorSpace is preferred to be ignored due to lib is trying to handle all color profile by itself
59- val sourceData = source.source.source().readByteArray()
59+ try {
60+ // ColorSpace is preferred to be ignored due to lib is trying to handle all color profile by itself
61+ val sourceData = source.source.source().readByteArray()
6062
61- var mPreferredColorConfig: PreferredColorConfig = when (options.config) {
62- Bitmap .Config .ALPHA_8 -> PreferredColorConfig .RGBA_8888
63- Bitmap .Config .RGB_565 -> if (options.allowRgb565) PreferredColorConfig .RGB_565 else PreferredColorConfig .DEFAULT
64- Bitmap .Config .ARGB_8888 -> PreferredColorConfig .RGBA_8888
65- else -> PreferredColorConfig .DEFAULT
66- }
67- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O && options.config == Bitmap .Config .RGBA_F16 ) {
68- mPreferredColorConfig = PreferredColorConfig .RGBA_F16
69- } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q && options.config == Bitmap .Config .HARDWARE ) {
70- mPreferredColorConfig = PreferredColorConfig .HARDWARE
71- } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU && options.config == Bitmap .Config .RGBA_1010102 ) {
72- mPreferredColorConfig = PreferredColorConfig .RGBA_1010102
73- }
63+ var mPreferredColorConfig: PreferredColorConfig = when (options.config) {
64+ Bitmap .Config .ALPHA_8 -> PreferredColorConfig .RGBA_8888
65+ Bitmap .Config .RGB_565 -> if (options.allowRgb565) PreferredColorConfig .RGB_565 else PreferredColorConfig .DEFAULT
66+ Bitmap .Config .ARGB_8888 -> PreferredColorConfig .RGBA_8888
67+ else -> PreferredColorConfig .DEFAULT
68+ }
69+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O && options.config == Bitmap .Config .RGBA_F16 ) {
70+ mPreferredColorConfig = PreferredColorConfig .RGBA_F16
71+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q && options.config == Bitmap .Config .HARDWARE ) {
72+ mPreferredColorConfig = PreferredColorConfig .HARDWARE
73+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU && options.config == Bitmap .Config .RGBA_1010102 ) {
74+ mPreferredColorConfig = PreferredColorConfig .RGBA_1010102
75+ }
76+
77+ if (options.size == coil.size.Size .ORIGINAL ) {
78+ val originalImage =
79+ coder.decode(
80+ sourceData,
81+ preferredColorConfig = mPreferredColorConfig
82+ )
83+ return @runInterruptible DecodeResult (
84+ BitmapDrawable (
85+ options.context.resources,
86+ originalImage
87+ ), false
88+ )
89+ }
90+
91+ val dstWidth = options.size.width.pxOrElse { 0 }
92+ val dstHeight = options.size.height.pxOrElse { 0 }
93+ val scaleMode = when (options.scale) {
94+ Scale .FILL -> ScaleMode .FILL
95+ Scale .FIT -> ScaleMode .FIT
96+ }
7497
75- if (options.size == coil.size.Size .ORIGINAL ) {
7698 val originalImage =
77- coder.decode (
99+ coder.decodeSampled (
78100 sourceData,
79- preferredColorConfig = mPreferredColorConfig
101+ dstWidth,
102+ dstHeight,
103+ preferredColorConfig = mPreferredColorConfig,
104+ scaleMode,
80105 )
81106 return @runInterruptible DecodeResult (
82107 BitmapDrawable (
83108 options.context.resources,
84109 originalImage
85- ), false
110+ ), true
86111 )
112+ } catch (e: Exception ) {
113+ exceptionLogger?.invoke(e)
114+ return @runInterruptible null
87115 }
88-
89- val dstWidth = options.size.width.pxOrElse { 0 }
90- val dstHeight = options.size.height.pxOrElse { 0 }
91- val scaleMode = when (options.scale) {
92- Scale .FILL -> ScaleMode .FILL
93- Scale .FIT -> ScaleMode .FIT
94- }
95-
96- val originalImage =
97- coder.decodeSampled(
98- sourceData,
99- dstWidth,
100- dstHeight,
101- preferredColorConfig = mPreferredColorConfig,
102- scaleMode,
103- )
104- return @runInterruptible DecodeResult (
105- BitmapDrawable (
106- options.context.resources,
107- originalImage
108- ), true
109- )
110116 }
111117
112118 /* *
0 commit comments