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 per-channel lookup table. Each 23 * channel of the input has an independant lookup table. The 24 * tables are 256 entries in size and can cover the full value 25 * range of {@link Element#U8_4}. 26 **/ 27public class ScriptIntrinsicLUT extends ScriptIntrinsic { 28 private final Matrix4f mMatrix = new Matrix4f(); 29 private Allocation mTables; 30 private final byte mCache[] = new byte[1024]; 31 private boolean mDirty = true; 32 33 protected ScriptIntrinsicLUT(int id, RenderScript rs) { 34 super(id, rs); 35 } 36 37 /** 38 * Supported elements types are {@link Element#U8_4} 39 * 40 * The defaults tables are identity. 41 * 42 * @param rs The RenderScript context 43 * @param e Element type for intputs and outputs 44 * 45 * @return ScriptIntrinsicLUT 46 */ 47 public static ScriptIntrinsicLUT create(RenderScript rs, Element e) { 48 if (rs.isNative) { 49 RenderScriptThunker rst = (RenderScriptThunker) rs; 50 return ScriptIntrinsicLUTThunker.create(rs, e); 51 } 52 53 int id = rs.nScriptIntrinsicCreate(3, e.getID(rs)); 54 55 ScriptIntrinsicLUT si = new ScriptIntrinsicLUT(id, rs); 56 si.mTables = Allocation.createSized(rs, Element.U8(rs), 1024); 57 for (int ct=0; ct < 256; ct++) { 58 si.mCache[ct] = (byte)ct; 59 si.mCache[ct + 256] = (byte)ct; 60 si.mCache[ct + 512] = (byte)ct; 61 si.mCache[ct + 768] = (byte)ct; 62 } 63 si.setVar(0, si.mTables); 64 return si; 65 } 66 67 68 private void validate(int index, int value) { 69 if (index < 0 || index > 255) { 70 throw new RSIllegalArgumentException("Index out of range (0-255)."); 71 } 72 if (value < 0 || value > 255) { 73 throw new RSIllegalArgumentException("Value out of range (0-255)."); 74 } 75 } 76 77 /** 78 * Set an entry in the red channel lookup table 79 * 80 * @param index Must be 0-255 81 * @param value Must be 0-255 82 */ 83 public void setRed(int index, int value) { 84 validate(index, value); 85 mCache[index] = (byte)value; 86 mDirty = true; 87 } 88 89 /** 90 * Set an entry in the green channel lookup table 91 * 92 * @param index Must be 0-255 93 * @param value Must be 0-255 94 */ 95 public void setGreen(int index, int value) { 96 validate(index, value); 97 mCache[index+256] = (byte)value; 98 mDirty = true; 99 } 100 101 /** 102 * Set an entry in the blue channel lookup table 103 * 104 * @param index Must be 0-255 105 * @param value Must be 0-255 106 */ 107 public void setBlue(int index, int value) { 108 validate(index, value); 109 mCache[index+512] = (byte)value; 110 mDirty = true; 111 } 112 113 /** 114 * Set an entry in the alpha channel lookup table 115 * 116 * @param index Must be 0-255 117 * @param value Must be 0-255 118 */ 119 public void setAlpha(int index, int value) { 120 validate(index, value); 121 mCache[index+768] = (byte)value; 122 mDirty = true; 123 } 124 125 126 /** 127 * Invoke the kernel and apply the lookup to each cell of ain 128 * and copy to aout. 129 * 130 * @param ain Input allocation 131 * @param aout Output allocation 132 */ 133 public void forEach(Allocation ain, Allocation aout) { 134 if (mDirty) { 135 mDirty = false; 136 mTables.copyFromUnchecked(mCache); 137 } 138 forEach(0, ain, aout, null); 139 } 140 141 /** 142 * Get a KernelID for this intrinsic kernel. 143 * 144 * @return Script.KernelID The KernelID object. 145 */ 146 public Script.KernelID getKernelID() { 147 return createKernelID(0, 3, null, null); 148 } 149} 150 151