Home | History | Annotate | Download | only in renderscript
      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 5x5 convolve to an allocation.
     23  *
     24  **/
     25 public class ScriptIntrinsicConvolve5x5 extends ScriptIntrinsic {
     26     private final float[] mValues = new float[25];
     27     private Allocation mInput;
     28     // API level for the intrinsic
     29     private static final int INTRINSIC_API_LEVEL = 19;
     30 
     31     ScriptIntrinsicConvolve5x5(long id, RenderScript rs) {
     32         super(id, rs);
     33     }
     34 
     35     /**
     36      * Supported elements types are {@link Element#U8}, {@link
     37      * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
     38      * {@link Element#F32}, {@link Element#F32_2}, {@link
     39      * Element#F32_3}, and {@link Element#F32_4}.
     40      *
     41      * <p> The default coefficients are:
     42      * <code>
     43      * <p> [ 0,  0,  0,  0,  0  ]
     44      * <p> [ 0,  0,  0,  0,  0  ]
     45      * <p> [ 0,  0,  1,  0,  0  ]
     46      * <p> [ 0,  0,  0,  0,  0  ]
     47      * <p> [ 0,  0,  0,  0,  0  ]
     48      * </code>
     49      *
     50      * @param rs The RenderScript context
     51      * @param e Element type for intputs and outputs
     52      *
     53      * @return ScriptIntrinsicConvolve5x5
     54      */
     55     public static ScriptIntrinsicConvolve5x5 create(RenderScript rs, Element e) {
     56         if (!e.isCompatible(Element.U8(rs)) &&
     57             !e.isCompatible(Element.U8_2(rs)) &&
     58             !e.isCompatible(Element.U8_3(rs)) &&
     59             !e.isCompatible(Element.U8_4(rs)) &&
     60             !e.isCompatible(Element.F32(rs)) &&
     61             !e.isCompatible(Element.F32_2(rs)) &&
     62             !e.isCompatible(Element.F32_3(rs)) &&
     63             !e.isCompatible(Element.F32_4(rs))) {
     64             throw new RSIllegalArgumentException("Unsupported element type.");
     65         }
     66         long id;
     67         boolean mUseIncSupp = rs.isUseNative() &&
     68                               android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
     69 
     70         id = rs.nScriptIntrinsicCreate(4, e.getID(rs), mUseIncSupp);
     71 
     72         ScriptIntrinsicConvolve5x5 si = new ScriptIntrinsicConvolve5x5(id, rs);
     73         si.setIncSupp(mUseIncSupp);
     74         return si;
     75 
     76     }
     77 
     78     /**
     79      * Set the input of the 5x5 convolve.
     80      * Must match the element type supplied during create.
     81      *
     82      * @param ain The input allocation.
     83      */
     84     public void setInput(Allocation ain) {
     85         mInput = ain;
     86         setVar(1, ain);
     87     }
     88 
     89     /**
     90     * Set the coefficients for the convolve.
     91     *
     92     * <p> The convolve layout is:
     93     * <code>
     94     * <p> [ 0,  1,  2,  3,  4  ]
     95     * <p> [ 5,  6,  7,  8,  9  ]
     96     * <p> [ 10, 11, 12, 13, 14 ]
     97     * <p> [ 15, 16, 17, 18, 19 ]
     98     * <p> [ 20, 21, 22, 23, 24 ]
     99     * </code>
    100     *
    101     * @param v The array of coefficients to set
    102     */
    103     public void setCoefficients(float v[]) {
    104         FieldPacker fp = new FieldPacker(25*4);
    105         for (int ct=0; ct < mValues.length; ct++) {
    106             mValues[ct] = v[ct];
    107             fp.addF32(mValues[ct]);
    108         }
    109         setVar(0, fp);
    110     }
    111 
    112     /**
    113      * Apply the filter to the input and save to the specified
    114      * allocation.
    115      *
    116      * @param aout Output allocation. Must match creation element
    117      *             type.
    118      */
    119     public void forEach(Allocation aout) {
    120         forEach(0, (Allocation) null, aout, null);
    121     }
    122 
    123     /**
    124      * Apply the filter to the input and save to the specified
    125      * allocation.
    126      *
    127      * @param aout Output allocation. Must match creation element
    128      *             type.
    129      * @param opt LaunchOptions for clipping
    130      */
    131     public void forEach(Allocation aout, Script.LaunchOptions opt) {
    132         forEach(0, (Allocation) null, aout, null, opt);
    133     }
    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