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