ScriptIntrinsicConvolve5x5.java revision 7d435ae5ba100be5710b685653cc351cab159c11
1ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams/* 2ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Copyright (C) 2012 The Android Open Source Project 3ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 4ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Licensed under the Apache License, Version 2.0 (the "License"); 5ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * you may not use this file except in compliance with the License. 6ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * You may obtain a copy of the License at 7ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 8ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * http://www.apache.org/licenses/LICENSE-2.0 9ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 10ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Unless required by applicable law or agreed to in writing, software 11ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * distributed under the License is distributed on an "AS IS" BASIS, 12ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * See the License for the specific language governing permissions and 14ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * limitations under the License. 15ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 16ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 17ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Samspackage android.support.v8.renderscript; 18ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 19ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Samsimport android.util.Log; 20ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 21ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams/** 22ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Intrinsic for applying a 5x5 convolve to an allocation. 23ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 24ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams **/ 25ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murraypublic class ScriptIntrinsicConvolve5x5 extends ScriptIntrinsic { 26ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams private final float[] mValues = new float[25]; 27ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams private Allocation mInput; 28ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 29ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray ScriptIntrinsicConvolve5x5(int id, RenderScript rs) { 30ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams super(id, rs); 31ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 32ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 33ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 34ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Supported elements types are {@link Element#U8_4} 35ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 36ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * The default coefficients are. 37ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <code> 38ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 39ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 40ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 1, 0, 0 ] 41ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 42ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 43ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * </code> 44ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * @param rs The RenderScript context 46ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param e Element type for intputs and outputs 47ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 48ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @return ScriptIntrinsicConvolve5x5 49ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 50ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public static ScriptIntrinsicConvolve5x5 create(RenderScript rs, Element e) { 51ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray if (rs.isNative) { 52ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray RenderScriptThunker rst = (RenderScriptThunker) rs; 53ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray return ScriptIntrinsicConvolve5x5Thunker.create(rs, e); 54ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray } 55ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams int id = rs.nScriptIntrinsicCreate(4, e.getID(rs)); 56ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams return new ScriptIntrinsicConvolve5x5(id, rs); 57ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 58ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 59ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 60ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 61ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Set the input of the blur. 62ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Must match the element type supplied during create. 63ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 64ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param ain The input allocation. 65ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 66ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public void setInput(Allocation ain) { 67ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams mInput = ain; 681b370e358d16cc3b50b169511d6b387db09f972dJason Sams setVar(1, ain); 69ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 70ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 71ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 72ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Set the coefficients for the convolve. 73ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 74ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * The convolve layout is 75ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <code> 76ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 1, 2, 3, 4 ] 77ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 5, 6, 7, 8, 9 ] 78ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 10, 11, 12, 13, 14 ] 79ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 15, 16, 17, 18, 19 ] 80ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 20, 21, 22, 23, 24 ] 81ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * </code> 82ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 83ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param v The array of coefficients to set 84ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 85ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public void setCoefficients(float v[]) { 86ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams FieldPacker fp = new FieldPacker(25*4); 87ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams for (int ct=0; ct < mValues.length; ct++) { 88ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams mValues[ct] = v[ct]; 89ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams fp.addF32(mValues[ct]); 90ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 91ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams setVar(0, fp); 92ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 93ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 94ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 95ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Apply the filter to the input and save to the specified 96ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * allocation. 97ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 98ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param aout Output allocation. Must match creation element 99ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * type. 100ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 101ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public void forEach(Allocation aout) { 102ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams forEach(0, null, aout, null); 103ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 104ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 105ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 106ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Get a KernelID for this intrinsic kernel. 107ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 108ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @return Script.KernelID The KernelID object. 109ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 110ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public Script.KernelID getKernelID() { 111ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams return createKernelID(0, 2, null, null); 112ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 113ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 114ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 115ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Get a FieldID for the input field of this intrinsic. 116ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 117ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @return Script.FieldID The FieldID object. 118ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 119ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public Script.FieldID getFieldID_Input() { 120ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams return createFieldID(1, null); 121ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 122ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams} 123ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 124