blob: 2a04b42a1b37117239c590c76e90fd05b6dcfe8f [file] [log] [blame]
Jason Sams3a5b8012012-09-08 22:16:14 -07001/*
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/**
Jason Sams80d81902012-09-13 17:00:48 -070024 * Intrinsic Gausian blur filter. Applies a gaussian blur of the
25 * specified radius to all elements of an allocation.
26 *
27 *
Jason Sams3a5b8012012-09-08 22:16:14 -070028 **/
Jason Sams80d81902012-09-13 17:00:48 -070029public final class ScriptIntrinsicBlur extends ScriptIntrinsic {
30 private final float[] mValues = new float[9];
Jason Sams3a5b8012012-09-08 22:16:14 -070031 private Allocation mInput;
32
Jason Sams80d81902012-09-13 17:00:48 -070033 private ScriptIntrinsicBlur(int id, RenderScript rs) {
Jason Sams3a5b8012012-09-08 22:16:14 -070034 super(id, rs);
35 }
36
37 /**
Jason Sams80d81902012-09-13 17:00:48 -070038 * Create an intrinsic for applying a blur to an allocation. The
39 * default radius is 5.0.
Jason Sams3a5b8012012-09-08 22:16:14 -070040 *
Jason Sams80d81902012-09-13 17:00:48 -070041 * Supported elements types are {@link Element#U8_4}
Jason Sams3a5b8012012-09-08 22:16:14 -070042 *
Jason Sams80d81902012-09-13 17:00:48 -070043 * @param rs The Renderscript context
44 * @param e Element type for inputs and outputs
Jason Sams3a5b8012012-09-08 22:16:14 -070045 *
Jason Sams80d81902012-09-13 17:00:48 -070046 * @return ScriptIntrinsicBlur
Jason Sams3a5b8012012-09-08 22:16:14 -070047 */
48 public static ScriptIntrinsicBlur create(RenderScript rs, Element e) {
Jason Sams80d81902012-09-13 17:00:48 -070049 if (e != Element.U8_4(rs)) {
50 throw new RSIllegalArgumentException("Unsuported element type.");
51 }
Jason Sams3a5b8012012-09-08 22:16:14 -070052 int id = rs.nScriptIntrinsicCreate(5, e.getID(rs));
Jason Sams80d81902012-09-13 17:00:48 -070053 ScriptIntrinsicBlur sib = new ScriptIntrinsicBlur(id, rs);
54 sib.setRadius(5.f);
55 return sib;
Jason Sams3a5b8012012-09-08 22:16:14 -070056 }
57
Jason Sams80d81902012-09-13 17:00:48 -070058 /**
59 * Set the input of the blur.
60 * Must match the element type supplied during create.
61 *
62 * @param ain The input allocation
63 */
Jason Sams3a5b8012012-09-08 22:16:14 -070064 public void setInput(Allocation ain) {
65 mInput = ain;
66 bindAllocation(ain, 1);
67 }
68
Jason Sams80d81902012-09-13 17:00:48 -070069 /**
70 * Set the radius of the Blur.
71 *
Jason Sams31864d72012-10-02 15:21:11 -070072 * Supported range 0 < radius <= 25
Jason Sams80d81902012-09-13 17:00:48 -070073 *
74 * @param radius The radius of the blur
75 */
76 public void setRadius(float radius) {
Jason Sams31864d72012-10-02 15:21:11 -070077 if (radius <= 0 || radius > 25) {
78 throw new RSIllegalArgumentException("Radius out of range (0 < r <= 25).");
Jason Sams3a5b8012012-09-08 22:16:14 -070079 }
Jason Sams80d81902012-09-13 17:00:48 -070080 setVar(0, radius);
Jason Sams3a5b8012012-09-08 22:16:14 -070081 }
82
Jason Sams80d81902012-09-13 17:00:48 -070083 /**
84 * Apply the filter to the input and save to the specified
85 * allocation.
86 *
87 * @param aout Output allocation. Must match creation element
88 * type.
89 */
Jason Sams3a5b8012012-09-08 22:16:14 -070090 public void forEach(Allocation aout) {
91 forEach(0, null, aout, null);
92 }
93
Jason Sams08a81582012-09-18 12:32:10 -070094 /**
95 * Get a KernelID for this intrinsic kernel.
96 *
97 * @return Script.KernelID The KernelID object.
98 */
99 public Script.KernelID getKernelID() {
100 return createKernelID(0, 2, null, null);
101 }
102
103 /**
104 * Get a FieldID for the input field of this intrinsic.
105 *
106 * @return Script.FieldID The FieldID object.
107 */
108 public Script.FieldID getFieldID_Input() {
109 return createFieldID(1, null);
110 }
Jason Sams3a5b8012012-09-08 22:16:14 -0700111}
112