Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal interface IWebpEncoderOptions

/// <summary>
/// Gets the number of entropy-analysis passes (in [1..10]).
/// Defaults to 1.
/// </summary>
int EntropyPasses { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Webp/WebpEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed class WebpEncoder : IImageEncoder, IWebpEncoderOptions
public bool UseAlphaCompression { get; set; }

/// <inheritdoc/>
public int EntropyPasses { get; set; }
public int EntropyPasses { get; set; } = 1;

/// <inheritdoc/>
public int SpatialNoiseShaping { get; set; } = 50;
Expand Down
45 changes: 39 additions & 6 deletions tests/ImageSharp.Benchmarks/Codecs/EncodeWebp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using BenchmarkDotNet.Attributes;
using ImageMagick;
using ImageMagick.Formats;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests;
Expand Down Expand Up @@ -44,8 +45,22 @@ public void Cleanup()
public void MagickWebpLossy()
{
using var memoryStream = new MemoryStream();
this.webpMagick.Settings.SetDefine(MagickFormat.WebP, "lossless", false);
this.webpMagick.Write(memoryStream, MagickFormat.WebP);

var defines = new WebPWriteDefines
{
Lossless = false,
Method = 4,
AlphaCompression = WebPAlphaCompression.None,
FilterStrength = 60,
SnsStrength = 50,
Pass = 1,

// 100 means off.
NearLossless = 100
};

this.webpMagick.Settings.SetDefine(MagickFormat.WebP, "quality", 75);
this.webpMagick.Write(memoryStream, defines);
}

[Benchmark(Description = "ImageSharp Webp Lossy")]
Expand All @@ -54,16 +69,31 @@ public void ImageSharpWebpLossy()
using var memoryStream = new MemoryStream();
this.webp.Save(memoryStream, new WebpEncoder()
{
FileFormat = WebpFileFormatType.Lossy
FileFormat = WebpFileFormatType.Lossy,
Method = WebpEncodingMethod.Level4,
UseAlphaCompression = false,
FilterStrength = 60,
SpatialNoiseShaping = 50,
EntropyPasses = 1
});
}

[Benchmark(Baseline = true, Description = "Magick Webp Lossless")]
public void MagickWebpLossless()
{
using var memoryStream = new MemoryStream();
this.webpMagick.Settings.SetDefine(MagickFormat.WebP, "lossless", true);
this.webpMagick.Write(memoryStream, MagickFormat.WebP);
var defines = new WebPWriteDefines
{
Lossless = true,
Method = 4,

// 100 means off.
NearLossless = 100
};

this.webpMagick.Settings.SetDefine(MagickFormat.WebP, "exact", false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dlemstra the exact and quality parameters seem to be missing in WebPWriteDefines. Should they maybe be added to WebPWriteDefines? If you think that makes sense, i could open a PR at Magick.Net.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need to exact option. You can use this.webpMagick.Settings.Quality to set the quality. Setting the quality too 100 might enable this?

Copy link
Collaborator Author

@brianpopow brianpopow Nov 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i dont set it, the libwebp default will be used, right? The default is false.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IMagickSettings interface does not seem to have a Quality Property.

Copy link
Member

@dlemstra dlemstra Nov 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry my bad, that should be webpMagick.Quality instead. And I just added the Exact property to the WebPWriteDefines. This will become available in the next release. And setting the Quality to 100 will make it lossless.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry my bad, that should be webpMagick.Quality instead. And I just added the Exact property to the WebPWriteDefines. This will become available in the next release. And setting the Quality to 100 will make it lossless.

No problem, thanks for adding exact to WebPWriteDefines

this.webpMagick.Settings.SetDefine(MagickFormat.WebP, "quality", 75);
this.webpMagick.Write(memoryStream, defines);
}

[Benchmark(Description = "ImageSharp Webp Lossless")]
Expand All @@ -72,7 +102,10 @@ public void ImageSharpWebpLossless()
using var memoryStream = new MemoryStream();
this.webp.Save(memoryStream, new WebpEncoder()
{
FileFormat = WebpFileFormatType.Lossless
FileFormat = WebpFileFormatType.Lossless,
Method = WebpEncodingMethod.Level4,
NearLossless = false,
TransparentColorMode = WebpTransparentColorMode.Clear
});
}

Expand Down