diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 7d51acb99..247024570 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers.Binary; using System.IO; using System.Linq; using System.Runtime.CompilerServices; @@ -75,9 +76,6 @@ namespace SixLabors.ImageSharp.Formats.Gif Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); - // Do not use IDisposable pattern here as we want to preserve the stream. - var writer = new EndianBinaryWriter(Endianness.LittleEndian, stream); - // Quantize the image returning a palette. QuantizedFrame quantized = this.quantizer.CreateFrameQuantizer().QuantizeFrame(image.Frames.RootFrame); @@ -87,18 +85,18 @@ namespace SixLabors.ImageSharp.Formats.Gif int index = this.GetTransparentIndex(quantized); // Write the header. - this.WriteHeader(writer); + this.WriteHeader(stream); // Write the LSD. We'll use local color tables for now. this.WriteLogicalScreenDescriptor(image, stream, index); // Write the first frame. - this.WriteComments(image, writer); + this.WriteComments(image, stream); // Write additional frames. if (image.Frames.Count > 1) { - this.WriteApplicationExtension(writer, image.MetaData.RepeatCount); + this.WriteApplicationExtension(stream, image.MetaData.RepeatCount); } foreach (ImageFrame frame in image.Frames) @@ -110,14 +108,14 @@ namespace SixLabors.ImageSharp.Formats.Gif this.WriteGraphicalControlExtension(frame.MetaData, stream, this.GetTransparentIndex(quantized)); this.WriteImageDescriptor(frame, stream); - this.WriteColorTable(quantized, writer); - this.WriteImageData(quantized, writer); + this.WriteColorTable(quantized, stream); + this.WriteImageData(quantized, stream); quantized = null; // So next frame can regenerate it } // TODO: Write extension etc - writer.Write(GifConstants.EndIntroducer); + stream.WriteByte(GifConstants.EndIntroducer); } /// @@ -153,11 +151,11 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Writes the file header signature and version to the stream. /// - /// The writer to write to the stream with. + /// The stream to write to. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void WriteHeader(EndianBinaryWriter writer) + private void WriteHeader(Stream stream) { - writer.Write(GifConstants.MagicNumber); + stream.Write(GifConstants.MagicNumber, 0, GifConstants.MagicNumber.Length); } /// @@ -189,9 +187,9 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Writes the application extension to the stream. /// - /// The writer to write to the stream with. + /// The stream to write to. /// The animated image repeat count. - private void WriteApplicationExtension(EndianBinaryWriter writer, ushort repeatCount) + private void WriteApplicationExtension(Stream stream, ushort repeatCount) { // Application Extension Header if (repeatCount != 1) @@ -200,17 +198,21 @@ namespace SixLabors.ImageSharp.Formats.Gif this.buffer[1] = GifConstants.ApplicationExtensionLabel; this.buffer[2] = GifConstants.ApplicationBlockSize; - writer.Write(this.buffer, 0, 3); + stream.Write(this.buffer, 0, 3); + + stream.Write(GifConstants.ApplicationIdentificationBytes, 0, 11); // NETSCAPE2.0 - writer.Write(GifConstants.ApplicationIdentificationBytes); // NETSCAPE2.0 - writer.Write((byte)3); // Application block length - writer.Write((byte)1); // Data sub-block index (always 1) + this.buffer[0] = 3; // Application block length + this.buffer[1] = 1; // Data sub-block index (always 1) // 0 means loop indefinitely. Count is set as play n + 1 times. repeatCount = (ushort)Math.Max(0, repeatCount - 1); - writer.Write(repeatCount); // Repeat count for images. - writer.Write(GifConstants.Terminator); // Terminator + BinaryPrimitives.WriteUInt16LittleEndian(this.buffer.AsSpan(2, 2), repeatCount); // Repeat count for images. + + this.buffer[4] = GifConstants.Terminator; // Terminator + + stream.Write(this.buffer, 0, 5); } } @@ -220,7 +222,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The pixel format. /// The to be encoded. /// The stream to write to. - private void WriteComments(Image image, EndianBinaryWriter writer) + private void WriteComments(Image image, Stream stream) where TPixel : struct, IPixel { if (this.ignoreMetadata) @@ -242,9 +244,9 @@ namespace SixLabors.ImageSharp.Formats.Gif this.buffer[1] = GifConstants.CommentLabel; this.buffer[2] = (byte)count; - writer.Write(this.buffer, 0, 3); - writer.Write(comments, 0, count); - writer.Write(GifConstants.Terminator); + stream.Write(this.buffer, 0, 3); + stream.Write(comments, 0, count); + stream.WriteByte(GifConstants.Terminator); } /// @@ -294,8 +296,8 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// The pixel format. /// The to encode. - /// The writer to write to the stream with. - private void WriteColorTable(QuantizedFrame image, EndianBinaryWriter writer) + /// The stream to write to. + private void WriteColorTable(QuantizedFrame image, Stream stream) where TPixel : struct, IPixel { // Grab the palette and write it to the stream. @@ -316,7 +318,7 @@ namespace SixLabors.ImageSharp.Formats.Gif Unsafe.Add(ref rgb24Ref, i) = rgb; } - writer.Write(colorTable.Array, 0, colorTableLength); + stream.Write(colorTable.Array, 0, colorTableLength); } } @@ -325,13 +327,13 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// The pixel format. /// The containing indexed pixels. - /// The stream to write to. - private void WriteImageData(QuantizedFrame image, EndianBinaryWriter writer) + /// The stream to write to. + private void WriteImageData(QuantizedFrame image, Stream stream) where TPixel : struct, IPixel { using (var encoder = new LzwEncoder(this.memoryManager, image.Pixels, (byte)this.bitDepth)) { - encoder.Encode(writer.BaseStream); + encoder.Encode(stream); } } }