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