4cb1d782c1b2cc26f82dc93ac1cb34d0c6d1f0ca
[opencv] / interfaces / swig / octave / highgui.i
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
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
46 // 2008-04-09  Xavier Delacour <xavier.delacour@gmail.com>
47
48 // todo remove these..
49 #pragma SWIG nowarn=312,362,303,365,366,367,368,370,371,372,451,454,503
50
51 %{
52 #include <cxtypes.h>
53 #include <cv.h>
54 #include <highgui.h>
55 #include "octhelpers.h"
56 #include "octcvseq.hpp"
57 %}
58
59 // include octave-specific files
60 %include "./octtypemaps.i"
61 %include "exception.i"
62
63 // the wrapping code to enable the use of Octave-based mouse callbacks
64 %{
65   // This encapsulates the octave callback and user_data for mouse callback
66   struct OctCvMouseCBData {
67     octave_value oct_func;
68     octave_value user_data;
69   };
70   // This encapsulates the octave callback and user_data for mouse callback
71   // C helper function which is responsible for calling
72   // the Octave real trackbar callback function
73   static void icvOctOnMouse (int event, int x, int y,
74                              int flags, OctCvMouseCBData * param) {
75     octave_value oct_func(param->oct_func);
76     if (!oct_func.is_function() && !oct_func.is_function_handle())
77       return;
78
79     octave_value_list args;
80     args.append(octave_value(event));
81     args.append(octave_value(x));
82     args.append(octave_value(y));
83     args.append(octave_value(flags));
84     args.append(param->user_data);
85     oct_func.subsref ("(", std::list<octave_value_list>(1, args), 0);
86   }
87 %}
88
89 // adapt cvSetMouseCallback to use octave callback
90 %rename (cvSetMouseCallbackOld) cvSetMouseCallback;
91 %rename (cvSetMouseCallback) cvSetMouseCallbackOct;
92 %inline {
93   void cvSetMouseCallbackOct( const char* window_name, octave_value on_mouse, octave_value param = octave_value() ){
94     OctCvMouseCBData * oct_callback = new OctCvMouseCBData;
95     oct_callback->oct_func = on_mouse;
96     oct_callback->user_data = param;
97     cvSetMouseCallback( window_name, (CvMouseCallback) icvOctOnMouse, (void *) oct_callback );
98   }
99 }
100
101 // The following code enables trackbar callbacks from octave.  Unfortunately, there is no 
102 // way to distinguish which trackbar the event originated from, so must hard code a 
103 // fixed number of unique c callback functions using the macros below
104 %{
105   // C helper function which is responsible for calling
106   // the Octave real trackbar callback function
107   static void icvOctOnTrackbar( octave_value oct_cb_func, int pos) {
108     if (!oct_cb_func.is_function() && !oct_cb_func.is_function_handle())
109       return;
110
111     octave_value_list args;
112     args.append(octave_value(pos));
113     oct_cb_func.subsref ("(", std::list<octave_value_list>(1, args), 0);
114   }
115
116 #define ICV_OCT_MAX_CB 10
117
118   struct OctCvTrackbar {
119     CvTrackbarCallback cv_func;
120     octave_value oct_func;
121     octave_value oct_pos;
122   };
123
124   static int my_trackbar_cb_size=0;
125   extern OctCvTrackbar my_trackbar_cb_funcs[ICV_OCT_MAX_CB];
126   %}
127
128 // Callback table entry
129 %define %ICV_OCT_CB_TAB_ENTRY(idx)
130 {(CvTrackbarCallback) icvOctTrackbarCB##idx, octave_value(), octave_value() }
131 %enddef
132
133 // Table of callbacks
134 %define %ICV_OCT_CB_TAB
135 %{
136   OctCvTrackbar my_trackbar_cb_funcs[ICV_OCT_MAX_CB] = {
137     %ICV_OCT_CB_TAB_ENTRY(0),
138     %ICV_OCT_CB_TAB_ENTRY(1),
139     %ICV_OCT_CB_TAB_ENTRY(2),
140     %ICV_OCT_CB_TAB_ENTRY(3),
141     %ICV_OCT_CB_TAB_ENTRY(4),
142     %ICV_OCT_CB_TAB_ENTRY(5),
143     %ICV_OCT_CB_TAB_ENTRY(6),
144     %ICV_OCT_CB_TAB_ENTRY(7),
145     %ICV_OCT_CB_TAB_ENTRY(8),
146     %ICV_OCT_CB_TAB_ENTRY(9)
147   };
148 %}       
149 %enddef
150
151 // Callback definition
152 %define %ICV_OCT_CB_IMPL(idx) 
153 %{
154 static void icvOctTrackbarCB##idx(int pos){                                      
155   icvOctOnTrackbar(my_trackbar_cb_funcs[idx].oct_func, pos);
156 }                                                                               
157 %}
158 %enddef
159
160 %ICV_OCT_CB_IMPL(0);
161 %ICV_OCT_CB_IMPL(1);
162 %ICV_OCT_CB_IMPL(2);
163 %ICV_OCT_CB_IMPL(3);
164 %ICV_OCT_CB_IMPL(4);
165 %ICV_OCT_CB_IMPL(5);
166 %ICV_OCT_CB_IMPL(6);
167 %ICV_OCT_CB_IMPL(7);
168 %ICV_OCT_CB_IMPL(8);
169 %ICV_OCT_CB_IMPL(9);
170
171 %ICV_OCT_CB_TAB;
172
173 // typemap to memorize the Octave callback when doing cvCreateTrackbar ()
174 %typemap(in) CvTrackbarCallback {
175   if(my_trackbar_cb_size == ICV_OCT_MAX_CB){
176     SWIG_exception(SWIG_IndexError, "Exceeded maximum number of trackbars");
177   }
178
179   my_trackbar_cb_size++;
180
181   // memorize the Octave address of the callback function
182   my_trackbar_cb_funcs[my_trackbar_cb_size-1].oct_func = (octave_value) $input;
183
184   // prepare to call the C function who will register the callback
185   $1 = my_trackbar_cb_funcs[ my_trackbar_cb_size-1 ].cv_func;
186 }
187
188
189 %include "../general/highgui.i"
190 %include "adapters.i"
191