Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webp encoder now defaults to lossy, if nothing else is specified #1961

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -10,6 +10,7 @@ internal interface IWebpEncoderOptions
{
/// <summary>
/// Gets the webp file format used. Either lossless or lossy.
/// Defaults to lossy.
/// </summary>
WebpFileFormatType? FileFormat { get; }

Expand Down
25 changes: 13 additions & 12 deletions src/ImageSharp/Formats/Webp/WebpEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals

/// <summary>
/// Indicating what file format compression should be used.
/// Defaults to lossy.
/// </summary>
private readonly WebpFileFormatType? fileFormat;

Expand Down Expand Up @@ -112,43 +113,43 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
Guard.NotNull(stream, nameof(stream));

this.configuration = image.GetConfiguration();
bool lossy;
bool lossless;
if (this.fileFormat is not null)
{
lossy = this.fileFormat == WebpFileFormatType.Lossy;
lossless = this.fileFormat == WebpFileFormatType.Lossless;
}
else
{
WebpMetadata webpMetadata = image.Metadata.GetWebpMetadata();
lossy = webpMetadata.FileFormat == WebpFileFormatType.Lossy;
lossless = webpMetadata.FileFormat == WebpFileFormatType.Lossless;
}

if (lossy)
if (lossless)
{
using var enc = new Vp8Encoder(
using var enc = new Vp8LEncoder(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.method,
this.entropyPasses,
this.filterStrength,
this.spatialNoiseShaping);
this.transparentColorMode,
this.nearLossless,
this.nearLosslessQuality);
enc.Encode(image, stream);
}
else
{
using var enc = new Vp8LEncoder(
using var enc = new Vp8Encoder(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.method,
this.transparentColorMode,
this.nearLossless,
this.nearLosslessQuality);
this.entropyPasses,
this.filterStrength,
this.spatialNoiseShaping);
enc.Encode(image, stream);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class WebpEncoderTests
private static string TestImageLossyFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, Lossy.NoFilter06);

[Theory]
[WithFile(Flag, PixelTypes.Rgba32, WebpFileFormatType.Lossless)] // if its not a webp input image, it should default to lossless.
[WithFile(Flag, PixelTypes.Rgba32, WebpFileFormatType.Lossy)] // If its not a webp input image, it should default to lossy.
[WithFile(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFileFormatType.Lossless)]
[WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFileFormatType.Lossy)]
public void Encode_PreserveRatio<TPixel>(TestImageProvider<TPixel> provider, WebpFileFormatType expectedFormat)
Expand Down