/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Copyright (C) 2013, OpenCV Foundation, all rights reserved. // Copyright (C) 2014, Itseez Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #ifndef OPENCV_CORE_SATURATE_HPP #define OPENCV_CORE_SATURATE_HPP #include "opencv2/core/cvdef.h" #include #include "opencv2/core/fast_math.hpp" namespace cv { //! @addtogroup core_utils //! @{ /////////////// saturate_cast (used in image & signal processing) /////////////////// /** @brief Template function for accurate conversion from one primitive type to another. The function saturate_cast resembles the standard C++ cast operations, such as static_cast\() and others. It perform an efficient and accurate conversion from one primitive type to another (see the introduction chapter). saturate in the name means that when the input value v is out of the range of the target type, the result is not formed just by taking low bits of the input, but instead the value is clipped. For example: @code uchar a = saturate_cast(-100); // a = 0 (UCHAR_MIN) short b = saturate_cast(33333.33333); // b = 32767 (SHRT_MAX) @endcode Such clipping is done when the target type is unsigned char , signed char , unsigned short or signed short . For 32-bit integers, no clipping is done. When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit), the floating-point value is first rounded to the nearest integer and then clipped if needed (when the target type is 8- or 16-bit). @param v Function parameter. @sa add, subtract, multiply, divide, Mat::convertTo */ template static inline _Tp saturate_cast(uchar v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(schar v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(ushort v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(short v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(unsigned v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(int v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(float v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(double v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(int64 v) { return _Tp(v); } /** @overload */ template static inline _Tp saturate_cast(uint64 v) { return _Tp(v); } template<> inline uchar saturate_cast(schar v) { return (uchar)std::max((int)v, 0); } template<> inline uchar saturate_cast(ushort v) { return (uchar)std::min((unsigned)v, (unsigned)UCHAR_MAX); } template<> inline uchar saturate_cast(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } template<> inline uchar saturate_cast(short v) { return saturate_cast((int)v); } template<> inline uchar saturate_cast(unsigned v) { return (uchar)std::min(v, (unsigned)UCHAR_MAX); } template<> inline uchar saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline uchar saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline uchar saturate_cast(int64 v) { return (uchar)((uint64)v <= (uint64)UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } template<> inline uchar saturate_cast(uint64 v) { return (uchar)std::min(v, (uint64)UCHAR_MAX); } template<> inline schar saturate_cast(uchar v) { return (schar)std::min((int)v, SCHAR_MAX); } template<> inline schar saturate_cast(ushort v) { return (schar)std::min((unsigned)v, (unsigned)SCHAR_MAX); } template<> inline schar saturate_cast(int v) { return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } template<> inline schar saturate_cast(short v) { return saturate_cast((int)v); } template<> inline schar saturate_cast(unsigned v) { return (schar)std::min(v, (unsigned)SCHAR_MAX); } template<> inline schar saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline schar saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline schar saturate_cast(int64 v) { return (schar)((uint64)((int64)v-SCHAR_MIN) <= (uint64)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } template<> inline schar saturate_cast(uint64 v) { return (schar)std::min(v, (uint64)SCHAR_MAX); } template<> inline ushort saturate_cast(schar v) { return (ushort)std::max((int)v, 0); } template<> inline ushort saturate_cast(short v) { return (ushort)std::max((int)v, 0); } template<> inline ushort saturate_cast(int v) { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } template<> inline ushort saturate_cast(unsigned v) { return (ushort)std::min(v, (unsigned)USHRT_MAX); } template<> inline ushort saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline ushort saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline ushort saturate_cast(int64 v) { return (ushort)((uint64)v <= (uint64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } template<> inline ushort saturate_cast(uint64 v) { return (ushort)std::min(v, (uint64)USHRT_MAX); } template<> inline short saturate_cast(ushort v) { return (short)std::min((int)v, SHRT_MAX); } template<> inline short saturate_cast(int v) { return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } template<> inline short saturate_cast(unsigned v) { return (short)std::min(v, (unsigned)SHRT_MAX); } template<> inline short saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline short saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } template<> inline short saturate_cast(int64 v) { return (short)((uint64)((int64)v - SHRT_MIN) <= (uint64)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } template<> inline short saturate_cast(uint64 v) { return (short)std::min(v, (uint64)SHRT_MAX); } template<> inline int saturate_cast(unsigned v) { return (int)std::min(v, (unsigned)INT_MAX); } template<> inline int saturate_cast(int64 v) { return (int)((uint64)(v - INT_MIN) <= (uint64)UINT_MAX ? v : v > 0 ? INT_MAX : INT_MIN); } template<> inline int saturate_cast(uint64 v) { return (int)std::min(v, (uint64)INT_MAX); } template<> inline int saturate_cast(float v) { return cvRound(v); } template<> inline int saturate_cast(double v) { return cvRound(v); } template<> inline unsigned saturate_cast(schar v) { return (unsigned)std::max(v, (schar)0); } template<> inline unsigned saturate_cast(short v) { return (unsigned)std::max(v, (short)0); } template<> inline unsigned saturate_cast(int v) { return (unsigned)std::max(v, (int)0); } template<> inline unsigned saturate_cast(int64 v) { return (unsigned)((uint64)v <= (uint64)UINT_MAX ? v : v > 0 ? UINT_MAX : 0); } template<> inline unsigned saturate_cast(uint64 v) { return (unsigned)std::min(v, (uint64)UINT_MAX); } // we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc. template<> inline unsigned saturate_cast(float v) { return static_cast(cvRound(v)); } template<> inline unsigned saturate_cast(double v) { return static_cast(cvRound(v)); } template<> inline uint64 saturate_cast(schar v) { return (uint64)std::max(v, (schar)0); } template<> inline uint64 saturate_cast(short v) { return (uint64)std::max(v, (short)0); } template<> inline uint64 saturate_cast(int v) { return (uint64)std::max(v, (int)0); } template<> inline uint64 saturate_cast(int64 v) { return (uint64)std::max(v, (int64)0); } template<> inline int64 saturate_cast(uint64 v) { return (int64)std::min(v, (uint64)LLONG_MAX); } /** @overload */ template static inline _Tp saturate_cast(float16_t v) { return saturate_cast<_Tp>((float)v); } // in theory, we could use a LUT for 8u/8s->16f conversion, // but with hardware support for FP32->FP16 conversion the current approach is preferable template<> inline float16_t saturate_cast(uchar v) { return float16_t((float)v); } template<> inline float16_t saturate_cast(schar v) { return float16_t((float)v); } template<> inline float16_t saturate_cast(ushort v) { return float16_t((float)v); } template<> inline float16_t saturate_cast(short v) { return float16_t((float)v); } template<> inline float16_t saturate_cast(unsigned v){ return float16_t((float)v); } template<> inline float16_t saturate_cast(int v) { return float16_t((float)v); } template<> inline float16_t saturate_cast(uint64 v) { return float16_t((float)v); } template<> inline float16_t saturate_cast(int64 v) { return float16_t((float)v); } template<> inline float16_t saturate_cast(float v) { return float16_t(v); } template<> inline float16_t saturate_cast(double v) { return float16_t((float)v); } //! @} } // cv #endif // OPENCV_CORE_SATURATE_HPP