Browse Source

Add webp to the default configuration

pull/1552/head
Brian Popow 5 years ago
parent
commit
795111be30
  1. 33
      src/ImageSharp/Configuration.cs
  2. 23
      src/ImageSharp/Formats/WebP/ConfigurationExtensions.cs
  3. 6
      tests/ImageSharp.Benchmarks/Codecs/DecodeWebp.cs
  4. 2
      tests/ImageSharp.Tests/ConfigurationTests.cs
  5. 3
      tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs
  6. 40
      tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs
  7. 10
      tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs
  8. 12
      tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs

33
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 <see cref="Configuration"/>.
/// </summary>
/// <returns>A new configuration instance.</returns>
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,
};
/// <summary>
/// Creates the default instance with the following <see cref="IConfigurationModule"/>s preregistered:
@ -182,17 +180,16 @@ namespace SixLabors.ImageSharp
/// <see cref="BmpConfigurationModule"/>.
/// <see cref="TgaConfigurationModule"/>.
/// <see cref="TiffConfigurationModule"/>.
/// <see cref="WebpConfigurationModule"/>.
/// </summary>
/// <returns>The default configuration of <see cref="Configuration"/>.</returns>
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());
}
}

23
src/ImageSharp/Formats/WebP/ConfigurationExtensions.cs

@ -1,23 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Formats.Webp
{
/// <summary>
/// Helper methods for the Configuration.
/// </summary>
public static class ConfigurationExtensions
{
/// <summary>
/// Registers the webp format detector, encoder and decoder.
/// </summary>
/// <param name="configuration">The configuration.</param>
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());
}
}
}

6
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;
}

2
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()
{

3
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<GifEncoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<TgaEncoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<TiffEncoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<WebpEncoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<PngDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
@ -45,6 +47,7 @@ namespace SixLabors.ImageSharp.Tests.Formats
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<TgaDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<TiffDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<WebpDecoder>().Count());
}
[Fact]

40
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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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<Rgba32>(this.configuration, 10, 10))
using (var image = new Image<Rgba32>(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);
}

10
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);

12
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<Rgba32> image = WebpDecoder.Decode<Rgba32>(this.configuration, memoryStream);
using var image = Image.Load<Rgba32>(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<Rgba32> image = WebpDecoder.Decode<Rgba32>(this.configuration, memoryStream);
using var image = Image.Load<Rgba32>(memoryStream);
ExifProfile actualExif = image.Metadata.ExifProfile;
Assert.NotNull(actualExif);
Assert.Equal(expectedExif.Values.Count, actualExif.Values.Count);

Loading…
Cancel
Save