| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -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 | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 19 | /** | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 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}. | 
| Xusong Wang | 8b4548c | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 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. | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 28 | **/ | 
| Xusong Wang | 8b4548c | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 29 | @Deprecated | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 30 | public final class ScriptIntrinsicLUT extends ScriptIntrinsic { | 
|  | 31 | private final Matrix4f mMatrix = new Matrix4f(); | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 32 | private Allocation mTables; | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 33 | private final byte mCache[] = new byte[1024]; | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 34 | private boolean mDirty = true; | 
|  | 35 |  | 
| Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 36 | private ScriptIntrinsicLUT(long id, RenderScript rs) { | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 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 | } | 
| Stephen Hines | 5b4f8f9 | 2012-10-16 13:16:10 -0700 | [diff] [blame] | 45 | setVar(0, mTables); | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
|  | 48 | /** | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 49 | * Supported elements types are {@link Element#U8_4} | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 50 | * | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 51 | * The defaults tables are identity. | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 52 | * | 
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 53 | * @param rs The RenderScript context | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 54 | * @param e Element type for intputs and outputs | 
|  | 55 | * | 
|  | 56 | * @return ScriptIntrinsicLUT | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 57 | */ | 
|  | 58 | public static ScriptIntrinsicLUT create(RenderScript rs, Element e) { | 
| Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 59 | long id = rs.nScriptIntrinsicCreate(3, e.getID(rs)); | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 60 | return new ScriptIntrinsicLUT(id, rs); | 
|  | 61 |  | 
|  | 62 | } | 
|  | 63 |  | 
| Yang Ni | 8b8f75a | 2017-04-28 08:50:33 -0700 | [diff] [blame] | 64 | public void destroy() { | 
|  | 65 | mTables.destroy(); | 
|  | 66 | super.destroy(); | 
|  | 67 | } | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 68 |  | 
|  | 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 |  | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 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 | */ | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 84 | public void setRed(int index, int value) { | 
|  | 85 | validate(index, value); | 
|  | 86 | mCache[index] = (byte)value; | 
|  | 87 | mDirty = true; | 
|  | 88 | } | 
|  | 89 |  | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 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 | */ | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 96 | public void setGreen(int index, int value) { | 
|  | 97 | validate(index, value); | 
|  | 98 | mCache[index+256] = (byte)value; | 
|  | 99 | mDirty = true; | 
|  | 100 | } | 
|  | 101 |  | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 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 | */ | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 108 | public void setBlue(int index, int value) { | 
|  | 109 | validate(index, value); | 
|  | 110 | mCache[index+512] = (byte)value; | 
|  | 111 | mDirty = true; | 
|  | 112 | } | 
|  | 113 |  | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 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 | */ | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 120 | public void setAlpha(int index, int value) { | 
|  | 121 | validate(index, value); | 
|  | 122 | mCache[index+768] = (byte)value; | 
|  | 123 | mDirty = true; | 
|  | 124 | } | 
|  | 125 |  | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 126 | /** | 
| Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 127 | * Invoke the kernel and apply the lookup to each cell of ain | 
|  | 128 | * and copy to aout. | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 129 | * | 
|  | 130 | * @param ain Input allocation | 
|  | 131 | * @param aout Output allocation | 
|  | 132 | */ | 
|  | 133 | public void forEach(Allocation ain, Allocation aout) { | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 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 | */ | 
|  | 145 | public void forEach(Allocation ain, Allocation aout, Script.LaunchOptions opt) { | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 146 | if (mDirty) { | 
|  | 147 | mDirty = false; | 
|  | 148 | mTables.copyFromUnchecked(mCache); | 
|  | 149 | } | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 150 | forEach(0, ain, aout, null, opt); | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 153 | /** | 
|  | 154 | * Get a KernelID for this intrinsic kernel. | 
|  | 155 | * | 
|  | 156 | * @return Script.KernelID The KernelID object. | 
|  | 157 | */ | 
|  | 158 | public Script.KernelID getKernelID() { | 
|  | 159 | return createKernelID(0, 3, null, null); | 
|  | 160 | } | 
| Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  |