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  *
23  * Intrinsic for converting RGB to RGBA by using a 3D lookup table.  The
24  * incoming r,g,b values are use as normalized x,y,z coordinates into a 3D
25  * allocation.  The 8 nearest values are sampled and linearly interpolated.  The
26  * result is placed in the output.
27  *
28  **/
29 public class ScriptIntrinsic3DLUT extends ScriptIntrinsic {
30     private Allocation mLUT;
31     private Element mElement;
32     // API level for the intrinsic
33     private static final int INTRINSIC_API_LEVEL = 19;
34 
ScriptIntrinsic3DLUT(long id, RenderScript rs, Element e)35     protected ScriptIntrinsic3DLUT(long id, RenderScript rs, Element e) {
36         super(id, rs);
37         mElement = e;
38     }
39 
40     /**
41      * Supported elements types are {@link Element#U8_4}
42      *
43      * The defaults tables are identity.
44      *
45      * @param rs The RenderScript context
46      * @param e Element type for intputs and outputs
47      *
48      * @return ScriptIntrinsic3DLUT
49      */
create(RenderScript rs, Element e)50     public static ScriptIntrinsic3DLUT create(RenderScript rs, Element e) {
51         if (!e.isCompatible(Element.U8_4(rs))) {
52             throw new RSIllegalArgumentException("Element must be compatible with uchar4.");
53         }
54         long id;
55         boolean mUseIncSupp = rs.isUseNative() &&
56                               android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
57 
58         id = rs.nScriptIntrinsicCreate(8, e.getID(rs), mUseIncSupp);
59 
60         ScriptIntrinsic3DLUT si = new ScriptIntrinsic3DLUT(id, rs, e);
61         si.setIncSupp(mUseIncSupp);
62         return si;
63     }
64 
65     /**
66      * Sets the {@link androidx.renderscript.Allocation} to be used as
67      * the lookup table.
68      *
69      * The lookup table must use the same
70      * {@link androidx.renderscript.Element} as the intrinsic.
71      *
72      */
73 
74     public void setLUT(Allocation lut) {
75         final Type t = lut.getType();
76 
77         if (t.getZ() == 0) {
78             throw new RSIllegalArgumentException("LUT must be 3d.");
79         }
80 
81         if (!t.getElement().isCompatible(mElement)) {
82             throw new RSIllegalArgumentException("LUT element type must match.");
83         }
84 
85         mLUT = lut;
86         setVar(0, mLUT);
87     }
88 
89 
90     /**
91      * Invoke the kernel and apply the lookup to each cell of ain
92      * and copy to aout.
93      *
94      * @param ain Input allocation
95      * @param aout Output allocation
96      */
97     public void forEach(Allocation ain, Allocation aout) {
98         forEach(0, ain, aout, null);
99     }
100 
101     /**
102      * Get a KernelID for this intrinsic kernel.
103      *
104      * @return Script.KernelID The KernelID object.
105      */
106     public Script.KernelID getKernelID() {
107         return createKernelID(0, 3, null, null);
108     }
109 }
110 
111