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 android.support.v8.renderscript.*;
20import android.widget.SeekBar;
21import android.widget.TextView;
22
23public class Fisheye extends TestBase {
24    private ScriptC_fisheye_full mScript_full = null;
25    private ScriptC_fisheye_relaxed mScript_relaxed = null;
26    private ScriptC_fisheye_approx_full mScript_approx_full = null;
27    private ScriptC_fisheye_approx_relaxed mScript_approx_relaxed = null;
28    private final boolean approx;
29    private final boolean relaxed;
30    private float center_x = 0.5f;
31    private float center_y = 0.5f;
32    private float scale = 0.5f;
33
34    public Fisheye(boolean approx, boolean relaxed) {
35        this.approx = approx;
36        this.relaxed = relaxed;
37    }
38
39    public boolean onBar1Setup(SeekBar b, TextView t) {
40        t.setText("Scale");
41        b.setMax(100);
42        b.setProgress(25);
43        return true;
44    }
45    public boolean onBar2Setup(SeekBar b, TextView t) {
46        t.setText("Shift center X");
47        b.setMax(100);
48        b.setProgress(50);
49        return true;
50    }
51    public boolean onBar3Setup(SeekBar b, TextView t) {
52        t.setText("Shift center Y");
53        b.setMax(100);
54        b.setProgress(50);
55        return true;
56    }
57
58    public void onBar1Changed(int progress) {
59        scale = progress / 50.0f;
60        do_init();
61    }
62    public void onBar2Changed(int progress) {
63        center_x = progress / 100.0f;
64        do_init();
65    }
66    public void onBar3Changed(int progress) {
67        center_y = progress / 100.0f;
68        do_init();
69    }
70
71    public void animateBars(float time) {
72        scale = time % 2.f;
73        do_init();
74    }
75
76    private void do_init() {
77        if (approx) {
78            if (relaxed)
79                mScript_approx_relaxed.invoke_init_filter(
80                        mInPixelsAllocation.getType().getX(),
81                        mInPixelsAllocation.getType().getY(), center_x,
82                        center_y, scale);
83            else
84                mScript_approx_full.invoke_init_filter(
85                        mInPixelsAllocation.getType().getX(),
86                        mInPixelsAllocation.getType().getY(), center_x,
87                        center_y, scale);
88        } else if (relaxed)
89            mScript_relaxed.invoke_init_filter(
90                    mInPixelsAllocation.getType().getX(),
91                    mInPixelsAllocation.getType().getY(), center_x, center_y,
92                    scale);
93        else
94            mScript_full.invoke_init_filter(
95                    mInPixelsAllocation.getType().getX(),
96                    mInPixelsAllocation.getType().getY(), center_x, center_y,
97                    scale);
98    }
99
100    public void createTest(android.content.res.Resources res) {
101        if (approx) {
102            if (relaxed) {
103                mScript_approx_relaxed = new ScriptC_fisheye_approx_relaxed(mRS);
104                mScript_approx_relaxed.set_in_alloc(mInPixelsAllocation);
105                mScript_approx_relaxed.set_sampler(Sampler.CLAMP_LINEAR(mRS));
106            } else {
107                mScript_approx_full = new ScriptC_fisheye_approx_full(mRS);
108                mScript_approx_full.set_in_alloc(mInPixelsAllocation);
109                mScript_approx_full.set_sampler(Sampler.CLAMP_LINEAR(mRS));
110            }
111        } else if (relaxed) {
112            mScript_relaxed = new ScriptC_fisheye_relaxed(mRS);
113            mScript_relaxed.set_in_alloc(mInPixelsAllocation);
114            mScript_relaxed.set_sampler(Sampler.CLAMP_LINEAR(mRS));
115        } else {
116            mScript_full = new ScriptC_fisheye_full(mRS);
117            mScript_full.set_in_alloc(mInPixelsAllocation);
118            mScript_full.set_sampler(Sampler.CLAMP_LINEAR(mRS));
119        }
120        do_init();
121    }
122
123    public void runTest() {
124        if (approx) {
125            if (relaxed)
126                mScript_approx_relaxed.forEach_root(mOutPixelsAllocation);
127            else
128                mScript_approx_full.forEach_root(mOutPixelsAllocation);
129        } else if (relaxed)
130            mScript_relaxed.forEach_root(mOutPixelsAllocation);
131        else
132            mScript_full.forEach_root(mOutPixelsAllocation);
133    }
134
135}
136
137