saturate.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #ifndef OPENCV_CORE_SATURATE_HPP
  45. #define OPENCV_CORE_SATURATE_HPP
  46. #include "opencv2/core/cvdef.h"
  47. #include "opencv2/core/fast_math.hpp"
  48. namespace cv
  49. {
  50. //! @addtogroup core_utils
  51. //! @{
  52. /////////////// saturate_cast (used in image & signal processing) ///////////////////
  53. /** @brief Template function for accurate conversion from one primitive type to another.
  54. The function saturate_cast resembles the standard C++ cast operations, such as static_cast\<T\>()
  55. and others. It perform an efficient and accurate conversion from one primitive type to another
  56. (see the introduction chapter). saturate in the name means that when the input value v is out of the
  57. range of the target type, the result is not formed just by taking low bits of the input, but instead
  58. the value is clipped. For example:
  59. @code
  60. uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN)
  61. short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX)
  62. @endcode
  63. Such clipping is done when the target type is unsigned char , signed char , unsigned short or
  64. signed short . For 32-bit integers, no clipping is done.
  65. When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit),
  66. the floating-point value is first rounded to the nearest integer and then clipped if needed (when
  67. the target type is 8- or 16-bit).
  68. This operation is used in the simplest or most complex image processing functions in OpenCV.
  69. @param v Function parameter.
  70. @sa add, subtract, multiply, divide, Mat::convertTo
  71. */
  72. template<typename _Tp> static inline _Tp saturate_cast(uchar v) { return _Tp(v); }
  73. /** @overload */
  74. template<typename _Tp> static inline _Tp saturate_cast(schar v) { return _Tp(v); }
  75. /** @overload */
  76. template<typename _Tp> static inline _Tp saturate_cast(ushort v) { return _Tp(v); }
  77. /** @overload */
  78. template<typename _Tp> static inline _Tp saturate_cast(short v) { return _Tp(v); }
  79. /** @overload */
  80. template<typename _Tp> static inline _Tp saturate_cast(unsigned v) { return _Tp(v); }
  81. /** @overload */
  82. template<typename _Tp> static inline _Tp saturate_cast(int v) { return _Tp(v); }
  83. /** @overload */
  84. template<typename _Tp> static inline _Tp saturate_cast(float v) { return _Tp(v); }
  85. /** @overload */
  86. template<typename _Tp> static inline _Tp saturate_cast(double v) { return _Tp(v); }
  87. /** @overload */
  88. template<typename _Tp> static inline _Tp saturate_cast(int64 v) { return _Tp(v); }
  89. /** @overload */
  90. template<typename _Tp> static inline _Tp saturate_cast(uint64 v) { return _Tp(v); }
  91. template<> inline uchar saturate_cast<uchar>(schar v) { return (uchar)std::max((int)v, 0); }
  92. template<> inline uchar saturate_cast<uchar>(ushort v) { return (uchar)std::min((unsigned)v, (unsigned)UCHAR_MAX); }
  93. template<> inline uchar saturate_cast<uchar>(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
  94. template<> inline uchar saturate_cast<uchar>(short v) { return saturate_cast<uchar>((int)v); }
  95. template<> inline uchar saturate_cast<uchar>(unsigned v) { return (uchar)std::min(v, (unsigned)UCHAR_MAX); }
  96. template<> inline uchar saturate_cast<uchar>(float v) { int iv = cvRound(v); return saturate_cast<uchar>(iv); }
  97. template<> inline uchar saturate_cast<uchar>(double v) { int iv = cvRound(v); return saturate_cast<uchar>(iv); }
  98. template<> inline uchar saturate_cast<uchar>(int64 v) { return (uchar)((uint64)v <= (uint64)UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
  99. template<> inline uchar saturate_cast<uchar>(uint64 v) { return (uchar)std::min(v, (uint64)UCHAR_MAX); }
  100. template<> inline schar saturate_cast<schar>(uchar v) { return (schar)std::min((int)v, SCHAR_MAX); }
  101. template<> inline schar saturate_cast<schar>(ushort v) { return (schar)std::min((unsigned)v, (unsigned)SCHAR_MAX); }
  102. template<> inline schar saturate_cast<schar>(int v) { return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); }
  103. template<> inline schar saturate_cast<schar>(short v) { return saturate_cast<schar>((int)v); }
  104. template<> inline schar saturate_cast<schar>(unsigned v) { return (schar)std::min(v, (unsigned)SCHAR_MAX); }
  105. template<> inline schar saturate_cast<schar>(float v) { int iv = cvRound(v); return saturate_cast<schar>(iv); }
  106. template<> inline schar saturate_cast<schar>(double v) { int iv = cvRound(v); return saturate_cast<schar>(iv); }
  107. 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); }
  108. template<> inline schar saturate_cast<schar>(uint64 v) { return (schar)std::min(v, (uint64)SCHAR_MAX); }
  109. template<> inline ushort saturate_cast<ushort>(schar v) { return (ushort)std::max((int)v, 0); }
  110. template<> inline ushort saturate_cast<ushort>(short v) { return (ushort)std::max((int)v, 0); }
  111. template<> inline ushort saturate_cast<ushort>(int v) { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }
  112. template<> inline ushort saturate_cast<ushort>(unsigned v) { return (ushort)std::min(v, (unsigned)USHRT_MAX); }
  113. template<> inline ushort saturate_cast<ushort>(float v) { int iv = cvRound(v); return saturate_cast<ushort>(iv); }
  114. template<> inline ushort saturate_cast<ushort>(double v) { int iv = cvRound(v); return saturate_cast<ushort>(iv); }
  115. template<> inline ushort saturate_cast<ushort>(int64 v) { return (ushort)((uint64)v <= (uint64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }
  116. template<> inline ushort saturate_cast<ushort>(uint64 v) { return (ushort)std::min(v, (uint64)USHRT_MAX); }
  117. template<> inline short saturate_cast<short>(ushort v) { return (short)std::min((int)v, SHRT_MAX); }
  118. template<> inline short saturate_cast<short>(int v) { return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); }
  119. template<> inline short saturate_cast<short>(unsigned v) { return (short)std::min(v, (unsigned)SHRT_MAX); }
  120. template<> inline short saturate_cast<short>(float v) { int iv = cvRound(v); return saturate_cast<short>(iv); }
  121. template<> inline short saturate_cast<short>(double v) { int iv = cvRound(v); return saturate_cast<short>(iv); }
  122. 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); }
  123. template<> inline short saturate_cast<short>(uint64 v) { return (short)std::min(v, (uint64)SHRT_MAX); }
  124. template<> inline int saturate_cast<int>(unsigned v) { return (int)std::min(v, (unsigned)INT_MAX); }
  125. template<> inline int saturate_cast<int>(int64 v) { return (int)((uint64)(v - INT_MIN) <= (uint64)UINT_MAX ? v : v > 0 ? INT_MAX : INT_MIN); }
  126. template<> inline int saturate_cast<int>(uint64 v) { return (int)std::min(v, (uint64)INT_MAX); }
  127. template<> inline int saturate_cast<int>(float v) { return cvRound(v); }
  128. template<> inline int saturate_cast<int>(double v) { return cvRound(v); }
  129. template<> inline unsigned saturate_cast<unsigned>(schar v) { return (unsigned)std::max(v, (schar)0); }
  130. template<> inline unsigned saturate_cast<unsigned>(short v) { return (unsigned)std::max(v, (short)0); }
  131. template<> inline unsigned saturate_cast<unsigned>(int v) { return (unsigned)std::max(v, (int)0); }
  132. template<> inline unsigned saturate_cast<unsigned>(int64 v) { return (unsigned)((uint64)v <= (uint64)UINT_MAX ? v : v > 0 ? UINT_MAX : 0); }
  133. template<> inline unsigned saturate_cast<unsigned>(uint64 v) { return (unsigned)std::min(v, (uint64)UINT_MAX); }
  134. // we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc.
  135. template<> inline unsigned saturate_cast<unsigned>(float v) { return static_cast<unsigned>(cvRound(v)); }
  136. template<> inline unsigned saturate_cast<unsigned>(double v) { return static_cast<unsigned>(cvRound(v)); }
  137. template<> inline uint64 saturate_cast<uint64>(schar v) { return (uint64)std::max(v, (schar)0); }
  138. template<> inline uint64 saturate_cast<uint64>(short v) { return (uint64)std::max(v, (short)0); }
  139. template<> inline uint64 saturate_cast<uint64>(int v) { return (uint64)std::max(v, (int)0); }
  140. template<> inline uint64 saturate_cast<uint64>(int64 v) { return (uint64)std::max(v, (int64)0); }
  141. template<> inline int64 saturate_cast<int64>(uint64 v) { return (int64)std::min(v, (uint64)LLONG_MAX); }
  142. /** @overload */
  143. template<typename _Tp> static inline _Tp saturate_cast(float16_t v) { return saturate_cast<_Tp>((float)v); }
  144. // in theory, we could use a LUT for 8u/8s->16f conversion,
  145. // but with hardware support for FP32->FP16 conversion the current approach is preferable
  146. template<> inline float16_t saturate_cast<float16_t>(uchar v) { return float16_t((float)v); }
  147. template<> inline float16_t saturate_cast<float16_t>(schar v) { return float16_t((float)v); }
  148. template<> inline float16_t saturate_cast<float16_t>(ushort v) { return float16_t((float)v); }
  149. template<> inline float16_t saturate_cast<float16_t>(short v) { return float16_t((float)v); }
  150. template<> inline float16_t saturate_cast<float16_t>(unsigned v){ return float16_t((float)v); }
  151. template<> inline float16_t saturate_cast<float16_t>(int v) { return float16_t((float)v); }
  152. template<> inline float16_t saturate_cast<float16_t>(uint64 v) { return float16_t((float)v); }
  153. template<> inline float16_t saturate_cast<float16_t>(int64 v) { return float16_t((float)v); }
  154. template<> inline float16_t saturate_cast<float16_t>(float v) { return float16_t(v); }
  155. template<> inline float16_t saturate_cast<float16_t>(double v) { return float16_t((float)v); }
  156. //! @}
  157. } // cv
  158. #endif // OPENCV_CORE_SATURATE_HPP