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 com.android.rs.image2;
18
19import java.lang.Math;
20
21import android.support.v8.renderscript.*;
22import android.util.Log;
23
24public class ColorCube extends TestBase {
25    private Allocation mCube;
26    private ScriptC_colorcube mScript;
27    private ScriptIntrinsic3DLUT mIntrinsic;
28    private boolean mUseIntrinsic;
29
30    public ColorCube(boolean useIntrinsic) {
31        mUseIntrinsic = useIntrinsic;
32    }
33
34    private void initCube() {
35        final int sx = 32;
36        final int sy = 32;
37        final int sz = 16;
38
39        Type.Builder tb = new Type.Builder(mRS, Element.U8_4(mRS));
40        tb.setX(sx);
41        tb.setY(sy);
42        tb.setZ(sz);
43        Type t = tb.create();
44        mCube = Allocation.createTyped(mRS, t);
45
46        int dat[] = new int[sx * sy * sz];
47        for (int z = 0; z < sz; z++) {
48            for (int y = 0; y < sy; y++) {
49                for (int x = 0; x < sx; x++ ) {
50                    int v = 0xff000000;
51                    v |= (0xff * x / (sx - 1));
52                    v |= (0xff * y / (sy - 1)) << 8;
53                    v |= (0xff * z / (sz - 1)) << 16;
54                    dat[z*sy*sx + y*sx + x] = v;
55                }
56            }
57        }
58
59        mCube.copyFromUnchecked(dat);
60    }
61
62    public void createTest(android.content.res.Resources res) {
63        mScript = new ScriptC_colorcube(mRS);
64        mIntrinsic = ScriptIntrinsic3DLUT.create(mRS, Element.U8_4(mRS));
65
66        initCube();
67        mScript.invoke_setCube(mCube);
68        mIntrinsic.setLUT(mCube);
69    }
70
71    public void runTest() {
72        if (mUseIntrinsic) {
73            mIntrinsic.forEach(mInPixelsAllocation, mOutPixelsAllocation);
74        } else {
75            mScript.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
76        }
77    }
78
79}
80