ソースを参照

improved KTX2 header reading. Fixes #188

vpenades 2 年 前
コミット
447b35056f
1 ファイル変更6 行追加3 行削除
  1. 6 3
      src/SharpGLTF.Core/Memory/MemoryImage.cs

+ 6 - 3
src/SharpGLTF.Core/Memory/MemoryImage.cs

@@ -470,6 +470,7 @@ namespace SharpGLTF.Memory
         #endregion
     }
 
+    [System.Diagnostics.DebuggerDisplay("{PixelWidth}x{PixelHeight}x{PixelDepth}")]
     readonly struct Ktx2Header
     {
         // http://github.khronos.org/KTX-Specification/
@@ -487,10 +488,12 @@ namespace SharpGLTF.Memory
         public readonly UInt32 LevelCount;
         public readonly UInt32 SupercompressionScheme;
 
-        public static bool TryGetHeader(IReadOnlyList<Byte> data, out Ktx2Header header)
+        public static unsafe bool TryGetHeader(IReadOnlyList<Byte> data, out Ktx2Header header)
         {
-            if (data.Count < 12) { header = default; return false; }
-            header = System.Runtime.InteropServices.MemoryMarshal.Cast<Byte, Ktx2Header>(data.ToArray())[0];
+            if (!(data is Byte[] array)) array = data?.ToArray() ?? Array.Empty<Byte>();
+
+            if (data.Count < sizeof(Ktx2Header)) { header = default; return false; }
+            header = System.Runtime.InteropServices.MemoryMarshal.Cast<Byte, Ktx2Header>(array)[0];
             return true;
         }