blob: 96ec875731ffecc3ced6ff2f602906d2d5305fd5 [file] [log] [blame]
Jason Sams40f1fa62013-01-08 11:52:32 -08001/*
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.renderscript;
18
19import android.util.Log;
20
21/**
22 *
Jason Sams02d56d92013-04-12 16:40:50 -070023 * 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 *
Jason Sams40f1fa62013-01-08 11:52:32 -080028 **/
29public final class ScriptIntrinsic3DLUT extends ScriptIntrinsic {
30 private Allocation mLUT;
31 private Element mElement;
32
Tim Murray7a629fa2013-11-19 12:45:54 -080033 private ScriptIntrinsic3DLUT(long id, RenderScript rs, Element e) {
Jason Sams40f1fa62013-01-08 11:52:32 -080034 super(id, rs);
35 mElement = e;
36 }
37
38 /**
39 * Supported elements types are {@link Element#U8_4}
40 *
41 * The defaults tables are identity.
42 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070043 * @param rs The RenderScript context
Jason Sams40f1fa62013-01-08 11:52:32 -080044 * @param e Element type for intputs and outputs
45 *
46 * @return ScriptIntrinsic3DLUT
47 */
48 public static ScriptIntrinsic3DLUT create(RenderScript rs, Element e) {
Tim Murray7a629fa2013-11-19 12:45:54 -080049 long id = rs.nScriptIntrinsicCreate(8, e.getID(rs));
Jason Sams40f1fa62013-01-08 11:52:32 -080050
51 if (!e.isCompatible(Element.U8_4(rs))) {
Jason Sams1e645d22013-03-08 14:32:43 -080052 throw new RSIllegalArgumentException("Element must be compatible with uchar4.");
Jason Sams40f1fa62013-01-08 11:52:32 -080053 }
54
55 return new ScriptIntrinsic3DLUT(id, rs, e);
56 }
57
Tim Murrayc11e25c2013-04-09 11:01:01 -070058 /**
59 * Sets the {@link android.renderscript.Allocation} to be used as the lookup table.
60 *
61 * The lookup table must use the same {@link android.renderscript.Element} as the intrinsic.
62 *
63 */
64
Jason Sams40f1fa62013-01-08 11:52:32 -080065 public void setLUT(Allocation lut) {
66 final Type t = lut.getType();
67
68 if (t.getZ() == 0) {
69 throw new RSIllegalArgumentException("LUT must be 3d.");
70 }
71
72 if (!t.getElement().isCompatible(mElement)) {
73 throw new RSIllegalArgumentException("LUT element type must match.");
74 }
75
76 mLUT = lut;
77 setVar(0, mLUT);
78 }
79
80
81 /**
82 * Invoke the kernel and apply the lookup to each cell of ain
83 * and copy to aout.
84 *
85 * @param ain Input allocation
86 * @param aout Output allocation
87 */
88 public void forEach(Allocation ain, Allocation aout) {
89 forEach(0, ain, aout, null);
90 }
91
92 /**
93 * Get a KernelID for this intrinsic kernel.
94 *
95 * @return Script.KernelID The KernelID object.
96 */
97 public Script.KernelID getKernelID() {
98 return createKernelID(0, 3, null, null);
99 }
100}
101