Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qastchandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 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// Qt-Security score:critical reason:data-parser
4
7
8#include <private/qnumeric_p.h>
9
10#include <QFile>
11#include <QDebug>
12#include <QSize>
13
15
26
27QAstcHandler::~QAstcHandler() = default;
28
29bool QAstcHandler::canRead(const QByteArray &suffix, const QByteArray &block)
30{
31 Q_UNUSED(suffix);
32
33 return block.startsWith("\x13\xAB\xA1\x5C");
34}
35
36quint32 QAstcHandler::astcGLFormat(quint8 xBlockDim, quint8 yBlockDim) const
37{
38 static const quint32 glFormatRGBABase = 0x93B0; // GL_COMPRESSED_RGBA_ASTC_4x4_KHR
39 static const quint32 glFormatSRGBBase = 0x93D0; // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
40
41 Q_CONSTINIT static QSize dims[14] = {
42 { 4, 4 }, // GL_COMPRESSED_xxx_ASTC_4x4_KHR
43 { 5, 4 }, // GL_COMPRESSED_xxx_ASTC_5x4_KHR
44 { 5, 5 }, // GL_COMPRESSED_xxx_ASTC_5x5_KHR
45 { 6, 5 }, // GL_COMPRESSED_xxx_ASTC_6x5_KHR
46 { 6, 6 }, // GL_COMPRESSED_xxx_ASTC_6x6_KHR
47 { 8, 5 }, // GL_COMPRESSED_xxx_ASTC_8x5_KHR
48 { 8, 6 }, // GL_COMPRESSED_xxx_ASTC_8x6_KHR
49 { 8, 8 }, // GL_COMPRESSED_xxx_ASTC_8x8_KHR
50 { 10, 5 }, // GL_COMPRESSED_xxx_ASTC_10x5_KHR
51 { 10, 6 }, // GL_COMPRESSED_xxx_ASTC_10x6_KHR
52 { 10, 8 }, // GL_COMPRESSED_xxx_ASTC_10x8_KHR
53 { 10, 10 }, // GL_COMPRESSED_xxx_ASTC_10x10_KHR
54 { 12, 10 }, // GL_COMPRESSED_xxx_ASTC_12x10_KHR
55 { 12, 12 } // GL_COMPRESSED_xxx_ASTC_12x12_KHR
56 };
57
58 const QSize dim(xBlockDim, yBlockDim);
59 int index = -1;
60 for (int i = 0; i < 14; i++) {
61 if (dim == dims[i]) {
62 index = i;
63 break;
64 }
65 }
66 if (index < 0)
67 return 0;
68
69 bool useSrgb = qEnvironmentVariableIsSet("QT_ASTCHANDLER_USE_SRGB")
70 || logName().toLower().contains("srgb");
71
72 return useSrgb ? (glFormatSRGBBase + index) : (glFormatRGBABase + index);
73}
74
76{
77 QTextureFileData nullData;
79
80 if (!device())
81 return nullData;
82
83 QByteArray fileData = device()->readAll();
84 if (fileData.size() < int(sizeof(AstcHeader)) || !canRead(QByteArray(), fileData)) {
85 qCDebug(lcQtGuiTextureIO, "Not an ASTC file: %s", logName().constData());
86 return nullData;
87 }
88 res.setData(fileData);
89
90 const AstcHeader *header = reinterpret_cast<const AstcHeader *>(fileData.constData());
91
92 int xSz = int(header->xSize[0]) | int(header->xSize[1]) << 8 | int(header->xSize[2]) << 16;
93 int ySz = int(header->ySize[0]) | int(header->ySize[1]) << 8 | int(header->ySize[2]) << 16;
94 int zSz = int(header->zSize[0]) | int(header->zSize[1]) << 8 | int(header->zSize[2]) << 16;
95
96 quint32 glFmt = astcGLFormat(header->blockDimX, header->blockDimY);
97
98 if (!xSz || !ySz || !zSz || !glFmt || header->blockDimZ != 1) {
99 qCDebug(lcQtGuiTextureIO, "Invalid ASTC header data in file %s", logName().constData());
100 return nullData;
101 }
102
103 res.setSize(QSize(xSz, ySz));
104 res.setGLFormat(0); // 0 for compressed textures
105 res.setGLInternalFormat(glFmt);
106 //? BaseInternalFormat
107
108 int xBlocks = (xSz + header->blockDimX - 1) / header->blockDimX;
109 int yBlocks = (ySz + header->blockDimY - 1) / header->blockDimY;
110 int zBlocks = (zSz + header->blockDimZ - 1) / header->blockDimZ;
111
112 int byteCount = 0;
113 bool oob = qMulOverflow(xBlocks, yBlocks, &byteCount)
114 || qMulOverflow(byteCount, zBlocks, &byteCount)
115 || qMulOverflow(byteCount, 16, &byteCount);
116
117
118 res.setDataOffset(sizeof(AstcHeader));
119 res.setNumLevels(1);
120 res.setNumFaces(1);
121 res.setDataLength(byteCount);
122
123 if (oob || !res.isValid()) {
124 qCDebug(lcQtGuiTextureIO, "Invalid ASTC file %s", logName().constData());
125 return nullData;
126 }
127
128 res.setLogName(logName());
129
130#if 0
131 qDebug() << "ASTC file handler read" << res << res.dataOffset() << res.dataLength();
132#endif
133 return res;
134}
135
136QT_END_NAMESPACE
QTextureFileData read() override
~QAstcHandler() override
quint8 ySize[3]
quint8 blockDimZ
quint8 magic[4]
quint8 xSize[3]
quint8 zSize[3]
quint8 blockDimX
quint8 blockDimY