From 3158d3da8c4458426808a2a7072e25172ac30999 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 23 Sep 2022 20:48:59 +0200 Subject: [PATCH] Change mipmap handling again. --- Penumbra/Import/Textures/TexFileParser.cs | 37 +++++++++++++------ Penumbra/UI/Classes/ModEditWindow.Textures.cs | 12 +++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Penumbra/Import/Textures/TexFileParser.cs b/Penumbra/Import/Textures/TexFileParser.cs index 7c0afbb5..f38d066e 100644 --- a/Penumbra/Import/Textures/TexFileParser.cs +++ b/Penumbra/Import/Textures/TexFileParser.cs @@ -24,7 +24,7 @@ public static class TexFileParser 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 ); @@ -33,23 +33,38 @@ public static class TexFileParser 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 levels = 1; - for( var i = 1; i < 13; ++i ) + var width = meta.Width; + var height = meta.Height; + var bits = meta.Format.BitsPerPixel(); + + var lastOffset = 0L; + var lastSize = 80L; + for( var i = 0; i < 13; ++i ) { var offset = header.OffsetToSurface[ i ]; - var diff = offset - lastOffset; - if( offset > 0 && offset < data.Length && diff > 0 ) + if( offset == 0 ) { - lastOffset = offset; - ++levels; + return i; } - 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; diff --git a/Penumbra/UI/Classes/ModEditWindow.Textures.cs b/Penumbra/UI/Classes/ModEditWindow.Textures.cs index 7d244dd6..03f984c0 100644 --- a/Penumbra/UI/Classes/ModEditWindow.Textures.cs +++ b/Penumbra/UI/Classes/ModEditWindow.Textures.cs @@ -64,7 +64,11 @@ public partial class ModEditWindow } ImGui.NewLine(); - tex.Draw( imageSize ); + using var child2 = ImRaii.Child( "image" ); + if( child2 ) + { + tex.Draw( imageSize ); + } } private void SaveAsCombo() @@ -157,7 +161,11 @@ public partial class ModEditWindow ImGuiUtil.TextWrapped( _center.SaveException.ToString() ); } - _center.Draw( imageSize ); + using var child2 = ImRaii.Child( "image" ); + if( child2 ) + { + _center.Draw( imageSize ); + } } private Vector2 GetChildWidth()