Skip to content

Commit 8809765

Browse files
committed
Using MemoryMarshal.AsBytes in Write8BitGray to get the bytes of a row
1 parent e62ae79 commit 8809765

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Buffers;
66
using System.IO;
7+
using System.Runtime.InteropServices;
78

89
using SixLabors.ImageSharp.Advanced;
910
using SixLabors.ImageSharp.Common.Helpers;
@@ -389,23 +390,15 @@ private void Write8BitGray<TPixel>(Stream stream, ImageFrame<TPixel> image, Span
389390

390391
stream.Write(colorPalette);
391392

392-
using (IMemoryOwner<byte> rowSpanBuffer = this.memoryAllocator.AllocateManagedByteBuffer(image.Width, AllocationOptions.Clean))
393+
for (int y = image.Height - 1; y >= 0; y--)
393394
{
394-
Span<byte> outputPixelRow = rowSpanBuffer.GetSpan();
395-
for (int y = image.Height - 1; y >= 0; y--)
396-
{
397-
Span<TPixel> inputPixelRow = image.GetPixelRowSpan(y);
398-
PixelOperations<TPixel>.Instance.ToGray8Bytes(
399-
this.configuration,
400-
inputPixelRow,
401-
outputPixelRow,
402-
image.Width);
403-
stream.Write(outputPixelRow);
395+
ReadOnlySpan<TPixel> inputPixelRow = image.GetPixelRowSpan(y);
396+
ReadOnlySpan<byte> outputPixelRow = MemoryMarshal.AsBytes(inputPixelRow);
397+
stream.Write(outputPixelRow);
404398

405-
for (int i = 0; i < this.padding; i++)
406-
{
407-
stream.WriteByte(0);
408-
}
399+
for (int i = 0; i < this.padding; i++)
400+
{
401+
stream.WriteByte(0);
409402
}
410403
}
411404
}

0 commit comments

Comments
 (0)