include/opencv2/gpu/device/saturate_cast.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_GPU_SATURATE_CAST_HPP__
00044 #define __OPENCV_GPU_SATURATE_CAST_HPP__
00045 
00046 #include "common.hpp"
00047 
00048 namespace cv { namespace gpu { namespace device
00049 {
00050     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(uchar v) { return _Tp(v); }
00051     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(schar v) { return _Tp(v); }
00052     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(ushort v) { return _Tp(v); }
00053     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(short v) { return _Tp(v); }
00054     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(uint v) { return _Tp(v); }
00055     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(int v) { return _Tp(v); }
00056     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(float v) { return _Tp(v); }
00057     template<typename _Tp> __device__ __forceinline__ _Tp saturate_cast(double v) { return _Tp(v); }
00058 
00059     template<> __device__ __forceinline__ uchar saturate_cast<uchar>(schar v)
00060     {
00061         return (uchar) ::max((int)v, 0);
00062     }
00063     template<> __device__ __forceinline__ uchar saturate_cast<uchar>(ushort v)
00064     {
00065         return (uchar) ::min((uint)v, (uint)UCHAR_MAX);
00066     }
00067     template<> __device__ __forceinline__ uchar saturate_cast<uchar>(int v)
00068     {
00069         return (uchar)((uint)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
00070     }
00071     template<> __device__ __forceinline__ uchar saturate_cast<uchar>(uint v)
00072     {
00073         return (uchar) ::min(v, (uint)UCHAR_MAX);
00074     }
00075     template<> __device__ __forceinline__ uchar saturate_cast<uchar>(short v)
00076     {
00077         return saturate_cast<uchar>((uint)v);
00078     }
00079 
00080     template<> __device__ __forceinline__ uchar saturate_cast<uchar>(float v)
00081     {
00082         int iv = __float2int_rn(v);
00083         return saturate_cast<uchar>(iv);
00084     }
00085     template<> __device__ __forceinline__ uchar saturate_cast<uchar>(double v)
00086     {
00087     #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130
00088         int iv = __double2int_rn(v);
00089         return saturate_cast<uchar>(iv);
00090     #else
00091         return saturate_cast<uchar>((float)v);
00092     #endif
00093     }
00094 
00095     template<> __device__ __forceinline__ schar saturate_cast<schar>(uchar v)
00096     {
00097         return (schar) ::min((int)v, SCHAR_MAX);
00098     }
00099     template<> __device__ __forceinline__ schar saturate_cast<schar>(ushort v)
00100     {
00101         return (schar) ::min((uint)v, (uint)SCHAR_MAX);
00102     }
00103     template<> __device__ __forceinline__ schar saturate_cast<schar>(int v)
00104     {
00105         return (schar)((uint)(v-SCHAR_MIN) <= (uint)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
00106     }
00107     template<> __device__ __forceinline__ schar saturate_cast<schar>(short v)
00108     {
00109         return saturate_cast<schar>((int)v);
00110     }
00111     template<> __device__ __forceinline__ schar saturate_cast<schar>(uint v)
00112     {
00113         return (schar) ::min(v, (uint)SCHAR_MAX);
00114     }
00115 
00116     template<> __device__ __forceinline__ schar saturate_cast<schar>(float v)
00117     {
00118         int iv = __float2int_rn(v);
00119         return saturate_cast<schar>(iv);
00120     }
00121     template<> __device__ __forceinline__ schar saturate_cast<schar>(double v)
00122     {
00123     #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130
00124         int iv = __double2int_rn(v);
00125         return saturate_cast<schar>(iv);
00126     #else
00127         return saturate_cast<schar>((float)v);
00128     #endif
00129     }
00130 
00131     template<> __device__ __forceinline__ ushort saturate_cast<ushort>(schar v)
00132     {
00133         return (ushort) ::max((int)v, 0);
00134     }
00135     template<> __device__ __forceinline__ ushort saturate_cast<ushort>(short v)
00136     {
00137         return (ushort) ::max((int)v, 0);
00138     }
00139     template<> __device__ __forceinline__ ushort saturate_cast<ushort>(int v)
00140     {
00141         return (ushort)((uint)v <= (uint)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0);
00142     }
00143     template<> __device__ __forceinline__ ushort saturate_cast<ushort>(uint v)
00144     {
00145         return (ushort) ::min(v, (uint)USHRT_MAX);
00146     }
00147     template<> __device__ __forceinline__ ushort saturate_cast<ushort>(float v)
00148     {
00149         int iv = __float2int_rn(v);
00150         return saturate_cast<ushort>(iv);
00151     }
00152     template<> __device__ __forceinline__ ushort saturate_cast<ushort>(double v)
00153     {
00154     #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130
00155         int iv = __double2int_rn(v);
00156         return saturate_cast<ushort>(iv);
00157     #else
00158         return saturate_cast<ushort>((float)v);
00159     #endif
00160     }
00161 
00162     template<> __device__ __forceinline__ short saturate_cast<short>(ushort v)
00163     {
00164         return (short) ::min((int)v, SHRT_MAX);
00165     }
00166     template<> __device__ __forceinline__ short saturate_cast<short>(int v)
00167     {
00168         return (short)((uint)(v - SHRT_MIN) <= (uint)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
00169     }
00170     template<> __device__ __forceinline__ short saturate_cast<short>(uint v)
00171     {
00172         return (short) ::min(v, (uint)SHRT_MAX);
00173     }
00174     template<> __device__ __forceinline__ short saturate_cast<short>(float v)
00175     {
00176         int iv = __float2int_rn(v);
00177         return saturate_cast<short>(iv);
00178     }
00179     template<> __device__ __forceinline__ short saturate_cast<short>(double v)
00180     {
00181     #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130
00182         int iv = __double2int_rn(v);
00183         return saturate_cast<short>(iv);
00184     #else
00185         return saturate_cast<short>((float)v);
00186     #endif
00187     }
00188 
00189     template<> __device__ __forceinline__ int saturate_cast<int>(float v)
00190     {
00191         return __float2int_rn(v);
00192     }
00193     template<> __device__ __forceinline__ int saturate_cast<int>(double v)
00194     {
00195     #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130
00196         return __double2int_rn(v);
00197     #else
00198         return saturate_cast<int>((float)v);
00199     #endif
00200     }
00201 
00202     template<> __device__ __forceinline__ uint saturate_cast<uint>(float v)
00203     {
00204         return __float2uint_rn(v);
00205     }
00206     template<> __device__ __forceinline__ uint saturate_cast<uint>(double v)
00207     {
00208     #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130
00209         return __double2uint_rn(v);
00210     #else
00211         return saturate_cast<uint>((float)v);
00212     #endif
00213     }
00214 }}}
00215 
00216 #endif /* __OPENCV_GPU_SATURATE_CAST_HPP__ */