1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.example.testapp
18
19 import android.graphics.Bitmap
20 import android.renderscript.Allocation
21 import android.renderscript.Element
22 import android.renderscript.RenderScript
23 import android.renderscript.Script
24 import android.renderscript.ScriptIntrinsicBlur
25 import android.renderscript.Type
26 import android.renderscript.toolkit.Range2d
27
28 /**
29 * Does a Blur operation using the RenderScript Intrinsics.
30 */
intrinsicBlurnull31 fun intrinsicBlur(
32 context: RenderScript,
33 inputArray: ByteArray,
34 vectorSize: Int,
35 sizeX: Int,
36 sizeY: Int,
37 radius: Int,
38 restriction: Range2d?
39 ): ByteArray {
40 val scriptBlur = ScriptIntrinsicBlur.create(
41 context,
42 if (vectorSize == 4) Element.RGBA_8888(context) else Element.U8(context)
43 )
44 val builder =
45 Type.Builder(
46 context,
47 renderScriptVectorElementForU8(context, vectorSize)
48 )
49 builder.setX(sizeX)
50 builder.setY(sizeY)
51 val arrayType = builder.create()
52 val inputAllocation = Allocation.createTyped(context, arrayType)
53 inputAllocation.copyFrom(inputArray)
54 val outAllocation = Allocation.createTyped(context, arrayType)
55
56 val intrinsicOutArray = ByteArray(sizeX * sizeY * vectorSize)
57 scriptBlur.setRadius(radius.toFloat())
58 scriptBlur.setInput(inputAllocation)
59
60 if (restriction != null) {
61 outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
62 val options = Script.LaunchOptions()
63 options.setX(restriction.startX, restriction.endX)
64 options.setY(restriction.startY, restriction.endY)
65 scriptBlur.forEach(outAllocation, options)
66 } else {
67 scriptBlur.forEach(outAllocation)
68 }
69 outAllocation.copyTo(intrinsicOutArray)
70 inputAllocation.destroy()
71 outAllocation.destroy()
72 arrayType.destroy()
73 scriptBlur.destroy()
74 return intrinsicOutArray
75 }
76
intrinsicBlurnull77 fun intrinsicBlur(
78 context: RenderScript,
79 bitmap: Bitmap,
80 radius: Int,
81 restriction: Range2d?
82 ): ByteArray {
83 val baseElement = renderScriptElementForBitmap(context, bitmap)
84 val scriptBlur = ScriptIntrinsicBlur.create(context, baseElement)
85 val inputAllocation = Allocation.createFromBitmap(context, bitmap)
86 inputAllocation.copyFrom(bitmap)
87 val outAllocation = Allocation.createTyped(context, inputAllocation.type)
88 val intrinsicOutArray = ByteArray(bitmap.byteCount)
89
90 scriptBlur.setRadius(radius.toFloat())
91 scriptBlur.setInput(inputAllocation)
92
93 if (restriction != null) {
94 outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
95 val options = Script.LaunchOptions()
96 options.setX(restriction.startX, restriction.endX)
97 options.setY(restriction.startY, restriction.endY)
98 scriptBlur.forEach(outAllocation, options)
99 } else {
100 scriptBlur.forEach(outAllocation)
101 }
102 outAllocation.copyTo(intrinsicOutArray)
103
104 inputAllocation.destroy()
105 outAllocation.destroy()
106 scriptBlur.destroy()
107 return intrinsicOutArray
108 }
109