summaryrefslogtreecommitdiff
path: root/src/gui/util/qtexturefiledata.cpp
blob: 470def8bb75219c6765d4b487538ce66e3d3fe7c (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qtexturefiledata_p.h"
#include <QMetaEnum>
#include <QSize>
#if QT_CONFIG(opengl)
#include <QOpenGLTexture>
#endif

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcQtGuiTextureIO, "qt.gui.textureio");

class QTextureFileDataPrivate : public QSharedData
{
public:
    QTextureFileDataPrivate()
    {
    }

    QTextureFileDataPrivate(const QTextureFileDataPrivate &other)
        : QSharedData(other),
          logName(other.logName),
          data(other.data),
          offsets(other.offsets),
          lengths(other.lengths),
          size(other.size),
          format(other.format)
    {
    }

    ~QTextureFileDataPrivate()
    {
    }

    void ensureLevels(int num, bool force = false)
    {
        const int newSize = force ? num : qMax(offsets.size(), num);
        offsets.resize(newSize);
        lengths.resize(newSize);
    }

    QByteArray logName;
    QByteArray data;
    QVector<int> offsets;
    QVector<int> lengths;
    QSize size;
    quint32 format = 0;
    quint32 internalFormat = 0;
    quint32 baseInternalFormat = 0;
};



QTextureFileData::QTextureFileData()
{
}

QTextureFileData::QTextureFileData(const QTextureFileData &other)
    : d(other.d)
{
}

QTextureFileData &QTextureFileData::operator=(const QTextureFileData &other)
{
    d = other.d;
    return *this;
}

QTextureFileData::~QTextureFileData()
{
}

bool QTextureFileData::isNull() const
{
    return !d;
}

bool QTextureFileData::isValid() const
{
    if (!d)
        return false;

    if (d->data.isEmpty() || d->size.isEmpty() || (!d->format && !d->internalFormat))
        return false;

    const int numChunks = d->offsets.size();
    if (numChunks == 0 || (d->lengths.size() != numChunks))
         return false;

    const qint64 sz = d->data.size();
    for (int i = 0; i < numChunks; i++) {
        qint64 offi = d->offsets.at(i);
        qint64 leni = d->lengths.at(i);
        if (offi < 0 || offi >= sz || leni <= 0 || (offi + leni > sz))
            return false;
    }
    return true;
}

void QTextureFileData::clear()
{
    d = nullptr;
}

QByteArray QTextureFileData::data() const
{
    return d ? d->data : QByteArray();
}

void QTextureFileData::setData(const QByteArray &data)
{
    if (!d.constData())  //### uh think about this design, this is the only way to create; should be constructor instead at least
        d = new QTextureFileDataPrivate;

    d->data = data;
}

int QTextureFileData::dataOffset(int level) const
{
    return (d && d->offsets.size() > level) ? d->offsets.at(level) : 0;
}

void QTextureFileData::setDataOffset(int offset, int level)
{
    if (d.constData() && level >= 0) {
        d->ensureLevels(level + 1);
        d->offsets[level] = offset;
    }
}

int QTextureFileData::dataLength(int level) const
{
    return (d && d->lengths.size() > level) ? d->lengths.at(level) : 0;
}

void QTextureFileData::setDataLength(int length, int level)
{
    if (d.constData() && level >= 0) {
        d->ensureLevels(level + 1);
        d->lengths[level] = length;
    }
}

int QTextureFileData::numLevels() const
{
    return d ? d->offsets.size() : 0;
}

void QTextureFileData::setNumLevels(int num)
{
    if (d && num >= 0)
        d->ensureLevels(num, true);
}

QSize QTextureFileData::size() const
{
    return d ? d->size : QSize();
}

void QTextureFileData::setSize(const QSize &size)
{
    if (d.constData())
        d->size = size;
}

quint32 QTextureFileData::glFormat() const
{
    return d ? d->format : 0;
}

void QTextureFileData::setGLFormat(quint32 format)
{
    if (d.constData())
        d->format = format;
}

quint32 QTextureFileData::glInternalFormat() const
{
    return d ? d->internalFormat : 0;
}

void QTextureFileData::setGLInternalFormat(quint32 format)
{
    if (d.constData())
        d->internalFormat = format;
}

quint32 QTextureFileData::glBaseInternalFormat() const
{
    return d ? d->baseInternalFormat : 0;
}

void QTextureFileData::setGLBaseInternalFormat(quint32 format)
{
    if (d.constData())
        d->baseInternalFormat = format;
}

QByteArray QTextureFileData::logName() const
{
    return d ? d->logName : QByteArray();
}

void QTextureFileData::setLogName(const QByteArray &name)
{
    if (d.constData())
        d->logName = name;
}

static QByteArray glFormatName(quint32 fmt)
{
    const char *id = nullptr;
#if QT_CONFIG(opengl)
    id = QMetaEnum::fromType<QOpenGLTexture::TextureFormat>().valueToKey(fmt);
#endif
    QByteArray res(id ? id : "(?)");
    res += " [0x" + QByteArray::number(fmt, 16).rightJustified(4, '0') + ']';
    return res;
}

QDebug operator<<(QDebug dbg, const QTextureFileData &d)
{
    QDebugStateSaver saver(dbg);

    dbg.nospace() << "QTextureFileData(";
    if (!d.isNull()) {
        dbg.space() << d.logName() << d.size();
        dbg << "glFormat:" << glFormatName(d.glFormat());
        dbg << "glInternalFormat:" << glFormatName(d.glInternalFormat());
        dbg << "glBaseInternalFormat:" << glFormatName(d.glBaseInternalFormat());
        dbg.nospace() << "Levels: " << d.numLevels();
        if (!d.isValid())
            dbg << " {Invalid}";
        dbg << ")";
    } else {
        dbg << "null)";
    }

    return dbg;
}

QT_END_NAMESPACE