| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using Oni.Imaging; |
| 4 |
|
| 5 |
namespace Oni.Motoko |
| 6 |
{ |
| 7 |
internal static class TextureDatReader |
| 8 |
{ |
| 9 |
public static Texture ReadInfo(InstanceDescriptor txmp) |
| 10 |
{ |
| 11 |
var texture = new Texture |
| 12 |
{ |
| 13 |
Name = txmp.Name |
| 14 |
}; |
| 15 |
|
| 16 |
using (var reader = txmp.OpenRead(128)) |
| 17 |
{ |
| 18 |
texture.Flags = (TextureFlags)reader.ReadInt32(); |
| 19 |
texture.Width = reader.ReadInt16(); |
| 20 |
texture.Height = reader.ReadInt16(); |
| 21 |
texture.Format = (TextureFormat)reader.ReadInt32(); |
| 22 |
reader.Skip(8); |
| 23 |
|
| 24 |
if (txmp.IsMacFile) |
| 25 |
reader.Skip(4); |
| 26 |
|
| 27 |
reader.Skip(4); |
| 28 |
} |
| 29 |
|
| 30 |
return texture; |
| 31 |
} |
| 32 |
|
| 33 |
public static Texture Read(InstanceDescriptor txmp) |
| 34 |
{ |
| 35 |
var texture = new Texture |
| 36 |
{ |
| 37 |
Name = txmp.Name |
| 38 |
}; |
| 39 |
|
| 40 |
int rawOffset; |
| 41 |
|
| 42 |
using (var reader = txmp.OpenRead(128)) |
| 43 |
{ |
| 44 |
texture.Flags = (TextureFlags)reader.ReadInt32(); |
| 45 |
texture.Width = reader.ReadInt16(); |
| 46 |
texture.Height = reader.ReadInt16(); |
| 47 |
texture.Format = (TextureFormat)reader.ReadInt32(); |
| 48 |
reader.Skip(8); |
| 49 |
|
| 50 |
if (txmp.IsMacFile) |
| 51 |
reader.Skip(4); |
| 52 |
|
| 53 |
rawOffset = reader.ReadInt32(); |
| 54 |
} |
| 55 |
|
| 56 |
using (var rawReader = txmp.GetSepReader(rawOffset)) |
| 57 |
ReadSurfaces(texture, rawReader); |
| 58 |
|
| 59 |
return texture; |
| 60 |
} |
| 61 |
|
| 62 |
private static void ReadSurfaces(Texture texture, BinaryReader reader) |
| 63 |
{ |
| 64 |
var format = texture.Format.ToSurfaceFormat(); |
| 65 |
int width = texture.Width; |
| 66 |
int height = texture.Height; |
| 67 |
bool hasMipMaps = (texture.Flags & TextureFlags.HasMipMaps) != 0; |
| 68 |
|
| 69 |
do |
| 70 |
{ |
| 71 |
var surface = new Surface(width, height, format); |
| 72 |
reader.Read(surface.Data, 0, surface.Data.Length); |
| 73 |
texture.Surfaces.Add(surface); |
| 74 |
|
| 75 |
width = Math.Max(width >> 1, 1); |
| 76 |
height = Math.Max(height >> 1, 1); |
| 77 |
|
| 78 |
} while (hasMipMaps && (width > 1 || height > 1)); |
| 79 |
} |
| 80 |
} |
| 81 |
} |