/
GifDrawableDrawableDecoder.kt
201 lines (188 loc) · 7.79 KB
/
GifDrawableDrawableDecoder.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (C) 2022 panpf <panpfpanpf@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.panpf.sketch.decode
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import androidx.annotation.WorkerThread
import androidx.exifinterface.media.ExifInterface
import com.github.panpf.sketch.ComponentRegistry
import com.github.panpf.sketch.Sketch
import com.github.panpf.sketch.datasource.AssetDataSource
import com.github.panpf.sketch.datasource.BasedFileDataSource
import com.github.panpf.sketch.datasource.BasedStreamDataSource
import com.github.panpf.sketch.datasource.ByteArrayDataSource
import com.github.panpf.sketch.datasource.ContentDataSource
import com.github.panpf.sketch.datasource.DataSource
import com.github.panpf.sketch.datasource.ResourceDataSource
import com.github.panpf.sketch.decode.internal.ImageFormat
import com.github.panpf.sketch.decode.internal.calculateSampleSize
import com.github.panpf.sketch.decode.internal.createInSampledTransformed
import com.github.panpf.sketch.decode.internal.isGif
import com.github.panpf.sketch.decode.internal.isSmallerSizeMode
import com.github.panpf.sketch.drawable.GifDrawableWrapperDrawable
import com.github.panpf.sketch.drawable.SketchAnimatableDrawable
import com.github.panpf.sketch.fetch.FetchResult
import com.github.panpf.sketch.request.ANIMATION_REPEAT_INFINITE
import com.github.panpf.sketch.request.animatable2CompatCallbackOf
import com.github.panpf.sketch.request.animatedTransformation
import com.github.panpf.sketch.request.animationEndCallback
import com.github.panpf.sketch.request.animationStartCallback
import com.github.panpf.sketch.request.internal.RequestContext
import com.github.panpf.sketch.request.repeatCount
import com.github.panpf.sketch.util.Size
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import pl.droidsonroids.gif.GifInfoHandleHelper
import pl.droidsonroids.gif.GifOptions
import pl.droidsonroids.gif.transforms.Transform
/**
* Adds gif support by koral GifDrawable
*/
fun ComponentRegistry.Builder.supportKoralGif(): ComponentRegistry.Builder = apply {
addDrawableDecoder(GifDrawableDrawableDecoder.Factory())
}
/**
* Only the following attributes are supported:
*
* resize.size
*
* resize.precision: It is always LESS_PIXELS
*
* repeatCount
*
* animatedTransformation
*
* onAnimationStart
*
* onAnimationEnd
*/
class GifDrawableDrawableDecoder(
private val requestContext: RequestContext,
private val dataSource: DataSource,
) : DrawableDecoder {
@WorkerThread
override suspend fun decode(): Result<DrawableDecodeResult> = kotlin.runCatching {
val request = requestContext.request
val gifInfoHandleHelper = GifInfoHandleHelper(dataSource)
val imageWidth = gifInfoHandleHelper.width
val imageHeight = gifInfoHandleHelper.height
val resizeSize = requestContext.resizeSize
val imageSize = Size(imageWidth, imageHeight)
val precision = request.resizePrecisionDecider.get(
imageWidth = imageSize.width,
imageHeight = imageSize.height,
resizeWidth = resizeSize.width,
resizeHeight = resizeSize.height
)
val inSampleSize = calculateSampleSize(
imageSize = imageSize,
targetSize = resizeSize,
smallerSizeMode = precision.isSmallerSizeMode(),
)
gifInfoHandleHelper.setOptions(GifOptions().apply {
setInSampleSize(inSampleSize)
})
val gifDrawable = gifInfoHandleHelper.createGifDrawable().apply {
loopCount =
(request.repeatCount ?: ANIMATION_REPEAT_INFINITE).takeIf { it != -1 } ?: 0
// Set the animated transformation to be applied on each frame.
val transformation = request.animatedTransformation
if (transformation != null) {
transform = object : Transform {
override fun onBoundsChange(bounds: Rect?) {
}
override fun onDraw(canvas: Canvas, paint: Paint?, buffer: Bitmap?) {
transformation.transform(canvas)
}
}
}
}
val transformedList =
if (inSampleSize != 1) listOf(createInSampledTransformed(inSampleSize)) else null
val imageInfo = ImageInfo(
imageWidth,
imageHeight,
ImageFormat.GIF.mimeType,
ExifInterface.ORIENTATION_UNDEFINED
)
val animatableDrawable = SketchAnimatableDrawable(
animatableDrawable = GifDrawableWrapperDrawable(gifDrawable),
imageUri = request.uriString,
requestKey = requestContext.key,
requestCacheKey = requestContext.cacheKey,
imageInfo = imageInfo,
dataFrom = dataSource.dataFrom,
transformedList = transformedList,
extras = null,
).apply {
// Set the start and end animation callbacks if any one is supplied through the request.
val onStart = request.animationStartCallback
val onEnd = request.animationEndCallback
if (onStart != null || onEnd != null) {
withContext(Dispatchers.Main) {
registerAnimationCallback(animatable2CompatCallbackOf(onStart, onEnd))
}
}
}
DrawableDecodeResult(
drawable = animatableDrawable,
imageInfo = animatableDrawable.imageInfo,
dataFrom = animatableDrawable.dataFrom,
transformedList = animatableDrawable.transformedList,
extras = animatableDrawable.extras,
)
}
class Factory : DrawableDecoder.Factory {
override fun create(
sketch: Sketch,
requestContext: RequestContext,
fetchResult: FetchResult
): GifDrawableDrawableDecoder? {
val dataSource = fetchResult.dataSource
if (!requestContext.request.disallowAnimatedImage
&& (dataSource is ByteArrayDataSource
|| dataSource is ResourceDataSource
|| dataSource is ContentDataSource
|| dataSource is AssetDataSource
|| dataSource is BasedFileDataSource
|| dataSource is BasedStreamDataSource)
) {
val imageFormat = ImageFormat.parseMimeType(fetchResult.mimeType)
// Some sites disguise the suffix of a GIF file as a JPEG, which must be identified by the file header
val isGif = if (imageFormat == null) {
fetchResult.headerBytes.isGif()
} else {
imageFormat == ImageFormat.GIF
}
if (isGif) {
return GifDrawableDrawableDecoder(requestContext, fetchResult.dataSource)
}
}
return null
}
override fun toString(): String = "GifDrawableDrawableDecoder"
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
return true
}
override fun hashCode(): Int {
return javaClass.hashCode()
}
}
}