blob: e7d8d34c17e66388d30a4fec5c2c4895c5fbccd5 [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
23
24/**
25 * @hide
26 **/
27public class ScriptIntrinsicLUT extends ScriptIntrinsic {
28 private Matrix4f mMatrix = new Matrix4f();
29 private Allocation mTables;
30 private byte mCache[] = new byte[1024];
31 private boolean mDirty = true;
32
33 ScriptIntrinsicLUT(int id, RenderScript rs) {
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 }
42 bindAllocation(mTables, 0);
43 }
44
45 /**
46 * Supported elements types are uchar4
47 *
48 * @param rs
49 * @param e
50 *
51 * @return ScriptIntrinsicColorMatrix
52 */
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
69 public void setRed(int index, int value) {
70 validate(index, value);
71 mCache[index] = (byte)value;
72 mDirty = true;
73 }
74
75 public void setGreen(int index, int value) {
76 validate(index, value);
77 mCache[index+256] = (byte)value;
78 mDirty = true;
79 }
80
81 public void setBlue(int index, int value) {
82 validate(index, value);
83 mCache[index+512] = (byte)value;
84 mDirty = true;
85 }
86
87 public void setAlpha(int index, int value) {
88 validate(index, value);
89 mCache[index+768] = (byte)value;
90 mDirty = true;
91 }
92
93
94 /**
95 * Invoke the kernel and apply the matrix to each cell of ain and copy to
96 * aout.
97 *
98 * @param ain Input allocation
99 * @param aout Output allocation
100 */
101 public void forEach(Allocation ain, Allocation aout) {
102 if (mDirty) {
103 mDirty = false;
104 mTables.copyFromUnchecked(mCache);
105 }
106 forEach(0, ain, aout, null);
107 }
108
109}
110