Add OpenCV source code
[platform/upstream/opencv.git] / modules / highgui / src / window.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 "precomp.hpp"
43 #include <map>
44 #include "opencv2/core/opengl_interop.hpp"
45
46 // in later times, use this file as a dispatcher to implementations like cvcap.cpp
47
48 CV_IMPL void cvSetWindowProperty(const char* name, int prop_id, double prop_value)
49 {
50     switch(prop_id)
51     {
52     //change between fullscreen or not.
53     case CV_WND_PROP_FULLSCREEN:
54
55         if (!name || (prop_value!=CV_WINDOW_NORMAL && prop_value!=CV_WINDOW_FULLSCREEN))//bad argument
56             break;
57
58         #if defined (HAVE_QT)
59             cvSetModeWindow_QT(name,prop_value);
60         #elif defined(HAVE_WIN32UI)
61             cvSetModeWindow_W32(name,prop_value);
62         #elif defined (HAVE_GTK)
63             cvSetModeWindow_GTK(name,prop_value);
64         #elif defined (HAVE_CARBON)
65             cvSetModeWindow_CARBON(name,prop_value);
66         #elif defined (HAVE_COCOA)
67             cvSetModeWindow_COCOA(name,prop_value);
68         #endif
69     break;
70
71     case CV_WND_PROP_AUTOSIZE:
72         #if defined (HAVE_QT)
73             cvSetPropWindow_QT(name,prop_value);
74         #endif
75     break;
76
77     case CV_WND_PROP_ASPECTRATIO:
78         #if defined (HAVE_QT)
79             cvSetRatioWindow_QT(name,prop_value);
80         #endif
81     break;
82
83     default:;
84     }
85 }
86
87 /* return -1 if error */
88 CV_IMPL double cvGetWindowProperty(const char* name, int prop_id)
89 {
90     if (!name)
91         return -1;
92
93     switch(prop_id)
94     {
95     case CV_WND_PROP_FULLSCREEN:
96
97         #if defined (HAVE_QT)
98             return cvGetModeWindow_QT(name);
99         #elif defined(HAVE_WIN32UI)
100             return cvGetModeWindow_W32(name);
101         #elif defined (HAVE_GTK)
102             return cvGetModeWindow_GTK(name);
103         #elif defined (HAVE_CARBON)
104             return cvGetModeWindow_CARBON(name);
105         #elif defined (HAVE_COCOA)
106             return cvGetModeWindow_COCOA(name);
107         #else
108             return -1;
109         #endif
110     break;
111
112     case CV_WND_PROP_AUTOSIZE:
113
114         #if defined (HAVE_QT)
115             return cvGetPropWindow_QT(name);
116         #elif defined(HAVE_WIN32UI)
117             return cvGetPropWindowAutoSize_W32(name);
118         #elif defined (HAVE_GTK)
119             return cvGetPropWindowAutoSize_GTK(name);
120         #else
121             return -1;
122         #endif
123     break;
124
125     case CV_WND_PROP_ASPECTRATIO:
126
127         #if defined (HAVE_QT)
128             return cvGetRatioWindow_QT(name);
129         #elif defined(HAVE_WIN32UI)
130             return cvGetRatioWindow_W32(name);
131         #elif defined (HAVE_GTK)
132             return cvGetRatioWindow_GTK(name);
133         #else
134             return -1;
135         #endif
136     break;
137
138     case CV_WND_PROP_OPENGL:
139
140         #if defined (HAVE_QT)
141             return cvGetOpenGlProp_QT(name);
142         #elif defined(HAVE_WIN32UI)
143             return cvGetOpenGlProp_W32(name);
144         #elif defined (HAVE_GTK)
145             return cvGetOpenGlProp_GTK(name);
146         #else
147             return -1;
148         #endif
149     break;
150
151     default:
152         return -1;
153     }
154 }
155
156 void cv::namedWindow( const string& winname, int flags )
157 {
158     cvNamedWindow( winname.c_str(), flags );
159 }
160
161 void cv::destroyWindow( const string& winname )
162 {
163     cvDestroyWindow( winname.c_str() );
164 }
165
166 void cv::destroyAllWindows()
167 {
168     cvDestroyAllWindows();
169 }
170
171 void cv::resizeWindow( const string& winname, int width, int height )
172 {
173     cvResizeWindow( winname.c_str(), width, height );
174 }
175
176 void cv::moveWindow( const string& winname, int x, int y )
177 {
178     cvMoveWindow( winname.c_str(), x, y );
179 }
180
181 void cv::setWindowProperty(const string& winname, int prop_id, double prop_value)
182 {
183     cvSetWindowProperty( winname.c_str(), prop_id, prop_value);
184 }
185
186 double cv::getWindowProperty(const string& winname, int prop_id)
187 {
188     return cvGetWindowProperty(winname.c_str(), prop_id);
189 }
190
191 int cv::waitKey(int delay)
192 {
193     return cvWaitKey(delay);
194 }
195
196 int cv::createTrackbar(const string& trackbarName, const string& winName,
197                    int* value, int count, TrackbarCallback callback,
198                    void* userdata)
199 {
200     return cvCreateTrackbar2(trackbarName.c_str(), winName.c_str(),
201                              value, count, callback, userdata);
202 }
203
204 void cv::setTrackbarPos( const string& trackbarName, const string& winName, int value )
205 {
206     cvSetTrackbarPos(trackbarName.c_str(), winName.c_str(), value );
207 }
208
209 int cv::getTrackbarPos( const string& trackbarName, const string& winName )
210 {
211     return cvGetTrackbarPos(trackbarName.c_str(), winName.c_str());
212 }
213
214 void cv::setMouseCallback( const string& windowName, MouseCallback onMouse, void* param)
215 {
216     cvSetMouseCallback(windowName.c_str(), onMouse, param);
217 }
218
219 int cv::startWindowThread()
220 {
221     return cvStartWindowThread();
222 }
223
224 // OpenGL support
225
226 void cv::setOpenGlDrawCallback(const string& name, OpenGlDrawCallback callback, void* userdata)
227 {
228     cvSetOpenGlDrawCallback(name.c_str(), callback, userdata);
229 }
230
231 void cv::setOpenGlContext(const string& windowName)
232 {
233     cvSetOpenGlContext(windowName.c_str());
234 }
235
236 void cv::updateWindow(const string& windowName)
237 {
238     cvUpdateWindow(windowName.c_str());
239 }
240
241 #ifdef HAVE_OPENGL
242 namespace
243 {
244     std::map<std::string, cv::ogl::Texture2D> wndTexs;
245     std::map<std::string, cv::ogl::Texture2D> ownWndTexs;
246     std::map<std::string, cv::ogl::Buffer> ownWndBufs;
247
248     void glDrawTextureCallback(void* userdata)
249     {
250         cv::ogl::Texture2D* texObj = static_cast<cv::ogl::Texture2D*>(userdata);
251
252         cv::ogl::render(*texObj);
253     }
254 }
255 #endif // HAVE_OPENGL
256
257 void cv::imshow( const string& winname, InputArray _img )
258 {
259     const Size size = _img.size();
260 #ifndef HAVE_OPENGL
261     CV_Assert(size.width>0 && size.height>0);
262     {
263         Mat img = _img.getMat();
264         CvMat c_img = img;
265         cvShowImage(winname.c_str(), &c_img);
266     }
267 #else
268     const double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
269     CV_Assert(size.width>0 && size.height>0);
270
271     if (useGl <= 0)
272     {
273         Mat img = _img.getMat();
274         CvMat c_img = img;
275         cvShowImage(winname.c_str(), &c_img);
276     }
277     else
278     {
279         const double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
280
281         if (autoSize > 0)
282         {
283             resizeWindow(winname, size.width, size.height);
284         }
285
286         setOpenGlContext(winname);
287
288         if (_img.kind() == _InputArray::OPENGL_TEXTURE)
289         {
290             cv::ogl::Texture2D& tex = wndTexs[winname];
291
292             tex = _img.getOGlTexture2D();
293
294             tex.setAutoRelease(false);
295
296             setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
297         }
298         else
299         {
300             cv::ogl::Texture2D& tex = ownWndTexs[winname];
301
302             if (_img.kind() == _InputArray::GPU_MAT)
303             {
304                 cv::ogl::Buffer& buf = ownWndBufs[winname];
305                 buf.copyFrom(_img);
306                 buf.setAutoRelease(false);
307
308                 tex.copyFrom(buf);
309                 tex.setAutoRelease(false);
310             }
311             else
312             {
313                 tex.copyFrom(_img);
314             }
315
316             tex.setAutoRelease(false);
317
318             setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
319         }
320
321         updateWindow(winname);
322     }
323 #endif
324 }
325
326 void cv::pointCloudShow(const string&, const GlCamera&, const GlArrays&)
327 {
328     CV_Error(CV_StsNotImplemented, "This function in deprecated, do not use it");
329 }
330
331 void cv::pointCloudShow(const string&, const GlCamera&, InputArray, InputArray)
332 {
333     CV_Error(CV_StsNotImplemented, "This function in deprecated, do not use it");
334 }
335
336 // Without OpenGL
337
338 #ifndef HAVE_OPENGL
339
340 CV_IMPL void cvSetOpenGlDrawCallback(const char*, CvOpenGlDrawCallback, void*)
341 {
342     CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
343 }
344
345 CV_IMPL void cvSetOpenGlContext(const char*)
346 {
347     CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
348 }
349
350 CV_IMPL void cvUpdateWindow(const char*)
351 {
352     CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
353 }
354
355 #endif // !HAVE_OPENGL
356
357 #if defined (HAVE_QT)
358
359 CvFont cv::fontQt(const string& nameFont, int pointSize, Scalar color, int weight,  int style, int /*spacing*/)
360 {
361 return cvFontQt(nameFont.c_str(), pointSize,color,weight, style);
362 }
363
364 void cv::addText( const Mat& img, const string& text, Point org, CvFont font)
365 {
366     CvMat _img = img;
367     cvAddText( &_img, text.c_str(), org,&font);
368 }
369
370 void cv::displayStatusBar(const string& name,  const string& text, int delayms)
371 {
372     cvDisplayStatusBar(name.c_str(),text.c_str(), delayms);
373 }
374
375 void cv::displayOverlay(const string& name,  const string& text, int delayms)
376 {
377     cvDisplayOverlay(name.c_str(),text.c_str(), delayms);
378 }
379
380 int cv::startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[])
381 {
382     return cvStartLoop(pt2Func, argc, argv);
383 }
384
385 void cv::stopLoop()
386 {
387     cvStopLoop();
388 }
389
390 void cv::saveWindowParameters(const string& windowName)
391 {
392     cvSaveWindowParameters(windowName.c_str());
393 }
394
395 void cv::loadWindowParameters(const string& windowName)
396 {
397     cvLoadWindowParameters(windowName.c_str());
398 }
399
400 int cv::createButton(const string& button_name, ButtonCallback on_change, void* userdata, int button_type , bool initial_button_state  )
401 {
402     return cvCreateButton(button_name.c_str(), on_change, userdata, button_type , initial_button_state );
403 }
404
405 #else
406
407 CvFont cv::fontQt(const string&, int, Scalar, int,  int, int)
408 {
409     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
410     return CvFont();
411 }
412
413 void cv::addText( const Mat&, const string&, Point, CvFont)
414 {
415     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
416 }
417
418 void cv::displayStatusBar(const string&,  const string&, int)
419 {
420     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
421 }
422
423 void cv::displayOverlay(const string&,  const string&, int )
424 {
425     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
426 }
427
428 int cv::startLoop(int (*)(int argc, char *argv[]), int , char**)
429 {
430     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
431     return 0;
432 }
433
434 void cv::stopLoop()
435 {
436     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
437 }
438
439 void cv::saveWindowParameters(const string&)
440 {
441     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
442 }
443
444 void cv::loadWindowParameters(const string&)
445 {
446     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
447 }
448
449 int cv::createButton(const string&, ButtonCallback, void*, int , bool )
450 {
451     CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
452     return 0;
453 }
454
455 #endif
456
457 #if   defined(HAVE_WIN32UI)   // see window_w32.cpp
458 #elif defined (HAVE_GTK)      // see window_gtk.cpp
459 #elif defined (HAVE_COCOA)    // see window_carbon.cpp
460 #elif defined (HAVE_CARBON)
461 #elif defined (HAVE_QT)       //YV see window_QT.cpp
462
463 #else
464
465 // No windowing system present at compile time ;-(
466 //
467 // We will build place holders that don't break the API but give an error
468 // at runtime. This way people can choose to replace an installed HighGUI
469 // version with a more capable one without a need to recompile dependent
470 // applications or libraries.
471
472
473 #define CV_NO_GUI_ERROR(funcname) \
474     cvError( CV_StsError, funcname, \
475     "The function is not implemented. " \
476     "Rebuild the library with Windows, GTK+ 2.x or Carbon support. "\
477     "If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script", \
478     __FILE__, __LINE__ )
479
480
481 CV_IMPL int cvNamedWindow( const char*, int )
482 {
483     CV_NO_GUI_ERROR("cvNamedWindow");
484     return -1;
485 }
486
487 CV_IMPL void cvDestroyWindow( const char* )
488 {
489     CV_NO_GUI_ERROR( "cvDestroyWindow" );
490 }
491
492 CV_IMPL void
493 cvDestroyAllWindows( void )
494 {
495     CV_NO_GUI_ERROR( "cvDestroyAllWindows" );
496 }
497
498 CV_IMPL void
499 cvShowImage( const char*, const CvArr* )
500 {
501     CV_NO_GUI_ERROR( "cvShowImage" );
502 }
503
504 CV_IMPL void cvResizeWindow( const char*, int, int )
505 {
506     CV_NO_GUI_ERROR( "cvResizeWindow" );
507 }
508
509 CV_IMPL void cvMoveWindow( const char*, int, int )
510 {
511     CV_NO_GUI_ERROR( "cvMoveWindow" );
512 }
513
514 CV_IMPL int
515 cvCreateTrackbar( const char*, const char*,
516                   int*, int, CvTrackbarCallback )
517 {
518     CV_NO_GUI_ERROR( "cvCreateTrackbar" );
519     return -1;
520 }
521
522 CV_IMPL int
523 cvCreateTrackbar2( const char* /*trackbar_name*/, const char* /*window_name*/,
524                    int* /*val*/, int /*count*/, CvTrackbarCallback2 /*on_notify2*/,
525                    void* /*userdata*/ )
526 {
527     CV_NO_GUI_ERROR( "cvCreateTrackbar2" );
528     return -1;
529 }
530
531 CV_IMPL void
532 cvSetMouseCallback( const char*, CvMouseCallback, void* )
533 {
534     CV_NO_GUI_ERROR( "cvSetMouseCallback" );
535 }
536
537 CV_IMPL int cvGetTrackbarPos( const char*, const char* )
538 {
539     CV_NO_GUI_ERROR( "cvGetTrackbarPos" );
540     return -1;
541 }
542
543 CV_IMPL void cvSetTrackbarPos( const char*, const char*, int )
544 {
545     CV_NO_GUI_ERROR( "cvSetTrackbarPos" );
546 }
547
548 CV_IMPL void* cvGetWindowHandle( const char* )
549 {
550     CV_NO_GUI_ERROR( "cvGetWindowHandle" );
551     return 0;
552 }
553
554 CV_IMPL const char* cvGetWindowName( void* )
555 {
556     CV_NO_GUI_ERROR( "cvGetWindowName" );
557     return 0;
558 }
559
560 CV_IMPL int cvWaitKey( int )
561 {
562     CV_NO_GUI_ERROR( "cvWaitKey" );
563     return -1;
564 }
565
566 CV_IMPL int cvInitSystem( int , char** )
567 {
568
569     CV_NO_GUI_ERROR( "cvInitSystem" );
570     return -1;
571 }
572
573 CV_IMPL int cvStartWindowThread()
574 {
575
576     CV_NO_GUI_ERROR( "cvStartWindowThread" );
577     return -1;
578 }
579
580 //-------- Qt ---------
581 CV_IMPL void cvAddText( const CvArr*, const char*, CvPoint , CvFont* )
582 {
583     CV_NO_GUI_ERROR("cvAddText");
584 }
585
586 CV_IMPL void cvDisplayStatusBar(const char* , const char* , int )
587 {
588     CV_NO_GUI_ERROR("cvDisplayStatusBar");
589 }
590
591 CV_IMPL void cvDisplayOverlay(const char* , const char* , int )
592 {
593     CV_NO_GUI_ERROR("cvNamedWindow");
594 }
595
596 CV_IMPL int cvStartLoop(int (*)(int argc, char *argv[]), int , char* argv[])
597 {
598     (void)argv;
599     CV_NO_GUI_ERROR("cvStartLoop");
600     return -1;
601 }
602
603 CV_IMPL void cvStopLoop()
604 {
605     CV_NO_GUI_ERROR("cvStopLoop");
606 }
607
608 CV_IMPL void cvSaveWindowParameters(const char* )
609 {
610     CV_NO_GUI_ERROR("cvSaveWindowParameters");
611 }
612
613 // CV_IMPL void cvLoadWindowParameterss(const char* name)
614 // {
615 //     CV_NO_GUI_ERROR("cvLoadWindowParameters");
616 // }
617
618 CV_IMPL int cvCreateButton(const char*, void (*)(int, void*), void*, int, int)
619 {
620     CV_NO_GUI_ERROR("cvCreateButton");
621     return -1;
622 }
623
624
625 #endif
626
627 /* End of file. */