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 /** 20 * Intrinsic Gausian blur filter. Applies a gaussian blur of the 21 * specified radius to all elements of an allocation. 22 * 23 * 24 **/ 25 public final class ScriptIntrinsicBlur extends ScriptIntrinsic { 26 private final float[] mValues = new float[9]; 27 private Allocation mInput; 28 ScriptIntrinsicBlur(long id, RenderScript rs)29 private ScriptIntrinsicBlur(long id, RenderScript rs) { 30 super(id, rs); 31 } 32 33 /** 34 * Create an intrinsic for applying a blur to an allocation. The 35 * default radius is 5.0. 36 * 37 * Supported elements types are {@link Element#U8}, 38 * {@link Element#U8_4}. 39 * 40 * @param rs The RenderScript context 41 * @param e Element type for inputs and outputs 42 * 43 * @return ScriptIntrinsicBlur 44 */ create(RenderScript rs, Element e)45 public static ScriptIntrinsicBlur create(RenderScript rs, Element e) { 46 if ((!e.isCompatible(Element.U8_4(rs))) && (!e.isCompatible(Element.U8(rs)))) { 47 throw new RSIllegalArgumentException("Unsupported element type."); 48 } 49 long id = rs.nScriptIntrinsicCreate(5, e.getID(rs)); 50 ScriptIntrinsicBlur sib = new ScriptIntrinsicBlur(id, rs); 51 sib.setRadius(5.f); 52 return sib; 53 } 54 55 /** 56 * Set the input of the blur. 57 * Must match the element type supplied during create. 58 * 59 * @param ain The input allocation 60 */ setInput(Allocation ain)61 public void setInput(Allocation ain) { 62 if (ain.getType().getY() == 0) { 63 throw new RSIllegalArgumentException("Input set to a 1D Allocation"); 64 } 65 Element e = ain.getElement(); 66 if ((!e.isCompatible(Element.U8_4(mRS))) && (!e.isCompatible(Element.U8(mRS)))) { 67 throw new RSIllegalArgumentException("Unsupported element type."); 68 } 69 mInput = ain; 70 setVar(1, ain); 71 } 72 73 /** 74 * Set the radius of the Blur. 75 * 76 * Supported range 0 < radius <= 25 77 * 78 * @param radius The radius of the blur 79 */ setRadius(float radius)80 public void setRadius(float radius) { 81 if (radius <= 0 || radius > 25) { 82 throw new RSIllegalArgumentException("Radius out of range (0 < r <= 25)."); 83 } 84 setVar(0, radius); 85 } 86 87 /** 88 * Apply the filter to the input and save to the specified 89 * allocation. 90 * 91 * @param aout Output allocation. Must match creation element 92 * type. 93 */ forEach(Allocation aout)94 public void forEach(Allocation aout) { 95 if (aout.getType().getY() == 0) { 96 throw new RSIllegalArgumentException("Output is a 1D Allocation"); 97 } 98 forEach(0, (Allocation) null, aout, null); 99 } 100 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 * @param opt LaunchOptions for clipping 108 */ forEach(Allocation aout, Script.LaunchOptions opt)109 public void forEach(Allocation aout, Script.LaunchOptions opt) { 110 if (aout.getType().getY() == 0) { 111 throw new RSIllegalArgumentException("Output is a 1D Allocation"); 112 } 113 forEach(0, (Allocation) null, aout, null, opt); 114 } 115 116 117 /** 118 * Get a KernelID for this intrinsic kernel. 119 * 120 * @return Script.KernelID The KernelID object. 121 */ getKernelID()122 public Script.KernelID getKernelID() { 123 return createKernelID(0, 2, null, null); 124 } 125 126 /** 127 * Get a FieldID for the input field of this intrinsic. 128 * 129 * @return Script.FieldID The FieldID object. 130 */ getFieldID_Input()131 public Script.FieldID getFieldID_Input() { 132 return createFieldID(1, null); 133 } 134 } 135