From 795111be30a8266a7ff36b8da886cda01766ac55 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 17 Aug 2021 09:22:20 +0200 Subject: [PATCH] Add webp to the default configuration --- src/ImageSharp/Configuration.cs | 33 +++++++-------- .../Formats/WebP/ConfigurationExtensions.cs | 23 ----------- .../Codecs/DecodeWebp.cs | 6 ++- tests/ImageSharp.Tests/ConfigurationTests.cs | 2 +- .../Formats/ImageFormatManagerTests.cs | 3 ++ .../Formats/WebP/ImageExtensionsTests.cs | 40 ++++++++----------- .../Formats/WebP/WebpDecoderTests.cs | 10 +---- .../Formats/WebP/WebpMetaDataTests.cs | 12 +----- 8 files changed, 42 insertions(+), 87 deletions(-) delete mode 100644 src/ImageSharp/Formats/WebP/ConfigurationExtensions.cs diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 49b7aa79b0..ea9524827f 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -11,6 +11,7 @@ using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.Formats.Tiff; +using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Processing; @@ -159,20 +160,17 @@ namespace SixLabors.ImageSharp /// Creates a shallow copy of the . /// /// A new configuration instance. - public Configuration Clone() + public Configuration Clone() => new Configuration { - return new Configuration - { - MaxDegreeOfParallelism = this.MaxDegreeOfParallelism, - StreamProcessingBufferSize = this.StreamProcessingBufferSize, - ImageFormatsManager = this.ImageFormatsManager, - MemoryAllocator = this.MemoryAllocator, - ImageOperationsProvider = this.ImageOperationsProvider, - ReadOrigin = this.ReadOrigin, - FileSystem = this.FileSystem, - WorkingBufferSizeHintInBytes = this.WorkingBufferSizeHintInBytes, - }; - } + MaxDegreeOfParallelism = this.MaxDegreeOfParallelism, + StreamProcessingBufferSize = this.StreamProcessingBufferSize, + ImageFormatsManager = this.ImageFormatsManager, + MemoryAllocator = this.MemoryAllocator, + ImageOperationsProvider = this.ImageOperationsProvider, + ReadOrigin = this.ReadOrigin, + FileSystem = this.FileSystem, + WorkingBufferSizeHintInBytes = this.WorkingBufferSizeHintInBytes, + }; /// /// Creates the default instance with the following s preregistered: @@ -182,17 +180,16 @@ namespace SixLabors.ImageSharp /// . /// . /// . + /// . /// /// The default configuration of . - internal static Configuration CreateDefaultInstance() - { - return new Configuration( + internal static Configuration CreateDefaultInstance() => new Configuration( new PngConfigurationModule(), new JpegConfigurationModule(), new GifConfigurationModule(), new BmpConfigurationModule(), new TgaConfigurationModule(), - new TiffConfigurationModule()); - } + new TiffConfigurationModule(), + new WebpConfigurationModule()); } } diff --git a/src/ImageSharp/Formats/WebP/ConfigurationExtensions.cs b/src/ImageSharp/Formats/WebP/ConfigurationExtensions.cs deleted file mode 100644 index 5a8f178da4..0000000000 --- a/src/ImageSharp/Formats/WebP/ConfigurationExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -namespace SixLabors.ImageSharp.Formats.Webp -{ - /// - /// Helper methods for the Configuration. - /// - public static class ConfigurationExtensions - { - /// - /// Registers the webp format detector, encoder and decoder. - /// - /// The configuration. - public static void AddWebp(this Configuration configuration) - { - configuration.ImageFormatsManager.AddImageFormat(WebpFormat.Instance); - configuration.ImageFormatsManager.AddImageFormatDetector(new WebpImageFormatDetector()); - configuration.ImageFormatsManager.SetDecoder(WebpFormat.Instance, new WebpDecoder()); - configuration.ImageFormatsManager.SetEncoder(WebpFormat.Instance, new WebpEncoder()); - } - } -} diff --git a/tests/ImageSharp.Benchmarks/Codecs/DecodeWebp.cs b/tests/ImageSharp.Benchmarks/Codecs/DecodeWebp.cs index fedc22eb1f..98e1f86897 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/DecodeWebp.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/DecodeWebp.cs @@ -46,7 +46,8 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs public int WebpLossyMagick() { var settings = new MagickReadSettings { Format = MagickFormat.WebP }; - using var image = new MagickImage(new MemoryStream(this.webpLossyBytes), settings); + using var memoryStream = new MemoryStream(this.webpLossyBytes); + using var image = new MagickImage(memoryStream, settings); return image.Width; } @@ -62,7 +63,8 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs public int WebpLosslessMagick() { var settings = new MagickReadSettings { Format = MagickFormat.WebP }; - using var image = new MagickImage(new MemoryStream(this.webpLosslessBytes), settings); + using var memoryStream = new MemoryStream(this.webpLossyBytes); + using var image = new MagickImage(memoryStream, settings); return image.Width; } diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index 3ad8ef2f8a..803babdfa5 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Tests public Configuration DefaultConfiguration { get; } - private readonly int expectedDefaultConfigurationCount = 6; + private readonly int expectedDefaultConfigurationCount = 7; public ConfigurationTests() { diff --git a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs index 1e00bfff8c..5cd70b1001 100644 --- a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs +++ b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs @@ -12,6 +12,7 @@ using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.Formats.Tiff; +using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.PixelFormats; using Xunit; @@ -38,6 +39,7 @@ namespace SixLabors.ImageSharp.Tests.Formats Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); + Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); @@ -45,6 +47,7 @@ namespace SixLabors.ImageSharp.Tests.Formats Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); + Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); } [Fact] diff --git a/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs b/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs index 31fc1919c4..a17248612d 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs @@ -13,26 +13,18 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [Trait("Format", "Webp")] public class ImageExtensionsTests { - private readonly Configuration configuration; - - public ImageExtensionsTests() - { - this.configuration = new Configuration(); - this.configuration.AddWebp(); - } - [Fact] public void SaveAsWebp_Path() { string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTests)); string file = Path.Combine(dir, "SaveAsWebp_Path.webp"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsWebp(file); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } @@ -44,12 +36,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTests)); string file = Path.Combine(dir, "SaveAsWebpAsync_Path.webp"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsWebpAsync(file); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } @@ -61,12 +53,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsWebp_Path_Encoder.webp"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsWebp(file, new WebpEncoder()); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } @@ -78,12 +70,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsWebpAsync_Path_Encoder.webp"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsWebpAsync(file, new WebpEncoder()); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } @@ -94,14 +86,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsWebp(memoryStream); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } @@ -112,14 +104,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsWebpAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } @@ -130,14 +122,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsWebp(memoryStream, new WebpEncoder()); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } @@ -148,14 +140,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsWebpAsync(memoryStream, new WebpEncoder()); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/webp", mime.DefaultMimeType); } diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs index 33f7930d16..262e1724b3 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs @@ -18,18 +18,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [Trait("Format", "Webp")] public class WebpDecoderTests { - private readonly Configuration configuration; - private static WebpDecoder WebpDecoder => new WebpDecoder(); private static MagickReferenceDecoder ReferenceDecoder => new MagickReferenceDecoder(); - public WebpDecoderTests() - { - this.configuration = new Configuration(); - this.configuration.AddWebp(); - } - [Theory] [InlineData(Lossless.GreenTransform1, 1000, 307, 32)] [InlineData(Lossless.BikeThreeTransforms, 250, 195, 32)] @@ -46,7 +38,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp var testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - IImageInfo imageInfo = Image.Identify(this.configuration, stream); + IImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedWidth, imageInfo.Width); Assert.Equal(expectedHeight, imageInfo.Height); diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs index 34ff9a1f5a..901da3dbd8 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs @@ -13,16 +13,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [Trait("Format", "Webp")] public class WebpMetaDataTests { - private readonly Configuration configuration; - private static WebpDecoder WebpDecoder => new WebpDecoder() { IgnoreMetadata = false }; - public WebpMetaDataTests() - { - this.configuration = new Configuration(); - this.configuration.AddWebp(); - } - [Theory] [WithFile(TestImages.WebP.Lossy.WithExif, PixelTypes.Rgba32, false)] [WithFile(TestImages.WebP.Lossy.WithExif, PixelTypes.Rgba32, true)] @@ -86,7 +78,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp memoryStream.Position = 0; // assert - using Image image = WebpDecoder.Decode(this.configuration, memoryStream); + using var image = Image.Load(memoryStream); ExifProfile actualExif = image.Metadata.ExifProfile; Assert.NotNull(actualExif); Assert.Equal(expectedExif.Values.Count, actualExif.Values.Count); @@ -107,7 +99,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp memoryStream.Position = 0; // assert - using Image image = WebpDecoder.Decode(this.configuration, memoryStream); + using var image = Image.Load(memoryStream); ExifProfile actualExif = image.Metadata.ExifProfile; Assert.NotNull(actualExif); Assert.Equal(expectedExif.Values.Count, actualExif.Values.Count);