blob: c9c54b2ea381487d363687ee2ec9816f115dc97d [file] [log] [blame]
Jason Sams6ab97682012-08-10 12:09:43 -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
Jason Sams6ab97682012-08-10 12:09:43 -070019import android.util.Log;
20
Jason Sams6ab97682012-08-10 12:09:43 -070021/**
Jason Sams80d81902012-09-13 17:00:48 -070022 * Intrinsic for applying a 3x3 convolve to an allocation.
23 *
Jason Sams6ab97682012-08-10 12:09:43 -070024 **/
Jason Sams80d81902012-09-13 17:00:48 -070025public final class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic {
26 private final float[] mValues = new float[9];
Jason Sams19e10862012-08-21 15:53:29 -070027 private Allocation mInput;
Jason Sams6ab97682012-08-10 12:09:43 -070028
Jason Sams80d81902012-09-13 17:00:48 -070029 private ScriptIntrinsicConvolve3x3(int id, RenderScript rs) {
Jason Sams6ab97682012-08-10 12:09:43 -070030 super(id, rs);
31 }
32
33 /**
Jason Sams80d81902012-09-13 17:00:48 -070034 * Supported elements types are {@link Element#U8_4}
Jason Sams6ab97682012-08-10 12:09:43 -070035 *
Jason Sams80d81902012-09-13 17:00:48 -070036 * The default coefficients are.
Jason Sams6ab97682012-08-10 12:09:43 -070037 *
Jason Sams80d81902012-09-13 17:00:48 -070038 * <code>
39 * <p> [ 0, 0, 0 ]
40 * <p> [ 0, 1, 0 ]
41 * <p> [ 0, 0, 0 ]
42 * </code>
43 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070044 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070045 * @param e Element type for intputs and outputs
Jason Sams6ab97682012-08-10 12:09:43 -070046 *
47 * @return ScriptIntrinsicConvolve3x3
48 */
49 public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) {
Jason Sams80d81902012-09-13 17:00:48 -070050 float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0};
Tim Murraybddc7ff2013-04-01 11:35:35 -070051 if (!e.isCompatible(Element.U8_4(rs))) {
Jason Sams80d81902012-09-13 17:00:48 -070052 throw new RSIllegalArgumentException("Unsuported element type.");
53 }
Jason Sams6ab97682012-08-10 12:09:43 -070054 int id = rs.nScriptIntrinsicCreate(1, e.getID(rs));
Jason Sams80d81902012-09-13 17:00:48 -070055 ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
56 si.setCoefficients(f);
57 return si;
Jason Sams6ab97682012-08-10 12:09:43 -070058
59 }
60
Jason Sams80d81902012-09-13 17:00:48 -070061 /**
62 * Set the input of the blur.
63 * Must match the element type supplied during create.
64 *
65 * @param ain The input allocation.
66 */
Jason Sams19e10862012-08-21 15:53:29 -070067 public void setInput(Allocation ain) {
68 mInput = ain;
Jason Samse6a78862012-10-15 15:45:12 -070069 setVar(1, ain);
Jason Sams19e10862012-08-21 15:53:29 -070070 }
Jason Sams6ab97682012-08-10 12:09:43 -070071
Jason Sams80d81902012-09-13 17:00:48 -070072 /**
73 * Set the coefficients for the convolve.
74 *
75 * The convolve layout is
76 * <code>
77 * <p> [ 0, 1, 2 ]
78 * <p> [ 3, 4, 5 ]
79 * <p> [ 6, 7, 8 ]
80 * </code>
81 *
82 * @param v The array of coefficients to set
83 */
84 public void setCoefficients(float v[]) {
Jason Sams6ab97682012-08-10 12:09:43 -070085 FieldPacker fp = new FieldPacker(9*4);
86 for (int ct=0; ct < mValues.length; ct++) {
87 mValues[ct] = v[ct];
88 fp.addF32(mValues[ct]);
89 }
90 setVar(0, fp);
91 }
Jason Sams19e10862012-08-21 15:53:29 -070092
Jason Sams80d81902012-09-13 17:00:48 -070093 /**
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 */
Jason Sams19e10862012-08-21 15:53:29 -0700100 public void forEach(Allocation aout) {
101 forEach(0, null, aout, null);
102 }
103
Jason Sams08a81582012-09-18 12:32:10 -0700104 /**
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
Jason Sams6ab97682012-08-10 12:09:43 -0700122}
123