Skip to content

Commit 4a45de8

Browse files
author
Jason Sams
committed
Add film grain test.
Change-Id: Ic7c7d7f66fbcf9a9e7a031a4c34f9a1372f0a7b7
1 parent 5254181 commit 4a45de8

File tree

3 files changed

+170
-1
lines changed

3 files changed

+170
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 java.lang.Math;
20+
21+
import android.renderscript.Allocation;
22+
import android.renderscript.Element;
23+
import android.renderscript.RenderScript;
24+
import android.renderscript.Script;
25+
import android.renderscript.ScriptC;
26+
import android.renderscript.Type;
27+
import android.util.Log;
28+
import android.widget.SeekBar;
29+
import android.widget.TextView;
30+
31+
public class Grain extends TestBase {
32+
private ScriptC_grain mScript;
33+
private Allocation mNoise;
34+
private Allocation mNoise2;
35+
36+
37+
public boolean onBar1Setup(SeekBar b, TextView t) {
38+
t.setText("Strength");
39+
b.setProgress(50);
40+
return true;
41+
}
42+
43+
public void onBar1Changed(int progress) {
44+
float s = progress / 100.0f;
45+
mScript.set_gNoiseStrength(s);
46+
}
47+
48+
public void createTest(android.content.res.Resources res) {
49+
int width = mInPixelsAllocation.getType().getX();
50+
int height = mInPixelsAllocation.getType().getY();
51+
52+
Type.Builder tb = new Type.Builder(mRS, Element.U8(mRS));
53+
tb.setX(width);
54+
tb.setY(height);
55+
mNoise = Allocation.createTyped(mRS, tb.create());
56+
mNoise2 = Allocation.createTyped(mRS, tb.create());
57+
58+
mScript = new ScriptC_grain(mRS, res, R.raw.grain);
59+
mScript.set_gWidth(width);
60+
mScript.set_gHeight(height);
61+
mScript.set_gNoiseStrength(0.5f);
62+
mScript.set_gBlendSource(mNoise);
63+
mScript.set_gNoise(mNoise2);
64+
}
65+
66+
public void runTest() {
67+
mScript.forEach_genRand(mNoise);
68+
mScript.forEach_blend9(mNoise2);
69+
mScript.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
70+
}
71+
72+
}
73+

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ void changeTest(int testID) {
140140
case 5:
141141
mTest = new Greyscale();
142142
break;
143+
case 6:
144+
mTest = new Grain();
145+
break;
143146
}
144147

145148
mTest.createBaseTest(this, mBitmapIn);
@@ -152,13 +155,14 @@ void changeTest(int testID) {
152155
}
153156

154157
void setupTests() {
155-
mTestNames = new String[6];
158+
mTestNames = new String[7];
156159
mTestNames[0] = "Levels Vec3 Relaxed";
157160
mTestNames[1] = "Levels Vec4 Relaxed";
158161
mTestNames[2] = "Levels Vec3 Full";
159162
mTestNames[3] = "Levels Vec4 Full";
160163
mTestNames[4] = "Blur radius 25";
161164
mTestNames[5] = "Greyscale";
165+
mTestNames[6] = "Grain";
162166
mTestSpinner.setAdapter(new ArrayAdapter<String>(
163167
this, R.layout.spinner_layout, mTestNames));
164168
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
void genRand(uchar *out) {
22+
*out = (uchar)rsRand(0xff);
23+
}
24+
25+
/*
26+
* Convolution matrix of distance 2 with fixed point of 'kShiftBits' bits
27+
* shifted. Thus the sum of this matrix should be 'kShiftValue'. Entries of
28+
* small values are not calculated to gain efficiency.
29+
* The order ot pixels represented in this matrix is:
30+
* 1 2 3
31+
* 4 0 5
32+
* 6 7 8
33+
* and the matrix should be: {230, 56, 114, 56, 114, 114, 56, 114, 56}.
34+
* However, since most of the valus are identical, we only use the first three
35+
* entries and the entries corresponding to the pixels is:
36+
* 1 2 1
37+
* 2 0 2
38+
* 1 2 1
39+
*/
40+
41+
int32_t gWidth;
42+
int32_t gHeight;
43+
44+
rs_allocation gBlendSource;
45+
void blend9(uchar *out, uint32_t x, uint32_t y) {
46+
uint32_t x1 = min(x+1, (uint32_t)gWidth);
47+
uint32_t x2 = max(x-1, (uint32_t)0);
48+
uint32_t y1 = min(y+1, (uint32_t)gHeight);
49+
uint32_t y2 = max(y-1, (uint32_t)0);
50+
51+
uint p00 = 56 * ((uchar *)rsGetElementAt(gBlendSource, x1, y1))[0];
52+
uint p01 = 114 * ((uchar *)rsGetElementAt(gBlendSource, x, y1))[0];
53+
uint p02 = 56 * ((uchar *)rsGetElementAt(gBlendSource, x2, y1))[0];
54+
uint p10 = 114 * ((uchar *)rsGetElementAt(gBlendSource, x1, y))[0];
55+
uint p11 = 230 * ((uchar *)rsGetElementAt(gBlendSource, x, y))[0];
56+
uint p12 = 114 * ((uchar *)rsGetElementAt(gBlendSource, x2, y))[0];
57+
uint p20 = 56 * ((uchar *)rsGetElementAt(gBlendSource, x1, y2))[0];
58+
uint p21 = 114 * ((uchar *)rsGetElementAt(gBlendSource, x, y2))[0];
59+
uint p22 = 56 * ((uchar *)rsGetElementAt(gBlendSource, x2, y2))[0];
60+
61+
p00 += p01;
62+
p02 += p10;
63+
p11 += p12;
64+
p20 += p21;
65+
66+
p22 += p00;
67+
p02 += p11;
68+
69+
p20 += p22;
70+
p20 += p02;
71+
72+
*out = (uchar)(p20 >> 10);
73+
}
74+
75+
float gNoiseStrength;
76+
77+
rs_allocation gNoise;
78+
void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
79+
float4 ip = convert_float4(*in);
80+
float pnoise = (float) ((uchar *)rsGetElementAt(gNoise, x, y))[0];
81+
82+
float energy_level = ip.r + ip.g + ip.b;
83+
float energy_mask = (28.f - sqrt(energy_level)) * 0.03571f;
84+
pnoise = (pnoise - 128.f) * energy_mask;
85+
86+
ip += pnoise * gNoiseStrength;
87+
ip = clamp(ip, 0.f, 255.f);
88+
89+
uchar4 p = convert_uchar4(ip);
90+
p.a = 0xff;
91+
*out = p;
92+
}

0 commit comments

Comments
 (0)