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 color matrix to allocations.
     23  *
     24  * This has the same effect as loading each element and
     25  * converting it to a {@link Element#F32_4}, multiplying the
     26  * result by the 4x4 color matrix as performed by
     27  * rsMatrixMultiply() and writing it to the output after
     28  * conversion back to {@link Element#U8_4}.
     29  **/
     30 public final class ScriptIntrinsicColorMatrix extends ScriptIntrinsic {
     31     private final Matrix4f mMatrix = new Matrix4f();
     32     private Allocation mInput;
     33 
     34     private ScriptIntrinsicColorMatrix(int id, RenderScript rs) {
     35         super(id, rs);
     36     }
     37 
     38     /**
     39      * Create an intrinsic for applying a color matrix to an
     40      * allocation.
     41      *
     42      * Supported elements types are {@link Element#U8_4}
     43      *
     44      * @param rs The RenderScript context
     45      * @param e Element type for intputs and outputs
     46      *
     47      * @return ScriptIntrinsicColorMatrix
     48      */
     49     public static ScriptIntrinsicColorMatrix create(RenderScript rs, Element e) {
     50         if (!e.isCompatible(Element.U8_4(rs))) {
     51             throw new RSIllegalArgumentException("Unsuported element type.");
     52         }
     53         int id = rs.nScriptIntrinsicCreate(2, e.getID(rs));
     54         return new ScriptIntrinsicColorMatrix(id, rs);
     55 
     56     }
     57 
     58     private void setMatrix() {
     59         FieldPacker fp = new FieldPacker(16*4);
     60         fp.addMatrix(mMatrix);
     61         setVar(0, fp);
     62     }
     63 
     64     /**
     65      * Set the color matrix which will be applied to each cell of
     66      * the image.
     67      *
     68      * @param m The 4x4 matrix to set.
     69      */
     70     public void setColorMatrix(Matrix4f m) {
     71         mMatrix.load(m);
     72         setMatrix();
     73     }
     74 
     75     /**
     76      * Set the color matrix which will be applied to each cell of the image.
     77      * This will set the alpha channel to be a copy.
     78      *
     79      * @param m The 3x3 matrix to set.
     80      */
     81     public void setColorMatrix(Matrix3f m) {
     82         mMatrix.load(m);
     83         setMatrix();
     84     }
     85 
     86     /**
     87      * Set a color matrix to convert from RGB to luminance. The alpha channel
     88      * will be a copy.
     89      *
     90      */
     91     public void setGreyscale() {
     92         mMatrix.loadIdentity();
     93         mMatrix.set(0, 0, 0.299f);
     94         mMatrix.set(1, 0, 0.587f);
     95         mMatrix.set(2, 0, 0.114f);
     96         mMatrix.set(0, 1, 0.299f);
     97         mMatrix.set(1, 1, 0.587f);
     98         mMatrix.set(2, 1, 0.114f);
     99         mMatrix.set(0, 2, 0.299f);
    100         mMatrix.set(1, 2, 0.587f);
    101         mMatrix.set(2, 2, 0.114f);
    102         setMatrix();
    103     }
    104 
    105     /**
    106      * Set the matrix to convert from YUV to RGB with a direct copy of the 4th
    107      * channel.
    108      *
    109      */
    110     public void setYUVtoRGB() {
    111         mMatrix.loadIdentity();
    112         mMatrix.set(0, 0, 1.f);
    113         mMatrix.set(1, 0, 0.f);
    114         mMatrix.set(2, 0, 1.13983f);
    115         mMatrix.set(0, 1, 1.f);
    116         mMatrix.set(1, 1, -0.39465f);
    117         mMatrix.set(2, 1, -0.5806f);
    118         mMatrix.set(0, 2, 1.f);
    119         mMatrix.set(1, 2, 2.03211f);
    120         mMatrix.set(2, 2, 0.f);
    121         setMatrix();
    122     }
    123 
    124     /**
    125      * Set the matrix to convert from RGB to YUV with a direct copy of the 4th
    126      * channel.
    127      *
    128      */
    129     public void setRGBtoYUV() {
    130         mMatrix.loadIdentity();
    131         mMatrix.set(0, 0, 0.299f);
    132         mMatrix.set(1, 0, 0.587f);
    133         mMatrix.set(2, 0, 0.114f);
    134         mMatrix.set(0, 1, -0.14713f);
    135         mMatrix.set(1, 1, -0.28886f);
    136         mMatrix.set(2, 1, 0.436f);
    137         mMatrix.set(0, 2, 0.615f);
    138         mMatrix.set(1, 2, -0.51499f);
    139         mMatrix.set(2, 2, -0.10001f);
    140         setMatrix();
    141     }
    142 
    143 
    144     /**
    145      * Invoke the kernel and apply the matrix to each cell of ain and copy to
    146      * aout.
    147      *
    148      * @param ain Input allocation
    149      * @param aout Output allocation
    150      */
    151     public void forEach(Allocation ain, Allocation aout) {
    152         forEach(0, ain, aout, null);
    153     }
    154 
    155     /**
    156      * Get a KernelID for this intrinsic kernel.
    157      *
    158      * @return Script.KernelID The KernelID object.
    159      */
    160     public Script.KernelID getKernelID() {
    161         return createKernelID(0, 3, null, null);
    162     }
    163 
    164 }
    165 
    166