blob: 4b9dff18f62ef5b23d596f4f98fca48861655575 [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 *
Xusong Wang8b4548c2021-01-05 10:09:52 -080022 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
23 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
24 * guide</a> for the proposed alternatives.
Jason Sams6ab97682012-08-10 12:09:43 -070025 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080026@Deprecated
Jason Sams80d81902012-09-13 17:00:48 -070027public final class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic {
28 private final float[] mValues = new float[9];
Jason Sams19e10862012-08-21 15:53:29 -070029 private Allocation mInput;
Jason Sams6ab97682012-08-10 12:09:43 -070030
Tim Murray460a0492013-11-19 12:45:54 -080031 private ScriptIntrinsicConvolve3x3(long id, RenderScript rs) {
Jason Sams6ab97682012-08-10 12:09:43 -070032 super(id, rs);
33 }
34
35 /**
Jason Samsd93fdb72013-06-27 15:42:30 -070036 * Supported elements types are {@link Element#U8}, {@link
37 * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
38 * {@link Element#F32}, {@link Element#F32_2}, {@link
Miao Wange58ed9b2016-03-29 15:29:10 -070039 * Element#F32_3}, and {@link Element#F32_4}.
Jason Sams6ab97682012-08-10 12:09:43 -070040 *
Miao Wange58ed9b2016-03-29 15:29:10 -070041 * <p> The default coefficients are:
Jason Sams80d81902012-09-13 17:00:48 -070042 * <code>
43 * <p> [ 0, 0, 0 ]
44 * <p> [ 0, 1, 0 ]
45 * <p> [ 0, 0, 0 ]
46 * </code>
47 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070048 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070049 * @param e Element type for intputs and outputs
Jason Sams6ab97682012-08-10 12:09:43 -070050 *
51 * @return ScriptIntrinsicConvolve3x3
52 */
53 public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) {
Jason Sams80d81902012-09-13 17:00:48 -070054 float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0};
Jason Sams048c9782013-06-17 18:35:31 -070055 if (!e.isCompatible(Element.U8(rs)) &&
Jason Samsd93fdb72013-06-27 15:42:30 -070056 !e.isCompatible(Element.U8_2(rs)) &&
57 !e.isCompatible(Element.U8_3(rs)) &&
Jason Sams048c9782013-06-17 18:35:31 -070058 !e.isCompatible(Element.U8_4(rs)) &&
59 !e.isCompatible(Element.F32(rs)) &&
Jason Samsd93fdb72013-06-27 15:42:30 -070060 !e.isCompatible(Element.F32_2(rs)) &&
61 !e.isCompatible(Element.F32_3(rs)) &&
Jason Sams048c9782013-06-17 18:35:31 -070062 !e.isCompatible(Element.F32_4(rs))) {
Stephen Hinesad57e332016-04-11 13:05:55 -070063 throw new RSIllegalArgumentException("Unsupported element type.");
Jason Sams80d81902012-09-13 17:00:48 -070064 }
Tim Murray460a0492013-11-19 12:45:54 -080065 long id = rs.nScriptIntrinsicCreate(1, e.getID(rs));
Jason Sams80d81902012-09-13 17:00:48 -070066 ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
67 si.setCoefficients(f);
68 return si;
Jason Sams6ab97682012-08-10 12:09:43 -070069
70 }
71
Jason Sams80d81902012-09-13 17:00:48 -070072 /**
Miao Wange58ed9b2016-03-29 15:29:10 -070073 * Set the input of the 3x3 convolve.
Jason Sams80d81902012-09-13 17:00:48 -070074 * Must match the element type supplied during create.
75 *
76 * @param ain The input allocation.
77 */
Jason Sams19e10862012-08-21 15:53:29 -070078 public void setInput(Allocation ain) {
79 mInput = ain;
Jason Samse6a78862012-10-15 15:45:12 -070080 setVar(1, ain);
Jason Sams19e10862012-08-21 15:53:29 -070081 }
Jason Sams6ab97682012-08-10 12:09:43 -070082
Jason Sams80d81902012-09-13 17:00:48 -070083 /**
84 * Set the coefficients for the convolve.
85 *
Miao Wange58ed9b2016-03-29 15:29:10 -070086 * <p> The convolve layout is:
Jason Sams80d81902012-09-13 17:00:48 -070087 * <code>
88 * <p> [ 0, 1, 2 ]
89 * <p> [ 3, 4, 5 ]
90 * <p> [ 6, 7, 8 ]
91 * </code>
92 *
93 * @param v The array of coefficients to set
94 */
95 public void setCoefficients(float v[]) {
Jason Sams6ab97682012-08-10 12:09:43 -070096 FieldPacker fp = new FieldPacker(9*4);
97 for (int ct=0; ct < mValues.length; ct++) {
98 mValues[ct] = v[ct];
99 fp.addF32(mValues[ct]);
100 }
101 setVar(0, fp);
102 }
Jason Sams19e10862012-08-21 15:53:29 -0700103
Jason Sams80d81902012-09-13 17:00:48 -0700104 /**
105 * Apply the filter to the input and save to the specified
106 * allocation.
107 *
108 * @param aout Output allocation. Must match creation element
109 * type.
110 */
Jason Sams19e10862012-08-21 15:53:29 -0700111 public void forEach(Allocation aout) {
Chris Wailes94961062014-06-11 12:01:28 -0700112 forEach(0, (Allocation) null, aout, null);
Tim Murray6f842ac2014-01-13 11:47:53 -0800113 }
114
115 /**
116 * Apply the filter to the input and save to the specified
117 * allocation.
118 *
119 * @param aout Output allocation. Must match creation element
120 * type.
121 * @param opt LaunchOptions for clipping
122 */
123 public void forEach(Allocation aout, Script.LaunchOptions opt) {
Stephen Hines48ba5062014-07-09 07:39:38 -0700124 forEach(0, (Allocation) null, aout, null, opt);
Jason Sams19e10862012-08-21 15:53:29 -0700125 }
126
Jason Sams08a81582012-09-18 12:32:10 -0700127 /**
128 * Get a KernelID for this intrinsic kernel.
129 *
130 * @return Script.KernelID The KernelID object.
131 */
132 public Script.KernelID getKernelID() {
133 return createKernelID(0, 2, null, null);
134 }
135
136 /**
137 * Get a FieldID for the input field of this intrinsic.
138 *
139 * @return Script.FieldID The FieldID object.
140 */
141 public Script.FieldID getFieldID_Input() {
142 return createFieldID(1, null);
143 }
144
Jason Sams6ab97682012-08-10 12:09:43 -0700145}