blob: e45c0fd735b0df798d7132542c618b0d4ab9cd3a [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
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.Log;
22
Jason Sams3a5b8012012-09-08 22:16:14 -070023/**
Jason Sams80d81902012-09-13 17:00:48 -070024 * Intrinsic for applying a per-channel lookup table. Each
25 * channel of the input has an independant lookup table. The
26 * tables are 256 entries in size and can cover the full value
27 * range of {@link Element#U8_4}.
Jason Sams3a5b8012012-09-08 22:16:14 -070028 **/
Jason Sams80d81902012-09-13 17:00:48 -070029public final class ScriptIntrinsicLUT extends ScriptIntrinsic {
30 private final Matrix4f mMatrix = new Matrix4f();
Jason Sams3a5b8012012-09-08 22:16:14 -070031 private Allocation mTables;
Jason Sams80d81902012-09-13 17:00:48 -070032 private final byte mCache[] = new byte[1024];
Jason Sams3a5b8012012-09-08 22:16:14 -070033 private boolean mDirty = true;
34
Jason Sams80d81902012-09-13 17:00:48 -070035 private ScriptIntrinsicLUT(int id, RenderScript rs) {
Jason Sams3a5b8012012-09-08 22:16:14 -070036 super(id, rs);
37 mTables = Allocation.createSized(rs, Element.U8(rs), 1024);
38 for (int ct=0; ct < 256; ct++) {
39 mCache[ct] = (byte)ct;
40 mCache[ct + 256] = (byte)ct;
41 mCache[ct + 512] = (byte)ct;
42 mCache[ct + 768] = (byte)ct;
43 }
44 bindAllocation(mTables, 0);
45 }
46
47 /**
Jason Sams80d81902012-09-13 17:00:48 -070048 * Supported elements types are {@link Element#U8_4}
Jason Sams3a5b8012012-09-08 22:16:14 -070049 *
Jason Sams80d81902012-09-13 17:00:48 -070050 * The defaults tables are identity.
Jason Sams3a5b8012012-09-08 22:16:14 -070051 *
Jason Sams80d81902012-09-13 17:00:48 -070052 * @param rs The Renderscript context
53 * @param e Element type for intputs and outputs
54 *
55 * @return ScriptIntrinsicLUT
Jason Sams3a5b8012012-09-08 22:16:14 -070056 */
57 public static ScriptIntrinsicLUT create(RenderScript rs, Element e) {
58 int id = rs.nScriptIntrinsicCreate(3, e.getID(rs));
59 return new ScriptIntrinsicLUT(id, rs);
60
61 }
62
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 Sams80d81902012-09-13 17:00:48 -070073 /**
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 Sams3a5b8012012-09-08 22:16:14 -070079 public void setRed(int index, int value) {
80 validate(index, value);
81 mCache[index] = (byte)value;
82 mDirty = true;
83 }
84
Jason Sams80d81902012-09-13 17:00:48 -070085 /**
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 Sams3a5b8012012-09-08 22:16:14 -070091 public void setGreen(int index, int value) {
92 validate(index, value);
93 mCache[index+256] = (byte)value;
94 mDirty = true;
95 }
96
Jason Sams80d81902012-09-13 17:00:48 -070097 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700103 public void setBlue(int index, int value) {
104 validate(index, value);
105 mCache[index+512] = (byte)value;
106 mDirty = true;
107 }
108
Jason Sams80d81902012-09-13 17:00:48 -0700109 /**
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 Sams3a5b8012012-09-08 22:16:14 -0700115 public void setAlpha(int index, int value) {
116 validate(index, value);
117 mCache[index+768] = (byte)value;
118 mDirty = true;
119 }
120
121
122 /**
Jason Sams80d81902012-09-13 17:00:48 -0700123 * Invoke the kernel and apply the lookup to each cell of ain
124 * and copy to aout.
Jason Sams3a5b8012012-09-08 22:16:14 -0700125 *
126 * @param ain Input allocation
127 * @param aout Output allocation
128 */
129 public void forEach(Allocation ain, Allocation aout) {
130 if (mDirty) {
131 mDirty = false;
132 mTables.copyFromUnchecked(mCache);
133 }
134 forEach(0, ain, aout, null);
135 }
136
137}
138