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
17static float2 neg_center, axis_scale, inv_dimensions;
18static float sloped_neg_range, sloped_inv_max_dist, shade, opp_shade;
19
20void init_vignette(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y,
21        float desired_scale, float desired_shade, float desired_slope) {
22
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.5f * length(axis_scale);
35    sloped_inv_max_dist = desired_slope * 1.f/max_dist;
36
37    // Range needs to be between 1.3 to 0.6. When scale is zero then range is
38    // 1.3 which means no vignette at all because the luminousity difference is
39    // less than 1/256.  Expect input scale to be between 0.0 and 1.0.
40    const float neg_range = 0.7f*sqrt(desired_scale) - 1.3f;
41    sloped_neg_range = exp(neg_range * desired_slope);
42
43    shade = desired_shade;
44    opp_shade = 1.f - desired_shade;
45}
46
47uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t x, uint32_t y) {
48    // Convert x and y to floating point coordinates with center as origin
49    const float4 fin = convert_float4(in);
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.0f + sloped_neg_range * exp(sloped_dist_ratio) );
54    float4 fout;
55    fout.rgb = fin.rgb * lumen;
56    fout.w = fin.w;
57    return convert_uchar4(fout);
58}
59
60