Move the sources to trunk
[opencv] / interfaces / matlab / src / main.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 //#include "qfrontend.h"
43
44 #include "qerr.h"
45 #include "hash.h"
46
47 #include "cv.h"
48
49 // !!!
50 // uncomment this function 
51 // if you wish that CV reports errors to Matlab and don't terminate programm
52
53 //------------- Error function ----------------------------------------------------------
54 char cvErrorMessage[1000]; // error message from some OpenCV function
55
56 extern "C"
57 {
58         CVStatus GuiBoxReport_NotFatal( CVStatus status, const char *funcName, 
59                                                                    const char *context, const char *file, int line)
60         {
61                 char mess[1000];
62                 //char *choice = 0;    
63                 const char* errText = cvErrorStr( status );
64                 
65                 if ( cvGetErrMode() != CV_ErrModeSilent ) 
66                 {
67                         if ( !funcName ) funcName = "<unknown>";
68                         if ( !context  ) context = "";
69                         if ( !file     ) file = "";
70                         if (  line < 0 ) line = 0;
71                         
72                         
73                         if ( status == CV_StsBackTrace)
74                                 sprintf(mess,"Called from %s(): [file %s, line %d]\n%s\n%s\n(status:%d)\n",
75                                 funcName, file,line,context, errText, status);
76                         else if ( status == CV_StsAutoTrace )
77                                 sprintf(mess,"Called from %s(): [file %s, line %d]\n%s\n%s\n(status:%d)\n",
78                                 funcName, file, line, context, errText, status);
79                         else
80                                 sprintf(mess,"In function %s(): [file %s, line %d]\n%s\n%s\n(status:%d)\n",
81                                 funcName, file, line, context, errText, status);
82                         
83                         sprintf(cvErrorMessage,"OpenCV Beta 1: %s\n%s",cvErrorStr(cvGetErrStatus()),mess);
84                         //throw_error(title);
85                 }
86                 
87                 return status;
88         }
89 }
90 //-------------------------------------------------------------------------------------
91
92 //------------- Wrappers --------------------------------------------------------------
93 #include "funcs.h"
94 #include "tmacro.h"
95 #include "wrmacro.h"
96 #include "wrappers.cpp"
97
98 // !!!
99 // you should better to add all names of wrapper functions in map!
100
101 MapEntry function_map[] = 
102   {
103           { "Erode"             , (void*) wrErode },
104           { "Dilate"            , (void*) wrDilate },
105           { "PyrUp"                     , (void*) wrPyrUp },
106           { "PyrDown"           , (void*) wrPyrDown },
107           { "Laplace"           , (void*) wrLaplace },
108           { "Sobel"                     , (void*) wrSobel },
109           { "DistTransform"                     , (void*) wrDistTransform },
110           { "Canny"                     , (void*) wrCanny },
111           { "PreCornerDetect"                   , (void*) wrPreCornerDetect },
112           { "MatchTemplate"                     , (void*) wrMatchTemplate },
113           { "UpdateMotionHistory"                       , (void*) wrUpdateMotionHistory },
114           { "MinMaxLoc"                 , (void *) wrMinMaxLoc },
115           { "Threshold"                 , (void *)wrThreshold },
116           { "AdaptiveThreshold"                 , (void*) wrAdaptiveThreshold },
117           { "FindContours"              , (void *)wrFindContours },
118           { "DrawContours"              , (void *)wrDrawContours },
119           { "ApproxPoly"                , (void *)wrApproxPoly },
120           { "ContourPerimeter"  , (void *)wrContourPerimeter },
121           { "ContourArea"       , (void *)wrContourArea },
122           { "FloodFill",          (void *)wrFloodFill },
123           { "CalcOpticalFlowPyrLK", (void *)wrCalcOpticalFlowPyrLK },
124           { "GoodFeaturesToTrack", (void *)wrGoodFeaturesToTrack },
125           { 0 , 0 }
126   };
127 //-------------------------------------------------------------------------------------
128
129 extern "C" {
130
131 void mexFunction( int nlhs, mxArray* plhs[],
132                                   int nrhs, const mxArray* prhs[] ) {
133         // uncomment these two calls
134         // if you wish that CV report errors to Matlab and don't terminate programm
135
136         cvRedirectError( GuiBoxReport_NotFatal ); 
137         cvSetErrMode( CV_ErrModeParent );
138
139         try
140         {
141                 if(nrhs < 2)
142                         throw_error( "Internal wrapper error: main function must have more than one argument" );
143                 
144                 if( !mxIsChar( prhs[0] ) ) 
145                         throw_error( "Internal wrapper error: in main function zero argument must be string" );
146
147                 std::string funcname = get_string( prhs[0] );
148                                 
149                 InitHashTable((MapEntry *)&function_map);
150                 MapEntry* entry = FindMapEntry(funcname.c_str());
151                 
152                 // if wrapper exist we call it
153                 if (entry)
154                 {
155                         void (*pfun) (int nlhs, mxArray* plhs[],int nrhs, const mxArray* prhs[])
156                                 = (void (__cdecl *)(int nlhs, mxArray* plhs[],
157                                                                         int nrhs, const mxArray* prhs[]))(entry->image);
158                         
159                         (*pfun)(nlhs, plhs, nrhs, prhs);
160                 } else
161                 {
162                         std::ostringstream ostr;
163                         ostr << "Internal wrapper error: "<< funcname << " function not found";
164                         throw_error( ostr.str() );
165                 }               
166                 return;
167         }
168         catch( Error e )
169         {
170                 mexErrMsgTxt( e.get_error_messages() );
171         }
172         catch (...)
173         {
174                 mexErrMsgTxt( "Unknown C++ exception occurred" );
175         }
176 }
177
178 }