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 androidx.renderscript; 18 19 import android.util.Log; 20 21 /** 22 * Intrinsic for applying a 3x3 convolve to an allocation. 23 * 24 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a 25 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration 26 * guide</a> for the proposed alternatives. 27 **/ 28 @Deprecated 29 public class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic { 30 private final float[] mValues = new float[9]; 31 private Allocation mInput; 32 // API level for the intrinsic 33 private static final int INTRINSIC_API_LEVEL = 19; 34 ScriptIntrinsicConvolve3x3(long id, RenderScript rs)35 ScriptIntrinsicConvolve3x3(long id, RenderScript rs) { 36 super(id, rs); 37 } 38 39 /** 40 * Supported elements types are {@link Element#U8}, {@link 41 * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4}, 42 * {@link Element#F32}, {@link Element#F32_2}, {@link 43 * Element#F32_3}, and {@link Element#F32_4}. 44 * 45 * <p> The default coefficients are: 46 * <code> 47 * <p> [ 0, 0, 0 ] 48 * <p> [ 0, 1, 0 ] 49 * <p> [ 0, 0, 0 ] 50 * </code> 51 * 52 * @param rs The RenderScript context 53 * @param e Element type for intputs and outputs 54 * 55 * @return ScriptIntrinsicConvolve3x3 56 */ create(RenderScript rs, Element e)57 public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) { 58 float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0}; 59 if (!e.isCompatible(Element.U8(rs)) && 60 !e.isCompatible(Element.U8_2(rs)) && 61 !e.isCompatible(Element.U8_3(rs)) && 62 !e.isCompatible(Element.U8_4(rs)) && 63 !e.isCompatible(Element.F32(rs)) && 64 !e.isCompatible(Element.F32_2(rs)) && 65 !e.isCompatible(Element.F32_3(rs)) && 66 !e.isCompatible(Element.F32_4(rs))) { 67 throw new RSIllegalArgumentException("Unsupported element type."); 68 } 69 long id; 70 boolean mUseIncSupp = rs.isUseNative() && 71 android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL; 72 73 id = rs.nScriptIntrinsicCreate(1, e.getID(rs), mUseIncSupp); 74 75 ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs); 76 si.setIncSupp(mUseIncSupp); 77 si.setCoefficients(f); 78 return si; 79 } 80 81 /** 82 * Set the input of the 3x3 convolve. 83 * Must match the element type supplied during create. 84 * 85 * @param ain The input allocation. 86 */ 87 public void setInput(Allocation ain) { 88 mInput = ain; 89 setVar(1, ain); 90 } 91 92 /** 93 * Set the coefficients for the convolve. 94 * 95 * <p> The convolve layout is: 96 * <code> 97 * <p> [ 0, 1, 2 ] 98 * <p> [ 3, 4, 5 ] 99 * <p> [ 6, 7, 8 ] 100 * </code> 101 * 102 * @param v The array of coefficients to set 103 */ 104 public void setCoefficients(float v[]) { 105 FieldPacker fp = new FieldPacker(9*4); 106 for (int ct=0; ct < mValues.length; ct++) { 107 mValues[ct] = v[ct]; 108 fp.addF32(mValues[ct]); 109 } 110 setVar(0, fp); 111 } 112 113 /** 114 * Apply the filter to the input and save to the specified 115 * allocation. 116 * 117 * @param aout Output allocation. Must match creation element 118 * type. 119 */ 120 public void forEach(Allocation aout) { 121 forEach(0, (Allocation) null, aout, null); 122 } 123 124 /** 125 * Apply the filter to the input and save to the specified 126 * allocation. 127 * 128 * @param aout Output allocation. Must match creation element 129 * type. 130 * @param opt LaunchOptions for clipping 131 */ 132 public void forEach(Allocation aout, Script.LaunchOptions opt) { 133 forEach(0, (Allocation) null, aout, null, opt); 134 } 135 136 /** 137 * Get a KernelID for this intrinsic kernel. 138 * 139 * @return Script.KernelID The KernelID object. 140 */ 141 public Script.KernelID getKernelID() { 142 return createKernelID(0, 2, null, null); 143 } 144 145 /** 146 * Get a FieldID for the input field of this intrinsic. 147 * 148 * @return Script.FieldID The FieldID object. 149 */ 150 public Script.FieldID getFieldID_Input() { 151 return createFieldID(1, null); 152 } 153 154 } 155 156