diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 453197b0c..80ee3d130 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -7,6 +7,7 @@ using System.IO; using System.Runtime.CompilerServices; using System.Text; using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -38,7 +39,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// The global color table. /// - private byte[] globalColorTable; + private Buffer globalColorTable; /// /// The global color table length @@ -123,10 +124,10 @@ namespace SixLabors.ImageSharp.Formats.Gif if (this.logicalScreenDescriptor.GlobalColorTableFlag) { this.globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; - this.globalColorTable = ArrayPool.Shared.Rent(this.globalColorTableLength); + this.globalColorTable = Buffer.CreateClean(this.globalColorTableLength); // Read the global color table from the stream - stream.Read(this.globalColorTable, 0, this.globalColorTableLength); + stream.Read(this.globalColorTable.Array, 0, this.globalColorTableLength); } // Loop though the respective gif parts and read the data. @@ -175,10 +176,7 @@ namespace SixLabors.ImageSharp.Formats.Gif } finally { - if (this.globalColorTable != null) - { - ArrayPool.Shared.Return(this.globalColorTable); - } + this.globalColorTable?.Dispose(); } return this.image; @@ -309,19 +307,19 @@ namespace SixLabors.ImageSharp.Formats.Gif { GifImageDescriptor imageDescriptor = this.ReadImageDescriptor(); - byte[] localColorTable = null; - byte[] indices = null; + Buffer localColorTable = null; + Buffer indices = null; try { // Determine the color table for this frame. If there is a local one, use it otherwise use the global color table. if (imageDescriptor.LocalColorTableFlag) { int length = imageDescriptor.LocalColorTableSize * 3; - localColorTable = ArrayPool.Shared.Rent(length); - this.currentStream.Read(localColorTable, 0, length); + localColorTable = Buffer.CreateClean(length); + this.currentStream.Read(localColorTable.Array, 0, length); } - indices = ArrayPool.Shared.Rent(imageDescriptor.Width * imageDescriptor.Height); + indices = Buffer.CreateClean(imageDescriptor.Width * imageDescriptor.Height); this.ReadFrameIndices(imageDescriptor, indices); this.ReadFrameColors(indices, localColorTable ?? this.globalColorTable, imageDescriptor); @@ -331,12 +329,8 @@ namespace SixLabors.ImageSharp.Formats.Gif } finally { - if (localColorTable != null) - { - ArrayPool.Shared.Return(localColorTable); - } - - ArrayPool.Shared.Return(indices); + localColorTable?.Dispose(); + indices?.Dispose(); } } @@ -346,7 +340,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The . /// The pixel array to write to. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadFrameIndices(GifImageDescriptor imageDescriptor, byte[] indices) + private void ReadFrameIndices(GifImageDescriptor imageDescriptor, Span indices) { int dataSize = this.currentStream.ReadByte(); using (var lzwDecoder = new LzwDecoder(this.currentStream)) @@ -361,7 +355,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The indexed pixels. /// The color table containing the available colors. /// The - private void ReadFrameColors(byte[] indices, byte[] colorTable, GifImageDescriptor descriptor) + private void ReadFrameColors(Span indices, Span colorTable, GifImageDescriptor descriptor) { int imageWidth = this.logicalScreenDescriptor.Width; int imageHeight = this.logicalScreenDescriptor.Height; diff --git a/src/ImageSharp/Formats/Gif/LzwDecoder.cs b/src/ImageSharp/Formats/Gif/LzwDecoder.cs index b8f12f930..3284dad65 100644 --- a/src/ImageSharp/Formats/Gif/LzwDecoder.cs +++ b/src/ImageSharp/Formats/Gif/LzwDecoder.cs @@ -83,7 +83,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The height of the pixel index array. /// Size of the data. /// The pixel array to decode to. - public void DecodePixels(int width, int height, int dataSize, byte[] pixels) + public void DecodePixels(int width, int height, int dataSize, Span pixels) { Guard.MustBeLessThan(dataSize, int.MaxValue, nameof(dataSize));