From 39a28668cf58c1f16842eb8211c495155a6d0777 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Fri, 13 Mar 2020 16:31:13 +0100 Subject: [PATCH] Add tests for reading webp metadata, fix some test images with metadata --- .../Formats/WebP/WebPDecoderCore.cs | 17 ++++-- .../Formats/WebP/WebPDecoderTests.cs | 1 - .../Formats/WebP/WebPFileHeaderTests.cs | 21 ------- .../Formats/WebP/WebPMetaDataTests.cs | 57 +++++++++++++++++++ tests/ImageSharp.Tests/TestImages.cs | 11 +++- .../Input/WebP/{exif.webp => exif_lossy.webp} | 0 ...{lossless_iccp.webp => iccp_lossless.webp} | 0 .../WebP/{lossy_iccp.webp => iccp_lossy.webp} | 0 8 files changed, 77 insertions(+), 30 deletions(-) delete mode 100644 tests/ImageSharp.Tests/Formats/WebP/WebPFileHeaderTests.cs create mode 100644 tests/ImageSharp.Tests/Formats/WebP/WebPMetaDataTests.cs rename tests/Images/Input/WebP/{exif.webp => exif_lossy.webp} (100%) rename tests/Images/Input/WebP/{lossless_iccp.webp => iccp_lossless.webp} (100%) rename tests/Images/Input/WebP/{lossy_iccp.webp => iccp_lossy.webp} (100%) diff --git a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs index 8f8f50cc02..2971b26389 100644 --- a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs +++ b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs @@ -215,12 +215,19 @@ namespace SixLabors.ImageSharp.Formats.WebP if (chunkType is WebPChunkType.Iccp) { uint iccpChunkSize = this.ReadChunkSize(); - var iccpData = new byte[iccpChunkSize]; - this.currentStream.Read(iccpData, 0, (int)iccpChunkSize); - var profile = new IccProfile(iccpData); - if (profile.CheckIsValid()) + if (!this.IgnoreMetadata) { - this.Metadata.IccProfile = profile; + var iccpData = new byte[iccpChunkSize]; + this.currentStream.Read(iccpData, 0, (int)iccpChunkSize); + var profile = new IccProfile(iccpData); + if (profile.CheckIsValid()) + { + this.Metadata.IccProfile = profile; + } + } + else + { + this.currentStream.Skip((int)iccpChunkSize); } } } diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs index dc7c6f0847..f2c2d9c2dc 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebPDecoderTests.cs @@ -10,7 +10,6 @@ using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; using Xunit; // ReSharper disable InconsistentNaming - namespace SixLabors.ImageSharp.Tests.Formats.WebP { using static SixLabors.ImageSharp.Tests.TestImages.WebP; diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebPFileHeaderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebPFileHeaderTests.cs deleted file mode 100644 index bcc177d347..0000000000 --- a/tests/ImageSharp.Tests/Formats/WebP/WebPFileHeaderTests.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using SixLabors.ImageSharp.Formats.Bmp; -using Xunit; - -namespace SixLabors.ImageSharp.Tests.Formats.WebP -{ - public class WebPFileHeaderTests - { - [Fact] - public void TestWrite() - { - var header = new BmpFileHeader(1, 2, 3, 4); - - var buffer = new byte[14]; - - header.WriteTo(buffer); - - Assert.Equal("AQACAAAAAwAAAAQAAAA=", Convert.ToBase64String(buffer)); - } - } -} diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebPMetaDataTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebPMetaDataTests.cs new file mode 100644 index 0000000000..44f46c05a6 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/WebP/WebPMetaDataTests.cs @@ -0,0 +1,57 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.Formats.WebP; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +// ReSharper disable InconsistentNaming +namespace SixLabors.ImageSharp.Tests.Formats.WebP +{ + public class WebPMetadataTests + { + [Theory] + [WithFile(TestImages.WebP.Lossless.WithExif, PixelTypes.Rgba32, false)] + [WithFile(TestImages.WebP.Lossy.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)) + { + if (ignoreMetadata) + { + Assert.Null(image.Metadata.ExifProfile); + } + else + { + Assert.NotNull(image.Metadata.ExifProfile); + Assert.NotEmpty(image.Metadata.ExifProfile.Values); + } + } + } + + [Theory] + [WithFile(TestImages.WebP.Lossless.WithIccp, PixelTypes.Rgba32, false)] + [WithFile(TestImages.WebP.Lossy.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)) + { + if (ignoreMetadata) + { + Assert.Null(image.Metadata.IccProfile); + } + else + { + Assert.NotNull(image.Metadata.IccProfile); + Assert.NotEmpty(image.Metadata.IccProfile.Entries); + } + } + } + } +} diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index af8be150f8..517fe38f1b 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -390,8 +390,8 @@ namespace SixLabors.ImageSharp.Tests public const string Bit32Rle = "Tga/targa_32bit_rle.tga"; public const string Bit16Pal = "Tga/targa_16bit_pal.tga"; public const string Bit24Pal = "Tga/targa_24bit_pal.tga"; - } - + } + public static class WebP { public static class Animated @@ -404,6 +404,8 @@ namespace SixLabors.ImageSharp.Tests public static class Lossless { + public const string WithExif = "WebP/exif_lossless.webp"; + public const string WithIccp = "WebP/iccp_lossless.webp"; public const string NoTransform1 = "WebP/lossless_vec_1_0.webp"; public const string NoTransform2 = "WebP/lossless_vec_2_0.webp"; public const string GreenTransform1 = "WebP/lossless1.webp"; @@ -452,6 +454,9 @@ namespace SixLabors.ImageSharp.Tests public static class Lossy { + public const string WithExif = "WebP/exif_lossy.webp"; + public const string WithIccp = "WebP/iccp_lossy.webp"; + // Lossy images without macroblock filtering. public const string Bike = "WebP/bike_lossy.webp"; public const string NoFilter01 = "WebP/vp80-01-intra-1400.webp"; @@ -469,7 +474,7 @@ namespace SixLabors.ImageSharp.Tests public const string SimpleFilter05 = "WebP/test-nostrong.webp"; // Lossy images with a complex filter. - public const string IccpComplexFilter = "WebP/lossy_iccp.webp"; + public const string IccpComplexFilter = "WebP/iccp_lossy.webp"; public const string VeryShort = "WebP/very_short.webp"; public const string BikeComplexFilter = "WebP/bike_lossy_complex_filter.webp"; public const string ComplexFilter01 = "WebP/vp80-02-inter-1418.webp"; diff --git a/tests/Images/Input/WebP/exif.webp b/tests/Images/Input/WebP/exif_lossy.webp similarity index 100% rename from tests/Images/Input/WebP/exif.webp rename to tests/Images/Input/WebP/exif_lossy.webp diff --git a/tests/Images/Input/WebP/lossless_iccp.webp b/tests/Images/Input/WebP/iccp_lossless.webp similarity index 100% rename from tests/Images/Input/WebP/lossless_iccp.webp rename to tests/Images/Input/WebP/iccp_lossless.webp diff --git a/tests/Images/Input/WebP/lossy_iccp.webp b/tests/Images/Input/WebP/iccp_lossy.webp similarity index 100% rename from tests/Images/Input/WebP/lossy_iccp.webp rename to tests/Images/Input/WebP/iccp_lossy.webp