blob: c45c015e82dc7bfb0dba5569c1405ecb7e535343 [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 -070019import android.util.Log;
20
Jason Sams3a5b8012012-09-08 22:16:14 -070021/**
Jason Sams80d81902012-09-13 17:00:48 -070022 * 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 Sams3a5b8012012-09-08 22:16:14 -070026 **/
Jason Sams80d81902012-09-13 17:00:48 -070027public final class ScriptIntrinsicLUT extends ScriptIntrinsic {
28 private final Matrix4f mMatrix = new Matrix4f();
Jason Sams3a5b8012012-09-08 22:16:14 -070029 private Allocation mTables;
Jason Sams80d81902012-09-13 17:00:48 -070030 private final byte mCache[] = new byte[1024];
Jason Sams3a5b8012012-09-08 22:16:14 -070031 private boolean mDirty = true;
32
Tim Murray7a629fa2013-11-19 12:45:54 -080033 private ScriptIntrinsicLUT(long id, RenderScript rs) {
Jason Sams3a5b8012012-09-08 22:16:14 -070034 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 Hines5b4f8f92012-10-16 13:16:10 -070042 setVar(0, mTables);
Jason Sams3a5b8012012-09-08 22:16:14 -070043 }
44
45 /**
Jason Sams80d81902012-09-13 17:00:48 -070046 * Supported elements types are {@link Element#U8_4}
Jason Sams3a5b8012012-09-08 22:16:14 -070047 *
Jason Sams80d81902012-09-13 17:00:48 -070048 * The defaults tables are identity.
Jason Sams3a5b8012012-09-08 22:16:14 -070049 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070050 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070051 * @param e Element type for intputs and outputs
52 *
53 * @return ScriptIntrinsicLUT
Jason Sams3a5b8012012-09-08 22:16:14 -070054 */
55 public static ScriptIntrinsicLUT create(RenderScript rs, Element e) {
Tim Murray7a629fa2013-11-19 12:45:54 -080056 long id = rs.nScriptIntrinsicCreate(3, e.getID(rs));
Jason Sams3a5b8012012-09-08 22:16:14 -070057 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 Sams80d81902012-09-13 17:00:48 -070071 /**
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 Sams3a5b8012012-09-08 22:16:14 -070077 public void setRed(int index, int value) {
78 validate(index, value);
79 mCache[index] = (byte)value;
80 mDirty = true;
81 }
82
Jason Sams80d81902012-09-13 17:00:48 -070083 /**
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 Sams3a5b8012012-09-08 22:16:14 -070089 public void setGreen(int index, int value) {
90 validate(index, value);
91 mCache[index+256] = (byte)value;
92 mDirty = true;
93 }
94
Jason Sams80d81902012-09-13 17:00:48 -070095 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700101 public void setBlue(int index, int value) {
102 validate(index, value);
103 mCache[index+512] = (byte)value;
104 mDirty = true;
105 }
106
Jason Sams80d81902012-09-13 17:00:48 -0700107 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700113 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 Sams80d81902012-09-13 17:00:48 -0700121 * Invoke the kernel and apply the lookup to each cell of ain
122 * and copy to aout.
Jason Sams3a5b8012012-09-08 22:16:14 -0700123 *
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 Sams08a81582012-09-18 12:32:10 -0700135 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700143}
144