1 /*
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 
17 package androidx.renderscript;
18 
19 import android.util.Log;
20 
21 /**
22  * 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}.
26  *
27  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
28  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
29  * guide</a> for the proposed alternatives.
30  **/
31 @Deprecated
32 public class ScriptIntrinsicLUT extends ScriptIntrinsic {
33     private final Matrix4f mMatrix = new Matrix4f();
34     private Allocation mTables;
35     private final byte mCache[] = new byte[1024];
36     private boolean mDirty = true;
37     // API level for the intrinsic
38     private static final int INTRINSIC_API_LEVEL = 19;
39 
ScriptIntrinsicLUT(long id, RenderScript rs)40     protected ScriptIntrinsicLUT(long id, RenderScript rs) {
41         super(id, rs);
42     }
43 
44     /**
45      * Supported elements types are {@link Element#U8_4}
46      *
47      * The defaults tables are identity.
48      *
49      * @param rs The RenderScript context
50      * @param e Element type for intputs and outputs
51      *
52      * @return ScriptIntrinsicLUT
53      */
create(RenderScript rs, Element e)54     public static ScriptIntrinsicLUT create(RenderScript rs, Element e) {
55         long id;
56         boolean mUseIncSupp = rs.isUseNative() &&
57                               android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
58 
59         id = rs.nScriptIntrinsicCreate(3, e.getID(rs), mUseIncSupp);
60 
61         ScriptIntrinsicLUT si = new ScriptIntrinsicLUT(id, rs);
62         si.setIncSupp(mUseIncSupp);
63         si.mTables = Allocation.createSized(rs, Element.U8(rs), 1024);
64         for (int ct=0; ct < 256; ct++) {
65             si.mCache[ct] = (byte)ct;
66             si.mCache[ct + 256] = (byte)ct;
67             si.mCache[ct + 512] = (byte)ct;
68             si.mCache[ct + 768] = (byte)ct;
69         }
70         si.setVar(0, si.mTables);
71         return si;
72     }
73 
74 
75     private void validate(int index, int value) {
76         if (index < 0 || index > 255) {
77             throw new RSIllegalArgumentException("Index out of range (0-255).");
78         }
79         if (value < 0 || value > 255) {
80             throw new RSIllegalArgumentException("Value out of range (0-255).");
81         }
82     }
83 
84     /**
85      * Set an entry in the red channel lookup table
86      *
87      * @param index Must be 0-255
88      * @param value Must be 0-255
89      */
90     public void setRed(int index, int value) {
91         validate(index, value);
92         mCache[index] = (byte)value;
93         mDirty = true;
94     }
95 
96     /**
97      * Set an entry in the green channel lookup table
98      *
99      * @param index Must be 0-255
100      * @param value Must be 0-255
101      */
102     public void setGreen(int index, int value) {
103         validate(index, value);
104         mCache[index+256] = (byte)value;
105         mDirty = true;
106     }
107 
108     /**
109      * Set an entry in the blue channel lookup table
110      *
111      * @param index Must be 0-255
112      * @param value Must be 0-255
113      */
114     public void setBlue(int index, int value) {
115         validate(index, value);
116         mCache[index+512] = (byte)value;
117         mDirty = true;
118     }
119 
120     /**
121      * Set an entry in the alpha channel lookup table
122      *
123      * @param index Must be 0-255
124      * @param value Must be 0-255
125      */
126     public void setAlpha(int index, int value) {
127         validate(index, value);
128         mCache[index+768] = (byte)value;
129         mDirty = true;
130     }
131 
132 
133     /**
134      * Invoke the kernel and apply the lookup to each cell of ain
135      * and copy to aout.
136      *
137      * @param ain Input allocation
138      * @param aout Output allocation
139      */
140     public void forEach(Allocation ain, Allocation aout) {
141         if (mDirty) {
142             mDirty = false;
143             mTables.copyFromUnchecked(mCache);
144         }
145         forEach(0, ain, aout, null);
146     }
147 
148     /**
149      * Get a KernelID for this intrinsic kernel.
150      *
151      * @return Script.KernelID The KernelID object.
152      */
153     public Script.KernelID getKernelID() {
154         return createKernelID(0, 3, null, null);
155     }
156 }
157 
158