qv4l2: refactored capwin variables
authorOve Brynestad <ovebryne@cisco.com>
Thu, 10 Jul 2014 07:21:10 +0000 (09:21 +0200)
committerHans Verkuil <hans.verkuil@cisco.com>
Fri, 11 Jul 2014 13:32:02 +0000 (15:32 +0200)
Use QSize instead of int w,h; to store sizes
Name improvements
Move non-common variables back to renderer specific class

Signed-off-by: Ove Brynestad <ovebryne@cisco.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
utils/qv4l2/capture-win-gl.cpp
utils/qv4l2/capture-win-qt.cpp
utils/qv4l2/capture-win-qt.h
utils/qv4l2/capture-win.cpp
utils/qv4l2/capture-win.h

index 013e21d..31ae239 100644 (file)
@@ -49,11 +49,11 @@ void CaptureWinGL::resizeEvent(QResizeEvent *event)
        // Get size of frame viewport. Can't use size of m_videoSurface
        // since it is a subwidget of this widget.
        QSize margins = getMargins();
-       m_curWinWidth  = width() - margins.width();
-       m_curWinHeight = height() - margins.height();
+       m_windowSize.setWidth(width() - margins.width());
+       m_windowSize.setHeight(height() - margins.height());
        // Re-calculate
-       m_frameInfo.updated = true;
-       CaptureWin::resizeScaleCrop();
+       m_frame.updated = true;
+       CaptureWin::updateSize();
        // Lock viewport size to follow calculated size
        m_videoSurface.lockSize(m_scaledSize);
 #endif
@@ -63,14 +63,14 @@ void CaptureWinGL::resizeEvent(QResizeEvent *event)
 void CaptureWinGL::setRenderFrame()
 {
 #ifdef HAVE_QTGL
-       m_videoSurface.setFrame(m_frameInfo.frameWidth, m_frameInfo.frameHeight,
-                               m_cropInfo.cropW, m_cropInfo.cropH,
-                               m_frameInfo.format,
-                               m_frameInfo.planeData[0],
-                               m_frameInfo.planeData[1]);
+       m_videoSurface.setFrame(m_frame.size.width(), m_frame.size.height(),
+                               m_crop.delta.width(), m_crop.delta.height(),
+                               m_frame.format,
+                               m_frame.planeData[0],
+                               m_frame.planeData[1]);
 #endif
-       m_frameInfo.updated = false;
-       m_cropInfo.updated = false;
+       m_frame.updated = false;
+       m_crop.updated = false;
 }
 
 bool CaptureWinGL::hasNativeFormat(__u32 format)
index 54de552..ce1d8c6 100644 (file)
 #include "capture-win-qt.h"
 
 CaptureWinQt::CaptureWinQt() :
-       m_frame(new QImage(0, 0, QImage::Format_Invalid)),
+       m_image(new QImage(0, 0, QImage::Format_Invalid)),
        m_data(NULL),
        m_supportedFormat(false),
-       m_filled(false)
+       m_filled(false),
+       m_cropBytes(0),
+       m_cropOffset(0)
 {
        CaptureWin::buildWindow(&m_videoSurface);
 }
 
 CaptureWinQt::~CaptureWinQt()
 {
-       delete m_frame;
+       delete m_image;
 }
 
 void CaptureWinQt::resizeEvent(QResizeEvent *event)
 {
-       m_curWinWidth  = m_videoSurface.width();
-       m_curWinHeight = m_videoSurface.height();
-       m_frameInfo.updated = true;
-       CaptureWin::resizeScaleCrop();
+       m_windowSize.setWidth(m_videoSurface.width());
+       m_windowSize.setHeight(m_videoSurface.height());
+       m_frame.updated = true;
+       CaptureWin::updateSize();
        paintFrame();
        event->accept();
 }
@@ -46,51 +48,56 @@ void CaptureWinQt::resizeEvent(QResizeEvent *event)
 void CaptureWinQt::setRenderFrame()
 {
        // Get/copy (TODO: use direct?)
-       m_data = m_frameInfo.planeData[0];
+       m_data = m_frame.planeData[0];
 
        QImage::Format dstFmt;
-       m_supportedFormat = findNativeFormat(m_frameInfo.format, dstFmt);
+       m_supportedFormat = findNativeFormat(m_frame.format, dstFmt);
        if (!m_supportedFormat)
                dstFmt = QImage::Format_RGB888;
 
-       if (m_frameInfo.updated || m_frame->format() != dstFmt) {
-               delete m_frame;
-               m_frame = new QImage(m_frameInfo.frameWidth, m_frameInfo.frameHeight, dstFmt);
+       if (m_frame.updated || m_image->format() != dstFmt) {
+               delete m_image;
+               m_image = new QImage(m_frame.size.width(),
+                                    m_frame.size.height(),
+                                    dstFmt);
        }
 
-       m_frameInfo.updated = false;
+       m_frame.updated = false;
 
        paintFrame();
 }
 
 void CaptureWinQt::paintFrame()
 {
-       if (m_cropInfo.updated) {
-               m_cropInfo.offset = m_cropInfo.cropH * (m_frame->depth() / 8)
-                       * m_frameInfo.frameWidth + m_cropInfo.cropW * (m_frame->depth() / 8);
-
-               // Even though the values above can be valid, it might be that there is no
-               // data at all. This makes sure that it is.
-               m_cropInfo.bytes = m_cropInfo.height * m_cropInfo.width
-                       * (m_frame->depth() / 8);
-               m_cropInfo.updated = false;
+       if (m_crop.updated) {
+               m_cropOffset = m_crop.delta.height() * (m_image->depth() / 8)
+                       * m_frame.size.width()
+                       + m_crop.delta.width() * (m_image->depth() / 8);
+
+               // Even though the values above can be valid, it might be that
+               // there is no data at all. This makes sure that it is.
+               m_cropBytes =  m_crop.size.width() * m_crop.size.height()
+                       * (m_image->depth() / 8);
+               m_crop.updated = false;
        }
 
-       if (!m_supportedFormat || !m_cropInfo.bytes) {
+       if (!m_supportedFormat || !m_cropBytes) {
                if (!m_filled) {
                        m_filled = true;
-                       m_frame->fill(0);
-                       QPixmap img = QPixmap::fromImage(*m_frame);
+                       m_image->fill(0);
+                       QPixmap img = QPixmap::fromImage(*m_image);
                        m_videoSurface.setPixmap(img);
                }
                return;
        }
        m_filled = false;
 
-       unsigned char *data = (m_data == NULL) ? m_frame->bits() : m_data;
+       unsigned char *data = (m_data == NULL) ? m_image->bits() : m_data;
 
-       QImage displayFrame(&data[m_cropInfo.offset], m_cropInfo.width, m_cropInfo.height,
-                           m_frame->width() * (m_frame->depth() / 8), m_frame->format());
+       QImage displayFrame(&data[m_cropOffset],
+                           m_crop.size.width(), m_crop.size.height(),
+                           m_image->width() * (m_image->depth() / 8),
+                           m_image->format());
 
        QPixmap img = QPixmap::fromImage(displayFrame);
 
@@ -103,7 +110,7 @@ void CaptureWinQt::paintFrame()
 void CaptureWinQt::stop()
 {
        if (m_data != NULL)
-               memcpy(m_frame->bits(), m_data, m_frame->numBytes());
+               memcpy(m_image->bits(), m_data, m_image->numBytes());
        m_data = NULL;
 }
 
index f4c4cb2..5076e48 100644 (file)
@@ -49,10 +49,12 @@ private:
        bool findNativeFormat(__u32 format, QImage::Format &dstFmt);
        void paintFrame();
 
-       QImage *m_frame;
+       QImage *m_image;
        unsigned char *m_data;
        QLabel m_videoSurface;
        bool m_supportedFormat;
        bool m_filled;
+       int m_cropBytes;
+       int m_cropOffset;
 };
 #endif
index be9a3d2..8139426 100644 (file)
@@ -33,31 +33,29 @@ bool CaptureWin::m_enableScaling = true;
 double CaptureWin::m_pixelAspectRatio = 1.0;
 CropMethod CaptureWin::m_cropMethod = QV4L2_CROP_NONE;
 
-CaptureWin::CaptureWin() :
-       m_sourceWinWidth(-1),
-       m_sourceWinHeight(-1),
-       m_curWinWidth(-1),
-       m_curWinHeight(-1)
+CaptureWin::CaptureWin()
 {
        setWindowTitle("V4L2 Capture");
        m_hotkeyClose = new QShortcut(Qt::CTRL+Qt::Key_W, this);
        connect(m_hotkeyClose, SIGNAL(activated()), this, SLOT(close()));
        m_hotkeyScaleReset = new QShortcut(Qt::CTRL+Qt::Key_F, this);
        connect(m_hotkeyScaleReset, SIGNAL(activated()), this, SLOT(resetSize()));
-       m_frameInfo.format      =  0;
-       m_frameInfo.frameHeight =  0;
-       m_frameInfo.frameWidth  =  0;
-       m_frameInfo.planeData[0] = NULL;
-       m_frameInfo.planeData[1] = NULL;
+       m_frame.format      =  0;
+       m_frame.size.setWidth(0);
+       m_frame.size.setHeight(0);
+       m_frame.planeData[0] = NULL;
+       m_frame.planeData[1] = NULL;
+       m_crop.delta.setWidth(0);
+       m_crop.delta.setHeight(0);
+       m_crop.size.setWidth(0);
+       m_crop.size.setHeight(0);
+       m_crop.updated = 0;
        m_scaledSize.setWidth(0);
        m_scaledSize.setHeight(0);
-       m_cropInfo.cropH  = 0;
-       m_cropInfo.cropW  = 0;
-       m_cropInfo.height = 0;
-       m_cropInfo.width  = 0;
-       m_cropInfo.offset = 0;
-       m_cropInfo.bytes  = 0;
-       m_cropInfo.updated = 0;
+       m_origFrameSize.setWidth(0);
+       m_origFrameSize.setHeight(0);
+       m_windowSize.setWidth(0);
+       m_windowSize.setHeight(0);
 }
 
 CaptureWin::~CaptureWin()
@@ -74,20 +72,20 @@ CaptureWin::~CaptureWin()
 void CaptureWin::setFrame(int width, int height, __u32 format,
                          unsigned char *data, unsigned char *data2, const QString &info)
 {
-       m_frameInfo.planeData[0] = data;
-       m_frameInfo.planeData[1] = data2;
-       m_frameInfo.info         = info;
-
-       m_frameInfo.updated = false;
-       if (width != m_frameInfo.frameWidth || height != m_frameInfo.frameHeight
-           || format != m_frameInfo.format) {
-               m_frameInfo.frameHeight  = height;
-               m_frameInfo.frameWidth   = width;
-               m_frameInfo.format       = format;
-               m_frameInfo.updated      = true;
-               resizeScaleCrop();
+       m_frame.planeData[0] = data;
+       m_frame.planeData[1] = data2;
+       m_frame.info         = info;
+
+       m_frame.updated = false;
+       if (width != m_frame.size.width() || height != m_frame.size.height()
+           || format != m_frame.format) {
+               m_frame.size.setHeight(height);
+               m_frame.size.setWidth(width);
+               m_frame.format       = format;
+               m_frame.updated      = true;
+               updateSize();
        }
-       m_information.setText(m_frameInfo.info);
+       m_information.setText(m_frame.info);
 
        setRenderFrame();
 }
@@ -109,10 +107,10 @@ void CaptureWin::resetSize()
                showNormal();
 
         // Force resize even if no size change
-       int w = m_sourceWinWidth;
-       int h = m_sourceWinHeight;
-       m_sourceWinWidth = -1;
-       m_sourceWinHeight = -1;
+       int w = m_origFrameSize.width();
+       int h = m_origFrameSize.height();
+       m_origFrameSize.setWidth(0);
+       m_origFrameSize.setHeight(0);
        resize(w, h);
 }
 
@@ -160,17 +158,16 @@ int CaptureWin::cropWidth(int width, int height)
        return (width - validWidth) / 2;
 }
 
-void CaptureWin::resizeScaleCrop()
+void CaptureWin::updateSize()
 {
-       m_cropInfo.updated = 0;
-       if (m_frameInfo.updated) {
-               m_scaledSize = scaleFrameSize(QSize(m_curWinWidth, m_curWinHeight),
-                                             QSize(m_frameInfo.frameWidth, m_frameInfo.frameHeight));
-               m_cropInfo.cropH  = cropHeight(m_frameInfo.frameWidth, m_frameInfo.frameHeight);
-               m_cropInfo.cropW  = cropWidth(m_frameInfo.frameWidth, m_frameInfo.frameHeight);
-               m_cropInfo.height = m_frameInfo.frameHeight - (m_cropInfo.cropH * 2);
-               m_cropInfo.width  = m_frameInfo.frameWidth - (m_cropInfo.cropW * 2);
-               m_cropInfo.updated = 1;
+       m_crop.updated = 0;
+       if (m_frame.updated) {
+               m_scaledSize = scaleFrameSize(m_windowSize, m_frame.size);
+               m_crop.delta.setHeight(cropHeight(m_frame.size.width(), m_frame.size.height()));
+               m_crop.delta.setWidth(cropWidth(m_frame.size.width(), m_frame.size.height()));
+               m_crop.size.setHeight(m_frame.size.height() - (m_crop.delta.height() * 2));
+               m_crop.size.setWidth(m_frame.size.width() - (m_crop.delta.width() * 2));
+               m_crop.updated = 1;
        }
 }
 
@@ -208,8 +205,8 @@ void CaptureWin::enableScaling(bool enable)
 {
        if (!enable) {
                QSize margins = getMargins();
-               QWidget::setMinimumSize(m_sourceWinWidth + margins.width(),
-                                       m_sourceWinHeight + margins.height());
+               QWidget::setMinimumSize(m_origFrameSize.width() + margins.width(),
+                                       m_origFrameSize.height() + margins.height());
        } else {
                QWidget::setMinimumSize(MIN_WIN_SIZE_WIDTH, MIN_WIN_SIZE_HEIGHT);
        }
@@ -224,11 +221,11 @@ void CaptureWin::resize(int width, int height)
 
        // Dont resize window if the frame size is the same in
        // the event the window has been paused when beeing resized.
-       if (width == m_sourceWinWidth && height == m_sourceWinHeight)
+       if (width == m_origFrameSize.width() && height == m_origFrameSize.height())
                return;
 
-       m_sourceWinWidth = width;
-       m_sourceWinHeight = height;
+       m_origFrameSize.setWidth(width);
+       m_origFrameSize.setHeight(height);
 
        QSize margins = getMargins();
        h = margins.height() - cropHeight(width, height) * 2 + actualFrameHeight(height);
index 1edeae7..a7a136b 100644 (file)
@@ -39,25 +39,21 @@ enum CropMethod {
        QV4L2_CROP_P43,
 };
 
-struct cropInfo {
-       int cropH;
-       int cropW;
-       int height;
-       int width;
-       int offset;
-       int bytes;
-       bool updated;
-};
-
-struct frameInfo {
+struct frame {
        __u32 format;
-       int   frameHeight;
-       int   frameWidth;
+       QSize size;        // int   frameHeight; int   frameWidth;
        unsigned char *planeData[2];
        QString info;
        bool updated;
 };
 
+struct crop {              // cropInfo
+       QSize delta;       // int cropH; int cropW;
+       QSize size;        // int height; int width;
+       bool updated;
+};
+
+
 class CaptureWin : public QWidget
 {
        Q_OBJECT
@@ -186,7 +182,7 @@ protected:
         * @brief Calculate source size after pixel aspect scaling and cropping
         *
         */
-       void resizeScaleCrop();
+       void updateSize();
 
        /**
         * @brief A label that can is used to display capture information.
@@ -200,12 +196,10 @@ protected:
         *
         * @note Set and accessed from derived render dependent classes.
         */
-       struct frameInfo m_frameInfo;
-       struct cropInfo  m_cropInfo;    // TODO: Temporary, consolidate with m_framInfo
-       int m_sourceWinWidth;
-       int m_sourceWinHeight;
-       int m_curWinWidth;
-       int m_curWinHeight;
+       struct frame m_frame;
+       struct crop  m_crop;
+       QSize m_origFrameSize;  // int m_sourceWinWidth; int m_sourceWinHeight;
+       QSize m_windowSize;     // int m_curWinWidth; int m_curWinHeight;
        QSize m_scaledSize;
 
        /**