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 android.renderscript;
     18 
     19 import android.util.Log;
     20 
     21 /**
     22  * Intrinsic for applying a per-channel lookup table. Each
     23  * channel of the input has an independant lookup table. The
     24  * tables are 256 entries in size and can cover the full value
     25  * range of {@link Element#U8_4}.
     26  **/
     27 public final class ScriptIntrinsicLUT extends ScriptIntrinsic {
     28     private final Matrix4f mMatrix = new Matrix4f();
     29     private Allocation mTables;
     30     private final byte mCache[] = new byte[1024];
     31     private boolean mDirty = true;
     32 
     33     private ScriptIntrinsicLUT(int id, RenderScript rs) {
     34         super(id, rs);
     35         mTables = Allocation.createSized(rs, Element.U8(rs), 1024);
     36         for (int ct=0; ct < 256; ct++) {
     37             mCache[ct] = (byte)ct;
     38             mCache[ct + 256] = (byte)ct;
     39             mCache[ct + 512] = (byte)ct;
     40             mCache[ct + 768] = (byte)ct;
     41         }
     42         setVar(0, mTables);
     43     }
     44 
     45     /**
     46      * Supported elements types are {@link Element#U8_4}
     47      *
     48      * The defaults tables are identity.
     49      *
     50      * @param rs The RenderScript context
     51      * @param e Element type for intputs and outputs
     52      *
     53      * @return ScriptIntrinsicLUT
     54      */
     55     public static ScriptIntrinsicLUT create(RenderScript rs, Element e) {
     56         int id = rs.nScriptIntrinsicCreate(3, e.getID(rs));
     57         return new ScriptIntrinsicLUT(id, rs);
     58 
     59     }
     60 
     61 
     62     private void validate(int index, int value) {
     63         if (index < 0 || index > 255) {
     64             throw new RSIllegalArgumentException("Index out of range (0-255).");
     65         }
     66         if (value < 0 || value > 255) {
     67             throw new RSIllegalArgumentException("Value out of range (0-255).");
     68         }
     69     }
     70 
     71     /**
     72      * Set an entry in the red channel lookup table
     73      *
     74      * @param index Must be 0-255
     75      * @param value Must be 0-255
     76      */
     77     public void setRed(int index, int value) {
     78         validate(index, value);
     79         mCache[index] = (byte)value;
     80         mDirty = true;
     81     }
     82 
     83     /**
     84      * Set an entry in the green channel lookup table
     85      *
     86      * @param index Must be 0-255
     87      * @param value Must be 0-255
     88      */
     89     public void setGreen(int index, int value) {
     90         validate(index, value);
     91         mCache[index+256] = (byte)value;
     92         mDirty = true;
     93     }
     94 
     95     /**
     96      * Set an entry in the blue channel lookup table
     97      *
     98      * @param index Must be 0-255
     99      * @param value Must be 0-255
    100      */
    101     public void setBlue(int index, int value) {
    102         validate(index, value);
    103         mCache[index+512] = (byte)value;
    104         mDirty = true;
    105     }
    106 
    107     /**
    108      * Set an entry in the alpha channel lookup table
    109      *
    110      * @param index Must be 0-255
    111      * @param value Must be 0-255
    112      */
    113     public void setAlpha(int index, int value) {
    114         validate(index, value);
    115         mCache[index+768] = (byte)value;
    116         mDirty = true;
    117     }
    118 
    119 
    120     /**
    121      * Invoke the kernel and apply the lookup to each cell of ain
    122      * and copy to aout.
    123      *
    124      * @param ain Input allocation
    125      * @param aout Output allocation
    126      */
    127     public void forEach(Allocation ain, Allocation aout) {
    128         if (mDirty) {
    129             mDirty = false;
    130             mTables.copyFromUnchecked(mCache);
    131         }
    132         forEach(0, ain, aout, null);
    133     }
    134 
    135     /**
    136      * Get a KernelID for this intrinsic kernel.
    137      *
    138      * @return Script.KernelID The KernelID object.
    139      */
    140     public Script.KernelID getKernelID() {
    141         return createKernelID(0, 3, null, null);
    142     }
    143 }
    144 
    145