ScriptIntrinsicColorMatrix.java revision 17fec32bba9da8df2062b52e414435574652ad50
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
17package android.renderscript;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.Log;
22
23import java.io.File;
24import java.io.IOException;
25import java.io.InputStream;
26import java.util.Map.Entry;
27import java.util.HashMap;
28
29
30/**
31 * @hide
32 **/
33public class ScriptIntrinsicColorMatrix extends ScriptIntrinsic {
34    private Matrix4f mMatrix = new Matrix4f();
35    private Allocation mInput;
36
37    ScriptIntrinsicColorMatrix(int id, RenderScript rs) {
38        super(id, rs);
39    }
40
41    /**
42     * Supported elements types are uchar4
43     *
44     * @param rs
45     * @param e
46     *
47     * @return ScriptIntrinsicColorMatrix
48     */
49    public static ScriptIntrinsicColorMatrix create(RenderScript rs, Element e) {
50        int id = rs.nScriptIntrinsicCreate(2, e.getID(rs));
51        return new ScriptIntrinsicColorMatrix(id, rs);
52
53    }
54
55    private void setMatrix() {
56        FieldPacker fp = new FieldPacker(16*4);
57        fp.addMatrix(mMatrix);
58        setVar(0, fp);
59    }
60
61    /**
62     * Set the color matrix which will be applied to each cell of the image.
63     *
64     * @param m The 4x4 matrix to set.
65     */
66    public void setColorMatrix(Matrix4f m) {
67        mMatrix.load(m);
68        setMatrix();
69    }
70
71    /**
72     * Set the color matrix which will be applied to each cell of the image.
73     * This will set the alpha channel to be a copy.
74     *
75     * @param m The 3x3 matrix to set.
76     */
77    public void setColorMatrix(Matrix3f m) {
78        mMatrix.load(m);
79        setMatrix();
80    }
81
82    /**
83     * Set a color matrix to convert from RGB to luminance. The alpha channel
84     * will be a copy.
85     *
86     */
87    public void setGreyscale() {
88        mMatrix.loadIdentity();
89        mMatrix.set(0, 0, 0.299f);
90        mMatrix.set(1, 0, 0.587f);
91        mMatrix.set(2, 0, 0.114f);
92        mMatrix.set(0, 1, 0.299f);
93        mMatrix.set(1, 1, 0.587f);
94        mMatrix.set(2, 1, 0.114f);
95        mMatrix.set(0, 2, 0.299f);
96        mMatrix.set(1, 2, 0.587f);
97        mMatrix.set(2, 2, 0.114f);
98        setMatrix();
99    }
100
101    /**
102     * Set the matrix to convert from YUV to RGB with a direct copy of the 4th
103     * channel.
104     *
105     */
106    public void setYUVtoRGB() {
107        mMatrix.loadIdentity();
108        mMatrix.set(0, 0, 1.f);
109        mMatrix.set(1, 0, 0.f);
110        mMatrix.set(2, 0, 1.13983f);
111        mMatrix.set(0, 1, 1.f);
112        mMatrix.set(1, 1, -0.39465f);
113        mMatrix.set(2, 1, -0.5806f);
114        mMatrix.set(0, 2, 1.f);
115        mMatrix.set(1, 2, 2.03211f);
116        mMatrix.set(2, 2, 0.f);
117        setMatrix();
118    }
119
120    /**
121     * Set the matrix to convert from RGB to YUV with a direct copy of the 4th
122     * channel.
123     *
124     */
125    public void setRGBtoYUV() {
126        mMatrix.loadIdentity();
127        mMatrix.set(0, 0, 0.299f);
128        mMatrix.set(1, 0, 0.587f);
129        mMatrix.set(2, 0, 0.114f);
130        mMatrix.set(0, 1, -0.14713f);
131        mMatrix.set(1, 1, -0.28886f);
132        mMatrix.set(2, 1, 0.436f);
133        mMatrix.set(0, 2, 0.615f);
134        mMatrix.set(1, 2, -0.51499f);
135        mMatrix.set(2, 2, -0.10001f);
136        setMatrix();
137    }
138
139
140    /**
141     * Invoke the kernel and apply the matrix to each cell of ain and copy to
142     * aout.
143     *
144     * @param ain Input allocation
145     * @param aout Output allocation
146     */
147    public void forEach(Allocation ain, Allocation aout) {
148        forEach(0, ain, aout, null);
149    }
150
151}
152
153