From 43ca448f39f38df280e60298baad26b66bada3f7 Mon Sep 17 00:00:00 2001 From: Jesse Gielen Date: Sun, 24 Jun 2018 16:00:04 +0200 Subject: [PATCH 01/23] Remove NotNull guard on value type --- .../Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs b/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs index 3bc1df0bb..f88123e5d 100644 --- a/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs +++ b/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs @@ -47,7 +47,6 @@ namespace SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion /// The divisor. internal ErrorDiffuserBase(DenseMatrix matrix, byte divisor) { - Guard.NotNull(matrix, nameof(matrix)); Guard.MustBeGreaterThan(divisor, 0, nameof(divisor)); this.matrix = matrix; From 6e0c827f488ec2839d4524e76c83e43749d921f4 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 26 Jun 2018 00:40:23 +0200 Subject: [PATCH 02/23] make Guard.NotNull() and DebugGuard.NotNull() generic --- src/ImageSharp/Common/Helpers/DebugGuard.cs | 9 +++++---- src/ImageSharp/Common/Helpers/Guard.cs | 3 ++- tests/ImageSharp.Tests/Helpers/GuardTests.cs | 8 ++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs index 6dcd0fd27..5a1d3a2e3 100644 --- a/src/ImageSharp/Common/Helpers/DebugGuard.cs +++ b/src/ImageSharp/Common/Helpers/DebugGuard.cs @@ -17,13 +17,14 @@ namespace SixLabors.ImageSharp /// Verifies, that the method parameter with specified object value is not null /// and throws an exception if it is found to be so. /// - /// The target object, which cannot be null. + /// The target object, which cannot be null. /// The name of the parameter that is to be checked. - /// is null + /// is null [Conditional("DEBUG")] - public static void NotNull(object target, string parameterName) + public static void NotNull(T value, string parameterName) + where T : class { - if (target == null) + if (value == null) { throw new ArgumentNullException(parameterName); } diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index 011d7fdaa..d09079062 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -19,7 +19,8 @@ namespace SixLabors.ImageSharp /// The target object, which cannot be null. /// The name of the parameter that is to be checked. /// is null - public static void NotNull(object value, string parameterName) + public static void NotNull(T value, string parameterName) + where T : class { if (value == null) { diff --git a/tests/ImageSharp.Tests/Helpers/GuardTests.cs b/tests/ImageSharp.Tests/Helpers/GuardTests.cs index 42913e02d..0d1bb5ce9 100644 --- a/tests/ImageSharp.Tests/Helpers/GuardTests.cs +++ b/tests/ImageSharp.Tests/Helpers/GuardTests.cs @@ -12,13 +12,17 @@ namespace SixLabors.ImageSharp.Tests.Helpers /// public class GuardTests { + class Test + { + } + /// /// Tests that the method throws when the argument is null. /// [Fact] public void NotNullThrowsWhenArgIsNull() { - Assert.Throws(() => Guard.NotNull(null, "foo")); + Assert.Throws(() => Guard.NotNull((Test)null, "foo")); } /// @@ -27,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Helpers [Fact] public void NotNullThrowsWhenArgNameEmpty() { - Assert.Throws(() => Guard.NotNull(null, string.Empty)); + Assert.Throws(() => Guard.NotNull((Test)null, string.Empty)); } /// From b5911782ad999ad808457341d0a7b28f7d6c85d5 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 26 Jun 2018 01:58:41 +0200 Subject: [PATCH 03/23] ImageMagick decoder works --- .../ImageSharp.Tests/ImageSharp.Tests.csproj | 1 + .../ReferenceCodecs/MagickReferenceDecoder.cs | 51 +++++++++ .../ReferenceCodecs/MagickReferenceEncoder.cs | 60 ++++++++++ .../TestUtilities/TestImageExtensions.cs | 6 +- .../Tests/MagickReferenceCodecTests.cs | 106 ++++++++++++++++++ ...cs => SystemDrawingReferenceCodecTests.cs} | 10 +- 6 files changed, 228 insertions(+), 6 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs rename tests/ImageSharp.Tests/TestUtilities/Tests/{ReferenceCodecTests.cs => SystemDrawingReferenceCodecTests.cs} (94%) diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index e7e2577a8..9e15b6aba 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -27,6 +27,7 @@ + diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs new file mode 100644 index 000000000..9b209137b --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs @@ -0,0 +1,51 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; +using System.Runtime.InteropServices; + +using ImageMagick; + +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs +{ + public class MagickReferenceDecoder : IImageDecoder + { + public Image Decode(Configuration configuration, Stream stream) + where TPixel : struct, IPixel + { + using (var magickImage = new MagickImage(stream)) + { + var result = new Image(configuration, magickImage.Width, magickImage.Height); + Span resultPixels = result.GetPixelSpan(); + + using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) + { + if (magickImage.Depth == 8) + { + byte[] data = pixels.ToByteArray("RGBA"); + + PixelOperations.Instance.PackFromRgba32Bytes(data, resultPixels, resultPixels.Length); + } + else if (magickImage.Depth == 16) + { + ushort[] data = pixels.ToShortArray("RGBA"); + Span bytes = MemoryMarshal.Cast(data.AsSpan()); + + PixelOperations.Instance.PackFromRgba64Bytes(bytes, resultPixels, resultPixels.Length); + } + else + { + throw new NotImplementedException(); + } + } + + return result; + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs new file mode 100644 index 000000000..97e4a55a4 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs @@ -0,0 +1,60 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; + +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.PixelFormats; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +using ImageMagick; + +using SixLabors.ImageSharp.Advanced; + +namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs +{ + public class MagickReferenceEncoder : IImageEncoder + { + public MagickReferenceEncoder(MagickFormat format) + { + this.Format = format; + } + + public MagickFormat Format { get; } + + public void Encode(Image image, Stream stream) + where TPixel : struct, IPixel + { + var black = MagickColor.FromRgba(0, 0, 0, 255); + using (var magickImage = new MagickImage(black, image.Width, image.Height)) + { + bool isDeep = Unsafe.SizeOf() > 32; + + magickImage.Depth = isDeep ? 16 : 8; + + Span allPixels = image.GetPixelSpan(); + + using (IPixelCollection magickPixels = magickImage.GetPixelsUnsafe()) + { + if (isDeep) + { + ushort[] data = new ushort[allPixels.Length * 4]; + Span dataSpan = MemoryMarshal.Cast(data); + PixelOperations.Instance.ToRgba64(allPixels, dataSpan, allPixels.Length); + magickPixels.SetPixels(data); + } + else + { + byte[] data = new byte[allPixels.Length * 4]; + PixelOperations.Instance.ToRgba32Bytes(allPixels, data, allPixels.Length); + magickPixels.SetPixels(data); + } + } + + magickImage.Write(stream, this.Format); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 016ae7ad2..79a0071ff 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -641,7 +641,8 @@ namespace SixLabors.ImageSharp.Tests IImageEncoder encoder, ImageComparer customComparer = null, bool appendPixelTypeToFileName = true, - string referenceImageExtension = null) + string referenceImageExtension = null, + IImageDecoder referenceDecoder = null) where TPixel : struct, IPixel { string actualOutputFile = provider.Utility.SaveTestOutputFile( @@ -650,7 +651,8 @@ namespace SixLabors.ImageSharp.Tests encoder, testOutputDetails, appendPixelTypeToFileName); - IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile); + + referenceDecoder = referenceDecoder ?? TestEnvironment.GetReferenceDecoder(actualOutputFile); using (var actualImage = Image.Load(actualOutputFile, referenceDecoder)) { diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs new file mode 100644 index 000000000..a797fca0e --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs @@ -0,0 +1,106 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using ImageMagick; +using Xunit; +// ReSharper disable InconsistentNaming + +namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests +{ + using SixLabors.ImageSharp.PixelFormats; + using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; + + using Xunit.Abstractions; + + public class MagickReferenceCodecTests + { + public MagickReferenceCodecTests(ITestOutputHelper output) + { + this.Output = output; + } + + private ITestOutputHelper Output { get; } + + public const PixelTypes PixelTypesToTest32 = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.Rgb24; + + public const PixelTypes PixelTypesToTest64 = + PixelTypes.Rgba32 | PixelTypes.Rgb24 | PixelTypes.Rgba64 | PixelTypes.Rgb48; + + public const PixelTypes PixelTypesToTest48 = + PixelTypes.Rgba32 | PixelTypes.Rgba64 | PixelTypes.Rgb48; + + [Theory] + [WithBlankImages(1, 1, PixelTypesToTest32, TestImages.Png.Splash)] + [WithBlankImages(1, 1, PixelTypesToTest32, TestImages.Png.Indexed)] + public void MagickDecode_8BitDepthImage_IsEquivalentTo_SystemDrawingResult(TestImageProvider dummyProvider, string testImage) + where TPixel : struct, IPixel + { + string path = TestFile.GetInputFileFullPath(testImage); + + var magickDecoder = new MagickReferenceDecoder(); + var sdDecoder = new SystemDrawingReferenceDecoder(); + + ImageComparer comparer = ImageComparer.Exact; + + using (var mImage = Image.Load(path, magickDecoder)) + using (var sdImage = Image.Load(path, sdDecoder)) + { + ImageSimilarityReport report = comparer.CompareImagesOrFrames(mImage, sdImage); + + mImage.DebugSave(dummyProvider); + + if (TestEnvironment.IsWindows) + { + Assert.True(report.IsEmpty); + } + } + } + + [Theory] + [WithBlankImages(1, 1, PixelTypesToTest64, TestImages.Png.Rgba64Bpp)] + [WithBlankImages(1, 1, PixelTypesToTest48, TestImages.Png.Rgb48Bpp)] + [WithBlankImages(1, 1, PixelTypesToTest48, TestImages.Png.Rgb48BppInterlaced)] + [WithBlankImages(1, 1, PixelTypesToTest48, TestImages.Png.Rgb48BppTrans)] + public void MagickDecode_16BitDepthImage_IsApproximatelyEquivalentTo_SystemDrawingResult(TestImageProvider dummyProvider, string testImage) + where TPixel : struct, IPixel + { + string path = TestFile.GetInputFileFullPath(testImage); + + var magickDecoder = new MagickReferenceDecoder(); + var sdDecoder = new SystemDrawingReferenceDecoder(); + + // 1020 == 4 * 255 (Equivalent to manhattan distance of 1+1+1+1=4 in Rgba32 space) + var comparer = ImageComparer.TolerantPercentage(1, 1020); + + using (var mImage = Image.Load(path, magickDecoder)) + using (var sdImage = Image.Load(path, sdDecoder)) + { + ImageSimilarityReport report = comparer.CompareImagesOrFrames(mImage, sdImage); + + mImage.DebugSave(dummyProvider); + + if (TestEnvironment.IsWindows) + { + Assert.True(report.IsEmpty); + } + } + } + + [Theory] + [WithTestPatternImages(100, 100, PixelTypesToTest32, MagickFormat.Png)] + [WithTestPatternImages(100, 100, PixelTypesToTest32, MagickFormat.Jpg)] + public void MagickEncode_8BitDepthImage(TestImageProvider provider, MagickFormat format) + where TPixel : struct, IPixel + { + string extension = format.ToString().ToLower(); + + var encoder = new MagickReferenceEncoder(format); + + using (Image image = provider.GetImage()) + { + image.VerifyEncoder(provider, extension, $"{format}", encoder, referenceDecoder: new SystemDrawingReferenceDecoder()); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/SystemDrawingReferenceCodecTests.cs similarity index 94% rename from tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs rename to tests/ImageSharp.Tests/TestUtilities/Tests/SystemDrawingReferenceCodecTests.cs index 3ad595b7e..3cdb67dbd 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/SystemDrawingReferenceCodecTests.cs @@ -1,4 +1,6 @@ -using System.IO; +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; @@ -8,13 +10,13 @@ using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; using Xunit; using Xunit.Abstractions; -namespace SixLabors.ImageSharp.Tests +namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests { - public class ReferenceCodecTests + public class SystemDrawingReferenceCodecTests { private ITestOutputHelper Output { get; } - public ReferenceCodecTests(ITestOutputHelper output) + public SystemDrawingReferenceCodecTests(ITestOutputHelper output) { this.Output = output; } From 817ac0b20475106162a5d786cf7540bad1dcb63c Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 26 Jun 2018 02:06:27 +0200 Subject: [PATCH 04/23] oops --- .../TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs index 97e4a55a4..effd30ec6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs @@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs var black = MagickColor.FromRgba(0, 0, 0, 255); using (var magickImage = new MagickImage(black, image.Width, image.Height)) { - bool isDeep = Unsafe.SizeOf() > 32; + bool isDeep = Unsafe.SizeOf() > 4; magickImage.Depth = isDeep ? 16 : 8; From 1d1b5d9ddf7d0d9ac6787750d42dfcb5df52f92e Mon Sep 17 00:00:00 2001 From: Anton Firsov Date: Tue, 26 Jun 2018 09:34:15 +0200 Subject: [PATCH 05/23] make Guard.NotNull() and DebugGuard.NotNull() generic (#634) --- src/ImageSharp/Common/Helpers/DebugGuard.cs | 9 +++++---- src/ImageSharp/Common/Helpers/Guard.cs | 3 ++- tests/ImageSharp.Tests/Helpers/GuardTests.cs | 8 ++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs index 6dcd0fd27..5a1d3a2e3 100644 --- a/src/ImageSharp/Common/Helpers/DebugGuard.cs +++ b/src/ImageSharp/Common/Helpers/DebugGuard.cs @@ -17,13 +17,14 @@ namespace SixLabors.ImageSharp /// Verifies, that the method parameter with specified object value is not null /// and throws an exception if it is found to be so. /// - /// The target object, which cannot be null. + /// The target object, which cannot be null. /// The name of the parameter that is to be checked. - /// is null + /// is null [Conditional("DEBUG")] - public static void NotNull(object target, string parameterName) + public static void NotNull(T value, string parameterName) + where T : class { - if (target == null) + if (value == null) { throw new ArgumentNullException(parameterName); } diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index 011d7fdaa..d09079062 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -19,7 +19,8 @@ namespace SixLabors.ImageSharp /// The target object, which cannot be null. /// The name of the parameter that is to be checked. /// is null - public static void NotNull(object value, string parameterName) + public static void NotNull(T value, string parameterName) + where T : class { if (value == null) { diff --git a/tests/ImageSharp.Tests/Helpers/GuardTests.cs b/tests/ImageSharp.Tests/Helpers/GuardTests.cs index 42913e02d..0d1bb5ce9 100644 --- a/tests/ImageSharp.Tests/Helpers/GuardTests.cs +++ b/tests/ImageSharp.Tests/Helpers/GuardTests.cs @@ -12,13 +12,17 @@ namespace SixLabors.ImageSharp.Tests.Helpers /// public class GuardTests { + class Test + { + } + /// /// Tests that the method throws when the argument is null. /// [Fact] public void NotNullThrowsWhenArgIsNull() { - Assert.Throws(() => Guard.NotNull(null, "foo")); + Assert.Throws(() => Guard.NotNull((Test)null, "foo")); } /// @@ -27,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Helpers [Fact] public void NotNullThrowsWhenArgNameEmpty() { - Assert.Throws(() => Guard.NotNull(null, string.Empty)); + Assert.Throws(() => Guard.NotNull((Test)null, string.Empty)); } /// From 824f568ed26509e114e28c8a80bd79cfcaff14e2 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 08:54:53 -0700 Subject: [PATCH 06/23] Remove unused ListExtensions class --- .../Common/Extensions/ListExtensions.cs | 46 ------------------- 1 file changed, 46 deletions(-) delete mode 100644 src/ImageSharp/Common/Extensions/ListExtensions.cs diff --git a/src/ImageSharp/Common/Extensions/ListExtensions.cs b/src/ImageSharp/Common/Extensions/ListExtensions.cs deleted file mode 100644 index 2713896c0..000000000 --- a/src/ImageSharp/Common/Extensions/ListExtensions.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Collections.Generic; - -namespace SixLabors.ImageSharp.Common.Extensions -{ - /// - /// Encapsulates a series of time saving extension methods to the class. - /// - internal static class ListExtensions - { - /// - /// Inserts an item at the given index automatically expanding the capacity if required. - /// - /// The type of object within the list - /// The list - /// The index - /// The item to insert - public static void SafeInsert(this List list, int index, T item) - { - if (index >= list.Count) - { - list.Add(item); - } - else - { - list[index] = item; - } - } - - /// - /// Removes the last element from a list and returns that element. This method changes the length of the list. - /// - /// The type of object within the list - /// The list - /// The last element in the specified sequence. - public static T Pop(this List list) - { - int last = list.Count - 1; - T item = list[last]; - list.RemoveAt(last); - return item; - } - } -} \ No newline at end of file From 79bba7b4e4d731a20634425afda918c8b0576ab3 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 26 Jun 2018 23:07:49 +0200 Subject: [PATCH 07/23] reference encoder is unnecessary --- .../ReferenceCodecs/MagickReferenceEncoder.cs | 60 ------------------- .../Tests/MagickReferenceCodecTests.cs | 17 ------ 2 files changed, 77 deletions(-) delete mode 100644 tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs deleted file mode 100644 index effd30ec6..000000000 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceEncoder.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.IO; - -using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.PixelFormats; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -using ImageMagick; - -using SixLabors.ImageSharp.Advanced; - -namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs -{ - public class MagickReferenceEncoder : IImageEncoder - { - public MagickReferenceEncoder(MagickFormat format) - { - this.Format = format; - } - - public MagickFormat Format { get; } - - public void Encode(Image image, Stream stream) - where TPixel : struct, IPixel - { - var black = MagickColor.FromRgba(0, 0, 0, 255); - using (var magickImage = new MagickImage(black, image.Width, image.Height)) - { - bool isDeep = Unsafe.SizeOf() > 4; - - magickImage.Depth = isDeep ? 16 : 8; - - Span allPixels = image.GetPixelSpan(); - - using (IPixelCollection magickPixels = magickImage.GetPixelsUnsafe()) - { - if (isDeep) - { - ushort[] data = new ushort[allPixels.Length * 4]; - Span dataSpan = MemoryMarshal.Cast(data); - PixelOperations.Instance.ToRgba64(allPixels, dataSpan, allPixels.Length); - magickPixels.SetPixels(data); - } - else - { - byte[] data = new byte[allPixels.Length * 4]; - PixelOperations.Instance.ToRgba32Bytes(allPixels, data, allPixels.Length); - magickPixels.SetPixels(data); - } - } - - magickImage.Write(stream, this.Format); - } - } - } -} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs index a797fca0e..db651886f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using ImageMagick; using Xunit; // ReSharper disable InconsistentNaming @@ -86,21 +85,5 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests } } } - - [Theory] - [WithTestPatternImages(100, 100, PixelTypesToTest32, MagickFormat.Png)] - [WithTestPatternImages(100, 100, PixelTypesToTest32, MagickFormat.Jpg)] - public void MagickEncode_8BitDepthImage(TestImageProvider provider, MagickFormat format) - where TPixel : struct, IPixel - { - string extension = format.ToString().ToLower(); - - var encoder = new MagickReferenceEncoder(format); - - using (Image image = provider.GetImage()) - { - image.VerifyEncoder(provider, extension, $"{format}", encoder, referenceDecoder: new SystemDrawingReferenceDecoder()); - } - } } } \ No newline at end of file From 507b2f8554281087322c02d49379f847345223a7 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 27 Jun 2018 00:30:23 +0200 Subject: [PATCH 08/23] using MagickReferenceDecoder everywhere, added ReferenceDecoderBenchmarks --- .../TestUtilities/ImagingTestCaseUtility.cs | 6 +- .../ReferenceCodecs/MagickReferenceDecoder.cs | 4 +- .../SystemDrawingReferenceEncoder.cs | 2 + .../TestUtilities/TestEnvironment.Formats.cs | 41 +++----- .../TestUtilities/TestEnvironment.cs | 8 +- .../TestUtilities/TestImageExtensions.cs | 19 ++-- .../Tests/ReferenceDecoderBenchmarks.cs | 96 +++++++++++++++++++ 7 files changed, 131 insertions(+), 45 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceDecoderBenchmarks.cs diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 2c4eb6c33..65b32e088 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -146,7 +146,6 @@ namespace SixLabors.ImageSharp.Tests appendSourceFileOrDescription); } - /// /// Encodes image by the format matching the required extension, than saves it to the recommended output file. /// @@ -154,7 +153,9 @@ namespace SixLabors.ImageSharp.Tests /// The image instance /// The requested extension /// Optional encoder - /// /// A boolean indicating whether to append to the test output file name. + /// A value indicating whether to append the pixel type to the test output file name + /// A boolean indicating whether to append to the test output file name. + /// Additional information to append to the test output file name public string SaveTestOutputFile( Image image, string extension = null, @@ -176,6 +177,7 @@ namespace SixLabors.ImageSharp.Tests { image.Save(stream, encoder); } + return path; } diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs index 9b209137b..8cfc2472f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs @@ -15,6 +15,8 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs { public class MagickReferenceDecoder : IImageDecoder { + public static MagickReferenceDecoder Instance { get; } = new MagickReferenceDecoder(); + public Image Decode(Configuration configuration, Stream stream) where TPixel : struct, IPixel { @@ -40,7 +42,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs } else { - throw new NotImplementedException(); + throw new InvalidOperationException(); } } diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs index 912333695..46dae17a1 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs @@ -20,6 +20,8 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs public static SystemDrawingReferenceEncoder Png { get; } = new SystemDrawingReferenceEncoder(ImageFormat.Png); + public static SystemDrawingReferenceEncoder Bmp { get; } = new SystemDrawingReferenceEncoder(ImageFormat.Bmp); + public void Encode(Image image, Stream stream) where TPixel : struct, IPixel { diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs index 566c22342..30067ec2d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs @@ -14,9 +14,9 @@ namespace SixLabors.ImageSharp.Tests { public static partial class TestEnvironment { - private static Lazy configuration = new Lazy(CreateDefaultConfiguration); + private static readonly Lazy ConfigurationLazy = new Lazy(CreateDefaultConfiguration); - internal static Configuration Configuration => configuration.Value; + internal static Configuration Configuration => ConfigurationLazy.Value; internal static IImageDecoder GetReferenceDecoder(string filePath) { @@ -52,36 +52,25 @@ namespace SixLabors.ImageSharp.Tests private static Configuration CreateDefaultConfiguration() { - var configuration = new Configuration( - new PngConfigurationModule(), + var cfg = new Configuration( new JpegConfigurationModule(), new GifConfigurationModule() ); - if (!IsLinux) - { - // TODO: System.Drawing on Windows can decode 48bit and 64bit pngs but - // it doesn't preserve the accuracy we require for comparison. - // This makes CompareToOriginal method non-useful. - configuration.ConfigureCodecs( - ImageFormats.Png, - SystemDrawingReferenceDecoder.Instance, - SystemDrawingReferenceEncoder.Png, - new PngImageFormatDetector()); + // Magick codecs should work on all + cfg.ConfigureCodecs( + ImageFormats.Png, + MagickReferenceDecoder.Instance, + SystemDrawingReferenceEncoder.Png, + new PngImageFormatDetector()); - configuration.ConfigureCodecs( - ImageFormats.Bmp, - SystemDrawingReferenceDecoder.Instance, - SystemDrawingReferenceEncoder.Png, - new PngImageFormatDetector()); - } - else - { - configuration.Configure(new PngConfigurationModule()); - configuration.Configure(new BmpConfigurationModule()); - } + cfg.ConfigureCodecs( + ImageFormats.Bmp, + MagickReferenceDecoder.Instance, + SystemDrawingReferenceEncoder.Bmp, + new BmpImageFormatDetector()); - return configuration; + return cfg; } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index 9a41e6602..f0b732998 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -21,9 +21,9 @@ namespace SixLabors.ImageSharp.Tests private const string ToolsDirectoryRelativePath = @"tests\Images\External\tools"; - private static Lazy solutionDirectoryFullPath = new Lazy(GetSolutionDirectoryFullPathImpl); + private static readonly Lazy SolutionDirectoryFullPathLazy = new Lazy(GetSolutionDirectoryFullPathImpl); - private static Lazy runsOnCi = new Lazy( + private static readonly Lazy RunsOnCiLazy = new Lazy( () => { bool isCi; @@ -41,9 +41,9 @@ namespace SixLabors.ImageSharp.Tests /// /// Gets a value indicating whether test execution runs on CI. /// - internal static bool RunsOnCI => runsOnCi.Value; + internal static bool RunsOnCI => RunsOnCiLazy.Value; - internal static string SolutionDirectoryFullPath => solutionDirectoryFullPath.Value; + internal static string SolutionDirectoryFullPath => SolutionDirectoryFullPathLazy.Value; private static string GetSolutionDirectoryFullPathImpl() { diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 79a0071ff..a1f97afb9 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -499,16 +499,18 @@ namespace SixLabors.ImageSharp.Tests public static Image CompareToOriginal( this Image image, - ITestImageProvider provider) + ITestImageProvider provider, + IImageDecoder referenceDecoder = null) where TPixel : struct, IPixel { - return CompareToOriginal(image, provider, ImageComparer.Tolerant()); + return CompareToOriginal(image, provider, ImageComparer.Tolerant(), referenceDecoder); } public static Image CompareToOriginal( this Image image, ITestImageProvider provider, - ImageComparer comparer) + ImageComparer comparer, + IImageDecoder referenceDecoder = null) where TPixel : struct, IPixel { string path = TestImageProvider.GetFilePathOrNull(provider); @@ -519,15 +521,8 @@ namespace SixLabors.ImageSharp.Tests var testFile = TestFile.Create(path); - IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(path); - IImageFormat format = TestEnvironment.GetImageFormat(path); - IImageDecoder defaultDecoder = Configuration.Default.ImageFormatsManager.FindDecoder(format); - - //if (referenceDecoder.GetType() == defaultDecoder.GetType()) - //{ - // throw new InvalidOperationException($"Can't use CompareToOriginal(): no actual reference decoder registered for {format.Name}"); - //} - + referenceDecoder = referenceDecoder ?? TestEnvironment.GetReferenceDecoder(path); + using (var original = Image.Load(testFile.Bytes, referenceDecoder)) { comparer.VerifySimilarity(original, image); diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceDecoderBenchmarks.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceDecoderBenchmarks.cs new file mode 100644 index 000000000..724c2e414 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceDecoderBenchmarks.cs @@ -0,0 +1,96 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Collections.Generic; + +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; + +using Xunit; +using Xunit.Abstractions; + +namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests +{ + public class ReferenceDecoderBenchmarks + { + private ITestOutputHelper Output { get; } + + public const string SkipBenchmarks = +#if false + "Benchmark, enable manually!"; +#else + null; +#endif + + public const int DefaultExecutionCount = 50; + + public static readonly string[] PngBenchmarkFiles = + { + TestImages.Png.CalliphoraPartial, + TestImages.Png.Kaboom, + TestImages.Png.Bike, + TestImages.Png.Splash, + TestImages.Png.SplashInterlaced + }; + + public static readonly string[] BmpBenchmarkFiles = + { + TestImages.Bmp.NegHeight, + TestImages.Bmp.Car, + TestImages.Bmp.V5Header + }; + + public ReferenceDecoderBenchmarks(ITestOutputHelper output) + { + this.Output = output; + } + + [Theory(Skip = SkipBenchmarks)] + [WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] + public void BenchmarkMagickPngDecoder(TestImageProvider provider) + where TPixel : struct, IPixel + { + this.BenckmarkDecoderImpl(PngBenchmarkFiles, new MagickReferenceDecoder(), $@"Magick Decode Png"); + } + + [Theory(Skip = SkipBenchmarks)] + [WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] + public void BenchmarkSystemDrawingPngDecoder(TestImageProvider provider) + where TPixel : struct, IPixel + { + this.BenckmarkDecoderImpl(PngBenchmarkFiles, new SystemDrawingReferenceDecoder(), $@"System.Drawing Decode Png"); + } + + [Theory(Skip = SkipBenchmarks)] + [WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] + public void BenchmarkMagickBmpDecoder(TestImageProvider provider) + where TPixel : struct, IPixel + { + this.BenckmarkDecoderImpl(BmpBenchmarkFiles, new MagickReferenceDecoder(), $@"Magick Decode Bmp"); + } + + [Theory(Skip = SkipBenchmarks)] + [WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] + public void BenchmarkSystemDrawingBmpDecoder(TestImageProvider provider) + where TPixel : struct, IPixel + { + this.BenckmarkDecoderImpl(BmpBenchmarkFiles, new SystemDrawingReferenceDecoder(), $@"System.Drawing Decode Bmp"); + } + + private void BenckmarkDecoderImpl(IEnumerable testFiles, IImageDecoder decoder, string info, int times = DefaultExecutionCount) + { + var measure = new MeasureFixture(this.Output); + measure.Measure(times, + () => + { + foreach (string testFile in testFiles) + { + Image image = TestFile.Create(testFile).CreateImage(decoder); + image.Dispose(); + } + }, + info); + } + } +} \ No newline at end of file From ce0dd481313ef3baad6686cd820c903488d9c336 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 27 Jun 2018 00:39:43 +0200 Subject: [PATCH 09/23] keep using SystemDrawingReferenceDecoder for Bmp-s --- .../TestUtilities/TestEnvironment.Formats.cs | 4 ++-- tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs | 3 ++- .../TestUtilities/Tests/TestEnvironmentTests.cs | 6 +++--- .../TestUtilities/Tests/TestImageProviderTests.cs | 7 ++++++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs index 30067ec2d..ccda71613 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs @@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp.Tests new GifConfigurationModule() ); - // Magick codecs should work on all + // Magick codecs should work on all platforms cfg.ConfigureCodecs( ImageFormats.Png, MagickReferenceDecoder.Instance, @@ -66,7 +66,7 @@ namespace SixLabors.ImageSharp.Tests cfg.ConfigureCodecs( ImageFormats.Bmp, - MagickReferenceDecoder.Instance, + SystemDrawingReferenceDecoder.Instance, SystemDrawingReferenceEncoder.Bmp, new BmpImageFormatDetector()); diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index f0b732998..a5a3e332c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -65,6 +65,7 @@ namespace SixLabors.ImageSharp.Tests $"Unable to find ImageSharp solution directory from {assemblyLocation} because of {ex.GetType().Name}!", ex); } + if (directory == null) { throw new Exception($"Unable to find ImageSharp solution directory from {assemblyLocation}!"); @@ -116,7 +117,7 @@ namespace SixLabors.ImageSharp.Tests /// internal static string CreateOutputDirectory(string path, params string[] pathParts) { - path = Path.Combine(TestEnvironment.ActualOutputDirectoryFullPath, path); + path = Path.Combine(ActualOutputDirectoryFullPath, path); if (pathParts != null && pathParts.Length > 0) { diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs index f6d3bdb7b..2c824729c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs @@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp.Tests } [Theory] - [InlineData("lol/foo.png", typeof(SystemDrawingReferenceDecoder))] + [InlineData("lol/foo.png", typeof(MagickReferenceDecoder))] [InlineData("lol/Rofl.bmp", typeof(SystemDrawingReferenceDecoder))] [InlineData("lol/Baz.JPG", typeof(JpegDecoder))] [InlineData("lol/Baz.gif", typeof(GifDecoder))] @@ -125,8 +125,8 @@ namespace SixLabors.ImageSharp.Tests } [Theory] - [InlineData("lol/foo.png", typeof(PngDecoder))] - [InlineData("lol/Rofl.bmp", typeof(BmpDecoder))] + [InlineData("lol/foo.png", typeof(MagickReferenceDecoder))] + [InlineData("lol/Rofl.bmp", typeof(SystemDrawingReferenceDecoder))] [InlineData("lol/Baz.JPG", typeof(JpegDecoder))] [InlineData("lol/Baz.gif", typeof(GifDecoder))] public void GetReferenceDecoder_ReturnsCorrectDecoders_Linux(string fileName, Type expectedDecoderType) diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 06c77235b..efc75773e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -241,7 +241,12 @@ namespace SixLabors.ImageSharp.Tests } - public static string[] AllBmpFiles => TestImages.Bmp.All; + public static string[] AllBmpFiles = + { + TestImages.Bmp.F, + TestImages.Bmp.CoreHeader, + TestImages.Bmp.Bit8 + }; [Theory] [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Rgba32 | PixelTypes.Argb32)] From 7ac9ed62afb9a054eee1def9c1fd99a7cfae1e6e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 27 Jun 2018 00:48:46 +0200 Subject: [PATCH 10/23] clanup + exact comparison for all PngDecoderTests --- .../Formats/Png/PngDecoderTests.cs | 97 ++++--------------- 1 file changed, 19 insertions(+), 78 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 53f71fb7b..586ca9bba 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -1,28 +1,24 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +// ReSharper disable InconsistentNaming + +using System.Buffers.Binary; using System.IO; using System.Text; + +using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + using Xunit; -// ReSharper disable InconsistentNaming -namespace SixLabors.ImageSharp.Tests +namespace SixLabors.ImageSharp.Tests.Formats.Png { - using System.Buffers.Binary; - using System.Linq; - - using SixLabors.ImageSharp.Formats.Png; - using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; - - // TODO: Fix all bugs, and re enable Skipped and commented stuff !!! public class PngDecoderTests { private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; - - // TODO: Cannot use exact comparer since System.Drawing doesn't preserve more than 32bits. - private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.1302F, 2134); - + // Contains the png marker, IHDR and pHYs chunks of a 1x1 pixel 32bit png 1 a single black pixel. private static readonly byte[] raw1x1PngIHDRAndpHYs = { @@ -105,30 +101,6 @@ namespace SixLabors.ImageSharp.Tests TestImages.Png.GrayTrns16BitInterlaced }; - // This is a workaround for Mono-s decoder being incompatible with ours and GDI+. - // We shouldn't mix these with the Interleaved cases (which are also failing with Mono System.Drawing). Let's go AAA! - private static readonly string[] SkipOnMono = - { - TestImages.Png.Bad.ChunkLength2, - TestImages.Png.VimImage2, - TestImages.Png.Splash, - TestImages.Png.Indexed, - TestImages.Png.Bad.ChunkLength1, - TestImages.Png.VersioningImage1, - TestImages.Png.Banner7Adam7InterlaceMode, - TestImages.Png.GrayTrns16BitInterlaced, - TestImages.Png.Rgb48BppInterlaced - }; - - private static bool SkipVerification(ITestImageProvider provider) - { - string fn = provider.SourceFileOrDescription; - - // This is a workaround for Mono-s decoder being incompatible with ours and GDI+. - // We shouldn't mix these with the Interleaved cases (which are also failing with Mono System.Drawing). Let's go AAA! - return (TestEnvironment.IsLinux || TestEnvironment.IsMono) && SkipOnMono.Contains(fn); - } - [Theory] [WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)] public void Decode(TestImageProvider provider) @@ -137,22 +109,7 @@ namespace SixLabors.ImageSharp.Tests using (Image image = provider.GetImage(new PngDecoder())) { image.DebugSave(provider); - - if (!SkipVerification(provider)) - { - image.CompareToOriginal(provider, ImageComparer.Exact); - } - } - } - - [Theory] - [WithFile(TestImages.Png.Interlaced, PixelTypes.Rgba32)] - public void Decode_Interlaced_DoesNotThrow(TestImageProvider provider) - where TPixel : struct, IPixel - { - using (Image image = provider.GetImage(new PngDecoder())) - { - image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Exact); } } @@ -175,12 +132,8 @@ namespace SixLabors.ImageSharp.Tests { using (Image image = provider.GetImage(new PngDecoder())) { - var encoder = new PngEncoder { ColorType = PngColorType.Rgb, BitDepth = PngBitDepth.Bit16 }; - - if (!SkipVerification(provider)) - { - image.VerifyEncoder(provider, "png", null, encoder, customComparer: ValidatorComparer); - } + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Exact); } } @@ -191,12 +144,8 @@ namespace SixLabors.ImageSharp.Tests { using (Image image = provider.GetImage(new PngDecoder())) { - var encoder = new PngEncoder { ColorType = PngColorType.RgbWithAlpha, BitDepth = PngBitDepth.Bit16 }; - - if (!SkipVerification(provider)) - { - image.VerifyEncoder(provider, "png", null, encoder, customComparer: ValidatorComparer); - } + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Exact); } } @@ -207,12 +156,8 @@ namespace SixLabors.ImageSharp.Tests { using (Image image = provider.GetImage(new PngDecoder())) { - var encoder = new PngEncoder { ColorType = PngColorType.Grayscale, BitDepth = PngBitDepth.Bit16 }; - - if (!SkipVerification(provider)) - { - image.VerifyEncoder(provider, "png", null, encoder, customComparer: ValidatorComparer); - } + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Exact); } } @@ -223,12 +168,8 @@ namespace SixLabors.ImageSharp.Tests { using (Image image = provider.GetImage(new PngDecoder())) { - var encoder = new PngEncoder { ColorType = PngColorType.GrayscaleWithAlpha, BitDepth = PngBitDepth.Bit16 }; - - if (!SkipVerification(provider)) - { - image.VerifyEncoder(provider, "png", null, encoder, customComparer: ValidatorComparer); - } + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Exact); } } @@ -303,7 +244,7 @@ namespace SixLabors.ImageSharp.Tests [InlineData(TestImages.Png.Blur, 32)] [InlineData(TestImages.Png.Rgb48Bpp, 48)] [InlineData(TestImages.Png.Rgb48BppInterlaced, 48)] - public void DetectPixelSize(string imagePath, int expectedPixelSize) + public void Identify(string imagePath, int expectedPixelSize) { var testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) From 055ca0c86a4ee39cc1316621c74add44779a1039 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 27 Jun 2018 00:51:29 +0200 Subject: [PATCH 11/23] PngDecoderTests.Chunks.cs --- .../Formats/Png/PngDecoderTests.Chunks.cs | 127 ++++++++++++++++++ .../Formats/Png/PngDecoderTests.cs | 110 +-------------- 2 files changed, 129 insertions(+), 108 deletions(-) create mode 100644 tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs new file mode 100644 index 000000000..6f04ba651 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs @@ -0,0 +1,127 @@ +using System.Buffers.Binary; +using System.IO; +using System.Text; + +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.PixelFormats; + +using Xunit; +// ReSharper disable InconsistentNaming + +namespace SixLabors.ImageSharp.Tests.Formats.Png +{ + public partial class PngDecoderTests + { + // Contains the png marker, IHDR and pHYs chunks of a 1x1 pixel 32bit png 1 a single black pixel. + private static readonly byte[] Raw1X1PngIhdrAndpHYs = + { + // PNG Identifier + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, + + // IHDR + 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, + 0x00, 0x00, 0x00, + // IHDR CRC + 0x90, 0x77, 0x53, 0xDE, + + // pHYS + 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, + 0x00, 0x0E, 0xC3, 0x00, 0x00, 0x0E, 0xC3, 0x01, + // pHYS CRC + 0xC7, 0x6F, 0xA8, 0x64 + }; + + // Contains the png marker, IDAT and IEND chunks of a 1x1 pixel 32bit png 1 a single black pixel. + private static readonly byte[] Raw1X1PngIdatAndIend = + { + // IDAT + 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x18, + 0x57, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x04, + 0x00, 0x01, + + // IDAT CRC + 0x5C, 0xCD, 0xFF, 0x69, + + // IEND + 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, + + // IEND CRC + 0xAE, 0x42, 0x60, 0x82 + }; + + [Theory] + [InlineData((uint)PngChunkType.Header)] // IHDR + [InlineData((uint)PngChunkType.Palette)] // PLTE + // [InlineData(PngChunkTypes.Data)] //TODO: Figure out how to test this + [InlineData((uint)PngChunkType.End)] // IEND + public void Decode_IncorrectCRCForCriticalChunk_ExceptionIsThrown(uint chunkType) + { + string chunkName = GetChunkTypeName(chunkType); + + using (var memStream = new MemoryStream()) + { + WriteHeaderChunk(memStream); + WriteChunk(memStream, chunkName); + WriteDataChunk(memStream); + + var decoder = new PngDecoder(); + + ImageFormatException exception = + Assert.Throws(() => decoder.Decode(null, memStream)); + + Assert.Equal($"CRC Error. PNG {chunkName} chunk is corrupt!", exception.Message); + } + } + + [Theory] + [InlineData((uint)PngChunkType.Gamma)] // gAMA + [InlineData((uint)PngChunkType.PaletteAlpha)] // tRNS + [InlineData( + (uint)PngChunkType.Physical)] // pHYs: It's ok to test physical as we don't throw for duplicate chunks. + //[InlineData(PngChunkTypes.Text)] //TODO: Figure out how to test this + public void Decode_IncorrectCRCForNonCriticalChunk_ExceptionIsThrown(uint chunkType) + { + string chunkName = GetChunkTypeName(chunkType); + + using (var memStream = new MemoryStream()) + { + WriteHeaderChunk(memStream); + WriteChunk(memStream, chunkName); + WriteDataChunk(memStream); + + var decoder = new PngDecoder(); + decoder.Decode(null, memStream); + } + } + + private static string GetChunkTypeName(uint value) + { + byte[] data = new byte[4]; + + BinaryPrimitives.WriteUInt32BigEndian(data, value); + + return Encoding.ASCII.GetString(data); + } + + private static void WriteHeaderChunk(MemoryStream memStream) + { + // Writes a 1x1 32bit png header chunk containing a single black pixel + memStream.Write(Raw1X1PngIhdrAndpHYs, 0, Raw1X1PngIhdrAndpHYs.Length); + } + + private static void WriteChunk(MemoryStream memStream, string chunkName) + { + memStream.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); + memStream.Write(Encoding.GetEncoding("ASCII").GetBytes(chunkName), 0, 4); + memStream.Write(new byte[] { 0, 0, 0, 0, 0 }, 0, 5); + } + + private static void WriteDataChunk(MemoryStream memStream) + { + // Writes a 1x1 32bit png data chunk containing a single black pixel + memStream.Write(Raw1X1PngIdatAndIend, 0, Raw1X1PngIdatAndIend.Length); + memStream.Position = 0; + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 586ca9bba..66e4f39fd 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -15,45 +15,11 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Formats.Png { - public class PngDecoderTests + public partial class PngDecoderTests { private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; - // Contains the png marker, IHDR and pHYs chunks of a 1x1 pixel 32bit png 1 a single black pixel. - private static readonly byte[] raw1x1PngIHDRAndpHYs = - { - // PNG Identifier - 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, - - // IHDR - 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, - 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, - // IHDR CRC - 0x90, 0x77, 0x53, 0xDE, - - // pHYS - 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0E, 0xC3, 0x00, 0x00, 0x0E, 0xC3, 0x01, - // pHYS CRC - 0xC7, 0x6F, 0xA8, 0x64 - }; - - // Contains the png marker, IDAT and IEND chunks of a 1x1 pixel 32bit png 1 a single black pixel. - private static readonly byte[] raw1x1PngIDATAndIEND = - { - // IDAT - 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x18, 0x57, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00, - 0x00, 0x04, 0x00, 0x01, - - // IDAT CRC - 0x5C, 0xCD, 0xFF, 0x69, - - // IEND - 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, - 0x4E, 0x44, - - // IEND CRC - 0xAE, 0x42, 0x60, 0x82 - }; + public static readonly string[] CommonTestImages = { @@ -252,77 +218,5 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel); } } - - [Theory] - [InlineData((uint)PngChunkType.Header)] // IHDR - [InlineData((uint)PngChunkType.Palette)] // PLTE - // [InlineData(PngChunkTypes.Data)] //TODO: Figure out how to test this - [InlineData((uint)PngChunkType.End)] // IEND - public void Decode_IncorrectCRCForCriticalChunk_ExceptionIsThrown(uint chunkType) - { - string chunkName = GetChunkTypeName(chunkType); - - using (var memStream = new MemoryStream()) - { - WriteHeaderChunk(memStream); - WriteChunk(memStream, chunkName); - WriteDataChunk(memStream); - - var decoder = new PngDecoder(); - - ImageFormatException exception = Assert.Throws(() => decoder.Decode(null, memStream)); - - Assert.Equal($"CRC Error. PNG {chunkName} chunk is corrupt!", exception.Message); - } - } - - [Theory] - [InlineData((uint)PngChunkType.Gamma)] // gAMA - [InlineData((uint)PngChunkType.PaletteAlpha)] // tRNS - [InlineData((uint)PngChunkType.Physical)] // pHYs: It's ok to test physical as we don't throw for duplicate chunks. - //[InlineData(PngChunkTypes.Text)] //TODO: Figure out how to test this - public void Decode_IncorrectCRCForNonCriticalChunk_ExceptionIsThrown(uint chunkType) - { - string chunkName = GetChunkTypeName(chunkType); - - using (var memStream = new MemoryStream()) - { - WriteHeaderChunk(memStream); - WriteChunk(memStream, chunkName); - WriteDataChunk(memStream); - - var decoder = new PngDecoder(); - decoder.Decode(null, memStream); - } - } - - private static string GetChunkTypeName(uint value) - { - byte[] data = new byte[4]; - - BinaryPrimitives.WriteUInt32BigEndian(data, value); - - return Encoding.ASCII.GetString(data); - } - - private static void WriteHeaderChunk(MemoryStream memStream) - { - // Writes a 1x1 32bit png header chunk containing a single black pixel - memStream.Write(raw1x1PngIHDRAndpHYs, 0, raw1x1PngIHDRAndpHYs.Length); - } - - private static void WriteChunk(MemoryStream memStream, string chunkName) - { - memStream.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); - memStream.Write(Encoding.GetEncoding("ASCII").GetBytes(chunkName), 0, 4); - memStream.Write(new byte[] { 0, 0, 0, 0, 0 }, 0, 5); - } - - private static void WriteDataChunk(MemoryStream memStream) - { - // Writes a 1x1 32bit png data chunk containing a single black pixel - memStream.Write(raw1x1PngIDATAndIEND, 0, raw1x1PngIDATAndIEND.Length); - memStream.Position = 0; - } } } \ No newline at end of file From d116d83ffb92c83fad31dad03af7c93f1b54213a Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 15:54:55 -0700 Subject: [PATCH 12/23] [ICC] Eliminate Version allocation using custom struct --- .../DataReader/IccDataReader.NonPrimitives.cs | 4 +- .../DataWriter/IccDataWriter.NonPrimitives.cs | 16 +++---- .../MetaData/Profiles/ICC/IccProfileHeader.cs | 34 +++++++------- .../Profiles/ICC/Various/IccVersion.cs | 45 +++++++++++++++++++ 4 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitives.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitives.cs index 8b942498a..7dc8cf98a 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitives.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitives.cs @@ -38,7 +38,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// Reads an ICC profile version number /// /// the version number - public Version ReadVersionNumber() + public IccVersion ReadVersionNumber() { int version = this.ReadInt32(); @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc int minor = (version >> 20) & 0x0F; int bugfix = (version >> 16) & 0x0F; - return new Version(major, minor, bugfix); + return new IccVersion(major, minor, bugfix); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs index 791a94a33..1a3c2c0ac 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs @@ -31,11 +31,11 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// The value to write /// the number of bytes written - public int WriteVersionNumber(Version value) + public int WriteVersionNumber(in IccVersion value) { int major = value.Major.Clamp(0, byte.MaxValue); int minor = value.Minor.Clamp(0, 15); - int bugfix = value.Build.Clamp(0, 15); + int bugfix = value.Patch.Clamp(0, 15); // TODO: This is not used? byte mb = (byte)((minor << 4) | bugfix); @@ -61,7 +61,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// The value to write /// the number of bytes written - public int WriteProfileId(IccProfileId value) + public int WriteProfileId(in IccProfileId value) { return this.WriteUInt32(value.Part1) + this.WriteUInt32(value.Part2) @@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// The value to write /// the number of bytes written - public int WritePositionNumber(IccPositionNumber value) + public int WritePositionNumber(in IccPositionNumber value) { return this.WriteUInt32(value.Offset) + this.WriteUInt32(value.Size); @@ -85,7 +85,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// The value to write /// the number of bytes written - public int WriteResponseNumber(IccResponseNumber value) + public int WriteResponseNumber(in IccResponseNumber value) { return this.WriteUInt16(value.DeviceCode) + this.WriteFix16(value.MeasurementValue); @@ -96,7 +96,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// The value to write /// the number of bytes written - public int WriteNamedColor(IccNamedColor value) + public int WriteNamedColor(in IccNamedColor value) { return this.WriteAsciiString(value.Name, 32, true) + this.WriteArray(value.PcsCoordinates) @@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// The value to write /// the number of bytes written - public int WriteProfileDescription(IccProfileDescription value) + public int WriteProfileDescription(in IccProfileDescription value) { return this.WriteUInt32(value.DeviceManufacturer) + this.WriteUInt32(value.DeviceModel) @@ -125,7 +125,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// The value to write /// the number of bytes written - public int WriteScreeningChannel(IccScreeningChannel value) + public int WriteScreeningChannel(in IccScreeningChannel value) { return this.WriteFix16(value.Frequency) + this.WriteFix16(value.Angle) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/IccProfileHeader.cs b/src/ImageSharp/MetaData/Profiles/ICC/IccProfileHeader.cs index f91572cfe..189b40275 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/IccProfileHeader.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/IccProfileHeader.cs @@ -7,42 +7,42 @@ using System.Numerics; namespace SixLabors.ImageSharp.MetaData.Profiles.Icc { /// - /// Contains all values of an ICC profile header + /// Contains all values of an ICC profile header. /// public sealed class IccProfileHeader { /// - /// Gets or sets the profile size in bytes (will be ignored when writing a profile) + /// Gets or sets the profile size in bytes (will be ignored when writing a profile). /// public uint Size { get; set; } /// - /// Gets or sets the preferred CMM (Color Management Module) type + /// Gets or sets the preferred CMM (Color Management Module) type. /// public string CmmType { get; set; } /// - /// Gets or sets the profiles version number + /// Gets or sets the profiles version number. /// - public Version Version { get; set; } + public IccVersion Version { get; set; } /// - /// Gets or sets the type of the profile + /// Gets or sets the type of the profile. /// public IccProfileClass Class { get; set; } /// - /// Gets or sets the data colorspace + /// Gets or sets the data colorspace. /// public IccColorSpaceType DataColorSpace { get; set; } /// - /// Gets or sets the profile connection space + /// Gets or sets the profile connection space. /// public IccColorSpaceType ProfileConnectionSpace { get; set; } /// - /// Gets or sets the date and time this profile was created + /// Gets or sets the date and time this profile was created. /// public DateTime CreationDate { get; set; } @@ -59,42 +59,42 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// /// Gets or sets the profile flags to indicate various options for the CMM - /// such as distributed processing and caching options + /// such as distributed processing and caching options. /// public IccProfileFlag Flags { get; set; } /// - /// Gets or sets the device manufacturer of the device for which this profile is created + /// Gets or sets the device manufacturer of the device for which this profile is created. /// public uint DeviceManufacturer { get; set; } /// - /// Gets or sets the model of the device for which this profile is created + /// Gets or sets the model of the device for which this profile is created. /// public uint DeviceModel { get; set; } /// - /// Gets or sets the device attributes unique to the particular device setup such as media type + /// Gets or sets the device attributes unique to the particular device setup such as media type. /// public IccDeviceAttribute DeviceAttributes { get; set; } /// - /// Gets or sets the rendering Intent + /// Gets or sets the rendering Intent. /// public IccRenderingIntent RenderingIntent { get; set; } /// - /// Gets or sets The normalized XYZ values of the illuminant of the PCS + /// Gets or sets The normalized XYZ values of the illuminant of the PCS. /// public Vector3 PcsIlluminant { get; set; } /// - /// Gets or sets Profile creator signature + /// Gets or sets profile creator signature. /// public string CreatorSignature { get; set; } /// - /// Gets or sets the profile ID (hash) + /// Gets or sets the profile ID (hash). /// public IccProfileId Id { get; set; } } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs new file mode 100644 index 000000000..f38bdcd5f --- /dev/null +++ b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs @@ -0,0 +1,45 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.MetaData.Profiles.Icc +{ + /// + /// Represents the ICC profile version number. + /// + public readonly struct IccVersion + { + /// + /// Initializes a new instance of the struct. + /// + /// The major version number. + /// The minor version number. + /// The patch version number. + public IccVersion(int major, int minor, int patch) + { + this.Major = major; + this.Minor = minor; + this.Patch = patch; + } + + /// + /// Gets the major version number. + /// + public int Major { get; } + + /// + /// Gets the minor version number. + /// + public int Minor { get; } + + /// + /// Gets the patch number. + /// + public int Patch { get; } + + /// + public override string ToString() + { + return string.Join(".", this.Major, this.Minor, this.Patch); + } + } +} From c9bab4cdfc9c2a883328b5f34789c3d0c633cbf0 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 15:56:23 -0700 Subject: [PATCH 13/23] Remove redundant equity conditions --- .../MetaData/Profiles/Exif/ExifValue.cs | 2 +- .../Profiles/ICC/Curves/IccParametricCurve.cs | 12 +------- .../Profiles/ICC/Curves/IccResponseCurve.cs | 10 ------- .../MetaData/Profiles/ICC/IccTagDataEntry.cs | 28 ++++++------------- .../IccChromaticityTagDataEntry.cs | 12 +------- .../IccColorantOrderTagDataEntry.cs | 10 ------- .../IccColorantTableTagDataEntry.cs | 10 ------- .../TagDataEntries/IccCrdInfoTagDataEntry.cs | 10 ------- .../TagDataEntries/IccCurveTagDataEntry.cs | 10 ------- .../ICC/TagDataEntries/IccDataTagDataEntry.cs | 10 ------- .../TagDataEntries/IccDateTimeTagDataEntry.cs | 10 ------- .../IccFix16ArrayTagDataEntry.cs | 10 ------- .../TagDataEntries/IccLut16TagDataEntry.cs | 12 +------- .../ICC/TagDataEntries/IccLut8TagDataEntry.cs | 12 +------- .../TagDataEntries/IccLutAToBTagDataEntry.cs | 10 ------- .../TagDataEntries/IccLutBToATagDataEntry.cs | 10 ------- .../IccMeasurementTagDataEntry.cs | 12 +------- .../IccMultiLocalizedUnicodeTagDataEntry.cs | 11 +------- .../IccMultiProcessElementsTagDataEntry.cs | 10 ------- .../IccNamedColor2TagDataEntry.cs | 10 ------- .../IccParametricCurveTagDataEntry.cs | 10 ------- .../IccProfileSequenceDescTagDataEntry.cs | 10 ------- ...ccProfileSequenceIdentifierTagDataEntry.cs | 12 +------- .../IccResponseCurveSet16TagDataEntry.cs | 4 +-- .../IccScreeningTagDataEntry.cs | 12 +------- .../IccSignatureTagDataEntry.cs | 12 +------- .../IccTextDescriptionTagDataEntry.cs | 12 +------- .../ICC/TagDataEntries/IccTextTagDataEntry.cs | 12 +------- .../IccUFix16ArrayTagDataEntry.cs | 10 ------- .../IccUInt16ArrayTagDataEntry.cs | 10 ------- .../IccUInt32ArrayTagDataEntry.cs | 12 +------- .../IccUInt64ArrayTagDataEntry.cs | 14 ++-------- .../IccUInt8ArrayTagDataEntry.cs | 10 ------- .../TagDataEntries/IccUcrBgTagDataEntry.cs | 10 ------- .../TagDataEntries/IccUnknownTagDataEntry.cs | 10 ------- .../IccViewingConditionsTagDataEntry.cs | 10 ------- .../ICC/TagDataEntries/IccXyzTagDataEntry.cs | 2 +- .../MetaData/Profiles/ICC/Various/IccClut.cs | 10 ------- 38 files changed, 27 insertions(+), 376 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs index bdd902e23..d475959c6 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs @@ -181,7 +181,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif /// public bool Equals(ExifValue other) { - if (ReferenceEquals(other, null)) + if (other is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccParametricCurve.cs b/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccParametricCurve.cs index 9c3f8aa5e..a241acd21 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccParametricCurve.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccParametricCurve.cs @@ -125,7 +125,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccParametricCurve other) { - if (other == null) + if (other is null) { return false; } @@ -148,16 +148,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccParametricCurve other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccResponseCurve.cs b/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccResponseCurve.cs index 02a817b8c..e15d8a434 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccResponseCurve.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/Curves/IccResponseCurve.cs @@ -67,16 +67,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccResponseCurve other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/IccTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/IccTagDataEntry.cs index 1b0d041b6..231f3818a 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/IccTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/IccTagDataEntry.cs @@ -44,28 +44,9 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccTagDataEntry entry && this.Equals(entry); } - /// - public override int GetHashCode() - { - unchecked - { - return (int)this.Signature * 397; - } - } - /// public virtual bool Equals(IccTagDataEntry other) { @@ -81,5 +62,14 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc return this.Signature == other.Signature; } + + /// + public override int GetHashCode() + { + unchecked + { + return (int)this.Signature * 397; + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs index b400e1bd7..b95b5c388 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs @@ -88,7 +88,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccChromaticityTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } @@ -104,16 +104,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccChromaticityTagDataEntry && this.Equals((IccChromaticityTagDataEntry)obj); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantOrderTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantOrderTagDataEntry.cs index 73024ee12..2194b8ab4 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantOrderTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantOrderTagDataEntry.cs @@ -65,16 +65,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccColorantOrderTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantTableTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantTableTagDataEntry.cs index 353dab604..90b1c304b 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantTableTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccColorantTableTagDataEntry.cs @@ -66,16 +66,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccColorantTableTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCrdInfoTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCrdInfoTagDataEntry.cs index 848418f95..b2bbb7b56 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCrdInfoTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCrdInfoTagDataEntry.cs @@ -115,16 +115,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccCrdInfoTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCurveTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCurveTagDataEntry.cs index c9a59bb32..154afd8ed 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCurveTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCurveTagDataEntry.cs @@ -113,16 +113,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccCurveTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDataTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDataTagDataEntry.cs index c8f5f8b7c..a1addaa90 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDataTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDataTagDataEntry.cs @@ -89,16 +89,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccDataTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs index 7a2d97571..6eeeaee7c 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs @@ -60,16 +60,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccDateTimeTagDataEntry && this.Equals((IccDateTimeTagDataEntry)obj); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccFix16ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccFix16ArrayTagDataEntry.cs index afe4e0bd3..b0d9e1ef9 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccFix16ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccFix16ArrayTagDataEntry.cs @@ -62,16 +62,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccFix16ArrayTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut16TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut16TagDataEntry.cs index d98e45ace..f296a8b07 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut16TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut16TagDataEntry.cs @@ -137,17 +137,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - return obj is IccLut16TagDataEntry && this.Equals((IccLut16TagDataEntry)obj); + return obj is IccLut16TagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut8TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut8TagDataEntry.cs index e57e0f543..f94d500c3 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut8TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLut8TagDataEntry.cs @@ -140,17 +140,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - return obj is IccLut8TagDataEntry && this.Equals((IccLut8TagDataEntry)obj); + return obj is IccLut8TagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutAToBTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutAToBTagDataEntry.cs index 59c80d409..c4f3f8a2a 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutAToBTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutAToBTagDataEntry.cs @@ -177,16 +177,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccLutAToBTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutBToATagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutBToATagDataEntry.cs index 57b17c452..17bbf915b 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutBToATagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccLutBToATagDataEntry.cs @@ -177,16 +177,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccLutBToATagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMeasurementTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMeasurementTagDataEntry.cs index 5f2dbe347..f32e17714 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMeasurementTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMeasurementTagDataEntry.cs @@ -100,17 +100,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - return obj is IccMeasurementTagDataEntry && this.Equals((IccMeasurementTagDataEntry)obj); + return obj is IccMeasurementTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs index d1745faac..6e49f372c 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs @@ -63,17 +63,8 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccMultiLocalizedUnicodeTagDataEntry && this.Equals((IccMultiLocalizedUnicodeTagDataEntry)obj); + return obj is IccMultiLocalizedUnicodeTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiProcessElementsTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiProcessElementsTagDataEntry.cs index 8b0c06568..dcfe010aa 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiProcessElementsTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiProcessElementsTagDataEntry.cs @@ -84,16 +84,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccMultiProcessElementsTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs index bdb1aacb3..7951784ee 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs @@ -148,16 +148,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccNamedColor2TagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs index e8bbc5e8f..2b779cfb1 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs @@ -61,16 +61,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccParametricCurveTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs index cde7c4043..58bbfb7cb 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs @@ -64,16 +64,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccProfileSequenceDescTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceIdentifierTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceIdentifierTagDataEntry.cs index 2309a460e..f6b0582fb 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceIdentifierTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceIdentifierTagDataEntry.cs @@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccProfileSequenceIdentifierTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } @@ -63,16 +63,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccProfileSequenceIdentifierTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs index 5925454a3..59041d1f7 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccResponseCurveSet16TagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } @@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) + if (obj is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccScreeningTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccScreeningTagDataEntry.cs index 1e17d0862..c93781d9e 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccScreeningTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccScreeningTagDataEntry.cs @@ -56,7 +56,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccScreeningTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } @@ -74,16 +74,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccScreeningTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccSignatureTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccSignatureTagDataEntry.cs index a808541cf..e469e7eab 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccSignatureTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccSignatureTagDataEntry.cs @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccSignatureTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } @@ -62,16 +62,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccSignatureTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs index c509197e4..c6e6af064 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs @@ -160,17 +160,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - return obj is IccTextDescriptionTagDataEntry && this.Equals((IccTextDescriptionTagDataEntry)obj); + return obj is IccTextDescriptionTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextTagDataEntry.cs index f5e31ea87..1cf321893 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextTagDataEntry.cs @@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccTextTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } @@ -61,16 +61,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccTextTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs index c619b40d4..79d8dc97a 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs @@ -62,16 +62,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccUFix16ArrayTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs index 4f1959cf1..408d0689f 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs @@ -62,16 +62,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccUInt16ArrayTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt32ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt32ArrayTagDataEntry.cs index 00ca43084..2e3efe1c7 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt32ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt32ArrayTagDataEntry.cs @@ -62,17 +62,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - return obj is IccUInt32ArrayTagDataEntry && this.Equals((IccUInt32ArrayTagDataEntry)obj); + return obj is IccUInt32ArrayTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs index 27c273e42..cad816ab9 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs @@ -61,18 +61,8 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) - { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - return obj is IccUInt64ArrayTagDataEntry && this.Equals((IccUInt64ArrayTagDataEntry)obj); + { + return obj is IccUInt64ArrayTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt8ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt8ArrayTagDataEntry.cs index bf6fdd662..a673abf68 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt8ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt8ArrayTagDataEntry.cs @@ -62,16 +62,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccUInt8ArrayTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUcrBgTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUcrBgTagDataEntry.cs index 0f190021f..fd38e659b 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUcrBgTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUcrBgTagDataEntry.cs @@ -85,16 +85,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccUcrBgTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUnknownTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUnknownTagDataEntry.cs index ce3be9b69..0f0a9d218 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUnknownTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUnknownTagDataEntry.cs @@ -62,16 +62,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccUnknownTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs index a4db8f7ab..ab6a44954 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs @@ -80,16 +80,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccViewingConditionsTagDataEntry && this.Equals((IccViewingConditionsTagDataEntry)obj); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccXyzTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccXyzTagDataEntry.cs index d704fee96..b776cc4c0 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccXyzTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccXyzTagDataEntry.cs @@ -55,4 +55,4 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc return this.Equals((IccTagDataEntry)other); } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/MetaData/Profiles/ICC/Various/IccClut.cs b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccClut.cs index c42d85134..e88115438 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/Various/IccClut.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccClut.cs @@ -136,16 +136,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - return obj is IccClut other && this.Equals(other); } From bb9238d447dde5a02055c21d1e8c90c4c8f43c5a Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 15:57:17 -0700 Subject: [PATCH 14/23] Use Unsafe.As<> --- src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs | 9 +++++---- .../Profiles/ICC/DataReader/IccDataReader.Primitives.cs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs index d3ea9743f..6d473fd4b 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs @@ -6,6 +6,7 @@ using System.Buffers.Binary; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using SixLabors.ImageSharp.IO; @@ -462,7 +463,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif } } - private unsafe double ConvertToDouble(ReadOnlySpan buffer) + private double ConvertToDouble(ReadOnlySpan buffer) { if (buffer.Length < 8) { @@ -473,7 +474,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif ? BinaryPrimitives.ReadInt64BigEndian(buffer) : BinaryPrimitives.ReadInt64LittleEndian(buffer); - return *((double*)&intValue); + return Unsafe.As(ref intValue); } private uint ConvertToUInt32(ReadOnlySpan buffer) @@ -501,7 +502,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif : BinaryPrimitives.ReadUInt16LittleEndian(buffer); } - private unsafe float ConvertToSingle(ReadOnlySpan buffer) + private float ConvertToSingle(ReadOnlySpan buffer) { if (buffer.Length < 4) { @@ -512,7 +513,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif ? BinaryPrimitives.ReadInt32BigEndian(buffer) : BinaryPrimitives.ReadInt32LittleEndian(buffer); - return *((float*)&intValue); + return Unsafe.As(ref intValue); } private Rational ToRational(ReadOnlySpan buffer) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs index 538a31d6a..5be0060f6 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs @@ -3,6 +3,7 @@ using System; using System.Buffers.Binary; +using System.Runtime.CompilerServices; using System.Text; namespace SixLabors.ImageSharp.MetaData.Profiles.Icc @@ -70,22 +71,22 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// Reads a float. /// /// the value - public unsafe float ReadSingle() + public float ReadSingle() { int intValue = this.ReadInt32(); - return *((float*)&intValue); + return Unsafe.As(ref intValue); } /// /// Reads a double /// /// the value - public unsafe double ReadDouble() + public double ReadDouble() { long intValue = this.ReadInt64(); - return *((double*)&intValue); + return Unsafe.As(ref intValue); } /// From 1b54767a8cdaaa99ce5f2e351e366f318ef81238 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 16:05:55 -0700 Subject: [PATCH 15/23] Implement IEquatable on IccVersion --- .../MetaData/Profiles/ICC/Various/IccVersion.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs index f38bdcd5f..2486cc80a 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccVersion.cs @@ -1,12 +1,14 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; + namespace SixLabors.ImageSharp.MetaData.Profiles.Icc { /// /// Represents the ICC profile version number. /// - public readonly struct IccVersion + public readonly struct IccVersion : IEquatable { /// /// Initializes a new instance of the struct. @@ -36,6 +38,12 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public int Patch { get; } + /// + public bool Equals(IccVersion other) => + this.Major == other.Major && + this.Minor == other.Minor && + this.Patch == other.Patch; + /// public override string ToString() { From 57e759c391aeddf4549b60a2c4dfbd44bcab8c6e Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 16:06:38 -0700 Subject: [PATCH 16/23] Update tests for IccVersion --- .../ICC/DataReader/IccDataReader.NonPrimitivesTests.cs | 4 ++-- .../ICC/DataWriter/IccDataWriter.NonPrimitivesTests.cs | 2 +- .../TestDataIcc/IccTestDataNonPrimitives.cs | 8 ++++---- tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitivesTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitivesTests.cs index 880fa0607..86f308ea1 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitivesTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataReader/IccDataReader.NonPrimitivesTests.cs @@ -23,11 +23,11 @@ namespace SixLabors.ImageSharp.Tests.Icc [Theory] [MemberData(nameof(IccTestDataNonPrimitives.VersionNumberTestData), MemberType = typeof(IccTestDataNonPrimitives))] - public void ReadVersionNumber(byte[] data, Version expected) + public void ReadVersionNumber(byte[] data, IccVersion expected) { IccDataReader reader = CreateReader(data); - Version output = reader.ReadVersionNumber(); + IccVersion output = reader.ReadVersionNumber(); Assert.Equal(expected, output); } diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitivesTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitivesTests.cs index eda6a33c7..1d482e2c1 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitivesTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitivesTests.cs @@ -24,7 +24,7 @@ namespace SixLabors.ImageSharp.Tests.Icc [Theory] [MemberData(nameof(IccTestDataNonPrimitives.VersionNumberTestData), MemberType = typeof(IccTestDataNonPrimitives))] - public void WriteVersionNumber(byte[] expected, Version data) + public void WriteVersionNumber(byte[] expected, IccVersion data) { IccDataWriter writer = CreateWriter(); diff --git a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataNonPrimitives.cs b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataNonPrimitives.cs index 3b8c3321a..f16da90c6 100644 --- a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataNonPrimitives.cs +++ b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataNonPrimitives.cs @@ -67,10 +67,10 @@ namespace SixLabors.ImageSharp.Tests #region VersionNumber - public static readonly Version VersionNumber_ValMin = new Version(0, 0, 0); - public static readonly Version VersionNumber_Val211 = new Version(2, 1, 1); - public static readonly Version VersionNumber_Val430 = new Version(4, 3, 0); - public static readonly Version VersionNumber_ValMax = new Version(255, 15, 15); + public static readonly IccVersion VersionNumber_ValMin = new IccVersion(0, 0, 0); + public static readonly IccVersion VersionNumber_Val211 = new IccVersion(2, 1, 1); + public static readonly IccVersion VersionNumber_Val430 = new IccVersion(4, 3, 0); + public static readonly IccVersion VersionNumber_ValMax = new IccVersion(255, 15, 15); public static readonly byte[] VersionNumber_Min = { 0x00, 0x00, 0x00, 0x00 }; public static readonly byte[] VersionNumber_211 = { 0x02, 0x11, 0x00, 0x00 }; diff --git a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs index 586bb818d..cf8cffb32 100644 --- a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs +++ b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Tests ProfileConnectionSpace = IccColorSpaceType.CieXyz, RenderingIntent = IccRenderingIntent.AbsoluteColorimetric, Size = size, - Version = new Version(4, 3, 0), + Version = new IccVersion(4, 3, 0), }; } From fcb8039b17f26b04cba0d398656a9e1f6167dacd Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 16:07:23 -0700 Subject: [PATCH 17/23] Simplify null comparison --- .../ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs | 1 - .../Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs | 2 +- .../ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs | 2 +- .../ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs | 2 +- .../ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs | 2 +- .../Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs | 2 +- .../Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs | 2 +- .../ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs | 2 +- 8 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs index 6e49f372c..c006c9556 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccMultiLocalizedUnicodeTagDataEntry.cs @@ -63,7 +63,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - return obj is IccMultiLocalizedUnicodeTagDataEntry other && this.Equals(other); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs index 7951784ee..c32a45182 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccNamedColor2TagDataEntry.cs @@ -127,7 +127,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccNamedColor2TagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs index 2b779cfb1..46719b80f 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccParametricCurveTagDataEntry.cs @@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccParametricCurveTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs index 58bbfb7cb..c42004634 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccProfileSequenceDescTagDataEntry.cs @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccProfileSequenceDescTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs index c6e6af064..cc67dd1b1 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccTextDescriptionTagDataEntry.cs @@ -139,7 +139,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccTextDescriptionTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs index 79d8dc97a..63a19d6d4 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUFix16ArrayTagDataEntry.cs @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccUFix16ArrayTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs index 408d0689f..d082df39a 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt16ArrayTagDataEntry.cs @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccUInt16ArrayTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs index ab6a44954..7ad9b2c21 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs @@ -61,7 +61,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public bool Equals(IccViewingConditionsTagDataEntry other) { - if (ReferenceEquals(null, other)) + if (other is null) { return false; } From 0b516fc4b9b0c61a735e54badbf3d2e0a604f10a Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 27 Jun 2018 01:10:51 +0200 Subject: [PATCH 18/23] PngEncoderTests.WorksWithBitDepth16 --- .../Formats/Png/PngEncoderTests.cs | 86 ++++++++++++++++--- 1 file changed, 73 insertions(+), 13 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs index eb046165d..a794bc03e 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs @@ -1,22 +1,21 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +// ReSharper disable InconsistentNaming using System.IO; using System.Linq; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Quantization; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using Xunit; -// ReSharper disable InconsistentNaming -namespace SixLabors.ImageSharp.Tests +namespace SixLabors.ImageSharp.Tests.Formats.Png { - using SixLabors.ImageSharp.Processing; - using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; - public class PngEncoderTests { private const float ToleranceThresholdForPaletteEncoder = 0.2f / 100; @@ -70,7 +69,12 @@ namespace SixLabors.ImageSharp.Tests public void WorksWithDifferentSizes(TestImageProvider provider, PngColorType pngColorType) where TPixel : struct, IPixel { - TestPngEncoderCore(provider, pngColorType, PngFilterMethod.Adaptive, appendPngColorType: true); + TestPngEncoderCore( + provider, + pngColorType, + PngFilterMethod.Adaptive, + PngBitDepth.Bit8, + appendPngColorType: true); } [Theory] @@ -78,7 +82,13 @@ namespace SixLabors.ImageSharp.Tests public void IsNotBoundToSinglePixelType(TestImageProvider provider, PngColorType pngColorType) where TPixel : struct, IPixel { - TestPngEncoderCore(provider, pngColorType, PngFilterMethod.Adaptive, appendPixelType: true, appendPngColorType: true); + TestPngEncoderCore( + provider, + pngColorType, + PngFilterMethod.Adaptive, + PngBitDepth.Bit8, + appendPixelType: true, + appendPngColorType: true); } [Theory] @@ -86,7 +96,12 @@ namespace SixLabors.ImageSharp.Tests public void WorksWithAllFilterMethods(TestImageProvider provider, PngFilterMethod pngFilterMethod) where TPixel : struct, IPixel { - TestPngEncoderCore(provider, PngColorType.RgbWithAlpha, pngFilterMethod, appendPngFilterMethod: true); + TestPngEncoderCore( + provider, + PngColorType.RgbWithAlpha, + pngFilterMethod, + PngBitDepth.Bit8, + appendPngFilterMethod: true); } [Theory] @@ -94,7 +109,29 @@ namespace SixLabors.ImageSharp.Tests public void WorksWithAllCompressionLevels(TestImageProvider provider, int compressionLevel) where TPixel : struct, IPixel { - TestPngEncoderCore(provider, PngColorType.RgbWithAlpha, PngFilterMethod.Adaptive, compressionLevel, appendCompressionLevel: true); + TestPngEncoderCore( + provider, + PngColorType.RgbWithAlpha, + PngFilterMethod.Adaptive, + PngBitDepth.Bit8, + compressionLevel, + appendCompressionLevel: true); + } + + [Theory] + [WithTestPatternImages(24, 24, PixelTypes.Rgba64, PngColorType.Rgb)] + [WithTestPatternImages(24, 24, PixelTypes.Rgba64, PngColorType.RgbWithAlpha)] + [WithTestPatternImages(24, 24, PixelTypes.Rgba32, PngColorType.RgbWithAlpha)] + public void WorksWithBitDepth16(TestImageProvider provider, PngColorType pngColorType) + where TPixel : struct, IPixel + { + TestPngEncoderCore( + provider, + pngColorType, + PngFilterMethod.Adaptive, + PngBitDepth.Bit16, + appendPngColorType: true, + appendPixelType: true); } [Theory] @@ -102,7 +139,13 @@ namespace SixLabors.ImageSharp.Tests public void PaletteColorType_WuQuantizer(TestImageProvider provider, int paletteSize) where TPixel : struct, IPixel { - TestPngEncoderCore(provider, PngColorType.Palette, PngFilterMethod.Adaptive, paletteSize: paletteSize, appendPaletteSize: true); + TestPngEncoderCore( + provider, + PngColorType.Palette, + PngFilterMethod.Adaptive, + PngBitDepth.Bit8, + paletteSize: paletteSize, + appendPaletteSize: true); } private static bool HasAlpha(PngColorType pngColorType) => @@ -112,6 +155,7 @@ namespace SixLabors.ImageSharp.Tests TestImageProvider provider, PngColorType pngColorType, PngFilterMethod pngFilterMethod, + PngBitDepth bitDepth, int compressionLevel = 6, int paletteSize = 255, bool appendPngColorType = false, @@ -133,6 +177,7 @@ namespace SixLabors.ImageSharp.Tests ColorType = pngColorType, FilterMethod = pngFilterMethod, CompressionLevel = compressionLevel, + BitDepth = bitDepth, Quantizer = new WuQuantizer(paletteSize) }; @@ -155,16 +200,31 @@ namespace SixLabors.ImageSharp.Tests IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile); string referenceOutputFile = ((ITestImageProvider)provider).Utility.GetReferenceOutputFileName("png", debugInfo, appendPixelType, true); + bool referenceOutputFileExists = File.Exists(referenceOutputFile); + using (var actualImage = Image.Load(actualOutputFile, referenceDecoder)) - using (var referenceImage = Image.Load(referenceOutputFile, referenceDecoder)) { + // TODO: Do we still need the reference output files? + Image referenceImage = referenceOutputFileExists + ? Image.Load(referenceOutputFile, referenceDecoder) + : image; + float paletteToleranceHack = 80f / paletteSize; paletteToleranceHack = paletteToleranceHack * paletteToleranceHack; ImageComparer comparer = pngColorType == PngColorType.Palette ? ImageComparer.Tolerant(ToleranceThresholdForPaletteEncoder * paletteToleranceHack) : ImageComparer.Exact; - - comparer.VerifySimilarity(referenceImage, actualImage); + try + { + comparer.VerifySimilarity(referenceImage, actualImage); + } + finally + { + if (referenceOutputFileExists) + { + referenceImage.Dispose(); + } + } } } } From c53112bc086a03c88364fc8c1afbd7e1ffd19549 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 16:13:42 -0700 Subject: [PATCH 19/23] Remove trailing whitespace --- .../Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs index cad816ab9..85ae2f9fa 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccUInt64ArrayTagDataEntry.cs @@ -61,7 +61,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) - { + { return obj is IccUInt64ArrayTagDataEntry other && this.Equals(other); } From 1449c67f58319d07881a584078d0cf6f98fa5395 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 26 Jun 2018 16:22:09 -0700 Subject: [PATCH 20/23] Optimize remaining equality checks using pattern matching --- src/ImageSharp/ColorSpaces/CieLab.cs | 7 +------ .../TagDataEntries/IccChromaticityTagDataEntry.cs | 2 +- .../ICC/TagDataEntries/IccDateTimeTagDataEntry.cs | 2 +- .../IccResponseCurveSet16TagDataEntry.cs | 12 +----------- .../IccViewingConditionsTagDataEntry.cs | 2 +- .../Profiles/ICC/Various/IccScreeningChannel.cs | 2 +- src/ImageSharp/PixelFormats/Bgr565.cs | 2 +- src/ImageSharp/PixelFormats/HalfSingle.cs | 2 +- src/ImageSharp/PixelFormats/HalfVector2.cs | 2 +- src/ImageSharp/PixelFormats/NormalizedShort2.cs | 2 +- src/ImageSharp/PixelFormats/Rg32.cs | 2 +- tests/ImageSharp.Tests/Memory/BufferTestSuite.cs | 3 +-- 12 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/ImageSharp/ColorSpaces/CieLab.cs b/src/ImageSharp/ColorSpaces/CieLab.cs index ce5c6c118..66900079f 100644 --- a/src/ImageSharp/ColorSpaces/CieLab.cs +++ b/src/ImageSharp/ColorSpaces/CieLab.cs @@ -194,12 +194,7 @@ namespace SixLabors.ImageSharp.ColorSpaces [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) { - if (obj is CieLab) - { - return this.Equals((CieLab)obj); - } - - return false; + return obj is CieLab other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs index b95b5c388..c008463ee 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccChromaticityTagDataEntry.cs @@ -104,7 +104,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - return obj is IccChromaticityTagDataEntry && this.Equals((IccChromaticityTagDataEntry)obj); + return obj is IccChromaticityTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs index 6eeeaee7c..004603a0e 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccDateTimeTagDataEntry.cs @@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - return obj is IccDateTimeTagDataEntry && this.Equals((IccDateTimeTagDataEntry)obj); + return obj is IccDateTimeTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs index 59041d1f7..e2cd5860b 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccResponseCurveSet16TagDataEntry.cs @@ -77,17 +77,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - if (obj is null) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - return obj is IccResponseCurveSet16TagDataEntry && this.Equals((IccResponseCurveSet16TagDataEntry)obj); + return obj is IccResponseCurveSet16TagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs index 7ad9b2c21..6be21dcc9 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccViewingConditionsTagDataEntry.cs @@ -80,7 +80,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - return obj is IccViewingConditionsTagDataEntry && this.Equals((IccViewingConditionsTagDataEntry)obj); + return obj is IccViewingConditionsTagDataEntry other && this.Equals(other); } /// diff --git a/src/ImageSharp/MetaData/Profiles/ICC/Various/IccScreeningChannel.cs b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccScreeningChannel.cs index c038cfaba..79c647bf1 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/Various/IccScreeningChannel.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/Various/IccScreeningChannel.cs @@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// public override bool Equals(object obj) { - return obj is IccScreeningChannel && this.Equals((IccScreeningChannel)obj); + return obj is IccScreeningChannel other && this.Equals(other); } /// diff --git a/src/ImageSharp/PixelFormats/Bgr565.cs b/src/ImageSharp/PixelFormats/Bgr565.cs index 8595c6b9b..f9a0ce9dc 100644 --- a/src/ImageSharp/PixelFormats/Bgr565.cs +++ b/src/ImageSharp/PixelFormats/Bgr565.cs @@ -206,7 +206,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is Bgr565) && this.Equals((Bgr565)obj); + return obj is Bgr565 other && this.Equals(other); } /// diff --git a/src/ImageSharp/PixelFormats/HalfSingle.cs b/src/ImageSharp/PixelFormats/HalfSingle.cs index 504992542..54c615f9b 100644 --- a/src/ImageSharp/PixelFormats/HalfSingle.cs +++ b/src/ImageSharp/PixelFormats/HalfSingle.cs @@ -211,7 +211,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is HalfSingle) && this.Equals((HalfSingle)obj); + return obj is HalfSingle other && this.Equals(other); } /// diff --git a/src/ImageSharp/PixelFormats/HalfVector2.cs b/src/ImageSharp/PixelFormats/HalfVector2.cs index 72eb4f79c..4a135a77c 100644 --- a/src/ImageSharp/PixelFormats/HalfVector2.cs +++ b/src/ImageSharp/PixelFormats/HalfVector2.cs @@ -239,7 +239,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is HalfVector2) && this.Equals((HalfVector2)obj); + return obj is HalfVector2 other && this.Equals(other); } /// diff --git a/src/ImageSharp/PixelFormats/NormalizedShort2.cs b/src/ImageSharp/PixelFormats/NormalizedShort2.cs index 2ddc83e76..1ced412d0 100644 --- a/src/ImageSharp/PixelFormats/NormalizedShort2.cs +++ b/src/ImageSharp/PixelFormats/NormalizedShort2.cs @@ -247,7 +247,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is NormalizedShort2) && this.Equals((NormalizedShort2)obj); + return obj is NormalizedShort2 other && this.Equals(other); } /// diff --git a/src/ImageSharp/PixelFormats/Rg32.cs b/src/ImageSharp/PixelFormats/Rg32.cs index 39a0ff424..e5ceeacec 100644 --- a/src/ImageSharp/PixelFormats/Rg32.cs +++ b/src/ImageSharp/PixelFormats/Rg32.cs @@ -210,7 +210,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is Rg32) && this.Equals((Rg32)obj); + return obj is Rg32 other && this.Equals(other); } /// diff --git a/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs b/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs index 6530850ec..a0a68a705 100644 --- a/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs +++ b/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs @@ -46,8 +46,7 @@ namespace SixLabors.ImageSharp.Tests.Memory public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) return false; - return obj is CustomStruct && this.Equals((CustomStruct)obj); + return obj is CustomStruct other && this.Equals(other); } public override int GetHashCode() From 065a8b831c8c9a55eb09a8d0c3a6839ac1f32f02 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 27 Jun 2018 01:39:59 +0200 Subject: [PATCH 21/23] no BMP assertions on linux --- tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs | 8 ++++++-- .../TestUtilities/Tests/TestImageProviderTests.cs | 1 - 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index d958278f6..b994af056 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -30,7 +30,11 @@ namespace SixLabors.ImageSharp.Tests using (Image image = provider.GetImage(new BmpDecoder())) { image.DebugSave(provider, "bmp"); - image.CompareToOriginal(provider); + + if (TestEnvironment.IsWindows) + { + image.CompareToOriginal(provider); + } } } @@ -52,7 +56,7 @@ namespace SixLabors.ImageSharp.Tests [InlineData(NegHeight, 24)] [InlineData(Bit8, 8)] [InlineData(Bit8Inverted, 8)] - public void DetectPixelSize(string imagePath, int expectedPixelSize) + public void Identify(string imagePath, int expectedPixelSize) { var testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index efc75773e..02acdfa18 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -244,7 +244,6 @@ namespace SixLabors.ImageSharp.Tests public static string[] AllBmpFiles = { TestImages.Bmp.F, - TestImages.Bmp.CoreHeader, TestImages.Bmp.Bit8 }; From 416bae672b22347345c3588b540ce34f773e368f Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 27 Jun 2018 01:48:29 +0200 Subject: [PATCH 22/23] do not register SystemDrawing reference encoders on linux --- .../TestUtilities/TestEnvironment.Formats.cs | 7 +++++-- .../TestUtilities/Tests/TestEnvironmentTests.cs | 1 - 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs index ccda71613..90c999f7c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs @@ -58,16 +58,19 @@ namespace SixLabors.ImageSharp.Tests ); // Magick codecs should work on all platforms + IImageEncoder pngEncoder = IsWindows ? (IImageEncoder)SystemDrawingReferenceEncoder.Png : new PngEncoder(); + IImageEncoder bmpEncoder = IsWindows ? (IImageEncoder)SystemDrawingReferenceEncoder.Bmp : new BmpEncoder(); + cfg.ConfigureCodecs( ImageFormats.Png, MagickReferenceDecoder.Instance, - SystemDrawingReferenceEncoder.Png, + pngEncoder, new PngImageFormatDetector()); cfg.ConfigureCodecs( ImageFormats.Bmp, SystemDrawingReferenceDecoder.Instance, - SystemDrawingReferenceEncoder.Bmp, + bmpEncoder, new BmpImageFormatDetector()); return cfg; diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs index 2c824729c..8a3e69059 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs @@ -18,7 +18,6 @@ using Xunit.Abstractions; namespace SixLabors.ImageSharp.Tests { - public class TestEnvironmentTests { public TestEnvironmentTests(ITestOutputHelper output) From 2d76d3d05a1092a33e4a73c20c1ae8bff46a7c70 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 28 Jun 2018 00:58:23 +0200 Subject: [PATCH 23/23] Fix Sandbox46 --- tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj index 245af5289..80cf162c5 100644 --- a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj +++ b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj @@ -19,6 +19,7 @@ +