Some texfile formatting.

This commit is contained in:
Ottermandias 2023-08-01 13:11:57 +02:00
parent a95877b9e4
commit 2da6a33a62

View file

@ -15,20 +15,14 @@ public static class TexFileParser
var meta = header.ToTexMeta();
if (meta.Format == DXGIFormat.Unknown)
{
throw new Exception($"Could not convert format {header.Format} to DXGI Format.");
}
if (meta.Dimension == TexDimension.Unknown)
{
throw new Exception($"Could not obtain dimensionality from {header.Type}.");
}
meta.MipLevels = CountMipLevels(data, in meta, in header);
if (meta.MipLevels == 0)
{
throw new Exception("Could not load file. Image is corrupted and does not contain enough data for its size.");
}
var scratch = ScratchImage.Initialize(meta);
@ -50,21 +44,15 @@ public static class TexFileParser
{
var offset = header.OffsetToSurface[i];
if (offset == 0)
{
return i;
}
var requiredSize = width * height * bits / 8;
if (offset + requiredSize > data.Length)
{
return i;
}
var diff = offset - lastOffset;
if (diff != lastSize)
{
return i;
}
width = Math.Max(width / 2, minSize);
height = Math.Max(height / 2, minSize);
@ -82,11 +70,9 @@ public static class TexFileParser
var span = new Span<byte>(ptr, image.Pixels.Length);
var readBytes = r.Read(span);
if (readBytes < image.Pixels.Length)
{
throw new Exception($"Invalid data length {readBytes} < {image.Pixels.Length}.");
}
}
}
public static void Write(this TexFile.TexHeader header, BinaryWriter w)
{
@ -95,18 +81,17 @@ public static class TexFileParser
w.Write(header.Width);
w.Write(header.Height);
w.Write(header.Depth);
w.Write( header.MipLevels );
w.Write((byte) header.MipLevels);
w.Write((byte) 0); // TODO Lumina Update
unsafe
{
w.Write(header.LodOffset[0]);
w.Write(header.LodOffset[1]);
w.Write(header.LodOffset[2]);
for (var i = 0; i < 13; ++i)
{
w.Write(header.OffsetToSurface[i]);
}
}
}
public static TexFile.TexHeader ToTexHeader(this ScratchImage scratch)
{
@ -116,7 +101,7 @@ public static class TexFileParser
Height = (ushort)meta.Height,
Width = (ushort)meta.Width,
Depth = (ushort)Math.Max(meta.Depth, 1),
MipLevels = ( ushort )Math.Min( meta.MipLevels, 12 ),
MipLevels = (byte)Math.Min(meta.MipLevels, 12),
Format = meta.Format.ToTexFormat(),
Type = meta.Dimension switch
{
@ -146,9 +131,7 @@ public static class TexFileParser
}
for (; idx < 13; ++idx)
{
header.OffsetToSurface[idx] = 0;
}
header.LodOffset[0] = 0;
header.LodOffset[1] = 1;