From 7d0838f97ca19e8c4d0aa3fdd9157238e6acbcdc Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 13 Jul 2022 13:27:22 +0200 Subject: [PATCH] Avoid encoding the same image twice --- .../Formats/WebP/Vp8ResidualTests.cs | 2 +- .../Formats/WebP/WebpEncoderTests.cs | 29 ++++--------------- .../TestUtilities/TestImageExtensions.cs | 5 +++- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs index 2bca632db..2a6c69ead 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs @@ -5,7 +5,7 @@ using SixLabors.ImageSharp.Formats.Webp.Lossy; using SixLabors.ImageSharp.Tests.TestUtilities; using Xunit; -namespace SixLabors.ImageSharp.Tests.Formats.WebP +namespace SixLabors.ImageSharp.Tests.Formats.Webp { [Trait("Format", "Webp")] public class Vp8ResidualTests diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs index 8b6e6633e..db713faca 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs @@ -269,9 +269,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp } [Theory] - [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, false)] - [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, true)] - public void Encode_Lossy_WithAlpha_Works(TestImageProvider provider, bool compressed) + [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, false, 64020)] + [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, true, 16200)] + public void Encode_Lossy_WithAlpha_Works(TestImageProvider provider, bool compressed, int expectedFileSize) where TPixel : unmanaged, IPixel { var encoder = new WebpEncoder() @@ -281,33 +281,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp }; using Image image = provider.GetImage(); - image.VerifyEncoder( + string encodedFile = image.VerifyEncoder( provider, "webp", $"with_alpha_compressed_{compressed}", encoder, ImageComparer.Tolerant(0.04f), referenceDecoder: new MagickReferenceDecoder()); - } - - [Theory] - [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32)] - public void Encode_Lossy_WithCompressedAlpha_AlphaShouldBeSmaller(TestImageProvider provider) - where TPixel : unmanaged, IPixel - { - var encoder = new WebpEncoder() - { - FileFormat = WebpFileFormatType.Lossy, - UseAlphaCompression = true - }; - using Image image = provider.GetImage(); - using (var memoryStream = new MemoryStream()) - { - image.SaveAsWebp(memoryStream, encoder); - int size = memoryStream.ToArray().Length; - Assert.True(size < 16300); - } + int encodedBytes = File.ReadAllBytes(encodedFile).Length; + Assert.True(encodedBytes <= expectedFileSize); } [Theory] diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 719e52946..3d27e4aae 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -661,7 +661,8 @@ namespace SixLabors.ImageSharp.Tests /// Loads the expected image with a reference decoder + compares it to . /// Also performs a debug save using . /// - internal static void VerifyEncoder( + /// The path to the encoded output file. + internal static string VerifyEncoder( this Image image, ITestImageProvider provider, string extension, @@ -687,6 +688,8 @@ namespace SixLabors.ImageSharp.Tests ImageComparer comparer = customComparer ?? ImageComparer.Exact; comparer.VerifySimilarity(encodedImage, image); } + + return actualOutputFile; } internal static AllocatorBufferCapacityConfigurator LimitAllocatorBufferCapacity(