diff --git a/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs b/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs index 5059ac73e1..7dbf49d45e 100644 --- a/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs +++ b/src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs @@ -9,10 +9,9 @@ namespace SixLabors.ImageSharp.Formats.Webp internal interface IWebpEncoderOptions { /// - /// Gets a value indicating whether lossy compression should be used. - /// If false, lossless compression will be used. + /// Gets the webp file format used. Either lossless or lossy. /// - bool? Lossy { get; } + WebpFileFormatType? FileFormat { get; } /// /// Gets the compression quality. Between 0 and 100. diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 4a5a15b1ca..44a55a4c65 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -262,7 +262,7 @@ namespace SixLabors.ImageSharp.Formats.Webp /// Information about this webp image. private WebpImageInfo ReadVp8Header(WebpFeatures features = null) { - this.webpMetadata.Format = WebpFormatType.Lossy; + this.webpMetadata.FileFormat = WebpFileFormatType.Lossy; // VP8 data size (not including this 4 bytes). this.currentStream.Read(this.buffer, 0, 4); @@ -367,7 +367,7 @@ namespace SixLabors.ImageSharp.Formats.Webp /// Information about this image. private WebpImageInfo ReadVp8LHeader(WebpFeatures features = null) { - this.webpMetadata.Format = WebpFormatType.Lossless; + this.webpMetadata.FileFormat = WebpFileFormatType.Lossless; // VP8 data size. uint imageDataSize = this.ReadChunkSize(); diff --git a/src/ImageSharp/Formats/Webp/WebpEncoder.cs b/src/ImageSharp/Formats/Webp/WebpEncoder.cs index 1667eb1db9..f85f65b635 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoder.cs @@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Formats.Webp public sealed class WebpEncoder : IImageEncoder, IWebpEncoderOptions { /// - public bool? Lossy { get; set; } + public WebpFileFormatType? FileFormat { get; set; } /// public int Quality { get; set; } = 75; diff --git a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs index 3be5409b11..a61fc72530 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs @@ -71,9 +71,9 @@ namespace SixLabors.ImageSharp.Formats.Webp private readonly int nearLosslessQuality; /// - /// Indicating whether lossy compression should be used. If false, lossless compression will be used. + /// Indicating what file format compression should be used. /// - private bool? lossy; + private readonly WebpFileFormatType? fileFormat; /// /// The global configuration. @@ -89,7 +89,7 @@ namespace SixLabors.ImageSharp.Formats.Webp { this.memoryAllocator = memoryAllocator; this.alphaCompression = options.UseAlphaCompression; - this.lossy = options.Lossy; + this.fileFormat = options.FileFormat; this.quality = options.Quality; this.method = options.Method; this.entropyPasses = options.EntropyPasses; @@ -114,11 +114,18 @@ namespace SixLabors.ImageSharp.Formats.Webp Guard.NotNull(stream, nameof(stream)); this.configuration = image.GetConfiguration(); - ImageMetadata metadata = image.Metadata; - WebpMetadata webpMetadata = metadata.GetWebpMetadata(); - this.lossy ??= webpMetadata.Format == WebpFormatType.Lossy; + bool lossy; + if (this.fileFormat is not null) + { + lossy = this.fileFormat == WebpFileFormatType.Lossy; + } + else + { + WebpMetadata webpMetadata = image.Metadata.GetWebpMetadata(); + lossy = webpMetadata.FileFormat == WebpFileFormatType.Lossy; + } - if (this.lossy.GetValueOrDefault()) + if (lossy) { using var enc = new Vp8Encoder( this.memoryAllocator, diff --git a/src/ImageSharp/Formats/Webp/WebpFormatType.cs b/src/ImageSharp/Formats/Webp/WebpFileFormatType.cs similarity index 82% rename from src/ImageSharp/Formats/Webp/WebpFormatType.cs rename to src/ImageSharp/Formats/Webp/WebpFileFormatType.cs index 5ef47f6e0f..c485f09693 100644 --- a/src/ImageSharp/Formats/Webp/WebpFormatType.cs +++ b/src/ImageSharp/Formats/Webp/WebpFileFormatType.cs @@ -4,9 +4,9 @@ namespace SixLabors.ImageSharp.Formats.Webp { /// - /// Info about the webp format used. + /// Info about the webp file format used. /// - public enum WebpFormatType + public enum WebpFileFormatType { /// /// The lossless webp format. diff --git a/src/ImageSharp/Formats/Webp/WebpMetadata.cs b/src/ImageSharp/Formats/Webp/WebpMetadata.cs index 6c44688b24..f398d3d874 100644 --- a/src/ImageSharp/Formats/Webp/WebpMetadata.cs +++ b/src/ImageSharp/Formats/Webp/WebpMetadata.cs @@ -19,12 +19,12 @@ namespace SixLabors.ImageSharp.Formats.Webp /// Initializes a new instance of the class. /// /// The metadata to create an instance from. - private WebpMetadata(WebpMetadata other) => this.Format = other.Format; + private WebpMetadata(WebpMetadata other) => this.FileFormat = other.FileFormat; /// - /// Gets or sets the webp format used. Either lossless or lossy. + /// Gets or sets the webp file format used. Either lossless or lossy. /// - public WebpFormatType? Format { get; set; } + public WebpFileFormatType? FileFormat { get; set; } /// public IDeepCloneable DeepClone() => new WebpMetadata(this); diff --git a/tests/ImageSharp.Benchmarks/Codecs/EncodeWebp.cs b/tests/ImageSharp.Benchmarks/Codecs/EncodeWebp.cs index 8f3869a529..7d3dfe693c 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/EncodeWebp.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/EncodeWebp.cs @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs using var memoryStream = new MemoryStream(); this.webp.Save(memoryStream, new WebpEncoder() { - Lossy = true + FileFormat = WebpFileFormatType.Lossy }); } @@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs using var memoryStream = new MemoryStream(); this.webp.Save(memoryStream, new WebpEncoder() { - Lossy = false + FileFormat = WebpFileFormatType.Lossless }); } diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs index de5a6171a5..70cc487bfe 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs @@ -16,10 +16,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp public class WebpEncoderTests { [Theory] - [WithFile(Flag, PixelTypes.Rgba32, WebpFormatType.Lossless)] // if its not a webp input image, it should default to lossless. - [WithFile(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFormatType.Lossless)] - [WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFormatType.Lossy)] - public void Encode_PreserveRatio(TestImageProvider provider, WebpFormatType expectedFormat) + [WithFile(Flag, PixelTypes.Rgba32, WebpFileFormatType.Lossless)] // if its not a webp input image, it should default to lossless. + [WithFile(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFileFormatType.Lossless)] + [WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFileFormatType.Lossy)] + public void Encode_PreserveRatio(TestImageProvider provider, WebpFileFormatType expectedFormat) where TPixel : unmanaged, IPixel { var options = new WebpEncoder(); @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp ImageMetadata meta = output.Metadata; WebpMetadata webpMetaData = meta.GetWebpMetadata(); - Assert.Equal(expectedFormat, webpMetaData.Format); + Assert.Equal(expectedFormat, webpMetaData.FileFormat); } [Theory] @@ -44,7 +44,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = false, + FileFormat = WebpFileFormatType.Lossless, Quality = 100, Method = WebpEncodingMethod.BestQuality }; @@ -62,7 +62,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = false, + FileFormat = WebpFileFormatType.Lossless, Quality = quality }; @@ -91,7 +91,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = false, + FileFormat = WebpFileFormatType.Lossless, Method = method, Quality = quality }; @@ -113,7 +113,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = false, + FileFormat = WebpFileFormatType.Lossless, NearLossless = true, NearLosslessQuality = nearLosslessQuality }; @@ -137,7 +137,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = false, + FileFormat = WebpFileFormatType.Lossless, Method = method, TransparentColorMode = WebpTransparentColorMode.Preserve }; @@ -155,7 +155,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { using Image image = provider.GetImage(); - var encoder = new WebpEncoder() { Lossy = false }; + var encoder = new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless }; image.VerifyEncoder(provider, "webp", string.Empty, encoder); } @@ -164,7 +164,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { // Just make sure, encoding 1 pixel by 1 pixel does not throw an exception. using var image = new Image(1, 1); - var encoder = new WebpEncoder() { Lossy = false }; + var encoder = new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless }; using (var memStream = new MemoryStream()) { image.SaveAsWebp(memStream, encoder); @@ -180,7 +180,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = true, + FileFormat = WebpFileFormatType.Lossy, Quality = quality }; @@ -200,7 +200,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = true, + FileFormat = WebpFileFormatType.Lossy, FilterStrength = filterStrength }; @@ -220,7 +220,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = true, + FileFormat = WebpFileFormatType.Lossy, SpatialNoiseShaping = snsStrength }; @@ -249,7 +249,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { var encoder = new WebpEncoder() { - Lossy = true, + FileFormat = WebpFileFormatType.Lossy, Method = method, Quality = quality }; @@ -267,7 +267,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp { using Image image = provider.GetImage(); - var encoder = new WebpEncoder() { Lossy = true }; + var encoder = new WebpEncoder() { FileFormat = WebpFileFormatType.Lossy }; image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.Tolerant(0.04f)); } diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs index 1b0dc3e3fc..81067a41f5 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [Trait("Format", "Webp")] public class WebpMetaDataTests { - private static WebpDecoder WebpDecoder => new WebpDecoder() { IgnoreMetadata = false }; + private static WebpDecoder WebpDecoder => new() { IgnoreMetadata = false }; [Theory] [WithFile(TestImages.Webp.Lossy.WithExif, PixelTypes.Rgba32, false)] @@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp ExifProfile expectedExif = input.Metadata.ExifProfile; // act - input.Save(memoryStream, new WebpEncoder() { Lossy = true }); + input.Save(memoryStream, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossy }); memoryStream.Position = 0; // assert @@ -95,7 +95,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp ExifProfile expectedExif = input.Metadata.ExifProfile; // act - input.Save(memoryStream, new WebpEncoder() { Lossy = false }); + input.Save(memoryStream, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless }); memoryStream.Position = 0; // assert diff --git a/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs index b14938ca8b..ebc0968524 100644 --- a/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs @@ -506,9 +506,9 @@ namespace SixLabors.ImageSharp.Tests.Metadata.Profiles.Exif case TestImageWriteFormat.Png: return WriteAndReadPng(image); case TestImageWriteFormat.WebpLossless: - return WriteAndReadWebp(image, lossy: false); + return WriteAndReadWebp(image, WebpFileFormatType.Lossless); case TestImageWriteFormat.WebpLossy: - return WriteAndReadWebp(image, lossy: true); + return WriteAndReadWebp(image, WebpFileFormatType.Lossy); default: throw new ArgumentException("Unexpected test image format, only Jpeg and Png are allowed"); } @@ -538,11 +538,11 @@ namespace SixLabors.ImageSharp.Tests.Metadata.Profiles.Exif } } - private static Image WriteAndReadWebp(Image image, bool lossy) + private static Image WriteAndReadWebp(Image image, WebpFileFormatType fileFormat) { using (var memStream = new MemoryStream()) { - image.SaveAsWebp(memStream, new WebpEncoder() { Lossy = lossy }); + image.SaveAsWebp(memStream, new WebpEncoder() { FileFormat = fileFormat }); image.Dispose(); memStream.Position = 0;