blob: d7e9f32ca2f59fe3feca0443b3371ae467990d3e [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
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.Log;
22
Jason Sams6ab97682012-08-10 12:09:43 -070023/**
Jason Sams80d81902012-09-13 17:00:48 -070024 * Intrinsic for applying a 3x3 convolve to an allocation.
25 *
Jason Sams6ab97682012-08-10 12:09:43 -070026 **/
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
Jason Sams80d81902012-09-13 17:00:48 -070031 private ScriptIntrinsicConvolve3x3(int id, RenderScript rs) {
Jason Sams6ab97682012-08-10 12:09:43 -070032 super(id, rs);
33 }
34
35 /**
Jason Sams80d81902012-09-13 17:00:48 -070036 * Supported elements types are {@link Element#U8_4}
Jason Sams6ab97682012-08-10 12:09:43 -070037 *
Jason Sams80d81902012-09-13 17:00:48 -070038 * The default coefficients are.
Jason Sams6ab97682012-08-10 12:09:43 -070039 *
Jason Sams80d81902012-09-13 17:00:48 -070040 * <code>
41 * <p> [ 0, 0, 0 ]
42 * <p> [ 0, 1, 0 ]
43 * <p> [ 0, 0, 0 ]
44 * </code>
45 *
46 * @param rs The Renderscript context
47 * @param e Element type for intputs and outputs
Jason Sams6ab97682012-08-10 12:09:43 -070048 *
49 * @return ScriptIntrinsicConvolve3x3
50 */
51 public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) {
Jason Sams80d81902012-09-13 17:00:48 -070052 float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0};
53 if (e != Element.U8_4(rs)) {
54 throw new RSIllegalArgumentException("Unsuported element type.");
55 }
Jason Sams6ab97682012-08-10 12:09:43 -070056 int id = rs.nScriptIntrinsicCreate(1, e.getID(rs));
Jason Sams80d81902012-09-13 17:00:48 -070057 ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
58 si.setCoefficients(f);
59 return si;
Jason Sams6ab97682012-08-10 12:09:43 -070060
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 Sams19e10862012-08-21 15:53:29 -070069 public void setInput(Allocation ain) {
70 mInput = ain;
71 bindAllocation(ain, 1);
72 }
Jason Sams6ab97682012-08-10 12:09:43 -070073
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 ]
80 * <p> [ 3, 4, 5 ]
81 * <p> [ 6, 7, 8 ]
82 * </code>
83 *
84 * @param v The array of coefficients to set
85 */
86 public void setCoefficients(float v[]) {
Jason Sams6ab97682012-08-10 12:09:43 -070087 FieldPacker fp = new FieldPacker(9*4);
88 for (int ct=0; ct < mValues.length; ct++) {
89 mValues[ct] = v[ct];
90 fp.addF32(mValues[ct]);
91 }
92 setVar(0, fp);
93 }
Jason Sams19e10862012-08-21 15:53:29 -070094
Jason Sams80d81902012-09-13 17:00:48 -070095 /**
96 * Apply the filter to the input and save to the specified
97 * allocation.
98 *
99 * @param aout Output allocation. Must match creation element
100 * type.
101 */
Jason Sams19e10862012-08-21 15:53:29 -0700102 public void forEach(Allocation aout) {
103 forEach(0, null, aout, null);
104 }
105
Jason Sams08a81582012-09-18 12:32:10 -0700106 /**
107 * Get a KernelID for this intrinsic kernel.
108 *
109 * @return Script.KernelID The KernelID object.
110 */
111 public Script.KernelID getKernelID() {
112 return createKernelID(0, 2, null, null);
113 }
114
115 /**
116 * Get a FieldID for the input field of this intrinsic.
117 *
118 * @return Script.FieldID The FieldID object.
119 */
120 public Script.FieldID getFieldID_Input() {
121 return createFieldID(1, null);
122 }
123
Jason Sams6ab97682012-08-10 12:09:43 -0700124}
125