Home | History | Annotate | Download | only in opencl
      1 //                           License Agreement
      2 //                For Open Source Computer Vision Library
      3 //
      4 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
      5 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
      6 // Third party copyrights are property of their respective owners.
      7 //
      8 // @Authors
      9 //    Niko Li, newlife20080214 (a] gmail.com
     10 //    Zero Lin, zero.lin (a] amd.com
     11 //    Yao Wang, bitwangyaoyao (a] gmail.com
     12 // Redistribution and use in source and binary forms, with or without modification,
     13 // are permitted provided that the following conditions are met:
     14 //
     15 //   * Redistribution's of source code must retain the above copyright notice,
     16 //     this list of conditions and the following disclaimer.
     17 //
     18 //   * Redistribution's in binary form must reproduce the above copyright notice,
     19 //     this list of conditions and the following disclaimer in the documentation
     20 //     and/or other materials provided with the distribution.
     21 //
     22 //   * The name of the copyright holders may not be used to endorse or promote products
     23 //     derived from this software without specific prior written permission.
     24 //
     25 // This software is provided by the copyright holders and contributors as is and
     26 // any express or implied warranties, including, but not limited to, the implied
     27 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     28 // In no event shall the Intel Corporation or contributors be liable for any direct,
     29 // indirect, incidental, special, exemplary, or consequential damages
     30 // (including, but not limited to, procurement of substitute goods or services;
     31 // loss of use, data, or profits; or business interruption) however caused
     32 // and on any theory of liability, whether in contract, strict liability,
     33 // or tort (including negligence or otherwise) arising in any way out of
     34 // the use of this software, even if advised of the possibility of such damage.
     35 //
     36 //
     37 
     38 #ifdef DOUBLE_SUPPORT
     39 #ifdef cl_amd_fp64
     40 #pragma OPENCL EXTENSION cl_amd_fp64:enable
     41 #elif defined (cl_khr_fp64)
     42 #pragma OPENCL EXTENSION cl_khr_fp64:enable
     43 #endif
     44 #endif
     45 
     46 #define noconvert
     47 
     48 #if cn != 3
     49 #define loadpix(addr) *(__global const T *)(addr)
     50 #define storepix(val, addr)  *(__global T *)(addr) = val
     51 #define TSIZE (int)sizeof(T)
     52 #else
     53 #define loadpix(addr) vload3(0, (__global const T1 *)(addr))
     54 #define storepix(val, addr) vstore3(val, 0, (__global T1 *)(addr))
     55 #define TSIZE ((int)sizeof(T1)*3)
     56 #endif
     57 
     58 #ifdef DEPTH_0
     59 #define MIN_VAL 0
     60 #define MAX_VAL UCHAR_MAX
     61 #elif defined DEPTH_1
     62 #define MIN_VAL SCHAR_MIN
     63 #define MAX_VAL SCHAR_MAX
     64 #elif defined DEPTH_2
     65 #define MIN_VAL 0
     66 #define MAX_VAL USHRT_MAX
     67 #elif defined DEPTH_3
     68 #define MIN_VAL SHRT_MIN
     69 #define MAX_VAL SHRT_MAX
     70 #elif defined DEPTH_4
     71 #define MIN_VAL INT_MIN
     72 #define MAX_VAL INT_MAX
     73 #elif defined DEPTH_5
     74 #define MIN_VAL (-FLT_MAX)
     75 #define MAX_VAL FLT_MAX
     76 #elif defined DEPTH_6
     77 #define MIN_VAL (-DBL_MAX)
     78 #define MAX_VAL DBL_MAX
     79 #endif
     80 
     81 #ifdef OP_ERODE
     82 #define VAL MAX_VAL
     83 #elif defined OP_DILATE
     84 #define VAL MIN_VAL
     85 #else
     86 #error "Unknown operation"
     87 #endif
     88 
     89 #ifdef OP_ERODE
     90 #if defined INTEL_DEVICE && defined DEPTH_0
     91 #define MORPH_OP(A, B) ((A) < (B) ? (A) : (B))
     92 #else
     93 #define MORPH_OP(A, B) min((A), (B))
     94 #endif
     95 #endif
     96 #ifdef OP_DILATE
     97 #define MORPH_OP(A, B) max((A), (B))
     98 #endif
     99 
    100 #define PROCESS(y, x) \
    101     temp = LDS_DAT[mad24(l_y + y, width, l_x + x)]; \
    102     res = MORPH_OP(res, temp);
    103 
    104 // BORDER_CONSTANT:      iiiiii|abcdefgh|iiiiiii
    105 #define ELEM(i, l_edge, r_edge, elem1, elem2) (i) < (l_edge) | (i) >= (r_edge) ? (elem1) : (elem2)
    106 
    107 #if defined OP_GRADIENT || defined OP_TOPHAT || defined OP_BLACKHAT
    108 #define EXTRA_PARAMS , __global const uchar * matptr, int mat_step, int mat_offset
    109 #else
    110 #define EXTRA_PARAMS
    111 #endif
    112 
    113 __kernel void morph(__global const uchar * srcptr, int src_step, int src_offset,
    114                     __global uchar * dstptr, int dst_step, int dst_offset,
    115                     int src_offset_x, int src_offset_y, int cols, int rows,
    116                     int src_whole_cols, int src_whole_rows EXTRA_PARAMS)
    117 {
    118     int gidx = get_global_id(0), gidy = get_global_id(1);
    119     int l_x = get_local_id(0), l_y = get_local_id(1);
    120     int x = get_group_id(0) * LSIZE0, y = get_group_id(1) * LSIZE1;
    121     int start_x = x + src_offset_x - RADIUSX;
    122     int width = mad24(RADIUSX, 2, LSIZE0 + 1);
    123     int start_y = y + src_offset_y - RADIUSY;
    124     int point1 = mad24(l_y, LSIZE0, l_x);
    125     int point2 = point1 + LSIZE0 * LSIZE1;
    126     int tl_x = point1 % width, tl_y = point1 / width;
    127     int tl_x2 = point2 % width, tl_y2 = point2 / width;
    128     int cur_x = start_x + tl_x, cur_y = start_y + tl_y;
    129     int cur_x2 = start_x + tl_x2, cur_y2 = start_y + tl_y2;
    130     int start_addr = mad24(cur_y, src_step, cur_x * TSIZE);
    131     int start_addr2 = mad24(cur_y2, src_step, cur_x2 * TSIZE);
    132 
    133     __local T LDS_DAT[2 * LSIZE1 * LSIZE0];
    134 
    135     // read pixels from src
    136     int end_addr = mad24(src_whole_rows - 1, src_step, src_whole_cols * TSIZE);
    137     start_addr = start_addr < end_addr && start_addr > 0 ? start_addr : 0;
    138     start_addr2 = start_addr2 < end_addr && start_addr2 > 0 ? start_addr2 : 0;
    139 
    140     T temp0 = loadpix(srcptr + start_addr);
    141     T temp1 = loadpix(srcptr + start_addr2);
    142 
    143     // judge if read out of boundary
    144     temp0 = ELEM(cur_x, 0, src_whole_cols, (T)(VAL), temp0);
    145     temp0 = ELEM(cur_y, 0, src_whole_rows, (T)(VAL), temp0);
    146 
    147     temp1 = ELEM(cur_x2, 0, src_whole_cols, (T)(VAL), temp1);
    148     temp1 = ELEM(cur_y2, 0, src_whole_rows, (T)(VAL), temp1);
    149 
    150     LDS_DAT[point1] = temp0;
    151     LDS_DAT[point2] = temp1;
    152     barrier(CLK_LOCAL_MEM_FENCE);
    153 
    154     if (gidx < cols && gidy < rows)
    155     {
    156         T res = (T)(VAL), temp;
    157         PROCESS_ELEMS;
    158 
    159         int dst_index = mad24(gidy, dst_step, mad24(gidx, TSIZE, dst_offset));
    160 
    161 #if defined OP_GRADIENT || defined OP_TOPHAT || defined OP_BLACKHAT
    162         int mat_index =  mad24(gidy, mat_step, mad24(gidx, TSIZE, mat_offset));
    163         T value = loadpix(matptr + mat_index);
    164 
    165 #ifdef OP_GRADIENT
    166         storepix(convertToT(convertToWT(res) - convertToWT(value)), dstptr + dst_index);
    167 #elif defined OP_TOPHAT
    168         storepix(convertToT(convertToWT(value) - convertToWT(res)), dstptr + dst_index);
    169 #elif defined OP_BLACKHAT
    170         storepix(convertToT(convertToWT(res) - convertToWT(value)), dstptr + dst_index);
    171 #endif
    172 #else // erode or dilate
    173         storepix(res, dstptr + dst_index);
    174 #endif
    175     }
    176 }
    177