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 \****************************************************************************************/
215 static inline IppiSize ippiSize(int width, int height)
217 IppiSize size = { width, height };
223 # define IPPI_CALL(func) CV_Assert((func) >= 0)
226 /* IPP-compatible return codes */
227 typedef enum CvStatus
229 CV_BADMEMBLOCK_ERR = -113,
230 CV_INPLACE_NOT_SUPPORTED_ERR= -112,
231 CV_UNMATCHED_ROI_ERR = -111,
232 CV_NOTFOUND_ERR = -110,
233 CV_BADCONVERGENCE_ERR = -109,
235 CV_BADDEPTH_ERR = -107,
236 CV_BADROI_ERR = -106,
237 CV_BADHEADER_ERR = -105,
238 CV_UNMATCHED_FORMATS_ERR = -104,
239 CV_UNSUPPORTED_COI_ERR = -103,
240 CV_UNSUPPORTED_CHANNELS_ERR = -102,
241 CV_UNSUPPORTED_DEPTH_ERR = -101,
242 CV_UNSUPPORTED_FORMAT_ERR = -100,
244 CV_BADARG_ERR = -49, //ipp comp
245 CV_NOTDEFINED_ERR = -48, //ipp comp
247 CV_BADCHANNELS_ERR = -47, //ipp comp
248 CV_BADRANGE_ERR = -44, //ipp comp
249 CV_BADSTEP_ERR = -29, //ipp comp
251 CV_BADFLAG_ERR = -12,
252 CV_DIV_BY_ZERO_ERR = -11, //ipp comp
253 CV_BADCOEF_ERR = -10,
255 CV_BADFACTOR_ERR = -7,
256 CV_BADPOINT_ERR = -6,
257 CV_BADSCALE_ERR = -4,
258 CV_OUTOFMEM_ERR = -3,
268 /****************************************************************************************\
269 * Auxiliary algorithms *
270 \****************************************************************************************/
275 // This function splits the input sequence or set into one or more equivalence classes and
276 // returns the vector of labels - 0-based class indexes for each element.
277 // predicate(a,b) returns true if the two sequence elements certainly belong to the same class.
279 // The algorithm is described in "Introduction to Algorithms"
280 // by Cormen, Leiserson and Rivest, the chapter "Data structures for disjoint sets"
281 template<typename _Tp, class _EqPredicate> int
282 partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
283 _EqPredicate predicate=_EqPredicate())
285 int i, j, N = (int)_vec.size();
286 const _Tp* vec = &_vec[0];
291 std::vector<int> _nodes(N*2);
292 int (*nodes)[2] = (int(*)[2])&_nodes[0];
294 // The first O(N) pass: create N single-vertex trees
295 for(i = 0; i < N; i++)
301 // The main O(N^2) pass: merge connected components
302 for( i = 0; i < N; i++ )
307 while( nodes[root][PARENT] >= 0 )
308 root = nodes[root][PARENT];
310 for( j = 0; j < N; j++ )
312 if( i == j || !predicate(vec[i], vec[j]))
316 while( nodes[root2][PARENT] >= 0 )
317 root2 = nodes[root2][PARENT];
322 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
324 nodes[root2][PARENT] = root;
327 nodes[root][PARENT] = root2;
328 nodes[root2][RANK] += rank == rank2;
331 CV_Assert( nodes[root][PARENT] < 0 );
335 // compress the path from node2 to root
336 while( (parent = nodes[k][PARENT]) >= 0 )
338 nodes[k][PARENT] = root;
342 // compress the path from node to root
344 while( (parent = nodes[k][PARENT]) >= 0 )
346 nodes[k][PARENT] = root;
353 // Final O(N) pass: enumerate classes
357 for( i = 0; i < N; i++ )
360 while( nodes[root][PARENT] >= 0 )
361 root = nodes[root][PARENT];
362 // re-use the rank as the class label
363 if( nodes[root][RANK] >= 0 )
364 nodes[root][RANK] = ~nclasses++;
365 labels[i] = ~nodes[root][RANK];
373 #endif // __OPENCV_CORE_PRIVATE_HPP__