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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#include "texpalette.h"
WiiTexPalette::WiiTexPalette() { }
WiiTexPalette::WiiTexPalette(QDataStream &stream) {
quint32 magic;
stream >> (quint32&)magic;
if (magic != 0x20AF30)
qWarning() << "WiiTexPalette: tried to load a TPL without the proper magic";
quint32 textureCount, headerSize;
stream >> textureCount;
stream >> headerSize;
qDebug() << textureCount << "textures";
textures.resize(textureCount);
for (int i = 0; i < textureCount; i++) {
quint32 textureOffs, paletteOffs;
stream >> textureOffs;
stream >> paletteOffs;
int savePos = stream.device()->pos();
readTexture(stream, textureOffs, paletteOffs, textures[i]);
stream.device()->seek(savePos);
}
}
static const int TexelWidths[] = {
8, 8, 8, 4, 4, 4, 4, -1, 8, 8, 4
};
static const int TexelHeights[] = {
8, 4, 4, 4, 4, 4, 4, -1, 8, 4, 4
};
static const int BitsPerPixel[] = {
4, 8, 8, 16, 16, 16, 32, -1, 8, 16
};
// This bit shamelessly stolen from Dolphin, but it didn't QUITE work right...
/*inline uchar _3to8(uchar v) { return (v << 5) | (v << 2) | (v >> 1); }
inline uchar _4to8(uchar v) { return (v << 4) | v; }
inline uchar _5to8(uchar v) { return (v << 3) | (v << 2); }
inline uchar _6to8(uchar v) { return (v << 2) | (v >> 4); }*/
inline uchar _3to8(uchar v) { return (v << 5); }
inline uchar _4to8(uchar v) { return (v << 4); }
inline uchar _5to8(uchar v) { return (v << 3); }
inline uchar _6to8(uchar v) { return (v << 2); }
void WiiTexPalette::readTexture(QDataStream &in, int textureOffs, int paletteOffs, WiiTPLTexture &tex) {
in.device()->seek(textureOffs);
quint16 width, height;
quint32 rawFormat, dataOffs, rawWrapS, rawWrapT, rawMinFilter, rawMagFilter;
quint8 rawEdgeLODEnable;
in >> height;
in >> width;
in >> rawFormat;
in >> dataOffs;
in >> rawWrapS;
in >> rawWrapT;
in >> rawMinFilter;
in >> rawMagFilter;
in >> tex.lodBias;
in >> rawEdgeLODEnable;
in >> tex.minLOD;
in >> tex.maxLOD;
tex.format = (GX::TextureFormat)rawFormat;
tex.wrapS = (GX::WrapType)rawWrapS;
tex.wrapT = (GX::WrapType)rawWrapT;
tex.minFilter = (GX::TextureFilter)rawMinFilter;
tex.magFilter = (GX::TextureFilter)rawMagFilter;
tex.edgeLODEnable = (rawEdgeLODEnable > 0);
bool formatValid = (
(rawFormat <= 6) ||
(rawFormat >= 8 && rawFormat <= 10) ||
rawFormat == 14);
if (!formatValid) {
qWarning("unknown texture format (%d)", rawFormat);
return;
}
tex.image = QImage(width, height, QImage::Format_ARGB32);
int texelWidth = TexelWidths[rawFormat];
int texelHeight = TexelHeights[rawFormat];
int bpp = BitsPerPixel[rawFormat];
// how much needs to be added on to get this texture aligned?
int padWidth = width % texelWidth;
int padHeight = height % texelHeight;
// get the fully padded width
int paddedWidth = width + ((padWidth > 0) ? (texelWidth - padWidth) : 0);
int paddedHeight = height + ((padHeight > 0) ? (texelHeight - padHeight) : 0);
int texDataSize = (paddedWidth * paddedHeight * bpp) / 8;
// decode the thing
in.device()->seek(dataOffs);
QImage &image = tex.image;
switch (tex.format) {
case GX::I4:
{
for (int texelY = 0; texelY < paddedHeight; texelY += 8) {
for (int texelX = 0; texelX < paddedWidth; texelX += 8) {
for (int y = texelY; y < (texelY + 8); y++) {
QRgb *scanline = (QRgb*)image.scanLine(y);
for (int x = texelX; x < (texelX + 8); x += 2) {
quint8 v;
in >> v;
if (x < width && y < height) {
int _v = _4to8(v >> 4);
scanline[x] = qRgb(_v, _v, _v);
}
if ((x+1) < width && y < height) {
int _v = _4to8(v & 0xF);
scanline[x+1] = qRgb(_v, _v, _v);
}
}
}
}
}
}
break;
case GX::I8:
{
for (int texelY = 0; texelY < paddedHeight; texelY += 4) {
for (int texelX = 0; texelX < paddedWidth; texelX += 8) {
for (int y = texelY; y < (texelY + 4); y++) {
QRgb *scanline = (QRgb*)image.scanLine(y);
for (int x = texelX; x < (texelX + 8); x++) {
quint8 v;
in >> v;
if (x < width && y < height)
scanline[x] = qRgb(v, v, v);
}
}
}
}
}
break;
case GX::IA4:
{
for (int texelY = 0; texelY < paddedHeight; texelY += 4) {
for (int texelX = 0; texelX < paddedWidth; texelX += 8) {
for (int y = texelY; y < (texelY + 4); y++) {
QRgb *scanline = (QRgb*)image.scanLine(y);
for (int x = texelX; x < (texelX + 8); x++) {
quint8 v;
in >> v;
if (x < width && y < height) {
int _i = _4to8(v & 0xF);
int _a = _4to8(v >> 4);
scanline[x] = qRgba(_i, _i, _i, _a);
}
}
}
}
}
}
break;
case GX::IA8:
{
for (int texelY = 0; texelY < paddedHeight; texelY += 4) {
for (int texelX = 0; texelX < paddedWidth; texelX += 4) {
for (int y = texelY; y < (texelY + 4); y++) {
QRgb *scanline = (QRgb*)image.scanLine(y);
for (int x = texelX; x < (texelX + 4); x++) {
quint16 v;
in >> v;
if (x < width && y < height)
scanline[x] = qRgba((v&0xFF00)>>8, (v&0xFF00)>>8, (v&0xFF00)>>8, v&0xFF);
}
}
}
}
}
break;
case GX::RGB565:
{
for (int texelY = 0; texelY < paddedHeight; texelY += 4) {
for (int texelX = 0; texelX < paddedWidth; texelX += 4) {
for (int y = texelY; y < (texelY + 4); y++) {
QRgb *scanline = (QRgb*)image.scanLine(y);
for (int x = texelX; x < (texelX + 4); x++) {
quint16 v;
in >> v;
if (x < width && y < height) {
scanline[x] = qRgb(
_5to8((v >> 11) & 0x1F),
_6to8((v >> 5) & 0x3F),
_5to8(v & 0x1F));
}
}
}
}
}
}
break;
case GX::RGB5A3:
{
for (int texelY = 0; texelY < paddedHeight; texelY += 4) {
for (int texelX = 0; texelX < paddedWidth; texelX += 4) {
for (int y = texelY; y < (texelY + 4); y++) {
QRgb *scanline = (QRgb*)image.scanLine(y);
for (int x = texelX; x < (texelX + 4); x++) {
quint16 v;
in >> v;
if (x < width && y < height) {
if (v & 0x8000) {
scanline[x] = qRgb(
_5to8((v >> 10) & 0x1F),
_5to8((v >> 5) & 0x1F),
_5to8(v & 0x1F));
} else {
scanline[x] = qRgba(
_4to8((v >> 8) & 0xF),
_4to8((v >> 4) & 0xF),
_4to8(v & 0xF),
_3to8((v >> 12) & 0x7));
}
}
}
}
}
}
}
break;
case GX::RGBA8:
{
QByteArray texel1(32, 0), texel2(32, 0);
for (int texelY = 0; texelY < paddedHeight; texelY += 4) {
for (int texelX = 0; texelX < paddedWidth; texelX += 4) {
in.readRawData(texel1.data(), 32);
in.readRawData(texel2.data(), 32);
int offs = 0;
for (int y = texelY; y < (texelY + 4); y++) {
QRgb *scanline = (QRgb*)image.scanLine(y);
for (int x = texelX; x < (texelX + 4); x++) {
if (x < width && y < height) {
scanline[x] = qRgba(texel1.at(offs+1), texel2.at(offs), texel2.at(offs+1), texel1.at(offs));
}
offs += 2;
}
}
}
}
}
break;
default:
qWarning("unhandled texture format (%d)", rawFormat);
}
}
void WiiTexPalette::writeToDataStream(QDataStream &out) const {
}
|