1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Wu Zailong, bullet@yeah.net
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
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.
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.
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.
46 #if defined (DOUBLE_SUPPORT)
48 #pragma OPENCL EXTENSION cl_khr_fp64:enable
49 #elif defined (cl_amd_fp64)
50 #pragma OPENCL EXTENSION cl_amd_fp64:enable
57 INTER_TAB_SIZE = 1 << INTER_BITS,
58 INTER_TAB_SIZE2 = INTER_TAB_SIZE * INTER_TAB_SIZE
65 #ifdef BORDER_CONSTANT
66 #define EXTRAPOLATE(v2, v) v = scalar;
67 #elif defined BORDER_REPLICATE
68 #define EXTRAPOLATE(v2, v) \
70 v2 = max(min(v2, (int2)(src_cols - 1, src_rows - 1)), (int2)(0)); \
71 v = convertToWT(src[mad24(v2.y, src_step, v2.x + src_offset)]); \
73 #elif defined BORDER_WRAP
74 #define EXTRAPOLATE(v2, v) \
77 v2.x -= ((v2.x - src_cols + 1) / src_cols) * src_cols; \
78 if (v2.x >= src_cols) \
82 v2.y -= ((v2.y - src_rows + 1) / src_rows) * src_rows; \
83 if( v2.y >= src_rows ) \
85 v = convertToWT(src[mad24(v2.y, src_step, v2.x + src_offset)]); \
87 #elif defined(BORDER_REFLECT) || defined(BORDER_REFLECT_101)
89 #define DELTA int delta = 0
91 #define DELTA int delta = 1
93 #define EXTRAPOLATE(v2, v) \
102 v2.x = -v2.x - 1 + delta; \
104 v2.x = src_cols - 1 - (v2.x - src_cols) - delta; \
106 while (v2.x >= src_cols || v2.x < 0); \
114 v2.y = -v2.y - 1 + delta; \
116 v2.y = src_rows - 1 - (v2.y - src_rows) - delta; \
118 while (v2.y >= src_rows || v2.y < 0); \
119 v = convertToWT(src[mad24(v2.y, src_step, v2.x + src_offset)]); \
122 #error No extrapolation method
125 #define NEED_EXTRAPOLATION(gx, gy) (gx >= src_cols || gy >= src_rows || gx < 0 || gy < 0)
129 __kernel void remap_2_32FC1(__global const T * restrict src, __global T * dst,
130 __global float * map1, __global float * map2,
131 int src_offset, int dst_offset, int map1_offset, int map2_offset,
132 int src_step, int dst_step, int map1_step, int map2_step,
133 int src_cols, int src_rows, int dst_cols, int dst_rows, T scalar)
135 int x = get_global_id(0);
136 int y = get_global_id(1);
138 if (x < dst_cols && y < dst_rows)
140 int dstIdx = mad24(y, dst_step, x + dst_offset);
141 int map1Idx = mad24(y, map1_step, x + map1_offset);
142 int map2Idx = mad24(y, map2_step, x + map2_offset);
144 int gx = convert_int_sat_rte(map1[map1Idx]);
145 int gy = convert_int_sat_rte(map2[map2Idx]);
147 if (NEED_EXTRAPOLATION(gx, gy))
149 #ifndef BORDER_CONSTANT
150 int2 gxy = (int2)(gx, gy);
152 EXTRAPOLATE(gxy, dst[dstIdx]);
156 int srcIdx = mad24(gy, src_step, gx + src_offset);
157 dst[dstIdx] = src[srcIdx];
162 __kernel void remap_32FC2(__global const T * restrict src, __global T * dst, __global float2 * map1,
163 int src_offset, int dst_offset, int map1_offset,
164 int src_step, int dst_step, int map1_step,
165 int src_cols, int src_rows, int dst_cols, int dst_rows, T scalar)
167 int x = get_global_id(0);
168 int y = get_global_id(1);
170 if (x < dst_cols && y < dst_rows)
172 int dstIdx = mad24(y, dst_step, x + dst_offset);
173 int map1Idx = mad24(y, map1_step, x + map1_offset);
175 int2 gxy = convert_int2_sat_rte(map1[map1Idx]);
176 int gx = gxy.x, gy = gxy.y;
178 if (NEED_EXTRAPOLATION(gx, gy))
179 EXTRAPOLATE(gxy, dst[dstIdx])
182 int srcIdx = mad24(gy, src_step, gx + src_offset);
183 dst[dstIdx] = src[srcIdx];
188 __kernel void remap_16SC2(__global const T * restrict src, __global T * dst, __global short2 * map1,
189 int src_offset, int dst_offset, int map1_offset,
190 int src_step, int dst_step, int map1_step,
191 int src_cols, int src_rows, int dst_cols, int dst_rows, T scalar)
193 int x = get_global_id(0);
194 int y = get_global_id(1);
196 if (x < dst_cols && y < dst_rows)
198 int dstIdx = mad24(y, dst_step, x + dst_offset);
199 int map1Idx = mad24(y, map1_step, x + map1_offset);
201 int2 gxy = convert_int2(map1[map1Idx]);
202 int gx = gxy.x, gy = gxy.y;
204 if (NEED_EXTRAPOLATION(gx, gy))
205 EXTRAPOLATE(gxy, dst[dstIdx])
208 int srcIdx = mad24(gy, src_step, gx + src_offset);
209 dst[dstIdx] = src[srcIdx];
214 __kernel void remap_16SC2_16UC1(__global const T * restrict src, __global T * dst, __global short2 * map1, __global ushort * map2,
215 int src_offset, int dst_offset, int map1_offset, int map2_offset,
216 int src_step, int dst_step, int map1_step, int map2_step,
217 int src_cols, int src_rows, int dst_cols, int dst_rows, T scalar)
219 int x = get_global_id(0);
220 int y = get_global_id(1);
222 if (x < dst_cols && y < dst_rows)
224 int dstIdx = mad24(y, dst_step, x + dst_offset);
225 int map1Idx = mad24(y, map1_step, x + map1_offset);
226 int map2Idx = mad24(y, map2_step, x + map2_offset);
228 int map2Value = convert_int(map2[map2Idx]) & (INTER_TAB_SIZE2 - 1);
229 int dx = (map2Value & (INTER_TAB_SIZE - 1)) < (INTER_TAB_SIZE >> 1) ? 1 : 0;
230 int dy = (map2Value >> INTER_BITS) < (INTER_TAB_SIZE >> 1) ? 1 : 0;
231 int2 gxy = convert_int2(map1[map1Idx]) + (int2)(dx, dy);
232 int gx = gxy.x, gy = gxy.y;
234 if (NEED_EXTRAPOLATION(gx, gy))
235 EXTRAPOLATE(gxy, dst[dstIdx])
238 int srcIdx = mad24(gy, src_step, gx + src_offset);
239 dst[dstIdx] = src[srcIdx];
246 __kernel void remap_2_32FC1(__global T const * restrict src, __global T * dst,
247 __global float * map1, __global float * map2,
248 int src_offset, int dst_offset, int map1_offset, int map2_offset,
249 int src_step, int dst_step, int map1_step, int map2_step,
250 int src_cols, int src_rows, int dst_cols, int dst_rows, T nVal)
252 int x = get_global_id(0);
253 int y = get_global_id(1);
255 if (x < dst_cols && y < dst_rows)
257 int dstIdx = mad24(y, dst_step, x + dst_offset);
258 int map1Idx = mad24(y, map1_step, x + map1_offset);
259 int map2Idx = mad24(y, map2_step, x + map2_offset);
261 float2 map_data = (float2)(map1[map1Idx], map2[map2Idx]);
263 int2 map_dataA = convert_int2_sat_rtn(map_data);
264 int2 map_dataB = (int2)(map_dataA.x + 1, map_dataA.y);
265 int2 map_dataC = (int2)(map_dataA.x, map_dataA.y + 1);
266 int2 map_dataD = (int2)(map_dataA.x + 1, map_dataA.y +1);
268 float2 _u = map_data - convert_float2(map_dataA);
269 WT2 u = convertToWT2(convert_int2_rte(convertToWT2(_u) * (WT2)INTER_TAB_SIZE)) / (WT2)INTER_TAB_SIZE;
270 WT scalar = convertToWT(nVal);
271 WT a = scalar, b = scalar, c = scalar, d = scalar;
273 if (!NEED_EXTRAPOLATION(map_dataA.x, map_dataA.y))
274 a = convertToWT(src[mad24(map_dataA.y, src_step, map_dataA.x + src_offset)]);
276 EXTRAPOLATE(map_dataA, a);
278 if (!NEED_EXTRAPOLATION(map_dataB.x, map_dataB.y))
279 b = convertToWT(src[mad24(map_dataB.y, src_step, map_dataB.x + src_offset)]);
281 EXTRAPOLATE(map_dataB, b);
283 if (!NEED_EXTRAPOLATION(map_dataC.x, map_dataC.y))
284 c = convertToWT(src[mad24(map_dataC.y, src_step, map_dataC.x + src_offset)]);
286 EXTRAPOLATE(map_dataC, c);
288 if (!NEED_EXTRAPOLATION(map_dataD.x, map_dataD.y))
289 d = convertToWT(src[mad24(map_dataD.y, src_step, map_dataD.x + src_offset)]);
291 EXTRAPOLATE(map_dataD, d);
293 WT dst_data = a * (WT)(1 - u.x) * (WT)(1 - u.y) +
294 b * (WT)(u.x) * (WT)(1 - u.y) +
295 c * (WT)(1 - u.x) * (WT)(u.y) +
296 d * (WT)(u.x) * (WT)(u.y);
297 dst[dstIdx] = convertToT(dst_data);
301 __kernel void remap_32FC2(__global T const * restrict src, __global T * dst,
302 __global float2 * map1,
303 int src_offset, int dst_offset, int map1_offset,
304 int src_step, int dst_step, int map1_step,
305 int src_cols, int src_rows, int dst_cols, int dst_rows, T nVal)
307 int x = get_global_id(0);
308 int y = get_global_id(1);
310 if (x < dst_cols && y < dst_rows)
312 int dstIdx = mad24(y, dst_step, x + dst_offset);
313 int map1Idx = mad24(y, map1_step, x + map1_offset);
315 float2 map_data = map1[map1Idx];
316 int2 map_dataA = convert_int2_sat_rtn(map_data);
317 int2 map_dataB = (int2)(map_dataA.x + 1, map_dataA.y);
318 int2 map_dataC = (int2)(map_dataA.x, map_dataA.y + 1);
319 int2 map_dataD = (int2)(map_dataA.x + 1, map_dataA.y + 1);
321 float2 _u = map_data - convert_float2(map_dataA);
322 WT2 u = convertToWT2(convert_int2_rte(convertToWT2(_u) * (WT2)INTER_TAB_SIZE)) / (WT2)INTER_TAB_SIZE;
323 WT scalar = convertToWT(nVal);
324 WT a = scalar, b = scalar, c = scalar, d = scalar;
326 if (!NEED_EXTRAPOLATION(map_dataA.x, map_dataA.y))
327 a = convertToWT(src[mad24(map_dataA.y, src_step, map_dataA.x + src_offset)]);
329 EXTRAPOLATE(map_dataA, a);
331 if (!NEED_EXTRAPOLATION(map_dataB.x, map_dataB.y))
332 b = convertToWT(src[mad24(map_dataB.y, src_step, map_dataB.x + src_offset)]);
334 EXTRAPOLATE(map_dataB, b);
336 if (!NEED_EXTRAPOLATION(map_dataC.x, map_dataC.y))
337 c = convertToWT(src[mad24(map_dataC.y, src_step, map_dataC.x + src_offset)]);
339 EXTRAPOLATE(map_dataC, c);
341 if (!NEED_EXTRAPOLATION(map_dataD.x, map_dataD.y))
342 d = convertToWT(src[mad24(map_dataD.y, src_step, map_dataD.x + src_offset)]);
344 EXTRAPOLATE(map_dataD, d);
346 WT dst_data = a * (WT)(1 - u.x) * (WT)(1 - u.y) +
347 b * (WT)(u.x) * (WT)(1 - u.y) +
348 c * (WT)(1 - u.x) * (WT)(u.y) +
349 d * (WT)(u.x) * (WT)(u.y);
350 dst[dstIdx] = convertToT(dst_data);