From 5c81970558c1cf65be05f7c389fd8b06341b5a67 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 23 Sep 2022 17:59:07 +0200 Subject: [PATCH] Handle weird mip map/size inconsistencies, maybe? --- Penumbra/Import/Textures/TexFileParser.cs | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Penumbra/Import/Textures/TexFileParser.cs b/Penumbra/Import/Textures/TexFileParser.cs index 1d416f02..7c0afbb5 100644 --- a/Penumbra/Import/Textures/TexFileParser.cs +++ b/Penumbra/Import/Textures/TexFileParser.cs @@ -24,12 +24,37 @@ public static class TexFileParser throw new Exception( $"Could not obtain dimensionality from {header.Type}." ); } + meta.MipLevels = CountMipLevels( data, in header ); + var scratch = ScratchImage.Initialize( meta ); + CopyData( scratch, r ); return scratch; } + private static unsafe int CountMipLevels( Stream data, in TexFile.TexHeader header ) + { + var lastOffset = 80u; + var levels = 1; + for( var i = 1; i < 13; ++i ) + { + var offset = header.OffsetToSurface[ i ]; + var diff = offset - lastOffset; + if( offset > 0 && offset < data.Length && diff > 0 ) + { + lastOffset = offset; + ++levels; + } + else + { + return levels; + } + } + + return 13; + } + private static unsafe void CopyData( ScratchImage image, BinaryReader r ) { fixed( byte* ptr = image.Pixels )