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 Gausian blur filter. Applies a gaussian blur of the |
| 21 | * specified radius to all elements of an allocation. |
| 22 | * |
| 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 ScriptIntrinsicBlur extends ScriptIntrinsic { |
| 26 | private final float[] mValues = new float[9]; |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 27 | private Allocation mInput; |
| 28 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 29 | private ScriptIntrinsicBlur(long 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 | * Create an intrinsic for applying a blur to an allocation. The |
| 35 | * default radius is 5.0. |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 36 | * |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 37 | * Supported elements types are {@link Element#U8_4} |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 38 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 39 | * @param rs The RenderScript context |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 40 | * @param e Element type for inputs and outputs |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 41 | * |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 42 | * @return ScriptIntrinsicBlur |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 43 | */ |
| 44 | public static ScriptIntrinsicBlur create(RenderScript rs, Element e) { |
Tim Murray | bddc7ff | 2013-04-01 11:35:35 -0700 | [diff] [blame] | 45 | if ((!e.isCompatible(Element.U8_4(rs))) && (!e.isCompatible(Element.U8(rs)))) { |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 46 | throw new RSIllegalArgumentException("Unsuported element type."); |
| 47 | } |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 48 | long id = rs.nScriptIntrinsicCreate(5, e.getID(rs)); |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 49 | ScriptIntrinsicBlur sib = new ScriptIntrinsicBlur(id, rs); |
| 50 | sib.setRadius(5.f); |
| 51 | return sib; |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Set the input of the blur. |
| 56 | * Must match the element type supplied during create. |
| 57 | * |
| 58 | * @param ain The input allocation |
| 59 | */ |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 60 | public void setInput(Allocation ain) { |
| 61 | mInput = ain; |
Jason Sams | e6a7886 | 2012-10-15 15:45:12 -0700 | [diff] [blame] | 62 | setVar(1, ain); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Set the radius of the Blur. |
| 67 | * |
Jason Sams | 31864d7 | 2012-10-02 15:21:11 -0700 | [diff] [blame] | 68 | * Supported range 0 < radius <= 25 |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 69 | * |
| 70 | * @param radius The radius of the blur |
| 71 | */ |
| 72 | public void setRadius(float radius) { |
Jason Sams | 31864d7 | 2012-10-02 15:21:11 -0700 | [diff] [blame] | 73 | if (radius <= 0 || radius > 25) { |
| 74 | throw new RSIllegalArgumentException("Radius out of range (0 < r <= 25)."); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 75 | } |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 76 | setVar(0, radius); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Apply the filter to the input and save to the specified |
| 81 | * allocation. |
| 82 | * |
| 83 | * @param aout Output allocation. Must match creation element |
| 84 | * type. |
| 85 | */ |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 86 | public void forEach(Allocation aout) { |
Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 87 | forEach(aout, null); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 90 | /** |
Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 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 | * @param opt LaunchOptions for clipping |
| 97 | */ |
| 98 | public void forEach(Allocation aout, Script.LaunchOptions opt) { |
| 99 | forEach(0, null, aout, null, opt); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 104 | * Get a KernelID for this intrinsic kernel. |
| 105 | * |
| 106 | * @return Script.KernelID The KernelID object. |
| 107 | */ |
| 108 | public Script.KernelID getKernelID() { |
| 109 | return createKernelID(0, 2, null, null); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get a FieldID for the input field of this intrinsic. |
| 114 | * |
| 115 | * @return Script.FieldID The FieldID object. |
| 116 | */ |
| 117 | public Script.FieldID getFieldID_Input() { |
| 118 | return createFieldID(1, null); |
| 119 | } |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 120 | } |
| 121 | |