Home | History | Annotate | Download | only in detail
      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) 2000-2008, Intel Corporation, all rights reserved.
     14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
     15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
     16 // Third party copyrights are property of their respective owners.
     17 //
     18 // Redistribution and use in source and binary forms, with or without modification,
     19 // are permitted provided that the following conditions are met:
     20 //
     21 //   * Redistribution's of source code must retain the above copyright notice,
     22 //     this list of conditions and the following disclaimer.
     23 //
     24 //   * Redistribution's in binary form must reproduce the above copyright notice,
     25 //     this list of conditions and the following disclaimer in the documentation
     26 //     and/or other materials provided with the distribution.
     27 //
     28 //   * The name of the copyright holders may not be used to endorse or promote products
     29 //     derived from this software without specific prior written permission.
     30 //
     31 // This software is provided by the copyright holders and contributors "as is" and
     32 // any express or implied warranties, including, but not limited to, the implied
     33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     34 // In no event shall the Intel Corporation or contributors be liable for any direct,
     35 // indirect, incidental, special, exemplary, or consequential damages
     36 // (including, but not limited to, procurement of substitute goods or services;
     37 // loss of use, data, or profits; or business interruption) however caused
     38 // and on any theory of liability, whether in contract, strict liability,
     39 // or tort (including negligence or otherwise) arising in any way out of
     40 // the use of this software, even if advised of the possibility of such damage.
     41 //
     42 //M*/
     43 
     44 #pragma once
     45 
     46 #ifndef __OPENCV_CUDEV_GRID_PYR_UP_DETAIL_HPP__
     47 #define __OPENCV_CUDEV_GRID_PYR_UP_DETAIL_HPP__
     48 
     49 #include "../../common.hpp"
     50 #include "../../util/vec_traits.hpp"
     51 #include "../../util/saturate_cast.hpp"
     52 #include "../../util/type_traits.hpp"
     53 #include "../../ptr2d/glob.hpp"
     54 #include "../../ptr2d/traits.hpp"
     55 
     56 namespace cv { namespace cudev {
     57 
     58 namespace pyramids_detail
     59 {
     60     template <class SrcPtr, typename DstType>
     61     __global__ void pyrUp(const SrcPtr src, GlobPtr<DstType> dst, const int src_rows, const int src_cols, const int dst_rows, const int dst_cols)
     62     {
     63         typedef typename PtrTraits<SrcPtr>::value_type src_type;
     64         typedef typename VecTraits<src_type>::elem_type src_elem_type;
     65         typedef typename LargerType<float, src_elem_type>::type work_elem_type;
     66         typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
     67 
     68         const int x = blockIdx.x * blockDim.x + threadIdx.x;
     69         const int y = blockIdx.y * blockDim.y + threadIdx.y;
     70 
     71         __shared__ work_type s_srcPatch[10][10];
     72         __shared__ work_type s_dstPatch[20][16];
     73 
     74         if (threadIdx.x < 10 && threadIdx.y < 10)
     75         {
     76             int srcx = static_cast<int>((blockIdx.x * blockDim.x) / 2 + threadIdx.x) - 1;
     77             int srcy = static_cast<int>((blockIdx.y * blockDim.y) / 2 + threadIdx.y) - 1;
     78 
     79             srcx = ::abs(srcx);
     80             srcx = ::min(src_cols - 1, srcx);
     81 
     82             srcy = ::abs(srcy);
     83             srcy = ::min(src_rows - 1, srcy);
     84 
     85             s_srcPatch[threadIdx.y][threadIdx.x] = saturate_cast<work_type>(src(srcy, srcx));
     86         }
     87 
     88         __syncthreads();
     89 
     90         work_type sum = VecTraits<work_type>::all(0);
     91 
     92         const int evenFlag = static_cast<int>((threadIdx.x & 1) == 0);
     93         const int oddFlag  = static_cast<int>((threadIdx.x & 1) != 0);
     94         const bool eveny = ((threadIdx.y & 1) == 0);
     95         const int tidx = threadIdx.x;
     96 
     97         if (eveny)
     98         {
     99             sum = sum + (evenFlag * 0.0625f) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx - 2) >> 1)];
    100             sum = sum + ( oddFlag * 0.25f  ) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx - 1) >> 1)];
    101             sum = sum + (evenFlag * 0.375f ) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx    ) >> 1)];
    102             sum = sum + ( oddFlag * 0.25f  ) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx + 1) >> 1)];
    103             sum = sum + (evenFlag * 0.0625f) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx + 2) >> 1)];
    104         }
    105 
    106         s_dstPatch[2 + threadIdx.y][threadIdx.x] = sum;
    107 
    108         if (threadIdx.y < 2)
    109         {
    110             sum = VecTraits<work_type>::all(0);
    111 
    112             if (eveny)
    113             {
    114                 sum = sum + (evenFlag * 0.0625f) * s_srcPatch[0][1 + ((tidx - 2) >> 1)];
    115                 sum = sum + ( oddFlag * 0.25f  ) * s_srcPatch[0][1 + ((tidx - 1) >> 1)];
    116                 sum = sum + (evenFlag * 0.375f ) * s_srcPatch[0][1 + ((tidx    ) >> 1)];
    117                 sum = sum + ( oddFlag * 0.25f  ) * s_srcPatch[0][1 + ((tidx + 1) >> 1)];
    118                 sum = sum + (evenFlag * 0.0625f) * s_srcPatch[0][1 + ((tidx + 2) >> 1)];
    119             }
    120 
    121             s_dstPatch[threadIdx.y][threadIdx.x] = sum;
    122         }
    123 
    124         if (threadIdx.y > 13)
    125         {
    126             sum = VecTraits<work_type>::all(0);
    127 
    128             if (eveny)
    129             {
    130                 sum = sum + (evenFlag * 0.0625f) * s_srcPatch[9][1 + ((tidx - 2) >> 1)];
    131                 sum = sum + ( oddFlag * 0.25f  ) * s_srcPatch[9][1 + ((tidx - 1) >> 1)];
    132                 sum = sum + (evenFlag * 0.375f ) * s_srcPatch[9][1 + ((tidx    ) >> 1)];
    133                 sum = sum + ( oddFlag * 0.25f  ) * s_srcPatch[9][1 + ((tidx + 1) >> 1)];
    134                 sum = sum + (evenFlag * 0.0625f) * s_srcPatch[9][1 + ((tidx + 2) >> 1)];
    135             }
    136 
    137             s_dstPatch[4 + threadIdx.y][threadIdx.x] = sum;
    138         }
    139 
    140         __syncthreads();
    141 
    142         sum = VecTraits<work_type>::all(0);
    143 
    144         const int tidy = threadIdx.y;
    145 
    146         sum = sum + 0.0625f * s_dstPatch[2 + tidy - 2][threadIdx.x];
    147         sum = sum + 0.25f   * s_dstPatch[2 + tidy - 1][threadIdx.x];
    148         sum = sum + 0.375f  * s_dstPatch[2 + tidy    ][threadIdx.x];
    149         sum = sum + 0.25f   * s_dstPatch[2 + tidy + 1][threadIdx.x];
    150         sum = sum + 0.0625f * s_dstPatch[2 + tidy + 2][threadIdx.x];
    151 
    152         if (x < dst_cols && y < dst_rows)
    153             dst(y, x) = saturate_cast<DstType>(4.0f * sum);
    154     }
    155 
    156     template <class SrcPtr, typename DstType>
    157     __host__ void pyrUp(const SrcPtr& src, const GlobPtr<DstType>& dst, int src_rows, int src_cols, int dst_rows, int dst_cols, cudaStream_t stream)
    158     {
    159         const dim3 block(16, 16);
    160         const dim3 grid(divUp(dst_cols, block.x), divUp(dst_rows, block.y));
    161 
    162         pyrUp<<<grid, block, 0, stream>>>(src, dst, src_rows, src_cols, dst_rows, dst_cols);
    163         CV_CUDEV_SAFE_CALL( cudaGetLastError() );
    164 
    165         if (stream == 0)
    166             CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
    167     }
    168 }
    169 
    170 }}
    171 
    172 #endif
    173