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 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "precomp.hpp"
45 #if defined WIN32 || defined _WIN32 || defined WINCE
46 #ifndef _WIN32_WINNT // This is needed for the declaration of TryEnterCriticalSection in winbase.h with Visual Studio 2005 (and older?)
47 #define _WIN32_WINNT 0x0400 // http://msdn.microsoft.com/en-us/library/ms686857(VS.85).aspx
59 static void __cpuid(int* cpuid_data, int)
83 #if defined __MACH__ && defined __APPLE__
84 #include <mach/mach.h>
85 #include <mach/mach_time.h>
96 #if defined __linux__ || defined __APPLE__
99 #include <sys/types.h>
101 #include <sys/sysconf.h>
103 #include <sys/sysctl.h>
108 # include <android/log.h>
114 Exception::Exception() { code = 0; line = 0; }
116 Exception::Exception(int _code, const string& _err, const string& _func, const string& _file, int _line)
117 : code(_code), err(_err), func(_func), file(_file), line(_line)
122 Exception::~Exception() throw() {}
125 \return the error description and the context as a text string.
127 const char* Exception::what() const throw() { return msg.c_str(); }
129 void Exception::formatMessage()
131 if( func.size() > 0 )
132 msg = format("%s:%d: error: (%d) %s in function %s\n", file.c_str(), line, code, err.c_str(), func.c_str());
134 msg = format("%s:%d: error: (%d) %s\n", file.c_str(), line, code, err.c_str());
139 enum { MAX_FEATURE = CV_HARDWARE_MAX_FEATURE };
143 memset( have, 0, sizeof(have) );
147 static HWFeatures initialize(void)
150 int cpuid_data[4] = { 0, 0, 0, 0 };
152 #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64)
153 __cpuid(cpuid_data, 1);
154 #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
160 :[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3])
171 : "=a"(cpuid_data[0]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3])
178 f.x86_family = (cpuid_data[0] >> 8) & 15;
179 if( f.x86_family >= 6 )
181 f.have[CV_CPU_MMX] = (cpuid_data[3] & (1 << 23)) != 0;
182 f.have[CV_CPU_SSE] = (cpuid_data[3] & (1<<25)) != 0;
183 f.have[CV_CPU_SSE2] = (cpuid_data[3] & (1<<26)) != 0;
184 f.have[CV_CPU_SSE3] = (cpuid_data[2] & (1<<0)) != 0;
185 f.have[CV_CPU_SSSE3] = (cpuid_data[2] & (1<<9)) != 0;
186 f.have[CV_CPU_SSE4_1] = (cpuid_data[2] & (1<<19)) != 0;
187 f.have[CV_CPU_SSE4_2] = (cpuid_data[2] & (1<<20)) != 0;
188 f.have[CV_CPU_POPCNT] = (cpuid_data[2] & (1<<23)) != 0;
189 f.have[CV_CPU_AVX] = (((cpuid_data[2] & (1<<28)) != 0)&&((cpuid_data[2] & (1<<27)) != 0));//OS uses XSAVE_XRSTORE and CPU support AVX
196 bool have[MAX_FEATURE+1];
199 static HWFeatures featuresEnabled = HWFeatures::initialize(), featuresDisabled = HWFeatures();
200 static HWFeatures* currentFeatures = &featuresEnabled;
202 bool checkHardwareSupport(int feature)
204 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
205 return currentFeatures->have[feature];
209 volatile bool useOptimizedFlag = true;
211 struct IPPInitializer
213 IPPInitializer(void) { ippStaticInit(); }
216 IPPInitializer ippInitializer;
219 volatile bool USE_SSE2 = featuresEnabled.have[CV_CPU_SSE2];
220 volatile bool USE_SSE4_2 = featuresEnabled.have[CV_CPU_SSE4_2];
221 volatile bool USE_AVX = featuresEnabled.have[CV_CPU_AVX];
223 void setUseOptimized( bool flag )
225 useOptimizedFlag = flag;
226 currentFeatures = flag ? &featuresEnabled : &featuresDisabled;
227 USE_SSE2 = currentFeatures->have[CV_CPU_SSE2];
230 bool useOptimized(void)
232 return useOptimizedFlag;
235 int64 getTickCount(void)
237 #if defined WIN32 || defined _WIN32 || defined WINCE
238 LARGE_INTEGER counter;
239 QueryPerformanceCounter( &counter );
240 return (int64)counter.QuadPart;
241 #elif defined __linux || defined __linux__
243 clock_gettime(CLOCK_MONOTONIC, &tp);
244 return (int64)tp.tv_sec*1000000000 + tp.tv_nsec;
245 #elif defined __MACH__ && defined __APPLE__
246 return (int64)mach_absolute_time();
250 gettimeofday( &tv, &tz );
251 return (int64)tv.tv_sec*1000000 + tv.tv_usec;
255 double getTickFrequency(void)
257 #if defined WIN32 || defined _WIN32 || defined WINCE
259 QueryPerformanceFrequency(&freq);
260 return (double)freq.QuadPart;
261 #elif defined __linux || defined __linux__
263 #elif defined __MACH__ && defined __APPLE__
264 static double freq = 0;
267 mach_timebase_info_data_t sTimebaseInfo;
268 mach_timebase_info(&sTimebaseInfo);
269 freq = sTimebaseInfo.denom*1e9/sTimebaseInfo.numer;
277 #if defined __GNUC__ && (defined __i386__ || defined __x86_64__ || defined __ppc__)
278 #if defined(__i386__)
280 int64 getCPUTickCount(void)
283 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
286 #elif defined(__x86_64__)
288 int64 getCPUTickCount(void)
291 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
292 return (int64)lo | ((int64)hi << 32);
295 #elif defined(__ppc__)
297 int64 getCPUTickCount(void)
300 unsigned upper, lower, tmp;
308 : "=r"(upper),"=r"(lower),"=r"(tmp)
310 return lower | ((int64)upper << 32);
315 #error "RDTSC not defined"
319 #elif defined _MSC_VER && defined WIN32 && defined _M_IX86
321 int64 getCPUTickCount(void)
330 int64 getCPUTickCount(void)
332 return ippGetCpuClocks();
335 int64 getCPUTickCount(void)
337 return getTickCount();
343 const std::string& getBuildInformation()
345 static std::string build_info =
346 #include "version_string.inc"
351 string format( const char* fmt, ... )
355 va_start( args, fmt );
356 vsprintf( buf, fmt, args );
360 string tempfile( const char* suffix )
362 const char *temp_dir = getenv("OPENCV_TEMP_PATH");
365 #if defined WIN32 || defined _WIN32
366 char temp_dir2[MAX_PATH + 1] = { 0 };
367 char temp_file[MAX_PATH + 1] = { 0 };
369 if (temp_dir == 0 || temp_dir[0] == 0)
371 ::GetTempPathA(sizeof(temp_dir2), temp_dir2);
372 temp_dir = temp_dir2;
374 if(0 == ::GetTempFileNameA(temp_dir, "ocv", 0, temp_file))
377 DeleteFileA(temp_file);
382 //char defaultTemplate[] = "/mnt/sdcard/__opencv_temp.XXXXXX";
383 char defaultTemplate[] = "/data/local/tmp/__opencv_temp.XXXXXX";
385 char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX";
388 if (temp_dir == 0 || temp_dir[0] == 0)
389 fname = defaultTemplate;
393 char ech = fname[fname.size() - 1];
394 if(ech != '/' && ech != '\\')
396 fname += "__opencv_temp.XXXXXX";
399 const int fd = mkstemp((char*)fname.c_str());
400 if (fd == -1) return string();
403 remove(fname.c_str());
408 if (suffix[0] != '.')
409 return fname + "." + suffix;
411 return fname + suffix;
416 static CvErrorCallback customErrorCallback = 0;
417 static void* customErrorCallbackData = 0;
418 static bool breakOnError = false;
420 bool setBreakOnError(bool value)
422 bool prevVal = breakOnError;
423 breakOnError = value;
427 void error( const Exception& exc )
429 if (customErrorCallback != 0)
430 customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
431 exc.file.c_str(), exc.line, customErrorCallbackData);
434 const char* errorStr = cvErrorStr(exc.code);
437 sprintf( buf, "OpenCV Error: %s (%s) in %s, file %s, line %d",
438 errorStr, exc.err.c_str(), exc.func.size() > 0 ?
439 exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line );
440 fprintf( stderr, "%s\n", buf );
443 __android_log_print(ANDROID_LOG_ERROR, "cv::error()", "%s", buf);
449 static volatile int* p = 0;
457 redirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata)
460 *prevUserdata = customErrorCallbackData;
462 CvErrorCallback prevCallback = customErrorCallback;
464 customErrorCallback = errCallback;
465 customErrorCallbackData = userdata;
473 cvGuiBoxReport( int code, const char *func_name, const char *err_msg,
474 const char *file, int line, void* )
476 #if (!defined WIN32 && !defined _WIN32) || defined WINCE
477 return cvStdErrReport( code, func_name, err_msg, file, line, 0 );
479 if( code != CV_StsBackTrace && code != CV_StsAutoTrace )
481 size_t msg_len = strlen(err_msg ? err_msg : "") + 1024;
482 char* message = (char*)alloca(msg_len);
485 wsprintf( message, "%s (%s)\nin function %s, %s(%d)\n\n"
486 "Press \"Abort\" to terminate application.\n"
487 "Press \"Retry\" to debug (if the app is running under debugger).\n"
488 "Press \"Ignore\" to continue (this is not safe).\n",
489 cvErrorStr(code), err_msg ? err_msg : "no description",
490 func_name, file, line );
492 wsprintf( title, "OpenCV GUI Error Handler" );
494 int answer = MessageBox( NULL, message, title, MB_ICONERROR|MB_ABORTRETRYIGNORE|MB_SYSTEMMODAL );
496 if( answer == IDRETRY )
500 return answer != IDIGNORE;
506 CV_IMPL int cvCheckHardwareSupport(int feature)
508 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
509 return cv::currentFeatures->have[feature];
512 CV_IMPL int cvUseOptimized( int flag )
514 int prevMode = cv::useOptimizedFlag;
515 cv::setUseOptimized( flag != 0 );
519 CV_IMPL int64 cvGetTickCount(void)
521 return cv::getTickCount();
524 CV_IMPL double cvGetTickFrequency(void)
526 return cv::getTickFrequency()*1e-6;
529 CV_IMPL CvErrorCallback
530 cvRedirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata)
532 return cv::redirectError(errCallback, userdata, prevUserdata);
535 CV_IMPL int cvNulDevReport( int, const char*, const char*,
536 const char*, int, void* )
541 CV_IMPL int cvStdErrReport( int, const char*, const char*,
542 const char*, int, void* )
547 CV_IMPL int cvGuiBoxReport( int, const char*, const char*,
548 const char*, int, void* )
553 CV_IMPL int cvGetErrInfo( const char**, const char**, const char**, int* )
559 CV_IMPL const char* cvErrorStr( int status )
561 static char buf[256];
565 case CV_StsOk : return "No Error";
566 case CV_StsBackTrace : return "Backtrace";
567 case CV_StsError : return "Unspecified error";
568 case CV_StsInternal : return "Internal error";
569 case CV_StsNoMem : return "Insufficient memory";
570 case CV_StsBadArg : return "Bad argument";
571 case CV_StsNoConv : return "Iterations do not converge";
572 case CV_StsAutoTrace : return "Autotrace call";
573 case CV_StsBadSize : return "Incorrect size of input array";
574 case CV_StsNullPtr : return "Null pointer";
575 case CV_StsDivByZero : return "Division by zero occured";
576 case CV_BadStep : return "Image step is wrong";
577 case CV_StsInplaceNotSupported : return "Inplace operation is not supported";
578 case CV_StsObjectNotFound : return "Requested object was not found";
579 case CV_BadDepth : return "Input image depth is not supported by function";
580 case CV_StsUnmatchedFormats : return "Formats of input arguments do not match";
581 case CV_StsUnmatchedSizes : return "Sizes of input arguments do not match";
582 case CV_StsOutOfRange : return "One of arguments\' values is out of range";
583 case CV_StsUnsupportedFormat : return "Unsupported format or combination of formats";
584 case CV_BadCOI : return "Input COI is not supported";
585 case CV_BadNumChannels : return "Bad number of channels";
586 case CV_StsBadFlag : return "Bad flag (parameter or structure field)";
587 case CV_StsBadPoint : return "Bad parameter of type CvPoint";
588 case CV_StsBadMask : return "Bad type of mask argument";
589 case CV_StsParseError : return "Parsing error";
590 case CV_StsNotImplemented : return "The function/feature is not implemented";
591 case CV_StsBadMemBlock : return "Memory block has been corrupted";
592 case CV_StsAssert : return "Assertion failed";
593 case CV_GpuNotSupported : return "No GPU support";
594 case CV_GpuApiCallError : return "Gpu API call";
595 case CV_OpenGlNotSupported : return "No OpenGL support";
596 case CV_OpenGlApiCallError : return "OpenGL API call";
599 sprintf(buf, "Unknown %s code %d", status >= 0 ? "status":"error", status);
603 CV_IMPL int cvGetErrMode(void)
608 CV_IMPL int cvSetErrMode(int)
613 CV_IMPL int cvGetErrStatus(void)
618 CV_IMPL void cvSetErrStatus(int)
623 CV_IMPL void cvError( int code, const char* func_name,
625 const char* file_name, int line )
627 cv::error(cv::Exception(code, err_msg, func_name, file_name, line));
630 /* function, which converts int to int */
632 cvErrorFromIppStatus( int status )
636 case CV_BADSIZE_ERR: return CV_StsBadSize;
637 case CV_BADMEMBLOCK_ERR: return CV_StsBadMemBlock;
638 case CV_NULLPTR_ERR: return CV_StsNullPtr;
639 case CV_DIV_BY_ZERO_ERR: return CV_StsDivByZero;
640 case CV_BADSTEP_ERR: return CV_BadStep;
641 case CV_OUTOFMEM_ERR: return CV_StsNoMem;
642 case CV_BADARG_ERR: return CV_StsBadArg;
643 case CV_NOTDEFINED_ERR: return CV_StsError;
644 case CV_INPLACE_NOT_SUPPORTED_ERR: return CV_StsInplaceNotSupported;
645 case CV_NOTFOUND_ERR: return CV_StsObjectNotFound;
646 case CV_BADCONVERGENCE_ERR: return CV_StsNoConv;
647 case CV_BADDEPTH_ERR: return CV_BadDepth;
648 case CV_UNMATCHED_FORMATS_ERR: return CV_StsUnmatchedFormats;
649 case CV_UNSUPPORTED_COI_ERR: return CV_BadCOI;
650 case CV_UNSUPPORTED_CHANNELS_ERR: return CV_BadNumChannels;
651 case CV_BADFLAG_ERR: return CV_StsBadFlag;
652 case CV_BADRANGE_ERR: return CV_StsBadArg;
653 case CV_BADCOEF_ERR: return CV_StsBadArg;
654 case CV_BADFACTOR_ERR: return CV_StsBadArg;
655 case CV_BADPOINT_ERR: return CV_StsBadPoint;
662 static CvModuleInfo cxcore_info = { 0, "cxcore", CV_VERSION, 0 };
664 CvModuleInfo* CvModule::first = 0, *CvModule::last = 0;
666 CvModule::CvModule( CvModuleInfo* _info )
668 cvRegisterModule( _info );
672 CvModule::~CvModule(void)
676 CvModuleInfo* p = first;
677 for( ; p != 0 && p->next != info; p = p->next )
681 p->next = info->next;
695 cvRegisterModule( const CvModuleInfo* module )
697 CV_Assert( module != 0 && module->name != 0 && module->version != 0 );
699 size_t name_len = strlen(module->name);
700 size_t version_len = strlen(module->version);
702 CvModuleInfo* module_copy = (CvModuleInfo*)malloc( sizeof(*module_copy) +
703 name_len + 1 + version_len + 1 );
705 *module_copy = *module;
706 module_copy->name = (char*)(module_copy + 1);
707 module_copy->version = (char*)(module_copy + 1) + name_len + 1;
709 memcpy( (void*)module_copy->name, module->name, name_len + 1 );
710 memcpy( (void*)module_copy->version, module->version, version_len + 1 );
711 module_copy->next = 0;
713 if( CvModule::first == 0 )
714 CvModule::first = module_copy;
716 CvModule::last->next = module_copy;
718 CvModule::last = module_copy;
723 CvModule cxcore_module( &cxcore_info );
726 cvGetModuleInfo( const char* name, const char **version, const char **plugin_list )
728 static char joint_verinfo[1024] = "";
729 static char plugin_list_buf[1024] = "";
737 CvModuleInfo* module;
743 size_t i, name_len = strlen(name);
745 for( module = CvModule::first; module != 0; module = module->next )
747 if( strlen(module->name) == name_len )
749 for( i = 0; i < name_len; i++ )
751 int c0 = toupper(module->name[i]), c1 = toupper(name[i]);
760 CV_Error( CV_StsObjectNotFound, "The module is not found" );
762 *version = module->version;
766 char* ptr = joint_verinfo;
768 for( module = CvModule::first; module != 0; module = module->next )
770 sprintf( ptr, "%s: %s%s", module->name, module->version, module->next ? ", " : "" );
774 *version = joint_verinfo;
779 *plugin_list = plugin_list_buf;
782 #if defined BUILD_SHARED_LIBS && defined CVAPI_EXPORTS && defined WIN32 && !defined WINCE
783 BOOL WINAPI DllMain( HINSTANCE, DWORD fdwReason, LPVOID );
785 BOOL WINAPI DllMain( HINSTANCE, DWORD fdwReason, LPVOID )
787 if( fdwReason == DLL_THREAD_DETACH || fdwReason == DLL_PROCESS_DETACH )
789 cv::deleteThreadAllocData();
790 cv::deleteThreadRNGData();
799 #if defined WIN32 || defined _WIN32 || defined WINCE
803 Impl() { InitializeCriticalSection(&cs); refcount = 1; }
804 ~Impl() { DeleteCriticalSection(&cs); }
806 void lock() { EnterCriticalSection(&cs); }
807 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
808 void unlock() { LeaveCriticalSection(&cs); }
815 int _interlockedExchangeAdd(int* addr, int delta)
817 #if defined _MSC_VER && _MSC_VER >= 1500
818 return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
820 return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
825 #elif defined __APPLE__
827 #include <libkern/OSAtomic.h>
831 Impl() { sl = OS_SPINLOCK_INIT; refcount = 1; }
834 void lock() { OSSpinLockLock(&sl); }
835 bool trylock() { return OSSpinLockTry(&sl); }
836 void unlock() { OSSpinLockUnlock(&sl); }
842 #elif defined __linux__ && !defined ANDROID
846 Impl() { pthread_spin_init(&sl, 0); refcount = 1; }
847 ~Impl() { pthread_spin_destroy(&sl); }
849 void lock() { pthread_spin_lock(&sl); }
850 bool trylock() { return pthread_spin_trylock(&sl) == 0; }
851 void unlock() { pthread_spin_unlock(&sl); }
853 pthread_spinlock_t sl;
861 Impl() { pthread_mutex_init(&sl, 0); refcount = 1; }
862 ~Impl() { pthread_mutex_destroy(&sl); }
864 void lock() { pthread_mutex_lock(&sl); }
865 bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
866 void unlock() { pthread_mutex_unlock(&sl); }
876 impl = new Mutex::Impl;
881 if( CV_XADD(&impl->refcount, -1) == 1 )
886 Mutex::Mutex(const Mutex& m)
889 CV_XADD(&impl->refcount, 1);
892 Mutex& Mutex::operator = (const Mutex& m)
894 CV_XADD(&m.impl->refcount, 1);
895 if( CV_XADD(&impl->refcount, -1) == 1 )
901 void Mutex::lock() { impl->lock(); }
902 void Mutex::unlock() { impl->unlock(); }
903 bool Mutex::trylock() { return impl->trylock(); }