Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtexturefiledata.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "QtGui/qimage.h"
6#include <QtCore/qsize.h>
7#include <QtCore/qvarlengtharray.h>
8#include <QtCore/qmap.h>
9
11
12Q_LOGGING_CATEGORY(lcQtGuiTextureIO, "qt.gui.textureio");
13
14constexpr size_t MAX_FACES = 6;
15
17{
18public:
22
38
42
43 void ensureSize(int levels, int faces, bool force = false)
44 {
45 numLevels = force ? levels : qMax(numLevels, levels);
46 numFaces = force ? faces : qMax(numFaces, faces);
48 offsets.resize(numFaces);
49 lengths.resize(numFaces);
50
51 for (auto faceList : { &offsets, &lengths })
52 for (auto &levelList : *faceList)
53 levelList.resize(numLevels);
54 } else {
56 for (auto &levelList : images)
57 levelList.resize(numLevels);
58 }
59 }
60
61 bool isValid(int level, int face) const { return level < numLevels && face < numFaces; }
62
63 int getOffset(int level, int face) const { return offsets[face][level]; }
64 void setOffset(int value, int level, int face) { offsets[face][level] = value; }
65 int getLength(int level, int face) const { return lengths[face][level]; }
66 void setLength(int value, int level, int face) { lengths[face][level] = value; }
67
71 QVarLengthArray<QList<int>, MAX_FACES> offsets; // [Face][Level] = offset
72 QVarLengthArray<QList<int>, MAX_FACES> lengths; // [Face][Level] = length
73 QVarLengthArray<QList<QImage>, MAX_FACES> images; // [Face][Level] = length
78 int numFaces = 0;
79 int numLevels = 0;
80 QMap<QByteArray, QByteArray> keyValues;
81};
82
88
93
95{
96 d = other.d;
97 return *this;
98}
99
103
105{
106 return !d;
107}
108
110{
111 if (!d)
112 return false;
113
114 if (d->mode == ImageMode)
115 return true; // Manually populated: the caller needs to do verification at that time.
116
117 if (d->data.isEmpty() || d->size.isEmpty() || (!d->format && !d->internalFormat))
118 return false;
119
120 const int numFacesOffset = d->offsets.size();
121 const int numFacesLength = d->lengths.size();
122 if (numFacesOffset == 0 || numFacesLength == 0 || d->numFaces != numFacesOffset
123 || d->numFaces != numFacesLength)
124 return false;
125
126 const qint64 dataSize = d->data.size();
127
128 // Go through all faces and levels and check that the range is inside the data size.
129 for (int face = 0; face < d->numFaces; face++) {
130 const int numLevelsOffset = d->offsets.at(face).size();
131 const int numLevelsLength = d->lengths.at(face).size();
132 if (numLevelsOffset == 0 || numLevelsLength == 0 || d->numLevels != numLevelsOffset
133 || d->numLevels != numLevelsLength)
134 return false;
135
136 for (int level = 0; level < d->numLevels; level++) {
137 const qint64 offset = d->getOffset(level, face);
138 const qint64 length = d->getLength(level, face);
139 if (offset < 0 || offset >= dataSize || length <= 0 || (offset + length > dataSize))
140 return false;
141 }
142 }
143 return true;
144}
145
147{
148 d = nullptr;
149}
150
152{
153 return d ? d->data : QByteArray();
154}
155
157{
159 d->data = data;
160}
161
163{
164 Q_ASSERT(d->mode == ImageMode);
165 d->ensureSize(level + 1, face + 1);
166 d->images[face][level] = image;
167}
168
170{
172 return (d && d->isValid(level, face)) ? d->getOffset(level, face) : 0;
173}
174
176{
178 if (d.constData() && level >= 0) {
179 d->ensureSize(level + 1, face + 1);
181 }
182}
183
185{
187 return (d && d->isValid(level, face)) ? d->getLength(level, face) : 0;
188}
189
191{
192 if (d->mode == ByteArrayMode) {
193 const int dataLength = this->dataLength(level, face);
194 const int dataOffset = this->dataOffset(level, face);
195
196 if (d == nullptr || dataLength == 0)
197 return QByteArrayView();
198
200 } else {
201 if (!d->isValid(level, face))
202 return QByteArrayView();
203 const QImage &img = d->images[face][level];
204 return img.isNull() ? QByteArrayView() : QByteArrayView(img.constBits(), img.sizeInBytes());
205 }
206}
207
209{
211 if (d.constData() && level >= 0) {
212 d->ensureSize(level + 1, face + 1);
214 }
215}
216
218{
219 return d ? d->numLevels : 0;
220}
221
223{
224 if (d && numLevels >= 0)
225 d->ensureSize(numLevels, d->numFaces, true);
226}
227
229{
230 return d ? d->numFaces : 0;
231}
232
234{
235 if (d && numFaces >= 0)
236 d->ensureSize(d->numLevels, numFaces, true);
237}
238
240{
241 return d ? d->size : QSize();
242}
243
245{
246 if (d.constData())
247 d->size = size;
248}
249
251{
252 return d ? d->format : 0;
253}
254
256{
257 if (d.constData())
258 d->format = format;
259}
260
262{
263 return d ? d->internalFormat : 0;
264}
265
271
276
282
284{
285 return d ? d->logName : QByteArray();
286}
287
289{
290 if (d.constData())
291 d->logName = name;
292}
293
294QMap<QByteArray, QByteArray> QTextureFileData::keyValueMetadata() const
295{
296 return d ? d->keyValues : QMap<QByteArray, QByteArray>();
297}
298
299void QTextureFileData::setKeyValueMetadata(const QMap<QByteArray, QByteArray> &keyValues)
300{
301 if (d)
302 d->keyValues = keyValues;
303}
304
306{
307 return QByteArray("0x" + QByteArray::number(fmt, 16).rightJustified(4, '0'));
308}
309
311{
312 QDebugStateSaver saver(dbg);
313
314 dbg.nospace() << "QTextureFileData(";
315 if (!d.isNull()) {
316 dbg.space() << d.logName() << d.size();
317 dbg << "glFormat:" << glFormatName(d.glFormat());
318 dbg << "glInternalFormat:" << glFormatName(d.glInternalFormat());
319 dbg << "glBaseInternalFormat:" << glFormatName(d.glBaseInternalFormat());
320 dbg.nospace() << "Levels: " << d.numLevels();
321 dbg.nospace() << "Faces: " << d.numFaces();
322 if (!d.isValid())
323 dbg << " {Invalid}";
324 dbg << ")";
325 dbg << (d.d->mode ? "[bytearray-based]" : "[image-based]");
326 } else {
327 dbg << "null)";
328 }
329
330 return dbg;
331}
332
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
\inmodule QtCore
\inmodule QtCore
\inmodule QtGui
Definition qimage.h:37
qsizetype size() const noexcept
Definition qlist.h:397
const T * constData() const noexcept
Returns a const pointer to the shared data object.
Definition qshareddata.h:51
\inmodule QtCore
Definition qshareddata.h:19
\inmodule QtCore
Definition qsize.h:25
constexpr bool isEmpty() const noexcept
Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
Definition qsize.h:124
void setLength(int value, int level, int face)
QTextureFileDataPrivate(const QTextureFileDataPrivate &other)
bool isValid(int level, int face) const
QMap< QByteArray, QByteArray > keyValues
QVarLengthArray< QList< QImage >, MAX_FACES > images
QVarLengthArray< QList< int >, MAX_FACES > lengths
QTextureFileData::Mode mode
int getLength(int level, int face) const
QVarLengthArray< QList< int >, MAX_FACES > offsets
void ensureSize(int levels, int faces, bool force=false)
void setOffset(int value, int level, int face)
int getOffset(int level, int face) const
void setKeyValueMetadata(const QMap< QByteArray, QByteArray > &keyValues)
int dataLength(int level=0, int face=0) const
QTextureFileData & operator=(const QTextureFileData &other)
void setDataLength(int length, int level=0, int face=0)
void setSize(const QSize &size)
quint32 glInternalFormat() const
quint32 glBaseInternalFormat() const
void setGLInternalFormat(quint32 format)
quint32 glFormat() const
QByteArray data() const
QByteArray logName() const
QTextureFileData(Mode mode=ByteArrayMode)
QMap< QByteArray, QByteArray > keyValueMetadata() const
void setGLFormat(quint32 format)
void setDataOffset(int offset, int level=0, int face=0)
void setData(const QByteArray &data)
int dataOffset(int level=0, int face=0) const
void setLogName(const QByteArray &name)
QByteArrayView getDataView(int level=0, int face=0) const
void setGLBaseInternalFormat(quint32 format)
void setNumLevels(int num)
void setNumFaces(int num)
constexpr size_type size() const noexcept
const T & at(qsizetype idx) const
void resize(qsizetype sz)
Combined button and popup list for selecting options.
Definition image.cpp:4
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define Q_LOGGING_CATEGORY(name,...)
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum mode
GLenum GLuint GLint level
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLsizei dataSize
GLenum GLuint GLenum GLsizei length
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum face
GLsizei levels
GLsizei GLenum GLenum GLuint GLenum GLsizei * lengths
GLenum GLuint GLintptr offset
GLuint name
GLint GLsizei GLsizei GLenum format
GLsizei GLenum internalFormat
GLint void * img
Definition qopenglext.h:233
GLuint GLsizei const GLuint const GLintptr * offsets
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
constexpr size_t MAX_FACES
static QByteArray glFormatName(quint32 fmt)
QDebug operator<<(QDebug dbg, const QTextureFileData &d)
unsigned int quint32
Definition qtypes.h:50
long long qint64
Definition qtypes.h:60
QVideoFrameFormat::PixelFormat fmt
QSharedPointer< T > other(t)
[5]