qv4l2: refactor aspect size calculation
authorOve Brynestad <ovebryne@cisco.com>
Thu, 10 Jul 2014 12:40:53 +0000 (14:40 +0200)
committerHans Verkuil <hans.verkuil@cisco.com>
Fri, 11 Jul 2014 13:32:03 +0000 (15:32 +0200)
Improve readability by using QSize members and operators for size calculations

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.cpp
utils/qv4l2/capture-win.h

index 31ae239..135de12 100644 (file)
@@ -49,9 +49,8 @@ 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_windowSize.setWidth(width() - margins.width());
-       m_windowSize.setHeight(height() - margins.height());
-       // Re-calculate
+       m_windowSize = size() - margins;
+       // Re-calculate sizes
        m_frame.updated = true;
        CaptureWin::updateSize();
        // Lock viewport size to follow calculated size
index ce1d8c6..d2f80c9 100644 (file)
@@ -37,10 +37,12 @@ CaptureWinQt::~CaptureWinQt()
 
 void CaptureWinQt::resizeEvent(QResizeEvent *event)
 {
-       m_windowSize.setWidth(m_videoSurface.width());
-       m_windowSize.setHeight(m_videoSurface.height());
+       // Get size of frame viewport.
+       m_windowSize = m_videoSurface.size();
+       // Re-calculate sizes
        m_frame.updated = true;
        CaptureWin::updateSize();
+       // Draw
        paintFrame();
        event->accept();
 }
index 8139426..89575d4 100644 (file)
@@ -178,20 +178,18 @@ void CaptureWin::setCropMethod(CropMethod crop)
        QCoreApplication::sendEvent(this, &event);
 }
 
-int CaptureWin::actualFrameWidth(int width)
+QSize CaptureWin::pixelAspectFrameSize(QSize size)
 {
-       if (m_enableScaling && m_pixelAspectRatio > 1)
-               return width * m_pixelAspectRatio;
+       if (!m_enableScaling)
+               return size;
 
-       return width;
-}
+       if (m_pixelAspectRatio > 1)
+               size.rwidth() *= m_pixelAspectRatio;
 
-int CaptureWin::actualFrameHeight(int height)
-{
-       if (m_enableScaling && m_pixelAspectRatio < 1)
-               return height / m_pixelAspectRatio;
+       if (m_pixelAspectRatio < 1)
+               size.rheight() /= m_pixelAspectRatio;
 
-       return height;
+       return size;
 }
 
 QSize CaptureWin::getMargins()
@@ -228,8 +226,10 @@ void CaptureWin::resize(int width, int height)
        m_origFrameSize.setHeight(height);
 
        QSize margins = getMargins();
-       h = margins.height() - cropHeight(width, height) * 2 + actualFrameHeight(height);
-       w = margins.width() - cropWidth(width, height) * 2 + actualFrameWidth(width);
+       QSize aspectedFrameSize = pixelAspectFrameSize(QSize(width, height));
+
+       h = margins.height() - cropHeight(width, height) * 2 + aspectedFrameSize.height();
+       w = margins.width() - cropWidth(width, height) * 2 + aspectedFrameSize.width();
        height = h;
        width = w;
 
@@ -252,25 +252,26 @@ void CaptureWin::resize(int width, int height)
 
 QSize CaptureWin::scaleFrameSize(QSize window, QSize frame)
 {
-       int actualWidth = actualFrameWidth(frame.width() - cropWidth(frame.width(), frame.height()) * 2);
-       int actualHeight = actualFrameHeight(frame.height() - cropHeight(frame.width(), frame.height()) * 2);
+       QSize croppedSize = frame - QSize((cropWidth(frame.width(), frame.height()) * 2),
+                                         (cropHeight(frame.width(), frame.height()) * 2));
+       QSize actualSize = pixelAspectFrameSize(croppedSize);
 
        if (!m_enableScaling) {
-               window.setWidth(actualWidth);
-               window.setHeight(actualHeight);
+               window.setWidth(actualSize.width());
+               window.setHeight(actualSize.height());
        }
 
-       double newW, newH;
+       qreal newW, newH;
        if (window.width() >= window.height()) {
-               newW = (double)window.width() / actualWidth;
-               newH = (double)window.height() / actualHeight;
+               newW = (qreal)window.width() / actualSize.width();
+               newH = (qreal)window.height() / actualSize.height();
        } else {
-               newH = (double)window.width() / actualWidth;
-               newW = (double)window.height() / actualHeight;
+               newH = (qreal)window.width() / actualSize.width();
+               newW = (qreal)window.height() / actualSize.height();
        }
-       double resized = std::min(newW, newH);
+       qreal resized = (qreal)std::min(newW, newH);
 
-       return QSize((int)(actualWidth * resized), (int)(actualHeight * resized));
+       return (actualSize * resized);
 }
 
 void CaptureWin::setPixelAspectRatio(double ratio)
index a7a136b..ccd0196 100644 (file)
@@ -139,20 +139,12 @@ public:
        static int cropWidth(int width, int height);
 
        /**
-        * @brief Get the frame width when aspect ratio is applied if ratio > 1.
+        * @brief Get the frame size when aspect ratio is applied and increases size.
         *
-        * @param width The original frame width.
-        * @return The width with aspect ratio correction (scaling must be enabled).
+        * @param size The original frame size.
+        * @return The new size with aspect ratio correction (scaling must be enabled).
         */
-       static int actualFrameWidth(int width);
-
-       /**
-        * @brief Get the frame height when aspect ratio is applied if ratio < 1.
-        *
-        * @param width The original frame width.
-        * @return The width with aspect ratio correction (scaling must be enabled).
-        */
-       static int actualFrameHeight(int height);
+       static QSize pixelAspectFrameSize(QSize size);
 
 public slots:
        void resetSize();