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