error.cpp 7.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*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.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., 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 the copyright holders 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 "precomp.hpp"

45

46 47 48 49 50 51 52 53 54 55 56
using namespace cv;
using namespace cv::gpu;


#if !defined (HAVE_CUDA)

#else /* !defined (HAVE_CUDA) */


namespace 
{
57 58
    #define error_entry(entry)  { entry, #entry }

59 60 61
    //////////////////////////////////////////////////////////////////////////
    // NPP errors

62 63 64
    struct NppError
    {
        int error;
65
        string str;
66
    } 
67
    
68 69
    npp_errors [] = 
    {
70 71 72
        error_entry( NPP_NOT_SUPPORTED_MODE_ERROR ),
        error_entry( NPP_ROUND_MODE_NOT_SUPPORTED_ERROR ),
        error_entry( NPP_RESIZE_NO_OPERATION_ERROR ),
73 74

#if defined (_MSC_VER)
75
        error_entry( NPP_NOT_SUFFICIENT_COMPUTE_CAPABILITY ),
76
#endif
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        error_entry( NPP_BAD_ARG_ERROR ),
        error_entry( NPP_LUT_NUMBER_OF_LEVELS_ERROR ),
        error_entry( NPP_TEXTURE_BIND_ERROR ),
        error_entry( NPP_COEFF_ERROR ),
        error_entry( NPP_RECT_ERROR ),
        error_entry( NPP_QUAD_ERROR ),
        error_entry( NPP_WRONG_INTERSECTION_ROI_ERROR ),
        error_entry( NPP_NOT_EVEN_STEP_ERROR ),
        error_entry( NPP_INTERPOLATION_ERROR ),
        error_entry( NPP_RESIZE_FACTOR_ERROR ),
        error_entry( NPP_HAAR_CLASSIFIER_PIXEL_MATCH_ERROR ),
        error_entry( NPP_MEMFREE_ERR ),
        error_entry( NPP_MEMSET_ERR ),
        error_entry( NPP_MEMCPY_ERROR ),
        error_entry( NPP_MEM_ALLOC_ERR ),
        error_entry( NPP_HISTO_NUMBER_OF_LEVELS_ERROR ),
        error_entry( NPP_MIRROR_FLIP_ERR ),
        error_entry( NPP_INVALID_INPUT ),
        error_entry( NPP_ALIGNMENT_ERROR ),
        error_entry( NPP_STEP_ERROR ),
        error_entry( NPP_SIZE_ERROR ),
        error_entry( NPP_POINTER_ERROR ),
        error_entry( NPP_NULL_POINTER_ERROR ),
        error_entry( NPP_CUDA_KERNEL_EXECUTION_ERROR ),
        error_entry( NPP_NOT_IMPLEMENTED_ERROR ),
        error_entry( NPP_ERROR ),
        error_entry( NPP_NO_ERROR ),
        error_entry( NPP_SUCCESS ),
        error_entry( NPP_WARNING ),
        error_entry( NPP_WRONG_INTERSECTION_QUAD_WARNING ),
        error_entry( NPP_MISALIGNED_DST_ROI_WARNING ),
        error_entry( NPP_AFFINE_QUAD_INCORRECT_WARNING ),
        error_entry( NPP_DOUBLE_SIZE_WARNING ),
        error_entry( NPP_ODD_ROI_WARNING )
111 112 113 114 115 116 117 118 119 120 121
    };

    int error_num = sizeof(npp_errors)/sizeof(npp_errors[0]);

    struct Searcher
    {
        int err;
        Searcher(int err_) : err(err_) {};
        bool operator()(const NppError& e) const { return e.error == err; }
    };

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    //////////////////////////////////////////////////////////////////////////
    // CUFFT errors

    struct CufftError
    {
        int code;
        string message;
    };

    const CufftError cufft_errors[] = 
    {
        error_entry(CUFFT_INVALID_PLAN),
        error_entry(CUFFT_ALLOC_FAILED),
        error_entry(CUFFT_INVALID_TYPE),
        error_entry(CUFFT_INVALID_VALUE),
        error_entry(CUFFT_INTERNAL_ERROR),
        error_entry(CUFFT_EXEC_FAILED),
        error_entry(CUFFT_SETUP_FAILED),
        error_entry(CUFFT_INVALID_SIZE),
        error_entry(CUFFT_UNALIGNED_DATA)
    };

    struct CufftErrorComparer
    {
        CufftErrorComparer(int code_): code(code_) {}
        bool operator()(const CufftError& other) const 
        { 
            return other.code == code; 
        }
        int code;
    };

    const int cufft_error_num = sizeof(cufft_errors) / sizeof(cufft_errors[0]);

156 157 158 159 160 161
}

namespace cv
{
    namespace gpu
    {
162
        const string getNppErrorString( int err )
163 164
        {
            int idx = std::find_if(npp_errors, npp_errors + error_num, Searcher(err)) - npp_errors;
165 166 167
            const string& msg = (idx != error_num) ? npp_errors[idx].str : string("Unknown error code");

            std::stringstream interpreter;
168
            interpreter << msg <<" [Code = " << err << "]";
169

170
            return interpreter.str();
171 172
        }

173
        void nppError( int err, const char *file, const int line, const char *func)
174 175 176
        {                    
            cv::error( cv::Exception(CV_GpuNppCallError, getNppErrorString(err), func, file, line) );                
        }
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        const string getCufftErrorString(int err_code)
        {
            const CufftError* cufft_error = std::find_if(
                    cufft_errors, cufft_errors + cufft_error_num, 
                    CufftErrorComparer(err_code));

            bool found = cufft_error != cufft_errors + cufft_error_num;

            std::stringstream ss;
            ss << (found ? cufft_error->message : "Unknown error code");
            ss << " [Code = " << err_code << "]";

            return ss.str();
        }

        void cufftError(int err, const char *file, const int line, const char *func)
        {
            cv::error(cv::Exception(CV_GpuCufftCallError, getCufftErrorString(err), func, file, line));
        }

198
        void error(const char *error_string, const char *file, const int line, const char *func)
199
        {          
200 201 202 203 204 205 206 207 208 209 210 211
            int code = CV_GpuApiCallError;

            if (std::uncaught_exception())
            {
                const char* errorStr = cvErrorStr(code);            
                const char* function = func ? func : "unknown function";    

                std::cerr << "OpenCV Error: " << errorStr << "(" << error_string << ") in " << function << ", file " << file << ", line " << line;
                std::cerr.flush();            
            }
            else    
                cv::error( cv::Exception(code, error_string, func, file, line) );
212
        }
213 214 215
    }
}

216
#endif