blob: 5d3c1d324a158eb3f8d0180f9232b2274e728f76 [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 Samsd93fdb72013-06-27 15:42:30 -070034 * Supported elements types are {@link Element#U8}, {@link
35 * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
36 * {@link Element#F32}, {@link Element#F32_2}, {@link
37 * Element#F32_3}, and {@link Element#F32_4}
Jason Sams6ab97682012-08-10 12:09:43 -070038 *
Jason Sams80d81902012-09-13 17:00:48 -070039 * The default coefficients are.
Jason Sams6ab97682012-08-10 12:09:43 -070040 *
Jason Sams80d81902012-09-13 17:00:48 -070041 * <code>
42 * <p> [ 0, 0, 0 ]
43 * <p> [ 0, 1, 0 ]
44 * <p> [ 0, 0, 0 ]
45 * </code>
46 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070047 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070048 * @param e Element type for intputs and outputs
Jason Sams6ab97682012-08-10 12:09:43 -070049 *
50 * @return ScriptIntrinsicConvolve3x3
51 */
52 public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) {
Jason Sams80d81902012-09-13 17:00:48 -070053 float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0};
Jason Sams048c9782013-06-17 18:35:31 -070054 if (!e.isCompatible(Element.U8(rs)) &&
Jason Samsd93fdb72013-06-27 15:42:30 -070055 !e.isCompatible(Element.U8_2(rs)) &&
56 !e.isCompatible(Element.U8_3(rs)) &&
Jason Sams048c9782013-06-17 18:35:31 -070057 !e.isCompatible(Element.U8_4(rs)) &&
58 !e.isCompatible(Element.F32(rs)) &&
Jason Samsd93fdb72013-06-27 15:42:30 -070059 !e.isCompatible(Element.F32_2(rs)) &&
60 !e.isCompatible(Element.F32_3(rs)) &&
Jason Sams048c9782013-06-17 18:35:31 -070061 !e.isCompatible(Element.F32_4(rs))) {
Jason Sams80d81902012-09-13 17:00:48 -070062 throw new RSIllegalArgumentException("Unsuported element type.");
63 }
Jason Sams6ab97682012-08-10 12:09:43 -070064 int id = rs.nScriptIntrinsicCreate(1, e.getID(rs));
Jason Sams80d81902012-09-13 17:00:48 -070065 ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
66 si.setCoefficients(f);
67 return si;
Jason Sams6ab97682012-08-10 12:09:43 -070068
69 }
70
Jason Sams80d81902012-09-13 17:00:48 -070071 /**
72 * Set the input of the blur.
73 * Must match the element type supplied during create.
74 *
75 * @param ain The input allocation.
76 */
Jason Sams19e10862012-08-21 15:53:29 -070077 public void setInput(Allocation ain) {
78 mInput = ain;
Jason Samse6a78862012-10-15 15:45:12 -070079 setVar(1, ain);
Jason Sams19e10862012-08-21 15:53:29 -070080 }
Jason Sams6ab97682012-08-10 12:09:43 -070081
Jason Sams80d81902012-09-13 17:00:48 -070082 /**
83 * Set the coefficients for the convolve.
84 *
85 * The convolve layout is
86 * <code>
87 * <p> [ 0, 1, 2 ]
88 * <p> [ 3, 4, 5 ]
89 * <p> [ 6, 7, 8 ]
90 * </code>
91 *
92 * @param v The array of coefficients to set
93 */
94 public void setCoefficients(float v[]) {
Jason Sams6ab97682012-08-10 12:09:43 -070095 FieldPacker fp = new FieldPacker(9*4);
96 for (int ct=0; ct < mValues.length; ct++) {
97 mValues[ct] = v[ct];
98 fp.addF32(mValues[ct]);
99 }
100 setVar(0, fp);
101 }
Jason Sams19e10862012-08-21 15:53:29 -0700102
Jason Sams80d81902012-09-13 17:00:48 -0700103 /**
104 * Apply the filter to the input and save to the specified
105 * allocation.
106 *
107 * @param aout Output allocation. Must match creation element
108 * type.
109 */
Jason Sams19e10862012-08-21 15:53:29 -0700110 public void forEach(Allocation aout) {
111 forEach(0, null, aout, null);
112 }
113
Jason Sams08a81582012-09-18 12:32:10 -0700114 /**
115 * Get a KernelID for this intrinsic kernel.
116 *
117 * @return Script.KernelID The KernelID object.
118 */
119 public Script.KernelID getKernelID() {
120 return createKernelID(0, 2, null, null);
121 }
122
123 /**
124 * Get a FieldID for the input field of this intrinsic.
125 *
126 * @return Script.FieldID The FieldID object.
127 */
128 public Script.FieldID getFieldID_Input() {
129 return createFieldID(1, null);
130 }
131
Jason Sams6ab97682012-08-10 12:09:43 -0700132}
133