| 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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.util.Log; |
| 22 | |
| Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 23 | /** |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 24 | * Intrinsic for applying a 3x3 convolve to an allocation. |
| 25 | * |
| Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 26 | **/ |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 27 | public final class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic { |
| 28 | private final float[] mValues = new float[9]; |
| Jason Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 29 | private Allocation mInput; |
| Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 30 | |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 31 | private ScriptIntrinsicConvolve3x3(int id, RenderScript rs) { |
| Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 32 | super(id, rs); |
| 33 | } |
| 34 | |
| 35 | /** |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 36 | * Supported elements types are {@link Element#U8_4} |
| 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 | * The default coefficients are. |
| Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 39 | * |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 40 | * <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 Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 48 | * |
| 49 | * @return ScriptIntrinsicConvolve3x3 |
| 50 | */ |
| 51 | public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) { |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 52 | 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 Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 56 | int id = rs.nScriptIntrinsicCreate(1, e.getID(rs)); |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 57 | ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs); |
| 58 | si.setCoefficients(f); |
| 59 | return si; |
| Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 60 | |
| 61 | } |
| 62 | |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 63 | /** |
| 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 Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 69 | public void setInput(Allocation ain) { |
| 70 | mInput = ain; |
| 71 | bindAllocation(ain, 1); |
| 72 | } |
| Jason Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 73 | |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 74 | /** |
| 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 Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 87 | 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 Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 94 | |
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 95 | /** |
| 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 Sams | 19e1086 | 2012-08-21 15:53:29 -0700 | [diff] [blame] | 102 | public void forEach(Allocation aout) { |
| 103 | forEach(0, null, aout, null); |
| 104 | } |
| 105 | |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 106 | /** |
| 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 Sams | 6ab9768 | 2012-08-10 12:09:43 -0700 | [diff] [blame] | 124 | } |
| 125 | |