From 9c96f023fcc59bcdb5e3ea64e9d8f04f8e3ba4f2 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 22 Jun 2021 11:23:19 +1000 Subject: [PATCH] Remove from Gif --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 25 ++++++++++---------- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 8 ++++--- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index fb3d989d4b..e59dad6826 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -33,7 +34,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// The global color table. /// - private IManagedByteBuffer globalColorTable; + private IMemoryOwner globalColorTable; /// /// The area to restore. @@ -323,12 +324,12 @@ namespace SixLabors.ImageSharp.Formats.Gif continue; } - using (IManagedByteBuffer commentsBuffer = this.MemoryAllocator.AllocateManagedByteBuffer(length)) - { - this.stream.Read(commentsBuffer.Array, 0, length); - string commentPart = GifConstants.Encoding.GetString(commentsBuffer.Array, 0, length); - stringBuilder.Append(commentPart); - } + using IMemoryOwner commentsBuffer = this.MemoryAllocator.Allocate(length); + Span commentsSpan = commentsBuffer.GetSpan(); + + this.stream.Read(commentsSpan); + string commentPart = GifConstants.Encoding.GetString(commentsSpan); + stringBuilder.Append(commentPart); } if (stringBuilder.Length > 0) @@ -348,7 +349,7 @@ namespace SixLabors.ImageSharp.Formats.Gif { this.ReadImageDescriptor(); - IManagedByteBuffer localColorTable = null; + IMemoryOwner localColorTable = null; Buffer2D indices = null; try { @@ -356,8 +357,8 @@ namespace SixLabors.ImageSharp.Formats.Gif if (this.imageDescriptor.LocalColorTableFlag) { int length = this.imageDescriptor.LocalColorTableSize * 3; - localColorTable = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(length, AllocationOptions.Clean); - this.stream.Read(localColorTable.Array, 0, length); + localColorTable = this.Configuration.MemoryAllocator.Allocate(length, AllocationOptions.Clean); + this.stream.Read(localColorTable.GetSpan()); } indices = this.Configuration.MemoryAllocator.Allocate2D(this.imageDescriptor.Width, this.imageDescriptor.Height, AllocationOptions.Clean); @@ -622,10 +623,10 @@ namespace SixLabors.ImageSharp.Formats.Gif int globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; this.gifMetadata.GlobalColorTableLength = globalColorTableLength; - this.globalColorTable = this.MemoryAllocator.AllocateManagedByteBuffer(globalColorTableLength, AllocationOptions.Clean); + this.globalColorTable = this.MemoryAllocator.Allocate(globalColorTableLength, AllocationOptions.Clean); // Read the global color table data from the stream - stream.Read(this.globalColorTable.Array, 0, globalColorTableLength); + stream.Read(this.globalColorTable.GetSpan()); } } } diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 4c881ec3f2..05ea14e9ce 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -470,14 +470,16 @@ namespace SixLabors.ImageSharp.Formats.Gif // The maximum number of colors for the bit depth int colorTableLength = ColorNumerics.GetColorCountForBitDepth(this.bitDepth) * Unsafe.SizeOf(); - using IManagedByteBuffer colorTable = this.memoryAllocator.AllocateManagedByteBuffer(colorTableLength, AllocationOptions.Clean); + using IMemoryOwner colorTable = this.memoryAllocator.Allocate(colorTableLength, AllocationOptions.Clean); + Span colorTableSpan = colorTable.GetSpan(); + PixelOperations.Instance.ToRgb24Bytes( this.configuration, image.Palette.Span, - colorTable.GetSpan(), + colorTableSpan, image.Palette.Length); - stream.Write(colorTable.Array, 0, colorTableLength); + stream.Write(colorTableSpan); } ///