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