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));
diff --git a/src/ImageSharp/Image/PixelArea{TPixel}.cs b/src/ImageSharp/Image/PixelArea{TPixel}.cs
index e9924f823..1c7256455 100644
--- a/src/ImageSharp/Image/PixelArea{TPixel}.cs
+++ b/src/ImageSharp/Image/PixelArea{TPixel}.cs
@@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp
///
/// The underlying buffer containing the raw pixel data.
///
- private Buffer byteBuffer;
+ private readonly Buffer byteBuffer;
///
/// Initializes a new instance of the class.
@@ -116,7 +116,7 @@ namespace SixLabors.ImageSharp
this.RowStride = (width * GetComponentCount(componentOrder)) + padding;
this.Length = this.RowStride * height;
- this.byteBuffer = new Buffer(this.Length);
+ this.byteBuffer = Buffer.CreateClean(this.Length);
}
///
diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
index cd78add75..a4a27bd83 100644
--- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
@@ -10,12 +10,15 @@ using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
+ using SixLabors.ImageSharp.Advanced;
+
public class GifDecoderTests
{
private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32;
public static readonly string[] TestFiles = { TestImages.Gif.Giphy, TestImages.Gif.Rings, TestImages.Gif.Trans };
+
[Theory]
[WithFileCollection(nameof(TestFiles), PixelTypes)]
public void DecodeAndReSave(TestImageProvider imageProvider)
@@ -113,5 +116,21 @@ namespace SixLabors.ImageSharp.Tests
Assert.True(image.Frames.Count > 1);
}
}
+
+ [Fact]
+ public void CanDecodeIntermingledImages()
+ {
+ using (var kumin1 = Image.Load(TestFile.Create(TestImages.Gif.Kumin).Bytes))
+ using (var icon = Image.Load(TestFile.Create(TestImages.Png.Icon).Bytes))
+ using (var kumin2 = Image.Load(TestFile.Create(TestImages.Gif.Kumin).Bytes))
+ {
+ for (int i = 0; i < kumin1.Frames.Count; i++)
+ {
+ ImageFrame first = kumin1.Frames[i];
+ ImageFrame second = kumin2.Frames[i];
+ first.ComparePixelBufferTo(second.GetPixelSpan());
+ }
+ }
+ }
}
}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs
index 1b5f0dbad..7e98e50ad 100644
--- a/tests/ImageSharp.Tests/TestImages.cs
+++ b/tests/ImageSharp.Tests/TestImages.cs
@@ -31,6 +31,7 @@ namespace SixLabors.ImageSharp.Tests
public const string BikeGrayscale = "Png/BikeGrayscale.png";
public const string Rgb48BppInterlaced = "Png/rgb-48bpp-interlaced.png";
public const string SnakeGame = "Png/SnakeGame.png";
+ public const string Icon = "Png/icon.png";
// Filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html
public const string Filter0 = "Png/filter0.png";
@@ -154,8 +155,9 @@ namespace SixLabors.ImageSharp.Tests
public const string Giphy = "Gif/giphy.gif";
public const string Cheers = "Gif/cheers.gif";
public const string Trans = "Gif/trans.gif";
+ public const string Kumin = "Gif/kumin.gif";
- public static readonly string[] All = { Rings, Giphy, Cheers, Trans };
+ public static readonly string[] All = { Rings, Giphy, Cheers, Trans, Kumin };
}
}
}
diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
index 505cdc172..2b3cb1bcc 100644
--- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
+++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
@@ -29,7 +29,6 @@ namespace SixLabors.ImageSharp.Tests
/// The image provider
/// Details to be concatenated to the test output file, describing the parameters of the test.
/// The extension
- /// A boolean indicating whether we should save a smaller in size.
/// A boolean indicating whether to append the pixel type to the output file name.
public static Image DebugSave(
this Image image,
diff --git a/tests/Images/Input/Gif/kumin.gif b/tests/Images/Input/Gif/kumin.gif
new file mode 100644
index 000000000..31efda7d8
--- /dev/null
+++ b/tests/Images/Input/Gif/kumin.gif
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:345556b9a0e064412acc3b2ee87a9226113eb65c0a1791c2f855ac3fa1e6b7ad
+size 868269
diff --git a/tests/Images/Input/Png/icon.png b/tests/Images/Input/Png/icon.png
new file mode 100644
index 000000000..bc355712b
--- /dev/null
+++ b/tests/Images/Input/Png/icon.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:18f1eb7c5019153f4a0b2de90e7e0f0521193f003fabd6ac31c2f58c2562ae42
+size 4040