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.
43 // 2004-03-16, Gabriel Schreiber <schreiber@ient.rwth-aachen.de>
44 // Mark Asbach <asbach@ient.rwth-aachen.de>
45 // Institute of Communications Engineering, RWTH Aachen University
51 #include "pyhelpers.h"
52 #include "pycvseq.hpp"
54 // include python-specific files
55 %include "./nointpb.i"
56 %include "./pytypemaps.i"
57 %include "exception.i"
59 /* the wrapping code to enable the use of Python-based mouse callbacks */
61 /* This encapsulates the python callback and user_data for mouse callback */
62 struct PyCvMouseCBData {
66 /* This encapsulates the python callback and user_data for mouse callback */
67 /* C helper function which is responsible for calling
68 the Python real trackbar callback function */
69 static void icvPyOnMouse (int event, int x, int y,
70 int flags, PyCvMouseCBData * param) {
72 /* Must ensure this thread has a lock on the interpreter */
73 PyGILState_STATE state = PyGILState_Ensure();
77 /* the argument of the callback ready to be passed to Python code */
78 PyObject *arg1 = PyInt_FromLong (event);
79 PyObject *arg2 = PyInt_FromLong (x);
80 PyObject *arg3 = PyInt_FromLong (y);
81 PyObject *arg4 = PyInt_FromLong (flags);
82 PyObject *arg5 = (PyObject *)param->user_data; // assume this is already a PyObject
84 /* build the tuple for calling the Python callback */
85 PyObject *arglist = Py_BuildValue ("(OOOOO)",
86 arg1, arg2, arg3, arg4, arg5);
88 /* call the Python callback */
89 result = PyEval_CallObject (param->py_func, arglist);
91 /* Errors in Python callback get swallowed, so report them here */
94 cvError( CV_StsInternal, "icvPyOnMouse", "", __FILE__, __LINE__);
100 /* Release Interpreter lock */
101 PyGILState_Release(state);
105 * adapt cvSetMouseCallback to use python callback
107 %rename (cvSetMouseCallbackOld) cvSetMouseCallback;
108 %rename (cvSetMouseCallback) cvSetMouseCallbackPy;
110 void cvSetMouseCallbackPy( const char* window_name, PyObject * on_mouse, void* param=NULL ){
111 // TODO potential memory leak if mouse callback is redefined
112 PyCvMouseCBData * py_callback = new PyCvMouseCBData;
113 py_callback->py_func = on_mouse;
114 py_callback->user_data = param ? param : Py_None;
115 cvSetMouseCallback( window_name, (CvMouseCallback) icvPyOnMouse, (void *) py_callback );
122 * The following code enables trackbar callbacks from python. Unfortunately, there is no
123 * way to distinguish which trackbar the event originated from, so must hard code a
124 * fixed number of unique c callback functions using the macros below
127 /* C helper function which is responsible for calling
128 the Python real trackbar callback function */
129 static void icvPyOnTrackbar( PyObject * py_cb_func, int pos) {
131 /* Must ensure this thread has a lock on the interpreter */
132 PyGILState_STATE state = PyGILState_Ensure();
136 /* the argument of the callback ready to be passed to Python code */
137 PyObject *arg1 = PyInt_FromLong (pos);
139 /* build the tuple for calling the Python callback */
140 PyObject *arglist = Py_BuildValue ("(O)", arg1);
142 /* call the Python callback */
143 result = PyEval_CallObject (py_cb_func, arglist);
145 /* Errors in Python callback get swallowed, so report them here */
148 cvError( CV_StsInternal, "icvPyOnTrackbar", "", __FILE__, __LINE__);
155 /* Release Interpreter lock */
156 PyGILState_Release(state);
159 #define ICV_PY_MAX_CB 10
161 struct PyCvTrackbar {
162 CvTrackbarCallback cv_func;
167 static int my_trackbar_cb_size=0;
168 extern PyCvTrackbar my_trackbar_cb_funcs[ICV_PY_MAX_CB];
171 /* Callback table entry */
172 %define %ICV_PY_CB_TAB_ENTRY(idx)
173 {(CvTrackbarCallback) icvPyTrackbarCB##idx, NULL, NULL }
176 /* Table of callbacks */
177 %define %ICV_PY_CB_TAB
179 PyCvTrackbar my_trackbar_cb_funcs[ICV_PY_MAX_CB] = {
180 %ICV_PY_CB_TAB_ENTRY(0),
181 %ICV_PY_CB_TAB_ENTRY(1),
182 %ICV_PY_CB_TAB_ENTRY(2),
183 %ICV_PY_CB_TAB_ENTRY(3),
184 %ICV_PY_CB_TAB_ENTRY(4),
185 %ICV_PY_CB_TAB_ENTRY(5),
186 %ICV_PY_CB_TAB_ENTRY(6),
187 %ICV_PY_CB_TAB_ENTRY(7),
188 %ICV_PY_CB_TAB_ENTRY(8),
189 %ICV_PY_CB_TAB_ENTRY(9)
194 /* Callback definition */
195 %define %ICV_PY_CB_IMPL(idx)
197 static void icvPyTrackbarCB##idx(int pos){
198 if(!my_trackbar_cb_funcs[idx].py_func) return;
199 icvPyOnTrackbar( my_trackbar_cb_funcs[idx].py_func, pos );
220 * typemap to memorize the Python callback when doing cvCreateTrackbar ()
222 %typemap(in) CvTrackbarCallback {
224 if(my_trackbar_cb_size == ICV_PY_MAX_CB){
225 SWIG_exception(SWIG_IndexError, "Exceeded maximum number of trackbars");
228 my_trackbar_cb_size++;
230 /* memorize the Python address of the callback function */
231 my_trackbar_cb_funcs[my_trackbar_cb_size-1].py_func = (PyObject *) $input;
233 /* prepare to call the C function who will register the callback */
234 $1 = my_trackbar_cb_funcs[ my_trackbar_cb_size-1 ].cv_func;
238 * typemap so that cvWaitKey returns a character in all cases except -1
240 %rename (cvWaitKeyC) cvWaitKey;
241 %rename (cvWaitKey) cvWaitKeyPy;
243 PyObject * cvWaitKeyPy(int delay=0){
244 // In order for the event processing thread to run a python callback
245 // it must acquire the global interpreter lock, but cvWaitKey blocks, so
246 // this thread can never release the lock. So release it here.
247 PyThreadState * thread_state = PyEval_SaveThread();
248 int res = cvWaitKey(delay);
249 PyEval_RestoreThread( thread_state );
251 char str[2]={(char)res,0};
253 return PyLong_FromLong(-1);
255 return PyString_FromString(str);
258 /* HighGUI Python module initialization
259 * needed for callbacks to work in a threaded environment
262 PyEval_InitThreads();
266 %include "../general/highgui.i"
271 __doc__ = """HighGUI provides minimalistic user interface parts and video input/output.
273 Dependent on the platform it was compiled on, this library provides methods
274 to draw a window for image display, capture video from a camera or framegrabber
275 or read/write video streams from/to the file system.
277 This wrapper was semi-automatically created from the C/C++ headers and therefore
278 contains no Python documentation. Because all identifiers are identical to their
279 C/C++ counterparts, you can consult the standard manuals that come with OpenCV.