utils.cpp 3.41 KB
Newer Older
Gus Grubba's avatar
Gus Grubba 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 44 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
/*
    Copyright (C) 2011-2013 Collabora Ltd. <info@collabora.com>
    Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
    Copyright (C) 2013 basysKom GmbH <info@basyskom.com>

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License version 2.1
    as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file
 *   @brief Extracted from QtGstreamer to avoid overly complex dependency
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include "utils.h"

void PaintAreas::calculate(const QRectF & targetArea,
        const QSize & videoSize,
        const Fraction & pixelAspectRatio,
        const Fraction & displayAspectRatio,
        Qt::AspectRatioMode aspectRatioMode)
{
    this->targetArea = targetArea;

    switch (aspectRatioMode) {
    case Qt::IgnoreAspectRatio:
        videoArea = targetArea;
        sourceRect = QRectF(0, 0, 1, 1);
        blackArea1 = blackArea2 = QRectF();
        break;
    default:
      {
        qreal aspectRatio = pixelAspectRatio.ratio() * displayAspectRatio.invRatio();

        QSizeF videoSizeAdjusted = QSizeF(videoSize.width() * aspectRatio, videoSize.height());
        videoSizeAdjusted.scale(targetArea.size(), aspectRatioMode);

        // the area that the original video occupies, scaled
        QRectF videoRect = QRectF(QPointF(), videoSizeAdjusted);
        videoRect.moveCenter(targetArea.center());

        if (aspectRatioMode == Qt::KeepAspectRatio) {
          videoArea = videoRect;
          sourceRect = QRectF(0, 0, 1, 1);
        } else { // Qt::KeepAspectRatioByExpanding
          videoArea = targetArea;
          sourceRect = QRectF(
              (videoArea.left() - videoRect.left()) / videoRect.width(),
              (videoArea.top() - videoRect.top()) / videoRect.height(),
              videoArea.width() / videoRect.width(),
              videoArea.height() / videoRect.height());
        }
        break;
      }
    }

    if (aspectRatioMode == Qt::IgnoreAspectRatio
        || aspectRatioMode == Qt::KeepAspectRatioByExpanding
        || videoArea == targetArea) {
        blackArea1 = blackArea2 = QRectF();
    } else {
        blackArea1 = QRectF(
            targetArea.left(),
            targetArea.top(),
            videoArea.left() == targetArea.left() ?
                targetArea.width() : videoArea.left() - targetArea.left(),
            videoArea.top() == targetArea.top() ?
                targetArea.height() : videoArea.top() - targetArea.top()
        );

        blackArea2 = QRectF(
            videoArea.right() == targetArea.right() ?
                targetArea.left() : videoArea.right(),
            videoArea.bottom() == targetArea.bottom() ?
                targetArea.top() : videoArea.bottom(),
            videoArea.right() == targetArea.right() ?
                targetArea.width() : targetArea.right() - videoArea.right(),
            videoArea.bottom() == targetArea.bottom() ?
                targetArea.height() : targetArea.bottom() - videoArea.bottom()
        );
    }
}