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