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