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 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_PRECOMP_H__
44 #define __OPENCV_PRECOMP_H__
45 
46 #ifdef BUILD_PLUGIN
47 #include "opencv2/core/utility.hpp"
48 #else  // BUILD_PLUGIN
49 
50 #include "opencv2/opencv_modules.hpp"
51 #include "cvconfig.h"
52 
53 #include "opencv2/core/utility.hpp"
54 #include "opencv2/core/core_c.h"
55 #include "opencv2/core/cuda.hpp"
56 #include "opencv2/core/opengl.hpp"
57 #include "opencv2/core/va_intel.hpp"
58 
59 #include "opencv2/core/private.hpp"
60 #include "opencv2/core/private.cuda.hpp"
61 #ifdef HAVE_OPENCL
62 #include "opencv2/core/ocl.hpp"
63 #endif
64 
65 #include <assert.h>
66 #include <ctype.h>
67 #include <float.h>
68 #include <limits.h>
69 #include <math.h>
70 #include <stdarg.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 
75 #include <algorithm>
76 #include <cmath>
77 #include <cstdlib>
78 #include <limits>
79 #include <float.h>
80 #include <cstring>
81 #include <cassert>
82 
83 #define USE_SSE2  (cv::checkHardwareSupport(CV_CPU_SSE2))
84 #define USE_SSE4_2  (cv::checkHardwareSupport(CV_CPU_SSE4_2))
85 #define USE_AVX  (cv::checkHardwareSupport(CV_CPU_AVX))
86 #define USE_AVX2  (cv::checkHardwareSupport(CV_CPU_AVX2))
87 
88 #include "opencv2/core/hal/hal.hpp"
89 #include "opencv2/core/hal/intrin.hpp"
90 #include "opencv2/core/sse_utils.hpp"
91 #include "opencv2/core/neon_utils.hpp"
92 #include "opencv2/core/vsx_utils.hpp"
93 #include "hal_replacement.hpp"
94 
95 #define GET_OPTIMIZED(func) (func)
96 
97 namespace cv
98 {
99 
100 // -128.f ... 255.f
101 extern const float g_8x32fTab[];
102 #define CV_8TO32F(x)  cv::g_8x32fTab[(x)+128]
103 
104 extern const ushort g_8x16uSqrTab[];
105 #define CV_SQR_8U(x)  cv::g_8x16uSqrTab[(x)+255]
106 
107 extern const uchar g_Saturate8u[];
108 #define CV_FAST_CAST_8U(t)   (assert(-256 <= (t) && (t) <= 512), cv::g_Saturate8u[(t)+256])
109 #define CV_MIN_8U(a,b)       ((a) - CV_FAST_CAST_8U((a) - (b)))
110 #define CV_MAX_8U(a,b)       ((a) + CV_FAST_CAST_8U((b) - (a)))
111 
112 template<typename T1, typename T2=T1, typename T3=T1> struct OpAdd
113 {
114     typedef T1 type1;
115     typedef T2 type2;
116     typedef T3 rtype;
operator ()cv::OpAdd117     T3 operator ()(const T1 a, const T2 b) const { return saturate_cast<T3>(a + b); }
118 };
119 
120 template<typename T1, typename T2=T1, typename T3=T1> struct OpSub
121 {
122     typedef T1 type1;
123     typedef T2 type2;
124     typedef T3 rtype;
operator ()cv::OpSub125     T3 operator ()(const T1 a, const T2 b) const { return saturate_cast<T3>(a - b); }
126 };
127 
128 template<typename T1, typename T2=T1, typename T3=T1> struct OpRSub
129 {
130     typedef T1 type1;
131     typedef T2 type2;
132     typedef T3 rtype;
operator ()cv::OpRSub133     T3 operator ()(const T1 a, const T2 b) const { return saturate_cast<T3>(b - a); }
134 };
135 
136 template<typename T> struct OpMin
137 {
138     typedef T type1;
139     typedef T type2;
140     typedef T rtype;
operator ()cv::OpMin141     T operator ()(const T a, const T b) const { return std::min(a, b); }
142 };
143 
144 template<typename T> struct OpMax
145 {
146     typedef T type1;
147     typedef T type2;
148     typedef T rtype;
operator ()cv::OpMax149     T operator ()(const T a, const T b) const { return std::max(a, b); }
150 };
151 
152 template<typename T> struct OpAbsDiff
153 {
154     typedef T type1;
155     typedef T type2;
156     typedef T rtype;
operator ()cv::OpAbsDiff157     T operator()(T a, T b) const { return a > b ? a - b : b - a; }
158 };
159 
160 // specializations to prevent "-0" results
161 template<> struct OpAbsDiff<float>
162 {
163     typedef float type1;
164     typedef float type2;
165     typedef float rtype;
operator ()cv::OpAbsDiff166     float operator()(float a, float b) const { return std::abs(a - b); }
167 };
168 template<> struct OpAbsDiff<double>
169 {
170     typedef double type1;
171     typedef double type2;
172     typedef double rtype;
operator ()cv::OpAbsDiff173     double operator()(double a, double b) const { return std::abs(a - b); }
174 };
175 
176 template<typename T> struct OpAnd
177 {
178     typedef T type1;
179     typedef T type2;
180     typedef T rtype;
operator ()cv::OpAnd181     T operator()( T a, T b ) const { return a & b; }
182 };
183 
184 template<typename T> struct OpOr
185 {
186     typedef T type1;
187     typedef T type2;
188     typedef T rtype;
operator ()cv::OpOr189     T operator()( T a, T b ) const { return a | b; }
190 };
191 
192 template<typename T> struct OpXor
193 {
194     typedef T type1;
195     typedef T type2;
196     typedef T rtype;
operator ()cv::OpXor197     T operator()( T a, T b ) const { return a ^ b; }
198 };
199 
200 template<typename T> struct OpNot
201 {
202     typedef T type1;
203     typedef T type2;
204     typedef T rtype;
operator ()cv::OpNot205     T operator()( T a, T ) const { return ~a; }
206 };
207 
operator ()(uchar a,uchar b) const208 template<> inline uchar OpAdd<uchar>::operator ()(uchar a, uchar b) const
209 { return CV_FAST_CAST_8U(a + b); }
210 
operator ()(uchar a,uchar b) const211 template<> inline uchar OpSub<uchar>::operator ()(uchar a, uchar b) const
212 { return CV_FAST_CAST_8U(a - b); }
213 
operator ()(short a,short b) const214 template<> inline short OpAbsDiff<short>::operator ()(short a, short b) const
215 { return saturate_cast<short>(std::abs(a - b)); }
216 
operator ()(schar a,schar b) const217 template<> inline schar OpAbsDiff<schar>::operator ()(schar a, schar b) const
218 { return saturate_cast<schar>(std::abs(a - b)); }
219 
operator ()(uchar a,uchar b) const220 template<> inline uchar OpMin<uchar>::operator ()(uchar a, uchar b) const { return CV_MIN_8U(a, b); }
221 
operator ()(uchar a,uchar b) const222 template<> inline uchar OpMax<uchar>::operator ()(uchar a, uchar b) const { return CV_MAX_8U(a, b); }
223 
224 typedef void (*UnaryFunc)(const uchar* src1, size_t step1,
225                        uchar* dst, size_t step, Size sz,
226                        void*);
227 
228 typedef void (*BinaryFunc)(const uchar* src1, size_t step1,
229                        const uchar* src2, size_t step2,
230                        uchar* dst, size_t step, Size sz,
231                        void*);
232 
233 typedef void (*BinaryFuncC)(const uchar* src1, size_t step1,
234                        const uchar* src2, size_t step2,
235                        uchar* dst, size_t step, int width, int height,
236                        void*);
237 
238 BinaryFunc getConvertFunc(int sdepth, int ddepth);
239 BinaryFunc getConvertScaleFunc(int sdepth, int ddepth);
240 BinaryFunc getCopyMaskFunc(size_t esz);
241 
242 /* default memory block for sparse array elements */
243 #define  CV_SPARSE_MAT_BLOCK     (1<<12)
244 
245 /* initial hash table size */
246 #define  CV_SPARSE_HASH_SIZE0    (1<<10)
247 
248 /* maximal average node_count/hash_size ratio beyond which hash table is resized */
249 #define  CV_SPARSE_HASH_RATIO    3
250 
251 // There is some mess in code with vectors representation.
252 // Both vector-column / vector-rows are used with dims=2 (as Mat2D always).
253 // Reshape matrices if necessary (in case of vectors) and returns size with scaled width.
254 Size getContinuousSize2D(Mat& m1, int widthScale=1);
255 Size getContinuousSize2D(Mat& m1, Mat& m2, int widthScale=1);
256 Size getContinuousSize2D(Mat& m1, Mat& m2, Mat& m3, int widthScale=1);
257 
258 void setSize( Mat& m, int _dims, const int* _sz, const size_t* _steps, bool autoSteps=false );
259 void finalizeHdr(Mat& m);
260 int updateContinuityFlag(int flags, int dims, const int* size, const size_t* step);
261 
262 struct NoVec
263 {
operator ()cv::NoVec264     size_t operator()(const void*, const void*, void*, size_t) const { return 0; }
265 };
266 
267 #define CV_SPLIT_MERGE_MAX_BLOCK_SIZE(cn) ((INT_MAX/4)/(cn)) // HAL implementation accepts 'int' len, so INT_MAX doesn't work here
268 
269 enum { BLOCK_SIZE = 1024 };
270 
271 #if defined HAVE_IPP && (IPP_VERSION_X100 >= 700)
272 #define ARITHM_USE_IPP 1
273 #else
274 #define ARITHM_USE_IPP 0
275 #endif
276 
checkScalar(const Mat & sc,int atype,_InputArray::KindFlag sckind,_InputArray::KindFlag akind)277 inline bool checkScalar(const Mat& sc, int atype, _InputArray::KindFlag sckind, _InputArray::KindFlag akind)
278 {
279     if( sc.dims > 2 || !sc.isContinuous() )
280         return false;
281     Size sz = sc.size();
282     if(sz.width != 1 && sz.height != 1)
283         return false;
284     int cn = CV_MAT_CN(atype);
285     if( akind == _InputArray::MATX && sckind != _InputArray::MATX )
286         return false;
287     return sz == Size(1, 1) || sz == Size(1, cn) || sz == Size(cn, 1) ||
288            (sz == Size(1, 4) && sc.type() == CV_64F && cn <= 4);
289 }
290 
checkScalar(InputArray sc,int atype,_InputArray::KindFlag sckind,_InputArray::KindFlag akind)291 inline bool checkScalar(InputArray sc, int atype, _InputArray::KindFlag sckind, _InputArray::KindFlag akind)
292 {
293     if( sc.dims() > 2 || !sc.isContinuous() )
294         return false;
295     Size sz = sc.size();
296     if(sz.width != 1 && sz.height != 1)
297         return false;
298     int cn = CV_MAT_CN(atype);
299     if( akind == _InputArray::MATX && sckind != _InputArray::MATX )
300         return false;
301     return sz == Size(1, 1) || sz == Size(1, cn) || sz == Size(cn, 1) ||
302            (sz == Size(1, 4) && sc.type() == CV_64F && cn <= 4);
303 }
304 
305 void convertAndUnrollScalar( const Mat& sc, int buftype, uchar* scbuf, size_t blocksize );
306 
307 #ifdef CV_COLLECT_IMPL_DATA
308 struct ImplCollector
309 {
ImplCollectorcv::ImplCollector310     ImplCollector()
311     {
312         useCollection   = false;
313         implFlags       = 0;
314     }
315     bool useCollection; // enable/disable impl data collection
316 
317     int implFlags;
318     std::vector<int>    implCode;
319     std::vector<String> implFun;
320 
321     cv::Mutex mutex;
322 };
323 #endif
324 
325 struct CoreTLSData
326 {
CoreTLSDatacv::CoreTLSData327     CoreTLSData() :
328 //#ifdef HAVE_OPENCL
329         oclExecutionContextInitialized(false), useOpenCL(-1),
330 //#endif
331         useIPP(-1),
332         useIPP_NE(-1)
333 #ifdef HAVE_OPENVX
334         ,useOpenVX(-1)
335 #endif
336     {}
337 
338     RNG rng;
339 //#ifdef HAVE_OPENCL
340     ocl::OpenCLExecutionContext oclExecutionContext;
341     bool oclExecutionContextInitialized;
342     int useOpenCL; // 1 - use, 0 - do not use, -1 - auto/not initialized
343 //#endif
344     int useIPP;    // 1 - use, 0 - do not use, -1 - auto/not initialized
345     int useIPP_NE; // 1 - use, 0 - do not use, -1 - auto/not initialized
346 #ifdef HAVE_OPENVX
347     int useOpenVX; // 1 - use, 0 - do not use, -1 - auto/not initialized
348 #endif
349 };
350 
351 CoreTLSData& getCoreTlsData();
352 
353 #if defined(BUILD_SHARED_LIBS)
354 #if defined _WIN32 || defined WINCE
355 #define CL_RUNTIME_EXPORT __declspec(dllexport)
356 #elif defined __GNUC__ && __GNUC__ >= 4
357 #define CL_RUNTIME_EXPORT __attribute__ ((visibility ("default")))
358 #else
359 #define CL_RUNTIME_EXPORT
360 #endif
361 #else
362 #define CL_RUNTIME_EXPORT
363 #endif
364 
365 extern CV_EXPORTS
366 bool __termination;  // skip some cleanups, because process is terminating
367                      // (for example, if ExitProcess() was already called)
368 
369 cv::Mutex& getInitializationMutex();
370 
371 #define CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, RET_VALUE) \
372     static TYPE* const instance = INITIALIZER; \
373     return RET_VALUE;
374 
375 #define CV_SINGLETON_LAZY_INIT(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, instance)
376 #define CV_SINGLETON_LAZY_INIT_REF(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, *instance)
377 
378 CV_EXPORTS void releaseTlsStorageThread();
379 
380 int cv_snprintf(char* buf, int len, const char* fmt, ...);
381 int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args);
382 }
383 
384 #endif  // BUILD_PLUGIN
385 #endif  // __OPENCV_PRECOMP_H__
386