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