FileDocCategorySizeDatePackage
ScriptIntrinsicColorMatrix.javaAPI DocAndroid 5.1 API8957Thu Mar 12 22:22:42 GMT 2015android.renderscript

ScriptIntrinsicColorMatrix

public final class ScriptIntrinsicColorMatrix extends ScriptIntrinsic
Intrinsic for applying a color matrix to allocations. If the element type is {@link Element.DataType#UNSIGNED_8}, it is converted to {@link Element.DataType#FLOAT_32} and normalized from (0-255) to (0-1). If the incoming vector size is less than four, a {@link Element#F32_4} is created by filling the missing vector channels with zero. This value is then multiplied by the 4x4 color matrix as performed by rsMatrixMultiply(), adding a {@link Element#F32_4}, and then writing it to the output {@link Allocation}. If the ouptut type is unsigned, the value is normalized from (0-1) to (0-255) and converted. If the output vector size is less than four, the unused channels are discarded. Supported elements types are {@link Element#U8}, {@link Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4}, {@link Element#F32}, {@link Element#F32_2}, {@link Element#F32_3}, and {@link Element#F32_4}.

Fields Summary
private final Matrix4f
mMatrix
private final Float4
mAdd
Constructors Summary
private ScriptIntrinsicColorMatrix(long id, RenderScript rs)


         
        super(id, rs);
    
Methods Summary
public static android.renderscript.ScriptIntrinsicColorMatrixcreate(RenderScript rs, Element e)
Create an intrinsic for applying a color matrix to an allocation.

param
rs The RenderScript context
param
e Element type for inputs and outputs, As of API 19, this parameter is ignored. The Element type check is performed in the kernel launch.
deprecated
Use the single argument version as Element is now ignored.
return
ScriptIntrinsicColorMatrix

        return create(rs);
    
public static android.renderscript.ScriptIntrinsicColorMatrixcreate(RenderScript rs)
Create an intrinsic for applying a color matrix to an allocation.

param
rs The RenderScript context
return
ScriptIntrinsicColorMatrix

        long id = rs.nScriptIntrinsicCreate(2, 0);
        return new ScriptIntrinsicColorMatrix(id, rs);

    
public voidforEach(Allocation ain, Allocation aout)
Invoke the kernel and apply the matrix to each cell of input {@link Allocation} and copy to the output {@link Allocation}. If the vector size of the input is less than four, the remaining components are treated as zero for the matrix multiply. If the output vector size is less than four, the unused vector components are discarded.

param
ain Input allocation
param
aout Output allocation

        forEach(ain, aout, null);
    
public voidforEach(Allocation ain, Allocation aout, Script.LaunchOptions opt)
Invoke the kernel and apply the matrix to each cell of input {@link Allocation} and copy to the output {@link Allocation}. If the vector size of the input is less than four, the remaining components are treated as zero for the matrix multiply. If the output vector size is less than four, the unused vector components are discarded.

param
ain Input allocation
param
aout Output allocation
param
opt LaunchOptions for clipping

        if (!ain.getElement().isCompatible(Element.U8(mRS)) &&
            !ain.getElement().isCompatible(Element.U8_2(mRS)) &&
            !ain.getElement().isCompatible(Element.U8_3(mRS)) &&
            !ain.getElement().isCompatible(Element.U8_4(mRS)) &&
            !ain.getElement().isCompatible(Element.F32(mRS)) &&
            !ain.getElement().isCompatible(Element.F32_2(mRS)) &&
            !ain.getElement().isCompatible(Element.F32_3(mRS)) &&
            !ain.getElement().isCompatible(Element.F32_4(mRS))) {

            throw new RSIllegalArgumentException("Unsuported element type.");
        }

        if (!aout.getElement().isCompatible(Element.U8(mRS)) &&
            !aout.getElement().isCompatible(Element.U8_2(mRS)) &&
            !aout.getElement().isCompatible(Element.U8_3(mRS)) &&
            !aout.getElement().isCompatible(Element.U8_4(mRS)) &&
            !aout.getElement().isCompatible(Element.F32(mRS)) &&
            !aout.getElement().isCompatible(Element.F32_2(mRS)) &&
            !aout.getElement().isCompatible(Element.F32_3(mRS)) &&
            !aout.getElement().isCompatible(Element.F32_4(mRS))) {

            throw new RSIllegalArgumentException("Unsuported element type.");
        }

        forEach(0, ain, aout, null, opt);
    
public Script.KernelIDgetKernelID()
Get a KernelID for this intrinsic kernel.

return
Script.KernelID The KernelID object.

        return createKernelID(0, 3, null, null);
    
public voidsetAdd(Float4 f)
Set the value to be added after the color matrix has been applied. The default value is {0, 0, 0, 0}

param
f The float4 value to be added.

        mAdd.x = f.x;
        mAdd.y = f.y;
        mAdd.z = f.z;
        mAdd.w = f.w;

        FieldPacker fp = new FieldPacker(4*4);
        fp.addF32(f.x);
        fp.addF32(f.y);
        fp.addF32(f.z);
        fp.addF32(f.w);
        setVar(1, fp);
    
public voidsetAdd(float r, float g, float b, float a)
Set the value to be added after the color matrix has been applied. The default value is {0, 0, 0, 0}

param
r The red add value.
param
g The green add value.
param
b The blue add value.
param
a The alpha add value.

        mAdd.x = r;
        mAdd.y = g;
        mAdd.z = b;
        mAdd.w = a;

        FieldPacker fp = new FieldPacker(4*4);
        fp.addF32(mAdd.x);
        fp.addF32(mAdd.y);
        fp.addF32(mAdd.z);
        fp.addF32(mAdd.w);
        setVar(1, fp);
    
public voidsetColorMatrix(Matrix4f m)
Set the color matrix which will be applied to each cell of the image.

param
m The 4x4 matrix to set.

        mMatrix.load(m);
        setMatrix();
    
public voidsetColorMatrix(Matrix3f m)
Set the color matrix which will be applied to each cell of the image. This will set the alpha channel to be a copy.

param
m The 3x3 matrix to set.

        mMatrix.load(m);
        setMatrix();
    
public voidsetGreyscale()
Set a color matrix to convert from RGB to luminance. The alpha channel will be a copy.

        mMatrix.loadIdentity();
        mMatrix.set(0, 0, 0.299f);
        mMatrix.set(1, 0, 0.587f);
        mMatrix.set(2, 0, 0.114f);
        mMatrix.set(0, 1, 0.299f);
        mMatrix.set(1, 1, 0.587f);
        mMatrix.set(2, 1, 0.114f);
        mMatrix.set(0, 2, 0.299f);
        mMatrix.set(1, 2, 0.587f);
        mMatrix.set(2, 2, 0.114f);
        setMatrix();
    
private voidsetMatrix()

        FieldPacker fp = new FieldPacker(16*4);
        fp.addMatrix(mMatrix);
        setVar(0, fp);
    
public voidsetRGBtoYUV()
Set the matrix to convert from RGB to YUV with a direct copy of the 4th channel.

        mMatrix.loadIdentity();
        mMatrix.set(0, 0, 0.299f);
        mMatrix.set(1, 0, 0.587f);
        mMatrix.set(2, 0, 0.114f);
        mMatrix.set(0, 1, -0.14713f);
        mMatrix.set(1, 1, -0.28886f);
        mMatrix.set(2, 1, 0.436f);
        mMatrix.set(0, 2, 0.615f);
        mMatrix.set(1, 2, -0.51499f);
        mMatrix.set(2, 2, -0.10001f);
        setMatrix();
    
public voidsetYUVtoRGB()
Set the matrix to convert from YUV to RGB with a direct copy of the 4th channel.

        mMatrix.loadIdentity();
        mMatrix.set(0, 0, 1.f);
        mMatrix.set(1, 0, 0.f);
        mMatrix.set(2, 0, 1.13983f);
        mMatrix.set(0, 1, 1.f);
        mMatrix.set(1, 1, -0.39465f);
        mMatrix.set(2, 1, -0.5806f);
        mMatrix.set(0, 2, 1.f);
        mMatrix.set(1, 2, 2.03211f);
        mMatrix.set(2, 2, 0.f);
        setMatrix();