-
Notifications
You must be signed in to change notification settings - Fork 307
/
BlurTransformation.kt
141 lines (126 loc) · 4.84 KB
/
BlurTransformation.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
/*
* 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.transform
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.IntRange
import androidx.annotation.WorkerThread
import com.github.panpf.sketch.Sketch
import com.github.panpf.sketch.decode.internal.freeBitmap
import com.github.panpf.sketch.decode.internal.getOrCreate
import com.github.panpf.sketch.decode.internal.logString
import com.github.panpf.sketch.request.internal.RequestContext
import com.github.panpf.sketch.util.fastGaussianBlur
import com.github.panpf.sketch.util.safeConfig
/**
* Bitmap blur transformation
*/
class BlurTransformation constructor(
/** Blur radius */
@IntRange(from = 0, to = 100)
val radius: Int = 15,
/** If the Bitmap has transparent pixels, it will force the Bitmap to add an opaque background color and then blur it */
@ColorInt
val hasAlphaBitmapBgColor: Int? = Color.BLACK,
/** Overlay the blurred image with a layer of color, often useful when using images as a background */
@ColorInt
val maskColor: Int? = null,
) : Transformation {
companion object {
private const val MODULE = "BlurTransformation"
}
init {
require(radius in 1..100) {
"Radius must range from 1 to 100: $radius"
}
require(hasAlphaBitmapBgColor == null || Color.alpha(hasAlphaBitmapBgColor) == 255) {
"hasAlphaBitmapBgColor must be not transparent"
}
}
override val key: String =
"BlurTransformation(${radius},$hasAlphaBitmapBgColor,$maskColor)"
@WorkerThread
override suspend fun transform(
sketch: Sketch,
requestContext: RequestContext,
input: Bitmap
): TransformResult {
// Transparent pixels cannot be blurred
val compatAlphaBitmap = if (hasAlphaBitmapBgColor != null && input.hasAlpha()) {
val bitmap = sketch.bitmapPool.getOrCreate(
input.width,
input.height,
input.safeConfig,
requestContext.request.disallowReuseBitmap,
"BlurTransformation"
)
val canvas = Canvas(bitmap)
canvas.drawColor(hasAlphaBitmapBgColor)
canvas.drawBitmap(input, 0f, 0f, null)
bitmap
} else {
input
}
val outBitmap = fastGaussianBlur(compatAlphaBitmap, radius)
if (outBitmap !== compatAlphaBitmap) {
sketch.logger.d(MODULE) {
"transform. newBitmap. ${outBitmap.logString}. '${requestContext.key}'"
}
if (compatAlphaBitmap !== input) {
sketch.bitmapPool.freeBitmap(
bitmap = compatAlphaBitmap,
disallowReuseBitmap = requestContext.request.disallowReuseBitmap,
caller = "BlurTransformation"
)
sketch.logger.d(MODULE) {
"transform. freeBitmap. bitmap=${compatAlphaBitmap.logString}. '${requestContext.key}'"
}
}
}
maskColor?.let {
Canvas(outBitmap).drawColor(it)
}
return TransformResult(
outBitmap,
createBlurTransformed(radius, hasAlphaBitmapBgColor, maskColor),
)
}
override fun toString(): String = key
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as BlurTransformation
if (radius != other.radius) return false
if (hasAlphaBitmapBgColor != other.hasAlphaBitmapBgColor) return false
if (maskColor != other.maskColor) return false
return true
}
override fun hashCode(): Int {
var result = radius
result = 31 * result + (hasAlphaBitmapBgColor ?: 0)
result = 31 * result + (maskColor ?: 0)
return result
}
}
fun createBlurTransformed(
radius: Int, hasAlphaBitmapBgColor: Int?, maskColor: Int?
) = "BlurTransformed($radius,$hasAlphaBitmapBgColor,$maskColor)"
fun isBlurTransformed(transformed: String): Boolean =
transformed.startsWith("BlurTransformed(")
fun List<String>.getBlurTransformed(): String? =
find { isBlurTransformed(it) }