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.
45 #if defined WIN32 || defined WIN64 || defined _WIN64 || defined WINCE
51 static void __cpuid(int* cpuid_data, int)
76 #include <mach/mach.h>
77 #include <mach/mach_time.h>
93 enum { MAX_FEATURE = CV_HARDWARE_MAX_FEATURE };
97 memset( have, 0, sizeof(have) );
101 static HWFeatures initialize()
104 int cpuid_data[4]={0,0,0,0};
106 #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64)
107 __cpuid(cpuid_data, 1);
108 #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
114 :[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3])
125 : "=a"(cpuid_data[0]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3])
132 f.x86_family = (cpuid_data[0] >> 8) & 15;
133 if( f.x86_family >= 6 )
135 f.have[CV_CPU_MMX] = (cpuid_data[3] & (1 << 23)) != 0;
136 f.have[CV_CPU_SSE] = (cpuid_data[3] & (1<<25)) != 0;
137 f.have[CV_CPU_SSE2] = (cpuid_data[3] & (1<<26)) != 0;
138 f.have[CV_CPU_SSE3] = (cpuid_data[2] & (1<<0)) != 0;
139 f.have[CV_CPU_SSSE3] = (cpuid_data[2] & (1<<9)) != 0;
140 f.have[CV_CPU_SSE4_1] = (cpuid_data[2] & (1<<19)) != 0;
141 f.have[CV_CPU_SSE4_2] = (cpuid_data[2] & (1<<20)) != 0;
142 f.have[CV_CPU_AVX] = (cpuid_data[2] & (1<<28)) != 0;
149 bool have[MAX_FEATURE+1];
152 static HWFeatures featuresEnabled = HWFeatures::initialize(), featuresDisabled = HWFeatures();
153 static HWFeatures* currentFeatures = &featuresEnabled;
155 bool checkHardwareSupport(int feature)
157 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
158 return currentFeatures->have[feature];
162 volatile bool useOptimizedFlag = true;
164 struct IPPInitializer
166 IPPInitializer() { ippStaticInit(); }
169 IPPInitializer ippInitializer;
171 volatile bool useOptimizedFlag = false;
174 void setUseOptimized( bool flag )
176 useOptimizedFlag = flag;
177 currentFeatures = flag ? &featuresEnabled : &featuresDisabled;
182 return useOptimizedFlag;
187 #if defined WIN32 || defined WIN64 || defined _WIN64 || defined WINCE
188 LARGE_INTEGER counter;
189 QueryPerformanceCounter( &counter );
190 return (int64)counter.QuadPart;
191 #elif defined __linux || defined __linux__
193 clock_gettime(CLOCK_MONOTONIC, &tp);
194 return (int64)tp.tv_sec*1000000000 + tp.tv_nsec;
195 #elif defined __MACH__
196 return (int64)mach_absolute_time();
200 gettimeofday( &tv, &tz );
201 return (int64)tv.tv_sec*1000000 + tv.tv_usec;
205 double getTickFrequency()
207 #if defined WIN32 || defined WIN64 || defined _WIN64 || defined WINCE
209 QueryPerformanceFrequency(&freq);
210 return (double)freq.QuadPart;
211 #elif defined __linux || defined __linux__
213 #elif defined __MACH__
214 static double freq = 0;
217 mach_timebase_info_data_t sTimebaseInfo;
218 mach_timebase_info(&sTimebaseInfo);
219 freq = sTimebaseInfo.denom*1e9/sTimebaseInfo.numer;
227 #if defined __GNUC__ && (defined __i386__ || defined __x86_64__ || defined __ppc__)
228 #if defined(__i386__)
230 int64 getCPUTickCount(void)
233 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
236 #elif defined(__x86_64__)
238 int64 getCPUTickCount(void)
241 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
242 return (int64)lo | ((int64)hi << 32);
245 #elif defined(__ppc__)
247 int64 getCPUTickCount(void)
250 unsigned upper, lower, tmp;
258 : "=r"(upper),"=r"(lower),"=r"(tmp)
260 return lower | ((int64)upper << 32);
265 #error "RDTSC not defined"
269 #elif defined _MSC_VER && defined WIN32 && !defined _WIN64
271 int64 getCPUTickCount(void)
279 int64 getCPUTickCount()
281 return getTickCount();
287 static int numThreads = 0;
288 static int numProcs = 0;
290 int getNumThreads(void)
297 void setNumThreads( int
306 numProcs = omp_get_num_procs();
316 threads = MIN( threads, numProcs );
318 numThreads = threads;
325 int getThreadNum(void)
328 return omp_get_thread_num();
335 string format( const char* fmt, ... )
339 va_start( args, fmt );
340 vsprintf( buf, fmt, args );
344 static CvErrorCallback customErrorCallback = 0;
345 static void* customErrorCallbackData = 0;
346 static bool breakOnError = false;
348 bool setBreakOnError(bool value)
350 bool prevVal = breakOnError;
351 breakOnError = value;
355 void error( const Exception& exc )
357 if (customErrorCallback != 0)
358 customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
359 exc.file.c_str(), exc.line, customErrorCallbackData);
362 const char* errorStr = cvErrorStr(exc.code);
365 sprintf( buf, "OpenCV Error: %s (%s) in %s, file %s, line %d",
366 errorStr, exc.err.c_str(), exc.func.size() > 0 ?
367 exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line );
368 fprintf( stderr, "%s\n", buf );
373 static volatile int* p = 0;
380 redirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata)
383 *prevUserdata = customErrorCallbackData;
384 CvErrorCallback prevCallback = customErrorCallback;
385 customErrorCallback = errCallback;
386 customErrorCallbackData = userdata;
394 cvGuiBoxReport( int code, const char *func_name, const char *err_msg,
395 const char *file, int line, void* )
397 #if (!defined WIN32 && !defined WIN64) || defined WINCE
398 return cvStdErrReport( code, func_name, err_msg, file, line, 0 );
400 if( code != CV_StsBackTrace && code != CV_StsAutoTrace )
402 size_t msg_len = strlen(err_msg ? err_msg : "") + 1024;
403 char* message = (char*)alloca(msg_len);
406 wsprintf( message, "%s (%s)\nin function %s, %s(%d)\n\n"
407 "Press \"Abort\" to terminate application.\n"
408 "Press \"Retry\" to debug (if the app is running under debugger).\n"
409 "Press \"Ignore\" to continue (this is not safe).\n",
410 cvErrorStr(code), err_msg ? err_msg : "no description",
411 func_name, file, line );
413 wsprintf( title, "OpenCV GUI Error Handler" );
415 int answer = MessageBox( NULL, message, title, MB_ICONERROR|MB_ABORTRETRYIGNORE|MB_SYSTEMMODAL );
417 if( answer == IDRETRY )
421 return answer != IDIGNORE;
427 CV_IMPL int cvCheckHardwareSupport(int feature)
429 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
430 return cv::currentFeatures->have[feature];
433 CV_IMPL int cvUseOptimized( int flag )
435 int prevMode = cv::useOptimizedFlag;
436 cv::setUseOptimized( flag != 0 );
440 CV_IMPL int64 cvGetTickCount(void)
442 return cv::getTickCount();
445 CV_IMPL double cvGetTickFrequency(void)
447 return cv::getTickFrequency()*1e-6;
450 CV_IMPL void cvSetNumThreads(int nt)
452 cv::setNumThreads(nt);
455 CV_IMPL int cvGetNumThreads()
457 return cv::getNumThreads();
460 CV_IMPL int cvGetThreadNum()
462 return cv::getThreadNum();
466 CV_IMPL CvErrorCallback
467 cvRedirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata)
469 return cv::redirectError(errCallback, userdata, prevUserdata);
472 CV_IMPL int cvNulDevReport( int, const char*, const char*,
473 const char*, int, void* )
478 CV_IMPL int cvStdErrReport( int, const char*, const char*,
479 const char*, int, void* )
484 CV_IMPL int cvGuiBoxReport( int, const char*, const char*,
485 const char*, int, void* )
490 CV_IMPL int cvGetErrInfo( const char**, const char**, const char**, int* )
496 CV_IMPL const char* cvErrorStr( int status )
498 static char buf[256];
502 case CV_StsOk : return "No Error";
503 case CV_StsBackTrace : return "Backtrace";
504 case CV_StsError : return "Unspecified error";
505 case CV_StsInternal : return "Internal error";
506 case CV_StsNoMem : return "Insufficient memory";
507 case CV_StsBadArg : return "Bad argument";
508 case CV_StsNoConv : return "Iterations do not converge";
509 case CV_StsAutoTrace : return "Autotrace call";
510 case CV_StsBadSize : return "Incorrect size of input array";
511 case CV_StsNullPtr : return "Null pointer";
512 case CV_StsDivByZero : return "Division by zero occured";
513 case CV_BadStep : return "Image step is wrong";
514 case CV_StsInplaceNotSupported : return "Inplace operation is not supported";
515 case CV_StsObjectNotFound : return "Requested object was not found";
516 case CV_BadDepth : return "Input image depth is not supported by function";
517 case CV_StsUnmatchedFormats : return "Formats of input arguments do not match";
518 case CV_StsUnmatchedSizes : return "Sizes of input arguments do not match";
519 case CV_StsOutOfRange : return "One of arguments\' values is out of range";
520 case CV_StsUnsupportedFormat : return "Unsupported format or combination of formats";
521 case CV_BadCOI : return "Input COI is not supported";
522 case CV_BadNumChannels : return "Bad number of channels";
523 case CV_StsBadFlag : return "Bad flag (parameter or structure field)";
524 case CV_StsBadPoint : return "Bad parameter of type CvPoint";
525 case CV_StsBadMask : return "Bad type of mask argument";
526 case CV_StsParseError : return "Parsing error";
527 case CV_StsNotImplemented : return "The function/feature is not implemented";
528 case CV_StsBadMemBlock : return "Memory block has been corrupted";
529 case CV_StsAssert : return "Assertion failed";
532 sprintf(buf, "Unknown %s code %d", status >= 0 ? "status":"error", status);
536 CV_IMPL int cvGetErrMode(void)
541 CV_IMPL int cvSetErrMode(int)
546 CV_IMPL int cvGetErrStatus()
551 CV_IMPL void cvSetErrStatus(int)
556 CV_IMPL void cvError( int code, const char* func_name,
558 const char* file_name, int line )
560 cv::error(cv::Exception(code, err_msg, func_name, file_name, line));
563 /* function, which converts int to int */
565 cvErrorFromIppStatus( int status )
569 case CV_BADSIZE_ERR: return CV_StsBadSize;
570 case CV_BADMEMBLOCK_ERR: return CV_StsBadMemBlock;
571 case CV_NULLPTR_ERR: return CV_StsNullPtr;
572 case CV_DIV_BY_ZERO_ERR: return CV_StsDivByZero;
573 case CV_BADSTEP_ERR: return CV_BadStep ;
574 case CV_OUTOFMEM_ERR: return CV_StsNoMem;
575 case CV_BADARG_ERR: return CV_StsBadArg;
576 case CV_NOTDEFINED_ERR: return CV_StsError;
577 case CV_INPLACE_NOT_SUPPORTED_ERR: return CV_StsInplaceNotSupported;
578 case CV_NOTFOUND_ERR: return CV_StsObjectNotFound;
579 case CV_BADCONVERGENCE_ERR: return CV_StsNoConv;
580 case CV_BADDEPTH_ERR: return CV_BadDepth;
581 case CV_UNMATCHED_FORMATS_ERR: return CV_StsUnmatchedFormats;
582 case CV_UNSUPPORTED_COI_ERR: return CV_BadCOI;
583 case CV_UNSUPPORTED_CHANNELS_ERR: return CV_BadNumChannels;
584 case CV_BADFLAG_ERR: return CV_StsBadFlag;
585 case CV_BADRANGE_ERR: return CV_StsBadArg;
586 case CV_BADCOEF_ERR: return CV_StsBadArg;
587 case CV_BADFACTOR_ERR: return CV_StsBadArg;
588 case CV_BADPOINT_ERR: return CV_StsBadPoint;
590 default: return CV_StsError;
594 static CvModuleInfo cxcore_info = { 0, "cxcore", CV_VERSION, 0 };
596 CvModuleInfo *CvModule::first = 0, *CvModule::last = 0;
598 CvModule::CvModule( CvModuleInfo* _info )
600 cvRegisterModule( _info );
604 CvModule::~CvModule()
608 CvModuleInfo* p = first;
609 for( ; p != 0 && p->next != info; p = p->next )
612 p->next = info->next;
623 cvRegisterModule( const CvModuleInfo* module )
625 CV_Assert( module != 0 && module->name != 0 && module->version != 0 );
627 size_t name_len = strlen(module->name);
628 size_t version_len = strlen(module->version);
630 CvModuleInfo* module_copy = (CvModuleInfo*)malloc( sizeof(*module_copy) +
631 name_len + 1 + version_len + 1 );
633 *module_copy = *module;
634 module_copy->name = (char*)(module_copy + 1);
635 module_copy->version = (char*)(module_copy + 1) + name_len + 1;
637 memcpy( (void*)module_copy->name, module->name, name_len + 1 );
638 memcpy( (void*)module_copy->version, module->version, version_len + 1 );
639 module_copy->next = 0;
641 if( CvModule::first == 0 )
642 CvModule::first = module_copy;
644 CvModule::last->next = module_copy;
645 CvModule::last = module_copy;
649 CvModule cxcore_module( &cxcore_info );
652 cvGetModuleInfo( const char* name, const char **version, const char **plugin_list )
654 static char joint_verinfo[1024] = "";
655 static char plugin_list_buf[1024] = "";
663 CvModuleInfo* module;
669 size_t i, name_len = strlen(name);
671 for( module = CvModule::first; module != 0; module = module->next )
673 if( strlen(module->name) == name_len )
675 for( i = 0; i < name_len; i++ )
677 int c0 = toupper(module->name[i]), c1 = toupper(name[i]);
686 CV_Error( CV_StsObjectNotFound, "The module is not found" );
688 *version = module->version;
692 char* ptr = joint_verinfo;
694 for( module = CvModule::first; module != 0; module = module->next )
696 sprintf( ptr, "%s: %s%s", module->name, module->version, module->next ? ", " : "" );
700 *version = joint_verinfo;
705 *plugin_list = plugin_list_buf;
708 #if defined CVAPI_EXPORTS && defined WIN32 && !defined WINCE
709 BOOL WINAPI DllMain( HINSTANCE, DWORD fdwReason, LPVOID )
711 if( fdwReason == DLL_THREAD_DETACH || fdwReason == DLL_PROCESS_DETACH )
713 cv::deleteThreadAllocData();
714 cv::deleteThreadRNGData();