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