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