From 67aace4daf7e6b4575b06667bfcef64ad936578f Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Fri, 22 Jan 2021 13:58:15 +0100 Subject: [PATCH] Add test for writing exif chunk --- .../Formats/WebP/WebpMetaDataTests.cs | 102 ++++++++++++++---- 1 file changed, 80 insertions(+), 22 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs index b73dcdfcfd..9567684db2 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs @@ -1,7 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.IO; using SixLabors.ImageSharp.Formats.Experimental.Webp; +using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.PixelFormats; using Xunit; @@ -11,48 +13,104 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [Trait("Format", "Webp")] public class WebpMetaDataTests { + private readonly Configuration configuration; + + private static WebpDecoder WebpDecoder => new WebpDecoder() { IgnoreMetadata = false }; + + public WebpMetaDataTests() + { + this.configuration = new Configuration(); + this.configuration.AddWebp(); + } + [Theory] - [WithFile(TestImages.WebP.Lossless.WithExif, PixelTypes.Rgba32, false)] + [WithFile(TestImages.WebP.Lossy.WithExif, PixelTypes.Rgba32, false)] [WithFile(TestImages.WebP.Lossy.WithExif, PixelTypes.Rgba32, true)] + [WithFile(TestImages.WebP.Lossless.WithExif, PixelTypes.Rgba32, false)] + [WithFile(TestImages.WebP.Lossless.WithExif, PixelTypes.Rgba32, true)] public void IgnoreMetadata_ControlsWhetherExifIsParsed(TestImageProvider provider, bool ignoreMetadata) where TPixel : unmanaged, IPixel { var decoder = new WebpDecoder { IgnoreMetadata = ignoreMetadata }; - using (Image image = provider.GetImage(decoder)) + using Image image = provider.GetImage(decoder); + if (ignoreMetadata) { - if (ignoreMetadata) - { - Assert.Null(image.Metadata.ExifProfile); - } - else - { - Assert.NotNull(image.Metadata.ExifProfile); - Assert.NotEmpty(image.Metadata.ExifProfile.Values); - } + Assert.Null(image.Metadata.ExifProfile); + } + else + { + ExifProfile exifProfile = image.Metadata.ExifProfile; + Assert.NotNull(exifProfile); + Assert.NotEmpty(exifProfile.Values); + Assert.Contains(exifProfile.Values, m => m.Tag.Equals(ExifTag.Make) && m.GetValue().Equals("Canon")); + Assert.Contains(exifProfile.Values, m => m.Tag.Equals(ExifTag.Model) && m.GetValue().Equals("Canon PowerShot S40")); + Assert.Contains(exifProfile.Values, m => m.Tag.Equals(ExifTag.Software) && m.GetValue().Equals("GIMP 2.10.2")); } } [Theory] - [WithFile(TestImages.WebP.Lossless.WithIccp, PixelTypes.Rgba32, false)] + [WithFile(TestImages.WebP.Lossy.WithIccp, PixelTypes.Rgba32, false)] [WithFile(TestImages.WebP.Lossy.WithIccp, PixelTypes.Rgba32, true)] + [WithFile(TestImages.WebP.Lossless.WithIccp, PixelTypes.Rgba32, false)] + [WithFile(TestImages.WebP.Lossless.WithIccp, PixelTypes.Rgba32, true)] public void IgnoreMetadata_ControlsWhetherIccpIsParsed(TestImageProvider provider, bool ignoreMetadata) where TPixel : unmanaged, IPixel { var decoder = new WebpDecoder { IgnoreMetadata = ignoreMetadata }; - using (Image image = provider.GetImage(decoder)) + using Image image = provider.GetImage(decoder); + if (ignoreMetadata) { - if (ignoreMetadata) - { - Assert.Null(image.Metadata.IccProfile); - } - else - { - Assert.NotNull(image.Metadata.IccProfile); - Assert.NotEmpty(image.Metadata.IccProfile.Entries); - } + Assert.Null(image.Metadata.IccProfile); } + else + { + Assert.NotNull(image.Metadata.IccProfile); + Assert.NotEmpty(image.Metadata.IccProfile.Entries); + } + } + + [Theory] + [WithFile(TestImages.WebP.Lossy.WithExif, PixelTypes.Rgba32)] + public void EncodeLossyWebp_PreservesExif(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // arrange + using Image input = provider.GetImage(WebpDecoder); + using var memoryStream = new MemoryStream(); + ExifProfile expectedExif = input.Metadata.ExifProfile; + + // act + input.Save(memoryStream, new WebpEncoder() { Lossy = true }); + memoryStream.Position = 0; + + // assert + using Image image = WebpDecoder.Decode(this.configuration, memoryStream); + ExifProfile actualExif = image.Metadata.ExifProfile; + Assert.NotNull(actualExif); + Assert.Equal(expectedExif.Values.Count, actualExif.Values.Count); + } + + [Theory] + [WithFile(TestImages.WebP.Lossless.WithExif, PixelTypes.Rgba32)] + public void EncodeLosslessWebp_PreservesExif(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // arrange + using Image input = provider.GetImage(WebpDecoder); + using var memoryStream = new MemoryStream(); + ExifProfile expectedExif = input.Metadata.ExifProfile; + + // act + input.Save(memoryStream, new WebpEncoder() { Lossy = false }); + memoryStream.Position = 0; + + // assert + using Image image = WebpDecoder.Decode(this.configuration, memoryStream); + ExifProfile actualExif = image.Metadata.ExifProfile; + Assert.NotNull(actualExif); + Assert.Equal(expectedExif.Values.Count, actualExif.Values.Count); } } }