window.cpp 16.1 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
#include <map>
wester committed
44
#include "opencv2/core/opengl_interop.hpp"
wester committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

// in later times, use this file as a dispatcher to implementations like cvcap.cpp

CV_IMPL void cvSetWindowProperty(const char* name, int prop_id, double prop_value)
{
    switch(prop_id)
    {
    //change between fullscreen or not.
    case CV_WND_PROP_FULLSCREEN:

        if (!name || (prop_value!=CV_WINDOW_NORMAL && prop_value!=CV_WINDOW_FULLSCREEN))//bad argument
            break;

        #if defined (HAVE_QT)
            cvSetModeWindow_QT(name,prop_value);
        #elif defined(HAVE_WIN32UI)
            cvSetModeWindow_W32(name,prop_value);
        #elif defined (HAVE_GTK)
            cvSetModeWindow_GTK(name,prop_value);
        #elif defined (HAVE_CARBON)
            cvSetModeWindow_CARBON(name,prop_value);
        #elif defined (HAVE_COCOA)
            cvSetModeWindow_COCOA(name,prop_value);
        #endif
    break;

    case CV_WND_PROP_AUTOSIZE:
        #if defined (HAVE_QT)
            cvSetPropWindow_QT(name,prop_value);
        #endif
    break;

    case CV_WND_PROP_ASPECTRATIO:
        #if defined (HAVE_QT)
            cvSetRatioWindow_QT(name,prop_value);
        #endif
    break;

    default:;
    }
}

/* return -1 if error */
CV_IMPL double cvGetWindowProperty(const char* name, int prop_id)
{
    if (!name)
        return -1;

    switch(prop_id)
    {
    case CV_WND_PROP_FULLSCREEN:

        #if defined (HAVE_QT)
            return cvGetModeWindow_QT(name);
        #elif defined(HAVE_WIN32UI)
            return cvGetModeWindow_W32(name);
        #elif defined (HAVE_GTK)
            return cvGetModeWindow_GTK(name);
        #elif defined (HAVE_CARBON)
            return cvGetModeWindow_CARBON(name);
        #elif defined (HAVE_COCOA)
            return cvGetModeWindow_COCOA(name);
        #else
            return -1;
        #endif
    break;

    case CV_WND_PROP_AUTOSIZE:

        #if defined (HAVE_QT)
            return cvGetPropWindow_QT(name);
        #elif defined(HAVE_WIN32UI)
            return cvGetPropWindowAutoSize_W32(name);
        #elif defined (HAVE_GTK)
            return cvGetPropWindowAutoSize_GTK(name);
        #else
            return -1;
        #endif
    break;

    case CV_WND_PROP_ASPECTRATIO:

        #if defined (HAVE_QT)
            return cvGetRatioWindow_QT(name);
        #elif defined(HAVE_WIN32UI)
            return cvGetRatioWindow_W32(name);
        #elif defined (HAVE_GTK)
            return cvGetRatioWindow_GTK(name);
        #else
            return -1;
        #endif
    break;

    case CV_WND_PROP_OPENGL:

        #if defined (HAVE_QT)
            return cvGetOpenGlProp_QT(name);
        #elif defined(HAVE_WIN32UI)
            return cvGetOpenGlProp_W32(name);
        #elif defined (HAVE_GTK)
            return cvGetOpenGlProp_GTK(name);
        #else
            return -1;
        #endif
    break;

    default:
        return -1;
    }
}

wester committed
156
void cv::namedWindow( const string& winname, int flags )
wester committed
157 158 159 160
{
    cvNamedWindow( winname.c_str(), flags );
}

wester committed
161
void cv::destroyWindow( const string& winname )
wester committed
162 163 164 165 166 167 168 169 170
{
    cvDestroyWindow( winname.c_str() );
}

void cv::destroyAllWindows()
{
    cvDestroyAllWindows();
}

wester committed
171
void cv::resizeWindow( const string& winname, int width, int height )
wester committed
172 173 174 175
{
    cvResizeWindow( winname.c_str(), width, height );
}

wester committed
176
void cv::moveWindow( const string& winname, int x, int y )
wester committed
177 178 179 180
{
    cvMoveWindow( winname.c_str(), x, y );
}

wester committed
181
void cv::setWindowProperty(const string& winname, int prop_id, double prop_value)
wester committed
182 183 184 185
{
    cvSetWindowProperty( winname.c_str(), prop_id, prop_value);
}

wester committed
186
double cv::getWindowProperty(const string& winname, int prop_id)
wester committed
187 188 189 190 191 192
{
    return cvGetWindowProperty(winname.c_str(), prop_id);
}

int cv::waitKey(int delay)
{
a  
Kai Westerkamp committed
193
    return cvWaitKey(delay);
wester committed
194 195
}

wester committed
196
int cv::createTrackbar(const string& trackbarName, const string& winName,
wester committed
197 198 199 200 201 202 203
                   int* value, int count, TrackbarCallback callback,
                   void* userdata)
{
    return cvCreateTrackbar2(trackbarName.c_str(), winName.c_str(),
                             value, count, callback, userdata);
}

wester committed
204
void cv::setTrackbarPos( const string& trackbarName, const string& winName, int value )
wester committed
205 206 207 208
{
    cvSetTrackbarPos(trackbarName.c_str(), winName.c_str(), value );
}

wester committed
209
int cv::getTrackbarPos( const string& trackbarName, const string& winName )
wester committed
210 211 212 213
{
    return cvGetTrackbarPos(trackbarName.c_str(), winName.c_str());
}

wester committed
214
void cv::setMouseCallback( const string& windowName, MouseCallback onMouse, void* param)
wester committed
215 216 217 218 219 220 221 222 223 224 225
{
    cvSetMouseCallback(windowName.c_str(), onMouse, param);
}

int cv::startWindowThread()
{
    return cvStartWindowThread();
}

// OpenGL support

wester committed
226
void cv::setOpenGlDrawCallback(const string& name, OpenGlDrawCallback callback, void* userdata)
wester committed
227 228 229 230
{
    cvSetOpenGlDrawCallback(name.c_str(), callback, userdata);
}

wester committed
231
void cv::setOpenGlContext(const string& windowName)
wester committed
232 233 234 235
{
    cvSetOpenGlContext(windowName.c_str());
}

wester committed
236
void cv::updateWindow(const string& windowName)
wester committed
237 238 239 240 241 242 243
{
    cvUpdateWindow(windowName.c_str());
}

#ifdef HAVE_OPENGL
namespace
{
wester committed
244 245 246
    std::map<std::string, cv::ogl::Texture2D> wndTexs;
    std::map<std::string, cv::ogl::Texture2D> ownWndTexs;
    std::map<std::string, cv::ogl::Buffer> ownWndBufs;
wester committed
247 248 249 250 251 252 253 254 255 256

    void glDrawTextureCallback(void* userdata)
    {
        cv::ogl::Texture2D* texObj = static_cast<cv::ogl::Texture2D*>(userdata);

        cv::ogl::render(*texObj);
    }
}
#endif // HAVE_OPENGL

wester committed
257
void cv::imshow( const string& winname, InputArray _img )
wester committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
{
    const Size size = _img.size();
#ifndef HAVE_OPENGL
    CV_Assert(size.width>0 && size.height>0);
    {
        Mat img = _img.getMat();
        CvMat c_img = img;
        cvShowImage(winname.c_str(), &c_img);
    }
#else
    const double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
    CV_Assert(size.width>0 && size.height>0);

    if (useGl <= 0)
    {
        Mat img = _img.getMat();
        CvMat c_img = img;
        cvShowImage(winname.c_str(), &c_img);
    }
    else
    {
        const double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);

        if (autoSize > 0)
        {
            resizeWindow(winname, size.width, size.height);
        }

        setOpenGlContext(winname);

wester committed
288
        if (_img.kind() == _InputArray::OPENGL_TEXTURE)
wester committed
289
        {
wester committed
290 291 292
            cv::ogl::Texture2D& tex = wndTexs[winname];

            tex = _img.getOGlTexture2D();
wester committed
293 294

            tex.setAutoRelease(false);
wester committed
295 296

            setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
wester committed
297 298 299
        }
        else
        {
wester committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
            cv::ogl::Texture2D& tex = ownWndTexs[winname];

            if (_img.kind() == _InputArray::GPU_MAT)
            {
                cv::ogl::Buffer& buf = ownWndBufs[winname];
                buf.copyFrom(_img);
                buf.setAutoRelease(false);

                tex.copyFrom(buf);
                tex.setAutoRelease(false);
            }
            else
            {
                tex.copyFrom(_img);
            }
wester committed
315

wester committed
316
            tex.setAutoRelease(false);
wester committed
317

wester committed
318 319
            setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
        }
wester committed
320 321 322 323 324 325

        updateWindow(winname);
    }
#endif
}

wester committed
326
void cv::pointCloudShow(const string&, const GlCamera&, const GlArrays&)
wester committed
327
{
wester committed
328 329
    CV_Error(CV_StsNotImplemented, "This function in deprecated, do not use it");
}
wester committed
330

wester committed
331 332 333
void cv::pointCloudShow(const string&, const GlCamera&, InputArray, InputArray)
{
    CV_Error(CV_StsNotImplemented, "This function in deprecated, do not use it");
wester committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
}

// Without OpenGL

#ifndef HAVE_OPENGL

CV_IMPL void cvSetOpenGlDrawCallback(const char*, CvOpenGlDrawCallback, void*)
{
    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
}

CV_IMPL void cvSetOpenGlContext(const char*)
{
    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
}

CV_IMPL void cvUpdateWindow(const char*)
{
    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
}

#endif // !HAVE_OPENGL

#if defined (HAVE_QT)

wester committed
359
CvFont cv::fontQt(const string& nameFont, int pointSize, Scalar color, int weight,  int style, int /*spacing*/)
wester committed
360
{
wester committed
361
return cvFontQt(nameFont.c_str(), pointSize,color,weight, style);
wester committed
362 363
}

wester committed
364
void cv::addText( const Mat& img, const string& text, Point org, CvFont font)
wester committed
365 366
{
    CvMat _img = img;
wester committed
367
    cvAddText( &_img, text.c_str(), org,&font);
wester committed
368 369
}

wester committed
370
void cv::displayStatusBar(const string& name,  const string& text, int delayms)
wester committed
371 372 373 374
{
    cvDisplayStatusBar(name.c_str(),text.c_str(), delayms);
}

wester committed
375
void cv::displayOverlay(const string& name,  const string& text, int delayms)
wester committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389
{
    cvDisplayOverlay(name.c_str(),text.c_str(), delayms);
}

int cv::startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[])
{
    return cvStartLoop(pt2Func, argc, argv);
}

void cv::stopLoop()
{
    cvStopLoop();
}

wester committed
390
void cv::saveWindowParameters(const string& windowName)
wester committed
391 392 393 394
{
    cvSaveWindowParameters(windowName.c_str());
}

wester committed
395
void cv::loadWindowParameters(const string& windowName)
wester committed
396 397 398 399
{
    cvLoadWindowParameters(windowName.c_str());
}

wester committed
400
int cv::createButton(const string& button_name, ButtonCallback on_change, void* userdata, int button_type , bool initial_button_state  )
wester committed
401 402 403 404 405 406
{
    return cvCreateButton(button_name.c_str(), on_change, userdata, button_type , initial_button_state );
}

#else

wester committed
407
CvFont cv::fontQt(const string&, int, Scalar, int,  int, int)
wester committed
408
{
a  
Kai Westerkamp committed
409
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
410
    return CvFont();
wester committed
411 412
}

wester committed
413
void cv::addText( const Mat&, const string&, Point, CvFont)
wester committed
414
{
a  
Kai Westerkamp committed
415
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
416 417
}

wester committed
418
void cv::displayStatusBar(const string&,  const string&, int)
wester committed
419
{
a  
Kai Westerkamp committed
420
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
421 422
}

wester committed
423
void cv::displayOverlay(const string&,  const string&, int )
wester committed
424
{
a  
Kai Westerkamp committed
425
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
426 427 428 429
}

int cv::startLoop(int (*)(int argc, char *argv[]), int , char**)
{
a  
Kai Westerkamp committed
430
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
431 432 433 434 435
    return 0;
}

void cv::stopLoop()
{
a  
Kai Westerkamp committed
436
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
437 438
}

wester committed
439
void cv::saveWindowParameters(const string&)
wester committed
440
{
a  
Kai Westerkamp committed
441
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
442 443
}

wester committed
444
void cv::loadWindowParameters(const string&)
wester committed
445
{
a  
Kai Westerkamp committed
446
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
447 448
}

wester committed
449
int cv::createButton(const string&, ButtonCallback, void*, int , bool )
wester committed
450
{
a  
Kai Westerkamp committed
451
    CV_Error(CV_StsNotImplemented, "The library is compiled without QT support");
wester committed
452 453 454 455 456
    return 0;
}

#endif

wester committed
457
#if   defined(HAVE_WIN32UI)   // see window_w32.cpp
wester committed
458 459 460
#elif defined (HAVE_GTK)      // see window_gtk.cpp
#elif defined (HAVE_COCOA)    // see window_carbon.cpp
#elif defined (HAVE_CARBON)
wester committed
461
#elif defined (HAVE_QT)       //YV see window_QT.cpp
wester committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

#else

// No windowing system present at compile time ;-(
//
// We will build place holders that don't break the API but give an error
// at runtime. This way people can choose to replace an installed HighGUI
// version with a more capable one without a need to recompile dependent
// applications or libraries.


#define CV_NO_GUI_ERROR(funcname) \
    cvError( CV_StsError, funcname, \
    "The function is not implemented. " \
    "Rebuild the library with Windows, GTK+ 2.x or Carbon support. "\
    "If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script", \
    __FILE__, __LINE__ )


CV_IMPL int cvNamedWindow( const char*, int )
{
    CV_NO_GUI_ERROR("cvNamedWindow");
    return -1;
}

CV_IMPL void cvDestroyWindow( const char* )
{
    CV_NO_GUI_ERROR( "cvDestroyWindow" );
}

CV_IMPL void
cvDestroyAllWindows( void )
{
    CV_NO_GUI_ERROR( "cvDestroyAllWindows" );
}

CV_IMPL void
cvShowImage( const char*, const CvArr* )
{
    CV_NO_GUI_ERROR( "cvShowImage" );
}

CV_IMPL void cvResizeWindow( const char*, int, int )
{
    CV_NO_GUI_ERROR( "cvResizeWindow" );
}

CV_IMPL void cvMoveWindow( const char*, int, int )
{
    CV_NO_GUI_ERROR( "cvMoveWindow" );
}

CV_IMPL int
cvCreateTrackbar( const char*, const char*,
                  int*, int, CvTrackbarCallback )
{
    CV_NO_GUI_ERROR( "cvCreateTrackbar" );
    return -1;
}

CV_IMPL int
cvCreateTrackbar2( const char* /*trackbar_name*/, const char* /*window_name*/,
                   int* /*val*/, int /*count*/, CvTrackbarCallback2 /*on_notify2*/,
                   void* /*userdata*/ )
{
    CV_NO_GUI_ERROR( "cvCreateTrackbar2" );
    return -1;
}

CV_IMPL void
cvSetMouseCallback( const char*, CvMouseCallback, void* )
{
    CV_NO_GUI_ERROR( "cvSetMouseCallback" );
}

CV_IMPL int cvGetTrackbarPos( const char*, const char* )
{
    CV_NO_GUI_ERROR( "cvGetTrackbarPos" );
    return -1;
}

CV_IMPL void cvSetTrackbarPos( const char*, const char*, int )
{
    CV_NO_GUI_ERROR( "cvSetTrackbarPos" );
}

CV_IMPL void cvSetTrackbarMax(const char*, const char*, int)
{
    CV_NO_GUI_ERROR( "cvSetTrackbarMax" );
}

CV_IMPL void* cvGetWindowHandle( const char* )
{
    CV_NO_GUI_ERROR( "cvGetWindowHandle" );
    return 0;
}

CV_IMPL const char* cvGetWindowName( void* )
{
    CV_NO_GUI_ERROR( "cvGetWindowName" );
    return 0;
}

CV_IMPL int cvWaitKey( int )
{
    CV_NO_GUI_ERROR( "cvWaitKey" );
    return -1;
}

CV_IMPL int cvInitSystem( int , char** )
{

    CV_NO_GUI_ERROR( "cvInitSystem" );
    return -1;
}

CV_IMPL int cvStartWindowThread()
{

    CV_NO_GUI_ERROR( "cvStartWindowThread" );
    return -1;
}

//-------- Qt ---------
CV_IMPL void cvAddText( const CvArr*, const char*, CvPoint , CvFont* )
{
    CV_NO_GUI_ERROR("cvAddText");
}

CV_IMPL void cvDisplayStatusBar(const char* , const char* , int )
{
    CV_NO_GUI_ERROR("cvDisplayStatusBar");
}

CV_IMPL void cvDisplayOverlay(const char* , const char* , int )
{
    CV_NO_GUI_ERROR("cvNamedWindow");
}

CV_IMPL int cvStartLoop(int (*)(int argc, char *argv[]), int , char* argv[])
{
    (void)argv;
    CV_NO_GUI_ERROR("cvStartLoop");
    return -1;
}

CV_IMPL void cvStopLoop()
{
    CV_NO_GUI_ERROR("cvStopLoop");
}

CV_IMPL void cvSaveWindowParameters(const char* )
{
    CV_NO_GUI_ERROR("cvSaveWindowParameters");
}

// CV_IMPL void cvLoadWindowParameterss(const char* name)
// {
//     CV_NO_GUI_ERROR("cvLoadWindowParameters");
// }

CV_IMPL int cvCreateButton(const char*, void (*)(int, void*), void*, int, int)
{
    CV_NO_GUI_ERROR("cvCreateButton");
    return -1;
}


#endif

/* End of file. */