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