Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / modules / ocl / src / opencl / arithm_cartToPolar.cl
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Jia Haipeng, jiahaipeng95@gmail.com
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
26 //   * Redistribution's in binary form must reproduce the above copyright notice,
27 //     this list of conditions and the following disclaimer in the documentation
28 //     and/or other materials provided with the distribution.
29 //
30 //   * The name of the copyright holders may not be used to endorse or promote products
31 //     derived from this software without specific prior written permission.
32 //
33 // This software is provided by the copyright holders and contributors as is and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
43 //
44 //M*/
45
46 #ifdef DOUBLE_SUPPORT
47 #ifdef cl_amd_fp64
48 #pragma OPENCL EXTENSION cl_amd_fp64:enable
49 #elif defined (cl_khr_fp64)
50 #pragma OPENCL EXTENSION cl_khr_fp64:enable
51 #endif
52 #define CV_PI M_PI
53 #else
54 #define CV_PI M_PI_F
55 #endif
56
57 __kernel void arithm_cartToPolar_D5 (__global float *src1, int src1_step, int src1_offset,
58                                      __global float *src2, int src2_step, int src2_offset,
59                                      __global float *dst1, int dst1_step, int dst1_offset, // magnitude
60                                      __global float *dst2, int dst2_step, int dst2_offset, // cartToPolar
61                                      int rows, int cols)
62 {
63     int x = get_global_id(0);
64     int y = get_global_id(1);
65
66     if (x < cols && y < rows)
67     {
68         int src1_index = mad24(y, src1_step, x + src1_offset);
69         int src2_index = mad24(y, src2_step, x + src2_offset);
70
71         int dst1_index = mad24(y, dst1_step, x + dst1_offset);
72         int dst2_index = mad24(y, dst2_step, x + dst2_offset);
73
74         float x = src1[src1_index];
75         float y = src2[src2_index];
76
77         float x2 = x * x;
78         float y2 = y * y;
79
80         float magnitude = sqrt(x2 + y2);
81
82         float tmp = y >= 0 ? 0 : CV_PI*2;
83         tmp = x < 0 ? CV_PI : tmp;
84
85         float tmp1 = y >= 0 ? CV_PI*0.5f : CV_PI*1.5f;
86         float cartToPolar = y2 <= x2 ? x*y/(x2 + 0.28f*y2 + FLT_EPSILON) + tmp :
87                                  tmp1 - x*y/(y2 + 0.28f*x2 + FLT_EPSILON);
88
89 #ifdef DEGREE
90         cartToPolar *= (180/CV_PI);
91 #endif
92
93         dst1[dst1_index] = magnitude;
94         dst2[dst2_index] = cartToPolar;
95     }
96 }
97
98 #if defined (DOUBLE_SUPPORT)
99
100 __kernel void arithm_cartToPolar_D6 (__global double *src1, int src1_step, int src1_offset,
101                                      __global double *src2, int src2_step, int src2_offset,
102                                      __global double *dst1, int dst1_step, int dst1_offset,
103                                      __global double *dst2, int dst2_step, int dst2_offset,
104                                      int rows, int cols)
105 {
106     int x = get_global_id(0);
107     int y = get_global_id(1);
108
109     if (x < cols && y < rows)
110     {
111         int src1_index = mad24(y, src1_step, x + src1_offset);
112         int src2_index = mad24(y, src2_step, x + src2_offset);
113
114         int dst1_index = mad24(y, dst1_step, x + dst1_offset);
115         int dst2_index = mad24(y, dst2_step, x + dst2_offset);
116
117         double x = src1[src1_index];
118         double y = src2[src2_index];
119
120         double x2 = x * x;
121         double y2 = y * y;
122
123         double magnitude = sqrt(x2 + y2);
124
125         float tmp = y >= 0 ? 0 : CV_PI*2;
126         tmp = x < 0 ? CV_PI : tmp;
127
128         float tmp1 = y >= 0 ? CV_PI*0.5 : CV_PI*1.5;
129         double cartToPolar = y2 <= x2 ? x*y/(x2 + 0.28f*y2 + DBL_EPSILON)  + tmp :
130                                  tmp1 - x*y/(y2 + 0.28f*x2 + DBL_EPSILON);
131
132 #ifdef DEGREE
133         cartToPolar *= (180/CV_PI);
134 #endif
135
136         dst1[dst1_index] = magnitude;
137         dst2[dst2_index] = cartToPolar;
138     }
139 }
140
141 #endif