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.RenderScript;
24import android.renderscript.Matrix3f;
25import android.renderscript.Script;
26import android.renderscript.ScriptC;
27import android.renderscript.Type;
28import android.util.Log;
29import android.widget.SeekBar;
30import android.widget.TextView;
31
32
33public class LevelsV4 extends TestBase {
34    private ScriptC_levels_relaxed mScriptR;
35    private ScriptC_levels_full mScriptF;
36    private float mInBlack = 0.0f;
37    private float mOutBlack = 0.0f;
38    private float mInWhite = 255.0f;
39    private float mOutWhite = 255.0f;
40    private float mSaturation = 1.0f;
41
42    Matrix3f satMatrix = new Matrix3f();
43    float mInWMinInB;
44    float mOutWMinOutB;
45    float mOverInWMinInB;
46
47    boolean mUseFull;
48    boolean mUseV4;
49
50    LevelsV4(boolean useFull, boolean useV4) {
51        mUseFull = useFull;
52        mUseV4 = useV4;
53    }
54
55
56    private void setLevels() {
57        mInWMinInB = mInWhite - mInBlack;
58        mOutWMinOutB = mOutWhite - mOutBlack;
59        mOverInWMinInB = 1.f / mInWMinInB;
60
61        mScriptR.set_inBlack(mInBlack);
62        mScriptR.set_outBlack(mOutBlack);
63        mScriptR.set_inWMinInB(mInWMinInB);
64        mScriptR.set_outWMinOutB(mOutWMinOutB);
65        mScriptR.set_overInWMinInB(mOverInWMinInB);
66        mScriptF.set_inBlack(mInBlack);
67        mScriptF.set_outBlack(mOutBlack);
68        mScriptF.set_inWMinInB(mInWMinInB);
69        mScriptF.set_outWMinOutB(mOutWMinOutB);
70        mScriptF.set_overInWMinInB(mOverInWMinInB);
71    }
72
73    private void setSaturation() {
74        float rWeight = 0.299f;
75        float gWeight = 0.587f;
76        float bWeight = 0.114f;
77        float oneMinusS = 1.0f - mSaturation;
78
79        satMatrix.set(0, 0, oneMinusS * rWeight + mSaturation);
80        satMatrix.set(0, 1, oneMinusS * rWeight);
81        satMatrix.set(0, 2, oneMinusS * rWeight);
82        satMatrix.set(1, 0, oneMinusS * gWeight);
83        satMatrix.set(1, 1, oneMinusS * gWeight + mSaturation);
84        satMatrix.set(1, 2, oneMinusS * gWeight);
85        satMatrix.set(2, 0, oneMinusS * bWeight);
86        satMatrix.set(2, 1, oneMinusS * bWeight);
87        satMatrix.set(2, 2, oneMinusS * bWeight + mSaturation);
88        mScriptR.set_colorMat(satMatrix);
89        mScriptF.set_colorMat(satMatrix);
90    }
91
92    public boolean onBar1Setup(SeekBar b, TextView t) {
93        b.setProgress(50);
94        t.setText("Saturation");
95        return true;
96    }
97    public boolean onBar2Setup(SeekBar b, TextView t) {
98        b.setMax(128);
99        b.setProgress(0);
100        t.setText("In Black");
101        return true;
102    }
103    public boolean onBar3Setup(SeekBar b, TextView t) {
104        b.setMax(128);
105        b.setProgress(0);
106        t.setText("Out Black");
107        return true;
108    }
109    public boolean onBar4Setup(SeekBar b, TextView t) {
110        b.setMax(128);
111        b.setProgress(128);
112        t.setText("In White");
113        return true;
114    }
115    public boolean onBar5Setup(SeekBar b, TextView t) {
116        b.setMax(128);
117        b.setProgress(128);
118        t.setText("Out White");
119        return true;
120    }
121
122    public void onBar1Changed(int progress) {
123        mSaturation = (float)progress / 50.0f;
124        setSaturation();
125    }
126    public void onBar2Changed(int progress) {
127        mInBlack = (float)progress;
128        setLevels();
129    }
130    public void onBar3Changed(int progress) {
131        mOutBlack = (float)progress;
132        setLevels();
133    }
134    public void onBar4Changed(int progress) {
135        mInWhite = (float)progress + 127.0f;
136        setLevels();
137    }
138    public void onBar5Changed(int progress) {
139        mOutWhite = (float)progress + 127.0f;
140        setLevels();
141    }
142
143    public void createTest(android.content.res.Resources res) {
144        mScriptR = new ScriptC_levels_relaxed(mRS);
145        mScriptF = new ScriptC_levels_full(mRS);
146        setSaturation();
147        setLevels();
148    }
149
150    public void runTest() {
151        if (mUseFull) {
152            if (mUseV4) {
153                mScriptF.forEach_root4(mInPixelsAllocation, mOutPixelsAllocation);
154            } else {
155                mScriptF.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
156            }
157        } else {
158            if (mUseV4) {
159                mScriptR.forEach_root4(mInPixelsAllocation, mOutPixelsAllocation);
160            } else {
161                mScriptR.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
162            }
163        }
164    }
165
166}
167
168