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},
44     * {@link Element#U8_4}.
45     *
46     * @param rs The RenderScript context
47     * @param e Element type for inputs and outputs
48     *
49     * @return ScriptIntrinsicBlur
50     */
51    public static ScriptIntrinsicBlur create(RenderScript rs, Element e) {
52        if ((!e.isCompatible(Element.U8_4(rs))) && (!e.isCompatible(Element.U8(rs)))) {
53            throw new RSIllegalArgumentException("Unsuported element type.");
54        }
55        long id;
56        boolean mUseIncSupp = rs.isUseNative() &&
57                              android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
58
59        id = rs.nScriptIntrinsicCreate(5, e.getID(rs), mUseIncSupp);
60
61        ScriptIntrinsicBlur si = new ScriptIntrinsicBlur(id, rs);
62        si.setIncSupp(mUseIncSupp);
63        si.setRadius(5.f);
64
65        return si;
66    }
67
68    /**
69     * Set the input of the blur.
70     * Must match the element type supplied during create.
71     *
72     * @param ain The input allocation
73     */
74    public void setInput(Allocation ain) {
75        mInput = ain;
76        setVar(1, ain);
77    }
78
79    /**
80     * Set the radius of the Blur.
81     *
82     * Supported range 0 < radius <= 25
83     *
84     * @param radius The radius of the blur
85     */
86    public void setRadius(float radius) {
87        if (radius <= 0 || radius > 25) {
88            throw new RSIllegalArgumentException("Radius out of range (0 < r <= 25).");
89        }
90        setVar(0, radius);
91    }
92
93    /**
94     * Apply the filter to the input and save to the specified
95     * allocation.
96     *
97     * @param aout Output allocation. Must match creation element
98     *             type.
99     */
100    public void forEach(Allocation aout) {
101        forEach(0, (Allocation) null, aout, null);
102    }
103
104    /**
105     * Get a KernelID for this intrinsic kernel.
106     *
107     * @return Script.KernelID The KernelID object.
108     */
109    public Script.KernelID getKernelID() {
110        return createKernelID(0, 2, null, null);
111    }
112
113    /**
114     * Get a FieldID for the input field of this intrinsic.
115     *
116     * @return Script.FieldID The FieldID object.
117     */
118    public Script.FieldID getFieldID_Input() {
119        return createFieldID(1, null);
120    }
121}
122
123