blob: da4d5a28c710cabfe858911741bfbc1f191df2c3 [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
19import android.util.Log;
20
21/**
Jason Sams80d81902012-09-13 17:00:48 -070022 * Intrinsic for applying a 5x5 convolve to an allocation.
23 *
Jason Sams3a5b8012012-09-08 22:16:14 -070024 **/
Jason Sams80d81902012-09-13 17:00:48 -070025public final class ScriptIntrinsicConvolve5x5 extends ScriptIntrinsic {
26 private final float[] mValues = new float[25];
Jason Sams3a5b8012012-09-08 22:16:14 -070027 private Allocation mInput;
28
Jason Sams80d81902012-09-13 17:00:48 -070029 private ScriptIntrinsicConvolve5x5(int id, RenderScript rs) {
Jason Sams3a5b8012012-09-08 22:16:14 -070030 super(id, rs);
31 }
32
33 /**
Jason Sams80d81902012-09-13 17:00:48 -070034 * Supported elements types are {@link Element#U8_4}
Jason Sams3a5b8012012-09-08 22:16:14 -070035 *
Jason Sams80d81902012-09-13 17:00:48 -070036 * The default coefficients are.
37 * <code>
38 * <p> [ 0, 0, 0, 0, 0 ]
39 * <p> [ 0, 0, 0, 0, 0 ]
40 * <p> [ 0, 0, 1, 0, 0 ]
41 * <p> [ 0, 0, 0, 0, 0 ]
42 * <p> [ 0, 0, 0, 0, 0 ]
43 * </code>
Jason Sams3a5b8012012-09-08 22:16:14 -070044 *
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 Sams3a5b8012012-09-08 22:16:14 -070047 *
48 * @return ScriptIntrinsicConvolve5x5
49 */
50 public static ScriptIntrinsicConvolve5x5 create(RenderScript rs, Element e) {
Jason Sams048c9782013-06-17 18:35:31 -070051 if (!e.isCompatible(Element.U8(rs)) &&
52 !e.isCompatible(Element.U8_4(rs)) &&
53 !e.isCompatible(Element.F32(rs)) &&
54 !e.isCompatible(Element.F32_4(rs))) {
55 throw new RSIllegalArgumentException("Unsuported element type.");
56 }
57
Jason Sams3a5b8012012-09-08 22:16:14 -070058 int id = rs.nScriptIntrinsicCreate(4, e.getID(rs));
59 return new ScriptIntrinsicConvolve5x5(id, rs);
60
61 }
62
Jason Sams80d81902012-09-13 17:00:48 -070063 /**
64 * Set the input of the blur.
65 * Must match the element type supplied during create.
66 *
67 * @param ain The input allocation.
68 */
Jason Sams3a5b8012012-09-08 22:16:14 -070069 public void setInput(Allocation ain) {
70 mInput = ain;
Jason Samse6a78862012-10-15 15:45:12 -070071 setVar(1, ain);
Jason Sams3a5b8012012-09-08 22:16:14 -070072 }
73
Jason Sams80d81902012-09-13 17:00:48 -070074 /**
75 * Set the coefficients for the convolve.
76 *
77 * The convolve layout is
78 * <code>
79 * <p> [ 0, 1, 2, 3, 4 ]
80 * <p> [ 5, 6, 7, 8, 9 ]
81 * <p> [ 10, 11, 12, 13, 14 ]
82 * <p> [ 15, 16, 17, 18, 19 ]
83 * <p> [ 20, 21, 22, 23, 24 ]
84 * </code>
85 *
86 * @param v The array of coefficients to set
87 */
Jason Sams3a5b8012012-09-08 22:16:14 -070088 public void setCoefficients(float v[]) {
89 FieldPacker fp = new FieldPacker(25*4);
90 for (int ct=0; ct < mValues.length; ct++) {
91 mValues[ct] = v[ct];
92 fp.addF32(mValues[ct]);
93 }
94 setVar(0, fp);
95 }
96
Jason Sams80d81902012-09-13 17:00:48 -070097 /**
98 * Apply the filter to the input and save to the specified
99 * allocation.
100 *
101 * @param aout Output allocation. Must match creation element
102 * type.
103 */
Jason Sams3a5b8012012-09-08 22:16:14 -0700104 public void forEach(Allocation aout) {
105 forEach(0, null, aout, null);
106 }
107
Jason Sams08a81582012-09-18 12:32:10 -0700108 /**
109 * Get a KernelID for this intrinsic kernel.
110 *
111 * @return Script.KernelID The KernelID object.
112 */
113 public Script.KernelID getKernelID() {
114 return createKernelID(0, 2, null, null);
115 }
116
117 /**
118 * Get a FieldID for the input field of this intrinsic.
119 *
120 * @return Script.FieldID The FieldID object.
121 */
122 public Script.FieldID getFieldID_Input() {
123 return createFieldID(1, null);
124 }
Jason Sams3a5b8012012-09-08 22:16:14 -0700125}
126