blob: 4b0d507d4527ef8e47fbdcdea3d2539f1ffe7ec3 [file] [log] [blame]
Jason Sams5729fcd2012-09-04 19:57:40 -07001/*
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
Jason Sams5729fcd2012-09-04 19:57:40 -070019/**
Jason Sams80d81902012-09-13 17:00:48 -070020 * Intrinsic for applying a color matrix to allocations.
21 *
Jason Samsd9792082013-07-26 20:24:34 -070022 * If the element type is {@link Element.DataType#UNSIGNED_8},
23 * it is converted to {@link Element.DataType#FLOAT_32} and
24 * normalized from (0-255) to (0-1). If the incoming vector size
25 * is less than four, a {@link Element#F32_4} is created by
26 * filling the missing vector channels with zero. This value is
27 * then multiplied by the 4x4 color matrix as performed by
28 * rsMatrixMultiply(), adding a {@link Element#F32_4}, and then
29 * writing it to the output {@link Allocation}.
30 *
31 * If the ouptut type is unsigned, the value is normalized from
32 * (0-1) to (0-255) and converted. If the output vector size is
33 * less than four, the unused channels are discarded.
34 *
35 * Supported elements types are {@link Element#U8}, {@link
36 * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
37 * {@link Element#F32}, {@link Element#F32_2}, {@link
38 * Element#F32_3}, and {@link Element#F32_4}.
Jason Sams5729fcd2012-09-04 19:57:40 -070039 **/
Jason Sams80d81902012-09-13 17:00:48 -070040public final class ScriptIntrinsicColorMatrix extends ScriptIntrinsic {
41 private final Matrix4f mMatrix = new Matrix4f();
Jason Samsd9792082013-07-26 20:24:34 -070042 private final Float4 mAdd = new Float4();
Jason Sams5729fcd2012-09-04 19:57:40 -070043
Tim Murray460a0492013-11-19 12:45:54 -080044 private ScriptIntrinsicColorMatrix(long id, RenderScript rs) {
Jason Sams5729fcd2012-09-04 19:57:40 -070045 super(id, rs);
46 }
47
48 /**
Jason Sams80d81902012-09-13 17:00:48 -070049 * Create an intrinsic for applying a color matrix to an
50 * allocation.
Jason Sams5729fcd2012-09-04 19:57:40 -070051 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070052 * @param rs The RenderScript context
Jason Samsd9792082013-07-26 20:24:34 -070053 * @param e Element type for inputs and outputs, As of API 19,
54 * this parameter is ignored. The Element type check is
55 * performed in the kernel launch.
Jason Sams5729fcd2012-09-04 19:57:40 -070056 *
Jason Samsa49e89d2013-08-29 17:00:37 -070057 * @deprecated Use the single argument version as Element is now
58 * ignored.
59 *
Jason Sams5729fcd2012-09-04 19:57:40 -070060 * @return ScriptIntrinsicColorMatrix
61 */
Jason Samsa49e89d2013-08-29 17:00:37 -070062 @Deprecated
Jason Sams5729fcd2012-09-04 19:57:40 -070063 public static ScriptIntrinsicColorMatrix create(RenderScript rs, Element e) {
Jason Samsd9792082013-07-26 20:24:34 -070064 return create(rs);
65 }
66
67 /**
68 * Create an intrinsic for applying a color matrix to an
69 * allocation.
70 *
71 * @param rs The RenderScript context
72 *
73 * @return ScriptIntrinsicColorMatrix
74 */
75 public static ScriptIntrinsicColorMatrix create(RenderScript rs) {
Tim Murray460a0492013-11-19 12:45:54 -080076 long id = rs.nScriptIntrinsicCreate(2, 0);
Jason Sams5729fcd2012-09-04 19:57:40 -070077 return new ScriptIntrinsicColorMatrix(id, rs);
78
79 }
80
Jason Sams8fd58532012-09-05 15:30:18 -070081 private void setMatrix() {
Jason Sams5729fcd2012-09-04 19:57:40 -070082 FieldPacker fp = new FieldPacker(16*4);
Jason Sams8fd58532012-09-05 15:30:18 -070083 fp.addMatrix(mMatrix);
Jason Sams5729fcd2012-09-04 19:57:40 -070084 setVar(0, fp);
85 }
86
Jason Sams8fd58532012-09-05 15:30:18 -070087 /**
Jason Sams80d81902012-09-13 17:00:48 -070088 * Set the color matrix which will be applied to each cell of
89 * the image.
Jason Sams8fd58532012-09-05 15:30:18 -070090 *
91 * @param m The 4x4 matrix to set.
92 */
93 public void setColorMatrix(Matrix4f m) {
94 mMatrix.load(m);
95 setMatrix();
96 }
97
98 /**
99 * Set the color matrix which will be applied to each cell of the image.
100 * This will set the alpha channel to be a copy.
101 *
102 * @param m The 3x3 matrix to set.
103 */
104 public void setColorMatrix(Matrix3f m) {
105 mMatrix.load(m);
106 setMatrix();
107 }
108
109 /**
Jason Samsd9792082013-07-26 20:24:34 -0700110 * Set the value to be added after the color matrix has been
111 * applied. The default value is {0, 0, 0, 0}
112 *
113 * @param f The float4 value to be added.
114 */
115 public void setAdd(Float4 f) {
116 mAdd.x = f.x;
117 mAdd.y = f.y;
118 mAdd.z = f.z;
119 mAdd.w = f.w;
120
121 FieldPacker fp = new FieldPacker(4*4);
122 fp.addF32(f.x);
123 fp.addF32(f.y);
124 fp.addF32(f.z);
125 fp.addF32(f.w);
126 setVar(1, fp);
127 }
128
129 /**
130 * Set the value to be added after the color matrix has been
131 * applied. The default value is {0, 0, 0, 0}
132 *
133 * @param r The red add value.
134 * @param g The green add value.
135 * @param b The blue add value.
136 * @param a The alpha add value.
137 */
138 public void setAdd(float r, float g, float b, float a) {
139 mAdd.x = r;
140 mAdd.y = g;
141 mAdd.z = b;
142 mAdd.w = a;
143
144 FieldPacker fp = new FieldPacker(4*4);
145 fp.addF32(mAdd.x);
146 fp.addF32(mAdd.y);
147 fp.addF32(mAdd.z);
148 fp.addF32(mAdd.w);
149 setVar(1, fp);
150 }
151
152 /**
Jason Sams17fec322012-09-07 15:42:55 -0700153 * Set a color matrix to convert from RGB to luminance. The alpha channel
Jason Sams8fd58532012-09-05 15:30:18 -0700154 * will be a copy.
155 *
156 */
157 public void setGreyscale() {
158 mMatrix.loadIdentity();
159 mMatrix.set(0, 0, 0.299f);
160 mMatrix.set(1, 0, 0.587f);
161 mMatrix.set(2, 0, 0.114f);
162 mMatrix.set(0, 1, 0.299f);
163 mMatrix.set(1, 1, 0.587f);
164 mMatrix.set(2, 1, 0.114f);
165 mMatrix.set(0, 2, 0.299f);
166 mMatrix.set(1, 2, 0.587f);
167 mMatrix.set(2, 2, 0.114f);
168 setMatrix();
169 }
170
171 /**
172 * Set the matrix to convert from YUV to RGB with a direct copy of the 4th
173 * channel.
174 *
175 */
176 public void setYUVtoRGB() {
177 mMatrix.loadIdentity();
178 mMatrix.set(0, 0, 1.f);
179 mMatrix.set(1, 0, 0.f);
180 mMatrix.set(2, 0, 1.13983f);
181 mMatrix.set(0, 1, 1.f);
182 mMatrix.set(1, 1, -0.39465f);
183 mMatrix.set(2, 1, -0.5806f);
184 mMatrix.set(0, 2, 1.f);
185 mMatrix.set(1, 2, 2.03211f);
186 mMatrix.set(2, 2, 0.f);
187 setMatrix();
188 }
189
190 /**
191 * Set the matrix to convert from RGB to YUV with a direct copy of the 4th
192 * channel.
193 *
194 */
195 public void setRGBtoYUV() {
196 mMatrix.loadIdentity();
197 mMatrix.set(0, 0, 0.299f);
198 mMatrix.set(1, 0, 0.587f);
199 mMatrix.set(2, 0, 0.114f);
200 mMatrix.set(0, 1, -0.14713f);
201 mMatrix.set(1, 1, -0.28886f);
202 mMatrix.set(2, 1, 0.436f);
203 mMatrix.set(0, 2, 0.615f);
204 mMatrix.set(1, 2, -0.51499f);
205 mMatrix.set(2, 2, -0.10001f);
206 setMatrix();
207 }
208
209
210 /**
Jason Samsd9792082013-07-26 20:24:34 -0700211 * Invoke the kernel and apply the matrix to each cell of input
212 * {@link Allocation} and copy to the output {@link Allocation}.
213 *
214 * If the vector size of the input is less than four, the
215 * remaining components are treated as zero for the matrix
216 * multiply.
217 *
218 * If the output vector size is less than four, the unused
219 * vector components are discarded.
220 *
Jason Sams8fd58532012-09-05 15:30:18 -0700221 *
222 * @param ain Input allocation
223 * @param aout Output allocation
224 */
Jason Sams5729fcd2012-09-04 19:57:40 -0700225 public void forEach(Allocation ain, Allocation aout) {
Jason Samsd9792082013-07-26 20:24:34 -0700226 if (!ain.getElement().isCompatible(Element.U8(mRS)) &&
227 !ain.getElement().isCompatible(Element.U8_2(mRS)) &&
228 !ain.getElement().isCompatible(Element.U8_3(mRS)) &&
229 !ain.getElement().isCompatible(Element.U8_4(mRS)) &&
230 !ain.getElement().isCompatible(Element.F32(mRS)) &&
231 !ain.getElement().isCompatible(Element.F32_2(mRS)) &&
232 !ain.getElement().isCompatible(Element.F32_3(mRS)) &&
233 !ain.getElement().isCompatible(Element.F32_4(mRS))) {
234
235 throw new RSIllegalArgumentException("Unsuported element type.");
236 }
237
238 if (!aout.getElement().isCompatible(Element.U8(mRS)) &&
239 !aout.getElement().isCompatible(Element.U8_2(mRS)) &&
240 !aout.getElement().isCompatible(Element.U8_3(mRS)) &&
241 !aout.getElement().isCompatible(Element.U8_4(mRS)) &&
242 !aout.getElement().isCompatible(Element.F32(mRS)) &&
243 !aout.getElement().isCompatible(Element.F32_2(mRS)) &&
244 !aout.getElement().isCompatible(Element.F32_3(mRS)) &&
245 !aout.getElement().isCompatible(Element.F32_4(mRS))) {
246
247 throw new RSIllegalArgumentException("Unsuported element type.");
248 }
249
Jason Sams5729fcd2012-09-04 19:57:40 -0700250 forEach(0, ain, aout, null);
251 }
252
Jason Sams08a81582012-09-18 12:32:10 -0700253 /**
254 * Get a KernelID for this intrinsic kernel.
255 *
256 * @return Script.KernelID The KernelID object.
257 */
258 public Script.KernelID getKernelID() {
259 return createKernelID(0, 3, null, null);
260 }
261
Jason Sams5729fcd2012-09-04 19:57:40 -0700262}
263