| 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 | // Copyright (C) 2014, Itseez Inc., all rights reserved. |
| 17 | // Third party copyrights are property of their respective owners. |
| 18 | // |
| 19 | // Redistribution and use in source and binary forms, with or without modification, |
| 20 | // are permitted provided that the following conditions are met: |
| 21 | // |
| 22 | // * Redistribution's of source code must retain the above copyright notice, |
| 23 | // this list of conditions and the following disclaimer. |
| 24 | // |
| 25 | // * Redistribution's in binary form must reproduce the above copyright notice, |
| 26 | // this list of conditions and the following disclaimer in the documentation |
| 27 | // and/or other materials provided with the distribution. |
| 28 | // |
| 29 | // * The name of the copyright holders may not be used to endorse or promote products |
| 30 | // derived from this software without specific prior written permission. |
| 31 | // |
| 32 | // This software is provided by the copyright holders and contributors "as is" and |
| 33 | // any express or implied warranties, including, but not limited to, the implied |
| 34 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
| 35 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
| 36 | // indirect, incidental, special, exemplary, or consequential damages |
| 37 | // (including, but not limited to, procurement of substitute goods or services; |
| 38 | // loss of use, data, or profits; or business interruption) however caused |
| 39 | // and on any theory of liability, whether in contract, strict liability, |
| 40 | // or tort (including negligence or otherwise) arising in any way out of |
| 41 | // the use of this software, even if advised of the possibility of such damage. |
| 42 | // |
| 43 | //M*/ |
| 44 | |
| 45 | #ifndef OPENCV_CORE_SATURATE_HPP |
| 46 | #define OPENCV_CORE_SATURATE_HPP |
| 47 | |
| 48 | #include "opencv2/core/cvdef.h" |
| 49 | #include <climits> |
| 50 | #include "opencv2/core/fast_math.hpp" |
| 51 | |
| 52 | namespace cv |
| 53 | { |
| 54 | |
| 55 | //! @addtogroup core_utils |
| 56 | //! @{ |
| 57 | |
| 58 | /////////////// saturate_cast (used in image & signal processing) /////////////////// |
| 59 | |
| 60 | /** @brief Template function for accurate conversion from one primitive type to another. |
| 61 | |
| 62 | The function saturate_cast resembles the standard C++ cast operations, such as static_cast\<T\>() |
| 63 | and others. It perform an efficient and accurate conversion from one primitive type to another |
| 64 | (see the introduction chapter). saturate in the name means that when the input value v is out of the |
| 65 | range of the target type, the result is not formed just by taking low bits of the input, but instead |
| 66 | the value is clipped. For example: |
| 67 | @code |
| 68 | uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN) |
| 69 | short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX) |
| 70 | @endcode |
| 71 | Such clipping is done when the target type is unsigned char , signed char , unsigned short or |
| 72 | signed short . For 32-bit integers, no clipping is done. |
| 73 | |
| 74 | When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit), |
| 75 | the floating-point value is first rounded to the nearest integer and then clipped if needed (when |
| 76 | the target type is 8- or 16-bit). |
| 77 | |
| 78 | @param v Function parameter. |
| 79 | @sa add, subtract, multiply, divide, Mat::convertTo |
| 80 | */ |
| 81 | template<typename _Tp> static inline _Tp saturate_cast(uchar v) { return _Tp(v); } |
| 82 | /** @overload */ |
| 83 | template<typename _Tp> static inline _Tp saturate_cast(schar v) { return _Tp(v); } |
| 84 | /** @overload */ |
| 85 | template<typename _Tp> static inline _Tp saturate_cast(ushort v) { return _Tp(v); } |
| 86 | /** @overload */ |
| 87 | template<typename _Tp> static inline _Tp saturate_cast(short v) { return _Tp(v); } |
| 88 | /** @overload */ |
| 89 | template<typename _Tp> static inline _Tp saturate_cast(unsigned v) { return _Tp(v); } |
| 90 | /** @overload */ |
| 91 | template<typename _Tp> static inline _Tp saturate_cast(int v) { return _Tp(v); } |
| 92 | /** @overload */ |
| 93 | template<typename _Tp> static inline _Tp saturate_cast(float v) { return _Tp(v); } |
| 94 | /** @overload */ |
| 95 | template<typename _Tp> static inline _Tp saturate_cast(double v) { return _Tp(v); } |
| 96 | /** @overload */ |
| 97 | template<typename _Tp> static inline _Tp saturate_cast(int64 v) { return _Tp(v); } |
| 98 | /** @overload */ |
| 99 | template<typename _Tp> static inline _Tp saturate_cast(uint64 v) { return _Tp(v); } |
| 100 | |
| 101 | template<> inline uchar saturate_cast<uchar>(schar v) { return (uchar)std::max(a: (int)v, b: 0); } |
| 102 | template<> inline uchar saturate_cast<uchar>(ushort v) { return (uchar)std::min(a: (unsigned)v, b: (unsigned)UCHAR_MAX); } |
| 103 | template<> inline uchar saturate_cast<uchar>(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } |
| 104 | template<> inline uchar saturate_cast<uchar>(short v) { return saturate_cast<uchar>(v: (int)v); } |
| 105 | template<> inline uchar saturate_cast<uchar>(unsigned v) { return (uchar)std::min(a: v, b: (unsigned)UCHAR_MAX); } |
| 106 | template<> inline uchar saturate_cast<uchar>(float v) { int iv = cvRound(value: v); return saturate_cast<uchar>(v: iv); } |
| 107 | template<> inline uchar saturate_cast<uchar>(double v) { int iv = cvRound(value: v); return saturate_cast<uchar>(v: iv); } |
| 108 | template<> inline uchar saturate_cast<uchar>(int64 v) { return (uchar)((uint64)v <= (uint64)UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } |
| 109 | template<> inline uchar saturate_cast<uchar>(uint64 v) { return (uchar)std::min(a: v, b: (uint64)UCHAR_MAX); } |
| 110 | |
| 111 | template<> inline schar saturate_cast<schar>(uchar v) { return (schar)std::min(a: (int)v, SCHAR_MAX); } |
| 112 | template<> inline schar saturate_cast<schar>(ushort v) { return (schar)std::min(a: (unsigned)v, b: (unsigned)SCHAR_MAX); } |
| 113 | template<> inline schar saturate_cast<schar>(int v) { return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } |
| 114 | template<> inline schar saturate_cast<schar>(short v) { return saturate_cast<schar>(v: (int)v); } |
| 115 | template<> inline schar saturate_cast<schar>(unsigned v) { return (schar)std::min(a: v, b: (unsigned)SCHAR_MAX); } |
| 116 | template<> inline schar saturate_cast<schar>(float v) { int iv = cvRound(value: v); return saturate_cast<schar>(v: iv); } |
| 117 | template<> inline schar saturate_cast<schar>(double v) { int iv = cvRound(value: v); return saturate_cast<schar>(v: iv); } |
| 118 | template<> inline schar saturate_cast<schar>(int64 v) { return (schar)((uint64)((int64)v-SCHAR_MIN) <= (uint64)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } |
| 119 | template<> inline schar saturate_cast<schar>(uint64 v) { return (schar)std::min(a: v, b: (uint64)SCHAR_MAX); } |
| 120 | |
| 121 | template<> inline ushort saturate_cast<ushort>(schar v) { return (ushort)std::max(a: (int)v, b: 0); } |
| 122 | template<> inline ushort saturate_cast<ushort>(short v) { return (ushort)std::max(a: (int)v, b: 0); } |
| 123 | template<> inline ushort saturate_cast<ushort>(int v) { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } |
| 124 | template<> inline ushort saturate_cast<ushort>(unsigned v) { return (ushort)std::min(a: v, b: (unsigned)USHRT_MAX); } |
| 125 | template<> inline ushort saturate_cast<ushort>(float v) { int iv = cvRound(value: v); return saturate_cast<ushort>(v: iv); } |
| 126 | template<> inline ushort saturate_cast<ushort>(double v) { int iv = cvRound(value: v); return saturate_cast<ushort>(v: iv); } |
| 127 | template<> inline ushort saturate_cast<ushort>(int64 v) { return (ushort)((uint64)v <= (uint64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } |
| 128 | template<> inline ushort saturate_cast<ushort>(uint64 v) { return (ushort)std::min(a: v, b: (uint64)USHRT_MAX); } |
| 129 | |
| 130 | template<> inline short saturate_cast<short>(ushort v) { return (short)std::min(a: (int)v, SHRT_MAX); } |
| 131 | template<> inline short saturate_cast<short>(int v) { return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } |
| 132 | template<> inline short saturate_cast<short>(unsigned v) { return (short)std::min(a: v, b: (unsigned)SHRT_MAX); } |
| 133 | template<> inline short saturate_cast<short>(float v) { int iv = cvRound(value: v); return saturate_cast<short>(v: iv); } |
| 134 | template<> inline short saturate_cast<short>(double v) { int iv = cvRound(value: v); return saturate_cast<short>(v: iv); } |
| 135 | template<> inline short saturate_cast<short>(int64 v) { return (short)((uint64)((int64)v - SHRT_MIN) <= (uint64)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } |
| 136 | template<> inline short saturate_cast<short>(uint64 v) { return (short)std::min(a: v, b: (uint64)SHRT_MAX); } |
| 137 | |
| 138 | template<> inline int saturate_cast<int>(unsigned v) { return (int)std::min(a: v, b: (unsigned)INT_MAX); } |
| 139 | template<> inline int saturate_cast<int>(int64 v) { return (int)((uint64)(v - INT_MIN) <= (uint64)UINT_MAX ? v : v > 0 ? INT_MAX : INT_MIN); } |
| 140 | template<> inline int saturate_cast<int>(uint64 v) { return (int)std::min(a: v, b: (uint64)INT_MAX); } |
| 141 | template<> inline int saturate_cast<int>(float v) { return cvRound(value: v); } |
| 142 | template<> inline int saturate_cast<int>(double v) { return cvRound(value: v); } |
| 143 | |
| 144 | template<> inline unsigned saturate_cast<unsigned>(schar v) { return (unsigned)std::max(a: v, b: (schar)0); } |
| 145 | template<> inline unsigned saturate_cast<unsigned>(short v) { return (unsigned)std::max(a: v, b: (short)0); } |
| 146 | template<> inline unsigned saturate_cast<unsigned>(int v) { return (unsigned)std::max(a: v, b: (int)0); } |
| 147 | template<> inline unsigned saturate_cast<unsigned>(int64 v) { return (unsigned)((uint64)v <= (uint64)UINT_MAX ? v : v > 0 ? UINT_MAX : 0); } |
| 148 | template<> inline unsigned saturate_cast<unsigned>(uint64 v) { return (unsigned)std::min(a: v, b: (uint64)UINT_MAX); } |
| 149 | // we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc. |
| 150 | template<> inline unsigned saturate_cast<unsigned>(float v) { return static_cast<unsigned>(cvRound(value: v)); } |
| 151 | template<> inline unsigned saturate_cast<unsigned>(double v) { return static_cast<unsigned>(cvRound(value: v)); } |
| 152 | |
| 153 | template<> inline uint64 saturate_cast<uint64>(schar v) { return (uint64)std::max(a: v, b: (schar)0); } |
| 154 | template<> inline uint64 saturate_cast<uint64>(short v) { return (uint64)std::max(a: v, b: (short)0); } |
| 155 | template<> inline uint64 saturate_cast<uint64>(int v) { return (uint64)std::max(a: v, b: (int)0); } |
| 156 | template<> inline uint64 saturate_cast<uint64>(int64 v) { return (uint64)std::max(a: v, b: (int64)0); } |
| 157 | |
| 158 | template<> inline int64 saturate_cast<int64>(uint64 v) { return (int64)std::min(a: v, b: (uint64)LLONG_MAX); } |
| 159 | |
| 160 | /** @overload */ |
| 161 | template<typename _Tp> static inline _Tp saturate_cast(hfloat v) { return saturate_cast<_Tp>((float)v); } |
| 162 | |
| 163 | // in theory, we could use a LUT for 8u/8s->16f conversion, |
| 164 | // but with hardware support for FP32->FP16 conversion the current approach is preferable |
| 165 | template<> inline hfloat saturate_cast<hfloat>(uchar v) { return hfloat((float)v); } |
| 166 | template<> inline hfloat saturate_cast<hfloat>(schar v) { return hfloat((float)v); } |
| 167 | template<> inline hfloat saturate_cast<hfloat>(ushort v) { return hfloat((float)v); } |
| 168 | template<> inline hfloat saturate_cast<hfloat>(short v) { return hfloat((float)v); } |
| 169 | template<> inline hfloat saturate_cast<hfloat>(unsigned v){ return hfloat((float)v); } |
| 170 | template<> inline hfloat saturate_cast<hfloat>(int v) { return hfloat((float)v); } |
| 171 | template<> inline hfloat saturate_cast<hfloat>(uint64 v) { return hfloat((float)v); } |
| 172 | template<> inline hfloat saturate_cast<hfloat>(int64 v) { return hfloat((float)v); } |
| 173 | template<> inline hfloat saturate_cast<hfloat>(float v) { return hfloat(v); } |
| 174 | template<> inline hfloat saturate_cast<hfloat>(double v) { return hfloat((float)v); } |
| 175 | |
| 176 | //! @} |
| 177 | |
| 178 | } // cv |
| 179 | |
| 180 | #endif // OPENCV_CORE_SATURATE_HPP |
| 181 | |