Change mipmap handling again.

This commit is contained in:
Ottermandias 2022-09-23 20:48:59 +02:00
parent cadaafb887
commit 3158d3da8c
2 changed files with 36 additions and 13 deletions

View file

@ -24,7 +24,7 @@ public static class TexFileParser
throw new Exception( $"Could not obtain dimensionality from {header.Type}." ); throw new Exception( $"Could not obtain dimensionality from {header.Type}." );
} }
meta.MipLevels = CountMipLevels( data, in header ); meta.MipLevels = CountMipLevels( data, in meta, in header );
var scratch = ScratchImage.Initialize( meta ); var scratch = ScratchImage.Initialize( meta );
@ -33,23 +33,38 @@ public static class TexFileParser
return scratch; return scratch;
} }
private static unsafe int CountMipLevels( Stream data, in TexFile.TexHeader header ) private static unsafe int CountMipLevels( Stream data, in TexMeta meta, in TexFile.TexHeader header )
{ {
var lastOffset = 80u; var width = meta.Width;
var levels = 1; var height = meta.Height;
for( var i = 1; i < 13; ++i ) var bits = meta.Format.BitsPerPixel();
var lastOffset = 0L;
var lastSize = 80L;
for( var i = 0; i < 13; ++i )
{ {
var offset = header.OffsetToSurface[ i ]; var offset = header.OffsetToSurface[ i ];
var diff = offset - lastOffset; if( offset == 0 )
if( offset > 0 && offset < data.Length && diff > 0 )
{ {
lastOffset = offset; return i;
++levels;
} }
else
var requiredSize = width * height * bits / 8;
if( offset + requiredSize > data.Length )
{ {
return levels; return i;
} }
var diff = offset - lastOffset;
if( diff != lastSize )
{
return i;
}
width = Math.Max( width / 2, 4 );
height = Math.Max( height / 2, 4 );
lastOffset = offset;
lastSize = requiredSize;
} }
return 13; return 13;

View file

@ -64,7 +64,11 @@ public partial class ModEditWindow
} }
ImGui.NewLine(); ImGui.NewLine();
tex.Draw( imageSize ); using var child2 = ImRaii.Child( "image" );
if( child2 )
{
tex.Draw( imageSize );
}
} }
private void SaveAsCombo() private void SaveAsCombo()
@ -157,7 +161,11 @@ public partial class ModEditWindow
ImGuiUtil.TextWrapped( _center.SaveException.ToString() ); ImGuiUtil.TextWrapped( _center.SaveException.ToString() );
} }
_center.Draw( imageSize ); using var child2 = ImRaii.Child( "image" );
if( child2 )
{
_center.Draw( imageSize );
}
} }
private Vector2 GetChildWidth() private Vector2 GetChildWidth()