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.imagejb;
18
19import java.lang.Math;
20
21import android.renderscript.Allocation;
22import android.renderscript.Element;
23import android.renderscript.RenderScript;
24import android.renderscript.Script;
25import android.renderscript.ScriptC;
26import android.renderscript.Type;
27import android.util.Log;
28import android.widget.SeekBar;
29import android.widget.TextView;
30
31public class Grain extends TestBase {
32    private ScriptC_grain mScript;
33    private Allocation mNoise;
34    private Allocation mNoise2;
35
36
37    public boolean onBar1Setup(SeekBar b, TextView t) {
38        t.setText("Strength");
39        b.setProgress(50);
40        return true;
41    }
42
43    public void onBar1Changed(int progress) {
44        float s = progress / 100.0f;
45        mScript.set_gNoiseStrength(s);
46    }
47
48    public void animateBars(float time) {
49        mScript.set_gNoiseStrength(time % 1.f);
50    }
51
52
53    private int findHighBit(int v) {
54        int bit = 0;
55        while (v > 1) {
56            bit++;
57            v >>= 1;
58        }
59        return bit;
60    }
61
62
63    public void createTest(android.content.res.Resources res) {
64        int width = mInPixelsAllocation.getType().getX();
65        int height = mInPixelsAllocation.getType().getY();
66
67        int noiseW = findHighBit(width);
68        int noiseH = findHighBit(height);
69        if (noiseW > 9) {
70            noiseW = 9;
71        }
72        if (noiseH > 9) {
73            noiseH = 9;
74        }
75        noiseW = 1 << noiseW;
76        noiseH = 1 << noiseH;
77
78        Type.Builder tb = new Type.Builder(mRS, Element.U8(mRS));
79        tb.setX(noiseW);
80        tb.setY(noiseH);
81        mNoise = Allocation.createTyped(mRS, tb.create());
82        mNoise2 = Allocation.createTyped(mRS, tb.create());
83
84        mScript = new ScriptC_grain(mRS);
85        mScript.set_gWMask(noiseW - 1);
86        mScript.set_gHMask(noiseH - 1);
87        mScript.set_gNoiseStrength(0.5f);
88        mScript.set_gBlendSource(mNoise);
89        mScript.set_gNoise(mNoise2);
90    }
91
92    public void runTest() {
93        mScript.forEach_genRand(mNoise);
94        mScript.forEach_blend9(mNoise2);
95        mScript.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
96    }
97}
98
99