Browse Source

extend ProjectiveTransformBuilder

af/merge-core
Anton Firszov 8 years ago
parent
commit
9d71fd381d
  1. 2
      src/ImageSharp/Common/Helpers/ImageMaths.cs
  2. 29
      src/ImageSharp/Processing/AffineTransformBuilder.cs
  3. 38
      src/ImageSharp/Processing/ProjectiveTransformBuilder.cs
  4. 2
      tests/ImageSharp.Tests/Drawing/DrawImageTest.cs
  5. 4
      tests/ImageSharp.Tests/Processing/Transforms/AffineTransformBuilderTests.cs
  6. 8
      tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs
  7. 5
      tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformBuilderTests.cs
  8. 2
      tests/ImageSharp.Tests/Processing/Transforms/TransformBuilderTestBase.cs

2
src/ImageSharp/Common/Helpers/ImageMaths.cs

@ -134,7 +134,7 @@ namespace SixLabors.ImageSharp
/// Converts degrees to radians
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
public static float ToRadian(float degrees)
public static float DegreesToRadians(float degrees)
{
return degrees * ((float)Math.PI / 180f);
}

29
src/ImageSharp/Processing/AffineTransformBuilder.cs

@ -42,37 +42,38 @@ namespace SixLabors.ImageSharp.Processing
/// </summary>
internal Size Size => this.sourceRectangle.Size;
/// <summary>
/// Prepends a centered rotation matrix using the given rotation in degrees.
/// Prepends a centered rotation matrix using the given rotation in radians.
/// </summary>
/// <param name="degrees">The amount of rotation, in degrees.</param>
/// <param name="radians">The amount of rotation, in radians.</param>
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
public AffineTransformBuilder PrependRotationDegrees(float degrees)
=> this.PrependMatrix(TransformUtils.CreateRotationMatrixDegrees(degrees, this.Size));
public AffineTransformBuilder PrependCenteredRotationRadians(float radians)
=> this.PrependMatrix(TransformUtils.CreateRotationMatrixRadians(radians, this.Size));
/// <summary>
/// Prepends a centered rotation matrix using the given rotation in radians.
/// Appends a centered rotation matrix using the given rotation in radians.
/// </summary>
/// <param name="radians">The amount of rotation, in radians.</param>
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
public AffineTransformBuilder PrependRotationRadians(float radians)
=> this.PrependMatrix(TransformUtils.CreateRotationMatrixRadians(radians, this.Size));
public AffineTransformBuilder AppendCenteredRotationRadians(float radians)
=> this.AppendMatrix(TransformUtils.CreateRotationMatrixRadians(radians, this.Size));
/// <summary>
/// Appends a centered rotation matrix using the given rotation in degrees.
/// Prepends a centered rotation matrix using the given rotation in degrees.
/// </summary>
/// <param name="degrees">The amount of rotation, in degrees.</param>
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
public AffineTransformBuilder AppendRotationDegrees(float degrees)
=> this.AppendRotationRadians(ImageMaths.ToRadian(degrees));
public AffineTransformBuilder PrependCenteredRotationDegrees(float degrees)
=> this.PrependCenteredRotationRadians(ImageMaths.DegreesToRadians(degrees));
/// <summary>
/// Appends a centered rotation matrix using the given rotation in radians.
/// Appends a centered rotation matrix using the given rotation in degrees.
/// </summary>
/// <param name="radians">The amount of rotation, in radians.</param>
/// <param name="degrees">The amount of rotation, in degrees.</param>
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
public AffineTransformBuilder AppendRotationRadians(float radians)
=> this.AppendMatrix(TransformUtils.CreateRotationMatrixRadians(radians, this.Size));
public AffineTransformBuilder AppendCenteredRotationDegrees(float degrees)
=> this.AppendCenteredRotationRadians(ImageMaths.DegreesToRadians(degrees));
/// <summary>
/// Prepends a scale matrix from the given uniform scale.

38
src/ImageSharp/Processing/ProjectiveTransformBuilder.cs

@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Processing
Guard.MustBeGreaterThan(sourceRectangle.Height, 0, nameof(sourceRectangle));
this.sourceRectangle = sourceRectangle;
}
}
/// <summary>
/// Gets the source image size.
@ -63,16 +63,44 @@ namespace SixLabors.ImageSharp.Processing
public ProjectiveTransformBuilder AppendTaperMatrix(TaperSide side, TaperCorner corner, float fraction)
=> this.AppendMatrix(TransformUtils.CreateTaperMatrix(this.Size, side, corner, fraction));
public void AppendRotationRadians(float radians)
/// <summary>
/// Prepends a centered rotation matrix using the given rotation in radians.
/// </summary>
/// <param name="radians">The amount of rotation, in radians.</param>
/// <returns>The <see cref="ProjectiveTransformBuilder"/>.</returns>
public ProjectiveTransformBuilder PrependCenteredRotationRadians(float radians)
{
throw new System.NotImplementedException();
var m = new Matrix4x4(TransformUtils.CreateRotationMatrixRadians(radians, this.Size));
return this.PrependMatrix(m);
}
public void PrependRotationRadians(float radians)
/// <summary>
/// Appends a centered rotation matrix using the given rotation in radians.
/// </summary>
/// <param name="radians">The amount of rotation, in radians.</param>
/// <returns>The <see cref="ProjectiveTransformBuilder"/>.</returns>
public ProjectiveTransformBuilder AppendCenteredRotationRadians(float radians)
{
throw new System.NotImplementedException();
var m = new Matrix4x4(TransformUtils.CreateRotationMatrixRadians(radians, this.Size));
return this.AppendMatrix(m);
}
/// <summary>
/// Prepends a centered rotation matrix using the given rotation in degrees.
/// </summary>
/// <param name="degrees">The amount of rotation, in degrees.</param>
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
public ProjectiveTransformBuilder PrependCenteredRotationDegrees(float degrees)
=> this.PrependCenteredRotationRadians(ImageMaths.DegreesToRadians(degrees));
/// <summary>
/// Appends a centered rotation matrix using the given rotation in degrees.
/// </summary>
/// <param name="degrees">The amount of rotation, in degrees.</param>
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
public ProjectiveTransformBuilder AppendCenteredRotationDegrees(float degrees)
=> this.AppendCenteredRotationRadians(ImageMaths.DegreesToRadians(degrees));
/// <summary>
/// Prepends a scale matrix from the given uniform scale.
/// </summary>

2
tests/ImageSharp.Tests/Drawing/DrawImageTest.cs

@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Tests
using (var blend = Image.Load<TPixel>(TestFile.Create(TestImages.Bmp.Car).Bytes))
{
AffineTransformBuilder builder = new AffineTransformBuilder(blend.Size())
.AppendRotationDegrees(45F)
.AppendCenteredRotationDegrees(45F)
.AppendScale(new SizeF(.25F, .25F))
.AppendTranslation(new PointF(10, 10));

4
tests/ImageSharp.Tests/Processing/Transforms/AffineTransformBuilderTests.cs

@ -38,11 +38,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
protected override void AppendTranslation(AffineTransformBuilder builder, PointF translate) => builder.AppendTranslation(translate);
protected override void AppendScale(AffineTransformBuilder builder, SizeF scale) => builder.AppendScale(scale);
protected override void AppendRotationRadians(AffineTransformBuilder builder, float radians) => builder.AppendRotationRadians(radians);
protected override void AppendRotationRadians(AffineTransformBuilder builder, float radians) => builder.AppendCenteredRotationRadians(radians);
protected override void PrependTranslation(AffineTransformBuilder builder, PointF translate) => builder.PrependTranslation(translate);
protected override void PrependScale(AffineTransformBuilder builder, SizeF scale) => builder.PrependScale(scale);
protected override void PrependRotationRadians(AffineTransformBuilder builder, float radians) => builder.PrependRotationRadians(radians);
protected override void PrependRotationRadians(AffineTransformBuilder builder, float radians) => builder.PrependCenteredRotationRadians(radians);
protected override Vector2 Execute(
AffineTransformBuilder builder,

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

@ -80,7 +80,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
using (Image<TPixel> image = provider.GetImage())
{
AffineTransformBuilder builder = new AffineTransformBuilder(image.Size())
.AppendRotationDegrees(30);
.AppendCenteredRotationDegrees(30);
image.Mutate(c => c.Transform(builder, resampler));
image.DebugSave(provider, resamplerName);
@ -102,7 +102,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{
image.DebugSave(provider, $"_original");
AffineTransformBuilder builder = new AffineTransformBuilder(image.Size())
.AppendRotationDegrees(angleDeg)
.AppendCenteredRotationDegrees(angleDeg)
.AppendScale(new SizeF(sx, sy))
.AppendTranslation(new PointF(tx, ty));
@ -124,7 +124,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
using (Image<TPixel> image = provider.GetImage())
{
AffineTransformBuilder builder = new AffineTransformBuilder(image.Size())
.AppendRotationDegrees(angleDeg)
.AppendCenteredRotationDegrees(angleDeg)
.AppendScale(new SizeF(s, s));
image.Mutate(i => i.Transform(builder, KnownResamplers.Bicubic));
@ -197,7 +197,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
using (Image<TPixel> image = provider.GetImage())
{
AffineTransformBuilder builder = new AffineTransformBuilder(image.Size())
.AppendRotationDegrees(50)
.AppendCenteredRotationDegrees(50)
.AppendScale(new SizeF(.6F, .6F));
image.Mutate(i => i.Transform(builder, sampler));

5
tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformBuilderTests.cs

@ -34,13 +34,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
var builder = new ProjectiveTransformBuilder(new Rectangle(Point.Empty, s));
});
}
protected override void AppendTranslation(ProjectiveTransformBuilder builder, PointF translate) => builder.AppendTranslation(translate);
protected override void AppendScale(ProjectiveTransformBuilder builder, SizeF scale) => builder.AppendScale(scale);
protected override void AppendRotationRadians(ProjectiveTransformBuilder builder, float radians) => builder.AppendRotationRadians(radians);
protected override void AppendRotationRadians(ProjectiveTransformBuilder builder, float radians) => builder.AppendCenteredRotationRadians(radians);
protected override void PrependTranslation(ProjectiveTransformBuilder builder, PointF translate) => builder.PrependTranslation(translate);
protected override void PrependScale(ProjectiveTransformBuilder builder, SizeF scale) => builder.PrependScale(scale);
protected override void PrependRotationRadians(ProjectiveTransformBuilder builder, float radians) => builder.PrependRotationRadians(radians);
protected override void PrependRotationRadians(ProjectiveTransformBuilder builder, float radians) => builder.PrependCenteredRotationRadians(radians);
protected override Vector2 Execute(
ProjectiveTransformBuilder builder,

2
tests/ImageSharp.Tests/Processing/Transforms/TransformBuilderTestBase.cs

@ -142,7 +142,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
protected abstract void PrependRotationRadians(TBuilder builder, float radians);
protected virtual void AppendRotationDegrees(TBuilder builder, float degrees) =>
this.AppendRotationRadians(builder, ImageMaths.ToRadian(degrees));
this.AppendRotationRadians(builder, ImageMaths.DegreesToRadians(degrees));
protected abstract Vector2 Execute(TBuilder builder, Rectangle rectangle, Vector2 sourcePoint);

Loading…
Cancel
Save