Browse Source

Merge pull request #387 from icanhasjonas/Core

Optimized Rotate() to use pre calculated rotation matrix

Former-commit-id: f983a6153e9a7f902f37f4c22b52025a0b70e3e4
Former-commit-id: 2d5a0d059c1d9d4387d93194ed487704e0fae6b3
Former-commit-id: ea8ed3c821b933940478b98af68140817c7d9b2e
af/merge-core
James Jackson-South 10 years ago
parent
commit
9320e5d97a
  1. 23
      src/ImageProcessorCore/Numerics/Point.cs
  2. 6
      src/ImageProcessorCore/Samplers/Rotate.cs

23
src/ImageProcessorCore/Numerics/Point.cs

@ -158,6 +158,29 @@ namespace ImageProcessorCore
return new Vector2(this.X, this.Y); return new Vector2(this.X, this.Y);
} }
/// <summary>
/// Rotates a point around a given a rotation matrix.
/// </summary>
/// <param name="point">The point to rotate</param>
/// <param name="rotation">Rotation matrix used</param>
/// <returns></returns>
public static Point Rotate( Point point, Matrix3x2 rotation )
{
return new Point( Vector2.Transform( point.backingVector, rotation ) );
}
/// <summary>
/// Creates a rotation matrix for
/// </summary>
/// <param name="origion">The origin point to rotate around</param>
/// <param name="degrees">Rotation in degrees</param>
/// <returns></returns>
public static Matrix3x2 CreateRotatation( Point origin, float degrees )
{
float radians = (float)ImageMaths.DegreesToRadians( degrees );
return Matrix3x2.CreateRotation( radians, origin.backingVector );
}
/// <summary> /// <summary>
/// Rotates a point around a given origin by the specified angle in degrees. /// Rotates a point around a given origin by the specified angle in degrees.
/// </summary> /// </summary>

6
src/ImageProcessorCore/Samplers/Rotate.cs

@ -3,6 +3,8 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
using System.Numerics;
namespace ImageProcessorCore.Samplers namespace ImageProcessorCore.Samplers
{ {
using System.Threading.Tasks; using System.Threading.Tasks;
@ -62,6 +64,8 @@ namespace ImageProcessorCore.Samplers
float widthFactor = source.Width / (float)target.Width; float widthFactor = source.Width / (float)target.Width;
float heightFactor = source.Height / (float)target.Height; float heightFactor = source.Height / (float)target.Height;
Matrix3x2 rotation = Point.CreateRotatation( centre, negativeAngle );
Parallel.For( Parallel.For(
startY, startY,
endY, endY,
@ -78,7 +82,7 @@ namespace ImageProcessorCore.Samplers
int originX = (int)((x - startX) * widthFactor); int originX = (int)((x - startX) * widthFactor);
// Rotate at the centre point // Rotate at the centre point
Point rotated = Point.Rotate(new Point(originX, originY), centre, negativeAngle); Point rotated = Point.Rotate(new Point(originX, originY), rotation);
if (sourceRectangle.Contains(rotated.X, rotated.Y)) if (sourceRectangle.Contains(rotated.X, rotated.Y))
{ {
target[x, y] = source[rotated.X, rotated.Y]; target[x, y] = source[rotated.X, rotated.Y];

Loading…
Cancel
Save