| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2019 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qastchandler_p.h" |
| 41 | #include "qtexturefiledata_p.h" |
| 42 | |
| 43 | #include <private/qnumeric_p.h> |
| 44 | |
| 45 | #include <QFile> |
| 46 | #include <QDebug> |
| 47 | #include <QSize> |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | struct |
| 52 | { |
| 53 | quint8 [4]; |
| 54 | quint8 ; |
| 55 | quint8 ; |
| 56 | quint8 ; |
| 57 | quint8 [3]; |
| 58 | quint8 [3]; |
| 59 | quint8 [3]; |
| 60 | }; |
| 61 | |
| 62 | bool QAstcHandler::canRead(const QByteArray &suffix, const QByteArray &block) |
| 63 | { |
| 64 | Q_UNUSED(suffix) |
| 65 | |
| 66 | return block.startsWith(c: "\x13\xAB\xA1\x5C" ); |
| 67 | } |
| 68 | |
| 69 | quint32 QAstcHandler::astcGLFormat(quint8 xBlockDim, quint8 yBlockDim) const |
| 70 | { |
| 71 | static const quint32 glFormatRGBABase = 0x93B0; // GL_COMPRESSED_RGBA_ASTC_4x4_KHR |
| 72 | static const quint32 glFormatSRGBBase = 0x93D0; // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR |
| 73 | |
| 74 | static QSize dims[14] = { |
| 75 | { 4, 4 }, // GL_COMPRESSED_xxx_ASTC_4x4_KHR |
| 76 | { 5, 4 }, // GL_COMPRESSED_xxx_ASTC_5x4_KHR |
| 77 | { 5, 5 }, // GL_COMPRESSED_xxx_ASTC_5x5_KHR |
| 78 | { 6, 5 }, // GL_COMPRESSED_xxx_ASTC_6x5_KHR |
| 79 | { 6, 6 }, // GL_COMPRESSED_xxx_ASTC_6x6_KHR |
| 80 | { 8, 5 }, // GL_COMPRESSED_xxx_ASTC_8x5_KHR |
| 81 | { 8, 6 }, // GL_COMPRESSED_xxx_ASTC_8x6_KHR |
| 82 | { 8, 8 }, // GL_COMPRESSED_xxx_ASTC_8x8_KHR |
| 83 | { 10, 5 }, // GL_COMPRESSED_xxx_ASTC_10x5_KHR |
| 84 | { 10, 6 }, // GL_COMPRESSED_xxx_ASTC_10x6_KHR |
| 85 | { 10, 8 }, // GL_COMPRESSED_xxx_ASTC_10x8_KHR |
| 86 | { 10, 10 }, // GL_COMPRESSED_xxx_ASTC_10x10_KHR |
| 87 | { 12, 10 }, // GL_COMPRESSED_xxx_ASTC_12x10_KHR |
| 88 | { 12, 12 } // GL_COMPRESSED_xxx_ASTC_12x12_KHR |
| 89 | }; |
| 90 | |
| 91 | const QSize dim(xBlockDim, yBlockDim); |
| 92 | int index = -1; |
| 93 | for (int i = 0; i < 14; i++) { |
| 94 | if (dim == dims[i]) { |
| 95 | index = i; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | if (index < 0) |
| 100 | return 0; |
| 101 | |
| 102 | bool useSrgb = qEnvironmentVariableIsSet(varName: "QT_ASTCHANDLER_USE_SRGB" ) |
| 103 | || logName().toLower().contains(c: "srgb" ); |
| 104 | |
| 105 | return useSrgb ? (glFormatSRGBBase + index) : (glFormatRGBABase + index); |
| 106 | } |
| 107 | |
| 108 | QTextureFileData QAstcHandler::read() |
| 109 | { |
| 110 | QTextureFileData nullData; |
| 111 | QTextureFileData res; |
| 112 | |
| 113 | if (!device()) |
| 114 | return nullData; |
| 115 | |
| 116 | QByteArray fileData = device()->readAll(); |
| 117 | if (fileData.size() < int(sizeof(AstcHeader)) || !canRead(suffix: QByteArray(), block: fileData)) { |
| 118 | qCDebug(lcQtGuiTextureIO, "Not an ASTC file: %s" , logName().constData()); |
| 119 | return nullData; |
| 120 | } |
| 121 | res.setData(fileData); |
| 122 | |
| 123 | const AstcHeader * = reinterpret_cast<const AstcHeader *>(fileData.constData()); |
| 124 | |
| 125 | int xSz = int(header->xSize[0]) | int(header->xSize[1]) << 8 | int(header->xSize[2]) << 16; |
| 126 | int ySz = int(header->ySize[0]) | int(header->ySize[1]) << 8 | int(header->ySize[2]) << 16; |
| 127 | int zSz = int(header->zSize[0]) | int(header->zSize[1]) << 8 | int(header->zSize[2]) << 16; |
| 128 | |
| 129 | quint32 glFmt = astcGLFormat(xBlockDim: header->blockDimX, yBlockDim: header->blockDimY); |
| 130 | |
| 131 | if (!xSz || !ySz || !zSz || !glFmt || header->blockDimZ != 1) { |
| 132 | qCDebug(lcQtGuiTextureIO, "Invalid ASTC header data in file %s" , logName().constData()); |
| 133 | return nullData; |
| 134 | } |
| 135 | |
| 136 | res.setSize(QSize(xSz, ySz)); |
| 137 | res.setGLFormat(0); // 0 for compressed textures |
| 138 | res.setGLInternalFormat(glFmt); |
| 139 | //? BaseInternalFormat |
| 140 | |
| 141 | int xBlocks = (xSz + header->blockDimX - 1) / header->blockDimX; |
| 142 | int yBlocks = (ySz + header->blockDimY - 1) / header->blockDimY; |
| 143 | int zBlocks = (zSz + header->blockDimZ - 1) / header->blockDimZ; |
| 144 | |
| 145 | int byteCount = 0; |
| 146 | bool oob = mul_overflow(v1: xBlocks, v2: yBlocks, r: &byteCount) |
| 147 | || mul_overflow(v1: byteCount, v2: zBlocks, r: &byteCount) |
| 148 | || mul_overflow(v1: byteCount, v2: 16, r: &byteCount); |
| 149 | |
| 150 | |
| 151 | res.setDataOffset(offset: sizeof(AstcHeader)); |
| 152 | res.setNumLevels(1); |
| 153 | res.setDataLength(length: byteCount); |
| 154 | |
| 155 | if (oob || !res.isValid()) { |
| 156 | qCDebug(lcQtGuiTextureIO, "Invalid ASTC file %s" , logName().constData()); |
| 157 | return nullData; |
| 158 | } |
| 159 | |
| 160 | res.setLogName(logName()); |
| 161 | |
| 162 | #if 0 |
| 163 | qDebug() << "ASTC file handler read" << res << res.dataOffset() << res.dataLength(); |
| 164 | #endif |
| 165 | return res; |
| 166 | } |
| 167 | |
| 168 | QT_END_NAMESPACE |
| 169 | |