Browse Source

Use the same property type for metadata & encoder

pull/1552/head
James Jackson-South 5 years ago
parent
commit
853c40c23d
  1. 5
      src/ImageSharp/Formats/Webp/IWebpEncoderOptions.cs
  2. 4
      src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
  3. 2
      src/ImageSharp/Formats/Webp/WebpEncoder.cs
  4. 21
      src/ImageSharp/Formats/Webp/WebpEncoderCore.cs
  5. 4
      src/ImageSharp/Formats/Webp/WebpFileFormatType.cs
  6. 6
      src/ImageSharp/Formats/Webp/WebpMetadata.cs
  7. 4
      tests/ImageSharp.Benchmarks/Codecs/EncodeWebp.cs
  8. 34
      tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs
  9. 6
      tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs
  10. 8
      tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs

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

@ -9,10 +9,9 @@ namespace SixLabors.ImageSharp.Formats.Webp
internal interface IWebpEncoderOptions internal interface IWebpEncoderOptions
{ {
/// <summary> /// <summary>
/// Gets a value indicating whether lossy compression should be used. /// Gets the webp file format used. Either lossless or lossy.
/// If false, lossless compression will be used.
/// </summary> /// </summary>
bool? Lossy { get; } WebpFileFormatType? FileFormat { get; }
/// <summary> /// <summary>
/// Gets the compression quality. Between 0 and 100. /// Gets the compression quality. Between 0 and 100.

4
src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

@ -262,7 +262,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// <returns>Information about this webp image.</returns> /// <returns>Information about this webp image.</returns>
private WebpImageInfo ReadVp8Header(WebpFeatures features = null) private WebpImageInfo ReadVp8Header(WebpFeatures features = null)
{ {
this.webpMetadata.Format = WebpFormatType.Lossy; this.webpMetadata.FileFormat = WebpFileFormatType.Lossy;
// VP8 data size (not including this 4 bytes). // VP8 data size (not including this 4 bytes).
this.currentStream.Read(this.buffer, 0, 4); this.currentStream.Read(this.buffer, 0, 4);
@ -367,7 +367,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// <returns>Information about this image.</returns> /// <returns>Information about this image.</returns>
private WebpImageInfo ReadVp8LHeader(WebpFeatures features = null) private WebpImageInfo ReadVp8LHeader(WebpFeatures features = null)
{ {
this.webpMetadata.Format = WebpFormatType.Lossless; this.webpMetadata.FileFormat = WebpFileFormatType.Lossless;
// VP8 data size. // VP8 data size.
uint imageDataSize = this.ReadChunkSize(); uint imageDataSize = this.ReadChunkSize();

2
src/ImageSharp/Formats/Webp/WebpEncoder.cs

@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
public sealed class WebpEncoder : IImageEncoder, IWebpEncoderOptions public sealed class WebpEncoder : IImageEncoder, IWebpEncoderOptions
{ {
/// <inheritdoc/> /// <inheritdoc/>
public bool? Lossy { get; set; } public WebpFileFormatType? FileFormat { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public int Quality { get; set; } = 75; public int Quality { get; set; } = 75;

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

@ -71,9 +71,9 @@ namespace SixLabors.ImageSharp.Formats.Webp
private readonly int nearLosslessQuality; private readonly int nearLosslessQuality;
/// <summary> /// <summary>
/// Indicating whether lossy compression should be used. If false, lossless compression will be used. /// Indicating what file format compression should be used.
/// </summary> /// </summary>
private bool? lossy; private readonly WebpFileFormatType? fileFormat;
/// <summary> /// <summary>
/// The global configuration. /// The global configuration.
@ -89,7 +89,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
{ {
this.memoryAllocator = memoryAllocator; this.memoryAllocator = memoryAllocator;
this.alphaCompression = options.UseAlphaCompression; this.alphaCompression = options.UseAlphaCompression;
this.lossy = options.Lossy; this.fileFormat = options.FileFormat;
this.quality = options.Quality; this.quality = options.Quality;
this.method = options.Method; this.method = options.Method;
this.entropyPasses = options.EntropyPasses; this.entropyPasses = options.EntropyPasses;
@ -114,11 +114,18 @@ namespace SixLabors.ImageSharp.Formats.Webp
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
this.configuration = image.GetConfiguration(); this.configuration = image.GetConfiguration();
ImageMetadata metadata = image.Metadata; bool lossy;
WebpMetadata webpMetadata = metadata.GetWebpMetadata(); if (this.fileFormat is not null)
this.lossy ??= webpMetadata.Format == WebpFormatType.Lossy; {
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( using var enc = new Vp8Encoder(
this.memoryAllocator, this.memoryAllocator,

4
src/ImageSharp/Formats/Webp/WebpFormatType.cs → src/ImageSharp/Formats/Webp/WebpFileFormatType.cs

@ -4,9 +4,9 @@
namespace SixLabors.ImageSharp.Formats.Webp namespace SixLabors.ImageSharp.Formats.Webp
{ {
/// <summary> /// <summary>
/// Info about the webp format used. /// Info about the webp file format used.
/// </summary> /// </summary>
public enum WebpFormatType public enum WebpFileFormatType
{ {
/// <summary> /// <summary>
/// The lossless webp format. /// The lossless webp format.

6
src/ImageSharp/Formats/Webp/WebpMetadata.cs

@ -19,12 +19,12 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// Initializes a new instance of the <see cref="WebpMetadata"/> class. /// Initializes a new instance of the <see cref="WebpMetadata"/> class.
/// </summary> /// </summary>
/// <param name="other">The metadata to create an instance from.</param> /// <param name="other">The metadata to create an instance from.</param>
private WebpMetadata(WebpMetadata other) => this.Format = other.Format; private WebpMetadata(WebpMetadata other) => this.FileFormat = other.FileFormat;
/// <summary> /// <summary>
/// Gets or sets the webp format used. Either lossless or lossy. /// Gets or sets the webp file format used. Either lossless or lossy.
/// </summary> /// </summary>
public WebpFormatType? Format { get; set; } public WebpFileFormatType? FileFormat { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public IDeepCloneable DeepClone() => new WebpMetadata(this); public IDeepCloneable DeepClone() => new WebpMetadata(this);

4
tests/ImageSharp.Benchmarks/Codecs/EncodeWebp.cs

@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
using var memoryStream = new MemoryStream(); using var memoryStream = new MemoryStream();
this.webp.Save(memoryStream, new WebpEncoder() 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(); using var memoryStream = new MemoryStream();
this.webp.Save(memoryStream, new WebpEncoder() this.webp.Save(memoryStream, new WebpEncoder()
{ {
Lossy = false FileFormat = WebpFileFormatType.Lossless
}); });
} }

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

@ -16,10 +16,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
public class WebpEncoderTests public class WebpEncoderTests
{ {
[Theory] [Theory]
[WithFile(Flag, PixelTypes.Rgba32, WebpFormatType.Lossless)] // if its not a webp input image, it should default to lossless. [WithFile(Flag, PixelTypes.Rgba32, WebpFileFormatType.Lossless)] // if its not a webp input image, it should default to lossless.
[WithFile(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFormatType.Lossless)] [WithFile(Lossless.NoTransform1, PixelTypes.Rgba32, WebpFileFormatType.Lossless)]
[WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFormatType.Lossy)] [WithFile(Lossy.Bike, PixelTypes.Rgba32, WebpFileFormatType.Lossy)]
public void Encode_PreserveRatio<TPixel>(TestImageProvider<TPixel> provider, WebpFormatType expectedFormat) public void Encode_PreserveRatio<TPixel>(TestImageProvider<TPixel> provider, WebpFileFormatType expectedFormat)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var options = new WebpEncoder(); var options = new WebpEncoder();
@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
ImageMetadata meta = output.Metadata; ImageMetadata meta = output.Metadata;
WebpMetadata webpMetaData = meta.GetWebpMetadata(); WebpMetadata webpMetaData = meta.GetWebpMetadata();
Assert.Equal(expectedFormat, webpMetaData.Format); Assert.Equal(expectedFormat, webpMetaData.FileFormat);
} }
[Theory] [Theory]
@ -44,7 +44,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = false, FileFormat = WebpFileFormatType.Lossless,
Quality = 100, Quality = 100,
Method = WebpEncodingMethod.BestQuality Method = WebpEncodingMethod.BestQuality
}; };
@ -62,7 +62,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = false, FileFormat = WebpFileFormatType.Lossless,
Quality = quality Quality = quality
}; };
@ -91,7 +91,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = false, FileFormat = WebpFileFormatType.Lossless,
Method = method, Method = method,
Quality = quality Quality = quality
}; };
@ -113,7 +113,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = false, FileFormat = WebpFileFormatType.Lossless,
NearLossless = true, NearLossless = true,
NearLosslessQuality = nearLosslessQuality NearLosslessQuality = nearLosslessQuality
}; };
@ -137,7 +137,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = false, FileFormat = WebpFileFormatType.Lossless,
Method = method, Method = method,
TransparentColorMode = WebpTransparentColorMode.Preserve TransparentColorMode = WebpTransparentColorMode.Preserve
}; };
@ -155,7 +155,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
using Image<TPixel> image = provider.GetImage(); using Image<TPixel> image = provider.GetImage();
var encoder = new WebpEncoder() { Lossy = false }; var encoder = new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless };
image.VerifyEncoder(provider, "webp", string.Empty, encoder); 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. // Just make sure, encoding 1 pixel by 1 pixel does not throw an exception.
using var image = new Image<Rgba32>(1, 1); using var image = new Image<Rgba32>(1, 1);
var encoder = new WebpEncoder() { Lossy = false }; var encoder = new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless };
using (var memStream = new MemoryStream()) using (var memStream = new MemoryStream())
{ {
image.SaveAsWebp(memStream, encoder); image.SaveAsWebp(memStream, encoder);
@ -180,7 +180,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = true, FileFormat = WebpFileFormatType.Lossy,
Quality = quality Quality = quality
}; };
@ -200,7 +200,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = true, FileFormat = WebpFileFormatType.Lossy,
FilterStrength = filterStrength FilterStrength = filterStrength
}; };
@ -220,7 +220,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = true, FileFormat = WebpFileFormatType.Lossy,
SpatialNoiseShaping = snsStrength SpatialNoiseShaping = snsStrength
}; };
@ -249,7 +249,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
{ {
Lossy = true, FileFormat = WebpFileFormatType.Lossy,
Method = method, Method = method,
Quality = quality Quality = quality
}; };
@ -267,7 +267,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
using Image<TPixel> image = provider.GetImage(); using Image<TPixel> 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)); image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.Tolerant(0.04f));
} }

6
tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
[Trait("Format", "Webp")] [Trait("Format", "Webp")]
public class WebpMetaDataTests public class WebpMetaDataTests
{ {
private static WebpDecoder WebpDecoder => new WebpDecoder() { IgnoreMetadata = false }; private static WebpDecoder WebpDecoder => new() { IgnoreMetadata = false };
[Theory] [Theory]
[WithFile(TestImages.Webp.Lossy.WithExif, PixelTypes.Rgba32, false)] [WithFile(TestImages.Webp.Lossy.WithExif, PixelTypes.Rgba32, false)]
@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
ExifProfile expectedExif = input.Metadata.ExifProfile; ExifProfile expectedExif = input.Metadata.ExifProfile;
// act // act
input.Save(memoryStream, new WebpEncoder() { Lossy = true }); input.Save(memoryStream, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossy });
memoryStream.Position = 0; memoryStream.Position = 0;
// assert // assert
@ -95,7 +95,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
ExifProfile expectedExif = input.Metadata.ExifProfile; ExifProfile expectedExif = input.Metadata.ExifProfile;
// act // act
input.Save(memoryStream, new WebpEncoder() { Lossy = false }); input.Save(memoryStream, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless });
memoryStream.Position = 0; memoryStream.Position = 0;
// assert // assert

8
tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs

@ -506,9 +506,9 @@ namespace SixLabors.ImageSharp.Tests.Metadata.Profiles.Exif
case TestImageWriteFormat.Png: case TestImageWriteFormat.Png:
return WriteAndReadPng(image); return WriteAndReadPng(image);
case TestImageWriteFormat.WebpLossless: case TestImageWriteFormat.WebpLossless:
return WriteAndReadWebp(image, lossy: false); return WriteAndReadWebp(image, WebpFileFormatType.Lossless);
case TestImageWriteFormat.WebpLossy: case TestImageWriteFormat.WebpLossy:
return WriteAndReadWebp(image, lossy: true); return WriteAndReadWebp(image, WebpFileFormatType.Lossy);
default: default:
throw new ArgumentException("Unexpected test image format, only Jpeg and Png are allowed"); 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<Rgba32> WriteAndReadWebp(Image<Rgba32> image, bool lossy) private static Image<Rgba32> WriteAndReadWebp(Image<Rgba32> image, WebpFileFormatType fileFormat)
{ {
using (var memStream = new MemoryStream()) using (var memStream = new MemoryStream())
{ {
image.SaveAsWebp(memStream, new WebpEncoder() { Lossy = lossy }); image.SaveAsWebp(memStream, new WebpEncoder() { FileFormat = fileFormat });
image.Dispose(); image.Dispose();
memStream.Position = 0; memStream.Position = 0;

Loading…
Cancel
Save