From 19591271da9b67d7076309ab727af004180223ce Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Sat, 14 Apr 2018 20:38:58 -0700 Subject: [PATCH 01/10] Write the BmpFileHeader directly to the output span --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 27 +++++++------------ src/ImageSharp/Formats/Bmp/BmpFileHeader.cs | 26 +++++++++++++++++- .../Formats/Bmp/BmpFileHeaderTests.cs | 21 +++++++++++++++ 3 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index be7c1d2e5..aa8f2b8a5 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -14,6 +14,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// internal sealed class BmpEncoderCore { + /// + /// A general use buffer for reading and writing data. + /// + private byte[] buffer = new byte[16]; + /// /// The amount to pad each row by. /// @@ -75,30 +80,16 @@ namespace SixLabors.ImageSharp.Formats.Bmp reserved: 0, fileSize: 54 + infoHeader.ImageSize); - WriteHeader(writer, fileHeader); + fileHeader.WriteTo(this.buffer); + + stream.Write(this.buffer, 0, BmpFileHeader.Size); + this.WriteInfo(writer, infoHeader); this.WriteImage(writer, image.Frames.RootFrame); writer.Flush(); } - /// - /// Writes the bitmap header data to the binary stream. - /// - /// - /// The containing the stream to write to. - /// - /// - /// The containing the header data. - /// - private static void WriteHeader(EndianBinaryWriter writer, in BmpFileHeader fileHeader) - { - writer.Write(fileHeader.Type); - writer.Write(fileHeader.FileSize); - writer.Write(fileHeader.Reserved); - writer.Write(fileHeader.Offset); - } - /// /// Writes the bitmap information to the binary stream. /// diff --git a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs index ed17164a2..113dc0d47 100644 --- a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs @@ -1,6 +1,10 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; +using System.Buffers.Binary; +using System.Runtime.InteropServices; + namespace SixLabors.ImageSharp.Formats.Bmp { /// @@ -13,6 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// All of the other integer values are stored in little-endian format /// (i.e. least-significant byte first). /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] internal readonly struct BmpFileHeader { /// @@ -44,12 +49,31 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Gets any reserved data; actual value depends on the application /// that creates the image. /// - public int Reserved { get; } + public int Reserved { get; } /// /// Gets the offset, i.e. starting address, of the byte where /// the bitmap data can be found. /// public int Offset { get; } + + public unsafe void WriteTo(Span buffer) + { + if (BitConverter.IsLittleEndian) + { + fixed (BmpFileHeader* pointer = &this) + { + MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); + } + } + else + { + // Big Endian Platform + BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(0, 2), this.Type); + BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(2, 4), this.FileSize); + BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(6, 4), this.Reserved); + BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(10, 4), this.Offset); + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs new file mode 100644 index 000000000..8ad227cfd --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs @@ -0,0 +1,21 @@ +using System; +using SixLabors.ImageSharp.Formats.Bmp; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Bmp +{ + public class BmpFileHeaderTests + { + [Fact] + public void TestWrite() + { + var header = new BmpFileHeader(1, 2, 3, 4); + + byte[] buffer = new byte[14]; + + header.WriteTo(buffer); + + Assert.Equal("AQACAAAAAwAAAAQAAAA=", Convert.ToBase64String(buffer)); + } + } +} \ No newline at end of file From ceb4989c0828be958b41d4cfc6e4a345e93fd069 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Sat, 14 Apr 2018 21:00:02 -0700 Subject: [PATCH 02/10] Remove EndianBinaryWriter from BmpEncoderCore --- src/ImageSharp/Formats/Bmp/BmpCompression.cs | 2 +- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 66 ++++++-------------- src/ImageSharp/Formats/Bmp/BmpFileHeader.cs | 17 ++--- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 20 +++++- 4 files changed, 42 insertions(+), 63 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpCompression.cs b/src/ImageSharp/Formats/Bmp/BmpCompression.cs index 22b12346f..ef063f010 100644 --- a/src/ImageSharp/Formats/Bmp/BmpCompression.cs +++ b/src/ImageSharp/Formats/Bmp/BmpCompression.cs @@ -7,7 +7,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Defines how the compression type of the image data /// in the bitmap file. /// - internal enum BmpCompression + internal enum BmpCompression : int { /// /// Each image row has a multiple of four elements. If the diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index aa8f2b8a5..1e0ecd3b1 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -3,7 +3,6 @@ using System; using System.IO; -using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -14,11 +13,6 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// internal sealed class BmpEncoderCore { - /// - /// A general use buffer for reading and writing data. - /// - private byte[] buffer = new byte[16]; - /// /// The amount to pad each row by. /// @@ -59,9 +53,6 @@ namespace SixLabors.ImageSharp.Formats.Bmp int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32); this.padding = bytesPerLine - (image.Width * (int)this.bitsPerPixel); - // Do not use IDisposable pattern here as we want to preserve the stream. - var writer = new EndianBinaryWriter(Endianness.LittleEndian, stream); - var infoHeader = new BmpInfoHeader { HeaderSize = BmpInfoHeader.BitmapInfoHeaderSize, @@ -80,49 +71,30 @@ namespace SixLabors.ImageSharp.Formats.Bmp reserved: 0, fileSize: 54 + infoHeader.ImageSize); - fileHeader.WriteTo(this.buffer); + byte[] buffer = new byte[40]; // TODO: stackalloc - stream.Write(this.buffer, 0, BmpFileHeader.Size); + fileHeader.WriteTo(buffer); - this.WriteInfo(writer, infoHeader); - this.WriteImage(writer, image.Frames.RootFrame); + stream.Write(buffer, 0, BmpFileHeader.Size); - writer.Flush(); - } + infoHeader.WriteTo(buffer); - /// - /// Writes the bitmap information to the binary stream. - /// - /// - /// The containing the stream to write to. - /// - /// - /// The containing the detailed information about the image. - /// - private void WriteInfo(EndianBinaryWriter writer, BmpInfoHeader infoHeader) - { - writer.Write(infoHeader.HeaderSize); - writer.Write(infoHeader.Width); - writer.Write(infoHeader.Height); - writer.Write(infoHeader.Planes); - writer.Write(infoHeader.BitsPerPixel); - writer.Write((int)infoHeader.Compression); - writer.Write(infoHeader.ImageSize); - writer.Write(infoHeader.XPelsPerMeter); - writer.Write(infoHeader.YPelsPerMeter); - writer.Write(infoHeader.ClrUsed); - writer.Write(infoHeader.ClrImportant); + stream.Write(buffer, 0, 40); + + this.WriteImage(stream, image.Frames.RootFrame); + + stream.Flush(); } /// /// Writes the pixel data to the binary stream. /// /// The pixel format. - /// The containing the stream to write to. + /// The to write to. /// /// The containing pixel data. /// - private void WriteImage(EndianBinaryWriter writer, ImageFrame image) + private void WriteImage(Stream stream, ImageFrame image) where TPixel : struct, IPixel { using (PixelAccessor pixels = image.Lock()) @@ -130,11 +102,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp switch (this.bitsPerPixel) { case BmpBitsPerPixel.Pixel32: - this.Write32Bit(writer, pixels); + this.Write32Bit(stream, pixels); break; case BmpBitsPerPixel.Pixel24: - this.Write24Bit(writer, pixels); + this.Write24Bit(stream, pixels); break; } } @@ -149,9 +121,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Writes the 32bit color palette to the stream. /// /// The pixel format. - /// The containing the stream to write to. + /// The to write to. /// The containing pixel data. - private void Write32Bit(EndianBinaryWriter writer, PixelAccessor pixels) + private void Write32Bit(Stream stream, PixelAccessor pixels) where TPixel : struct, IPixel { using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 4)) @@ -160,7 +132,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { Span pixelSpan = pixels.GetRowSpan(y); PixelOperations.Instance.ToBgra32Bytes(pixelSpan, row.Span, pixelSpan.Length); - writer.Write(row.Array, 0, row.Length()); + stream.Write(row.Array, 0, row.Length()); } } } @@ -169,9 +141,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Writes the 24bit color palette to the stream. /// /// The pixel format. - /// The containing the stream to write to. + /// The to write to. /// The containing pixel data. - private void Write24Bit(EndianBinaryWriter writer, PixelAccessor pixels) + private void Write24Bit(Stream stream, PixelAccessor pixels) where TPixel : struct, IPixel { using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 3)) @@ -180,7 +152,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { Span pixelSpan = pixels.GetRowSpan(y); PixelOperations.Instance.ToBgr24Bytes(pixelSpan, row.Span, pixelSpan.Length); - writer.Write(row.Array, 0, row.Length()); + stream.Write(row.Array, 0, row.Length()); } } } diff --git a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs index 113dc0d47..64474ad90 100644 --- a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs @@ -59,21 +59,12 @@ namespace SixLabors.ImageSharp.Formats.Bmp public unsafe void WriteTo(Span buffer) { - if (BitConverter.IsLittleEndian) + fixed (BmpFileHeader* pointer = &this) { - fixed (BmpFileHeader* pointer = &this) - { - MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); - } - } - else - { - // Big Endian Platform - BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(0, 2), this.Type); - BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(2, 4), this.FileSize); - BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(6, 4), this.Reserved); - BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(10, 4), this.Offset); + MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); } + + // TODO: Big Endian Platforms } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index b24404cac..b38cfd450 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -1,5 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; +using System.Runtime.InteropServices; + namespace SixLabors.ImageSharp.Formats.Bmp { /// @@ -8,8 +11,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// the screen. /// /// - internal sealed class BmpInfoHeader + [StructLayout(LayoutKind.Sequential, Pack = 1)] + internal struct BmpInfoHeader { + // TODO: Make readonly + /// /// Defines the size of the BITMAPINFOHEADER data structure in the bitmap file. /// @@ -91,5 +97,15 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// or 0 when every color is important{ get; set; } generally ignored. /// public int ClrImportant { get; set; } + + public unsafe void WriteTo(Span buffer) + { + fixed (BmpInfoHeader* pointer = &this) + { + MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); + } + + // TODO: Big Endian Platforms + } } -} +} \ No newline at end of file From f9830e363667d02fc34ad28602d56a0f9cb31c88 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 16 Apr 2018 12:39:09 -0700 Subject: [PATCH 03/10] Move & simplify BmpHeader & BmpInfoHeader parsing logic --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 135 +++++------------- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 23 ++- src/ImageSharp/Formats/Bmp/BmpFileHeader.cs | 8 +- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 65 ++++++++- .../Formats/Bmp/IBmpDecoderOptions.cs | 7 +- .../Formats/Bmp/IBmpEncoderOptions.cs | 5 - src/ImageSharp/Formats/Bmp/ImageExtensions.cs | 2 - 7 files changed, 111 insertions(+), 134 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index dfbd44c04..f4a009dd2 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -34,29 +34,29 @@ namespace SixLabors.ImageSharp.Formats.Bmp private const int Rgb16BMask = 0x1F; /// - /// RLE8 flag value that indicates following byte has special meaning + /// RLE8 flag value that indicates following byte has special meaning. /// private const int RleCommand = 0x00; /// - /// RLE8 flag value marking end of a scan line + /// RLE8 flag value marking end of a scan line. /// private const int RleEndOfLine = 0x00; /// - /// RLE8 flag value marking end of bitmap data + /// RLE8 flag value marking end of bitmap data. /// private const int RleEndOfBitmap = 0x01; /// - /// RLE8 flag value marking the start of [x,y] offset instruction + /// RLE8 flag value marking the start of [x,y] offset instruction. /// private const int RleDelta = 0x02; /// /// The stream to decode from. /// - private Stream currentStream; + private Stream stream; /// /// The file header containing general information. @@ -261,7 +261,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp while (count < buffer.Length) { - if (this.currentStream.Read(cmd, 0, cmd.Length) != 2) + if (this.stream.Read(cmd, 0, cmd.Length) != 2) { throw new Exception("Failed to read 2 bytes from stream"); } @@ -283,8 +283,8 @@ namespace SixLabors.ImageSharp.Formats.Bmp break; case RleDelta: - int dx = this.currentStream.ReadByte(); - int dy = this.currentStream.ReadByte(); + int dx = this.stream.ReadByte(); + int dy = this.stream.ReadByte(); count += (w * dy) + dx; break; @@ -299,7 +299,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp length += length & 1; byte[] run = new byte[length]; - this.currentStream.Read(run, 0, run.Length); + this.stream.Read(run, 0, run.Length); for (int i = 0; i < copyLength; i++) { buffer[count++] = run[i]; @@ -356,7 +356,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp for (int y = 0; y < height; y++) { int newY = Invert(y, height, inverted); - this.currentStream.Read(row.Array, 0, row.Length()); + this.stream.Read(row.Array, 0, row.Length()); int offset = 0; Span pixelRow = pixels.GetRowSpan(newY); @@ -402,7 +402,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { for (int y = 0; y < height; y++) { - this.currentStream.Read(buffer.Array, 0, stride); + this.stream.Read(buffer.Array, 0, stride); int newY = Invert(y, height, inverted); Span pixelRow = pixels.GetRowSpan(newY); @@ -440,7 +440,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { for (int y = 0; y < height; y++) { - this.currentStream.Read(row); + this.stream.Read(row); int newY = Invert(y, height, inverted); Span pixelSpan = pixels.GetRowSpan(newY); PixelOperations.Instance.PackFromBgr24Bytes(row.Span, pixelSpan, width); @@ -465,7 +465,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { for (int y = 0; y < height; y++) { - this.currentStream.Read(row); + this.stream.Read(row); int newY = Invert(y, height, inverted); Span pixelSpan = pixels.GetRowSpan(newY); PixelOperations.Instance.PackFromBgra32Bytes(row.Span, pixelSpan, width); @@ -478,98 +478,43 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadInfoHeader() { - byte[] data = new byte[BmpInfoHeader.MaxHeaderSize]; + byte[] buffer = new byte[BmpInfoHeader.MaxHeaderSize]; // TODO: Stackalloc // read header size - this.currentStream.Read(data, 0, BmpInfoHeader.HeaderSizeSize); - int headerSize = BitConverter.ToInt32(data, 0); - if (headerSize < BmpInfoHeader.BitmapCoreHeaderSize) + this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); + int headerSize = BitConverter.ToInt32(buffer, 0); + if (headerSize < BmpInfoHeader.CoreSize) { throw new NotSupportedException($"This kind of bitmap files (header size $headerSize) is not supported."); } - int skipAmmount = 0; + int skipAmount = 0; if (headerSize > BmpInfoHeader.MaxHeaderSize) { - skipAmmount = headerSize - BmpInfoHeader.MaxHeaderSize; + skipAmount = headerSize - BmpInfoHeader.MaxHeaderSize; headerSize = BmpInfoHeader.MaxHeaderSize; } // read the rest of the header - this.currentStream.Read(data, BmpInfoHeader.HeaderSizeSize, headerSize - BmpInfoHeader.HeaderSizeSize); + this.stream.Read(buffer, BmpInfoHeader.HeaderSizeSize, headerSize - BmpInfoHeader.HeaderSizeSize); - switch (headerSize) + if (headerSize == BmpInfoHeader.CoreSize) { - case BmpInfoHeader.BitmapCoreHeaderSize: - this.infoHeader = this.ParseBitmapCoreHeader(data); - break; - case BmpInfoHeader.BitmapInfoHeaderSize: - this.infoHeader = this.ParseBitmapInfoHeader(data); - break; - default: - if (headerSize > BmpInfoHeader.BitmapInfoHeaderSize) - { - this.infoHeader = this.ParseBitmapInfoHeader(data); - break; - } - else - { - throw new NotSupportedException($"This kind of bitmap files (header size $headerSize) is not supported."); - } + // 12 bytes + this.infoHeader = BmpInfoHeader.ParseCore(buffer); } - - // skip the remaining header because we can't read those parts - this.currentStream.Skip(skipAmmount); - } - - /// - /// Parses the from the stream, assuming it uses the BITMAPCOREHEADER format. - /// - /// Header bytes read from the stream - /// Parsed header - /// - private BmpInfoHeader ParseBitmapCoreHeader(byte[] data) - { - return new BmpInfoHeader + else if (headerSize >= BmpInfoHeader.Size) { - HeaderSize = BitConverter.ToInt32(data, 0), - Width = BitConverter.ToUInt16(data, 4), - Height = BitConverter.ToUInt16(data, 6), - Planes = BitConverter.ToInt16(data, 8), - BitsPerPixel = BitConverter.ToInt16(data, 10), - - // the rest is not present in the core header - ImageSize = 0, - XPelsPerMeter = 0, - YPelsPerMeter = 0, - ClrUsed = 0, - ClrImportant = 0, - Compression = BmpCompression.RGB - }; - } - - /// - /// Parses the from the stream, assuming it uses the BITMAPINFOHEADER format. - /// - /// Header bytes read from the stream - /// Parsed header - /// - private BmpInfoHeader ParseBitmapInfoHeader(byte[] data) - { - return new BmpInfoHeader + // >= 40 bytes + this.infoHeader = BmpInfoHeader.Parse(buffer); + } + else { - HeaderSize = BitConverter.ToInt32(data, 0), - Width = BitConverter.ToInt32(data, 4), - Height = BitConverter.ToInt32(data, 8), - Planes = BitConverter.ToInt16(data, 12), - BitsPerPixel = BitConverter.ToInt16(data, 14), - ImageSize = BitConverter.ToInt32(data, 20), - XPelsPerMeter = BitConverter.ToInt32(data, 24), - YPelsPerMeter = BitConverter.ToInt32(data, 28), - ClrUsed = BitConverter.ToInt32(data, 32), - ClrImportant = BitConverter.ToInt32(data, 36), - Compression = (BmpCompression)BitConverter.ToInt32(data, 16) - }; + throw new NotSupportedException($"ImageSharp does not support this BMP file. HeaderSize: {headerSize}."); + } + + // skip the remaining header because we can't read those parts + this.stream.Skip(skipAmount); } /// @@ -577,15 +522,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadFileHeader() { - byte[] data = new byte[BmpFileHeader.Size]; + byte[] data = new byte[BmpFileHeader.Size]; // TODO: Stackalloc - this.currentStream.Read(data, 0, BmpFileHeader.Size); + this.stream.Read(data, 0, BmpFileHeader.Size); - this.fileHeader = new BmpFileHeader( - type: BitConverter.ToInt16(data, 0), - fileSize: BitConverter.ToInt32(data, 2), - reserved: BitConverter.ToInt32(data, 6), - offset: BitConverter.ToInt32(data, 10)); + this.fileHeader = BmpFileHeader.Parse(data); } /// @@ -593,7 +534,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadImageHeaders(Stream stream, out bool inverted, out byte[] palette) { - this.currentStream = stream; + this.stream = stream; try { @@ -640,7 +581,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp palette = new byte[colorMapSize]; - this.currentStream.Read(palette, 0, colorMapSize); + this.stream.Read(palette, 0, colorMapSize); } if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 1e0ecd3b1..2b0c90733 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -18,9 +18,6 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private int padding; - /// - /// Gets or sets the number of bits per pixel. - /// private readonly BmpBitsPerPixel bitsPerPixel; private readonly MemoryManager memoryManager; @@ -53,17 +50,15 @@ namespace SixLabors.ImageSharp.Formats.Bmp int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32); this.padding = bytesPerLine - (image.Width * (int)this.bitsPerPixel); - var infoHeader = new BmpInfoHeader - { - HeaderSize = BmpInfoHeader.BitmapInfoHeaderSize, - Height = image.Height, - Width = image.Width, - BitsPerPixel = bpp, - Planes = 1, - ImageSize = image.Height * bytesPerLine, - ClrUsed = 0, - ClrImportant = 0 - }; + var infoHeader = new BmpInfoHeader( + headerSize: BmpInfoHeader.Size, + height: image.Height, + width: image.Width, + bitsPerPixel: bpp, + planes: 1, + imageSize: image.Height * bytesPerLine, + clrUsed: 0, + clrImportant: 0); var fileHeader = new BmpFileHeader( type: 19778, // BM diff --git a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs index 64474ad90..d94fefa05 100644 --- a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Buffers.Binary; using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.Formats.Bmp @@ -57,14 +56,17 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// public int Offset { get; } + public static BmpFileHeader Parse(Span data) + { + return MemoryMarshal.Cast(data)[0]; + } + public unsafe void WriteTo(Span buffer) { fixed (BmpFileHeader* pointer = &this) { MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); } - - // TODO: Big Endian Platforms } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index b38cfd450..d7fb2f844 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers.Binary; using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.Formats.Bmp @@ -14,28 +15,52 @@ namespace SixLabors.ImageSharp.Formats.Bmp [StructLayout(LayoutKind.Sequential, Pack = 1)] internal struct BmpInfoHeader { - // TODO: Make readonly - /// /// Defines the size of the BITMAPINFOHEADER data structure in the bitmap file. /// - public const int BitmapInfoHeaderSize = 40; + public const int Size = 40; /// /// Defines the size of the BITMAPCOREHEADER data structure in the bitmap file. /// - public const int BitmapCoreHeaderSize = 12; + public const int CoreSize = 12; /// /// Defines the size of the biggest supported header data structure in the bitmap file. /// - public const int MaxHeaderSize = BitmapInfoHeaderSize; + public const int MaxHeaderSize = Size; /// /// Defines the size of the field. /// public const int HeaderSizeSize = 4; + public BmpInfoHeader( + int headerSize, + int width, + int height, + short planes, + short bitsPerPixel, + BmpCompression compression = default, + int imageSize = 0, + int xPelsPerMeter = 0, + int yPelsPerMeter = 0, + int clrUsed = 0, + int clrImportant = 0) + { + this.HeaderSize = headerSize; + this.Width = width; + this.Height = height; + this.Planes = planes; + this.BitsPerPixel = bitsPerPixel; + this.Compression = compression; + this.ImageSize = imageSize; + this.XPelsPerMeter = xPelsPerMeter; + this.YPelsPerMeter = yPelsPerMeter; + this.ClrUsed = clrUsed; + this.ClrImportant = clrImportant; + } + /// /// Gets or sets the size of this header /// @@ -98,14 +123,40 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// public int ClrImportant { get; set; } + + /// + /// Parses the full BITMAPINFOHEADER header (40 bytes). + /// + /// The data to parse. + /// Parsed header + /// + public static BmpInfoHeader Parse(ReadOnlySpan data) + { + return MemoryMarshal.Cast(data)[0]; + } + + /// + /// Parses the BITMAPCOREHEADER consisting of the headerSize, width, height, planes, and bitsPerPixel fields (12 bytes). + /// + /// The data to parse, + /// Parsed header + /// + public static BmpInfoHeader ParseCore(ReadOnlySpan data) + { + return new BmpInfoHeader( + headerSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)), + width: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(4, 2)), + height: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(6, 2)), + planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(8, 2)), + bitsPerPixel: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(10, 2))); + } + public unsafe void WriteTo(Span buffer) { fixed (BmpInfoHeader* pointer = &this) { MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); } - - // TODO: Big Endian Platforms } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Bmp/IBmpDecoderOptions.cs b/src/ImageSharp/Formats/Bmp/IBmpDecoderOptions.cs index 920c9ce02..c44ca73f2 100644 --- a/src/ImageSharp/Formats/Bmp/IBmpDecoderOptions.cs +++ b/src/ImageSharp/Formats/Bmp/IBmpDecoderOptions.cs @@ -1,11 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Collections.Generic; -using System.IO; -using SixLabors.ImageSharp.PixelFormats; - namespace SixLabors.ImageSharp.Formats.Bmp { /// @@ -15,4 +10,4 @@ namespace SixLabors.ImageSharp.Formats.Bmp { // added this for consistancy so we can add stuff as required, no options currently availible } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Bmp/IBmpEncoderOptions.cs b/src/ImageSharp/Formats/Bmp/IBmpEncoderOptions.cs index c4e219889..0bfd6980b 100644 --- a/src/ImageSharp/Formats/Bmp/IBmpEncoderOptions.cs +++ b/src/ImageSharp/Formats/Bmp/IBmpEncoderOptions.cs @@ -1,11 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Collections.Generic; -using System.IO; -using SixLabors.ImageSharp.PixelFormats; - namespace SixLabors.ImageSharp.Formats.Bmp { /// diff --git a/src/ImageSharp/Formats/Bmp/ImageExtensions.cs b/src/ImageSharp/Formats/Bmp/ImageExtensions.cs index 35e168e27..57e4615ba 100644 --- a/src/ImageSharp/Formats/Bmp/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Bmp/ImageExtensions.cs @@ -1,10 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; using System.IO; using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.PixelFormats; From ac3b94f6011f26ca9438e434b25f7b591cd198ae Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 16 Apr 2018 12:59:16 -0700 Subject: [PATCH 04/10] Delete empty line for STYLECOP --- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index d7fb2f844..3638ed35b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -123,7 +123,6 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// public int ClrImportant { get; set; } - /// /// Parses the full BITMAPINFOHEADER header (40 bytes). /// From 12554abf89126fc99c36dcbe63aff7684a42ef0a Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 16 Apr 2018 16:39:00 -0700 Subject: [PATCH 05/10] Minor cleanup --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 28 +++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index f4a009dd2..f1e93886b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -163,18 +163,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int Invert(int y, int height, bool inverted) { - int row; - - if (!inverted) - { - row = height - y - 1; - } - else - { - row = y; - } - - return row; + return (!inverted) ? height - y - 1 : y; } /// @@ -348,7 +337,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp using (IManagedByteBuffer row = this.memoryManager.AllocateCleanManagedByteBuffer(arrayWidth + padding)) { - var color = default(TPixel); + TPixel color = default; var rgba = new Rgba32(0, 0, 0, 255); Span rowSpan = row.Span; @@ -478,14 +467,15 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadInfoHeader() { - byte[] buffer = new byte[BmpInfoHeader.MaxHeaderSize]; // TODO: Stackalloc + byte[] buffer = new byte[BmpInfoHeader.MaxHeaderSize]; // TODO: stackalloc // read header size this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); + int headerSize = BitConverter.ToInt32(buffer, 0); if (headerSize < BmpInfoHeader.CoreSize) { - throw new NotSupportedException($"This kind of bitmap files (header size $headerSize) is not supported."); + throw new NotSupportedException($"ImageSharp does not support this BMP file. HeaderSize: {headerSize}."); } int skipAmount = 0; @@ -522,11 +512,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadFileHeader() { - byte[] data = new byte[BmpFileHeader.Size]; // TODO: Stackalloc + byte[] buffer = new byte[BmpFileHeader.Size]; // TODO: stackalloc - this.stream.Read(data, 0, BmpFileHeader.Size); + this.stream.Read(buffer, 0, BmpFileHeader.Size); - this.fileHeader = BmpFileHeader.Parse(data); + this.fileHeader = BmpFileHeader.Parse(buffer); } /// @@ -587,7 +577,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) { throw new ArgumentOutOfRangeException( - $"The input bitmap '{this.infoHeader.Width}x{this.infoHeader.Height}' is " + $"The input bmp '{this.infoHeader.Width}x{this.infoHeader.Height}' is " + $"bigger then the max allowed size '{int.MaxValue}x{int.MaxValue}'"); } } From 091b5295b03eacccce21a9815595f3c7915f3df8 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 16 Apr 2018 16:41:26 -0700 Subject: [PATCH 06/10] Remove nested try / catch This is caught a level above --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 85 +++++++++----------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index f1e93886b..bb6934479 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -526,64 +526,57 @@ namespace SixLabors.ImageSharp.Formats.Bmp { this.stream = stream; - try + this.ReadFileHeader(); + this.ReadInfoHeader(); + + // see http://www.drdobbs.com/architecture-and-design/the-bmp-file-format-part-1/184409517 + // If the height is negative, then this is a Windows bitmap whose origin + // is the upper-left corner and not the lower-left. The inverted flag + // indicates a lower-left origin.Our code will be outputting an + // upper-left origin pixel array. + inverted = false; + if (this.infoHeader.Height < 0) { - this.ReadFileHeader(); - this.ReadInfoHeader(); - - // see http://www.drdobbs.com/architecture-and-design/the-bmp-file-format-part-1/184409517 - // If the height is negative, then this is a Windows bitmap whose origin - // is the upper-left corner and not the lower-left.The inverted flag - // indicates a lower-left origin.Our code will be outputting an - // upper-left origin pixel array. - inverted = false; - if (this.infoHeader.Height < 0) - { - inverted = true; - this.infoHeader.Height = -this.infoHeader.Height; - } + inverted = true; + this.infoHeader.Height = -this.infoHeader.Height; + } - int colorMapSize = -1; + int colorMapSize = -1; - if (this.infoHeader.ClrUsed == 0) - { - if (this.infoHeader.BitsPerPixel == 1 || - this.infoHeader.BitsPerPixel == 4 || - this.infoHeader.BitsPerPixel == 8) - { - colorMapSize = (int)Math.Pow(2, this.infoHeader.BitsPerPixel) * 4; - } - } - else + if (this.infoHeader.ClrUsed == 0) + { + if (this.infoHeader.BitsPerPixel == 1 || + this.infoHeader.BitsPerPixel == 4 || + this.infoHeader.BitsPerPixel == 8) { - colorMapSize = this.infoHeader.ClrUsed * 4; + colorMapSize = (int)Math.Pow(2, this.infoHeader.BitsPerPixel) * 4; } + } + else + { + colorMapSize = this.infoHeader.ClrUsed * 4; + } - palette = null; + palette = null; - if (colorMapSize > 0) + if (colorMapSize > 0) + { + // 256 * 4 + if (colorMapSize > 1024) { - // 256 * 4 - if (colorMapSize > 1024) - { - throw new ImageFormatException($"Invalid bmp colormap size '{colorMapSize}'"); - } - - palette = new byte[colorMapSize]; - - this.stream.Read(palette, 0, colorMapSize); + throw new ImageFormatException($"Invalid bmp colormap size '{colorMapSize}'"); } - if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) - { - throw new ArgumentOutOfRangeException( - $"The input bmp '{this.infoHeader.Width}x{this.infoHeader.Height}' is " - + $"bigger then the max allowed size '{int.MaxValue}x{int.MaxValue}'"); - } + palette = new byte[colorMapSize]; + + this.stream.Read(palette, 0, colorMapSize); } - catch (IndexOutOfRangeException e) + + if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) { - throw new ImageFormatException("Bitmap does not have a valid format.", e); + throw new ArgumentOutOfRangeException( + $"The input bmp '{this.infoHeader.Width}x{this.infoHeader.Height}' is " + + $"bigger then the max allowed size '{int.MaxValue}x{int.MaxValue}'"); } } } From b7cf530da2a972698908264ddf46a0ee96dbbf7c Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 17 Apr 2018 08:24:16 -0700 Subject: [PATCH 07/10] Simplify absolute mode reading & remove stackalloc notes --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 22 ++++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index bb6934479..1d195f597 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -279,20 +279,20 @@ namespace SixLabors.ImageSharp.Formats.Bmp break; default: - // If the second byte > 2, signals 'absolute mode' + // If the second byte > 2, we are in 'absolute mode' // Take this number of bytes from the stream as uncompressed data int length = cmd[1]; - int copyLength = length; + + byte[] run = new byte[length]; + + this.stream.Read(run, 0, length); + + run.AsSpan().CopyTo(buffer); // Absolute mode data is aligned to two-byte word-boundary - length += length & 1; + int padding = length & 0; - byte[] run = new byte[length]; - this.stream.Read(run, 0, run.Length); - for (int i = 0; i < copyLength; i++) - { - buffer[count++] = run[i]; - } + this.stream.Skip(padding); break; } @@ -467,7 +467,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadInfoHeader() { - byte[] buffer = new byte[BmpInfoHeader.MaxHeaderSize]; // TODO: stackalloc + byte[] buffer = new byte[BmpInfoHeader.MaxHeaderSize]; // read header size this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); @@ -512,7 +512,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadFileHeader() { - byte[] buffer = new byte[BmpFileHeader.Size]; // TODO: stackalloc + byte[] buffer = new byte[BmpFileHeader.Size]; this.stream.Read(buffer, 0, BmpFileHeader.Size); From 886bee972405bf54fe5c1dcb3f3168d5144c43cc Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 17 Apr 2018 12:45:58 -0700 Subject: [PATCH 08/10] Fix offset when copying to buffer --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 1d195f597..40674ddf1 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -285,12 +285,14 @@ namespace SixLabors.ImageSharp.Formats.Bmp byte[] run = new byte[length]; - this.stream.Read(run, 0, length); + this.stream.Read(run, 0, run.Length); - run.AsSpan().CopyTo(buffer); + run.AsSpan().CopyTo(buffer.Slice(count)); + + count += run.Length; // Absolute mode data is aligned to two-byte word-boundary - int padding = length & 0; + int padding = length & 1; this.stream.Skip(padding); From 36ec32e45d3a86cc9591ae6e9c69d99c63394b39 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 17 Apr 2018 14:43:09 -0700 Subject: [PATCH 09/10] Ensure we pass exactly 40 bytes when parsing the BmpHeader --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 40674ddf1..26bd97b81 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -498,7 +498,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp else if (headerSize >= BmpInfoHeader.Size) { // >= 40 bytes - this.infoHeader = BmpInfoHeader.Parse(buffer); + this.infoHeader = BmpInfoHeader.Parse(buffer.AsSpan(0, 40)); } else { From be19776e5d087d9e3e4fb775fa3b97382331af05 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 17 Apr 2018 14:43:46 -0700 Subject: [PATCH 10/10] Favor Unsafe.As to pinning --- src/ImageSharp/Formats/Bmp/BmpFileHeader.cs | 8 ++++---- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs index d94fefa05..e39a2af0e 100644 --- a/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpFileHeader.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.Formats.Bmp @@ -63,10 +64,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp public unsafe void WriteTo(Span buffer) { - fixed (BmpFileHeader* pointer = &this) - { - MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); - } + ref BmpFileHeader dest = ref Unsafe.As(ref MemoryMarshal.GetReference(buffer)); + + dest = this; } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index 3638ed35b..a088a9b13 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; using System.Buffers.Binary; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.Formats.Bmp @@ -152,10 +153,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp public unsafe void WriteTo(Span buffer) { - fixed (BmpInfoHeader* pointer = &this) - { - MemoryMarshal.AsBytes(new ReadOnlySpan(pointer, 1)).CopyTo(buffer); - } + ref BmpInfoHeader dest = ref Unsafe.As(ref MemoryMarshal.GetReference(buffer)); + + dest = this; } } } \ No newline at end of file