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
qpkmhandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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 "qpkmhandler_p.h"
6
7#include <QFile>
8#include <QDebug>
9#include <QSize>
10#include <qendian.h>
11
12//#define ETC_DEBUG
13
15
16static const int qpkmh_headerSize = 16;
17
23
24static constexpr PkmType typeMap[5] = {
25 { 0x8D64, 8 }, // GL_ETC1_RGB8_OES
26 { 0x9274, 8 }, // GL_COMPRESSED_RGB8_ETC2
27 { 0, 0 }, // unused (obsolete)
28 { 0x9278, 16}, // GL_COMPRESSED_RGBA8_ETC2_EAC
29 { 0x9276, 8 } // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
30};
31
33
34bool QPkmHandler::canRead(const QByteArray &suffix, const QByteArray &block)
35{
36 Q_UNUSED(suffix);
37
38 return block.startsWith("PKM ");
39}
40
42{
43 QTextureFileData texData;
44
45 if (!device())
46 return texData;
47
48 QByteArray fileData = device()->readAll();
49 if (fileData.size() < qpkmh_headerSize || !canRead(QByteArray(), fileData)) {
50 qCDebug(lcQtGuiTextureIO, "Invalid PKM file %s", logName().constData());
51 return QTextureFileData();
52 }
53 texData.setData(fileData);
54
55 const char *rawData = fileData.constData();
56
57 // ignore version (rawData + 4 & 5)
58
59 // texture type
60 quint16 type = qFromBigEndian<quint16>(rawData + 6);
61 if (type >= sizeof(typeMap)/sizeof(typeMap[0])) {
62 qCDebug(lcQtGuiTextureIO, "Unknown compression format in PKM file %s", logName().constData());
63 return QTextureFileData();
64 }
65 texData.setGLFormat(0); // 0 for compressed textures
66 texData.setGLInternalFormat(typeMap[type].glFormat);
67 //### setBaseInternalFormat
68
69 // texture size
70 texData.setNumLevels(1);
71 texData.setNumFaces(1);
72 const int bpb = typeMap[type].bytesPerBlock;
73 QSize paddedSize(qFromBigEndian<quint16>(rawData + 8), qFromBigEndian<quint16>(rawData + 10));
74 texData.setDataLength((paddedSize.width() / 4) * (paddedSize.height() / 4) * bpb);
75 QSize texSize(qFromBigEndian<quint16>(rawData + 12), qFromBigEndian<quint16>(rawData + 14));
76 texData.setSize(texSize);
77
78 texData.setDataOffset(qpkmh_headerSize);
79
80 if (!texData.isValid()) {
81 qCDebug(lcQtGuiTextureIO, "Invalid values in header of PKM file %s", logName().constData());
82 return QTextureFileData();
83 }
84
85 texData.setLogName(logName());
86
87#ifdef ETC_DEBUG
88 qDebug() << "PKM file handler read" << texData;
89#endif
90 return texData;
91}
92
\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 startsWith(QByteArrayView bv) const
Definition qbytearray.h:223
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
~QPkmHandler() override
static bool canRead(const QByteArray &suffix, const QByteArray &block)
QTextureFileData read() override
\inmodule QtCore
Definition qsize.h:25
QIODevice * device() const
QByteArray logName() const
Combined button and popup list for selecting options.
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qDebug
[1]
Definition qlogging.h:164
#define qCDebug(category,...)
GLenum type
static constexpr PkmType typeMap[5]
static QT_BEGIN_NAMESPACE const int qpkmh_headerSize
#define Q_UNUSED(x)
unsigned int quint32
Definition qtypes.h:50
unsigned short quint16
Definition qtypes.h:48
quint32 bytesPerBlock
quint32 glFormat