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 #include "opencv2/opencv_modules.hpp"
47 #include "cvconfig.h"
48 
49 #include "opencv2/core/utility.hpp"
50 #include "opencv2/core/core_c.h"
51 #include "opencv2/core/cuda.hpp"
52 #include "opencv2/core/opengl.hpp"
53 
54 #include "opencv2/core/private.hpp"
55 #include "opencv2/core/private.cuda.hpp"
56 #include "opencv2/core/ocl.hpp"
57 
58 #include "opencv2/hal.hpp"
59 
60 #include <assert.h>
61 #include <ctype.h>
62 #include <float.h>
63 #include <limits.h>
64 #include <math.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 
69 #ifdef HAVE_TEGRA_OPTIMIZATION
70 #include "opencv2/core/core_tegra.hpp"
71 #else
72 #define GET_OPTIMIZED(func) (func)
73 #endif
74 
75 namespace cv
76 {
77 
78 typedef void (*BinaryFunc)(const uchar* src1, size_t step1,
79                        const uchar* src2, size_t step2,
80                        uchar* dst, size_t step, Size sz,
81                        void*);
82 
83 BinaryFunc getConvertFunc(int sdepth, int ddepth);
84 BinaryFunc getCopyMaskFunc(size_t esz);
85 
86 /* default memory block for sparse array elements */
87 #define  CV_SPARSE_MAT_BLOCK     (1<<12)
88 
89 /* initial hash table size */
90 #define  CV_SPARSE_HASH_SIZE0    (1<<10)
91 
92 /* maximal average node_count/hash_size ratio beyond which hash table is resized */
93 #define  CV_SPARSE_HASH_RATIO    3
94 
95 
96 
97 // -128.f ... 255.f
98 extern const float g_8x32fTab[];
99 #define CV_8TO32F(x)  cv::g_8x32fTab[(x)+128]
100 
101 extern const ushort g_8x16uSqrTab[];
102 #define CV_SQR_8U(x)  cv::g_8x16uSqrTab[(x)+255]
103 
104 extern const uchar g_Saturate8u[];
105 #define CV_FAST_CAST_8U(t)   (assert(-256 <= (t) && (t) <= 512), cv::g_Saturate8u[(t)+256])
106 #define CV_MIN_8U(a,b)       ((a) - CV_FAST_CAST_8U((a) - (b)))
107 #define CV_MAX_8U(a,b)       ((a) + CV_FAST_CAST_8U((b) - (a)))
108 
109 
110 #if defined WIN32 || defined _WIN32
111 void deleteThreadAllocData();
112 #endif
113 
114 template<typename T1, typename T2=T1, typename T3=T1> struct OpAdd
115 {
116     typedef T1 type1;
117     typedef T2 type2;
118     typedef T3 rtype;
operator ()cv::OpAdd119     T3 operator ()(const T1 a, const T2 b) const { return saturate_cast<T3>(a + b); }
120 };
121 
122 template<typename T1, typename T2=T1, typename T3=T1> struct OpSub
123 {
124     typedef T1 type1;
125     typedef T2 type2;
126     typedef T3 rtype;
operator ()cv::OpSub127     T3 operator ()(const T1 a, const T2 b) const { return saturate_cast<T3>(a - b); }
128 };
129 
130 template<typename T1, typename T2=T1, typename T3=T1> struct OpRSub
131 {
132     typedef T1 type1;
133     typedef T2 type2;
134     typedef T3 rtype;
operator ()cv::OpRSub135     T3 operator ()(const T1 a, const T2 b) const { return saturate_cast<T3>(b - a); }
136 };
137 
138 template<typename T> struct OpMin
139 {
140     typedef T type1;
141     typedef T type2;
142     typedef T rtype;
operator ()cv::OpMin143     T operator ()(const T a, const T b) const { return std::min(a, b); }
144 };
145 
146 template<typename T> struct OpMax
147 {
148     typedef T type1;
149     typedef T type2;
150     typedef T rtype;
operator ()cv::OpMax151     T operator ()(const T a, const T b) const { return std::max(a, b); }
152 };
153 
getContinuousSize_(int flags,int cols,int rows,int widthScale)154 inline Size getContinuousSize_( int flags, int cols, int rows, int widthScale )
155 {
156     int64 sz = (int64)cols * rows * widthScale;
157     return (flags & Mat::CONTINUOUS_FLAG) != 0 &&
158         (int)sz == sz ? Size((int)sz, 1) : Size(cols * widthScale, rows);
159 }
160 
getContinuousSize(const Mat & m1,int widthScale=1)161 inline Size getContinuousSize( const Mat& m1, int widthScale=1 )
162 {
163     return getContinuousSize_(m1.flags,
164                               m1.cols, m1.rows, widthScale);
165 }
166 
getContinuousSize(const Mat & m1,const Mat & m2,int widthScale=1)167 inline Size getContinuousSize( const Mat& m1, const Mat& m2, int widthScale=1 )
168 {
169     return getContinuousSize_(m1.flags & m2.flags,
170                               m1.cols, m1.rows, widthScale);
171 }
172 
getContinuousSize(const Mat & m1,const Mat & m2,const Mat & m3,int widthScale=1)173 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
174                                const Mat& m3, int widthScale=1 )
175 {
176     return getContinuousSize_(m1.flags & m2.flags & m3.flags,
177                               m1.cols, m1.rows, widthScale);
178 }
179 
getContinuousSize(const Mat & m1,const Mat & m2,const Mat & m3,const Mat & m4,int widthScale=1)180 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
181                                const Mat& m3, const Mat& m4,
182                                int widthScale=1 )
183 {
184     return getContinuousSize_(m1.flags & m2.flags & m3.flags & m4.flags,
185                               m1.cols, m1.rows, widthScale);
186 }
187 
getContinuousSize(const Mat & m1,const Mat & m2,const Mat & m3,const Mat & m4,const Mat & m5,int widthScale=1)188 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
189                                const Mat& m3, const Mat& m4,
190                                const Mat& m5, int widthScale=1 )
191 {
192     return getContinuousSize_(m1.flags & m2.flags & m3.flags & m4.flags & m5.flags,
193                               m1.cols, m1.rows, widthScale);
194 }
195 
196 struct NoVec
197 {
operator ()cv::NoVec198     size_t operator()(const void*, const void*, void*, size_t) const { return 0; }
199 };
200 
201 extern volatile bool USE_SSE2;
202 extern volatile bool USE_SSE4_2;
203 extern volatile bool USE_AVX;
204 extern volatile bool USE_AVX2;
205 
206 enum { BLOCK_SIZE = 1024 };
207 
208 #if defined HAVE_IPP && (IPP_VERSION_MAJOR >= 7)
209 #define ARITHM_USE_IPP 1
210 #else
211 #define ARITHM_USE_IPP 0
212 #endif
213 
checkScalar(const Mat & sc,int atype,int sckind,int akind)214 inline bool checkScalar(const Mat& sc, int atype, int sckind, int akind)
215 {
216     if( sc.dims > 2 || !sc.isContinuous() )
217         return false;
218     Size sz = sc.size();
219     if(sz.width != 1 && sz.height != 1)
220         return false;
221     int cn = CV_MAT_CN(atype);
222     if( akind == _InputArray::MATX && sckind != _InputArray::MATX )
223         return false;
224     return sz == Size(1, 1) || sz == Size(1, cn) || sz == Size(cn, 1) ||
225            (sz == Size(1, 4) && sc.type() == CV_64F && cn <= 4);
226 }
227 
checkScalar(InputArray sc,int atype,int sckind,int akind)228 inline bool checkScalar(InputArray sc, int atype, int sckind, int akind)
229 {
230     if( sc.dims() > 2 || !sc.isContinuous() )
231         return false;
232     Size sz = sc.size();
233     if(sz.width != 1 && sz.height != 1)
234         return false;
235     int cn = CV_MAT_CN(atype);
236     if( akind == _InputArray::MATX && sckind != _InputArray::MATX )
237         return false;
238     return sz == Size(1, 1) || sz == Size(1, cn) || sz == Size(cn, 1) ||
239            (sz == Size(1, 4) && sc.type() == CV_64F && cn <= 4);
240 }
241 
242 void convertAndUnrollScalar( const Mat& sc, int buftype, uchar* scbuf, size_t blocksize );
243 
244 #ifdef CV_COLLECT_IMPL_DATA
245 struct ImplCollector
246 {
ImplCollectorcv::ImplCollector247     ImplCollector()
248     {
249         useCollection   = false;
250         implFlags       = 0;
251     }
252     bool useCollection; // enable/disable impl data collection
253 
254     int implFlags;
255     std::vector<int>    implCode;
256     std::vector<String> implFun;
257 
258     cv::Mutex mutex;
259 };
260 #endif
261 
262 struct CoreTLSData
263 {
CoreTLSDatacv::CoreTLSData264     CoreTLSData() : device(0), useOpenCL(-1), useIPP(-1)
265     {
266 #ifdef HAVE_TEGRA_OPTIMIZATION
267         useTegra = -1;
268 #endif
269     }
270 
271     RNG rng;
272     int device;
273     ocl::Queue oclQueue;
274     int useOpenCL; // 1 - use, 0 - do not use, -1 - auto/not initialized
275     int useIPP; // 1 - use, 0 - do not use, -1 - auto/not initialized
276 #ifdef HAVE_TEGRA_OPTIMIZATION
277     int useTegra; // 1 - use, 0 - do not use, -1 - auto/not initialized
278 #endif
279 };
280 
281 TLSData<CoreTLSData>& getCoreTlsData();
282 
283 #if defined(BUILD_SHARED_LIBS)
284 #if defined WIN32 || defined _WIN32 || defined WINCE
285 #define CL_RUNTIME_EXPORT __declspec(dllexport)
286 #elif defined __GNUC__ && __GNUC__ >= 4
287 #define CL_RUNTIME_EXPORT __attribute__ ((visibility ("default")))
288 #else
289 #define CL_RUNTIME_EXPORT
290 #endif
291 #else
292 #define CL_RUNTIME_EXPORT
293 #endif
294 
295 #ifndef HAVE_PTHREADS
296 #if !(defined WIN32 || defined _WIN32 || defined WINCE || defined HAVE_WINRT)
297 #define HAVE_PTHREADS 1
298 #endif
299 #endif
300 
301 extern bool __termination; // skip some cleanups, because process is terminating
302                            // (for example, if ExitProcess() was already called)
303 
304 }
305 
306 #include "opencv2/hal/intrin.hpp"
307 
308 #endif /*_CXCORE_INTERNAL_H_*/
309