ScriptIntrinsicBlur.java revision 6f5555db1af436bb5aad430e6e00aa5b69d5ca6c
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.content.Context;
20import android.content.res.Resources;
21import android.util.Log;
22
23/**
24 * Intrinsic Gausian blur filter. Applies a gaussian blur of the
25 * specified radius to all elements of an allocation.
26 *
27 *
28 **/
29public class ScriptIntrinsicBlur extends ScriptIntrinsic {
30    private final float[] mValues = new float[9];
31    private Allocation mInput;
32    // API level for the intrinsic
33    private static final int INTRINSIC_API_LEVEL = 19;
34
35    protected ScriptIntrinsicBlur(long id, RenderScript rs) {
36        super(id, rs);
37    }
38
39    /**
40     * Create an intrinsic for applying a blur to an allocation. The
41     * default radius is 5.0.
42     *
43     * Supported elements types are {@link Element#U8_4}
44     *
45     * @param rs The RenderScript context
46     * @param e Element type for inputs and outputs
47     *
48     * @return ScriptIntrinsicBlur
49     */
50    public static ScriptIntrinsicBlur create(RenderScript rs, Element e) {
51        if ((!e.isCompatible(Element.U8_4(rs))) && (!e.isCompatible(Element.U8(rs)))) {
52            throw new RSIllegalArgumentException("Unsuported element type.");
53        }
54        long id;
55        boolean mUseIncSupp = rs.isUseNative() &&
56                              android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
57
58        id = rs.nScriptIntrinsicCreate(5, e.getID(rs), mUseIncSupp);
59
60        ScriptIntrinsicBlur si = new ScriptIntrinsicBlur(id, rs);
61        si.setIncSupp(mUseIncSupp);
62        si.setRadius(5.f);
63
64        return si;
65    }
66
67    /**
68     * Set the input of the blur.
69     * Must match the element type supplied during create.
70     *
71     * @param ain The input allocation
72     */
73    public void setInput(Allocation ain) {
74        mInput = ain;
75        setVar(1, ain);
76    }
77
78    /**
79     * Set the radius of the Blur.
80     *
81     * Supported range 0 < radius <= 25
82     *
83     * @param radius The radius of the blur
84     */
85    public void setRadius(float radius) {
86        if (radius <= 0 || radius > 25) {
87            throw new RSIllegalArgumentException("Radius out of range (0 < r <= 25).");
88        }
89        setVar(0, radius);
90    }
91
92    /**
93     * Apply the filter to the input and save to the specified
94     * allocation.
95     *
96     * @param aout Output allocation. Must match creation element
97     *             type.
98     */
99    public void forEach(Allocation aout) {
100        forEach(0, null, aout, null);
101    }
102
103    /**
104     * Get a KernelID for this intrinsic kernel.
105     *
106     * @return Script.KernelID The KernelID object.
107     */
108    public Script.KernelID getKernelID() {
109        return createKernelID(0, 2, null, null);
110    }
111
112    /**
113     * Get a FieldID for the input field of this intrinsic.
114     *
115     * @return Script.FieldID The FieldID object.
116     */
117    public Script.FieldID getFieldID_Input() {
118        return createFieldID(1, null);
119    }
120}
121
122