Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ allprojects {
repositories {
google()
mavenCentral()
maven("https://jitpack.io")
}
}
2 changes: 1 addition & 1 deletion sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles("shrinker-rules.pro", "shrinker-rules-android.pro")
proguardFiles("shrinker-rules.pro", "shrinker-rules-android.pro", "okhttp-rules.pro")
signingConfig = signingConfigs.getByName("debug")
}
}
Expand Down
25 changes: 25 additions & 0 deletions sample/okhttp-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# OkHttp ProGuard Rules
# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**

# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform
-dontwarn org.conscrypt.ConscryptHostnameVerifier
-dontwarn org.conscrypt.Conscrypt$Version
-dontwarn org.conscrypt.Conscrypt

# BouncyCastle
-dontwarn org.bouncycastle.jsse.BCSSLParameters
-dontwarn org.bouncycastle.jsse.BCSSLSocket
-dontwarn org.bouncycastle.jsse.provider.BouncyCastleJsseProvider

# OpenJSSE
-dontwarn org.openjsse.javax.net.ssl.SSLParameters
-dontwarn org.openjsse.javax.net.ssl.SSLSocket
-dontwarn org.openjsse.net.ssl.OpenJSSE
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
transformation = GrayscaleTransformation()
)
images += Image(
transformation = BlurTransformation(context)
transformation = BlurTransformation()
)
images += Image(
transformation = MaskTransformation(context, R.drawable.mask_starfish)
Expand Down
1 change: 1 addition & 0 deletions transformations/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ android {
dependencies {
api("io.coil-kt:coil-base:${project.coilVersion}")
api("androidx.core:core-ktx:1.7.0")
implementation("com.github.android:renderscript-intrinsics-replacement-toolkit:598692ab79")
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
@file:Suppress("unused")

package com.commit451.coiltransformations

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Paint
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur
import androidx.annotation.RequiresApi
import androidx.core.graphics.applyCanvas
import androidx.core.graphics.createBitmap
import coil.size.Size
import coil.transform.Transformation
import com.commit451.coiltransformations.Util.safeConfig
import com.google.android.renderscript.Toolkit

/**
* A [Transformation] that applies a Gaussian blur to an image.
*
* @param context The [Context] used to create a [RenderScript] instance.
* @param radius The radius of the blur.
* @param sampling The sampling multiplier used to scale the image. Values > 1
* will downscale the image. Values between 0 and 1 will upscale the image.
*/
@RequiresApi(18)
class BlurTransformation @JvmOverloads constructor(
private val context: Context,
private val radius: Float = DEFAULT_RADIUS,
data class BlurTransformation @JvmOverloads constructor(
private val radius: Int = DEFAULT_RADIUS,
private val sampling: Float = DEFAULT_SAMPLING
) : Transformation {

init {
require(radius in 0.0..25.0) { "radius must be in [0, 25]." }
require(radius in 1..25) { "radius must be in [1, 25]." }
require(sampling > 0) { "sampling must be > 0." }
}

@Suppress("NullableToStringCall")
override val cacheKey = "${BlurTransformation::class.java.name}-$radius-$sampling"

override suspend fun transform(input: Bitmap, size: Size): Bitmap {
Expand All @@ -50,31 +39,11 @@ class BlurTransformation @JvmOverloads constructor(
drawBitmap(input, 0f, 0f, paint)
}

var script: RenderScript? = null
var tmpInt: Allocation? = null
var tmpOut: Allocation? = null
var blur: ScriptIntrinsicBlur? = null
try {
script = RenderScript.create(context)
tmpInt = Allocation.createFromBitmap(script, output, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT)
tmpOut = Allocation.createTyped(script, tmpInt.type)
blur = ScriptIntrinsicBlur.create(script, Element.U8_4(script))
blur.setRadius(radius)
blur.setInput(tmpInt)
blur.forEach(tmpOut)
tmpOut.copyTo(output)
} finally {
script?.destroy()
tmpInt?.destroy()
tmpOut?.destroy()
blur?.destroy()
}

return output
return Toolkit.blur(output, radius)
}

private companion object {
private const val DEFAULT_RADIUS = 10f
private const val DEFAULT_RADIUS = 10
private const val DEFAULT_SAMPLING = 1f
}
}