ScriptIntrinsic3DLUT.java revision 3d9b60c9ae71c4c09df0b4e59c825a5d631e1254
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
17package android.support.v8.renderscript;
18
19import 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 * @hide
29 **/
30public class ScriptIntrinsic3DLUT extends ScriptIntrinsic {
31    private Allocation mLUT;
32    private Element mElement;
33
34    protected ScriptIntrinsic3DLUT(long id, RenderScript rs, Element e) {
35        super(id, rs);
36        mElement = e;
37    }
38
39    /**
40     * Supported elements types are {@link Element#U8_4}
41     *
42     * The defaults tables are identity.
43     *
44     * @param rs The RenderScript context
45     * @param e Element type for intputs and outputs
46     *
47     * @return ScriptIntrinsic3DLUT
48     */
49    public static ScriptIntrinsic3DLUT create(RenderScript rs, Element e) {
50        long id = rs.nScriptIntrinsicCreate(8, e.getID(rs));
51
52        if (!e.isCompatible(Element.U8_4(rs))) {
53            throw new RSIllegalArgumentException("Element must be compatible with uchar4.");
54        }
55
56        return new ScriptIntrinsic3DLUT(id, rs, e);
57    }
58
59    /**
60     * Sets the {@link android.support.v8.renderscript.Allocation} to be used as
61     * the lookup table.
62     *
63     * The lookup table must use the same
64     * {@link android.support.v8.renderscript.Element} as the intrinsic.
65     *
66     */
67
68    public void setLUT(Allocation lut) {
69        final Type t = lut.getType();
70
71        if (t.getZ() == 0) {
72            throw new RSIllegalArgumentException("LUT must be 3d.");
73        }
74
75        if (!t.getElement().isCompatible(mElement)) {
76            throw new RSIllegalArgumentException("LUT element type must match.");
77        }
78
79        mLUT = lut;
80        setVar(0, mLUT);
81    }
82
83
84    /**
85     * Invoke the kernel and apply the lookup to each cell of ain
86     * and copy to aout.
87     *
88     * @param ain Input allocation
89     * @param aout Output allocation
90     */
91    public void forEach(Allocation ain, Allocation aout) {
92        forEach(0, ain, aout, null);
93    }
94
95    /**
96     * Get a KernelID for this intrinsic kernel.
97     *
98     * @return Script.KernelID The KernelID object.
99     */
100    public Script.KernelID getKernelID() {
101        return createKernelID(0, 3, null, null);
102    }
103}
104
105