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