Skip to content

Commit c1deaa2

Browse files
committed
Vignette filter: precompute more values, simplify math, cleanup code
Change-Id: I29cfc313a5173fd393944dd9af58d0111a3d726a
1 parent 880209e commit c1deaa2

File tree

1 file changed

+23
-28
lines changed
  • tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image

1 file changed

+23
-28
lines changed

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/vignette.rsh

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,43 @@
1414
* limitations under the License.
1515
*/
1616

17-
static float2 scale;
18-
static float2 center, dimensions;
19-
static float range, inv_max_dist, shade, slope;
20-
static float2 overDimensions;
17+
static float2 neg_center, axis_scale, inv_dimensions;
18+
static float sloped_neg_range, sloped_inv_max_dist, shade, opp_shade;
2119

2220
void init_vignette(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y,
2321
float desired_scale, float desired_shade, float desired_slope) {
2422

25-
center.x = center_x;
26-
center.y = center_y;
27-
dimensions.x = (float)dim_x;
28-
dimensions.y = (float)dim_y;
23+
neg_center.x = -center_x;
24+
neg_center.y = -center_y;
25+
inv_dimensions.x = 1.f / (float)dim_x;
26+
inv_dimensions.y = 1.f / (float)dim_y;
27+
28+
axis_scale = (float2)1.f;
29+
if (dim_x > dim_y)
30+
axis_scale.y = (float)dim_y / (float)dim_x;
31+
else
32+
axis_scale.x = (float)dim_x / (float)dim_y;
33+
34+
const float max_dist = 0.5 * length(axis_scale);
35+
sloped_inv_max_dist = desired_slope * 1.f/max_dist;
2936

30-
float max_dist = 0.5;
31-
if (dim_x > dim_y) {
32-
scale.x = 1.0;
33-
scale.y = dimensions.y / dimensions.x;
34-
max_dist *= sqrt(scale.y*scale.y + 1);
35-
} else {
36-
scale.x = dimensions.x / dimensions.y;
37-
scale.y = 1.0;
38-
max_dist *= sqrt(scale.x*scale.x + 1);
39-
}
40-
inv_max_dist = 1.0/max_dist;
4137
// Range needs to be between 1.3 to 0.6. When scale is zero then range is
4238
// 1.3 which means no vignette at all because the luminousity difference is
4339
// less than 1/256. Expect input scale to be between 0.0 and 1.0.
44-
range = 1.3 - 0.7*sqrt(desired_scale);
45-
shade = desired_shade;
46-
slope = desired_slope;
40+
const float neg_range = 0.7*sqrt(desired_scale) - 1.3;
41+
sloped_neg_range = exp(neg_range * desired_slope);
4742

48-
overDimensions = ((float2)1.f) / dimensions;
43+
shade = desired_shade;
44+
opp_shade = 1.f - desired_shade;
4945
}
5046

5147
void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
5248
// Convert x and y to floating point coordinates with center as origin
5349
const float4 fin = convert_float4(*in);
54-
float2 coord = {(float)x, (float)y};
55-
coord *= overDimensions;
56-
coord -= center;
57-
const float dist = length(scale * coord);
58-
const float lumen = shade / (1.0 + exp((dist * inv_max_dist - range) * slope)) + (1.0 - shade);
50+
const float2 inCoord = {(float)x, (float)y};
51+
const float2 coord = mad(inCoord, inv_dimensions, neg_center);
52+
const float sloped_dist_ratio = length(axis_scale * coord) * sloped_inv_max_dist;
53+
const float lumen = opp_shade + shade / ( 1.0 + sloped_neg_range * exp(sloped_dist_ratio) );
5954
float4 fout;
6055
fout.rgb = fin.rgb * lumen;
6156
fout.w = fin.w;

0 commit comments

Comments
 (0)