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