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