diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 6c03bd2b1..4fbd4baf5 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -39,11 +39,6 @@ namespace SixLabors.ImageSharp.Formats.Gif /// private IManagedByteBuffer globalColorTable; - /// - /// The global color table length - /// - private int globalColorTableLength; - /// /// The area to restore. /// @@ -333,8 +328,8 @@ namespace SixLabors.ImageSharp.Formats.Gif indices = this.configuration.MemoryManager.AllocateManagedByteBuffer(imageDescriptor.Width * imageDescriptor.Height, true); this.ReadFrameIndices(imageDescriptor, indices.Span); - IManagedByteBuffer colorTable = localColorTable ?? this.globalColorTable; - this.ReadFrameColors(ref image, ref previousFrame, indices.Span, colorTable.Span, imageDescriptor); + ReadOnlySpan colorTable = MemoryMarshal.Cast((localColorTable ?? this.globalColorTable).Span); + this.ReadFrameColors(ref image, ref previousFrame, indices.Span, colorTable, imageDescriptor); // Skip any remaining blocks this.Skip(0); @@ -370,7 +365,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The indexed pixels. /// The color table containing the available colors. /// The - private void ReadFrameColors(ref Image image, ref ImageFrame previousFrame, Span indices, Span colorTable, in GifImageDescriptor descriptor) + private void ReadFrameColors(ref Image image, ref ImageFrame previousFrame, Span indices, ReadOnlySpan colorTable, in GifImageDescriptor descriptor) where TPixel : struct, IPixel { ref byte indicesRef = ref MemoryMarshal.GetReference(indices); @@ -458,11 +453,8 @@ namespace SixLabors.ImageSharp.Formats.Gif if (this.graphicsControlExtension.TransparencyFlag == false || this.graphicsControlExtension.TransparencyIndex != index) { - int indexOffset = index * 3; - ref TPixel pixel = ref Unsafe.Add(ref rowRef, x); - rgba.Rgb = colorTable.Slice(indexOffset).AsRgb24(); - + rgba.Rgb = colorTable[index]; pixel.PackFromRgba32(rgba); } @@ -534,12 +526,12 @@ namespace SixLabors.ImageSharp.Formats.Gif if (this.logicalScreenDescriptor.GlobalColorTableFlag) { - this.globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; + int globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; - this.globalColorTable = this.MemoryManager.AllocateManagedByteBuffer(this.globalColorTableLength, true); + this.globalColorTable = this.MemoryManager.AllocateManagedByteBuffer(globalColorTableLength, true); - // Read the global color table from the stream - stream.Read(this.globalColorTable.Array, 0, this.globalColorTableLength); + // Read the global color table data from the stream + stream.Read(this.globalColorTable.Array, 0, globalColorTableLength); } } }