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.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 void cv::setTrackbarMax(const String& trackbarName, const String& winName, int maxval) 210 { 211 cvSetTrackbarMax(trackbarName.c_str(), winName.c_str(), maxval); 212 } 213 214 int cv::getTrackbarPos( const String& trackbarName, const String& winName ) 215 { 216 return cvGetTrackbarPos(trackbarName.c_str(), winName.c_str()); 217 } 218 219 void cv::setMouseCallback( const String& windowName, MouseCallback onMouse, void* param) 220 { 221 cvSetMouseCallback(windowName.c_str(), onMouse, param); 222 } 223 224 int cv::getMouseWheelDelta( int flags ) 225 { 226 return CV_GET_WHEEL_DELTA(flags); 227 } 228 229 int cv::startWindowThread() 230 { 231 return cvStartWindowThread(); 232 } 233 234 // OpenGL support 235 236 void cv::setOpenGlDrawCallback(const String& name, OpenGlDrawCallback callback, void* userdata) 237 { 238 cvSetOpenGlDrawCallback(name.c_str(), callback, userdata); 239 } 240 241 void cv::setOpenGlContext(const String& windowName) 242 { 243 cvSetOpenGlContext(windowName.c_str()); 244 } 245 246 void cv::updateWindow(const String& windowName) 247 { 248 cvUpdateWindow(windowName.c_str()); 249 } 250 251 #ifdef HAVE_OPENGL 252 namespace 253 { 254 std::map<cv::String, cv::ogl::Texture2D> wndTexs; 255 std::map<cv::String, cv::ogl::Texture2D> ownWndTexs; 256 std::map<cv::String, cv::ogl::Buffer> ownWndBufs; 257 258 void glDrawTextureCallback(void* userdata) 259 { 260 cv::ogl::Texture2D* texObj = static_cast<cv::ogl::Texture2D*>(userdata); 261 262 cv::ogl::render(*texObj); 263 } 264 } 265 #endif // HAVE_OPENGL 266 267 void cv::imshow( const String& winname, InputArray _img ) 268 { 269 const Size size = _img.size(); 270 #ifndef HAVE_OPENGL 271 CV_Assert(size.width>0 && size.height>0); 272 { 273 Mat img = _img.getMat(); 274 CvMat c_img = img; 275 cvShowImage(winname.c_str(), &c_img); 276 } 277 #else 278 const double useGl = getWindowProperty(winname, WND_PROP_OPENGL); 279 CV_Assert(size.width>0 && size.height>0); 280 281 if (useGl <= 0) 282 { 283 Mat img = _img.getMat(); 284 CvMat c_img = img; 285 cvShowImage(winname.c_str(), &c_img); 286 } 287 else 288 { 289 const double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE); 290 291 if (autoSize > 0) 292 { 293 resizeWindow(winname, size.width, size.height); 294 } 295 296 setOpenGlContext(winname); 297 298 cv::ogl::Texture2D& tex = ownWndTexs[winname]; 299 300 if (_img.kind() == _InputArray::CUDA_GPU_MAT) 301 { 302 cv::ogl::Buffer& buf = ownWndBufs[winname]; 303 buf.copyFrom(_img); 304 buf.setAutoRelease(false); 305 306 tex.copyFrom(buf); 307 tex.setAutoRelease(false); 308 } 309 else 310 { 311 tex.copyFrom(_img); 312 } 313 314 tex.setAutoRelease(false); 315 316 setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex); 317 318 updateWindow(winname); 319 } 320 #endif 321 } 322 323 void cv::imshow(const String& winname, const ogl::Texture2D& _tex) 324 { 325 #ifndef HAVE_OPENGL 326 (void) winname; 327 (void) _tex; 328 CV_Error(cv::Error::OpenGlNotSupported, "The library is compiled without OpenGL support"); 329 #else 330 const double useGl = getWindowProperty(winname, WND_PROP_OPENGL); 331 332 if (useGl <= 0) 333 { 334 CV_Error(cv::Error::OpenGlNotSupported, "The window was created without OpenGL context"); 335 } 336 else 337 { 338 const double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE); 339 340 if (autoSize > 0) 341 { 342 Size size = _tex.size(); 343 resizeWindow(winname, size.width, size.height); 344 } 345 346 setOpenGlContext(winname); 347 348 cv::ogl::Texture2D& tex = wndTexs[winname]; 349 350 tex = _tex; 351 352 tex.setAutoRelease(false); 353 354 setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex); 355 356 updateWindow(winname); 357 } 358 #endif 359 } 360 361 // Without OpenGL 362 363 #ifndef HAVE_OPENGL 364 365 CV_IMPL void cvSetOpenGlDrawCallback(const char*, CvOpenGlDrawCallback, void*) 366 { 367 CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support"); 368 } 369 370 CV_IMPL void cvSetOpenGlContext(const char*) 371 { 372 CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support"); 373 } 374 375 CV_IMPL void cvUpdateWindow(const char*) 376 { 377 CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support"); 378 } 379 380 #endif // !HAVE_OPENGL 381 382 #if defined (HAVE_QT) 383 384 cv::QtFont cv::fontQt(const String& nameFont, int pointSize, Scalar color, int weight, int style, int /*spacing*/) 385 { 386 CvFont f = cvFontQt(nameFont.c_str(), pointSize,color,weight, style); 387 void* pf = &f; // to suppress strict-aliasing 388 return *(cv::QtFont*)pf; 389 } 390 391 void cv::addText( const Mat& img, const String& text, Point org, const QtFont& font) 392 { 393 CvMat _img = img; 394 cvAddText( &_img, text.c_str(), org, (CvFont*)&font); 395 } 396 397 void cv::displayStatusBar(const String& name, const String& text, int delayms) 398 { 399 cvDisplayStatusBar(name.c_str(),text.c_str(), delayms); 400 } 401 402 void cv::displayOverlay(const String& name, const String& text, int delayms) 403 { 404 cvDisplayOverlay(name.c_str(),text.c_str(), delayms); 405 } 406 407 int cv::startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]) 408 { 409 return cvStartLoop(pt2Func, argc, argv); 410 } 411 412 void cv::stopLoop() 413 { 414 cvStopLoop(); 415 } 416 417 void cv::saveWindowParameters(const String& windowName) 418 { 419 cvSaveWindowParameters(windowName.c_str()); 420 } 421 422 void cv::loadWindowParameters(const String& windowName) 423 { 424 cvLoadWindowParameters(windowName.c_str()); 425 } 426 427 int cv::createButton(const String& button_name, ButtonCallback on_change, void* userdata, int button_type , bool initial_button_state ) 428 { 429 return cvCreateButton(button_name.c_str(), on_change, userdata, button_type , initial_button_state ); 430 } 431 432 #else 433 434 cv::QtFont cv::fontQt(const String&, int, Scalar, int, int, int) 435 { 436 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 437 return QtFont(); 438 } 439 440 void cv::addText( const Mat&, const String&, Point, const QtFont&) 441 { 442 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 443 } 444 445 void cv::displayStatusBar(const String&, const String&, int) 446 { 447 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 448 } 449 450 void cv::displayOverlay(const String&, const String&, int ) 451 { 452 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 453 } 454 455 int cv::startLoop(int (*)(int argc, char *argv[]), int , char**) 456 { 457 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 458 return 0; 459 } 460 461 void cv::stopLoop() 462 { 463 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 464 } 465 466 void cv::saveWindowParameters(const String&) 467 { 468 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 469 } 470 471 void cv::loadWindowParameters(const String&) 472 { 473 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 474 } 475 476 int cv::createButton(const String&, ButtonCallback, void*, int , bool ) 477 { 478 CV_Error(CV_StsNotImplemented, "The library is compiled without QT support"); 479 return 0; 480 } 481 482 #endif 483 484 #if defined(HAVE_WIN32UI) // see window_w32.cpp 485 #elif defined (HAVE_GTK) // see window_gtk.cpp 486 #elif defined (HAVE_COCOA) // see window_carbon.cpp 487 #elif defined (HAVE_CARBON) 488 #elif defined (HAVE_QT) //YV see window_QT.cpp 489 490 #else 491 492 // No windowing system present at compile time ;-( 493 // 494 // We will build place holders that don't break the API but give an error 495 // at runtime. This way people can choose to replace an installed HighGUI 496 // version with a more capable one without a need to recompile dependent 497 // applications or libraries. 498 499 void cv::setWindowTitle(const String&, const String&) 500 { 501 CV_Error(Error::StsNotImplemented, "The function is not implemented. " 502 "Rebuild the library with Windows, GTK+ 2.x or Carbon support. " 503 "If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script"); 504 } 505 506 #define CV_NO_GUI_ERROR(funcname) \ 507 cvError( CV_StsError, funcname, \ 508 "The function is not implemented. " \ 509 "Rebuild the library with Windows, GTK+ 2.x or Carbon support. "\ 510 "If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script", \ 511 __FILE__, __LINE__ ) 512 513 514 CV_IMPL int cvNamedWindow( const char*, int ) 515 { 516 CV_NO_GUI_ERROR("cvNamedWindow"); 517 return -1; 518 } 519 520 CV_IMPL void cvDestroyWindow( const char* ) 521 { 522 CV_NO_GUI_ERROR( "cvDestroyWindow" ); 523 } 524 525 CV_IMPL void 526 cvDestroyAllWindows( void ) 527 { 528 CV_NO_GUI_ERROR( "cvDestroyAllWindows" ); 529 } 530 531 CV_IMPL void 532 cvShowImage( const char*, const CvArr* ) 533 { 534 CV_NO_GUI_ERROR( "cvShowImage" ); 535 } 536 537 CV_IMPL void cvResizeWindow( const char*, int, int ) 538 { 539 CV_NO_GUI_ERROR( "cvResizeWindow" ); 540 } 541 542 CV_IMPL void cvMoveWindow( const char*, int, int ) 543 { 544 CV_NO_GUI_ERROR( "cvMoveWindow" ); 545 } 546 547 CV_IMPL int 548 cvCreateTrackbar( const char*, const char*, 549 int*, int, CvTrackbarCallback ) 550 { 551 CV_NO_GUI_ERROR( "cvCreateTrackbar" ); 552 return -1; 553 } 554 555 CV_IMPL int 556 cvCreateTrackbar2( const char* /*trackbar_name*/, const char* /*window_name*/, 557 int* /*val*/, int /*count*/, CvTrackbarCallback2 /*on_notify2*/, 558 void* /*userdata*/ ) 559 { 560 CV_NO_GUI_ERROR( "cvCreateTrackbar2" ); 561 return -1; 562 } 563 564 CV_IMPL void 565 cvSetMouseCallback( const char*, CvMouseCallback, void* ) 566 { 567 CV_NO_GUI_ERROR( "cvSetMouseCallback" ); 568 } 569 570 CV_IMPL int cvGetTrackbarPos( const char*, const char* ) 571 { 572 CV_NO_GUI_ERROR( "cvGetTrackbarPos" ); 573 return -1; 574 } 575 576 CV_IMPL void cvSetTrackbarPos( const char*, const char*, int ) 577 { 578 CV_NO_GUI_ERROR( "cvSetTrackbarPos" ); 579 } 580 581 CV_IMPL void cvSetTrackbarMax(const char*, const char*, int) 582 { 583 CV_NO_GUI_ERROR( "cvSetTrackbarMax" ); 584 } 585 586 CV_IMPL void* cvGetWindowHandle( const char* ) 587 { 588 CV_NO_GUI_ERROR( "cvGetWindowHandle" ); 589 return 0; 590 } 591 592 CV_IMPL const char* cvGetWindowName( void* ) 593 { 594 CV_NO_GUI_ERROR( "cvGetWindowName" ); 595 return 0; 596 } 597 598 CV_IMPL int cvWaitKey( int ) 599 { 600 CV_NO_GUI_ERROR( "cvWaitKey" ); 601 return -1; 602 } 603 604 CV_IMPL int cvInitSystem( int , char** ) 605 { 606 607 CV_NO_GUI_ERROR( "cvInitSystem" ); 608 return -1; 609 } 610 611 CV_IMPL int cvStartWindowThread() 612 { 613 614 CV_NO_GUI_ERROR( "cvStartWindowThread" ); 615 return -1; 616 } 617 618 //-------- Qt --------- 619 CV_IMPL void cvAddText( const CvArr*, const char*, CvPoint , CvFont* ) 620 { 621 CV_NO_GUI_ERROR("cvAddText"); 622 } 623 624 CV_IMPL void cvDisplayStatusBar(const char* , const char* , int ) 625 { 626 CV_NO_GUI_ERROR("cvDisplayStatusBar"); 627 } 628 629 CV_IMPL void cvDisplayOverlay(const char* , const char* , int ) 630 { 631 CV_NO_GUI_ERROR("cvNamedWindow"); 632 } 633 634 CV_IMPL int cvStartLoop(int (*)(int argc, char *argv[]), int , char* argv[]) 635 { 636 (void)argv; 637 CV_NO_GUI_ERROR("cvStartLoop"); 638 return -1; 639 } 640 641 CV_IMPL void cvStopLoop() 642 { 643 CV_NO_GUI_ERROR("cvStopLoop"); 644 } 645 646 CV_IMPL void cvSaveWindowParameters(const char* ) 647 { 648 CV_NO_GUI_ERROR("cvSaveWindowParameters"); 649 } 650 651 // CV_IMPL void cvLoadWindowParameterss(const char* name) 652 // { 653 // CV_NO_GUI_ERROR("cvLoadWindowParameters"); 654 // } 655 656 CV_IMPL int cvCreateButton(const char*, void (*)(int, void*), void*, int, int) 657 { 658 CV_NO_GUI_ERROR("cvCreateButton"); 659 return -1; 660 } 661 662 663 #endif 664 665 /* End of file. */ 666