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 17package 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 **/ 25public final class ScriptIntrinsicBlur extends ScriptIntrinsic { 26 private final float[] mValues = new float[9]; 27 private Allocation mInput; 28 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 */ 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("Unsuported 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 */ 61 public void setInput(Allocation ain) { 62 mInput = ain; 63 setVar(1, ain); 64 } 65 66 /** 67 * Set the radius of the Blur. 68 * 69 * Supported range 0 < radius <= 25 70 * 71 * @param radius The radius of the blur 72 */ 73 public void setRadius(float radius) { 74 if (radius <= 0 || radius > 25) { 75 throw new RSIllegalArgumentException("Radius out of range (0 < r <= 25)."); 76 } 77 setVar(0, radius); 78 } 79 80 /** 81 * Apply the filter to the input and save to the specified 82 * allocation. 83 * 84 * @param aout Output allocation. Must match creation element 85 * type. 86 */ 87 public void forEach(Allocation aout) { 88 forEach(0, (Allocation) null, aout, null); 89 } 90 91 /** 92 * Apply the filter to the input and save to the specified 93 * allocation. 94 * 95 * @param aout Output allocation. Must match creation element 96 * type. 97 * @param opt LaunchOptions for clipping 98 */ 99 public void forEach(Allocation aout, Script.LaunchOptions opt) { 100 forEach(0, (Allocation) null, aout, null, opt); 101 } 102 103 104 /** 105 * Get a KernelID for this intrinsic kernel. 106 * 107 * @return Script.KernelID The KernelID object. 108 */ 109 public Script.KernelID getKernelID() { 110 return createKernelID(0, 2, null, null); 111 } 112 113 /** 114 * Get a FieldID for the input field of this intrinsic. 115 * 116 * @return Script.FieldID The FieldID object. 117 */ 118 public Script.FieldID getFieldID_Input() { 119 return createFieldID(1, null); 120 } 121} 122