Browse Source

Merge remote-tracking branch 'origin/antonfirsov/qa-lab' into jpeg-lab

# Conflicts:
#	tests/Images/External
pull/298/head
Anton Firszov 9 years ago
parent
commit
f6904d94b1
  1. 2
      src/ImageSharp/Configuration.cs
  2. 2
      src/ImageSharp/Image/ImageExtensions.cs
  3. 4
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  4. 12
      tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs
  5. 12
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  6. 8
      tests/ImageSharp.Tests/Image/PixelAccessorTests.cs
  7. 3
      tests/ImageSharp.Tests/ImageSharp.Tests.csproj
  8. 14
      tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs
  9. 1
      tests/ImageSharp.Tests/TestImages.cs
  10. 3
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs
  11. 36
      tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs
  12. 24
      tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs
  13. 2
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs
  14. 4
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs
  15. 2
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs
  16. 13
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs
  17. 8
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs
  18. 12
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  19. 42
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs
  20. 4
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs
  21. 6
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs
  22. 58
      tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs
  23. 16
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
  24. 7
      tests/ImageSharp.Tests/TestUtilities/TestUtils.cs
  25. 4
      tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs
  26. 24
      tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs
  27. 4
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs
  28. 10
      tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs
  29. 2
      tests/Images/External
  30. BIN
      tests/Images/Input/Png/SnakeGame.png

2
src/ImageSharp/Configuration.cs

@ -140,7 +140,7 @@ namespace ImageSharp
/// </summary> /// </summary>
/// <param name="extension">The extension to discover</param> /// <param name="extension">The extension to discover</param>
/// <returns>The <see cref="IImageFormat"/> if found otherwise null</returns> /// <returns>The <see cref="IImageFormat"/> if found otherwise null</returns>
public IImageFormat FindFormatByFileExtensions(string extension) public IImageFormat FindFormatByFileExtension(string extension)
{ {
return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)); return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));
} }

2
src/ImageSharp/Image/ImageExtensions.cs

@ -52,7 +52,7 @@ namespace ImageSharp
Guard.NotNullOrEmpty(filePath, nameof(filePath)); Guard.NotNullOrEmpty(filePath, nameof(filePath));
string ext = Path.GetExtension(filePath).Trim('.'); string ext = Path.GetExtension(filePath).Trim('.');
IImageFormat format = source.Configuration.FindFormatByFileExtensions(ext); IImageFormat format = source.Configuration.FindFormatByFileExtension(ext);
if (format == null) if (format == null)
{ {
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();

4
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -139,8 +139,8 @@ namespace ImageSharp.Tests
image.Save(ms, encoder); image.Save(ms, encoder);
} }
} }
Image<TPixel> mirror = provider.Factory.CreateImage(data); Image<TPixel> mirror = Image.Load<TPixel>(data);
mirror.DebugSave(provider, $"_{subsample}_Q{quality}"); mirror.DebugSave(provider, $"_{subsample}_Q{quality}");
} }

12
tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs

@ -16,19 +16,19 @@ namespace ImageSharp.Tests
public class JpegUtilsTests : TestBase public class JpegUtilsTests : TestBase
{ {
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory) public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Image<TPixel> image = factory.CreateImage(10, 10); var image = new Image<TPixel>(10, 10);
using (PixelAccessor<TPixel> pixels = image.Lock()) using (PixelAccessor<TPixel> pixels = image.Lock())
{ {
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
for (int j = 0; j < 10; j++) for (int j = 0; j < 10; j++)
{ {
Vector4 v = new Vector4(i / 10f, j / 10f, 0, 1); var v = new Vector4(i / 10f, j / 10f, 0, 1);
TPixel color = default(TPixel); var color = default(TPixel);
color.PackFromVector4(v); color.PackFromVector4(v);
pixels[i, j] = color; pixels[i, j] = color;
@ -45,7 +45,7 @@ namespace ImageSharp.Tests
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> src = provider.GetImage()) using (Image<TPixel> src = provider.GetImage())
using (Image<TPixel> dest = provider.Factory.CreateImage(8, 8)) using (Image<TPixel> dest = new Image<TPixel>(8,8))
using (PixelArea<TPixel> area = new PixelArea<TPixel>(8, 8, ComponentOrder.Xyz)) using (PixelArea<TPixel> area = new PixelArea<TPixel>(8, 8, ComponentOrder.Xyz))
using (PixelAccessor<TPixel> s = src.Lock()) using (PixelAccessor<TPixel> s = src.Lock())
using (PixelAccessor<TPixel> d = dest.Lock()) using (PixelAccessor<TPixel> d = dest.Lock())
@ -68,7 +68,7 @@ namespace ImageSharp.Tests
{ {
using (Image<TPixel> src = provider.GetImage()) using (Image<TPixel> src = provider.GetImage())
using (PixelArea<TPixel> area = new PixelArea<TPixel>(8, 8, ComponentOrder.Xyz)) using (PixelArea<TPixel> area = new PixelArea<TPixel>(8, 8, ComponentOrder.Xyz))
using (Image<TPixel> dest = provider.Factory.CreateImage(8, 8)) using (Image<TPixel> dest = new Image<TPixel>(8, 8))
using (PixelAccessor<TPixel> s = src.Lock()) using (PixelAccessor<TPixel> s = src.Lock())
using (PixelAccessor<TPixel> d = dest.Lock()) using (PixelAccessor<TPixel> d = dest.Lock())
{ {

12
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

@ -12,6 +12,8 @@ using Xunit;
namespace ImageSharp.Tests namespace ImageSharp.Tests
{ {
using ImageSharp.Tests.TestUtilities.ImageComparison;
public class PngDecoderTests public class PngDecoderTests
{ {
private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32;
@ -19,17 +21,19 @@ namespace ImageSharp.Tests
public static readonly string[] TestFiles = public static readonly string[] TestFiles =
{ {
TestImages.Png.Splash, TestImages.Png.Indexed, TestImages.Png.Interlaced, TestImages.Png.FilterVar, TestImages.Png.Splash, TestImages.Png.Indexed, TestImages.Png.Interlaced, TestImages.Png.FilterVar,
TestImages.Png.Bad.ChunkLength1, TestImages.Png.Bad.ChunkLength2, TestImages.Png.Rgb48Bpp, TestImages.Png.Rgb48BppInterlaced TestImages.Png.Bad.ChunkLength1, TestImages.Png.Bad.ChunkLength2, TestImages.Png.Rgb48Bpp,
TestImages.Png.Rgb48BppInterlaced, TestImages.Png.SnakeGame
}; };
[Theory] [Theory]
[WithFileCollection(nameof(TestFiles), PixelTypes)] [WithFileCollection(nameof(TestFiles), PixelTypes)]
public void Decode<TPixel>(TestImageProvider<TPixel> imageProvider) public void Decode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = imageProvider.GetImage()) using (Image<TPixel> image = provider.GetImage(new PngDecoder()))
{ {
image.DebugSave(imageProvider); image.DebugSave(provider);
image.CompareToOriginal(provider, ImageComparer.Exact);
} }
} }

8
tests/ImageSharp.Tests/Image/PixelAccessorTests.cs

@ -17,20 +17,20 @@ namespace ImageSharp.Tests
/// </summary> /// </summary>
public class PixelAccessorTests public class PixelAccessorTests
{ {
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory) public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Image<TPixel> image = factory.CreateImage(10, 10); var image = new Image<TPixel>(10, 10);
using (PixelAccessor<TPixel> pixels = image.Lock()) using (PixelAccessor<TPixel> pixels = image.Lock())
{ {
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
for (int j = 0; j < 10; j++) for (int j = 0; j < 10; j++)
{ {
Vector4 v = new Vector4(i, j, 0, 1); var v = new Vector4(i, j, 0, 1);
v /= 10; v /= 10;
TPixel color = default(TPixel); var color = default(TPixel);
color.PackFromVector4(v); color.PackFromVector4(v);
pixels[i, j] = color; pixels[i, j] = color;

3
tests/ImageSharp.Tests/ImageSharp.Tests.csproj

@ -28,4 +28,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="TestUtilities\Factories\" />
</ItemGroup>
</Project> </Project>

14
tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs

@ -40,7 +40,8 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
image.Mutate(x => x.DetectEdges(detector)); image.Mutate(x => x.DetectEdges(detector));
image.DebugSave(provider, detector.ToString(), grayscale: true); image.DebugSave(provider, detector.ToString());
image.CompareToReferenceOutput(provider, detector.ToString());
} }
} }
@ -52,7 +53,8 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
image.Mutate(x => x.DetectEdges()); image.Mutate(x => x.DetectEdges());
image.DebugSave(provider, grayscale: true); image.DebugSave(provider);
image.CompareToReferenceOutput(provider);
} }
} }
@ -73,16 +75,16 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider) public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> source = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
using (Image<TPixel> image = source.Clone())
{ {
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.Mutate(x => x.DetectEdges(bounds)); image.Mutate(x => x.DetectEdges(bounds));
image.DebugSave(provider, grayscale: true); image.DebugSave(provider);
image.CompareToReferenceOutput(provider);
// TODO: We don't need this any longer after switching to ReferenceImages // TODO: We don't need this any longer after switching to ReferenceImages
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds); //ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
} }
} }
} }

1
tests/ImageSharp.Tests/TestImages.cs

@ -31,6 +31,7 @@ namespace ImageSharp.Tests
public const string Bike = "Png/Bike.png"; public const string Bike = "Png/Bike.png";
public const string BikeGrayscale = "Png/BikeGrayscale.png"; public const string BikeGrayscale = "Png/BikeGrayscale.png";
public const string Rgb48BppInterlaced = "Png/rgb-48bpp-interlaced.png"; public const string Rgb48BppInterlaced = "Png/rgb-48bpp-interlaced.png";
public const string SnakeGame = "Png/SnakeGame.png";
// Filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html // Filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html
public const string Filter0 = "Png/filter0.png"; public const string Filter0 = "Png/filter0.png";

3
tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs

@ -39,9 +39,8 @@ namespace ImageSharp.Tests
Type colorType = args.Single(); Type colorType = args.Single();
Type imgType = typeof(Image<>).MakeGenericType(colorType); Type imgType = typeof(Image<>).MakeGenericType(colorType);
Type genericFactoryType = (typeof(GenericFactory<>)).MakeGenericType(colorType);
Type funcType = typeof(Func<,>).MakeGenericType(genericFactoryType, imgType); Type funcType = typeof(Func<>).MakeGenericType(imgType);
MethodInfo genericMethod = m.MakeGenericMethod(args); MethodInfo genericMethod = m.MakeGenericMethod(args);

36
tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs

@ -1,36 +0,0 @@
// <copyright file="GenericFactory.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using ImageSharp.PixelFormats;
/// <summary>
/// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here!
///
/// Utility class to create specialized subclasses of generic classes (eg. <see cref="Image"/>)
/// Used as parameter for <see cref="WithMemberFactoryAttribute"/> -based factory methods
/// </summary>
public class GenericFactory<TPixel>
where TPixel : struct, IPixel<TPixel>
{
public virtual Image<TPixel> CreateImage(int width, int height)
{
return new Image<TPixel>(width, height);
}
public virtual Image<TPixel> CreateImage(byte[] bytes)
{
return Image.Load<TPixel>(bytes);
}
public virtual Image<TPixel> CreateImage(Image<TPixel> other)
{
return other.Clone();
}
}
}

24
tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs

@ -1,24 +0,0 @@
// <copyright file="ImageFactory.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using ImageSharp.PixelFormats;
/// <summary>
/// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here!
/// </summary>
public class ImageFactory : GenericFactory<Rgba32>
{
public override Image<Rgba32> CreateImage(byte[] bytes) => Image.Load<Rgba32>(bytes);
public override Image<Rgba32> CreateImage(int width, int height) => new Image<Rgba32>(width, height);
public override Image<Rgba32> CreateImage(Image<Rgba32> other)
{
return other.Clone();
}
}
}

2
tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs

@ -10,7 +10,7 @@ namespace ImageSharp.Tests.TestUtilities.ImageComparison
public abstract class ImageComparer public abstract class ImageComparer
{ {
public static ImageComparer Exact { get; } = ExactImageComparer.Instance; public static ImageComparer Exact { get; } = Tolerant(0, 0);
public static ImageComparer Tolerant( public static ImageComparer Tolerant(
float imageThreshold = TolerantImageComparer.DefaultImageThreshold, float imageThreshold = TolerantImageComparer.DefaultImageThreshold,

4
tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs

@ -66,7 +66,7 @@
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
int d = GetDifferenceInPixelByteSum(ref aBuffer[x], ref bBuffer[x]); int d = GetHammingDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]);
if (d > this.PixelThresholdInPixelByteSum) if (d > this.PixelThresholdInPixelByteSum)
{ {
@ -92,7 +92,7 @@
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetDifferenceInPixelByteSum(ref Rgba32 a, ref Rgba32 b) private static int GetHammingDistanceInRgbaSpace(ref Rgba32 a, ref Rgba32 b)
{ {
return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A);
} }

2
tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs

@ -33,7 +33,7 @@ namespace ImageSharp.Tests
protected int Width { get; private set; } protected int Width { get; private set; }
public override Image<TPixel> GetImage() => this.Factory.CreateImage(this.Width, this.Height); public override Image<TPixel> GetImage() => new Image<TPixel>(this.Width, this.Height);
public override void Deserialize(IXunitSerializationInfo info) public override void Deserialize(IXunitSerializationInfo info)

13
tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs

@ -45,17 +45,8 @@ namespace ImageSharp.Tests
public override Image<TPixel> GetImage() public override Image<TPixel> GetImage()
{ {
Key key = new Key(this.PixelType, this.FilePath); IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(this.FilePath);
return this.GetImage(decoder);
Image<TPixel> cachedImage = cache.GetOrAdd(
key,
fn =>
{
TestFile testFile = TestFile.Create(this.FilePath);
return Image.Load<TPixel>(testFile.Bytes);
});
return cachedImage.Clone();
} }
public override Image<TPixel> GetImage(IImageDecoder decoder) public override Image<TPixel> GetImage(IImageDecoder decoder)

8
tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs

@ -18,14 +18,14 @@ namespace ImageSharp.Tests
{ {
private class LambdaProvider : TestImageProvider<TPixel> private class LambdaProvider : TestImageProvider<TPixel>
{ {
private readonly Func<GenericFactory<TPixel>, Image<TPixel>> creator; private readonly Func<Image<TPixel>> factoryFunc;
public LambdaProvider(Func<GenericFactory<TPixel>, Image<TPixel>> creator) public LambdaProvider(Func<Image<TPixel>> factoryFunc)
{ {
this.creator = creator; this.factoryFunc = factoryFunc;
} }
public override Image<TPixel> GetImage() => this.creator(this.Factory); public override Image<TPixel> GetImage() => this.factoryFunc();
} }
} }
} }

12
tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs

@ -33,7 +33,6 @@ namespace ImageSharp.Tests
/// </summary> /// </summary>
public ImagingTestCaseUtility Utility { get; private set; } public ImagingTestCaseUtility Utility { get; private set; }
public GenericFactory<TPixel> Factory { get; private set; } = new GenericFactory<TPixel>();
public string TypeName { get; private set; } public string TypeName { get; private set; }
public string MethodName { get; private set; } public string MethodName { get; private set; }
@ -60,10 +59,10 @@ namespace ImageSharp.Tests
} }
public static TestImageProvider<TPixel> Lambda( public static TestImageProvider<TPixel> Lambda(
Func<GenericFactory<TPixel>, Image<TPixel>> func, Func<Image<TPixel>> factoryFunc,
MethodInfo testMethod = null, MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined) PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new LambdaProvider(func).Init(testMethod, pixelTypeOverride); => new LambdaProvider(factoryFunc).Init(testMethod, pixelTypeOverride);
public static TestImageProvider<TPixel> Solid( public static TestImageProvider<TPixel> Solid(
int width, int width,
@ -122,12 +121,7 @@ namespace ImageSharp.Tests
} }
this.TypeName = typeName; this.TypeName = typeName;
this.MethodName = methodName; this.MethodName = methodName;
if (pixelTypeOverride == PixelTypes.Rgba32)
{
this.Factory = new ImageFactory() as GenericFactory<TPixel>;
}
this.Utility = new ImagingTestCaseUtility this.Utility = new ImagingTestCaseUtility
{ {
SourceFileOrDescription = this.SourceFileOrDescription, SourceFileOrDescription = this.SourceFileOrDescription,

42
tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

@ -138,13 +138,11 @@ namespace ImageSharp.Tests
string extension = null, string extension = null,
IImageEncoder encoder = null, IImageEncoder encoder = null,
object testOutputDetails = null, object testOutputDetails = null,
bool grayscale = false,
bool appendPixelTypeToFileName = true) bool appendPixelTypeToFileName = true)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
string path = this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); string path = this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName);
string extension1 = Path.GetExtension(path); encoder = encoder ?? TestEnvironment.GetReferenceEncoder(path);
encoder = encoder ?? GetImageFormatByExtension(extension1, grayscale);
using (FileStream stream = File.OpenWrite(path)) using (FileStream stream = File.OpenWrite(path))
{ {
@ -173,26 +171,26 @@ namespace ImageSharp.Tests
this.Init(method.DeclaringType.Name, method.Name); this.Init(method.DeclaringType.Name, method.Name);
} }
private static IImageEncoder GetImageFormatByExtension(string extension, bool grayscale) //private static IImageEncoder GetEncoderByExtension(string extension, bool grayscale)
{ //{
extension = extension?.TrimStart('.'); // extension = extension?.TrimStart('.');
var format = Configuration.Default.FindFormatByFileExtensions(extension); // var format = Configuration.Default.FindFormatByFileExtension(extension);
IImageEncoder encoder = Configuration.Default.FindEncoder(format); // IImageEncoder encoder = Configuration.Default.FindEncoder(format);
PngEncoder pngEncoder = encoder as PngEncoder; // PngEncoder pngEncoder = encoder as PngEncoder;
if (pngEncoder != null) // if (pngEncoder != null)
{ // {
pngEncoder = new PngEncoder(); // pngEncoder = new PngEncoder();
encoder = pngEncoder; // encoder = pngEncoder;
pngEncoder.CompressionLevel = 9; // pngEncoder.CompressionLevel = 9;
if (grayscale) // if (grayscale)
{ // {
pngEncoder.PngColorType = PngColorType.Grayscale; // pngEncoder.PngColorType = PngColorType.Grayscale;
} // }
} // }
return encoder; // return encoder;
} //}
private string GetTestOutputDir() private string GetTestOutputDir()
{ {

4
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceDecoder.cs → tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs

@ -8,9 +8,9 @@ namespace ImageSharp.Tests.TestUtilities.ReferenceCodecs
using ImageSharp.Formats; using ImageSharp.Formats;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
public class ReferenceDecoder : IImageDecoder public class SystemDrawingReferenceDecoder : IImageDecoder
{ {
public static ReferenceDecoder Instance { get; } = new ReferenceDecoder(); public static SystemDrawingReferenceDecoder Instance { get; } = new SystemDrawingReferenceDecoder();
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream) public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>

6
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/ReferenceEncoder.cs → tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs

@ -9,16 +9,16 @@ namespace ImageSharp.Tests.TestUtilities.ReferenceCodecs
using ImageSharp.Formats; using ImageSharp.Formats;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
public class ReferenceEncoder : IImageEncoder public class SystemDrawingReferenceEncoder : IImageEncoder
{ {
private readonly System.Drawing.Imaging.ImageFormat imageFormat; private readonly System.Drawing.Imaging.ImageFormat imageFormat;
public ReferenceEncoder(ImageFormat imageFormat) public SystemDrawingReferenceEncoder(ImageFormat imageFormat)
{ {
this.imageFormat = imageFormat; this.imageFormat = imageFormat;
} }
public static ReferenceEncoder Png { get; } = new ReferenceEncoder(System.Drawing.Imaging.ImageFormat.Png); public static SystemDrawingReferenceEncoder Png { get; } = new SystemDrawingReferenceEncoder(System.Drawing.Imaging.ImageFormat.Png);
public void Encode<TPixel>(Image<TPixel> image, Stream stream) public void Encode<TPixel>(Image<TPixel> image, Stream stream)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>

58
tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs

@ -1,12 +1,13 @@
namespace ImageSharp.Tests namespace ImageSharp.Tests
{ {
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Security;
using ImageSharp.Formats;
using ImageSharp.Tests.TestUtilities.ReferenceCodecs;
public static class TestEnvironment public static class TestEnvironment
{ {
private const string ImageSharpSolutionFileName = "ImageSharp.sln"; private const string ImageSharpSolutionFileName = "ImageSharp.sln";
@ -26,6 +27,8 @@ namespace ImageSharp.Tests
return bool.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi; return bool.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi;
}); });
private static Lazy<Configuration> configuration = new Lazy<Configuration>(CreateDefaultConfiguration);
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
/// <summary> /// <summary>
/// Gets a value indicating whether test execution runs on CI. /// Gets a value indicating whether test execution runs on CI.
@ -34,6 +37,24 @@ namespace ImageSharp.Tests
internal static string SolutionDirectoryFullPath => solutionDirectoryFullPath.Value; internal static string SolutionDirectoryFullPath => solutionDirectoryFullPath.Value;
internal static Configuration Configuration => configuration.Value;
private static Configuration CreateDefaultConfiguration()
{
var configuration = new Configuration(
new PngConfigurationModule(),
new JpegConfigurationModule(),
new GifConfigurationModule(),
new BmpConfigurationModule()
);
configuration.SetDecoder(ImageFormats.Png, SystemDrawingReferenceDecoder.Instance);
configuration.SetEncoder(ImageFormats.Png, SystemDrawingReferenceEncoder.Png);
configuration.AddImageFormatDetector(new PngImageFormatDetector());
return configuration;
}
private static string GetSolutionDirectoryFullPathImpl() private static string GetSolutionDirectoryFullPathImpl()
{ {
string assemblyLocation = typeof(TestFile).GetTypeInfo().Assembly.Location; string assemblyLocation = typeof(TestFile).GetTypeInfo().Assembly.Location;
@ -62,23 +83,48 @@ namespace ImageSharp.Tests
return directory.FullName; return directory.FullName;
} }
/// <summary> /// <summary>
/// Gets the correct full path to the Input Images directory. /// Gets the correct full path to the Input Images directory.
/// </summary> /// </summary>
internal static string InputImagesDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, InputImagesRelativePath); internal static string InputImagesDirectoryFullPath =>
Path.Combine(SolutionDirectoryFullPath, InputImagesRelativePath);
/// <summary> /// <summary>
/// Gets the correct full path to the Actual Output directory. (To be written to by the test cases.) /// Gets the correct full path to the Actual Output directory. (To be written to by the test cases.)
/// </summary> /// </summary>
internal static string ActualOutputDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, ActualOutputDirectoryRelativePath); internal static string ActualOutputDirectoryFullPath => Path.Combine(
SolutionDirectoryFullPath,
ActualOutputDirectoryRelativePath);
/// <summary> /// <summary>
/// Gets the correct full path to the Expected Output directory. (To compare the test results to.) /// Gets the correct full path to the Expected Output directory. (To compare the test results to.)
/// </summary> /// </summary>
internal static string ReferenceOutputDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, ReferenceOutputDirectoryRelativePath); internal static string ReferenceOutputDirectoryFullPath => Path.Combine(
SolutionDirectoryFullPath,
ReferenceOutputDirectoryRelativePath);
internal static string GetReferenceOutputFileName(string actualOutputFileName) => internal static string GetReferenceOutputFileName(string actualOutputFileName) =>
actualOutputFileName.Replace("ActualOutput", @"External\ReferenceOutput"); actualOutputFileName.Replace("ActualOutput", @"External\ReferenceOutput");
internal static IImageDecoder GetReferenceDecoder(string filePath)
{
IImageFormat format = GetImageFormat(filePath);
return Configuration.FindDecoder(format);
}
internal static IImageEncoder GetReferenceEncoder(string filePath)
{
IImageFormat format = GetImageFormat(filePath);
return Configuration.FindEncoder(format);
}
internal static IImageFormat GetImageFormat(string filePath)
{
string extension = Path.GetExtension(filePath).ToLower();
if (extension[0] == '.') extension = extension.Substring(1);
IImageFormat format = Configuration.FindFormatByFileExtension(extension);
return format;
}
} }
} }

16
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -8,7 +8,9 @@ namespace ImageSharp.Tests
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using ImageSharp.Formats;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
using ImageSharp.Tests.TestUtilities.ImageComparison; using ImageSharp.Tests.TestUtilities.ImageComparison;
using ImageSharp.Tests.TestUtilities.ReferenceCodecs; using ImageSharp.Tests.TestUtilities.ReferenceCodecs;
@ -30,7 +32,6 @@ namespace ImageSharp.Tests
ITestImageProvider provider, ITestImageProvider provider,
object testOutputDetails = null, object testOutputDetails = null,
string extension = "png", string extension = "png",
bool grayscale = false,
bool appendPixelTypeToFileName = true) bool appendPixelTypeToFileName = true)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
@ -44,7 +45,6 @@ namespace ImageSharp.Tests
image, image,
extension, extension,
testOutputDetails: testOutputDetails, testOutputDetails: testOutputDetails,
grayscale: grayscale,
appendPixelTypeToFileName: appendPixelTypeToFileName); appendPixelTypeToFileName: appendPixelTypeToFileName);
return image; return image;
} }
@ -171,9 +171,17 @@ namespace ImageSharp.Tests
var testFile = TestFile.Create(path); var testFile = TestFile.Create(path);
using (var original = Image.Load<TPixel>(testFile.Bytes, ReferenceDecoder.Instance)) IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(path);
IImageFormat format = TestEnvironment.GetImageFormat(path);
IImageDecoder defaultDecoder = Configuration.Default.FindDecoder(format);
if (referenceDecoder.GetType() == defaultDecoder.GetType())
{
throw new InvalidOperationException($"Can't use CompareToOriginal(): no actual reference decoder registered for {format.Name}");
}
using (var original = Image.Load<TPixel>(testFile.Bytes, referenceDecoder))
{ {
//original.DebugSave(provider, "__SYSTEMDRAWING__");
comparer.VerifySimilarity(original, image); comparer.VerifySimilarity(original, image);
} }

7
tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs → tests/ImageSharp.Tests/TestUtilities/TestUtils.cs

@ -11,12 +11,13 @@ namespace ImageSharp.Tests
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using ImageSharp.Formats;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
/// <summary> /// <summary>
/// Extension methods for TestUtilities /// Various utility and extension methods.
/// </summary> /// </summary>
public static class TestUtilityExtensions public static class TestUtils
{ {
private static readonly Dictionary<Type, PixelTypes> ClrTypes2PixelTypes = new Dictionary<Type, PixelTypes>(); private static readonly Dictionary<Type, PixelTypes> ClrTypes2PixelTypes = new Dictionary<Type, PixelTypes>();
@ -28,7 +29,7 @@ namespace ImageSharp.Tests
.Except(new[] { PixelTypes.Undefined, PixelTypes.All }) .Except(new[] { PixelTypes.Undefined, PixelTypes.All })
.ToArray(); .ToArray();
static TestUtilityExtensions() static TestUtils()
{ {
// Add Rgba32 Our default. // Add Rgba32 Our default.
Type defaultPixelFormatType = typeof(Rgba32); Type defaultPixelFormatType = typeof(Rgba32);

4
tests/ImageSharp.Tests/TestUtilities/Tests/ReferenceCodecTests.cs

@ -52,7 +52,7 @@ namespace ImageSharp.Tests
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
string path = TestFile.GetInputFileFullPath(TestImages.Png.Splash); string path = TestFile.GetInputFileFullPath(TestImages.Png.Splash);
using (Image<TPixel> image = Image.Load<TPixel>(path, ReferenceDecoder.Instance)) using (Image<TPixel> image = Image.Load<TPixel>(path, SystemDrawingReferenceDecoder.Instance))
{ {
image.DebugSave(dummyProvider); image.DebugSave(dummyProvider);
} }
@ -65,7 +65,7 @@ namespace ImageSharp.Tests
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
provider.Utility.SaveTestOutputFile(image, "png", ReferenceEncoder.Png); provider.Utility.SaveTestOutputFile(image, "png", SystemDrawingReferenceEncoder.Png);
} }
} }
} }

24
tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs

@ -6,8 +6,12 @@
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
namespace ImageSharp.Tests namespace ImageSharp.Tests
{ {
using System;
using System.IO; using System.IO;
using ImageSharp.Formats;
using ImageSharp.Tests.TestUtilities.ReferenceCodecs;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
@ -59,5 +63,25 @@ namespace ImageSharp.Tests
this.Output.WriteLine(expected); this.Output.WriteLine(expected);
Assert.Contains(TestEnvironment.ReferenceOutputDirectoryFullPath, expected); Assert.Contains(TestEnvironment.ReferenceOutputDirectoryFullPath, expected);
} }
[Theory]
[InlineData("lol/foo.png", typeof(SystemDrawingReferenceEncoder))]
[InlineData("lol/Baz.JPG", typeof(JpegEncoder))]
[InlineData("lol/Baz.gif", typeof(GifEncoder))]
public void GetReferenceEncoder_ReturnsCorrectEncoders(string fileName, Type expectedEncoderType)
{
IImageEncoder encoder = TestEnvironment.GetReferenceEncoder(fileName);
Assert.IsType(expectedEncoderType, encoder);
}
[Theory]
[InlineData("lol/foo.png", typeof(SystemDrawingReferenceDecoder))]
[InlineData("lol/Baz.JPG", typeof(JpegDecoder))]
[InlineData("lol/Baz.gif", typeof(GifDecoder))]
public void GetReferenceDecoder_ReturnsCorrectEncoders(string fileName, Type expectedDecoderType)
{
IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(fileName);
Assert.IsType(expectedDecoderType, decoder);
}
} }
} }

4
tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

@ -165,10 +165,10 @@ namespace ImageSharp.Tests
/// <typeparam name="TPixel"></typeparam> /// <typeparam name="TPixel"></typeparam>
/// <param name="factory"></param> /// <param name="factory"></param>
/// <returns></returns> /// <returns></returns>
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory) public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
return factory.CreateImage(3, 3); return new Image<TPixel>(3, 3);
} }
[Theory] [Theory]

10
tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

@ -27,10 +27,10 @@ namespace ImageSharp.Tests
private ITestOutputHelper Output { get; } private ITestOutputHelper Output { get; }
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory) public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Image<TPixel> image = factory.CreateImage(10, 10); var image = new Image<TPixel>(10, 10);
using (PixelAccessor<TPixel> pixels = image.Lock()) using (PixelAccessor<TPixel> pixels = image.Lock())
{ {
@ -38,10 +38,10 @@ namespace ImageSharp.Tests
{ {
for (int j = 0; j < 10; j++) for (int j = 0; j < 10; j++)
{ {
Vector4 v = new Vector4(i, j, 0, 1); var v = new Vector4(i, j, 0, 1);
v /= 10; v /= 10;
TPixel color = default(TPixel); var color = default(TPixel);
color.PackFromVector4(v); color.PackFromVector4(v);
pixels[i, j] = color; pixels[i, j] = color;
@ -147,7 +147,7 @@ namespace ImageSharp.Tests
{ {
KeyValuePair<PixelTypes, Type>[] expanded = PixelTypes.All.ExpandAllTypes().ToArray(); KeyValuePair<PixelTypes, Type>[] expanded = PixelTypes.All.ExpandAllTypes().ToArray();
Assert.True(expanded.Length >= TestUtilityExtensions.GetAllPixelTypes().Length - 2); Assert.True(expanded.Length >= TestUtils.GetAllPixelTypes().Length - 2);
AssertContainsPixelType<Rgba32>(PixelTypes.Rgba32, expanded); AssertContainsPixelType<Rgba32>(PixelTypes.Rgba32, expanded);
AssertContainsPixelType<Rgba32>(PixelTypes.Rgba32, expanded); AssertContainsPixelType<Rgba32>(PixelTypes.Rgba32, expanded);
} }

2
tests/Images/External

@ -1 +1 @@
Subproject commit a91dd59e1cc2abbfa5ff2a8fb5690143343a3434 Subproject commit 5029858a874a7e5127d516f0875b1e9df82d01b6

BIN
tests/Images/Input/Png/SnakeGame.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Loading…
Cancel
Save