ScriptIntrinsicConvolve3x3.java revision 099deb8fb1715e62bcb24513f8e9305ab4f7743a
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.support.v8.renderscript; 18 19import android.util.Log; 20 21/** 22 * Intrinsic for applying a 3x3 convolve to an allocation. 23 * 24 **/ 25public class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic { 26 private final float[] mValues = new float[9]; 27 private Allocation mInput; 28 29 ScriptIntrinsicConvolve3x3(int id, RenderScript rs) { 30 super(id, rs); 31 } 32 33 /** 34 * Supported elements types are {@link Element#U8_4} 35 * 36 * The default coefficients are. 37 * 38 * <code> 39 * <p> [ 0, 0, 0 ] 40 * <p> [ 0, 1, 0 ] 41 * <p> [ 0, 0, 0 ] 42 * </code> 43 * 44 * @param rs The Renderscript context 45 * @param e Element type for intputs and outputs 46 * 47 * @return ScriptIntrinsicConvolve3x3 48 */ 49 public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) { 50 if (rs.isNative) { 51 RenderScriptThunker rst = (RenderScriptThunker) rs; 52 return ScriptIntrinsicConvolve3x3Thunker.create(rs, e); 53 } 54 55 float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0}; 56 if (!e.isCompatible(Element.U8_4(rs))) { 57 throw new RSIllegalArgumentException("Unsuported element type."); 58 } 59 int id = rs.nScriptIntrinsicCreate(1, e.getID(rs)); 60 ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs); 61 si.setCoefficients(f); 62 return si; 63 64 } 65 66 /** 67 * Set the input of the blur. 68 * Must match the element type supplied during create. 69 * 70 * @param ain The input allocation. 71 */ 72 public void setInput(Allocation ain) { 73 mInput = ain; 74 setVar(1, ain); 75 } 76 77 /** 78 * Set the coefficients for the convolve. 79 * 80 * The convolve layout is 81 * <code> 82 * <p> [ 0, 1, 2 ] 83 * <p> [ 3, 4, 5 ] 84 * <p> [ 6, 7, 8 ] 85 * </code> 86 * 87 * @param v The array of coefficients to set 88 */ 89 public void setCoefficients(float v[]) { 90 FieldPacker fp = new FieldPacker(9*4); 91 for (int ct=0; ct < mValues.length; ct++) { 92 mValues[ct] = v[ct]; 93 fp.addF32(mValues[ct]); 94 } 95 setVar(0, fp); 96 } 97 98 /** 99 * Apply the filter to the input and save to the specified 100 * allocation. 101 * 102 * @param aout Output allocation. Must match creation element 103 * type. 104 */ 105 public void forEach(Allocation aout) { 106 forEach(0, null, aout, null); 107 } 108 109 /** 110 * Get a KernelID for this intrinsic kernel. 111 * 112 * @return Script.KernelID The KernelID object. 113 */ 114 public Script.KernelID getKernelID() { 115 return createKernelID(0, 2, null, null); 116 } 117 118 /** 119 * Get a FieldID for the input field of this intrinsic. 120 * 121 * @return Script.FieldID The FieldID object. 122 */ 123 public Script.FieldID getFieldID_Input() { 124 return createFieldID(1, null); 125 } 126 127} 128 129