cvstd.inl.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_CVSTDINL_HPP
  44. #define OPENCV_CORE_CVSTDINL_HPP
  45. #include <complex>
  46. #include <ostream>
  47. #include <sstream>
  48. //! @cond IGNORED
  49. #ifdef _MSC_VER
  50. #pragma warning( push )
  51. #pragma warning( disable: 4127 )
  52. #endif
  53. namespace cv
  54. {
  55. template<typename _Tp> class DataType< std::complex<_Tp> >
  56. {
  57. public:
  58. typedef std::complex<_Tp> value_type;
  59. typedef value_type work_type;
  60. typedef _Tp channel_type;
  61. enum { generic_type = 0,
  62. depth = DataType<channel_type>::depth,
  63. channels = 2,
  64. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  65. type = CV_MAKETYPE(depth, channels) };
  66. typedef Vec<channel_type, channels> vec_type;
  67. };
  68. inline
  69. String::String(const std::string& str)
  70. : cstr_(0), len_(0)
  71. {
  72. size_t len = str.size();
  73. if (len) memcpy(allocate(len), str.c_str(), len);
  74. }
  75. inline
  76. String::String(const std::string& str, size_t pos, size_t len)
  77. : cstr_(0), len_(0)
  78. {
  79. size_t strlen = str.size();
  80. pos = min(pos, strlen);
  81. len = min(strlen - pos, len);
  82. if (!len) return;
  83. memcpy(allocate(len), str.c_str() + pos, len);
  84. }
  85. inline
  86. String& String::operator = (const std::string& str)
  87. {
  88. deallocate();
  89. size_t len = str.size();
  90. if (len) memcpy(allocate(len), str.c_str(), len);
  91. return *this;
  92. }
  93. inline
  94. String& String::operator += (const std::string& str)
  95. {
  96. *this = *this + str;
  97. return *this;
  98. }
  99. inline
  100. String::operator std::string() const
  101. {
  102. return std::string(cstr_, len_);
  103. }
  104. inline
  105. String operator + (const String& lhs, const std::string& rhs)
  106. {
  107. String s;
  108. size_t rhslen = rhs.size();
  109. s.allocate(lhs.len_ + rhslen);
  110. if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
  111. if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
  112. return s;
  113. }
  114. inline
  115. String operator + (const std::string& lhs, const String& rhs)
  116. {
  117. String s;
  118. size_t lhslen = lhs.size();
  119. s.allocate(lhslen + rhs.len_);
  120. if (lhslen) memcpy(s.cstr_, lhs.c_str(), lhslen);
  121. if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
  122. return s;
  123. }
  124. inline
  125. FileNode::operator std::string() const
  126. {
  127. String value;
  128. read(*this, value, value);
  129. return value;
  130. }
  131. template<> inline
  132. void operator >> (const FileNode& n, std::string& value)
  133. {
  134. read(n, value, std::string());
  135. }
  136. template<> inline
  137. FileStorage& operator << (FileStorage& fs, const std::string& value)
  138. {
  139. return fs << cv::String(value);
  140. }
  141. static inline
  142. std::ostream& operator << (std::ostream& os, const String& str)
  143. {
  144. return os << str.c_str();
  145. }
  146. static inline
  147. std::ostream& operator << (std::ostream& out, Ptr<Formatted> fmtd)
  148. {
  149. fmtd->reset();
  150. for(const char* str = fmtd->next(); str; str = fmtd->next())
  151. out << str;
  152. return out;
  153. }
  154. static inline
  155. std::ostream& operator << (std::ostream& out, const Mat& mtx)
  156. {
  157. return out << Formatter::get()->format(mtx);
  158. }
  159. static inline
  160. std::ostream& operator << (std::ostream& out, const UMat& m)
  161. {
  162. return out << m.getMat(ACCESS_READ);
  163. }
  164. template<typename _Tp> static inline
  165. std::ostream& operator << (std::ostream& out, const Complex<_Tp>& c)
  166. {
  167. return out << "(" << c.re << "," << c.im << ")";
  168. }
  169. template<typename _Tp> static inline
  170. std::ostream& operator << (std::ostream& out, const std::vector<Point_<_Tp> >& vec)
  171. {
  172. return out << Formatter::get()->format(Mat(vec));
  173. }
  174. template<typename _Tp> static inline
  175. std::ostream& operator << (std::ostream& out, const std::vector<Point3_<_Tp> >& vec)
  176. {
  177. return out << Formatter::get()->format(Mat(vec));
  178. }
  179. template<typename _Tp, int m, int n> static inline
  180. std::ostream& operator << (std::ostream& out, const Matx<_Tp, m, n>& matx)
  181. {
  182. return out << Formatter::get()->format(Mat(matx));
  183. }
  184. template<typename _Tp> static inline
  185. std::ostream& operator << (std::ostream& out, const Point_<_Tp>& p)
  186. {
  187. out << "[" << p.x << ", " << p.y << "]";
  188. return out;
  189. }
  190. template<typename _Tp> static inline
  191. std::ostream& operator << (std::ostream& out, const Point3_<_Tp>& p)
  192. {
  193. out << "[" << p.x << ", " << p.y << ", " << p.z << "]";
  194. return out;
  195. }
  196. template<typename _Tp, int n> static inline
  197. std::ostream& operator << (std::ostream& out, const Vec<_Tp, n>& vec)
  198. {
  199. out << "[";
  200. if (cv::traits::Depth<_Tp>::value <= CV_32S)
  201. {
  202. for (int i = 0; i < n - 1; ++i) {
  203. out << (int)vec[i] << ", ";
  204. }
  205. out << (int)vec[n-1] << "]";
  206. }
  207. else
  208. {
  209. for (int i = 0; i < n - 1; ++i) {
  210. out << vec[i] << ", ";
  211. }
  212. out << vec[n-1] << "]";
  213. }
  214. return out;
  215. }
  216. template<typename _Tp> static inline
  217. std::ostream& operator << (std::ostream& out, const Size_<_Tp>& size)
  218. {
  219. return out << "[" << size.width << " x " << size.height << "]";
  220. }
  221. template<typename _Tp> static inline
  222. std::ostream& operator << (std::ostream& out, const Rect_<_Tp>& rect)
  223. {
  224. return out << "[" << rect.width << " x " << rect.height << " from (" << rect.x << ", " << rect.y << ")]";
  225. }
  226. static inline std::ostream& operator << (std::ostream& out, const MatSize& msize)
  227. {
  228. int i, dims = msize.dims();
  229. for( i = 0; i < dims; i++ )
  230. {
  231. out << msize[i];
  232. if( i < dims-1 )
  233. out << " x ";
  234. }
  235. return out;
  236. }
  237. static inline std::ostream &operator<< (std::ostream &s, cv::Range &r)
  238. {
  239. return s << "[" << r.start << " : " << r.end << ")";
  240. }
  241. } // cv
  242. #ifdef _MSC_VER
  243. #pragma warning( pop )
  244. #endif
  245. //! @endcond
  246. #endif // OPENCV_CORE_CVSTDINL_HPP