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
17rs_allocation in_alloc;
18rs_sampler sampler;
19
20static float2 center, neg_center, inv_dimensions, axis_scale;
21static float alpha, radius2, factor;
22
23void init_filter(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y, float k) {
24    center.x = center_x;
25    center.y = center_y;
26    neg_center = -center;
27    inv_dimensions.x = 1.f / (float)dim_x;
28    inv_dimensions.y = 1.f / (float)dim_y;
29    alpha = k * 2.0f + 0.75f;
30
31    axis_scale = (float2)1.f;
32    if (dim_x > dim_y)
33        axis_scale.y = (float)dim_y / (float)dim_x;
34    else
35        axis_scale.x = (float)dim_x / (float)dim_y;
36
37    const float bound2 = 0.25f * (axis_scale.x*axis_scale.x + axis_scale.y*axis_scale.y);
38    const float bound = sqrt(bound2);
39    const float radius = 1.15f * bound;
40    radius2 = radius*radius;
41    const float max_radian = M_PI_2 - atan(alpha / bound * sqrt(radius2 - bound2));
42    factor = bound / max_radian;
43}
44
45uchar4 __attribute__((kernel)) root(uint32_t x, uint32_t y) {
46    // Convert x and y to floating point coordinates with center as origin
47    const float2 inCoord = {(float)x, (float)y};
48    const float2 coord = mad(inCoord, inv_dimensions, neg_center);
49    const float2 scaledCoord = axis_scale * coord;
50    const float dist2 = scaledCoord.x*scaledCoord.x + scaledCoord.y*scaledCoord.y;
51    const float inv_dist = rsqrt(dist2);
52    const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist2)) * inv_dist);
53    const float scalar = radian * factor * inv_dist;
54    const float2 new_coord = mad(coord, scalar, center);
55    const float4 fout = rsSample(in_alloc, sampler, new_coord);
56    return rsPackColorTo8888(fout);
57}
58
59