1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
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.
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.
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.
44 #ifndef __OPENCV_CORE_PRIVATE_HPP__
45 #define __OPENCV_CORE_PRIVATE_HPP__
47 #ifndef __OPENCV_BUILD
48 # error this is a private header which should not be used from outside of the OpenCV library
51 #include "opencv2/core.hpp"
55 # if defined __GNUC__ && defined __APPLE__
56 # pragma GCC diagnostic ignored "-Wshadow"
58 # include <Eigen/Core>
59 # include "opencv2/core/eigen.hpp"
63 # include "tbb/tbb_stddef.h"
64 # if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
66 # include "tbb/task.h"
78 typedef tbb::blocked_range<int> BlockedRange;
80 template<typename Body> static inline
81 void parallel_for( const BlockedRange& range, const Body& body )
83 tbb::parallel_for(range, body);
86 typedef tbb::split Split;
88 template<typename Body> static inline
89 void parallel_reduce( const BlockedRange& range, Body& body )
91 tbb::parallel_reduce(range, body);
94 typedef tbb::concurrent_vector<Rect> ConcurrentRectVector;
99 BlockedRange() : _begin(0), _end(0), _grainsize(0) {}
100 BlockedRange(int b, int e, int g=1) : _begin(b), _end(e), _grainsize(g) {}
101 int begin() const { return _begin; }
102 int end() const { return _end; }
103 int grainsize() const { return _grainsize; }
106 int _begin, _end, _grainsize;
109 template<typename Body> static inline
110 void parallel_for( const BlockedRange& range, const Body& body )
114 typedef std::vector<Rect> ConcurrentRectVector;
118 template<typename Body> static inline
119 void parallel_reduce( const BlockedRange& range, Body& body )
125 // Returns a static string if there is a parallel framework,
127 CV_EXPORTS const char* currentParallelFramework();
130 #define CV_INIT_ALGORITHM(classname, algname, memberinit) \
131 static inline ::cv::Algorithm* create##classname##_hidden() \
133 return new classname; \
136 static inline ::cv::Ptr< ::cv::Algorithm> create##classname##_ptr_hidden() \
138 return ::cv::makePtr<classname>(); \
141 static inline ::cv::AlgorithmInfo& classname##_info() \
143 static ::cv::AlgorithmInfo classname##_info_var(algname, create##classname##_hidden); \
144 return classname##_info_var; \
147 static ::cv::AlgorithmInfo& classname##_info_auto = classname##_info(); \
149 ::cv::AlgorithmInfo* classname::info() const \
151 static volatile bool initialized = false; \
155 initialized = true; \
159 return &classname##_info(); \
164 /****************************************************************************************\
165 * Common declarations *
166 \****************************************************************************************/
168 /* the alignment of all the allocated buffers */
169 #define CV_MALLOC_ALIGN 16
172 # define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
173 #elif defined _MSC_VER
174 # define CV_DECL_ALIGNED(x) __declspec(align(x))
176 # define CV_DECL_ALIGNED(x)
179 /* IEEE754 constants and macros */
180 #define CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))
181 #define CV_TOGGLE_DBL(x) ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))
183 static inline void* cvAlignPtr( const void* ptr, int align = 32 )
185 CV_DbgAssert ( (align & (align-1)) == 0 );
186 return (void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) );
189 static inline int cvAlign( int size, int align )
191 CV_DbgAssert( (align & (align-1)) == 0 && size < INT_MAX );
192 return (size + align - 1) & -align;
196 static inline cv::Size cvGetMatSize( const CvMat* mat )
198 return cv::Size(mat->cols, mat->rows);
204 CV_EXPORTS void scalarToRawData(const cv::Scalar& s, void* buf, int type, int unroll_to = 0);
208 /****************************************************************************************\
209 * Structures and macros for integration with IPP *
210 \****************************************************************************************/
213 # ifdef HAVE_IPP_ICV_ONLY
214 # include "ipp_redefine.h"
219 # define IPP_VERSION_X100 (IPP_VERSION_MAJOR * 100 + IPP_VERSION_MINOR)
221 static inline IppiSize ippiSize(int width, int height)
223 IppiSize size = { width, height };
227 static inline IppiSize ippiSize(const cv::Size & _size)
229 IppiSize size = { _size.width, _size.height };
234 # define IPP_VERSION_X100 0
238 # define IPPI_CALL(func) CV_Assert((func) >= 0)
241 /* IPP-compatible return codes */
242 typedef enum CvStatus
244 CV_BADMEMBLOCK_ERR = -113,
245 CV_INPLACE_NOT_SUPPORTED_ERR= -112,
246 CV_UNMATCHED_ROI_ERR = -111,
247 CV_NOTFOUND_ERR = -110,
248 CV_BADCONVERGENCE_ERR = -109,
250 CV_BADDEPTH_ERR = -107,
251 CV_BADROI_ERR = -106,
252 CV_BADHEADER_ERR = -105,
253 CV_UNMATCHED_FORMATS_ERR = -104,
254 CV_UNSUPPORTED_COI_ERR = -103,
255 CV_UNSUPPORTED_CHANNELS_ERR = -102,
256 CV_UNSUPPORTED_DEPTH_ERR = -101,
257 CV_UNSUPPORTED_FORMAT_ERR = -100,
259 CV_BADARG_ERR = -49, //ipp comp
260 CV_NOTDEFINED_ERR = -48, //ipp comp
262 CV_BADCHANNELS_ERR = -47, //ipp comp
263 CV_BADRANGE_ERR = -44, //ipp comp
264 CV_BADSTEP_ERR = -29, //ipp comp
266 CV_BADFLAG_ERR = -12,
267 CV_DIV_BY_ZERO_ERR = -11, //ipp comp
268 CV_BADCOEF_ERR = -10,
270 CV_BADFACTOR_ERR = -7,
271 CV_BADPOINT_ERR = -6,
272 CV_BADSCALE_ERR = -4,
273 CV_OUTOFMEM_ERR = -3,
283 /****************************************************************************************\
284 * Auxiliary algorithms *
285 \****************************************************************************************/
290 // This function splits the input sequence or set into one or more equivalence classes and
291 // returns the vector of labels - 0-based class indexes for each element.
292 // predicate(a,b) returns true if the two sequence elements certainly belong to the same class.
294 // The algorithm is described in "Introduction to Algorithms"
295 // by Cormen, Leiserson and Rivest, the chapter "Data structures for disjoint sets"
296 template<typename _Tp, class _EqPredicate> int
297 partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
298 _EqPredicate predicate=_EqPredicate())
300 int i, j, N = (int)_vec.size();
301 const _Tp* vec = &_vec[0];
306 std::vector<int> _nodes(N*2);
307 int (*nodes)[2] = (int(*)[2])&_nodes[0];
309 // The first O(N) pass: create N single-vertex trees
310 for(i = 0; i < N; i++)
316 // The main O(N^2) pass: merge connected components
317 for( i = 0; i < N; i++ )
322 while( nodes[root][PARENT] >= 0 )
323 root = nodes[root][PARENT];
325 for( j = 0; j < N; j++ )
327 if( i == j || !predicate(vec[i], vec[j]))
331 while( nodes[root2][PARENT] >= 0 )
332 root2 = nodes[root2][PARENT];
337 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
339 nodes[root2][PARENT] = root;
342 nodes[root][PARENT] = root2;
343 nodes[root2][RANK] += rank == rank2;
346 CV_Assert( nodes[root][PARENT] < 0 );
350 // compress the path from node2 to root
351 while( (parent = nodes[k][PARENT]) >= 0 )
353 nodes[k][PARENT] = root;
357 // compress the path from node to root
359 while( (parent = nodes[k][PARENT]) >= 0 )
361 nodes[k][PARENT] = root;
368 // Final O(N) pass: enumerate classes
372 for( i = 0; i < N; i++ )
375 while( nodes[root][PARENT] >= 0 )
376 root = nodes[root][PARENT];
377 // re-use the rank as the class label
378 if( nodes[root][RANK] >= 0 )
379 nodes[root][RANK] = ~nclasses++;
380 labels[i] = ~nodes[root][RANK];
388 #endif // __OPENCV_CORE_PRIVATE_HPP__