From b51d127bd27b665fb9beea9dede3a9ec44e1e96e Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 19 May 2019 20:56:20 +0200 Subject: [PATCH] Using MemoryMarshal.AsBytes in Write8BitGray to get the bytes of a row --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 23 +++++++------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index e9600f640..9fbd0b5ad 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -4,6 +4,7 @@ using System; using System.Buffers; using System.IO; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Common.Helpers; @@ -389,23 +390,15 @@ namespace SixLabors.ImageSharp.Formats.Bmp stream.Write(colorPalette); - using (IMemoryOwner rowSpanBuffer = this.memoryAllocator.AllocateManagedByteBuffer(image.Width, AllocationOptions.Clean)) + for (int y = image.Height - 1; y >= 0; y--) { - Span outputPixelRow = rowSpanBuffer.GetSpan(); - for (int y = image.Height - 1; y >= 0; y--) - { - Span inputPixelRow = image.GetPixelRowSpan(y); - PixelOperations.Instance.ToGray8Bytes( - this.configuration, - inputPixelRow, - outputPixelRow, - image.Width); - stream.Write(outputPixelRow); + ReadOnlySpan inputPixelRow = image.GetPixelRowSpan(y); + ReadOnlySpan outputPixelRow = MemoryMarshal.AsBytes(inputPixelRow); + stream.Write(outputPixelRow); - for (int i = 0; i < this.padding; i++) - { - stream.WriteByte(0); - } + for (int i = 0; i < this.padding; i++) + { + stream.WriteByte(0); } } }