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