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
19
20/**
21 * Intrinsic for converting an Android YUV buffer to RGB.
22 *
23 * The input allocation is supplied in NV21 format as a U8
24 * element type. The output is RGBA, the alpha channel will be
25 * set to 255.
26 */
27public class ScriptIntrinsicYuvToRGB extends ScriptIntrinsic {
28    private Allocation mInput;
29    // API level for the intrinsic
30    private static final int INTRINSIC_API_LEVEL = 19;
31
32    ScriptIntrinsicYuvToRGB(long id, RenderScript rs) {
33        super(id, rs);
34    }
35
36    /**
37     * Create an intrinsic for converting YUV to RGB.
38     *
39     * Supported elements types are {@link Element#U8_4}
40     *
41     * @param rs The RenderScript context
42     * @param e Element type for output
43     *
44     * @return ScriptIntrinsicYuvToRGB
45     */
46    public static ScriptIntrinsicYuvToRGB create(RenderScript rs, Element e) {
47        // 6 comes from RS_SCRIPT_INTRINSIC_YUV_TO_RGB in rsDefines.h
48        long id;
49        boolean mUseIncSupp = rs.isUseNative() &&
50                              android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
51
52        id = rs.nScriptIntrinsicCreate(6, e.getID(rs), mUseIncSupp);
53
54        ScriptIntrinsicYuvToRGB si = new ScriptIntrinsicYuvToRGB(id, rs);
55        si.setIncSupp(mUseIncSupp);
56        return si;
57    }
58
59
60    /**
61     * Set the input yuv allocation, must be {@link Element#U8}.
62     *
63     * @param ain The input allocation.
64     */
65    public void setInput(Allocation ain) {
66        mInput = ain;
67        setVar(0, ain);
68    }
69
70    /**
71     * Convert the image to RGB.
72     *
73     * @param aout Output allocation. Must match creation element
74     *             type.
75     */
76    public void forEach(Allocation aout) {
77        forEach(0, (Allocation) null, aout, null);
78    }
79
80    /**
81     * Get a KernelID for this intrinsic kernel.
82     *
83     * @return Script.KernelID The KernelID object.
84     */
85    public Script.KernelID getKernelID() {
86        return createKernelID(0, 2, null, null);
87    }
88
89    /**
90     * Get a FieldID for the input field of this intrinsic.
91     *
92     * @return Script.FieldID The FieldID object.
93     */
94    public Script.FieldID getFieldID_Input() {
95        return createFieldID(0, null);
96    }
97}
98