summaryrefslogtreecommitdiffstats
path: root/src/multimedia/video/qimagevideobuffer.cpp
blob: bc825004e12f320211423557a218eb4a10c3f6cb (plain)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qimagevideobuffer_p.h"
#include "qvideoframeformat.h"

QT_BEGIN_NAMESPACE

namespace {

QImage::Format fixImageFormat(QImage::Format format)
{
    switch (format) {
    case QImage::Format_ARGB32_Premultiplied:
    case QImage::Format_ARGB8565_Premultiplied:
    case QImage::Format_ARGB6666_Premultiplied:
    case QImage::Format_ARGB8555_Premultiplied:
    case QImage::Format_ARGB4444_Premultiplied:
    case QImage::Format_RGBA8888_Premultiplied:
    case QImage::Format_A2BGR30_Premultiplied:
    case QImage::Format_A2RGB30_Premultiplied:
    case QImage::Format_RGBA64_Premultiplied:
    case QImage::Format_RGBA16FPx4_Premultiplied:
    case QImage::Format_RGBA32FPx4_Premultiplied:
        return QImage::Format_ARGB32_Premultiplied;
    case QImage::Format_ARGB32:
    case QImage::Format_RGBA8888:
    case QImage::Format_Alpha8:
    case QImage::Format_RGBA64:
    case QImage::Format_RGBA16FPx4:
    case QImage::Format_RGBA32FPx4:
        return QImage::Format_ARGB32;
    case QImage::Format_Invalid:
        return QImage::Format_Invalid;
    default:
        return QImage::Format_RGB32;
    }
}

QImage fixImage(QImage image)
{
    if (image.format() == QImage::Format_Invalid)
        return image;

    const auto frameFormat = QVideoFrameFormat::pixelFormatFromImageFormat(image.format());
    if (frameFormat != QVideoFrameFormat::Format_Invalid)
        return image;

    return image.convertToFormat(fixImageFormat(image.format()));
}

} // namespace

QImageVideoBuffer::QImageVideoBuffer(QImage image)
    : QAbstractVideoBuffer(QVideoFrame::NoHandle), m_image(fixImage(std::move(image)))
{
}

QVideoFrame::MapMode QImageVideoBuffer::mapMode() const
{
    return m_mapMode;
}

QAbstractVideoBuffer::MapData QImageVideoBuffer::map(QVideoFrame::MapMode mode)
{
    MapData mapData;
    if (m_mapMode == QVideoFrame::NotMapped && !m_image.isNull()
        && mode != QVideoFrame::NotMapped) {
        m_mapMode = mode;

        mapData.nPlanes = 1;
        mapData.bytesPerLine[0] = m_image.bytesPerLine();
        mapData.data[0] = m_image.bits();
        mapData.size[0] = m_image.sizeInBytes();
    }

    return mapData;
}

void QImageVideoBuffer::unmap()
{
    m_mapMode = QVideoFrame::NotMapped;
}

QImage QImageVideoBuffer::underlyingImage() const
{
    return m_image;
}

QT_END_NAMESPACE