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