diff --git a/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.Generated.cs b/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.Generated.cs index 055538df7a..a5b07c45f6 100644 --- a/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.Generated.cs +++ b/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.Generated.cs @@ -5,7 +5,6 @@ using System.Buffers.Binary; namespace SixLabors.ImageSharp.Formats.Jxl.IO; - /// /// Reads primitives from streams with correct endianness. /// diff --git a/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.tt b/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.tt index c83e9e4c4b..ba98aca4ad 100644 --- a/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.tt +++ b/src/ImageSharp/Formats/Jxl/IO/BinaryUtils.tt @@ -20,7 +20,6 @@ namespace SixLabors.ImageSharp.Formats.Jxl.IO; typeof(ulong) ]; #> - /// /// Reads primitives from streams with correct endianness. /// diff --git a/src/ImageSharp/Formats/Jxl/IO/Container/JxlBoxHeader.cs b/src/ImageSharp/Formats/Jxl/IO/Container/JxlBoxHeader.cs index dc5eb13513..93c0e992bc 100644 --- a/src/ImageSharp/Formats/Jxl/IO/Container/JxlBoxHeader.cs +++ b/src/ImageSharp/Formats/Jxl/IO/Container/JxlBoxHeader.cs @@ -2,6 +2,7 @@ // Licensed under the Six Labors Split License. using System.Buffers.Binary; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -13,6 +14,18 @@ namespace SixLabors.ImageSharp.Formats.Jxl.IO.Container; [StructLayout(LayoutKind.Sequential, Size = 16)] internal struct JxlBoxHeader { + private static readonly Dictionary KnownTypeCodes = new() + { + { 0x6A786C20, "jxl " }, + { 0x6A786C70, "jxlp" }, + { 0x6A786C63, "jxlc" }, + { 0x66747970, "ftyp" }, + { 0x6A627264, "jbrd" }, + { 0x45786966, "Exif" }, + { 0x786D6C20, "xml " }, + { 0x6A756D62, "jumb" } + }; + /// /// Box size in bytes. /// @@ -53,6 +66,7 @@ internal struct JxlBoxHeader /// /// Input type string to convert /// Unsigned integer representation of the type string + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint TypeFromString(string typeString) { if (typeString.Length != 4) @@ -60,10 +74,10 @@ internal struct JxlBoxHeader throw new ArgumentException("Box type must be exactly 4 characters", nameof(typeString)); } - Span buffer = stackalloc byte[4]; - _ = Encoding.ASCII.GetBytes(typeString, buffer); - - return BinaryPrimitives.ReadUInt32BigEndian(buffer); + return ((uint)typeString[0] << 24) | + ((uint)typeString[1] << 16) | + ((uint)typeString[2] << 8) | + typeString[3]; } /// @@ -73,6 +87,12 @@ internal struct JxlBoxHeader /// The string representing the type code. public static string TypeToString(uint typeCode) { + if (KnownTypeCodes.TryGetValue(typeCode, out string? str)) + { + return str; + } + + // The box type is not known Span buffer = stackalloc byte[4]; BinaryPrimitives.WriteUInt32BigEndian(buffer, typeCode);