Browse Source

finalized transform with source rectangle + tests

af/merge-core
Anton Firszov 8 years ago
parent
commit
5c12546100
  1. 11
      src/ImageSharp/Processing/Transforms/Transform.cs
  2. 52
      tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs
  3. 2
      tests/Images/External

11
src/ImageSharp/Processing/Transforms/Transform.cs

@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp
/// <returns>The <see cref="Image{TPixel}"/></returns> /// <returns>The <see cref="Image{TPixel}"/></returns>
public static IImageProcessingContext<TPixel> Transform<TPixel>(this IImageProcessingContext<TPixel> source, Matrix3x2 matrix, IResampler sampler) public static IImageProcessingContext<TPixel> Transform<TPixel>(this IImageProcessingContext<TPixel> source, Matrix3x2 matrix, IResampler sampler)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
=> Transform(source, matrix, sampler, Rectangle.Empty); => Transform(source, matrix, sampler, Size.Empty);
/// <summary> /// <summary>
/// Transforms an image by the given matrix using the specified sampling algorithm. /// Transforms an image by the given matrix using the specified sampling algorithm.
@ -44,17 +44,18 @@ namespace SixLabors.ImageSharp
/// <param name="source">The image to transform.</param> /// <param name="source">The image to transform.</param>
/// <param name="matrix">The transformation matrix.</param> /// <param name="matrix">The transformation matrix.</param>
/// <param name="sampler">The <see cref="IResampler"/> to perform the resampling.</param> /// <param name="sampler">The <see cref="IResampler"/> to perform the resampling.</param>
/// <param name="rectangle">The rectangle to constrain the transformed image to.</param> /// <param name="sourceRectangle">The rectangle defining the source pixel area to transform. 'sourceRectangle.Location' becomes the origo of the transformed image.</param>
/// <returns>The <see cref="Image{TPixel}"/></returns> /// <returns>The <see cref="Image{TPixel}"/></returns>
public static IImageProcessingContext<TPixel> Transform<TPixel>( public static IImageProcessingContext<TPixel> Transform<TPixel>(
this IImageProcessingContext<TPixel> source, this IImageProcessingContext<TPixel> source,
Matrix3x2 matrix, Matrix3x2 matrix,
IResampler sampler, IResampler sampler,
Rectangle rectangle) Rectangle sourceRectangle)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
// TODO: Fixme! var t = Matrix3x2.CreateTranslation(-sourceRectangle.Location);
return source.ApplyProcessor(new AffineTransformProcessor<TPixel>(matrix, sampler, rectangle.Size)); Matrix3x2 combinedMatrix = t * matrix;
return source.ApplyProcessor(new AffineTransformProcessor<TPixel>(combinedMatrix, sampler, sourceRectangle.Size));
} }
/// <summary> /// <summary>

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

@ -143,40 +143,66 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{ 0, 0, 5, 10 }, { 0, 0, 5, 10 },
{ 0, 0, 10, 5 }, { 0, 0, 10, 5 },
{ 5, 0, 5, 10 }, { 5, 0, 5, 10 },
{-5,-5, 15, 15 } {-5,-5, 20, 20 }
}; };
/// <summary>
/// Testing transforms using custom source rectangles:
/// https://github.com/SixLabors/ImageSharp/pull/386#issuecomment-357104963
/// </summary>
[Theory]
[WithTestPatternImages(96, 48, PixelTypes.Rgba32)]
public void Transform_FromSourceRectangle1<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
var rectangle = new Rectangle(48, 0, 96, 36);
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);
image.CompareToReferenceOutput(provider);
}
}
[Theory] [Theory]
[WithSolidFilledImages(nameof(Transform_IntoRectangle_Data), 10, 10, nameof(Rgba32.Red), PixelTypes.Rgba32)] [WithTestPatternImages(96, 48, PixelTypes.Rgba32)]
public void Transform_IntoRectangle<TPixel>(TestImageProvider<TPixel> provider, int x0, int y0, int w, int h) public void Transform_FromSourceRectangle2<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
var rectangle = new Rectangle(x0, y0, w, h); var rectangle = new Rectangle(0, 24, 48, 48);
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
Matrix3x2 m = this.MakeManuallyCenteredMatrix(45, 0.8f, image); var m = Matrix3x2.CreateScale(1.0F, 2.0F);
image.Mutate(i => i.Transform(m, KnownResamplers.Spline, rectangle)); image.Mutate(i => i.Transform(m, KnownResamplers.Spline, rectangle));
string testDetails = $"({x0},{y0}-W{w},H{h})";
image.DebugSave(provider, testDetails); image.DebugSave(provider);
image.CompareToReferenceOutput(provider);
} }
} }
[Theory] [Theory]
[WithTestPatternImages(nameof(ResamplerNames), 100, 200, PixelTypes.Rgba32)] [WithTestPatternImages(nameof(ResamplerNames), 150, 150, PixelTypes.Rgba32)]
public void Transform_WithSampler<TPixel>(TestImageProvider<TPixel> provider, string resamplerName) public void Transform_WithSampler<TPixel>(TestImageProvider<TPixel> provider, string resamplerName)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
IResampler sampler = GetResampler(resamplerName); IResampler sampler = GetResampler(resamplerName);
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
Matrix3x2 rotate = Matrix3x2Extensions.CreateRotationDegrees(50); Matrix3x2 m = this.MakeManuallyCenteredMatrix(50, 0.6f, image);
Matrix3x2 scale = Matrix3x2Extensions.CreateScale(new SizeF(.5F, .5F));
var translate = Matrix3x2.CreateTranslation(75, 0); image.Mutate(i =>
{
image.Mutate(i => i.Transform(rotate * scale * translate, sampler)); i.Transform(m, sampler);
});
image.DebugSave(provider, resamplerName); image.DebugSave(provider, resamplerName);
image.CompareToReferenceOutput(provider, resamplerName);
} }
} }

2
tests/Images/External

@ -1 +1 @@
Subproject commit 9d4070dab9e4aefa70dcd41ef98176a5d54e644f Subproject commit 02687e772f84b1d518ab83eacdfafba476f824e6
Loading…
Cancel
Save