diff --git a/src/ImageSharp/Formats/WebP/Vp8FilterInfo.cs b/src/ImageSharp/Formats/WebP/Vp8FilterInfo.cs index a95e0ba925..760e1b5c55 100644 --- a/src/ImageSharp/Formats/WebP/Vp8FilterInfo.cs +++ b/src/ImageSharp/Formats/WebP/Vp8FilterInfo.cs @@ -6,8 +6,27 @@ namespace SixLabors.ImageSharp.Formats.WebP /// /// Filter information. /// - internal class Vp8FilterInfo + internal class Vp8FilterInfo : IDeepCloneable { + /// + /// Initializes a new instance of the class. + /// + public Vp8FilterInfo() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The filter info to create an instance from. + public Vp8FilterInfo(Vp8FilterInfo other) + { + this.Limit = other.Limit; + this.HighEdgeVarianceThreshold = other.HighEdgeVarianceThreshold; + this.InnerLevel = other.InnerLevel; + this.UseInnerFiltering = other.UseInnerFiltering; + } + /// /// Gets or sets the filter limit in [3..189], or 0 if no filtering. /// @@ -28,5 +47,8 @@ namespace SixLabors.ImageSharp.Formats.WebP /// Gets or sets the high edge variance threshold in [0..2]. /// public byte HighEdgeVarianceThreshold { get; set; } + + /// + public IDeepCloneable DeepClone() => new Vp8FilterInfo(this); } } diff --git a/src/ImageSharp/Formats/WebP/Vp8LDecoder.cs b/src/ImageSharp/Formats/WebP/Vp8LDecoder.cs index 69b3c3ed74..162106f8ea 100644 --- a/src/ImageSharp/Formats/WebP/Vp8LDecoder.cs +++ b/src/ImageSharp/Formats/WebP/Vp8LDecoder.cs @@ -57,12 +57,15 @@ namespace SixLabors.ImageSharp.Formats.WebP public void Dispose() { this.Pixels.Dispose(); - foreach (Vp8LTransform transform in this.Transforms) + this.Metadata?.HuffmanImage?.Dispose(); + + if (this.Transforms != null) { - transform.Data?.Dispose(); + foreach (Vp8LTransform transform in this.Transforms) + { + transform.Data?.Dispose(); + } } - - this.Metadata?.HuffmanImage?.Dispose(); } } } diff --git a/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs b/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs index 2a2effd921..8b9a3569c2 100644 --- a/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs +++ b/src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs @@ -79,9 +79,11 @@ namespace SixLabors.ImageSharp.Formats.WebP if (info.Features?.Alpha is true) { - var alphaDecoder = new AlphaDecoder(width, height, info.Features.AlphaData, info.Features.AlphaChunkHeader, this.memoryAllocator); - alphaDecoder.Decode(); - this.DecodePixelValues(width, height, decoder.Pixels, pixels, alphaDecoder.Alpha); + using (var alphaDecoder = new AlphaDecoder(width, height, info.Features.AlphaData, info.Features.AlphaChunkHeader, this.memoryAllocator)) + { + alphaDecoder.Decode(); + this.DecodePixelValues(width, height, decoder.Pixels, pixels, alphaDecoder.Alpha); + } } else { @@ -100,7 +102,7 @@ namespace SixLabors.ImageSharp.Formats.WebP hasAlpha = true; alphaSpan = alpha.Memory.Span; } - + for (int y = 0; y < height; y++) { Span pixelRow = pixels.GetRowSpan(y); @@ -826,7 +828,8 @@ namespace SixLabors.ImageSharp.Formats.WebP // Store filter info. if (dec.Filter != LoopFilter.None) { - dec.FilterInfo[dec.MbX] = dec.FilterStrength[blockData.Segment, blockData.IsI4x4 ? 1 : 0]; + Vp8FilterInfo precomputedFilterInfo = dec.FilterStrength[blockData.Segment, blockData.IsI4x4 ? 1 : 0]; + dec.FilterInfo[dec.MbX] = (Vp8FilterInfo)precomputedFilterInfo.DeepClone(); dec.FilterInfo[dec.MbX].UseInnerFiltering |= (byte)(skip is 0 ? 1 : 0); } }