openCV library for Renesas RZ/A
Dependents: RZ_A2M_Mbed_samples
include/opencv2/core/saturate.hpp@0:0e0631af0305, 2021-01-29 (annotated)
- Committer:
- RyoheiHagimoto
- Date:
- Fri Jan 29 04:53:38 2021 +0000
- Revision:
- 0:0e0631af0305
copied from https://github.com/d-kato/opencv-lib.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RyoheiHagimoto | 0:0e0631af0305 | 1 | /*M/////////////////////////////////////////////////////////////////////////////////////// |
RyoheiHagimoto | 0:0e0631af0305 | 2 | // |
RyoheiHagimoto | 0:0e0631af0305 | 3 | // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
RyoheiHagimoto | 0:0e0631af0305 | 4 | // |
RyoheiHagimoto | 0:0e0631af0305 | 5 | // By downloading, copying, installing or using the software you agree to this license. |
RyoheiHagimoto | 0:0e0631af0305 | 6 | // If you do not agree to this license, do not download, install, |
RyoheiHagimoto | 0:0e0631af0305 | 7 | // copy or use the software. |
RyoheiHagimoto | 0:0e0631af0305 | 8 | // |
RyoheiHagimoto | 0:0e0631af0305 | 9 | // |
RyoheiHagimoto | 0:0e0631af0305 | 10 | // License Agreement |
RyoheiHagimoto | 0:0e0631af0305 | 11 | // For Open Source Computer Vision Library |
RyoheiHagimoto | 0:0e0631af0305 | 12 | // |
RyoheiHagimoto | 0:0e0631af0305 | 13 | // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 14 | // Copyright (C) 2009, Willow Garage Inc., all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 15 | // Copyright (C) 2013, OpenCV Foundation, all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 16 | // Copyright (C) 2014, Itseez Inc., all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 17 | // Third party copyrights are property of their respective owners. |
RyoheiHagimoto | 0:0e0631af0305 | 18 | // |
RyoheiHagimoto | 0:0e0631af0305 | 19 | // Redistribution and use in source and binary forms, with or without modification, |
RyoheiHagimoto | 0:0e0631af0305 | 20 | // are permitted provided that the following conditions are met: |
RyoheiHagimoto | 0:0e0631af0305 | 21 | // |
RyoheiHagimoto | 0:0e0631af0305 | 22 | // * Redistribution's of source code must retain the above copyright notice, |
RyoheiHagimoto | 0:0e0631af0305 | 23 | // this list of conditions and the following disclaimer. |
RyoheiHagimoto | 0:0e0631af0305 | 24 | // |
RyoheiHagimoto | 0:0e0631af0305 | 25 | // * Redistribution's in binary form must reproduce the above copyright notice, |
RyoheiHagimoto | 0:0e0631af0305 | 26 | // this list of conditions and the following disclaimer in the documentation |
RyoheiHagimoto | 0:0e0631af0305 | 27 | // and/or other materials provided with the distribution. |
RyoheiHagimoto | 0:0e0631af0305 | 28 | // |
RyoheiHagimoto | 0:0e0631af0305 | 29 | // * The name of the copyright holders may not be used to endorse or promote products |
RyoheiHagimoto | 0:0e0631af0305 | 30 | // derived from this software without specific prior written permission. |
RyoheiHagimoto | 0:0e0631af0305 | 31 | // |
RyoheiHagimoto | 0:0e0631af0305 | 32 | // This software is provided by the copyright holders and contributors "as is" and |
RyoheiHagimoto | 0:0e0631af0305 | 33 | // any express or implied warranties, including, but not limited to, the implied |
RyoheiHagimoto | 0:0e0631af0305 | 34 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
RyoheiHagimoto | 0:0e0631af0305 | 35 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
RyoheiHagimoto | 0:0e0631af0305 | 36 | // indirect, incidental, special, exemplary, or consequential damages |
RyoheiHagimoto | 0:0e0631af0305 | 37 | // (including, but not limited to, procurement of substitute goods or services; |
RyoheiHagimoto | 0:0e0631af0305 | 38 | // loss of use, data, or profits; or business interruption) however caused |
RyoheiHagimoto | 0:0e0631af0305 | 39 | // and on any theory of liability, whether in contract, strict liability, |
RyoheiHagimoto | 0:0e0631af0305 | 40 | // or tort (including negligence or otherwise) arising in any way out of |
RyoheiHagimoto | 0:0e0631af0305 | 41 | // the use of this software, even if advised of the possibility of such damage. |
RyoheiHagimoto | 0:0e0631af0305 | 42 | // |
RyoheiHagimoto | 0:0e0631af0305 | 43 | //M*/ |
RyoheiHagimoto | 0:0e0631af0305 | 44 | |
RyoheiHagimoto | 0:0e0631af0305 | 45 | #ifndef OPENCV_CORE_SATURATE_HPP |
RyoheiHagimoto | 0:0e0631af0305 | 46 | #define OPENCV_CORE_SATURATE_HPP |
RyoheiHagimoto | 0:0e0631af0305 | 47 | |
RyoheiHagimoto | 0:0e0631af0305 | 48 | #include "opencv2/core/cvdef.h" |
RyoheiHagimoto | 0:0e0631af0305 | 49 | #include "opencv2/core/fast_math.hpp" |
RyoheiHagimoto | 0:0e0631af0305 | 50 | |
RyoheiHagimoto | 0:0e0631af0305 | 51 | namespace cv |
RyoheiHagimoto | 0:0e0631af0305 | 52 | { |
RyoheiHagimoto | 0:0e0631af0305 | 53 | |
RyoheiHagimoto | 0:0e0631af0305 | 54 | //! @addtogroup core_utils |
RyoheiHagimoto | 0:0e0631af0305 | 55 | //! @{ |
RyoheiHagimoto | 0:0e0631af0305 | 56 | |
RyoheiHagimoto | 0:0e0631af0305 | 57 | /////////////// saturate_cast (used in image & signal processing) /////////////////// |
RyoheiHagimoto | 0:0e0631af0305 | 58 | |
RyoheiHagimoto | 0:0e0631af0305 | 59 | /** @brief Template function for accurate conversion from one primitive type to another. |
RyoheiHagimoto | 0:0e0631af0305 | 60 | |
RyoheiHagimoto | 0:0e0631af0305 | 61 | The functions saturate_cast resemble the standard C++ cast operations, such as static_cast\<T\>() |
RyoheiHagimoto | 0:0e0631af0305 | 62 | and others. They perform an efficient and accurate conversion from one primitive type to another |
RyoheiHagimoto | 0:0e0631af0305 | 63 | (see the introduction chapter). saturate in the name means that when the input value v is out of the |
RyoheiHagimoto | 0:0e0631af0305 | 64 | range of the target type, the result is not formed just by taking low bits of the input, but instead |
RyoheiHagimoto | 0:0e0631af0305 | 65 | the value is clipped. For example: |
RyoheiHagimoto | 0:0e0631af0305 | 66 | @code |
RyoheiHagimoto | 0:0e0631af0305 | 67 | uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN) |
RyoheiHagimoto | 0:0e0631af0305 | 68 | short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX) |
RyoheiHagimoto | 0:0e0631af0305 | 69 | @endcode |
RyoheiHagimoto | 0:0e0631af0305 | 70 | Such clipping is done when the target type is unsigned char , signed char , unsigned short or |
RyoheiHagimoto | 0:0e0631af0305 | 71 | signed short . For 32-bit integers, no clipping is done. |
RyoheiHagimoto | 0:0e0631af0305 | 72 | |
RyoheiHagimoto | 0:0e0631af0305 | 73 | When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit), |
RyoheiHagimoto | 0:0e0631af0305 | 74 | the floating-point value is first rounded to the nearest integer and then clipped if needed (when |
RyoheiHagimoto | 0:0e0631af0305 | 75 | the target type is 8- or 16-bit). |
RyoheiHagimoto | 0:0e0631af0305 | 76 | |
RyoheiHagimoto | 0:0e0631af0305 | 77 | This operation is used in the simplest or most complex image processing functions in OpenCV. |
RyoheiHagimoto | 0:0e0631af0305 | 78 | |
RyoheiHagimoto | 0:0e0631af0305 | 79 | @param v Function parameter. |
RyoheiHagimoto | 0:0e0631af0305 | 80 | @sa add, subtract, multiply, divide, Mat::convertTo |
RyoheiHagimoto | 0:0e0631af0305 | 81 | */ |
RyoheiHagimoto | 0:0e0631af0305 | 82 | template<typename _Tp> static inline _Tp saturate_cast(uchar v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 83 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 84 | template<typename _Tp> static inline _Tp saturate_cast(schar v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 85 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 86 | template<typename _Tp> static inline _Tp saturate_cast(ushort v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 87 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 88 | template<typename _Tp> static inline _Tp saturate_cast(short v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 89 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 90 | template<typename _Tp> static inline _Tp saturate_cast(unsigned v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 91 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 92 | template<typename _Tp> static inline _Tp saturate_cast(int v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 93 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 94 | template<typename _Tp> static inline _Tp saturate_cast(float v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 95 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 96 | template<typename _Tp> static inline _Tp saturate_cast(double v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 97 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 98 | template<typename _Tp> static inline _Tp saturate_cast(int64 v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 99 | /** @overload */ |
RyoheiHagimoto | 0:0e0631af0305 | 100 | template<typename _Tp> static inline _Tp saturate_cast(uint64 v) { return _Tp(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 101 | |
RyoheiHagimoto | 0:0e0631af0305 | 102 | template<> inline uchar saturate_cast<uchar>(schar v) { return (uchar)std::max((int)v, 0); } |
RyoheiHagimoto | 0:0e0631af0305 | 103 | template<> inline uchar saturate_cast<uchar>(ushort v) { return (uchar)std::min((unsigned)v, (unsigned)UCHAR_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 104 | template<> inline uchar saturate_cast<uchar>(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } |
RyoheiHagimoto | 0:0e0631af0305 | 105 | template<> inline uchar saturate_cast<uchar>(short v) { return saturate_cast<uchar>((int)v); } |
RyoheiHagimoto | 0:0e0631af0305 | 106 | template<> inline uchar saturate_cast<uchar>(unsigned v) { return (uchar)std::min(v, (unsigned)UCHAR_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 107 | template<> inline uchar saturate_cast<uchar>(float v) { int iv = cvRound(v); return saturate_cast<uchar>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 108 | template<> inline uchar saturate_cast<uchar>(double v) { int iv = cvRound(v); return saturate_cast<uchar>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 109 | template<> inline uchar saturate_cast<uchar>(int64 v) { return (uchar)((uint64)v <= (uint64)UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } |
RyoheiHagimoto | 0:0e0631af0305 | 110 | template<> inline uchar saturate_cast<uchar>(uint64 v) { return (uchar)std::min(v, (uint64)UCHAR_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 111 | |
RyoheiHagimoto | 0:0e0631af0305 | 112 | template<> inline schar saturate_cast<schar>(uchar v) { return (schar)std::min((int)v, SCHAR_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 113 | template<> inline schar saturate_cast<schar>(ushort v) { return (schar)std::min((unsigned)v, (unsigned)SCHAR_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 114 | template<> inline schar saturate_cast<schar>(int v) { return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } |
RyoheiHagimoto | 0:0e0631af0305 | 115 | template<> inline schar saturate_cast<schar>(short v) { return saturate_cast<schar>((int)v); } |
RyoheiHagimoto | 0:0e0631af0305 | 116 | template<> inline schar saturate_cast<schar>(unsigned v) { return (schar)std::min(v, (unsigned)SCHAR_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 117 | template<> inline schar saturate_cast<schar>(float v) { int iv = cvRound(v); return saturate_cast<schar>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 118 | template<> inline schar saturate_cast<schar>(double v) { int iv = cvRound(v); return saturate_cast<schar>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 119 | 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); } |
RyoheiHagimoto | 0:0e0631af0305 | 120 | template<> inline schar saturate_cast<schar>(uint64 v) { return (schar)std::min(v, (uint64)SCHAR_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 121 | |
RyoheiHagimoto | 0:0e0631af0305 | 122 | template<> inline ushort saturate_cast<ushort>(schar v) { return (ushort)std::max((int)v, 0); } |
RyoheiHagimoto | 0:0e0631af0305 | 123 | template<> inline ushort saturate_cast<ushort>(short v) { return (ushort)std::max((int)v, 0); } |
RyoheiHagimoto | 0:0e0631af0305 | 124 | template<> inline ushort saturate_cast<ushort>(int v) { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } |
RyoheiHagimoto | 0:0e0631af0305 | 125 | template<> inline ushort saturate_cast<ushort>(unsigned v) { return (ushort)std::min(v, (unsigned)USHRT_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 126 | template<> inline ushort saturate_cast<ushort>(float v) { int iv = cvRound(v); return saturate_cast<ushort>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 127 | template<> inline ushort saturate_cast<ushort>(double v) { int iv = cvRound(v); return saturate_cast<ushort>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 128 | template<> inline ushort saturate_cast<ushort>(int64 v) { return (ushort)((uint64)v <= (uint64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } |
RyoheiHagimoto | 0:0e0631af0305 | 129 | template<> inline ushort saturate_cast<ushort>(uint64 v) { return (ushort)std::min(v, (uint64)USHRT_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 130 | |
RyoheiHagimoto | 0:0e0631af0305 | 131 | template<> inline short saturate_cast<short>(ushort v) { return (short)std::min((int)v, SHRT_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 132 | template<> inline short saturate_cast<short>(int v) { return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } |
RyoheiHagimoto | 0:0e0631af0305 | 133 | template<> inline short saturate_cast<short>(unsigned v) { return (short)std::min(v, (unsigned)SHRT_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 134 | template<> inline short saturate_cast<short>(float v) { int iv = cvRound(v); return saturate_cast<short>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 135 | template<> inline short saturate_cast<short>(double v) { int iv = cvRound(v); return saturate_cast<short>(iv); } |
RyoheiHagimoto | 0:0e0631af0305 | 136 | 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); } |
RyoheiHagimoto | 0:0e0631af0305 | 137 | template<> inline short saturate_cast<short>(uint64 v) { return (short)std::min(v, (uint64)SHRT_MAX); } |
RyoheiHagimoto | 0:0e0631af0305 | 138 | |
RyoheiHagimoto | 0:0e0631af0305 | 139 | template<> inline int saturate_cast<int>(float v) { return cvRound(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 140 | template<> inline int saturate_cast<int>(double v) { return cvRound(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 141 | |
RyoheiHagimoto | 0:0e0631af0305 | 142 | // we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc. |
RyoheiHagimoto | 0:0e0631af0305 | 143 | template<> inline unsigned saturate_cast<unsigned>(float v) { return cvRound(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 144 | template<> inline unsigned saturate_cast<unsigned>(double v) { return cvRound(v); } |
RyoheiHagimoto | 0:0e0631af0305 | 145 | |
RyoheiHagimoto | 0:0e0631af0305 | 146 | //! @} |
RyoheiHagimoto | 0:0e0631af0305 | 147 | |
RyoheiHagimoto | 0:0e0631af0305 | 148 | } // cv |
RyoheiHagimoto | 0:0e0631af0305 | 149 | |
RyoheiHagimoto | 0:0e0631af0305 | 150 | #endif // OPENCV_CORE_SATURATE_HPP |