Debian Codegraph @

tests/manual/rhi/shared/dds_bc1.h

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 static const quint32 DDS_MAGIC = 0x20534444; // 'DDS '
52 static const quint32 DDS_FOURCC = 4;
53
54 #define FOURCC(c0, c1, c2, c3) ((c0) | ((c1) << 8) | ((c2) << 16) | ((c3 << 24)))
55
56 struct DDS_PIXELFORMAT {
57 quint32 size;
58 quint32 flags;
59 quint32 fourCC;
60 quint32 rgbBitCount;
61 quint32 rBitMask;
62 quint32 gBitMask;
63 quint32 bBitMask;
64 quint32 aBitMask;
65 };
66
67 struct DDS_HEADER {
68 quint32 size;
69 quint32 flags;
70 quint32 height;
71 quint32 width;
72 quint32 pitch;
73 quint32 depth;
74 quint32 mipMapCount;
75 quint32 reserved1[11];
76 DDS_PIXELFORMAT pixelFormat;
77 quint32 caps;
78 quint32 caps2;
79 quint32 caps3;
80 quint32 caps4;
81 quint32 reserved2;
82 };
83
84 static quint32 bc1size(const QSize &size)
85 {
86 static const quint32 blockSize = 8; // 8 bytes for BC1
87 const quint32 bytesPerLine = qMax<quint32>(1, (size.width() + 3) / 4) * blockSize;
88 const quint32 ySize = qMax<quint32>(1, (size.height() + 3) / 4);
89 return bytesPerLine * ySize;
90 }
91
92 static QByteArrayList loadBC1(const QString &filename, QSize *size)
93 {
94 QFile f(filename);
95 if (!f.open(QIODevice::ReadOnly)) {
96 qWarning("Failed to open %s", qPrintable(filename));
97 return QByteArrayList();
98 }
99
100 quint32 magic = 0;
101 f.read(reinterpret_cast<char *>(&magic), sizeof(magic));
102 if (magic != DDS_MAGIC) {
103 qWarning("%s is not a DDS file", qPrintable(filename));
104 return QByteArrayList();
105 }
106 DDS_HEADER header;
107 f.read(reinterpret_cast<char *>(&header), sizeof(header));
108 if (header.size != sizeof(DDS_HEADER)) {
109 qWarning("Invalid DDS header size");
110 return QByteArrayList();
111 }
112 if (header.pixelFormat.size != sizeof(DDS_PIXELFORMAT)) {
113 qWarning("Invalid DDS pixel format size");
114 return QByteArrayList();
115 }
116 if (!(header.pixelFormat.flags & DDS_FOURCC)) {
117 qWarning("Invalid DDS pixel format");
118 return QByteArrayList();
119 }
120 if (header.pixelFormat.fourCC != FOURCC('D', 'X', 'T', '1')) {
121 qWarning("Only DXT1 (BC1) is supported");
122 return QByteArrayList();
123 }
124
125 QByteArrayList data;
126 QSize sz(header.width, header.height);
127 for (quint32 level = 0; level < header.mipMapCount; ++level) {
128 data.append(f.read(bc1size(sz)));
129 sz.setWidth(qMax(1, sz.width() / 2));
130 sz.setHeight(qMax(1, sz.height() / 2));
131 }
132
133 if (size)
134 *size = QSize(header.width, header.height);
135
136 return data;
137 }