blob: 98f3da454722c7a8e6d4280e949f9a6949ebfdc3 [file] [log] [blame]
Jason Sams3a5b8012012-09-08 22:16:14 -07001/*
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
17package android.renderscript;
18
Jason Sams3a5b8012012-09-08 22:16:14 -070019/**
Jason Sams80d81902012-09-13 17:00:48 -070020 * 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 Sams3a5b8012012-09-08 22:16:14 -070024 **/
Jason Sams80d81902012-09-13 17:00:48 -070025public final class ScriptIntrinsicLUT extends ScriptIntrinsic {
26 private final Matrix4f mMatrix = new Matrix4f();
Jason Sams3a5b8012012-09-08 22:16:14 -070027 private Allocation mTables;
Jason Sams80d81902012-09-13 17:00:48 -070028 private final byte mCache[] = new byte[1024];
Jason Sams3a5b8012012-09-08 22:16:14 -070029 private boolean mDirty = true;
30
Jason Sams80d81902012-09-13 17:00:48 -070031 private ScriptIntrinsicLUT(int id, RenderScript rs) {
Jason Sams3a5b8012012-09-08 22:16:14 -070032 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 Hines5b4f8f92012-10-16 13:16:10 -070040 setVar(0, mTables);
Jason Sams3a5b8012012-09-08 22:16:14 -070041 }
42
43 /**
Jason Sams80d81902012-09-13 17:00:48 -070044 * Supported elements types are {@link Element#U8_4}
Jason Sams3a5b8012012-09-08 22:16:14 -070045 *
Jason Sams80d81902012-09-13 17:00:48 -070046 * The defaults tables are identity.
Jason Sams3a5b8012012-09-08 22:16:14 -070047 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070048 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070049 * @param e Element type for intputs and outputs
50 *
51 * @return ScriptIntrinsicLUT
Jason Sams3a5b8012012-09-08 22:16:14 -070052 */
53 public static ScriptIntrinsicLUT create(RenderScript rs, Element e) {
54 int id = rs.nScriptIntrinsicCreate(3, e.getID(rs));
55 return new ScriptIntrinsicLUT(id, rs);
56
57 }
58
59
60 private void validate(int index, int value) {
61 if (index < 0 || index > 255) {
62 throw new RSIllegalArgumentException("Index out of range (0-255).");
63 }
64 if (value < 0 || value > 255) {
65 throw new RSIllegalArgumentException("Value out of range (0-255).");
66 }
67 }
68
Jason Sams80d81902012-09-13 17:00:48 -070069 /**
70 * Set an entry in the red channel lookup table
71 *
72 * @param index Must be 0-255
73 * @param value Must be 0-255
74 */
Jason Sams3a5b8012012-09-08 22:16:14 -070075 public void setRed(int index, int value) {
76 validate(index, value);
77 mCache[index] = (byte)value;
78 mDirty = true;
79 }
80
Jason Sams80d81902012-09-13 17:00:48 -070081 /**
82 * Set an entry in the green channel lookup table
83 *
84 * @param index Must be 0-255
85 * @param value Must be 0-255
86 */
Jason Sams3a5b8012012-09-08 22:16:14 -070087 public void setGreen(int index, int value) {
88 validate(index, value);
89 mCache[index+256] = (byte)value;
90 mDirty = true;
91 }
92
Jason Sams80d81902012-09-13 17:00:48 -070093 /**
94 * Set an entry in the blue channel lookup table
95 *
96 * @param index Must be 0-255
97 * @param value Must be 0-255
98 */
Jason Sams3a5b8012012-09-08 22:16:14 -070099 public void setBlue(int index, int value) {
100 validate(index, value);
101 mCache[index+512] = (byte)value;
102 mDirty = true;
103 }
104
Jason Sams80d81902012-09-13 17:00:48 -0700105 /**
106 * Set an entry in the alpha channel lookup table
107 *
108 * @param index Must be 0-255
109 * @param value Must be 0-255
110 */
Jason Sams3a5b8012012-09-08 22:16:14 -0700111 public void setAlpha(int index, int value) {
112 validate(index, value);
113 mCache[index+768] = (byte)value;
114 mDirty = true;
115 }
116
117
118 /**
Jason Sams80d81902012-09-13 17:00:48 -0700119 * Invoke the kernel and apply the lookup to each cell of ain
120 * and copy to aout.
Jason Sams3a5b8012012-09-08 22:16:14 -0700121 *
122 * @param ain Input allocation
123 * @param aout Output allocation
124 */
125 public void forEach(Allocation ain, Allocation aout) {
126 if (mDirty) {
127 mDirty = false;
128 mTables.copyFromUnchecked(mCache);
129 }
130 forEach(0, ain, aout, null);
131 }
132
Jason Sams08a81582012-09-18 12:32:10 -0700133 /**
134 * Get a KernelID for this intrinsic kernel.
135 *
136 * @return Script.KernelID The KernelID object.
137 */
138 public Script.KernelID getKernelID() {
139 return createKernelID(0, 3, null, null);
140 }
Jason Sams3a5b8012012-09-08 22:16:14 -0700141}
142