blob: 7d5b09fb41aa5bc3ed458519e0321d18de9a65fe [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}.
Xusong Wang8b4548c2021-01-05 10:09:52 -080024 *
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 Sams3a5b8012012-09-08 22:16:14 -070028 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080029@Deprecated
Jason Sams80d81902012-09-13 17:00:48 -070030public final class ScriptIntrinsicLUT extends ScriptIntrinsic {
31 private final Matrix4f mMatrix = new Matrix4f();
Jason Sams3a5b8012012-09-08 22:16:14 -070032 private Allocation mTables;
Jason Sams80d81902012-09-13 17:00:48 -070033 private final byte mCache[] = new byte[1024];
Jason Sams3a5b8012012-09-08 22:16:14 -070034 private boolean mDirty = true;
35
Tim Murray460a0492013-11-19 12:45:54 -080036 private ScriptIntrinsicLUT(long id, RenderScript rs) {
Jason Sams3a5b8012012-09-08 22:16:14 -070037 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 Hines5b4f8f92012-10-16 13:16:10 -070045 setVar(0, mTables);
Jason Sams3a5b8012012-09-08 22:16:14 -070046 }
47
48 /**
Jason Sams80d81902012-09-13 17:00:48 -070049 * Supported elements types are {@link Element#U8_4}
Jason Sams3a5b8012012-09-08 22:16:14 -070050 *
Jason Sams80d81902012-09-13 17:00:48 -070051 * The defaults tables are identity.
Jason Sams3a5b8012012-09-08 22:16:14 -070052 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070053 * @param rs The RenderScript context
Jason Sams80d81902012-09-13 17:00:48 -070054 * @param e Element type for intputs and outputs
55 *
56 * @return ScriptIntrinsicLUT
Jason Sams3a5b8012012-09-08 22:16:14 -070057 */
58 public static ScriptIntrinsicLUT create(RenderScript rs, Element e) {
Tim Murray460a0492013-11-19 12:45:54 -080059 long id = rs.nScriptIntrinsicCreate(3, e.getID(rs));
Jason Sams3a5b8012012-09-08 22:16:14 -070060 return new ScriptIntrinsicLUT(id, rs);
61
62 }
63
Yang Ni8b8f75a2017-04-28 08:50:33 -070064 public void destroy() {
65 mTables.destroy();
66 super.destroy();
67 }
Jason Sams3a5b8012012-09-08 22:16:14 -070068
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 Sams80d81902012-09-13 17:00:48 -070078 /**
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 Sams3a5b8012012-09-08 22:16:14 -070084 public void setRed(int index, int value) {
85 validate(index, value);
86 mCache[index] = (byte)value;
87 mDirty = true;
88 }
89
Jason Sams80d81902012-09-13 17:00:48 -070090 /**
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 Sams3a5b8012012-09-08 22:16:14 -070096 public void setGreen(int index, int value) {
97 validate(index, value);
98 mCache[index+256] = (byte)value;
99 mDirty = true;
100 }
101
Jason Sams80d81902012-09-13 17:00:48 -0700102 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700108 public void setBlue(int index, int value) {
109 validate(index, value);
110 mCache[index+512] = (byte)value;
111 mDirty = true;
112 }
113
Jason Sams80d81902012-09-13 17:00:48 -0700114 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700120 public void setAlpha(int index, int value) {
121 validate(index, value);
122 mCache[index+768] = (byte)value;
123 mDirty = true;
124 }
125
Jason Sams3a5b8012012-09-08 22:16:14 -0700126 /**
Jason Sams80d81902012-09-13 17:00:48 -0700127 * Invoke the kernel and apply the lookup to each cell of ain
128 * and copy to aout.
Jason Sams3a5b8012012-09-08 22:16:14 -0700129 *
130 * @param ain Input allocation
131 * @param aout Output allocation
132 */
133 public void forEach(Allocation ain, Allocation aout) {
Tim Murray6f842ac2014-01-13 11:47:53 -0800134 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 Sams3a5b8012012-09-08 22:16:14 -0700146 if (mDirty) {
147 mDirty = false;
148 mTables.copyFromUnchecked(mCache);
149 }
Tim Murray6f842ac2014-01-13 11:47:53 -0800150 forEach(0, ain, aout, null, opt);
Jason Sams3a5b8012012-09-08 22:16:14 -0700151 }
152
Jason Sams08a81582012-09-18 12:32:10 -0700153 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700161}
162