1- @file:Suppress(" unused" 
2- 
31package  com.commit451.coiltransformations 
42
5- import  android.content.Context 
63import  android.graphics.Bitmap 
74import  android.graphics.Paint 
8- import  android.renderscript.Allocation 
9- import  android.renderscript.Element 
10- import  android.renderscript.RenderScript 
11- import  android.renderscript.ScriptIntrinsicBlur 
12- import  androidx.annotation.RequiresApi 
135import  androidx.core.graphics.applyCanvas 
146import  androidx.core.graphics.createBitmap 
157import  coil.size.Size 
168import  coil.transform.Transformation 
179import  com.commit451.coiltransformations.Util.safeConfig 
10+ import  com.google.android.renderscript.Toolkit 
1811
1912/* *
2013 * A [Transformation] that applies a Gaussian blur to an image. 
2114 * 
22-  * @param context The [Context] used to create a [RenderScript] instance. 
2315 * @param radius The radius of the blur. 
2416 * @param sampling The sampling multiplier used to scale the image. Values > 1 
2517 *  will downscale the image. Values between 0 and 1 will upscale the image. 
2618 */  
27- @RequiresApi(18 )
28- class  BlurTransformation  @JvmOverloads constructor(
29-     private  val  context :  Context ,
30-     private  val  radius :  Float  = DEFAULT_RADIUS ,
19+ data class  BlurTransformation  @JvmOverloads constructor(
20+     private  val  radius :  Int  = DEFAULT_RADIUS ,
3121    private  val  sampling :  Float  = DEFAULT_SAMPLING 
3222) : Transformation {
3323
3424    init  {
35-         require(radius in  0.0 .. 25.0 ) { " radius must be in [0 , 25]." 
25+         require(radius in  1 .. 25 ) { " radius must be in [1 , 25]." 
3626        require(sampling >  0 ) { " sampling must be > 0." 
3727    }
3828
39-     @Suppress(" NullableToStringCall" 
4029    override  val  cacheKey =  " ${BlurTransformation ::class .java.name} -$radius -$sampling " 
4130
4231    override  suspend  fun  transform (input :  Bitmap , size :  Size ): Bitmap  {
@@ -50,31 +39,11 @@ class BlurTransformation @JvmOverloads constructor(
5039            drawBitmap(input, 0f , 0f , paint)
5140        }
5241
53-         var  script:  RenderScript ?  =  null 
54-         var  tmpInt:  Allocation ?  =  null 
55-         var  tmpOut:  Allocation ?  =  null 
56-         var  blur:  ScriptIntrinsicBlur ?  =  null 
57-         try  {
58-             script =  RenderScript .create(context)
59-             tmpInt =  Allocation .createFromBitmap(script, output, Allocation .MipmapControl .MIPMAP_NONE , Allocation .USAGE_SCRIPT )
60-             tmpOut =  Allocation .createTyped(script, tmpInt.type)
61-             blur =  ScriptIntrinsicBlur .create(script, Element .U8_4 (script))
62-             blur.setRadius(radius)
63-             blur.setInput(tmpInt)
64-             blur.forEach(tmpOut)
65-             tmpOut.copyTo(output)
66-         } finally  {
67-             script?.destroy()
68-             tmpInt?.destroy()
69-             tmpOut?.destroy()
70-             blur?.destroy()
71-         }
72- 
73-         return  output
42+         return  Toolkit .blur(output, radius)
7443    }
7544
7645    private  companion  object  {
77-         private  const  val  DEFAULT_RADIUS  =  10f 
46+         private  const  val  DEFAULT_RADIUS  =  10 
7847        private  const  val  DEFAULT_SAMPLING  =  1f 
7948    }
8049}
0 commit comments