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