Repository navigation
PngEncoder ignores FilterMethod #2771
Copy link
Copy link
Closed
Labels
Description
Activity
@melnikov77 The filter method is used in the
release/3.1.xbranch here.ImageSharp/src/ImageSharp/Formats/Png/PngEncoderCore.cs
Lines 574 to 600 in d7ef0e2
private void FilterPixelBytes(ref Span<byte> filter, ref Span<byte> attempt) { switch (this.filterMethod) { case PngFilterMethod.None: NoneFilter.Encode(this.currentScanline.GetSpan(), filter); break; case PngFilterMethod.Sub: SubFilter.Encode(this.currentScanline.GetSpan(), filter, this.bytesPerPixel, out int _); break; case PngFilterMethod.Up: UpFilter.Encode(this.currentScanline.GetSpan(), this.previousScanline.GetSpan(), filter, out int _); break; case PngFilterMethod.Average: AverageFilter.Encode(this.currentScanline.GetSpan(), this.previousScanline.GetSpan(), filter, (uint)this.bytesPerPixel, out int _); break; case PngFilterMethod.Paeth: PaethFilter.Encode(this.currentScanline.GetSpan(), this.previousScanline.GetSpan(), filter, this.bytesPerPixel, out int _); break; default: this.ApplyOptimalFilteredScanline(ref filter, ref attempt); break; } } Your test confused me given you are use
null!to define the source image.SixLabors.ImageSharp.Image img = null!;
However, you are correct! We are not assigning the correct filter on encode, your finding actually uncovers a bug in our SIMD accelerated filtering methods where we suffered from an unsigned integer overflow defect!
Many thanks for raising this.
I've opened a PR #2775 with the fix.
Fixed with 3.1.5
Prerequisites
DEBUGandRELEASEmodeImageSharp version
3.1.4
Other ImageSharp packages and versions
NA
Environment (Operating system, version and so on)
NA
.NET Framework version
net8
Description
When a new
PngEncoderis created with customFilterMethodit always produces the same file size,i.e. the
FilterMethodis being ignored.Example:
I found only one usage of this property here
ImageSharp/src/ImageSharp/Formats/Png/PngEncoderCore.cs
Lines 1522 to 1526 in 10e9c49
i.e.
filterMethodis assigned only ifencoder.FilterMethodisnull,otherwise, it is always 0 (
PngFilterMethod.None).Steps to Reproduce
Images
No response