-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNTexture.cs
More file actions
610 lines (509 loc) · 23.3 KB
/
NTexture.cs
File metadata and controls
610 lines (509 loc) · 23.3 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
* NTexture.cs / Semi-intelligent N64 texture converter
* Analyzes image to determine best possible N64 texture format (well, sort of)
* Written in 2011 by xdaniel
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
/*
* - Supported N64 texture formats:
* -> RGBA 16-bit, max. 2048 byte (ex. 64x32)
* -> CI 4-bit, max. 4096 byte (ex. 64x64)
* -> CI 8-bit, max. 2048 byte (ex. 64x32)
* -> IA 8-bit, max. 4096 byte (ex. 64x64)
* -> IA 16-bit, max. 2048 byte (ex. 64x32)
* -> I 4-bit, max. 4096 byte (ex. 128x64)
* -> I 8-bit, max. 4096 byte (ex. 64x64)
* -> RGBA 32-bit, max. 1024 byte (ex. 32x32)
*/
namespace SharpOcarina
{
public class NTexture
{
/// <summary>
/// Texture width in pixels
/// </summary>
public int Width;
/// <summary>
/// Texture height in pixels
/// </summary>
public int Height;
/// <summary>
/// N64-side texture type
/// </summary>
public byte Type;
/// <summary>
/// N64-side texture format
/// </summary>
public byte Format;
/// <summary>
/// N64-side texture size
/// </summary>
public byte Size;
/// <summary>
/// Converted N64 texture data
/// </summary>
public byte[] Data;
/// <summary>
/// Converted N64 palette data, if applicable
/// </summary>
public byte[] Palette;
/// <summary>
/// Address of texture in N64 memory map
/// </summary>
public uint TexOffset;
/// <summary>
/// Address of palette in N64 memory map
/// </summary>
public uint PalOffset;
/// <summary>
/// Is texture image grayscale? (set by CheckImageProperties)
/// </summary>
public bool IsGrayscale;
/// <summary>
/// Does texture image have alpha channel? (set by CheckImageProperties)
/// </summary>
public bool HasAlpha;
public string Name;
/// <summary>
/// Find and return all unique colors of a bitmap
/// </summary>
/// <param name="Image">Bitmap to get colors from</param>
/// <returns>List of unique colors</returns>
private List<Color> GetUniqueColors(Bitmap Image)
{
List<Color> Colors = new List<Color>();
for (int X = 0; X < Image.Width; ++X)
for (int Y = 0; Y < Image.Height; ++Y)
{
Colors.Add(Image.GetPixel(X, Y));
}
Colors = Colors.Distinct().ToList();
return Colors;
}
private int GetUniqueColorsCount(Bitmap Image)
{
List<Color> Colors = new List<Color>();
for (int X = 0; X < Image.Width; ++X)
for (int Y = 0; Y < Image.Height; ++Y)
{
Color c = Image.GetPixel(X, Y);
if (c.A != 0) Colors.Add(Color.FromArgb(1, c.R, c.G, c.B));
}
Colors = Colors.Distinct().ToList();
return Colors.Count;
}
private int GetUniqueColorsCountIA(Bitmap Image)
{
List<Color> Colors = new List<Color>();
for (int X = 0; X < Image.Width; ++X)
for (int Y = 0; Y < Image.Height; ++Y)
{
Color c = Image.GetPixel(X, Y);
c = Color.FromArgb(c.A, (int) (Math.Round((double)(c.R / 17)) * 17), (int) (Math.Round((double)(c.G / 17)) * 17), (int) (Math.Round((double)(c.B / 17)) * 17));
if (c.A != 0) Colors.Add(Color.FromArgb(1, c.R, c.G, c.B));
}
Colors = Colors.Distinct().ToList();
return Colors.Count;
}
private int GetUniqueAlphaCount(Bitmap Image)
{
List<Color> Colors = new List<Color>();
for (int X = 0; X < Image.Width; ++X)
for (int Y = 0; Y < Image.Height; ++Y)
{
Color c = Image.GetPixel(X, Y);
Colors.Add(Color.FromArgb(c.A, 0,0,0));
}
Colors = Colors.Distinct().ToList();
return Colors.Count;
}
/// <summary>
/// Checks given bitmap for alpha channel and if color or grayscale
/// </summary>
/// <param name="Image">Bitmap to check</param>
private void CheckImageProperties(Bitmap Image)
{
IsGrayscale = true;
HasAlpha = false;
Color Pixel;
for (int X = 0; X < Image.Width; ++X)
for (int Y = 0; Y < Image.Height; ++Y)
{
Pixel = Image.GetPixel(X, Y);
if (Pixel.R != Pixel.G || Pixel.R != Pixel.B || Pixel.G != Pixel.B)
IsGrayscale = false;
if (Pixel.A != 0xFF)
HasAlpha = true;
}
Width = Image.Width;
Height = Image.Height;
}
/// <summary>
/// Checks if the texture's size is valid
/// </summary>
/// <returns>True or False, depending on size validity</returns>
private bool IsSizeValid()
{
int[] ValidValues = new int[] { 8, 16, 32, 64, 128, 256, 512 };
if (Array.Find(ValidValues, element => element == Width) == 0) return false;
if (Array.Find(ValidValues, element => element == Height) == 0) return false;
return true;
}
/// <summary>
/// Converts 8-bit RGBA values into 16-bit RGBA5551 value
/// </summary>
/// <param name="R">8-bit Red channel</param>
/// <param name="G">8-bit Green channel</param>
/// <param name="B">8-bit Blue channel</param>
/// <param name="A">8-bit Alpha channel</param>
/// <returns>16-bit RGBA5551 value</returns>
private ushort ToRGBA5551(byte R, byte G, byte B, byte A)
{
return (ushort)((((R) << 8) & 0xF800) | (((G) << 3) & 0x7C0) | (((B) >> 2) & 0x3E) | (((A) >> 7) & 0x1));
}
/// <summary>
/// Generates N64 color palette from a list of colors
/// </summary>
/// <param name="Colors">List of color to convert</param>
/// <param name="ColorCount">Amount of colors</param>
/// <returns>Byte array containing N64 palette</returns>
private byte[] GeneratePalette(List<Color> Colors, int ColorCount)
{
byte[] Palette = new byte[ColorCount * 2];
List<ushort> PalEntries = new List<ushort>();
foreach (Color Col in Colors)
PalEntries.Add(ToRGBA5551(Col.R, Col.G, Col.B, Col.A));
PalEntries = PalEntries.Distinct().ToList();
for (int i = 0, j = 0; i < PalEntries.Count; i++, j += 2)
{
Palette[j] = (byte)(PalEntries[i] >> 8);
Palette[j + 1] = (byte)(PalEntries[i] & 0xFF);
}
return Palette;
}
/// <summary>
/// Find 16-bit RGBA5551 value in N64 color palette
/// </summary>
/// <param name="Palette">Byte array with N64 palette</param>
/// <param name="RGBA5551">16-bit RGBA5551 value to search</param>
/// <returns>Offset of color in N64 palette</returns>
private int GetPaletteIndex(byte[] Palette, ushort RGBA5551)
{
for (int i = 0; i < Palette.Length; i += 2)
{
if (RGBA5551 == (ushort)((Palette[i] << 8) | (Palette[i + 1]))) return (i / 2);
}
return -1;
}
/// <summary>
/// Converts given ObjFile.Material into N64 texture
/// </summary>
/// <param name="Material">Material to convert</param>
public void Convert(ObjFile.Material Material)
{
try
{
BitmapData RawBmp = null;
byte[] Raw = null;
IsGrayscale = false;
HasAlpha = false;
CheckImageProperties(Material.TexImage);
//TODO
if (IsSizeValid() == false)
{
if (!MainForm.settings.DisableTextureWarnings && Material.ForcedFormat == "") MessageBox.Show(string.Format("Invalid texture size {0}x{1}, converting to RGB format meanwhile for \"{2}\"", Width, Height, Material.map_Kd), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Material.ForceRGBA = true;
}
try
{
RawBmp = Material.TexImage.LockBits(
new Rectangle(0, 0, (int)Material.Width, (int)Material.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb
);
int TotalSize = RawBmp.Height * RawBmp.Stride;
Raw = new byte[TotalSize];
System.Runtime.InteropServices.Marshal.Copy(RawBmp.Scan0, Raw, 0, TotalSize);
}
finally
{
if (RawBmp != null)
Material.TexImage.UnlockBits(RawBmp);
}
//throw new Exception("Too many grayshades in texture OR invalid size");
List<Color> UniqueColors = GetUniqueColors(Material.TexImage);
int ColorCount = GetUniqueColorsCount(Material.TexImage);
int AlphaCount = GetUniqueAlphaCount(Material.TexImage);
// int ColorCountIA = GetUniqueColorsCountIA(Material.TexImage);
if (Material.ForcedFormat != "")
{
ConvertTexture(Material.ForcedFormat, Material, Raw, UniqueColors);
/*if (Material.ForcedFormat == "I4")
HasAlpha = true;*/
}
else if (IsGrayscale == true && Material.ForceRGBA == false && HasAlpha == true)
{
/* Convert to IA */
if (ColorCount <= 64 && AlphaCount <= 16 && (Material.Width*Material.Height) <= 4096)
{
#if DEBUG
DebugConsole.WriteLine("IA 8-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " grayshades");
#endif
ConvertTexture("IA8", Material, Raw);
}
else if (ColorCount <= 256 && Material.Width * Material.Height <= 2048)
{
#if DEBUG
DebugConsole.WriteLine("IA 16-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " grayshades");
#endif
ConvertTexture("IA16", Material, Raw);
}
else if (UniqueColors.Count <= 256 && Material.Width * Material.Height <= 4096)
{
#if DEBUG
DebugConsole.WriteLine("I 8-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " grayshades");
#endif
ConvertTexture("I8", Material, Raw);
}
/* Convert to I */
else if (UniqueColors.Count <= 16)
{
#if DEBUG
DebugConsole.WriteLine("I 4-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " grayshades");
#endif
ConvertTexture("I4", Material, Raw);
}
else
{
/* Uh-oh, too many grayshades OR invalid size! */
throw new Exception("Too many grayshades in texture OR invalid size");
}
}
else
{
/* Convert to CI */
if (UniqueColors.Count <= 16 && Material.Width * Material.Height <= 4096 && Material.ForceRGBA == false)
{
#if DEBUG
DebugConsole.WriteLine("CI 4-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " unique colors");
#endif
ConvertTexture("CI4", Material, Raw, UniqueColors);
// DebugConsole.WriteLine("Size: " + Data.Length);
// DebugConsole.WriteLine(Data[Data.Length-1]);
}
else if (UniqueColors.Count <= 256 && Material.Width * Material.Height <= 2048 && Material.ForceRGBA == false)
{
#if DEBUG
DebugConsole.WriteLine("CI 8-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " unique colors");
#endif
ConvertTexture("CI8", Material, Raw, UniqueColors);
}
else if (AlphaCount > 2 && Material.Width * Material.Height <= 1024 && Material.ForceRGBA == false && !MainForm.settings.DisableRGBA32)
{
/* Convert to RGBA */
#if DEBUG
DebugConsole.WriteLine("RGBA 32-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString());
#endif
ConvertTexture("RGBA32", Material, Raw);
}
else if (UniqueColors.Count <= 16 && IsGrayscale == true && Material.ForceRGBA == false)
{
#if DEBUG
DebugConsole.WriteLine("I 4-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " grayshades");
#endif
ConvertTexture("I4",Material, Raw);
}
else if (UniqueColors.Count <= 256 && IsGrayscale == true && Material.Width * Material.Height <= 4096 && Material.ForceRGBA == false)
{
#if DEBUG
DebugConsole.WriteLine("I 8-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString() + ", " + UniqueColors.Count.ToString() + " grayshades");
#endif
ConvertTexture("I8", Material, Raw);
}
else
{
/* Convert to RGBA */
#if DEBUG
DebugConsole.WriteLine("RGBA 16-bit <- " + Material.Name + ", " + Material.Width.ToString() + "*" + Material.Height.ToString());
#endif
ConvertTexture("RGBA16", Material, Raw);
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Material '" + Material.DisplayName + "': " + ex.Message, "Exception", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
SetInvalidTexture(Material);
}
/* Pack texture type */
Type = (byte)((Format << 5) | (Size << 3));
}
private void ConvertTexture(string type, ObjFile.Material Material, byte[] Raw, List<Color> UniqueColors = null)
{
if (type == "RGBA16" || type == "")
{
/* Set type, RGBA 16-bit */
Format = GBI.G_IM_FMT_RGBA;
Size = GBI.G_IM_SIZ_16b;
/* Generate texture buffer */
Data = new byte[Material.Width * Material.Height * 2 ];
Palette = null;
/* Loop through pixels, convert to RGBA5551, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length; i += 4, j += 2)
{
ushort RGBA5551 = ToRGBA5551(Raw[i + 2], Raw[i + 1], Raw[i], Raw[i + 3]);
Data[j] = (byte)(RGBA5551 >> 8);
Data[j + 1] = (byte)(RGBA5551 & 0xFF);
}
}
else if (type == "RGBA32")
{
/* Set type, RGBA 32-bit */
Format = GBI.G_IM_FMT_RGBA;
Size = GBI.G_IM_SIZ_32b;
/* Generate texture buffer */
Data = new byte[Material.Width * Material.Height * 4];
Palette = null;
/* Loop through pixels, convert to RGBA32, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length; i += 4, j += 4)
{
Data[j] = Raw[i + 2];
Data[j + 1] = Raw[i + 1];
Data[j + 2] = Raw[i];
Data[j + 3] = Raw[i + 3];
}
}
else if (type == "IA4")
{
/* Set type, IA 4-bit */
Format = GBI.G_IM_FMT_IA;
Size = GBI.G_IM_SIZ_4b;
/* Generate texture buffer */
Data = new byte[(Material.Width * Material.Height) / 2];
Palette = null;
/* Loop through pixels, convert to IA 4-bit, write to texture buffer */
for (int i = 0, j = 0; i+7 < Raw.Length; i += 8, j++)
{
byte a1 = (byte)((Raw[i + 3] > 127) ? 1 : 0);
byte a2 = (byte)((Raw[i + 7] > 127) ? 1 : 0);
byte g1 = (byte)((Raw[i] + Raw[i + 1] + Raw[i + 2]) / 3);
byte g2 = (byte)((Raw[i + 4] + Raw[i + 5] + Raw[i + 6]) / 3);
byte c1 = (byte)(g1 * 7 / 255);
byte c2 = (byte)(g2 * 7 / 255);
Data[j] = (byte)((c1 << 5) | (a1 << 4) | (c2 << 1) | a2);
}
}
else if (type == "IA8")
{
/* Set type, IA 8-bit */
Format = GBI.G_IM_FMT_IA;
Size = GBI.G_IM_SIZ_8b;
/* Generate texture buffer */
Data = new byte[Material.Width * Material.Height];
Palette = null;
/* Loop through pixels, convert to IA 8-bit, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length; i += 4, j++)
{
Data[j] = (byte)(((Raw[i] / 16) << 4) | ((Raw[i + 3] / 16) & 0xF));
}
}
else if (type == "IA16")
{
/* Set type, IA 16-bit */
Format = GBI.G_IM_FMT_IA;
Size = GBI.G_IM_SIZ_16b;
/* Generate texture buffer */
Data = new byte[Material.Width * Material.Height * 2];
Palette = null;
/* Loop through pixels, convert to IA 16-bit, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length; i += 4, j += 2)
{
Data[j] = Raw[i + 2];
Data[j + 1] = Raw[i + 3];
}
}
else if (type == "I4")
{
/* Set type, I 4-bit */
Format = GBI.G_IM_FMT_I;
Size = GBI.G_IM_SIZ_4b;
/* Generate texture buffer */
Data = new byte[(Material.Width * Material.Height) / 2];
Palette = null;
/* Loop through pixels, convert to I 4-bit, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length && j < Data.Length; i += 8, j++)
{
Data[j] = (byte)(((Raw[i] / 16) << 4) | ((Raw[i + 4] / 16) & 0xF));
}
}
else if (type == "I8")
{
/* Set type, I 8-bit */
Format = GBI.G_IM_FMT_I;
Size = GBI.G_IM_SIZ_8b;
/* Generate texture buffer */
Data = new byte[Material.Width * Material.Height];
Palette = null;
/* Loop through pixels, convert to I 8-bit, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length; i += 4, j++)
{
Data[j] = Raw[i];
}
}
else if (type == "CI4")
{
/* Set type, CI 4-bit */
Format = GBI.G_IM_FMT_CI;
Size = GBI.G_IM_SIZ_4b;
/* Generate texture buffer */
Data = new byte[(Material.Width * Material.Height) / 2];
/* Generate 16-color RGBA5551 palette */
Palette = GeneratePalette(UniqueColors, 16);
/* Loop through pixels, get palette indexes, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length; i += 8, j++)
{
ushort RGBA5551_1 = ToRGBA5551(Raw[i + 2], Raw[i + 1], Raw[i], Raw[i + 3]);
ushort RGBA5551_2 = ToRGBA5551(Raw[i + 6], Raw[i + 5], Raw[i + 4], Raw[i + 7]);
byte Value = (byte)(
((GetPaletteIndex(Palette, RGBA5551_1)) << 4) |
((GetPaletteIndex(Palette, RGBA5551_2) & 0xF)));
Data[j] = Value;
}
}
else if (type == "CI8")
{
/* Set type, CI 8-bit */
Format = GBI.G_IM_FMT_CI;
Size = GBI.G_IM_SIZ_8b;
/* Generate texture buffer */
Data = new byte[Material.Width * Material.Height];
/* Generate 256-color RGBA5551 palette */
Palette = GeneratePalette(UniqueColors, 256);
/* Loop through pixels, get palette indexes, write to texture buffer */
for (int i = 0, j = 0; i < Raw.Length; i += 4, j++)
{
ushort RGBA5551 = ToRGBA5551(Raw[i + 2], Raw[i + 1], Raw[i], Raw[i + 3]);
Data[j] = (byte)GetPaletteIndex(Palette, RGBA5551);
}
}
}
/// <summary>
/// Creates dummy texture
/// </summary>
/// <param name="Material">Material to use for texture parameters</param>
private void SetInvalidTexture(ObjFile.Material Material)
{
Format = GBI.G_IM_FMT_RGBA;
Size = GBI.G_IM_SIZ_16b;
Data = new byte[Material.Width * Material.Height * 2];
Palette = null;
}
}
}