blob: 8518bb27379d04dd2c940a6ec0ecab1d83eb63f8 [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
Jason Sams3a5b8012012-09-08 22:16:14 -070019/**
Jason Sams80d81902012-09-13 17:00:48 -070020 * Intrinsic for applying a 5x5 convolve to an allocation.
21 *
Jason Sams3a5b8012012-09-08 22:16:14 -070022 **/
Jason Sams80d81902012-09-13 17:00:48 -070023public final class ScriptIntrinsicConvolve5x5 extends ScriptIntrinsic {
24 private final float[] mValues = new float[25];
Jason Sams3a5b8012012-09-08 22:16:14 -070025 private Allocation mInput;
26
Tim Murray460a0492013-11-19 12:45:54 -080027 private ScriptIntrinsicConvolve5x5(long id, RenderScript rs) {
Jason Sams3a5b8012012-09-08 22:16:14 -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
Miao Wange58ed9b2016-03-29 15:29:10 -070035 * Element#F32_3}, and {@link Element#F32_4}.
Jason Sams3a5b8012012-09-08 22:16:14 -070036 *
Miao Wange58ed9b2016-03-29 15:29:10 -070037 * <p> The default coefficients are:
Jason Sams80d81902012-09-13 17:00:48 -070038 * <code>
39 * <p> [ 0, 0, 0, 0, 0 ]
40 * <p> [ 0, 0, 0, 0, 0 ]
41 * <p> [ 0, 0, 1, 0, 0 ]
42 * <p> [ 0, 0, 0, 0, 0 ]
43 * <p> [ 0, 0, 0, 0, 0 ]
44 * </code>
Jason Sams3a5b8012012-09-08 22:16:14 -070045 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070046 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070047 * @param e Element type for intputs and outputs
Jason Sams3a5b8012012-09-08 22:16:14 -070048 *
49 * @return ScriptIntrinsicConvolve5x5
50 */
51 public static ScriptIntrinsicConvolve5x5 create(RenderScript rs, Element e) {
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))) {
Stephen Hinesad57e332016-04-11 13:05:55 -070060 throw new RSIllegalArgumentException("Unsupported element type.");
Jason Sams048c9782013-06-17 18:35:31 -070061 }
62
Tim Murray460a0492013-11-19 12:45:54 -080063 long id = rs.nScriptIntrinsicCreate(4, e.getID(rs));
Jason Sams3a5b8012012-09-08 22:16:14 -070064 return new ScriptIntrinsicConvolve5x5(id, rs);
65
66 }
67
Jason Sams80d81902012-09-13 17:00:48 -070068 /**
Miao Wange58ed9b2016-03-29 15:29:10 -070069 * Set the input of the 5x5 convolve.
Jason Sams80d81902012-09-13 17:00:48 -070070 * Must match the element type supplied during create.
71 *
72 * @param ain The input allocation.
73 */
Jason Sams3a5b8012012-09-08 22:16:14 -070074 public void setInput(Allocation ain) {
75 mInput = ain;
Jason Samse6a78862012-10-15 15:45:12 -070076 setVar(1, ain);
Jason Sams3a5b8012012-09-08 22:16:14 -070077 }
78
Jason Sams80d81902012-09-13 17:00:48 -070079 /**
80 * Set the coefficients for the convolve.
81 *
Miao Wange58ed9b2016-03-29 15:29:10 -070082 * <p> The convolve layout is:
Jason Sams80d81902012-09-13 17:00:48 -070083 * <code>
84 * <p> [ 0, 1, 2, 3, 4 ]
85 * <p> [ 5, 6, 7, 8, 9 ]
86 * <p> [ 10, 11, 12, 13, 14 ]
87 * <p> [ 15, 16, 17, 18, 19 ]
88 * <p> [ 20, 21, 22, 23, 24 ]
89 * </code>
90 *
91 * @param v The array of coefficients to set
92 */
Jason Sams3a5b8012012-09-08 22:16:14 -070093 public void setCoefficients(float v[]) {
94 FieldPacker fp = new FieldPacker(25*4);
95 for (int ct=0; ct < mValues.length; ct++) {
96 mValues[ct] = v[ct];
97 fp.addF32(mValues[ct]);
98 }
99 setVar(0, fp);
100 }
101
Jason Sams80d81902012-09-13 17:00:48 -0700102 /**
103 * Apply the filter to the input and save to the specified
104 * allocation.
105 *
106 * @param aout Output allocation. Must match creation element
107 * type.
108 */
Jason Sams3a5b8012012-09-08 22:16:14 -0700109 public void forEach(Allocation aout) {
Chris Wailes94961062014-06-11 12:01:28 -0700110 forEach(0, (Allocation) null, aout, null);
Jason Sams3a5b8012012-09-08 22:16:14 -0700111 }
112
Jason Sams08a81582012-09-18 12:32:10 -0700113 /**
Tim Murray6f842ac2014-01-13 11:47:53 -0800114 * Apply the filter to the input and save to the specified
115 * allocation.
116 *
117 * @param aout Output allocation. Must match creation element
118 * type.
119 * @param opt LaunchOptions for clipping
120 */
121 public void forEach(Allocation aout, Script.LaunchOptions opt) {
Stephen Hines48ba5062014-07-09 07:39:38 -0700122 forEach(0, (Allocation) null, aout, null, opt);
Tim Murray6f842ac2014-01-13 11:47:53 -0800123 }
124
125
126 /**
Jason Sams08a81582012-09-18 12:32:10 -0700127 * Get a KernelID for this intrinsic kernel.
128 *
129 * @return Script.KernelID The KernelID object.
130 */
131 public Script.KernelID getKernelID() {
132 return createKernelID(0, 2, null, null);
133 }
134
135 /**
136 * Get a FieldID for the input field of this intrinsic.
137 *
138 * @return Script.FieldID The FieldID object.
139 */
140 public Script.FieldID getFieldID_Input() {
141 return createFieldID(1, null);
142 }
Jason Sams3a5b8012012-09-08 22:16:14 -0700143}