-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDSLoader.cs
More file actions
479 lines (455 loc) · 18.8 KB
/
Copy pathDDSLoader.cs
File metadata and controls
479 lines (455 loc) · 18.8 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class DDSLoader {
// to ease up memory usage and GC, we reuse datare
static byte[] datare;
// RGBA Masks
static uint[] A1R5G5B5_MASKS = { 0x7C00, 0x03E0, 0x001F, 0x8000 };
static uint[] X1R5G5B5_MASKS = { 0x7C00, 0x03E0, 0x001F, 0x0000 };
static uint[] A4R4G4B4_MASKS = { 0x0F00, 0x00F0, 0x000F, 0xF000 };
static uint[] X4R4G4B4_MASKS = { 0x0F00, 0x00F0, 0x000F, 0x0000 };
static uint[] R5G6B5_MASKS = { 0xF800, 0x07E0, 0x001F, 0x0000 };
static uint[] R8G8B8_MASKS = { 0xFF0000, 0x00FF00, 0x0000FF, 0x000000 };
static uint[] A8B8G8R8_MASKS = { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 };
static uint[] X8B8G8R8_MASKS = { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 };
static uint[] A8R8G8B8_MASKS = { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 };
static uint[] X8R8G8B8_MASKS = { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 };
const int DXT1 = 0x31545844;
const int DXT2 = 0x32545844;
const int DXT3 = 0x33545844;
const int DXT4 = 0x34545844;
const int DXT5 = 0x35545844;
const int A1R5G5B5 = 1;
const int X1R5G5B5 = 2;
const int A4R4G4B4 = 3;
const int X4R4G4B4 = 4;
const int R5G6B5 = 5;
const int R8G8B8 = 6;
const int A8B8G8R8 = 7;
const int X8B8G8R8 = 8;
const int A8R8G8B8 = 9;
const int X8R8G8B8 = 10;
static int GetWidth(byte[] data)
{
return System.BitConverter.ToInt32(data, 16);
}
static int GetHeight(byte[] data)
{
return System.BitConverter.ToInt32(data, 12);
}
static int GetFlags(byte[] data)
{
return System.BitConverter.ToInt32(data, 80);
}
static int GetType(byte[] data)
{
int flags = GetFlags(data);
if ((flags & 0x04) != 0)
{
// FourCC
return System.BitConverter.ToInt32(data, 84);
}
// otherwise...
int bitCount = System.BitConverter.ToInt32(data, 88);
int redMask = System.BitConverter.ToInt32(data, 92);
int greenMask = System.BitConverter.ToInt32(data, 96);
int blueMask = System.BitConverter.ToInt32(data, 100);
// check for 0x01 alpha flag
int alphaMask = (flags & 0x01) != 0 ? System.BitConverter.ToInt32(data, 104) : 0;
switch (bitCount)
{
case 16:
{
if (redMask == A1R5G5B5_MASKS[0] && greenMask == A1R5G5B5_MASKS[1] && blueMask == A1R5G5B5_MASKS[2] && alphaMask == A1R5G5B5_MASKS[3])
{
// A1R5G5B5
return A1R5G5B5;
}
else if (redMask == X1R5G5B5_MASKS[0] && greenMask == X1R5G5B5_MASKS[1] && blueMask == X1R5G5B5_MASKS[2] && alphaMask == X1R5G5B5_MASKS[3])
{
// X1R5G5B5
return X1R5G5B5;
}
else if (redMask == A4R4G4B4_MASKS[0] && greenMask == A4R4G4B4_MASKS[1] && blueMask == A4R4G4B4_MASKS[2] && alphaMask == A4R4G4B4_MASKS[3])
{
// A4R4G4B4
return A4R4G4B4;
}
else if (redMask == X4R4G4B4_MASKS[0] && greenMask == X4R4G4B4_MASKS[1] && blueMask == X4R4G4B4_MASKS[2] && alphaMask == X4R4G4B4_MASKS[3])
{
// X4R4G4B4
return X4R4G4B4;
}
else if (redMask == R5G6B5_MASKS[0] && greenMask == R5G6B5_MASKS[1] && blueMask == R5G6B5_MASKS[2] && alphaMask == R5G6B5_MASKS[3])
{
// R5G6B5
return R5G6B5;
}
else
{
return -1;
// bad
}
}
case 24:
{
if (redMask == R8G8B8_MASKS[0] && greenMask == R8G8B8_MASKS[1] && blueMask == R8G8B8_MASKS[2] && alphaMask == R8G8B8_MASKS[3])
{
// R8G8B8
return R8G8B8;
}
else
{
return -1;
// bad
}
}
case 32:
{
if (redMask == A8B8G8R8_MASKS[0] && greenMask == A8B8G8R8_MASKS[1] && blueMask == A8B8G8R8_MASKS[2] && alphaMask == A8B8G8R8_MASKS[3])
{
// A8B8G8R8
return A8B8G8R8;
}
else if (redMask == X8B8G8R8_MASKS[0] && greenMask == X8B8G8R8_MASKS[1] && blueMask == X8B8G8R8_MASKS[2] && alphaMask == X8B8G8R8_MASKS[3])
{
// X8B8G8R8
return X8B8G8R8;
}
else if (redMask == A8R8G8B8_MASKS[0] && greenMask == A8R8G8B8_MASKS[1] && blueMask == A8R8G8B8_MASKS[2] && alphaMask == A8R8G8B8_MASKS[3])
{
// A8R8G8B8
return A8R8G8B8;
}
else if (redMask == X8R8G8B8_MASKS[0] && greenMask == X8R8G8B8_MASKS[1] && blueMask == X8R8G8B8_MASKS[2] && alphaMask == X8R8G8B8_MASKS[3])
{
// X8R8G8B8
return X8R8G8B8;
}
else
{
return -1;
// bad
}
}
}
return -1;
}
static int Bit5Convert(int input)
{
float col = input;
col = Mathf.Min(col / 31, 1) * 255;
return (int)col;
}
static int Bit6Convert(int input)
{
float col = input;
col = Mathf.Min(col / 63, 1) * 255;
return (int)col;
}
// converts 8 bit color to 4 bit color
static int Color4bit(int input)
{
return input >> 4;
}
static Color32 GetDXTColor1(int c0, int a)
{
Color32 col = new Color32();
col.a = (byte)a;
col.r = (byte)Bit5Convert((c0 & 0xFC00) >> 11);
col.g = (byte)Bit6Convert((c0 & 0x07E0) >> 5);
col.b = (byte)Bit5Convert(c0 & 0x001F);
return col;
}
static Color32 GetDXTColor2(int c0, int c1, int a)
{
Color32 col = new Color32();
col.r = (byte)((2 * Bit5Convert((c0 & 0xFC00) >> 11) + Bit5Convert((c1 & 0xFC00) >> 11)) / 3);
col.g = (byte)((2 * Bit6Convert((c0 & 0x07E0) >> 5) + Bit5Convert((c1 & 0x07E0) >> 5)) / 3);
col.b = (byte)((2 * Bit5Convert(c0 & 0x001F) + Bit5Convert(c1 & 0x001F)) / 3);
col.a = (byte)a;
return col;
}
static Color32 GetDXTColor3(int c0, int c1, int a)
{
Color32 col = new Color32();
col.r = (byte)((Bit5Convert((c0 & 0xFC00) >> 11) + Bit5Convert((c1 & 0xFC00) >> 11)) / 2);
col.g = (byte)((Bit6Convert((c0 & 0x07E0) >> 5) + Bit5Convert((c1 & 0x07E0) >> 5)) / 2);
col.b = (byte)((Bit5Convert(c0 & 0x001F) + Bit5Convert(c1 & 0x001F)) / 2);
col.a = (byte)a;
return col;
}
static Color32 GetDXTColor(int c0, int c1, int a, int t)
{
switch (t)
{
case 0:
{
return GetDXTColor1(c0, a);
}
case 1:
{
return GetDXTColor1(c1, a);
}
case 2:
{
return c0 > c1 ? GetDXTColor2(c0, c1, a) : GetDXTColor3(c0, c1, a);
}
case 3:
{
return c0 > c1 ? GetDXTColor2(c1, c0, a) : new Color32(0, 0, 0, 0);
}
}
return new Color32(0, 0, 0, 0);
}
static void DecodeDXT1(int width, int height, Texture2D tex, byte[] data)
{
Color32[] pixels = new Color32[width * height];
int index = 128;
// calculate number of rows and columns, aligned to 4. mandatory for dds format
int w = (width + 3) / 4;
int h = (height + 3) / 4;
for (int i = 0; i < h; i++)
{
for (int i2 = 0; i2 < w; i2++)
{
// read color0 and color1, which are both 16 bit (r5, g6, b5) colors
int c0 = data[index] | (data[index + 1] << 8);
index += 2;
int c1 = data[index] | (data[index + 1] << 8);
index += 2;
for (int i3 = 0; i3 < 4; i3++)
{
// 4 more bytes telling us what color mixing method to use for each pixel
if (4 * i + i3 >= height) break;
int t0 = (data[index] & 0x03);
int t1 = (data[index] & 0x0C) >> 2;
int t2 = (data[index] & 0x30) >> 4;
int t3 = (data[index++] & 0xC0) >> 6;
// generate colors from the functions
pixels[4 * width * i + 4 * i2 + width * i3] = GetDXTColor(c0, c1, 255, t0);
if (4 * i2 + 1 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 1] = GetDXTColor(c0, c1, 255, t1);
if (4 * i2 + 2 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 2] = GetDXTColor(c0, c1, 255, t2);
if (4 * i2 + 3 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 3] = GetDXTColor(c0, c1, 255, t3);
}
}
}
tex.SetPixels32(pixels);
// we now have our colors, let's convert them to rgba4444
/*ushort[] bit16col = new ushort[width * height];
for (int i = 0; i < width * height; i++)
{
bit16col[i] = (ushort)((Color4bit(pixels[i].r) << 12) | (Color4bit(pixels[i].g) << 8) | (Color4bit(pixels[i].b) << 4) | (Color4bit(pixels[i].a)));
}
byte[] pntr = new byte[width * height * 2];
System.Buffer.BlockCopy(bit16col, 0, pntr, 0, width * height * 2);
tex.LoadRawTextureData(pntr);*/
}
static void DecodeDXT3(int width, int height, Texture2D tex, byte[] data)
{
Color32[] pixels = new Color32[width * height];
int index = 128;
// calculate number of rows and columns, aligned to 4. mandatory for dds format
int w = (width + 3) / 4;
int h = (height + 3) / 4;
int[] alphaTable = new int[16];
for (int i = 0; i < h; i++)
{
for (int i2 = 0; i2 < w; i2++)
{
// create alpha table
for (int i3 = 0; i3 < 4; i3++)
{
int a0 = data[index++];
int a1 = data[index++];
// 4 bit to 8 bit conversion
alphaTable[4 * i3 + 0] = 17 * ((a0 & 0xF0) >> 4);
alphaTable[4 * i3 + 1] = 17 * (a0 & 0x0F);
alphaTable[4 * i3 + 2] = 17 * ((a1 & 0xF0) >> 4);
alphaTable[4 * i3 + 3] = 17 * (a1 & 0x0F);
}
// read color0 and color1, which are both 16 bit (r5, g6, b5) colors
int c0 = data[index] | (data[index + 1] << 8);
index += 2;
int c1 = data[index] | (data[index + 1] << 8);
index += 2;
for (int i3 = 0; i3 < 4; i3++)
{
// 4 more bytes telling us what color mixing method to use for each pixel
if (4 * i + i3 >= height) break;
int t0 = (data[index] & 0x03);
int t1 = (data[index] & 0x0C) >> 2;
int t2 = (data[index] & 0x30) >> 4;
int t3 = (data[index++] & 0xC0) >> 6;
// generate colors from the functions
pixels[4 * width * i + 4 * i2 + width * i3] = GetDXTColor(c0, c1, alphaTable[4 * i3], t0);
if (4 * i2 + 1 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 1] = GetDXTColor(c0, c1, alphaTable[4 * i3 + 1], t1);
if (4 * i2 + 2 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 2] = GetDXTColor(c0, c1, alphaTable[4 * i3 + 2], t2);
if (4 * i2 + 3 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 3] = GetDXTColor(c0, c1, alphaTable[4 * i3 + 3], t3);
}
}
}
tex.SetPixels32(pixels);
// we now have our colors, let's convert them to rgba4444
/*ushort[] bit16col = new ushort[width * height];
for (int i = 0; i < width * height; i++)
{
bit16col[i] = (ushort)((Color4bit(pixels[i].r) << 12) | (Color4bit(pixels[i].g) << 8) | (Color4bit(pixels[i].b) << 4) | (Color4bit(pixels[i].a)));
}
byte[] pntr = new byte[width * height * 2];
System.Buffer.BlockCopy(bit16col, 0, pntr, 0, width * height * 2);
tex.LoadRawTextureData(pntr);*/
}
static int GetAlpha(int a0, int a1, int b)
{
if (a0 > a1) switch (b)
{
case 0: return a0;
case 1: return a1;
case 2: return (6 * a0 + a1) / 7;
case 3: return (5 * a0 + 2 * a1) / 7;
case 4: return (4 * a0 + 3 * a1) / 7;
case 5: return (3 * a0 + 4 * a1) / 7;
case 6: return (2 * a0 + 5 * a1) / 7;
case 7: return (a0 + 6 * a1) / 7;
}
else switch (b)
{
case 0: return a0;
case 1: return a1;
case 2: return (4 * a0 + a1) / 5;
case 3: return (3 * a0 + 2 * a1) / 5;
case 4: return (2 * a0 + 3 * a1) / 5;
case 5: return (a0 + 4 * a1) / 5;
case 6: return 0;
case 7: return 255;
}
return 0;
}
static void DecodeDXT5(int width, int height, Texture2D tex, byte[] data)
{
Color32[] pixels = new Color32[width * height];
int index = 128;
// calculate number of rows and columns, aligned to 4. mandatory for dds format
int w = (width + 3) / 4;
int h = (height + 3) / 4;
int[] alphaTable = new int[16];
for (int i = 0; i < h; i++)
{
for (int i2 = 0; i2 < w; i2++)
{
// create alpha table
int a0 = data[index++];
int a1 = data[index++];
int b0 = data[index] | (data[index + 1] << 8) | (data[index + 2] << 16);
index += 3;
int b1 = data[index] | (data[index + 1] << 8) | (data[index + 2] << 16);
index += 3;
alphaTable[0] = b0 & 0x07;
alphaTable[1] = (b0 >> 3) & 0x07;
alphaTable[2] = (b0 >> 6) & 0x07;
alphaTable[3] = (b0 >> 9) & 0x07;
alphaTable[4] = (b0 >> 12) & 0x07;
alphaTable[5] = (b0 >> 15) & 0x07;
alphaTable[6] = (b0 >> 18) & 0x07;
alphaTable[7] = (b0 >> 21) & 0x07;
alphaTable[8] = b1 & 0x07;
alphaTable[9] = (b1 >> 3) & 0x07;
alphaTable[10] = (b1 >> 6) & 0x07;
alphaTable[11] = (b1 >> 9) & 0x07;
alphaTable[12] = (b1 >> 12) & 0x07;
alphaTable[13] = (b1 >> 15) & 0x07;
alphaTable[14] = (b1 >> 18) & 0x07;
alphaTable[15] = (b1 >> 21) & 0x07;
// read color0 and color1, which are both 16 bit (r5, g6, b5) colors
int c0 = data[index] | (data[index + 1] << 8);
index += 2;
int c1 = data[index] | (data[index + 1] << 8);
index += 2;
for (int i3 = 0; i3 < 4; i3++)
{
// 4 more bytes telling us what color mixing method to use for each pixel
if (4 * i + i3 >= height) break;
int t0 = (data[index] & 0x03);
int t1 = (data[index] & 0x0C) >> 2;
int t2 = (data[index] & 0x30) >> 4;
int t3 = (data[index++] & 0xC0) >> 6;
// generate colors from the functions
pixels[4 * width * i + 4 * i2 + width * i3] = GetDXTColor(c0, c1, GetAlpha(a0, a1, alphaTable[4 * i3]), t0);
if (4 * i2 + 1 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 1] = GetDXTColor(c0, c1, GetAlpha(a0, a1, alphaTable[4 * i3]), t1);
if (4 * i2 + 2 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 2] = GetDXTColor(c0, c1, GetAlpha(a0, a1, alphaTable[4 * i3]), t2);
if (4 * i2 + 3 >= width) continue;
pixels[4 * width * i + 4 * i2 + width * i3 + 3] = GetDXTColor(c0, c1, GetAlpha(a0, a1, alphaTable[4 * i3]), t3);
}
}
}
tex.SetPixels32(pixels);
// we now have our colors, let's convert them to rgba4444
/*ushort[] bit16col = new ushort[width * height];
for (int i = 0; i < width * height; i++)
{
bit16col[i] = (ushort)((Color4bit(pixels[i].r) << 12) | (Color4bit(pixels[i].g) << 8) | (Color4bit(pixels[i].b) << 4) | (Color4bit(pixels[i].a)));
}
byte[] pntr = new byte[width * height * 2];
System.Buffer.BlockCopy(bit16col, 0, pntr, 0, width * height * 2);
tex.LoadRawTextureData(pntr);*/
}
public static Texture2D Load(string filename)
{
// open file, return null if failed to open
FileStream fs = null;
if (!File.Exists(filename))
{
return null;
}
try
{
fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch
{
return null;
}
fs.Seek(0, SeekOrigin.End);
datare = new byte[fs.Position];
fs.Seek(0, SeekOrigin.Begin);
fs.Read(datare, 0, datare.Length);
fs.Close();
Texture2D tex = new Texture2D(GetWidth(datare), GetHeight(datare));
int type = GetType(datare);
switch (type)
{
case DXT1:
{
DecodeDXT1(GetWidth(datare), GetHeight(datare), tex, datare);
}
break;
case DXT2:
case DXT3:
{
DecodeDXT3(GetWidth(datare), GetHeight(datare), tex, datare);
}
break;
case DXT4:
case DXT5:
{
DecodeDXT5(GetWidth(datare), GetHeight(datare), tex, datare);
}
break;
// TODO: uncompressed formats
}
datare = null;
tex.Apply();
return tex;
}
}