From 337aa3e8c5bf9974fc35e30df04af86ed7930dcc Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:12:24 -0700 Subject: [PATCH] Eliminate allocation reading BMP header --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 13 ++++++++----- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 5 +++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index d10b05ce7..85461c0d2 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers.Binary; using System.IO; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Memory; @@ -473,12 +474,14 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadInfoHeader() { +#if NETCOREAPP2_1 + Span buffer = stackalloc byte[BmpInfoHeader.MaxHeaderSize]; +#else byte[] buffer = new byte[BmpInfoHeader.MaxHeaderSize]; +#endif + this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); // read the header size - // read header size - this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); - - int headerSize = BitConverter.ToInt32(buffer, 0); + int headerSize = BinaryPrimitives.ReadInt32LittleEndian(buffer); if (headerSize < BmpInfoHeader.CoreSize) { throw new NotSupportedException($"ImageSharp does not support this BMP file. HeaderSize: {headerSize}."); @@ -502,7 +505,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp else if (headerSize >= BmpInfoHeader.Size) { // >= 40 bytes - this.infoHeader = BmpInfoHeader.Parse(buffer.AsSpan(0, 40)); + this.infoHeader = BmpInfoHeader.Parse(buffer); } else { diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index a088a9b13..872c24286 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -132,6 +132,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// public static BmpInfoHeader Parse(ReadOnlySpan data) { + if (data.Length != Size) + { + throw new ArgumentException(nameof(data), $"Must be 40 bytes. Was {data.Length} bytes."); + } + return MemoryMarshal.Cast(data)[0]; }