defs.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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) 2015, 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_DEF_H__
  45. #define __OPENCV_DEF_H__
  46. #if !defined _CRT_SECURE_NO_DEPRECATE && defined _MSC_VER && _MSC_VER > 1300
  47. # define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio warnings */
  48. #endif
  49. #include <limits.h>
  50. #if defined __ICL
  51. # define CV_ICC __ICL
  52. #elif defined __ICC
  53. # define CV_ICC __ICC
  54. #elif defined __ECL
  55. # define CV_ICC __ECL
  56. #elif defined __ECC
  57. # define CV_ICC __ECC
  58. #elif defined __INTEL_COMPILER
  59. # define CV_ICC __INTEL_COMPILER
  60. #endif
  61. #ifndef CV_INLINE
  62. # if defined __cplusplus
  63. # define CV_INLINE static inline
  64. # elif defined _MSC_VER
  65. # define CV_INLINE __inline
  66. # else
  67. # define CV_INLINE static
  68. # endif
  69. #endif
  70. #if defined CV_ICC && !defined CV_ENABLE_UNROLLED
  71. # define CV_ENABLE_UNROLLED 0
  72. #else
  73. # define CV_ENABLE_UNROLLED 1
  74. #endif
  75. #ifdef __GNUC__
  76. # define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
  77. #elif defined _MSC_VER
  78. # define CV_DECL_ALIGNED(x) __declspec(align(x))
  79. #else
  80. # define CV_DECL_ALIGNED(x)
  81. #endif
  82. /* CPU features and intrinsics support */
  83. #define CV_CPU_NONE 0
  84. #define CV_CPU_MMX 1
  85. #define CV_CPU_SSE 2
  86. #define CV_CPU_SSE2 3
  87. #define CV_CPU_SSE3 4
  88. #define CV_CPU_SSSE3 5
  89. #define CV_CPU_SSE4_1 6
  90. #define CV_CPU_SSE4_2 7
  91. #define CV_CPU_POPCNT 8
  92. #define CV_CPU_AVX 10
  93. #define CV_CPU_AVX2 11
  94. #define CV_CPU_FMA3 12
  95. #define CV_CPU_AVX_512F 13
  96. #define CV_CPU_AVX_512BW 14
  97. #define CV_CPU_AVX_512CD 15
  98. #define CV_CPU_AVX_512DQ 16
  99. #define CV_CPU_AVX_512ER 17
  100. #define CV_CPU_AVX_512IFMA512 18
  101. #define CV_CPU_AVX_512PF 19
  102. #define CV_CPU_AVX_512VBMI 20
  103. #define CV_CPU_AVX_512VL 21
  104. #define CV_CPU_NEON 100
  105. // when adding to this list remember to update the enum in core/utility.cpp
  106. #define CV_HARDWARE_MAX_FEATURE 255
  107. // do not include SSE/AVX/NEON headers for NVCC compiler
  108. #ifndef __CUDACC__
  109. #if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2)
  110. # include <emmintrin.h>
  111. # define CV_MMX 1
  112. # define CV_SSE 1
  113. # define CV_SSE2 1
  114. # if defined __SSE3__ || (defined _MSC_VER && _MSC_VER >= 1500)
  115. # include <pmmintrin.h>
  116. # define CV_SSE3 1
  117. # endif
  118. # if defined __SSSE3__ || (defined _MSC_VER && _MSC_VER >= 1500)
  119. # include <tmmintrin.h>
  120. # define CV_SSSE3 1
  121. # endif
  122. # if defined __SSE4_1__ || (defined _MSC_VER && _MSC_VER >= 1500)
  123. # include <smmintrin.h>
  124. # define CV_SSE4_1 1
  125. # endif
  126. # if defined __SSE4_2__ || (defined _MSC_VER && _MSC_VER >= 1500)
  127. # include <nmmintrin.h>
  128. # define CV_SSE4_2 1
  129. # endif
  130. # if defined __POPCNT__ || (defined _MSC_VER && _MSC_VER >= 1500)
  131. # ifdef _MSC_VER
  132. # include <nmmintrin.h>
  133. # else
  134. # include <popcntintrin.h>
  135. # endif
  136. # define CV_POPCNT 1
  137. # endif
  138. # if defined __AVX__ || (defined _MSC_VER && _MSC_VER >= 1600 && 0)
  139. // MS Visual Studio 2010 (2012?) has no macro pre-defined to identify the use of /arch:AVX
  140. // See: http://connect.microsoft.com/VisualStudio/feedback/details/605858/arch-avx-should-define-a-predefined-macro-in-x64-and-set-a-unique-value-for-m-ix86-fp-in-win32
  141. # include <immintrin.h>
  142. # define CV_AVX 1
  143. # if defined(_XCR_XFEATURE_ENABLED_MASK)
  144. # define __xgetbv() _xgetbv(_XCR_XFEATURE_ENABLED_MASK)
  145. # else
  146. # define __xgetbv() 0
  147. # endif
  148. # endif
  149. # if defined __AVX2__ || (defined _MSC_VER && _MSC_VER >= 1800 && 0)
  150. # include <immintrin.h>
  151. # define CV_AVX2 1
  152. # if defined __FMA__
  153. # define CV_FMA3 1
  154. # endif
  155. # endif
  156. #endif
  157. #if (defined WIN32 || defined _WIN32) && defined(_M_ARM)
  158. # include <Intrin.h>
  159. # include "arm_neon.h"
  160. # define CV_NEON 1
  161. # define CPU_HAS_NEON_FEATURE (true)
  162. #elif defined(__ARM_NEON__) || (defined (__ARM_NEON) && defined(__aarch64__))
  163. # include <arm_neon.h>
  164. # define CV_NEON 1
  165. #endif
  166. #if defined __GNUC__ && defined __arm__ && (defined __ARM_PCS_VFP || defined __ARM_VFPV3__)
  167. # define CV_VFP 1
  168. #endif
  169. #endif // __CUDACC__
  170. #ifndef CV_POPCNT
  171. #define CV_POPCNT 0
  172. #endif
  173. #ifndef CV_MMX
  174. # define CV_MMX 0
  175. #endif
  176. #ifndef CV_SSE
  177. # define CV_SSE 0
  178. #endif
  179. #ifndef CV_SSE2
  180. # define CV_SSE2 0
  181. #endif
  182. #ifndef CV_SSE3
  183. # define CV_SSE3 0
  184. #endif
  185. #ifndef CV_SSSE3
  186. # define CV_SSSE3 0
  187. #endif
  188. #ifndef CV_SSE4_1
  189. # define CV_SSE4_1 0
  190. #endif
  191. #ifndef CV_SSE4_2
  192. # define CV_SSE4_2 0
  193. #endif
  194. #ifndef CV_AVX
  195. # define CV_AVX 0
  196. #endif
  197. #ifndef CV_AVX2
  198. # define CV_AVX2 0
  199. #endif
  200. #ifndef CV_FMA3
  201. # define CV_FMA3 0
  202. #endif
  203. #ifndef CV_AVX_512F
  204. # define CV_AVX_512F 0
  205. #endif
  206. #ifndef CV_AVX_512BW
  207. # define CV_AVX_512BW 0
  208. #endif
  209. #ifndef CV_AVX_512CD
  210. # define CV_AVX_512CD 0
  211. #endif
  212. #ifndef CV_AVX_512DQ
  213. # define CV_AVX_512DQ 0
  214. #endif
  215. #ifndef CV_AVX_512ER
  216. # define CV_AVX_512ER 0
  217. #endif
  218. #ifndef CV_AVX_512IFMA512
  219. # define CV_AVX_512IFMA512 0
  220. #endif
  221. #ifndef CV_AVX_512PF
  222. # define CV_AVX_512PF 0
  223. #endif
  224. #ifndef CV_AVX_512VBMI
  225. # define CV_AVX_512VBMI 0
  226. #endif
  227. #ifndef CV_AVX_512VL
  228. # define CV_AVX_512VL 0
  229. #endif
  230. #ifndef CV_NEON
  231. # define CV_NEON 0
  232. #endif
  233. #ifndef CV_VFP
  234. # define CV_VFP 0
  235. #endif
  236. /* primitive types */
  237. /*
  238. schar - signed 1 byte integer
  239. uchar - unsigned 1 byte integer
  240. short - signed 2 byte integer
  241. ushort - unsigned 2 byte integer
  242. int - signed 4 byte integer
  243. uint - unsigned 4 byte integer
  244. int64 - signed 8 byte integer
  245. uint64 - unsigned 8 byte integer
  246. */
  247. #if !defined _MSC_VER && !defined __BORLANDC__
  248. # if defined __cplusplus && __cplusplus >= 201103L
  249. # include <cstdint>
  250. typedef std::uint32_t uint;
  251. # else
  252. # include <stdint.h>
  253. typedef uint32_t uint;
  254. # endif
  255. #else
  256. typedef unsigned uint;
  257. #endif
  258. typedef signed char schar;
  259. #ifndef __IPL_H__
  260. typedef unsigned char uchar;
  261. typedef unsigned short ushort;
  262. #endif
  263. #if defined _MSC_VER || defined __BORLANDC__
  264. typedef __int64 int64;
  265. typedef unsigned __int64 uint64;
  266. # define CV_BIG_INT(n) n##I64
  267. # define CV_BIG_UINT(n) n##UI64
  268. #else
  269. typedef int64_t int64;
  270. typedef uint64_t uint64;
  271. # define CV_BIG_INT(n) n##LL
  272. # define CV_BIG_UINT(n) n##ULL
  273. #endif
  274. /* fundamental constants */
  275. #define CV_PI 3.1415926535897932384626433832795
  276. #define CV_2PI 6.283185307179586476925286766559
  277. #define CV_LOG2 0.69314718055994530941723212145818
  278. typedef union Cv32suf
  279. {
  280. int i;
  281. unsigned u;
  282. float f;
  283. }
  284. Cv32suf;
  285. typedef union Cv64suf
  286. {
  287. int64 i;
  288. uint64 u;
  289. double f;
  290. }
  291. Cv64suf;
  292. /****************************************************************************************\
  293. * fast math *
  294. \****************************************************************************************/
  295. #if defined __BORLANDC__
  296. # include <fastmath.h>
  297. #elif defined __cplusplus
  298. # include <cmath>
  299. #else
  300. # include <math.h>
  301. #endif
  302. #ifdef HAVE_TEGRA_OPTIMIZATION
  303. # include "tegra_round.hpp"
  304. #endif
  305. //! @addtogroup core_utils
  306. //! @{
  307. #if CV_VFP
  308. // 1. general scheme
  309. #define ARM_ROUND(_value, _asm_string) \
  310. int res; \
  311. float temp; \
  312. asm(_asm_string : [res] "=r" (res), [temp] "=w" (temp) : [value] "w" (_value)); \
  313. return res
  314. // 2. version for double
  315. #ifdef __clang__
  316. #define ARM_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %[value] \n vmov %[res], %[temp]")
  317. #else
  318. #define ARM_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %P[value] \n vmov %[res], %[temp]")
  319. #endif
  320. // 3. version for float
  321. #define ARM_ROUND_FLT(value) ARM_ROUND(value, "vcvtr.s32.f32 %[temp], %[value]\n vmov %[res], %[temp]")
  322. #endif // CV_VFP
  323. /** @brief Rounds floating-point number to the nearest integer
  324. @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the
  325. result is not defined.
  326. */
  327. CV_INLINE int
  328. cvRound( double value )
  329. {
  330. #if ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__ \
  331. && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__)
  332. __m128d t = _mm_set_sd( value );
  333. return _mm_cvtsd_si32(t);
  334. #elif defined _MSC_VER && defined _M_IX86
  335. int t;
  336. __asm
  337. {
  338. fld value;
  339. fistp t;
  340. }
  341. return t;
  342. #elif ((defined _MSC_VER && defined _M_ARM) || defined CV_ICC || \
  343. defined __GNUC__) && defined HAVE_TEGRA_OPTIMIZATION
  344. TEGRA_ROUND_DBL(value);
  345. #elif defined CV_ICC || defined __GNUC__
  346. # if CV_VFP
  347. ARM_ROUND_DBL(value);
  348. # else
  349. return (int)lrint(value);
  350. # endif
  351. #else
  352. /* it's ok if round does not comply with IEEE754 standard;
  353. the tests should allow +/-1 difference when the tested functions use round */
  354. return (int)(value + (value >= 0 ? 0.5 : -0.5));
  355. #endif
  356. }
  357. /** @brief Rounds floating-point number to the nearest integer not larger than the original.
  358. The function computes an integer i such that:
  359. \f[i \le \texttt{value} < i+1\f]
  360. @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the
  361. result is not defined.
  362. */
  363. CV_INLINE int cvFloor( double value )
  364. {
  365. #if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__)
  366. __m128d t = _mm_set_sd( value );
  367. int i = _mm_cvtsd_si32(t);
  368. return i - _mm_movemask_pd(_mm_cmplt_sd(t, _mm_cvtsi32_sd(t,i)));
  369. #elif defined __GNUC__
  370. int i = (int)value;
  371. return i - (i > value);
  372. #else
  373. int i = cvRound(value);
  374. float diff = (float)(value - i);
  375. return i - (diff < 0);
  376. #endif
  377. }
  378. /** @brief Rounds floating-point number to the nearest integer not larger than the original.
  379. The function computes an integer i such that:
  380. \f[i \le \texttt{value} < i+1\f]
  381. @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the
  382. result is not defined.
  383. */
  384. CV_INLINE int cvCeil( double value )
  385. {
  386. #if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__&& !defined __APPLE__)) && !defined(__CUDACC__)
  387. __m128d t = _mm_set_sd( value );
  388. int i = _mm_cvtsd_si32(t);
  389. return i + _mm_movemask_pd(_mm_cmplt_sd(_mm_cvtsi32_sd(t,i), t));
  390. #elif defined __GNUC__
  391. int i = (int)value;
  392. return i + (i < value);
  393. #else
  394. int i = cvRound(value);
  395. float diff = (float)(i - value);
  396. return i + (diff < 0);
  397. #endif
  398. }
  399. /** @brief Determines if the argument is Not A Number.
  400. @param value The input floating-point value
  401. The function returns 1 if the argument is Not A Number (as defined by IEEE754 standard), 0
  402. otherwise. */
  403. CV_INLINE int cvIsNaN( double value )
  404. {
  405. Cv64suf ieee754;
  406. ieee754.f = value;
  407. return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) +
  408. ((unsigned)ieee754.u != 0) > 0x7ff00000;
  409. }
  410. /** @brief Determines if the argument is Infinity.
  411. @param value The input floating-point value
  412. The function returns 1 if the argument is a plus or minus infinity (as defined by IEEE754 standard)
  413. and 0 otherwise. */
  414. CV_INLINE int cvIsInf( double value )
  415. {
  416. Cv64suf ieee754;
  417. ieee754.f = value;
  418. return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
  419. (unsigned)ieee754.u == 0;
  420. }
  421. #ifdef __cplusplus
  422. /** @overload */
  423. CV_INLINE int cvRound(float value)
  424. {
  425. #if ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__ && \
  426. defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__)
  427. __m128 t = _mm_set_ss( value );
  428. return _mm_cvtss_si32(t);
  429. #elif defined _MSC_VER && defined _M_IX86
  430. int t;
  431. __asm
  432. {
  433. fld value;
  434. fistp t;
  435. }
  436. return t;
  437. #elif ((defined _MSC_VER && defined _M_ARM) || defined CV_ICC || \
  438. defined __GNUC__) && defined HAVE_TEGRA_OPTIMIZATION
  439. TEGRA_ROUND_FLT(value);
  440. #elif defined CV_ICC || defined __GNUC__
  441. # if CV_VFP
  442. ARM_ROUND_FLT(value);
  443. # else
  444. return (int)lrintf(value);
  445. # endif
  446. #else
  447. /* it's ok if round does not comply with IEEE754 standard;
  448. the tests should allow +/-1 difference when the tested functions use round */
  449. return (int)(value + (value >= 0 ? 0.5f : -0.5f));
  450. #endif
  451. }
  452. /** @overload */
  453. CV_INLINE int cvRound( int value )
  454. {
  455. return value;
  456. }
  457. /** @overload */
  458. CV_INLINE int cvFloor( float value )
  459. {
  460. #if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__)
  461. __m128 t = _mm_set_ss( value );
  462. int i = _mm_cvtss_si32(t);
  463. return i - _mm_movemask_ps(_mm_cmplt_ss(t, _mm_cvtsi32_ss(t,i)));
  464. #elif defined __GNUC__
  465. int i = (int)value;
  466. return i - (i > value);
  467. #else
  468. int i = cvRound(value);
  469. float diff = (float)(value - i);
  470. return i - (diff < 0);
  471. #endif
  472. }
  473. /** @overload */
  474. CV_INLINE int cvFloor( int value )
  475. {
  476. return value;
  477. }
  478. /** @overload */
  479. CV_INLINE int cvCeil( float value )
  480. {
  481. #if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__&& !defined __APPLE__)) && !defined(__CUDACC__)
  482. __m128 t = _mm_set_ss( value );
  483. int i = _mm_cvtss_si32(t);
  484. return i + _mm_movemask_ps(_mm_cmplt_ss(_mm_cvtsi32_ss(t,i), t));
  485. #elif defined __GNUC__
  486. int i = (int)value;
  487. return i + (i < value);
  488. #else
  489. int i = cvRound(value);
  490. float diff = (float)(i - value);
  491. return i + (diff < 0);
  492. #endif
  493. }
  494. /** @overload */
  495. CV_INLINE int cvCeil( int value )
  496. {
  497. return value;
  498. }
  499. /** @overload */
  500. CV_INLINE int cvIsNaN( float value )
  501. {
  502. Cv32suf ieee754;
  503. ieee754.f = value;
  504. return (ieee754.u & 0x7fffffff) > 0x7f800000;
  505. }
  506. /** @overload */
  507. CV_INLINE int cvIsInf( float value )
  508. {
  509. Cv32suf ieee754;
  510. ieee754.f = value;
  511. return (ieee754.u & 0x7fffffff) == 0x7f800000;
  512. }
  513. #include <algorithm>
  514. namespace cv
  515. {
  516. /////////////// saturate_cast (used in image & signal processing) ///////////////////
  517. /**
  518. Template function for accurate conversion from one primitive type to another.
  519. The functions saturate_cast resemble the standard C++ cast operations, such as static_cast\<T\>()
  520. and others. They perform an efficient and accurate conversion from one primitive type to another
  521. (see the introduction chapter). saturate in the name means that when the input value v is out of the
  522. range of the target type, the result is not formed just by taking low bits of the input, but instead
  523. the value is clipped. For example:
  524. @code
  525. uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN)
  526. short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX)
  527. @endcode
  528. Such clipping is done when the target type is unsigned char , signed char , unsigned short or
  529. signed short . For 32-bit integers, no clipping is done.
  530. When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit),
  531. the floating-point value is first rounded to the nearest integer and then clipped if needed (when
  532. the target type is 8- or 16-bit).
  533. This operation is used in the simplest or most complex image processing functions in OpenCV.
  534. @param v Function parameter.
  535. @sa add, subtract, multiply, divide, Mat::convertTo
  536. */
  537. template<typename _Tp> static inline _Tp saturate_cast(uchar v) { return _Tp(v); }
  538. /** @overload */
  539. template<typename _Tp> static inline _Tp saturate_cast(schar v) { return _Tp(v); }
  540. /** @overload */
  541. template<typename _Tp> static inline _Tp saturate_cast(ushort v) { return _Tp(v); }
  542. /** @overload */
  543. template<typename _Tp> static inline _Tp saturate_cast(short v) { return _Tp(v); }
  544. /** @overload */
  545. template<typename _Tp> static inline _Tp saturate_cast(unsigned v) { return _Tp(v); }
  546. /** @overload */
  547. template<typename _Tp> static inline _Tp saturate_cast(int v) { return _Tp(v); }
  548. /** @overload */
  549. template<typename _Tp> static inline _Tp saturate_cast(float v) { return _Tp(v); }
  550. /** @overload */
  551. template<typename _Tp> static inline _Tp saturate_cast(double v) { return _Tp(v); }
  552. /** @overload */
  553. template<typename _Tp> static inline _Tp saturate_cast(int64 v) { return _Tp(v); }
  554. /** @overload */
  555. template<typename _Tp> static inline _Tp saturate_cast(uint64 v) { return _Tp(v); }
  556. //! @cond IGNORED
  557. template<> inline uchar saturate_cast<uchar>(schar v) { return (uchar)std::max((int)v, 0); }
  558. template<> inline uchar saturate_cast<uchar>(ushort v) { return (uchar)std::min((unsigned)v, (unsigned)UCHAR_MAX); }
  559. template<> inline uchar saturate_cast<uchar>(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
  560. template<> inline uchar saturate_cast<uchar>(short v) { return saturate_cast<uchar>((int)v); }
  561. template<> inline uchar saturate_cast<uchar>(unsigned v) { return (uchar)std::min(v, (unsigned)UCHAR_MAX); }
  562. template<> inline uchar saturate_cast<uchar>(float v) { int iv = cvRound(v); return saturate_cast<uchar>(iv); }
  563. template<> inline uchar saturate_cast<uchar>(double v) { int iv = cvRound(v); return saturate_cast<uchar>(iv); }
  564. template<> inline uchar saturate_cast<uchar>(int64 v) { return (uchar)((uint64)v <= (uint64)UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
  565. template<> inline uchar saturate_cast<uchar>(uint64 v) { return (uchar)std::min(v, (uint64)UCHAR_MAX); }
  566. template<> inline schar saturate_cast<schar>(uchar v) { return (schar)std::min((int)v, SCHAR_MAX); }
  567. template<> inline schar saturate_cast<schar>(ushort v) { return (schar)std::min((unsigned)v, (unsigned)SCHAR_MAX); }
  568. template<> inline schar saturate_cast<schar>(int v) { return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); }
  569. template<> inline schar saturate_cast<schar>(short v) { return saturate_cast<schar>((int)v); }
  570. template<> inline schar saturate_cast<schar>(unsigned v) { return (schar)std::min(v, (unsigned)SCHAR_MAX); }
  571. template<> inline schar saturate_cast<schar>(float v) { int iv = cvRound(v); return saturate_cast<schar>(iv); }
  572. template<> inline schar saturate_cast<schar>(double v) { int iv = cvRound(v); return saturate_cast<schar>(iv); }
  573. 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); }
  574. template<> inline schar saturate_cast<schar>(uint64 v) { return (schar)std::min(v, (uint64)SCHAR_MAX); }
  575. template<> inline ushort saturate_cast<ushort>(schar v) { return (ushort)std::max((int)v, 0); }
  576. template<> inline ushort saturate_cast<ushort>(short v) { return (ushort)std::max((int)v, 0); }
  577. template<> inline ushort saturate_cast<ushort>(int v) { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }
  578. template<> inline ushort saturate_cast<ushort>(unsigned v) { return (ushort)std::min(v, (unsigned)USHRT_MAX); }
  579. template<> inline ushort saturate_cast<ushort>(float v) { int iv = cvRound(v); return saturate_cast<ushort>(iv); }
  580. template<> inline ushort saturate_cast<ushort>(double v) { int iv = cvRound(v); return saturate_cast<ushort>(iv); }
  581. template<> inline ushort saturate_cast<ushort>(int64 v) { return (ushort)((uint64)v <= (uint64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }
  582. template<> inline ushort saturate_cast<ushort>(uint64 v) { return (ushort)std::min(v, (uint64)USHRT_MAX); }
  583. template<> inline short saturate_cast<short>(ushort v) { return (short)std::min((int)v, SHRT_MAX); }
  584. template<> inline short saturate_cast<short>(int v) { return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); }
  585. template<> inline short saturate_cast<short>(unsigned v) { return (short)std::min(v, (unsigned)SHRT_MAX); }
  586. template<> inline short saturate_cast<short>(float v) { int iv = cvRound(v); return saturate_cast<short>(iv); }
  587. template<> inline short saturate_cast<short>(double v) { int iv = cvRound(v); return saturate_cast<short>(iv); }
  588. 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); }
  589. template<> inline short saturate_cast<short>(uint64 v) { return (short)std::min(v, (uint64)SHRT_MAX); }
  590. template<> inline int saturate_cast<int>(float v) { return cvRound(v); }
  591. template<> inline int saturate_cast<int>(double v) { return cvRound(v); }
  592. // we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc.
  593. template<> inline unsigned saturate_cast<unsigned>(float v) { return cvRound(v); }
  594. template<> inline unsigned saturate_cast<unsigned>(double v) { return cvRound(v); }
  595. //! @endcond
  596. }
  597. #endif // __cplusplus
  598. //! @} core_utils
  599. #endif //__OPENCV_HAL_H__