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