Update the changelog
[opencv] / apps / Hawk / CVEiCL / wrappers / HighGUIwrap.c
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 #include <stdlib.h>
42 #include <varargs.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <setjmp.h>
47
48 #include <eic.h>
49 #ifdef malloc /* This macros is defined in eic.h */
50 #undef malloc
51 #endif /* malloc */
52 #ifdef realloc
53 #undef realloc
54 #endif /* realloc */
55 #ifdef free
56 #undef free
57 #endif /* free */
58
59 #ifdef WIN32
60 #undef WIN32
61 #include <highgui.h>
62 #define WIN32
63 #else
64 #include <highgui.h>
65 #endif
66
67 #include "hawkwrap.h"
68
69 static jmp_buf* highgui_mark = NULL;
70 static errlevel_t highgui_errlevel = unsafe;
71 int trackbar_count = 0;
72 void (**trackbar_notifies)(int) = NULL;
73
74 //define many small trackbar callbacks
75 #define TB_CB(i) void tb_cb##i(int val)                 \
76 {                                                       \
77     setArg(0, (code_t*)trackbar_notifies[i], int, val); \
78     EiC_callBack(trackbar_notifies[i]);                 \
79 }                                        
80 TB_CB(0); TB_CB(1); TB_CB(2); TB_CB(3); TB_CB(4); 
81 TB_CB(5); TB_CB(6); TB_CB(7); TB_CB(8); TB_CB(9); 
82 TB_CB(10); TB_CB(11); TB_CB(12); TB_CB(13); TB_CB(14); 
83 TB_CB(15); TB_CB(16); TB_CB(17); TB_CB(18); TB_CB(19); 
84 TB_CB(20); TB_CB(21); TB_CB(22); TB_CB(23); TB_CB(24); 
85 TB_CB(25); TB_CB(26); TB_CB(27); TB_CB(28); TB_CB(29); 
86 TB_CB(30); TB_CB(31); TB_CB(32); TB_CB(33); TB_CB(34); 
87 TB_CB(35); TB_CB(36); TB_CB(37); TB_CB(38); TB_CB(39);
88
89 void(*tb_cbs[40])(int) = 
90 { tb_cb0, tb_cb1, tb_cb2, tb_cb3, tb_cb4, 
91   tb_cb5, tb_cb6, tb_cb7, tb_cb8, tb_cb9,
92   tb_cb10, tb_cb11, tb_cb12, tb_cb13, tb_cb14, 
93   tb_cb15, tb_cb16, tb_cb17, tb_cb18, tb_cb19,
94   tb_cb20, tb_cb21, tb_cb22, tb_cb23, tb_cb24, 
95   tb_cb25, tb_cb26, tb_cb27, tb_cb28, tb_cb29,
96   tb_cb30, tb_cb31, tb_cb32, tb_cb33, tb_cb34, 
97   tb_cb35, tb_cb36, tb_cb37, tb_cb38, tb_cb39 }; //array of callbacks
98
99 static val_t eic_create_trackbar(void)
100 {
101         val_t v;
102     void* pnotify;
103
104     if( trackbar_count == 40 )  
105     {
106         char str[1024];
107         sprintf(str, "The maximal number of trackbars is 40");
108         WRAP_THROW_MESSAGE(highgui_mark, str);
109     }
110
111     pnotify = arg(4,getargs(),ptr_t).p;
112         v.ival = cvCreateTrackbar((const char*)arg(0,getargs(),ptr_t).p,
113         (const char*)arg(1,getargs(),ptr_t).p,
114         (int*)arg(2,getargs(),ptr_t).p,
115         arg(3,getargs(),int),
116         pnotify ? tb_cbs[trackbar_count] : NULL);
117     trackbar_count++;
118     trackbar_notifies = (void(**)(int))realloc(trackbar_notifies, trackbar_count*sizeof(char*));
119     trackbar_notifies[trackbar_count - 1] = (void(*)(int))pnotify;
120         return v;
121 }
122
123 int mouse_callback_count = 0;
124 void (**mouse_notifies)(int,int,int,int) = NULL;
125
126 //define many small mouse callbacks
127 #define M_CB(i) void m_cb##i(int event, int x, int y, int flag, void* userdata )  \
128 {                                                        \
129     setArg(0, (code_t*)mouse_notifies[i], int, event);   \
130     setArg(1, (code_t*)mouse_notifies[i], int, x);       \
131     setArg(2, (code_t*)mouse_notifies[i], int, y);       \
132     setArg(3, (code_t*)mouse_notifies[i], int, flag);    \
133     setArg(4, (code_t*)mouse_notifies[i], void*, userdata); \
134     EiC_callBack(mouse_notifies[i]);                     \
135 }                                        
136
137 M_CB(0); M_CB(1); M_CB(2); M_CB(3); M_CB(4); 
138 M_CB(5); M_CB(6); M_CB(7); M_CB(8); M_CB(9); 
139 M_CB(10); M_CB(11); M_CB(12); M_CB(13); M_CB(14); 
140 M_CB(15); M_CB(16); M_CB(17); M_CB(18); M_CB(19); 
141 M_CB(20); M_CB(21); M_CB(22); M_CB(23); M_CB(24); 
142 M_CB(25); M_CB(26); M_CB(27); M_CB(28); M_CB(29); 
143 M_CB(30); M_CB(31); M_CB(32); M_CB(33); M_CB(34); 
144 M_CB(35); M_CB(36); M_CB(37); M_CB(38); M_CB(39);
145
146 void(*m_cbs[40])(int) = 
147 { m_cb0, m_cb1, m_cb2, m_cb3, m_cb4, 
148   m_cb5, m_cb6, m_cb7, m_cb8, m_cb9,
149   m_cb10, m_cb11, m_cb12, m_cb13, m_cb14, 
150   m_cb15, m_cb16, m_cb17, m_cb18, m_cb19,
151   m_cb20, m_cb21, m_cb22, m_cb23, m_cb24, 
152   m_cb25, m_cb26, m_cb27, m_cb28, m_cb29,
153   m_cb30, m_cb31, m_cb32, m_cb33, m_cb34, 
154   m_cb35, m_cb36, m_cb37, m_cb38, m_cb39 }; //array of callbacks
155
156 static val_t eic_set_mouse_callback(void)
157 {
158         val_t v;
159     void* pnotify;
160
161     if( mouse_callback_count == 40 )  
162     {
163         char str[1024];
164         sprintf(str, "The maximal number of mouse callbacks is 40");
165         WRAP_THROW_MESSAGE(highgui_mark, str);
166     }
167
168     pnotify = arg(1,getargs(),ptr_t).p;
169         cvSetMouseCallback((const char*)arg(0,getargs(),ptr_t).p,
170                        pnotify ? m_cbs[mouse_callback_count] : NULL,NULL);
171     mouse_callback_count++;
172     mouse_notifies = (void(**)(int,int,int,int))realloc(mouse_notifies, 
173                                        mouse_callback_count*sizeof(char*));
174     mouse_notifies[mouse_callback_count - 1] = (void(*)(int,int,int,int))pnotify;
175         return v;
176 }
177
178 /**********************************/
179
180 void module_highguiwrap()
181 {
182     //wrap highgui functions which have callbacks as parameters
183     EiC_add_builtinfunc("cvCreateTrackbar",eic_create_trackbar); 
184     EiC_add_builtinfunc("cvSetMouseCallback",eic_set_mouse_callback); 
185 }
186
187 void set_highgui_mark(jmp_buf* mark)
188 {
189     highgui_mark = mark;
190 }
191
192 void set_highgui_errlevel(errlevel_t level)
193 {
194     highgui_errlevel = level;
195 }
196 /**********************************/
197