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
17package android.support.v8.renderscript;
18
19import android.util.Log;
20
21/**
22 * Intrinsic for applying a 3x3 convolve to an allocation.
23 *
24 **/
25public class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic {
26    private final float[] mValues = new float[9];
27    private Allocation mInput;
28    // API level for the intrinsic
29    private static final int INTRINSIC_API_LEVEL = 19;
30
31    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     */
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;
66        boolean mUseIncSupp = rs.isUseNative() &&
67                              android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
68
69        id = rs.nScriptIntrinsicCreate(1, e.getID(rs), mUseIncSupp);
70
71        ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
72        si.setIncSupp(mUseIncSupp);
73        si.setCoefficients(f);
74        return si;
75    }
76
77    /**
78     * Set the input of the 3x3 convolve.
79     * Must match the element type supplied during create.
80     *
81     * @param ain The input allocation.
82     */
83    public void setInput(Allocation ain) {
84        mInput = ain;
85        setVar(1, ain);
86    }
87
88    /**
89     * Set the coefficients for the convolve.
90     *
91     * <p> The convolve layout is:
92     * <code>
93     * <p> [ 0,  1,  2 ]
94     * <p> [ 3,  4,  5 ]
95     * <p> [ 6,  7,  8 ]
96     * </code>
97     *
98     * @param v The array of coefficients to set
99     */
100    public void setCoefficients(float v[]) {
101        FieldPacker fp = new FieldPacker(9*4);
102        for (int ct=0; ct < mValues.length; ct++) {
103            mValues[ct] = v[ct];
104            fp.addF32(mValues[ct]);
105        }
106        setVar(0, fp);
107    }
108
109    /**
110     * Apply the filter to the input and save to the specified
111     * allocation.
112     *
113     * @param aout Output allocation. Must match creation element
114     *             type.
115     */
116    public void forEach(Allocation aout) {
117        forEach(0, (Allocation) null, aout, null);
118    }
119
120    /**
121     * Apply the filter to the input and save to the specified
122     * allocation.
123     *
124     * @param aout Output allocation. Must match creation element
125     *             type.
126     * @param opt LaunchOptions for clipping
127     */
128    public void forEach(Allocation aout, Script.LaunchOptions opt) {
129        forEach(0, (Allocation) null, aout, null, opt);
130    }
131
132    /**
133     * Get a KernelID for this intrinsic kernel.
134     *
135     * @return Script.KernelID The KernelID object.
136     */
137    public Script.KernelID getKernelID() {
138        return createKernelID(0, 2, null, null);
139    }
140
141    /**
142     * Get a FieldID for the input field of this intrinsic.
143     *
144     * @return Script.FieldID The FieldID object.
145     */
146    public Script.FieldID getFieldID_Input() {
147        return createFieldID(1, null);
148    }
149
150}
151
152