3
#include "dds_decompress.h"
6
DXT1/DXT3/DXT5 texture decompression
8
The original code is from Benjamin Dobell, see below for details. Compared to
9
the original this one adds DXT3 decompression, is valid C89, and is x64
10
compatible as it uses fixed size integers everywhere. It also uses a different
15
Copyright (c) 2012, Matthäus G. "Anteru" Chajdas (http://anteru.net)
17
Permission is hereby granted, free of charge, to any person obtaining a copy of
18
this software and associated documentation files (the "Software"), to deal in
19
the Software without restriction, including without limitation the rights to
20
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
21
of the Software, and to permit persons to whom the Software is furnished to do
22
so, subject to the following conditions:
24
The above copyright notice and this permission notice shall be included in all
25
copies or substantial portions of the Software.
27
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
Copyright (C) 2009 Benjamin Dobell, Glass Echidna
39
Permission is hereby granted, free of charge, to any person obtaining a copy of
40
this software and associated documentation files (the "Software"), to deal in
41
the Software without restriction, including without limitation the rights to
42
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
43
of the Software, and to permit persons to whom the Software is furnished to do
44
so, subject to the following conditions:
46
The above copyright notice and this permission notice shall be included in all
47
copies or substantial portions of the Software.
49
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
60
namespace celestia::engine
65
constexpr std::uint32_t PackRGBA(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a)
67
return static_cast<std::uint32_t>(r) |
68
static_cast<std::uint32_t>(g) << 8 |
69
static_cast<std::uint32_t>(b) << 16 |
70
static_cast<std::uint32_t>(a) << 24;
73
constexpr std::uint16_t PackRG(std::uint8_t r, std::uint8_t g)
75
return static_cast<std::uint16_t>(r) | static_cast<std::uint16_t>(g) << 8;
78
void DecompressBlockDXT1Internal(const std::uint8_t* block,
79
std::uint32_t* output,
80
std::uint32_t outputStride,
82
const std::uint8_t* alphaValues)
84
std::uint16_t color0 = *reinterpret_cast<const std::uint16_t*>(block);
85
std::uint16_t color1 = *reinterpret_cast<const std::uint16_t*>(block + 2);
87
std::uint32_t temp = (color0 >> 11) * 255 + 16;
88
std::uint8_t r0 = static_cast<std::uint8_t>((temp / 32 + temp) / 32);
89
temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
90
std::uint8_t g0 = static_cast<std::uint8_t>((temp / 64 + temp) / 64);
91
temp = (color0 & 0x001F) * 255 + 16;
92
std::uint8_t b0 = static_cast<std::uint8_t>((temp / 32 + temp) / 32);
94
temp = (color1 >> 11) * 255 + 16;
95
std::uint8_t r1 = static_cast<std::uint8_t>((temp / 32 + temp) / 32);
96
temp = ((color1 & 0x07E0) >> 5) * 255 + 32;
97
std::uint8_t g1 = static_cast<std::uint8_t>((temp / 64 + temp) / 64);
98
temp = (color1 & 0x001F) * 255 + 16;
99
std::uint8_t b1 = static_cast<std::uint8_t>((temp / 32 + temp) / 32);
101
std::uint32_t code = *reinterpret_cast<const std::uint32_t*>(block + 4);
105
for (int j = 0; j < 4; ++j)
107
for (int i = 0; i < 4; ++i)
109
std::uint8_t alpha = alphaValues[j * 4 + i];
110
std::uint32_t finalColor = 0u;
112
switch (std::uint32_t positionCode = (code >> 2 * (4 * j + i)) & 0x03; positionCode)
115
finalColor = PackRGBA(r0, g0, b0, alpha);
118
finalColor = PackRGBA(r1, g1, b1, alpha);
121
finalColor = PackRGBA((2 * r0 + r1) / 3, (2 * g0 + g1) / 3,
122
(2 * b0 + b1) / 3, alpha);
125
finalColor = PackRGBA((r0 + 2 * r1) / 3, (g0 + 2 * g1) / 3,
126
(b0 + 2 * b1) / 3, alpha);
129
if (transparent0 && (finalColor == PackRGBA(0, 0, 0, 0xff)))
134
output[j * outputStride + i] = finalColor;
140
for (int j = 0; j < 4; ++j)
142
for (int i = 0; i < 4; ++i)
144
std::uint8_t alpha = alphaValues[j * 4 + i];
146
std::uint32_t finalColor = 0u;
147
std::uint32_t positionCode = (code >> 2 * (4 * j + i)) & 0x03;
149
switch (positionCode)
152
finalColor = PackRGBA(r0, g0, b0, alpha);
155
finalColor = PackRGBA(r1, g1, b1, alpha);
158
finalColor = PackRGBA((r0 + r1) / 2, (g0 + g1) / 2, (b0 + b1) / 2, alpha);
161
finalColor = PackRGBA(0, 0, 0, alpha);
165
if (transparent0 && (finalColor == PackRGBA(0, 0, 0, 0xff)))
171
output[j * outputStride + i] = finalColor;
179
void DecompressBlockDXT1(std::uint32_t x,
182
const std::uint8_t* blockStorage,
184
std::uint32_t* image)
186
static std::array<const std::uint8_t, 16> constAlpha = {
193
DecompressBlockDXT1Internal(blockStorage,
194
image + x + (y * width),
200
void DecompressBlockDXT5(std::uint32_t x,
203
const std::uint8_t* blockStorage,
205
std::uint32_t* image)
207
std::uint8_t alpha0 = *(blockStorage);
208
std::uint8_t alpha1 = *(blockStorage + 1);
210
const std::uint8_t* bits = blockStorage + 2;
211
std::uint32_t alphaCode1 = PackRGBA(bits[2], bits[3], bits[4], bits[5]);
212
std::uint16_t alphaCode2 = PackRG(bits[0], bits[1]);
214
std::uint16_t color0 = *reinterpret_cast<const std::uint16_t*>(blockStorage + 8);
215
std::uint16_t color1 = *reinterpret_cast<const std::uint16_t*>(blockStorage + 10);
217
std::uint32_t temp = (color0 >> 11) * 255 + 16;
218
std::uint8_t r0 = (uint8_t)((temp / 32 + temp) / 32);
219
temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
220
std::uint8_t g0 = (uint8_t)((temp / 64 + temp) / 64);
221
temp = (color0 & 0x001F) * 255 + 16;
222
std::uint8_t b0 = (uint8_t)((temp / 32 + temp) / 32);
224
temp = (color1 >> 11) * 255 + 16;
225
std::uint8_t r1 = (uint8_t)((temp / 32 + temp) / 32);
226
temp = ((color1 & 0x07E0) >> 5) * 255 + 32;
227
std::uint8_t g1 = (uint8_t)((temp / 64 + temp) / 64);
228
temp = (color1 & 0x001F) * 255 + 16;
229
std::uint8_t b1 = (uint8_t)((temp / 32 + temp) / 32);
231
std::uint32_t code = *reinterpret_cast<const std::uint32_t*>(blockStorage + 12);
233
for (int j = 0; j < 4; j++)
235
for (int i = 0; i < 4; i++)
237
std::uint8_t finalAlpha;
239
int alphaCodeIndex = 3 * (4 * j + i);
241
if (alphaCodeIndex <= 12)
243
alphaCode = (alphaCode2 >> alphaCodeIndex) & 0x07;
245
else if (alphaCodeIndex == 15)
247
alphaCode = (alphaCode2 >> 15) | ((alphaCode1 << 1) & 0x06);
249
else /* alphaCodeIndex >= 18 && alphaCodeIndex <= 45 */
251
alphaCode = (alphaCode1 >> (alphaCodeIndex - 16)) & 0x07;
258
else if (alphaCode == 1)
266
finalAlpha = static_cast<std::uint8_t>(((8 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) / 7);
274
else if (alphaCode == 7)
280
finalAlpha = static_cast<std::uint8_t>(((6 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) / 5);
285
std::uint8_t colorCode = (code >> 2 * (4 * j + i)) & 0x03;
286
std::uint32_t finalColor = 0;
291
finalColor = PackRGBA(r0, g0, b0, finalAlpha);
294
finalColor = PackRGBA(r1, g1, b1, finalAlpha);
297
finalColor = PackRGBA((2 * r0 + r1) / 3, (2 * g0 + g1) / 3,
298
(2 * b0 + b1) / 3, finalAlpha);
301
finalColor = PackRGBA((r0 + 2 * r1) / 3, (g0 + 2 * g1) / 3,
302
(b0 + 2 * b1) / 3, finalAlpha);
306
image[i + x + (width * (y + j))] = finalColor;
311
void DecompressBlockDXT3(std::uint32_t x,
314
const std::uint8_t* blockStorage,
316
std::uint32_t* image)
318
std::array<std::uint8_t, 16> alphaValues = { 0 };
320
for (int i = 0; i < 4; ++i)
322
const std::uint16_t* alphaData = reinterpret_cast<const std::uint16_t*>(blockStorage);
324
alphaValues[i * 4 + 0] = (((*alphaData) >> 0) & 0xF) * 17;
325
alphaValues[i * 4 + 1] = (((*alphaData) >> 4) & 0xF) * 17;
326
alphaValues[i * 4 + 2] = (((*alphaData) >> 8) & 0xF) * 17;
327
alphaValues[i * 4 + 3] = (((*alphaData) >> 12) & 0xF) * 17;
332
DecompressBlockDXT1Internal(blockStorage,
333
image + x + (y * width),
339
} // namespace celestia::engine