forked from raduprv/Eternal-Lands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dds.c
219 lines (184 loc) · 5.4 KB
/
dds.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/****************************************************************************
* dds.c
*
* Author: 2011 Daniel Jungmann <[email protected]>
* Copyright: See COPYING file that comes with this distribution
****************************************************************************/
#include "dds.h"
void unpack_dxt_color(DXTColorBlock *block, Uint8 *values, Uint32 dxt1)
{
float colors[4][4];
Uint32 i, j, index;
colors[0][0] = (block->m_colors[0] & 0xF800) >> 11;
colors[0][1] = (block->m_colors[0] & 0x07E0) >> 5;
colors[0][2] = block->m_colors[0] & 0x001F;
colors[0][3] = 255.0f;
colors[0][0] *= 255.0f / 31.0f;
colors[0][1] *= 255.0f / 63.0f;
colors[0][2] *= 255.0f / 31.0f;
colors[1][0] = (block->m_colors[1] & 0xF800) >> 11;
colors[1][1] = (block->m_colors[1] & 0x07E0) >> 5;
colors[1][2] = block->m_colors[1] & 0x001F;
colors[1][3] = 255.0f;
colors[1][0] *= 255.0f / 31.0f;
colors[1][1] *= 255.0f / 63.0f;
colors[1][2] *= 255.0f / 31.0f;
if ((dxt1 == 1) && (block->m_colors[0] <= block->m_colors[1]))
{
// 1-bit alpha
// one intermediate colour, half way between the other two
colors[2][0] = (colors[0][0] + colors[1][0]) / 2.0f;
colors[2][1] = (colors[0][1] + colors[1][1]) / 2.0f;
colors[2][2] = (colors[0][2] + colors[1][2]) / 2.0f;
colors[2][3] = (colors[0][3] + colors[1][3]) / 2.0f;
// transparent colour
colors[3][0] = 0.0f;
colors[3][1] = 0.0f;
colors[3][2] = 0.0f;
colors[3][3] = 0.0f;
}
else
{
// first interpolated colour, 1/3 of the way along
colors[2][0] = (2.0f * colors[0][0] + colors[1][0]) / 3.0f;
colors[2][1] = (2.0f * colors[0][1] + colors[1][1]) / 3.0f;
colors[2][2] = (2.0f * colors[0][2] + colors[1][2]) / 3.0f;
colors[2][3] = (2.0f * colors[0][3] + colors[1][3]) / 3.0f;
// second interpolated colour, 2/3 of the way along
colors[3][0] = (colors[0][0] + 2.0f * colors[1][0]) / 3.0f;
colors[3][1] = (colors[0][1] + 2.0f * colors[1][1]) / 3.0f;
colors[3][2] = (colors[0][2] + 2.0f * colors[1][2]) / 3.0f;
colors[3][3] = (colors[0][3] + 2.0f * colors[1][3]) / 3.0f;
}
// Process 4x4 block of texels
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
// LSB come first
index = block->m_indices[i] >> (j * 2) & 0x3;
values[((i * 4) + j) * 4 + 0] = colors[index][0];
values[((i * 4) + j) * 4 + 1] = colors[index][1];
values[((i * 4) + j) * 4 + 2] = colors[index][2];
if (dxt1)
{
// Overwrite entire colour
values[((i * 4) + j) * 4 + 3] = colors[index][3];
}
}
}
}
void unpack_dxt_explicit_alpha(DXTExplicitAlphaBlock *block, Uint8 *values)
{
float value;
Uint32 i, j, index;
index = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = block->m_alphas[i] >> (j * 4) & 0xF;
values[index] = value * 17.0f; // = (value * 255.0f) / 15.0f;
index++;
}
}
}
void unpack_dxt_interpolated_alpha(DXTInterpolatedAlphaBlock *block, Uint8 *values)
{
float alphas[8];
float scale, f0, f1;
Uint32 i, index, idx0, idx1;
alphas[0] = block->m_alphas[0];
alphas[1] = block->m_alphas[1];
if (block->m_alphas[0] > block->m_alphas[1])
{
scale = 1.0f / 7.0f;
for (i = 0; i < 6; i++)
{
f0 = (6 - i) * scale;
f1 = (i + 1) * scale;
alphas[i + 2] = (f0 * block->m_alphas[0]) + (f1 * block->m_alphas[1]);
}
}
else
{
// 4 interpolated alphas, plus zero and one
// full range including extremes at [0] and [5]
// we want to fill in [1] through [4] at weights ranging
// from 1/5 to 4/5
scale = 1.0f / 5.0f;
for (i = 0; i < 4; i++)
{
f0 = (4 - i) * scale;
f1 = (i + 1) * scale;
alphas[i + 2] = (f0 * block->m_alphas[0]) + (f1 * block->m_alphas[1]);
}
alphas[6] = 0.0f;
alphas[7] = 255.0f;
}
for (i = 0; i < 16; i++)
{
idx0 = (i * 3) / 8;
idx1 = (i * 3) % 8;
index = (block->m_indices[idx0] >> idx1) & 0x07;
if (idx1 > 5)
{
index |= (block->m_indices[idx0 + 1] << (8 - idx1)) & 0x07;
}
values[i] = alphas[index];
}
}
void unpack_dxt1(DXTColorBlock *block, Uint8 *values)
{
unpack_dxt_color(block, values, 1);
}
void unpack_dxt3(DXTExplicitAlphaBlock *alpha_block, DXTColorBlock *color_block, Uint8 *values)
{
Uint8 alpha_values[16];
Uint32 i;
unpack_dxt_color(color_block, values, 0);
unpack_dxt_explicit_alpha(alpha_block, alpha_values);
for (i = 0; i < 16; i++)
{
values[i * 4 + 3] = alpha_values[i];
}
}
void unpack_dxt5(DXTInterpolatedAlphaBlock *alpha_block, DXTColorBlock *color_block, Uint8 *values)
{
Uint8 alpha_values[16];
Uint32 i;
unpack_dxt_color(color_block, values, 0);
unpack_dxt_interpolated_alpha(alpha_block, alpha_values);
for (i = 0; i < 16; i++)
{
values[i * 4 + 3] = alpha_values[i];
}
}
void unpack_ati1(DXTInterpolatedAlphaBlock *block, Uint8 *values)
{
Uint8 alpha_values[16];
Uint32 i;
unpack_dxt_interpolated_alpha(block, alpha_values);
for (i = 0; i < 16; i++)
{
values[i * 4 + 0] = alpha_values[i];
values[i * 4 + 1] = alpha_values[i];
values[i * 4 + 2] = alpha_values[i];
values[i * 4 + 3] = alpha_values[i];
}
}
void unpack_ati2(DXTInterpolatedAlphaBlock *first_block, DXTInterpolatedAlphaBlock *second_block,
Uint8 *values)
{
Uint8 first_values[16], second_values[16];
Uint32 i;
unpack_dxt_interpolated_alpha(first_block, first_values);
unpack_dxt_interpolated_alpha(second_block, second_values);
for (i = 0; i < 16; i++)
{
values[i * 4 + 0] = first_values[i];
values[i * 4 + 1] = first_values[i];
values[i * 4 + 2] = first_values[i];
values[i * 4 + 3] = second_values[i];
}
}