-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDxtDecoder.cs
More file actions
184 lines (160 loc) · 7.5 KB
/
Copy pathDxtDecoder.cs
File metadata and controls
184 lines (160 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
namespace ShadowTXD;
internal static class DxtDecoder
{
public static void Decode(byte[] srcData, int width, int height, int dxtType, bool hasAlpha, byte[] dstRgba, int dstWidth, int dstHeight)
{
int blockSize = (dxtType == 1) ? 8 : 16;
int blocksX = width / 4;
int blocksY = height / 4;
for (int by = 0; by < blocksY; by++)
{
for (int bx = 0; bx < blocksX; bx++)
{
int blockIndex = by * blocksX + bx;
int blockOffset = blockIndex * blockSize;
DecodeBlock(srcData, blockOffset, dxtType, out byte[,] colors);
for (int ly = 0; ly < 4; ly++)
{
for (int lx = 0; lx < 4; lx++)
{
int px = bx * 4 + lx;
int py = by * 4 + ly;
if (px < dstWidth && py < dstHeight)
{
int outIdx = (py * dstWidth + px) * 4;
dstRgba[outIdx + 0] = colors[ly, lx];
dstRgba[outIdx + 1] = colors[ly, lx + 4];
dstRgba[outIdx + 2] = colors[ly, lx + 8];
dstRgba[outIdx + 3] = colors[ly, lx + 12];
}
}
}
}
}
}
private static void DecodeBlock(byte[] data, int offset, int dxtType, out byte[,] colors)
{
colors = new byte[4, 16];
if (dxtType == 1)
{
ushort c0 = (ushort)(data[offset + 0] | (data[offset + 1] << 8));
ushort c1 = (ushort)(data[offset + 2] | (data[offset + 3] << 8));
uint indices = (uint)(data[offset + 4] | (data[offset + 5] << 8) | (data[offset + 6] << 16) | (data[offset + 7] << 24));
byte r0 = Scale5((byte)(c0 >> 11));
byte g0 = Scale6((byte)((c0 >> 5) & 0x3F));
byte b0 = Scale5((byte)(c0 & 0x1F));
byte r1 = Scale5((byte)(c1 >> 11));
byte g1 = Scale6((byte)((c1 >> 5) & 0x3F));
byte b1 = Scale5((byte)(c1 & 0x1F));
uint[] c = new uint[4];
c[0] = PackColor(r0, g0, b0, 255);
c[1] = PackColor(r1, g1, b1, 255);
if (c0 > c1)
{
c[2] = PackColor((byte)((2 * r0 + r1) / 3), (byte)((2 * g0 + g1) / 3), (byte)((2 * b0 + b1) / 3), 255);
c[3] = PackColor((byte)((r0 + 2 * r1) / 3), (byte)((g0 + 2 * g1) / 3), (byte)((b0 + 2 * b1) / 3), 255);
}
else
{
c[2] = PackColor((byte)((r0 + r1) / 2), (byte)((g0 + g1) / 2), (byte)((b0 + b1) / 2), 255);
c[3] = PackColor(0, 0, 0, 0);
}
for (int i = 0; i < 16; i++)
{
byte idx = (byte)((indices >> (i * 2)) & 3);
uint col = c[idx];
int row = i / 4;
int col2 = i % 4;
colors[row, col2] = (byte)(col & 0xFF);
colors[row, col2 + 4] = (byte)((col >> 8) & 0xFF);
colors[row, col2 + 8] = (byte)((col >> 16) & 0xFF);
colors[row, col2 + 12] = (byte)((col >> 24) & 0xFF);
}
}
else if (dxtType == 2 || dxtType == 3)
{
ulong alphaBits = 0;
for (int i = 0; i < 8; i++)
alphaBits |= ((ulong)data[offset + i]) << (i * 8);
ushort c0 = (ushort)(data[offset + 8] | (data[offset + 9] << 8));
ushort c1 = (ushort)(data[offset + 10] | (data[offset + 11] << 8));
uint indices = (uint)(data[offset + 12] | (data[offset + 13] << 8) | (data[offset + 14] << 16) | (data[offset + 15] << 24));
byte r0 = Scale5((byte)(c0 >> 11));
byte g0 = Scale6((byte)((c0 >> 5) & 0x3F));
byte b0 = Scale5((byte)(c0 & 0x1F));
byte r1 = Scale5((byte)(c1 >> 11));
byte g1 = Scale6((byte)((c1 >> 5) & 0x3F));
byte b1 = Scale5((byte)(c1 & 0x1F));
uint[] c = new uint[4];
c[0] = PackColor(r0, g0, b0, 255);
c[1] = PackColor(r1, g1, b1, 255);
c[2] = PackColor((byte)((2 * r0 + r1) / 3), (byte)((2 * g0 + g1) / 3), (byte)((2 * b0 + b1) / 3), 255);
c[3] = PackColor((byte)((r0 + 2 * r1) / 3), (byte)((g0 + 2 * g1) / 3), (byte)((b0 + 2 * b1) / 3), 255);
for (int i = 0; i < 16; i++)
{
byte aIdx = (byte)((alphaBits >> (i * 4)) & 0xF);
byte alpha = (byte)(aIdx * 17);
byte cIdx = (byte)((indices >> (i * 2)) & 3);
uint col = c[cIdx];
int row = i / 4;
int col2 = i % 4;
colors[row, col2] = (byte)(col & 0xFF);
colors[row, col2 + 4] = (byte)((col >> 8) & 0xFF);
colors[row, col2 + 8] = (byte)((col >> 16) & 0xFF);
colors[row, col2 + 12] = alpha;
}
}
else if (dxtType == 4 || dxtType == 5)
{
byte a0 = data[offset + 0];
byte a1 = data[offset + 1];
ulong alphaBits = 0;
for (int i = 0; i < 6; i++)
alphaBits |= ((ulong)data[offset + 2 + i]) << (i * 8);
ushort c0 = (ushort)(data[offset + 8] | (data[offset + 9] << 8));
ushort c1 = (ushort)(data[offset + 10] | (data[offset + 11] << 8));
uint colorIndices = (uint)(data[offset + 12] | (data[offset + 13] << 8) | (data[offset + 14] << 16) | (data[offset + 15] << 24));
byte r0 = Scale5((byte)(c0 >> 11));
byte g0 = Scale6((byte)((c0 >> 5) & 0x3F));
byte b0 = Scale5((byte)(c0 & 0x1F));
byte r1 = Scale5((byte)(c1 >> 11));
byte g1 = Scale6((byte)((c1 >> 5) & 0x3F));
byte b1 = Scale5((byte)(c1 & 0x1F));
uint[] c = new uint[4];
c[0] = PackColor(r0, g0, b0, 255);
c[1] = PackColor(r1, g1, b1, 255);
c[2] = PackColor((byte)((2 * r0 + r1) / 3), (byte)((2 * g0 + g1) / 3), (byte)((2 * b0 + b1) / 3), 255);
c[3] = PackColor((byte)((r0 + 2 * r1) / 3), (byte)((g0 + 2 * g1) / 3), (byte)((b0 + 2 * b1) / 3), 255);
byte[] alphas = new byte[8];
alphas[0] = a0;
alphas[1] = a1;
for (int i = 2; i < 8; i++)
{
if (a0 > a1)
alphas[i] = (byte)(((6 - (i - 2)) * a0 + ((i - 2) + 1) * a1) / 7);
else if (i == 6)
alphas[i] = 0;
else if (i == 7)
alphas[i] = 255;
else
alphas[i] = (byte)(((4 - (i - 2)) * a0 + ((i - 2) + 1) * a1) / 5);
}
for (int i = 0; i < 16; i++)
{
byte aIdx = (byte)((alphaBits >> (i * 3)) & 7);
byte alpha = alphas[aIdx];
byte cIdx = (byte)((colorIndices >> (i * 2)) & 3);
uint col = c[cIdx];
int row = i / 4;
int col2 = i % 4;
colors[row, col2] = (byte)(col & 0xFF);
colors[row, col2 + 4] = (byte)((col >> 8) & 0xFF);
colors[row, col2 + 8] = (byte)((col >> 16) & 0xFF);
colors[row, col2 + 12] = alpha;
}
}
}
private static byte Scale5(byte v) => (byte)(v * 255 / 31);
private static byte Scale6(byte v) => (byte)(v * 255 / 63);
private static uint PackColor(byte r, byte g, byte b, byte a) => (uint)(r | (g << 8) | (b << 16) | ((uint)a << 24));
}