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}. |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 24 | **/ |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 25 | public final class ScriptIntrinsicLUT extends ScriptIntrinsic { |
| 26 | private final Matrix4f mMatrix = new Matrix4f(); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 27 | private Allocation mTables; |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 28 | private final byte mCache[] = new byte[1024]; |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 29 | private boolean mDirty = true; |
| 30 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 31 | private ScriptIntrinsicLUT(long id, RenderScript rs) { |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 32 | super(id, rs); |
| 33 | mTables = Allocation.createSized(rs, Element.U8(rs), 1024); |
| 34 | for (int ct=0; ct < 256; ct++) { |
| 35 | mCache[ct] = (byte)ct; |
| 36 | mCache[ct + 256] = (byte)ct; |
| 37 | mCache[ct + 512] = (byte)ct; |
| 38 | mCache[ct + 768] = (byte)ct; |
| 39 | } |
Stephen Hines | 5b4f8f9 | 2012-10-16 13:16:10 -0700 | [diff] [blame] | 40 | setVar(0, mTables); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 44 | * Supported elements types are {@link Element#U8_4} |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 45 | * |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 46 | * The defaults tables are identity. |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 47 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 48 | * @param rs The RenderScript context |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 49 | * @param e Element type for intputs and outputs |
| 50 | * |
| 51 | * @return ScriptIntrinsicLUT |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 52 | */ |
| 53 | public static ScriptIntrinsicLUT create(RenderScript rs, Element e) { |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 54 | long id = rs.nScriptIntrinsicCreate(3, e.getID(rs)); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 55 | return new ScriptIntrinsicLUT(id, rs); |
| 56 | |
| 57 | } |
| 58 | |
Yang Ni | 8b8f75a | 2017-04-28 08:50:33 -0700 | [diff] [blame] | 59 | public void destroy() { |
| 60 | mTables.destroy(); |
| 61 | super.destroy(); |
| 62 | } |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 63 | |
| 64 | private void validate(int index, int value) { |
| 65 | if (index < 0 || index > 255) { |
| 66 | throw new RSIllegalArgumentException("Index out of range (0-255)."); |
| 67 | } |
| 68 | if (value < 0 || value > 255) { |
| 69 | throw new RSIllegalArgumentException("Value out of range (0-255)."); |
| 70 | } |
| 71 | } |
| 72 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Set an entry in the red channel lookup table |
| 75 | * |
| 76 | * @param index Must be 0-255 |
| 77 | * @param value Must be 0-255 |
| 78 | */ |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 79 | public void setRed(int index, int value) { |
| 80 | validate(index, value); |
| 81 | mCache[index] = (byte)value; |
| 82 | mDirty = true; |
| 83 | } |
| 84 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Set an entry in the green channel lookup table |
| 87 | * |
| 88 | * @param index Must be 0-255 |
| 89 | * @param value Must be 0-255 |
| 90 | */ |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 91 | public void setGreen(int index, int value) { |
| 92 | validate(index, value); |
| 93 | mCache[index+256] = (byte)value; |
| 94 | mDirty = true; |
| 95 | } |
| 96 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Set an entry in the blue channel lookup table |
| 99 | * |
| 100 | * @param index Must be 0-255 |
| 101 | * @param value Must be 0-255 |
| 102 | */ |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 103 | public void setBlue(int index, int value) { |
| 104 | validate(index, value); |
| 105 | mCache[index+512] = (byte)value; |
| 106 | mDirty = true; |
| 107 | } |
| 108 | |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Set an entry in the alpha channel lookup table |
| 111 | * |
| 112 | * @param index Must be 0-255 |
| 113 | * @param value Must be 0-255 |
| 114 | */ |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 115 | public void setAlpha(int index, int value) { |
| 116 | validate(index, value); |
| 117 | mCache[index+768] = (byte)value; |
| 118 | mDirty = true; |
| 119 | } |
| 120 | |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 121 | /** |
Jason Sams | 80d8190 | 2012-09-13 17:00:48 -0700 | [diff] [blame] | 122 | * Invoke the kernel and apply the lookup to each cell of ain |
| 123 | * and copy to aout. |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 124 | * |
| 125 | * @param ain Input allocation |
| 126 | * @param aout Output allocation |
| 127 | */ |
| 128 | public void forEach(Allocation ain, Allocation aout) { |
Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 129 | forEach(ain, aout, null); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Invoke the kernel and apply the lookup to each cell of ain |
| 134 | * and copy to aout. |
| 135 | * |
| 136 | * @param ain Input allocation |
| 137 | * @param aout Output allocation |
| 138 | * @param opt Options for clipping |
| 139 | */ |
| 140 | public void forEach(Allocation ain, Allocation aout, Script.LaunchOptions opt) { |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 141 | if (mDirty) { |
| 142 | mDirty = false; |
| 143 | mTables.copyFromUnchecked(mCache); |
| 144 | } |
Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 145 | forEach(0, ain, aout, null, opt); |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Get a KernelID for this intrinsic kernel. |
| 150 | * |
| 151 | * @return Script.KernelID The KernelID object. |
| 152 | */ |
| 153 | public Script.KernelID getKernelID() { |
| 154 | return createKernelID(0, 3, null, null); |
| 155 | } |
Jason Sams | 3a5b801 | 2012-09-08 22:16:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |