Browse Source

Using tolerant comparer for 16 and 8 bit

pull/1026/head
Brian Popow 7 years ago
parent
commit
45a6a6bde8
  1. 10
      tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs
  2. 18
      tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs
  3. 14
      tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs

10
tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// ReSharper disable InconsistentNaming
using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.Formats.Tga;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -122,7 +124,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
[Theory] [Theory]
[WithFile(GreyRle, PixelTypes.Rgba32)] [WithFile(GreyRle, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_RunLenghtEncoded_MonoChrome<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_MonoChrome<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))
@ -134,7 +136,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
[Theory] [Theory]
[WithFile(Bit16Rle, PixelTypes.Rgba32)] [WithFile(Bit16Rle, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_RunLenghtEncoded_16Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))
@ -146,7 +148,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
[Theory] [Theory]
[WithFile(Bit24Rle, PixelTypes.Rgba32)] [WithFile(Bit24Rle, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_RunLenghtEncoded_24Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))
@ -158,7 +160,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
[Theory] [Theory]
[WithFile(Bit32Rle, PixelTypes.Rgba32)] [WithFile(Bit32Rle, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_RunLenghtEncoded_32Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_32Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using (Image<TPixel> image = provider.GetImage(new TgaDecoder()))

18
tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// ReSharper disable InconsistentNaming
using System.IO; using System.IO;
using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.Formats.Tga;
@ -80,12 +82,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
[Theory] [Theory]
[WithFile(Bit32, PixelTypes.Rgba32)] [WithFile(Bit32, PixelTypes.Rgba32)]
public void Encode_Bit8_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel8) public void Encode_Bit8_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel8)
where TPixel : struct, IPixel<TPixel> => 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<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: true, useExactComparer: false, compareTolerance: 0.03f);
[Theory] [Theory]
[WithFile(Bit32, PixelTypes.Rgba32)] [WithFile(Bit32, PixelTypes.Rgba32)]
public void Encode_Bit16_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel16) public void Encode_Bit16_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel16)
where TPixel : struct, IPixel<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel); where TPixel : struct, IPixel<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: false, useExactComparer: false);
[Theory] [Theory]
[WithFile(Bit32, PixelTypes.Rgba32)] [WithFile(Bit32, PixelTypes.Rgba32)]
@ -100,12 +103,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
[Theory] [Theory]
[WithFile(Bit32, PixelTypes.Rgba32)] [WithFile(Bit32, PixelTypes.Rgba32)]
public void Encode_Bit8_WithRunLengthEncoding_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel8) public void Encode_Bit8_WithRunLengthEncoding_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel8)
where TPixel : struct, IPixel<TPixel> => 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<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: true, useExactComparer: false, compareTolerance: 0.03f);
[Theory] [Theory]
[WithFile(Bit32, PixelTypes.Rgba32)] [WithFile(Bit32, PixelTypes.Rgba32)]
public void Encode_Bit16_WithRunLengthEncoding_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel16) public void Encode_Bit16_WithRunLengthEncoding_Works<TPixel>(TestImageProvider<TPixel> provider, TgaBitsPerPixel bitsPerPixel = TgaBitsPerPixel.Pixel16)
where TPixel : struct, IPixel<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel, true); where TPixel : struct, IPixel<TPixel> => TestTgaEncoderCore(provider, bitsPerPixel, useCompression: true, useExactComparer: false);
[Theory] [Theory]
[WithFile(Bit32, PixelTypes.Rgba32)] [WithFile(Bit32, PixelTypes.Rgba32)]
@ -120,7 +124,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
private static void TestTgaEncoderCore<TPixel>( private static void TestTgaEncoderCore<TPixel>(
TestImageProvider<TPixel> provider, TestImageProvider<TPixel> provider,
TgaBitsPerPixel bitsPerPixel, TgaBitsPerPixel bitsPerPixel,
bool useCompression = false) bool useCompression = false,
bool useExactComparer = true,
float compareTolerance = 0.01f)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
@ -133,7 +139,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
memStream.Position = 0; memStream.Position = 0;
using (var encodedImage = (Image<TPixel>)Image.Load(memStream)) using (var encodedImage = (Image<TPixel>)Image.Load(memStream))
{ {
TgaTestUtils.CompareWithReferenceDecoder(provider, encodedImage); TgaTestUtils.CompareWithReferenceDecoder(provider, encodedImage, useExactComparer, compareTolerance);
} }
} }
} }

14
tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs

@ -11,7 +11,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
{ {
public static class TgaTestUtils public static class TgaTestUtils
{ {
public static void CompareWithReferenceDecoder<TPixel>(TestImageProvider<TPixel> provider, Image<TPixel> image) public static void CompareWithReferenceDecoder<TPixel>(TestImageProvider<TPixel> provider,
Image<TPixel> image,
bool useExactComparer = true,
float compareTolerance = 0.01f)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
string path = TestImageProvider<TPixel>.GetFilePathOrNull(provider); string path = TestImageProvider<TPixel>.GetFilePathOrNull(provider);
@ -22,7 +25,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
TestFile testFile = TestFile.Create(path); TestFile testFile = TestFile.Create(path);
Image<Rgba32> magickImage = DecodeWithMagick<Rgba32>(Configuration.Default, new FileInfo(testFile.FullPath)); Image<Rgba32> magickImage = DecodeWithMagick<Rgba32>(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<TPixel> DecodeWithMagick<TPixel>(Configuration configuration, FileInfo fileInfo) public static Image<TPixel> DecodeWithMagick<TPixel>(Configuration configuration, FileInfo fileInfo)

Loading…
Cancel
Save