From 2500e619fc42a7ef3ff58dcf0f5c451d320cd5c1 Mon Sep 17 00:00:00 2001 From: Ove Brynestad Date: Thu, 10 Jul 2014 14:40:53 +0200 Subject: [PATCH] qv4l2: refactor aspect size calculation Improve readability by using QSize members and operators for size calculations Signed-off-by: Ove Brynestad Signed-off-by: Hans Verkuil --- utils/qv4l2/capture-win-gl.cpp | 5 ++--- utils/qv4l2/capture-win-qt.cpp | 6 ++++-- utils/qv4l2/capture-win.cpp | 47 +++++++++++++++++++++--------------------- utils/qv4l2/capture-win.h | 16 ++++---------- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/utils/qv4l2/capture-win-gl.cpp b/utils/qv4l2/capture-win-gl.cpp index 31ae239..135de12 100644 --- a/utils/qv4l2/capture-win-gl.cpp +++ b/utils/qv4l2/capture-win-gl.cpp @@ -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 diff --git a/utils/qv4l2/capture-win-qt.cpp b/utils/qv4l2/capture-win-qt.cpp index ce1d8c6..d2f80c9 100644 --- a/utils/qv4l2/capture-win-qt.cpp +++ b/utils/qv4l2/capture-win-qt.cpp @@ -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(); } diff --git a/utils/qv4l2/capture-win.cpp b/utils/qv4l2/capture-win.cpp index 8139426..89575d4 100644 --- a/utils/qv4l2/capture-win.cpp +++ b/utils/qv4l2/capture-win.cpp @@ -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) diff --git a/utils/qv4l2/capture-win.h b/utils/qv4l2/capture-win.h index a7a136b..ccd0196 100644 --- a/utils/qv4l2/capture-win.h +++ b/utils/qv4l2/capture-win.h @@ -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(); -- 2.7.4