Skip to content

Commit

Permalink
Merge pull request #2749 from SixLabors/js/identify-bad-lzw
Browse files Browse the repository at this point in the history
GifDecoder : Allow skipping bad metadata using identify
  • Loading branch information
JimBobSquarePants authored Jun 13, 2024
2 parents ede2f2d + af007fa commit 339b26d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Gif/LzwDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void SkipIndices(int minCodeSize, int length)
// 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;
Expand Down
26 changes: 23 additions & 3 deletions tests/ImageSharp.Tests/Formats/Gif/GifMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void CloneIsDeep()
RepeatCount = 1,
ColorTableMode = GifColorTableMode.Global,
GlobalColorTable = new[] { Color.Black, Color.White },
Comments = new List<string> { "Foo" }
Comments = ["Foo"]
};

GifMetadata clone = (GifMetadata)meta.DeepClone();
Expand Down Expand Up @@ -126,7 +126,7 @@ public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolut
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);
Expand All @@ -152,7 +152,7 @@ public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolutio
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);
Expand Down Expand Up @@ -214,4 +214,24 @@ public void Identify_Frames(
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);
}
}

0 comments on commit 339b26d

Please sign in to comment.