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(int 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        if (rs.isNative) {
51            RenderScriptThunker rst = (RenderScriptThunker) rs;
52            return ScriptIntrinsic3DLUTThunker.create(rs, e);
53        }
54        int id = rs.nScriptIntrinsicCreate(8, e.getID(rs));
55
56        if (!e.isCompatible(Element.U8_4(rs))) {
57            throw new RSIllegalArgumentException("Element must be compatible with uchar4.");
58        }
59
60        return new ScriptIntrinsic3DLUT(id, rs, e);
61    }
62
63    /**
64     * Sets the {@link android.support.v8.renderscript.Allocation} to be used as
65     * the lookup table.
66     *
67     * The lookup table must use the same
68     * {@link android.support.v8.renderscript.Element} as the intrinsic.
69     *
70     */
71
72    public void setLUT(Allocation lut) {
73        final Type t = lut.getType();
74
75        if (t.getZ() == 0) {
76            throw new RSIllegalArgumentException("LUT must be 3d.");
77        }
78
79        if (!t.getElement().isCompatible(mElement)) {
80            throw new RSIllegalArgumentException("LUT element type must match.");
81        }
82
83        mLUT = lut;
84        setVar(0, mLUT);
85    }
86
87
88    /**
89     * Invoke the kernel and apply the lookup to each cell of ain
90     * and copy to aout.
91     *
92     * @param ain Input allocation
93     * @param aout Output allocation
94     */
95    public void forEach(Allocation ain, Allocation aout) {
96        forEach(0, ain, aout, null);
97    }
98
99    /**
100     * Get a KernelID for this intrinsic kernel.
101     *
102     * @return Script.KernelID The KernelID object.
103     */
104    public Script.KernelID getKernelID() {
105        return createKernelID(0, 3, null, null);
106    }
107}
108
109