Browse Source

Merge pull request #2749 from SixLabors/js/identify-bad-lzw

GifDecoder : Allow skipping bad metadata using identify
pull/2761/head
James Jackson-South 2 years ago
committed by GitHub
parent
commit
339b26d409
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      src/ImageSharp/Formats/Gif/LzwDecoder.cs
  2. 26
      tests/ImageSharp.Tests/Formats/Gif/GifMetadataTests.cs

2
src/ImageSharp/Formats/Gif/LzwDecoder.cs

@ -255,7 +255,7 @@ internal sealed class LzwDecoder : IDisposable
// Don't attempt to decode the frame indices.
// Theoretically we could determine a min code size from the length of the provided
// color palette but we won't bother since the image is most likely corrupted.
GifThrowHelper.ThrowInvalidImageContentException("Gif Image does not contain a valid LZW minimum code.");
return;
}
int codeSize = minCodeSize + 1;

26
tests/ImageSharp.Tests/Formats/Gif/GifMetadataTests.cs

@ -35,7 +35,7 @@ public class GifMetadataTests
RepeatCount = 1,
ColorTableMode = GifColorTableMode.Global,
GlobalColorTable = new[] { Color.Black, Color.White },
Comments = new List<string> { "Foo" }
Comments = ["Foo"]
};
GifMetadata clone = (GifMetadata)meta.DeepClone();
@ -126,7 +126,7 @@ public class GifMetadataTests
public async Task Identify_VerifyRatioAsync(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{
TestFile testFile = TestFile.Create(imagePath);
using MemoryStream stream = new(testFile.Bytes, false);
await using MemoryStream stream = new(testFile.Bytes, false);
ImageInfo image = await GifDecoder.Instance.IdentifyAsync(DecoderOptions.Default, stream);
ImageMetadata meta = image.Metadata;
Assert.Equal(xResolution, meta.HorizontalResolution);
@ -152,7 +152,7 @@ public class GifMetadataTests
public async Task Decode_VerifyRatioAsync(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{
TestFile testFile = TestFile.Create(imagePath);
using MemoryStream stream = new(testFile.Bytes, false);
await using MemoryStream stream = new(testFile.Bytes, false);
using Image<Rgba32> image = await GifDecoder.Instance.DecodeAsync<Rgba32>(DecoderOptions.Default, stream);
ImageMetadata meta = image.Metadata;
Assert.Equal(xResolution, meta.HorizontalResolution);
@ -214,4 +214,24 @@ public class GifMetadataTests
Assert.Equal(frameDelay, gifFrameMetadata.FrameDelay);
Assert.Equal(disposalMethod, gifFrameMetadata.DisposalMethod);
}
[Theory]
[InlineData(TestImages.Gif.Issues.BadMaxLzwBits, 8)]
[InlineData(TestImages.Gif.Issues.Issue2012BadMinCode, 1)]
public void Identify_Frames_Bad_Lzw(string imagePath, int framesCount)
{
TestFile testFile = TestFile.Create(imagePath);
using MemoryStream stream = new(testFile.Bytes, false);
ImageInfo imageInfo = Image.Identify(stream);
Assert.NotNull(imageInfo);
GifMetadata gifMetadata = imageInfo.Metadata.GetGifMetadata();
Assert.NotNull(gifMetadata);
Assert.Equal(framesCount, imageInfo.FrameMetadataCollection.Count);
GifFrameMetadata gifFrameMetadata = imageInfo.FrameMetadataCollection[imageInfo.FrameMetadataCollection.Count - 1].GetGifMetadata();
Assert.NotNull(gifFrameMetadata);
}
}

Loading…
Cancel
Save