opencv on mbed

Dependencies:   mbed

Committer:
joeverbout
Date:
Thu Mar 31 21:16:38 2016 +0000
Revision:
0:ea44dc9ed014
OpenCV on mbed attempt

Who changed what in which revision?

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