Skip to content

Commit f063fa0

Browse files
Jason SamsAndroid (Google) Code Review
authored andcommitted
Merge "Add fisheye filter to Image Processing benchmark" into jb-mr1-dev
2 parents bad7eed + bb2c947 commit f063fa0

File tree

5 files changed

+221
-1
lines changed

5 files changed

+221
-1
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
17+
package com.android.rs.image;
18+
19+
import android.renderscript.Allocation;
20+
import android.renderscript.Element;
21+
import android.renderscript.Sampler;
22+
import android.renderscript.Type;
23+
import android.widget.SeekBar;
24+
import android.widget.TextView;
25+
26+
public class Fisheye extends TestBase {
27+
private ScriptC_fisheye_full mScript_full = null;
28+
private ScriptC_fisheye_relaxed mScript_relaxed = null;
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 relaxed) {
35+
this.relaxed = relaxed;
36+
}
37+
38+
public boolean onBar1Setup(SeekBar b, TextView t) {
39+
t.setText("Scale");
40+
b.setMax(100);
41+
b.setProgress(25);
42+
return true;
43+
}
44+
public boolean onBar2Setup(SeekBar b, TextView t) {
45+
t.setText("Shift center X");
46+
b.setMax(100);
47+
b.setProgress(50);
48+
return true;
49+
}
50+
public boolean onBar3Setup(SeekBar b, TextView t) {
51+
t.setText("Shift center Y");
52+
b.setMax(100);
53+
b.setProgress(50);
54+
return true;
55+
}
56+
57+
public void onBar1Changed(int progress) {
58+
scale = progress / 50.0f;
59+
do_init();
60+
}
61+
public void onBar2Changed(int progress) {
62+
center_x = progress / 100.0f;
63+
do_init();
64+
}
65+
public void onBar3Changed(int progress) {
66+
center_y = progress / 100.0f;
67+
do_init();
68+
}
69+
70+
private void do_init() {
71+
if (relaxed)
72+
mScript_relaxed.invoke_init_filter(
73+
mInPixelsAllocation.getType().getX(),
74+
mInPixelsAllocation.getType().getY(), center_x, center_y,
75+
scale);
76+
else
77+
mScript_full.invoke_init_filter(
78+
mInPixelsAllocation.getType().getX(),
79+
mInPixelsAllocation.getType().getY(), center_x, center_y,
80+
scale);
81+
}
82+
83+
public void createTest(android.content.res.Resources res) {
84+
if (relaxed) {
85+
mScript_relaxed = new ScriptC_fisheye_relaxed(mRS, res,
86+
R.raw.fisheye_relaxed);
87+
mScript_relaxed.set_in_alloc(mInPixelsAllocation);
88+
mScript_relaxed.set_sampler(Sampler.CLAMP_LINEAR(mRS));
89+
} else {
90+
mScript_full = new ScriptC_fisheye_full(mRS, res,
91+
R.raw.fisheye_full);
92+
mScript_full.set_in_alloc(mInPixelsAllocation);
93+
mScript_full.set_sampler(Sampler.CLAMP_LINEAR(mRS));
94+
}
95+
do_init();
96+
}
97+
98+
public void runTest() {
99+
if (relaxed)
100+
mScript_relaxed.forEach_root(mOutPixelsAllocation);
101+
else
102+
mScript_full.forEach_root(mOutPixelsAllocation);
103+
}
104+
105+
}
106+

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ void changeTest(int testID) {
143143
case 6:
144144
mTest = new Grain();
145145
break;
146+
case 7:
147+
mTest = new Fisheye(false);
148+
break;
149+
case 8:
150+
mTest = new Fisheye(true);
151+
break;
146152
}
147153

148154
mTest.createBaseTest(this, mBitmapIn);
@@ -155,14 +161,16 @@ void changeTest(int testID) {
155161
}
156162

157163
void setupTests() {
158-
mTestNames = new String[7];
164+
mTestNames = new String[9];
159165
mTestNames[0] = "Levels Vec3 Relaxed";
160166
mTestNames[1] = "Levels Vec4 Relaxed";
161167
mTestNames[2] = "Levels Vec3 Full";
162168
mTestNames[3] = "Levels Vec4 Full";
163169
mTestNames[4] = "Blur radius 25";
164170
mTestNames[5] = "Greyscale";
165171
mTestNames[6] = "Grain";
172+
mTestNames[7] = "Fisheye Full";
173+
mTestNames[8] = "Fisheye Relaxed";
166174
mTestSpinner.setAdapter(new ArrayAdapter<String>(
167175
this, R.layout.spinner_layout, mTestNames));
168176
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
17+
rs_allocation in_alloc;
18+
rs_sampler sampler;
19+
20+
static float2 center, dimensions;
21+
static float2 scale;
22+
static float alpha;
23+
static float radius2;
24+
static float factor;
25+
26+
void init_filter(uint32_t dim_x, uint32_t dim_y, float focus_x, float focus_y, float k) {
27+
center.x = focus_x;
28+
center.y = focus_y;
29+
dimensions.x = (float)dim_x;
30+
dimensions.y = (float)dim_y;
31+
32+
alpha = k * 2.0 + 0.75;
33+
float bound2 = 0.25;
34+
if (dim_x > dim_y) {
35+
scale.x = 1.0;
36+
scale.y = dimensions.y / dimensions.x;
37+
bound2 *= (scale.y*scale.y + 1);
38+
} else {
39+
scale.x = dimensions.x / dimensions.y;
40+
scale.y = 1.0;
41+
bound2 *= (scale.x*scale.x + 1);
42+
}
43+
const float bound = sqrt(bound2);
44+
const float radius = 1.15 * bound;
45+
radius2 = radius*radius;
46+
const float max_radian = 0.5f * M_PI - atan(alpha / bound * sqrt(radius2 - bound2));
47+
factor = bound / max_radian;
48+
}
49+
50+
void root(uchar4 *out, uint32_t x, uint32_t y) {
51+
// Convert x and y to floating point coordinates with center as origin
52+
float2 coord;
53+
coord.x = (float)x / dimensions.x;
54+
coord.y = (float)y / dimensions.y;
55+
coord -= center;
56+
const float dist = length(scale * coord);
57+
const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist * dist)) / dist);
58+
const float scalar = radian * factor / dist;
59+
const float2 new_coord = coord * scalar + center;
60+
const float4 fout = rsSample(in_alloc, sampler, new_coord);
61+
*out = rsPackColorTo8888(fout);
62+
}
63+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
17+
#pragma version(1)
18+
#pragma rs java_package_name(com.android.rs.image)
19+
20+
#include "fisheye.rsh"
21+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
17+
#pragma version(1)
18+
#pragma rs java_package_name(com.android.rs.image)
19+
#pragma rs_fp_relaxed
20+
21+
#include "fisheye.rsh"
22+

0 commit comments

Comments
 (0)