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