Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ImageWidget.h"
  2.  
  3. ImageWidget::ImageWidget(QWidget* parent, int width, int height) :
  4.     QGLWidget(parent), _imageRef(NULL) {
  5.  
  6.     setFormat(QGLFormat(QGL::SampleBuffers));
  7.     setAutoFillBackground(true);
  8.  
  9.     setMinimumSize(width, height);
  10.     setMaximumSize(width, height);
  11. }
  12.  
  13. void ImageWidget::refreshImage(IplImage* image) {
  14.  
  15.     _imageRef = image;
  16.     update();
  17. }
  18.  
  19. void ImageWidget::setupViewPort(int width, int height) {
  20.  
  21.     glViewport(0, 0, static_cast<GLsizei> (width), static_cast<GLsizei> (height));
  22.     glMatrixMode(GL_PROJECTION);
  23.     glLoadIdentity();
  24.  
  25.     gluOrtho2D(0.0, static_cast<GLdouble> (width), 0.0, static_cast<GLdouble> (height));
  26.     glMatrixMode(GL_MODELVIEW);
  27.     glLoadIdentity();
  28. }
  29.  
  30. void ImageWidget::resizeGL(int width, int height) {
  31.  
  32.     setupViewPort(width, height);
  33. }
  34.  
  35. void ImageWidget::paintEvent(QPaintEvent *paintEvent) {
  36.  
  37.     //--- Make this widget the current context for OpenGL drawing
  38.     makeCurrent();
  39.  
  40.     //Save current state, QPainter modifies MODELVIEW matrix.
  41.     glMatrixMode(GL_MODELVIEW);
  42.     glPushMatrix();
  43.  
  44.     setupViewPort(width(), height());
  45.  
  46.     glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
  47.     glLoadIdentity();
  48.  
  49.     drawBackgroundImage();
  50.  
  51.     //Restore state after drawing pure openGL
  52.     glMatrixMode(GL_MODELVIEW);
  53.     glPopMatrix();
  54.  
  55.     QPainter painter(this);
  56.     painter.end();
  57.  
  58.     //--- Set OpenGL drawing context to no area
  59.     doneCurrent();
  60. }
  61.  
  62. void ImageWidget::drawBackgroundImage() {
  63.  
  64.     if (_imageRef != NULL) {
  65.  
  66.         //Flip image around y axis to be displayed correctly; IplImage's coordinate system is the upside-down of openGL's.
  67.         glPixelZoom(1.0f, -1.0f);
  68.         glRasterPos2i(0, height());
  69.         glDrawPixels(_imageRef->width, _imageRef->height, GL_BGR_EXT, GL_UNSIGNED_BYTE, _imageRef->imageData);
  70.     } else {
  71.         logWARNING("Null background image.");
  72.     }
  73. }
  74.  
  75. #include "ImageWidget.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement