Browse Source

Merge pull request #1961 from SixLabors/bp/webplossydefault

Webp encoder now defaults to lossy, if nothing else is specified
pull/1970/head
Brian Popow 4 years ago
committed by GitHub
parent
commit
f15bc74a1a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs
  2. 25
      src/ImageSharp/Formats/Webp/WebpEncoderCore.cs
  3. 2
      tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

1
src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs

@ -10,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
{ {
/// <summary> /// <summary>
/// Gets the webp file format used. Either lossless or lossy. /// Gets the webp file format used. Either lossless or lossy.
/// Defaults to lossy.
/// </summary> /// </summary>
WebpFileFormatType? FileFormat { get; } WebpFileFormatType? FileFormat { get; }

25
src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

@ -70,6 +70,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// <summary> /// <summary>
/// Indicating what file format compression should be used. /// Indicating what file format compression should be used.
/// Defaults to lossy.
/// </summary> /// </summary>
private readonly WebpFileFormatType? fileFormat; private readonly WebpFileFormatType? fileFormat;
@ -112,43 +113,43 @@ namespace SixLabors.ImageSharp.Formats.Webp
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
this.configuration = image.GetConfiguration(); this.configuration = image.GetConfiguration();
bool lossy; bool lossless;
if (this.fileFormat is not null) if (this.fileFormat is not null)
{ {
lossy = this.fileFormat == WebpFileFormatType.Lossy; lossless = this.fileFormat == WebpFileFormatType.Lossless;
} }
else else
{ {
WebpMetadata webpMetadata = image.Metadata.GetWebpMetadata(); 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.memoryAllocator,
this.configuration, this.configuration,
image.Width, image.Width,
image.Height, image.Height,
this.quality, this.quality,
this.method, this.method,
this.entropyPasses, this.transparentColorMode,
this.filterStrength, this.nearLossless,
this.spatialNoiseShaping); this.nearLosslessQuality);
enc.Encode(image, stream); enc.Encode(image, stream);
} }
else else
{ {
using var enc = new Vp8LEncoder( using var enc = new Vp8Encoder(
this.memoryAllocator, this.memoryAllocator,
this.configuration, this.configuration,
image.Width, image.Width,
image.Height, image.Height,
this.quality, this.quality,
this.method, this.method,
this.transparentColorMode, this.entropyPasses,
this.nearLossless, this.filterStrength,
this.nearLosslessQuality); this.spatialNoiseShaping);
enc.Encode(image, stream); enc.Encode(image, stream);
} }
} }

2
tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
private static string TestImageLossyFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, Lossy.NoFilter06); private static string TestImageLossyFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, Lossy.NoFilter06);
[Theory] [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(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFileFormatType.Lossless)]
[WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFileFormatType.Lossy)] [WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFileFormatType.Lossy)]
public void Encode_PreserveRatio<TPixel>(TestImageProvider<TPixel> provider, WebpFileFormatType expectedFormat) public void Encode_PreserveRatio<TPixel>(TestImageProvider<TPixel> provider, WebpFileFormatType expectedFormat)

Loading…
Cancel
Save