Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 19 | import android.util.Log; |
| 20 | |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 21 | /** |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 22 | * Intrinsic for applying a 3x3 convolve to an allocation. |
| 23 | * |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 24 | **/ |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 25 | public final class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic { |
| 26 | private final float[] mValues = new float[9]; |
Jason Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 27 | private Allocation mInput; |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 28 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 29 | private ScriptIntrinsicConvolve3x3(int id, RenderScript rs) { |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 30 | super(id, rs); |
| 31 | } |
| 32 | |
| 33 | /** |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 34 | * Supported elements types are {@link Element#U8_4} |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 35 | * |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 36 | * The default coefficients are. |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 37 | * |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 38 | * <code> |
| 39 | * <p> [ 0, 0, 0 ] |
| 40 | * <p> [ 0, 1, 0 ] |
| 41 | * <p> [ 0, 0, 0 ] |
| 42 | * </code> |
| 43 | * |
| 44 | * @param rs The Renderscript context |
| 45 | * @param e Element type for intputs and outputs |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 46 | * |
| 47 | * @return ScriptIntrinsicConvolve3x3 |
| 48 | */ |
| 49 | public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) { |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 50 | float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0}; |
Tim Murray | bddc7ff | 2013-04-01 11:35:35 -0700 | [diff] [blame^] | 51 | if (!e.isCompatible(Element.U8_4(rs))) { |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 52 | throw new RSIllegalArgumentException("Unsuported element type."); |
| 53 | } |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 54 | int id = rs.nScriptIntrinsicCreate(1, e.getID(rs)); |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 55 | ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs); |
| 56 | si.setCoefficients(f); |
| 57 | return si; |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 58 | |
| 59 | } |
| 60 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Set the input of the blur. |
| 63 | * Must match the element type supplied during create. |
| 64 | * |
| 65 | * @param ain The input allocation. |
| 66 | */ |
Jason Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 67 | public void setInput(Allocation ain) { |
| 68 | mInput = ain; |
Jason Sams | e6a7886 | 2012-10-15 15:45:12 -0700 | [diff] [blame] | 69 | setVar(1, ain); |
Jason Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 70 | } |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 71 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Set the coefficients for the convolve. |
| 74 | * |
| 75 | * The convolve layout is |
| 76 | * <code> |
| 77 | * <p> [ 0, 1, 2 ] |
| 78 | * <p> [ 3, 4, 5 ] |
| 79 | * <p> [ 6, 7, 8 ] |
| 80 | * </code> |
| 81 | * |
| 82 | * @param v The array of coefficients to set |
| 83 | */ |
| 84 | public void setCoefficients(float v[]) { |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 85 | FieldPacker fp = new FieldPacker(9*4); |
| 86 | for (int ct=0; ct < mValues.length; ct++) { |
| 87 | mValues[ct] = v[ct]; |
| 88 | fp.addF32(mValues[ct]); |
| 89 | } |
| 90 | setVar(0, fp); |
| 91 | } |
Jason Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 92 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Apply the filter to the input and save to the specified |
| 95 | * allocation. |
| 96 | * |
| 97 | * @param aout Output allocation. Must match creation element |
| 98 | * type. |
| 99 | */ |
Jason Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 100 | public void forEach(Allocation aout) { |
| 101 | forEach(0, null, aout, null); |
| 102 | } |
| 103 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Get a KernelID for this intrinsic kernel. |
| 106 | * |
| 107 | * @return Script.KernelID The KernelID object. |
| 108 | */ |
| 109 | public Script.KernelID getKernelID() { |
| 110 | return createKernelID(0, 2, null, null); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get a FieldID for the input field of this intrinsic. |
| 115 | * |
| 116 | * @return Script.FieldID The FieldID object. |
| 117 | */ |
| 118 | public Script.FieldID getFieldID_Input() { |
| 119 | return createFieldID(1, null); |
| 120 | } |
| 121 | |
Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 122 | } |
| 123 | |