blob: a1a1b7eab22f1956e305e1091020adeabbe06218 [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 -070019/**
Jason Sams80d81902012-09-13 17:00:48 -070020 * Intrinsic for applying a 3x3 convolve to an allocation.
21 *
Jason Sams6ab97682012-08-10 12:09:43 -070022 **/
Jason Sams80d81902012-09-13 17:00:48 -070023public final class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic {
24 private final float[] mValues = new float[9];
Jason Sams19e10862012-08-21 15:53:29 -070025 private Allocation mInput;
Jason Sams6ab97682012-08-10 12:09:43 -070026
Tim Murray460a0492013-11-19 12:45:54 -080027 private ScriptIntrinsicConvolve3x3(long id, RenderScript rs) {
Jason Sams6ab97682012-08-10 12:09:43 -070028 super(id, rs);
29 }
30
31 /**
Jason Samsd93fdb72013-06-27 15:42:30 -070032 * Supported elements types are {@link Element#U8}, {@link
33 * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
34 * {@link Element#F32}, {@link Element#F32_2}, {@link
35 * Element#F32_3}, and {@link Element#F32_4}
Jason Sams6ab97682012-08-10 12:09:43 -070036 *
Jason Sams80d81902012-09-13 17:00:48 -070037 * The default coefficients are.
Jason Sams6ab97682012-08-10 12:09:43 -070038 *
Jason Sams80d81902012-09-13 17:00:48 -070039 * <code>
40 * <p> [ 0, 0, 0 ]
41 * <p> [ 0, 1, 0 ]
42 * <p> [ 0, 0, 0 ]
43 * </code>
44 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070045 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070046 * @param e Element type for intputs and outputs
Jason Sams6ab97682012-08-10 12:09:43 -070047 *
48 * @return ScriptIntrinsicConvolve3x3
49 */
50 public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) {
Jason Sams80d81902012-09-13 17:00:48 -070051 float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0};
Jason Sams048c9782013-06-17 18:35:31 -070052 if (!e.isCompatible(Element.U8(rs)) &&
Jason Samsd93fdb72013-06-27 15:42:30 -070053 !e.isCompatible(Element.U8_2(rs)) &&
54 !e.isCompatible(Element.U8_3(rs)) &&
Jason Sams048c9782013-06-17 18:35:31 -070055 !e.isCompatible(Element.U8_4(rs)) &&
56 !e.isCompatible(Element.F32(rs)) &&
Jason Samsd93fdb72013-06-27 15:42:30 -070057 !e.isCompatible(Element.F32_2(rs)) &&
58 !e.isCompatible(Element.F32_3(rs)) &&
Jason Sams048c9782013-06-17 18:35:31 -070059 !e.isCompatible(Element.F32_4(rs))) {
Jason Sams80d81902012-09-13 17:00:48 -070060 throw new RSIllegalArgumentException("Unsuported element type.");
61 }
Tim Murray460a0492013-11-19 12:45:54 -080062 long id = rs.nScriptIntrinsicCreate(1, e.getID(rs));
Jason Sams80d81902012-09-13 17:00:48 -070063 ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
64 si.setCoefficients(f);
65 return si;
Jason Sams6ab97682012-08-10 12:09:43 -070066
67 }
68
Jason Sams80d81902012-09-13 17:00:48 -070069 /**
70 * Set the input of the blur.
71 * Must match the element type supplied during create.
72 *
73 * @param ain The input allocation.
74 */
Jason Sams19e10862012-08-21 15:53:29 -070075 public void setInput(Allocation ain) {
76 mInput = ain;
Jason Samse6a78862012-10-15 15:45:12 -070077 setVar(1, ain);
Jason Sams19e10862012-08-21 15:53:29 -070078 }
Jason Sams6ab97682012-08-10 12:09:43 -070079
Jason Sams80d81902012-09-13 17:00:48 -070080 /**
81 * Set the coefficients for the convolve.
82 *
83 * The convolve layout is
84 * <code>
85 * <p> [ 0, 1, 2 ]
86 * <p> [ 3, 4, 5 ]
87 * <p> [ 6, 7, 8 ]
88 * </code>
89 *
90 * @param v The array of coefficients to set
91 */
92 public void setCoefficients(float v[]) {
Jason Sams6ab97682012-08-10 12:09:43 -070093 FieldPacker fp = new FieldPacker(9*4);
94 for (int ct=0; ct < mValues.length; ct++) {
95 mValues[ct] = v[ct];
96 fp.addF32(mValues[ct]);
97 }
98 setVar(0, fp);
99 }
Jason Sams19e10862012-08-21 15:53:29 -0700100
Jason Sams80d81902012-09-13 17:00:48 -0700101 /**
102 * Apply the filter to the input and save to the specified
103 * allocation.
104 *
105 * @param aout Output allocation. Must match creation element
106 * type.
107 */
Jason Sams19e10862012-08-21 15:53:29 -0700108 public void forEach(Allocation aout) {
109 forEach(0, null, aout, null);
110 }
111
Jason Sams08a81582012-09-18 12:32:10 -0700112 /**
113 * Get a KernelID for this intrinsic kernel.
114 *
115 * @return Script.KernelID The KernelID object.
116 */
117 public Script.KernelID getKernelID() {
118 return createKernelID(0, 2, null, null);
119 }
120
121 /**
122 * Get a FieldID for the input field of this intrinsic.
123 *
124 * @return Script.FieldID The FieldID object.
125 */
126 public Script.FieldID getFieldID_Input() {
127 return createFieldID(1, null);
128 }
129
Jason Sams6ab97682012-08-10 12:09:43 -0700130}
131