2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.renderscript;
19 import android.util.Log;
22 * Intrinsic for applying a color matrix to allocations.
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}.
30 public final class ScriptIntrinsicColorMatrix extends ScriptIntrinsic {
31 private final Matrix4f mMatrix = new Matrix4f();
32 private Allocation mInput;
34 private ScriptIntrinsicColorMatrix(int id, RenderScript rs) {
39 * Create an intrinsic for applying a color matrix to an
42 * Supported elements types are {@link Element#U8_4}
44 * @param rs The Renderscript context
45 * @param e Element type for intputs and outputs
47 * @return ScriptIntrinsicColorMatrix
49 public static ScriptIntrinsicColorMatrix create(RenderScript rs, Element e) {
50 if (!e.isCompatible(Element.U8_4(rs))) {
51 throw new RSIllegalArgumentException("Unsuported element type.");
53 int id = rs.nScriptIntrinsicCreate(2, e.getID(rs));
54 return new ScriptIntrinsicColorMatrix(id, rs);
58 private void setMatrix() {
59 FieldPacker fp = new FieldPacker(16*4);
60 fp.addMatrix(mMatrix);
65 * Set the color matrix which will be applied to each cell of
68 * @param m The 4x4 matrix to set.
70 public void setColorMatrix(Matrix4f m) {
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.
79 * @param m The 3x3 matrix to set.
81 public void setColorMatrix(Matrix3f m) {
87 * Set a color matrix to convert from RGB to luminance. The alpha channel
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);
106 * Set the matrix to convert from YUV to RGB with a direct copy of the 4th
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);
125 * Set the matrix to convert from RGB to YUV with a direct copy of the 4th
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);
145 * Invoke the kernel and apply the matrix to each cell of ain and copy to
148 * @param ain Input allocation
149 * @param aout Output allocation
151 public void forEach(Allocation ain, Allocation aout) {
152 forEach(0, ain, aout, null);
156 * Get a KernelID for this intrinsic kernel.
158 * @return Script.KernelID The KernelID object.
160 public Script.KernelID getKernelID() {
161 return createKernelID(0, 3, null, null);