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 android.renderscript;
18 
19 /**
20  * Intrinsic for applying a 3x3 convolve to an allocation.
21  *
22  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
23  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
24  * guide</a> for the proposed alternatives.
25  **/
26 @Deprecated
27 public final class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic {
28     private final float[] mValues = new float[9];
29     private Allocation mInput;
30 
ScriptIntrinsicConvolve3x3(long id, RenderScript rs)31     private ScriptIntrinsicConvolve3x3(long id, RenderScript rs) {
32         super(id, rs);
33     }
34 
35     /**
36      * Supported elements types are {@link Element#U8}, {@link
37      * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
38      * {@link Element#F32}, {@link Element#F32_2}, {@link
39      * Element#F32_3}, and {@link Element#F32_4}.
40      *
41      * <p> The default coefficients are:
42      * <code>
43      * <p> [ 0,  0,  0 ]
44      * <p> [ 0,  1,  0 ]
45      * <p> [ 0,  0,  0 ]
46      * </code>
47      *
48      * @param rs The RenderScript context
49      * @param e Element type for intputs and outputs
50      *
51      * @return ScriptIntrinsicConvolve3x3
52      */
create(RenderScript rs, Element e)53     public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) {
54         float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0};
55         if (!e.isCompatible(Element.U8(rs)) &&
56             !e.isCompatible(Element.U8_2(rs)) &&
57             !e.isCompatible(Element.U8_3(rs)) &&
58             !e.isCompatible(Element.U8_4(rs)) &&
59             !e.isCompatible(Element.F32(rs)) &&
60             !e.isCompatible(Element.F32_2(rs)) &&
61             !e.isCompatible(Element.F32_3(rs)) &&
62             !e.isCompatible(Element.F32_4(rs))) {
63             throw new RSIllegalArgumentException("Unsupported element type.");
64         }
65         long id = rs.nScriptIntrinsicCreate(1, e.getID(rs));
66         ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
67         si.setCoefficients(f);
68         return si;
69 
70     }
71 
72     /**
73      * Set the input of the 3x3 convolve.
74      * Must match the element type supplied during create.
75      *
76      * @param ain The input allocation.
77      */
setInput(Allocation ain)78     public void setInput(Allocation ain) {
79         mInput = ain;
80         setVar(1, ain);
81     }
82 
83     /**
84      * Set the coefficients for the convolve.
85      *
86      * <p> The convolve layout is:
87      * <code>
88      * <p> [ 0,  1,  2 ]
89      * <p> [ 3,  4,  5 ]
90      * <p> [ 6,  7,  8 ]
91      * </code>
92      *
93      * @param v The array of coefficients to set
94      */
setCoefficients(float v[])95     public void setCoefficients(float v[]) {
96         FieldPacker fp = new FieldPacker(9*4);
97         for (int ct=0; ct < mValues.length; ct++) {
98             mValues[ct] = v[ct];
99             fp.addF32(mValues[ct]);
100         }
101         setVar(0, fp);
102     }
103 
104     /**
105      * Apply the filter to the input and save to the specified
106      * allocation.
107      *
108      * @param aout Output allocation. Must match creation element
109      *             type.
110      */
forEach(Allocation aout)111     public void forEach(Allocation aout) {
112         forEach(0, (Allocation) null, aout, null);
113     }
114 
115     /**
116      * Apply the filter to the input and save to the specified
117      * allocation.
118      *
119      * @param aout Output allocation. Must match creation element
120      *             type.
121      * @param opt LaunchOptions for clipping
122      */
forEach(Allocation aout, Script.LaunchOptions opt)123     public void forEach(Allocation aout, Script.LaunchOptions opt) {
124         forEach(0, (Allocation) null, aout, null, opt);
125     }
126 
127     /**
128      * Get a KernelID for this intrinsic kernel.
129      *
130      * @return Script.KernelID The KernelID object.
131      */
getKernelID()132     public Script.KernelID getKernelID() {
133         return createKernelID(0, 2, null, null);
134     }
135 
136     /**
137      * Get a FieldID for the input field of this intrinsic.
138      *
139      * @return Script.FieldID The FieldID object.
140      */
getFieldID_Input()141     public Script.FieldID getFieldID_Input() {
142         return createFieldID(1, null);
143     }
144 
145 }
146