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
{
/// <summary>
/// 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.
/// </summary>
bool? Lossy { get; }
WebpFileFormatType? FileFormat { get; }
/// <summary>
/// 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>
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
/// <returns>Information about this image.</returns>
private WebpImageInfo ReadVp8LHeader(WebpFeatures features = null)
{
this.webpMetadata.Format = WebpFormatType.Lossless;
this.webpMetadata.FileFormat = WebpFileFormatType.Lossless;
// VP8 data size.
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
{
/// <inheritdoc/>
public bool? Lossy { get; set; }
public WebpFileFormatType? FileFormat { get; set; }
/// <inheritdoc/>
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;
/// <summary>
/// Indicating whether lossy compression should be used. If false, lossless compression will be used.
/// Indicating what file format compression should be used.
/// </summary>
private bool? lossy;
private readonly WebpFileFormatType? fileFormat;
/// <summary>
/// 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,

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

@ -4,9 +4,9 @@
namespace SixLabors.ImageSharp.Formats.Webp
{
/// <summary>
/// Info about the webp format used.
/// Info about the webp file format used.
/// </summary>
public enum WebpFormatType
public enum WebpFileFormatType
{
/// <summary>
/// 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.
/// </summary>
/// <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>
/// Gets or sets the webp format used. Either lossless or lossy.
/// Gets or sets the webp file format used. Either lossless or lossy.
/// </summary>
public WebpFormatType? Format { get; set; }
public WebpFileFormatType? FileFormat { get; set; }
/// <inheritdoc/>
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();
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
});
}

34
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<TPixel>(TestImageProvider<TPixel> 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<TPixel>(TestImageProvider<TPixel> provider, WebpFileFormatType expectedFormat)
where TPixel : unmanaged, IPixel<TPixel>
{
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<TPixel> 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<Rgba32>(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<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));
}

6
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

8
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<Rgba32> WriteAndReadWebp(Image<Rgba32> image, bool lossy)
private static Image<Rgba32> WriteAndReadWebp(Image<Rgba32> 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;

Loading…
Cancel
Save