cverror.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 535 行 · 第 1/2 页

SVN-BASE
535
字号
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//            Intel License Agreement
//        For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "_cv.h"
#include <stdio.h>

#ifdef WIN32  
#include <windows.h>  
#else
#include <pthread.h>
#endif 

CvContext* icvCreateContext()
{
    CvContext* context = (CvContext*)malloc( sizeof( CvContext ) );

    context->CVErrMode    = CV_ErrModeLeaf;
    context->CVLastStatus = CV_StsOk;

#ifdef WIN32
   context->CVErrorFunc   = cvGuiBoxReport;
#else
   context->CVErrorFunc   = cvStdErrReport;
#endif

   /* below is stuff for profiling */
   context->CVStackCapacity = 100; /* let it be so*/
   context->CVStackSize = 0;
   context->CVStack =
       (CvStackRecord*)malloc( sizeof(CvStackRecord) * context->CVStackCapacity );
   return context;
}

void icvDestroyContext(CvContext* context)
{
    free(context->CVStack);
    free(context);
}

#ifdef WIN32
    DWORD g_TlsIndex = TLS_OUT_OF_INDEXES;
#else
    pthread_key_t g_TlsIndex;
#endif

CvContext* icvGetContext()
{
#ifdef CV_DLL
#ifdef WIN32
    CvContext* pContext = (CvContext*)TlsGetValue( g_TlsIndex );
    if( !pContext )
    {
    pContext = icvCreateContext();

    if( !pContext )
    {
        FatalAppExit( 0, "OpenCV. Problem to allocate memory for TLS OpenCV context." );
    }
    TlsSetValue( g_TlsIndex, pContext );
    }
    return pContext;
#else
    CvContext* pContext = (CvContext*)pthread_getspecific( g_TlsIndex );
    if( !pContext )
    {
    pContext = icvCreateContext();
    if( !pContext )
    {
            fprintf(stderr,"OpenCV. Problem to allocate memory for OpenCV context.");
        exit(1);
    }
    pthread_setspecific( g_TlsIndex, pContext );
    }
    return pContext;
#endif
#else /* CV_DLL */
    static CvContext* pContext = 0;

    if( !pContext )
    pContext = icvCreateContext();

    return pContext;
#endif
}


CV_IMPL CVStatus cvStdErrReport( CVStatus status, const char *funcName,
                                 const char *context, const char *file, int line )
{
    CvContext* cont = icvGetContext();

    if ( cvGetErrMode() == CV_ErrModeSilent )
    return ( status != CV_StsBackTrace ) ? ( cont->CVLastStatus = status ) : status;
    
    if (( status == CV_StsBackTrace ) || ( status == CV_StsAutoTrace ))
    fprintf(stderr, "\tcalled from ");
    else 
    {
    cont->CVLastStatus = status;
    fprintf(stderr, "OpenCV Error: %s \n\tin function ", cvErrorStr(status));
    }
    if ( line > 0 || file != NULL )
    fprintf(stderr,"[%s:%d]", file, line);
    fprintf(stderr,":%s", funcName ? funcName : "<unknown>");
    if ( context != NULL ) {
    if ( status != CV_StsAutoTrace )
        fprintf(stderr, "():\n%s", context);    /* Print context      */
    else
        fprintf(stderr, "(%s)", context);          /* Print arguments    */
    }
    fprintf(stderr, "\n");
    if ( cont->CVErrMode == CV_ErrModeLeaf ) {
    fprintf(stderr, "OpenCV: %s\n","terminating the application");
    exit(1);
    };

    return status;
}

CV_IMPL CVStatus cvGuiBoxReport( CVStatus status, const char *funcName, 
                 const char *context, const char *file, int line)
{

#ifdef WIN32

    char mess[1000];
    char title[100];
    char *choice = 0;
    const char* errText = cvErrorStr( status );


    if ( cvGetErrMode() != CV_ErrModeSilent )
    {
        if( !funcName ) funcName = "<unknown>";
        if( !context  ) context = "";
        if( !file     ) file = "";
        if(  line < 0 ) line = 0;

        if( cvGetErrMode() == CV_ErrModeLeaf )
            choice="\nErrMode=CV_ErrorModeLeaf\n"
                   "\nTerminate the application?";

        if( cvGetErrMode() == CV_ErrModeParent)
            choice="\nErrMode=CV_ErrorModeParent\n"
            "\nContinue?";

        if( status == CV_StsBackTrace)
            wsprintf( mess,"Called from %s(): [file %s, line %d]\n%s\n%s\n(status:%d)\n%s",
                      funcName, file,line,context, errText, status, choice);
        else if ( status == CV_StsAutoTrace )
            wsprintf( mess,"Called from %s(): [file %s, line %d]\n%s\n%s\n(status:%d)\n%s",
                      funcName, file, line, context, errText, status, choice);
        else
            wsprintf( mess,"In function %s(): [file %s, line %d]\n%s\n%s\n(status:%d)\n%s",
                      funcName, file, line, context,errText, status, choice);

        wsprintf(title,"OpenCV Beta 2: %s",cvErrorStr(cvGetErrStatus()));

        int answer = -1;

        if( (( cvGetErrMode()==CV_ErrModeParent) &&
            (IDCANCEL==MessageBox(NULL,mess,title,MB_ICONERROR|MB_OKCANCEL|MB_SYSTEMMODAL) ) ||
            ((cvGetErrMode() == CV_ErrModeLeaf) &&
            //(IDYES==MessageBox(NULL,mess,title,MB_ICONERROR|MB_YESNO|MB_SYSTEMMODAL))
            (IDABORT == (answer=MessageBox(NULL,mess,title,MB_ICONERROR|MB_ABORTRETRYIGNORE|MB_SYSTEMMODAL))||
            IDRETRY == answer)
            )))
        {
            if( answer == IDRETRY )
            {

    #if _MSC_VER >= 1200 || defined __ICL
                __asm int 3;
    #else
                assert(0);
    #endif
            }
            FatalAppExit(0,"OpenCV:\nterminating the application");
        }
    }

#else
    cvStdErrReport( status, funcName, context, file, line);
#endif

    return status;
}


CV_IMPL CVStatus cvNulDevReport( CVStatus status, const char *funcName,
                 const char *context, const char *file, int line)
{
  if( status||funcName||context||file||line )
  if ( cvGetErrMode() == CV_ErrModeLeaf )
      exit(1);
  return status;
}

CV_IMPL CVErrorCallBack cvRedirectError(CVErrorCallBack func)
{
    CvContext* context = icvGetContext();

    CVErrorCallBack old = context->CVErrorFunc;
    context->CVErrorFunc = func;
    return old;
} 
 
CV_IMPL const char* cvErrorStr(CVStatus status)
{
    static char buf[80];

    switch (status) 
    {
    case CV_StsOk :    return "No Error";
    case CV_StsBackTrace : return "Backtrace";
    case CV_StsError :     return "Unknown error";
    case CV_StsInternal :  return "Internal error";
    case CV_StsNoMem :     return "Insufficient memory";
    case CV_StsBadArg :    return "Bad argument";
    case CV_StsNoConv :    return "Iteration convergence failed";
    case CV_StsAutoTrace : return "Autotrace call";
    case CV_StsBadSize :   return "Bad/unsupported parameter of type CvSize";
    case CV_StsNullPtr :   return "Null pointer";
    case CV_StsDivByZero : return "Divizion by zero occured";
    case CV_BadStep :      return "Image step is wrong";
    case CV_StsInplaceNotSupported : return "Inplace operation is not supported";
    case CV_StsObjectNotFound :      return "Requested object was not found";
    case CV_BadDepth :     return "Input image depth is not supported by function";
    case CV_StsUnmatchedFormats : return "Formats of input arguments do not match"; 
    case CV_StsUnmatchedSizes :  return "Sizes of input arguments do not match";

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?