1 /*
2  * Copyright (C) 2012 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 androidx.renderscript;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.util.Log;
22 
23 /**
24  * Intrinsic Gausian blur filter. Applies a gaussian blur of the
25  * specified radius to all elements of an allocation.
26  *
27  *
28  **/
29 public class ScriptIntrinsicBlur extends ScriptIntrinsic {
30     private final float[] mValues = new float[9];
31     private Allocation mInput;
32     // API level for the intrinsic
33     private static final int INTRINSIC_API_LEVEL = 19;
34 
ScriptIntrinsicBlur(long id, RenderScript rs)35     protected ScriptIntrinsicBlur(long id, RenderScript rs) {
36         super(id, rs);
37     }
38 
39     /**
40      * Create an intrinsic for applying a blur to an allocation. The
41      * default radius is 5.0.
42      *
43      * Supported elements types are {@link Element#U8},
44      * {@link Element#U8_4}.
45      *
46      * @param rs The RenderScript context
47      * @param e Element type for inputs and outputs
48      *
49      * @return ScriptIntrinsicBlur
50      */
create(RenderScript rs, Element e)51     public static ScriptIntrinsicBlur create(RenderScript rs, Element e) {
52         if ((!e.isCompatible(Element.U8_4(rs))) && (!e.isCompatible(Element.U8(rs)))) {
53             throw new RSIllegalArgumentException("Unsupported element type.");
54         }
55         long id;
56         boolean mUseIncSupp = rs.isUseNative() &&
57                               android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
58 
59         id = rs.nScriptIntrinsicCreate(5, e.getID(rs), mUseIncSupp);
60 
61         ScriptIntrinsicBlur si = new ScriptIntrinsicBlur(id, rs);
62         si.setIncSupp(mUseIncSupp);
63         si.setRadius(5.f);
64 
65         return si;
66     }
67 
68     /**
69      * Set the input of the blur.
70      * Must match the element type supplied during create.
71      *
72      * @param ain The input allocation
73      */
74     public void setInput(Allocation ain) {
75         if (ain.getType().getY() == 0) {
76             throw new RSIllegalArgumentException("Input set to a 1D Allocation");
77         }
78         mInput = ain;
79         setVar(1, ain);
80     }
81 
82     /**
83      * Set the radius of the Blur.
84      *
85      * Supported range 0 < radius <= 25
86      *
87      * @param radius The radius of the blur
88      */
89     public void setRadius(float radius) {
90         if (radius <= 0 || radius > 25) {
91             throw new RSIllegalArgumentException("Radius out of range (0 < r <= 25).");
92         }
93         setVar(0, radius);
94     }
95 
96     /**
97      * Apply the filter to the input and save to the specified
98      * allocation.
99      *
100      * @param aout Output allocation. Must match creation element
101      *             type.
102      */
103     public void forEach(Allocation aout) {
104         if (aout.getType().getY() == 0) {
105             throw new RSIllegalArgumentException("Output is a 1D Allocation");
106         }
107         forEach(0, (Allocation) null, aout, null);
108     }
109 
110     /**
111      * Get a KernelID for this intrinsic kernel.
112      *
113      * @return Script.KernelID The KernelID object.
114      */
115     public Script.KernelID getKernelID() {
116         return createKernelID(0, 2, null, null);
117     }
118 
119     /**
120      * Get a FieldID for the input field of this intrinsic.
121      *
122      * @return Script.FieldID The FieldID object.
123      */
124     public Script.FieldID getFieldID_Input() {
125         return createFieldID(1, null);
126     }
127 }
128 
129