-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextureConverter.cs
466 lines (394 loc) · 18.5 KB
/
TextureConverter.cs
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
using ManagedSquish;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using System.Drawing;
namespace Melt
{
class TextureConverter
{
static public void folderToPNG(string path)
{
if (!Directory.Exists(path))
{
Console.WriteLine("Unable to open {0}", path);
return;
}
string[] fileEntries = Directory.GetFiles(path);
foreach (string fileName in fileEntries)
toPNG(fileName);
Console.WriteLine("Done");
}
static public void folderBMPToPNG(string path)
{
if (!Directory.Exists(path))
{
Console.WriteLine("Unable to open {0}", path);
return;
}
string[] fileEntries = Directory.GetFiles(path);
foreach (string fileName in fileEntries)
BMPtoPNG(fileName);
Console.WriteLine("Done");
}
static public void toBin(string filename, uint fileid, uint format)
{
try
{
Console.WriteLine("Converting to Bin...");
//string outputFileName = filename.Replace(".png", "_new.bin");
//string[] splitFilename = filename.Split('\\');
string outputFileName = fileid.ToString("x8") + ".bin";
StreamWriter outputFile = new StreamWriter(new FileStream(outputFileName, FileMode.Create, FileAccess.Write));
if (outputFile == null)
{
Console.WriteLine("Unable to open {0}", outputFileName);
return;
}
Bitmap inputBMP = new Bitmap(filename);
uint width = (uint)inputBMP.Size.Width;
uint height = (uint)inputBMP.Size.Height;
uint lenght = 0;
switch (format)
{
case 21:
{
lenght = width * height * 4;
outputFile.BaseStream.Write(BitConverter.GetBytes((uint)fileid), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes((uint)2), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(width), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(height), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(format), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(lenght), 0, 4);
outputFile.Flush();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixel = inputBMP.GetPixel(x, y);
outputFile.BaseStream.Write(BitConverter.GetBytes(pixel.B), 0, 1);
outputFile.BaseStream.Write(BitConverter.GetBytes(pixel.G), 0, 1);
outputFile.BaseStream.Write(BitConverter.GetBytes(pixel.R), 0, 1);
outputFile.BaseStream.Write(BitConverter.GetBytes(pixel.A), 0, 1);
outputFile.Flush();
}
}
break;
}
case 244:
{
lenght = width * height;
outputFile.BaseStream.Write(BitConverter.GetBytes((uint)fileid), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes((uint)2), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(width), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(height), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(format), 0, 4);
outputFile.BaseStream.Write(BitConverter.GetBytes(lenght), 0, 4);
outputFile.Flush();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixel = inputBMP.GetPixel(x, y);
outputFile.BaseStream.Write(BitConverter.GetBytes(pixel.A), 0, 1);
outputFile.Flush();
}
}
break;
}
default:
{
Console.WriteLine("Unsupported texture type: {0}", format);
break;
}
}
outputFile.Close();
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine("Unable to open {0}", filename);
}
Console.WriteLine("Done");
}
static public void BMPtoPNG(string filename)
{
if (!filename.Contains(".bmp"))
return;
Bitmap bmp = new Bitmap(filename);
string outputName = filename.Replace("bmp", "png");
bmp.Save(outputName, System.Drawing.Imaging.ImageFormat.Png);
}
static public void toPNG(string filename)
{
StreamReader inputFile = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
if (inputFile == null)
{
Console.WriteLine("Unable to open {0}", filename);
return;
}
Console.WriteLine("Converting to PNG...");
byte[] buffer = new byte[1024];
uint fileHeader = Utils.ReadUInt32(buffer, inputFile);
uint textureType = Utils.ReadUInt32(buffer, inputFile);
uint width = Utils.ReadUInt32(buffer, inputFile);
uint height = Utils.ReadUInt32(buffer, inputFile);
uint format = Utils.ReadUInt32(buffer, inputFile);
uint length = Utils.ReadUInt32(buffer, inputFile);
if (textureType != 8)
return;
switch (format)
{
case 20: //D3DFMT_R8G8B8
{
Bitmap bmp = new Bitmap((int)width, (int)height);
byte r;
byte g;
byte b;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
b = Utils.ReadByte(buffer, inputFile);
g = Utils.ReadByte(buffer, inputFile);
r = Utils.ReadByte(buffer, inputFile);
bmp.SetPixel(x, y, Color.FromArgb(255, r, g, b));
}
}
bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
case 21: //D3DFMT_A8R8G8B8
{
Bitmap bmp = new Bitmap((int)width, (int)height);
byte a;
byte r;
byte g;
byte b;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
b = Utils.ReadByte(buffer, inputFile);
g = Utils.ReadByte(buffer, inputFile);
r = Utils.ReadByte(buffer, inputFile);
a = Utils.ReadByte(buffer, inputFile);
bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
}
}
bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
case 244:
{
Bitmap bmp = new Bitmap((int)width, (int)height);
byte a;
byte r;
byte g;
byte b;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
b = 255;
g = 255;
r = 255;
a = Utils.ReadByte(buffer, inputFile);
bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
}
}
bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
default:
if (textureType == 8)
{
byte[] buffer2 = new byte[(int)length];
byte[] data = Utils.ReadBytes(buffer2, inputFile, (int)length);
byte[] data2 = Squish.DecompressImage(data, (int)width, (int)height, SquishFlags.Dxt1);
Bitmap bmp = new Bitmap((int)width, (int)height);
byte r;
byte g;
byte b;
byte a;
int i = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
r = data2[i++];
g = data2[i++];
b = data2[i++];
a = data2[i++];
bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
}
}
bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
else
{
Console.WriteLine("Unsupported texture type: {0}", format);
break;
}
}
}
static public void darkMajestyfolderToPNG(string path)
{
if (!Directory.Exists(path))
{
Console.WriteLine("Unable to open {0}", path);
return;
}
string[] fileEntries = Directory.GetFiles(path);
foreach (string fileName in fileEntries)
darkMajestyToPNG(fileName);
Console.WriteLine("Done");
}
static public void darkMajestyToPNG(string filename)
{
StreamReader inputFile = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
if (inputFile == null)
{
Console.WriteLine("Unable to open {0}", filename);
return;
}
Console.WriteLine("Converting to PNG...");
byte[] buffer = new byte[1024];
uint fileHeader = Utils.ReadUInt32(buffer, inputFile);
uint textureType = Utils.ReadUInt32(buffer, inputFile);
uint width = Utils.ReadUInt32(buffer, inputFile);
uint height = Utils.ReadUInt32(buffer, inputFile);
switch (textureType)
{
case 4:
{
Bitmap bmp = new Bitmap((int)width, (int)height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
byte rgb = Utils.ReadByte(buffer, inputFile);
byte alpha = Utils.ReadByte(buffer, inputFile);
bmp.SetPixel(x, y, Color.FromArgb(alpha, rgb, rgb, rgb));
}
}
bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
case 10:
{
Bitmap bmp = new Bitmap((int)width, (int)height);
List<byte> r = new List<byte>();
List<byte> g = new List<byte>();
List<byte> b = new List<byte>();
uint sizePerColor = width * height;
for (int i = 0; i < sizePerColor; i++)
r.Add(Utils.ReadByte(buffer, inputFile));
for (int i = 0; i < sizePerColor; i++)
g.Add(Utils.ReadByte(buffer, inputFile));
for (int i = 0; i < sizePerColor; i++)
b.Add(Utils.ReadByte(buffer, inputFile));
int count = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
bmp.SetPixel(x, y, Color.FromArgb(r[count], g[count], b[count]));
count++;
}
}
bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
case 11:
{
Bitmap bmp = new Bitmap((int)width, (int)height);
List<byte> a = new List<byte>();
List<byte> r = new List<byte>();
List<byte> g = new List<byte>();
List<byte> b = new List<byte>();
uint sizePerColor = width * height;
for (int i = 0; i < sizePerColor; i++)
a.Add(Utils.ReadByte(buffer, inputFile));
for (int i = 0; i < sizePerColor; i++)
r.Add(Utils.ReadByte(buffer, inputFile));
for (int i = 0; i < sizePerColor; i++)
g.Add(Utils.ReadByte(buffer, inputFile));
for (int i = 0; i < sizePerColor; i++)
b.Add(Utils.ReadByte(buffer, inputFile));
int count = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
bmp.SetPixel(x, y, Color.FromArgb(a[count],r[count], g[count], b[count]));
count++;
}
}
bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
default:
Console.WriteLine("{0}: Unsupported texture type: {1}", filename, textureType);
break;
}
inputFile.Close();
Console.WriteLine("Done");
}
}
}
//case 7:
//{ //RGB555
// //reverse = WORD pixel555 = (red << 10) | (green << 5) | blue;
// Bitmap bmp = new Bitmap(width, height);
// ushort redMask = 0x7C00;
// ushort greenMask = 0x3E0;
// ushort blueMask = 0x1F;
// byte r, g, b;
// ushort pixelColor;
// for (int y = 0; y < height; y++)
// {
// for (int x = 0; x < width; x++)
// {
// pixelColor = (ushort)Utils.ReadShort(buffer, inputFile);
// r = (byte)((pixelColor & redMask) >> 10);
// g = (byte)((pixelColor & greenMask) >> 5);
// b = (byte)(pixelColor & blueMask);
// // Expand to 8-bit values.
// r = (byte)(r << 3);
// g = (byte)(g << 3);
// b = (byte)(b << 3);
// bmp.SetPixel(x, y, Color.FromArgb(r, g, b));
// }
// }
// bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
// break;
//}
//{ //RGB565
//reverse = WORD pixel565 = (red_value << 11) | (green_value << 5) | blue_value;
// Bitmap bmp = new Bitmap((int)width, (int)height);
// ushort redMask = 0xF800;
// ushort greenMask = 0x7E0;
// ushort blueMask = 0x1F;
// byte r, g, b;
// ushort pixelColor;
// for (int y = 0; y < height; y++)
// {
// for (int x = 0; x < width; x++)
// {
// pixelColor = (ushort)Utils.ReadShort(buffer, inputFile);
// r = (byte)((pixelColor & redMask) >> 11);
// g = (byte)((pixelColor & greenMask) >> 5);
// b = (byte)(pixelColor & blueMask);
// // Expand to 8-bit values.
// r = (byte)(r << 3);
// g = (byte)(g << 2);
// b = (byte)(b << 3);
// bmp.SetPixel(x, y, Color.FromArgb(r, g, b));
// }
// }
// bmp.Save(fileHeader.ToString("x8") + ".png", System.Drawing.Imaging.ImageFormat.Png);
// break;
//}