From 72c6f529098b8069d03a4b2db9f1c34a25bca79c Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 4 Dec 2021 18:23:58 +0100 Subject: [PATCH 1/2] Allocate clean buffer in AnalyzeEntropy --- src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index adabd0ac3..7769a0a6c 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -192,7 +192,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless public bool UseCrossColorTransform { get; set; } /// - /// Gets or sets a value indicating whether to use the substract green transform. + /// Gets or sets a value indicating whether to use the subtract green transform. /// public bool UseSubtractGreenTransform { get; set; } @@ -1049,7 +1049,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless return EntropyIx.Palette; } - using IMemoryOwner histoBuffer = this.memoryAllocator.Allocate((int)HistoIx.HistoTotal * 256); + using IMemoryOwner histoBuffer = this.memoryAllocator.Allocate((int)HistoIx.HistoTotal * 256, AllocationOptions.Clean); Span histo = histoBuffer.Memory.Span; uint pixPrev = bgra[0]; // Skip the first pixel. ReadOnlySpan prevRow = null; From 3780ca78e0f6567e94ccecd8ab2856fca362ec21 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 4 Dec 2021 19:54:49 +0100 Subject: [PATCH 2/2] Add lossless encode test which checks the expected bytes count of the encoded file --- .../Formats/WebP/WebpEncoderTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs index 70cc487bf..ec0040a46 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs @@ -101,6 +101,24 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp image.VerifyEncoder(provider, "webp", testOutputDetails, encoder); } + [Theory] + [WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 15114)] + public void Encode_Lossless_WithBestQuality_HasExpectedSize(TestImageProvider provider, int expectedBytes) + where TPixel : unmanaged, IPixel + { + var encoder = new WebpEncoder() + { + FileFormat = WebpFileFormatType.Lossless, + Method = WebpEncodingMethod.BestQuality + }; + + using Image image = provider.GetImage(); + using var memoryStream = new MemoryStream(); + image.Save(memoryStream, encoder); + + Assert.Equal(memoryStream.Length, expectedBytes); + } + [Theory] [WithFile(RgbTestPattern100x100, PixelTypes.Rgba32, 85)] [WithFile(RgbTestPattern100x100, PixelTypes.Rgba32, 60)]