Prison

videoscannerframe.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: MIT
4*/
5
6#include "videoscannerframe_p.h"
7
8#include <cstring>
9
10using namespace Prison;
11
12VideoScannerFrame::VideoScannerFrame() = default;
13
14VideoScannerFrame::VideoScannerFrame(const QVideoFrame &frame, bool isVerticallyFlipped, Format::BarcodeFormats formats)
15 : m_frame(frame)
16 , m_formats(formats)
17 , m_verticallyFlipped(isVerticallyFlipped)
18{
19}
20
21VideoScannerFrame::~VideoScannerFrame() = default;
22
23int VideoScannerFrame::width() const
24{
25 return m_frame.width();
26}
27
28int VideoScannerFrame::height() const
29{
30 return m_frame.height();
31}
32
33int VideoScannerFrame::bytesPerLine() const
34{
35 return m_frame.bytesPerLine(0);
36}
37
38QVideoFrameFormat::PixelFormat VideoScannerFrame::pixelFormat() const
39{
40 return m_frame.pixelFormat();
41}
42
43void VideoScannerFrame::map()
44{
45 if (!m_frameData && m_image.isNull()) {
46 m_frame.map(QVideoFrame::ReadOnly);
47 }
48}
49
50void VideoScannerFrame::unmap()
51{
52 if (m_frame.isMapped()) {
53 m_frame.unmap();
54 }
55}
56
57const uint8_t *VideoScannerFrame::bits() const
58{
59 if (m_frameData) {
60 return m_frameData;
61 }
62 if (!m_image.isNull()) {
63 return m_image.bits();
64 }
65
66 Q_ASSERT(m_frame.isMapped());
67 return m_frame.bits(0);
68}
69
70bool VideoScannerFrame::copyRequired() const
71{
72 return m_frame.handleType() == QVideoFrame::RhiTextureHandle;
73}
74
75void VideoScannerFrame::copyFrameData(QByteArray &buffer)
76{
77 Q_ASSERT(m_frame.isMapped());
78
79 const auto size = frameDataSize();
80 if (buffer.size() != size) {
81 buffer.resize(size);
82 }
83 std::memcpy(buffer.data(), m_frame.bits(0), size);
84 m_frameData = reinterpret_cast<const uint8_t *>(buffer.constData());
85}
86
87int VideoScannerFrame::frameDataSize() const
88{
89 Q_ASSERT(m_frame.isMapped());
90
91 switch (m_frame.pixelFormat()) {
92 case QVideoFrameFormat::Format_YUV420P:
93 case QVideoFrameFormat::Format_YUV422P:
94 case QVideoFrameFormat::Format_YV12:
95 case QVideoFrameFormat::Format_NV12:
96 case QVideoFrameFormat::Format_NV21:
97 case QVideoFrameFormat::Format_IMC1:
98 case QVideoFrameFormat::Format_IMC2:
99 case QVideoFrameFormat::Format_IMC3:
100 case QVideoFrameFormat::Format_IMC4:
101 return m_frame.mappedBytes(0) / 2;
102 default:
103 return m_frame.mappedBytes(0);
104 }
105}
106
107bool VideoScannerFrame::needsConversion() const
108{
109 switch (m_frame.pixelFormat()) {
110 case QVideoFrameFormat::Format_Jpeg:
111 case QVideoFrameFormat::Format_SamplerExternalOES:
112 case QVideoFrameFormat::Format_SamplerRect:
113 return true;
114 default:
115 return false;
116 }
117}
118
119void VideoScannerFrame::convertToImage()
120{
121 if (!m_image.isNull()) {
122 return;
123 }
124
125 Q_ASSERT(m_frame.isMapped());
126 m_image = m_frame.toImage();
127}
128
129QImage VideoScannerFrame::image() const
130{
131 return m_image;
132}
133
134bool VideoScannerFrame::isVerticallyFlipped() const
135{
136 return m_verticallyFlipped;
137}
138
139Format::BarcodeFormats VideoScannerFrame::formats() const
140{
141 return m_formats;
142}
Provides classes and methods for generating barcodes.
Definition barcode.h:24
const char * constData() const const
char * data()
void resize(qsizetype newSize, char c)
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.