From 45a6a6bde8c90d67d476b5ffb060100fd0ec80be Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 26 Oct 2019 20:18:19 +0200 Subject: [PATCH] Using tolerant comparer for 16 and 8 bit --- .../Formats/Tga/TgaDecoderTests.cs | 10 ++++++---- .../Formats/Tga/TgaEncoderTests.cs | 18 ++++++++++++------ .../Formats/Tga/TgaTestUtils.cs | 14 ++++++++++++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs index 68a3fbe28a..03ad10de40 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +// ReSharper disable InconsistentNaming + using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.PixelFormats; @@ -122,7 +124,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga [Theory] [WithFile(GreyRle, PixelTypes.Rgba32)] - public void TgaDecoder_CanDecode_RunLenghtEncoded_MonoChrome(TestImageProvider provider) + public void TgaDecoder_CanDecode_RunLengthEncoded_MonoChrome(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage(new TgaDecoder())) @@ -134,7 +136,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga [Theory] [WithFile(Bit16Rle, PixelTypes.Rgba32)] - public void TgaDecoder_CanDecode_RunLenghtEncoded_16Bit(TestImageProvider provider) + public void TgaDecoder_CanDecode_RunLengthEncoded_16Bit(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage(new TgaDecoder())) @@ -146,7 +148,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga [Theory] [WithFile(Bit24Rle, PixelTypes.Rgba32)] - public void TgaDecoder_CanDecode_RunLenghtEncoded_24Bit(TestImageProvider provider) + public void TgaDecoder_CanDecode_RunLengthEncoded_24Bit(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage(new TgaDecoder())) @@ -158,7 +160,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga [Theory] [WithFile(Bit32Rle, PixelTypes.Rgba32)] - public void TgaDecoder_CanDecode_RunLenghtEncoded_32Bit(TestImageProvider provider) + public void TgaDecoder_CanDecode_RunLengthEncoded_32Bit(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage(new TgaDecoder())) diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs index 4e1cea226d..5dd49f4faa 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +// ReSharper disable InconsistentNaming + using System.IO; using SixLabors.ImageSharp.Formats.Tga; @@ -80,12 +82,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga [Theory] [WithFile(Bit32, PixelTypes.Rgba32)] public void Encode_Bit8_Works(TestImageProvider provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel8) - where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel); + // using tolerant comparer here. The results from magick differ slightly. Maybe a different ToGrey method is used. The image looks otherwise ok. + where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: true, useExactComparer: false, compareTolerance: 0.03f); [Theory] [WithFile(Bit32, PixelTypes.Rgba32)] public void Encode_Bit16_Works(TestImageProvider provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel16) - where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel); + where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: false, useExactComparer: false); [Theory] [WithFile(Bit32, PixelTypes.Rgba32)] @@ -100,12 +103,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga [Theory] [WithFile(Bit32, PixelTypes.Rgba32)] public void Encode_Bit8_WithRunLengthEncoding_Works(TestImageProvider provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel8) - where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel, true); + // using tolerant comparer here. The results from magick differ slightly. Maybe a different ToGrey method is used. The image looks otherwise ok. + where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: true, useExactComparer: false, compareTolerance: 0.03f); [Theory] [WithFile(Bit32, PixelTypes.Rgba32)] public void Encode_Bit16_WithRunLengthEncoding_Works(TestImageProvider provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel16) - where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel, true); + where TPixel : struct, IPixel => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: true, useExactComparer: false); [Theory] [WithFile(Bit32, PixelTypes.Rgba32)] @@ -120,7 +124,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga private static void TestTgaEncoderCore( TestImageProvider provider, TgaBitsPerPixel bitsPerPixel, - bool useCompression = false) + bool useCompression = false, + bool useExactComparer = true, + float compareTolerance = 0.01f) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) @@ -133,7 +139,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga memStream.Position = 0; using (var encodedImage = (Image)Image.Load(memStream)) { - TgaTestUtils.CompareWithReferenceDecoder(provider, encodedImage); + TgaTestUtils.CompareWithReferenceDecoder(provider, encodedImage, useExactComparer, compareTolerance); } } } diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs index f127322fda..a2f2e86d7d 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs @@ -11,7 +11,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga { public static class TgaTestUtils { - public static void CompareWithReferenceDecoder(TestImageProvider provider, Image image) + public static void CompareWithReferenceDecoder(TestImageProvider provider, + Image image, + bool useExactComparer = true, + float compareTolerance = 0.01f) where TPixel : struct, IPixel { string path = TestImageProvider.GetFilePathOrNull(provider); @@ -22,7 +25,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga TestFile testFile = TestFile.Create(path); Image magickImage = DecodeWithMagick(Configuration.Default, new FileInfo(testFile.FullPath)); - ImageComparer.Exact.VerifySimilarity(image, magickImage); + if (useExactComparer) + { + ImageComparer.Exact.VerifySimilarity(magickImage, image); + } + else + { + ImageComparer.Tolerant(compareTolerance).VerifySimilarity(magickImage, image); + } } public static Image DecodeWithMagick(Configuration configuration, FileInfo fileInfo)