Browse Source

Begin adding tests.

af/merge-core
James Jackson-South 8 years ago
parent
commit
d023ebc379
  1. 75
      src/ImageSharp/Processing/Transforms/TaperTransform.cs
  2. 41
      tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs
  3. 63
      tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs

75
tests/ImageSharp.Tests/Processing/Transforms/TaperTransform.cs → src/ImageSharp/Processing/Transforms/TaperTransform.cs

@ -1,15 +1,73 @@
using System.Numerics;
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Tests.Processing.Transforms
namespace SixLabors.ImageSharp.Processing.Transforms
{
public enum TaperSide { Left, Top, Right, Bottom }
/// <summary>
/// Enumerates the various options which determine which side to taper
/// </summary>
public enum TaperSide
{
/// <summary>
/// Taper the left side
/// </summary>
Left,
/// <summary>
/// Taper the top side
/// </summary>
Top,
/// <summary>
/// Taper the right side
/// </summary>
Right,
/// <summary>
/// Taper the bottom side
/// </summary>
Bottom
}
public enum TaperCorner { LeftOrTop, RightOrBottom, Both }
/// <summary>
/// Enumerates the various options which determine how to taper corners
/// </summary>
public enum TaperCorner
{
/// <summary>
/// Taper the left or top corner
/// </summary>
LeftOrTop,
/// <summary>
/// Taper the right or bottom corner
/// </summary>
RightOrBottom,
/// <summary>
/// Taper the both sets of corners
/// </summary>
Both
}
/// <summary>
/// Provides methods for the creation of generalized tapering projective transforms.
/// <see href="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/transforms/non-affine"/>
/// </summary>
public static class TaperTransform
{
public static Matrix4x4 Make(Size size, TaperSide taperSide, TaperCorner taperCorner, float taperFraction)
/// <summary>
/// Creates a matrix that performs a tapering projective transform.
/// </summary>
/// <param name="size">The resultant size of the tapered output</param>
/// <param name="taperSide">The taper side option</param>
/// <param name="taperCorner">The taper corner option</param>
/// <param name="taperFraction">The amount to taper</param>
/// <returns>The <see cref="Matrix4x4"/></returns>
public static Matrix4x4 Create(Size size, TaperSide taperSide, TaperCorner taperCorner, float taperFraction)
{
Matrix4x4 matrix = Matrix4x4.Identity;
@ -35,6 +93,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
matrix.M32 = size.Height * (1 - taperFraction) / 2;
break;
}
break;
case TaperSide.Top:
@ -57,6 +116,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
matrix.M31 = size.Width * (1 - taperFraction) / 2;
break;
}
break;
case TaperSide.Right:
@ -76,6 +136,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
matrix.M12 = (size.Height / 2) * matrix.M13;
break;
}
break;
case TaperSide.Bottom:
@ -95,9 +156,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
matrix.M21 = (size.Width / 2) * matrix.M23;
break;
}
break;
}
return matrix;
}
}
}
}

41
tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs

@ -39,25 +39,24 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{ 0, 1f, 2f, 0, 0 },
};
public static readonly TheoryData<string> ResamplerNames =
new TheoryData<string>
{
nameof(KnownResamplers.Bicubic),
nameof(KnownResamplers.Box),
nameof(KnownResamplers.CatmullRom),
nameof(KnownResamplers.Hermite),
nameof(KnownResamplers.Lanczos2),
nameof(KnownResamplers.Lanczos3),
nameof(KnownResamplers.Lanczos5),
nameof(KnownResamplers.Lanczos8),
nameof(KnownResamplers.MitchellNetravali),
nameof(KnownResamplers.NearestNeighbor),
nameof(KnownResamplers.Robidoux),
nameof(KnownResamplers.RobidouxSharp),
nameof(KnownResamplers.Spline),
nameof(KnownResamplers.Triangle),
nameof(KnownResamplers.Welch),
};
public static readonly TheoryData<string> ResamplerNames = new TheoryData<string>
{
nameof(KnownResamplers.Bicubic),
nameof(KnownResamplers.Box),
nameof(KnownResamplers.CatmullRom),
nameof(KnownResamplers.Hermite),
nameof(KnownResamplers.Lanczos2),
nameof(KnownResamplers.Lanczos3),
nameof(KnownResamplers.Lanczos5),
nameof(KnownResamplers.Lanczos8),
nameof(KnownResamplers.MitchellNetravali),
nameof(KnownResamplers.NearestNeighbor),
nameof(KnownResamplers.Robidoux),
nameof(KnownResamplers.RobidouxSharp),
nameof(KnownResamplers.Spline),
nameof(KnownResamplers.Triangle),
nameof(KnownResamplers.Welch),
};
public static readonly TheoryData<string> Transform_DoesNotCreateEdgeArtifacts_ResamplerNames =
new TheoryData<string>
@ -124,7 +123,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails);
}
}
[Theory]
[WithTestPatternImages(96, 96, PixelTypes.Rgba32, 50, 0.8f)]
public void Transform_RotateScale_ManuallyCentered<TPixel>(TestImageProvider<TPixel> provider, float angleDeg, float s)
@ -166,7 +165,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
using (Image<TPixel> image = provider.GetImage())
{
var m = Matrix3x2.CreateScale(2.0F, 1.5F);
image.Mutate(i => i.Transform(m, KnownResamplers.Spline, rectangle));
image.DebugSave(provider);

63
tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs

@ -1,15 +1,76 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using System.Reflection;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Transforms;
using SixLabors.ImageSharp.Processing.Transforms.Resamplers;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using Xunit;
using Xunit.Abstractions;
namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{
public class ProjectiveTransformTests
{
private readonly ITestOutputHelper Output;
// private readonly ITestOutputHelper Output;
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.005f, 3);
public static readonly TheoryData<string> ResamplerNames = new TheoryData<string>
{
nameof(KnownResamplers.Bicubic),
nameof(KnownResamplers.Box),
nameof(KnownResamplers.CatmullRom),
nameof(KnownResamplers.Hermite),
nameof(KnownResamplers.Lanczos2),
nameof(KnownResamplers.Lanczos3),
nameof(KnownResamplers.Lanczos5),
nameof(KnownResamplers.Lanczos8),
nameof(KnownResamplers.MitchellNetravali),
nameof(KnownResamplers.NearestNeighbor),
nameof(KnownResamplers.Robidoux),
nameof(KnownResamplers.RobidouxSharp),
nameof(KnownResamplers.Spline),
nameof(KnownResamplers.Triangle),
nameof(KnownResamplers.Welch),
};
[Theory]
[WithTestPatternImages(nameof(ResamplerNames), 150, 150, PixelTypes.Rgba32)]
public void Transform_WithSampler<TPixel>(TestImageProvider<TPixel> provider, string resamplerName)
where TPixel : struct, IPixel<TPixel>
{
IResampler sampler = GetResampler(resamplerName);
using (Image<TPixel> image = provider.GetImage())
{
Matrix4x4 m = TaperTransform.Create(image.Size(), TaperSide.Right, TaperCorner.Both, .5F);
image.Mutate(i =>
{
i.Transform(m, sampler);
});
image.DebugSave(provider, resamplerName);
// TODO: Enable and add more tests.
// image.CompareToReferenceOutput(ValidatorComparer, provider, resamplerName);
}
}
private static IResampler GetResampler(string name)
{
PropertyInfo property = typeof(KnownResamplers).GetTypeInfo().GetProperty(name);
if (property == null)
{
throw new Exception("Invalid property name!");
}
return (IResampler)property.GetValue(null);
}
}
}

Loading…
Cancel
Save