-
Notifications
You must be signed in to change notification settings - Fork 638
/
ImageDecoderDecoder.kt
187 lines (167 loc) · 7.48 KB
/
ImageDecoderDecoder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package coil.decode
import android.graphics.ImageDecoder
import android.graphics.drawable.AnimatedImageDrawable
import android.graphics.drawable.AnimatedImageDrawable.REPEAT_INFINITE
import android.graphics.drawable.Drawable
import android.os.Build.VERSION.SDK_INT
import androidx.annotation.RequiresApi
import androidx.core.graphics.decodeDrawable
import androidx.core.util.component1
import androidx.core.util.component2
import coil.ImageLoader
import coil.drawable.ScaleDrawable
import coil.fetch.SourceResult
import coil.request.Options
import coil.request.animatedTransformation
import coil.request.animationEndCallback
import coil.request.animationStartCallback
import coil.request.repeatCount
import coil.util.animatable2CallbackOf
import coil.util.asPostProcessor
import coil.util.heightPx
import coil.util.isHardware
import coil.util.widthPx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withContext
import okio.BufferedSource
import okio.buffer
import java.nio.ByteBuffer
import kotlin.math.roundToInt
/**
* A [Decoder] that uses [ImageDecoder] to decode GIFs, animated WebPs, and animated HEIFs.
*
* NOTE: Animated HEIF files are only supported on API 30 and above.
*
* @param enforceMinimumFrameDelay If true, rewrite a GIF's frame delay to a default value if
* it is below a threshold. See https://github.com/coil-kt/coil/issues/540 for more info.
*/
@RequiresApi(28)
class ImageDecoderDecoder @JvmOverloads constructor(
private val source: ImageSource,
private val options: Options,
private val enforceMinimumFrameDelay: Boolean = true
) : Decoder {
override suspend fun decode(): DecodeResult {
var isSampled = false
val drawable = runInterruptible {
var imageDecoder: ImageDecoder? = null
val wrappedSource = wrapImageSource(source)
try {
wrappedSource.toImageDecoderSource().decodeDrawable { info, _ ->
// Capture the image decoder to manually close it later.
imageDecoder = this
// Configure the output image's size.
val (srcWidth, srcHeight) = info.size
val dstWidth = options.size.widthPx(options.scale) { srcWidth }
val dstHeight = options.size.heightPx(options.scale) { srcHeight }
if (srcWidth > 0 && srcHeight > 0 &&
(srcWidth != dstWidth || srcHeight != dstHeight)) {
val multiplier = DecodeUtils.computeSizeMultiplier(
srcWidth = srcWidth,
srcHeight = srcHeight,
dstWidth = dstWidth,
dstHeight = dstHeight,
scale = options.scale
)
// Set the target size if the image is larger than the requested dimensions
// or the request requires exact dimensions.
isSampled = multiplier < 1
if (isSampled || !options.allowInexactSize) {
val targetWidth = (multiplier * srcWidth).roundToInt()
val targetHeight = (multiplier * srcHeight).roundToInt()
setTargetSize(targetWidth, targetHeight)
}
}
// Configure any other attributes.
configureImageDecoderProperties()
}
} finally {
imageDecoder?.close()
wrappedSource.close()
}
}
return DecodeResult(wrapDrawable(drawable), isSampled)
}
private fun wrapImageSource(source: ImageSource): ImageSource {
return if (enforceMinimumFrameDelay && DecodeUtils.isGif(source.source())) {
// Wrap the source to rewrite its frame delay as it's read.
val rewritingSource = FrameDelayRewritingSource(source.source())
ImageSource(rewritingSource.buffer(), options.context)
} else {
source
}
}
private fun ImageSource.toImageDecoderSource(): ImageDecoder.Source {
val file = fileOrNull()
if (file != null) {
return ImageDecoder.createSource(file.toFile())
}
val metadata = metadata
if (metadata is AssetMetadata) {
return ImageDecoder.createSource(options.context.assets, metadata.fileName)
}
if (metadata is ContentMetadata) {
return ImageDecoder.createSource(options.context.contentResolver, metadata.uri)
}
if (metadata is ResourceMetadata && metadata.packageName == options.context.packageName) {
return ImageDecoder.createSource(options.context.resources, metadata.resId)
}
return when {
SDK_INT >= 31 -> ImageDecoder.createSource(source().readByteArray())
SDK_INT == 30 -> ImageDecoder.createSource(ByteBuffer.wrap(source().readByteArray()))
// https://issuetracker.google.com/issues/139371066
else -> ImageDecoder.createSource(file().toFile())
}
}
private fun ImageDecoder.configureImageDecoderProperties() {
allocator = if (options.config.isHardware) {
ImageDecoder.ALLOCATOR_HARDWARE
} else {
ImageDecoder.ALLOCATOR_SOFTWARE
}
memorySizePolicy = if (options.allowRgb565) {
ImageDecoder.MEMORY_POLICY_LOW_RAM
} else {
ImageDecoder.MEMORY_POLICY_DEFAULT
}
if (options.colorSpace != null) {
setTargetColorSpace(options.colorSpace)
}
isUnpremultipliedRequired = !options.premultipliedAlpha
postProcessor = options.parameters.animatedTransformation()?.asPostProcessor()
}
private suspend fun wrapDrawable(baseDrawable: Drawable): Drawable {
if (baseDrawable !is AnimatedImageDrawable) {
return baseDrawable
}
baseDrawable.repeatCount = options.parameters.repeatCount() ?: REPEAT_INFINITE
// Set the start and end animation callbacks if any one is supplied through the request.
val onStart = options.parameters.animationStartCallback()
val onEnd = options.parameters.animationEndCallback()
if (onStart != null || onEnd != null) {
// Animation callbacks must be set on the main thread.
withContext(Dispatchers.Main.immediate) {
baseDrawable.registerAnimationCallback(animatable2CallbackOf(onStart, onEnd))
}
}
// Wrap AnimatedImageDrawable in a ScaleDrawable so it always scales to fill its bounds.
return ScaleDrawable(baseDrawable, options.scale)
}
@RequiresApi(28)
class Factory @JvmOverloads constructor(
private val enforceMinimumFrameDelay: Boolean = true
) : Decoder.Factory {
override fun create(result: SourceResult, options: Options, imageLoader: ImageLoader): Decoder? {
if (!isApplicable(result.source.source())) return null
return ImageDecoderDecoder(result.source, options, enforceMinimumFrameDelay)
}
private fun isApplicable(source: BufferedSource): Boolean {
return DecodeUtils.isGif(source) ||
DecodeUtils.isAnimatedWebP(source) ||
(SDK_INT >= 30 && DecodeUtils.isAnimatedHeif(source))
}
override fun equals(other: Any?) = other is Factory
override fun hashCode() = javaClass.hashCode()
}
}