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