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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
44 #if defined WIN32 || defined WIN64
57 typedef struct CvContext
61 CvErrorCallback error_callback;
64 CvStackRecord err_ctx;
67 #if defined WIN32 || defined WIN64
68 #define CV_DEFAULT_ERROR_CALLBACK cvGuiBoxReport
70 #define CV_DEFAULT_ERROR_CALLBACK cvStdErrReport
74 icvCreateContext(void)
76 CvContext* context = (CvContext*)malloc( sizeof(*context) );
78 context->err_mode = CV_ErrModeLeaf;
79 context->err_code = CV_StsOk;
81 context->error_callback = CV_DEFAULT_ERROR_CALLBACK;
82 context->userdata = 0;
88 icvDestroyContext(CvContext* context)
93 #if defined WIN32 || defined WIN64
94 static DWORD g_TlsIndex = TLS_OUT_OF_INDEXES;
96 static pthread_key_t g_TlsIndex;
103 #if defined WIN32 || defined WIN64
106 //assert(g_TlsIndex != TLS_OUT_OF_INDEXES);
107 if( g_TlsIndex == TLS_OUT_OF_INDEXES )
109 g_TlsIndex = TlsAlloc();
110 if( g_TlsIndex == TLS_OUT_OF_INDEXES )
111 FatalAppExit( 0, "Only set CV_DLL for DLL usage" );
114 context = (CvContext*)TlsGetValue( g_TlsIndex );
117 context = icvCreateContext();
119 FatalAppExit( 0, "OpenCV. Problem to allocate memory for TLS OpenCV context." );
121 TlsSetValue( g_TlsIndex, context );
125 CvContext* context = (CvContext*)pthread_getspecific( g_TlsIndex );
128 context = icvCreateContext();
131 fprintf(stderr,"OpenCV. Problem to allocate memory for OpenCV context.");
134 pthread_setspecific( g_TlsIndex, context );
138 #else /* static single-thread library case */
139 static CvContext* context = 0;
142 context = icvCreateContext();
150 cvStdErrReport( int code, const char *func_name, const char *err_msg,
151 const char *file, int line, void* )
153 if( code == CV_StsBackTrace || code == CV_StsAutoTrace )
154 fprintf( stderr, "\tcalled from " );
156 fprintf( stderr, "OpenCV ERROR: %s (%s)\n\tin function ",
157 cvErrorStr(code), err_msg ? err_msg : "no description" );
159 fprintf( stderr, "%s, %s(%d)\n", func_name ? func_name : "<unknown>",
160 file != NULL ? file : "", line );
162 if( cvGetErrMode() == CV_ErrModeLeaf )
164 fprintf( stderr, "Terminating the application...\n" );
173 cvGuiBoxReport( int code, const char *func_name, const char *err_msg,
174 const char *file, int line, void* )
176 #if !defined WIN32 && !defined WIN64
177 return cvStdErrReport( code, func_name, err_msg, file, line, 0 );
179 if( code != CV_StsBackTrace && code != CV_StsAutoTrace )
181 size_t msg_len = strlen(err_msg ? err_msg : "") + 1024;
182 char* message = (char*)alloca(msg_len);
185 wsprintf( message, "%s (%s)\nin function %s, %s(%d)\n\n"
186 "Press \"Abort\" to terminate application.\n"
187 "Press \"Retry\" to debug (if the app is running under debugger).\n"
188 "Press \"Ignore\" to continue (this is not safe).\n",
189 cvErrorStr(code), err_msg ? err_msg : "no description",
190 func_name, file, line );
192 wsprintf( title, "OpenCV GUI Error Handler" );
194 int answer = MessageBox( NULL, message, title, MB_ICONERROR|MB_ABORTRETRYIGNORE|MB_SYSTEMMODAL );
196 if( answer == IDRETRY )
200 return answer != IDIGNORE;
207 CV_IMPL int cvNulDevReport( int /*code*/, const char* /*func_name*/,
208 const char* /*err_msg*/, const char* /*file*/, int /*line*/, void* )
210 return cvGetErrMode() == CV_ErrModeLeaf;
214 CV_IMPL CvErrorCallback
215 cvRedirectError( CvErrorCallback func, void* userdata, void** prev_userdata )
217 CvContext* context = icvGetContext();
219 CvErrorCallback old = context->error_callback;
221 *prev_userdata = context->userdata;
224 context->error_callback = func;
225 context->userdata = userdata;
229 context->error_callback = CV_DEFAULT_ERROR_CALLBACK;
230 context->userdata = 0;
237 CV_IMPL int cvGetErrInfo( const char** errorcode_desc, const char** description,
238 const char** filename, int* line )
240 int code = cvGetErrStatus();
243 *errorcode_desc = cvErrorStr( code );
256 CvContext* ctx = icvGetContext();
259 *description = ctx->err_msg;
261 *filename = ctx->err_ctx.file;
263 *line = ctx->err_ctx.line;
270 CV_IMPL const char* cvErrorStr( int status )
272 static char buf[256];
276 case CV_StsOk : return "No Error";
277 case CV_StsBackTrace : return "Backtrace";
278 case CV_StsError : return "Unspecified error";
279 case CV_StsInternal : return "Internal error";
280 case CV_StsNoMem : return "Insufficient memory";
281 case CV_StsBadArg : return "Bad argument";
282 case CV_StsNoConv : return "Iterations do not converge";
283 case CV_StsAutoTrace : return "Autotrace call";
284 case CV_StsBadSize : return "Incorrect size of input array";
285 case CV_StsNullPtr : return "Null pointer";
286 case CV_StsDivByZero : return "Divizion by zero occured";
287 case CV_BadStep : return "Image step is wrong";
288 case CV_StsInplaceNotSupported : return "Inplace operation is not supported";
289 case CV_StsObjectNotFound : return "Requested object was not found";
290 case CV_BadDepth : return "Input image depth is not supported by function";
291 case CV_StsUnmatchedFormats : return "Formats of input arguments do not match";
292 case CV_StsUnmatchedSizes : return "Sizes of input arguments do not match";
293 case CV_StsOutOfRange : return "One of arguments\' values is out of range";
294 case CV_StsUnsupportedFormat : return "Unsupported format or combination of formats";
295 case CV_BadCOI : return "Input COI is not supported";
296 case CV_BadNumChannels : return "Bad number of channels";
297 case CV_StsBadFlag : return "Bad flag (parameter or structure field)";
298 case CV_StsBadPoint : return "Bad parameter of type CvPoint";
299 case CV_StsBadMask : return "Bad type of mask argument";
300 case CV_StsParseError : return "Parsing error";
301 case CV_StsNotImplemented : return "The function/feature is not implemented";
302 case CV_StsBadMemBlock : return "Memory block has been corrupted";
305 sprintf(buf, "Unknown %s code %d", status >= 0 ? "status":"error", status);
309 CV_IMPL int cvGetErrMode(void)
311 return icvGetContext()->err_mode;
314 CV_IMPL int cvSetErrMode( int mode )
316 CvContext* context = icvGetContext();
317 int prev_mode = context->err_mode;
318 context->err_mode = mode;
322 CV_IMPL int cvGetErrStatus()
324 return icvGetContext()->err_code;
327 CV_IMPL void cvSetErrStatus( int code )
329 icvGetContext()->err_code = code;
333 CV_IMPL void cvError( int code, const char* func_name,
335 const char* file_name, int line )
337 if( code == CV_StsOk )
338 cvSetErrStatus( code );
341 CvContext* context = icvGetContext();
343 if( code != CV_StsBackTrace && code != CV_StsAutoTrace )
345 char* message = context->err_msg;
346 context->err_code = code;
348 strcpy( message, err_msg );
349 context->err_ctx.file = file_name;
350 context->err_ctx.line = line;
353 if( context->err_mode != CV_ErrModeSilent )
355 int terminate = context->error_callback( code, func_name, err_msg,
356 file_name, line, context->userdata );
359 #if !defined WIN32 && !defined WIN64
360 assert(0); // for post-mortem analysis with GDB
362 exit(-abs(terminate));
369 /******************** End of implementation of profiling stuff *********************/
372 /**********************DllMain********************************/
374 #if defined WIN32 || defined WIN64
375 BOOL WINAPI DllMain( HINSTANCE, DWORD fdwReason, LPVOID )
381 case DLL_PROCESS_ATTACH:
382 g_TlsIndex = TlsAlloc();
383 if( g_TlsIndex == TLS_OUT_OF_INDEXES ) return FALSE;
386 case DLL_THREAD_ATTACH:
387 pContext = icvCreateContext();
388 if( pContext == NULL)
390 TlsSetValue( g_TlsIndex, (LPVOID)pContext );
393 case DLL_THREAD_DETACH:
394 if( g_TlsIndex != TLS_OUT_OF_INDEXES )
396 pContext = (CvContext*)TlsGetValue( g_TlsIndex );
397 if( pContext != NULL )
398 icvDestroyContext( pContext );
402 case DLL_PROCESS_DETACH:
403 if( g_TlsIndex != TLS_OUT_OF_INDEXES )
405 pContext = (CvContext*)TlsGetValue( g_TlsIndex );
406 if( pContext != NULL )
407 icvDestroyContext( pContext );
409 TlsFree( g_TlsIndex );
419 /* function - destructor of thread */
420 void icvPthreadDestructor(void* key_val)
422 CvContext* context = (CvContext*) key_val;
423 icvDestroyContext( context );
426 int pthrerr = pthread_key_create( &g_TlsIndex, icvPthreadDestructor );
430 /* function, which converts int to int */
432 cvErrorFromIppStatus( int status )
436 case CV_BADSIZE_ERR: return CV_StsBadSize;
437 case CV_BADMEMBLOCK_ERR: return CV_StsBadMemBlock;
438 case CV_NULLPTR_ERR: return CV_StsNullPtr;
439 case CV_DIV_BY_ZERO_ERR: return CV_StsDivByZero;
440 case CV_BADSTEP_ERR: return CV_BadStep ;
441 case CV_OUTOFMEM_ERR: return CV_StsNoMem;
442 case CV_BADARG_ERR: return CV_StsBadArg;
443 case CV_NOTDEFINED_ERR: return CV_StsError;
444 case CV_INPLACE_NOT_SUPPORTED_ERR: return CV_StsInplaceNotSupported;
445 case CV_NOTFOUND_ERR: return CV_StsObjectNotFound;
446 case CV_BADCONVERGENCE_ERR: return CV_StsNoConv;
447 case CV_BADDEPTH_ERR: return CV_BadDepth;
448 case CV_UNMATCHED_FORMATS_ERR: return CV_StsUnmatchedFormats;
449 case CV_UNSUPPORTED_COI_ERR: return CV_BadCOI;
450 case CV_UNSUPPORTED_CHANNELS_ERR: return CV_BadNumChannels;
451 case CV_BADFLAG_ERR: return CV_StsBadFlag;
452 case CV_BADRANGE_ERR: return CV_StsBadArg;
453 case CV_BADCOEF_ERR: return CV_StsBadArg;
454 case CV_BADFACTOR_ERR: return CV_StsBadArg;
455 case CV_BADPOINT_ERR: return CV_StsBadPoint;
457 default: return CV_StsError;